@lvce-editor/json-rpc 1.4.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +37 -15
- package/dist/index.js +127 -13
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,24 +6,46 @@ export const invoke: (
|
|
|
6
6
|
...params: any[]
|
|
7
7
|
) => Promise<any>
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
ipc: any,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
interface InvokeAndTransfer {
|
|
10
|
+
(ipc: any, transfer: any, method: string, ...params: any[]): Promise<any>
|
|
11
|
+
(ipc: any, method: string, ...params: any[]): Promise<any>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const invokeAndTransfer: InvokeAndTransfer
|
|
15
15
|
|
|
16
16
|
export const resolve: (id: number, message: any) => void
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
) =>
|
|
18
|
+
interface HandleJsonRpcMessage {
|
|
19
|
+
(
|
|
20
|
+
ipc: any,
|
|
21
|
+
message: any,
|
|
22
|
+
execute: (method: string, ...params: any[]) => Promise<any>,
|
|
23
|
+
resolve: any,
|
|
24
|
+
preparePrettyError: (error: any) => any,
|
|
25
|
+
logError: (error: any) => void,
|
|
26
|
+
requiresSocket: (method: string) => void,
|
|
27
|
+
): Promise<void>
|
|
28
|
+
|
|
29
|
+
({
|
|
30
|
+
ipc,
|
|
31
|
+
message,
|
|
32
|
+
execute,
|
|
33
|
+
resolve,
|
|
34
|
+
preparePrettyError,
|
|
35
|
+
logError,
|
|
36
|
+
requiresSocket,
|
|
37
|
+
}: {
|
|
38
|
+
ipc: any
|
|
39
|
+
message: any
|
|
40
|
+
execute: (method: string, ...params: any[]) => Promise<any>
|
|
41
|
+
resolve?: any
|
|
42
|
+
preparePrettyError: (error: any) => any
|
|
43
|
+
logError: (error: any) => void
|
|
44
|
+
requiresSocket: (method: string) => void
|
|
45
|
+
}): Promise<void>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const handleJsonRpcMessage: HandleJsonRpcMessage
|
|
27
49
|
|
|
28
50
|
export const getErrorResponse: (
|
|
29
51
|
message: any,
|
package/dist/index.js
CHANGED
|
@@ -150,7 +150,6 @@ const getErrorConstructor = (message, type) => {
|
|
|
150
150
|
if (type) {
|
|
151
151
|
switch (type) {
|
|
152
152
|
case DomException:
|
|
153
|
-
// @ts-ignore
|
|
154
153
|
return DOMException;
|
|
155
154
|
case TypeError$1:
|
|
156
155
|
return TypeError;
|
|
@@ -176,7 +175,6 @@ const getErrorConstructor = (message, type) => {
|
|
|
176
175
|
|
|
177
176
|
const constructError = (message, type, name) => {
|
|
178
177
|
const ErrorConstructor = getErrorConstructor(message, type);
|
|
179
|
-
// @ts-ignore
|
|
180
178
|
if (ErrorConstructor === DOMException && name) {
|
|
181
179
|
return new ErrorConstructor(message, name);
|
|
182
180
|
}
|
|
@@ -194,6 +192,14 @@ const getNewLineIndex = (string, startIndex = undefined) => {
|
|
|
194
192
|
return string.indexOf(NewLine, startIndex);
|
|
195
193
|
};
|
|
196
194
|
|
|
195
|
+
const getParentStack = error => {
|
|
196
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
197
|
+
if (parentStack.startsWith(' at')) {
|
|
198
|
+
parentStack = error.message + NewLine + parentStack;
|
|
199
|
+
}
|
|
200
|
+
return parentStack;
|
|
201
|
+
};
|
|
202
|
+
|
|
197
203
|
const joinLines = lines => {
|
|
198
204
|
return lines.join(NewLine);
|
|
199
205
|
};
|
|
@@ -205,18 +211,11 @@ const splitLines = lines => {
|
|
|
205
211
|
return lines.split(NewLine);
|
|
206
212
|
};
|
|
207
213
|
|
|
208
|
-
const getParentStack = error => {
|
|
209
|
-
let parentStack = error.stack || error.data || error.message || '';
|
|
210
|
-
if (parentStack.startsWith(' at')) {
|
|
211
|
-
parentStack = error.message + NewLine + parentStack;
|
|
212
|
-
}
|
|
213
|
-
return parentStack;
|
|
214
|
-
};
|
|
215
214
|
const restoreJsonRpcError = error => {
|
|
216
215
|
if (error && error instanceof Error) {
|
|
217
216
|
return error;
|
|
218
217
|
}
|
|
219
|
-
const currentStack = joinLines(splitLines(new Error().stack).slice(1));
|
|
218
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
|
|
220
219
|
if (error && error.code && error.code === MethodNotFound) {
|
|
221
220
|
const restoredError = new JsonRpcError(error.message);
|
|
222
221
|
const parentStack = getParentStack(error);
|
|
@@ -245,7 +244,6 @@ const restoreJsonRpcError = error => {
|
|
|
245
244
|
}
|
|
246
245
|
} else {
|
|
247
246
|
if (error.stack) {
|
|
248
|
-
// TODO accessing stack might be slow
|
|
249
247
|
const lowerStack = restoredError.stack || '';
|
|
250
248
|
// @ts-ignore
|
|
251
249
|
const indexNewLine = getNewLineIndex(lowerStack);
|
|
@@ -277,6 +275,78 @@ const unwrapJsonRpcResult = responseMessage => {
|
|
|
277
275
|
throw new JsonRpcError('unexpected response message');
|
|
278
276
|
};
|
|
279
277
|
|
|
278
|
+
const isMessagePort = value => {
|
|
279
|
+
return typeof MessagePort !== 'undefined' && value instanceof MessagePort;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const isInstanceOf = (value, constructorName) => {
|
|
283
|
+
return value?.constructor?.name === constructorName;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const isMessagePortMain = value => {
|
|
287
|
+
return isInstanceOf(value, 'MessagePortMain');
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const isOffscreenCanvas = value => {
|
|
291
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const isSocket = value => {
|
|
295
|
+
return isInstanceOf(value, 'Socket');
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
299
|
+
|
|
300
|
+
const isTransferrable = value => {
|
|
301
|
+
for (const fn of transferrables) {
|
|
302
|
+
if (fn(value)) {
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return false;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const walkValue = (value, transferrables) => {
|
|
310
|
+
if (!value) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (isTransferrable(value)) {
|
|
314
|
+
transferrables.push(value);
|
|
315
|
+
}
|
|
316
|
+
if (Array.isArray(value)) {
|
|
317
|
+
for (const item of value) {
|
|
318
|
+
walkValue(item, transferrables);
|
|
319
|
+
}
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (typeof value === 'object') {
|
|
323
|
+
for (const property of Object.values(value)) {
|
|
324
|
+
walkValue(property, transferrables);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const getTransferrables = value => {
|
|
330
|
+
const transferrables = [];
|
|
331
|
+
walkValue(value, transferrables);
|
|
332
|
+
return transferrables;
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const isSingleTransferrable = value => {
|
|
336
|
+
return isSocket(value);
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
const getTransferrableParams = value => {
|
|
340
|
+
const transferrables = getTransferrables(value);
|
|
341
|
+
if (transferrables.length === 0) {
|
|
342
|
+
return undefined;
|
|
343
|
+
}
|
|
344
|
+
if (isSingleTransferrable(transferrables[0])) {
|
|
345
|
+
return transferrables[0];
|
|
346
|
+
}
|
|
347
|
+
return transferrables;
|
|
348
|
+
};
|
|
349
|
+
|
|
280
350
|
const create$1 = (message, error) => {
|
|
281
351
|
return {
|
|
282
352
|
jsonrpc: Two,
|
|
@@ -335,7 +405,42 @@ const getResponse = async (message, ipc, execute, preparePrettyError, logError,
|
|
|
335
405
|
}
|
|
336
406
|
};
|
|
337
407
|
|
|
338
|
-
const
|
|
408
|
+
const defaultPreparePrettyError = error => {
|
|
409
|
+
return error;
|
|
410
|
+
};
|
|
411
|
+
const defaultLogError = () => {
|
|
412
|
+
// ignore
|
|
413
|
+
};
|
|
414
|
+
const defaultRequiresSocket = () => {
|
|
415
|
+
return false;
|
|
416
|
+
};
|
|
417
|
+
const defaultResolve = resolve;
|
|
418
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
419
|
+
let message;
|
|
420
|
+
let ipc;
|
|
421
|
+
let execute;
|
|
422
|
+
let preparePrettyError;
|
|
423
|
+
let logError;
|
|
424
|
+
let resolve;
|
|
425
|
+
let requiresSocket;
|
|
426
|
+
if (args.length === 1) {
|
|
427
|
+
const arg = args[0];
|
|
428
|
+
message = arg.message;
|
|
429
|
+
ipc = arg.ipc;
|
|
430
|
+
execute = arg.execute;
|
|
431
|
+
preparePrettyError = arg.preparePrettyError || defaultPreparePrettyError;
|
|
432
|
+
logError = arg.logError || defaultLogError;
|
|
433
|
+
requiresSocket = arg.requiresSocket || defaultRequiresSocket;
|
|
434
|
+
resolve = arg.resolve || defaultResolve;
|
|
435
|
+
} else {
|
|
436
|
+
ipc = args[0];
|
|
437
|
+
message = args[1];
|
|
438
|
+
execute = args[2];
|
|
439
|
+
resolve = args[3];
|
|
440
|
+
preparePrettyError = args[4];
|
|
441
|
+
logError = args[5];
|
|
442
|
+
requiresSocket = args[6];
|
|
443
|
+
}
|
|
339
444
|
if ('id' in message) {
|
|
340
445
|
if ('method' in message) {
|
|
341
446
|
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
@@ -371,12 +476,21 @@ const invoke = async (ipc, method, ...params) => {
|
|
|
371
476
|
const result = unwrapJsonRpcResult(responseMessage);
|
|
372
477
|
return result;
|
|
373
478
|
};
|
|
479
|
+
|
|
480
|
+
// TODO deprecated old typings,
|
|
481
|
+
// always use automatic transferrable detection
|
|
374
482
|
const invokeAndTransfer = async (ipc, handle, method, ...params) => {
|
|
483
|
+
let transfer = handle;
|
|
484
|
+
if (typeof handle === 'string') {
|
|
485
|
+
params = [method, ...params];
|
|
486
|
+
method = handle;
|
|
487
|
+
transfer = getTransferrableParams(params);
|
|
488
|
+
}
|
|
375
489
|
const {
|
|
376
490
|
message,
|
|
377
491
|
promise
|
|
378
492
|
} = create$2(method, params);
|
|
379
|
-
ipc.sendAndTransfer(message,
|
|
493
|
+
ipc.sendAndTransfer(message, transfer);
|
|
380
494
|
const responseMessage = await promise;
|
|
381
495
|
const result = unwrapJsonRpcResult(responseMessage);
|
|
382
496
|
return result;
|