X Tutup
The Wayback Machine - https://web.archive.org/web/20210814023021/https://github.com/nodejs/node/commit/4ad46e2fef
Skip to content
Permalink
Browse files
stream: refactor to avoid unsafe array iteration
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 4ad46e2fefba1b3b4d02a978bdd7e65338854960
Showing with 12 additions and 10 deletions.
  1. +5 −4 lib/internal/streams/readable.js
  2. +7 −6 lib/internal/streams/writable.js
@@ -22,6 +22,7 @@
'use strict';

const {
ArrayPrototypeForEach,
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeSplice,
@@ -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;
}

@@ -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;
};
@@ -26,6 +26,7 @@
'use strict';

const {
ArrayPrototypeForEach,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
@@ -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);
}
@@ -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');

0 comments on commit 4ad46e2

Please sign in to comment.
X Tutup