diff --git a/lib/internal/worker.js b/lib/internal/worker.js index cb51c0a1a5efd0..7d90e0b91c086a 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -4,6 +4,7 @@ const { ArrayIsArray, + ArrayPrototypeForEach, ArrayPrototypeMap, ArrayPrototypePush, Float64Array, @@ -14,7 +15,9 @@ const { ObjectEntries, Promise, PromiseResolve, + ReflectApply, RegExpPrototypeTest, + SafeArrayIterator, String, Symbol, SymbolFor, @@ -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) { @@ -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, @@ -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: @@ -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) {