Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: make sure pass the cli_options to worker #52827

Merged
merged 1 commit into from
May 12, 2024
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
39 changes: 21 additions & 18 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -569,26 +569,30 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
}
}
#endif // NODE_WITHOUT_NODE_OPTIONS
}

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;
if (!array->Get(env->context(), i).ToLocal(&arg)) {
return;
}
Local<String> arg_v8;
if (!arg->ToString(env->context()).ToLocal(&arg_v8)) {
return;
if (args[2]->IsArray()) {
Local<Array> array = args[2].As<Array>();
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; i++) {
Local<Value> arg;
if (!array->Get(env->context(), i).ToLocal(&arg)) {
return;
}
Local<String> arg_v8;
if (!arg->ToString(env->context()).ToLocal(&arg_v8)) {
return;
}
Utf8Value arg_utf8_value(args.GetIsolate(), arg_v8);
std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length());
exec_argv.push_back(arg_string);
}
Utf8Value arg_utf8_value(args.GetIsolate(), arg_v8);
std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length());
exec_argv.push_back(arg_string);
} 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{};
Expand All @@ -606,9 +610,8 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
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)) {
if (!ToV8Value(env->context(), errors.size() > 0 ? errors : invalid_args)
.ToLocal(&error)) {
return;
}
Local<String> key =
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-worker-cli-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Flags: --expose-internals
'use strict';
require('../common');
const { Worker } = require('worker_threads');

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