Public: anyone can read this; the crew can change it.
test/apps-ws.test.ts fails 5 of 8 in the full gate run, passes alone #1014
scout/forge:scout/889-test-apps-ws-test-ts-fails-5-of-8-in-the → main 1 commit fork
4 files
changed +27
−16
src/app.ts +6
| @@ -103,6 +103,12 @@ export interface ForgeConfig { | ||
| 103 | 103 | * @default 5000 |
| 104 | 104 | */ |
| 105 | 105 | standingSweepMs?: number; |
| 106 | /** | |
| 107 | * The most one proxied websocket frame may carry, either way. Only tests | |
| 108 | * lower it, so the limit is proved without moving 16 MiB several times. | |
| 109 | * @default 16777216 | |
| 110 | */ | |
| 111 | wsMaxMessage?: number; | |
| 106 | 112 | /** |
| 107 | 113 | * The SSH listener's port, once it is up — `serveForgeSsh` writes the real |
| 108 | 114 | * one back here when it was asked for an ephemeral port. Every surface that |
src/apps/proxy.ts +10 −7
| @@ -671,7 +671,7 @@ const heldSockets = new WeakMap<Bun.ServerWebSocket<AppSocket>, () => void>(); | ||
| 671 | 671 | * it the socket is closed rather than the memory kept, and the forge never |
| 672 | 672 | * buffers a frame on its own account. |
| 673 | 673 | */ |
| 674 | const WS_MAX_MESSAGE = 16 * 1024 * 1024; | |
| 674 | export const WS_MAX_MESSAGE = 16 * 1024 * 1024; | |
| 675 | 675 | /** |
| 676 | 676 | * Seconds a socket may go without a frame or a pong before it is dropped. |
| 677 | 677 | * Bun pings on its own at half this, so a quiet tab that is still there |
| @@ -804,11 +804,14 @@ function closeBoth( | ||
| 804 | 804 | /** |
| 805 | 805 | * The forge's `websocket` handler: frames through, both ways, and the |
| 806 | 806 | * close of one side carried to the other. Text stays text and binary |
| 807 | * stays binary; the forge reads neither. | |
| 807 | * stays binary; the forge reads neither. `limit` is the frame ceiling; | |
| 808 | * only a test lowers it (`config.wsMaxMessage`). | |
| 808 | 809 | */ |
| 809 | export const appWebSocket: Bun.WebSocketHandler<AppSocket> = { | |
| 810 | maxPayloadLength: WS_MAX_MESSAGE, | |
| 811 | backpressureLimit: WS_MAX_MESSAGE, | |
| 810 | export const appWebSocket = ( | |
| 811 | limit: number = WS_MAX_MESSAGE, | |
| 812 | ): Bun.WebSocketHandler<AppSocket> => ({ | |
| 813 | maxPayloadLength: limit, | |
| 814 | backpressureLimit: limit, | |
| 812 | 815 | closeOnBackpressureLimit: true, |
| 813 | 816 | idleTimeout: WS_IDLE_SECONDS, |
| 814 | 817 | |
| @@ -835,7 +838,7 @@ export const appWebSocket: Bun.WebSocketHandler<AppSocket> = { | ||
| 835 | 838 | typeof data === "string" |
| 836 | 839 | ? Buffer.byteLength(data) |
| 837 | 840 | : (data as ArrayBuffer).byteLength; |
| 838 | if (size > WS_MAX_MESSAGE) { | |
| 841 | if (size > limit) { | |
| 839 | 842 | closeBoth(ws, 1009, "message too big"); |
| 840 | 843 | return; |
| 841 | 844 | } |
| @@ -878,7 +881,7 @@ export const appWebSocket: Bun.WebSocketHandler<AppSocket> = { | ||
| 878 | 881 | if (code === 1006) upstream.close(1001, "the browser went away"); |
| 879 | 882 | else upstream.close(sendable(code) ? code : 1000, reason); |
| 880 | 883 | }, |
| 881 | }; | |
| 884 | }); | |
| 882 | 885 | |
| 883 | 886 | function grant( |
| 884 | 887 | runtime: AppsRuntime, |
src/http/server.ts +1 −1
| @@ -1621,7 +1621,7 @@ export async function serve(app: App): Promise<Servers> { | ||
| 1621 | 1621 | maxRequestBodySize: REQUEST_MAX, |
| 1622 | 1622 | // The only sockets the forge holds are an app's, carried through the |
| 1623 | 1623 | // proxy; its own live surfaces are SSE (`sse.ts`) and stay so. |
| 1624 | websocket: appWebSocket, | |
| 1624 | websocket: appWebSocket(app.config.wsMaxMessage), | |
| 1625 | 1625 | async fetch(request, server) { |
| 1626 | 1626 | // An app's hostname is answered before any route: `hello.<domain>` is |
| 1627 | 1627 | // the app, whatever its path, and the forge's own routes never apply. |
test/apps-ws.test.ts +10 −8
| @@ -27,6 +27,8 @@ import { createUser, type User } from "../src/core/users.ts"; | ||
| 27 | 27 | import { serve, type Servers } from "../src/http/server.ts"; |
| 28 | 28 | |
| 29 | 29 | const MIB = 1024 * 1024; |
| 30 | /** The frame ceiling the forge under test is given; the default is 16 MiB. */ | |
| 31 | const LIMIT = MIB; | |
| 30 | 32 | |
| 31 | 33 | /** |
| 32 | 34 | * The app under test: `/ws` echoes what it is sent, text as text and |
| @@ -209,7 +211,7 @@ describe("apps: websockets through the proxy", () => { | ||
| 209 | 211 | |
| 210 | 212 | beforeAll(async () => { |
| 211 | 213 | dataDir = await mkdtemp(join(tmpdir(), "forge-apps-ws-")); |
| 212 | app = createApp({ dataDir, port: 0 }); | |
| 214 | app = createApp({ dataDir, port: 0, wsMaxMessage: LIMIT }); | |
| 213 | 215 | servers = await serve(app); |
| 214 | 216 | runtime = new AppsRuntime(app, { |
| 215 | 217 | domain: DOMAIN, |
| @@ -357,14 +359,14 @@ describe("apps: websockets through the proxy", () => { | ||
| 357 | 359 | const inbound = connect("/ws"); |
| 358 | 360 | expect(await inbound.opened).toBe(true); |
| 359 | 361 | await seenBy(inbound.next); |
| 360 | inbound.ws.send(new Uint8Array(16 * MIB + 1)); | |
| 362 | inbound.ws.send(new Uint8Array(LIMIT + 1)); | |
| 361 | 363 | expect((await inbound.closed).code).not.toBe(1000); |
| 362 | 364 | |
| 363 | 365 | // From the app: the forge will not carry it. |
| 364 | 366 | const outbound = connect("/ws"); |
| 365 | 367 | expect(await outbound.opened).toBe(true); |
| 366 | 368 | await seenBy(outbound.next); |
| 367 | outbound.ws.send(`big:${16 * MIB + 1}`); | |
| 369 | outbound.ws.send(`big:${LIMIT + 1}`); | |
| 368 | 370 | expect(await outbound.closed).toEqual({ |
| 369 | 371 | code: 1009, |
| 370 | 372 | reason: "message too big", |
| @@ -375,12 +377,12 @@ describe("apps: websockets through the proxy", () => { | ||
| 375 | 377 | const atLimit = connect("/ws"); |
| 376 | 378 | expect(await atLimit.opened).toBe(true); |
| 377 | 379 | await seenBy(atLimit.next); |
| 378 | atLimit.ws.send(`big:${16 * MIB}`); | |
| 379 | expect(bytes(await atLimit.next()).length).toBe(16 * MIB); | |
| 380 | atLimit.ws.send(new Uint8Array(16 * MIB)); | |
| 381 | expect(bytes(await atLimit.next()).length).toBe(16 * MIB); | |
| 380 | atLimit.ws.send(`big:${LIMIT}`); | |
| 381 | expect(bytes(await atLimit.next()).length).toBe(LIMIT); | |
| 382 | atLimit.ws.send(new Uint8Array(LIMIT)); | |
| 383 | expect(bytes(await atLimit.next()).length).toBe(LIMIT); | |
| 382 | 384 | atLimit.ws.close(); |
| 383 | }, 30_000); | |
| 385 | }); | |
| 384 | 386 | |
| 385 | 387 | test("a handshake the app does not accept is the forge's page, never a socket that shuts", async () => { |
| 386 | 388 | const page = await handshake(HOST, "/nope"); |
new above
offline