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: 296 320 92.5 %
Date: 2019-05-05 22:32:45 Branches: 174 262 66.4 %

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
7
#include <cstdlib>  // strtoul, errno
8
9
using v8::Boolean;
10
using v8::Context;
11
using v8::FunctionCallbackInfo;
12
using v8::Integer;
13
using v8::Isolate;
14
using v8::Local;
15
using v8::Map;
16
using v8::Number;
17
using v8::Object;
18
using v8::String;
19
using v8::Undefined;
20
using v8::Value;
21
22
namespace node {
23
24
namespace per_process {
25
4525
Mutex cli_options_mutex;
26
4525
std::shared_ptr<PerProcessOptions> cli_options{new PerProcessOptions()};
27
}  // namespace per_process
28
29
8897
void DebugOptions::CheckOptions(std::vector<std::string>* errors) {
30
#if !NODE_USE_V8_PLATFORM
31
  if (inspector_enabled) {
32
    errors->push_back("Inspector is not available when Node is compiled "
33
                      "--without-v8-platform");
34
  }
35
#endif
36
37

8897
  if (deprecated_debug && !inspector_enabled) {
38
    errors->push_back("[DEP0062]: `node --debug` and `node --debug-brk` "
39
                      "are invalid. Please use `node --inspect` or "
40
2
                      "`node --inspect-brk` instead.");
41
  }
42
43

8897
  if (deprecated_debug && inspector_enabled && break_first_line) {
44
    errors->push_back("[DEP0062]: `node --inspect --debug-brk` is deprecated. "
45
3
                      "Please use `node --inspect-brk` instead.");
46
  }
47
8897
}
48
49
8886
void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) {
50
#if HAVE_OPENSSL
51

8886
  if (use_openssl_ca && use_bundled_ca) {
52
    errors->push_back("either --use-openssl-ca or --use-bundled-ca can be "
53
1
                      "used, not both");
54
  }
55
#endif
56
8886
  per_isolate->CheckOptions(errors);
57
8886
}
58
59
8897
void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors) {
60
8897
  per_env->CheckOptions(errors);
61
#ifdef NODE_REPORT
62
8897
  if (per_env->experimental_report) {
63
    // Assign the report_signal default value here. Once the
64
    // --experimental-report flag is dropped, move this initialization to
65
    // node_options.h, where report_signal is declared.
66
10
    if (report_signal.empty())
67
10
      report_signal = "SIGUSR2";
68
8907
    return;
69
  }
70
71
8887
  if (!report_directory.empty()) {
72
    errors->push_back("--report-directory option is valid only when "
73
                      "--experimental-report is set");
74
  }
75
76
8887
  if (!report_filename.empty()) {
77
    errors->push_back("--report-filename option is valid only when "
78
                      "--experimental-report is set");
79
  }
80
81
8887
  if (!report_signal.empty()) {
82
    errors->push_back("--report-signal option is valid only when "
83
                      "--experimental-report is set");
84
  }
85
86
8887
  if (report_on_fatalerror) {
87
    errors->push_back(
88
        "--report-on-fatalerror option is valid only when "
89
        "--experimental-report is set");
90
  }
91
92
8887
  if (report_on_signal) {
93
    errors->push_back("--report-on-signal option is valid only when "
94
                      "--experimental-report is set");
95
  }
96
97
8887
  if (report_uncaught_exception) {
98
    errors->push_back(
99
        "--report-uncaught-exception option is valid only when "
100
        "--experimental-report is set");
101
  }
102
#endif  // NODE_REPORT
103
}
104
105
8897
void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
106

8897
  if (!userland_loader.empty() && !experimental_modules) {
107
    errors->push_back("--loader requires --experimental-modules be enabled");
108
  }
109
110
8897
  if (!module_type.empty()) {
111
7
    if (!experimental_modules) {
112
      errors->push_back("--input-type requires "
113
                        "--experimental-modules to be enabled");
114
    }
115

7
    if (module_type != "commonjs" && module_type != "module") {
116
      errors->push_back("--input-type must be \"module\" or \"commonjs\"");
117
    }
118
  }
119
120

8897
  if (experimental_json_modules && !experimental_modules) {
121
    errors->push_back("--experimental-json-modules requires "
122
                      "--experimental-modules be enabled");
123
  }
124
125
8897
  if (!es_module_specifier_resolution.empty()) {
126
1
    if (!experimental_modules) {
127
      errors->push_back("--es-module-specifier-resolution requires "
128
                        "--experimental-modules be enabled");
129
    }
130

1
    if (es_module_specifier_resolution != "node" &&
131
        es_module_specifier_resolution != "explicit") {
132
      errors->push_back("invalid value for --es-module-specifier-resolution");
133
    }
134
  }
135
136

