Skip to content

Commit ef68349

Browse files
joyeecheungBethGriggs
authored andcommitted
lib: move setupAllowedFlags() into per_thread.js
Because most of its code (the getter) has nothing to do with the actual bootstrapping and it is run per-thread. PR-URL: #24704 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Christopher Hiller <[email protected]>
1 parent d4b1666 commit ef68349

File tree

2 files changed

+126
-124
lines changed

2 files changed

+126
-124
lines changed

lib/internal/bootstrap/node.js

+1-124
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209

210210
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
211211

212-
setupAllowedFlags();
212+
perThreadSetup.setupAllowedFlags();
213213

214214
// There are various modes that Node can run in. The most common two
215215
// are running from a script and running the REPL - but there are a few
@@ -617,128 +617,5 @@
617617
new vm.Script(source, { displayErrors: true, filename });
618618
}
619619

620-
function setupAllowedFlags() {
621-
// This builds process.allowedNodeEnvironmentFlags
622-
// from data in the config binding
623-
624-
const replaceUnderscoresRegex = /_/g;
625-
const leadingDashesRegex = /^--?/;
626-
const trailingValuesRegex = /=.*$/;
627-
628-
// Save references so user code does not interfere
629-
const replace = Function.call.bind(String.prototype.replace);
630-
const has = Function.call.bind(Set.prototype.has);
631-
const test = Function.call.bind(RegExp.prototype.test);
632-
633-
const get = () => {
634-
const {
635-
envSettings: { kAllowedInEnvironment }
636-
} = internalBinding('options');
637-
const { options, aliases } = NativeModule.require('internal/options');
638-
639-
const allowedNodeEnvironmentFlags = [];
640-
for (const [name, info] of options) {
641-
if (info.envVarSettings === kAllowedInEnvironment) {
642-
allowedNodeEnvironmentFlags.push(name);
643-
}
644-
}
645-
646-
for (const [ from, expansion ] of aliases) {
647-
let isAccepted = true;
648-
for (const to of expansion) {
649-
if (!to.startsWith('-') || to === '--') continue;
650-
const recursiveExpansion = aliases.get(to);
651-
if (recursiveExpansion) {
652-
if (recursiveExpansion[0] === to)
653-
recursiveExpansion.splice(0, 1);
654-
expansion.push(...recursiveExpansion);
655-
continue;
656-
}
657-
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
658-
if (!isAccepted) break;
659-
}
660-
if (isAccepted) {
661-
let canonical = from;
662-
if (canonical.endsWith('='))
663-
canonical = canonical.substr(0, canonical.length - 1);
664-
if (canonical.endsWith(' <arg>'))
665-
canonical = canonical.substr(0, canonical.length - 4);
666-
allowedNodeEnvironmentFlags.push(canonical);
667-
}
668-
}
669-
670-
const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, '');
671-
672-
// Save these for comparison against flags provided to
673-
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
674-
// Avoid interference w/ user code by flattening `Set.prototype` into
675-
// each object.
676-
const nodeFlags = Object.defineProperties(
677-
new Set(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
678-
Object.getOwnPropertyDescriptors(Set.prototype)
679-
);
680-
681-
class NodeEnvironmentFlagsSet extends Set {
682-
constructor(...args) {
683-
super(...args);
684-
685-
// the super constructor consumes `add`, but
686-
// disallow any future adds.
687-
this.add = () => this;
688-
}
689-
690-
delete() {
691-
// noop, `Set` API compatible
692-
return false;
693-
}
694-
695-
clear() {
696-
// noop
697-
}
698-
699-
has(key) {
700-
// This will return `true` based on various possible
701-
// permutations of a flag, including present/missing leading
702-
// dash(es) and/or underscores-for-dashes.
703-
// Strips any values after `=`, inclusive.
704-
// TODO(addaleax): It might be more flexible to run the option parser
705-
// on a dummy option set and see whether it rejects the argument or
706-
// not.
707-
if (typeof key === 'string') {
708-
key = replace(key, replaceUnderscoresRegex, '-');
709-
if (test(leadingDashesRegex, key)) {
710-
key = replace(key, trailingValuesRegex, '');
711-
return has(this, key);
712-
}
713-
return has(nodeFlags, key);
714-
}
715-
return false;
716-
}
717-
}
718-
719-
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
720-
Object.freeze(NodeEnvironmentFlagsSet.prototype);
721-
722-
return process.allowedNodeEnvironmentFlags = Object.freeze(
723-
new NodeEnvironmentFlagsSet(
724-
allowedNodeEnvironmentFlags
725-
));
726-
};
727-
728-
Object.defineProperty(process, 'allowedNodeEnvironmentFlags', {
729-
get,
730-
set(value) {
731-
Object.defineProperty(this, 'allowedNodeEnvironmentFlags', {
732-
value,
733-
configurable: true,
734-
enumerable: true,
735-
writable: true
736-
});
737-
},
738-
enumerable: true,
739-
configurable: true
740-
});
741-
}
742-
743620
startup();
744621
});

