|
| 1 | +#include "node_rc.h" |
| 2 | +#include "debug_utils-inl.h" |
| 3 | +#include "env-inl.h" |
| 4 | +#include "node_errors.h" |
| 5 | +#include "node_file.h" |
| 6 | +#include "node_internals.h" |
| 7 | +#include "simdjson.h" |
| 8 | + |
| 9 | +#include <functional> |
| 10 | +#include <map> |
| 11 | +#include <string> |
| 12 | + |
| 13 | +namespace node { |
| 14 | + |
| 15 | +std::optional<std::string> ConfigReader::GetDataFromArgs( |
| 16 | + const std::vector<std::string>& args) { |
| 17 | + constexpr std::string_view flag = "--experimental-config-file"; |
| 18 | + |
| 19 | + for (auto it = args.begin(); it != args.end(); ++it) { |
| 20 | + if (*it == flag) { |
| 21 | + // Case: "--experimental-config-file foo" |
| 22 | + if (auto next = std::next(it); next != args.end()) { |
| 23 | + return *next; |
| 24 | + } |
| 25 | + } else if (it->starts_with(flag)) { |
| 26 | + // Case: "--experimental-config-file=foo" |
| 27 | + if (it->size() > flag.size() && (*it)[flag.size()] == '=') { |
| 28 | + return it->substr(flag.size() + 1); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return std::nullopt; |
| 34 | +} |
| 35 | + |
| 36 | +ConfigReader::ParseResult ConfigReader::ParseConfig( |
| 37 | + const std::string& config_path) { |
| 38 | + std::string file_content; |
| 39 | + // Read the configuration file |
| 40 | + int r = ReadFileSync(&file_content, config_path.c_str()); |
| 41 | + if (r != 0) { |
| 42 | + const char* err = uv_strerror(r); |
| 43 | + FPrintF( |
| 44 | + stderr, "Cannot read configuration from %s: %s\n", config_path, err); |
| 45 | + return ParseResult::FileError; |
| 46 | + } |
| 47 | + |
| 48 | + // Parse the configuration file |
| 49 | + simdjson::ondemand::parser json_parser; |
| 50 | + simdjson::ondemand::document document; |
| 51 | + if (json_parser.iterate(file_content).get(document)) { |
| 52 | + FPrintF(stderr, "Can't parse %s\n", config_path.c_str()); |
| 53 | + return ParseResult::InvalidContent; |
| 54 | + } |
| 55 | + |
| 56 | + simdjson::ondemand::object main_object; |
| 57 | + // If document is not an object, throw an error. |
| 58 | + if (auto root_error = document.get_object().get(main_object)) { |
| 59 | + if (root_error == simdjson::error_code::INCORRECT_TYPE) { |
| 60 | + FPrintF(stderr, |
| 61 | + "Root value unexpected not an object for %s\n\n", |
| 62 | + config_path.c_str()); |
| 63 | + } else { |
| 64 | + FPrintF(stderr, "Can't parse %s\n", config_path.c_str()); |
| 65 | + } |
| 66 | + return ParseResult::InvalidContent; |
| 67 | + } |
| 68 | + |
| 69 | + ConfigReader::Config config; |
| 70 | + simdjson::ondemand::value ondemand_value; |
| 71 | + simdjson::ondemand::raw_json_string key; |
| 72 | + |
| 73 | + for (auto field : main_object) { |
| 74 | + if (field.key().get(key) || field.value().get(ondemand_value)) { |
| 75 | + return ParseResult::InvalidContent; |
| 76 | + } |
| 77 | + if (key == "experimental_transform_types") { |
| 78 | + if (ondemand_value.get_bool().get(config.experimental_transform_types)) { |
| 79 | + FPrintF(stderr, "Invalid value for experimental_transform_types\n"); |
| 80 | + return ParseResult::InvalidContent; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (key == "import") { |
| 85 | + simdjson::ondemand::array raw_imports; |
| 86 | + if (ondemand_value.get_array().get(raw_imports)) { |
| 87 | + FPrintF(stderr, "Invalid value for import\n"); |
| 88 | + return ParseResult::InvalidContent; |
| 89 | + } |
| 90 | + for (auto raw_import : raw_imports) { |
| 91 | + std::string_view import; |
| 92 | + if (raw_import.get_string(import)) { |
| 93 | + FPrintF(stderr, "Invalid value for import\n"); |
| 94 | + return ParseResult::InvalidContent; |
| 95 | + } |
| 96 | + config.import.push_back(std::string(import)); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + config_ = config; |
| 102 | + return ParseResult::Valid; |
| 103 | +} |
| 104 | + |
| 105 | +void ConfigReader::AssignNodeOptions(std::string* node_options) { |
| 106 | + std::string result = ""; |
| 107 | + if (config_.experimental_transform_types) { |
| 108 | + result += "--experimental-transform-types"; |
| 109 | + } |
| 110 | + |
| 111 | + if(config_.import.size() > 0) { |
| 112 | + for (const auto& import : config_.import) { |
| 113 | + result += " --import=" + import; |
| 114 | + } |
| 115 | + } |
| 116 | + *node_options = result; |
| 117 | + return; |
| 118 | +} |
| 119 | +} // namespace node |
0 commit comments