Skip to content

Commit a0752dd

Browse files
committed
Allow cli flags in ELECTRON_RUN_AS_NODE mode
1 parent 6cf2a51 commit a0752dd

3 files changed

Lines changed: 51 additions & 26 deletions

File tree

‎script/node-disabled-tests.json‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313
"parallel/test-buffer-backing-arraybuffer",
1414
"parallel/test-buffer-constructor-node-modules-paths",
1515
"parallel/test-child-process-fork-exec-path",
16-
"parallel/test-cli-bad-options",
1716
"parallel/test-cli-node-print-help",
18-
"parallel/test-child-process-windows-hide",
1917
"parallel/test-cli-eval",
20-
"parallel/test-cli-syntax-eval",
21-
"parallel/test-cli-node-options",
22-
"parallel/test-cli-syntax-piped-good",
23-
"parallel/test-cli-syntax-piped-bad",
2418
"parallel/test-code-cache",
2519
"parallel/test-common-gc",
2620
"parallel/test-crypto-aes-wrap",
@@ -49,7 +43,6 @@
4943
"parallel/test-debug-usage",
5044
"parallel/test-domain-async-id-map-leak",
5145
"parallel/test-domain-abort-on-uncaught",
52-
"parallel/test-dummy-stdio",
5346
"parallel/test-domain-with-abort-on-uncaught-exception",
5447
"parallel/test-freeze-intrinsics",
5548
"parallel/test-fs-write-sigxfsz",
@@ -62,7 +55,6 @@
6255
"parallel/test-inspector-port-zero-cluster",
6356
"parallel/test-inspector-vm-global-accessors-getter-sideeffect",
6457
"parallel/test-inspector-vm-global-accessors-sideeffects",
65-
"parallel/test-internal-util-weakreference",
6658
"parallel/test-inspector-heap-allocation-tracker",
6759
"parallel/test-module-loading-globalpaths",
6860
"parallel/test-module-version",
@@ -74,21 +66,17 @@
7466
"parallel/test-process-exception-capture",
7567
"parallel/test-process-exception-capture-should-abort-on-uncaught",
7668
"parallel/test-process-exception-capture-should-abort-on-uncaught-setflagsfromstring",
77-
"parallel/test-process-exec-argv",
7869
"parallel/test-process-external-stdio-close",
7970
"parallel/test-process-external-stdio-close-spawn",
8071
"parallel/test-process-versions",
8172
"parallel/test-readline-interface",
82-
"parallel/test-inspector-esm",
83-
"parallel/test-repl-harmony",
8473
"parallel/test-inspector-tracing-domain",
8574
"parallel/test-repl",
8675
"parallel/test-repl-pretty-custom-stack",
8776
"parallel/test-repl-pretty-stack",
8877
"parallel/test-repl-uncaught-exception",
8978
"parallel/test-repl-underscore",
9079
"parallel/test-signal-handler",
91-
"parallel/test-security-revert-unknown",
9280
"parallel/test-stdout-close-catch",
9381
"parallel/test-policy-integrity",
9482
"parallel/test-repl-tab-complete",
@@ -152,7 +140,6 @@
152140
"parallel/test-trace-events-vm",
153141
"parallel/test-trace-events-v8",
154142
"parallel/test-trace-events-worker-metadata",
155-
"parallel/test-v8-flags",
156143
"parallel/test-v8-coverage",
157144
"parallel/test-vm-basic",
158145
"parallel/test-vm-codegen",

‎shell/app/node_main.cc‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
#include "shell/app/node_main.h"
66

77
#include <memory>
8+
#include <set>
89
#include <string>
910
#include <utility>
11+
#include <vector>
1012

1113
#include "base/command_line.h"
1214
#include "base/feature_list.h"
15+
#include "base/strings/string_util.h"
1316
#include "base/task/thread_pool/thread_pool_instance.h"
1417
#include "base/threading/thread_task_runner_handle.h"
1518
#include "electron/electron_version.h"
@@ -29,6 +32,42 @@
2932
#include "shell/common/crash_reporter/crash_reporter_win.h"
3033
#endif
3134

