Files
core/src/mesh/teapot.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

211 lines
6.7 KiB
TypeScript

// node: prefix, not a bare specifier: this is the mesh auth path, and on Deno/Bun a bare
// 'crypto' leans on node-compat resolution heuristics. Buffer + timingSafeEqual resolve
// under node-compat on both; edge/workerd is out of scope (see the cross-runtime spec).
import crypto from 'node:crypto';
import type { Bus } from '../bus';
import type { WsSocket } from '../http/ws-core';
import type { ResponseShape } from '../pipeline';
import { isHttpError } from '../signals';
import {
encode,
decode,
MESH_PROTOCOL_VERSION,
type Manifest,
type RequestEnvelope,
type RouteEntry,
type Frame,
} from './protocol';
/** WebSocket path where a teapot exposes its mesh control channel. */
export const MESH_CONTROL_PATH = '/__mesh__/control';
/** Assemble a {@link Manifest} from exported provider/step tokens and buffered routes. */
export function buildManifest(args: {
providers: string[]; // exported provider tokens (app-scope)
steps: string[]; // exported step tokens (request-scope)
routes: RouteEntry[]; // exported buffered routes
}): Manifest {
return {
scopes: [
...args.providers.map((token) => ({ token, scope: 'app' as const })),
...args.steps.map((token) => ({ token, scope: 'request' as const })),
],
routes: args.routes,
};
}
function safeEqual(left: string, right: string): boolean {
const leftBuf = Buffer.from(left);
const rightBuf = Buffer.from(right);
if (leftBuf.length !== rightBuf.length) return false;
return crypto.timingSafeEqual(leftBuf, rightBuf);
}
/** Dependencies for the mesh control handler: shared secret, manifest, and scope/route resolvers. */
export interface MeshControlDeps {
secret: string;
manifest: Manifest;
resolveScope: (name: string, env: RequestEnvelope) => Promise<unknown>;
resolveRoute: (name: string, env: RequestEnvelope) => Promise<ResponseShape>;
bus?: Bus;
}
/** The rpc-req member of the {@link Frame} union. */
type RpcReqFrame = Extract<Frame, { type: 'rpc-req' }>;
/** Build a 403 "not exported" error for an unknown scope/route token. */
function notExported(name: string): Error {
const error: any = new Error(`not exported: ${name}`);
error.status = 403;
return error;
}
/**
* Validate an unauthenticated hello frame; reply with the manifest on success or close on mismatch.
* The version is checked *before* the secret: a skewed peer is not an auth failure, and telling it
* "wrong secret" would send its operator hunting the wrong bug.
*/
function handleHandshake(socket: WsSocket, frame: Frame, deps: MeshControlDeps): boolean {
if (frame.type !== 'hello') {
socket.close(1008);
return false;
}
if (frame.v !== MESH_PROTOCOL_VERSION) {
socket.close(
1008,
`mesh protocol version mismatch: peer speaks v${frame.v}, this teapot speaks v${MESH_PROTOCOL_VERSION}`,
);
return false;
}
if (!safeEqual(frame.secret, deps.secret)) {
socket.close(1008);
return false;
}
socket.send(
encode({
type: 'manifest',
v: MESH_PROTOCOL_VERSION,
scopes: deps.manifest.scopes,
routes: deps.manifest.routes,
}),
);
deps.bus?.emit('mesh:connect', { name: 'teapot' });
return true;
}
/** Resolve an authenticated rpc-req against the exported scopes/routes, enforcing export gating. */
function resolveRpc(
frame: RpcReqFrame,
deps: MeshControlDeps,
exportedScopes: Set<string>,
exportedRoutes: Set<string>,
): Promise<unknown> {
const { kind, name, ctx } = frame;
if (kind === 'scope') {
if (!exportedScopes.has(name)) throw notExported(name);
return deps.resolveScope(name, ctx);
}
if (![...exportedRoutes].some((key) => key.endsWith(` ${name}`))) throw notExported(name);
return deps.resolveRoute(name, ctx);
}
/** Serve one authenticated rpc-req: resolve it and frame the ok/error response back to the peer. */
async function handleRpc(
socket: WsSocket,
frame: RpcReqFrame,
deps: MeshControlDeps,
exportedScopes: Set<string>,
exportedRoutes: Set<string>,
): Promise<void> {
const { id, name } = frame;
try {
const result = await resolveRpc(frame, deps, exportedScopes, exportedRoutes);
socket.send(encode({ type: 'rpc-res', id, ok: true, result }));
} catch (err) {
const status = isHttpError(err) ? err.status : ((err as any)?.status ?? 500);
deps.bus?.emit('mesh:rpc:error', { name, error: err });
socket.send(encode({ type: 'rpc-res', id, ok: false, error: { message: (err as Error).message, status } }));
}
}
/** Drive one control connection to completion: handshake, then serve RPCs until the socket ends. */
async function serveFrames(
socket: WsSocket,
deps: MeshControlDeps,
exportedScopes: Set<string>,
exportedRoutes: Set<string>,
): Promise<void> {
let authed = false;
try {
for await (const data of socket.inbound) {
let frame: Frame;
try {
frame = decode(String(data));
} catch {
continue; // undecodable frames are ignored, as before
}
if (!authed) {
authed = handleHandshake(socket, frame, deps);
continue;
}
// answered only after the handshake: an unauthenticated peer gets no signal at all
if (frame.type === 'ping') {
socket.send(encode({ type: 'pong' }));
continue;
}
if (frame.type !== 'rpc-req') continue;
// deliberately not awaited: RPCs overlap, exactly as they did under the `ws.on('message')`
// listener. The id-keyed `pending` map on the peer exists to allow in-flight concurrency —
// awaiting here would silently serialize every mesh call.
void handleRpc(socket, frame, deps, exportedScopes, exportedRoutes);
}
} catch {
/* socket failed; the disconnect emit below is the same signal a clean close gives */
} finally {
deps.bus?.emit('mesh:disconnect', { name: 'teapot' });
}
}
/** Build a control handler that authenticates peers and serves scope/route RPCs from the manifest. */
export function createMeshControl(deps: MeshControlDeps): {
path: string;
handle: (socket: WsSocket) => Promise<void>;
} {
const exportedScopes = new Set(deps.manifest.scopes.map((scope) => scope.token));
const exportedRoutes = new Set(deps.manifest.routes.map((route) => `${route.method} ${route.pattern}`));
return {
path: MESH_CONTROL_PATH,
/**
* Resolves when the connection ends, mirroring `runWsConnection` — so `app.upgrade` can await
* the control channel's lifetime the same way it awaits a regular ws route's.
* `channel` is fan-out and subscribes when the async iterator is created, which `for await`
* does synchronously, so the loop is live before any frame can arrive.
*/
handle(socket: WsSocket): Promise<void> {
return serveFrames(socket, deps, exportedScopes, exportedRoutes);
},
};
}