scope-agent-workbench 0.2.0 → 0.2.3
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/CHANGELOG.md +41 -0
- package/bridge/agent-email-relay.mjs +160 -160
- package/bridge/app-server-websocket-transport.mjs +219 -219
- package/bridge/archive/service.mjs +130 -130
- package/bridge/auth-profile-failover.mjs +58 -58
- package/bridge/auth-profile-manager.mjs +215 -215
- package/bridge/capacity-recovery.mjs +90 -90
- package/bridge/gpu-usage-history.mjs +322 -322
- package/bridge/local/history.mjs +20 -5
- package/bridge/plugins/aliyun-dlc/monitor.mjs +236 -236
- package/bridge/plugins/aliyun-dlc/probe.mjs +701 -701
- package/bridge/private-gateway.mjs +371 -371
- package/bridge/private-start.mjs +22 -22
- package/bridge/probes/app-server-handoff-probe.mjs +87 -87
- package/bridge/probes/app-server-writer-probe.mjs +69 -69
- package/bridge/probes/assistant-search-probe.mjs +321 -321
- package/bridge/probes/dialogue-probe.mjs +462 -462
- package/bridge/probes/history-probe.mjs +430 -345
- package/bridge/probes/new-session-preflight.mjs +14 -14
- package/bridge/probes/profile-cancel-probe.mjs +17 -17
- package/bridge/probes/profile-delete-probe.mjs +26 -26
- package/bridge/probes/profile-list-probe.mjs +114 -114
- package/bridge/probes/profile-write-probe.mjs +109 -109
- package/bridge/probes/raw-context-probe.mjs +51 -51
- package/bridge/probes/remote-probe.mjs +627 -626
- package/bridge/probes/submission-probe.mjs +2 -2
- package/bridge/probes/takeover-probe.mjs +326 -326
- package/bridge/probes/vscode-open-preflight.mjs +21 -21
- package/bridge/providers/claude-interactive.py +463 -341
- package/bridge/providers/cli-command.mjs +103 -103
- package/bridge/providers/conversations.mjs +231 -231
- package/bridge/providers/native-commands.py +38 -38
- package/bridge/providers/native-session-state.mjs +14 -14
- package/bridge/providers/native-session-state.py +96 -96
- package/bridge/providers/remote-job.py +130 -130
- package/bridge/remote-app-server-command.mjs +121 -121
- package/bridge/remote-directory-listing.mjs +148 -148
- package/bridge/rollout-summary.mjs +20 -2
- package/bridge/routes/dialogue.mjs +48 -3
- package/bridge/routes/models.mjs +312 -312
- package/bridge/server.mjs +3357 -3332
- package/bridge/session-attachments.mjs +150 -150
- package/bridge/session-catalog/agy-history.py +141 -2
- package/bridge/session-catalog/catalog.mjs +17 -0
- package/bridge/session-catalog/probe.py +1207 -331
- package/bridge/session-catalog/routes.mjs +6 -0
- package/bridge/start.mjs +205 -205
- package/bridge/state-persistence.mjs +9 -1
- package/bridge/tmux-session-control.mjs +29 -29
- package/bridge/vscode-opener.mjs +157 -157
- package/bridge/web-terminal.mjs +52 -52
- package/bridge/workbench/manager-context.mjs +51 -51
- package/bridge/workbench/service.mjs +301 -301
- package/dist/client/.vite/manifest.json +4 -4
- package/dist/client/assets/{index-Enu1x0PS.js → index-BMior55c.js} +2 -2
- package/dist/client/assets/{layout-segment-context-CmJzExWq.js → layout-segment-context-DjGudgXn.js} +1 -1
- package/dist/client/assets/page-CJqXlhDs.js +551 -0
- package/dist/client/assets/page-LYzRz6_S.css +1 -0
- package/dist/client/favicon.svg +6 -6
- package/dist/server/__vite_rsc_assets_manifest.js +8 -8
- package/dist/server/index.js +7 -7
- package/dist/server/ssr/__vite_rsc_assets_manifest.js +8 -8
- package/dist/server/ssr/assets/{page-CYYyCnKD.js → page-BB9Dri2u.js} +2416 -271
- package/dist/server/ssr/assets/page-LYzRz6_S.css +1 -0
- package/dist/server/ssr/index.js +1 -1
- package/dist/server/ssr/vinext-server.json +1 -1
- package/dist/server/vinext-server.json +1 -1
- package/dist/server/wrangler.json +1 -1
- package/lib/browser-snapshot-cache.js +53 -53
- package/lib/formula-preview.js +1 -1
- package/lib/incremental-snapshot.js +300 -300
- package/lib/markdown-math.js +151 -151
- package/lib/remote-links.js +95 -95
- package/lib/runtime-config.js +70 -70
- package/lib/session-filters.js +38 -38
- package/lib/ssh-config.js +26 -26
- package/lib/stable-order.js +39 -39
- package/lib/subagent-display-prefs.ts +130 -0
- package/package.json +1 -1
- package/scripts/agent-email-relay.mjs +104 -104
- package/scripts/email-notification-cli.mjs +28 -28
- package/scripts/enable-private-access.ps1 +109 -109
- package/scripts/fixtures/native-conversations.mjs +164 -6
- package/scripts/fixtures/workbench-conversations.mjs +475 -352
- package/scripts/preview-workbench.mjs +669 -669
- package/dist/client/assets/page-COBv6VWj.css +0 -1
- package/dist/client/assets/page-DVjU6VG_.js +0 -550
- package/dist/server/ssr/assets/page-COBv6VWj.css +0 -1
|
@@ -1,219 +1,219 @@
|
|
|
1
|
-
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
-
import { EventEmitter } from "node:events";
|
|
3
|
-
import { PassThrough, Writable } from "node:stream";
|
|
4
|
-
|
|
5
|
-
const WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
6
|
-
|
|
7
|
-
function frame(opcode, payload = Buffer.alloc(0)) {
|
|
8
|
-
const body = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
9
|
-
const mask = randomBytes(4);
|
|
10
|
-
let header;
|
|
11
|
-
if (body.length < 126) {
|
|
12
|
-
header = Buffer.alloc(2);
|
|
13
|
-
header[1] = 0x80 | body.length;
|
|
14
|
-
} else if (body.length <= 0xffff) {
|
|
15
|
-
header = Buffer.alloc(4);
|
|
16
|
-
header[1] = 0x80 | 126;
|
|
17
|
-
header.writeUInt16BE(body.length, 2);
|
|
18
|
-
} else {
|
|
19
|
-
header = Buffer.alloc(10);
|
|
20
|
-
header[1] = 0x80 | 127;
|
|
21
|
-
header.writeBigUInt64BE(BigInt(body.length), 2);
|
|
22
|
-
}
|
|
23
|
-
header[0] = 0x80 | opcode;
|
|
24
|
-
const masked = Buffer.alloc(body.length);
|
|
25
|
-
for (let index = 0; index < body.length; index += 1) {
|
|
26
|
-
masked[index] = body[index] ^ mask[index % 4];
|
|
27
|
-
}
|
|
28
|
-
return Buffer.concat([header, mask, masked]);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export class WebSocketJsonlChild extends EventEmitter {
|
|
32
|
-
constructor(
|
|
33
|
-
child,
|
|
34
|
-
{
|
|
35
|
-
path = "/",
|
|
36
|
-
host = "localhost",
|
|
37
|
-
pingIntervalMs = 25_000,
|
|
38
|
-
setIntervalFn = setInterval,
|
|
39
|
-
clearIntervalFn = clearInterval,
|
|
40
|
-
} = {},
|
|
41
|
-
) {
|
|
42
|
-
super();
|
|
43
|
-
this.child = child;
|
|
44
|
-
this.stdout = new PassThrough();
|
|
45
|
-
this.stderr = child.stderr;
|
|
46
|
-
this.killed = false;
|
|
47
|
-
this.ready = false;
|
|
48
|
-
this.closed = false;
|
|
49
|
-
this.buffer = Buffer.alloc(0);
|
|
50
|
-
this.fragments = [];
|
|
51
|
-
this.pendingWrites = [];
|
|
52
|
-
this.pingIntervalMs = Math.max(0, Number(pingIntervalMs) || 0);
|
|
53
|
-
this.setIntervalFn = setIntervalFn;
|
|
54
|
-
this.clearIntervalFn = clearIntervalFn;
|
|
55
|
-
this.pingTimer = null;
|
|
56
|
-
// Lets JsonlAppServerConnection distinguish this disposable SSH/WebSocket
|
|
57
|
-
// bridge from an actual remote app-server process exit.
|
|
58
|
-
this.scopeTransport = "ssh-websocket";
|
|
59
|
-
this.key = randomBytes(16).toString("base64");
|
|
60
|
-
this.stdin = new Writable({
|
|
61
|
-
write: (chunk, _encoding, callback) => {
|
|
62
|
-
try {
|
|
63
|
-
const payload = Buffer.from(chunk);
|
|
64
|
-
if (this.ready) this.child.stdin.write(frame(0x1, payload));
|
|
65
|
-
else this.pendingWrites.push(payload);
|
|
66
|
-
callback();
|
|
67
|
-
} catch (error) {
|
|
68
|
-
callback(error);
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
final: (callback) => {
|
|
72
|
-
try {
|
|
73
|
-
this.#stopHeartbeat();
|
|
74
|
-
if (!this.child.stdin.destroyed) {
|
|
75
|
-
if (this.ready) this.child.stdin.write(frame(0x8));
|
|
76
|
-
this.child.stdin.end();
|
|
77
|
-
}
|
|
78
|
-
callback();
|
|
79
|
-
} catch (error) {
|
|
80
|
-
callback(error);
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
});
|
|
84
|
-
child.stdout.on("data", (chunk) => this.#read(chunk));
|
|
85
|
-
child.once("error", (error) => this.emit("error", error));
|
|
86
|
-
child.once("exit", (code, signal) => {
|
|
87
|
-
this.#stopHeartbeat();
|
|
88
|
-
this.closed = true;
|
|
89
|
-
this.stdout.end();
|
|
90
|
-
this.emit("exit", code, signal);
|
|
91
|
-
});
|
|
92
|
-
child.stdin.on("error", (error) => this.stdin.destroy(error));
|
|
93
|
-
const request = [
|
|
94
|
-
`GET ${path} HTTP/1.1`,
|
|
95
|
-
`Host: ${host}`,
|
|
96
|
-
"Upgrade: websocket",
|
|
97
|
-
"Connection: Upgrade",
|
|
98
|
-
`Sec-WebSocket-Key: ${this.key}`,
|
|
99
|
-
"Sec-WebSocket-Version: 13",
|
|
100
|
-
"",
|
|
101
|
-
"",
|
|
102
|
-
].join("\r\n");
|
|
103
|
-
child.stdin.write(request);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
kill() {
|
|
107
|
-
// Compatibility with ChildProcess consumers. This intentionally closes the
|
|
108
|
-
// proxy stream instead of signalling the durable remote app-server.
|
|
109
|
-
this.killed = true;
|
|
110
|
-
this.#stopHeartbeat();
|
|
111
|
-
this.stdin.end();
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
#read(chunk) {
|
|
115
|
-
this.buffer = Buffer.concat([this.buffer, Buffer.from(chunk)]);
|
|
116
|
-
if (!this.ready) {
|
|
117
|
-
const marker = this.buffer.indexOf("\r\n\r\n");
|
|
118
|
-
if (marker < 0) return;
|
|
119
|
-
const response = this.buffer.subarray(0, marker + 4).toString("utf8");
|
|
120
|
-
this.buffer = this.buffer.subarray(marker + 4);
|
|
121
|
-
const status = response.split("\r\n", 1)[0] || "";
|
|
122
|
-
const accept = response.match(/\r\nsec-websocket-accept:\s*([^\r\n]+)/i)?.[1]?.trim();
|
|
123
|
-
const expected = createHash("sha1")
|
|
124
|
-
.update(`${this.key}${WEBSOCKET_GUID}`)
|
|
125
|
-
.digest("base64");
|
|
126
|
-
if (!/^HTTP\/1\.[01] 101\b/.test(status) || accept !== expected) {
|
|
127
|
-
this.emit(
|
|
128
|
-
"error",
|
|
129
|
-
new Error(`app-server WebSocket 握手失败:${status || "无响应"}`),
|
|
130
|
-
);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
this.ready = true;
|
|
134
|
-
this.#startHeartbeat();
|
|
135
|
-
for (const payload of this.pendingWrites.splice(0)) {
|
|
136
|
-
this.child.stdin.write(frame(0x1, payload));
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
this.#drainFrames();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
#drainFrames() {
|
|
143
|
-
while (this.buffer.length >= 2) {
|
|
144
|
-
const first = this.buffer[0];
|
|
145
|
-
const second = this.buffer[1];
|
|
146
|
-
const fin = Boolean(first & 0x80);
|
|
147
|
-
const opcode = first & 0x0f;
|
|
148
|
-
const masked = Boolean(second & 0x80);
|
|
149
|
-
let length = second & 0x7f;
|
|
150
|
-
let offset = 2;
|
|
151
|
-
if (length === 126) {
|
|
152
|
-
if (this.buffer.length < 4) return;
|
|
153
|
-
length = this.buffer.readUInt16BE(2);
|
|
154
|
-
offset = 4;
|
|
155
|
-
} else if (length === 127) {
|
|
156
|
-
if (this.buffer.length < 10) return;
|
|
157
|
-
const wide = this.buffer.readBigUInt64BE(2);
|
|
158
|
-
if (wide > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
159
|
-
this.emit("error", new Error("app-server WebSocket 帧过大"));
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
length = Number(wide);
|
|
163
|
-
offset = 10;
|
|
164
|
-
}
|
|
165
|
-
const maskOffset = offset;
|
|
166
|
-
if (masked) offset += 4;
|
|
167
|
-
if (this.buffer.length < offset + length) return;
|
|
168
|
-
const mask = masked
|
|
169
|
-
? Buffer.from(this.buffer.subarray(maskOffset, maskOffset + 4))
|
|
170
|
-
: null;
|
|
171
|
-
let payload = Buffer.from(this.buffer.subarray(offset, offset + length));
|
|
172
|
-
this.buffer = this.buffer.subarray(offset + length);
|
|
173
|
-
if (mask) {
|
|
174
|
-
for (let index = 0; index < payload.length; index += 1) {
|
|
175
|
-
payload[index] ^= mask[index % 4];
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (opcode === 0x8) {
|
|
179
|
-
this.#stopHeartbeat();
|
|
180
|
-
if (!this.child.stdin.destroyed) this.child.stdin.end(frame(0x8));
|
|
181
|
-
continue;
|
|
182
|
-
}
|
|
183
|
-
if (opcode === 0x9) {
|
|
184
|
-
if (!this.child.stdin.destroyed) this.child.stdin.write(frame(0xa, payload));
|
|
185
|
-
continue;
|
|
186
|
-
}
|
|
187
|
-
if (opcode === 0xa) continue;
|
|
188
|
-
if (opcode === 0x1) this.fragments = [payload];
|
|
189
|
-
else if (opcode === 0x0) this.fragments.push(payload);
|
|
190
|
-
else continue;
|
|
191
|
-
if (fin) {
|
|
192
|
-
this.stdout.write(Buffer.concat(this.fragments));
|
|
193
|
-
this.stdout.write("\n");
|
|
194
|
-
this.fragments = [];
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
#startHeartbeat() {
|
|
200
|
-
if (this.pingTimer || !this.ready || this.closed || !this.pingIntervalMs) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
this.pingTimer = this.setIntervalFn(() => {
|
|
204
|
-
if (!this.ready || this.closed || this.child.stdin.destroyed) return;
|
|
205
|
-
try {
|
|
206
|
-
this.child.stdin.write(frame(0x9));
|
|
207
|
-
} catch {
|
|
208
|
-
// The child exit/error handlers surface the real disconnect.
|
|
209
|
-
}
|
|
210
|
-
}, this.pingIntervalMs);
|
|
211
|
-
this.pingTimer?.unref?.();
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
#stopHeartbeat() {
|
|
215
|
-
if (!this.pingTimer) return;
|
|
216
|
-
this.clearIntervalFn(this.pingTimer);
|
|
217
|
-
this.pingTimer = null;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import { PassThrough, Writable } from "node:stream";
|
|
4
|
+
|
|
5
|
+
const WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
6
|
+
|
|
7
|
+
function frame(opcode, payload = Buffer.alloc(0)) {
|
|
8
|
+
const body = Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
|
|
9
|
+
const mask = randomBytes(4);
|
|
10
|
+
let header;
|
|
11
|
+
if (body.length < 126) {
|
|
12
|
+
header = Buffer.alloc(2);
|
|
13
|
+
header[1] = 0x80 | body.length;
|
|
14
|
+
} else if (body.length <= 0xffff) {
|
|
15
|
+
header = Buffer.alloc(4);
|
|
16
|
+
header[1] = 0x80 | 126;
|
|
17
|
+
header.writeUInt16BE(body.length, 2);
|
|
18
|
+
} else {
|
|
19
|
+
header = Buffer.alloc(10);
|
|
20
|
+
header[1] = 0x80 | 127;
|
|
21
|
+
header.writeBigUInt64BE(BigInt(body.length), 2);
|
|
22
|
+
}
|
|
23
|
+
header[0] = 0x80 | opcode;
|
|
24
|
+
const masked = Buffer.alloc(body.length);
|
|
25
|
+
for (let index = 0; index < body.length; index += 1) {
|
|
26
|
+
masked[index] = body[index] ^ mask[index % 4];
|
|
27
|
+
}
|
|
28
|
+
return Buffer.concat([header, mask, masked]);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class WebSocketJsonlChild extends EventEmitter {
|
|
32
|
+
constructor(
|
|
33
|
+
child,
|
|
34
|
+
{
|
|
35
|
+
path = "/",
|
|
36
|
+
host = "localhost",
|
|
37
|
+
pingIntervalMs = 25_000,
|
|
38
|
+
setIntervalFn = setInterval,
|
|
39
|
+
clearIntervalFn = clearInterval,
|
|
40
|
+
} = {},
|
|
41
|
+
) {
|
|
42
|
+
super();
|
|
43
|
+
this.child = child;
|
|
44
|
+
this.stdout = new PassThrough();
|
|
45
|
+
this.stderr = child.stderr;
|
|
46
|
+
this.killed = false;
|
|
47
|
+
this.ready = false;
|
|
48
|
+
this.closed = false;
|
|
49
|
+
this.buffer = Buffer.alloc(0);
|
|
50
|
+
this.fragments = [];
|
|
51
|
+
this.pendingWrites = [];
|
|
52
|
+
this.pingIntervalMs = Math.max(0, Number(pingIntervalMs) || 0);
|
|
53
|
+
this.setIntervalFn = setIntervalFn;
|
|
54
|
+
this.clearIntervalFn = clearIntervalFn;
|
|
55
|
+
this.pingTimer = null;
|
|
56
|
+
// Lets JsonlAppServerConnection distinguish this disposable SSH/WebSocket
|
|
57
|
+
// bridge from an actual remote app-server process exit.
|
|
58
|
+
this.scopeTransport = "ssh-websocket";
|
|
59
|
+
this.key = randomBytes(16).toString("base64");
|
|
60
|
+
this.stdin = new Writable({
|
|
61
|
+
write: (chunk, _encoding, callback) => {
|
|
62
|
+
try {
|
|
63
|
+
const payload = Buffer.from(chunk);
|
|
64
|
+
if (this.ready) this.child.stdin.write(frame(0x1, payload));
|
|
65
|
+
else this.pendingWrites.push(payload);
|
|
66
|
+
callback();
|
|
67
|
+
} catch (error) {
|
|
68
|
+
callback(error);
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
final: (callback) => {
|
|
72
|
+
try {
|
|
73
|
+
this.#stopHeartbeat();
|
|
74
|
+
if (!this.child.stdin.destroyed) {
|
|
75
|
+
if (this.ready) this.child.stdin.write(frame(0x8));
|
|
76
|
+
this.child.stdin.end();
|
|
77
|
+
}
|
|
78
|
+
callback();
|
|
79
|
+
} catch (error) {
|
|
80
|
+
callback(error);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
child.stdout.on("data", (chunk) => this.#read(chunk));
|
|
85
|
+
child.once("error", (error) => this.emit("error", error));
|
|
86
|
+
child.once("exit", (code, signal) => {
|
|
87
|
+
this.#stopHeartbeat();
|
|
88
|
+
this.closed = true;
|
|
89
|
+
this.stdout.end();
|
|
90
|
+
this.emit("exit", code, signal);
|
|
91
|
+
});
|
|
92
|
+
child.stdin.on("error", (error) => this.stdin.destroy(error));
|
|
93
|
+
const request = [
|
|
94
|
+
`GET ${path} HTTP/1.1`,
|
|
95
|
+
`Host: ${host}`,
|
|
96
|
+
"Upgrade: websocket",
|
|
97
|
+
"Connection: Upgrade",
|
|
98
|
+
`Sec-WebSocket-Key: ${this.key}`,
|
|
99
|
+
"Sec-WebSocket-Version: 13",
|
|
100
|
+
"",
|
|
101
|
+
"",
|
|
102
|
+
].join("\r\n");
|
|
103
|
+
child.stdin.write(request);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
kill() {
|
|
107
|
+
// Compatibility with ChildProcess consumers. This intentionally closes the
|
|
108
|
+
// proxy stream instead of signalling the durable remote app-server.
|
|
109
|
+
this.killed = true;
|
|
110
|
+
this.#stopHeartbeat();
|
|
111
|
+
this.stdin.end();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
#read(chunk) {
|
|
115
|
+
this.buffer = Buffer.concat([this.buffer, Buffer.from(chunk)]);
|
|
116
|
+
if (!this.ready) {
|
|
117
|
+
const marker = this.buffer.indexOf("\r\n\r\n");
|
|
118
|
+
if (marker < 0) return;
|
|
119
|
+
const response = this.buffer.subarray(0, marker + 4).toString("utf8");
|
|
120
|
+
this.buffer = this.buffer.subarray(marker + 4);
|
|
121
|
+
const status = response.split("\r\n", 1)[0] || "";
|
|
122
|
+
const accept = response.match(/\r\nsec-websocket-accept:\s*([^\r\n]+)/i)?.[1]?.trim();
|
|
123
|
+
const expected = createHash("sha1")
|
|
124
|
+
.update(`${this.key}${WEBSOCKET_GUID}`)
|
|
125
|
+
.digest("base64");
|
|
126
|
+
if (!/^HTTP\/1\.[01] 101\b/.test(status) || accept !== expected) {
|
|
127
|
+
this.emit(
|
|
128
|
+
"error",
|
|
129
|
+
new Error(`app-server WebSocket 握手失败:${status || "无响应"}`),
|
|
130
|
+
);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
this.ready = true;
|
|
134
|
+
this.#startHeartbeat();
|
|
135
|
+
for (const payload of this.pendingWrites.splice(0)) {
|
|
136
|
+
this.child.stdin.write(frame(0x1, payload));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
this.#drainFrames();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
#drainFrames() {
|
|
143
|
+
while (this.buffer.length >= 2) {
|
|
144
|
+
const first = this.buffer[0];
|
|
145
|
+
const second = this.buffer[1];
|
|
146
|
+
const fin = Boolean(first & 0x80);
|
|
147
|
+
const opcode = first & 0x0f;
|
|
148
|
+
const masked = Boolean(second & 0x80);
|
|
149
|
+
let length = second & 0x7f;
|
|
150
|
+
let offset = 2;
|
|
151
|
+
if (length === 126) {
|
|
152
|
+
if (this.buffer.length < 4) return;
|
|
153
|
+
length = this.buffer.readUInt16BE(2);
|
|
154
|
+
offset = 4;
|
|
155
|
+
} else if (length === 127) {
|
|
156
|
+
if (this.buffer.length < 10) return;
|
|
157
|
+
const wide = this.buffer.readBigUInt64BE(2);
|
|
158
|
+
if (wide > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
159
|
+
this.emit("error", new Error("app-server WebSocket 帧过大"));
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
length = Number(wide);
|
|
163
|
+
offset = 10;
|
|
164
|
+
}
|
|
165
|
+
const maskOffset = offset;
|
|
166
|
+
if (masked) offset += 4;
|
|
167
|
+
if (this.buffer.length < offset + length) return;
|
|
168
|
+
const mask = masked
|
|
169
|
+
? Buffer.from(this.buffer.subarray(maskOffset, maskOffset + 4))
|
|
170
|
+
: null;
|
|
171
|
+
let payload = Buffer.from(this.buffer.subarray(offset, offset + length));
|
|
172
|
+
this.buffer = this.buffer.subarray(offset + length);
|
|
173
|
+
if (mask) {
|
|
174
|
+
for (let index = 0; index < payload.length; index += 1) {
|
|
175
|
+
payload[index] ^= mask[index % 4];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (opcode === 0x8) {
|
|
179
|
+
this.#stopHeartbeat();
|
|
180
|
+
if (!this.child.stdin.destroyed) this.child.stdin.end(frame(0x8));
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (opcode === 0x9) {
|
|
184
|
+
if (!this.child.stdin.destroyed) this.child.stdin.write(frame(0xa, payload));
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (opcode === 0xa) continue;
|
|
188
|
+
if (opcode === 0x1) this.fragments = [payload];
|
|
189
|
+
else if (opcode === 0x0) this.fragments.push(payload);
|
|
190
|
+
else continue;
|
|
191
|
+
if (fin) {
|
|
192
|
+
this.stdout.write(Buffer.concat(this.fragments));
|
|
193
|
+
this.stdout.write("\n");
|
|
194
|
+
this.fragments = [];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
#startHeartbeat() {
|
|
200
|
+
if (this.pingTimer || !this.ready || this.closed || !this.pingIntervalMs) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this.pingTimer = this.setIntervalFn(() => {
|
|
204
|
+
if (!this.ready || this.closed || this.child.stdin.destroyed) return;
|
|
205
|
+
try {
|
|
206
|
+
this.child.stdin.write(frame(0x9));
|
|
207
|
+
} catch {
|
|
208
|
+
// The child exit/error handlers surface the real disconnect.
|
|
209
|
+
}
|
|
210
|
+
}, this.pingIntervalMs);
|
|
211
|
+
this.pingTimer?.unref?.();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
#stopHeartbeat() {
|
|
215
|
+
if (!this.pingTimer) return;
|
|
216
|
+
this.clearIntervalFn(this.pingTimer);
|
|
217
|
+
this.pingTimer = null;
|
|
218
|
+
}
|
|
219
|
+
}
|