From 2bf46ae45ed49073369627f0e60aa720354ce0d3 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Tue, 13 Mar 2018 15:53:39 -0700 Subject: [PATCH] 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 PR-URL: https://github.com/nodejs/node/pull/19335 Reviewed-By: Ben Noordhuis Reviewed-By: Benjamin Gruenbaum Reviewed-By: Michael Dawson Reviewed-By: Matteo Collina Reviewed-By: Gibson Fahnestock Reviewed-By: John-David Dalton Reviewed-By: Sam Ruby Reviewed-By: Anna Henningsen --- doc/api/process.md | 51 +++++++++++ lib/internal/bootstrap/node.js | 89 +++++++++++++++++++ src/node.cc | 62 +++++++++++++ src/node_config.cc | 17 ++++ src/node_internals.h | 5 ++ .../test-process-env-allowed-flags.js | 74 +++++++++++++++ tools/doc/type-parser.js | 2 +- 7 files changed, 299 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-process-env-allowed-flags.js diff --git a/doc/api/process.md b/doc/api/process.md index 0c35007103c253..5aec229e9e8ed9 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -416,6 +416,55 @@ generate a core file. This feature is not available in [`Worker`][] threads. +## process.allowedNodeEnvironmentFlags + + +* {Set} + +The `process.allowedNodeEnvironmentFlags` property is a special, +read-only `Set` of flags allowable within the [`NODE_OPTIONS`][] +environment variable. + +`process.allowedNodeEnvironmentFlags` extends `Set`, but overrides +`Set.prototype.has` to recognize several different possible flag +representations. `process.allowedNodeEnvironmentFlags.has()` will +return `true` in the following cases: + +- Flags may omit leading single (`-`) or double (`--`) dashes; e.g., + `inspect-brk` for `--inspect-brk`, or `r` for `-r`. +- Flags passed through to V8 (as listed in `--v8-options`) may replace + one or more *non-leading* dashes for an underscore, or vice-versa; + e.g., `--perf_basic_prof`, `--perf-basic-prof`, `--perf_basic-prof`, + etc. +- Flags may contain one or more equals (`=`) characters; all + characters after and including the first equals will be ignored; + e.g., `--stack-trace-limit=100`. +- Flags *must* be allowable within [`NODE_OPTIONS`][]. + +When iterating over `process.allowedNodeEnvironmentFlags`, flags will +appear only *once*; each will begin with one or more dashes. Flags +passed through to V8 will contain underscores instead of non-leading +dashes: + +```js +process.allowedNodeEnvironmentFlags.forEach((flag) => { + // -r + // --inspect-brk + // --abort_on_uncaught_exception + // ... +}); +``` + +The methods `add()`, `clear()`, and `delete()` of +`process.allowedNodeEnvironmentFlags` do nothing, and will fail +silently. + +If Node.js was compiled *without* [`NODE_OPTIONS`][] support (shown in +[`process.config`][]), `process.allowedNodeEnvironmentFlags` will +contain what *would have* been allowable. + ## process.arch