Skip to content

Commit 65fd468

Browse files
committed
fix(node): make toWebReadableStream cancel-safe on every Node release
The Readable.toWeb + TransformStream approach only shielded the adapter's controller on some Node releases: on Node 24 a chunk arriving after cancel still hit the closed controller and crashed the process with an uncaught ERR_INVALID_STATE, even for plain Readable sources. Hand-roll the adapter instead of wrapping Readable.toWeb: chunks are only enqueued inside pull(), which never runs after cancel, so the crash is impossible by construction regardless of Node's adapter internals. Cancel destroys the source, except http1 server requests — they share their socket with the response, so they are abandoned (stalled by backpressure, reclaimed on connection teardown) to keep in-flight responses deliverable. All package and e2e suites pass on Node 22, 24, and 26.
1 parent eca913f commit 65fd468

1 file changed

Lines changed: 42 additions & 12 deletions

File tree

‎packages/node/src/utils.ts‎

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
1+
import type { Readable } from 'node:stream'
12
import type Stream from 'node:stream'
23
import type { NodeHttpResponse } from './types'
3-
import { Readable } from 'node:stream'
4+
import { IncomingMessage } from 'node:http'
45

56
/**
6-
* A cancel-safe alternative to a bare `Readable.toWeb`.
7+
* A cancel-safe alternative to `Readable.toWeb`.
78
*
8-
* Bare `Readable.toWeb` enqueues from `'data'` events, so a chunk arriving
9-
* after the consumer cancels hits a closed controller and crashes the process
10-
* with an uncaught `ERR_INVALID_STATE` (nodejs/node#54205). The intermediate
11-
* `TransformStream` shields the adapter's controller from that cancellation
12-
* (which still destroys the source), and the per-chunk copy detaches chunks
13-
* from Node's pooled `Buffer` memory.
9+
* Node's adapter enqueues from `'data'` events, so a chunk arriving after the
10+
* consumer cancels hits a closed controller and crashes the process with an
11+
* uncaught `ERR_INVALID_STATE` (nodejs/node#54205) — on some Node releases even
12+
* through an intermediate `TransformStream`. Pulling through the stream's async
13+
* iterator makes that impossible: chunks are only enqueued inside `pull`, never
14+
* after cancel. The per-chunk copy detaches chunks from Node's pooled `Buffer`
15+
* memory.
16+
*
17+
* Cancel destroys the source, except http1 server requests: they share their
18+
* socket with the response, so destroying them would kill an in-flight
19+
* response. They are abandoned instead — stalled by backpressure and reclaimed
20+
* on connection teardown.
1421
*/
1522
export function toWebReadableStream(stream: Readable): ReadableStream<Uint8Array<ArrayBuffer>> {
16-
return Readable.toWeb(stream).pipeThrough(new TransformStream({
17-
transform(chunk, controller) {
18-
controller.enqueue(new Uint8Array(chunk))
23+
const iterator = stream[Symbol.asyncIterator]()
24+
let canceled = false
25+
26+
return new ReadableStream({
27+
async pull(controller) {
28+
const { done, value } = await iterator.next()
29+
30+
if (canceled) {
31+
return // a chunk in flight while cancel happened; drop it
32+
}
33+
34+
if (done) {
35+
controller.close()
36+
}
37+
else {
38+
controller.enqueue(new Uint8Array(value))
39+
}
40+
},
41+
cancel(reason) {
42+
canceled = true
43+
44+
const isHttp1ServerRequest = stream instanceof IncomingMessage && stream.method !== null
45+
46+
if (!isHttp1ServerRequest) {
47+
stream.destroy(reason instanceof Error ? reason : undefined)
48+
}
1949
},
20-
}))
50+
})
2151
}
2252

2353
/**

0 commit comments

Comments
 (0)