Skip to content

Commit 34adf7f

Browse files
aduh95targos
authored andcommitted
lib: refactor to avoid unsafe array iteration
PR-URL: #37029 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Zijian Liu <[email protected]>
1 parent e4b88b5 commit 34adf7f

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

lib/internal/process/per_thread.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// thread and the worker threads.
66

77
const {
8+
ArrayPrototypeEvery,
89
ArrayPrototypeMap,
910
ArrayPrototypePush,
1011
ArrayPrototypeSplice,
@@ -258,27 +259,24 @@ function buildAllowedFlags() {
258259
const { options, aliases } = require('internal/options');
259260

260261
const allowedNodeEnvironmentFlags = [];
261-
for (const [name, info] of options) {
262+
for (const { 0: name, 1: info } of options) {
262263
if (info.envVarSettings === kAllowedInEnvironment) {
263264
ArrayPrototypePush(allowedNodeEnvironmentFlags, name);
264265
}
265266
}
266267

267-
for (const [ from, expansion ] of aliases) {
268-
let isAccepted = true;
269-
for (const to of expansion) {
270-
if (!StringPrototypeStartsWith(to, '-') || to === '--') continue;
271-
const recursiveExpansion = aliases.get(to);
272-
if (recursiveExpansion) {
273-
if (recursiveExpansion[0] === to)
274-
ArrayPrototypeSplice(recursiveExpansion, 0, 1);
275-
ArrayPrototypePush(expansion, ...recursiveExpansion);
276-
continue;
277-
}
278-
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
279-
if (!isAccepted) break;
268+
function isAccepted(to) {
269+
if (!StringPrototypeStartsWith(to, '-') || to === '--') return true;
270+
const recursiveExpansion = aliases.get(to);
271+
if (recursiveExpansion) {
272+
if (recursiveExpansion[0] === to)
273+
ArrayPrototypeSplice(recursiveExpansion, 0, 1);
274+
return ArrayPrototypeEvery(recursiveExpansion, isAccepted);
280275
}
281-
if (isAccepted) {
276+
return options.get(to).envVarSettings === kAllowedInEnvironment;
277+
}
278+
for (const { 0: from, 1: expansion } of aliases) {
279+
if (ArrayPrototypeEvery(expansion, isAccepted)) {
282280
let canonical = from;
283281
if (StringPrototypeEndsWith(canonical, '='))
284282
canonical = StringPrototypeSlice(canonical, 0, canonical.length - 1);

0 commit comments

Comments
 (0)