From 547e74bb22a0e5f0adbbec006250337c581f9bbf Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Wed, 5 Apr 2017 14:06:52 -0700 Subject: [PATCH] src: use option parser for expose_internals bootstrap_node.js was directly parsing process.execArgv to see if internals should be exposed, even though the argv was already parsed by node. This is unusual and unnecessary, change it to set the option value from the parser onto the config binding. Backport-PR-URL: https://github.com/nodejs/node/pull/14483 PR-URL: https://github.com/nodejs/node/pull/12245 Reviewed-By: Richard Lau Reviewed-By: James M Snell Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig --- lib/internal/bootstrap_node.js | 6 ++---- src/node.cc | 8 +++++++- src/node_config.cc | 3 +++ src/node_internals.h | 6 ++++++ test/parallel/test-internal-modules-expose.js | 4 ++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 55dd4057ac95ce..de215dc2dce62b 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -443,11 +443,9 @@ return NativeModule._source.hasOwnProperty(id); }; - const EXPOSE_INTERNALS = process.execArgv.some(function(arg) { - return arg.match(/^--expose[-_]internals$/); - }); + const config = process.binding('config'); - if (EXPOSE_INTERNALS) { + if (config.exposeInternals) { NativeModule.nonInternalExists = NativeModule.exists; NativeModule.isInternal = function(id) { diff --git a/src/node.cc b/src/node.cc index 180afa25f78f3e..dfe84c67c165f8 100644 --- a/src/node.cc +++ b/src/node.cc @@ -199,6 +199,12 @@ bool trace_warnings = false; // that is used by lib/module.js bool config_preserve_symlinks = false; +// Set in node.cc by ParseArgs when --expose-internals or --expose_internals is +// used. +// Used in node_config.cc to set a constant on process.binding('config') +// that is used by lib/internal/bootstrap_node.js +bool config_expose_internals = false; + // process-relative uptime base, initialized at start-up static double prog_start_time; static bool debugger_running; @@ -3896,7 +3902,7 @@ static void ParseArgs(int* argc, #endif } else if (strcmp(arg, "--expose-internals") == 0 || strcmp(arg, "--expose_internals") == 0) { - // consumed in js + config_expose_internals = true; } else if (strcmp(arg, "--") == 0) { index += 1; break; diff --git a/src/node_config.cc b/src/node_config.cc index 401345f6a608be..4408bc3cf33ffc 100644 --- a/src/node_config.cc +++ b/src/node_config.cc @@ -44,6 +44,9 @@ void InitConfig(Local target, if (config_preserve_symlinks) READONLY_BOOLEAN_PROPERTY("preserveSymlinks"); + + if (config_expose_internals) + READONLY_BOOLEAN_PROPERTY("exposeInternals"); } // InitConfig } // namespace node diff --git a/src/node_internals.h b/src/node_internals.h index cb71720e8205d9..e93244a94c6927 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -43,6 +43,12 @@ extern std::string openssl_config; // that is used by lib/module.js extern bool config_preserve_symlinks; +// Set in node.cc by ParseArgs when --expose-internals or --expose_internals is +// used. +// Used in node_config.cc to set a constant on process.binding('config') +// that is used by lib/internal/bootstrap_node.js +extern bool config_expose_internals; + // Forward declaration class Environment; diff --git a/test/parallel/test-internal-modules-expose.js b/test/parallel/test-internal-modules-expose.js index 02ddfd87c363fc..ab48e36881268c 100644 --- a/test/parallel/test-internal-modules-expose.js +++ b/test/parallel/test-internal-modules-expose.js @@ -3,5 +3,9 @@ require('../common'); const assert = require('assert'); +const config = process.binding('config'); + +console.log(config, process.argv); assert.strictEqual(typeof require('internal/freelist').FreeList, 'function'); +assert.strictEqual(config.exposeInternals, true);