@lvce-editor/json-rpc 5.1.0 → 5.3.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.js +60 -90
- package/package.json +2 -5
package/dist/index.js
CHANGED
|
@@ -13,59 +13,20 @@ const JsonRpcEvent = {
|
|
|
13
13
|
create: create$4
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
constructor(message) {
|
|
18
|
-
super(message);
|
|
19
|
-
this.name = 'AssertionError';
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
const getType = value => {
|
|
23
|
-
switch (typeof value) {
|
|
24
|
-
case 'number':
|
|
25
|
-
return 'number';
|
|
26
|
-
case 'function':
|
|
27
|
-
return 'function';
|
|
28
|
-
case 'string':
|
|
29
|
-
return 'string';
|
|
30
|
-
case 'object':
|
|
31
|
-
if (value === null) {
|
|
32
|
-
return 'null';
|
|
33
|
-
}
|
|
34
|
-
if (Array.isArray(value)) {
|
|
35
|
-
return 'array';
|
|
36
|
-
}
|
|
37
|
-
return 'object';
|
|
38
|
-
case 'boolean':
|
|
39
|
-
return 'boolean';
|
|
40
|
-
default:
|
|
41
|
-
return 'unknown';
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
const number = value => {
|
|
45
|
-
const type = getType(value);
|
|
46
|
-
if (type !== 'number') {
|
|
47
|
-
throw new AssertionError('expected value to be of type number');
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const state$1 = {
|
|
52
|
-
callbacks: Object.create(null)
|
|
53
|
-
};
|
|
16
|
+
const callbacks = Object.create(null);
|
|
54
17
|
const set = (id, fn) => {
|
|
55
|
-
|
|
18
|
+
callbacks[id] = fn;
|
|
56
19
|
};
|
|
57
20
|
const get = id => {
|
|
58
|
-
return
|
|
21
|
+
return callbacks[id];
|
|
59
22
|
};
|
|
60
23
|
const remove = id => {
|
|
61
|
-
delete
|
|
24
|
+
delete callbacks[id];
|
|
62
25
|
};
|
|
63
26
|
|
|
64
|
-
|
|
65
|
-
id: 0
|
|
66
|
-
};
|
|
27
|
+
let id = 0;
|
|
67
28
|
const create$3 = () => {
|
|
68
|
-
return ++
|
|
29
|
+
return ++id;
|
|
69
30
|
};
|
|
70
31
|
|
|
71
32
|
const warn = (...args) => {
|
|
@@ -84,15 +45,14 @@ const registerPromise = () => {
|
|
|
84
45
|
promise
|
|
85
46
|
};
|
|
86
47
|
};
|
|
87
|
-
const resolve = (id,
|
|
88
|
-
number(id);
|
|
48
|
+
const resolve = (id, response) => {
|
|
89
49
|
const fn = get(id);
|
|
90
50
|
if (!fn) {
|
|
91
|
-
console.log(
|
|
51
|
+
console.log(response);
|
|
92
52
|
warn(`callback ${id} may already be disposed`);
|
|
93
53
|
return;
|
|
94
54
|
}
|
|
95
|
-
fn(
|
|
55
|
+
fn(response);
|
|
96
56
|
remove(id);
|
|
97
57
|
};
|
|
98
58
|
|
|
@@ -341,32 +301,43 @@ const defaultRequiresSocket = () => {
|
|
|
341
301
|
return false;
|
|
342
302
|
};
|
|
343
303
|
const defaultResolve = resolve;
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
let execute;
|
|
348
|
-
let preparePrettyError;
|
|
349
|
-
let logError;
|
|
350
|
-
let resolve;
|
|
351
|
-
let requiresSocket;
|
|
304
|
+
|
|
305
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
|
306
|
+
const normalizeParams = args => {
|
|
352
307
|
if (args.length === 1) {
|
|
353
|
-
const
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
message = args[1];
|
|
364
|
-
execute = args[2];
|
|
365
|
-
resolve = args[3];
|
|
366
|
-
preparePrettyError = args[4];
|
|
367
|
-
logError = args[5];
|
|
368
|
-
requiresSocket = args[6];
|
|
308
|
+
const options = args[0];
|
|
309
|
+
return {
|
|
310
|
+
ipc: options.ipc,
|
|
311
|
+
message: options.message,
|
|
312
|
+
execute: options.execute,
|
|
313
|
+
resolve: options.resolve || defaultResolve,
|
|
314
|
+
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
|
315
|
+
logError: options.logError || defaultLogError,
|
|
316
|
+
requiresSocket: options.requiresSocket || defaultRequiresSocket
|
|
317
|
+
};
|
|
369
318
|
}
|
|
319
|
+
return {
|
|
320
|
+
ipc: args[0],
|
|
321
|
+
message: args[1],
|
|
322
|
+
execute: args[2],
|
|
323
|
+
resolve: args[3],
|
|
324
|
+
preparePrettyError: args[4],
|
|
325
|
+
logError: args[5],
|
|
326
|
+
requiresSocket: args[6]
|
|
327
|
+
};
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
331
|
+
const options = normalizeParams(args);
|
|
332
|
+
const {
|
|
333
|
+
message,
|
|
334
|
+
ipc,
|
|
335
|
+
execute,
|
|
336
|
+
resolve,
|
|
337
|
+
preparePrettyError,
|
|
338
|
+
logError,
|
|
339
|
+
requiresSocket
|
|
340
|
+
} = options;
|
|
370
341
|
if ('id' in message) {
|
|
371
342
|
if ('method' in message) {
|
|
372
343
|
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
@@ -388,29 +359,28 @@ const handleJsonRpcMessage = async (...args) => {
|
|
|
388
359
|
throw new JsonRpcError('unexpected message');
|
|
389
360
|
};
|
|
390
361
|
|
|
391
|
-
const
|
|
392
|
-
const message = create$4(method, params);
|
|
393
|
-
transport.send(message);
|
|
394
|
-
};
|
|
395
|
-
const invoke = async (ipc, method, ...params) => {
|
|
362
|
+
const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
|
|
396
363
|
const {
|
|
397
364
|
message,
|
|
398
365
|
promise
|
|
399
366
|
} = create$2(method, params);
|
|
400
|
-
ipc.
|
|
367
|
+
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
368
|
+
ipc.sendAndTransfer(message);
|
|
369
|
+
} else {
|
|
370
|
+
ipc.send(message);
|
|
371
|
+
}
|
|
401
372
|
const responseMessage = await promise;
|
|
402
|
-
|
|
403
|
-
return result;
|
|
373
|
+
return unwrapJsonRpcResult(responseMessage);
|
|
404
374
|
};
|
|
405
|
-
const
|
|
406
|
-
const
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
ipc
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
return
|
|
375
|
+
const send = (transport, method, ...params) => {
|
|
376
|
+
const message = create$4(method, params);
|
|
377
|
+
transport.send(message);
|
|
378
|
+
};
|
|
379
|
+
const invoke = (ipc, method, ...params) => {
|
|
380
|
+
return invokeHelper(ipc, method, params, false);
|
|
381
|
+
};
|
|
382
|
+
const invokeAndTransfer = (ipc, method, ...params) => {
|
|
383
|
+
return invokeHelper(ipc, method, params, true);
|
|
414
384
|
};
|
|
415
385
|
|
|
416
386
|
export { JsonRpcEvent, JsonRpcRequest, getErrorResponse, getSuccessResponse, handleJsonRpcMessage, invoke, invokeAndTransfer, registerPromise, resolve, send };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lvce-editor/json-rpc",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"description": "JsonRpc implementation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -9,8 +9,5 @@
|
|
|
9
9
|
"json-rpc"
|
|
10
10
|
],
|
|
11
11
|
"author": "Lvce Editor",
|
|
12
|
-
"license": "MIT"
|
|
13
|
-
"dependencies": {
|
|
14
|
-
"@lvce-editor/assert": "^1.3.0"
|
|
15
|
-
}
|
|
12
|
+
"license": "MIT"
|
|
16
13
|
}
|