From d3d4f4aa119dc378a94cbb623498deee0123ccea Mon Sep 17 00:00:00 2001 From: Ilyas Shabi Date: Sun, 17 Mar 2024 00:14:34 +0100 Subject: [PATCH] src: remove comments from env file --- src/node_dotenv.cc | 26 ++++++++++++++++++++++---- src/node_dotenv.h | 3 ++- test/fixtures/dotenv/valid.env | 5 +++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/node_dotenv.cc b/src/node_dotenv.cc index 8d92e917082488..3d5d4e2f95532f 100644 --- a/src/node_dotenv.cc +++ b/src/node_dotenv.cc @@ -18,8 +18,7 @@ using v8::String; */ const std::regex LINE( "\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|[^']" - ")*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\r\n]+)?\\s*(?" - ":#.*)?"); // NOLINT(whitespace/line_length) + ")*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\r\n]+)?\\s*"); std::vector Dotenv::GetPathFromArgs( const std::vector& args) { @@ -104,6 +103,7 @@ Local Dotenv::ToObject(Environment* env) { void Dotenv::ParseContent(const std::string_view content) { std::string lines = std::string(content); lines = std::regex_replace(lines, std::regex("\r\n?"), "\n"); + lines = RemoveComments(lines); std::smatch match; while (std::regex_search(lines, match, LINE)) { @@ -126,7 +126,7 @@ void Dotenv::ParseContent(const std::string_view content) { } // Remove surrounding quotes - value = trim_quotes(value); + value = TrimQuotes(value); store_.insert_or_assign(std::string(key), value); lines = match.suffix(); @@ -179,7 +179,7 @@ void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) { } } -std::string_view Dotenv::trim_quotes(std::string_view str) { +std::string_view Dotenv::TrimQuotes(std::string_view str) { static const std::unordered_set quotes = {'"', '\'', '`'}; if (str.size() >= 2 && quotes.count(str.front()) && quotes.count(str.back())) { @@ -188,4 +188,22 @@ std::string_view Dotenv::trim_quotes(std::string_view str) { return str; } +std::string Dotenv::RemoveComments(const std::string& content) { + std::istringstream iss(content); + std::string result; + std::string line; + + while (std::getline(iss, line)) { + if (line.empty()) { + continue; + } + // Trim leading whitespace and check if the line starts with '#' + std::string trimmed = line.substr(line.find_first_not_of(" \t")); + if (trimmed.front() != '#') { + result.append(line + "\n"); + } + } + return result; +} + } // namespace node diff --git a/src/node_dotenv.h b/src/node_dotenv.h index 5872627f0c25fe..a9e6df5658ccdf 100644 --- a/src/node_dotenv.h +++ b/src/node_dotenv.h @@ -32,7 +32,8 @@ class Dotenv { private: std::map store_; - std::string_view trim_quotes(std::string_view str); + std::string_view TrimQuotes(std::string_view str); + std::string RemoveComments(const std::string& str); }; } // namespace node diff --git a/test/fixtures/dotenv/valid.env b/test/fixtures/dotenv/valid.env index ccffa779cb0d86..522e49cdba8ade 100644 --- a/test/fixtures/dotenv/valid.env +++ b/test/fixtures/dotenv/valid.env @@ -24,6 +24,8 @@ EXPAND_NEWLINES="expand\nnew\nlines" DONT_EXPAND_UNQUOTED=dontexpand\nnewlines DONT_EXPAND_SQUOTED='dontexpand\nnewlines' # COMMENTS=work +#BASIC=basic2 +#BASIC=basic3 INLINE_COMMENTS=inline comments # work #very #well INLINE_COMMENTS_SINGLE_QUOTES='inline comments outside of #singlequotes' # work INLINE_COMMENTS_DOUBLE_QUOTES="inline comments outside of #doublequotes" # work @@ -37,7 +39,7 @@ TRIM_SPACE_FROM_UNQUOTED= some spaced out string EMAIL=therealnerdybeast@example.tld SPACED_KEY = parsed EDGE_CASE_INLINE_COMMENTS="VALUE1" # or "VALUE2" or "VALUE3" - +export EXAMPLE = ignore export MULTI_DOUBLE_QUOTED="THIS IS A @@ -58,4 +60,3 @@ STRING` MULTI_NOT_VALID_QUOTE=" MULTI_NOT_VALID=THIS IS NOT MULTILINE -export EXAMPLE = ignore export