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: 554 584 94.9 %
Date: 2021-06-10 04:11:54 Branches: 194 252 77.0 %

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 <limits>
11
#include <algorithm>
12
#include <cstdlib>  // strtoul, errno
13
14
using v8::Boolean;
15
using v8::Context;
16
using v8::FunctionCallbackInfo;
17
using v8::Integer;
18
using v8::Isolate;
19
using v8::Local;
20
using v8::Map;
21
using v8::Number;
22
using v8::Object;
23
using v8::Undefined;
24
using v8::Value;
25
26
namespace node {
27
28
namespace per_process {
29
4846
Mutex cli_options_mutex;
30
4846
std::shared_ptr<PerProcessOptions> cli_options{new PerProcessOptions()};
31
}  // namespace per_process
32
33
9769
void DebugOptions::CheckOptions(std::vector<std::string>* errors) {
34
#if !NODE_USE_V8_PLATFORM && !HAVE_INSPECTOR
35
  if (inspector_enabled) {
36
    errors->push_back("Inspector is not available when Node is compiled "
37
                      "--without-v8-platform and --without-inspector.");
38
  }
39
#endif
40
41
9769
  if (deprecated_debug) {
42
8
    errors->push_back("[DEP0062]: `node --debug` and `node --debug-brk` "
43
                      "are invalid. Please use `node --inspect` and "
44
4
                      "`node --inspect-brk` instead.");
45
  }
46
47
  std::vector<std::string> destinations =
48
19538
      SplitString(inspect_publish_uid_string, ',');
49
9769
  inspect_publish_uid.console = false;
50
9769
  inspect_publish_uid.http = false;
51
29305
  for (const std::string& destination : destinations) {
52
19536
    if (destination == "stderr") {
53
9768
      inspect_publish_uid.console = true;
54
9768
    } else if (destination == "http") {
55
9768
      inspect_publish_uid.http = true;
56
    } else {
57
      errors->push_back("--inspect-publish-uid destination can be "
58
                        "stderr or http");
59
    }
60
  }
61
9769
}
62
63
9541
void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) {
64
#if HAVE_OPENSSL
65

9541
  if (use_openssl_ca && use_bundled_ca) {
66
2
    errors->push_back("either --use-openssl-ca or --use-bundled-ca can be "
67
1
                      "used, not both");
68
  }
69
70
  // Any value less than 2 disables use of the secure heap.
71
9541
  if (secure_heap >= 2) {
72
2
    if ((secure_heap & (secure_heap - 1)) != 0)
73
1
      errors->push_back("--secure-heap must be a power of 2");
74
2
    secure_heap_min =
75
6
        std::min({
76
2
            secure_heap,
77
2
            secure_heap_min,
78
            static_cast<int64_t>(std::numeric_limits<int>::max())});
79
2
    secure_heap_min = std::max(static_cast<int64_t>(2), secure_heap_min);
80
2
    if ((secure_heap_min & (secure_heap_min - 1)) != 0)
81
1
      errors->push_back("--secure-heap-min must be a power of 2");
82
  }
83
#endif
84

19084
  if (use_largepages != "off" &&
85

9542
      use_largepages != "on" &&
86
1
      use_largepages != "silent") {
87
1
    errors->push_back("invalid value for --use-largepages");
88
  }
89
9541
  per_isolate->CheckOptions(errors);
90
9541
}
91
92
9769
void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors) {
93
9769
  per_env->CheckOptions(errors);
94
9769
}
95
96
9769
void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
97

9769
  if (has_policy_integrity_string && experimental_policy.empty()) {
98
    errors->push_back("--policy-integrity requires "
99
                      "--experimental-policy be enabled");
100
  }
101

9769
  if (has_policy_integrity_string && experimental_policy_integrity.empty()) {
102
1
    errors->push_back("--policy-integrity cannot be empty");
103
  }
104
105
9769
  if (!module_type.empty()) {
106

20
    if (module_type != "commonjs" && module_type != "module") {
107
      errors->push_back("--input-type must be \"module\" or \"commonjs\"");
108
    }
109
  }
