Skip to content

Commit de3d605

Browse files
committed
src: support multi-line values for .env file
1 parent 89ddc98 commit de3d605

File tree

4 files changed

+64
-56
lines changed

4 files changed

+64
-56
lines changed

src/node_dotenv.cc

+36-55
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "node_dotenv.h"
2+
#include <regex> // NOLINT(build/c++11)
3+
#include <unordered_set>
24
#include "env-inl.h"
35
#include "node_file.h"
46
#include "uv.h"
@@ -8,6 +10,15 @@ namespace node {
810
using v8::NewStringType;
911
using v8::String;
1012

13+
/**
14+
* The inspiration for this implementation comes from the original dotenv code,
15+
* available at https://github.com/motdotla/dotenv
16+
*/
17+
const std::regex LINE(
18+
"\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|[^']"
19+
")*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\r\n]+)?\\s*(?"
20+
":#.*)?"); // NOLINT(whitespace/line_length)
21+
1122
std::vector<std::string> Dotenv::GetPathFromArgs(
1223
const std::vector<std::string>& args) {
1324
const auto find_match = [](const std::string& arg) {
@@ -81,7 +92,7 @@ bool Dotenv::ParsePath(const std::string_view path) {
8192
uv_fs_req_cleanup(&close_req);
8293
});
8394

84-
std::string result{};
95+
std::string lines{};
8596
char buffer[8192];
8697
uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer));
8798

@@ -95,15 +106,29 @@ bool Dotenv::ParsePath(const std::string_view path) {
95106
if (r <= 0) {
96107
break;
97108
}
98-
result.append(buf.base, r);
109+
lines.append(buf.base, r);
99110
}
100111

101-
using std::string_view_literals::operator""sv;
102-
auto lines = SplitString(result, "\n"sv);
112+
std::smatch match;
113+
while (std::regex_search(lines, match, LINE)) {
114+
const std::string key = match[1].str();
115+
116+
// Default undefined or null to an empty string
117+
std::string value = match[2].str();
118+
119+
// Remove leading whitespaces
120+
value.erase(0, value.find_first_not_of(" \t"));
121+
122+
// Remove trailing whitespaces
123+
value.erase(value.find_last_not_of(" \t") + 1);
103124

104-
for (const auto& line : lines) {
105-
ParseLine(line);
125+
// Remove surrounding quotes
126+
value = trim_quotes(value);
127+
128+
store_.insert_or_assign(std::string(key), value);
129+
lines = match.suffix();
106130
}
131+
107132
return true;
108133
}
109134

@@ -115,56 +140,12 @@ void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {
115140
}
116141
}
117142

118-
void Dotenv::ParseLine(const std::string_view line) {
119-
auto equal_index = line.find('=');
120-
121-
if (equal_index == std::string_view::npos) {
122-
return;
123-
}
124-
125-
auto key = line.substr(0, equal_index);
126-
127-
// Remove leading and trailing space characters from key.
128-
while (!key.empty() && std::isspace(key.front())) key.remove_prefix(1);
129-
while (!key.empty() && std::isspace(key.back())) key.remove_suffix(1);
130-
131-
// Omit lines with comments
132-
if (key.front() == '#' || key.empty()) {
133-
return;
143+
std::string Dotenv::trim_quotes(std::string str) {
144+
static const std::unordered_set<char> quotes = {'"', '\'', '`'};
145+
if (str.size() >= 2 && quotes.count(str[0]) && quotes.count(str.back())) {
146+
str = str.substr(1, str.size() - 2);
134147
}
135-
136-
auto value = std::string(line.substr(equal_index + 1));
137-
138-
// Might start and end with `"' characters.
139-
auto quotation_index = value.find_first_of("`\"'");
140-
141-
if (quotation_index == 0) {
142-
auto quote_character = value[quotation_index];
143-
value.erase(0, 1);
144-
145-
auto end_quotation_index = value.find_last_of(quote_character);
146-
147-
// We couldn't find the closing quotation character. Terminate.
148-
if (end_quotation_index == std::string::npos) {
149-
return;
150-
}
151-
152-
value.erase(end_quotation_index);
153-
} else {
154-
auto hash_index = value.find('#');
155-
156-
// Remove any inline comments
157-
if (hash_index != std::string::npos) {
158-
value.erase(hash_index);
159-
}
160-
161-
// Remove any leading/trailing spaces from unquoted values.
162-
while (!value.empty() && std::isspace(value.front())) value.erase(0, 1);
163-
while (!value.empty() && std::isspace(value.back()))
164-
value.erase(value.size() - 1);
165-
}
166-
167-
store_.insert_or_assign(std::string(key), value);
148+
return str;
168149
}
169150

170151
} // namespace node

src/node_dotenv.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class Dotenv {
2626
const std::vector<std::string>& args);
2727

2828
private:
29-
void ParseLine(const std::string_view line);
3029
std::map<std::string, std::string> store_;
30+
std::string trim_quotes(std::string str);
3131
};
3232

3333
} // namespace node

test/fixtures/dotenv/valid.env

+21
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,24 @@ RETAIN_INNER_QUOTES_AS_BACKTICKS=`{"foo": "bar's"}`
3333
TRIM_SPACE_FROM_UNQUOTED= some spaced out string
3434
EMAIL=therealnerdybeast@example.tld
3535
SPACED_KEY = parsed
36+
37+
MULTI_DOUBLE_QUOTED="THIS
38+
IS
39+
A
40+
MULTILINE
41+
STRING"
42+
43+
MULTI_SINGLE_QUOTED='THIS
44+
IS
45+
A
46+
MULTILINE
47+
STRING'
48+
49+
MULTI_BACKTICKED=`THIS
50+
IS
51+
A
52+
"MULTILINE'S"
53+
STRING`
54+
MULTI_NOT_VALID_QUOTE="
55+
MULTI_NOT_VALID=THIS
56+
IS NOT MULTILINE

test/parallel/test-dotenv.js

+6
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,9 @@ assert.strictEqual(process.env.TRIM_SPACE_FROM_UNQUOTED, 'some spaced out string
6868
assert.strictEqual(process.env.EMAIL, 'therealnerdybeast@example.tld');
6969
// Parses keys and values surrounded by spaces
7070
assert.strictEqual(process.env.SPACED_KEY, 'parsed');
71+
// Test multiple-line value
72+
assert.strictEqual(process.env.MULTI_DOUBLE_QUOTED, 'THIS\nIS\nA\nMULTILINE\nSTRING');
73+
assert.strictEqual(process.env.MULTI_SINGLE_QUOTED, 'THIS\nIS\nA\nMULTILINE\nSTRING');
74+
assert.strictEqual(process.env.MULTI_BACKTICKED, 'THIS\nIS\nA\n"MULTILINE\'S"\nSTRING');
75+
assert.strictEqual(process.env.MULTI_NOT_VALID_QUOTE, '"');
76+
assert.strictEqual(process.env.MULTI_NOT_VALID, 'THIS');

0 commit comments

Comments
 (0)