Skip to content

Commit d77cf92

Browse files
joyeecheungBridgeAR
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 0894899 commit d77cf92

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
@@ -223,7 +223,7 @@
223223

224224
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
225225

226-
setupAllowedFlags();
226+
perThreadSetup.setupAllowedFlags();
227227

228228
startExecution();
229229
}
@@ -739,128 +739,5 @@
739739
new vm.Script(source, { displayErrors: true, filename });
740740
}
741741

742-
function setupAllowedFlags() {
743-
// This builds process.allowedNodeEnvironmentFlags
744-
// from data in the config binding
745-
746-
const replaceUnderscoresRegex = /_/g;
747-
const leadingDashesRegex = /^--?/;
748-
const trailingValuesRegex = /=.*$/;
749-
750-
// Save references so user code does not interfere
751-
const replace = Function.call.bind(String.prototype.replace);
752-
const has = Function.call.bind(Set.prototype.has);
753-
const test = Function.call.bind(RegExp.prototype.test);
754-
755-
const get = () => {
756-
const {
757-
envSettings: { kAllowedInEnvironment }
758-
} = internalBinding('options');
759-
const { options, aliases } = NativeModule.require('internal/options');
760-
761-
const allowedNodeEnvironmentFlags = [];
762-
for (const [name, info] of options) {
763-
if (info.envVarSettings === kAllowedInEnvironment) {
764-
allowedNodeEnvironmentFlags.push(name);
765-
}
766-
}
767-
768-
for (const [ from, expansion ] of aliases) {
769-
let isAccepted = true;
770-
for (const to of expansion) {
771-
if (!to.startsWith('-') || to === '--') continue;
772-
const recursiveExpansion = aliases.get(to);
773-
if (recursiveExpansion) {
774-
if (recursiveExpansion[0] === to)
775-
recursiveExpansion.splice(0, 1);
776-
expansion.push(...recursiveExpansion);
777-
continue;
778-
}
779-
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
780-
if (!isAccepted) break;
781-
}
782-
if (isAccepted) {
783-
let canonical = from;
784-
if (canonical.endsWith('='))
785-
canonical = canonical.substr(0, canonical.length - 1);
786-
if (canonical.endsWith(' <arg>'))
787-
canonical = canonical.substr(0, canonical.length - 4);
788-
allowedNodeEnvironmentFlags.push(canonical);
789-
}
790-
}
791-
792-
const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, '');
793-
794-
// Save these for comparison against flags provided to
795-
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
796-
// Avoid interference w/ user code by flattening `Set.prototype` into
797-
// each object.
798-
const nodeFlags = Object.defineProperties(
799-
new Set(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
800-
Object.getOwnPropertyDescriptors(Set.prototype)
801-
);
802-
803-
class NodeEnvironmentFlagsSet extends Set {
804-
constructor(...args) {
805-
super(...args);
806-
807-
// the super constructor consumes `add`, but
808-
// disallow any future adds.
809-
this.add = () => this;
810-
}
811-
812-
delete() {
813-
// noop, `Set` API compatible
814-
return false;
815-
}
816-
817-
clear() {
818-
// noop
819-
}
820-
821-
has(key) {
822-
// This will return `true` based on various possible
823-
// permutations of a flag, including present/missing leading
824-
// dash(es) and/or underscores-for-dashes.
825-
// Strips any values after `=`, inclusive.
826-
// TODO(addaleax): It might be more flexible to run the option parser
827-
// on a dummy option set and see whether it rejects the argument or
828-
// not.
829-
if (typeof key === 'string') {
830-
key = replace(key, replaceUnderscoresRegex, '-');
831-
if (test(leadingDashesRegex, key)) {
832-
key = replace(key, trailingValuesRegex, '');
833-
return has(this, key);
834-
}
835-
return has(nodeFlags, key);
836-
}
837-
return false;
838-
}
839-
}
840-
841-
Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
842-
Object.freeze(NodeEnvironmentFlagsSet.prototype);
843-
844-
return process.allowedNodeEnvironmentFlags = Object.freeze(
845-
new NodeEnvironmentFlagsSet(
846-
allowedNodeEnvironmentFlags
847-
));
848-
};
849-
850-
Object.defineProperty(process, 'allowedNodeEnvironmentFlags', {
851-
get,
852-
set(value) {
853-
Object.defineProperty(this, 'allowedNodeEnvironmentFlags', {
854-
value,
855-
configurable: true,
856-
enumerable: true,
857-
writable: true
858-
});
859-
},
860-
enumerable: true,
861-
configurable: true
862-
});
863-
}
864-
865742
startup();
866743
});

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)