GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_options.cc Lines: 530 557 95.2 %
Date: 2020-11-20 19:51:53 Branches: 187 242 77.3 %

Line Branch Exec Source
1
#include "node_options.h"  // NOLINT(build/include_inline)
2
#include "node_options-inl.h"
3
4
#include "env-inl.h"
5
#include "node_binding.h"
6
#include "node_internals.h"
7
8
#include <errno.h>
9
#include <sstream>
10
#include <cstdlib>  // strtoul, errno
11
12
using v8::Boolean;
13
using v8::Context;
14
using v8::FunctionCallbackInfo;
15
using v8::Integer;
16
using v8::Isolate;
17
using v8::Local;
18
using v8::Map;
19
using v8::Number;
20
using v8::Object;
21
using v8::Undefined;
22
using v8::Value;
23
24
namespace node {
25
26
namespace per_process {
27
4678
Mutex cli_options_mutex;
28
4678
std::shared_ptr<PerProcessOptions> cli_options{new PerProcessOptions()};
29
}  // namespace per_process
30
31
9410
void DebugOptions::CheckOptions(std::vector<std::string>* errors) {
32
#if !NODE_USE_V8_PLATFORM && !HAVE_INSPECTOR
33
  if (inspector_enabled) {
34
    errors->push_back("Inspector is not available when Node is compiled "
35
                      "--without-v8-platform and --without-inspector.");
36
  }
37
#endif
38
39
9410
  if (deprecated_debug) {
40
8
    errors->push_back("[DEP0062]: `node --debug` and `node --debug-brk` "
41
                      "are invalid. Please use `node --inspect` and "
42
4
                      "`node --inspect-brk` instead.");
43
  }
44
45
  std::vector<std::string> destinations =
46
18820
      SplitString(inspect_publish_uid_string, ',');
47
9410
  inspect_publish_uid.console = false;
48
9410
  inspect_publish_uid.http = false;
49
28228
  for (const std::string& destination : destinations) {
50
18818
    if (destination == "stderr") {
51
9409
      inspect_publish_uid.console = true;
52
9409
    } else if (destination == "http") {
53
9409
      inspect_publish_uid.http = true;
54
    } else {
55
      errors->push_back("--inspect-publish-uid destination can be "
56
                        "stderr or http");
57
    }
58
  }
59
9410
}
60
61
9207
void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) {
62
#if HAVE_OPENSSL
63

9207
  if (use_openssl_ca && use_bundled_ca) {
64
2
    errors->push_back("either --use-openssl-ca or --use-bundled-ca can be "
65
1
                      "used, not both");
66
  }
67
#endif
68

18416
  if (use_largepages != "off" &&
69

9208
      use_largepages != "on" &&
70
1
      use_largepages != "silent") {
71
1
    errors->push_back("invalid value for --use-largepages");
72
  }
73
9207
  per_isolate->CheckOptions(errors);
74
9207
}
75
76
9410
void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors) {
77
9410
  per_env->CheckOptions(errors);
78
9410
}
79
80
9410
void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
81

9410
  if (has_policy_integrity_string && experimental_policy.empty()) {
82
    errors->push_back("--policy-integrity requires "
83
                      "--experimental-policy be enabled");
84
  }
85

9410
  if (has_policy_integrity_string && experimental_policy_integrity.empty()) {
86
1
    errors->push_back("--policy-integrity cannot be empty");
87
  }
88
89
9410
  if (!module_type.empty()) {
90

20
    if (module_type != "commonjs" && module_type != "module") {
91
      errors->push_back("--input-type must be \"module\" or \"commonjs\"");
92
    }
93
  }
94
95
9410
  if (!experimental_specifier_resolution.empty()) {
96

10
    if (experimental_specifier_resolution != "node" &&
97
        experimental_specifier_resolution != "explicit") {
98
      errors->push_back(
99
        "invalid value for --experimental-specifier-resolution");
100
    }
101
  }
102
103

9410
  if (syntax_check_only && has_eval_string) {
104
4
    errors->push_back("either --check or --eval can be used, not both");
105
  }
106
107

18837
  if (!unhandled_rejections.empty() &&
108
33
      unhandled_rejections != "warn-with-error-code" &&
109
30
      unhandled_rejections != "throw" &&
110
26
      unhandled_rejections != "strict" &&
111

9429
      unhandled_rejections != "warn" &&
112
7
      unhandled_rejections != "none") {
113
1
    errors->push_back("invalid value for --unhandled-rejections");
114
  }
115
116

9410
  if (tls_min_v1_3 && tls_max_v1_2) {
117
2
    errors->push_back("either --tls-min-v1.3 or --tls-max-v1.2 can be "
118
1
                      "used, not both");
119
  }
120
121
9410
  if (heap_snapshot_near_heap_limit < 0) {
122
1
    errors->push_back("--heap-snapshot-near-heap-limit must not be negative");
123
  }
124
125
#if HAVE_INSPECTOR
126
9410
  if (!cpu_prof) {
127
9399
    if (!cpu_prof_name.empty()) {
128
1
      errors->push_back("--cpu-prof-name must be used with --cpu-prof");
129
    }
130
9399
    if (!cpu_prof_dir.empty()) {
131
1
      errors->push_back("--cpu-prof-dir must be used with --cpu-prof");
132
    }
133
    // We can't catch the case where the value passed is the default value,
134
    // then the option just becomes a noop which is fine.
135
9399
    if (cpu_prof_interval != kDefaultCpuProfInterval) {
136
1
      errors->push_back("--cpu-prof-interval must be used with --cpu-prof");
137
    }
138
  }
139
140


9410
  if (cpu_prof && cpu_prof_dir.empty() && !diagnostic_dir.empty()) {
141
1
      cpu_prof_dir = diagnostic_dir;
142
    }
143
144
9410
  if (!heap_prof) {
145
9399
    if (!heap_prof_name.empty()) {
146
1
      errors->push_back("--heap-prof-name must be used with --heap-prof");
147
    }
148
9399
    if (!heap_prof_dir.empty()) {
149
1
      errors->push_back("--heap-prof-dir must be used with --heap-prof");
150
    }
151
    // We can't catch the case where the value passed is the default value,
152
    // then the option just becomes a noop which is fine.
153
9399
    if (heap_prof_interval != kDefaultHeapProfInterval) {
154
1
      errors->push_back("--heap-prof-interval must be used with --heap-prof");
155
    }
156
  }
157
158


