@@ -47,6 +47,30 @@ function erroredStream(error: unknown): ReadableStream {
4747 } ) ;
4848}
4949
50+ /**
51+ * Whether the client hung up before the request body finished arriving.
52+ *
53+ * The two Node request types report this very differently, and neither one flag
54+ * covers both:
55+ *
56+ * - HTTP/1 `IncomingMessage` is `autoDestroy`, so `destroyed` on its own is *not*
57+ * a disconnect: a fully read body leaves a perfectly healthy request destroyed.
58+ * Only an errored stream, or one destroyed while `complete` is still false, is.
59+ * - HTTP/2 `Http2ServerRequest` is constructed with `autoDestroy: false` and never
60+ * destroys itself, so `destroyed` stays false; `complete` is *true* for an
61+ * aborted (truncated) body because its getter ORs in the abort flag; and its
62+ * `onStreamError` is deliberately a no-op, so `errored` stays null. Every
63+ * HTTP/1 condition above is therefore permanently false there.
64+ *
65+ * `aborted` is what closes the gap. Node's `closeStream()` raises it only when the
66+ * stream closed while the server's writable side was still open — the same "client
67+ * left before we finished responding" test the `res` close handler applies below,
68+ * so it cannot fire on a request that completed normally.
69+ */
70+ function isClientGone ( req : NodeServerRequest ) : boolean {
71+ return req . aborted || ! ! req . errored || ( req . destroyed && ! req . complete ) ;
72+ }
73+
5074export const NodeRequest : {
5175 new ( nodeCtx : NodeRequestContext ) : ServerRequest ;
5276} = /* @__PURE__ */ ( ( ) => {
@@ -198,18 +222,20 @@ export const NodeRequest: {
198222 // destroyed on a perfectly healthy request, while the response is
199223 // still open (`writableEnded === false`) — replaying on that would
200224 // abort every handler that reads the body before touching `signal`.
201- // Only an errored or *incomplete* request stream is a disconnect.
202- if ( res . destroyed || req . errored || ( req . destroyed && ! req . complete ) ) {
225+ // `isClientGone()` is the predicate that holds for both protocols.
226+ // (`res.destroyed` is HTTP/1-only; `Http2ServerResponse` has no such
227+ // property, which is why the request-side check has to carry HTTP/2.)
228+ if ( res . destroyed || isClientGone ( req ) ) {
203229 onClose ( ) ;
204230 }
205231 } else {
206232 const onClose = ( ) => {
207- if ( ! req . complete ) {
233+ if ( ! req . complete || req . aborted ) {
208234 abort ( ) ; // client disconnected
209235 }
210236 } ;
211237 req . once ( "close" , onClose ) ;
212- if ( req . destroyed ) {
238+ if ( isClientGone ( req ) ) {
213239 onClose ( ) ;
214240 }
215241 }
@@ -238,11 +264,13 @@ export const NodeRequest: {
238264 // fresh `Readable.toWeb` would produce a stream whose `end` never fires).
239265 let stream : ReadableStream | null = null ;
240266 if ( this . #hasBody( ) && ! this . #bodyUsed) {
241- // TODO: HTTP2ServerRequest
242267 stream = Readable . toWeb ( this . #req as NodeJS . ReadableStream ) as unknown as ReadableStream ;
243268 // `toWeb()` hands back an already-cancelled (and therefore disturbed)
244- // stream when the source is destroyed — a client that hung up before
245- // the handler read the body. Passing that to `new Request()` throws
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
246274 // undici's "Response body object should not be disturbed or locked"
247275 // TypeError out of `_request`, which tells the handler nothing about
248276 // what happened. Serve a stream carrying the abort reason instead, so
@@ -303,22 +331,25 @@ export const NodeRequest: {
303331 if ( signal . aborted ) {
304332 return signal . reason ;
305333 }
306- // A destroyed-but-`complete` stream delivered its whole body and was then
307- // auto-destroyed: it was consumed out of band (e.g. directly off
308- // `runtime.node.req`), which is an unusable body rather than an abort.
309- return (
310- this . #req. errored ||
311- ( this . #req. destroyed && ! this . #req. complete ? abortError ( ) : bodyUnusable ( ) )
312- ) ;
334+ // A stream that ended without srvx reading it delivered its whole body and
335+ // was consumed out of band (e.g. directly off `runtime.node.req`), which is
336+ // an unusable body rather than an abort.
337+ return this . #req. errored || ( isClientGone ( this . #req) ? abortError ( ) : bodyUnusable ( ) ) ;
313338 }
314339
315340 // Buffer the raw request body once; consumers add their own single
316341 // continuation (`.toString()` / `JSON.parse`) so no extra promise or
317342 // microtask hop is introduced vs. inlining the read.
318343 #readBuffered( ) {
319- // A destroyed source emits no further `data` / `end` / `error`, so
320- // `readBody()` would wait forever on a body that can no longer arrive.
321- if ( this . #req. destroyed || this . #req. errored ) {
344+ // A source that is already finished emits no further `data` / `end` /
345+ // `error`, so `readBody()` would wait forever on a body that can no longer
346+ // arrive. Beyond a disconnect that means a stream someone else consumed:
347+ // HTTP/1 leaves it `destroyed` (autoDestroy), while `Http2ServerRequest`
348+ // never destroys itself — on close the compat layer pushes EOF and dumps
349+ // whatever it buffered, leaving a live-looking stream with nothing left to
350+ // give, so `readableEnded` is what catches it. `#bodyError()` picks the
351+ // right rejection for each.
352+ if ( isClientGone ( this . #req) || this . #req. destroyed || this . #req. readableEnded ) {
322353 return Promise . reject ( this . #bodyError( ) ) ;
323354 }
324355 return readBody ( this . #req, this . #maxRequestBodySize) ;
@@ -533,6 +564,16 @@ function readBody(req: NodeServerRequest, maxRequestBodySize?: number): Promise<
533564 } ;
534565 const onEnd = ( ) => {
535566 cleanup ( ) ;
567+ // HTTP/2 signals a client that hung up mid-body by pushing EOF onto the
568+ // request (`onStreamCloseRequest`), and it does so *before* the `close`
569+ // below — so `end` here can mean "that is the whole body" or "the rest of
570+ // the body will never arrive", and resolving blindly hands the handler a
571+ // silently truncated body it cannot tell from a complete one. The abort
572+ // flag is already set by then, so it separates the two.
573+ if ( isClientGone ( req ) ) {
574+ reject ( req . errored || abortError ( ) ) ;
575+ return ;
576+ }
536577 // Single-chunk bodies (the common case) skip Buffer.concat's alloc+copy
537578 resolve ( chunks . length === 1 ? chunks [ 0 ] : Buffer . concat ( chunks ) ) ;
538579 } ;
0 commit comments