-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
/
Copy pathnode_config_file.cc
224 lines (207 loc) · 7.64 KB
/
node_config_file.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "node_config_file.h"
#include "debug_utils-inl.h"
#include "simdjson.h"
#include <string>
namespace node {
std::optional<std::string_view> ConfigReader::GetDataFromArgs(
const std::vector<std::string>& args) {
constexpr std::string_view flag_path = "--experimental-config-file";
constexpr std::string_view default_file =
"--experimental-default-config-file";
bool has_default_config_file = false;
for (auto it = args.begin(); it != args.end(); ++it) {
if (*it == flag_path) {
// Case: "--experimental-config-file foo"
if (auto next = std::next(it); next != args.end()) {
return *next;
}
} else if (it->starts_with(flag_path)) {
// Case: "--experimental-config-file=foo"
if (it->size() > flag_path.size() && (*it)[flag_path.size()] == '=') {
return it->substr(flag_path.size() + 1);
}
} else if (*it == default_file || it->starts_with(default_file)) {
has_default_config_file = true;
}
}
if (has_default_config_file) {
return "node.config.json";
}
return std::nullopt;
}
ParseResult ConfigReader::ParseNodeOptions(
simdjson::ondemand::object* node_options_object) {
auto env_options_map = options_parser::MapEnvOptionsFlagInputType();
simdjson::ondemand::value ondemand_value;
std::string_view key;
for (auto field : *node_options_object) {
if (field.unescaped_key().get(key) || field.value().get(ondemand_value)) {
return ParseResult::InvalidContent;
}
// The key needs to match the CLI option
std::string prefix = "--";
auto it = env_options_map.find(prefix.append(key));
if (it != env_options_map.end()) {
switch (it->second) {
case options_parser::OptionType::kBoolean: {
bool result;
if (ondemand_value.get_bool().get(result)) {
FPrintF(stderr, "Invalid value for %s\n", it->first.c_str());
return ParseResult::InvalidContent;
}
node_options_.push_back(it->first + "=" +
(result ? "true" : "false"));
break;
}
// String array can allow both string and array types
case options_parser::OptionType::kStringList: {
simdjson::ondemand::json_type field_type;
if (ondemand_value.type().get(field_type)) {
return ParseResult::InvalidContent;
}
switch (field_type) {
case simdjson::ondemand::json_type::array: {
std::vector<std::string> result;
simdjson::ondemand::array raw_imports;
if (ondemand_value.get_array().get(raw_imports)) {
FPrintF(stderr, "Invalid value for %s\n", it->first.c_str());
return ParseResult::InvalidContent;
}
for (auto raw_import : raw_imports) {
std::string_view import;
if (raw_import.get_string(import)) {
FPrintF(stderr, "Invalid value for %s\n", it->first.c_str());
return ParseResult::InvalidContent;
}
node_options_.push_back(it->first + "=" + std::string(import));
}
break;
}
case simdjson::ondemand::json_type::string: {
std::string result;
if (ondemand_value.get_string(result)) {
FPrintF(stderr, "Invalid value for %s\n", it->first.c_str());
return ParseResult::InvalidContent;
}
node_options_.push_back(it->first + "=" + result);
break;
}
default:
FPrintF(stderr, "Invalid value for %s\n", it->first.c_str());
return ParseResult::InvalidContent;
}
break;
}
case options_parser::OptionType::kString: {
std::string result;
if (ondemand_value.get_string(result)) {
FPrintF(stderr, "Invalid value for %s\n", it->first.c_str());
return ParseResult::InvalidContent;
}
node_options_.push_back(it->first + "=" + result);
break;
}
case options_parser::OptionType::kInteger: {
int64_t result;
if (ondemand_value.get_int64().get(result)) {
FPrintF(stderr, "Invalid value for %s\n", it->first.c_str());
return ParseResult::InvalidContent;
}
node_options_.push_back(it->first + "=" + std::to_string(result));
break;
}
case options_parser::OptionType::kHostPort:
case options_parser::OptionType::kUInteger: {
uint64_t result;
if (ondemand_value.get_uint64().get(result)) {
FPrintF(stderr, "Invalid value for %s\n", it->first.c_str());
return ParseResult::InvalidContent;
}
node_options_.push_back(it->first + "=" + std::to_string(result));
break;
}
case options_parser::OptionType::kNoOp: {
FPrintF(stderr,
"No-op flag %s is currently not supported\n",
it->first.c_str());
return ParseResult::InvalidContent;
break;
}
case options_parser::OptionType::kV8Option: {
FPrintF(stderr,
"V8 flag %s is currently not supported\n",
it->first.c_str());
return ParseResult::InvalidContent;
}
default:
UNREACHABLE();
}
} else {
FPrintF(stderr, "Unknown or not allowed option %s\n", key.data());
return ParseResult::InvalidContent;
}
}
return ParseResult::Valid;
}
ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
std::string file_content;
// Read the configuration file
int r = ReadFileSync(&file_content, config_path.data());
if (r != 0) {
const char* err = uv_strerror(r);
FPrintF(
stderr, "Cannot read configuration from %s: %s\n", config_path, err);
return ParseResult::FileError;
}
// Parse the configuration file
simdjson::ondemand::parser json_parser;
simdjson::ondemand::document document;
if (json_parser.iterate(file_content).get(document)) {
FPrintF(stderr, "Can't parse %s\n", config_path.data());
return ParseResult::InvalidContent;
}
simdjson::ondemand::object main_object;
// If document is not an object, throw an error.
if (auto root_error = document.get_object().get(main_object)) {
if (root_error == simdjson::error_code::INCORRECT_TYPE) {
FPrintF(stderr,
"Root value unexpected not an object for %s\n\n",
config_path.data());
} else {
FPrintF(stderr, "Can't parse %s\n", config_path.data());
}
return ParseResult::InvalidContent;
}
simdjson::ondemand::object node_options_object;
// If "nodeOptions" is an object, parse it
if (auto node_options_error =
main_object["nodeOptions"].get_object().get(node_options_object)) {
if (node_options_error != simdjson::error_code::NO_SUCH_FIELD) {
FPrintF(stderr,
"\"nodeOptions\" value unexpected for %s\n\n",
config_path.data());
return ParseResult::InvalidContent;
}
} else {
return ParseNodeOptions(&node_options_object);
}
return ParseResult::Valid;
}
std::string ConfigReader::AssignNodeOptions() {
if (node_options_.empty()) {
return "";
} else {
DCHECK_GT(node_options_.size(), 0);
std::string acc;
acc.reserve(node_options_.size() * 2);
for (size_t i = 0; i < node_options_.size(); ++i) {
// The space is necessary at the beginning of the string
acc += " " + node_options_[i];
}
return acc;
}
}
size_t ConfigReader::GetFlagsSize() {
return node_options_.size();
}
} // namespace node