6 Commits
Author SHA1 Message Date
chrnx 07d9b7441c fix(mesh): RPC frames wait for the send buffer too (CARD-26, #41)
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 19s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 34s
CARD-26 made the WebSocket route pump await `drained()` before each pull,
but mesh RPC traffic sends outside that pump, so it never waited:

- teapot: `handleRpc` runs concurrently per rpc-req, so a teacup that
  keeps sending requests and stops reading grows the teapot's buffer by
  one response each.
- link: `sendRpc` sent each rpc-req at once, whatever the buffer held.

Both now wait for `drained()` before sending. The teapot drops a response
whose socket closed while waiting. The link starts its timeout before
waiting, so a teapot that stops reading still answers 504, and a call
that settles while waiting (timeout or close) is never sent late.

Found in the pre-freeze integrated review.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-24 14:08:35 -05:00
chrnx bb3d759f3a Merge pull request 'fix(http): a handler-set ETag array is still the handler's (CARD-15)' (#151) from fix/etag-array-header into develop
CI / dco (push) Has been skipped
CI / runtimes (bun) (push) Successful in 19s
CI / build (push) Successful in 29s
CI / runtimes (edge) (push) Successful in 18s
CI / runtimes (deno) (push) Successful in 32s
Promote → GitHub / push-github (push) Successful in 6s
2026-10-24 19:08:27 +00:00
chrnx d1abc4951a Merge pull request 'chore: clear the dependency advisories blocking CI' (#150) from chore/npm-audit-2026-10-24 into develop
CI / dco (push) Has been skipped
CI / runtimes (bun) (push) Successful in 20s
CI / build (push) Successful in 29s
CI / runtimes (edge) (push) Successful in 19s
CI / runtimes (deno) (push) Successful in 32s
Promote → GitHub / push-github (push) Successful in 6s
2026-10-24 19:06:54 +00:00
chrnx 0017093626 chore: mirror the new overrides in deno.lock
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 18s
CI / runtimes (deno) (pull_request) Successful in 33s
deno.lock records package.json's overrides, so the runtimes (deno) job's
publish dry-run found it out of date after the bump and aborted.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-24 13:59:25 -05:00
chrnx 3efe049bb5 fix(http): a handler-set ETag array is still the handler's (CARD-15)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 30s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 33s
With `etag: true`, `tagged()` only recognised a handler ETag given as a
string. Given as an array, it compared If-None-Match against the body's
computed tag while sending the handler's array, so a client revalidating
with the tag it was sent got a 200 instead of a 304, and two ETag values
went on the wire.

ETag is single-valued, so the last value of an array wins, as a repeated
`setHeader` would. It is sent alone and drives the comparison.

Found in the pre-freeze integrated review.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-24 13:55:06 -05:00
chrnx 74dbbe0b1a chore: clear the dependency advisories blocking CI
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / build (pull_request) Successful in 29s
CI / runtimes (deno) (pull_request) Failing after 33s
`npm audit --audit-level=moderate` fails on develop: six advisories, all
in devDependencies, none in what ships.

- body-parser (high) and fast-uri (moderate): a plain `npm audit fix`.
- undici (high): the existing override pinned 7.29.1, now inside the
  advisory range. Move it to 7.30.1. `--force` wanted a miniflare
  downgrade instead.
- multer (high): reached through @nestjs/platform-express, the bench's
  Nest comparison. 2.5.0 carries the fix. `--force` wanted a Nest
  downgrade, so add an override like the others.

Verified: audit reports 0 vulnerabilities; lint, typecheck, `npm test`
(1149), test:edge (exercises miniflare on the new undici), test:bun,
test:deno and build all pass.

test/mesh/large-body.test.ts T2 fails about 1 run in 10 with EPIPE. That
rate is the same with and without this change (2/20 each), and Node's
fetch uses its bundled undici, not this one. Tracked separately.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-24 13:53:51 -05:00
9 changed files with 136 additions and 34 deletions

No files matched your search

Generated
+3 -2
View File
@@ -56,9 +56,10 @@
"uuid": "11.1.1"
}
},
"undici": "7.29.1",
"undici": "7.30.1",
"sharp": "0.35.5",
"fastify": "$fastify"
"fastify": "$fastify",
"multer": "2.5.0"
}
}
}
+24 -24
View File
@@ -1814,9 +1814,9 @@
}
},
"node_modules/@nestjs/platform-express": {
"version": "11.2.7",
"resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-11.2.7.tgz",
"integrity": "sha512-+F0hV/4yJM+D/Qwyo5XmKMe6YGEb+ssT2jFWrgdFe1obumXCUeC1GNob/kNeXyzAXm7dcxZVUtmcoqaF6z36jA==",
"version": "11.2.8",
"resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-11.2.8.tgz",
"integrity": "sha512-aSAAz8E31qv+sDsL0s7Cw/HoXmeM0TVf35ny17BjLL10c2PgFDp2vunycL7muVyp3/IP5TStMb3Ia4B/RTNHZA==",
"dev": true,
"license": "MIT",
"dependencies": {
@@ -3164,19 +3164,19 @@
"license": "MIT"
},
"node_modules/body-parser": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.3.0.tgz",
"integrity": "sha512-2cGmJupaNgg+QUwVLAucDuWuoMZ6EX9iHDRswZ5lsNYEmwPaRknMPCLZz07yTzVq/83p4o/wzbDZbBrTvGGTIw==",
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.4.0.tgz",
"integrity": "sha512-Ifk1KcLrxaGt1occQagQNFNNOqRt1LFuehEnHHzB8Z6cRgIxRU1TBaiIsUiCdA+gy5PL7Y1IX31DNKkRmxtXjQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"bytes": "^3.1.2",
"content-type": "^2.0.0",
"content-type": "^2.1.0",
"debug": "^4.4.3",
"http-errors": "^2.0.1",
"iconv-lite": "^0.7.2",
"iconv-lite": "^0.7.3",
"on-finished": "^2.4.1",
"qs": "^6.15.2",
"qs": "^6.16.1",
"raw-body": "^3.0.2",
"type-is": "^2.1.0"
},
@@ -3189,9 +3189,9 @@
}
},
"node_modules/body-parser/node_modules/content-type": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/content-type/-/content-type-2.0.0.tgz",
"integrity": "sha512-j/O/d7GcZCyNl7/hwZAb606rzqkyvaDctLmckbxLzHvFBzTJHuGEdodATcP3yIRoDrLHkIATJuvzbFlp/ki2cQ==",
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/content-type/-/content-type-2.1.0.tgz",
"integrity": "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag==",
"dev": true,
"license": "MIT",
"engines": {
@@ -4267,9 +4267,9 @@
"license": "MIT"
},
"node_modules/fast-uri": {
"version": "3.1.8",
"resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.8.tgz",
"integrity": "sha512-GZMtZUTNRpOVIECoXwLNZS5xUGE+mVNbTB8h/7Rwh2TFWcBQiPzTgyZi05BF9UMZKkLJv8XBRJTlU7zg8+ZfMg==",
"version": "3.1.9",
"resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.1.9.tgz",
"integrity": "sha512-hS/0Wt3Ya043oKXs0sIxrMpKQITXhMLIrVAKKXi4gozGC9v7YRWVnXIZzjNPTO02M9BdxLCWqK1yRXgNziVyJQ==",
"dev": true,
"funding": [
{
@@ -5545,9 +5545,9 @@
"license": "MIT"
},
"node_modules/multer": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/multer/-/multer-2.4.0.tgz",
"integrity": "sha512-7dqa0ZcFfzbefdTuIkzOSMvZWC0J7FLqBOjJUZvDCXShIURWKxAyTT1wHhnE5q19c7jOJf43IYYKjBmZVZmvhg==",
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/multer/-/multer-2.5.0.tgz",
"integrity": "sha512-BsqOzYGVq49JcW2IzoSAxeKYRevb+dGGPPWleOR8Lrlk4igwxu1/9pwSXlByhCb7MVZDrNJFC0IBP5/O5nLYRw==",
"dev": true,
"license": "MIT",
"dependencies": {
@@ -6196,9 +6196,9 @@
}
},
"node_modules/qs": {
"version": "6.16.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.16.0.tgz",
"integrity": "sha512-h6fhOIaRrID2CbEY2fqs+7t+UXZo+MLAnU5gRIq85uFtdiUPCdsApMlHhXogKVM4HM2DVbIjGNTTYH2OcmP1vA==",
"version": "6.16.1",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.16.1.tgz",
"integrity": "sha512-5Px6J7rfHBAGUhNQ3es4YzcOdBBw0SDcVtThJml4RdnKw4tKs2epA0kPZ+K8X+WiHktT93RHVcQ/3ym5aTCgtg==",
"dev": true,
"license": "BSD-3-Clause",
"dependencies": {
@@ -7808,9 +7808,9 @@
}
},
"node_modules/undici": {
"version": "7.29.1",
"resolved": "https://registry.npmjs.org/undici/-/undici-7.29.1.tgz",
"integrity": "sha512-RYONW2MeafgYlkVOKYKkA/Ag7BmXqgIWCa8t1m0JcxrQg9pI9lEqRhAOruOBCbAohOa/gkCF+iPi9hrgvTzu6Q==",
"version": "7.30.1",
"resolved": "https://registry.npmjs.org/undici/-/undici-7.30.1.tgz",
"integrity": "sha512-03KrMbjIFDWQR5RNPrSw3bd1r12JpyFQl9/yxjUbJtqkjmllqC5lFjZh6ujeuJCKr8EOQvRW872Oy/az+nLYxg==",
"dev": true,
"license": "MIT",
"engines": {
+3 -2
View File
@@ -130,9 +130,10 @@
"uuid": "11.1.1"
}
},
"undici": "7.29.1",
"undici": "7.30.1",
"sharp": "0.35.5",
"fastify": "$fastify"
"fastify": "$fastify",
"multer": "2.5.0"
},
"devDependencies": {
"@eslint/js": "^9.39.5",
+4 -2
View File
@@ -191,8 +191,10 @@ function tagged(
): Buffered {
const key = findHeader(result.headers, 'etag');
const existing = key === undefined ? undefined : result.headers[key];
const etag = typeof existing === 'string' ? existing : bodyEtag(result.body);
const headers = key === undefined ? { ...result.headers, etag } : result.headers;
// ETag is single-valued: from an array, the last value wins, as a repeated `setHeader` would.
const own = Array.isArray(existing) ? existing[existing.length - 1] : existing;
const etag = own ?? bodyEtag(result.body);
const headers = { ...result.headers, [key ?? 'etag']: etag };
if (isNotModified(req.headers, etag)) return bodyless({ kind: 'buffered', status: 304, headers, body: '' });
+5 -1
View File
@@ -119,7 +119,11 @@ function sendRpc(
}, timeoutMs);
pending.set(id, { name, resolve: resolveCall, reject: rejectCall, timer });
socket.send(encode({ type: 'rpc-req', id, kind, name, ctx }));
// Not before the socket has room (#41); the timeout already runs, so a teapot that stops
// reading still answers 504. Settled while waiting (timeout, close): nothing left to send.
void (socket.drained?.() ?? Promise.resolve()).then(() => {
if (pending.has(id) && !socket.abort.aborted) socket.send(encode({ type: 'rpc-req', id, kind, name, ctx }));
});
});
}
+11 -2
View File
@@ -170,6 +170,15 @@ function resolveRpc(
return deps.resolveRoute(name, ctx);
}
/**
* Sends one frame once the socket has room (#41): RPCs are served concurrently, so without the wait
* a peer that stops reading grows the send buffer by one response per request it keeps sending.
*/
async function reply(socket: WsSocket, frame: Frame): Promise<void> {
if (socket.drained) await socket.drained();
if (!socket.abort.aborted) socket.send(encode(frame));
}
/** Serve one authenticated rpc-req: resolve it and frame the ok/error response back to the peer. */
async function handleRpc(
socket: WsSocket,
@@ -196,12 +205,12 @@ async function handleRpc(
throw error;
}
socket.send(encode({ type: 'rpc-res', id, ok: true, result }));
await reply(socket, { 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 } }));
await reply(socket, { type: 'rpc-res', id, ok: false, error: { message: (err as Error).message, status } });
}
}
+14
View File
@@ -29,6 +29,12 @@ class Handlers {
return null;
}
@Get('/own-list')
@Transformer(() => ({ headers: { ETag: ['"v6"', '"v7"'] }, body: 'v7' }))
ownList() {
return null;
}
@Post('/post')
post() {
return { n: 1 };
@@ -182,6 +188,14 @@ describe('handlers (etag: true)', () => {
expect(res.headers.get('etag')).toBe('"v7"');
});
it("T6: a handler-set ETag array is still the handler's: the last value is sent and compared", async () => {
const first = await req(etagApp, '/h/own-list');
expect(first.headers.get('etag')).toBe('"v7"');
const res = await req(etagApp, '/h/own-list', { headers: { 'if-none-match': '"v7"' } });
expect(res.status).toBe(304);
expect(res.headers.get('etag')).toBe('"v7"');
});
it('T7/T8: a list containing the tag, and *', async () => {
const etag = (await req(etagApp, '/h/json')).headers.get('etag')!;
const list = await req(etagApp, '/h/json', { headers: { 'if-none-match': `"a", ${etag}, "c"` } });
+38 -1
View File
@@ -11,6 +11,7 @@ const env = { method: 'GET', params: {}, query: {}, body: undefined, headers: {}
// teapot that validates secret 'good', returns a manifest, answers scope rpc (boom -> error)
function startTeapot(secret = 'good') {
const wss = new WebSocketServer({ port: 0 });
const names: string[] = []; // every rpc-req name received, in order
wss.on('connection', (ws) => {
let authed = false;
ws.on('message', (d) => {
@@ -21,13 +22,14 @@ function startTeapot(secret = 'good') {
return;
}
if (f.type === 'rpc-req') {
names.push(f.name);
if (f.name === 'boom') ws.send(encode({ type: 'rpc-res', id: f.id, ok: false, error: { message: 'denied', status: 403 } }));
else ws.send(encode({ type: 'rpc-res', id: f.id, ok: true, result: { got: f.name } }));
}
});
});
const port = (wss.address() as any).port;
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, close: () => wss.close() };
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, names, close: () => wss.close() };
}
// teapot that completes the handshake but NEVER answers rpc-req (for timeout)
@@ -135,4 +137,39 @@ describe('connectLink', () => {
await expect(link.rpc('scope', 'whatever', env)).rejects.toThrow(/timeout/i);
link.close(); t.close();
});
/** The `ws` client, reporting a send buffer over the 1 MiB mark while `stalled`. */
let stalled = false;
const realBuffered = Object.getOwnPropertyDescriptor(WS.prototype, 'bufferedAmount')!.get!;
class Backed extends WS {
constructor(url: string) {
super(url);
Object.defineProperty(this, 'bufferedAmount', { get: () => (stalled ? 2_000_000 : realBuffered.call(this)) });
}
}
const BackedCtor = Backed as unknown as SocketCtor;
it('holds an rpc-req while the link is backed up, and sends it once it drains (#41)', async () => {
const t = startTeapot();
const link = await connectLink({ url: t.url, secret: 'good', Ctor: BackedCtor });
stalled = true;
let settled = false;
const call = link.rpc('scope', 'auth', env).finally(() => (settled = true));
await new Promise((resolve) => setTimeout(resolve, 60));
expect(settled).toBe(false);
stalled = false;
expect(await call).toEqual({ got: 'auth' });
link.close(); t.close();
});
it('times out an rpc still waiting for room, and never sends it (#41)', async () => {
const t = startTeapot();
const link = await connectLink({ url: t.url, secret: 'good', timeoutMs: 100, Ctor: BackedCtor });
stalled = true;
await expect(link.rpc('scope', 'auth', env)).rejects.toMatchObject({ status: 504 });
stalled = false;
expect(await link.rpc('scope', 'next', env)).toEqual({ got: 'next' });
expect(t.names).toEqual(['next']); // the timed-out call was dropped, not sent late
link.close(); t.close();
});
});
+34
View File
@@ -165,4 +165,38 @@ describe('createMeshControl', () => {
const ids = t.sent.filter((f: any) => f.type === 'rpc-res').map((f: any) => f.id);
expect(ids).toEqual(['fast', 'slow']);
});
it('holds rpc responses while the peer is backed up, and sends them once it drains (#41)', async () => {
const t = fakeSocket();
let open: () => void = () => {};
let gate: Promise<void> | undefined;
t.socket.drained = () => gate ?? Promise.resolve();
make().handle(t.socket);
await t.deliver({ type: 'hello', v: V, secret: 'good' });
gate = new Promise<void>((resolve) => (open = resolve));
await t.deliver({ type: 'rpc-req', id: '1', kind: 'scope', name: 'config', ctx: {} });
await t.deliver({ type: 'rpc-req', id: '2', kind: 'scope', name: 'secretsvc', ctx: {} });
expect(t.sent.filter((f: any) => f.type === 'rpc-res')).toEqual([]);
open();
await settle();
expect(t.sent.filter((f: any) => f.type === 'rpc-res').map((f: any) => [f.id, f.ok])).toEqual([
['1', true],
['2', false],
]);
});
it('drops a held response when the socket closes while waiting (#41)', async () => {
const t = fakeSocket();
let gate: Promise<void> | undefined;
t.socket.drained = () => gate ?? Promise.resolve();
make().handle(t.socket);
await t.deliver({ type: 'hello', v: V, secret: 'good' });
let open: () => void = () => {};
gate = new Promise<void>((resolve) => (open = resolve));
await t.deliver({ type: 'rpc-req', id: '1', kind: 'scope', name: 'config', ctx: {} });
t.close();
open(); // a real socket releases its drained() waiters on close
await settle();
expect(t.sent.filter((f: any) => f.type === 'rpc-res')).toEqual([]);
});
});