8897
  if (syntax_check_only && has_eval_string) {
137
4
    errors->push_back("either --check or --eval can be used, not both");
138
  }
139
140

8897
  if (http_parser != "legacy" && http_parser != "llhttp") {
141
    errors->push_back("invalid value for --http-parser");
142
  }
143
144

17803
  if (!unhandled_rejections.empty() &&
145
16
      unhandled_rejections != "strict" &&
146

8909
      unhandled_rejections != "warn" &&
147
5
      unhandled_rejections != "none") {
148
1
    errors->push_back("invalid value for --unhandled-rejections");
149
  }
150
151

8897
  if (tls_min_v1_3 && tls_max_v1_2) {
152
    errors->push_back("either --tls-min-v1.3 or --tls-max-v1.2 can be "
153
1
                      "used, not both");
154
  }
155
156
#if HAVE_INSPECTOR
157
8897
  if (!cpu_prof) {
158
8888
    if (!cpu_prof_name.empty()) {
159
1
      errors->push_back("--cpu-prof-name must be used with --cpu-prof");
160
    }
161
8888
    if (!cpu_prof_dir.empty()) {
162
1
      errors->push_back("--cpu-prof-dir must be used with --cpu-prof");
163
    }
164
    // We can't catch the case where the value passed is the default value,
165
    // then the option just becomes a noop which is fine.
166
8888
    if (cpu_prof_interval != kDefaultCpuProfInterval) {
167
1
      errors->push_back("--cpu-prof-interval must be used with --cpu-prof");
168
    }
169
  }
170
171
8897
  debug_options_.CheckOptions(errors);
