-
Notifications
You must be signed in to change notification settings - Fork 30.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ce1fff
commit 3f46529
Showing
18 changed files
with
423 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#include "node_rc.h" | ||
#include "debug_utils-inl.h" | ||
#include "env-inl.h" | ||
#include "node_errors.h" | ||
#include "node_file.h" | ||
#include "node_internals.h" | ||
#include "simdjson.h" | ||
|
||
#include <functional> | ||
#include <map> | ||
#include <string> | ||
|
||
namespace node { | ||
|
||
std::optional<ConfigReader::ConfigV0> ConfigReader::ParseConfigV0( | ||
simdjson::ondemand::object* main_object) { | ||
ConfigReader::ConfigV0 config; | ||
config.version = 0; | ||
|
||
if (auto value = (*main_object)["experimental_transform_types"]; | ||
value.error() != simdjson::NO_SUCH_FIELD) { | ||
if (value.get_bool().get(config.experimental_transform_types)) { | ||
FPrintF(stderr, "Invalid value for experimental_transform_types\n"); | ||
return std::nullopt; | ||
} | ||
} | ||
|
||
return config; | ||
} | ||
|
||
ConfigReader::ConfigReader() { | ||
config_parsers_[0] = &ConfigReader::ParseConfigV0; | ||
} | ||
|
||
std::optional<std::string> ConfigReader::GetDataFromArgs( | ||
const std::vector<std::string>& args) { | ||
constexpr std::string_view flag = "--experimental-config-file"; | ||
|
||
for (auto it = args.begin(); it != args.end(); ++it) { | ||
if (*it == flag) { | ||
// Case: "--experimental-config-file foo" | ||
if (auto next = std::next(it); next != args.end()) { | ||
return *next; | ||
} | ||
} else if (it->starts_with(flag)) { | ||
// Case: "--experimental-config-file=foo" | ||
if (it->size() > flag.size() && (*it)[flag.size()] == '=') { | ||
return it->substr(flag.size() + 1); | ||
} | ||
} | ||
} | ||
|
||
return std::nullopt; | ||
} | ||
|
||
ConfigReader::ParseResult ConfigReader::ParseConfig( | ||
const std::string& config_path) { | ||
std::string file_content; | ||
// Read the configuration file | ||
int r = ReadFileSync(&file_content, config_path.c_str()); | ||
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.c_str()); | ||
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.c_str()); | ||
} else { | ||
FPrintF(stderr, "Can't parse %s\n", config_path.c_str()); | ||
} | ||
return ParseResult::InvalidContent; | ||
} | ||
|
||
// If json object doesn't have "version" field, throw an error. | ||
simdjson::ondemand::number version_field; | ||
if (main_object["version"].get_number().get(version_field)) { | ||
FPrintF(stderr, | ||
"Can't find numeric \"version\" field in %s\n", | ||
config_path.c_str()); | ||
return ParseResult::InvalidVersion; | ||
} | ||
|
||
// Check if version is an integer | ||
if (!version_field.is_int64()) { | ||
FPrintF( | ||
stderr, "Version field is not an integer in %s\n", config_path.c_str()); | ||
return ParseResult::InvalidVersion; | ||
} | ||
|
||
uint64_t version = version_field.get_int64(); | ||
if (version < 0 || version >= config_parsers_.size()) { | ||
FPrintF(stderr, "Version %" PRIu64 " does not exist\n", version); | ||
return ParseResult::InvalidVersion; | ||
} | ||
|
||
// Get the config parser for the specific version | ||
auto config_parser = config_parsers_.at(version_field.get_int64()); | ||
auto config = config_parser(&main_object); | ||
if (!config.has_value()) { | ||
return ParseResult::InvalidContent; | ||
} | ||
|
||
// save the config for later | ||
config_ = config.value(); | ||
return ParseResult::Valid; | ||
} | ||
|
||
void ConfigReader::AssignNodeOptions(std::string* node_options) { | ||
if (ConfigV0* config = std::get_if<ConfigReader::ConfigV0>(&config_)) { | ||
std::string result = ""; | ||
if (config->experimental_transform_types) { | ||
result += "--experimental-transform-types"; | ||
} | ||
*node_options = result; | ||
return; | ||
} | ||
} | ||
} // namespace node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef SRC_NODE_RC_H_ | ||
#define SRC_NODE_RC_H_ | ||
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include <map> | ||
#include <string> | ||
#include <variant> | ||
#include "simdjson.h" | ||
#include "util-inl.h" | ||
|
||
namespace node { | ||
|
||
class ConfigReader { | ||
public: | ||
enum ParseResult { Valid, FileError, InvalidContent, InvalidVersion }; | ||
struct ConfigV0 { | ||
int64_t version; | ||
bool experimental_transform_types; | ||
}; | ||
using Config = std::variant<ConfigV0>; | ||
using ConfigParser = | ||
std::function<std::optional<Config>(simdjson::ondemand::object*)>; | ||
|
||
ConfigReader(); | ||
|
||
ConfigReader::ParseResult ParseConfig(const std::string& config_path); | ||
|
||
std::optional<std::string> GetDataFromArgs( | ||
const std::vector<std::string>& args); | ||
|
||
void AssignNodeOptions(std::string* node_options); | ||
|
||
private: | ||
simdjson::ondemand::parser json_parser_; | ||
|
||
ConfigReader::Config config_; | ||
|
||
static std::optional<ConfigReader::ConfigV0> ParseConfigV0( | ||
simdjson::ondemand::object* main_object); | ||
|
||
std::array<ConfigParser, 1> config_parsers_; | ||
}; | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_NODE_RC_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"version": 9999 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"version": "foo" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"version": 0, | ||
"experimental_transform_types": true, | ||
"experimental_transform_types": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"version": 0, | ||
"version": 9999 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"version": 0, | ||
"experimental_transform_types": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"version": 0 | ||
} |
Oops, something went wrong.