@schwarzdavid/anyrpc 0.0.0-stage → 0.0.2-alpha.1
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 +551 -3
- package/dist/child-process.cjs +1 -0
- package/dist/child-process.js +134 -0
- package/dist/chunks/AbstractRpcAdapter-7gOLZtYD.js +82 -0
- package/dist/chunks/AbstractRpcAdapter-BPIitR0A.cjs +1 -0
- package/dist/chunks/AbstractRpcHubAdapter-BVPYUoL9.js +26 -0
- package/dist/chunks/AbstractRpcHubAdapter-Bqcrs7Mb.cjs +1 -0
- package/dist/chunks/ControlProtocol-0b5_21ZU.js +82 -0
- package/dist/chunks/ControlProtocol-BMvwQecN.cjs +1 -0
- package/dist/chunks/handshake-CWtm9Y59.cjs +1 -0
- package/dist/chunks/handshake-CZWqKvZe.js +39 -0
- package/dist/chunks/index-B_SfPYCc.js +133 -0
- package/dist/chunks/index-D5LbDuYg.cjs +1 -0
- package/dist/chunks/rpc-Brf2MoZo.cjs +1 -0
- package/dist/chunks/rpc-DeJEGj4-.js +134 -0
- package/dist/chunks/uuid-C2SCOpSM.cjs +1 -0
- package/dist/chunks/uuid-DTlgoKBw.js +9 -0
- package/dist/constants/JSON_RPC_ERRORS.d.ts +19 -0
- package/dist/constants/JSON_RPC_ERRORS.d.ts.map +1 -0
- package/dist/createRpcChannel.d.ts +18 -0
- package/dist/createRpcChannel.d.ts.map +1 -0
- package/dist/createRpcHub.d.ts +7 -0
- package/dist/createRpcHub.d.ts.map +1 -0
- package/dist/iframe.cjs +1 -0
- package/dist/iframe.js +80 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +447 -0
- package/dist/lib/ControlProtocol.d.ts +20 -0
- package/dist/lib/ControlProtocol.d.ts.map +1 -0
- package/dist/lib/EventEmitter.d.ts +12 -0
- package/dist/lib/EventEmitter.d.ts.map +1 -0
- package/dist/lib/HandlerRegistry.d.ts +8 -0
- package/dist/lib/HandlerRegistry.d.ts.map +1 -0
- package/dist/lib/Heartbeat.d.ts +24 -0
- package/dist/lib/Heartbeat.d.ts.map +1 -0
- package/dist/lib/Logger.d.ts +13 -0
- package/dist/lib/Logger.d.ts.map +1 -0
- package/dist/lib/RpcError.d.ts +11 -0
- package/dist/lib/RpcError.d.ts.map +1 -0
- package/dist/lib/RpcMessageError.d.ts +15 -0
- package/dist/lib/RpcMessageError.d.ts.map +1 -0
- package/dist/lib/adapter/AbstractRpcAdapter.d.ts +28 -0
- package/dist/lib/adapter/AbstractRpcAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/AbstractRpcHubAdapter.d.ts +24 -0
- package/dist/lib/adapter/AbstractRpcHubAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/child-process/ChildProcessClientAdapter.d.ts +19 -0
- package/dist/lib/adapter/child-process/ChildProcessClientAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/child-process/ChildProcessHostAdapter.d.ts +43 -0
- package/dist/lib/adapter/child-process/ChildProcessHostAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/child-process/constants/childProcessHandshake.d.ts +3 -0
- package/dist/lib/adapter/child-process/constants/childProcessHandshake.d.ts.map +1 -0
- package/dist/lib/adapter/child-process/index.d.ts +4 -0
- package/dist/lib/adapter/child-process/index.d.ts.map +1 -0
- package/dist/lib/adapter/child-process/utils/currentProcess.d.ts +16 -0
- package/dist/lib/adapter/child-process/utils/currentProcess.d.ts.map +1 -0
- package/dist/lib/adapter/child-process/utils/ipcEnvelope.d.ts +7 -0
- package/dist/lib/adapter/child-process/utils/ipcEnvelope.d.ts.map +1 -0
- package/dist/lib/adapter/iframe/IFrameClientAdapter.d.ts +17 -0
- package/dist/lib/adapter/iframe/IFrameClientAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/iframe/IFrameHostAdapter.d.ts +20 -0
- package/dist/lib/adapter/iframe/IFrameHostAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/iframe/constants/iframeHandshake.d.ts +3 -0
- package/dist/lib/adapter/iframe/constants/iframeHandshake.d.ts.map +1 -0
- package/dist/lib/adapter/iframe/index.d.ts +5 -0
- package/dist/lib/adapter/iframe/index.d.ts.map +1 -0
- package/dist/lib/adapter/iframe/utils/iframeOrigin.d.ts +5 -0
- package/dist/lib/adapter/iframe/utils/iframeOrigin.d.ts.map +1 -0
- package/dist/lib/adapter/message-port/AbstractMessagePortAdapter.d.ts +38 -0
- package/dist/lib/adapter/message-port/AbstractMessagePortAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/message-port/AbstractMessagePortClientAdapter.d.ts +22 -0
- package/dist/lib/adapter/message-port/AbstractMessagePortClientAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/message-port/AbstractMessagePortHostAdapter.d.ts +20 -0
- package/dist/lib/adapter/message-port/AbstractMessagePortHostAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/message-port/index.d.ts +6 -0
- package/dist/lib/adapter/message-port/index.d.ts.map +1 -0
- package/dist/lib/adapter/message-port/utils/handshake.d.ts +13 -0
- package/dist/lib/adapter/message-port/utils/handshake.d.ts.map +1 -0
- package/dist/lib/adapter/message-port/utils/messageData.d.ts +2 -0
- package/dist/lib/adapter/message-port/utils/messageData.d.ts.map +1 -0
- package/dist/lib/adapter/node-worker/NodeWorkerClientAdapter.d.ts +12 -0
- package/dist/lib/adapter/node-worker/NodeWorkerClientAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/node-worker/NodeWorkerHostAdapter.d.ts +35 -0
- package/dist/lib/adapter/node-worker/NodeWorkerHostAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/node-worker/constants/nodeWorkerHandshake.d.ts +3 -0
- package/dist/lib/adapter/node-worker/constants/nodeWorkerHandshake.d.ts.map +1 -0
- package/dist/lib/adapter/node-worker/index.d.ts +4 -0
- package/dist/lib/adapter/node-worker/index.d.ts.map +1 -0
- package/dist/lib/adapter/node-worker/utils/initEnvelope.d.ts +13 -0
- package/dist/lib/adapter/node-worker/utils/initEnvelope.d.ts.map +1 -0
- package/dist/lib/adapter/socket/AbstractSocketAdapter.d.ts +23 -0
- package/dist/lib/adapter/socket/AbstractSocketAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/socket/index.d.ts +3 -0
- package/dist/lib/adapter/socket/index.d.ts.map +1 -0
- package/dist/lib/adapter/socket/utils/parseObject.d.ts +2 -0
- package/dist/lib/adapter/socket/utils/parseObject.d.ts.map +1 -0
- package/dist/lib/adapter/web-worker/SharedWorkerHubAdapter.d.ts +18 -0
- package/dist/lib/adapter/web-worker/SharedWorkerHubAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/web-worker/SharedWorkerPeerAdapter.d.ts +18 -0
- package/dist/lib/adapter/web-worker/SharedWorkerPeerAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/web-worker/WebWorkerClientAdapter.d.ts +10 -0
- package/dist/lib/adapter/web-worker/WebWorkerClientAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/web-worker/WebWorkerHostAdapter.d.ts +33 -0
- package/dist/lib/adapter/web-worker/WebWorkerHostAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/web-worker/constants/webWorkerHandshake.d.ts +3 -0
- package/dist/lib/adapter/web-worker/constants/webWorkerHandshake.d.ts.map +1 -0
- package/dist/lib/adapter/web-worker/index.d.ts +5 -0
- package/dist/lib/adapter/web-worker/index.d.ts.map +1 -0
- package/dist/lib/adapter/web-worker/utils/describeWorkerError.d.ts +3 -0
- package/dist/lib/adapter/web-worker/utils/describeWorkerError.d.ts.map +1 -0
- package/dist/lib/adapter/web-worker/utils/globalScope.d.ts +8 -0
- package/dist/lib/adapter/web-worker/utils/globalScope.d.ts.map +1 -0
- package/dist/lib/adapter/websocket/WebSocketClientAdapter.d.ts +27 -0
- package/dist/lib/adapter/websocket/WebSocketClientAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/websocket/WebSocketHubAdapter.d.ts +21 -0
- package/dist/lib/adapter/websocket/WebSocketHubAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/websocket/WebSocketPeerAdapter.d.ts +28 -0
- package/dist/lib/adapter/websocket/WebSocketPeerAdapter.d.ts.map +1 -0
- package/dist/lib/adapter/websocket/constants/webSocket.d.ts +5 -0
- package/dist/lib/adapter/websocket/constants/webSocket.d.ts.map +1 -0
- package/dist/lib/adapter/websocket/index.d.ts +5 -0
- package/dist/lib/adapter/websocket/index.d.ts.map +1 -0
- package/dist/lib/adapter/websocket/utils/reconnectDelay.d.ts +2 -0
- package/dist/lib/adapter/websocket/utils/reconnectDelay.d.ts.map +1 -0
- package/dist/message-port.cjs +1 -0
- package/dist/message-port.js +6 -0
- package/dist/node-worker.cjs +1 -0
- package/dist/node-worker.js +80 -0
- package/dist/socket.cjs +1 -0
- package/dist/socket.js +66 -0
- package/dist/types/ConnectionStatus.d.ts +6 -0
- package/dist/types/ConnectionStatus.d.ts.map +1 -0
- package/dist/types/EventEmitter.d.ts +6 -0
- package/dist/types/EventEmitter.d.ts.map +1 -0
- package/dist/types/LogLevel.d.ts +9 -0
- package/dist/types/LogLevel.d.ts.map +1 -0
- package/dist/types/MethodMap.d.ts +5 -0
- package/dist/types/MethodMap.d.ts.map +1 -0
- package/dist/types/MethodName.d.ts +9 -0
- package/dist/types/MethodName.d.ts.map +1 -0
- package/dist/types/RpcChannel.d.ts +29 -0
- package/dist/types/RpcChannel.d.ts.map +1 -0
- package/dist/types/RpcChannelEvents.d.ts +9 -0
- package/dist/types/RpcChannelEvents.d.ts.map +1 -0
- package/dist/types/RpcHub.d.ts +36 -0
- package/dist/types/RpcHub.d.ts.map +1 -0
- package/dist/types/RpcMessages.d.ts +37 -0
- package/dist/types/RpcMessages.d.ts.map +1 -0
- package/dist/utils/controlMessages.d.ts +14 -0
- package/dist/utils/controlMessages.d.ts.map +1 -0
- package/dist/utils/createMethodProxy.d.ts +5 -0
- package/dist/utils/createMethodProxy.d.ts.map +1 -0
- package/dist/utils/rpc.d.ts +26 -0
- package/dist/utils/rpc.d.ts.map +1 -0
- package/dist/utils/uuid.d.ts +2 -0
- package/dist/utils/uuid.d.ts.map +1 -0
- package/dist/web-worker.cjs +1 -0
- package/dist/web-worker.js +159 -0
- package/dist/websocket.cjs +1 -0
- package/dist/websocket.js +113 -0
- package/package.json +104 -6
- package/src/constants/JSON_RPC_ERRORS.ts +33 -0
- package/src/createRpcChannel.ts +478 -0
- package/src/createRpcHub.ts +242 -0
- package/src/index.ts +33 -0
- package/src/lib/ControlProtocol.ts +62 -0
- package/src/lib/EventEmitter.ts +42 -0
- package/src/lib/HandlerRegistry.ts +26 -0
- package/src/lib/Heartbeat.ts +55 -0
- package/src/lib/Logger.ts +44 -0
- package/src/lib/RpcError.ts +41 -0
- package/src/lib/RpcMessageError.ts +25 -0
- package/src/lib/adapter/AbstractRpcAdapter.ts +63 -0
- package/src/lib/adapter/AbstractRpcHubAdapter.ts +49 -0
- package/src/lib/adapter/child-process/ChildProcessClientAdapter.ts +89 -0
- package/src/lib/adapter/child-process/ChildProcessHostAdapter.ts +136 -0
- package/src/lib/adapter/child-process/constants/childProcessHandshake.ts +3 -0
- package/src/lib/adapter/child-process/index.ts +3 -0
- package/src/lib/adapter/child-process/utils/currentProcess.ts +27 -0
- package/src/lib/adapter/child-process/utils/ipcEnvelope.ts +19 -0
- package/src/lib/adapter/iframe/IFrameClientAdapter.ts +62 -0
- package/src/lib/adapter/iframe/IFrameHostAdapter.ts +55 -0
- package/src/lib/adapter/iframe/constants/iframeHandshake.ts +3 -0
- package/src/lib/adapter/iframe/index.ts +4 -0
- package/src/lib/adapter/iframe/utils/iframeOrigin.ts +48 -0
- package/src/lib/adapter/message-port/AbstractMessagePortAdapter.ts +130 -0
- package/src/lib/adapter/message-port/AbstractMessagePortClientAdapter.ts +88 -0
- package/src/lib/adapter/message-port/AbstractMessagePortHostAdapter.ts +75 -0
- package/src/lib/adapter/message-port/index.ts +11 -0
- package/src/lib/adapter/message-port/utils/handshake.ts +60 -0
- package/src/lib/adapter/message-port/utils/messageData.ts +3 -0
- package/src/lib/adapter/node-worker/NodeWorkerClientAdapter.ts +46 -0
- package/src/lib/adapter/node-worker/NodeWorkerHostAdapter.ts +89 -0
- package/src/lib/adapter/node-worker/constants/nodeWorkerHandshake.ts +3 -0
- package/src/lib/adapter/node-worker/index.ts +3 -0
- package/src/lib/adapter/node-worker/utils/initEnvelope.ts +38 -0
- package/src/lib/adapter/socket/AbstractSocketAdapter.ts +99 -0
- package/src/lib/adapter/socket/index.ts +2 -0
- package/src/lib/adapter/socket/utils/parseObject.ts +8 -0
- package/src/lib/adapter/web-worker/SharedWorkerHubAdapter.ts +90 -0
- package/src/lib/adapter/web-worker/SharedWorkerPeerAdapter.ts +41 -0
- package/src/lib/adapter/web-worker/WebWorkerClientAdapter.ts +45 -0
- package/src/lib/adapter/web-worker/WebWorkerHostAdapter.ts +97 -0
- package/src/lib/adapter/web-worker/constants/webWorkerHandshake.ts +3 -0
- package/src/lib/adapter/web-worker/index.ts +4 -0
- package/src/lib/adapter/web-worker/utils/describeWorkerError.ts +13 -0
- package/src/lib/adapter/web-worker/utils/globalScope.ts +15 -0
- package/src/lib/adapter/websocket/WebSocketClientAdapter.ts +109 -0
- package/src/lib/adapter/websocket/WebSocketHubAdapter.ts +45 -0
- package/src/lib/adapter/websocket/WebSocketPeerAdapter.ts +76 -0
- package/src/lib/adapter/websocket/constants/webSocket.ts +7 -0
- package/src/lib/adapter/websocket/index.ts +4 -0
- package/src/lib/adapter/websocket/utils/reconnectDelay.ts +5 -0
- package/src/types/ConnectionStatus.ts +6 -0
- package/src/types/EventEmitter.ts +8 -0
- package/src/types/LogLevel.ts +9 -0
- package/src/types/MethodMap.ts +6 -0
- package/src/types/MethodName.ts +13 -0
- package/src/types/RpcChannel.ts +53 -0
- package/src/types/RpcChannelEvents.ts +9 -0
- package/src/types/RpcHub.ts +69 -0
- package/src/types/RpcMessages.ts +64 -0
- package/src/utils/controlMessages.ts +30 -0
- package/src/utils/createMethodProxy.ts +24 -0
- package/src/utils/rpc.ts +186 -0
- package/src/utils/uuid.ts +10 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { AbstractSocketAdapter as i } from "./socket.js";
|
|
2
|
+
import { r as c } from "./chunks/ControlProtocol-0b5_21ZU.js";
|
|
3
|
+
import { c as r, i as l, a as p, b as g } from "./chunks/handshake-CZWqKvZe.js";
|
|
4
|
+
const h = "anyrpc::child-process-init", o = "anyrpc-child-process-adapter";
|
|
5
|
+
function d(s, e) {
|
|
6
|
+
return { anyrpc: s, message: e };
|
|
7
|
+
}
|
|
8
|
+
function a(s, e) {
|
|
9
|
+
if (typeof s != "object" || s === null || !("anyrpc" in s) || !("message" in s) || s.anyrpc !== e)
|
|
10
|
+
return;
|
|
11
|
+
const { message: t } = s;
|
|
12
|
+
return typeof t == "object" && t !== null ? t : void 0;
|
|
13
|
+
}
|
|
14
|
+
class y extends i {
|
|
15
|
+
constructor(e, t = o, n) {
|
|
16
|
+
super(c(n?.heartbeat ?? !1)), this.child = e, this.name = t, this.pollingInterval = n?.pollingInterval ?? 1e3, this.killOnDestroy = n?.killOnDestroy ?? !1;
|
|
17
|
+
}
|
|
18
|
+
pollingInterval;
|
|
19
|
+
killOnDestroy;
|
|
20
|
+
handshake;
|
|
21
|
+
onChildMessage = (e) => this.handleChildMessage(e);
|
|
22
|
+
onChildExit = (e) => this.handleChildExit(e);
|
|
23
|
+
onChildDisconnect = () => this.handleChildDisconnect();
|
|
24
|
+
onSendResult = (e) => this.handleSendResult(e);
|
|
25
|
+
connect() {
|
|
26
|
+
if (this.stopListening(), !this.child.connected) {
|
|
27
|
+
this.handleChildDisconnect();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.child.on("message", this.onChildMessage), this.child.on("exit", this.onChildExit), this.child.on("disconnect", this.onChildDisconnect), this.startHandshake();
|
|
31
|
+
}
|
|
32
|
+
send(e) {
|
|
33
|
+
this.child.connected && this.child.send(d(this.name, e), this.onSendResult);
|
|
34
|
+
}
|
|
35
|
+
onRemoteGoodbye() {
|
|
36
|
+
super.onRemoteGoodbye(), this.startHandshake();
|
|
37
|
+
}
|
|
38
|
+
destroy() {
|
|
39
|
+
this.stopHandshake(), super.destroy(), this.stopListening(), this.killOnDestroy && this.child.kill();
|
|
40
|
+
}
|
|
41
|
+
startHandshake() {
|
|
42
|
+
this.stopHandshake();
|
|
43
|
+
const e = r(h, this.name);
|
|
44
|
+
this.handshake = {
|
|
45
|
+
request: e,
|
|
46
|
+
pollingIntervalId: setInterval(() => this.send(e), this.pollingInterval)
|
|
47
|
+
}, this.send(e);
|
|
48
|
+
}
|
|
49
|
+
stopHandshake() {
|
|
50
|
+
clearInterval(this.handshake?.pollingIntervalId), this.handshake = void 0;
|
|
51
|
+
}
|
|
52
|
+
handleChildMessage(e) {
|
|
53
|
+
const t = a(e, this.name);
|
|
54
|
+
if (t) {
|
|
55
|
+
if (this.handshake && l(t, this.handshake.request.id)) {
|
|
56
|
+
this.stopHandshake(), this.opened();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
this.receive(t);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
handleChildExit(e) {
|
|
63
|
+
this.logger?.info(`The child process exited with code ${String(e)}, so the channel will not reconnect`), this.destroy();
|
|
64
|
+
}
|
|
65
|
+
handleChildDisconnect() {
|
|
66
|
+
this.logger?.info("The IPC channel to the child process is closed, so the channel will not reconnect"), this.destroy();
|
|
67
|
+
}
|
|
68
|
+
handleSendResult(e) {
|
|
69
|
+
e && this.logger?.debug("Could not send a message to the child process", e);
|
|
70
|
+
}
|
|
71
|
+
stopListening() {
|
|
72
|
+
this.child.off("message", this.onChildMessage), this.child.off("exit", this.onChildExit), this.child.off("disconnect", this.onChildDisconnect);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function u() {
|
|
76
|
+
return process;
|
|
77
|
+
}
|
|
78
|
+
function f(s) {
|
|
79
|
+
return typeof s.send == "function";
|
|
80
|
+
}
|
|
81
|
+
function I() {
|
|
82
|
+
const s = u();
|
|
83
|
+
if (!f(s))
|
|
84
|
+
throw new Error(
|
|
85
|
+
"ChildProcessClientAdapter must be created in a process with an IPC channel, e.g. one started with child_process.fork(). Use ChildProcessHostAdapter in the parent process."
|
|
86
|
+
);
|
|
87
|
+
return s;
|
|
88
|
+
}
|
|
89
|
+
class R extends i {
|
|
90
|
+
constructor(e = o) {
|
|
91
|
+
super(), this.name = e, this.ipc = I();
|
|
92
|
+
}
|
|
93
|
+
ipc;
|
|
94
|
+
answeredInitId;
|
|
95
|
+
onParentMessage = (e) => this.handleParentMessage(e);
|
|
96
|
+
onDisconnect = () => this.closed();
|
|
97
|
+
onSendResult = (e) => this.handleSendResult(e);
|
|
98
|
+
connect() {
|
|
99
|
+
this.stopListening(), this.ipc.on("message", this.onParentMessage), this.ipc.on("disconnect", this.onDisconnect);
|
|
100
|
+
}
|
|
101
|
+
send(e) {
|
|
102
|
+
this.ipc.connected && this.ipc.send(d(this.name, e), void 0, void 0, this.onSendResult);
|
|
103
|
+
}
|
|
104
|
+
destroy() {
|
|
105
|
+
super.destroy(), this.stopListening();
|
|
106
|
+
}
|
|
107
|
+
handleSendResult(e) {
|
|
108
|
+
e && this.logger?.debug("Could not send a message to the parent process", e);
|
|
109
|
+
}
|
|
110
|
+
handleParentMessage(e) {
|
|
111
|
+
const t = a(e, this.name);
|
|
112
|
+
if (t) {
|
|
113
|
+
if (p(t, h, this.name)) {
|
|
114
|
+
this.acceptInitRequest(t);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
this.receive(t);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
acceptInitRequest(e) {
|
|
121
|
+
if (e.id === this.answeredInitId) {
|
|
122
|
+
this.logger?.debug("Ignored a repeated handshake request that was already answered");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.answeredInitId !== void 0 && this.closed(), this.answeredInitId = e.id, this.send(g(e)), this.opened();
|
|
126
|
+
}
|
|
127
|
+
stopListening() {
|
|
128
|
+
this.ipc.off("message", this.onParentMessage), this.ipc.off("disconnect", this.onDisconnect);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export {
|
|
132
|
+
R as ChildProcessClientAdapter,
|
|
133
|
+
y as ChildProcessHostAdapter
|
|
134
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const s = {
|
|
2
|
+
CONNECTED: "connected",
|
|
3
|
+
DISCONNECTED: "disconnected"
|
|
4
|
+
}, e = {
|
|
5
|
+
DEBUG: 100,
|
|
6
|
+
INFO: 200,
|
|
7
|
+
WARN: 300,
|
|
8
|
+
ERROR: 400,
|
|
9
|
+
NONE: 9999
|
|
10
|
+
};
|
|
11
|
+
class i {
|
|
12
|
+
constructor(n, t) {
|
|
13
|
+
this.name = n, this.level = t;
|
|
14
|
+
}
|
|
15
|
+
debug(...n) {
|
|
16
|
+
this.printMessage(e.DEBUG, ...n);
|
|
17
|
+
}
|
|
18
|
+
info(...n) {
|
|
19
|
+
this.printMessage(e.INFO, ...n);
|
|
20
|
+
}
|
|
21
|
+
warn(...n) {
|
|
22
|
+
this.printMessage(e.WARN, ...n);
|
|
23
|
+
}
|
|
24
|
+
error(...n) {
|
|
25
|
+
this.printMessage(e.ERROR, ...n);
|
|
26
|
+
}
|
|
27
|
+
printMessage(n, ...t) {
|
|
28
|
+
const c = this.getLogFn(n);
|
|
29
|
+
this.level <= n && c(`[${this.name}]`, ...t);
|
|
30
|
+
}
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
+
getLogFn(n) {
|
|
33
|
+
switch (n) {
|
|
34
|
+
case e.ERROR:
|
|
35
|
+
return console.error;
|
|
36
|
+
case e.WARN:
|
|
37
|
+
return console.warn;
|
|
38
|
+
default:
|
|
39
|
+
return console.log;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
class a {
|
|
44
|
+
_messageHandler;
|
|
45
|
+
_connectionHandler;
|
|
46
|
+
_connectionLostHandler;
|
|
47
|
+
_connectionStatus = s.DISCONNECTED;
|
|
48
|
+
logger;
|
|
49
|
+
get connectionStatus() {
|
|
50
|
+
return this._connectionStatus;
|
|
51
|
+
}
|
|
52
|
+
set connectionStatus(n) {
|
|
53
|
+
this._connectionStatus !== n && (this._connectionStatus = n, this._connectionHandler?.(n));
|
|
54
|
+
}
|
|
55
|
+
set connectionHandler(n) {
|
|
56
|
+
this._connectionHandler = n;
|
|
57
|
+
}
|
|
58
|
+
set connectionLostHandler(n) {
|
|
59
|
+
this._connectionLostHandler = n;
|
|
60
|
+
}
|
|
61
|
+
set messageHandler(n) {
|
|
62
|
+
this._messageHandler = n;
|
|
63
|
+
}
|
|
64
|
+
async onMessage(n) {
|
|
65
|
+
await this._messageHandler?.(n);
|
|
66
|
+
}
|
|
67
|
+
closeConnection() {
|
|
68
|
+
this._connectionLostHandler?.(), this.connectionStatus = s.DISCONNECTED;
|
|
69
|
+
}
|
|
70
|
+
initLogger(n) {
|
|
71
|
+
this.logger = new i(this.constructor.name, n);
|
|
72
|
+
}
|
|
73
|
+
destroy() {
|
|
74
|
+
this.closeConnection();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export {
|
|
78
|
+
a as A,
|
|
79
|
+
s as C,
|
|
80
|
+
i as L,
|
|
81
|
+
e as a
|
|
82
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const s={CONNECTED:"connected",DISCONNECTED:"disconnected"},t={DEBUG:100,INFO:200,WARN:300,ERROR:400,NONE:9999};class o{constructor(n,e){this.name=n,this.level=e}debug(...n){this.printMessage(t.DEBUG,...n)}info(...n){this.printMessage(t.INFO,...n)}warn(...n){this.printMessage(t.WARN,...n)}error(...n){this.printMessage(t.ERROR,...n)}printMessage(n,...e){const i=this.getLogFn(n);this.level<=n&&i(`[${this.name}]`,...e)}getLogFn(n){switch(n){case t.ERROR:return console.error;case t.WARN:return console.warn;default:return console.log}}}class r{_messageHandler;_connectionHandler;_connectionLostHandler;_connectionStatus=s.DISCONNECTED;logger;get connectionStatus(){return this._connectionStatus}set connectionStatus(n){this._connectionStatus!==n&&(this._connectionStatus=n,this._connectionHandler?.(n))}set connectionHandler(n){this._connectionHandler=n}set connectionLostHandler(n){this._connectionLostHandler=n}set messageHandler(n){this._messageHandler=n}async onMessage(n){await this._messageHandler?.(n)}closeConnection(){this._connectionLostHandler?.(),this.connectionStatus=s.DISCONNECTED}initLogger(n){this.logger=new o(this.constructor.name,n)}destroy(){this.closeConnection()}}exports.AbstractRpcAdapter=r;exports.ConnectionStatus=s;exports.LogLevel=t;exports.Logger=o;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { L as t } from "./AbstractRpcAdapter-7gOLZtYD.js";
|
|
2
|
+
import { u as s } from "./uuid-DTlgoKBw.js";
|
|
3
|
+
class d {
|
|
4
|
+
_peerHandler;
|
|
5
|
+
_peerLostHandler;
|
|
6
|
+
logger;
|
|
7
|
+
set peerHandler(e) {
|
|
8
|
+
this._peerHandler = e;
|
|
9
|
+
}
|
|
10
|
+
set peerLostHandler(e) {
|
|
11
|
+
this._peerLostHandler = e;
|
|
12
|
+
}
|
|
13
|
+
addPeer(e) {
|
|
14
|
+
const r = s();
|
|
15
|
+
return this._peerHandler?.({ id: r, adapter: e }), r;
|
|
16
|
+
}
|
|
17
|
+
removePeer(e) {
|
|
18
|
+
this._peerLostHandler?.(e);
|
|
19
|
+
}
|
|
20
|
+
initLogger(e) {
|
|
21
|
+
this.logger = new t(this.constructor.name, e);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export {
|
|
25
|
+
d as A
|
|
26
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const t=require("./AbstractRpcAdapter-BPIitR0A.cjs"),s=require("./uuid-C2SCOpSM.cjs");class a{_peerHandler;_peerLostHandler;logger;set peerHandler(e){this._peerHandler=e}set peerLostHandler(e){this._peerLostHandler=e}addPeer(e){const r=s.uuid();return this._peerHandler?.({id:r,adapter:e}),r}removePeer(e){this._peerLostHandler?.(e)}initLogger(e){this.logger=new t.Logger(this.constructor.name,e)}}exports.AbstractRpcHubAdapter=a;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const i = { interval: 5e3, timeout: 1e4 };
|
|
2
|
+
function h(t) {
|
|
3
|
+
if (t !== !1)
|
|
4
|
+
return { ...i, ...t === !0 ? {} : t };
|
|
5
|
+
}
|
|
6
|
+
class r {
|
|
7
|
+
constructor(e, o) {
|
|
8
|
+
this.options = e, this.callbacks = o;
|
|
9
|
+
}
|
|
10
|
+
intervalId;
|
|
11
|
+
timeoutId;
|
|
12
|
+
start() {
|
|
13
|
+
this.stop(), this.intervalId = setInterval(() => this.beat(), this.options.interval);
|
|
14
|
+
}
|
|
15
|
+
acknowledge() {
|
|
16
|
+
clearTimeout(this.timeoutId), this.timeoutId = void 0;
|
|
17
|
+
}
|
|
18
|
+
stop() {
|
|
19
|
+
clearInterval(this.intervalId), this.acknowledge();
|
|
20
|
+
}
|
|
21
|
+
beat() {
|
|
22
|
+
this.callbacks.ping(), this.timeoutId ??= setTimeout(() => this.expire(), this.options.timeout);
|
|
23
|
+
}
|
|
24
|
+
expire() {
|
|
25
|
+
this.timeoutId = void 0, this.callbacks.onTimeout();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const s = {
|
|
29
|
+
PING: "anyrpc::ping",
|
|
30
|
+
PONG: "anyrpc::pong",
|
|
31
|
+
GOODBYE: "anyrpc::goodbye"
|
|
32
|
+
}, n = Object.values(s);
|
|
33
|
+
function a(t) {
|
|
34
|
+
return { jsonrpc: "2.0", method: t, params: [] };
|
|
35
|
+
}
|
|
36
|
+
function c(t) {
|
|
37
|
+
if (!(typeof t != "object" || t === null || !("method" in t)))
|
|
38
|
+
return l(t.method) ? t.method : void 0;
|
|
39
|
+
}
|
|
40
|
+
function l(t) {
|
|
41
|
+
return n.includes(t);
|
|
42
|
+
}
|
|
43
|
+
class u {
|
|
44
|
+
constructor(e, o) {
|
|
45
|
+
this.callbacks = o, e && (this.heartbeat = new r(e, {
|
|
46
|
+
ping: () => this.ping(),
|
|
47
|
+
onTimeout: () => this.callbacks.onTimeout()
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
heartbeat;
|
|
51
|
+
handle(e) {
|
|
52
|
+
switch (c(e)) {
|
|
53
|
+
case s.PING:
|
|
54
|
+
return this.send(s.PONG), !0;
|
|
55
|
+
case s.PONG:
|
|
56
|
+
return this.heartbeat?.acknowledge(), this.callbacks.onPong(), !0;
|
|
57
|
+
case s.GOODBYE:
|
|
58
|
+
return this.callbacks.onGoodbye(), !0;
|
|
59
|
+
default:
|
|
60
|
+
return !1;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
ping() {
|
|
64
|
+
this.send(s.PING);
|
|
65
|
+
}
|
|
66
|
+
sayGoodbye() {
|
|
67
|
+
this.send(s.GOODBYE);
|
|
68
|
+
}
|
|
69
|
+
startHeartbeat() {
|
|
70
|
+
this.heartbeat?.start();
|
|
71
|
+
}
|
|
72
|
+
stopHeartbeat() {
|
|
73
|
+
this.heartbeat?.stop();
|
|
74
|
+
}
|
|
75
|
+
send(e) {
|
|
76
|
+
this.callbacks.send(a(e));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
u as C,
|
|
81
|
+
h as r
|
|
82
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const i={interval:5e3,timeout:1e4};function r(t){if(t!==!1)return{...i,...t===!0?{}:t}}class n{constructor(e,o){this.options=e,this.callbacks=o}intervalId;timeoutId;start(){this.stop(),this.intervalId=setInterval(()=>this.beat(),this.options.interval)}acknowledge(){clearTimeout(this.timeoutId),this.timeoutId=void 0}stop(){clearInterval(this.intervalId),this.acknowledge()}beat(){this.callbacks.ping(),this.timeoutId??=setTimeout(()=>this.expire(),this.options.timeout)}expire(){this.timeoutId=void 0,this.callbacks.onTimeout()}}const s={PING:"anyrpc::ping",PONG:"anyrpc::pong",GOODBYE:"anyrpc::goodbye"},a=Object.values(s);function c(t){return{jsonrpc:"2.0",method:t,params:[]}}function l(t){if(!(typeof t!="object"||t===null||!("method"in t)))return h(t.method)?t.method:void 0}function h(t){return a.includes(t)}class u{constructor(e,o){this.callbacks=o,e&&(this.heartbeat=new n(e,{ping:()=>this.ping(),onTimeout:()=>this.callbacks.onTimeout()}))}heartbeat;handle(e){switch(l(e)){case s.PING:return this.send(s.PONG),!0;case s.PONG:return this.heartbeat?.acknowledge(),this.callbacks.onPong(),!0;case s.GOODBYE:return this.callbacks.onGoodbye(),!0;default:return!1}}ping(){this.send(s.PING)}sayGoodbye(){this.send(s.GOODBYE)}startHeartbeat(){this.heartbeat?.start()}stopHeartbeat(){this.heartbeat?.stop()}send(e){this.callbacks.send(c(e))}}exports.ControlProtocol=u;exports.resolveHeartbeatOptions=r;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const n=require("./rpc-Brf2MoZo.cjs"),o=require("./uuid-C2SCOpSM.cjs");function p(e,s){return{jsonrpc:"2.0",id:o.uuid(),method:e,params:[s]}}function a(e,s,t){if(!u(e,"method",s))return!1;const r=c(e);if(r?.type!==n.RpcMessageType.REQUEST)return!1;const{params:i}=r.message;return Array.isArray(i)&&i[0]===t}function l(e){return{jsonrpc:"2.0",id:e.id,result:null}}function R(e,s){if(!u(e,"id",s))return!1;const t=c(e);return t?.type===n.RpcMessageType.RESPONSE&&"result"in t.message&&t.message.result===null}function u(e,s,t){return typeof e=="object"&&e!==null&&e[s]===t}function c(e){try{return n.parseRpcMessage(e)}catch{return}}exports.createInitRequest=p;exports.createInitResponse=l;exports.isInitRequest=a;exports.isInitResponse=R;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { R as u, p as o } from "./rpc-DeJEGj4-.js";
|
|
2
|
+
import { u as c } from "./uuid-DTlgoKBw.js";
|
|
3
|
+
function f(e, s) {
|
|
4
|
+
return { jsonrpc: "2.0", id: c(), method: e, params: [s] };
|
|
5
|
+
}
|
|
6
|
+
function R(e, s, r) {
|
|
7
|
+
if (!i(e, "method", s))
|
|
8
|
+
return !1;
|
|
9
|
+
const n = a(e);
|
|
10
|
+
if (n?.type !== u.REQUEST)
|
|
11
|
+
return !1;
|
|
12
|
+
const { params: t } = n.message;
|
|
13
|
+
return Array.isArray(t) && t[0] === r;
|
|
14
|
+
}
|
|
15
|
+
function m(e) {
|
|
16
|
+
return { jsonrpc: "2.0", id: e.id, result: null };
|
|
17
|
+
}
|
|
18
|
+
function g(e, s) {
|
|
19
|
+
if (!i(e, "id", s))
|
|
20
|
+
return !1;
|
|
21
|
+
const r = a(e);
|
|
22
|
+
return r?.type === u.RESPONSE && "result" in r.message && r.message.result === null;
|
|
23
|
+
}
|
|
24
|
+
function i(e, s, r) {
|
|
25
|
+
return typeof e == "object" && e !== null && e[s] === r;
|
|
26
|
+
}
|
|
27
|
+
function a(e) {
|
|
28
|
+
try {
|
|
29
|
+
return o(e);
|
|
30
|
+
} catch {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
R as a,
|
|
36
|
+
m as b,
|
|
37
|
+
f as c,
|
|
38
|
+
g as i
|
|
39
|
+
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { A as d, C as n } from "./AbstractRpcAdapter-7gOLZtYD.js";
|
|
2
|
+
import { C as l, r as a } from "./ControlProtocol-0b5_21ZU.js";
|
|
3
|
+
import { b as c, a as p, c as g, i as u } from "./handshake-CZWqKvZe.js";
|
|
4
|
+
function i(o) {
|
|
5
|
+
return "data" in o ? o.data : void 0;
|
|
6
|
+
}
|
|
7
|
+
class r extends d {
|
|
8
|
+
ports;
|
|
9
|
+
awaitingRemote = !1;
|
|
10
|
+
control;
|
|
11
|
+
constructor(e) {
|
|
12
|
+
super(), this.control = new l(e, {
|
|
13
|
+
send: (t) => this.ports?.control?.postMessage(t),
|
|
14
|
+
onPong: () => this.markResponsive(),
|
|
15
|
+
onTimeout: () => this.markUnresponsive(),
|
|
16
|
+
onGoodbye: () => this.handleRemoteGoodbye()
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
sendMessage(e) {
|
|
20
|
+
this.ports?.data.postMessage(e);
|
|
21
|
+
}
|
|
22
|
+
attachPorts(e, { awaitRemote: t = !1 } = {}) {
|
|
23
|
+
this.ports && (this.closePorts(), this.closeConnection()), this.ports = e, e.data.addEventListener("message", (s) => void this.onMessage(i(s))), e.data.addEventListener(
|
|
24
|
+
"messageerror",
|
|
25
|
+
() => this.logger?.warn("Dropped a message that could not be deserialized")
|
|
26
|
+
), e.data.start(), e.control?.addEventListener("message", (s) => this.onControlMessage(i(s))), e.control?.start(), t ? (this.awaitingRemote = !0, this.control.ping()) : this.connectionStatus = n.CONNECTED, e.control && this.control.startHeartbeat();
|
|
27
|
+
}
|
|
28
|
+
onRemoteGoodbye() {
|
|
29
|
+
this.closePorts(), this.closeConnection();
|
|
30
|
+
}
|
|
31
|
+
destroy() {
|
|
32
|
+
this.control.sayGoodbye(), this.closePorts(), super.destroy();
|
|
33
|
+
}
|
|
34
|
+
onControlMessage(e) {
|
|
35
|
+
this.control.handle(e) || this.logger?.warn("Ignored an unknown control message", e);
|
|
36
|
+
}
|
|
37
|
+
handleRemoteGoodbye() {
|
|
38
|
+
this.logger?.debug("The remote side closed the connection"), this.onRemoteGoodbye();
|
|
39
|
+
}
|
|
40
|
+
markResponsive() {
|
|
41
|
+
this.connectionStatus !== n.CONNECTED && (this.logger?.info(
|
|
42
|
+
this.awaitingRemote ? "The remote side accepted the connection" : "The remote side is responding again"
|
|
43
|
+
), this.awaitingRemote = !1, this.connectionStatus = n.CONNECTED);
|
|
44
|
+
}
|
|
45
|
+
onRemoteUnresponsive() {
|
|
46
|
+
this.connectionStatus = n.DISCONNECTED;
|
|
47
|
+
}
|
|
48
|
+
markUnresponsive() {
|
|
49
|
+
this.connectionStatus === n.CONNECTED && (this.logger?.warn("The remote side did not answer the heartbeat in time"), this.onRemoteUnresponsive());
|
|
50
|
+
}
|
|
51
|
+
closePorts() {
|
|
52
|
+
this.control.stopHeartbeat(), this.awaitingRemote = !1, this.ports?.data.close(), this.ports?.control?.close(), this.ports = void 0;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
class b extends r {
|
|
56
|
+
constructor(e, t, s) {
|
|
57
|
+
super(a(s)), this.initMethodName = e, this.adapterName = t;
|
|
58
|
+
}
|
|
59
|
+
answeredInitId;
|
|
60
|
+
listenersAbortController;
|
|
61
|
+
connect() {
|
|
62
|
+
this.stopListening(), this.listenersAbortController = new AbortController(), this.listenForInitRequests(this.listenersAbortController.signal);
|
|
63
|
+
}
|
|
64
|
+
acceptInitRequest(e) {
|
|
65
|
+
if (!this.isExpectedInitRequest(e))
|
|
66
|
+
return !1;
|
|
67
|
+
if (e.id === this.answeredInitId)
|
|
68
|
+
return this.onRepeatedInitRequest(), !1;
|
|
69
|
+
this.answeredInitId = e.id;
|
|
70
|
+
const t = new MessageChannel(), s = new MessageChannel();
|
|
71
|
+
return this.attachPorts({ data: t.port1, control: s.port1 }, { awaitRemote: !0 }), this.postInitResponse(c(e), [t.port2, s.port2]), !0;
|
|
72
|
+
}
|
|
73
|
+
onRepeatedInitRequest() {
|
|
74
|
+
this.logger?.warn(
|
|
75
|
+
"Ignored a repeated handshake request that was already answered. If the connection never gets established, check the origin options on both sides."
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
isExpectedInitRequest(e) {
|
|
79
|
+
return p(e, this.initMethodName, this.adapterName);
|
|
80
|
+
}
|
|
81
|
+
stopListening() {
|
|
82
|
+
this.listenersAbortController?.abort(), this.listenersAbortController = void 0;
|
|
83
|
+
}
|
|
84
|
+
destroy() {
|
|
85
|
+
this.stopListening(), super.destroy();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
class C extends r {
|
|
89
|
+
constructor(e, t, s, h = !1) {
|
|
90
|
+
super(a(h)), this.initMethod = e, this.name = t, this.pollingInterval = s;
|
|
91
|
+
}
|
|
92
|
+
handshake;
|
|
93
|
+
connect() {
|
|
94
|
+
this.stopHandshake();
|
|
95
|
+
const e = g(this.initMethod, this.name), t = new AbortController();
|
|
96
|
+
this.listenForInitResponses(t.signal), this.handshake = {
|
|
97
|
+
request: e,
|
|
98
|
+
listeners: t,
|
|
99
|
+
pollingIntervalId: setInterval(() => this.postInitRequest(e), this.pollingInterval)
|
|
100
|
+
}, this.postInitRequest(e);
|
|
101
|
+
}
|
|
102
|
+
acceptInitResponse(e) {
|
|
103
|
+
this.acceptInitResponseData(e.data, e.ports);
|
|
104
|
+
}
|
|
105
|
+
acceptInitResponseData(e, t) {
|
|
106
|
+
this.isExpectedInitResponse(e) && this.completeHandshake(t);
|
|
107
|
+
}
|
|
108
|
+
isExpectedInitResponse(e) {
|
|
109
|
+
return this.handshake !== void 0 && u(e, this.handshake.request.id);
|
|
110
|
+
}
|
|
111
|
+
onRemoteGoodbye() {
|
|
112
|
+
super.onRemoteGoodbye(), this.connect();
|
|
113
|
+
}
|
|
114
|
+
completeHandshake([e, t]) {
|
|
115
|
+
if (!e || !t) {
|
|
116
|
+
this.logger?.warn("Ignored a handshake response without the expected message ports");
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
this.stopHandshake(), this.attachPorts({ data: e, control: t });
|
|
120
|
+
}
|
|
121
|
+
stopHandshake() {
|
|
122
|
+
this.handshake?.listeners.abort(), clearInterval(this.handshake?.pollingIntervalId), this.handshake = void 0;
|
|
123
|
+
}
|
|
124
|
+
destroy() {
|
|
125
|
+
this.stopHandshake(), super.destroy();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export {
|
|
129
|
+
b as A,
|
|
130
|
+
C as a,
|
|
131
|
+
r as b,
|
|
132
|
+
i as r
|
|
133
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const o=require("./AbstractRpcAdapter-BPIitR0A.cjs"),r=require("./ControlProtocol-BMvwQecN.cjs"),i=require("./handshake-CWtm9Y59.cjs");function a(n){return"data"in n?n.data:void 0}class h extends o.AbstractRpcAdapter{ports;awaitingRemote=!1;control;constructor(t){super(),this.control=new r.ControlProtocol(t,{send:e=>this.ports?.control?.postMessage(e),onPong:()=>this.markResponsive(),onTimeout:()=>this.markUnresponsive(),onGoodbye:()=>this.handleRemoteGoodbye()})}sendMessage(t){this.ports?.data.postMessage(t)}attachPorts(t,{awaitRemote:e=!1}={}){this.ports&&(this.closePorts(),this.closeConnection()),this.ports=t,t.data.addEventListener("message",s=>void this.onMessage(a(s))),t.data.addEventListener("messageerror",()=>this.logger?.warn("Dropped a message that could not be deserialized")),t.data.start(),t.control?.addEventListener("message",s=>this.onControlMessage(a(s))),t.control?.start(),e?(this.awaitingRemote=!0,this.control.ping()):this.connectionStatus=o.ConnectionStatus.CONNECTED,t.control&&this.control.startHeartbeat()}onRemoteGoodbye(){this.closePorts(),this.closeConnection()}destroy(){this.control.sayGoodbye(),this.closePorts(),super.destroy()}onControlMessage(t){this.control.handle(t)||this.logger?.warn("Ignored an unknown control message",t)}handleRemoteGoodbye(){this.logger?.debug("The remote side closed the connection"),this.onRemoteGoodbye()}markResponsive(){this.connectionStatus!==o.ConnectionStatus.CONNECTED&&(this.logger?.info(this.awaitingRemote?"The remote side accepted the connection":"The remote side is responding again"),this.awaitingRemote=!1,this.connectionStatus=o.ConnectionStatus.CONNECTED)}onRemoteUnresponsive(){this.connectionStatus=o.ConnectionStatus.DISCONNECTED}markUnresponsive(){this.connectionStatus===o.ConnectionStatus.CONNECTED&&(this.logger?.warn("The remote side did not answer the heartbeat in time"),this.onRemoteUnresponsive())}closePorts(){this.control.stopHeartbeat(),this.awaitingRemote=!1,this.ports?.data.close(),this.ports?.control?.close(),this.ports=void 0}}class c extends h{constructor(t,e,s){super(r.resolveHeartbeatOptions(s)),this.initMethodName=t,this.adapterName=e}answeredInitId;listenersAbortController;connect(){this.stopListening(),this.listenersAbortController=new AbortController,this.listenForInitRequests(this.listenersAbortController.signal)}acceptInitRequest(t){if(!this.isExpectedInitRequest(t))return!1;if(t.id===this.answeredInitId)return this.onRepeatedInitRequest(),!1;this.answeredInitId=t.id;const e=new MessageChannel,s=new MessageChannel;return this.attachPorts({data:e.port1,control:s.port1},{awaitRemote:!0}),this.postInitResponse(i.createInitResponse(t),[e.port2,s.port2]),!0}onRepeatedInitRequest(){this.logger?.warn("Ignored a repeated handshake request that was already answered. If the connection never gets established, check the origin options on both sides.")}isExpectedInitRequest(t){return i.isInitRequest(t,this.initMethodName,this.adapterName)}stopListening(){this.listenersAbortController?.abort(),this.listenersAbortController=void 0}destroy(){this.stopListening(),super.destroy()}}class l extends h{constructor(t,e,s,d=!1){super(r.resolveHeartbeatOptions(d)),this.initMethod=t,this.name=e,this.pollingInterval=s}handshake;connect(){this.stopHandshake();const t=i.createInitRequest(this.initMethod,this.name),e=new AbortController;this.listenForInitResponses(e.signal),this.handshake={request:t,listeners:e,pollingIntervalId:setInterval(()=>this.postInitRequest(t),this.pollingInterval)},this.postInitRequest(t)}acceptInitResponse(t){this.acceptInitResponseData(t.data,t.ports)}acceptInitResponseData(t,e){this.isExpectedInitResponse(t)&&this.completeHandshake(e)}isExpectedInitResponse(t){return this.handshake!==void 0&&i.isInitResponse(t,this.handshake.request.id)}onRemoteGoodbye(){super.onRemoteGoodbye(),this.connect()}completeHandshake([t,e]){if(!t||!e){this.logger?.warn("Ignored a handshake response without the expected message ports");return}this.stopHandshake(),this.attachPorts({data:t,control:e})}stopHandshake(){this.handshake?.listeners.abort(),clearInterval(this.handshake?.pollingIntervalId),this.handshake=void 0}destroy(){this.stopHandshake(),super.destroy()}}exports.AbstractMessagePortAdapter=h;exports.AbstractMessagePortClientAdapter=l;exports.AbstractMessagePortHostAdapter=c;exports.readMessageData=a;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const r={PARSE_ERROR:-32700,INVALID_REQUEST:-32600,METHOD_NOT_FOUND:-32601,INVALID_PARAMS:-32602,INTERNAL_ERROR:-32603,SERVER_ERROR:-32e3,TIMEOUT_ERROR:-31001,NOT_CONNECTED:-31002,CONNECTION_LOST:-31003},c={min:-32768,max:-32e3},f=[r.TIMEOUT_ERROR,r.NOT_CONNECTED,r.CONNECTION_LOST],R={[r.PARSE_ERROR]:"Parse error",[r.INVALID_REQUEST]:"Invalid Request",[r.METHOD_NOT_FOUND]:"Method not found",[r.INVALID_PARAMS]:"Invalid params",[r.INTERNAL_ERROR]:"Internal error",[r.SERVER_ERROR]:"Server error",[r.TIMEOUT_ERROR]:"No response received within the configured timeout",[r.NOT_CONNECTED]:"No connection has been established yet",[r.CONNECTION_LOST]:"The connection was closed before a response was received"};class u extends Error{constructor(s,t=!1){var n=(...i)=>(super(...i),this.includeStack=t,this);typeof s=="number"?(n(R[s]),this.code=s):(n(s.message),this.code=s.code,this.message=s.message,this.data=s.data)}code;data;toJSON(){return{code:this.code,message:this.message,data:this.data instanceof Error?{message:this.data.message,name:this.data.name,stack:this.includeStack?this.data.stack:void 0}:this.data}}}class O extends u{reason;requestId;isResponse;constructor(s,t){super({code:s,message:R[s],data:{reason:t.reason}}),this.reason=t.reason,this.requestId=t.requestId,this.isResponse=t.isResponse}}var d=(e=>(e[e.REQUEST=0]="REQUEST",e[e.NOTIFICATION=1]="NOTIFICATION",e[e.RESPONSE=2]="RESPONSE",e))(d||{});const _="rpc.";function m(e){const s=typeof e=="string"?I(e):e,t=o=>{throw new O(r.INVALID_REQUEST,{reason:o,requestId:S(s),isResponse:E(s)})};Array.isArray(s)&&t("batch requests are not supported"),a(s)||t("message must be a JSON object"),l(s,t);const n=Object.hasOwn(s,"method"),i=E(s);if(n&&i&&t('message must not contain "method" together with "result" or "error"'),n){const o=T(s,t);return Object.hasOwn(o,"id")?{type:0,message:o}:{type:1,message:o}}return i?(b(s,t),{type:2,message:s}):t('message must contain either "method" or "result"/"error"')}function I(e){try{return JSON.parse(e)}catch{throw new O(r.PARSE_ERROR,{reason:"message is not valid JSON",requestId:null,isResponse:!1})}}function a(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function h(e){return typeof e=="string"||e===null||Number.isInteger(e)}function S(e){return a(e)&&h(e.id)?e.id:null}function E(e){return a(e)&&(Object.hasOwn(e,"result")||Object.hasOwn(e,"error"))}function l(e,s){e.jsonrpc!=="2.0"&&s('"jsonrpc" must be exactly "2.0"'),Object.hasOwn(e,"id")&&!h(e.id)&&s('"id" must be a string, an integer or null')}function T(e,s){typeof e.method!="string"&&s('"method" must be a string');const t=Object.hasOwn(e,"params")?e.params:[];return!Array.isArray(t)&&!a(t)&&s('"params" must be an array or an object'),{...e,params:t}}function b(e,s){Object.hasOwn(e,"id")||s('responses must contain an "id"'),N(e)&&(Object.hasOwn(e,"result")&&s('responses must not contain both "result" and "error"'),A(e.error,s))}function A(e,s){a(e)||s('"error" must be an object'),Number.isInteger(e.code)||s('"error.code" must be an integer'),typeof e.message!="string"&&s('"error.message" must be a string')}function N(e){return Object.hasOwn(e,"error")}function p(e){return typeof e=="string"&&e.startsWith(_)}function g(e){return!(e>=c.min&&e<=c.max)&&!f.includes(e)}function C(e,s){if(p(e))throw new Error(`Cannot ${s} "${String(e)}": method names starting with "rpc." are reserved by JSON-RPC 2.0`)}exports.RPC_ERROR_MESSAGE_MAP=R;exports.RpcError=u;exports.RpcErrorCode=r;exports.RpcMessageType=d;exports.assertNotReserved=C;exports.isApplicationErrorCode=g;exports.isReservedMethodName=p;exports.isRpcErrorResponse=N;exports.parseRpcMessage=m;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
const r = {
|
|
2
|
+
PARSE_ERROR: -32700,
|
|
3
|
+
INVALID_REQUEST: -32600,
|
|
4
|
+
METHOD_NOT_FOUND: -32601,
|
|
5
|
+
INVALID_PARAMS: -32602,
|
|
6
|
+
INTERNAL_ERROR: -32603,
|
|
7
|
+
SERVER_ERROR: -32e3,
|
|
8
|
+
TIMEOUT_ERROR: -31001,
|
|
9
|
+
NOT_CONNECTED: -31002,
|
|
10
|
+
CONNECTION_LOST: -31003
|
|
11
|
+
}, c = { min: -32768, max: -32e3 }, d = [
|
|
12
|
+
r.TIMEOUT_ERROR,
|
|
13
|
+
r.NOT_CONNECTED,
|
|
14
|
+
r.CONNECTION_LOST
|
|
15
|
+
], u = {
|
|
16
|
+
[r.PARSE_ERROR]: "Parse error",
|
|
17
|
+
[r.INVALID_REQUEST]: "Invalid Request",
|
|
18
|
+
[r.METHOD_NOT_FOUND]: "Method not found",
|
|
19
|
+
[r.INVALID_PARAMS]: "Invalid params",
|
|
20
|
+
[r.INTERNAL_ERROR]: "Internal error",
|
|
21
|
+
[r.SERVER_ERROR]: "Server error",
|
|
22
|
+
[r.TIMEOUT_ERROR]: "No response received within the configured timeout",
|
|
23
|
+
[r.NOT_CONNECTED]: "No connection has been established yet",
|
|
24
|
+
[r.CONNECTION_LOST]: "The connection was closed before a response was received"
|
|
25
|
+
};
|
|
26
|
+
class h extends Error {
|
|
27
|
+
constructor(s, t = !1) {
|
|
28
|
+
var n = (...i) => (super(...i), this.includeStack = t, this);
|
|
29
|
+
typeof s == "number" ? (n(u[s]), this.code = s) : (n(s.message), this.code = s.code, this.message = s.message, this.data = s.data);
|
|
30
|
+
}
|
|
31
|
+
code;
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
data;
|
|
34
|
+
toJSON() {
|
|
35
|
+
return {
|
|
36
|
+
code: this.code,
|
|
37
|
+
message: this.message,
|
|
38
|
+
data: this.data instanceof Error ? {
|
|
39
|
+
message: this.data.message,
|
|
40
|
+
name: this.data.name,
|
|
41
|
+
stack: this.includeStack ? this.data.stack : void 0
|
|
42
|
+
} : this.data
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
class E extends h {
|
|
47
|
+
reason;
|
|
48
|
+
requestId;
|
|
49
|
+
isResponse;
|
|
50
|
+
constructor(s, t) {
|
|
51
|
+
super({ code: s, message: u[s], data: { reason: t.reason } }), this.reason = t.reason, this.requestId = t.requestId, this.isResponse = t.isResponse;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
var N = /* @__PURE__ */ ((e) => (e[e.REQUEST = 0] = "REQUEST", e[e.NOTIFICATION = 1] = "NOTIFICATION", e[e.RESPONSE = 2] = "RESPONSE", e))(N || {});
|
|
55
|
+
const f = "rpc.";
|
|
56
|
+
function A(e) {
|
|
57
|
+
const s = typeof e == "string" ? p(e) : e, t = (o) => {
|
|
58
|
+
throw new E(r.INVALID_REQUEST, {
|
|
59
|
+
reason: o,
|
|
60
|
+
requestId: m(s),
|
|
61
|
+
isResponse: R(s)
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
Array.isArray(s) && t("batch requests are not supported"), a(s) || t("message must be a JSON object"), I(s, t);
|
|
65
|
+
const n = Object.hasOwn(s, "method"), i = R(s);
|
|
66
|
+
if (n && i && t('message must not contain "method" together with "result" or "error"'), n) {
|
|
67
|
+
const o = _(s, t);
|
|
68
|
+
return Object.hasOwn(o, "id") ? { type: 0, message: o } : { type: 1, message: o };
|
|
69
|
+
}
|
|
70
|
+
return i ? (S(s, t), { type: 2, message: s }) : t('message must contain either "method" or "result"/"error"');
|
|
71
|
+
}
|
|
72
|
+
function p(e) {
|
|
73
|
+
try {
|
|
74
|
+
return JSON.parse(e);
|
|
75
|
+
} catch {
|
|
76
|
+
throw new E(r.PARSE_ERROR, {
|
|
77
|
+
reason: "message is not valid JSON",
|
|
78
|
+
requestId: null,
|
|
79
|
+
isResponse: !1
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function a(e) {
|
|
84
|
+
return typeof e == "object" && e !== null && !Array.isArray(e);
|
|
85
|
+
}
|
|
86
|
+
function O(e) {
|
|
87
|
+
return typeof e == "string" || e === null || Number.isInteger(e);
|
|
88
|
+
}
|
|
89
|
+
function m(e) {
|
|
90
|
+
return a(e) && O(e.id) ? e.id : null;
|
|
91
|
+
}
|
|
92
|
+
function R(e) {
|
|
93
|
+
return a(e) && (Object.hasOwn(e, "result") || Object.hasOwn(e, "error"));
|
|
94
|
+
}
|
|
95
|
+
function I(e, s) {
|
|
96
|
+
e.jsonrpc !== "2.0" && s('"jsonrpc" must be exactly "2.0"'), Object.hasOwn(e, "id") && !O(e.id) && s('"id" must be a string, an integer or null');
|
|
97
|
+
}
|
|
98
|
+
function _(e, s) {
|
|
99
|
+
typeof e.method != "string" && s('"method" must be a string');
|
|
100
|
+
const t = Object.hasOwn(e, "params") ? e.params : [];
|
|
101
|
+
return !Array.isArray(t) && !a(t) && s('"params" must be an array or an object'), { ...e, params: t };
|
|
102
|
+
}
|
|
103
|
+
function S(e, s) {
|
|
104
|
+
Object.hasOwn(e, "id") || s('responses must contain an "id"'), l(e) && (Object.hasOwn(e, "result") && s('responses must not contain both "result" and "error"'), b(e.error, s));
|
|
105
|
+
}
|
|
106
|
+
function b(e, s) {
|
|
107
|
+
a(e) || s('"error" must be an object'), Number.isInteger(e.code) || s('"error.code" must be an integer'), typeof e.message != "string" && s('"error.message" must be a string');
|
|
108
|
+
}
|
|
109
|
+
function l(e) {
|
|
110
|
+
return Object.hasOwn(e, "error");
|
|
111
|
+
}
|
|
112
|
+
function T(e) {
|
|
113
|
+
return typeof e == "string" && e.startsWith(f);
|
|
114
|
+
}
|
|
115
|
+
function g(e) {
|
|
116
|
+
return !(e >= c.min && e <= c.max) && !d.includes(e);
|
|
117
|
+
}
|
|
118
|
+
function w(e, s) {
|
|
119
|
+
if (T(e))
|
|
120
|
+
throw new Error(
|
|
121
|
+
`Cannot ${s} "${String(e)}": method names starting with "rpc." are reserved by JSON-RPC 2.0`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
N as R,
|
|
126
|
+
h as a,
|
|
127
|
+
r as b,
|
|
128
|
+
u as c,
|
|
129
|
+
T as d,
|
|
130
|
+
g as e,
|
|
131
|
+
w as f,
|
|
132
|
+
l as i,
|
|
133
|
+
A as p
|
|
134
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";function x(){return typeof globalThis.crypto.randomUUID=="function"?globalThis.crypto.randomUUID():`xxxxxxxx-xxxx-4xxx-${(Math.floor(Math.random()*4)+8).toString(16)}xxx-xxxxxxxxxxxx`.replaceAll("x",()=>Math.floor(Math.random()*16).toString(16))}exports.uuid=x;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
function o() {
|
|
2
|
+
return typeof globalThis.crypto.randomUUID == "function" ? globalThis.crypto.randomUUID() : `xxxxxxxx-xxxx-4xxx-${(Math.floor(Math.random() * 4) + 8).toString(16)}xxx-xxxxxxxxxxxx`.replaceAll(
|
|
3
|
+
"x",
|
|
4
|
+
() => Math.floor(Math.random() * 16).toString(16)
|
|
5
|
+
);
|
|
6
|
+
}
|
|
7
|
+
export {
|
|
8
|
+
o as u
|
|
9
|
+
};
|