-
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 5342c22
Showing
18 changed files
with
418 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "object", | ||
"properties": { | ||
"experimental_transform_types": { | ||
"type": "boolean" | ||
}, | ||
"import": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
} | ||
}, | ||
"additionalProperties": 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
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,127 @@ | ||
#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 <numeric> | ||
#include <string> | ||
|
||
namespace node { | ||
|
||
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; | ||
} | ||
|
||
ConfigReader::Config config; | ||
simdjson::ondemand::value ondemand_value; | ||
simdjson::ondemand::raw_json_string key; | ||
|
||
for (auto field : main_object) { | ||
if (field.key().get(key) || field.value().get(ondemand_value)) { | ||
return ParseResult::InvalidContent; | ||
} | ||
if (key == "experimental_transform_types") { | ||
if (ondemand_value.get_bool().get(config.experimental_transform_types)) { | ||
FPrintF(stderr, "Invalid value for experimental_transform_types\n"); | ||
return ParseResult::InvalidContent; | ||
} | ||
} | ||
|
||
if (key == "import") { | ||
simdjson::ondemand::array raw_imports; | ||
if (ondemand_value.get_array().get(raw_imports)) { | ||
FPrintF(stderr, "Invalid value for import\n"); | ||
return ParseResult::InvalidContent; | ||
} | ||
for (auto raw_import : raw_imports) { | ||
std::string_view import; | ||
if (raw_import.get_string(import)) { | ||
FPrintF(stderr, "Invalid value for import\n"); | ||
return ParseResult::InvalidContent; | ||
} | ||
config.import.push_back(std::string(import)); | ||
} | ||
} | ||
} | ||
|
||
config_ = config; | ||
return ParseResult::Valid; | ||
} | ||
|
||
void ConfigReader::AssignNodeOptions(std::string* node_options) { | ||
std::vector<std::string> result; | ||
if (config_.experimental_transform_types) { | ||
result.push_back("--experimental-transform-types"); | ||
} | ||
|
||
if (config_.import.size() > 0) { | ||
for (const auto& import : config_.import) { | ||
result.push_back("--import=" + import); | ||
} | ||
} | ||
if (result.empty()) { | ||
*node_options = ""; | ||
} else { | ||
*node_options = result[0]; | ||
for (size_t i = 1; i < result.size(); ++i) { | ||
*node_options += " " + result[i]; | ||
} | ||
} | ||
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,36 @@ | ||
#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 }; | ||
struct Config { | ||
bool experimental_transform_types; | ||
std::vector<std::string> import; | ||
}; | ||
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: | ||
ConfigReader::Config config_; | ||
}; | ||
|
||
} // 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 @@ | ||
NODE_OPTIONS="--no-experimental-strip-types" |
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 @@ | ||
{ | ||
"import": [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,7 @@ | ||
{ | ||
"import": [ | ||
"./test/fixtures/printA.js", | ||
"./test/fixtures/printB.js", | ||
"./test/fixtures/printC.js" | ||
] | ||
} |
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 @@ | ||
{ | ||
"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,3 @@ | ||
{ | ||
"experimental_transform_types": true | ||
} |
Oops, something went wrong.