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 #1037

Closed opened this
Closed without merging

scout/forge:scout/889-test-apps-ws-test-ts-fails-5-of-8-in-the → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +27 −16
src/app.ts +6
@@ -103,6 +103,12 @@ export interface ForgeConfig {
103103 * @default 5000
104104 */
105105 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;
106112 /**
107113 * The SSH listener's port, once it is up — `serveForgeSsh` writes the real
108114 * 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>();
671671 * it the socket is closed rather than the memory kept, and the forge never
672672 * buffers a frame on its own account.
673673 */
674 const WS_MAX_MESSAGE = 16 * 1024 * 1024;
674 export const WS_MAX_MESSAGE = 16 * 1024 * 1024;
675675 /**
676676 * Seconds a socket may go without a frame or a pong before it is dropped.
677677 * Bun pings on its own at half this, so a quiet tab that is still there
@@ -804,11 +804,14 @@ function closeBoth(
804804 /**
805805 * The forge's `websocket` handler: frames through, both ways, and the
806806 * 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`).
808809 */
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,
812815 closeOnBackpressureLimit: true,
813816 idleTimeout: WS_IDLE_SECONDS,
814817
@@ -835,7 +838,7 @@ export const appWebSocket: Bun.WebSocketHandler<AppSocket> = {
835838 typeof data === "string"
836839 ? Buffer.byteLength(data)
837840 : (data as ArrayBuffer).byteLength;
838 if (size > WS_MAX_MESSAGE) {
841 if (size > limit) {
839842 closeBoth(ws, 1009, "message too big");
840843 return;
841844 }
@@ -878,7 +881,7 @@ export const appWebSocket: Bun.WebSocketHandler<AppSocket> = {
878881 if (code === 1006) upstream.close(1001, "the browser went away");
879882 else upstream.close(sendable(code) ? code : 1000, reason);
880883 },
881 };
884 });
882885
883886 function grant(
884887 runtime: AppsRuntime,
src/http/server.ts +1 −1
@@ -1621,7 +1621,7 @@ export async function serve(app: App): Promise<Servers> {
16211621 maxRequestBodySize: REQUEST_MAX,
16221622 // The only sockets the forge holds are an app's, carried through the
16231623 // proxy; its own live surfaces are SSE (`sse.ts`) and stay so.
1624 websocket: appWebSocket,
1624 websocket: appWebSocket(app.config.wsMaxMessage),
16251625 async fetch(request, server) {
16261626 // An app's hostname is answered before any route: `hello.<domain>` is
16271627 // 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";
2727 import { serve, type Servers } from "../src/http/server.ts";
2828
2929 const MIB = 1024 * 1024;
30 /** The frame ceiling the forge under test is given; the default is 16 MiB. */
31 const LIMIT = MIB;
3032
3133 /**
3234 * The app under test: `/ws` echoes what it is sent, text as text and
@@ -209,7 +211,7 @@ describe("apps: websockets through the proxy", () => {
209211
210212 beforeAll(async () => {
211213 dataDir = await mkdtemp(join(tmpdir(), "forge-apps-ws-"));
212 app = createApp({ dataDir, port: 0 });
214 app = createApp({ dataDir, port: 0, wsMaxMessage: LIMIT });
213215 servers = await serve(app);
214216 runtime = new AppsRuntime(app, {
215217 domain: DOMAIN,
@@ -357,14 +359,14 @@ describe("apps: websockets through the proxy", () => {
357359 const inbound = connect("/ws");
358360 expect(await inbound.opened).toBe(true);
359361 await seenBy(inbound.next);
360 inbound.ws.send(new Uint8Array(16 * MIB + 1));
362 inbound.ws.send(new Uint8Array(LIMIT + 1));
361363 expect((await inbound.closed).code).not.toBe(1000);
362364
363365 // From the app: the forge will not carry it.
364366 const outbound = connect("/ws");
365367 expect(await outbound.opened).toBe(true);
366368 await seenBy(outbound.next);
367 outbound.ws.send(`big:${16 * MIB + 1}`);
369 outbound.ws.send(`big:${LIMIT + 1}`);
368370 expect(await outbound.closed).toEqual({
369371 code: 1009,
370372 reason: "message too big",
@@ -375,12 +377,12 @@ describe("apps: websockets through the proxy", () => {
375377 const atLimit = connect("/ws");
376378 expect(await atLimit.opened).toBe(true);
377379 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);
382384 atLimit.ws.close();
383 }, 30_000);
385 });
384386
385387 test("a handshake the app does not accept is the forge's page, never a socket that shuts", async () => {
386388 const page = await handshake(HOST, "/nope");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs