Skip to content

Commit

Permalink
src: support multi-line values for .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasShabiCS committed Dec 26, 2023
1 parent 89ddc98 commit 7089fd7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 56 deletions.
95 changes: 40 additions & 55 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "node_dotenv.h"
#include <regex> // NOLINT(build/c++11)
#include "env-inl.h"
#include "node_file.h"
#include "uv.h"
Expand All @@ -8,6 +9,17 @@ namespace node {
using v8::NewStringType;
using v8::String;

/**
* The inspiration for this implementation comes from the original dotenv code,
* available at https://github.com/motdotla/dotenv/blob/master/lib/main.js#L12
*/

std::regex LINE(
"(?:^|^)\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|"
"[^'])*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\\r\\n]+)?"
"\\s*(?:#.*)?(?:$|$)",
std::regex::multiline);

std::vector<std::string> Dotenv::GetPathFromArgs(
const std::vector<std::string>& args) {
const auto find_match = [](const std::string& arg) {
Expand Down Expand Up @@ -98,73 +110,46 @@ bool Dotenv::ParsePath(const std::string_view path) {
result.append(buf.base, r);
}

using std::string_view_literals::operator""sv;
auto lines = SplitString(result, "\n"sv);
// Convert line breaks to the same format
std::string lines = result;
std::regex_replace(lines, std::regex("\r\n?"), "\n");

for (const auto& line : lines) {
ParseLine(line);
}
return true;
}
std::smatch match;
while (std::regex_search(lines, match, LINE)) {
const std::string key = match[1].str();

void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {
auto match = store_.find("NODE_OPTIONS");
// Default undefined or null to an empty string
std::string value = match[2].str();

if (match != store_.end()) {
*node_options = match->second;
}
}
// Remove whitespace
value = std::regex_replace(value, std::regex("^\\s+|\\s+$"), "");

void Dotenv::ParseLine(const std::string_view line) {
auto equal_index = line.find('=');
// Check if double-quoted
const char maybeQuote = value[0];

if (equal_index == std::string_view::npos) {
return;
}

auto key = line.substr(0, equal_index);
// Remove surrounding quotes
value =
std::regex_replace(value, std::regex("^(['\"`])([\\s\\S]*)\\1$"), "$2");

// Remove leading and trailing space characters from key.
while (!key.empty() && std::isspace(key.front())) key.remove_prefix(1);
while (!key.empty() && std::isspace(key.back())) key.remove_suffix(1);
// Expand newlines if double quoted
if (maybeQuote == '"') {
value = std::regex_replace(value, std::regex("\\\\n"), "\n");
value = std::regex_replace(value, std::regex("\\\\r"), "\r");
}

// Omit lines with comments
if (key.front() == '#' || key.empty()) {
return;
store_.insert_or_assign(std::string(key), value);
lines = match.suffix();
}

auto value = std::string(line.substr(equal_index + 1));

// Might start and end with `"' characters.
auto quotation_index = value.find_first_of("`\"'");

if (quotation_index == 0) {
auto quote_character = value[quotation_index];
value.erase(0, 1);

auto end_quotation_index = value.find_last_of(quote_character);

// We couldn't find the closing quotation character. Terminate.
if (end_quotation_index == std::string::npos) {
return;
}

value.erase(end_quotation_index);
} else {
auto hash_index = value.find('#');
return true;
}

// Remove any inline comments
if (hash_index != std::string::npos) {
value.erase(hash_index);
}
void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {
auto match = store_.find("NODE_OPTIONS");

// Remove any leading/trailing spaces from unquoted values.
while (!value.empty() && std::isspace(value.front())) value.erase(0, 1);
while (!value.empty() && std::isspace(value.back()))
value.erase(value.size() - 1);
if (match != store_.end()) {
*node_options = match->second;
}

store_.insert_or_assign(std::string(key), value);
}

} // namespace node
1 change: 0 additions & 1 deletion src/node_dotenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Dotenv {
const std::vector<std::string>& args);

private:
void ParseLine(const std::string_view line);
std::map<std::string, std::string> store_;
};

Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/dotenv/valid.env
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ RETAIN_INNER_QUOTES_AS_BACKTICKS=`{"foo": "bar's"}`
TRIM_SPACE_FROM_UNQUOTED= some spaced out string
EMAIL=[email protected]
SPACED_KEY = parsed

MULTI_DOUBLE_QUOTED="THIS
IS
A
MULTILINE
STRING"

MULTI_SINGLE_QUOTED='THIS
IS
A
MULTILINE
STRING'

MULTI_BACKTICKED=`THIS
IS
A
"MULTILINE'S"
STRING`
MULTI_NOT_VALID=THIS
IS NOT MULTILINE
5 changes: 5 additions & 0 deletions test/parallel/test-dotenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ assert.strictEqual(process.env.TRIM_SPACE_FROM_UNQUOTED, 'some spaced out string
assert.strictEqual(process.env.EMAIL, '[email protected]');
// Parses keys and values surrounded by spaces
assert.strictEqual(process.env.SPACED_KEY, 'parsed');
// Test multiple-line value
assert.strictEqual(process.env.MULTI_DOUBLE_QUOTED, 'THIS\nIS\nA\nMULTILINE\nSTRING');
assert.strictEqual(process.env.MULTI_SINGLE_QUOTED, 'THIS\nIS\nA\nMULTILINE\nSTRING');
assert.strictEqual(process.env.MULTI_BACKTICKED, 'THIS\nIS\nA\n"MULTILINE\'S"\nSTRING');
assert.strictEqual(process.env.MULTI_NOT_VALID, 'THIS');

0 comments on commit 7089fd7

Please sign in to comment.