kkrpc 0.2.0 → 0.2.2
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/README.md +46 -4
- package/dist/browser-mod.cjs +36 -29
- package/dist/browser-mod.d.cts +4 -4
- package/dist/browser-mod.d.ts +4 -4
- package/dist/browser-mod.js +3 -7
- package/dist/{channel-D6ZClufP.d.cts → channel-C01VCxab.d.cts} +35 -1
- package/dist/{channel-D6ZClufP.d.ts → channel-C01VCxab.d.ts} +35 -1
- package/dist/chrome.cjs +36 -29
- package/dist/chrome.d.cts +3 -42
- package/dist/chrome.d.ts +3 -42
- package/dist/chrome.js +3 -7
- package/dist/chunk-GRCUBSPR.js +27 -0
- package/dist/{chunk-ZSSFWNSX.js → chunk-YIQVRWAJ.js} +34 -25
- package/dist/deno-mod.cjs +380 -0
- package/dist/deno-mod.d.cts +19 -0
- package/dist/deno-mod.d.ts +19 -0
- package/dist/deno-mod.js +10 -0
- package/dist/{http-CvGfNM3D.d.cts → http-D0k1TiAJ.d.cts} +1 -1
- package/dist/{http-Bz7mwStC.d.ts → http-D6N0U5-p.d.ts} +1 -1
- package/dist/http.cjs +34 -9
- package/dist/http.d.cts +2 -2
- package/dist/http.d.ts +2 -2
- package/dist/http.js +1 -1
- package/dist/mod.cjs +36 -29
- package/dist/mod.d.cts +7 -20
- package/dist/mod.d.ts +7 -20
- package/dist/mod.js +6 -31
- package/dist/{tauri-CRNRbu3f.d.cts → tauri-ohph68oo.d.cts} +1 -1
- package/dist/{tauri-PdcZTVUI.d.ts → tauri-pC0wuvjw.d.ts} +1 -1
- package/package.json +10 -1
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# kkrpc
|
|
2
2
|
|
|
3
|
+
> This project is created for building extension system for a Tauri app (https://github.com/kunkunsh/kunkun).
|
|
4
|
+
>
|
|
5
|
+
> It's potential can be used in other types of apps, so I open sourced it as a standalone package.
|
|
6
|
+
|
|
3
7
|
[](https://www.npmjs.com/package/kkrpc)
|
|
4
8
|
[](https://jsr.io/@kunkun/kkrpc)
|
|
5
9
|

|
|
@@ -12,11 +16,12 @@
|
|
|
12
16
|
- [Documentation by JSR](https://jsr.io/@kunkun/kkrpc/doc)
|
|
13
17
|
- [Typedoc Documentation](https://kunkunsh.github.io/kkrpc/)
|
|
14
18
|
|
|
15
|
-
[Excalidraw Diagrams](https://excalidraw.com/#json=
|
|
19
|
+
[Excalidraw Diagrams](https://excalidraw.com/#json=xp6GbAJVAx3nU-h3PhaxW,oYBNvYmCRsQ2XR3MQo73Ug)
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
21
|
+
<img src="https://imgur.com/vR3Lmv0.png" style="max-height: 200px;"/>
|
|
22
|
+
<img src="https://i.imgur.com/zmOHNfu.png" style="max-height: 250px;"/>
|
|
23
|
+
<img src="https://imgur.com/u728aVv.png" style="max-height: 400px;"/>
|
|
24
|
+
<img src="https://i.imgur.com/Gu7jH1v.png" style="max-height: 300px;"/>
|
|
20
25
|
|
|
21
26
|
## Supported Environments
|
|
22
27
|
|
|
@@ -53,6 +58,34 @@ class RPCChannel<
|
|
|
53
58
|
> {}
|
|
54
59
|
```
|
|
55
60
|
|
|
61
|
+
## Serialization
|
|
62
|
+
|
|
63
|
+
kkrpc supports two serialization formats for message transmission:
|
|
64
|
+
|
|
65
|
+
- `json`: Standard JSON serialization
|
|
66
|
+
- `superjson`: Enhanced JSON serialization with support for more data types like Date, Map, Set, BigInt, and Uint8Array (default since v0.2.0)
|
|
67
|
+
|
|
68
|
+
You can specify the serialization format when creating a new RPCChannel:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
// Using default serialization (superjson)
|
|
72
|
+
const rpc = new RPCChannel(io, { expose: apiImplementation })
|
|
73
|
+
|
|
74
|
+
// Explicitly using superjson serialization (recommended for clarity)
|
|
75
|
+
const rpc = new RPCChannel(io, {
|
|
76
|
+
expose: apiImplementation,
|
|
77
|
+
serialization: { version: "superjson" }
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
// Using standard JSON serialization (for backward compatibility)
|
|
81
|
+
const rpc = new RPCChannel(io, {
|
|
82
|
+
expose: apiImplementation,
|
|
83
|
+
serialization: { version: "json" }
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
For backward compatibility, the receiving side will automatically detect the serialization format so older clients can communicate with newer servers and vice versa.
|
|
88
|
+
|
|
56
89
|
## Examples
|
|
57
90
|
|
|
58
91
|
Below are simple examples.
|
|
@@ -225,6 +258,10 @@ console.log(data) // { message: "Hello from background!" }
|
|
|
225
258
|
|
|
226
259
|
Call functions in bun/node/deno processes from Tauri app with JS/TS.
|
|
227
260
|
|
|
261
|
+
It allows you to call any JS/TS code in Deno/Bun/Node processes from Tauri app, just like using Electron.
|
|
262
|
+
|
|
263
|
+
Seamless integration with Tauri's official shell plugin and [unlocked shellx plugin](https://github.com/HuakunShen/tauri-plugin-shellx).
|
|
264
|
+
|
|
228
265
|
```ts
|
|
229
266
|
import { RPCChannel, TauriShellStdio } from "kkrpc/browser"
|
|
230
267
|
import { Child, Command } from "@tauri-apps/plugin-shell"
|
|
@@ -250,6 +287,7 @@ async function spawnCmd(runtime: "deno" | "bun" | "node") {
|
|
|
250
287
|
throw new Error(`Invalid runtime: ${runtime}, pick either deno or bun`)
|
|
251
288
|
}
|
|
252
289
|
|
|
290
|
+
// monitor stdout/stderr/close/error for debugging and error handling
|
|
253
291
|
cmd.stdout.on("data", (data) => {
|
|
254
292
|
console.log("stdout", data)
|
|
255
293
|
})
|
|
@@ -281,3 +319,7 @@ async function spawnCmd(runtime: "deno" | "bun" | "node") {
|
|
|
281
319
|
process?.kill()
|
|
282
320
|
}
|
|
283
321
|
```
|
|
322
|
+
|
|
323
|
+
I provided a sample tauri app in `examples/tauri-demo`.
|
|
324
|
+
|
|
325
|
+

|
package/dist/browser-mod.cjs
CHANGED
|
@@ -39,10 +39,8 @@ __export(browser_mod_exports, {
|
|
|
39
39
|
WorkerChildIO: () => WorkerChildIO,
|
|
40
40
|
WorkerParentIO: () => WorkerParentIO,
|
|
41
41
|
deserializeMessage: () => deserializeMessage,
|
|
42
|
-
deserializeResponse: () => deserializeResponse,
|
|
43
42
|
generateUUID: () => generateUUID,
|
|
44
|
-
serializeMessage: () => serializeMessage
|
|
45
|
-
serializeResponse: () => serializeResponse
|
|
43
|
+
serializeMessage: () => serializeMessage
|
|
46
44
|
});
|
|
47
45
|
module.exports = __toCommonJS(browser_mod_exports);
|
|
48
46
|
|
|
@@ -369,30 +367,39 @@ var import_node_buffer = require("buffer");
|
|
|
369
367
|
|
|
370
368
|
// src/serialization.ts
|
|
371
369
|
var import_superjson = __toESM(require("superjson"), 1);
|
|
372
|
-
function
|
|
373
|
-
|
|
370
|
+
function replacer(key, value) {
|
|
371
|
+
if (value instanceof Uint8Array) {
|
|
372
|
+
return {
|
|
373
|
+
type: "Uint8Array",
|
|
374
|
+
data: Array.from(value)
|
|
375
|
+
// Convert to regular array
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
return value;
|
|
374
379
|
}
|
|
375
|
-
function
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
} catch (error) {
|
|
381
|
-
console.error("failed to parse message", typeof message, message, error);
|
|
382
|
-
reject(error);
|
|
383
|
-
}
|
|
384
|
-
});
|
|
380
|
+
function reviver(key, value) {
|
|
381
|
+
if (value && value.type === "Uint8Array" && Array.isArray(value.data)) {
|
|
382
|
+
return new Uint8Array(value.data);
|
|
383
|
+
}
|
|
384
|
+
return value;
|
|
385
385
|
}
|
|
386
|
-
function
|
|
387
|
-
|
|
386
|
+
function serializeMessage(message, options = {}) {
|
|
387
|
+
const version = options.version || "superjson";
|
|
388
|
+
const msgWithVersion = { ...message, version };
|
|
389
|
+
return version === "json" ? JSON.stringify(msgWithVersion, replacer) + "\n" : import_superjson.default.stringify(msgWithVersion) + "\n";
|
|
388
390
|
}
|
|
389
|
-
function
|
|
391
|
+
function deserializeMessage(message) {
|
|
390
392
|
return new Promise((resolve, reject) => {
|
|
391
393
|
try {
|
|
392
|
-
|
|
393
|
-
|
|
394
|
+
if (message.startsWith('{"json":')) {
|
|
395
|
+
const parsed = import_superjson.default.parse(message);
|
|
396
|
+
resolve(parsed);
|
|
397
|
+
} else {
|
|
398
|
+
const parsed = JSON.parse(message, reviver);
|
|
399
|
+
resolve(parsed);
|
|
400
|
+
}
|
|
394
401
|
} catch (error) {
|
|
395
|
-
console.error("failed to parse
|
|
402
|
+
console.error("failed to parse message", typeof message, message, error);
|
|
396
403
|
reject(error);
|
|
397
404
|
}
|
|
398
405
|
});
|
|
@@ -408,6 +415,7 @@ var RPCChannel = class {
|
|
|
408
415
|
constructor(io, options) {
|
|
409
416
|
this.io = io;
|
|
410
417
|
this.apiImplementation = options?.expose;
|
|
418
|
+
this.serializationOptions = options?.serialization || {};
|
|
411
419
|
this.listen();
|
|
412
420
|
}
|
|
413
421
|
pendingRequests = {};
|
|
@@ -416,6 +424,7 @@ var RPCChannel = class {
|
|
|
416
424
|
count = 0;
|
|
417
425
|
messageStr = "";
|
|
418
426
|
apiImplementation;
|
|
427
|
+
serializationOptions;
|
|
419
428
|
/**
|
|
420
429
|
* Exposes a local API implementation that can be called remotely
|
|
421
430
|
* @param api The local API implementation to expose
|
|
@@ -462,7 +471,7 @@ var RPCChannel = class {
|
|
|
462
471
|
/**
|
|
463
472
|
* Handles a single message string by parsing and routing it
|
|
464
473
|
* @param messageStr The message string to handle
|
|
465
|
-
* @private
|
|
474
|
+
* @private
|
|
466
475
|
*/
|
|
467
476
|
async handleMessageStr(messageStr) {
|
|
468
477
|
this.count++;
|
|
@@ -512,7 +521,7 @@ var RPCChannel = class {
|
|
|
512
521
|
type: "request",
|
|
513
522
|
callbackIds: callbackIds.length > 0 ? callbackIds : void 0
|
|
514
523
|
};
|
|
515
|
-
this.io.write(serializeMessage(message));
|
|
524
|
+
this.io.write(serializeMessage(message, this.serializationOptions));
|
|
516
525
|
});
|
|
517
526
|
}
|
|
518
527
|
/**
|
|
@@ -586,7 +595,7 @@ var RPCChannel = class {
|
|
|
586
595
|
args,
|
|
587
596
|
type: "callback"
|
|
588
597
|
};
|
|
589
|
-
this.io.write(serializeMessage(message));
|
|
598
|
+
this.io.write(serializeMessage(message, this.serializationOptions));
|
|
590
599
|
}
|
|
591
600
|
/**
|
|
592
601
|
* Handles callback invocations received from the remote endpoint
|
|
@@ -615,7 +624,7 @@ var RPCChannel = class {
|
|
|
615
624
|
args: { result },
|
|
616
625
|
type: "response"
|
|
617
626
|
};
|
|
618
|
-
this.io.write(serializeMessage(response));
|
|
627
|
+
this.io.write(serializeMessage(response, this.serializationOptions));
|
|
619
628
|
}
|
|
620
629
|
/**
|
|
621
630
|
* Sends an error response back to the remote endpoint
|
|
@@ -630,7 +639,7 @@ var RPCChannel = class {
|
|
|
630
639
|
args: { error },
|
|
631
640
|
type: "response"
|
|
632
641
|
};
|
|
633
|
-
this.io.write(serializeMessage(response));
|
|
642
|
+
this.io.write(serializeMessage(response, this.serializationOptions));
|
|
634
643
|
}
|
|
635
644
|
/**
|
|
636
645
|
* Creates a nested proxy object for chaining remote method calls
|
|
@@ -681,8 +690,6 @@ var RPCChannel = class {
|
|
|
681
690
|
WorkerChildIO,
|
|
682
691
|
WorkerParentIO,
|
|
683
692
|
deserializeMessage,
|
|
684
|
-
deserializeResponse,
|
|
685
693
|
generateUUID,
|
|
686
|
-
serializeMessage
|
|
687
|
-
serializeResponse
|
|
694
|
+
serializeMessage
|
|
688
695
|
});
|
package/dist/browser-mod.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { T as TauriShellStdio, a as WorkerChildIO, W as WorkerParentIO } from './tauri-
|
|
2
|
-
export { ChromeBackgroundIO, ChromeContentIO,
|
|
3
|
-
import { D as DestroyableIoInterface } from './channel-
|
|
4
|
-
export { I as IoInterface, R as RPCChannel } from './channel-
|
|
1
|
+
export { T as TauriShellStdio, a as WorkerChildIO, W as WorkerParentIO } from './tauri-ohph68oo.cjs';
|
|
2
|
+
export { ChromeBackgroundIO, ChromeContentIO, generateUUID } from './chrome.cjs';
|
|
3
|
+
import { D as DestroyableIoInterface } from './channel-C01VCxab.cjs';
|
|
4
|
+
export { I as IoInterface, M as Message, R as RPCChannel, a as Response, S as SerializationOptions, d as deserializeMessage, s as serializeMessage } from './channel-C01VCxab.cjs';
|
|
5
5
|
import '@tauri-apps/plugin-shell';
|
|
6
6
|
import 'node:buffer';
|
|
7
7
|
|
package/dist/browser-mod.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { T as TauriShellStdio, a as WorkerChildIO, W as WorkerParentIO } from './tauri-
|
|
2
|
-
export { ChromeBackgroundIO, ChromeContentIO,
|
|
3
|
-
import { D as DestroyableIoInterface } from './channel-
|
|
4
|
-
export { I as IoInterface, R as RPCChannel } from './channel-
|
|
1
|
+
export { T as TauriShellStdio, a as WorkerChildIO, W as WorkerParentIO } from './tauri-pC0wuvjw.js';
|
|
2
|
+
export { ChromeBackgroundIO, ChromeContentIO, generateUUID } from './chrome.js';
|
|
3
|
+
import { D as DestroyableIoInterface } from './channel-C01VCxab.js';
|
|
4
|
+
export { I as IoInterface, M as Message, R as RPCChannel, a as Response, S as SerializationOptions, d as deserializeMessage, s as serializeMessage } from './channel-C01VCxab.js';
|
|
5
5
|
import '@tauri-apps/plugin-shell';
|
|
6
6
|
import 'node:buffer';
|
|
7
7
|
|
package/dist/browser-mod.js
CHANGED
|
@@ -10,11 +10,9 @@ import {
|
|
|
10
10
|
import {
|
|
11
11
|
RPCChannel,
|
|
12
12
|
deserializeMessage,
|
|
13
|
-
deserializeResponse,
|
|
14
13
|
generateUUID,
|
|
15
|
-
serializeMessage
|
|
16
|
-
|
|
17
|
-
} from "./chunk-ZSSFWNSX.js";
|
|
14
|
+
serializeMessage
|
|
15
|
+
} from "./chunk-YIQVRWAJ.js";
|
|
18
16
|
|
|
19
17
|
// src/adapters/iframe.ts
|
|
20
18
|
var DESTROY_SIGNAL = "__DESTROY__";
|
|
@@ -159,8 +157,6 @@ export {
|
|
|
159
157
|
WorkerChildIO,
|
|
160
158
|
WorkerParentIO,
|
|
161
159
|
deserializeMessage,
|
|
162
|
-
deserializeResponse,
|
|
163
160
|
generateUUID,
|
|
164
|
-
serializeMessage
|
|
165
|
-
serializeResponse
|
|
161
|
+
serializeMessage
|
|
166
162
|
};
|
|
@@ -21,6 +21,38 @@ interface DestroyableIoInterface extends IoInterface {
|
|
|
21
21
|
signalDestroy(): void;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* This file contains the serialization and deserialization functions for the RPC protocol.
|
|
26
|
+
*/
|
|
27
|
+
interface Message<T = any> {
|
|
28
|
+
id: string;
|
|
29
|
+
method: string;
|
|
30
|
+
args: T;
|
|
31
|
+
type: "request" | "response" | "callback";
|
|
32
|
+
callbackIds?: string[];
|
|
33
|
+
version?: "json" | "superjson";
|
|
34
|
+
}
|
|
35
|
+
interface Response<T = any> {
|
|
36
|
+
result?: T;
|
|
37
|
+
error?: string;
|
|
38
|
+
}
|
|
39
|
+
interface SerializationOptions {
|
|
40
|
+
version?: "json" | "superjson";
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Serialize a message with superjson (supports all data types supported by superjson)
|
|
44
|
+
* @param message - The message to serialize, an object of any shape
|
|
45
|
+
* @param options - Serialization options, default to use superjson
|
|
46
|
+
* @returns The serialized message
|
|
47
|
+
*/
|
|
48
|
+
declare function serializeMessage<T>(message: Message<T>, options?: SerializationOptions): string;
|
|
49
|
+
/**
|
|
50
|
+
* Deserialize a message with superjson (supports all data types supported by superjson)
|
|
51
|
+
* @param message - The serialized message
|
|
52
|
+
* @returns The deserialized message
|
|
53
|
+
*/
|
|
54
|
+
declare function deserializeMessage<T>(message: string): Promise<Message<T>>;
|
|
55
|
+
|
|
24
56
|
/**
|
|
25
57
|
* A bidirectional Stdio IPC channel in RPC style.
|
|
26
58
|
* This allows 2 JS/TS processes to call each other's API like using libraries in RPC style,
|
|
@@ -34,8 +66,10 @@ declare class RPCChannel<LocalAPI extends Record<string, any>, RemoteAPI extends
|
|
|
34
66
|
private count;
|
|
35
67
|
private messageStr;
|
|
36
68
|
private apiImplementation?;
|
|
69
|
+
private serializationOptions;
|
|
37
70
|
constructor(io: Io, options?: {
|
|
38
71
|
expose?: LocalAPI;
|
|
72
|
+
serialization?: SerializationOptions;
|
|
39
73
|
});
|
|
40
74
|
/**
|
|
41
75
|
* Exposes a local API implementation that can be called remotely
|
|
@@ -125,4 +159,4 @@ declare class RPCChannel<LocalAPI extends Record<string, any>, RemoteAPI extends
|
|
|
125
159
|
freeCallbacks(): void;
|
|
126
160
|
}
|
|
127
161
|
|
|
128
|
-
export { type DestroyableIoInterface as D, type IoInterface as I, RPCChannel as R };
|
|
162
|
+
export { type DestroyableIoInterface as D, type IoInterface as I, type Message as M, RPCChannel as R, type SerializationOptions as S, type Response as a, deserializeMessage as d, serializeMessage as s };
|
|
@@ -21,6 +21,38 @@ interface DestroyableIoInterface extends IoInterface {
|
|
|
21
21
|
signalDestroy(): void;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* This file contains the serialization and deserialization functions for the RPC protocol.
|
|
26
|
+
*/
|
|
27
|
+
interface Message<T = any> {
|
|
28
|
+
id: string;
|
|
29
|
+
method: string;
|
|
30
|
+
args: T;
|
|
31
|
+
type: "request" | "response" | "callback";
|
|
32
|
+
callbackIds?: string[];
|
|
33
|
+
version?: "json" | "superjson";
|
|
34
|
+
}
|
|
35
|
+
interface Response<T = any> {
|
|
36
|
+
result?: T;
|
|
37
|
+
error?: string;
|
|
38
|
+
}
|
|
39
|
+
interface SerializationOptions {
|
|
40
|
+
version?: "json" | "superjson";
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Serialize a message with superjson (supports all data types supported by superjson)
|
|
44
|
+
* @param message - The message to serialize, an object of any shape
|
|
45
|
+
* @param options - Serialization options, default to use superjson
|
|
46
|
+
* @returns The serialized message
|
|
47
|
+
*/
|
|
48
|
+
declare function serializeMessage<T>(message: Message<T>, options?: SerializationOptions): string;
|
|
49
|
+
/**
|
|
50
|
+
* Deserialize a message with superjson (supports all data types supported by superjson)
|
|
51
|
+
* @param message - The serialized message
|
|
52
|
+
* @returns The deserialized message
|
|
53
|
+
*/
|
|
54
|
+
declare function deserializeMessage<T>(message: string): Promise<Message<T>>;
|
|
55
|
+
|
|
24
56
|
/**
|
|
25
57
|
* A bidirectional Stdio IPC channel in RPC style.
|
|
26
58
|
* This allows 2 JS/TS processes to call each other's API like using libraries in RPC style,
|
|
@@ -34,8 +66,10 @@ declare class RPCChannel<LocalAPI extends Record<string, any>, RemoteAPI extends
|
|
|
34
66
|
private count;
|
|
35
67
|
private messageStr;
|
|
36
68
|
private apiImplementation?;
|
|
69
|
+
private serializationOptions;
|
|
37
70
|
constructor(io: Io, options?: {
|
|
38
71
|
expose?: LocalAPI;
|
|
72
|
+
serialization?: SerializationOptions;
|
|
39
73
|
});
|
|
40
74
|
/**
|
|
41
75
|
* Exposes a local API implementation that can be called remotely
|
|
@@ -125,4 +159,4 @@ declare class RPCChannel<LocalAPI extends Record<string, any>, RemoteAPI extends
|
|
|
125
159
|
freeCallbacks(): void;
|
|
126
160
|
}
|
|
127
161
|
|
|
128
|
-
export { type DestroyableIoInterface as D, type IoInterface as I, RPCChannel as R };
|
|
162
|
+
export { type DestroyableIoInterface as D, type IoInterface as I, type Message as M, RPCChannel as R, type SerializationOptions as S, type Response as a, deserializeMessage as d, serializeMessage as s };
|
package/dist/chrome.cjs
CHANGED
|
@@ -34,10 +34,8 @@ __export(chrome_exports, {
|
|
|
34
34
|
ChromeContentIO: () => ChromeContentIO,
|
|
35
35
|
RPCChannel: () => RPCChannel,
|
|
36
36
|
deserializeMessage: () => deserializeMessage,
|
|
37
|
-
deserializeResponse: () => deserializeResponse,
|
|
38
37
|
generateUUID: () => generateUUID,
|
|
39
|
-
serializeMessage: () => serializeMessage
|
|
40
|
-
serializeResponse: () => serializeResponse
|
|
38
|
+
serializeMessage: () => serializeMessage
|
|
41
39
|
});
|
|
42
40
|
module.exports = __toCommonJS(chrome_exports);
|
|
43
41
|
|
|
@@ -126,30 +124,39 @@ var import_node_buffer = require("buffer");
|
|
|
126
124
|
|
|
127
125
|
// src/serialization.ts
|
|
128
126
|
var import_superjson = __toESM(require("superjson"), 1);
|
|
129
|
-
function
|
|
130
|
-
|
|
127
|
+
function replacer(key, value) {
|
|
128
|
+
if (value instanceof Uint8Array) {
|
|
129
|
+
return {
|
|
130
|
+
type: "Uint8Array",
|
|
131
|
+
data: Array.from(value)
|
|
132
|
+
// Convert to regular array
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return value;
|
|
131
136
|
}
|
|
132
|
-
function
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
} catch (error) {
|
|
138
|
-
console.error("failed to parse message", typeof message, message, error);
|
|
139
|
-
reject(error);
|
|
140
|
-
}
|
|
141
|
-
});
|
|
137
|
+
function reviver(key, value) {
|
|
138
|
+
if (value && value.type === "Uint8Array" && Array.isArray(value.data)) {
|
|
139
|
+
return new Uint8Array(value.data);
|
|
140
|
+
}
|
|
141
|
+
return value;
|
|
142
142
|
}
|
|
143
|
-
function
|
|
144
|
-
|
|
143
|
+
function serializeMessage(message, options = {}) {
|
|
144
|
+
const version = options.version || "superjson";
|
|
145
|
+
const msgWithVersion = { ...message, version };
|
|
146
|
+
return version === "json" ? JSON.stringify(msgWithVersion, replacer) + "\n" : import_superjson.default.stringify(msgWithVersion) + "\n";
|
|
145
147
|
}
|
|
146
|
-
function
|
|
148
|
+
function deserializeMessage(message) {
|
|
147
149
|
return new Promise((resolve, reject) => {
|
|
148
150
|
try {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
+
if (message.startsWith('{"json":')) {
|
|
152
|
+
const parsed = import_superjson.default.parse(message);
|
|
153
|
+
resolve(parsed);
|
|
154
|
+
} else {
|
|
155
|
+
const parsed = JSON.parse(message, reviver);
|
|
156
|
+
resolve(parsed);
|
|
157
|
+
}
|
|
151
158
|
} catch (error) {
|
|
152
|
-
console.error("failed to parse
|
|
159
|
+
console.error("failed to parse message", typeof message, message, error);
|
|
153
160
|
reject(error);
|
|
154
161
|
}
|
|
155
162
|
});
|
|
@@ -165,6 +172,7 @@ var RPCChannel = class {
|
|
|
165
172
|
constructor(io, options) {
|
|
166
173
|
this.io = io;
|
|
167
174
|
this.apiImplementation = options?.expose;
|
|
175
|
+
this.serializationOptions = options?.serialization || {};
|
|
168
176
|
this.listen();
|
|
169
177
|
}
|
|
170
178
|
pendingRequests = {};
|
|
@@ -173,6 +181,7 @@ var RPCChannel = class {
|
|
|
173
181
|
count = 0;
|
|
174
182
|
messageStr = "";
|
|
175
183
|
apiImplementation;
|
|
184
|
+
serializationOptions;
|
|
176
185
|
/**
|
|
177
186
|
* Exposes a local API implementation that can be called remotely
|
|
178
187
|
* @param api The local API implementation to expose
|
|
@@ -219,7 +228,7 @@ var RPCChannel = class {
|
|
|
219
228
|
/**
|
|
220
229
|
* Handles a single message string by parsing and routing it
|
|
221
230
|
* @param messageStr The message string to handle
|
|
222
|
-
* @private
|
|
231
|
+
* @private
|
|
223
232
|
*/
|
|
224
233
|
async handleMessageStr(messageStr) {
|
|
225
234
|
this.count++;
|
|
@@ -269,7 +278,7 @@ var RPCChannel = class {
|
|
|
269
278
|
type: "request",
|
|
270
279
|
callbackIds: callbackIds.length > 0 ? callbackIds : void 0
|
|
271
280
|
};
|
|
272
|
-
this.io.write(serializeMessage(message));
|
|
281
|
+
this.io.write(serializeMessage(message, this.serializationOptions));
|
|
273
282
|
});
|
|
274
283
|
}
|
|
275
284
|
/**
|
|
@@ -343,7 +352,7 @@ var RPCChannel = class {
|
|
|
343
352
|
args,
|
|
344
353
|
type: "callback"
|
|
345
354
|
};
|
|
346
|
-
this.io.write(serializeMessage(message));
|
|
355
|
+
this.io.write(serializeMessage(message, this.serializationOptions));
|
|
347
356
|
}
|
|
348
357
|
/**
|
|
349
358
|
* Handles callback invocations received from the remote endpoint
|
|
@@ -372,7 +381,7 @@ var RPCChannel = class {
|
|
|
372
381
|
args: { result },
|
|
373
382
|
type: "response"
|
|
374
383
|
};
|
|
375
|
-
this.io.write(serializeMessage(response));
|
|
384
|
+
this.io.write(serializeMessage(response, this.serializationOptions));
|
|
376
385
|
}
|
|
377
386
|
/**
|
|
378
387
|
* Sends an error response back to the remote endpoint
|
|
@@ -387,7 +396,7 @@ var RPCChannel = class {
|
|
|
387
396
|
args: { error },
|
|
388
397
|
type: "response"
|
|
389
398
|
};
|
|
390
|
-
this.io.write(serializeMessage(response));
|
|
399
|
+
this.io.write(serializeMessage(response, this.serializationOptions));
|
|
391
400
|
}
|
|
392
401
|
/**
|
|
393
402
|
* Creates a nested proxy object for chaining remote method calls
|
|
@@ -433,8 +442,6 @@ var RPCChannel = class {
|
|
|
433
442
|
ChromeContentIO,
|
|
434
443
|
RPCChannel,
|
|
435
444
|
deserializeMessage,
|
|
436
|
-
deserializeResponse,
|
|
437
445
|
generateUUID,
|
|
438
|
-
serializeMessage
|
|
439
|
-
serializeResponse
|
|
446
|
+
serializeMessage
|
|
440
447
|
});
|
package/dist/chrome.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { D as DestroyableIoInterface } from './channel-
|
|
2
|
-
export { I as IoInterface, R as RPCChannel } from './channel-
|
|
1
|
+
import { D as DestroyableIoInterface } from './channel-C01VCxab.cjs';
|
|
2
|
+
export { I as IoInterface, M as Message, R as RPCChannel, a as Response, S as SerializationOptions, d as deserializeMessage, s as serializeMessage } from './channel-C01VCxab.cjs';
|
|
3
3
|
import 'node:buffer';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -40,43 +40,4 @@ declare class ChromeContentIO implements DestroyableIoInterface {
|
|
|
40
40
|
*/
|
|
41
41
|
declare function generateUUID(): string;
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
* This file contains the serialization and deserialization functions for the RPC protocol.
|
|
45
|
-
*/
|
|
46
|
-
interface Message<T = any> {
|
|
47
|
-
id: string;
|
|
48
|
-
method: string;
|
|
49
|
-
args: T;
|
|
50
|
-
type: "request" | "response" | "callback";
|
|
51
|
-
callbackIds?: string[];
|
|
52
|
-
}
|
|
53
|
-
interface Response<T = any> {
|
|
54
|
-
result?: T;
|
|
55
|
-
error?: string;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Serialize a message with superjson (supports all data types supported by superjson)
|
|
59
|
-
* @param message - The message to serialize, an object of any shape
|
|
60
|
-
* @returns The serialized message
|
|
61
|
-
*/
|
|
62
|
-
declare function serializeMessage<T>(message: Message<T>): string;
|
|
63
|
-
/**
|
|
64
|
-
* Deserialize a message with superjson (supports all data types supported by superjson)
|
|
65
|
-
* @param message - The serialized message
|
|
66
|
-
* @returns The deserialized message
|
|
67
|
-
*/
|
|
68
|
-
declare function deserializeMessage<T>(message: string): Promise<Message<T>>;
|
|
69
|
-
/**
|
|
70
|
-
* Serialize a response with JSON (only supports primitive types)
|
|
71
|
-
* @param response - The response to serialize, an object of primitive types
|
|
72
|
-
* @returns The serialized response
|
|
73
|
-
*/
|
|
74
|
-
declare function serializeResponse<T>(response: Response<T>): string;
|
|
75
|
-
/**
|
|
76
|
-
* Deserialize a response with superjson (supports all data types supported by superjson)
|
|
77
|
-
* @param response - The serialized response
|
|
78
|
-
* @returns The deserialized response
|
|
79
|
-
*/
|
|
80
|
-
declare function deserializeResponse<T>(response: string): Promise<Response<T>>;
|
|
81
|
-
|
|
82
|
-
export { ChromeBackgroundIO, ChromeContentIO, DestroyableIoInterface, type Message, type Response, deserializeMessage, deserializeResponse, generateUUID, serializeMessage, serializeResponse };
|
|
43
|
+
export { ChromeBackgroundIO, ChromeContentIO, DestroyableIoInterface, generateUUID };
|
package/dist/chrome.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { D as DestroyableIoInterface } from './channel-
|
|
2
|
-
export { I as IoInterface, R as RPCChannel } from './channel-
|
|
1
|
+
import { D as DestroyableIoInterface } from './channel-C01VCxab.js';
|
|
2
|
+
export { I as IoInterface, M as Message, R as RPCChannel, a as Response, S as SerializationOptions, d as deserializeMessage, s as serializeMessage } from './channel-C01VCxab.js';
|
|
3
3
|
import 'node:buffer';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -40,43 +40,4 @@ declare class ChromeContentIO implements DestroyableIoInterface {
|
|
|
40
40
|
*/
|
|
41
41
|
declare function generateUUID(): string;
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
* This file contains the serialization and deserialization functions for the RPC protocol.
|
|
45
|
-
*/
|
|
46
|
-
interface Message<T = any> {
|
|
47
|
-
id: string;
|
|
48
|
-
method: string;
|
|
49
|
-
args: T;
|
|
50
|
-
type: "request" | "response" | "callback";
|
|
51
|
-
callbackIds?: string[];
|
|
52
|
-
}
|
|
53
|
-
interface Response<T = any> {
|
|
54
|
-
result?: T;
|
|
55
|
-
error?: string;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Serialize a message with superjson (supports all data types supported by superjson)
|
|
59
|
-
* @param message - The message to serialize, an object of any shape
|
|
60
|
-
* @returns The serialized message
|
|
61
|
-
*/
|
|
62
|
-
declare function serializeMessage<T>(message: Message<T>): string;
|
|
63
|
-
/**
|
|
64
|
-
* Deserialize a message with superjson (supports all data types supported by superjson)
|
|
65
|
-
* @param message - The serialized message
|
|
66
|
-
* @returns The deserialized message
|
|
67
|
-
*/
|
|
68
|
-
declare function deserializeMessage<T>(message: string): Promise<Message<T>>;
|
|
69
|
-
/**
|
|
70
|
-
* Serialize a response with JSON (only supports primitive types)
|
|
71
|
-
* @param response - The response to serialize, an object of primitive types
|
|
72
|
-
* @returns The serialized response
|
|
73
|
-
*/
|
|
74
|
-
declare function serializeResponse<T>(response: Response<T>): string;
|
|
75
|
-
/**
|
|
76
|
-
* Deserialize a response with superjson (supports all data types supported by superjson)
|
|
77
|
-
* @param response - The serialized response
|
|
78
|
-
* @returns The deserialized response
|
|
79
|
-
*/
|
|
80
|
-
declare function deserializeResponse<T>(response: string): Promise<Response<T>>;
|
|
81
|
-
|
|
82
|
-
export { ChromeBackgroundIO, ChromeContentIO, DestroyableIoInterface, type Message, type Response, deserializeMessage, deserializeResponse, generateUUID, serializeMessage, serializeResponse };
|
|
43
|
+
export { ChromeBackgroundIO, ChromeContentIO, DestroyableIoInterface, generateUUID };
|
package/dist/chrome.js
CHANGED
|
@@ -5,18 +5,14 @@ import {
|
|
|
5
5
|
import {
|
|
6
6
|
RPCChannel,
|
|
7
7
|
deserializeMessage,
|
|
8
|
-
deserializeResponse,
|
|
9
8
|
generateUUID,
|
|
10
|
-
serializeMessage
|
|
11
|
-
|
|
12
|
-
} from "./chunk-ZSSFWNSX.js";
|
|
9
|
+
serializeMessage
|
|
10
|
+
} from "./chunk-YIQVRWAJ.js";
|
|
13
11
|
export {
|
|
14
12
|
ChromeBackgroundIO,
|
|
15
13
|
ChromeContentIO,
|
|
16
14
|
RPCChannel,
|
|
17
15
|
deserializeMessage,
|
|
18
|
-
deserializeResponse,
|
|
19
16
|
generateUUID,
|
|
20
|
-
serializeMessage
|
|
21
|
-
serializeResponse
|
|
17
|
+
serializeMessage
|
|
22
18
|
};
|