110
111
9769
  if (!experimental_specifier_resolution.empty()) {
112

10
    if (experimental_specifier_resolution != "node" &&
113
        experimental_specifier_resolution != "explicit") {
114
      errors->push_back(
115
        "invalid value for --experimental-specifier-resolution");
116
    }
117
  }
118
119

9769
  if (syntax_check_only && has_eval_string) {
120
4
    errors->push_back("either --check or --eval can be used, not both");
121
  }
122
123

19558
  if (!unhandled_rejections.empty() &&
124
39
      unhandled_rejections != "warn-with-error-code" &&
125
36
      unhandled_rejections != "throw" &&
126
32
      unhandled_rejections != "strict" &&
127

9794
      unhandled_rejections != "warn" &&
128
10
      unhandled_rejections != "none") {
129
1
    errors->push_back("invalid value for --unhandled-rejections");
130
  }
131
132

9769
  if (tls_min_v1_3 && tls_max_v1_2) {
133
2
    errors->push_back("either --tls-min-v1.3 or --tls-max-v1.2 can be "
134
1
                      "used, not both");
135
  }
136
137
9769
  if (heap_snapshot_near_heap_limit < 0) {
138
    errors->push_back("--heap-snapshot-near-heap-limit must not be negative");
139
  }
140
141
#if HAVE_INSPECTOR
142
9769
  if (!cpu_prof) {
143
9758
    if (!cpu_prof_name.empty()) {
144
1
      errors->push_back("--cpu-prof-name must be used with --cpu-prof");
145
    }
146
9758
    if (!cpu_prof_dir.empty()) {
147
1
      errors->push_back("--cpu-prof-dir must be used with --cpu-prof");
148
    }
149
    // We can't catch the case where the value passed is the default value,
150
    // then the option just becomes a noop which is fine.
151
9758
    if (cpu_prof_interval != kDefaultCpuProfInterval) {
152
1
      errors->push_back("--cpu-prof-interval must be used with --cpu-prof");
153
    }
154
  }
155
156


9769
  if (cpu_prof && cpu_prof_dir.empty() && !diagnostic_dir.empty()) {
157
1
      cpu_prof_dir = diagnostic_dir;
158
    }
159
160
9769
  if (!heap_prof) {
161
9758
    if (!heap_prof_name.empty()) {
162
1
      errors->push_back("--heap-prof-name must be used with --heap-prof");
163
    }
164
9758
    if (!heap_prof_dir.empty()) {
165
1
      errors->push_back("--heap-prof-dir must be used with --heap-prof");
166
    }
167
    // We can't catch the case where the value passed is the default value,
168
    // then the option just becomes a noop which is fine.
169
9758
    if (heap_prof_interval != kDefaultHeapProfInterval) {
170
1
      errors->push_back("--heap-prof-interval must be used with --heap-prof");
171
    }
172
  }
173
174


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


112
  if (!host.empty() && host.front() == '[' && host.back() == ']')
822
4
    return host.substr(1, host.size() - 2);
823
  else
824
108
    return host;
825
}
826
827
100
inline int ParseAndValidatePort(const std::string& port,
828
                                std::vector<std::string>* errors) {
829
  char* endptr;
830
100
  errno = 0;
831
  const unsigned long result =                 // NOLINT(runtime/int)
832
100
    strtoul(port.c_str(), &endptr, 10);
833

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

63
      (result != 0 && result < 1024) || result > 65535) {
835
    errors->push_back(" must be 0 or in range 1024 to 65535.");
836
  }
837
100
  return static_cast<int>(result);