172
#endif  // HAVE_INSPECTOR
173
8897
}
174
175
namespace options_parser {
176
177
4525
class DebugOptionsParser : public OptionsParser<DebugOptions> {
178
 public:
179
  DebugOptionsParser();
180
};
181
182
4525
class EnvironmentOptionsParser : public OptionsParser<EnvironmentOptions> {
183
 public:
184
  EnvironmentOptionsParser();
185
4525
  explicit EnvironmentOptionsParser(const DebugOptionsParser& dop)
186
4525
    : EnvironmentOptionsParser() {
187
4525
    Insert(dop, &EnvironmentOptions::get_debug_options);
188
4525
  }
189
};
190
191
4525
class PerIsolateOptionsParser : public OptionsParser<PerIsolateOptions> {
192
 public:
193
  PerIsolateOptionsParser() = delete;
194
  explicit PerIsolateOptionsParser(const EnvironmentOptionsParser& eop);
195
};
196
197
4525
class PerProcessOptionsParser : public OptionsParser<PerProcessOptions> {
198
 public:
199
  PerProcessOptionsParser() = delete;
200
  explicit PerProcessOptionsParser(const PerIsolateOptionsParser& iop);
201
};
202
203
#if HAVE_INSPECTOR
204
4525
const DebugOptionsParser _dop_instance{};
205
4525
const EnvironmentOptionsParser _eop_instance{_dop_instance};
206
#else
207
const EnvironmentOptionsParser _eop_instance{};
208
#endif  // HAVE_INSPECTOR
209
4525
const PerIsolateOptionsParser _piop_instance{_eop_instance};
210
4525
const PerProcessOptionsParser _ppop_instance{_piop_instance};
211
212
template <>
213
11
void Parse(
214
  StringVector* const args, StringVector* const exec_args,
215
  StringVector* const v8_args,
216
  PerIsolateOptions* const options,
217
  OptionEnvvarSettings required_env_settings, StringVector* const errors) {
218
  _piop_instance.Parse(
219
11
    args, exec_args, v8_args, options, required_env_settings, errors);
220
11
}
221
222
template <>
223
8886
void Parse(
224
  StringVector* const args, StringVector* const exec_args,
225
  StringVector* const v8_args,
226
  PerProcessOptions* const options,
227
  OptionEnvvarSettings required_env_settings, StringVector* const errors) {
228
  _ppop_instance.Parse(
229
8886
    args, exec_args, v8_args, options, required_env_settings, errors);
230
8886
}
231
232
// XXX: If you add an option here, please also add it to doc/node.1 and
233
// doc/api/cli.md
234
// TODO(addaleax): Make that unnecessary.
235
236
4525
DebugOptionsParser::DebugOptionsParser() {
237
  AddOption("--inspect-port",
238
            "set host:port for inspector",
239
            &DebugOptions::host_port,
240
4525
            kAllowedInEnvironment);
241
4525
  AddAlias("--debug-port", "--inspect-port");
242
243
  AddOption("--inspect",
244
            "activate inspector on host:port (default: 127.0.0.1:9229)",
245
            &DebugOptions::inspector_enabled,
246
4525
            kAllowedInEnvironment);
247
4525
  AddAlias("--inspect=", { "--inspect-port", "--inspect" });
248
249
4525
  AddOption("--debug", "", &DebugOptions::deprecated_debug);
250
4525
  AddAlias("--debug=", { "--inspect-port", "--debug" });
251
252
  AddOption("--inspect-brk",
253
            "activate inspector on host:port and break at start of user script",
254
            &DebugOptions::break_first_line,
255
4525
            kAllowedInEnvironment);
256
4525
  Implies("--inspect-brk", "--inspect");
257
4525
  AddAlias("--inspect-brk=", { "--inspect-port", "--inspect-brk" });
258
259
4525
  AddOption("--inspect-brk-node", "", &DebugOptions::break_node_first_line);
260
4525
  Implies("--inspect-brk-node", "--inspect");
261
4525
  AddAlias("--inspect-brk-node=", { "--inspect-port", "--inspect-brk-node" });
262
263
4525
  AddOption("--debug-brk", "", &DebugOptions::break_first_line);
264
4525
  Implies("--debug-brk", "--debug");
265
4525
  AddAlias("--debug-brk=", { "--inspect-port", "--debug-brk" });
266
4525
}
267
268
4525
EnvironmentOptionsParser::EnvironmentOptionsParser() {
269
  AddOption("--experimental-json-modules",
270
            "experimental JSON interop support for the ES Module loader",
271
            &EnvironmentOptions::experimental_json_modules,
272
4525
            kAllowedInEnvironment);
273
  AddOption("--experimental-modules",
274
            "experimental ES Module support and caching modules",
275
            &EnvironmentOptions::experimental_modules,
276
4525
            kAllowedInEnvironment);
277
  AddOption("--experimental-policy",
278
            "use the specified file as a "
279
            "security policy",
280
            &EnvironmentOptions::experimental_policy,
281
4525
            kAllowedInEnvironment);
282
  AddOption("--experimental-repl-await",
283
            "experimental await keyword support in REPL",
284
            &EnvironmentOptions::experimental_repl_await,
285
4525
            kAllowedInEnvironment);
286
  AddOption("--experimental-vm-modules",
287
            "experimental ES Module support in vm module",
288
            &EnvironmentOptions::experimental_vm_modules,
289
4525
            kAllowedInEnvironment);
290
4525
  AddOption("--experimental-worker", "", NoOp{}, kAllowedInEnvironment);
291
#ifdef NODE_REPORT
292
  AddOption("--experimental-report",
293
            "enable report generation",
294
            &EnvironmentOptions::experimental_report,
295
4525
            kAllowedInEnvironment);
296
#endif  // NODE_REPORT
297
4525
  AddOption("--expose-internals", "", &EnvironmentOptions::expose_internals);
298
  AddOption("--frozen-intrinsics",
299
            "experimental frozen intrinsics support",
300
            &EnvironmentOptions::frozen_intrinsics,
301
4525
            kAllowedInEnvironment);
302
  AddOption("--heapsnapshot-signal",
303
            "Generate heap snapshot on specified signal",
304
            &EnvironmentOptions::heap_snapshot_signal,
305
4525
            kAllowedInEnvironment);
306
  AddOption("--http-parser",
307
            "Select which HTTP parser to use; either 'legacy' or 'llhttp' "
308
            "(default: llhttp).",
309
            &EnvironmentOptions::http_parser,
310
4525
            kAllowedInEnvironment);
311
  AddOption("--input-type",
312
            "set module type for string input",
313
            &EnvironmentOptions::module_type,
314
4525
            kAllowedInEnvironment);
315
  AddOption("--loader",
316
            "(with --experimental-modules) use the specified file as a "
317
            "custom loader",
318
            &EnvironmentOptions::userland_loader,
319
4525
            kAllowedInEnvironment);
320
  AddOption("--es-module-specifier-resolution",
321
            "Select extension resolution algorithm for es modules; "
322
            "either 'explicit' (default) or 'node'",
323
            &EnvironmentOptions::es_module_specifier_resolution,
324
4525
            kAllowedInEnvironment);
325
  AddOption("--no-deprecation",
326
            "silence deprecation warnings",
327
            &EnvironmentOptions::no_deprecation,
328
4525
            kAllowedInEnvironment);
329
  AddOption("--no-force-async-hooks-checks",
330
            "disable checks for async_hooks",
331
            &EnvironmentOptions::no_force_async_hooks_checks,
332
4525
            kAllowedInEnvironment);
333
  AddOption("--no-warnings",
334
            "silence all process warnings",
335
            &EnvironmentOptions::no_warnings,
336
4525
            kAllowedInEnvironment);
337
  AddOption("--pending-deprecation",
338
            "emit pending deprecation warnings",
339
            &EnvironmentOptions::pending_deprecation,
340
4525
            kAllowedInEnvironment);
341
  AddOption("--preserve-symlinks",
342
            "preserve symbolic links when resolving",
343
            &EnvironmentOptions::preserve_symlinks,
344
4525
            kAllowedInEnvironment);
345
  AddOption("--preserve-symlinks-main",
346
            "preserve symbolic links when resolving the main module",
347
            &EnvironmentOptions::preserve_symlinks_main,
348
4525
            kAllowedInEnvironment);
349
  AddOption("--prof-process",
350
            "process V8 profiler output generated using --prof",
351
4525
            &EnvironmentOptions::prof_process);
352
  // Options after --prof-process are passed through to the prof processor.
353
4525
  AddAlias("--prof-process", { "--prof-process", "--" });
354
#if HAVE_INSPECTOR
355
  AddOption("--cpu-prof",
356
            "Start the V8 CPU profiler on start up, and write the CPU profile "
357
            "to disk before exit. If --cpu-prof-dir is not specified, write "
358
            "the profile to the current working directory.",
359
4525
            &EnvironmentOptions::cpu_prof);
360
  AddOption("--cpu-prof-name",
361
            "specified file name of the V8 CPU profile generated with "
362
            "--cpu-prof",
363
4525
            &EnvironmentOptions::cpu_prof_name);
364
  AddOption("--cpu-prof-interval",
365
            "specified sampling interval in microseconds for the V8 CPU "
366
            "profile generated with --cpu-prof. (default: 1000)",
367
4525
            &EnvironmentOptions::cpu_prof_interval);
368
  AddOption("--cpu-prof-dir",
369
            "Directory where the V8 profiles generated by --cpu-prof will be "
370
            "placed. Does not affect --prof.",
371
4525
            &EnvironmentOptions::cpu_prof_dir);
372
#endif  // HAVE_INSPECTOR
373
  AddOption("--redirect-warnings",
374
            "write warnings to file instead of stderr",
375
            &EnvironmentOptions::redirect_warnings,
376
4525
            kAllowedInEnvironment);
377
  AddOption("--throw-deprecation",
378
            "throw an exception on deprecations",
379
            &EnvironmentOptions::throw_deprecation,
380
4525
            kAllowedInEnvironment);
381
  AddOption("--trace-deprecation",
382
            "show stack traces on deprecations",
383
            &EnvironmentOptions::trace_deprecation,
384
4525
            kAllowedInEnvironment);
385
  AddOption("--trace-sync-io",
386
            "show stack trace when use of sync IO is detected after the "
387
            "first tick",
388
            &EnvironmentOptions::trace_sync_io,
389
4525
            kAllowedInEnvironment);
390
  AddOption("--trace-tls",
391
            "prints TLS packet trace information to stderr",
392
            &EnvironmentOptions::trace_tls,
393
4525
            kAllowedInEnvironment);
394
  AddOption("--trace-warnings",
395
            "show stack traces on process warnings",
396
            &EnvironmentOptions::trace_warnings,
397
4525
            kAllowedInEnvironment);
398
  AddOption("--unhandled-rejections",
399
            "define unhandled rejections behavior. Options are 'strict' (raise "
400
            "an error), 'warn' (enforce warnings) or 'none' (silence warnings)",
401
            &EnvironmentOptions::unhandled_rejections,
402
4525
            kAllowedInEnvironment);
403
404
  AddOption("--check",
405
            "syntax check script without executing",
406
4525
            &EnvironmentOptions::syntax_check_only);
407
4525
  AddAlias("-c", "--check");
408
  // This option is only so that we can tell --eval with an empty string from
409
  // no eval at all. Having it not start with a dash makes it inaccessible
410
  // from the parser itself, but available for using Implies().
411
  // TODO(addaleax): When moving --help over to something generated from the
412
  // programmatic descriptions, this will need some special care.
413
  // (See also [ssl_openssl_cert_store] below.)
414
4525
  AddOption("[has_eval_string]", "", &EnvironmentOptions::has_eval_string);
415
4525
  AddOption("--eval", "evaluate script", &EnvironmentOptions::eval_string);
416
4525
  Implies("--eval", "[has_eval_string]");
417
  AddOption("--print",
418
            "evaluate script and print result",
419
4525
            &EnvironmentOptions::print_eval);
420
4525
  AddAlias("-e", "--eval");
421
4525
  AddAlias("--print <arg>", "-pe");
422
4525
  AddAlias("-pe", { "--print", "--eval" });
423
4525
  AddAlias("-p", "--print");
424
  AddOption("--require",
425
            "module to preload (option can be repeated)",
426
            &EnvironmentOptions::preload_modules,
427
4525
            kAllowedInEnvironment);
428
4525
  AddAlias("-r", "--require");
429
  AddOption("--interactive",
430
            "always enter the REPL even if stdin does not appear "
431
            "to be a terminal",
432
4525
            &EnvironmentOptions::force_repl);
433
4525
  AddAlias("-i", "--interactive");
434
435
4525
  AddOption("--napi-modules", "", NoOp{}, kAllowedInEnvironment);
436
437
  AddOption("--tls-min-v1.0",
438
            "set default TLS minimum to TLSv1.0 (default: TLSv1.2)",
439
            &EnvironmentOptions::tls_min_v1_0,
440
4525
            kAllowedInEnvironment);
441
  AddOption("--tls-min-v1.1",
442
            "set default TLS minimum to TLSv1.1 (default: TLSv1.2)",
443
            &EnvironmentOptions::tls_min_v1_1,
444
4525
            kAllowedInEnvironment);
445
  AddOption("--tls-min-v1.2",
446
            "set default TLS minimum to TLSv1.2 (default: TLSv1.2)",
447
            &EnvironmentOptions::tls_min_v1_2,
448
4525
            kAllowedInEnvironment);
449
  AddOption("--tls-min-v1.3",
450
            "set default TLS minimum to TLSv1.3 (default: TLSv1.2)",
451
            &EnvironmentOptions::tls_min_v1_3,
452
4525
            kAllowedInEnvironment);
453
  AddOption("--tls-max-v1.2",
454
            "set default TLS maximum to TLSv1.2 (default: TLSv1.3)",
455
            &EnvironmentOptions::tls_max_v1_2,
456
4525
            kAllowedInEnvironment);
457
  // Current plan is:
458
  // - 11.x and below: TLS1.3 is opt-in with --tls-max-v1.3
459
  // - 12.x: TLS1.3 is opt-out with --tls-max-v1.2
460
  // In either case, support both options they are uniformly available.
461
  AddOption("--tls-max-v1.3",
462
            "set default TLS maximum to TLSv1.3 (default: TLSv1.3)",
463
            &EnvironmentOptions::tls_max_v1_3,
464
4525
            kAllowedInEnvironment);
465
4525
}
466
467
4525
PerIsolateOptionsParser::PerIsolateOptionsParser(
468
4525
  const EnvironmentOptionsParser& eop) {
469
  AddOption("--track-heap-objects",
470
            "track heap object allocations for heap snapshots",
471
            &PerIsolateOptions::track_heap_objects,
472
4525
            kAllowedInEnvironment);
473
474
  // Explicitly add some V8 flags to mark them as allowed in NODE_OPTIONS.
475
  AddOption("--abort-on-uncaught-exception",
476
            "aborting instead of exiting causes a core file to be generated "
477
            "for analysis",
478
            V8Option{},
479
4525
            kAllowedInEnvironment);
480
4525
  AddOption("--max-old-space-size", "", V8Option{}, kAllowedInEnvironment);
481
4525
  AddOption("--perf-basic-prof", "", V8Option{}, kAllowedInEnvironment);
482
  AddOption("--perf-basic-prof-only-functions",
483
            "",
484
            V8Option{},
485
4525
            kAllowedInEnvironment);
486
4525
  AddOption("--perf-prof", "", V8Option{}, kAllowedInEnvironment);
487
  AddOption("--perf-prof-unwinding-info",
488
            "",
489
            V8Option{},
490
4525
            kAllowedInEnvironment);
491
4525
  AddOption("--stack-trace-limit", "", V8Option{}, kAllowedInEnvironment);
492
493
#ifdef NODE_REPORT
494
  AddOption("--report-uncaught-exception",
495
            "generate diagnostic report on uncaught exceptions",
496
            &PerIsolateOptions::report_uncaught_exception,
497
4525
            kAllowedInEnvironment);
498
  AddOption("--report-on-signal",
499
            "generate diagnostic report upon receiving signals",
500
            &PerIsolateOptions::report_on_signal,
501
4525
            kAllowedInEnvironment);
502
  AddOption("--report-on-fatalerror",
503
            "generate diagnostic report on fatal (internal) errors",
504
            &PerIsolateOptions::report_on_fatalerror,
505
4525
            kAllowedInEnvironment);
506
  AddOption("--report-signal",
507
            "causes diagnostic report to be produced on provided signal,"
508
            " unsupported in Windows. (default: SIGUSR2)",
509
            &PerIsolateOptions::report_signal,
510
4525
            kAllowedInEnvironment);
511
4525
  Implies("--report-signal", "--report-on-signal");
512
  AddOption("--report-filename",
513
            "define custom report file name."
514
            " (default: YYYYMMDD.HHMMSS.PID.SEQUENCE#.txt)",
515
            &PerIsolateOptions::report_filename,
516
4525
            kAllowedInEnvironment);
517
  AddOption("--report-directory",
518
            "define custom report pathname."
519
            " (default: current working directory of Node.js process)",
520
            &PerIsolateOptions::report_directory,
521
4525
            kAllowedInEnvironment);
522
#endif  // NODE_REPORT
523
524
4525
  Insert(eop, &PerIsolateOptions::get_per_env_options);
525
4525
}
526
527
4525
PerProcessOptionsParser::PerProcessOptionsParser(
528
4525
  const PerIsolateOptionsParser& iop) {
529
  AddOption("--title",
530
            "the process title to use on startup",
531
            &PerProcessOptions::title,
532
4525
            kAllowedInEnvironment);
533
  AddOption("--trace-event-categories",
534
            "comma separated list of trace event categories to record",
535
            &PerProcessOptions::trace_event_categories,
536
4525
            kAllowedInEnvironment);
537
  AddOption("--trace-event-file-pattern",
538
            "Template string specifying the filepath for the trace-events "
539
            "data, it supports ${rotation} and ${pid}.",
540
            &PerProcessOptions::trace_event_file_pattern,
541
4525
            kAllowedInEnvironment);
542
  AddAlias("--trace-events-enabled", {
543
4525
    "--trace-event-categories", "v8,node,node.async_hooks" });
544
  AddOption("--max-http-header-size",
545
            "set the maximum size of HTTP headers (default: 8KB)",
546
            &PerProcessOptions::max_http_header_size,
547
4525
            kAllowedInEnvironment);
548
  AddOption("--v8-pool-size",
549
            "set V8's thread pool size",
550
            &PerProcessOptions::v8_thread_pool_size,
551
4525
            kAllowedInEnvironment);
552
  AddOption("--zero-fill-buffers",
553
            "automatically zero-fill all newly allocated Buffer and "
554
            "SlowBuffer instances",
555
            &PerProcessOptions::zero_fill_all_buffers,
556
4525
            kAllowedInEnvironment);
557
  AddOption("--debug-arraybuffer-allocations",
558
            "", /* undocumented, only for debugging */
559
            &PerProcessOptions::debug_arraybuffer_allocations,
560
4525
            kAllowedInEnvironment);
561
562
4525
  AddOption("--security-reverts", "", &PerProcessOptions::security_reverts);
563
  AddOption("--completion-bash",
564
            "print source-able bash completion script",
565
4525
            &PerProcessOptions::print_bash_completion);
566
  AddOption("--help",
567
            "print node command line options",
568
4525
            &PerProcessOptions::print_help);
569
4525
  AddAlias("-h", "--help");
570
  AddOption(
571
4525
      "--version", "print Node.js version", &PerProcessOptions::print_version);
572
4525
  AddAlias("-v", "--version");
573
  AddOption("--v8-options",
574
            "print V8 command line options",
575
4525
            &PerProcessOptions::print_v8_help);
576
577
#ifdef NODE_HAVE_I18N_SUPPORT
578
  AddOption("--icu-data-dir",
579
            "set ICU data load path to dir (overrides NODE_ICU_DATA)"
580
#ifndef NODE_HAVE_SMALL_ICU
581
            " (note: linked-in ICU data is present)\n"
582
#endif
583
            ,
584
            &PerProcessOptions::icu_data_dir,
585
4525
            kAllowedInEnvironment);
586
#endif
587
588
#if HAVE_OPENSSL
589
  AddOption("--openssl-config",
590
            "load OpenSSL configuration from the specified file "
591
            "(overrides OPENSSL_CONF)",
592
            &PerProcessOptions::openssl_config,
593
4525
            kAllowedInEnvironment);
594
  AddOption("--tls-cipher-list",
595
            "use an alternative default TLS cipher list",
596
            &PerProcessOptions::tls_cipher_list,
597
4525
            kAllowedInEnvironment);
598
  AddOption("--use-openssl-ca",
599
            "use OpenSSL's default CA store"
600
#if defined(NODE_OPENSSL_CERT_STORE)
601
            " (default)"
602
#endif
603
            ,
604
            &PerProcessOptions::use_openssl_ca,
605
4525
            kAllowedInEnvironment);
606
  AddOption("--use-bundled-ca",
607
            "use bundled CA store"
608
#if !defined(NODE_OPENSSL_CERT_STORE)
609
            " (default)"
610
#endif
611
            ,
612
            &PerProcessOptions::use_bundled_ca,
613
4525
            kAllowedInEnvironment);
614
  // Similar to [has_eval_string] above, except that the separation between
615
  // this and use_openssl_ca only exists for option validation after parsing.
616
  // This is not ideal.
617
  AddOption("[ssl_openssl_cert_store]",
618
            "",
619
4525
            &PerProcessOptions::ssl_openssl_cert_store);
620
4525
  Implies("--use-openssl-ca", "[ssl_openssl_cert_store]");
621
4525
  ImpliesNot("--use-bundled-ca", "[ssl_openssl_cert_store]");
622
#if NODE_FIPS_MODE
623
  AddOption("--enable-fips",
624
            "enable FIPS crypto at startup",
625
            &PerProcessOptions::enable_fips_crypto,
626
            kAllowedInEnvironment);
627
  AddOption("--force-fips",
628
            "force FIPS crypto (cannot be disabled)",
629
            &PerProcessOptions::force_fips_crypto,
630
            kAllowedInEnvironment);
631
#endif
632
#endif
633
634
4525
  Insert(iop, &PerProcessOptions::get_per_isolate_options);
635
4525
}
636
637
107
inline std::string RemoveBrackets(const std::string& host) {
638


107
  if (!host.empty() && host.front() == '[' && host.back() == ']')
639
4
    return host.substr(1, host.size() - 2);
640
  else
641
103
    return host;
642
}
643
644
95
inline int ParseAndValidatePort(const std::string& port,
645
                                std::vector<std::string>* errors) {
646
  char* endptr;
647
95
  errno = 0;
648
  const unsigned long result =                 // NOLINT(runtime/int)
649
95
    strtoul(port.c_str(), &endptr, 10);
650

95
  if (errno != 0 || *endptr != '\0'||
651

64
      (result != 0 && result < 1024) || result > 65535) {
652
    errors->push_back(" must be 0 or in range 1024 to 65535.");
653
  }
654
95
  return static_cast<int>(result);
655
}
656
657
95
HostPort SplitHostPort(const std::string& arg,
658
                      std::vector<std::string>* errors) {
659
  // remove_brackets only works if no port is specified
660
  // so if it has an effect only an IPv6 address was specified.
661
95
  std::string host = RemoveBrackets(arg);
662
95
  if (host.length() < arg.length())
663
    return HostPort{host, DebugOptions::kDefaultInspectorPort};
664
665
95
  size_t colon = arg.rfind(':');
666
95
  if (colon == std::string::npos) {
667
    // Either a port number or a host name.  Assume that
668
    // if it's not all decimal digits, it's a host name.
669
383
    for (char c : arg) {
670

300
      if (c < '0' || c > '9') {
671
        return HostPort{arg, DebugOptions::kDefaultInspectorPort};
672
      }
673
    }
674
83
    return HostPort { "", ParseAndValidatePort(arg, errors) };
675
  }
676
  // Host and port found:
677
  return HostPort { RemoveBrackets(arg.substr(0, colon)),
678
12
                    ParseAndValidatePort(arg.substr(colon + 1), errors) };
679
}
680
681
// Return a map containing all the options and their metadata as well
682
// as the aliases
683
4643
void GetOptions(const FunctionCallbackInfo<Value>& args) {
684
4643
  Mutex::ScopedLock lock(per_process::cli_options_mutex);
685
4643
  Environment* env = Environment::GetCurrent(args);
686
4643
  if (!env->has_run_bootstrapping_code()) {
687
    // No code because this is an assertion.
688
    return env->ThrowError(
689
        "Should not query options before bootstrapping is done");
690
  }
691
4643
  env->set_has_serialized_options(true);
692
693
4643
  Isolate* isolate = env->isolate();
694
4643
  Local<Context> context = env->context();
695
696
  // Temporarily act as if the current Environment's/IsolateData's options were
697
  // the default options, i.e. like they are the ones we'd access for global
698
  // options parsing, so that all options are available from the main parser.
699
9286
  auto original_per_isolate = per_process::cli_options->per_isolate;
700
4643
  per_process::cli_options->per_isolate = env->isolate_data()->options();
701
9286
  auto original_per_env = per_process::cli_options->per_isolate->per_env;
702
4643
  per_process::cli_options->per_isolate->per_env = env->options();
703
4643
  OnScopeLeave on_scope_leave([&]() {
704
4643
    per_process::cli_options->per_isolate->per_env = original_per_env;
705
4643
    per_process::cli_options->per_isolate = original_per_isolate;
706
13929
  });
707
708
4643
  Local<Map> options = Map::New(isolate);
709
390012
  for (const auto& item : _ppop_instance.options_) {
710
    Local<Value> value;
711
385369
    const auto& option_info = item.second;
712
385369
    auto field = option_info.field;
713
385369
    PerProcessOptions* opts = per_process::cli_options.get();
714


385369
    switch (option_info.type) {
715
      case kNoOp:
716
      case kV8Option:
717
        // Special case for --abort-on-uncaught-exception which is also
718
        // respected by Node.js internals
719
41787
        if (item.first == "--abort-on-uncaught-exception") {
720
          value = Boolean::New(
721
9286
            isolate, original_per_env->abort_on_uncaught_exception);
722
        } else {
723
37144
          value = Undefined(isolate);
724
        }
725
41787
        break;
726
      case kBoolean:
727
        value = Boolean::New(isolate,
728
445728
                             *_ppop_instance.Lookup<bool>(field, opts));
729
222864
        break;
730
      case kInteger:
731
        value = Number::New(isolate,
732
9286
                            *_ppop_instance.Lookup<int64_t>(field, opts));
733
4643
        break;
734
      case kUInteger:
735
        value = Number::New(isolate,
736
18572
                            *_ppop_instance.Lookup<uint64_t>(field, opts));
737
9286
        break;
738
      case kString:
739
185720
        if (!ToV8Value(context,
740
185720
                       *_ppop_instance.Lookup<std::string>(field, opts))
741
278580
                 .ToLocal(&value)) {
742
          return;
743
        }
744
92860
        break;
745
      case kStringList:
746
18572
        if (!ToV8Value(context,
747
18572
                       *_ppop_instance.Lookup<StringVector>(field, opts))
748
27858
                 .ToLocal(&value)) {
749
          return;
750
        }
751
9286
        break;
752
      case kHostPort: {
753
        const HostPort& host_port =
754
4643
          *_ppop_instance.Lookup<HostPort>(field, opts);
755
4643
        Local<Object> obj = Object::New(isolate);
756
        Local<Value> host;
757

27858
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
758


41787
            obj->Set(context, env->host_string(), host).IsNothing() ||
759
            obj->Set(context,
760
                     env->port_string(),
761

18572
                     Integer::New(isolate, host_port.port()))
762
23215
                .IsNothing()) {
763
          return;
764
        }
765
4643
        value = obj;
766
4643
        break;
767
      }
768
      default:
769
        UNREACHABLE();
770
    }
771
385369
    CHECK(!value.IsEmpty());
772
773
770738
    Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
774
385369
    Local<Object> info = Object::New(isolate);
775
    Local<Value> help_text;
776

2697583
    if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) ||
777
1156107
        !info->Set(context, env->help_text_string(), help_text)
778

3468321
             .FromMaybe(false) ||
779
        !info->Set(context,
780
                   env->env_var_settings_string(),
781
                   Integer::New(isolate,
782

1541476
                                static_cast<int>(option_info.env_setting)))
783

3853690
             .FromMaybe(false) ||
784
        !info->Set(context,
785
                   env->type_string(),
786

1541476
                   Integer::New(isolate, static_cast<int>(option_info.type)))
787

3468321
             .FromMaybe(false) ||
788


3853690
        info->Set(context, env->value_string(), value).IsNothing() ||
789

1541476
        options->Set(context, name, info).IsEmpty()) {
790
      return;
791
    }
