11 const int default_inspector_port = 9229;
13 inline std::string remove_brackets(
const std::string&
host) {
14 if (!host.empty() && host.front() ==
'[' && host.back() ==
']')
15 return host.substr(1, host.size() - 2);
20 int parse_and_validate_port(
const std::string& port) {
23 const long result = strtol(port.c_str(), &endptr, 10);
24 if (errno != 0 || *endptr !=
'\0'||
25 (result != 0 && result < 1024) || result > 65535) {
26 fprintf(stderr,
"Debug port must be 0 or in range 1024 to 65535.\n");
29 return static_cast<int>(result);
32 std::pair<std::string, int> split_host_port(
const std::string& arg) {
35 std::string host = remove_brackets(arg);
36 if (host.length() < arg.length())
39 size_t colon = arg.rfind(
':');
40 if (colon == std::string::npos) {
44 if (c < '0' || c >
'9') {
48 return {
"", parse_and_validate_port(arg)};
51 return std::make_pair(remove_brackets(arg.substr(0, colon)),
52 parse_and_validate_port(arg.substr(colon + 1)));
58 inspector_enabled_(false),
59 deprecated_debug_(false),
60 break_first_line_(false),
61 host_name_(
"127.0.0.1"), port_(-1) { }
64 bool has_argument =
false;
65 std::string option_name;
68 auto pos = option.find(
"=");
69 if (pos == std::string::npos) {
72 option_name = option.substr(0, pos);
73 argument = option.substr(pos + 1);
75 if (argument.length() > 0)
86 if (option_name ==
"--inspect") {
87 inspector_enabled_ =
true;
88 }
else if (option_name ==
"--debug") {
89 deprecated_debug_ =
true;
90 }
else if (option_name ==
"--inspect-brk") {
91 inspector_enabled_ =
true;
92 break_first_line_ =
true;
93 }
else if (option_name ==
"--debug-brk") {
94 break_first_line_ =
true;
95 deprecated_debug_ =
true;
96 }
else if (option_name ==
"--debug-port" ||
97 option_name ==
"--inspect-port") {
99 fprintf(stderr,
"%s: %s requires an argument\n",
100 argv0, option.c_str());
108 if (inspector_enabled_) {
110 "Inspector support is not available with this Node.js build\n");
112 inspector_enabled_ =
false;
118 std::pair<std::string, int> host_port = split_host_port(argument);
119 if (!host_port.first.empty()) {
120 host_name_ = host_port.first;
122 if (host_port.second >= 0) {
123 port_ = host_port.second;
133 port = default_inspector_port;
bool ParseOption(const char *argv0, const std::string &option)