lib/internal/process/per_thread.js

+125
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,132 @@ function setupUncaughtExceptionCapture(exceptionHandlerState,
234234
};
235235
}
236236

237+
const replaceUnderscoresRegex = /_/g;
238+
const leadingDashesRegex = /^--?/;
239+
const trailingValuesRegex = /=.*$/;
240+
241+
// Save references so user code does not interfere
242+
const replace = Function.call.bind(String.prototype.replace);
243+
const has = Function.call.bind(Set.prototype.has);
244+
const test = Function.call.bind(RegExp.prototype.test);
245+
246+
// This builds the initial process.allowedNodeEnvironmentFlags
247+
// from data in the config binding.
248+
function buildAllowedFlags() {
249+
const {
250+
envSettings: { kAllowedInEnvironment }
251+
} = internalBinding('options');
252+
const { options, aliases } = require('internal/options');
253+
254+
const allowedNodeEnvironmentFlags = [];
255+
for (const [name, info] of options) {
256+
if (info.envVarSettings === kAllowedInEnvironment) {
257+
allowedNodeEnvironmentFlags.push(name);
258+
}
259+
}
260+
261+
for (const [ from, expansion ] of aliases) {
262+
let isAccepted = true;
263+
for (const to of expansion) {
264+
if (!to.startsWith('-') || to === '--') continue;
265+
const recursiveExpansion = aliases.get(to);
266+
if (recursiveExpansion) {
267+
if (recursiveExpansion[0] === to)
268+
recursiveExpansion.splice(0, 1);
269+
expansion.push(...recursiveExpansion);
270+
continue;
271+
}
272+
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
273+
if (!isAccepted) break;
274+
}
275+
if (isAccepted) {
276+
let canonical = from;
277+
if (canonical.endsWith('='))
278+
canonical = canonical.substr(0, canonical.length - 1);
279+
if (canonical.endsWith(' <arg>'))
280+
canonical = canonical.substr(0, canonical.length - 4);
281+
allowedNodeEnvironmentFlags.push(canonical);
282+
}
283+
}
284+
285+
const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, '');
286+
287+
// Save these for comparison against flags provided to
288+
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
289+
// Avoid interference w/ user code by flattening `Set.prototype` into
290+
// each object.
291+
const nodeFlags = Object.defineProperties(
292+
new Set(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
293+
Object.getOwnPropertyDescriptors(Set.prototype)
294+
);
295+
296+
class NodeEnvironmentFlagsSet extends Set {
297+
constructor(...args) {
298+
super(...args);
299+
300+
// the super constructor consumes `add`, but
301+
// disallow any future adds.
302+
this.add = () => this;
303+
}
304+
305+
delete() {
306+
// noop, `Set` API compatible
307+
return false;
308+
}
309+
310+
clear() {
311+
// noop
312+
}
313+
314+
has(key) {
315+
// This will return `true` based on various possible
316+
// permutations of a flag, including present/missing leading
317+
// dash(es) and/or underscores-for-dashes.
318+
// Strips any values after `=`, inclusive.
319+
// TODO(addaleax): It might be more flexible to run the option parser
320+
// on a dummy option set and see whether it rejects the argument or
321+
// not.
322+
if (typeof key === 'string') {
323+
key = replace(key, replaceUnderscoresRegex, '-');
324+
if (test(leadingDashesRegex, key)) {
325+
key = replace(key, trailingValuesRegex, '');
326+
return has(this, key);
327+
}
328+
return has(nodeFlags, key);
329+
}
330+
return false;
331+
}
332+
}
333+
334+
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
335+
Object.freeze(NodeEnvironmentFlagsSet.prototype);
336+
337+
return process.allowedNodeEnvironmentFlags = Object.freeze(
338+
new NodeEnvironmentFlagsSet(
339+
allowedNodeEnvironmentFlags
340+
));
341+
}
342+
343+
function setupAllowedFlags() {
344+
Object.defineProperty(process, 'allowedNodeEnvironmentFlags', {
345+
get: buildAllowedFlags,
346+
set(value) {
347+
// If the user tries to set this to another value, override
348+
// this completely to that value.
349+
Object.defineProperty(this, 'allowedNodeEnvironmentFlags', {
350+
value,
351+
configurable: true,
352+
enumerable: true,
353+
writable: true
354+
});
355+
},
356+
enumerable: true,
357+
configurable: true
358+
});
359+
}
360+
237361
module.exports = {
362+
setupAllowedFlags,
238363
setupAssert,
239364
setupCpuUsage,
240365
setupHrtime,

0 commit comments

Comments
 (0)