792
385369
  }
793
794
  Local<Value> aliases;
795
9286
  if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return;
796
797
4643
  Local<Object> ret = Object::New(isolate);
798



37144
  if (ret->Set(context, env->options_string(), options).IsNothing() ||
799

23215
      ret->Set(context, env->aliases_string(), aliases).IsNothing()) {
800
    return;
801
  }
802
803
13929
  args.GetReturnValue().Set(ret);
804
}
805
806
4643
void Initialize(Local<Object> target,
807
                Local<Value> unused,
808
                Local<Context> context,
809
                void* priv) {
810
4643
  Environment* env = Environment::GetCurrent(context);
811
4643
  Isolate* isolate = env->isolate();
812
4643
  env->SetMethodNoSideEffect(target, "getOptions", GetOptions);
813
814
4643
  Local<Object> env_settings = Object::New(isolate);
815
18572
  NODE_DEFINE_CONSTANT(env_settings, kAllowedInEnvironment);
816
18572
  NODE_DEFINE_CONSTANT(env_settings, kDisallowedInEnvironment);
817
  target
818
      ->Set(
819
13929
          context, FIXED_ONE_BYTE_STRING(isolate, "envSettings"), env_settings)
820
9286
      .Check();
821
822
4643
  Local<Object> types = Object::New(isolate);
823
18572
  NODE_DEFINE_CONSTANT(types, kNoOp);
824
18572
  NODE_DEFINE_CONSTANT(types, kV8Option);
825
18572
  NODE_DEFINE_CONSTANT(types, kBoolean);
826
18572
  NODE_DEFINE_CONSTANT(types, kInteger);
827
18572
  NODE_DEFINE_CONSTANT(types, kUInteger);
828
18572
  NODE_DEFINE_CONSTANT(types, kString);
829
18572
  NODE_DEFINE_CONSTANT(types, kHostPort);
830
18572
  NODE_DEFINE_CONSTANT(types, kStringList);
831
13929
  target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "types"), types)
832
9286
      .Check();
833
4643
}
834
835
}  // namespace options_parser
836
}  // namespace node
837
838

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