838
}
839
840
100
HostPort SplitHostPort(const std::string& arg,
841
                      std::vector<std::string>* errors) {
842
  // remove_brackets only works if no port is specified
843
  // so if it has an effect only an IPv6 address was specified.
844
200
  std::string host = RemoveBrackets(arg);
845
100
  if (host.length() < arg.length())
846
    return HostPort{host, DebugOptions::kDefaultInspectorPort};
847
848
100
  size_t colon = arg.rfind(':');
849
100
  if (colon == std::string::npos) {
850
    // Either a port number or a host name.  Assume that
851
    // if it's not all decimal digits, it's a host name.
852
390
    for (char c : arg) {
853

302
      if (c < '0' || c > '9') {
854
        return HostPort{arg, DebugOptions::kDefaultInspectorPort};
855
      }
856
    }
857
88
    return HostPort { "", ParseAndValidatePort(arg, errors) };
858
  }
859
  // Host and port found:
860
24
  return HostPort { RemoveBrackets(arg.substr(0, colon)),
861
12
                    ParseAndValidatePort(arg.substr(colon + 1), errors) };
862
}
863
864
1
std::string GetBashCompletion() {
865
2
  Mutex::ScopedLock lock(per_process::cli_options_mutex);
866
1
  const auto& parser = _ppop_instance;
867
868
2
  std::ostringstream out;
869
870
  out << "_node_complete() {\n"
871
         "  local cur_word options\n"
872
         "  cur_word=\"${COMP_WORDS[COMP_CWORD]}\"\n"
873
         "  if [[ \"${cur_word}\" == -* ]] ; then\n"
874
1
         "    COMPREPLY=( $(compgen -W '";
875
876
125
  for (const auto& item : parser.options_) {
877
124
    if (item.first[0] != '[') {
878
121
      out << item.first << " ";
879
    }
880
  }
881
23
  for (const auto& item : parser.aliases_) {
882
22
    if (item.first[0] != '[') {
883
22
      out << item.first << " ";
884
    }
885
  }
886
1
  if (parser.aliases_.size() > 0) {
887
1
    out.seekp(-1, out.cur);  // Strip the trailing space
888
  }
889
890
  out << "' -- \"${cur_word}\") )\n"
891
         "    return 0\n"
892
         "  else\n"
893
         "    COMPREPLY=( $(compgen -f \"${cur_word}\") )\n"
894
         "    return 0\n"
895
         "  fi\n"
896
         "}\n"
897
         "complete -o filenames -o nospace -o bashdefault "
898
1
         "-F _node_complete node node_g";
899
2
  return out.str();
900
}
901
902
// Return a map containing all the options and their metadata as well
903
// as the aliases
904
5181
void GetOptions(const FunctionCallbackInfo<Value>& args) {
905
10362
  Mutex::ScopedLock lock(per_process::cli_options_mutex);
906
5181
  Environment* env = Environment::GetCurrent(args);
907
5181
  if (!env->has_run_bootstrapping_code()) {
908
    // No code because this is an assertion.
909
    return env->ThrowError(
910
        "Should not query options before bootstrapping is done");
911
  }
912
5181
  env->set_has_serialized_options(true);
913
914
5181
  Isolate* isolate = env->isolate();
915
5181
  Local<Context> context = env->context();
916
917
  // Temporarily act as if the current Environment's/IsolateData's options were
918
  // the default options, i.e. like they are the ones we'd access for global
919
  // options parsing, so that all options are available from the main parser.
920
10362
  auto original_per_isolate = per_process::cli_options->per_isolate;
921
5181
  per_process::cli_options->per_isolate = env->isolate_data()->options();
922
10362
  auto original_per_env = per_process::cli_options->per_isolate->per_env;
923
5181
  per_process::cli_options->per_isolate->per_env = env->options();
924
5181
  auto on_scope_leave = OnScopeLeave([&]() {
925
5181
    per_process::cli_options->per_isolate->per_env = original_per_env;
926
5181
    per_process::cli_options->per_isolate = original_per_isolate;
927
15543
  });
928
929
5181
  Local<Map> options = Map::New(isolate);
930
10362
  if (options
931
15543
          ->SetPrototype(context, env->primordials_safe_map_prototype_object())
932
          .IsNothing()) {
933
    return;
934
  }
935
936
647625
  for (const auto& item : _ppop_instance.options_) {
937
    Local<Value> value;
938
642444
    const auto& option_info = item.second;
939
1284888
    auto field = option_info.field;
940
642444
    PerProcessOptions* opts = per_process::cli_options.get();
941


642444
    switch (option_info.type) {
942
      case kNoOp:
943
      case kV8Option:
944
        // Special case for --abort-on-uncaught-exception which is also
945
        // respected by Node.js internals
946
98439
        if (item.first == "--abort-on-uncaught-exception") {
947
5181
          value = Boolean::New(
948
10362
            isolate, original_per_env->abort_on_uncaught_exception);
949
        } else {
950
93258
          value = Undefined(isolate);
951
        }
952
98439
        break;
953
      case kBoolean:
954
341946
        value = Boolean::New(isolate,
955
683892
                             *_ppop_instance.Lookup<bool>(field, opts));
956
341946
        break;
957
      case kInteger:
958
20724
        value = Number::New(
959
            isolate,
960
41448
            static_cast<double>(*_ppop_instance.Lookup<int64_t>(field, opts)));
961
20724
        break;
962
      case kUInteger:
963
15543
        value = Number::New(
964
            isolate,
965
31086
            static_cast<double>(*_ppop_instance.Lookup<uint64_t>(field, opts)));
966
15543
        break;
967
      case kString:
968
435204
        if (!ToV8Value(context,
969
290136
                       *_ppop_instance.Lookup<std::string>(field, opts))
970
145068
                 .ToLocal(&value)) {
971
          return;
972
        }
973
145068
        break;
974
      case kStringList:
975
46629
        if (!ToV8Value(context,
976
31086
                       *_ppop_instance.Lookup<StringVector>(field, opts))
977
15543
                 .ToLocal(&value)) {
978
          return;
979
        }
980
15543
        break;
981
      case kHostPort: {
982
        const HostPort& host_port =
983
5181
          *_ppop_instance.Lookup<HostPort>(field, opts);
984
5181
        Local<Object> obj = Object::New(isolate);
985
        Local<Value> host;
986

25905
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
987

25905
            obj->Set(context, env->host_string(), host).IsNothing() ||
988
10362
            obj->Set(context,
989
                     env->port_string(),
990
20724
                     Integer::New(isolate, host_port.port()))
991
5181
                .IsNothing()) {
992
          return;
993
        }
994
5181
        value = obj;
995
5181
        break;
996
      }
997
      default:
998
        UNREACHABLE();
999
    }
1000
642444
    CHECK(!value.IsEmpty());
1001
1002
1284888
    Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
1003
642444
    Local<Object> info = Object::New(isolate);
1004
    Local<Value> help_text;
1005

3212220
    if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) ||
