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