35+
namespace {
36+
37+
// Initialize Node.js cli options to pass to Node.js
38+
// See https://nodejs.org/api/cli.html#cli_options
39+
void SetNodeCliFlags() {
40+
// Options that are unilaterally disallowed
41+
const std::set<std::string> disallowed = {
42+
"--openssl-config", "--use-bundled-ca", "--use-openssl-ca",
43+
"--force-fips", "--enable-fips"};
44+
45+
std::vector<std::string> args;
46+
for (const auto& arg : base::CommandLine::ForCurrentProcess()->argv()) {
47+
#if defined(OS_WIN)
48+
std::string option = base::UTF16ToUTF8(arg);
49+
#else
50+
std::string option = arg;
51+
#endif
52+
auto stripped = option.substr(0, option.find("="));
53+
if (disallowed.find(stripped) != disallowed.end()) {
54+
LOG(ERROR) << "The Node.js cli flag " << stripped
55+
<< " is not supported in Electron";
56+
} else {
57+
args.push_back(option);
58+
}
59+
}
60+
61+
std::vector<std::string> exec_args;
62+
std::vector<std::string> errors;
63+
64+
// Node.js itself will output parsing errors to
65+
// console so we don't need to handle that ourselves
66+
ProcessGlobalArgs(&args, &exec_args, &errors, node::kDisallowedInEnvironment);
67+
}
68+
69+
} // namespace
70+
3271
namespace electron {
3372

3473
#if !defined(OS_LINUX)
@@ -64,6 +103,9 @@ int NodeMain(int argc, char* argv[]) {
64103
// Explicitly register electron's builtin modules.
65104
NodeBindings::RegisterBuiltinModules();
66105

106+
// Parse and set Node.js cli flags.
107+
SetNodeCliFlags();
108+
67109
int exec_argc;
68110
const char** exec_argv;
69111
node::Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);

‎shell/common/node_bindings.cc‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,36 +139,32 @@ bool IsPackagedApp() {
139139
// Initialize Node.js cli options to pass to Node.js
140140
// See https://nodejs.org/api/cli.html#cli_options
141141
void SetNodeCliFlags() {
142-
// Only allow DebugOptions for now
142+
// Only allow DebugOptions in non-ELECTRON_RUN_AS_NODE mode
143143
const std::set<std::string> allowed = {"--inspect", "--inspect-brk",
144144
"--debug", "--inspect-brk-node",
145145
"--inspect-publish-uid"};
146146

147147
std::vector<std::string> args;
148148
for (const auto& arg : base::CommandLine::ForCurrentProcess()->argv()) {
149-
auto stripped = arg.substr(0, arg.find("="));
150149
#if defined(OS_WIN)
151-
std::string option = base::UTF16ToUTF8(stripped);
150+
std::string option = base::UTF16ToUTF8(arg);
152151
#else
153-
std::string option = stripped;
152+
std::string option = arg;
154153
#endif
155-
if (allowed.find(option) != allowed.end()) {
154+
auto stripped = arg.substr(0, arg.find("="));
155+
if (allowed.find(stripped) != allowed.end())
156156
args.push_back(option);
157-
} else {
158-
LOG(ERROR) << "The Node.js cli flag " << option
159-
<< " is not supported in Electron";
160-
}
161157
}
162158

163159
std::vector<std::string> exec_args;
164160
std::vector<std::string> errors;
165161
const int exit_code = ProcessGlobalArgs(&args, &exec_args, &errors,
166162
node::kAllowedInEnvironment);
167163
if (exit_code != 0) {
168-
LOG(ERROR) << "Error parsing Node.js cli flags";
169-
} else if (!errors.empty()) {
170-
LOG(ERROR) << "Error parsing node cli flags: "
171-
<< base::JoinString(errors, " ");
164+
if (!errors.empty())
165+
LOG(ERROR) << base::JoinString(errors, " ");
166+
else
167+
LOG(ERROR) << "Error parsing Node.js cli flags";
172168
}
173169
}
174170

0 commit comments

Comments
 (0)