Skip to content

Commit bf695eb

Browse files
aduh95ruyadorno
authored andcommitted
worker: refactor to avoid unsafe array iteration
PR-URL: #36735 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 0398167 commit bf695eb

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

‎lib/internal/worker.js‎

Lines changed: 14 additions & 7 deletions
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
letenv;
156159
if(typeofoptions.env==='object'&&options.env!==null){
157160
env=ObjectCreate(null);
158-
for(const[key,value]ofObjectEntries(options.env))
159-
env[key]=`${value}`;
161+
ArrayPrototypeForEach(
162+
ObjectEntries(options.env),
163+
({0: key,1: value})=>{env[key]=`${value}`;}
164+
);
160165
}elseif(options.env==null){
161166
env=process.env;
162167
}elseif(options.env!==SHARE_ENV){
@@ -209,12 +214,13 @@ class Worker extends EventEmitter {
209214
consttransferList=[port2];
210215
// If transferList is provided.
211216
if(options.transferList)
212-
ArrayPrototypePush(transferList, ...options.transferList);
217+
ArrayPrototypePush(transferList,
218+
...newSafeArrayIterator(options.transferList));
213219

214220
this[kPublicPort]=port1;
215-
for(consteventof['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
constreadable=this[kParentSideStdio][stream];
282-
for(const{ chunk, encoding }ofchunks)
288+
ArrayPrototypeForEach(chunks,({ chunk, encoding })=>{
283289
readable.push(chunk,encoding);
290+
});
284291
return;
285292
}
286293
casemessageTypes.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)