From 33b3fe0c586eeadc9d95fb87558c8a9365a809b5 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sun, 26 Apr 2026 20:35:52 +0000 Subject: [PATCH] stream: fix Writable.toWeb() desiredSize for non-object-mode Writable.toWeb() used a plain { highWaterMark } as the queuing strategy for non-object-mode streams, which meant no size function was provided. The WritableStream defaulted to counting each chunk as size 1, so desiredSize was incorrect (e.g., a 3-byte Uint8Array was counted as size 1 instead of 3). Add a size() function to the strategy that mirrors how the Node.js stream computes chunk length: byteLength for typed arrays, length for strings/buffers, falling back to 1. Fixes: https://github.com/nodejs/node/issues/56269 --- lib/internal/webstreams/adapters.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 8befa6bbbafd72..73abd095dadfe5 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -183,7 +183,12 @@ function newWritableStreamFromStreamWritable(streamWritable, options = kEmptyObj const strategy = streamWritable.writableObjectMode ? new CountQueuingStrategy({ highWaterMark }) : - { highWaterMark }; + { + highWaterMark, + size(chunk) { + return chunk?.byteLength ?? chunk?.length ?? 1; + }, + }; let controller; let backpressurePromise;