stream: fix pipeTo to defer writes per WHATWG spec

The WHATWG Streams spec requires that pipeTo's chunk handling must
queue a microtask before calling the write algorithm. This ensures
that enqueue() does not synchronously trigger writes.

Previously, PipeToReadableStreamReadRequest[kChunk] would synchronously
call writableStreamDefaultWriterWrite(), which violated the spec and
caused the WPT test "enqueue() must not synchronously call write
algorithm" to fail.

Fix by wrapping the write operation in queueMicrotask(), which defers
it to the next microtask as required by the spec.

Refs: https://github.com/whatwg/streams/issues/1243
PR-URL: https://github.com/nodejs/node/pull/61800
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Mattias Buelens <mattias@buelens.com>
This commit is contained in:
Matteo Collina authored and Antoine du Hamel committed 2026-02-28 12:07:00 +01:00
1 parent 9ddd1a9c27
commit ea2df2a16f
2 files changed
+8 -11

No files matched your search

+8 -3
View File
@@ -1662,9 +1662,14 @@ class PipeToReadableStreamReadRequest {
}
[kChunk](chunk) {
this.state.currentWrite = writableStreamDefaultWriterWrite(this.writer, chunk);
setPromiseHandled(this.state.currentWrite);
this.promise.resolve(false);
// Per spec, pipeTo must queue a microtask for the write to avoid
// synchronous write during enqueue(). See WHATWG Streams spec
// "ReadableStreamPipeTo" step 15's "chunk steps".
queueMicrotask(() => {
this.state.currentWrite = writableStreamDefaultWriterWrite(this.writer, chunk);
setPromiseHandled(this.state.currentWrite);
this.promise.resolve(false);
});
}
[kClose]() {
-8
View File
@@ -2,14 +2,6 @@
"idlharness-shadowrealm.window.js": {
"skip": "ShadowRealm support is not enabled"
},
"piping/general-addition.any.js": {
"fail": {
"note": "Blocked on https://github.com/whatwg/streams/issues/1243",
"expected": [
"enqueue() must not synchronously call write algorithm"
]
}
},
"queuing-strategies-size-function-per-global.window.js": {
"skip": "Browser-specific test"
},