1 #ifndef SRC_NODE_OPTIONS_H_ 2 #define SRC_NODE_OPTIONS_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include <memory> 7 #include <string> 8 #include <unordered_map> 9 #include <vector> 10 #include "node_constants.h" 11 #include "node_mutex.h" 12 #include "util.h" 13 14 namespace node { 15 16 class HostPort { 17 public: HostPort(const std::string & host_name,int port)18 HostPort(const std::string& host_name, int port) 19 : host_name_(host_name), port_(port) {} 20 HostPort(const HostPort&) = default; 21 HostPort& operator=(const HostPort&) = default; 22 HostPort(HostPort&&) = default; 23 HostPort& operator=(HostPort&&) = default; 24 set_host(const std::string & host)25 void set_host(const std::string& host) { host_name_ = host; } 26 set_port(int port)27 void set_port(int port) { port_ = port; } 28 host()29 const std::string& host() const { return host_name_; } 30 port()31 int port() const { 32 // TODO(joyeecheung): make port a uint16_t 33 CHECK_GE(port_, 0); 34 return port_; 35 } 36 Update(const HostPort & other)37 void Update(const HostPort& other) { 38 if (!other.host_name_.empty()) host_name_ = other.host_name_; 39 if (other.port_ >= 0) port_ = other.port_; 40 } 41 42 private: 43 std::string host_name_; 44 int port_; 45 }; 46 47 class Options { 48 public: CheckOptions(std::vector<std::string> * errors)49 virtual void CheckOptions(std::vector<std::string>* errors) {} 50 virtual ~Options() = default; 51 }; 52 53 struct InspectPublishUid { 54 bool console; 55 bool http; 56 }; 57 58 // These options are currently essentially per-Environment, but it can be nice 59 // to keep them separate since they are a group of options applying to a very 60 // specific part of Node. It might also make more sense for them to be 61 // per-Isolate, rather than per-Environment. 62 class DebugOptions : public Options { 63 public: 64 DebugOptions() = default; 65 DebugOptions(const DebugOptions&) = default; 66 DebugOptions& operator=(const DebugOptions&) = default; 67 DebugOptions(DebugOptions&&) = default; 68 DebugOptions& operator=(DebugOptions&&) = default; 69 70 // --inspect 71 bool inspector_enabled = false; 72 // --debug 73 bool deprecated_debug = false; 74 // --inspect-brk 75 bool break_first_line = false; 76 // --inspect-brk-node 77 bool break_node_first_line = false; 78 // --inspect-publish-uid 79 std::string inspect_publish_uid_string = "stderr,http"; 80 81 InspectPublishUid inspect_publish_uid; 82 83 enum { kDefaultInspectorPort = 9229 }; 84 85 HostPort host_port{"127.0.0.1", kDefaultInspectorPort}; 86 87 // Used to patch the options as if --inspect-brk is passed. EnableBreakFirstLine()88 void EnableBreakFirstLine() { 89 inspector_enabled = true; 90 break_first_line = true; 91 } 92 wait_for_connect()93 bool wait_for_connect() const { 94 return break_first_line || break_node_first_line; 95 } 96 97 void CheckOptions(std::vector<std::string>* errors) override; 98 }; 99 100 class EnvironmentOptions : public Options { 101 public: 102 bool abort_on_uncaught_exception = false; 103 std::vector<std::string> conditions; 104 std::string dns_result_order; 105 bool enable_source_maps = false; 106 bool experimental_abortcontroller = false; 107 bool experimental_json_modules = false; 108 bool experimental_modules = false; 109 std::string experimental_specifier_resolution; 110 bool experimental_wasm_modules = false; 111 bool experimental_import_meta_resolve = false; 112 std::string module_type; 113 std::string experimental_policy; 114 std::string experimental_policy_integrity; 115 bool has_policy_integrity_string; 116 bool experimental_repl_await = false; 117 bool experimental_vm_modules = false; 118 bool expose_internals = false; 119 bool frozen_intrinsics = false; 120 int64_t heap_snapshot_near_heap_limit = 0; 121 std::string heap_snapshot_signal; 122 uint64_t max_http_header_size = 16 * 1024; 123 bool deprecation = true; 124 bool force_async_hooks_checks = true; 125 bool allow_native_addons = true; 126 bool warnings = true; 127 bool force_context_aware = false; 128 bool pending_deprecation = false; 129 bool preserve_symlinks = false; 130 bool preserve_symlinks_main = false; 131 bool prof_process = false; 132 #if HAVE_INSPECTOR 133 std::string cpu_prof_dir; 134 static const uint64_t kDefaultCpuProfInterval = 1000; 135 uint64_t cpu_prof_interval = kDefaultCpuProfInterval; 136 std::string cpu_prof_name; 137 bool cpu_prof = false; 138 std::string heap_prof_dir; 139 std::string heap_prof_name; 140 static const uint64_t kDefaultHeapProfInterval = 512 * 1024; 141 uint64_t heap_prof_interval = kDefaultHeapProfInterval; 142 bool heap_prof = false; 143 #endif // HAVE_INSPECTOR 144 std::string redirect_warnings; 145 std::string diagnostic_dir; 146 bool test_udp_no_try_send = false; 147 bool throw_deprecation = false; 148 bool trace_atomics_wait = false; 149 bool trace_deprecation = false; 150 bool trace_exit = false; 151 bool trace_sync_io = false; 152 bool trace_tls = false; 153 bool trace_uncaught = false; 154 bool trace_warnings = false; 155 std::string unhandled_rejections; 156 std::string userland_loader; 157 bool verify_base_objects = 158 #ifdef DEBUG 159 true; 160 #else 161 false; 162 #endif // DEBUG 163 164 bool syntax_check_only = false; 165 bool has_eval_string = false; 166 bool experimental_wasi = false; 167 std::string eval_string; 168 bool print_eval = false; 169 bool force_repl = false; 170 171 bool insecure_http_parser = false; 172 173 bool tls_min_v1_0 = false; 174 bool tls_min_v1_1 = false; 175 bool tls_min_v1_2 = false; 176 bool tls_min_v1_3 = false; 177 bool tls_max_v1_2 = false; 178 bool tls_max_v1_3 = false; 179 std::string tls_keylog; 180 181 std::vector<std::string> preload_modules; 182 183 std::vector<std::string> user_argv; 184 get_debug_options()185 inline DebugOptions* get_debug_options() { return &debug_options_; } debug_options()186 inline const DebugOptions& debug_options() const { return debug_options_; } 187 188 void CheckOptions(std::vector<std::string>* errors) override; 189 190 private: 191 DebugOptions debug_options_; 192 }; 193 194 class PerIsolateOptions : public Options { 195 public: 196 std::shared_ptr<EnvironmentOptions> per_env { new EnvironmentOptions() }; 197 bool track_heap_objects = false; 198 bool node_snapshot = true; 199 bool report_uncaught_exception = false; 200 bool report_on_signal = false; 201 bool experimental_top_level_await = true; 202 std::string report_signal = "SIGUSR2"; 203 inline EnvironmentOptions* get_per_env_options(); 204 void CheckOptions(std::vector<std::string>* errors) override; 205 }; 206 207 class PerProcessOptions : public Options { 208 public: 209 // Options shouldn't be here unless they affect the entire process scope, and 210 // that should avoided when possible. 211 // 212 // When an option is used during process initialization, it does not need 213 // protection, but any use after that will likely require synchronization 214 // using the node::per_process::cli_options_mutex, typically: 215 // 216 // Mutex::ScopedLock lock(node::per_process::cli_options_mutex); 217 std::shared_ptr<PerIsolateOptions> per_isolate { new PerIsolateOptions() }; 218 219 std::string title; 220 std::string trace_event_categories; 221 std::string trace_event_file_pattern = "node_trace.${rotation}.log"; 222 int64_t v8_thread_pool_size = 4; 223 bool zero_fill_all_buffers = false; 224 bool debug_arraybuffer_allocations = false; 225 std::string disable_proto; 226 227 std::vector<std::string> security_reverts; 228 bool print_bash_completion = false; 229 bool print_help = false; 230 bool print_v8_help = false; 231 bool print_version = false; 232 233 #ifdef NODE_HAVE_I18N_SUPPORT 234 std::string icu_data_dir; 235 #endif 236 237 // Per-process because they affect singleton OpenSSL shared library state, 238 // or are used once during process initialization. 239 #if HAVE_OPENSSL 240 std::string openssl_config; 241 bool openssl_shared_config = false; 242 std::string tls_cipher_list = DEFAULT_CIPHER_LIST_CORE; 243 #ifdef NODE_OPENSSL_CERT_STORE 244 bool ssl_openssl_cert_store = true; 245 #else 246 bool ssl_openssl_cert_store = false; 247 #endif 248 bool use_openssl_ca = false; 249 bool use_bundled_ca = false; 250 bool enable_fips_crypto = false; 251 bool force_fips_crypto = false; 252 #endif 253 254 // Per-process because reports can be triggered outside a known V8 context. 255 bool report_on_fatalerror = false; 256 bool report_compact = false; 257 std::string report_directory; 258 std::string report_filename; 259 260 // TODO(addaleax): Some of these could probably be per-Environment. 261 std::string use_largepages = "off"; 262 bool trace_sigint = false; 263 std::vector<std::string> cmdline; 264 265 inline PerIsolateOptions* get_per_isolate_options(); 266 void CheckOptions(std::vector<std::string>* errors) override; 267 }; 268 269 // The actual options parser, as opposed to the structs containing them: 270 271 namespace options_parser { 272 273 HostPort SplitHostPort(const std::string& arg, 274 std::vector<std::string>* errors); 275 void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args); 276 std::string GetBashCompletion(); 277 278 enum OptionType { 279 kNoOp, 280 kV8Option, 281 kBoolean, 282 kInteger, 283 kUInteger, 284 kString, 285 kHostPort, 286 kStringList, 287 }; 288 289 template <typename Options> 290 class OptionsParser { 291 public: 292 virtual ~OptionsParser() = default; 293 294 typedef Options TargetType; 295 296 struct NoOp {}; 297 struct V8Option {}; 298 299 // These methods add a single option to the parser. Optionally, it can be 300 // specified whether the option should be allowed from environment variable 301 // sources (i.e. NODE_OPTIONS). 302 void AddOption(const char* name, 303 const char* help_text, 304 bool Options::* field, 305 OptionEnvvarSettings env_setting = kDisallowedInEnvironment, 306 bool default_is_true = false); 307 void AddOption(const char* name, 308 const char* help_text, 309 uint64_t Options::* field, 310 OptionEnvvarSettings env_setting = kDisallowedInEnvironment); 311 void AddOption(const char* name, 312 const char* help_text, 313 int64_t Options::* field, 314 OptionEnvvarSettings env_setting = kDisallowedInEnvironment); 315 void AddOption(const char* name, 316 const char* help_text, 317 std::string Options::* field, 318 OptionEnvvarSettings env_setting = kDisallowedInEnvironment); 319 void AddOption(const char* name, 320 const char* help_text, 321 std::vector<std::string> Options::* field, 322 OptionEnvvarSettings env_setting = kDisallowedInEnvironment); 323 void AddOption(const char* name, 324 const char* help_text, 325 HostPort Options::* field, 326 OptionEnvvarSettings env_setting = kDisallowedInEnvironment); 327 void AddOption(const char* name, 328 const char* help_text, 329 NoOp no_op_tag, 330 OptionEnvvarSettings env_setting = kDisallowedInEnvironment); 331 void AddOption(const char* name, 332 const char* help_text, 333 V8Option v8_option_tag, 334 OptionEnvvarSettings env_setting = kDisallowedInEnvironment); 335 336 // Adds aliases. An alias can be of the form "--option-a" -> "--option-b", 337 // or have a more complex group expansion, like 338 // "--option-a" -> { "--option-b", "--harmony-foobar", "--eval", "42" } 339 // If `from` has the form "--option-a=", the alias will only be expanded if 340 // the option is presented in that form (i.e. with a '='). 341 // If `from` has the form "--option-a <arg>", the alias will only be expanded 342 // if the option has a non-option argument (not starting with -) following it. 343 void AddAlias(const char* from, const char* to); 344 void AddAlias(const char* from, const std::vector<std::string>& to); 345 void AddAlias(const char* from, 346 const std::initializer_list<std::string>& to); 347 348 // Add implications from some arbitrary option to a boolean one, either 349 // in a way that makes `from` set `to` to true or to false. 350 void Implies(const char* from, const char* to); 351 void ImpliesNot(const char* from, const char* to); 352 353 // Insert options from another options parser into this one, along with 354 // a method that yields the target options type from this parser's options 355 // type. 356 template <typename ChildOptions> 357 void Insert(const OptionsParser<ChildOptions>& child_options_parser, 358 ChildOptions* (Options::* get_child)()); 359 360 // Parse a sequence of options into an options struct, a list of 361 // arguments that were parsed as options, a list of unknown/JS engine options, 362 // and leave the remainder in the input `args` vector. 363 // 364 // For example, an `args` input of 365 // 366 // node --foo --harmony-bar --fizzle=42 -- /path/to/cow moo 367 // 368 // expands as 369 // 370 // - `args` -> { "node", "/path/to/cow", "moo" } 371 // - `exec_args` -> { "--foo", "--harmony-bar", "--fizzle=42" } 372 // - `v8_args` -> `{ "node", "--harmony-bar" } 373 // - `options->foo == true`, `options->fizzle == 42`. 374 // 375 // If `*error` is set, the result of the parsing should be discarded and the 376 // contents of any of the argument vectors should be considered undefined. 377 void Parse(std::vector<std::string>* const args, 378 std::vector<std::string>* const exec_args, 379 std::vector<std::string>* const v8_args, 380 Options* const options, 381 OptionEnvvarSettings required_env_settings, 382 std::vector<std::string>* const errors) const; 383 384 private: 385 // We support the wide variety of different option types by remembering 386 // how to access them, given a certain `Options` struct. 387 388 // Represents a field within `Options`. 389 class BaseOptionField { 390 public: 391 virtual ~BaseOptionField() = default; 392 virtual void* LookupImpl(Options* options) const = 0; 393 394 template <typename T> Lookup(Options * options)395 inline T* Lookup(Options* options) const { 396 return static_cast<T*>(LookupImpl(options)); 397 } 398 }; 399 400 // Represents a field of type T within `Options` that can be looked up 401 // as a C++ member field. 402 template <typename T> 403 class SimpleOptionField : public BaseOptionField { 404 public: SimpleOptionField(T Options::* field)405 explicit SimpleOptionField(T Options::* field) : field_(field) {} LookupImpl(Options * options)406 void* LookupImpl(Options* options) const override { 407 return static_cast<void*>(&(options->*field_)); 408 } 409 410 private: 411 T Options::* field_; 412 }; 413 414 template <typename T> Lookup(std::shared_ptr<BaseOptionField> field,Options * options)415 inline T* Lookup(std::shared_ptr<BaseOptionField> field, 416 Options* options) const { 417 return field->template Lookup<T>(options); 418 } 419 420 // An option consists of: 421 // - A type. 422 // - A way to store/access the property value. 423 // - The information of whether it may occur in an env var or not. 424 struct OptionInfo { 425 OptionType type; 426 std::shared_ptr<BaseOptionField> field; 427 OptionEnvvarSettings env_setting; 428 std::string help_text; 429 bool default_is_true = false; 430 }; 431 432 // An implied option is composed of the information on where to store a 433 // specific boolean value (if another specific option is encountered). 434 struct Implication { 435 OptionType type; 436 std::string name; 437 std::shared_ptr<BaseOptionField> target_field; 438 bool target_value; 439 }; 440 441 // These are helpers that make `Insert()` support properties of other 442 // options structs, if we know how to access them. 443 template <typename OriginalField, typename ChildOptions> 444 static auto Convert( 445 std::shared_ptr<OriginalField> original, 446 ChildOptions* (Options::* get_child)()); 447 template <typename ChildOptions> 448 static auto Convert( 449 typename OptionsParser<ChildOptions>::OptionInfo original, 450 ChildOptions* (Options::* get_child)()); 451 template <typename ChildOptions> 452 static auto Convert( 453 typename OptionsParser<ChildOptions>::Implication original, 454 ChildOptions* (Options::* get_child)()); 455 456 std::unordered_map<std::string, OptionInfo> options_; 457 std::unordered_map<std::string, std::vector<std::string>> aliases_; 458 std::unordered_multimap<std::string, Implication> implications_; 459 460 template <typename OtherOptions> 461 friend class OptionsParser; 462 463 friend void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args); 464 friend std::string GetBashCompletion(); 465 }; 466 467 using StringVector = std::vector<std::string>; 468 template <class OptionsType, class = Options> 469 void Parse( 470 StringVector* const args, StringVector* const exec_args, 471 StringVector* const v8_args, OptionsType* const options, 472 OptionEnvvarSettings required_env_settings, StringVector* const errors); 473 474 } // namespace options_parser 475 476 namespace per_process { 477 478 extern Mutex cli_options_mutex; 479 extern std::shared_ptr<PerProcessOptions> cli_options; 480 481 } // namespace per_process 482 483 void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options); 484 void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options, 485 std::function<std::string(const char*)> opt_getter); 486 487 std::vector<std::string> ParseNodeOptionsEnvVar( 488 const std::string& node_options, std::vector<std::string>* errors); 489 } // namespace node 490 491 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 492 493 #endif // SRC_NODE_OPTIONS_H_ 494