js-rpc2 2.0.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.
Files changed (3) hide show
  1. package/package.json +1 -1
  2. package/src/lib.js +11 -3
  3. package/src/types.ts +48 -48
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-rpc2",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "js web websocket http rpc",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/lib.js CHANGED
@@ -364,16 +364,24 @@ export const RPC_DATA_ARG_TYPE_FUNCTION = 0x7ff45f
364
364
  * @param {RPC_DATA} box
365
365
  */
366
366
  export function buildRpcData(box) {
367
- return Uint8Array.from(packr.pack(box))
367
+ let type = box.type
368
+ let data = box.data
369
+ if (type == RPC_TYPE_CALL || type == RPC_TYPE_CALLBACK) {
370
+ data = data.map(({ type, data }) => [type, data])
371
+ }
372
+ return Uint8Array.from(packr.pack([box.id, type, data]))
368
373
  }
369
374
 
370
375
  /**
371
376
  * @param {Uint8Array} buffer
372
377
  */
373
378
  export function parseRpcData(buffer) {
379
+ let [id, type, data] = packr.unpack(buffer)
380
+ if (type == RPC_TYPE_CALL || type == RPC_TYPE_CALLBACK) {
381
+ data = data.map(([type, data]) => ({ type, data }))
382
+ }
374
383
  /** @type{RPC_DATA} */
375
- let box = packr.unpack(buffer)
376
- return box
384
+ return { id, type, data }
377
385
  }
378
386
 
379
387
  /**
package/src/types.ts CHANGED
@@ -1,49 +1,49 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
-
3
- export type ExtensionApi<T> = { asyncLocalStorage: AsyncLocalStorage<T> } & object;
4
-
5
-
6
- export type RPC_TYPE_CALL = 0xdf68f4cb
7
- export type RPC_TYPE_RETURN = 0x68b17581
8
- export type RPC_TYPE_CALLBACK = 0x8d65e5cc
9
- export type RPC_TYPE_ERROR = 0xa07c0f84
10
-
11
- export type RPC_DATA_ARG_TYPE_OTHERS = 0xa7f68c
12
- export type RPC_DATA_ARG_TYPE_FUNCTION = 0x7ff45f
13
-
14
-
15
- export type RPC_TYPES = RPC_TYPE_CALL |
16
- RPC_TYPE_RETURN |
17
- RPC_TYPE_CALLBACK |
18
- RPC_TYPE_ERROR;
19
-
20
-
21
- export type PromiseResolvers = {
22
- promise: Promise<any>;
23
- resolve: (value: any | null) => void;
24
- reject: (reason: any | null) => void;
25
- };
26
-
27
- export type CALLBACK_ITEM = {
28
- id: number;
29
- type: RPC_TYPES;
30
- promise?: PromiseResolvers;
31
- callback?: (...data: object[]) => void;
32
- };
33
-
34
- export type RPC_DATA_ARG_ITEM = { type: RPC_DATA_ARG_TYPE; data: object; };
35
- export type RPC_DATA = {
36
- id: number;
37
- type: RPC_TYPE_CALL | RPC_TYPE_CALLBACK;
38
- data: { type: RPC_DATA_ARG_TYPE; data: any; }[];
39
- } | {
40
- id: number;
41
- type: RPC_TYPE_RETURN;
42
- data: any;
43
- } | {
44
- id: number;
45
- type: RPC_TYPE_ERROR;
46
- data: { message: string, stack: string };
47
- };
48
-
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+
3
+ export type ExtensionApi<T> = { asyncLocalStorage: AsyncLocalStorage<T> } & object;
4
+
5
+
6
+ export type RPC_TYPE_CALL = 0xdf68f4cb
7
+ export type RPC_TYPE_RETURN = 0x68b17581
8
+ export type RPC_TYPE_CALLBACK = 0x8d65e5cc
9
+ export type RPC_TYPE_ERROR = 0xa07c0f84
10
+
11
+ export type RPC_DATA_ARG_TYPE_OTHERS = 0xa7f68c
12
+ export type RPC_DATA_ARG_TYPE_FUNCTION = 0x7ff45f
13
+
14
+
15
+ export type RPC_TYPES = RPC_TYPE_CALL |
16
+ RPC_TYPE_RETURN |
17
+ RPC_TYPE_CALLBACK |
18
+ RPC_TYPE_ERROR;
19
+
20
+
21
+ export type PromiseResolvers = {
22
+ promise: Promise<any>;
23
+ resolve: (value: any | null) => void;
24
+ reject: (reason: any | null) => void;
25
+ };
26
+
27
+ export type CALLBACK_ITEM = {
28
+ id: number;
29
+ type: RPC_TYPES;
30
+ promise?: PromiseResolvers;
31
+ callback?: (...data: object[]) => void;
32
+ };
33
+
34
+ export type RPC_DATA_ARG_ITEM = { type: RPC_DATA_ARG_TYPE; data: object; };
35
+ export type RPC_DATA = {
36
+ id: number;
37
+ type: RPC_TYPE_CALL | RPC_TYPE_CALLBACK;
38
+ data: { type: RPC_DATA_ARG_TYPE; data: any; }[];
39
+ } | {
40
+ id: number;
41
+ type: RPC_TYPE_RETURN;
42
+ data: any;
43
+ } | {
44
+ id: number;
45
+ type: RPC_TYPE_ERROR;
46
+ data: { message: string, stack: string };
47
+ };
48
+
49
49
  export type RPC_DATA_ARG_TYPE = RPC_DATA_ARG_TYPE_OTHERS | RPC_DATA_ARG_TYPE_FUNCTION;