1006
2569776
        !info->Set(context, env->help_text_string(), help_text)
1007
2569776
             .FromMaybe(false) ||
1008
1927332
        !info->Set(context,
1009
                   env->env_var_settings_string(),
1010
                   Integer::New(isolate,
1011
2569776
                                static_cast<int>(option_info.env_setting)))
1012
2569776
             .FromMaybe(false) ||
1013
1927332
        !info->Set(context,
1014
                   env->type_string(),
1015
2569776
                   Integer::New(isolate, static_cast<int>(option_info.type)))
1016
2569776
             .FromMaybe(false) ||
1017

3854664
        info->Set(context, env->value_string(), value).IsNothing() ||
1018
1927332
        options->Set(context, name, info).IsEmpty()) {
1019
      return;
1020
    }
1021
  }
1022
1023
  Local<Value> aliases;
1024
10362
  if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return;
1025
1026
20724
  if (aliases.As<Object>()
1027
20724
          ->SetPrototype(context, env->primordials_safe_map_prototype_object())
1028
          .IsNothing()) {
1029
    return;
1030
  }
1031
1032
5181
  Local<Object> ret = Object::New(isolate);
1033

25905
  if (ret->Set(context, env->options_string(), options).IsNothing() ||
1034
20724
      ret->Set(context, env->aliases_string(), aliases).IsNothing()) {
1035
    return;
1036
  }
1037
1038
10362
  args.GetReturnValue().Set(ret);
