Skip to content

Commit 7089fd7

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

File tree

4 files changed

+65
-56
lines changed

4 files changed

+65
-56
lines changed

src/node_dotenv.cc

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "node_dotenv.h"
2+
#include <regex> // NOLINT(build/c++11)
23
#include "env-inl.h"
34
#include "node_file.h"
45
#include "uv.h"
@@ -8,6 +9,17 @@ namespace node {
89
using v8::NewStringType;
910
using v8::String;
1011

12+
/**
13+
* The inspiration for this implementation comes from the original dotenv code,
14+
* available at https://github.com/motdotla/dotenv/blob/master/lib/main.js#L12
15+
*/
16+
17+
std::regex LINE(
18+
"(?:^|^)\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|"
19+
"[^'])*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\\r\\n]+)?"
20+
"\\s*(?:#.*)?(?:$|$)",
21+
std::regex::multiline);
22+
1123
std::vector<std::string> Dotenv::GetPathFromArgs(
1224
const std::vector<std::string>& args) {
1325
const auto find_match = [](const std::string& arg) {
@@ -98,73 +110,46 @@ bool Dotenv::ParsePath(const std::string_view path) {
98110
result.append(buf.base, r);
99111
}
100112

101-
using std::string_view_literals::operator""sv;
102-
auto lines = SplitString(result, "\n"sv);
113+
// Convert line breaks to the same format
114+
std::string lines = result;
115+
std::regex_replace(lines, std::regex("\r\n?"), "\n");
103116

104-
for (const auto& line : lines) {
105-
ParseLine(line);
106-
}
107-
return true;
108-
}
117+
std::smatch match;
118+
while (std::regex_search(lines, match, LINE)) {
119+
const std::string key = match[1].str();
109120

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

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

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

121-
if (equal_index == std::string_view::npos) {
122-
return;
123-
}
124-
125-
auto key = line.substr(0, equal_index);
130+
// Remove surrounding quotes
131+
value =
132+
std::regex_replace(value, std::regex("^(['\"`])([\\s\\S]*)\\1$"), "$2");
126133

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);
134+
// Expand newlines if double quoted
135+
if (maybeQuote == '"') {
136+
value = std::regex_replace(value, std::regex("\\\\n"), "\n");
137+
value = std::regex_replace(value, std::regex("\\\\r"), "\r");
138+
}
130139

131-
// Omit lines with comments
132-
if (key.front() == '#' || key.empty()) {
133-
return;
140+
store_.insert_or_assign(std::string(key), value);
141+
lines = match.suffix();
134142
}
135143

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('#');
144+
return true;
145+
}
155146

156-
// Remove any inline comments
157-
if (hash_index != std::string::npos) {
158-
value.erase(hash_index);
159-
}
147+
void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {
148+
auto match = store_.find("NODE_OPTIONS");
160149

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);
150+
if (match != store_.end()) {
151+
*node_options = match->second;
165152
}
166-
167-
store_.insert_or_assign(std::string(key), value);
168153
}
169154

170155
} // namespace node

src/node_dotenv.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ 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_;
3130
};
3231

test/fixtures/dotenv/valid.env

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,23 @@ RETAIN_INNER_QUOTES_AS_BACKTICKS=`{"foo": "bar's"}`
3333
TRIM_SPACE_FROM_UNQUOTED= some spaced out string
3434
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=THIS
55+
IS NOT MULTILINE

test/parallel/test-dotenv.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@ assert.strictEqual(process.env.TRIM_SPACE_FROM_UNQUOTED, 'some spaced out string
6868
assert.strictEqual(process.env.EMAIL, '[email protected]');
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, 'THIS');

0 commit comments

Comments
 (0)