Skip to content

Commit f599463

Browse files
committed
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: whatwg/streams#1243
1 parent 95245a7 commit f599463

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

lib/internal/webstreams/readablestream.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,9 +1584,14 @@ class PipeToReadableStreamReadRequest {
15841584
}
15851585

15861586
[kChunk](chunk) {
1587-
this.state.currentWrite = writableStreamDefaultWriterWrite(this.writer, chunk);
1588-
setPromiseHandled(this.state.currentWrite);
1589-
this.promise.resolve(false);
1587+
// Per spec, pipeTo must queue a microtask for the write to avoid
1588+
// synchronous write during enqueue(). See WHATWG Streams spec
1589+
// "ReadableStreamPipeTo" step 15's "chunk steps".
1590+
queueMicrotask(() => {
1591+
this.state.currentWrite = writableStreamDefaultWriterWrite(this.writer, chunk);
1592+
setPromiseHandled(this.state.currentWrite);
1593+
this.promise.resolve(false);
1594+
});
15901595
}
15911596

15921597
[kClose]() {

test/wpt/status/streams.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
"idlharness-shadowrealm.window.js": {
33
"skip": "ShadowRealm support is not enabled"
44
},
5-
"piping/general-addition.any.js": {
6-
"fail": {
7-
"note": "Blocked on https://github.com/whatwg/streams/issues/1243",
8-
"expected": [
9-
"enqueue() must not synchronously call write algorithm"
10-
]
11-
}
12-
},
135
"queuing-strategies-size-function-per-global.window.js": {
146
"skip": "Browser-specific test"
157
},

0 commit comments

Comments
 (0)