@@ -29,6 +29,24 @@ function bodyUnusable(): TypeError {
2929 return new TypeError ( "Body is unusable: Body has already been read" ) ;
3030}
3131
32+ /**
33+ * Rejection for a body that can no longer be read because the client hung up
34+ * without the socket surfacing an error of its own. Shaped like the reason an
35+ * `AbortController` produces so handlers can branch on `err.name === "AbortError"`.
36+ */
37+ function abortError ( ) : DOMException {
38+ return new DOMException ( "The request was aborted." , "AbortError" ) ;
39+ }
40+
41+ /** A `ReadableStream` that errors immediately with `error`, without producing any bytes. */
42+ function erroredStream ( error : unknown ) : ReadableStream {
43+ return new ReadableStream ( {
44+ start ( controller ) {
45+ controller . error ( error ) ;
46+ } ,
47+ } ) ;
48+ }
49+
3250export const NodeRequest : {
3351 new ( nodeCtx : NodeRequestContext ) : ServerRequest ;
3452} = /* @__PURE__ */ ( ( ) => {
@@ -160,21 +178,40 @@ export const NodeRequest: {
160178 const { req, res } = this . runtime ! . node ! ;
161179 const abortController = this . #abortController;
162180 const abort = ( err ?: Error ) => abortController . abort ?.( err ) ;
181+ // The controller is created lazily, so `close` may already have fired
182+ // before this listener existed (a handler that only touches `signal`
183+ // after awaiting, or not at all until a body read fails). Replaying the
184+ // handler against the current state is what keeps `signal.aborted` from
185+ // reporting `false` for a client that is long gone.
163186 if ( res ) {
164- res . once ( "close" , ( ) => {
187+ const onClose = ( ) => {
165188 const reqError = req . errored ;
166189 if ( reqError ) {
167190 abort ( reqError ) ; // request error
168191 } else if ( ! res . writableEnded ) {
169192 abort ( ) ; // server closed before finishing response
170193 }
171- } ) ;
194+ } ;
195+ res . once ( "close" , onClose ) ;
196+ // `req.destroyed` alone does *not* mean the client hung up:
197+ // `IncomingMessage` is `autoDestroy`, so a fully read body leaves it
198+ // destroyed on a perfectly healthy request, while the response is
199+ // still open (`writableEnded === false`) — replaying on that would
200+ // 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 ) ) {
203+ onClose ( ) ;
204+ }
172205 } else {
173- req . once ( "close" , ( ) => {
206+ const onClose = ( ) => {
174207 if ( ! req . complete ) {
175208 abort ( ) ; // client disconnected
176209 }
177- } ) ;
210+ } ;
211+ req . once ( "close" , onClose ) ;
212+ if ( req . destroyed ) {
213+ onClose ( ) ;
214+ }
178215 }
179216 }
180217 return this . #abortController;
@@ -199,11 +236,21 @@ export const NodeRequest: {
199236 // No stream for a null-body (GET/HEAD) request, and never re-wrap an
200237 // already-consumed IncomingMessage (the fast path leaves it ended, so a
201238 // fresh `Readable.toWeb` would produce a stream whose `end` never fires).
202- let stream =
203- this . #hasBody( ) && ! this . #bodyUsed
204- ? // TODO: HTTP2ServerRequest
205- ( Readable . toWeb ( this . #req as NodeJS . ReadableStream ) as unknown as ReadableStream )
206- : null ;
239+ let stream : ReadableStream | null = null ;
240+ if ( this . #hasBody( ) && ! this . #bodyUsed) {
241+ // TODO: HTTP2ServerRequest
242+ stream = Readable . toWeb ( this . #req as NodeJS . ReadableStream ) as unknown as ReadableStream ;
243+ // `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
246+ // undici's "Response body object should not be disturbed or locked"
247+ // TypeError out of `_request`, which tells the handler nothing about
248+ // what happened. Serve a stream carrying the abort reason instead, so
249+ // every read (including through the native Request) rejects with it.
250+ if ( Readable . isDisturbed ( stream as unknown as NodeJS . ReadableStream ) ) {
251+ stream = erroredStream ( this . #bodyError( ) ) ;
252+ }
253+ }
207254 // Enforce `maxRequestBodySize` at the single choke point every consumer funnels
208255 // through (`request.body`, and therefore the native `Request` methods
209256 // `arrayBuffer()` / `blob()` / `bytes()` / `formData()` and streaming).
@@ -245,10 +292,35 @@ export const NodeRequest: {
245292 return this . #bodyUsed;
246293 }
247294
295+ // Why a body read cannot proceed once the underlying Node stream is gone.
296+ // The abort reason comes first (the socket error, or the controller's
297+ // `AbortError`) so a handler can tell "client hung up" — the case worth a
298+ // 499 rather than a 5xx — from a malformed request. A stream that ended
299+ // without srvx reading it was consumed elsewhere: an unusable body, not an
300+ // abort.
301+ #bodyError( ) : unknown {
302+ const signal = this . _abortController . signal ;
303+ if ( signal . aborted ) {
304+ return signal . reason ;
305+ }
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+ ) ;
313+ }
314+
248315 // Buffer the raw request body once; consumers add their own single
249316 // continuation (`.toString()` / `JSON.parse`) so no extra promise or
250317 // microtask hop is introduced vs. inlining the read.
251318 #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 ) {
322+ return Promise . reject ( this . #bodyError( ) ) ;
323+ }
252324 return readBody ( this . #req, this . #maxRequestBodySize) ;
253325 }
254326
@@ -439,6 +511,7 @@ function readBody(req: NodeServerRequest, maxRequestBodySize?: number): Promise<
439511 req . off ( "data" , onData ) ;
440512 req . off ( "end" , onEnd ) ;
441513 req . off ( "error" , onError ) ;
514+ req . off ( "close" , onClose ) ;
442515 } ;
443516 const onData = ( chunk : any ) => {
444517 if ( maxRequestBodySize !== undefined ) {
@@ -463,7 +536,15 @@ function readBody(req: NodeServerRequest, maxRequestBodySize?: number): Promise<
463536 // Single-chunk bodies (the common case) skip Buffer.concat's alloc+copy
464537 resolve ( chunks . length === 1 ? chunks [ 0 ] : Buffer . concat ( chunks ) ) ;
465538 } ;
466- req . on ( "data" , onData ) . once ( "end" , onEnd ) . once ( "error" , onError ) ;
539+ // A destroy that surfaces no `error` (`req.destroy()`, or a reset that only
540+ // shows up as a close) would otherwise leave this promise — and the handler
541+ // awaiting it — pending forever. `end` always precedes `close` and cleans
542+ // this listener up first, so it only fires on an incomplete body.
543+ const onClose = ( ) => {
544+ cleanup ( ) ;
545+ reject ( req . errored || abortError ( ) ) ;
546+ } ;
547+ req . on ( "data" , onData ) . once ( "end" , onEnd ) . once ( "error" , onError ) . once ( "close" , onClose ) ;
467548 } ) ;
468549}
469550
0 commit comments