9410
  if (heap_prof && heap_prof_dir.empty() && !diagnostic_dir.empty()) {
159
1
    heap_prof_dir = diagnostic_dir;
160
  }
161
162
9410
  debug_options_.CheckOptions(errors);
163
#endif  // HAVE_INSPECTOR
164
9410
}
165
166
namespace options_parser {
167
168
4678
class DebugOptionsParser : public OptionsParser<DebugOptions> {
169
 public:
170
  DebugOptionsParser();
171
};
172
173
4678
class EnvironmentOptionsParser : public OptionsParser<EnvironmentOptions> {
174
 public:
175
  EnvironmentOptionsParser();
176
4678
  explicit EnvironmentOptionsParser(const DebugOptionsParser& dop)
177
4678
    : EnvironmentOptionsParser() {
178
4678
    Insert(dop, &EnvironmentOptions::get_debug_options);
179
4678
  }
180
};
181
182
4678
class PerIsolateOptionsParser : public OptionsParser<PerIsolateOptions> {
183
 public:
184
  PerIsolateOptionsParser() = delete;
185
  explicit PerIsolateOptionsParser(const EnvironmentOptionsParser& eop);
186
};
187
188
4678
class PerProcessOptionsParser : public OptionsParser<PerProcessOptions> {
189
 public:
190
  PerProcessOptionsParser() = delete;
191
  explicit PerProcessOptionsParser(const PerIsolateOptionsParser& iop);
192
};
193
194
#if HAVE_INSPECTOR
195
4678
const DebugOptionsParser _dop_instance{};
196
4678
const EnvironmentOptionsParser _eop_instance{_dop_instance};
197
198
// This Parse is not dead code. It is used by embedders (e.g., Electron).
199
template <>
200
void Parse(
201
  StringVector* const args, StringVector* const exec_args,
202
  StringVector* const v8_args,
203
  DebugOptions* const options,
204
  OptionEnvvarSettings required_env_settings, StringVector* const errors) {
205
  _dop_instance.Parse(
206
    args, exec_args, v8_args, options, required_env_settings, errors);
207
}
208
#else
209
const EnvironmentOptionsParser _eop_instance{};
210
#endif  // HAVE_INSPECTOR
211
4678
const PerIsolateOptionsParser _piop_instance{_eop_instance};
212
4678
const PerProcessOptionsParser _ppop_instance{_piop_instance};
213
214
template <>
215
203
void Parse(
216
  StringVector* const args, StringVector* const exec_args,
217
  StringVector* const v8_args,
218
  PerIsolateOptions* const options,
219
  OptionEnvvarSettings required_env_settings, StringVector* const errors) {
220
  _piop_instance.Parse(
221
203
    args, exec_args, v8_args, options, required_env_settings, errors);
222
203
}
223
224
template <>
225
9207
void Parse(
226
  StringVector* const args, StringVector* const exec_args,
227
  StringVector* const v8_args,
228
  PerProcessOptions* const options,
229
  OptionEnvvarSettings required_env_settings, StringVector* const errors) {
230
  _ppop_instance.Parse(
231
9207
    args, exec_args, v8_args, options, required_env_settings, errors);
232
9207
}
233
234
// XXX: If you add an option here, please also add it to doc/node.1 and
235
// doc/api/cli.md
236
// TODO(addaleax): Make that unnecessary.
237
238
4678
DebugOptionsParser::DebugOptionsParser() {
239
4678
  AddOption("--inspect-port",
240
            "set host:port for inspector",
241
            &DebugOptions::host_port,
242
4678
            kAllowedInEnvironment);
243
4678
  AddAlias("--debug-port", "--inspect-port");
244
245
4678
  AddOption("--inspect",
246
            "activate inspector on host:port (default: 127.0.0.1:9229)",
247
            &DebugOptions::inspector_enabled,
248
4678
            kAllowedInEnvironment);
249
4678
  AddAlias("--inspect=", { "--inspect-port", "--inspect" });
250
251
4678
  AddOption("--debug", "", &DebugOptions::deprecated_debug);
252
4678
  AddAlias("--debug=", "--debug");
253
4678
  AddOption("--debug-brk", "", &DebugOptions::deprecated_debug);
254
4678
  AddAlias("--debug-brk=", "--debug-brk");
255
256
4678
  AddOption("--inspect-brk",
257
            "activate inspector on host:port and break at start of user script",
258
            &DebugOptions::break_first_line,
259
4678
            kAllowedInEnvironment);
260
4678
  Implies("--inspect-brk", "--inspect");
261
4678
  AddAlias("--inspect-brk=", { "--inspect-port", "--inspect-brk" });
262
263
4678
  AddOption("--inspect-brk-node", "", &DebugOptions::break_node_first_line);
264
4678
  Implies("--inspect-brk-node", "--inspect");
265
4678
  AddAlias("--inspect-brk-node=", { "--inspect-port", "--inspect-brk-node" });
266
267
4678
  AddOption("--inspect-publish-uid",
268
            "comma separated list of destinations for inspector uid"
269
            "(default: stderr,http)",
270
            &DebugOptions::inspect_publish_uid_string,
271
4678
            kAllowedInEnvironment);
272
4678
}
273
274
4678
EnvironmentOptionsParser::EnvironmentOptionsParser() {
275
4678
  AddOption("--conditions",
276
            "additional user conditions for conditional exports and imports",
277
            &EnvironmentOptions::conditions,
278
4678
            kAllowedInEnvironment);
279
4678
  AddOption("--diagnostic-dir",
280
            "set dir for all output files"
281
            " (default: current working directory)",
282
            &EnvironmentOptions::diagnostic_dir,
283
4678
            kAllowedInEnvironment);
284
4678
  AddOption("--enable-source-maps",
285
            "experimental Source Map V3 support",
286
            &EnvironmentOptions::enable_source_maps,
287
4678
            kAllowedInEnvironment);
288
4678
  AddOption("--experimental-abortcontroller", "",
289
4678
            NoOp{}, kAllowedInEnvironment);
290
4678
  AddOption("--experimental-json-modules",
291
            "experimental JSON interop support for the ES Module loader",
292
            &EnvironmentOptions::experimental_json_modules,
293
4678
            kAllowedInEnvironment);
294
4678
  AddOption("--experimental-loader",
295
            "use the specified module as a custom loader",
296
            &EnvironmentOptions::userland_loader,
297
4678
            kAllowedInEnvironment);
298
4678
  AddAlias("--loader", "--experimental-loader");
299
4678
  AddOption("--experimental-modules",
300
            "",
301
            &EnvironmentOptions::experimental_modules,
302
4678
            kAllowedInEnvironment);
303
4678
  AddOption("--experimental-wasm-modules",
304
            "experimental ES Module support for webassembly modules",
305
            &EnvironmentOptions::experimental_wasm_modules,
306
4678
            kAllowedInEnvironment);
307
4678
  AddOption("--experimental-import-meta-resolve",
308
            "experimental ES Module import.meta.resolve() support",
309
            &EnvironmentOptions::experimental_import_meta_resolve,
310
4678
            kAllowedInEnvironment);
311
4678
  AddOption("--experimental-policy",
312
            "use the specified file as a "
313
            "security policy",
314
            &EnvironmentOptions::experimental_policy,
315
4678
            kAllowedInEnvironment);
316
4678
  AddOption("[has_policy_integrity_string]",
317
            "",
318
4678
            &EnvironmentOptions::has_policy_integrity_string);
319
4678
  AddOption("--policy-integrity",
320
            "ensure the security policy contents match "
321
            "the specified integrity",
322
            &EnvironmentOptions::experimental_policy_integrity,
323
4678
            kAllowedInEnvironment);
324
4678
  Implies("--policy-integrity", "[has_policy_integrity_string]");
325
4678
  AddOption("--experimental-repl-await",
326
            "experimental await keyword support in REPL",
327
            &EnvironmentOptions::experimental_repl_await,
328
4678
            kAllowedInEnvironment);
329
4678
  AddOption("--experimental-vm-modules",
330
            "experimental ES Module support in vm module",
331
            &EnvironmentOptions::experimental_vm_modules,
332
4678
            kAllowedInEnvironment);
333
4678
  AddOption("--experimental-worker", "", NoOp{}, kAllowedInEnvironment);
334
4678
  AddOption("--experimental-report", "", NoOp{}, kAllowedInEnvironment);
335
4678
  AddOption("--experimental-wasi-unstable-preview1",
336
            "experimental WASI support",
337
            &EnvironmentOptions::experimental_wasi,
338
4678
            kAllowedInEnvironment);
339
4678
  AddOption("--expose-internals", "", &EnvironmentOptions::expose_internals);
340
4678
  AddOption("--frozen-intrinsics",
341
            "experimental frozen intrinsics support",
342
            &EnvironmentOptions::frozen_intrinsics,
343
4678
            kAllowedInEnvironment);
344
4678
  AddOption("--heapsnapshot-signal",
345
            "Generate heap snapshot on specified signal",
346
            &EnvironmentOptions::heap_snapshot_signal,
347
4678
            kAllowedInEnvironment);
348
4678
  AddOption("--heapsnapshot-near-heap-limit",
349
            "Generate heap snapshots whenever V8 is approaching "
350
            "the heap limit. No more than the specified number of "
351
            "heap snapshots will be generated.",
352
            &EnvironmentOptions::heap_snapshot_near_heap_limit,
353
4678
            kAllowedInEnvironment);
354
4678
  AddOption("--http-parser", "", NoOp{}, kAllowedInEnvironment);
355
4678
  AddOption("--insecure-http-parser",
356
            "use an insecure HTTP parser that accepts invalid HTTP headers",
357
            &EnvironmentOptions::insecure_http_parser,
358
4678
            kAllowedInEnvironment);
359
4678
  AddOption("--input-type",
360
            "set module type for string input",
361
            &EnvironmentOptions::module_type,
362
4678
            kAllowedInEnvironment);
363
4678
  AddOption("--experimental-specifier-resolution",
364
            "Select extension resolution algorithm for es modules; "
365
            "either 'explicit' (default) or 'node'",
366
            &EnvironmentOptions::experimental_specifier_resolution,
367
4678
            kAllowedInEnvironment);
368
4678
  AddAlias("--es-module-specifier-resolution",
369
4678
           "--experimental-specifier-resolution");
370
4678
  AddOption("--no-deprecation",
371
            "silence deprecation warnings",
372
            &EnvironmentOptions::no_deprecation,
373
4678
            kAllowedInEnvironment);
374
4678
  AddOption("--no-force-async-hooks-checks",
375
            "disable checks for async_hooks",
376
            &EnvironmentOptions::no_force_async_hooks_checks,
377
4678
            kAllowedInEnvironment);
378
4678
  AddOption("--no-warnings",
379
            "silence all process warnings",
380
            &EnvironmentOptions::no_warnings,
381
4678
            kAllowedInEnvironment);
382
4678
  AddOption("--force-context-aware",
383
            "disable loading non-context-aware addons",
384
            &EnvironmentOptions::force_context_aware,
385
4678
            kAllowedInEnvironment);
386
4678
  AddOption("--pending-deprecation",
387
            "emit pending deprecation warnings",
388
            &EnvironmentOptions::pending_deprecation,
389
4678
            kAllowedInEnvironment);
390
4678
  AddOption("--preserve-symlinks",
391
            "preserve symbolic links when resolving",
392
            &EnvironmentOptions::preserve_symlinks,
393
4678
            kAllowedInEnvironment);
394
4678
  AddOption("--preserve-symlinks-main",
395
            "preserve symbolic links when resolving the main module",
396
            &EnvironmentOptions::preserve_symlinks_main,
397
4678
            kAllowedInEnvironment);
398
4678
  AddOption("--prof",
399
            "Generate V8 profiler output.",
400
4678
            V8Option{});
401
4678
  AddOption("--prof-process",
402
            "process V8 profiler output generated using --prof",
403
4678
            &EnvironmentOptions::prof_process);
404
  // Options after --prof-process are passed through to the prof processor.
405
4678
  AddAlias("--prof-process", { "--prof-process", "--" });
406
#if HAVE_INSPECTOR
407
4678
  AddOption("--cpu-prof",
408
            "Start the V8 CPU profiler on start up, and write the CPU profile "
409
            "to disk before exit. If --cpu-prof-dir is not specified, write "
410
            "the profile to the current working directory.",
411
4678
            &EnvironmentOptions::cpu_prof);
412
4678
  AddOption("--cpu-prof-name",
413
            "specified file name of the V8 CPU profile generated with "
414
            "--cpu-prof",
415
4678
            &EnvironmentOptions::cpu_prof_name);
416
4678
  AddOption("--cpu-prof-interval",
417
            "specified sampling interval in microseconds for the V8 CPU "
418
            "profile generated with --cpu-prof. (default: 1000)",
419
4678
            &EnvironmentOptions::cpu_prof_interval);
420
4678
  AddOption("--cpu-prof-dir",
421
            "Directory where the V8 profiles generated by --cpu-prof will be "
422
            "placed. Does not affect --prof.",
423
4678
            &EnvironmentOptions::cpu_prof_dir);
424
4678
  AddOption(
425
      "--heap-prof",
426
      "Start the V8 heap profiler on start up, and write the heap profile "
427
      "to disk before exit. If --heap-prof-dir is not specified, write "
428
      "the profile to the current working directory.",
429
4678
      &EnvironmentOptions::heap_prof);
430
4678
  AddOption("--heap-prof-name",
431
            "specified file name of the V8 heap profile generated with "
432
            "--heap-prof",
433
4678
            &EnvironmentOptions::heap_prof_name);
434
4678
  AddOption("--heap-prof-dir",
435
            "Directory where the V8 heap profiles generated by --heap-prof "
436
            "will be placed.",
437
4678
            &EnvironmentOptions::heap_prof_dir);
438
4678
  AddOption("--heap-prof-interval",
439
            "specified sampling interval in bytes for the V8 heap "
440
            "profile generated with --heap-prof. (default: 512 * 1024)",
441
4678
            &EnvironmentOptions::heap_prof_interval);
442
#endif  // HAVE_INSPECTOR
443
4678
  AddOption("--max-http-header-size",
444
            "set the maximum size of HTTP headers (default: 16384 (16KB))",
445
            &EnvironmentOptions::max_http_header_size,
446
4678
            kAllowedInEnvironment);
447
4678
  AddOption("--redirect-warnings",
448
            "write warnings to file instead of stderr",
449
            &EnvironmentOptions::redirect_warnings,
450
4678
            kAllowedInEnvironment);
451
4678
  AddOption("--test-udp-no-try-send", "",  // For testing only.
452
4678
            &EnvironmentOptions::test_udp_no_try_send);
453
4678
  AddOption("--throw-deprecation",
454
            "throw an exception on deprecations",
455
            &EnvironmentOptions::throw_deprecation,
456
4678
            kAllowedInEnvironment);
457
4678
  AddOption("--trace-atomics-wait",
458
            "trace Atomics.wait() operations",
459
            &EnvironmentOptions::trace_atomics_wait,
460
4678
            kAllowedInEnvironment);
461
4678
  AddOption("--trace-deprecation",
462
            "show stack traces on deprecations",
463
            &EnvironmentOptions::trace_deprecation,
464
4678
            kAllowedInEnvironment);
465
4678
  AddOption("--trace-exit",
466
            "show stack trace when an environment exits",
467
            &EnvironmentOptions::trace_exit,
468
4678
            kAllowedInEnvironment);
469
4678
  AddOption("--trace-sync-io",
470
            "show stack trace when use of sync IO is detected after the "
471
            "first tick",
472
            &EnvironmentOptions::trace_sync_io,
473
4678
            kAllowedInEnvironment);
474
4678
  AddOption("--trace-tls",
475
            "prints TLS packet trace information to stderr",
476
            &EnvironmentOptions::trace_tls,
477
4678
            kAllowedInEnvironment);
478
4678
  AddOption("--trace-uncaught",
479
            "show stack traces for the `throw` behind uncaught exceptions",
480
            &EnvironmentOptions::trace_uncaught,
481
4678
            kAllowedInEnvironment);
482
4678
  AddOption("--trace-warnings",
483
            "show stack traces on process warnings",
484
            &EnvironmentOptions::trace_warnings,
485
4678
            kAllowedInEnvironment);
486
4678
  AddOption("--unhandled-rejections",
487
            "define unhandled rejections behavior. Options are 'strict' (raise "
488
            "an error), 'warn' (enforce warnings) or 'none' (silence warnings)",
489
            &EnvironmentOptions::unhandled_rejections,
490
4678
            kAllowedInEnvironment);
491
4678
  AddOption("--verify-base-objects",
492
            "", /* undocumented, only for debugging */
493
            &EnvironmentOptions::verify_base_objects,
494
4678
            kAllowedInEnvironment);
495
496
4678
  AddOption("--check",
497
            "syntax check script without executing",
498
4678
            &EnvironmentOptions::syntax_check_only);
499
4678
  AddAlias("-c", "--check");
500
  // This option is only so that we can tell --eval with an empty string from
501
  // no eval at all. Having it not start with a dash makes it inaccessible
502
  // from the parser itself, but available for using Implies().
503
  // TODO(addaleax): When moving --help over to something generated from the
504
  // programmatic descriptions, this will need some special care.
505
  // (See also [ssl_openssl_cert_store] below.)
506
4678
  AddOption("[has_eval_string]", "", &EnvironmentOptions::has_eval_string);
507
4678
  AddOption("--eval", "evaluate script", &EnvironmentOptions::eval_string);
508
4678
  Implies("--eval", "[has_eval_string]");
509
4678
  AddOption("--print",
510
            "evaluate script and print result",
511
4678
            &EnvironmentOptions::print_eval);
512
4678
  AddAlias("-e", "--eval");
513
4678
  AddAlias("--print <arg>", "-pe");
514
4678
  AddAlias("-pe", { "--print", "--eval" });
515
4678
  AddAlias("-p", "--print");
516
4678
  AddOption("--require",
517
            "module to preload (option can be repeated)",
518
            &EnvironmentOptions::preload_modules,
519
4678
            kAllowedInEnvironment);
520
4678
  AddAlias("-r", "--require");
521
4678
  AddOption("--interactive",
522
            "always enter the REPL even if stdin does not appear "
523
            "to be a terminal",
524
4678
            &EnvironmentOptions::force_repl);
525
4678
  AddAlias("-i", "--interactive");
526
527
4678
  AddOption("--napi-modules", "", NoOp{}, kAllowedInEnvironment);
528
529
4678
  AddOption("--tls-keylog",
530
            "log TLS decryption keys to named file for traffic analysis",
531
4678
            &EnvironmentOptions::tls_keylog, kAllowedInEnvironment);
532
533
4678
  AddOption("--tls-min-v1.0",
534
            "set default TLS minimum to TLSv1.0 (default: TLSv1.2)",
535
            &EnvironmentOptions::tls_min_v1_0,
536
4678
            kAllowedInEnvironment);
537
4678
  AddOption("--tls-min-v1.1",
538
            "set default TLS minimum to TLSv1.1 (default: TLSv1.2)",
539
            &EnvironmentOptions::tls_min_v1_1,
540
4678
            kAllowedInEnvironment);
541
4678
  AddOption("--tls-min-v1.2",
542
            "set default TLS minimum to TLSv1.2 (default: TLSv1.2)",
543
            &EnvironmentOptions::tls_min_v1_2,
544
4678
            kAllowedInEnvironment);
545
4678
  AddOption("--tls-min-v1.3",
546
            "set default TLS minimum to TLSv1.3 (default: TLSv1.2)",
547
            &EnvironmentOptions::tls_min_v1_3,
548
4678
            kAllowedInEnvironment);
549
4678
  AddOption("--tls-max-v1.2",
550
            "set default TLS maximum to TLSv1.2 (default: TLSv1.3)",
551
            &EnvironmentOptions::tls_max_v1_2,
552
4678
            kAllowedInEnvironment);
553
  // Current plan is:
554
  // - 11.x and below: TLS1.3 is opt-in with --tls-max-v1.3
555
  // - 12.x: TLS1.3 is opt-out with --tls-max-v1.2
556
  // In either case, support both options they are uniformly available.
557
4678
  AddOption("--tls-max-v1.3",
558
            "set default TLS maximum to TLSv1.3 (default: TLSv1.3)",
559
            &EnvironmentOptions::tls_max_v1_3,
560
4678
            kAllowedInEnvironment);
561
4678
}
562
563
4678
PerIsolateOptionsParser::PerIsolateOptionsParser(
564
4678
  const EnvironmentOptionsParser& eop) {
565
4678
  AddOption("--track-heap-objects",
566
            "track heap object allocations for heap snapshots",
567
            &PerIsolateOptions::track_heap_objects,
568
4678
            kAllowedInEnvironment);
569
4678
  AddOption("--no-node-snapshot",
570
            "",  // It's a debug-only option.
571
            &PerIsolateOptions::no_node_snapshot,
572
4678
            kAllowedInEnvironment);
573
574
  // Explicitly add some V8 flags to mark them as allowed in NODE_OPTIONS.
575
4678
  AddOption("--abort-on-uncaught-exception",
576
            "aborting instead of exiting causes a core file to be generated "
577
            "for analysis",
578
4678
            V8Option{},
579
4678
            kAllowedInEnvironment);
580
4678
  AddOption("--interpreted-frames-native-stack",
581
            "help system profilers to translate JavaScript interpreted frames",
582
4678
            V8Option{}, kAllowedInEnvironment);
583
4678
  AddOption("--max-old-space-size", "", V8Option{}, kAllowedInEnvironment);
584
4678
  AddOption("--perf-basic-prof", "", V8Option{}, kAllowedInEnvironment);
585
4678
  AddOption("--perf-basic-prof-only-functions",
586
            "",
587
4678
            V8Option{},
588
4678
            kAllowedInEnvironment);
589
4678
  AddOption("--perf-prof", "", V8Option{}, kAllowedInEnvironment);
590
4678
  AddOption("--perf-prof-unwinding-info",
591
            "",
592
4678
            V8Option{},
593
4678
            kAllowedInEnvironment);
594
4678
  AddOption("--stack-trace-limit", "", V8Option{}, kAllowedInEnvironment);
595
4678
  AddOption("--disallow-code-generation-from-strings",
596
            "disallow eval and friends",
597
4678
            V8Option{},
598
4678
            kAllowedInEnvironment);
599
4678
  AddOption("--huge-max-old-generation-size",
600
             "increase default maximum heap size on machines with 16GB memory "
601
             "or more",
602
4678
             V8Option{},
603
4678
             kAllowedInEnvironment);
604
4678
  AddOption("--jitless",
605
             "disable runtime allocation of executable memory",
606
4678
             V8Option{},
607
4678
             kAllowedInEnvironment);
608
4678
  AddOption("--report-uncaught-exception",
609
            "generate diagnostic report on uncaught exceptions",
610
            &PerIsolateOptions::report_uncaught_exception,
611
4678
            kAllowedInEnvironment);
612
4678
  AddOption("--report-on-signal",
613
            "generate diagnostic report upon receiving signals",
614
            &PerIsolateOptions::report_on_signal,
615
4678
            kAllowedInEnvironment);
616
4678
  AddOption("--report-signal",
617
            "causes diagnostic report to be produced on provided signal,"
618
            " unsupported in Windows. (default: SIGUSR2)",
619
            &PerIsolateOptions::report_signal,
620
4678
            kAllowedInEnvironment);
621
4678
  Implies("--report-signal", "--report-on-signal");
622
623
4678
  AddOption("--experimental-top-level-await",
624
            "",
625
            &PerIsolateOptions::experimental_top_level_await,
626
4678
            kAllowedInEnvironment);
627
4678
  AddOption("--harmony-top-level-await", "", V8Option{});
628
4678
  Implies("--experimental-top-level-await", "--harmony-top-level-await");
629
4678
  Implies("--harmony-top-level-await", "--experimental-top-level-await");
630
4678
  ImpliesNot("--no-harmony-top-level-await", "--experimental-top-level-await");
631
632
4678
  Insert(eop, &PerIsolateOptions::get_per_env_options);
633
4678
}
634
635
4678
PerProcessOptionsParser::PerProcessOptionsParser(
636
4678
  const PerIsolateOptionsParser& iop) {
637
4678
  AddOption("--title",
638
            "the process title to use on startup",
639
            &PerProcessOptions::title,
640
4678
            kAllowedInEnvironment);
641
4678
  AddOption("--trace-event-categories",
642
            "comma separated list of trace event categories to record",
643
            &PerProcessOptions::trace_event_categories,
644
4678
            kAllowedInEnvironment);
645
4678
  AddOption("--trace-event-file-pattern",
646
            "Template string specifying the filepath for the trace-events "
647
            "data, it supports ${rotation} and ${pid}.",
648
            &PerProcessOptions::trace_event_file_pattern,
649
4678
            kAllowedInEnvironment);
650
18712
  AddAlias("--trace-events-enabled", {
651
14034
    "--trace-event-categories", "v8,node,node.async_hooks" });
652
4678
  AddOption("--v8-pool-size",
653
            "set V8's thread pool size",
654
            &PerProcessOptions::v8_thread_pool_size,
655
4678
            kAllowedInEnvironment);
656
4678
  AddOption("--zero-fill-buffers",
657
            "automatically zero-fill all newly allocated Buffer and "
658
            "SlowBuffer instances",
659
            &PerProcessOptions::zero_fill_all_buffers,
660
4678
            kAllowedInEnvironment);
661
4678
  AddOption("--debug-arraybuffer-allocations",
662
            "", /* undocumented, only for debugging */
663
            &PerProcessOptions::debug_arraybuffer_allocations,
664
4678
            kAllowedInEnvironment);
665
4678
  AddOption("--disable-proto",
666
            "disable Object.prototype.__proto__",
667
            &PerProcessOptions::disable_proto,
668
4678
            kAllowedInEnvironment);
669
670
  // 12.x renamed this inadvertently, so alias it for consistency within the
671
  // release line, while using the original name for consistency with older
672
  // release lines.
673
4678
  AddOption("--security-revert", "", &PerProcessOptions::security_reverts);
674
4678
  AddAlias("--security-reverts", "--security-revert");
675
4678
  AddOption("--completion-bash",
676
            "print source-able bash completion script",
677
4678
            &PerProcessOptions::print_bash_completion);
678
4678
  AddOption("--help",
679
            "print node command line options",
680
4678
            &PerProcessOptions::print_help);
681
4678
  AddAlias("-h", "--help");
682
4678
  AddOption(
683
4678
      "--version", "print Node.js version", &PerProcessOptions::print_version);
684
4678
  AddAlias("-v", "--version");
685
4678
  AddOption("--v8-options",
686
            "print V8 command line options",
687
4678
            &PerProcessOptions::print_v8_help);
688
4678
  AddOption("--report-compact",
689
            "output compact single-line JSON",
690
            &PerProcessOptions::report_compact,
691
4678
            kAllowedInEnvironment);
692
4678
  AddOption("--report-dir",
693
            "define custom report pathname."
694
            " (default: current working directory)",
695
            &PerProcessOptions::report_directory,
696
4678
            kAllowedInEnvironment);
697
4678
  AddAlias("--report-directory", "--report-dir");
698
4678
  AddOption("--report-filename",
699
            "define custom report file name."
700
            " (default: YYYYMMDD.HHMMSS.PID.SEQUENCE#.txt)",
701
            &PerProcessOptions::report_filename,
702
4678
            kAllowedInEnvironment);
703
4678
  AddOption("--report-on-fatalerror",
704
              "generate diagnostic report on fatal (internal) errors",
705
              &PerProcessOptions::report_on_fatalerror,
706
4678
              kAllowedInEnvironment);
707
708
#ifdef NODE_HAVE_I18N_SUPPORT
709
4678
  AddOption("--icu-data-dir",
710
            "set ICU data load path to dir (overrides NODE_ICU_DATA)"
711
#ifndef NODE_HAVE_SMALL_ICU
712
            " (note: linked-in ICU data is present)\n"
713
#endif
714
            ,
715
            &PerProcessOptions::icu_data_dir,
716
4678
            kAllowedInEnvironment);
717
#endif
718
719
#if HAVE_OPENSSL
720
4678
  AddOption("--openssl-config",
721
            "load OpenSSL configuration from the specified file "
722
            "(overrides OPENSSL_CONF)",
723
            &PerProcessOptions::openssl_config,
724
4678
            kAllowedInEnvironment);
725
4678
  AddOption("--tls-cipher-list",
726
            "use an alternative default TLS cipher list",
727
            &PerProcessOptions::tls_cipher_list,
728
4678
            kAllowedInEnvironment);
729
4678
  AddOption("--use-openssl-ca",
730
            "use OpenSSL's default CA store"
731
#if defined(NODE_OPENSSL_CERT_STORE)
732
            " (default)"
733
#endif
734
            ,
735
            &PerProcessOptions::use_openssl_ca,
736
4678
            kAllowedInEnvironment);
737
4678
  AddOption("--use-bundled-ca",
738
            "use bundled CA store"
739
#if !defined(NODE_OPENSSL_CERT_STORE)
740
            " (default)"
741
#endif
742
            ,
743
            &PerProcessOptions::use_bundled_ca,
744
4678
            kAllowedInEnvironment);
745
  // Similar to [has_eval_string] above, except that the separation between
746
  // this and use_openssl_ca only exists for option validation after parsing.
747
  // This is not ideal.
748
4678
  AddOption("[ssl_openssl_cert_store]",
749
            "",
750
4678
            &PerProcessOptions::ssl_openssl_cert_store);
751
4678
  Implies("--use-openssl-ca", "[ssl_openssl_cert_store]");
752
4678
  ImpliesNot("--use-bundled-ca", "[ssl_openssl_cert_store]");
753
#if NODE_FIPS_MODE
754
  AddOption("--enable-fips",
755
            "enable FIPS crypto at startup",
756
            &PerProcessOptions::enable_fips_crypto,
757
            kAllowedInEnvironment);
758
  AddOption("--force-fips",
759
            "force FIPS crypto (cannot be disabled)",
760
            &PerProcessOptions::force_fips_crypto,
761
            kAllowedInEnvironment);
762
#endif
763
#endif
764
4678
  AddOption("--use-largepages",
765
            "Map the Node.js static code to large pages. Options are "
766
            "'off' (the default value, meaning do not map), "
767
            "'on' (map and ignore failure, reporting it to stderr), "
768
            "or 'silent' (map and silently ignore failure)",
769
            &PerProcessOptions::use_largepages,
770
4678
            kAllowedInEnvironment);
771
772
4678
  AddOption("--trace-sigint",
773
            "enable printing JavaScript stacktrace on SIGINT",
774
            &PerProcessOptions::trace_sigint,
775
4678
            kAllowedInEnvironment);
776
777
4678
  Insert(iop, &PerProcessOptions::get_per_isolate_options);
778
779
4678
  AddOption("--node-memory-debug",
780
            "Run with extra debug checks for memory leaks in Node.js itself",
781
4678
            NoOp{}, kAllowedInEnvironment);
782
4678
  Implies("--node-memory-debug", "--debug-arraybuffer-allocations");
783
4678
  Implies("--node-memory-debug", "--verify-base-objects");
784
4678
}
785
786
112
inline std::string RemoveBrackets(const std::string& host) {
787


112
  if (!host.empty() && host.front() == '[' && host.back() == ']')
788
4
    return host.substr(1, host.size() - 2);
789
  else
790
108
    return host;
791
}
792
793
100
inline int ParseAndValidatePort(const std::string& port,
794
                                std::vector<std::string>* errors) {
795
  char* endptr;
796
100
  errno = 0;
797
  const unsigned long result =                 // NOLINT(runtime/int)
798
100
    strtoul(port.c_str(), &endptr, 10);
799

100
  if (errno != 0 || *endptr != '\0'||
800

63
      (result != 0 && result < 1024) || result > 65535) {
801
    errors->push_back(" must be 0 or in range 1024 to 65535.");
802
  }
803
100
  return static_cast<int>(result);
804
}
805
806
100
HostPort SplitHostPort(const std::string& arg,
807
                      std::vector<std::string>* errors) {
808
  // remove_brackets only works if no port is specified
809
  // so if it has an effect only an IPv6 address was specified.
810
200
  std::string host = RemoveBrackets(arg);
811
100
  if (host.length() < arg.length())
812
    return HostPort{host, DebugOptions::kDefaultInspectorPort};
813
814
100
  size_t colon = arg.rfind(':');
815
100
  if (colon == std::string::npos) {
816
    // Either a port number or a host name.  Assume that
817
    // if it's not all decimal digits, it's a host name.
818
390
    for (char c : arg) {
819

302
      if (c < '0' || c > '9') {
820
        return HostPort{arg, DebugOptions::kDefaultInspectorPort};
821
      }
822
    }
823
88
    return HostPort { "", ParseAndValidatePort(arg, errors) };
824
  }
825
  // Host and port found:
826
24
  return HostPort { RemoveBrackets(arg.substr(0, colon)),
827
12
                    ParseAndValidatePort(arg.substr(colon + 1), errors) };
828
}
829
830
1
std::string GetBashCompletion() {
831
2
  Mutex::ScopedLock lock(per_process::cli_options_mutex);
832
1
  const auto& parser = _ppop_instance;
833
834
2
  std::ostringstream out;
835
836
  out << "_node_complete() {\n"
837
         "  local cur_word options\n"
838
         "  cur_word=\"${COMP_WORDS[COMP_CWORD]}\"\n"
839
         "  if [[ \"${cur_word}\" == -* ]] ; then\n"
840
1
         "    COMPREPLY=( $(compgen -W '";
841
842
120
  for (const auto& item : parser.options_) {
843
119
    if (item.first[0] != '[') {
844
116
      out << item.first << " ";
845
    }
846
  }
847
22
  for (const auto& item : parser.aliases_) {
848
21
    if (item.first[0] != '[') {
849
21
      out << item.first << " ";
850
    }
851
  }
852
1
  if (parser.aliases_.size() > 0) {
853
1
    out.seekp(-1, out.cur);  // Strip the trailing space
854
  }
855
856
  out << "' -- \"${cur_word}\") )\n"
857
         "    return 0\n"
858
         "  else\n"
859
         "    COMPREPLY=( $(compgen -f \"${cur_word}\") )\n"
860
         "    return 0\n"
861
         "  fi\n"
862
         "}\n"
863
         "complete -o filenames -o nospace -o bashdefault "
864
1
         "-F _node_complete node node_g";
865
2
  return out.str();
866
}
867
868
// Return a map containing all the options and their metadata as well
869
// as the aliases
870
4998
void GetOptions(const FunctionCallbackInfo<Value>& args) {
871
9996
  Mutex::ScopedLock lock(per_process::cli_options_mutex);
872
4998
  Environment* env = Environment::GetCurrent(args);
873
4998
  if (!env->has_run_bootstrapping_code()) {
874
    // No code because this is an assertion.
875
    return env->ThrowError(
876
        "Should not query options before bootstrapping is done");
877
  }
878
4998
  env->set_has_serialized_options(true);
879
880
4998
  Isolate* isolate = env->isolate();
881
4998
  Local<Context> context = env->context();
882
883
  // Temporarily act as if the current Environment's/IsolateData's options were
884
  // the default options, i.e. like they are the ones we'd access for global
885
  // options parsing, so that all options are available from the main parser.
886
9996
  auto original_per_isolate = per_process::cli_options->per_isolate;
887
4998
  per_process::cli_options->per_isolate = env->isolate_data()->options();
888
9996
  auto original_per_env = per_process::cli_options->per_isolate->per_env;
889
4998
  per_process::cli_options->per_isolate->per_env = env->options();
890
4998
  auto on_scope_leave = OnScopeLeave([&]() {
891
4998
    per_process::cli_options->per_isolate->per_env = original_per_env;
892
4998
    per_process::cli_options->per_isolate = original_per_isolate;
893
14994
  });
894
895
4998
  Local<Map> options = Map::New(isolate);
896
599760
  for (const auto& item : _ppop_instance.options_) {
897
    Local<Value> value;
898
594762
    const auto& option_info = item.second;
899
1189524
    auto field = option_info.field;
900
594762
    PerProcessOptions* opts = per_process::cli_options.get();
901


594762
    switch (option_info.type) {
902
      case kNoOp:
903
      case kV8Option:
904
        // Special case for --abort-on-uncaught-exception which is also
905
        // respected by Node.js internals
906
94962
        if (item.first == "--abort-on-uncaught-exception") {
907
4998
          value = Boolean::New(
908
9996
            isolate, original_per_env->abort_on_uncaught_exception);
909
        } else {
910
89964
          value = Undefined(isolate);
911
        }
912
94962
        break;
913
      case kBoolean:
914
319872
        value = Boolean::New(isolate,
915
639744
                             *_ppop_instance.Lookup<bool>(field, opts));
916
319872
        break;
917
      case kInteger:
918
9996
        value = Number::New(isolate,
919
19992
                            *_ppop_instance.Lookup<int64_t>(field, opts));
920
9996
        break;
921
      case kUInteger:
922
14994
        value = Number::New(isolate,
923
29988
                            *_ppop_instance.Lookup<uint64_t>(field, opts));
924
14994
        break;
925
      case kString:
926
404838
        if (!ToV8Value(context,
927
269892
                       *_ppop_instance.Lookup<std::string>(field, opts))
928
134946
                 .ToLocal(&value)) {
929
          return;
930
        }
931
134946
        break;
932
      case kStringList:
933
44982
        if (!ToV8Value(context,
934
29988
                       *_ppop_instance.Lookup<StringVector>(field, opts))
935
14994
                 .ToLocal(&value)) {
936
          return;
937
        }
938
14994
        break;
939
      case kHostPort: {
940
        const HostPort& host_port =
941
4998
          *_ppop_instance.Lookup<HostPort>(field, opts);
942
4998
        Local<Object> obj = Object::New(isolate);
943
        Local<Value> host;
944

24990
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
945

24990
            obj->Set(context, env->host_string(), host).IsNothing() ||
946
9996
            obj->Set(context,
947
                     env->port_string(),
948
19992
                     Integer::New(isolate, host_port.port()))
949
4998
                .IsNothing()) {
950
          return;
951
        }
952
4998
        value = obj;
953
4998
        break;
954
      }
955
      default:
956
        UNREACHABLE();
957
    }
958
594762
    CHECK(!value.IsEmpty());
959
960
1189524
    Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
961
594762
    Local<Object> info = Object::New(isolate);
962
    Local<Value> help_text;
963

2973810
    if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) ||
964
2379048
        !info->Set(context, env->help_text_string(), help_text)
965
2379048
             .FromMaybe(false) ||
966
1784286
        !info->Set(context,
967
                   env->env_var_settings_string(),
968
                   Integer::New(isolate,
969
2379048
                                static_cast<int>(option_info.env_setting)))
970
2379048
             .FromMaybe(false) ||
971
1784286
        !info->Set(context,
972
                   env->type_string(),
973
2379048
                   Integer::New(isolate, static_cast<int>(option_info.type)))
974
2379048
             .FromMaybe(false) ||
975

3568572
        info->Set(context, env->value_string(), value).IsNothing() ||
976
1784286
        options->Set(context, name, info).IsEmpty()) {
977
      return;
978
    }
