Skip to content

Commit dfb0306

Browse files
committed
fix(node/http2): detect client disconnects on Node 26
1 parent 0ba6e48 commit dfb0306

1 file changed

Lines changed: 32 additions & 13 deletions

File tree

‎src/adapters/_node/request.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ function isClientGone(req: NodeServerRequest): boolean {
7171
return req.aborted || !!req.errored || (req.destroyed && !req.complete);
7272
}
7373

74+
/**
75+
* Whether the request stream can no longer deliver the rest of its body, so
76+
* anything still waiting on it would wait forever: a client that hung up, or a
77+
* source that is finished (destroyed on HTTP/1, EOF pushed on HTTP/2 — see
78+
* `#readBuffered()`) because someone already consumed it out of band.
79+
*
80+
* Keyed off the request's own state rather than what `Readable.toWeb()` makes of
81+
* it: Node <= 25 handed back a pre-cancelled (and therefore disturbed) stream for
82+
* an unreadable source, which is the only thing the `body` getter used to key
83+
* off; Node 26 hands back a clean, empty one instead, so a client that hung up
84+
* mid-body read as a complete zero-byte request.
85+
*/
86+
function isBodySourceFinished(req: NodeServerRequest): boolean {
87+
return isClientGone(req) || req.destroyed || req.readableEnded;
88+
}
89+
7490
export const NodeRequest: {
7591
new (nodeCtx: NodeRequestContext): ServerRequest;
7692
} = /* @__PURE__ */ (() => {
@@ -264,19 +280,22 @@ export const NodeRequest: {
264280
// fresh `Readable.toWeb` would produce a stream whose `end` never fires).
265281
let stream: ReadableStream | null = null;
266282
if (this.#hasBody() && !this.#bodyUsed) {
267-
stream = Readable.toWeb(this.#req as NodeJS.ReadableStream) as unknown as ReadableStream;
268-
// `toWeb()` hands back an already-cancelled (and therefore disturbed)
269-
// stream when the source can no longer be read — a client that hung up
270-
// before the handler read the body. (HTTP/1 reaches that via
271-
// `isDestroyed()`; `Http2ServerRequest`, which never destroys itself,
272-
// via `!isReadable()` once the compat layer has pushed EOF on close.)
273-
// Passing that to `new Request()` throws
274-
// undici's "Response body object should not be disturbed or locked"
275-
// TypeError out of `_request`, which tells the handler nothing about
276-
// what happened. Serve a stream carrying the abort reason instead, so
277-
// every read (including through the native Request) rejects with it.
278-
if (Readable.isDisturbed(stream as unknown as NodeJS.ReadableStream)) {
283+
// A source with nothing left to give must not be wrapped: what a read
284+
// of the resulting stream does is entirely up to the Node version.
285+
// Node <= 25 handed back an already-cancelled (and therefore disturbed)
286+
// stream, so `new Request()` threw undici's "Response body object should
287+
// not be disturbed or locked" TypeError out of `_request`, which tells
288+
// the handler nothing about what happened; Node 26 hands back a clean,
289+
// empty one, so a client that hung up mid-body read as a complete
290+
// zero-byte request — a silently truncated body, which is worse.
291+
// Serve a stream carrying the abort reason instead, so every read
292+
// (including through the native Request) rejects with it.
293+
if (isBodySourceFinished(this.#req)) {
279294
stream = erroredStream(this.#bodyError());
295+
} else {
296+
stream = Readable.toWeb(
297+
this.#req as NodeJS.ReadableStream,
298+
) as unknown as ReadableStream;
280299
}
281300
}
282301
// Enforce `maxRequestBodySize` at the single choke point every consumer funnels
@@ -349,7 +368,7 @@ export const NodeRequest: {
349368
// whatever it buffered, leaving a live-looking stream with nothing left to
350369
// give, so `readableEnded` is what catches it. `#bodyError()` picks the
351370
// right rejection for each.
352-
if (isClientGone(this.#req) || this.#req.destroyed || this.#req.readableEnded) {
371+
if (isBodySourceFinished(this.#req)) {
353372
return Promise.reject(this.#bodyError());
354373
}
355374
return readBody(this.#req, this.#maxRequestBodySize);

0 commit comments

Comments
 (0)