-
Notifications
You must be signed in to change notification settings - Fork 237
/
iframe-rpc.ts
234 lines (202 loc) · 7.12 KB
/
iframe-rpc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import EventEmitter from 'events';
import {IFrameRPCError} from './iframe-rpc-error';
import {
windowReceiver,
IMessageEvent,
IMessagePoster,
IMessageReceiver,
IRPCMethod,
IRPCResponse,
isRPCMessage,
RPCMessage,
} from './types';
function responseObjToError(obj: { code: number; message: string; }) {
return new IFrameRPCError(obj.message, obj.code);
}
export interface IRPCOptions {
target: IMessagePoster;
requesterId: string;
origin?: string;
protocolVersion?: string;
receiver?: IMessageReceiver;
}
const readyId = -1;
export class IFrameRPC extends EventEmitter {
public readonly isReady: Promise<void>;
private calls: {
[id: number]: (err: null | IFrameRPCError, result: any) => void;
} = Object.create(null);
private lastCallId = 0;
private remoteProtocolVersion: string | undefined;
private readonly removeMessageListener: () => void;
/**
* Creates an instance of IFrameRPC.
* @param options The configuration options for IFrameRPC.
*/
constructor(private readonly options: IRPCOptions) {
super();
this.removeMessageListener = (options.receiver || windowReceiver).readMessages(this.messageEventListener);
this.isReady = this.createReadyPromise();
}
private createReadyPromise() {
return new Promise<void>(resolve => {
const response = {protocolVersion: this.options.protocolVersion || '1.0'};
this.bindMethodHandler('ready', () => {
resolve();
return response;
});
this.callMethod<void>('ready', response)
.then(resolve)
.catch(resolve);
});
}
/**
* Static method to get a ready instance of IFrameRPC based on the provided options.
* @param options The configuration options for IFrameRPC.
* @returns A Promise that resolves to the ready IFrameRPC instance.
*/
static getReadyInstance(options: IRPCOptions): Promise<IFrameRPC> {
const rpc = new IFrameRPC(options);
return rpc.isReady.then(() => rpc);
}
/**
* Binds a method handler for the specified RPC method.
* @param method The RPC method name.
* @param handler The method handler function.
* @returns The current IFrameRPC instance.
*/
public bindMethodHandler<T>(method: string, handler: (params: T) => Promise<any> | any): this {
this.on(method, (data: IRPCMethod<T>) => {
new Promise(resolve => resolve(handler(data.params)))
.then((result) => ({
type: 'response',
requesterId: this.options.requesterId,
id: data.id,
result,
} as IRPCResponse<any>))
.catch((err: Error) => ({
type: 'response',
requesterId: this.options.requesterId,
id: data.id,
error:
err instanceof IFrameRPCError
? err.toResponseError()
: {code: 0, message: err.stack || err.message},
} as IRPCResponse<any>))
.then(message => {
this.emit('sendResponse', message);
this.post(message);
});
});
return this;
}
/**
* Calls an RPC method with the specified method name and parameters.
* @param method The RPC method name.
* @param params The parameters for the RPC method.
* @returns A Promise that resolves with the result of the RPC method.
*/
public callMethod<T>(method: string, params: object): Promise<T> {
const id = method === 'ready' ? readyId : this.lastCallId;
const message: IRPCMethod<any> = {
type: 'method',
requesterId: this.options.requesterId,
id,
params,
method,
};
this.emit('sendMethod', message);
this.post(message);
return new Promise((resolve, reject) => {
this.calls[id] = (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
};
});
}
/**
* Destroys the IFrameRPC instance, removing event listeners.
*/
public destroy() {
this.emit('destroy');
this.removeMessageListener();
}
/**
* Retrieves the remote protocol version.
* @returns The remote protocol version.
*/
public remoteVersion(): string | undefined {
return this.remoteProtocolVersion;
}
private handleResponse(message: IRPCResponse<any>) {
const handler = this.calls[message.id];
if (!handler) {
return;
}
if (message.error) {
handler(responseObjToError(message.error), null);
} else {
handler(null, message.result);
}
delete this.calls[message.id];
}
private post<T>(message: RPCMessage<T>) {
this.options.target.postMessage(JSON.stringify(message), this.options.origin || '*');
}
static isReadySignal(message: RPCMessage<any>) {
if (message.type === 'method' && message.method === 'ready') {
return true;
}
return message.type === 'response' && message.id === readyId;
}
private messageEventListener = (ev: IMessageEvent) => {
if (this.options.origin && this.options.origin !== '*' && ev.origin !== this.options.origin) {
return;
}
let message: RPCMessage<any>;
try {
message = JSON.parse(ev.data);
} catch (e) {
return;
}
if (!isRPCMessage(message) || message.requesterId !== this.options.requesterId) {
return;
}
if (IFrameRPC.isReadySignal(message)) {
const params: { protocolVersion: string } | undefined =
message.type === 'method' ? message.params : message.result;
this.remoteProtocolVersion = params?.protocolVersion ?? this.remoteProtocolVersion;
this.emit('isReady', true);
return;
}
this.emit('dataReceived', message);
this.handleMessage(message);
};
private handleMessage(message: RPCMessage<any>) {
switch (message.type) {
case 'method':
this.emit('methodReceived', message);
if (this.listeners(message.method).length > 0) {
this.emit(message.method, message);
return;
}
this.post({
type: 'response',
requesterId: this.options.requesterId,
id: message.id,
error: {code: 4003, message: `Unknown method name "${message.method}"`},
result: null,
});
break;
case 'response':
this.emit('responseReceived', message);
this.handleResponse(message);
break;
default:
// Ignore
}
}
}