Skip to content

Commit

Permalink
src: make sure pass the cli_options to worker
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed May 4, 2024
1 parent 4e9ce7c commit 452bedf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
64 changes: 32 additions & 32 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
}

if (args[1]->IsObject() || args[2]->IsArray()) {
Mutex::ScopedLock lock(per_process::cli_options_mutex);
per_isolate_opts.reset(new PerIsolateOptions());

HandleEnvOptions(per_isolate_opts->per_env, [&env_vars](const char* name) {
Expand Down Expand Up @@ -570,12 +571,11 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
}
#endif // NODE_WITHOUT_NODE_OPTIONS
}

// The first argument is reserved for program name, but we don't need it
// in workers.
std::vector<std::string> exec_argv = {""};
if (args[2]->IsArray()) {
Local<Array> array = args[2].As<Array>();
// The first argument is reserved for program name, but we don't need it
// in workers.
std::vector<std::string> exec_argv = {""};
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; i++) {
Local<Value> arg;
Expand All @@ -590,38 +590,38 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length());
exec_argv.push_back(arg_string);
}

std::vector<std::string> invalid_args{};
std::vector<std::string> errors{};
// Using invalid_args as the v8_args argument as it stores unknown
// options for the per isolate parser.
options_parser::Parse(&exec_argv,
&exec_argv_out,
&invalid_args,
per_isolate_opts.get(),
kDisallowedInEnvvar,
&errors);

// The first argument is program name.
invalid_args.erase(invalid_args.begin());
if (errors.size() > 0 || invalid_args.size() > 0) {
Local<Value> error;
if (!ToV8Value(env->context(),
errors.size() > 0 ? errors : invalid_args)
.ToLocal(&error)) {
return;
}
Local<String> key =
FIXED_ONE_BYTE_STRING(env->isolate(), "invalidExecArgv");
// Ignore the return value of Set() because exceptions bubble up to JS
// when we return anyway.
USE(args.This()->Set(env->context(), key, error));
return;
}
} else {
exec_argv_out = env->exec_argv();
exec_argv.insert(exec_argv.end(), exec_argv_out.begin(), exec_argv_out.end());
}

std::vector<std::string> invalid_args{};
std::vector<std::string> errors{};
// Using invalid_args as the v8_args argument as it stores unknown
// options for the per isolate parser.
options_parser::Parse(&exec_argv,
&exec_argv_out,
&invalid_args,
per_isolate_opts.get(),
kDisallowedInEnvvar,
&errors);

// The first argument is program name.
invalid_args.erase(invalid_args.begin());
if (errors.size() > 0 || invalid_args.size() > 0) {
Local<Value> error;
if (!ToV8Value(env->context(),
errors.size() > 0 ? errors : invalid_args)
.ToLocal(&error)) {
return;
}
Local<String> key =
FIXED_ONE_BYTE_STRING(env->isolate(), "invalidExecArgv");
// Ignore the return value of Set() because exceptions bubble up to JS
// when we return anyway.
USE(args.This()->Set(env->context(), key, error));
return;
}
const SnapshotData* snapshot_data = env->isolate_data()->snapshot_data();

Worker* worker = new Worker(env,
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-worker-cli-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Flags: --expose-internals
'use strict';
require('../common');
const { Worker } = require('worker_threads');

// Test if the flags is passed to worker threads
// See https://github.com/nodejs/node/issues/52825
new Worker(`
// If the --expose-internals flag does not pass to worker
// require function will throw an error
require('internal/options');
`, { eval: true, env: process.env });

0 comments on commit 452bedf

Please sign in to comment.