The 1 MiB inbound cap from #107 applied to every WebSocket, the mesh control channel included. An RPC carries a whole request body, so a body inside maxBodyBytes could still overflow the cap once wrapped in an rpc-req frame, and the teapot then closed the whole channel with 1009: every request through that link failed 503 until it reconnected. The test asked for by the card (T1, a 1 MB JSON body at defaults) passes on Node, Deno and Bun, because a JSON body re-encodes to about its own size. Two other cases fail on all three before this change: - T1b: a 999 KB text body of quotes doubles under JSON escaping; - T4: maxBodyBytes raised to 4 MB, a 3.9 MB JSON body. The control channel's bound is now derived from the teapot's limits.maxBodyBytes via controlFrameLimit(): MAX_FRAME_CHARS (4,000,000) at the 1 MB default, scaled by the same ratio above it, never below maxWsMessageBytes. No new option. User @Ws routes keep maxWsMessageBytes: Node gets a second WebSocketServer for the control path, app.upgrade picks the bound from the path before subscribing, and Bun's single native maxPayloadLength is raised on a teapot only, leaving app.upgrade to close user routes 1009 itself. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
370 lines
13 KiB
TypeScript
370 lines
13 KiB
TypeScript
import { isHttpError } from '../signals';
|
||
import { CHANNEL_TAP, type TappableChannel } from '../channel';
|
||
import type { Bus } from '../bus';
|
||
import { correlateRequest } from './core';
|
||
import { matchPattern } from './router';
|
||
import type { WsRouteDef, RequestLimits } from './types';
|
||
|
||
/** Neutral, runtime-free WebSocket request context (replaces the raw Node req). */
|
||
export interface WsRequest {
|
||
url: string;
|
||
headers: Record<string, string | string[] | undefined>;
|
||
protocol: 'http' | 'https';
|
||
ip: string;
|
||
}
|
||
|
||
/** Capability an adapter builds from its runtime socket. The neutral core talks only to this. */
|
||
export interface WsSocket {
|
||
readonly inbound: AsyncIterable<unknown>;
|
||
readonly abort: AbortSignal;
|
||
readonly isOpen: boolean;
|
||
send(data: string): void;
|
||
close(code?: number, reason?: string): void;
|
||
terminate(): void;
|
||
}
|
||
|
||
/** Default for `limits.maxWsMessageBytes`: 1 MiB. */
|
||
export const DEFAULT_MAX_WS_MESSAGE_BYTES = 1_048_576;
|
||
/** Default for `limits.maxWsQueue`, in undelivered inbound messages per connection (D1). */
|
||
export const DEFAULT_MAX_WS_QUEUE = 16;
|
||
|
||
/**
|
||
* Where `createApp` leaves the resolved `maxWsMessageBytes` for `serveBun`, which needs it as
|
||
* Bun's native `maxPayloadLength` and has no other path to the app's options. Internal, not part
|
||
* of the public `App` type; `Symbol.for` because each tsup entry carries its own copy of this
|
||
* module, and only the global registry resolves to one symbol across them.
|
||
*/
|
||
export const WS_MAX_MESSAGE_BYTES: unique symbol = Symbol.for('green-tea.ws-max-message-bytes');
|
||
|
||
/** The effective `limits.maxWsMessageBytes`. */
|
||
export function wsMessageBytes(limits: Pick<RequestLimits, 'maxWsMessageBytes'> | undefined): number {
|
||
return limits?.maxWsMessageBytes ?? DEFAULT_MAX_WS_MESSAGE_BYTES;
|
||
}
|
||
|
||
/**
|
||
* The limits the mesh control channel's socket is bounded by (#120): `limits` with
|
||
* `maxWsMessageBytes` raised to the channel's own frame bound when that is larger. A user route
|
||
* never goes through this, so it keeps `maxWsMessageBytes` as configured.
|
||
*/
|
||
export function controlLimits(
|
||
limits: Pick<RequestLimits, 'maxWsMessageBytes' | 'maxWsQueue'> | undefined,
|
||
maxFrameBytes: number | undefined,
|
||
): Pick<RequestLimits, 'maxWsQueue'> & { maxWsMessageBytes: number } {
|
||
return { ...limits, maxWsMessageBytes: Math.max(wsMessageBytes(limits), maxFrameBytes ?? 0) };
|
||
}
|
||
|
||
/** Throws when a WebSocket limit is present but not a positive safe integer (#107). */
|
||
export function validateWsLimits(limits: RequestLimits | undefined): void {
|
||
for (const key of ['maxWsMessageBytes', 'maxWsQueue'] as const) {
|
||
const value: unknown = limits?.[key];
|
||
if (value === undefined || (Number.isSafeInteger(value) && (value as number) > 0)) continue;
|
||
const shown = typeof value === 'string' ? `'${value}'` : String(value);
|
||
throw new TypeError(`limits.${key} must be a positive integer, got ${shown}`);
|
||
}
|
||
}
|
||
|
||
const encoder = new TextEncoder();
|
||
|
||
/** True when `msg`'s UTF-8 encoding is longer than `max` bytes. */
|
||
function exceedsBytes(msg: unknown, max: number): boolean {
|
||
if (typeof msg !== 'string') return false; // every adapter delivers strings; nothing else to measure
|
||
// A UTF-16 unit encodes to 1–3 UTF-8 bytes, so both bounds below are exact. Only a string in
|
||
// between pays for the encode — which is never one of the short messages that dominate.
|
||
if (msg.length * 3 <= max) return false;
|
||
if (msg.length > max) return true;
|
||
return encoder.encode(msg).byteLength > max;
|
||
}
|
||
|
||
/**
|
||
* Bounds a socket's inbound side (#107): a frame over `maxWsMessageBytes` closes `1009`, and a
|
||
* frame that arrives while `maxWsQueue` frames are still undelivered closes `1013`. Nothing is
|
||
* dropped silently — the peer learns which bound it hit from the close code.
|
||
*
|
||
* Subscribes to `socket.inbound` immediately. `channel` is fan-out, so a frame pushed with no
|
||
* subscriber is lost; subscribing here, before any boot or step runs, is what keeps the first
|
||
* frames — for the mesh control channel, the `hello` itself. Those early frames go to the first
|
||
* consumer. After that the returned `inbound` fans out like `channel`: each iterator gets every
|
||
* frame from the moment it subscribes, through its own queue, and the bound applies to each queue.
|
||
* Every other capability is the original socket's.
|
||
*/
|
||
export function boundInbound(
|
||
socket: WsSocket,
|
||
limits?: Pick<RequestLimits, 'maxWsMessageBytes' | 'maxWsQueue'>,
|
||
): WsSocket {
|
||
const fanout = new InboundFanout(socket, wsMessageBytes(limits), limits?.maxWsQueue ?? DEFAULT_MAX_WS_QUEUE);
|
||
|
||
const tap = (socket.inbound as Partial<TappableChannel<unknown>>)[CHANNEL_TAP];
|
||
if (tap) fanout.tap(tap);
|
||
else void fanout.pump();
|
||
|
||
return Object.create(socket, {
|
||
inbound: { value: { [Symbol.asyncIterator]: () => fanout.subscribe() } },
|
||
}) as WsSocket;
|
||
}
|
||
|
||
/** The fan-out behind {@link boundInbound}: one bounded queue per consumer. */
|
||
class InboundFanout {
|
||
private readonly subs = new Set<InboundSub>();
|
||
/** Holds the frames that arrive before anyone reads; handed to the first consumer. */
|
||
private first: InboundSub | null;
|
||
private ended = false;
|
||
private failure: { err: unknown } | null = null;
|
||
|
||
constructor(
|
||
private readonly socket: WsSocket,
|
||
private readonly maxBytes: number,
|
||
private readonly maxQueue: number,
|
||
) {
|
||
this.first = this.add();
|
||
}
|
||
|
||
/**
|
||
* Reads the adapter's channel inside its `push`. Runtimes emit a whole read's worth of frames
|
||
* synchronously — `ws` does — so a bound checked after the event loop yields is checked after an
|
||
* unbounded queue has already taken the burst. Here each frame is bounded before it is queued.
|
||
*/
|
||
tap(tap: TappableChannel<unknown>[typeof CHANNEL_TAP]): void {
|
||
const untap = tap({
|
||
push: (msg) => {
|
||
if (this.ended || this.accept(msg)) return;
|
||
untap();
|
||
this.settle();
|
||
},
|
||
end: () => this.settle(),
|
||
fail: (err) => this.settle({ err }),
|
||
});
|
||
}
|
||
|
||
// ponytail: only for an inbound that is not a channel (no adapter builds one); bounds late.
|
||
async pump(): Promise<void> {
|
||
try {
|
||
for await (const msg of this.socket.inbound) {
|
||
if (this.ended || !this.accept(msg)) break;
|
||
}
|
||
|
||
this.settle();
|
||
} catch (err) {
|
||
this.settle({ err });
|
||
}
|
||
}
|
||
|
||
subscribe(): AsyncIterator<unknown> {
|
||
const sub = this.first ?? this.add();
|
||
this.first = null;
|
||
return sub;
|
||
}
|
||
|
||
private add(): InboundSub {
|
||
const sub = new InboundSub(() => this.subs.delete(sub));
|
||
if (this.ended) sub.settle(this.failure ?? undefined);
|
||
else this.subs.add(sub);
|
||
return sub;
|
||
}
|
||
|
||
/** Delivers or queues one frame for every consumer; returns false once it has closed the socket. */
|
||
private accept(msg: unknown): boolean {
|
||
if (exceedsBytes(msg, this.maxBytes)) {
|
||
this.socket.close(1009, 'message too large');
|
||
return false;
|
||
}
|
||
|
||
// Capacity first, for every consumer: a frame that overflows one queue reaches none, so a
|
||
// rejected frame cannot run some handlers' side effects and not others'.
|
||
for (const sub of this.subs) {
|
||
if (sub.full(this.maxQueue)) {
|
||
this.socket.close(1013, 'inbound queue full');
|
||
return false;
|
||
}
|
||
}
|
||
|
||
for (const sub of this.subs) sub.offer(msg);
|
||
return true;
|
||
}
|
||
|
||
/** Ends every consumer once, cleanly or with a failure; whatever is still queued is delivered first. */
|
||
private settle(failure?: { err: unknown }): void {
|
||
if (this.ended) return;
|
||
this.ended = true;
|
||
this.failure = failure ?? null;
|
||
for (const sub of this.subs) sub.settle(failure);
|
||
this.subs.clear();
|
||
}
|
||
}
|
||
|
||
/** One consumer's queue; it is its own async iterator. */
|
||
class InboundSub implements AsyncIterator<unknown> {
|
||
private readonly queue: unknown[] = [];
|
||
private waiter: { resolve: (r: IteratorResult<unknown>) => void; reject: (e: unknown) => void } | null = null;
|
||
private ended = false;
|
||
private failure: { err: unknown } | null = null;
|
||
|
||
constructor(private readonly onReturn: () => void) {}
|
||
|
||
/** True when `max` frames are already waiting and no `next()` is there to take another. */
|
||
full(max: number): boolean {
|
||
return !this.waiter && this.queue.length >= max;
|
||
}
|
||
|
||
/** Hands `msg` to a waiting `next()` or queues it; callers check {@link full} first. */
|
||
offer(msg: unknown): void {
|
||
const waiter = this.waiter;
|
||
|
||
if (waiter) {
|
||
this.waiter = null;
|
||
waiter.resolve({ value: msg, done: false });
|
||
return;
|
||
}
|
||
|
||
this.queue.push(msg);
|
||
}
|
||
|
||
next(): Promise<IteratorResult<unknown>> {
|
||
if (this.queue.length) return Promise.resolve({ value: this.queue.shift(), done: false });
|
||
if (this.failure) return Promise.reject(this.failure.err);
|
||
if (this.ended) return Promise.resolve({ value: undefined, done: true });
|
||
return new Promise((resolve, reject) => {
|
||
this.waiter = { resolve, reject };
|
||
});
|
||
}
|
||
|
||
// A consumer that stops reading has said it wants no more; queueing for it would only close
|
||
// the connection for a bound it no longer cares about. Other consumers keep receiving.
|
||
return(): Promise<IteratorResult<unknown>> {
|
||
this.queue.length = 0;
|
||
this.onReturn();
|
||
this.settle();
|
||
return Promise.resolve({ value: undefined, done: true });
|
||
}
|
||
|
||
settle(failure?: { err: unknown }): void {
|
||
if (this.ended) return;
|
||
this.ended = true;
|
||
this.failure = failure ?? null;
|
||
const waiter = this.waiter;
|
||
this.waiter = null;
|
||
if (!waiter) return;
|
||
if (failure) waiter.reject(failure.err);
|
||
else waiter.resolve({ value: undefined, done: true });
|
||
}
|
||
}
|
||
|
||
/** Finds the first ws route whose pattern matches `path`, capturing its params. */
|
||
export function matchWsRoute(
|
||
wsRoutes: WsRouteDef[],
|
||
path: string,
|
||
): { def: WsRouteDef; params: Record<string, string> | undefined } | undefined {
|
||
return wsRoutes
|
||
.map((candidate) => ({ def: candidate, params: matchPattern(candidate.pattern, path) }))
|
||
.find((entry) => entry.params);
|
||
}
|
||
|
||
/**
|
||
* Runs `connection` with the socket registered for force-close on shutdown, unregistering it
|
||
* when the connection ends. Without this a long-lived socket keeps `server.close()` waiting for
|
||
* a peer that has no reason to hang up — the mesh control channel is exactly that shape.
|
||
*/
|
||
export async function trackUntil(
|
||
socket: WsSocket,
|
||
connection: Promise<void>,
|
||
streams?: Set<() => void>,
|
||
): Promise<void> {
|
||
const { closer } = trackConnection(socket, streams);
|
||
|
||
try {
|
||
await connection;
|
||
} finally {
|
||
streams?.delete(closer);
|
||
}
|
||
}
|
||
|
||
/** Registers the connection with the stream registry so it can be force-closed on shutdown. */
|
||
function trackConnection(socket: WsSocket, streams?: Set<() => void>): { closer: () => void } {
|
||
const closer = () => {
|
||
try {
|
||
socket.terminate();
|
||
} catch {
|
||
/* */
|
||
}
|
||
};
|
||
|
||
streams?.add(closer);
|
||
return { closer };
|
||
}
|
||
|
||
/** Drives the outbound async iterator, sending each yielded value until done or the socket closes. */
|
||
async function pumpOutbound(socket: WsSocket, iterator: AsyncIterator<unknown>): Promise<void> {
|
||
while (true) {
|
||
const { value, done } = await iterator.next();
|
||
if (done) break;
|
||
if (!socket.isOpen) break;
|
||
socket.send(typeof value === 'string' ? value : JSON.stringify(value));
|
||
}
|
||
|
||
try {
|
||
socket.close();
|
||
} catch {
|
||
/* already closed */
|
||
}
|
||
}
|
||
|
||
/** Closes with 4000+status for HttpErrors, 1011 otherwise. */
|
||
function closeOnError(socket: WsSocket, err: unknown): void {
|
||
if (isHttpError(err)) {
|
||
const reason = new TextDecoder().decode(new TextEncoder().encode(String(err.message)).subarray(0, 120));
|
||
|
||
try {
|
||
socket.close(4000 + err.status, reason);
|
||
} catch {
|
||
/* already closed */
|
||
}
|
||
} else {
|
||
try {
|
||
socket.close(1011);
|
||
} catch {
|
||
/* already closed */
|
||
}
|
||
}
|
||
}
|
||
|
||
/** Runs the full per-connection lifecycle over a neutral socket capability. */
|
||
export async function runWsConnection(
|
||
socket: WsSocket,
|
||
request: WsRequest,
|
||
route: { def: WsRouteDef; params: Record<string, string> | undefined },
|
||
bus?: Bus,
|
||
streams?: Set<() => void>,
|
||
): Promise<void> {
|
||
const { closer } = trackConnection(socket, streams);
|
||
const name = route.def.pattern;
|
||
// Derived here rather than handed down: an upgrade is an HTTP request with headers like any
|
||
// other, so it adopts an `x-request-id` from a gateway and carries `traceparent` through by the
|
||
// same rule. `runWsConnection` is the only place a ws connection is correlated, which is what
|
||
// keeps one identity per connection instead of one per emitter.
|
||
const trace = { name, route: name, transport: 'ws', ...correlateRequest(request.headers) };
|
||
let iterator: AsyncIterator<unknown> | undefined;
|
||
|
||
const onAbort = () => {
|
||
streams?.delete(closer);
|
||
void Promise.resolve(iterator?.return?.()).catch(() => {});
|
||
};
|
||
|
||
socket.abort.addEventListener('abort', onAbort, { once: true });
|
||
bus?.emit('stream:open', trace);
|
||
|
||
try {
|
||
const outbound = await route.def.open({
|
||
params: route.params ?? {},
|
||
inbound: socket.inbound,
|
||
abort: socket.abort,
|
||
req: request,
|
||
});
|
||
iterator = outbound[Symbol.asyncIterator]();
|
||
await pumpOutbound(socket, iterator);
|
||
} catch (err) {
|
||
bus?.emit('stream:error', { ...trace, error: err });
|
||
closeOnError(socket, err);
|
||
} finally {
|
||
bus?.emit('stream:close', trace);
|
||
}
|
||
}
|