`pipeStream` emitted `stream:open`/`:close`/`:error` and wrote the encoder's error frame. `asReadableStream` — the path every Fetch runtime takes, so Deno, Bun, workerd and `app.fetch()` on Node — emitted nothing and called `controller.error(err)`. Both halves of that were silent. A consumer counting `stream:error` saw zero on three of the four runtimes while the streams were failing normally, and the client got a truncated body with no explanation, which from the outside is indistinguishable from a clean end of stream. The observability guide documents these three events without qualification; on three runtimes they did not exist. The error path now frames rather than breaks. Enqueue `encodeError`'s frame, then close cleanly — the Node behaviour, and the better one: an SSE consumer that received an `error` event knows what happened, where a transport-level failure looks exactly like the connection dropping. `controller.error` is kept for an encoder with no error frame to offer, because then breaking the stream is the only signal left. `stream:close` is latched. A `cancel` can land after the body already ended, and the three exits — done, error, cancel — must not each emit their own. The test drives both adapters from one route table rather than asserting them in separate blocks, since a table that describes one and then the other is how the two drifted in the first place. Verified non-vacuous: reverting `src/http/web.ts` fails all four fetch rows and leaves the three Node ones green, which is the split that says the test is measuring the adapter and not the framework. The `sse()` injection test from the previous commit went back to `app.fetch`. It had been written against `listen()` only because the fetch path swallowed the error — that was a workaround for this bug, and it is no longer needed. Closes #83 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
268 lines
11 KiB
TypeScript
268 lines
11 KiB
TypeScript
import type { StreamEncoder } from '../encoders';
|
|
import type { Bus } from '../bus';
|
|
import { createRequestGate } from './request-gate';
|
|
import {
|
|
handle,
|
|
computeInjected,
|
|
correlateRequest,
|
|
emitShedPair,
|
|
watchingRequests,
|
|
type HandleResult,
|
|
type Preflight,
|
|
} from './core';
|
|
import { mergeInjectedHeaders } from './headers';
|
|
import type { BodyFailure, BodyReader, MaybePromise } from './body';
|
|
import { HttpError } from '../signals';
|
|
import { renderError, type ErrorRequest } from '../transformers';
|
|
import type { RouteDef, HttpOptions } from './types';
|
|
|
|
/**
|
|
* Wraps an AsyncIterable as a web ReadableStream, encoding each value. Cancels the source on stream
|
|
* cancel, and reports the connection on the bus exactly as {@link pipeStream} does for Node — the
|
|
* lifecycle of a stream must not depend on which adapter is serving it.
|
|
*/
|
|
export function asReadableStream(
|
|
source: AsyncIterable<unknown>,
|
|
encoder: StreamEncoder,
|
|
bus?: Bus,
|
|
name = '',
|
|
): ReadableStream<Uint8Array> {
|
|
const iterator = source[Symbol.asyncIterator]();
|
|
const te = new TextEncoder();
|
|
const frame = (chunk: string | Buffer): Uint8Array => (typeof chunk === 'string' ? te.encode(chunk) : chunk);
|
|
// `cancel` can land after the body has already ended, and an errored stream still unwinds through
|
|
// its own path — so the close is latched rather than emitted from each of the three exits.
|
|
let closed = false;
|
|
|
|
const close = (): void => {
|
|
if (closed) return;
|
|
closed = true;
|
|
bus?.emit('stream:close', { name });
|
|
};
|
|
|
|
return new ReadableStream<Uint8Array>({
|
|
start() {
|
|
bus?.emit('stream:open', { name });
|
|
},
|
|
async pull(controller) {
|
|
try {
|
|
const { value, done } = await iterator.next();
|
|
|
|
if (done) {
|
|
close();
|
|
controller.close();
|
|
return;
|
|
}
|
|
|
|
controller.enqueue(frame(encoder.encode(value)));
|
|
} catch (err) {
|
|
bus?.emit('stream:error', { name, error: err });
|
|
const errorFrame = encoder.encodeError(err);
|
|
close();
|
|
|
|
// Framed and closed cleanly, which is what the Node adapter does and the better answer for
|
|
// the client: an SSE consumer that received an `error` event knows what happened, where a
|
|
// transport-level failure is indistinguishable from the connection simply dropping.
|
|
// `controller.error` is kept for the encoder that has no error frame to offer, because
|
|
// then breaking the stream is the only signal left.
|
|
if (errorFrame === null) {
|
|
controller.error(err);
|
|
return;
|
|
}
|
|
|
|
controller.enqueue(frame(errorFrame));
|
|
controller.close();
|
|
}
|
|
},
|
|
async cancel() {
|
|
close();
|
|
await iterator.return?.();
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Converts a web `Headers` into the plain record shape the neutral core and body parser expect. */
|
|
function headersToRecord(headers: Headers): Record<string, string> {
|
|
const out: Record<string, string> = {};
|
|
headers.forEach((value, key) => {
|
|
out[key] = value;
|
|
});
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* The fetch half of body acquisition: bytes out of the `Request`, and the over-limit answer.
|
|
*
|
|
* Unlike Node, there is no incremental read to abort — `arrayBuffer()` resolves with the whole body
|
|
* or not at all, so the limit can only be checked once the bytes are already in memory. That
|
|
* difference is exactly why the limit is passed in rather than applied by the shared core.
|
|
*/
|
|
function readFetchBytes(
|
|
source: unknown,
|
|
limit: number,
|
|
opts: HttpOptions | undefined,
|
|
): MaybePromise<{ bytes: Buffer | undefined } | BodyFailure> {
|
|
const request = source as Request;
|
|
// Not `async`: a bodiless request answers without allocating a promise at all.
|
|
if (!request.body) return { bytes: undefined };
|
|
|
|
return request.arrayBuffer().then((arrayBuffer) => {
|
|
if (arrayBuffer.byteLength > limit) {
|
|
// Rebuilt here rather than captured per request: only an over-limit body ever needs it, and
|
|
// a `url`/`headers` pair captured up-front cost more than the work it saved.
|
|
const url = new URL(request.url);
|
|
const errReq: ErrorRequest = {
|
|
method: request.method,
|
|
url: url.pathname + url.search,
|
|
headers: headersToRecord(request.headers),
|
|
};
|
|
return { fail: renderError(new HttpError(413, 'Payload Too Large'), errReq, opts?.onError, opts?.logger) };
|
|
}
|
|
|
|
return { bytes: arrayBuffer.byteLength === 0 ? undefined : Buffer.from(arrayBuffer) };
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Whatever the host runtime's `Response` accepts as a body. Derived from the constructor rather
|
|
* than written as `BodyInit`, because that name is not global under this `lib` (`es2020` plus
|
|
* `@types/node`) even though `Response` itself is — and Deno spells the same idea from its own lib.
|
|
*/
|
|
type ResponseBody = ConstructorParameters<typeof Response>[0];
|
|
|
|
/**
|
|
* Narrows a buffered body to something the runtime's `Response` accepts.
|
|
*
|
|
* A Node `Buffer` is a `Uint8Array` at runtime, so every runtime here takes one — but its declared
|
|
* backing store is `ArrayBufferLike`, which admits `SharedArrayBuffer`, and `BodyInit` accepts only
|
|
* `ArrayBuffer`-backed views. Deno's lib therefore rejects it outright: this is the `app.fetch` path,
|
|
* so the mismatch reaches Deno, Bun and edge alike. Node never allocates a Buffer over a
|
|
* `SharedArrayBuffer`, which is what makes the assertion on the backing store safe to state.
|
|
*
|
|
* `byteOffset`/`byteLength` are load-bearing, not defensive. `Buffer.from` and `Buffer.allocUnsafe`
|
|
* hand back a window into a shared 8 KB pool, so re-viewing the backing store without them yields
|
|
* the whole pool — neighbouring allocations included. Today the only Buffer reaching here comes from
|
|
* `fs.promises.readFile`, which returns an exactly-sized allocation, so nothing currently trips that.
|
|
* `StaticHit.body` is typed `Buffer` though, and a pooled one is a correct value for it.
|
|
*
|
|
* Re-viewing rather than copying: this is the buffered-response path, and a copy per response would
|
|
* be a real cost on the runtimes that have no other adapter.
|
|
*/
|
|
export function toBodyInit(body: string | Buffer): ResponseBody {
|
|
if (typeof body === 'string') return body;
|
|
return new Uint8Array(body.buffer as ArrayBuffer, body.byteOffset, body.byteLength);
|
|
}
|
|
|
|
/** Converts a {@link HandleResult}'s outcome into a web `Response`, buffered or streamed. */
|
|
function outcomeToResponse(result: HandleResult, bus?: Bus): Response {
|
|
const headers = mergeInjectedHeaders(result.outcome.headers, result.injected) as Record<string, string>;
|
|
|
|
if (result.outcome.kind === 'stream') {
|
|
const body = asReadableStream(result.outcome.stream, result.outcome.encoder, bus, result.outcome.route);
|
|
return new Response(body, { headers });
|
|
}
|
|
|
|
const body = [204, 205, 304].includes(result.outcome.status) ? null : toBodyInit(result.outcome.body);
|
|
return new Response(body, { status: result.outcome.status, headers });
|
|
}
|
|
|
|
/**
|
|
* Builds a Web-Standards `fetch(request)` handler over the same route table the Node adapter uses:
|
|
* describes the request for the runtime-neutral {@link handle}, supplies the {@link BodyReader} it
|
|
* calls once a route matches, and converts the outcome to a `Response` (buffered or streamed).
|
|
* Routing, body parsing, limits and error rendering all live in the core; this file is the part
|
|
* that knows how to get bytes out of a `Request` and how to put them into a `Response`.
|
|
*/
|
|
export function buildFetch(routes: RouteDef[], opts: HttpOptions | undefined) {
|
|
// Built once per server, not once per request — see `BodyReader`.
|
|
const readBody: BodyReader = (source, limit) => readFetchBytes(source, limit, opts);
|
|
const gate = createRequestGate(opts?.limits?.maxConcurrentRequests);
|
|
|
|
return async (request: Request): Promise<Response> => {
|
|
const url = new URL(request.url);
|
|
const path = url.pathname + url.search;
|
|
const headers = headersToRecord(request.headers);
|
|
// Derived before the body is read, so a 413 rejected below still carries an identity.
|
|
const correlation = correlateRequest(headers);
|
|
const secure = url.protocol === 'https:';
|
|
// Computed up-front from the same inputs `handle()` uses, so a preflight — the one response
|
|
// this adapter builds itself — carries the same security/CORS headers every other one does.
|
|
const injected = computeInjected(opts, { secure, headers });
|
|
|
|
let result: HandleResult | Preflight;
|
|
let acquired = false;
|
|
const watching = watchingRequests(opts?.bus);
|
|
const startedAt = watching ? performance.now() : 0;
|
|
|
|
try {
|
|
if (!gate.acquire()) {
|
|
if (watching)
|
|
emitShedPair(
|
|
opts!.bus!,
|
|
{
|
|
method: request.method,
|
|
url: path,
|
|
requestId: correlation.requestId,
|
|
traceId: correlation.traceId,
|
|
},
|
|
startedAt,
|
|
);
|
|
|
|
return new Response(JSON.stringify({ error: 'Service Unavailable' }), {
|
|
status: 503,
|
|
headers: mergeInjectedHeaders(
|
|
{
|
|
'content-type': 'application/json',
|
|
'retry-after': '1',
|
|
},
|
|
injected,
|
|
),
|
|
});
|
|
}
|
|
|
|
acquired = gate.limited;
|
|
|
|
result = await handle(
|
|
routes,
|
|
opts,
|
|
{
|
|
...correlation,
|
|
method: request.method,
|
|
url: path,
|
|
headers,
|
|
readBody,
|
|
source: request,
|
|
secure,
|
|
// Mirrors Node's `deriveIp` (src/http/request.ts): only trust `x-forwarded-for` when the
|
|
// proxy is trusted, and take its first hop — an untrusted XFF is client-spoofable, and
|
|
// there's no socket peer address to fall back to on the fetch path, so the untrusted case
|
|
// is simply ''.
|
|
ip: opts?.trustProxy
|
|
? String(headers['x-forwarded-for'] ?? '')
|
|
.split(',')[0]
|
|
.trim()
|
|
: '',
|
|
},
|
|
// Already computed above for the shed-503 path; handing it down keeps `cors.origins` to
|
|
// one call per request.
|
|
injected,
|
|
);
|
|
} finally {
|
|
// Release when routing/handling finishes, not when a returned stream finishes.
|
|
// Long-lived streams therefore do not permanently consume the request budget.
|
|
if (acquired) gate.release();
|
|
}
|
|
|
|
// Mirrors the Node adapter, where `result.preflight` is written through the writeHead patch and
|
|
// so picks up the same authoritative security headers `injected` carries.
|
|
if ('preflight' in result) {
|
|
return new Response(null, {
|
|
status: 204,
|
|
headers: mergeInjectedHeaders(result.preflight, injected),
|
|
});
|
|
}
|
|
|
|
return outcomeToResponse(result, opts?.bus);
|
|
};
|
|
}
|