@xiaohhhh1/canvas-agent 0.4.9 → 0.4.11
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/relay-bridge.js +101 -24
- package/dist/server/supervisor.d.ts +1 -1
- package/dist/server/supervisor.js +1 -1
- package/package.json +1 -1
package/dist/relay-bridge.js
CHANGED
|
@@ -2,6 +2,8 @@ import WebSocket from "ws";
|
|
|
2
2
|
const DEFAULT_RELAY_URL = "wss://canvas.xiaohhhh1.com/api/agent-relay";
|
|
3
3
|
const RECONNECT_DELAY_MS = 3_000;
|
|
4
4
|
const HEARTBEAT_INTERVAL_MS = 15_000;
|
|
5
|
+
const READY_TIMEOUT_MS = 12_000;
|
|
6
|
+
const LIVENESS_TIMEOUT_MS = 45_000;
|
|
5
7
|
/**
|
|
6
8
|
* Keeps an outbound, encrypted connection to the production relay. The canvas
|
|
7
9
|
* browser can then use same-origin requests instead of directly reaching a
|
|
@@ -12,11 +14,35 @@ export function startRelayBridge(config) {
|
|
|
12
14
|
const subscriptions = new Map();
|
|
13
15
|
let socket = null;
|
|
14
16
|
let stopped = false;
|
|
17
|
+
let relayReady = false;
|
|
18
|
+
let lastHeartbeatAck = 0;
|
|
15
19
|
let reconnectTimer = null;
|
|
16
20
|
let heartbeatTimer = null;
|
|
21
|
+
let readyTimer = null;
|
|
17
22
|
const send = (message) => {
|
|
18
|
-
|
|
19
|
-
|
|
23
|
+
const current = socket;
|
|
24
|
+
if (!relayReady || current?.readyState !== WebSocket.OPEN)
|
|
25
|
+
return false;
|
|
26
|
+
try {
|
|
27
|
+
current.send(JSON.stringify(message));
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
current.terminate();
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const clearConnectionTimers = () => {
|
|
36
|
+
if (heartbeatTimer)
|
|
37
|
+
clearInterval(heartbeatTimer);
|
|
38
|
+
if (readyTimer)
|
|
39
|
+
clearTimeout(readyTimer);
|
|
40
|
+
heartbeatTimer = null;
|
|
41
|
+
readyTimer = null;
|
|
42
|
+
};
|
|
43
|
+
const abortSubscriptions = () => {
|
|
44
|
+
subscriptions.forEach((controller) => controller.abort());
|
|
45
|
+
subscriptions.clear();
|
|
20
46
|
};
|
|
21
47
|
const stopSubscription = (clientId) => {
|
|
22
48
|
subscriptions.get(clientId)?.abort();
|
|
@@ -67,31 +93,83 @@ export function startRelayBridge(config) {
|
|
|
67
93
|
// Ignore malformed relay messages. The relay never receives a secret in an error response.
|
|
68
94
|
}
|
|
69
95
|
};
|
|
96
|
+
const scheduleReconnect = () => {
|
|
97
|
+
if (stopped || reconnectTimer)
|
|
98
|
+
return;
|
|
99
|
+
reconnectTimer = setTimeout(() => {
|
|
100
|
+
reconnectTimer = null;
|
|
101
|
+
connect();
|
|
102
|
+
}, RECONNECT_DELAY_MS);
|
|
103
|
+
};
|
|
70
104
|
const connect = () => {
|
|
71
|
-
if (stopped)
|
|
105
|
+
if (stopped || socket)
|
|
72
106
|
return;
|
|
73
107
|
try {
|
|
74
|
-
|
|
75
|
-
socket
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
108
|
+
const current = new WebSocket(relayUrl);
|
|
109
|
+
socket = current;
|
|
110
|
+
let disconnected = false;
|
|
111
|
+
const disconnect = () => {
|
|
112
|
+
if (disconnected)
|
|
113
|
+
return;
|
|
114
|
+
disconnected = true;
|
|
115
|
+
if (socket === current)
|
|
116
|
+
socket = null;
|
|
117
|
+
relayReady = false;
|
|
118
|
+
clearConnectionTimers();
|
|
119
|
+
abortSubscriptions();
|
|
120
|
+
scheduleReconnect();
|
|
121
|
+
};
|
|
122
|
+
current.on("open", () => {
|
|
123
|
+
try {
|
|
124
|
+
current.send(JSON.stringify({ type: "hello", role: "agent", token: config.token }));
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
current.terminate();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
readyTimer = setTimeout(() => current.terminate(), READY_TIMEOUT_MS);
|
|
80
131
|
});
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
132
|
+
current.on("message", (raw) => {
|
|
133
|
+
try {
|
|
134
|
+
const message = JSON.parse(raw.toString());
|
|
135
|
+
if (message.type === "ready") {
|
|
136
|
+
relayReady = true;
|
|
137
|
+
lastHeartbeatAck = Date.now();
|
|
138
|
+
if (readyTimer)
|
|
139
|
+
clearTimeout(readyTimer);
|
|
140
|
+
readyTimer = null;
|
|
141
|
+
if (heartbeatTimer)
|
|
142
|
+
clearInterval(heartbeatTimer);
|
|
143
|
+
heartbeatTimer = setInterval(() => {
|
|
144
|
+
if (Date.now() - lastHeartbeatAck > LIVENESS_TIMEOUT_MS) {
|
|
145
|
+
current.terminate();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
send({ type: "heartbeat", time: Date.now() });
|
|
149
|
+
}, HEARTBEAT_INTERVAL_MS);
|
|
150
|
+
heartbeatTimer.unref();
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (message.type === "heartbeat_ack") {
|
|
154
|
+
lastHeartbeatAck = Date.now();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (!relayReady)
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
onMessage(raw);
|
|
90
164
|
});
|
|
91
|
-
|
|
165
|
+
current.on("close", disconnect);
|
|
166
|
+
current.on("error", () => current.terminate());
|
|
92
167
|
}
|
|
93
168
|
catch {
|
|
94
|
-
|
|
169
|
+
socket = null;
|
|
170
|
+
relayReady = false;
|
|
171
|
+
clearConnectionTimers();
|
|
172
|
+
scheduleReconnect();
|
|
95
173
|
}
|
|
96
174
|
};
|
|
97
175
|
connect();
|
|
@@ -99,11 +177,10 @@ export function startRelayBridge(config) {
|
|
|
99
177
|
stopped = true;
|
|
100
178
|
if (reconnectTimer)
|
|
101
179
|
clearTimeout(reconnectTimer);
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
subscriptions.forEach((controller) => controller.abort());
|
|
105
|
-
subscriptions.clear();
|
|
180
|
+
clearConnectionTimers();
|
|
181
|
+
abortSubscriptions();
|
|
106
182
|
socket?.close();
|
|
183
|
+
socket = null;
|
|
107
184
|
};
|
|
108
185
|
}
|
|
109
186
|
async function pipeEvents(clientId, config, signal, send) {
|
|
@@ -2,4 +2,4 @@ export declare const AGENT_REPLACED_EXIT_CODE = 75;
|
|
|
2
2
|
export declare function shouldRestartAgent(code: number | null, stopping: boolean): boolean;
|
|
3
3
|
export declare function shouldReplaceSupervisorLock(ownerPid: number, ageMs: number, ownerAlive: boolean): boolean;
|
|
4
4
|
/** Keep the local HTTP worker alive, but step aside when a newer package replaces it. */
|
|
5
|
-
export declare function startHttpSupervisor():
|
|
5
|
+
export declare function startHttpSupervisor(): undefined;
|
|
@@ -91,7 +91,7 @@ export function startHttpSupervisor() {
|
|
|
91
91
|
throw new Error("无法定位 Canvas Agent 启动文件");
|
|
92
92
|
const lockDescriptor = acquireSupervisorLock();
|
|
93
93
|
if (lockDescriptor === undefined)
|
|
94
|
-
return;
|
|
94
|
+
return void process.exit(0);
|
|
95
95
|
let child;
|
|
96
96
|
let stopping = false;
|
|
97
97
|
let restartTimer;
|