-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "node_dotenv.h" | ||
#include "uv.h" | ||
|
||
namespace node { | ||
|
||
using v8::Isolate; | ||
using v8::NewStringType; | ||
|
||
namespace dotenv { | ||
|
||
void LoadFromFile(Isolate* isolate, | ||
const std::string_view src, | ||
std::shared_ptr<KVStore> store) { | ||
std::string path = std::string(src) + "/.env"; | ||
|
||
uv_fs_t req; | ||
auto defer_req_cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); }); | ||
|
||
uv_file file = uv_fs_open(nullptr, &req, path.c_str(), 0, 438, nullptr); | ||
if (req.result < 0) { | ||
// req will be cleaned up by scope leave. | ||
return; | ||
} | ||
uv_fs_req_cleanup(&req); | ||
|
||
auto defer_close = OnScopeLeave([file]() { | ||
uv_fs_t close_req; | ||
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr)); | ||
uv_fs_req_cleanup(&close_req); | ||
}); | ||
|
||
std::string result{}; | ||
char buffer[8192]; | ||
uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer)); | ||
|
||
while (true) { | ||
auto r = uv_fs_read(nullptr, &req, file, &buf, 1, -1, nullptr); | ||
if (req.result < 0) { | ||
// req will be cleaned up by scope leave. | ||
return; | ||
} | ||
uv_fs_req_cleanup(&req); | ||
if (r <= 0) { | ||
break; | ||
} | ||
result.append(buf.base, r); | ||
} | ||
|
||
using std::string_view_literals::operator""sv; | ||
|
||
for (const auto& line : SplitString(result, "\n"sv)) { | ||
auto equal_index = line.find('='); | ||
|
||
if (equal_index == std::string_view::npos) { | ||
continue; | ||
} | ||
|
||
std::string_view key = line.substr(0, equal_index); | ||
std::string_view value = line.substr(equal_index + 1); | ||
|
||
store->Set(isolate, | ||
v8::String::NewFromUtf8( | ||
isolate, key.data(), NewStringType::kNormal, key.size()) | ||
.ToLocalChecked(), | ||
v8::String::NewFromUtf8( | ||
isolate, value.data(), NewStringType::kNormal, value.size()) | ||
.ToLocalChecked()); | ||
} | ||
} | ||
|
||
} // namespace dotenv | ||
|
||
} // namespace node |
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef SRC_NODE_DOTENV_H_ | ||
#define SRC_NODE_DOTENV_H_ | ||
|
||
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#include "util-inl.h" | ||
|
||
namespace node { | ||
|
||
namespace dotenv { | ||
|
||
void LoadFromFile(v8::Isolate* isolate, | ||
const std::string_view path, | ||
std::shared_ptr<KVStore> store); | ||
|
||
} // namespace dotenv | ||
|
||
} // namespace node | ||
|
||
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | ||
|
||
#endif // SRC_NODE_DOTENV_H_ |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
DATABASE_PASSWORD=nodejs |
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('node:assert'); | ||
const { spawnSync } = require('node:child_process'); | ||
const path = require('node:path'); | ||
|
||
{ | ||
const child = spawnSync( | ||
process.execPath, | ||
['--load-dotenv', '-e', 'console.log(process.env.DATABASE_PASSWORD)'], | ||
{ | ||
cwd: path.join(__dirname, '../fixtures/dotenv/valid') | ||
} | ||
); | ||
|
||
assert.strictEqual(child.stdout.toString(), 'nodejs\n'); | ||
} |