|
9 | 9 | } from "openclaw/plugin-sdk/runtime-config-snapshot"; |
10 | 10 | import { withEnvAsync, withTempDir } from "openclaw/plugin-sdk/test-env"; |
11 | 11 | import { afterEach, describe, expect, it } from "vitest"; |
12 | | -import { WebSocket, type ClientOptions, type RawData } from "ws"; |
| 12 | +import { WebSocket, type RawData } from "ws"; |
13 | 13 | import { parsePairingString } from "../../../chrome-extension/modules/relay-core.js"; |
14 | 14 | import { relayTestKey } from "../../../chrome-extension/relay-key.test-support.js"; |
15 | 15 | import { |
@@ -83,33 +83,21 @@ async function relayInventory(port: number): Promise<unknown> { |
83 | 83 | } |
84 | 84 | } |
85 | 85 |
|
86 | | -function upgradeWithFrames(messages: object[]): { |
87 | | - bytes: number; |
88 | | - finishRequest: NonNullable<ClientOptions["finishRequest"]>; |
89 | | -} { |
90 | | - const frames = messages.map((message) => { |
91 | | - const payload = Buffer.from(JSON.stringify(message)); |
92 | | - const extended = payload.length >= 126; |
93 | | - const header = Buffer.alloc(extended ? 8 : 6); |
94 | | - header[0] = 0x81; |
95 | | - header[1] = 0x80 | (extended ? 126 : payload.length); |
96 | | - if (extended) { |
97 | | - header.writeUInt16BE(payload.length, 2); |
98 | | - } |
99 | | - // The fixed zero mask leaves these synthetic payload bytes unchanged. |
100 | | - return Buffer.concat([header, payload]); |
101 | | - }); |
102 | | - const finishRequest: NonNullable<ClientOptions["finishRequest"]> = (request) => { |
103 | | - request.once("socket", (socket) => { |
104 | | - socket.once("connect", () => { |
105 | | - socket.cork(); |
106 | | - request.end(); |
107 | | - socket.write(Buffer.concat(frames)); |
108 | | - socket.uncork(); |
109 | | - }); |
110 | | - }); |
111 | | - }; |
112 | | - return { bytes: frames.reduce((sum, frame) => sum + frame.byteLength, 0), finishRequest }; |
| 86 | +function encodeUpgradeHead(messages: object[]): Buffer { |
| 87 | + return Buffer.concat( |
| 88 | + messages.map((message) => { |
| 89 | + const payload = Buffer.from(JSON.stringify(message)); |
| 90 | + const extended = payload.length >= 126; |
| 91 | + const header = Buffer.alloc(extended ? 8 : 6); |
| 92 | + header[0] = 0x81; |
| 93 | + header[1] = 0x80 | (extended ? 126 : payload.length); |
| 94 | + if (extended) { |
| 95 | + header.writeUInt16BE(payload.length, 2); |
| 96 | + } |
| 97 | + // The fixed zero mask leaves these synthetic payload bytes unchanged. |
| 98 | + return Buffer.concat([header, payload]); |
| 99 | + }), |
| 100 | + ); |
113 | 101 | } |
114 | 102 |
|
115 | 103 | function rawDataText(data: RawData): string { |
@@ -219,14 +207,29 @@ describe.sequential("local Gateway extension relay wakeup", () => { |
219 | 207 | OPENCLAW_GATEWAY_PORT: String(gatewayPort), |
220 | 208 | }, |
221 | 209 | async () => { |
| 210 | + const inventory = { |
| 211 | + type: "tabs", |
| 212 | + tabs: [ |
| 213 | + { |
| 214 | + tabId: 7, |
| 215 | + url: "https://example.test/initial", |
| 216 | + title: "Coalesced inventory", |
| 217 | + active: true, |
| 218 | + }, |
| 219 | + ], |
| 220 | + }; |
| 221 | + const coalesced = |
| 222 | + legacy === "head" ? encodeUpgradeHead([EXTENSION_HELLO, inventory]) : undefined; |
222 | 223 | const gatewayServer = http.createServer((_req, res) => { |
223 | 224 | res.writeHead(426); |
224 | 225 | res.end(); |
225 | 226 | }); |
226 | 227 | let upgradeHeadBytes = 0; |
227 | 228 | gatewayServer.on("upgrade", (req, socket, head) => { |
228 | | - upgradeHeadBytes = head.byteLength; |
229 | | - void handleGatewayExtensionUpgrade(req, socket, head); |
| 229 | + // TCP writes cannot guarantee Node's upgrade-head size; supply that exact boundary. |
| 230 | + const initialHead = coalesced ?? head; |
| 231 | + upgradeHeadBytes = initialHead.byteLength; |
| 232 | + void handleGatewayExtensionUpgrade(req, socket, initialHead); |
230 | 233 | }); |
231 | 234 | let extension: WebSocket | undefined; |
232 | 235 | let daemon: Awaited<ReturnType<typeof runExtensionRelayDaemon>> | undefined; |
@@ -261,22 +264,8 @@ describe.sequential("local Gateway extension relay wakeup", () => { |
261 | 264 | const protocols = legacy |
262 | 265 | ? ["openclaw-extension-relay", `openclaw-extension-token.${RELAY_KEY}`] |
263 | 266 | : BROWSER_RELAY_EXTENSION_SUBPROTOCOL; |
264 | | - const inventory = { |
265 | | - type: "tabs", |
266 | | - tabs: [ |
267 | | - { |
268 | | - tabId: 7, |
269 | | - url: "https://example.test/initial", |
270 | | - title: "Coalesced inventory", |
271 | | - active: true, |
272 | | - }, |
273 | | - ], |
274 | | - }; |
275 | | - const coalesced = |
276 | | - legacy === "head" ? upgradeWithFrames([EXTENSION_HELLO, inventory]) : undefined; |
277 | 267 | extension = new WebSocket(parsed.relayUrl, protocols, { |
278 | 268 | origin: "chrome-extension://gateway-wakeup-integration", |
279 | | - finishRequest: coalesced?.finishRequest, |
280 | 269 | }); |
281 | 270 | await once(extension, "open"); |
282 | 271 | if (!legacy) { |
@@ -307,7 +296,7 @@ describe.sequential("local Gateway extension relay wakeup", () => { |
307 | 296 | expect(JSON.parse(rawDataText(okData))).toMatchObject({ type: "auth.ok", v: 2 }); |
308 | 297 | } |
309 | 298 | if (legacy === "head") { |
310 | | - expect(upgradeHeadBytes).toBe(coalesced?.bytes); |
| 299 | + expect(upgradeHeadBytes).toBe(coalesced?.byteLength); |
311 | 300 | } else { |
312 | 301 | extension.send(JSON.stringify(EXTENSION_HELLO)); |
313 | 302 | } |
|
0 commit comments