-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
365 lines (363 loc) · 13 KB
/
index.js
File metadata and controls
365 lines (363 loc) · 13 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
(() => {
// extract values
const recuFilter = (tree, copy, valCreator) => {
const recu = (tree, copy) => {
if (typeof tree === 'object') {
for (let k of Object.keys(tree)) {
if (['__proto__', 'constructor', 'prototype'].includes(k)) continue;
copy[k] = {};
if (tree[k] instanceof Function || typeof tree[k] === "function") {
copy[k] = valCreator(tree[k]);
} else {
recu(tree[k], copy[k]);
if (Object.keys(copy[k]).length === 0) delete copy[k];
}
}
}
};
recu(tree, copy);
};
// replace values
const recuPatch = (tree, target, patchFn) => {
const recu = (tree, target) => {
for (let k of Object.keys(tree)) {
if (['__proto__', 'constructor', 'prototype'].includes(k)) continue;
if (typeof tree[k] === 'object') {
recu(tree[k], target[k]);
} else {
patchFn(tree, target, k);
}
}
};
recu(tree, target);
};
// copy values from args
const recuCopy = (tree, mutable) => {
const copy = [];
const recu = (tree, mutable, copy) => {
for (let k of Object.keys(tree)) {
if (['__proto__', 'constructor', 'prototype'].includes(k)) continue;
if (typeof tree[k] === 'object') {
if (k in mutable) {
if (Array.isArray(tree[k])) copy[k] = [];else copy[k] = {};
recu(tree[k], mutable[k], copy[k]);
} else {
copy[k] = tree[k];
}
} else {
copy[k] = tree[k];
}
}
};
recu(tree, mutable, copy);
return copy;
};
/**
* Create new iorpc instance
*
* @param {function} sendFn - Function to send data to the iorpc instance on the other side.
* @param {object} [localApi] - An object containing methods callable from the remote side;
* this is optional if you don't need the remote side to call methods back on this side.
* @param {object} [options] - Configuration options.
* @param {number} [options.maxPendingResponses=10000] - Maximum number of unresolved function calls allowed at a time.
* Prevents memory overuse or flooding; throws error if exceeded.
* @param {boolean} [options.allowNestedFunctions=true] - If true, allows functions to be passed inside arrays or objects.
* They will be automatically serialized and wrapped for remote calls.
* @param {boolean} [options.exposeErrors=true] - If true, remote errors (including stack traces) will be forwarded.
* If false, remote errors will be replaced with a generic message.
* @param {boolean} [options.injectToThis=true] - If true, 'this' inside a function will be replaced with the iorpc object.
* Useful for context-aware APIs.
*
* @returns {{ remoteApi: object, routeInput: function, pending: function }} - Returns an object with:
* - `remoteApi`: proxy API to call remote functions,
* - `routeInput`: function to pass incoming messages from the other side,
* - `pending`: returns the size of the waiting list to check if it is overflowing.
*/
const createIorpc = (sendFn, localApi = {}, {
maxPendingResponses = 10000,
allowNestedFunctions = false,
exposeErrors = true,
injectToThis = true,
ignoreCallbackUnavailable = false
}) => {
const clbs = {};
let trimWarningFired = false;
let noWait = false;
let pending = 0;
const clbsTrim = () => {
if (!trimWarningFired) {
console.warn(`maxPendingResponses > ${maxPendingResponses}. Check if callback bindings are being released after use. The oldest ones have been removed, this operation requires some extra resources. Now the oldest ones may not work.`);
trimWarningFired = true;
}
const clbsArr = Object.values(clbs);
clbsArr.sort((a, b) => a.lastAck - b.lastAck);
clbsArr.slice(0, pending - maxPendingResponses).map(v => v.cbId).forEach(cbId => {
pending--;
delete clbs[cbId];
});
};
const genCbid = () => {
while (true) {
const cbId = Math.round(Math.random() * Number.MAX_SAFE_INTEGER);
if (cbId in clbs) continue;
return cbId;
}
};
/**
* An proxy-object with remoteApi callers
*/
const remoteApi = new Proxy({}, {
get: (_, thisArg) => {
if (thisArg === 'noWait') {
noWait = true;
return remoteApi;
} else {
return function (...args) {
const packet = {
apiFunc: thisArg,
cbId: false,
args,
argsTransform: allowNestedFunctions ? {} : []
};
if (!noWait) packet.cbId = genCbid();
if (allowNestedFunctions) {
recuFilter(args, packet.argsTransform, resolve => {
const newCbId = genCbid();
pending++;
clbs[newCbId] = {
resolve: resolve,
cbId: newCbId,
lastAck: Date.now()
};
return newCbId;
});
packet.args = recuCopy(args, packet.argsTransform); // making the data immutable
recuPatch(packet.argsTransform, packet.args, (at, a, k) => {
a[k] = at[k];
at[k] = 1;
});
} else {
for (let i = 0; i < args.length; i++) {
packet.args[i] = args[i];
if (packet.args[i] instanceof Function || typeof packet.args[i] === "function") {
const newCbId = genCbid();
const resolve = packet.args[i];
pending++;
clbs[newCbId] = {
resolve: resolve,
cbId: newCbId,
lastAck: Date.now()
};
packet.args[i] = newCbId;
packet.argsTransform.push(i);
}
}
}
sendFn(packet);
if (!noWait) {
const delayPromise = new Promise((resolve, reject) => {
if (pending > maxPendingResponses) clbsTrim();
pending++;
clbs[packet.cbId] = {
resolve: arg => {
delete clbs[packet.cbId];
pending--;
return resolve(arg);
},
reject,
cbId: packet.cbId,
lastAck: Date.now()
};
});
return delayPromise.catch(e => {
let err = e;
if (e instanceof Error) {
err = new RemoteError(e.stack, e.message);
}
throw err;
});
}
noWait = false;
};
}
}
});
/**
* Input message handler
* @param message
*/
const routeInput = message => {
if (message.apiFunc === 'iorpcThrowError') {
const [cbId, e, stack] = message.args;
if (stack) {
const err = new Error(e);
err.stack = stack;
clbs[cbId].reject(err);
} else {
clbs[cbId].reject(e);
}
pending--;
delete clbs[cbId];
return;
}
if (message.apiFunc === 'iorpcUnbind') {
pending--;
delete clbs[message.args[0]];
return;
}
let fn;
if (message.apiFunc in localApi) {
fn = localApi[message.apiFunc];
} else {
if (message.apiFunc in clbs) {
clbs[message.apiFunc].lastAck = Date.now();
fn = clbs[message.apiFunc].resolve;
}
}
if (fn) {
if (allowNestedFunctions) {
recuPatch(message.argsTransform, message.args, (at, a, k) => {
const cbId = a[k];
a[k] = function (...args) {
return remoteApi[cbId](...args);
};
a[k].unbind = () => {
noWait = true;
remoteApi.iorpcUnbind(cbId);
};
});
} else {
for (let i of message.argsTransform) {
const cbId = message.args[i];
message.args[i] = function (...args) {
return remoteApi[cbId](...args);
};
message.args[i].unbind = () => {
noWait = true;
remoteApi.iorpcUnbind(cbId);
};
}
}
let retCb;
let suc = true;
try {
if (injectToThis) {
retCb = fn.apply({
remoteApi,
iorpcPending: () => pending
}, message.args);
} else {
retCb = fn(...message.args);
}
} catch (e) {
if (exposeErrors) {
suc = false;
noWait = true;
if (e instanceof Error) {
remoteApi.iorpcThrowError(message.cbId, e.message, e.stack);
} else {
remoteApi.iorpcThrowError(message.cbId, e, false);
}
} else {
throw e;
}
}
if (suc) {
if (message.cbId === false) return;
if (retCb instanceof Function || typeof retCb === "function") {
remoteApi[message.cbId](retCb); // function as return
} else {
Promise.resolve(retCb).then(ret => {
noWait = true;
remoteApi[message.cbId](ret);
});
}
}
} else {
noWait = true;
let errMsg;
if (isNaN(message.apiFunc)) {
errMsg = `Function '${message.apiFunc}' is not registered for the iorpc API. Please verify it is properly defined and exposed.`;
} else {
if (ignoreCallbackUnavailable) return;
errMsg = `Callback '${message.apiFunc}' is unavailable. It might have been removed from the waiting queue (maxPendingResponses overflow) or via unbind().`;
}
const e = new Error(errMsg);
remoteApi.iorpcThrowError(message.cbId, e.message, e.stack);
}
};
return {
remoteApi /* Object with remote functions */,
routeInput /* Function to handle incoming messages */,
pending: () => pending
};
};
class RemoteError extends Error {
constructor(stack = "", message, ...args) {
super("\n" + stack, ...args);
this.name = 'RemoteError';
const stack2 = this.stack;
Object.defineProperties(this, {
message: {
value: message
},
stack: {
value: stack2
}
});
}
}
const iorpc = {
/**
* Creates an RPC interface with support for local and remote method calls,
* and a system for handling pending responses.
*
* @param {Function} on - A function to subscribe to incoming messages. Takes a callback: (data) => void.
* @param {Function} send - A function to send messages: (data: any) => void.
* @param {Object} local - An object containing local methods that can be called remotely.
* @param {Object} [options] - Optional configuration parameters.
* @param {number} [options.maxPendingResponses=10000] - Maximum number of unresolved function calls allowed at a time.
* Prevents memory overuse or flooding; throws an error if the limit is exceeded.
* @param {boolean} [options.allowNestedFunctions=true] - If true, allows functions to be passed inside arrays or objects.
* These functions will be automatically serialized and wrapped for remote calls.
* @param {boolean} [options.exposeErrors=true] - If true, remote errors (including stack traces) will be forwarded.
* If false, remote errors will be replaced with a generic message.
* @param {boolean} [options.injectToThis=true] - If true, the `this` context inside a function will be replaced with the iorpc object.
* Useful for context-aware APIs.
*
* @returns {{
* pending: Function, // A function that returns a promise awaiting a response from the remote side.
* remote: Object, // An object with remote methods that can be called locally.
* local: Object // An object with registered local methods callable remotely.
* }}
*/
pair({
on,
send,
local = {},
options = {}
}) {
const {
routeInput,
remoteApi,
pending
} = createIorpc(send, local, options);
on(routeInput);
return {
local,
remote: remoteApi,
pending
};
}
};
if (typeof exports !== 'undefined') {
module.exports = iorpc;
module.exports.default = iorpc;
} else if (typeof define === 'function') define(() => {
return iorpc;
});else {
const _self = typeof self !== 'undefined' ? self : this;
if (typeof _self === 'object') _self.iorpc = iorpc; // browser export
}
return iorpc;
})();