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: 551 581 94.8 %
Date: 2021-04-29 04:13:25 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
4828
Mutex cli_options_mutex;
30
4828
std::shared_ptr<PerProcessOptions> cli_options{new PerProcessOptions()};
31
}  // namespace per_process
32
33
9731
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
9731
  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
19462
      SplitString(inspect_publish_uid_string, ',');
49
9731
  inspect_publish_uid.console = false;
50
9731
  inspect_publish_uid.http = false;
51
29191
  for (const std::string& destination : destinations) {
52
19460
    if (destination == "stderr") {
53
9730
      inspect_publish_uid.console = true;
54
9730
    } else if (destination == "http") {
55
9730
      inspect_publish_uid.http = true;
56
    } else {
57
      errors->push_back("--inspect-publish-uid destination can be "
58
                        "stderr or http");
59
    }
60
  }
61
9731
}
62
63
9505
void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) {
64
#if HAVE_OPENSSL
65

9505
  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
9505
  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

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

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

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

9731
  if (has_policy_integrity_string && experimental_policy_integrity.empty()) {
102
1
    errors->push_back("--policy-integrity cannot be empty");
103
  }
104
105
9731
  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
9731
  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

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

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

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

9731
  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
9731
  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
9731
  if (!cpu_prof) {
143
9720
    if (!cpu_prof_name.empty()) {
144
1
      errors->push_back("--cpu-prof-name must be used with --cpu-prof");
145
    }
146
9720
    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
9720
    if (cpu_prof_interval != kDefaultCpuProfInterval) {
152
1
      errors->push_back("--cpu-prof-interval must be used with --cpu-prof");
153
    }
154
  }
155
156


9731
  if (cpu_prof && cpu_prof_dir.empty() && !diagnostic_dir.empty()) {
157
1
      cpu_prof_dir = diagnostic_dir;
158
    }
159
160
9731
  if (!heap_prof) {
161
9720
    if (!heap_prof_name.empty()) {
162
1
      errors->push_back("--heap-prof-name must be used with --heap-prof");
163
    }
164
9720
    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
9720
    if (heap_prof_interval != kDefaultHeapProfInterval) {
170
1
      errors->push_back("--heap-prof-interval must be used with --heap-prof");
171
    }
172
  }
173
174


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


112
  if (!host.empty() && host.front() == '[' && host.back() == ']')
814
4
    return host.substr(1, host.size() - 2);
815
  else
816
108
    return host;
817
}
818
819
100
inline int ParseAndValidatePort(const std::string& port,
820
                                std::vector<std::string>* errors) {
821
  char* endptr;
822
100
  errno = 0;
823
  const unsigned long result =                 // NOLINT(runtime/int)
824
100
    strtoul(port.c_str(), &endptr, 10);
825

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

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

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


636648
    switch (option_info.type) {
934
      case kNoOp:
935
      case kV8Option:
936
        // Special case for --abort-on-uncaught-exception which is also
937
        // respected by Node.js internals
938
98344
        if (item.first == "--abort-on-uncaught-exception") {
939
5176
          value = Boolean::New(
940
10352
            isolate, original_per_env->abort_on_uncaught_exception);
941
        } else {
942
93168
          value = Undefined(isolate);
943
        }
944
98344
        break;
945
      case kBoolean:
946
341616
        value = Boolean::New(isolate,
947
683232
                             *_ppop_instance.Lookup<bool>(field, opts));
948
341616
        break;
949
      case kInteger:
950
20704
        value = Number::New(
951
            isolate,
952
41408
            static_cast<double>(*_ppop_instance.Lookup<int64_t>(field, opts)));
953
20704
        break;
954
      case kUInteger:
955
15528
        value = Number::New(
956
            isolate,
957
31056
            static_cast<double>(*_ppop_instance.Lookup<uint64_t>(field, opts)));
958
15528
        break;
959
      case kString:
960
419256
        if (!ToV8Value(context,
961
279504
                       *_ppop_instance.Lookup<std::string>(field, opts))
962
139752
                 .ToLocal(&value)) {
963
          return;
964
        }
965
139752
        break;
966
      case kStringList:
967
46584
        if (!ToV8Value(context,
968
31056
                       *_ppop_instance.Lookup<StringVector>(field, opts))
969
15528
                 .ToLocal(&value)) {
970
          return;
971
        }
972
15528
        break;
973
      case kHostPort: {
974
        const HostPort& host_port =
975
5176
          *_ppop_instance.Lookup<HostPort>(field, opts);
976
5176
        Local<Object> obj = Object::New(isolate);
977
        Local<Value> host;
978

25880
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
979

25880
            obj->Set(context, env->host_string(), host).IsNothing() ||
980
10352
            obj->Set(context,
981
                     env->port_string(),
982
20704
                     Integer::New(isolate, host_port.port()))
983
5176
                .IsNothing()) {
984
          return;
985
        }
986
5176
        value = obj;
987
5176
        break;
988
      }
989
      default:
990
        UNREACHABLE();
991
    }
992
636648
    CHECK(!value.IsEmpty());
993
994
1273296
    Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
995
636648
    Local<Object> info = Object::New(isolate);
996
    Local<Value> help_text;
997

3183240
    if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) ||
998
2546592
        !info->Set(context, env->help_text_string(), help_text)
999
2546592
             .FromMaybe(false) ||
1000
1909944
        !info->Set(context,
1001
                   env->env_var_settings_string(),
1002
                   Integer::New(isolate,
1003
2546592
                                static_cast<int>(option_info.env_setting)))
1004
2546592
             .FromMaybe(false) ||
