Skip to content

Commit d994e26

Browse files
committed
lib: generate allowedNodeEnvironmentFlags lazily
PR-URL: #22638 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 995782c commit d994e26

File tree

1 file changed

+111
-96
lines changed

1 file changed

+111
-96
lines changed

lib/internal/bootstrap/node.js

Lines changed: 111 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -670,117 +670,132 @@
670670
const has = Function.call.bind(Set.prototype.has);
671671
const test = Function.call.bind(RegExp.prototype.test);
672672

673-
const {
674-
getOptions,
675-
types: { kV8Option },
676-
envSettings: { kAllowedInEnvironment }
677-
} = internalBinding('options');
678-
const { options, aliases } = getOptions();
679-
680-
const allowedV8EnvironmentFlags = [];
681-
const allowedNodeEnvironmentFlags = [];
682-
for (const [name, info] of options) {
683-
if (info.envVarSettings === kAllowedInEnvironment) {
684-
if (info.type === kV8Option) {
685-
allowedV8EnvironmentFlags.push(name);
686-
} else {
687-
allowedNodeEnvironmentFlags.push(name);
673+
const get = () => {
674+
const {
675+
getOptions,
676+
types: { kV8Option },
677+
envSettings: { kAllowedInEnvironment }
678+
} = internalBinding('options');
679+
const { options, aliases } = getOptions();
680+
681+
const allowedV8EnvironmentFlags = [];
682+
const allowedNodeEnvironmentFlags = [];
683+
for (const [name, info] of options) {
684+
if (info.envVarSettings === kAllowedInEnvironment) {
685+
if (info.type === kV8Option) {
686+
allowedV8EnvironmentFlags.push(name);
687+
} else {
688+
allowedNodeEnvironmentFlags.push(name);
689+
}
688690
}
689691
}
690-
}
691692

692-
for (const [ from, expansion ] of aliases) {
693-
let isAccepted = true;
694-
for (const to of expansion) {
695-
if (!to.startsWith('-')) continue;
696-
const recursiveExpansion = aliases.get(to);
697-
if (recursiveExpansion) {
698-
expansion.push(...recursiveExpansion);
699-
continue;
693+
for (const [ from, expansion ] of aliases) {
694+
let isAccepted = true;
695+
for (const to of expansion) {
696+
if (!to.startsWith('-')) continue;
697+
const recursiveExpansion = aliases.get(to);
698+
if (recursiveExpansion) {
699+
expansion.push(...recursiveExpansion);
700+
continue;
701+
}
702+
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
703+
if (!isAccepted) break;
704+
}
705+
if (isAccepted) {
706+
let canonical = from;
707+
if (canonical.endsWith('='))
708+
canonical = canonical.substr(0, canonical.length - 1);
709+
if (canonical.endsWith(' <arg>'))
710+
canonical = canonical.substr(0, canonical.length - 4);
711+
allowedNodeEnvironmentFlags.push(canonical);
700712
}
701-
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
702-
if (!isAccepted) break;
703-
}
704-
if (isAccepted) {
705-
let canonical = from;
706-
if (canonical.endsWith('='))
707-
canonical = canonical.substr(0, canonical.length - 1);
708-
if (canonical.endsWith(' <arg>'))
709-
canonical = canonical.substr(0, canonical.length - 4);
710-
allowedNodeEnvironmentFlags.push(canonical);
711713
}
712-
}
713714

714-
const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, '');
715-
716-
// Save these for comparison against flags provided to
717-
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
718-
// Avoid interference w/ user code by flattening `Set.prototype` into
719-
// each object.
720-
const [nodeFlags, v8Flags] = [
721-
allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags
722-
].map((flags) => Object.defineProperties(
723-
new Set(flags.map(trimLeadingDashes)),
724-
Object.getOwnPropertyDescriptors(Set.prototype))
725-
);
726-
727-
class NodeEnvironmentFlagsSet extends Set {
728-
constructor(...args) {
729-
super(...args);
730-
731-
// the super constructor consumes `add`, but
732-
// disallow any future adds.
733-
this.add = () => this;
734-
}
715+
const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, '');
716+
717+
// Save these for comparison against flags provided to
718+
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
719+
// Avoid interference w/ user code by flattening `Set.prototype` into
720+
// each object.
721+
const [nodeFlags, v8Flags] = [
722+
allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags
723+
].map((flags) => Object.defineProperties(
724+
new Set(flags.map(trimLeadingDashes)),
725+
Object.getOwnPropertyDescriptors(Set.prototype))
726+
);
735727

736-
delete() {
737-
// noop, `Set` API compatible
738-
return false;
739-
}
728+
class NodeEnvironmentFlagsSet extends Set {
729+
constructor(...args) {
730+
super(...args);
740731

741-
clear() {
742-
// noop
743-
}
732+
// the super constructor consumes `add`, but
733+
// disallow any future adds.
734+
this.add = () => this;
735+
}
744736

745-
has(key) {
746-
// This will return `true` based on various possible
747-
// permutations of a flag, including present/missing leading
748-
// dash(es) and/or underscores-for-dashes in the case of V8-specific
749-
// flags. Strips any values after `=`, inclusive.
750-
// TODO(addaleax): It might be more flexible to run the option parser
751-
// on a dummy option set and see whether it rejects the argument or
752-
// not.
753-
if (typeof key === 'string') {
754-
key = replace(key, trailingValuesRegex, '');
755-
if (test(leadingDashesRegex, key)) {
756-
return has(this, key) ||
757-
has(v8Flags,
758-
replace(
737+
delete() {
738+
// noop, `Set` API compatible
739+
return false;
740+
}
741+
742+
clear() {
743+
// noop
744+
}
745+
746+
has(key) {
747+
// This will return `true` based on various possible
748+
// permutations of a flag, including present/missing leading
749+
// dash(es) and/or underscores-for-dashes in the case of V8-specific
750+
// flags. Strips any values after `=`, inclusive.
751+
// TODO(addaleax): It might be more flexible to run the option parser
752+
// on a dummy option set and see whether it rejects the argument or
753+
// not.
754+
if (typeof key === 'string') {
755+
key = replace(key, trailingValuesRegex, '');
756+
if (test(leadingDashesRegex, key)) {
757+
return has(this, key) ||
758+
has(v8Flags,
759759
replace(
760-
key,
761-
leadingDashesRegex,
762-
''
763-
),
764-
replaceDashesRegex,
765-
'_'
766-
)
767-
);
760+
replace(
761+
key,
762+
leadingDashesRegex,
763+
''
764+
),
765+
replaceDashesRegex,
766+
'_'
767+
)
768+
);
769+
}
770+
return has(nodeFlags, key) ||
771+
has(v8Flags, replace(key, replaceDashesRegex, '_'));
768772
}
769-
return has(nodeFlags, key) ||
770-
has(v8Flags, replace(key, replaceDashesRegex, '_'));
773+
return false;
771774
}
772-
return false;
773775
}
774-
}
775776

776-
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
777-
Object.freeze(NodeEnvironmentFlagsSet.prototype);
777+
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
778+
Object.freeze(NodeEnvironmentFlagsSet.prototype);
779+
780+
return process.allowedNodeEnvironmentFlags = Object.freeze(
781+
new NodeEnvironmentFlagsSet(
782+
allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags)
783+
));
784+
};
778785

779-
process.allowedNodeEnvironmentFlags = Object.freeze(
780-
new NodeEnvironmentFlagsSet(
781-
allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags)
782-
)
783-
);
786+
Object.defineProperty(process, 'allowedNodeEnvironmentFlags', {
787+
get,
788+
set(value) {
789+
Object.defineProperty(this, 'allowedNodeEnvironmentFlags', {
790+
value,
791+
configurable: true,
792+
enumerable: true,
793+
writable: true
794+
});
795+
},
796+
enumerable: true,
797+
configurable: true
798+
});
784799
}
785800

786801
startup();

0 commit comments

Comments
 (0)