Skip to content

Commit 1666ca7

Browse files
committed
stream: refactor to avoid unsafe array iteration
1 parent 6be2855 commit 1666ca7

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/internal/streams/readable.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
ArrayPrototypeForEach,
2526
ArrayPrototypeIndexOf,
2627
ArrayPrototypePush,
2728
ArrayPrototypeSplice,
@@ -854,8 +855,8 @@ Readable.prototype.unpipe = function(dest) {
854855
state.pipes = [];
855856
this.pause();
856857

857-
for (const dest of dests)
858-
dest.emit('unpipe', this, { hasUnpiped: false });
858+
ArrayPrototypeForEach(dests, (dest) =>
859+
dest.emit('unpipe', this, { hasUnpiped: false }));
859860
return this;
860861
}
861862

@@ -1056,11 +1057,11 @@ Readable.prototype.wrap = function(stream) {
10561057
};
10571058

10581059
// Proxy all the other methods. Important when wrapping filters and duplexes.
1059-
for (const i of ObjectKeys(stream)) {
1060+
ArrayPrototypeForEach(ObjectKeys(stream), (i) => {
10601061
if (this[i] === undefined && typeof stream[i] === 'function') {
10611062
this[i] = FunctionPrototypeBind(stream[i], stream);
10621063
}
1063-
}
1064+
});
10641065

10651066
return this;
10661067
};

lib/internal/streams/writable.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'use strict';
2727

2828
const {
29+
ArrayPrototypeForEach,
2930
ArrayPrototypePush,
3031
ArrayPrototypeSlice,
3132
ArrayPrototypeSplice,
@@ -522,9 +523,10 @@ function errorBuffer(state) {
522523
callback(new ERR_STREAM_DESTROYED('write'));
523524
}
524525

525-
for (const callback of ArrayPrototypeSplice(state[kOnFinished], 0)) {
526-
callback(new ERR_STREAM_DESTROYED('end'));
527-
}
526+
ArrayPrototypeForEach(
527+
ArrayPrototypeSplice(state[kOnFinished], 0),
528+
(callback) => callback(new ERR_STREAM_DESTROYED('end'))
529+
);
528530

529531
resetBuffer(state);
530532
}
@@ -744,9 +746,8 @@ function finish(stream, state) {
744746

745747
state.finished = true;
746748

747-
for (const callback of ArrayPrototypeSplice(state[kOnFinished], 0)) {
748-
callback();
749-
}
749+
ArrayPrototypeForEach(ArrayPrototypeSplice(state[kOnFinished], 0),
750+
(callback) => callback());
750751

751752
stream.emit('finish');
752753

0 commit comments

Comments
 (0)