From abddfc921bf2af02a04a6a5d2bca8e2d91d80958 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 26 Feb 2026 08:54:39 +0100 Subject: [PATCH] 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 Reviewed-By: Colin Ihrig Reviewed-By: Mattias Buelens --- lib/internal/webstreams/readablestream.js | 11 ++++++++--- test/wpt/status/streams.json | 8 -------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 43cfcded03def4..40083ce65ee8f4 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -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]() { diff --git a/test/wpt/status/streams.json b/test/wpt/status/streams.json index af3646c65ea660..222a68014f4af6 100644 --- a/test/wpt/status/streams.json +++ b/test/wpt/status/streams.json @@ -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" },