UNPKG

2.99 kBJavaScriptView Raw
1// Replacement for node's internal 'internal/options' module
2
3exports.getOptionValue = getOptionValue;
4function getOptionValue(opt) {
5 parseOptions();
6 return options[opt];
7}
8
9let options;
10function parseOptions() {
11 if (!options) {
12 options = {
13 '--preserve-symlinks': false,
14 '--preserve-symlinks-main': false,
15 '--input-type': undefined,
16 '--experimental-specifier-resolution': 'explicit',
17 '--experimental-policy': undefined,
18 '--conditions': [],
19 '--pending-deprecation': false,
20 ...parseArgv(getNodeOptionsEnvArgv()),
21 ...parseArgv(process.execArgv),
22 ...getOptionValuesFromOtherEnvVars()
23 }
24 }
25}
26
27function parseArgv(argv) {
28 return require('arg')({
29 '--preserve-symlinks': Boolean,
30 '--preserve-symlinks-main': Boolean,
31 '--input-type': String,
32 '--experimental-specifier-resolution': String,
33 // Legacy alias for node versions prior to 12.16
34 '--es-module-specifier-resolution': '--experimental-specifier-resolution',
35 '--experimental-policy': String,
36 '--conditions': [String],
37 '--pending-deprecation': Boolean,
38 '--experimental-json-modules': Boolean,
39 '--experimental-wasm-modules': Boolean,
40 }, {
41 argv,
42 permissive: true
43 });
44}
45
46function getNodeOptionsEnvArgv() {
47 const errors = [];
48 const envArgv = ParseNodeOptionsEnvVar(process.env.NODE_OPTIONS || '', errors);
49 if (errors.length !== 0) {
50 // TODO: handle errors somehow
51 }
52 return envArgv;
53}
54
55// Direct JS port of C implementation: https://github.com/nodejs/node/blob/67ba825037b4082d5d16f922fb9ce54516b4a869/src/node_options.cc#L1024-L1063
56function ParseNodeOptionsEnvVar(node_options, errors) {
57 const env_argv = [];
58
59 let is_in_string = false;
60 let will_start_new_arg = true;
61 for (let index = 0; index < node_options.length; ++index) {
62 let c = node_options[index];
63
64 // Backslashes escape the following character
65 if (c === '\\' && is_in_string) {
66 if (index + 1 === node_options.length) {
67 errors.push("invalid value for NODE_OPTIONS " +
68 "(invalid escape)\n");
69 return env_argv;
70 } else {
71 c = node_options[++index];
72 }
73 } else if (c === ' ' && !is_in_string) {
74 will_start_new_arg = true;
75 continue;
76 } else if (c === '"') {
77 is_in_string = !is_in_string;
78 continue;
79 }
80
81 if (will_start_new_arg) {
82 env_argv.push(c);
83 will_start_new_arg = false;
84 } else {
85 env_argv[env_argv.length - 1] += c;
86 }
87 }
88
89 if (is_in_string) {
90 errors.push("invalid value for NODE_OPTIONS " +
91 "(unterminated string)\n");
92 }
93 return env_argv;
94}
95
96// Get option values that can be specified via env vars besides NODE_OPTIONS
97function getOptionValuesFromOtherEnvVars() {
98 const options = {};
99 if(process.env.NODE_PENDING_DEPRECATION === '1') {
100 options['--pending-deprecation'] = true;
101 }
102 return options;
103}