Compare commits
3
Commits
a2f905fa30
...
b23b4e4161
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b23b4e4161 | ||
|
|
4e02cdc351 | ||
|
|
07d9b7441c |
No files matched your search
+7
-6
@@ -99,6 +99,13 @@ npm treats versions as semver and semver forbids leading zeros.
|
|||||||
`app.upgrade` already did. An app with no `@Ws`/`@Stream` routes and no mesh attaches no upgrade
|
`app.upgrade` already did. An app with no `@Ws`/`@Stream` routes and no mesh attaches no upgrade
|
||||||
listener on Node, as before, so there the request still reaches the HTTP route table.
|
listener on Node, as before, so there the request still reaches the HTTP route table.
|
||||||
|
|
||||||
|
- **Static files are revalidated, not re-downloaded** (#114, D12). Every static hit carries a weak
|
||||||
|
`ETag` (`W/"<size>-<mtime>"`), `Last-Modified` and `Cache-Control: no-cache`. A matching
|
||||||
|
`If-None-Match`, or `If-Modified-Since` when there is no `If-None-Match`, answers `304` from the
|
||||||
|
file's `stat` without reading it. A `HEAD` no longer reads the file either. `no-cache` stops
|
||||||
|
browsers from reusing a file for a lifetime they guessed from `Last-Modified`. To keep the old
|
||||||
|
behaviour, set `static: { cacheControl }` to whatever your proxy expects.
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- **Health probes: `createApp({ health: true })`** (#115). Off by default. `GET`/`HEAD /__live__`
|
- **Health probes: `createApp({ health: true })`** (#115). Off by default. `GET`/`HEAD /__live__`
|
||||||
@@ -177,12 +184,6 @@ npm treats versions as semver and semver forbids leading zeros.
|
|||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- **Static files are revalidated, not re-downloaded** (#114, D12). Every static hit carries a weak
|
|
||||||
`ETag` (`W/"<size>-<mtime>"`), `Last-Modified` and `Cache-Control: no-cache`. A matching
|
|
||||||
`If-None-Match`, or `If-Modified-Since` when there is no `If-None-Match`, answers `304` from the
|
|
||||||
file's `stat` without reading it. A `HEAD` no longer reads the file either. `no-cache` stops
|
|
||||||
browsers from reusing a file for a lifetime they guessed from `Last-Modified`. To keep the old
|
|
||||||
behaviour, set `static: { cacheControl }` to whatever your proxy expects.
|
|
||||||
- **Every `304` is bodiless** (#114). A `304` has no body and no `content-*` headers on any adapter,
|
- **Every `304` is bodiless** (#114). A `304` has no body and no `content-*` headers on any adapter,
|
||||||
including one from `throw new NotModified()`, which used to go out with the default JSON
|
including one from `throw new NotModified()`, which used to go out with the default JSON
|
||||||
envelope's `content-type`.
|
envelope's `content-type`.
|
||||||
|
|||||||
+5
-1
@@ -119,7 +119,11 @@ function sendRpc(
|
|||||||
}, timeoutMs);
|
}, timeoutMs);
|
||||||
|
|
||||||
pending.set(id, { name, resolve: resolveCall, reject: rejectCall, timer });
|
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);
|
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. */
|
/** Serve one authenticated rpc-req: resolve it and frame the ok/error response back to the peer. */
|
||||||
async function handleRpc(
|
async function handleRpc(
|
||||||
socket: WsSocket,
|
socket: WsSocket,
|
||||||
@@ -196,12 +205,12 @@ async function handleRpc(
|
|||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.send(encode({ type: 'rpc-res', id, ok: true, result }));
|
await reply(socket, { type: 'rpc-res', id, ok: true, result });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const status = isHttpError(err) ? err.status : ((err as any)?.status ?? 500);
|
const status = isHttpError(err) ? err.status : ((err as any)?.status ?? 500);
|
||||||
|
|
||||||
deps.bus?.emit('mesh:rpc:error', { name, error: err });
|
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 } });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+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)
|
// teapot that validates secret 'good', returns a manifest, answers scope rpc (boom -> error)
|
||||||
function startTeapot(secret = 'good') {
|
function startTeapot(secret = 'good') {
|
||||||
const wss = new WebSocketServer({ port: 0 });
|
const wss = new WebSocketServer({ port: 0 });
|
||||||
|
const names: string[] = []; // every rpc-req name received, in order
|
||||||
wss.on('connection', (ws) => {
|
wss.on('connection', (ws) => {
|
||||||
let authed = false;
|
let authed = false;
|
||||||
ws.on('message', (d) => {
|
ws.on('message', (d) => {
|
||||||
@@ -21,13 +22,14 @@ function startTeapot(secret = 'good') {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (f.type === 'rpc-req') {
|
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 } }));
|
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 } }));
|
else ws.send(encode({ type: 'rpc-res', id: f.id, ok: true, result: { got: f.name } }));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
const port = (wss.address() as any).port;
|
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)
|
// 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);
|
await expect(link.rpc('scope', 'whatever', env)).rejects.toThrow(/timeout/i);
|
||||||
link.close(); t.close();
|
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);
|
const ids = t.sent.filter((f: any) => f.type === 'rpc-res').map((f: any) => f.id);
|
||||||
expect(ids).toEqual(['fast', 'slow']);
|
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