Skip to content

Commit bf695eb

Browse files
aduh95ruyadorno
authored andcommitted
worker: refactor to avoid unsafe array iteration
PR-URL: #36735 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 0398167 commit bf695eb

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

Diff for: lib/internal/worker.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
const {
66
ArrayIsArray,
7+
ArrayPrototypeForEach,
78
ArrayPrototypeMap,
89
ArrayPrototypePush,
910
Float64Array,
@@ -14,7 +15,9 @@ const {
1415
ObjectEntries,
1516
Promise,
1617
PromiseResolve,
18+
ReflectApply,
1719
RegExpPrototypeTest,
20+
SafeArrayIterator,
1821
String,
1922
Symbol,
2023
SymbolFor,
@@ -155,8 +158,10 @@ class Worker extends EventEmitter {
155158
let env;
156159
if (typeof options.env === 'object' && options.env !== null) {
157160
env = ObjectCreate(null);
158-
for (const [ key, value ] of ObjectEntries(options.env))
159-
env[key] = `${value}`;
161+
ArrayPrototypeForEach(
162+
ObjectEntries(options.env),
163+
({ 0: key, 1: value }) => { env[key] = `${value}`; }
164+
);
160165
} else if (options.env == null) {
161166
env = process.env;
162167
} else if (options.env !== SHARE_ENV) {
@@ -209,12 +214,13 @@ class Worker extends EventEmitter {
209214
const transferList = [port2];
210215
// If transferList is provided.
211216
if (options.transferList)
212-
ArrayPrototypePush(transferList, ...options.transferList);
217+
ArrayPrototypePush(transferList,
218+
...new SafeArrayIterator(options.transferList));
213219

214220
this[kPublicPort] = port1;
215-
for (const event of ['message', 'messageerror']) {
221+
ArrayPrototypeForEach(['message', 'messageerror'], (event) => {
216222
this[kPublicPort].on(event, (message) => this.emit(event, message));
217-
}
223+
});
218224
setupPortReferencing(this[kPublicPort], this, 'message');
219225
this[kPort].postMessage({
220226
argv,
@@ -279,8 +285,9 @@ class Worker extends EventEmitter {
279285
{
280286
const { stream, chunks } = message;
281287
const readable = this[kParentSideStdio][stream];
282-
for (const { chunk, encoding } of chunks)
288+
ArrayPrototypeForEach(chunks, ({ chunk, encoding }) => {
283289
readable.push(chunk, encoding);
290+
});
284291
return;
285292
}
286293
case messageTypes.STDIO_WANTS_MORE_DATA:
@@ -314,7 +321,7 @@ class Worker extends EventEmitter {
314321
postMessage(...args) {
315322
if (this[kPublicPort] === null) return;
316323

317-
this[kPublicPort].postMessage(...args);
324+
ReflectApply(this[kPublicPort].postMessage, this[kPublicPort], args);
318325
}
319326

320327
terminate(callback) {

0 commit comments

Comments
 (0)