summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristopher Hiller <boneskull@boneskull.com>2018-03-13 15:53:39 -0700
committerRich Trott <rtrott@gmail.com>2018-08-24 18:15:19 -0700
commit80143f616d8fb56c1d5e98c0d0857a1288b8bf87 (patch)
treecd24ad53424854e90e6390d122cbc82a84cc701e /lib
parent6dd694c1257ded6d8a9f3b48e8c9027c3986a285 (diff)
downloadandroid-node-v8-80143f616d8fb56c1d5e98c0d0857a1288b8bf87.tar.gz
android-node-v8-80143f616d8fb56c1d5e98c0d0857a1288b8bf87.tar.bz2
android-node-v8-80143f616d8fb56c1d5e98c0d0857a1288b8bf87.zip
process: add allowedNodeEnvironmentFlags property
`process.allowedNodeEnvironmentFlags` provides an API to validate and list flags as specified in `NODE_OPTIONS` from user code. Refs: https://github.com/nodejs/node/issues/17740 Signed-off-by: Christopher Hiller <boneskull@boneskull.com> PR-URL: https://github.com/nodejs/node/pull/19335 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Sam Ruby <rubys@intertwingly.net> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/bootstrap/node.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/internal/bootstrap/node.js b/lib/internal/bootstrap/node.js
index e4748602f3..c98ea20798 100644
--- a/lib/internal/bootstrap/node.js
+++ b/lib/internal/bootstrap/node.js
@@ -185,6 +185,8 @@
perf.markMilestone(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
+ setupAllowedFlags();
+
// There are various modes that Node can run in. The most common two
// are running from a script and running the REPL - but there are a few
// others like the debugger or running --eval arguments. Here we decide
@@ -635,5 +637,92 @@
new vm.Script(source, { displayErrors: true, filename });
}
+ function setupAllowedFlags() {
+ // This builds process.allowedNodeEnvironmentFlags
+ // from data in the config binding
+
+ const replaceDashesRegex = /-/g;
+ const leadingDashesRegex = /^--?/;
+ const trailingValuesRegex = /=.*$/;
+
+ // Save references so user code does not interfere
+ const replace = Function.call.bind(String.prototype.replace);
+ const has = Function.call.bind(Set.prototype.has);
+ const test = Function.call.bind(RegExp.prototype.test);
+
+ const {
+ allowedV8EnvironmentFlags,
+ allowedNodeEnvironmentFlags
+ } = process.binding('config');
+
+ const trimLeadingDashes = (flag) => replace(flag, leadingDashesRegex, '');
+
+ // Save these for comparison against flags provided to
+ // process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
+ // Avoid interference w/ user code by flattening `Set.prototype` into
+ // each object.
+ const [nodeFlags, v8Flags] = [
+ allowedNodeEnvironmentFlags, allowedV8EnvironmentFlags
+ ].map((flags) => Object.defineProperties(
+ new Set(flags.map(trimLeadingDashes)),
+ Object.getOwnPropertyDescriptors(Set.prototype))
+ );
+
+ class NodeEnvironmentFlagsSet extends Set {
+ constructor(...args) {
+ super(...args);
+
+ // the super constructor consumes `add`, but
+ // disallow any future adds.
+ this.add = () => this;
+ }
+
+ delete() {
+ // noop, `Set` API compatible
+ return false;
+ }
+
+ clear() {
+ // noop
+ }
+
+ has(key) {
+ // This will return `true` based on various possible
+ // permutations of a flag, including present/missing leading
+ // dash(es) and/or underscores-for-dashes in the case of V8-specific
+ // flags. Strips any values after `=`, inclusive.
+ if (typeof key === 'string') {
+ key = replace(key, trailingValuesRegex, '');
+ if (test(leadingDashesRegex, key)) {
+ return has(this, key) ||
+ has(v8Flags,
+ replace(
+ replace(
+ key,
+ leadingDashesRegex,
+ ''
+ ),
+ replaceDashesRegex,
+ '_'
+ )
+ );
+ }
+ return has(nodeFlags, key) ||
+ has(v8Flags, replace(key, replaceDashesRegex, '_'));
+ }
+ return false;
+ }
+ }
+
+ Object.freeze(NodeEnvironmentFlagsSet.prototype.constructor);
+ Object.freeze(NodeEnvironmentFlagsSet.prototype);
+
+ process.allowedNodeEnvironmentFlags = Object.freeze(
+ new NodeEnvironmentFlagsSet(
+ allowedNodeEnvironmentFlags.concat(allowedV8EnvironmentFlags)
+ )
+ );
+ }
+
startup();
});