-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
src: make parsing compatible with motdotla/dotenv package #54215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
marekpiechut
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
marekpiechut:env-inner-quotes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+395
−110
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
cleanup & add comments
- Loading branch information
commit 1456714fc651b543d9b6e89fb63183fcf5345b7c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,17 +87,25 @@ Local<Object> Dotenv::ToObject(Environment* env) const { | |
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Remove trailing and leading quotes from a string | ||
| * if they match. | ||
| */ | ||
| std::string_view trim_quotes(std::string_view input) { | ||
| if (input.empty()) return ""; | ||
| auto first = input.front(); | ||
| if ((first == '\'' || first == '"' || first == '`') && | ||
| input.back() == first) { | ||
| input = input.substr(1, input.size() - 2); | ||
| input.remove_prefix(1); | ||
| input.remove_suffix(1); | ||
| } | ||
|
|
||
| return input; | ||
| } | ||
|
|
||
| /** | ||
| * Remove leading and trailing spaces from a string. | ||
| */ | ||
| std::string_view trim_spaces(std::string_view input) { | ||
| if (input.empty()) return ""; | ||
| if (input.front() == ' ') { | ||
|
|
@@ -109,6 +117,10 @@ std::string_view trim_spaces(std::string_view input) { | |
| return input; | ||
| } | ||
|
|
||
| /** | ||
| * Trim key from .env file, remove leading "export" if found, | ||
| * like it is ignored in dotenv. | ||
| */ | ||
| std::string_view parse_key(std::string_view key) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment to what this function does?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| key = trim_spaces(key); | ||
| if (key.empty()) return key; | ||
|
|
@@ -119,23 +131,30 @@ std::string_view parse_key(std::string_view key) { | |
| return key; | ||
| } | ||
|
|
||
| /** | ||
| * Parse single value from .env file. | ||
| * Remove leading and trailing spaces and quotes. | ||
| * Leave empty space inside the quotes as is. | ||
| * Expand \n to newline only in double-quote strings. | ||
| * (like dotenv) | ||
| */ | ||
| std::string parse_value(std::string_view value) { | ||
| value = trim_spaces(value); | ||
| if (value.empty()) return ""; | ||
|
|
||
| auto trimmed = trim_quotes(value); | ||
| if (value.front() == '\"' && value.back() == '\"') { | ||
| // Expand \n to newline in double-quote strings | ||
| size_t pos = 0; | ||
| auto expanded = std::string(trimmed); | ||
| while ((pos = expanded.find("\\n", pos)) != std::string_view::npos) { | ||
| expanded.replace(pos, 2, "\n"); | ||
| pos += 1; | ||
| } | ||
| return expanded; | ||
| } else { | ||
| if (value.front() != '\"' || value.back() != '\"') { | ||
| return std::string(trimmed); | ||
| } | ||
|
|
||
| // Expand \n to newline in double-quote strings | ||
| size_t pos = 0; | ||
| auto expanded = std::string(trimmed); | ||
| while ((pos = expanded.find("\\n", pos)) != std::string::npos) { | ||
| expanded.replace(pos, 2, "\n"); | ||
| pos += 1; | ||
| } | ||
| return expanded; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -163,21 +182,26 @@ void Dotenv::ParseContent(const std::string_view input) { | |
| // Skip whitespace after key | ||
| i++; | ||
| } | ||
| //Move start/end pointers to the beginning of the value | ||
| start = i + 1; | ||
| end = i + 1; | ||
| continue; | ||
| } else if (!inComment && (c == '"' || c == '\'' || c == '`')) { | ||
| if (start == i) { | ||
| // Whole value stars with a quote, parse it as a quoted string | ||
| quote = c; | ||
| } else if (quote == c) { | ||
| // Closing quote for quoted string found, no longer in quoted value | ||
| quote = 0; | ||
| } | ||
|
|
||
| end++; | ||
| } else if (!inComment && c == '#' && quote == 0) { | ||
| // Mark end as i, skips rest of the line | ||
| end = i; | ||
| inComment = true; | ||
| } else if ((c == '\n' || c == '\r') && quote == 0) { | ||
| // If found any key store it | ||
| if (!key.empty()) { | ||
| auto value_str = parse_value(input.substr(start, end - start)); | ||
| store_.insert_or_assign(std::string(key), value_str); | ||
|
|
@@ -187,13 +211,15 @@ void Dotenv::ParseContent(const std::string_view input) { | |
| if (i + 1 < input.size() && input[i + 1] == '\n') { | ||
| i++; | ||
| } | ||
| // Reset counters and flags as we're about to parse a new key | ||
| start = i + 1; | ||
| end = start; | ||
| value = ""; | ||
| key = ""; | ||
| quote = 0; | ||
| inComment = false; | ||
| } else if (!inComment) { | ||
| //Char is not a comment, advance the key/value end pointer | ||
| end++; | ||
| } | ||
| } | ||
|
|
@@ -250,11 +276,12 @@ void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) const { | |
| } | ||
| } | ||
|
|
||
| std::optional<std::string> Dotenv::GetValue(const std::string_view key) const { | ||
| std::optional<std::string_view> Dotenv::GetValue( | ||
| const std::string_view key) const { | ||
| auto match = store_.find(key.data()); | ||
|
|
||
| if (match != store_.end()) { | ||
| return std::optional<std::string>{match->second}; | ||
| return match->second; | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.