979
  }
980
981
  Local<Value> aliases;
982
9996
  if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return;
983
984
4998
  Local<Object> ret = Object::New(isolate);
985

24990
  if (ret->Set(context, env->options_string(), options).IsNothing() ||
986
19992
      ret->Set(context, env->aliases_string(), aliases).IsNothing()) {
987
    return;
988
  }
989
990
9996
  args.GetReturnValue().Set(ret);
991
}
992
993
4998
void Initialize(Local<Object> target,
994
                Local<Value> unused,
995
                Local<Context> context,
996
                void* priv) {
997
4998
  Environment* env = Environment::GetCurrent(context);
998
4998
  Isolate* isolate = env->isolate();
999
4998
  env->SetMethodNoSideEffect(target, "getOptions", GetOptions);
1000
1001
4998
  Local<Object> env_settings = Object::New(isolate);
1002
9996
  NODE_DEFINE_CONSTANT(env_settings, kAllowedInEnvironment);
1003
4998
  NODE_DEFINE_CONSTANT(env_settings, kDisallowedInEnvironment);
1004
19992
  target
1005
24990
      ->Set(
1006
29988
          context, FIXED_ONE_BYTE_STRING(isolate, "envSettings"), env_settings)
1007
9996
      .Check();
1008
1009
  target
1010
9996
      ->Set(context,
1011
            FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"),
1012
19992
            Boolean::New(isolate, env->should_not_register_esm_loader()))
1013
      .Check();
1014
1015
4998
  Local<Object> types = Object::New(isolate);
1016
4998
  NODE_DEFINE_CONSTANT(types, kNoOp);
1017
14994
  NODE_DEFINE_CONSTANT(types, kV8Option);
1018
19992
  NODE_DEFINE_CONSTANT(types, kBoolean);
1019
34986
  NODE_DEFINE_CONSTANT(types, kInteger);
1020
44982
  NODE_DEFINE_CONSTANT(types, kUInteger);
1021
44982
  NODE_DEFINE_CONSTANT(types, kString);
1022
44982
  NODE_DEFINE_CONSTANT(types, kHostPort);
1023
44982
  NODE_DEFINE_CONSTANT(types, kStringList);
1024
54978
  target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "types"), types)
