Skip to content

Commit bdc8eaf

Browse files
committed
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 <roberto.bianchi@spendesk.com>
1 parent 1611823 commit bdc8eaf

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

‎lib/internal/streams/operators.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function map(fn, options) {
124124

125125
PromisePrototypeThen(val, afterItemProcessed, onCatch);
126126

127-
queue.push(val);
127+
queue[queue.length] = val;
128128
if (next) {
129129
next();
130130
next = null;
@@ -136,11 +136,11 @@ function map(fn, options) {
136136
});
137137
}
138138
}
139-
queue.push(kEof);
139+
queue[queue.length] = kEof;
140140
} catch (err) {
141141
const val = PromiseReject(err);
142142
PromisePrototypeThen(val, afterItemProcessed, onCatch);
143-
queue.push(val);
143+
queue[queue.length] = val;
144144
} finally {
145145
done = true;
146146
if (next) {

‎lib/internal/streams/pipeline.js‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ function pipelineImpl(streams, callback, opts) {
270270

271271
if (end) {
272272
const { destroy, cleanup } = destroyer(stream, reading, writing);
273-
destroys.push(destroy);
273+
destroys[destroys.length] = destroy;
274274

275275
if (isReadable(stream) && isLastStream) {
276-
lastStreamCleanup.push(cleanup);
276+
lastStreamCleanup[lastStreamCleanup.length] = cleanup;
277277
}
278278
}
279279

@@ -289,9 +289,9 @@ function pipelineImpl(streams, callback, opts) {
289289
}
290290
stream.on('error', onError);
291291
if (isReadable(stream) && isLastStream) {
292-
lastStreamCleanup.push(() => {
292+
lastStreamCleanup[lastStreamCleanup.length] = () => {
293293
stream.removeListener('error', onError);
294-
});
294+
};
295295
}
296296
}
297297

@@ -367,17 +367,17 @@ function pipelineImpl(streams, callback, opts) {
367367
ret = pt;
368368

369369
const { destroy, cleanup } = destroyer(ret, false, true);
370-
destroys.push(destroy);
370+
destroys[destroys.length] = destroy;
371371
if (isLastStream) {
372-
lastStreamCleanup.push(cleanup);
372+
lastStreamCleanup[lastStreamCleanup.length] = cleanup;
373373
}
374374
}
375375
} else if (isNodeStream(stream)) {
376376
if (isReadableNodeStream(ret)) {
377377
finishCount += 2;
378378
const cleanup = pipe(ret, stream, finish, finishOnlyHandleError, { end });
379379
if (isReadable(stream) && isLastStream) {
380-
lastStreamCleanup.push(cleanup);
380+
lastStreamCleanup[lastStreamCleanup.length] = cleanup;
381381
}
382382
} else if (isTransformStream(ret) || isReadableStream(ret)) {
383383
const toRead = ret.readable || ret;

‎lib/stream/consumers.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const {
5151
async function blob(stream) {
5252
const chunks = [];
5353
for await (const chunk of stream)
54-
chunks.push(chunk);
54+
chunks[chunks.length] = chunk;
5555
return new Blob(chunks);
5656
}
5757

0 commit comments

Comments
 (0)