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