1025
29988
      .Check();
1026
29988
}
1027
9996
1028
}  // namespace options_parser
1029
1030
4670
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options) {
1031
28020
  HandleEnvOptions(env_options, [](const char* name) {
1032
37360
    std::string text;
1033

37360
    return credentials::SafeGetenv(name, &text) ? text : "";
1034
4670
  });
1035
4670
}
1036
1037
4773
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
1038
                      std::function<std::string(const char*)> opt_getter) {
1039
9546
  env_options->pending_deprecation =
1040
9546
      opt_getter("NODE_PENDING_DEPRECATION") == "1";
1041
1042
4773
  env_options->preserve_symlinks = opt_getter("NODE_PRESERVE_SYMLINKS") == "1";
1043
1044
9546
  env_options->preserve_symlinks_main =
1045
9546
      opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1";
1046
1047
4773
  if (env_options->redirect_warnings.empty())
1048
4773
    env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS");
1049
4773
}
1050
1051
4656
std::vector<std::string> ParseNodeOptionsEnvVar(
1052
    const std::string& node_options, std::vector<std::string>* errors) {
1053
4656
  std::vector<std::string> env_argv;
1054
1055
4656
  bool is_in_string = false;
1056
4656
  bool will_start_new_arg = true;
1057
7357
  for (std::string::size_type index = 0; index < node_options.size(); ++index) {
1058
2701
    char c = node_options.at(index);
1059
1060
    // Backslashes escape the following character
1061

2701
    if (c == '\\' && is_in_string) {
1062
      if (index + 1 == node_options.size()) {
1063
        errors->push_back("invalid value for NODE_OPTIONS "
1064
                          "(invalid escape)\n");
1065
        return env_argv;
1066
      } else {
1067
        c = node_options.at(++index);
1068
      }
1069

2701
    } else if (c == ' ' && !is_in_string) {
1070
32
      will_start_new_arg = true;
1071
32
      continue;
1072
2669
    } else if (c == '"') {
1073
4
      is_in_string = !is_in_string;
1074
4
      continue;
1075
    }
1076
1077
2665
    if (will_start_new_arg) {
1078
106
      env_argv.emplace_back(std::string(1, c));
1079
106
      will_start_new_arg = false;
1080
    } else {
1081
2559
      env_argv.back() += c;
1082
    }
1083
  }
1084
1085
4656
  if (is_in_string) {
1086
    errors->push_back("invalid value for NODE_OPTIONS "
1087
                      "(unterminated string)\n");
1088
  }
1089
4656
  return env_argv;
1090
}
1091
}  // namespace node
1092
1093

18704
NODE_MODULE_CONTEXT_AWARE_INTERNAL(options, node::options_parser::Initialize)