Compare commits
6
Commits
77d4f32eff
...
07d9b7441c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07d9b7441c | ||
|
|
bb3d759f3a | ||
|
|
d1abc4951a | ||
|
|
0017093626 | ||
|
|
3efe049bb5 | ||
|
|
74dbbe0b1a |
No files matched your search
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+24
-24
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 } });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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([]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user