From f0e653d2af83e5fa6a85200d129a3f76433cca1a Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Tue, 27 Aug 2024 13:46:11 +0200 Subject: [PATCH] src: add config file support PR-URL: https://github.com/nodejs/node/pull/57016 Refs: https://github.com/nodejs/node/issues/53787 Reviewed-By: Yagiz Nizipli Reviewed-By: Tierney Cyren Reviewed-By: Michael Dawson Reviewed-By: James M Snell Reviewed-By: Paolo Insogna --- Makefile | 1 + doc/api/cli.md | 63 ++ doc/node.1 | 3 + doc/node_config_json_schema.json | 578 ++++++++++++++++++ lib/internal/options.js | 49 ++ lib/internal/process/pre_execution.js | 8 + node.gyp | 2 + src/node.cc | 34 ++ src/node_config_file.cc | 195 ++++++ src/node_config_file.h | 43 ++ src/node_options.cc | 96 ++- src/node_options.h | 6 + .../dotenv/node-options-no-tranform.env | 1 + test/fixtures/rc/empty-object.json | 3 + test/fixtures/rc/empty.json | 1 + test/fixtures/rc/host-port.json | 3 + test/fixtures/rc/import-as-string.json | 3 + test/fixtures/rc/import.json | 7 + test/fixtures/rc/invalid-import.json | 3 + test/fixtures/rc/negative-numeric.json | 3 + test/fixtures/rc/no-op.json | 3 + test/fixtures/rc/not-node-options-flag.json | 3 + test/fixtures/rc/numeric.json | 3 + test/fixtures/rc/override-property.json | 4 + test/fixtures/rc/sneaky-flag.json | 3 + test/fixtures/rc/string.json | 3 + test/fixtures/rc/test.js | 6 + test/fixtures/rc/transform-types.json | 3 + test/fixtures/rc/unknown-flag.json | 3 + test/fixtures/rc/v8-flag.json | 3 + test/parallel/test-config-file.js | 256 ++++++++ test/parallel/test-config-json-schema.js | 40 ++ tools/doc/generate-json-schema.mjs | 7 + 33 files changed, 1438 insertions(+), 1 deletion(-) create mode 100644 doc/node_config_json_schema.json create mode 100644 src/node_config_file.cc create mode 100644 src/node_config_file.h create mode 100644 test/fixtures/dotenv/node-options-no-tranform.env create mode 100644 test/fixtures/rc/empty-object.json create mode 100644 test/fixtures/rc/empty.json create mode 100644 test/fixtures/rc/host-port.json create mode 100644 test/fixtures/rc/import-as-string.json create mode 100644 test/fixtures/rc/import.json create mode 100644 test/fixtures/rc/invalid-import.json create mode 100644 test/fixtures/rc/negative-numeric.json create mode 100644 test/fixtures/rc/no-op.json create mode 100644 test/fixtures/rc/not-node-options-flag.json create mode 100644 test/fixtures/rc/numeric.json create mode 100644 test/fixtures/rc/override-property.json create mode 100644 test/fixtures/rc/sneaky-flag.json create mode 100644 test/fixtures/rc/string.json create mode 100644 test/fixtures/rc/test.js create mode 100644 test/fixtures/rc/transform-types.json create mode 100644 test/fixtures/rc/unknown-flag.json create mode 100644 test/fixtures/rc/v8-flag.json create mode 100644 test/parallel/test-config-file.js create mode 100644 test/parallel/test-config-json-schema.js create mode 100644 tools/doc/generate-json-schema.mjs diff --git a/Makefile b/Makefile index 39f5ee5e12..69c026755b 100644 --- a/Makefile +++ b/Makefile @@ -809,6 +809,7 @@ doc: $(NODE_EXE) doc-only ## Build Node.js, and then build the documentation wit out/doc: mkdir -p $@ + cp doc/node_config_json_schema.json $@ # If it's a source tarball, doc/api already contains the generated docs. # Just copy everything under doc/api over. diff --git a/doc/api/cli.md b/doc/api/cli.md index 855d83a2d2..37ce213570 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -911,6 +911,69 @@ added: v23.6.0 Enable experimental import support for `.node` addons. +### `--experimental-config-file` + + + +> Stability: 1.0 - Early development + +Use this flag to specify a configuration file that will be loaded and parsed +before the application starts. +Node.js will read the configuration file and apply the settings. +The configuration file should be a JSON file +with the following structure: + +```json +{ + "$schema": "https://nodejs.org/dist/REPLACEME/docs/node_config_json_schema.json", + "experimental-transform-types": true, + "import": [ + "amaro/transform" + ], + "disable-warning": "ExperimentalWarning", + "watch-path": "src", + "watch-preserve-output": true +} +``` + +Only flags that are allowed in [`NODE_OPTIONS`][] are supported. +No-op flags are not supported. +Not all V8 flags are currently supported. + +It is possible to use the [official JSON schema](../node_config_json_schema.json) +to validate the configuration file, which may vary depending on the Node.js version. +Each key in the configuration file corresponds to a flag that can be passed +as a command-line argument. The value of the key is the value that would be +passed to the flag. + +For example, the configuration file above is equivalent to +the following command-line arguments: + +```bash +node --experimental-transform-types --import amaro/transform --disable-warning=ExperimentalWarning --watch-path=src --watch-preserve-output +``` + +The priority in configuration is as follows: + +1. NODE\_OPTIONS and command-line options +2. Configuration file +3. Dotenv NODE\_OPTIONS + +Values in the configuration file will not override the values in the environment +variables and command-line options, but will override the values in the `NODE_OPTIONS` +env file parsed by the `--env-file` flag. + +If duplicate keys are present in the configuration file, only +the first key will be used. + +The configuration parser will throw an error if the configuration file contains +unknown keys or keys that cannot used in `NODE_OPTIONS`. + +Node.js will not sanitize or perform validation on the user-provided configuration, +so **NEVER** use untrusted configuration files. + ### `--experimental-eventsource`