Files
core/src/http/ws.ts
T
chrnx dba40b0c8b feat(mesh): heartbeat the link, and tell the truth when a teapot is gone
Three bugs, found by writing the tests for the status codes.

A teapot with a live control channel could never shut down. Control connections
were never registered with the stream registry, so server.close() sat waiting on
a connected teacup that had no reason to hang up, and app.close() never resolved.
Every other long-lived socket was already tracked; the control channel was the
one that skipped it.

A downed teapot answered 500 — after 30 seconds. sendRpc queued the frame on a
closed socket and waited out the full timeout for a verdict already known. It now
rejects immediately with 503: a dead upstream is not "this service broke", and the
status is what tells a caller it may retry and an operator where to look. An RPC
that times out on a *live* link is 504 — a slow teapot and a gone one are
different stories.

Heartbeat: each teacup pings its teapots (`mesh.heartbeatMs`, default 15s) and
closes a link after two unanswered rounds. This is what catches a half-open
connection, where the socket looks open with nobody home — otherwise a teacup only
learns on the next request. Ping/pong are mesh frames rather than WebSocket
protocol pings because Deno's and Bun's platform WebSocket does not expose
ws.ping(), so a protocol-level heartbeat could not work everywhere mesh runs.
Added to v1 while the wire still has no deployed peers; later it would be a
version bump that refuses every old peer.

The heartbeat's first close code was 1011, which silently did nothing: a *client*
may only send 1000 or 3000-4999, so it threw `invalid code` — and our neutral
close swallows throws, so the link stayed open while looking closed. 4011 mirrors
1011's meaning inside the application range.

Reconnect (and reconciling app-scope values, which survive their teapot from
cache) stays out — it is a post-beta spec of its own.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-07-15 23:32:45 -05:00

126 lines
4.0 KiB
TypeScript

import type http from 'http';
import { channel } from '../channel';
import type { Bus } from '../bus';
import { runWsConnection, matchWsRoute, trackUntil, type WsSocket, type WsRequest } from './ws-core';
import type { WsRouteDef, MeshControl } from './types';
/** Lazily loads the optional `ws` peer dependency's `WebSocketServer`, or null when it is not installed. */
function loadWss(): any | null {
try {
// `ws` is an OPTIONAL peer dependency: lazy-required so installs without WebSocket
// support don't fail. When absent this throws and we fall back to null (501 on upgrade).
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require('ws').WebSocketServer;
} catch {
return null;
}
}
/** Writes a 501 Not Implemented status to the raw socket and destroys it. */
function reject501(socket: any): void {
socket.write('HTTP/1.1 501 Not Implemented\r\n\r\n');
socket.destroy();
}
/** Handles the mesh-control upgrade path; returns true when the request was for the mesh control endpoint. */
function handleMeshUpgrade(
wss: any,
meshControl: MeshControl,
path: string,
req: http.IncomingMessage,
socket: any,
head: Buffer,
streams?: Set<() => void>,
): boolean {
if (path !== meshControl.path) return false;
if (!wss) {
reject501(socket);
return true;
}
// nodeSocket, not the raw ws: the control channel is the last consumer that spoke the
// `ws` EventEmitter API directly. It now takes the same neutral socket every route does.
// Tracked like any other long-lived socket: a connected teacup has no reason to hang up, so
// an untracked control channel leaves the teapot's server.close() waiting on it forever.
// serveFrames swallows its own failures, so the floating promise cannot reject.
wss.handleUpgrade(req, socket, head, (ws: any) => {
const neutral = nodeSocket(ws);
void trackUntil(neutral, meshControl.handle(neutral), streams);
});
return true;
}
/** Builds the neutral WsRequest from a Node upgrade request. Same values the old open() read from req.socket. */
function nodeWsRequest(req: http.IncomingMessage): WsRequest {
return {
url: req.url ?? '/',
headers: req.headers,
protocol: (req.socket as any).encrypted ? 'https' : 'http',
ip: req.socket.remoteAddress ?? '',
};
}
/** Wraps a `ws` socket as a neutral WsSocket: inbound channel + abort on close + send/close/terminate. */
function nodeSocket(ws: any): WsSocket {
const inbound = channel<unknown>();
const ac = new AbortController();
ws.on('message', (data: Buffer) => inbound.push(data.toString()));
ws.on('close', () => {
inbound.close();
ac.abort();
});
ws.on('error', (err: unknown) => inbound.fail(err));
return {
inbound,
abort: ac.signal,
get isOpen() {
return ws.readyState === 1; // 1 = OPEN
},
send: (data) => ws.send(data),
close: (code, reason) => {
try {
ws.close(code, reason);
} catch {
/* already closed */
}
},
terminate: () => (ws.terminate ?? ws.close).call(ws),
};
}
/** Attaches WebSocket upgrade handling to the server: routes to `wsRoutes` or a mesh control path. */
export function attachWs(
server: http.Server,
wsRoutes: WsRouteDef[],
bus?: Bus,
meshControl?: MeshControl,
streams?: Set<() => void>,
): void {
if (wsRoutes.length === 0 && !meshControl) return;
const WebSocketServer = loadWss();
const wss = WebSocketServer ? new WebSocketServer({ noServer: true }) : null;
server.on('upgrade', (req, socket, head) => {
const path = (req.url ?? '/').split('?')[0];
if (meshControl && handleMeshUpgrade(wss, meshControl, path, req, socket, head, streams)) return;
const route = matchWsRoute(wsRoutes, path);
if (!route) {
socket.destroy();
return;
}
if (!wss) {
reject501(socket);
return;
}
wss.handleUpgrade(req, socket, head, (ws: any) => {
void runWsConnection(nodeSocket(ws), nodeWsRequest(req), route, bus, streams);
});
});
}