Skip to content

Commit 2abdbf4

Browse files
addaleaxtargos
authored andcommitted
lib: generate allowedNodeEnvironmentFlags lazily
Backport-PR-URL: #22847 PR-URL: #22638 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 4ab9d6f commit 2abdbf4

File tree

1 file changed

+111
-96
lines changed

1 file changed

+111
-96
lines changed

lib/internal/bootstrap/node.js

+111-96
Original file line numberDiff line numberDiff line change
@@ -619,117 +619,132 @@
619619
const has = Function.call.bind(Set.prototype.has);
620620
const test = Function.call.bind(RegExp.prototype.test);
621621

622-
const {
623-
getOptions,
624-
types: { kV8Option },
625-
envSettings: { kAllowedInEnvironment }
626-
} = internalBinding('options');
627-
const { options, aliases } = getOptions();
628-
629-
const allowedV8EnvironmentFlags = [];
630-
const allowedNodeEnvironmentFlags = [];
631-
for (const [name, info] of options) {
632-
if (info.envVarSettings === kAllowedInEnvironment) {
633-
if (info.type === kV8Option) {
634-
allowedV8EnvironmentFlags.push(name);
635-
} else {
636-
allowedNodeEnvironmentFlags.push(name);
622+
const get = () => {
623+
const {
624+
getOptions,
625+
types: { kV8Option },
626+
envSettings: { kAllowedInEnvironment }
627+
} = internalBinding('options');
628+
const { options, aliases } = getOptions();
629+
630+
const allowedV8EnvironmentFlags = [];
631+
const allowedNodeEnvironmentFlags = [];
632+
for (const [name, info] of options) {
633+
if (info.envVarSettings === kAllowedInEnvironment) {
634+
if (info.type === kV8Option) {
635+
allowedV8EnvironmentFlags.push(name);
636+
} else {
637+
allowedNodeEnvironmentFlags.push(name);
638+
}
637639
}
638640
}
639-
}
640641

641-
for (const [ from, expansion ] of aliases) {
642-
let isAccepted = true;
643-
for (const to of expansion) {
644-
if (!to.startsWith('-')) continue;
645-
const recursiveExpansion = aliases.get(to);
646-
if (recursiveExpansion) {
647-
expansion.push(...recursiveExpansion);
648-
continue;
642+
for (const [ from, expansion ] of aliases) {
643+
let isAccepted = true;
644+
for (const to of expansion) {
645+
if (!to.startsWith('-')) continue;
646+
const recursiveExpansion = aliases.get(to);
647+
if (recursiveExpansion) {
648+
expansion.push(...recursiveExpansion);
649+
continue;
650+
}
651+
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
652+
if (!isAccepted) break;
653+
}
654+
if (isAccepted) {
655+
let canonical = from;
656+
if (canonical.endsWith('='))
657+
canonical = canonical.substr(0, canonical.length - 1);
658+
if (canonical.endsWith(' <arg>'))
659+
canonical = canonical.substr(0, canonical.length - 4);
660+
allowedNodeEnvironmentFlags.push(canonical);
649661
}
650-
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
651-
if (!isAccepted) break;
652-
}
653-
if (isAccepted) {
654-
let canonical = from;
655-
if (canonical.endsWith('='))
656-
canonical = canonical.substr(0, canonical.length - 1);
657-
if (canonical.endsWith(' <arg>'))
658-
canonical = canonical.substr(0, canonical.length - 4);
659-
allowedNodeEnvironmentFlags.push(canonical);
660662
}
661-
}
662663

663-
const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, '');
664-
665-
// Save these for comparison against flags provided to
666-
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
667-
// Avoid interference w/ user code by flattening `Set.prototype` into
668-
// each object.
669-
const [nodeFlags, v8Flags] = [
670-
allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags
671-
].map((flags) => Object.defineProperties(
672-
new Set(flags.map(trimLeadingDashes)),
673-
Object.getOwnPropertyDescriptors(Set.prototype))
674-
);
675-
676-
class NodeEnvironmentFlagsSet extends Set {
677-
constructor(...args) {
678-
super(...args);
679-
680-
// the super constructor consumes `add`, but
681-
// disallow any future adds.
682-
this.add = () => this;
683-
}
664+
const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, '');
665+
666+
// Save these for comparison against flags provided to
667+
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
668+
// Avoid interference w/ user code by flattening `Set.prototype` into
669+
// each object.
670+
const [nodeFlags, v8Flags] = [
671+
allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags
672+
].map((flags) => Object.defineProperties(
673+
new Set(flags.map(trimLeadingDashes)),
674+
Object.getOwnPropertyDescriptors(Set.prototype))
675+
);
684676

685-
delete() {
686-
// noop, `Set` API compatible
687-
return false;
688-
}
677+
class NodeEnvironmentFlagsSet extends Set {
678+
constructor(...args) {
679+
super(...args);
689680

690-
clear() {
691-
// noop
692-
}
681+
// the super constructor consumes `add`, but
682+
// disallow any future adds.
683+
this.add = () => this;
684+
}
693685

694-
has(key) {
695-
// This will return `true` based on various possible
696-
// permutations of a flag, including present/missing leading
697-
// dash(es) and/or underscores-for-dashes in the case of V8-specific
698-
// flags. Strips any values after `=`, inclusive.
699-
// TODO(addaleax): It might be more flexible to run the option parser
700-
// on a dummy option set and see whether it rejects the argument or
701-
// not.
702-
if (typeof key === 'string') {
703-
key = replace(key, trailingValuesRegex, '');
704-
if (test(leadingDashesRegex, key)) {
705-
return has(this, key) ||
706-
has(v8Flags,
707-
replace(
686+
delete() {
687+
// noop, `Set` API compatible
688+
return false;
689+
}
690+
691+
clear() {
692+
// noop
693+
}
694+
695+
has(key) {
696+
// This will return `true` based on various possible
697+
// permutations of a flag, including present/missing leading
698+
// dash(es) and/or underscores-for-dashes in the case of V8-specific
699+
// flags. Strips any values after `=`, inclusive.
700+
// TODO(addaleax): It might be more flexible to run the option parser
701+
// on a dummy option set and see whether it rejects the argument or
702+
// not.
703+
if (typeof key === 'string') {
704+
key = replace(key, trailingValuesRegex, '');
705+
if (test(leadingDashesRegex, key)) {
706+
return has(this, key) ||
707+
has(v8Flags,
708708
replace(
709-
key,
710-
leadingDashesRegex,
711-
''
712-
),
713-
replaceDashesRegex,
714-
'_'
715-
)
716-
);
709+
replace(
710+
key,
711+
leadingDashesRegex,
712+
''
713+
),
714+
replaceDashesRegex,
715+
'_'
716+
)
717+
);
718+
}
719+
return has(nodeFlags, key) ||
720+
has(v8Flags, replace(key, replaceDashesRegex, '_'));
717721
}
718-
return has(nodeFlags, key) ||
719-
has(v8Flags, replace(key, replaceDashesRegex, '_'));
722+
return false;
720723
}
721-
return false;
722724
}
723-
}
724725

725-
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
726-
Object.freeze(NodeEnvironmentFlagsSet.prototype);
726+
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
727+
Object.freeze(NodeEnvironmentFlagsSet.prototype);
728+
729+
return process.allowedNodeEnvironmentFlags = Object.freeze(
730+
new NodeEnvironmentFlagsSet(
731+
allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags)
732+
));
733+
};
727734

728-
process.allowedNodeEnvironmentFlags = Object.freeze(
729-
new NodeEnvironmentFlagsSet(
730-
allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags)
731-
)
732-
);
735+
Object.defineProperty(process, 'allowedNodeEnvironmentFlags', {
736+
get,
737+
set(value) {
738+
Object.defineProperty(this, 'allowedNodeEnvironmentFlags', {
739+
value,
740+
configurable: true,
741+
enumerable: true,
742+
writable: true
743+
});
744+
},
745+
enumerable: true,
746+
configurable: true
747+
});
733748
}
734749

735750
startup();

0 commit comments

Comments
 (0)