Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
with
12 additions
and
10 deletions.
-
+5
−4
lib/internal/streams/readable.js
-
+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'); |
|
|
|
|
|
|