|
| 1 | +#include "node_dotenv.h" |
| 2 | +#include "env-inl.h" |
| 3 | +#include "node_file.h" |
| 4 | +#include "uv.h" |
| 5 | + |
| 6 | +namespace node { |
| 7 | + |
| 8 | +using v8::NewStringType; |
| 9 | +using v8::String; |
| 10 | + |
| 11 | +std::optional<std::string> Dotenv::GetPathFromArgs( |
| 12 | + const std::vector<std::string>& args) { |
| 13 | + std::string_view flag = "--env-file"; |
| 14 | + // Match the last `--env-file` |
| 15 | + // This is required to imitate the default behavior of Node.js CLI argument |
| 16 | + // matching. |
| 17 | + auto path = |
| 18 | + std::find_if(args.rbegin(), args.rend(), [&flag](const std::string& arg) { |
| 19 | + return strncmp(arg.c_str(), flag.data(), flag.size()) == 0; |
| 20 | + }); |
| 21 | + |
| 22 | + if (path == args.rend()) { |
| 23 | + return std::nullopt; |
| 24 | + } |
| 25 | + |
| 26 | + auto equal_char = path->find('='); |
| 27 | + |
| 28 | + if (equal_char != std::string::npos) { |
| 29 | + return path->substr(equal_char + 1); |
| 30 | + } |
| 31 | + |
| 32 | + auto next_arg = std::prev(path); |
| 33 | + |
| 34 | + if (next_arg == args.rend()) { |
| 35 | + return std::nullopt; |
| 36 | + } |
| 37 | + |
| 38 | + return *next_arg; |
| 39 | +} |
| 40 | + |
| 41 | +void Dotenv::SetEnvironment(node::Environment* env) { |
| 42 | + if (store_.empty()) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + auto isolate = env->isolate(); |
| 47 | + |
| 48 | + for (const auto& entry : store_) { |
| 49 | + auto key = entry.first; |
| 50 | + auto value = entry.second; |
| 51 | + env->env_vars()->Set( |
| 52 | + isolate, |
| 53 | + v8::String::NewFromUtf8( |
| 54 | + isolate, key.data(), NewStringType::kNormal, key.size()) |
| 55 | + .ToLocalChecked(), |
| 56 | + v8::String::NewFromUtf8( |
| 57 | + isolate, value.data(), NewStringType::kNormal, value.size()) |
| 58 | + .ToLocalChecked()); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +void Dotenv::ParsePath(const std::string_view path) { |
| 63 | + uv_fs_t req; |
| 64 | + auto defer_req_cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); }); |
| 65 | + |
| 66 | + uv_file file = uv_fs_open(nullptr, &req, path.data(), 0, 438, nullptr); |
| 67 | + if (req.result < 0) { |
| 68 | + // req will be cleaned up by scope leave. |
| 69 | + return; |
| 70 | + } |
| 71 | + uv_fs_req_cleanup(&req); |
| 72 | + |
| 73 | + auto defer_close = OnScopeLeave([file]() { |
| 74 | + uv_fs_t close_req; |
| 75 | + CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr)); |
| 76 | + uv_fs_req_cleanup(&close_req); |
| 77 | + }); |
| 78 | + |
| 79 | + std::string result{}; |
| 80 | + char buffer[8192]; |
| 81 | + uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer)); |
| 82 | + |
| 83 | + while (true) { |
| 84 | + auto r = uv_fs_read(nullptr, &req, file, &buf, 1, -1, nullptr); |
| 85 | + if (req.result < 0) { |
| 86 | + // req will be cleaned up by scope leave. |
| 87 | + return; |
| 88 | + } |
| 89 | + uv_fs_req_cleanup(&req); |
| 90 | + if (r <= 0) { |
| 91 | + break; |
| 92 | + } |
| 93 | + result.append(buf.base, r); |
| 94 | + } |
| 95 | + |
| 96 | + using std::string_view_literals::operator""sv; |
| 97 | + auto lines = SplitString(result, "\n"sv); |
| 98 | + |
| 99 | + for (const auto& line : lines) { |
| 100 | + ParseLine(line); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) { |
| 105 | + auto match = store_.find("NODE_OPTIONS"); |
| 106 | + |
| 107 | + if (match != store_.end()) { |
| 108 | + *node_options = match->second; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +void Dotenv::ParseLine(const std::string_view line) { |
| 113 | + auto equal_index = line.find('='); |
| 114 | + |
| 115 | + if (equal_index == std::string_view::npos) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + auto key = line.substr(0, equal_index); |
| 120 | + |
| 121 | + // Remove leading and trailing space characters from key. |
| 122 | + while (!key.empty() && std::isspace(key.front())) key.remove_prefix(1); |
| 123 | + while (!key.empty() && std::isspace(key.back())) key.remove_suffix(1); |
| 124 | + |
| 125 | + // Omit lines with comments |
| 126 | + if (key.front() == '#' || key.empty()) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + auto value = std::string(line.substr(equal_index + 1)); |
| 131 | + |
| 132 | + // Might start and end with `"' characters. |
| 133 | + auto quotation_index = value.find_first_of("`\"'"); |
| 134 | + |
| 135 | + if (quotation_index == 0) { |
| 136 | + auto quote_character = value[quotation_index]; |
| 137 | + value.erase(0, 1); |
| 138 | + |
| 139 | + auto end_quotation_index = value.find_last_of(quote_character); |
| 140 | + |
| 141 | + // We couldn't find the closing quotation character. Terminate. |
| 142 | + if (end_quotation_index == std::string::npos) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + value.erase(end_quotation_index); |
| 147 | + } else { |
| 148 | + auto hash_index = value.find('#'); |
| 149 | + |
| 150 | + // Remove any inline comments |
| 151 | + if (hash_index != std::string::npos) { |
| 152 | + value.erase(hash_index); |
| 153 | + } |
| 154 | + |
| 155 | + // Remove any leading/trailing spaces from unquoted values. |
| 156 | + while (!value.empty() && std::isspace(value.front())) value.erase(0, 1); |
| 157 | + while (!value.empty() && std::isspace(value.back())) |
| 158 | + value.erase(value.size() - 1); |
| 159 | + } |
| 160 | + |
| 161 | + store_.emplace(key, value); |
| 162 | +} |
| 163 | + |
| 164 | +} // namespace node |
0 commit comments