|
| 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<ConfigReader::ConfigV0> ConfigReader::ParseConfigV0( |
| 16 | + simdjson::ondemand::object* main_object) { |
| 17 | + ConfigReader::ConfigV0 config; |
| 18 | + config.version = 0; |
| 19 | + |
| 20 | + if (auto value = (*main_object)["experimental_transform_types"]; |
| 21 | + value.error() != simdjson::NO_SUCH_FIELD) { |
| 22 | + if (value.get_bool().get(config.experimental_transform_types)) { |
| 23 | + FPrintF(stderr, "Invalid value for experimental_transform_types\n"); |
| 24 | + return std::nullopt; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return config; |
| 29 | +} |
| 30 | + |
| 31 | +ConfigReader::ConfigReader() { |
| 32 | + config_parsers_[0] = &ConfigReader::ParseConfigV0; |
| 33 | +} |
| 34 | + |
| 35 | +std::optional<std::string> ConfigReader::GetDataFromArgs( |
| 36 | + const std::vector<std::string>& args) { |
| 37 | + constexpr std::string_view flag = "--experimental-config-file"; |
| 38 | + |
| 39 | + for (auto it = args.begin(); it != args.end(); ++it) { |
| 40 | + if (*it == flag) { |
| 41 | + // Case: "--experimental-config-file foo" |
| 42 | + if (auto next = std::next(it); next != args.end()) { |
| 43 | + return *next; |
| 44 | + } |
| 45 | + } else if (it->starts_with(flag)) { |
| 46 | + // Case: "--experimental-config-file=foo" |
| 47 | + if (it->size() > flag.size() && (*it)[flag.size()] == '=') { |
| 48 | + return it->substr(flag.size() + 1); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return std::nullopt; |
| 54 | +} |
| 55 | + |
| 56 | +ConfigReader::ParseResult ConfigReader::ParseConfig( |
| 57 | + const std::string& config_path) { |
| 58 | + std::string file_content; |
| 59 | + // Read the configuration file |
| 60 | + int r = ReadFileSync(&file_content, config_path.c_str()); |
| 61 | + if (r != 0) { |
| 62 | + const char* err = uv_strerror(r); |
| 63 | + FPrintF( |
| 64 | + stderr, "Cannot read configuration from %s: %s\n", config_path, err); |
| 65 | + return ParseResult::FileError; |
| 66 | + } |
| 67 | + |
| 68 | + // Parse the configuration file |
| 69 | + simdjson::ondemand::parser json_parser; |
| 70 | + simdjson::ondemand::document document; |
| 71 | + if (json_parser.iterate(file_content).get(document)) { |
| 72 | + FPrintF(stderr, "Can't parse %s\n", config_path.c_str()); |
| 73 | + return ParseResult::InvalidContent; |
| 74 | + } |
| 75 | + |
| 76 | + simdjson::ondemand::object main_object; |
| 77 | + // If document is not an object, throw an error. |
| 78 | + if (auto root_error = document.get_object().get(main_object)) { |
| 79 | + if (root_error == simdjson::error_code::INCORRECT_TYPE) { |
| 80 | + FPrintF(stderr, |
| 81 | + "Root value unexpected not an object for %s\n\n", |
| 82 | + config_path.c_str()); |
| 83 | + } else { |
| 84 | + FPrintF(stderr, "Can't parse %s\n", config_path.c_str()); |
| 85 | + } |
| 86 | + return ParseResult::InvalidContent; |
| 87 | + } |
| 88 | + |
| 89 | + // If json object doesn't have "version" field, throw an error. |
| 90 | + simdjson::ondemand::number version_field; |
| 91 | + if (main_object["version"].get_number().get(version_field)) { |
| 92 | + FPrintF(stderr, |
| 93 | + "Can't find numeric \"version\" field in %s\n", |
| 94 | + config_path.c_str()); |
| 95 | + return ParseResult::InvalidVersion; |
| 96 | + } |
| 97 | + |
| 98 | + // Check if version is an integer |
| 99 | + if (!version_field.is_int64()) { |
| 100 | + FPrintF( |
| 101 | + stderr, "Version field is not an integer in %s\n", config_path.c_str()); |
| 102 | + return ParseResult::InvalidVersion; |
| 103 | + } |
| 104 | + |
| 105 | + uint64_t version = version_field.get_int64(); |
| 106 | + if (version < 0 || version >= config_parsers_.size()) { |
| 107 | + FPrintF(stderr, "Version %" PRIu64 " does not exist\n", version); |
| 108 | + return ParseResult::InvalidVersion; |
| 109 | + } |
| 110 | + |
| 111 | + // Get the config parser for the specific version |
| 112 | + auto config_parser = config_parsers_.at(version_field.get_int64()); |
| 113 | + auto config = config_parser(&main_object); |
| 114 | + if (!config.has_value()) { |
| 115 | + return ParseResult::InvalidContent; |
| 116 | + } |
| 117 | + |
| 118 | + // save the config for later |
| 119 | + config_ = config.value(); |
| 120 | + return ParseResult::Valid; |
| 121 | +} |
| 122 | + |
| 123 | +void ConfigReader::AssignNodeOptions(std::string* node_options) { |
| 124 | + if (ConfigV0* config = std::get_if<ConfigReader::ConfigV0>(&config_)) { |
| 125 | + std::string result = ""; |
| 126 | + if (config->experimental_transform_types) { |
| 127 | + result += "--experimental-transform-types"; |
| 128 | + } |
| 129 | + *node_options = result; |
| 130 | + return; |
| 131 | + } |
| 132 | +} |
| 133 | +} // namespace node |
0 commit comments