1039
}
1040
1041
5181
void Initialize(Local<Object> target,
1042
                Local<Value> unused,
1043
                Local<Context> context,
1044
                void* priv) {
1045
5181
  Environment* env = Environment::GetCurrent(context);
1046
5181
  Isolate* isolate = env->isolate();
1047
5181
  env->SetMethodNoSideEffect(target, "getOptions", GetOptions);
1048
1049
5181
  Local<Object> env_settings = Object::New(isolate);
1050
10362
  NODE_DEFINE_CONSTANT(env_settings, kAllowedInEnvironment);
1051
5181
  NODE_DEFINE_CONSTANT(env_settings, kDisallowedInEnvironment);
1052
20724
  target
1053
25905
      ->Set(
1054
31086
          context, FIXED_ONE_BYTE_STRING(isolate, "envSettings"), env_settings)
1055
10362
      .Check();
1056
1057
  target
1058
10362
      ->Set(context,
1059
            FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"),
1060
20724
            Boolean::New(isolate, env->should_not_register_esm_loader()))
1061
      .Check();
1062
1063
5181
  Local<Object> types = Object::New(isolate);
1064
5181
  NODE_DEFINE_CONSTANT(types, kNoOp);
1065
15543
  NODE_DEFINE_CONSTANT(types, kV8Option);
1066
20724
  NODE_DEFINE_CONSTANT(types, kBoolean);
1067
36267
  NODE_DEFINE_CONSTANT(types, kInteger);
1068
46629
  NODE_DEFINE_CONSTANT(types, kUInteger);
1069
46629
  NODE_DEFINE_CONSTANT(types, kString);
1070
46629
  NODE_DEFINE_CONSTANT(types, kHostPort);
1071
46629
  NODE_DEFINE_CONSTANT(types, kStringList);
1072
56991
  target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "types"), types)
1073
31086
      .Check();
1074
31086
}
1075
10362
1076
}  // namespace options_parser
1077
1078
4838
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options) {
1079
29028
  HandleEnvOptions(env_options, [](const char* name) {
1080
38704
    std::string text;
1081

38704
    return credentials::SafeGetenv(name, &text) ? text : "";
1082
4838
  });
1083
4838
}
1084
1085
4954
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
1086
                      std::function<std::string(const char*)> opt_getter) {
1087
9908
  env_options->pending_deprecation =
1088
9908
      opt_getter("NODE_PENDING_DEPRECATION") == "1";
1089
1090
4954
  env_options->preserve_symlinks = opt_getter("NODE_PRESERVE_SYMLINKS") == "1";
1091
1092
9908
  env_options->preserve_symlinks_main =
1093
9908
      opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1";
1094
1095
4954
  if (env_options->redirect_warnings.empty())
1096
4954
    env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS");
1097
4954
}
1098
1099
4835
std::vector<std::string> ParseNodeOptionsEnvVar(
1100
    const std::string& node_options, std::vector<std::string>* errors) {
1101
4835
  std::vector<std::string> env_argv;
1102
1103
4835
  bool is_in_string = false;
1104
4835
  bool will_start_new_arg = true;
1105
7557
  for (std::string::size_type index = 0; index < node_options.size(); ++index) {
1106
2722
    char c = node_options.at(index);
1107
1108
    // Backslashes escape the following character
1109

2722
    if (c == '\\' && is_in_string) {
1110
      if (index + 1 == node_options.size()) {
1111
        errors->push_back("invalid value for NODE_OPTIONS "
1112
                          "(invalid escape)\n");
1113
        return env_argv;
1114
      } else {
1115
        c = node_options.at(++index);
1116
      }
1117

2722
    } else if (c == ' ' && !is_in_string) {
1118
32
      will_start_new_arg = true;
1119
32
      continue;
1120
2690
    } else if (c == '"') {
1121
4
      is_in_string = !is_in_string;
1122
4
      continue;
1123
    }
1124
1125
2686
    if (will_start_new_arg) {
1126
107
      env_argv.emplace_back(std::string(1, c));
1127
107
      will_start_new_arg = false;
1128
    } else {
1129
2579
      env_argv.back() += c;
1130
    }
1131
  }
1132
1133
4835
  if (is_in_string) {
1134
    errors->push_back("invalid value for NODE_OPTIONS "
1135
                      "(unterminated string)\n");
1136
  }
1137
4835
  return env_argv;
1138
}
1139
}  // namespace node
1140
1141

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