Skip to content

Commit

Permalink
worker: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #36735
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
aduh95 authored and ruyadorno committed Jan 21, 2021
1 parent 0398167 commit bf695eb
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const {
ArrayIsArray,
ArrayPrototypeForEach,
ArrayPrototypeMap,
ArrayPrototypePush,
Float64Array,
Expand All @@ -14,7 +15,9 @@ const {
ObjectEntries,
Promise,
PromiseResolve,
ReflectApply,
RegExpPrototypeTest,
SafeArrayIterator,
String,
Symbol,
SymbolFor,
Expand Down Expand Up @@ -155,8 +158,10 @@ class Worker extends EventEmitter {
let env;
if (typeof options.env === 'object' && options.env !== null) {
env = ObjectCreate(null);
for (const [ key, value ] of ObjectEntries(options.env))
env[key] = `${value}`;
ArrayPrototypeForEach(
ObjectEntries(options.env),
({ 0: key, 1: value }) => { env[key] = `${value}`; }
);
} else if (options.env == null) {
env = process.env;
} else if (options.env !== SHARE_ENV) {
Expand Down Expand Up @@ -209,12 +214,13 @@ class Worker extends EventEmitter {
const transferList = [port2];
// If transferList is provided.
if (options.transferList)
ArrayPrototypePush(transferList, ...options.transferList);
ArrayPrototypePush(transferList,
...new SafeArrayIterator(options.transferList));

this[kPublicPort] = port1;
for (const event of ['message', 'messageerror']) {
ArrayPrototypeForEach(['message', 'messageerror'], (event) => {
this[kPublicPort].on(event, (message) => this.emit(event, message));
}
});
setupPortReferencing(this[kPublicPort], this, 'message');
this[kPort].postMessage({
argv,
Expand Down Expand Up @@ -279,8 +285,9 @@ class Worker extends EventEmitter {
{
const { stream, chunks } = message;
const readable = this[kParentSideStdio][stream];
for (const { chunk, encoding } of chunks)
ArrayPrototypeForEach(chunks, ({ chunk, encoding }) => {
readable.push(chunk, encoding);
});
return;
}
case messageTypes.STDIO_WANTS_MORE_DATA:
Expand Down Expand Up @@ -314,7 +321,7 @@ class Worker extends EventEmitter {
postMessage(...args) {
if (this[kPublicPort] === null) return;

this[kPublicPort].postMessage(...args);
ReflectApply(this[kPublicPort].postMessage, this[kPublicPort], args);
}

terminate(callback) {
Expand Down

0 comments on commit bf695eb

Please sign in to comment.