Skip to content

Commit 4ad46e2

Browse files
committed
stream: refactor to avoid unsafe array iteration
PR-URL: #37126 Reviewed-By: Zijian Liu <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 419686c commit 4ad46e2

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

Diff for: lib/internal/streams/readable.js

+5-4
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
};

Diff for: lib/internal/streams/writable.js

+7-6
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,
@@ -521,9 +522,10 @@ function errorBuffer(state) {
521522
callback(new ERR_STREAM_DESTROYED('write'));
522523
}
523524

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

528530
resetBuffer(state);
529531
}
@@ -743,9 +745,8 @@ function finish(stream, state) {
743745

744746
state.finished = true;
745747

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

750751
stream.emit('finish');
751752

0 commit comments

Comments
 (0)