watch: strip watch flags from NODE_OPTIONS in child process by marcopiraccini · Pull Request #62143 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion lib/internal/main/watch_mode.js
2 changes: 2 additions & 0 deletions lib/internal/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
getEmbedderOptions: getEmbedderOptionsFromBinding,
getEnvOptionsInputType,
getNamespaceOptionsInputType,
parseNodeOptionsEnvVar,
} = internalBinding('options');

let warnOnAllowUnauthorized = true;
Expand Down Expand Up @@ -222,5 +223,6 @@ module.exports = {
getAllowUnauthorized,
getEmbedderOptions,
generateConfigJsonSchema,
parseNodeOptionsEnvVar,
refreshOptions,
};
26 changes: 26 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,29 @@ void GetOptionsAsFlags(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(result);
}

void ParseNodeOptionsEnvVarBinding(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();

Utf8Value node_options(isolate, args[0]);
std::string options_str(*node_options, node_options.length());

std::vector<std::string> errors;
std::vector<std::string> result =
ParseNodeOptionsEnvVar(options_str, &errors);

if (!errors.empty()) {
Environment* env = Environment::GetCurrent(context);
env->ThrowError(errors[0].c_str());
return;
}

Local<Value> v8_result;
if (ToV8Value(context, result).ToLocal(&v8_result)) {
args.GetReturnValue().Set(v8_result);
}
}

void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand All @@ -2166,6 +2189,8 @@ void Initialize(Local<Object> target,
target,
"getNamespaceOptionsInputType",
GetNamespaceOptionsInputType);
SetMethodNoSideEffect(
context, target, "parseNodeOptionsEnvVar", ParseNodeOptionsEnvVarBinding);
Local<Object> env_settings = Object::New(isolate);
NODE_DEFINE_CONSTANT(env_settings, kAllowedInEnvvar);
NODE_DEFINE_CONSTANT(env_settings, kDisallowedInEnvvar);
Expand Down Expand Up @@ -2194,6 +2219,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetEmbedderOptions);
registry->Register(GetEnvOptionsInputType);
registry->Register(GetNamespaceOptionsInputType);
registry->Register(ParseNodeOptionsEnvVarBinding);
}
} // namespace options_parser

Expand Down
29 changes: 28 additions & 1 deletion test/parallel/test-options-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const common = require('../common');
const assert = require('assert');
const { getOptionValue } = require('internal/options');
const { getOptionValue, parseNodeOptionsEnvVar } = require('internal/options');

Map.prototype.get =
common.mustNotCall('`getOptionValue` must not call user-mutable method');
Expand All @@ -14,3 +14,30 @@ assert.strictEqual(getOptionValue('--nonexistent-option'), undefined);

// Make the test common global leak test happy.
delete Object.prototype['--nonexistent-option'];

// parseNodeOptionsEnvVar tokenizes a NODE_OPTIONS-style string.
assert.deepStrictEqual(
parseNodeOptionsEnvVar('--max-old-space-size=4096 --no-warnings'),
['--max-old-space-size=4096', '--no-warnings']
);

// Quoted strings are unquoted during parsing.
assert.deepStrictEqual(
parseNodeOptionsEnvVar('--require "file with spaces.js"'),
['--require', 'file with spaces.js']
);

// Empty string returns an empty array.
assert.deepStrictEqual(parseNodeOptionsEnvVar(''), []);

// Throws on unterminated string.
assert.throws(
() => parseNodeOptionsEnvVar('--require "unterminated'),
{ name: 'Error', message: /unterminated string/ }
);

// Throws on invalid escape at end of string.
assert.throws(
() => parseNodeOptionsEnvVar('--require "foo\\'),
{ name: 'Error', message: /invalid escape/ }
);
109 changes: 109 additions & 0 deletions test/sequential/test-watch-mode.mjs
Loading