refactor: close type holes on the transport and error axes
- OpenApiRoute.transport was typed `string`, so the compiler could not check the transport branching at all. Type it Transport and key the documented 200 off a Record, so a new transport fails the build instead of silently going undocumented. - pickEncoder fell through to ndjson for unknown transports; make the mapping exhaustive so a new transport cannot inherit the wrong wire format by accident. - HttpError carries optional headers, removing the instanceof Redirect special case that coupled the error renderer to one subclass. Custom errors needing headers (retry-after, etag) no longer require touching transformers. - Drop the module-global connSeq in mesh/link: `pending` is already per-connection, so a per-link counter suffices and connection ids stop depending on test order. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+55
-36
No files matched your search
+18
-4
@@ -6,11 +6,25 @@ import type { Bus } from '../bus';
|
||||
|
||||
const PING_MS = 15_000;
|
||||
|
||||
/** Selects the stream encoder for a transport, falling back to the client's `Accept` header for `stream`. */
|
||||
/**
|
||||
* Stream encoder per transport, or `null` for transports that never produce an HTTP stream.
|
||||
* A new transport must add a row — the compiler fails until it does, so it can't silently
|
||||
* inherit ndjson framing.
|
||||
*/
|
||||
const STREAM_ENCODERS: Record<Transport, ((accept: string) => StreamEncoder) | null> = {
|
||||
buffer: null, // a buffered handler returning a stream throws TransportMismatchError before reaching here
|
||||
ws: null, // not an HTTP response; ws routes are served via upgrade, not handle()
|
||||
sse: () => sseEncoder,
|
||||
ndjson: () => ndjsonEncoder,
|
||||
negotiate: (accept) => (accept.includes('text/event-stream') ? sseEncoder : ndjsonEncoder),
|
||||
};
|
||||
|
||||
/** Selects the stream encoder for a transport, falling back to the client's `Accept` header for `negotiate`. */
|
||||
export function pickEncoder(transport: Transport, accept: string): StreamEncoder {
|
||||
if (transport === 'sse') return sseEncoder;
|
||||
if (transport === 'ndjson') return ndjsonEncoder;
|
||||
return accept.includes('text/event-stream') ? sseEncoder : ndjsonEncoder;
|
||||
const pick = STREAM_ENCODERS[transport];
|
||||
if (!pick) throw new Error(`transport '${transport}' does not produce an HTTP stream`);
|
||||
|
||||
return pick(accept);
|
||||
}
|
||||
|
||||
/** Pipes an async-iterable result to the response as an HTTP stream, with keep-alive pings and backpressure handling. */
|
||||
|
||||
Reference in new issue
Block a user