bunite-core 0.0.13 → 0.0.14
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/package.json +1 -1
- package/src/shared/rpc.ts +11 -0
- package/src/shared/webRpcHandler.ts +16 -6
package/package.json
CHANGED
package/src/shared/rpc.ts
CHANGED
|
@@ -343,6 +343,16 @@ export function createRPC<
|
|
|
343
343
|
) => void;
|
|
344
344
|
};
|
|
345
345
|
|
|
346
|
+
function dispose() {
|
|
347
|
+
for (const [id, pending] of pendingRequests) {
|
|
348
|
+
clearTimeout(pending.timeout);
|
|
349
|
+
pending.reject(new Error("RPC disposed"));
|
|
350
|
+
}
|
|
351
|
+
pendingRequests.clear();
|
|
352
|
+
transport.unregisterHandler?.();
|
|
353
|
+
transport = {};
|
|
354
|
+
}
|
|
355
|
+
|
|
346
356
|
return {
|
|
347
357
|
setTransport,
|
|
348
358
|
setRequestHandler,
|
|
@@ -352,6 +362,7 @@ export function createRPC<
|
|
|
352
362
|
sendProxy,
|
|
353
363
|
addMessageListener,
|
|
354
364
|
removeMessageListener,
|
|
365
|
+
dispose,
|
|
355
366
|
proxy: {
|
|
356
367
|
request: requestProxy,
|
|
357
368
|
send: sendProxy
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
type RPCPacket
|
|
7
7
|
} from "./rpc";
|
|
8
8
|
import { decodeRPCPacket, encodeRPCPacket, asUint8Array } from "./rpcWire";
|
|
9
|
+
import { log } from "./log";
|
|
9
10
|
|
|
10
11
|
type WebRPCSocket = { send(data: Uint8Array | ArrayBuffer): void | number };
|
|
11
12
|
|
|
@@ -60,9 +61,11 @@ export function createWebRPCHandler<Schema extends BuniteRPCSchema>(
|
|
|
60
61
|
if (!client) return;
|
|
61
62
|
|
|
62
63
|
try {
|
|
63
|
-
client.handlePacket(decodeRPCPacket(asUint8Array(raw)))
|
|
64
|
+
Promise.resolve(client.handlePacket(decodeRPCPacket(asUint8Array(raw)))).catch((error) => {
|
|
65
|
+
log.error("Web RPC packet handler error", error);
|
|
66
|
+
});
|
|
64
67
|
} catch {
|
|
65
|
-
// malformed packet
|
|
68
|
+
// malformed packet — decode failure
|
|
66
69
|
}
|
|
67
70
|
},
|
|
68
71
|
|
|
@@ -70,17 +73,24 @@ export function createWebRPCHandler<Schema extends BuniteRPCSchema>(
|
|
|
70
73
|
const client = connections.get(ws);
|
|
71
74
|
if (!client) return;
|
|
72
75
|
|
|
76
|
+
client.rpc.dispose();
|
|
73
77
|
webClients.delete(client);
|
|
74
|
-
handler.onWebClientDisconnected?.(client);
|
|
75
|
-
client.rpc.setTransport({});
|
|
76
78
|
connections.delete(ws);
|
|
79
|
+
handler.onWebClientDisconnected?.(client);
|
|
77
80
|
},
|
|
78
81
|
|
|
79
82
|
webClients: webClients as ReadonlySet<WebRPCClient<Schema>>,
|
|
80
83
|
|
|
81
|
-
broadcast
|
|
84
|
+
broadcast<M extends keyof Schema["bun"]["messages"]>(
|
|
85
|
+
messageName: M,
|
|
86
|
+
...args: void extends Schema["bun"]["messages"][M]
|
|
87
|
+
? []
|
|
88
|
+
: undefined extends Schema["bun"]["messages"][M]
|
|
89
|
+
? [payload?: Schema["bun"]["messages"][M]]
|
|
90
|
+
: [payload: Schema["bun"]["messages"][M]]
|
|
91
|
+
) {
|
|
82
92
|
for (const client of webClients) {
|
|
83
|
-
(client.rpc.send as any)(messageName,
|
|
93
|
+
(client.rpc.send as any)(messageName, ...args);
|
|
84
94
|
}
|
|
85
95
|
},
|
|
86
96
|
|