From bdc8eafe363ce7799ee588079b2a43c0d99afefe Mon Sep 17 00:00:00 2001 From: Roberto Bianchi Date: Mon, 30 Nov 2026 11:05:01 +0100 Subject: [PATCH] stream: avoid mutable push in consumers and operators The blob() consumer, the readable.map() queue, and pipeline()'s destroy and cleanup lists collected values with .push() on private arrays, which looks up Array.prototype.push at call time. Replacing that method from user code changed how these operations ran. Use indexed assignments instead, as arrayBuffer() in stream/consumers already does. Signed-off-by: Roberto Bianchi --- lib/internal/streams/operators.js | 6 +++--- lib/internal/streams/pipeline.js | 14 +++++++------- lib/stream/consumers.js | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/internal/streams/operators.js b/lib/internal/streams/operators.js index 1cd4d7aa8951..5002bfb76abf 100644 --- a/lib/internal/streams/operators.js +++ b/lib/internal/streams/operators.js @@ -124,7 +124,7 @@ function map(fn, options) { PromisePrototypeThen(val, afterItemProcessed, onCatch); - queue.push(val); + queue[queue.length] = val; if (next) { next(); next = null; @@ -136,11 +136,11 @@ function map(fn, options) { }); } } - queue.push(kEof); + queue[queue.length] = kEof; } catch (err) { const val = PromiseReject(err); PromisePrototypeThen(val, afterItemProcessed, onCatch); - queue.push(val); + queue[queue.length] = val; } finally { done = true; if (next) { diff --git a/lib/internal/streams/pipeline.js b/lib/internal/streams/pipeline.js index 6937d4354340..b70e1443f0fa 100644 --- a/lib/internal/streams/pipeline.js +++ b/lib/internal/streams/pipeline.js @@ -270,10 +270,10 @@ function pipelineImpl(streams, callback, opts) { if (end) { const { destroy, cleanup } = destroyer(stream, reading, writing); - destroys.push(destroy); + destroys[destroys.length] = destroy; if (isReadable(stream) && isLastStream) { - lastStreamCleanup.push(cleanup); + lastStreamCleanup[lastStreamCleanup.length] = cleanup; } } @@ -289,9 +289,9 @@ function pipelineImpl(streams, callback, opts) { } stream.on('error', onError); if (isReadable(stream) && isLastStream) { - lastStreamCleanup.push(() => { + lastStreamCleanup[lastStreamCleanup.length] = () => { stream.removeListener('error', onError); - }); + }; } } @@ -367,9 +367,9 @@ function pipelineImpl(streams, callback, opts) { ret = pt; const { destroy, cleanup } = destroyer(ret, false, true); - destroys.push(destroy); + destroys[destroys.length] = destroy; if (isLastStream) { - lastStreamCleanup.push(cleanup); + lastStreamCleanup[lastStreamCleanup.length] = cleanup; } } } else if (isNodeStream(stream)) { @@ -377,7 +377,7 @@ function pipelineImpl(streams, callback, opts) { finishCount += 2; const cleanup = pipe(ret, stream, finish, finishOnlyHandleError, { end }); if (isReadable(stream) && isLastStream) { - lastStreamCleanup.push(cleanup); + lastStreamCleanup[lastStreamCleanup.length] = cleanup; } } else if (isTransformStream(ret) || isReadableStream(ret)) { const toRead = ret.readable || ret; diff --git a/lib/stream/consumers.js b/lib/stream/consumers.js index 09bd692bc17c..d61704f53276 100644 --- a/lib/stream/consumers.js +++ b/lib/stream/consumers.js @@ -51,7 +51,7 @@ const { async function blob(stream) { const chunks = []; for await (const chunk of stream) - chunks.push(chunk); + chunks[chunks.length] = chunk; return new Blob(chunks); }