Inbound WebSocket traffic was unbounded per connection: Node used ws's 100 MiB default payload, the other runtimes had no cap of their own, and the queue behind `inbound` was an unbounded channel on every runtime, so one client could exhaust memory with large messages or a slow consumer. Add `limits.maxWsMessageBytes` (default 1 MiB, close 1009) and `limits.maxWsQueue` (default 16 unread messages, close 1013), as recorded in D1. Both are server-wide and validated by createApp, which throws a TypeError for anything but a positive integer. Enforcement is shared in `boundInbound`, which subscribes immediately and replaces the subscribe-before-boot trick in app.upgrade. Node and Bun also get the native pre-buffer cap; Bun drops the connection without a close frame, so its peer sees 1006, which is documented and tested. Deno and workerd expose no such option and check the size after receipt. BREAKING CHANGE: messages over 1 MiB now close the connection with 1009, and a connection with 16 unread inbound messages is closed with 1013 on the next one, including a handler that never reads `inbound`. Closes #107 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
// Worker entry for the real workerd (Miniflare) integration test.
|
|
// Mirrors test/deno/ws.test.ts's graph so HTTP + WebSocket behavior is proven
|
|
// identically across runtimes. Run via `npm run test:edge` (see test/edge/run.ts).
|
|
import { createApp, Route, Get, Ws, ctx, Module } from '../../src/index';
|
|
import { edgeHandler } from '../../src/edge';
|
|
|
|
@Route('/rt')
|
|
class Rt {
|
|
@Get('/hello')
|
|
hello() {
|
|
return { ok: true };
|
|
}
|
|
|
|
@Ws('/echo')
|
|
echo(@ctx() { inbound }: any) {
|
|
return (async function* () {
|
|
for await (const msg of inbound) yield `echo:${msg}`;
|
|
})();
|
|
}
|
|
|
|
// Never reads `inbound`, so every frame the client sends stays queued (#107).
|
|
@Ws('/stall')
|
|
stall() {
|
|
return (async function* () {
|
|
await new Promise<never>(() => {});
|
|
yield '';
|
|
})();
|
|
}
|
|
|
|
@Ws('/room/:name')
|
|
room(@ctx() { inbound }: any) {
|
|
return (async function* () {
|
|
for await (const msg of inbound) yield `room:${msg}`;
|
|
})();
|
|
}
|
|
}
|
|
|
|
@Module({ mountpoint: '/', controllers: [Rt] })
|
|
class M {}
|
|
|
|
const app = createApp({ modules: [M] });
|
|
// #107: the same graph with tight ws limits, reached through the `limited` host.
|
|
const limited = createApp({ modules: [M], limits: { maxWsMessageBytes: 1024, maxWsQueue: 4 } });
|
|
const handlers = { default: edgeHandler(app), limited: edgeHandler(limited) };
|
|
|
|
export default {
|
|
fetch: (request: Request, env?: unknown, ctx?: Parameters<typeof handlers.default>[2]) =>
|
|
(new URL(request.url).hostname === 'limited' ? handlers.limited : handlers.default)(request, env, ctx),
|
|
};
|