1005
1909944
        !info->Set(context,
1006
                   env->type_string(),
1007
2546592
                   Integer::New(isolate, static_cast<int>(option_info.type)))
1008
2546592
             .FromMaybe(false) ||
1009

3819888
        info->Set(context, env->value_string(), value).IsNothing() ||
1010
1909944
        options->Set(context, name, info).IsEmpty()) {
1011
      return;
1012
    }
1013
  }
1014
1015
  Local<Value> aliases;
1016
10352
  if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return;
1017
1018
20704
  if (aliases.As<Object>()
1019
20704
          ->SetPrototype(context, env->primordials_safe_map_prototype_object())
1020
          .IsNothing()) {
1021
    return;
1022
  }
1023
1024
5176
  Local<Object> ret = Object::New(isolate);
1025

25880
  if (ret->Set(context, env->options_string(), options).IsNothing() ||
1026
20704
      ret->Set(context, env->aliases_string(), aliases).IsNothing()) {
1027
    return;
1028
  }
1029
1030
10352
  args.GetReturnValue().Set(ret);
1031
}
1032
1033
5176
void Initialize(Local<Object> target,
1034
                Local<Value> unused,
1035
                Local<Context> context,
1036
                void* priv) {
1037
5176
  Environment* env = Environment::GetCurrent(context);
1038
5176
  Isolate* isolate = env->isolate();
1039
5176
  env->SetMethodNoSideEffect(target, "getOptions", GetOptions);
1040
1041
5176
  Local<Object> env_settings = Object::New(isolate);
1042
10352
  NODE_DEFINE_CONSTANT(env_settings, kAllowedInEnvironment);
1043
5176
  NODE_DEFINE_CONSTANT(env_settings, kDisallowedInEnvironment);
1044
20704
  target
1045
25880
      ->Set(
1046
31056
          context, FIXED_ONE_BYTE_STRING(isolate, "envSettings"), env_settings)
1047
10352
      .Check();
1048
1049
  target
1050
10352
      ->Set(context,
1051
            FIXED_ONE_BYTE_STRING(env->isolate(), "shouldNotRegisterESMLoader"),
1052
20704
            Boolean::New(isolate, env->should_not_register_esm_loader()))
1053
      .Check();
1054
1055
5176
  Local<Object> types = Object::New(isolate);
1056
5176
  NODE_DEFINE_CONSTANT(types, kNoOp);
1057
15528
  NODE_DEFINE_CONSTANT(types, kV8Option);
1058
20704
  NODE_DEFINE_CONSTANT(types, kBoolean);
1059
36232
  NODE_DEFINE_CONSTANT(types, kInteger);
1060
46584
  NODE_DEFINE_CONSTANT(types, kUInteger);
1061
46584
  NODE_DEFINE_CONSTANT(types, kString);
1062
46584
  NODE_DEFINE_CONSTANT(types, kHostPort);
1063
46584
  NODE_DEFINE_CONSTANT(types, kStringList);
1064
56936
  target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "types"), types)
1065
31056
      .Check();
1066
31056
}
1067
10352
1068
}  // namespace options_parser
1069
1070
4820
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options) {
1071
28920
  HandleEnvOptions(env_options, [](const char* name) {
1072
38560
    std::string text;
1073

38560
    return credentials::SafeGetenv(name, &text) ? text : "";
1074
4820
  });
1075
4820
}
1076
1077
4935
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
1078
                      std::function<std::string(const char*)> opt_getter) {
1079
9870
  env_options->pending_deprecation =
1080
9870
      opt_getter("NODE_PENDING_DEPRECATION") == "1";
1081
1082
4935
  env_options->preserve_symlinks = opt_getter("NODE_PRESERVE_SYMLINKS") == "1";
1083
1084
9870
  env_options->preserve_symlinks_main =
1085
9870
      opt_getter("NODE_PRESERVE_SYMLINKS_MAIN") == "1";
1086
1087
4935
  if (env_options->redirect_warnings.empty())
1088
4935
    env_options->redirect_warnings = opt_getter("NODE_REDIRECT_WARNINGS");
1089
4935
}
1090
1091
4816
std::vector<std::string> ParseNodeOptionsEnvVar(
1092
    const std::string& node_options, std::vector<std::string>* errors) {
1093
4816
  std::vector<std::string> env_argv;
1094
1095
4816
  bool is_in_string = false;
1096
4816
  bool will_start_new_arg = true;
1097
7538
  for (std::string::size_type index = 0; index < node_options.size(); ++index) {
1098
2722
    char c = node_options.at(index);
1099
1100
    // Backslashes escape the following character
1101

2722
    if (c == '\\' && is_in_string) {
1102
      if (index + 1 == node_options.size()) {
1103
        errors->push_back("invalid value for NODE_OPTIONS "
1104
                          "(invalid escape)\n");
1105
        return env_argv;
1106
      } else {
1107
        c = node_options.at(++index);
1108
      }
1109

2722
    } else if (c == ' ' && !is_in_string) {
1110
32
      will_start_new_arg = true;
1111
32
      continue;
1112
2690
    } else if (c == '"') {
1113
4
      is_in_string = !is_in_string;
1114
4
      continue;
1115
    }
1116
1117
2686
    if (will_start_new_arg) {
1118
107
      env_argv.emplace_back(std::string(1, c));
1119
107
      will_start_new_arg = false;
1120
    } else {
1121
2579
      env_argv.back() += c;
1122
    }
1123
  }
1124
1125
4816
  if (is_in_string) {
1126
    errors->push_back("invalid value for NODE_OPTIONS "
1127
                      "(unterminated string)\n");
1128
  }
1129
4816
  return env_argv;
1130
}
1131
}  // namespace node
1132
1133

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