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: 552 582 94.8 %
Date: 2021-06-02 04:11:51 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
4840
Mutex cli_options_mutex;
30
4840
std::shared_ptr<PerProcessOptions> cli_options{new PerProcessOptions()};
31
}  // namespace per_process
32
33
9757
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
9757
  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
19514
      SplitString(inspect_publish_uid_string, ',');
49
9757
  inspect_publish_uid.console = false;
50
9757
  inspect_publish_uid.http = false;
51
29269
  for (const std::string& destination : destinations) {
52
19512
    if (destination == "stderr") {
53
9756
      inspect_publish_uid.console = true;
54
9756
    } else if (destination == "http") {
55
9756
      inspect_publish_uid.http = true;
56
    } else {
57
      errors->push_back("--inspect-publish-uid destination can be "
58
                        "stderr or http");
59
    }
60
  }
61
9757
}
62
63
9529
void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) {
64
#if HAVE_OPENSSL
65

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

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

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

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

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

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

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

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

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


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


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


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

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

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

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


639846
    switch (option_info.type) {
935
      case kNoOp:
936
      case kV8Option:
937
        // Special case for --abort-on-uncaught-exception which is also
938
        // respected by Node.js internals
939
98838
        if (item.first == "--abort-on-uncaught-exception") {
940
5202
          value = Boolean::New(
941
10404
            isolate, original_per_env->abort_on_uncaught_exception);
942
        } else {
943
93636
          value = Undefined(isolate);
944
        }
945
98838
        break;
946
      case kBoolean:
947
343332
        value = Boolean::New(isolate,
948
686664
                             *_ppop_instance.Lookup<bool>(field, opts));
949
343332
        break;
950
      case kInteger:
951
20808
        value = Number::New(
952
            isolate,
953
41616
            static_cast<double>(*_ppop_instance.Lookup<int64_t>(field, opts)));
954
20808
        break;
955
      case kUInteger:
956
15606
        value = Number::New(
957
            isolate,
958
31212
            static_cast<double>(*_ppop_instance.Lookup<uint64_t>(field, opts)));
959
15606
        break;
960
      case kString:
961
421362
        if (!ToV8Value(context,
962
280908
                       *_ppop_instance.Lookup<std::string>(field, opts))
963
140454
                 .ToLocal(&value)) {
964
          return;
965
        }
966
140454
        break;
967
      case kStringList:
968
46818
        if (!ToV8Value(context,
969
31212
                       *_ppop_instance.Lookup<StringVector>(field, opts))
970
15606
                 .ToLocal(&value)) {
971
          return;
972
        }
973
15606
        break;
974
      case kHostPort: {
975
        const HostPort& host_port =
976
5202
          *_ppop_instance.Lookup<HostPort>(field, opts);
977
5202
        Local<Object> obj = Object::New(isolate);
978
        Local<Value> host;
979

26010
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
980

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

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

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

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

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

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

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

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