Skip to content

Commit

Permalink
stream: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #37126
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
aduh95 committed Feb 1, 2021
1 parent 419686c commit 4ad46e2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'use strict';

const {
ArrayPrototypeForEach,
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeSplice,
Expand Down Expand Up @@ -854,8 +855,8 @@ Readable.prototype.unpipe = function(dest) {
state.pipes = [];
this.pause();

for (const dest of dests)
dest.emit('unpipe', this, { hasUnpiped: false });
ArrayPrototypeForEach(dests, (dest) =>
dest.emit('unpipe', this, { hasUnpiped: false }));
return this;
}

Expand Down Expand Up @@ -1056,11 +1057,11 @@ Readable.prototype.wrap = function(stream) {
};

// Proxy all the other methods. Important when wrapping filters and duplexes.
for (const i of ObjectKeys(stream)) {
ArrayPrototypeForEach(ObjectKeys(stream), (i) => {
if (this[i] === undefined && typeof stream[i] === 'function') {
this[i] = FunctionPrototypeBind(stream[i], stream);
}
}
});

return this;
};
Expand Down
13 changes: 7 additions & 6 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'use strict';

const {
ArrayPrototypeForEach,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
Expand Down Expand Up @@ -521,9 +522,10 @@ function errorBuffer(state) {
callback(new ERR_STREAM_DESTROYED('write'));
}

for (const callback of ArrayPrototypeSplice(state[kOnFinished], 0)) {
callback(new ERR_STREAM_DESTROYED('end'));
}
ArrayPrototypeForEach(
ArrayPrototypeSplice(state[kOnFinished], 0),
(callback) => callback(new ERR_STREAM_DESTROYED('end'))
);

resetBuffer(state);
}
Expand Down Expand Up @@ -743,9 +745,8 @@ function finish(stream, state) {

state.finished = true;

for (const callback of ArrayPrototypeSplice(state[kOnFinished], 0)) {
callback();
}
ArrayPrototypeForEach(ArrayPrototypeSplice(state[kOnFinished], 0),
(callback) => callback());

stream.emit('finish');

Expand Down

0 comments on commit 4ad46e2

Please sign in to comment.