From 0cf71e27fba3322e6afd7edbb856c30ff7f5d3e8 Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Mon, 31 Jul 2023 14:26:31 +0900 Subject: [PATCH 1/3] worker: support case-insensitive process.env usage on Windows This patch fixes the reported issue by using a case-insensitive `unordered_map` for the copied `process.env` on Windows. Signed-off-by: Daeyeon Jeong --- src/node_env_var.cc | 19 ++++++++++++++ .../test-worker-process-env-windows.js | 26 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 test/parallel/test-worker-process-env-windows.js diff --git a/src/node_env_var.cc b/src/node_env_var.cc index 94936ea1c2bd22..6ac94c2c5eefd1 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -62,7 +62,26 @@ class MapKVStore final : public KVStore { private: mutable Mutex mutex_; +#ifdef _WIN32 + struct Hash { + inline size_t operator()(const std::string& str) const { + return std::hash()(ToLower(str)); + } + }; + + struct Equal { + inline bool operator()(const std::string& a, const std::string& b) const { + return a.size() == b.size() && + std::equal(a.begin(), a.end(), b.begin(), [](char a, char b) { + return ToLower(a) == ToLower(b); + }); + } + }; + + std::unordered_map map_; +#else std::unordered_map map_; +#endif }; namespace per_process { diff --git a/test/parallel/test-worker-process-env-windows.js b/test/parallel/test-worker-process-env-windows.js new file mode 100644 index 00000000000000..205c68bee01c52 --- /dev/null +++ b/test/parallel/test-worker-process-env-windows.js @@ -0,0 +1,26 @@ +'use strict'; + +const common = require('../common'); +const { Worker, isMainThread, parentPort } = require('node:worker_threads'); +const { strictEqual } = require('node:assert'); + +// Test for https://github.com/nodejs/node/issues/48955. + +if (!common.isWindows) { + common.skip('this test is Windows-specific.'); +} + +if (isMainThread) { + process.env.barkey = 'foovalue'; + strictEqual(process.env.barkey, process.env.BARKEY); + + const worker = new Worker(__filename); + worker.once('message', (data) => { + strictEqual(data.barkey, data.BARKEY); + }); +} else { + parentPort.postMessage({ + barkey: process.env.barkey, + BARKEY: process.env.BARKEY, + }); +} From f373f7e61a85ee85da3aef701ca3cd52ab4c539d Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Tue, 1 Aug 2023 10:38:55 +0900 Subject: [PATCH 2/3] fixup! worker: support case-insensitive process.env usage on Windows Signed-off-by: Daeyeon Jeong --- src/node_env_var.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/node_env_var.cc b/src/node_env_var.cc index 6ac94c2c5eefd1..57715cd23547e6 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -71,10 +71,7 @@ class MapKVStore final : public KVStore { struct Equal { inline bool operator()(const std::string& a, const std::string& b) const { - return a.size() == b.size() && - std::equal(a.begin(), a.end(), b.begin(), [](char a, char b) { - return ToLower(a) == ToLower(b); - }); + return a.size() == b.size() && StringEqualNoCase(a.c_str(), b.c_str()); } }; From a955522cf6b0e82298d2c146cc89ff042755489b Mon Sep 17 00:00:00 2001 From: Daeyeon Jeong Date: Thu, 3 Aug 2023 10:11:54 +0900 Subject: [PATCH 3/3] fixup! fixup! worker: support case-insensitive process.env usage on Windows Signed-off-by: Daeyeon Jeong --- src/node_env_var.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/node_env_var.cc b/src/node_env_var.cc index 57715cd23547e6..39fb26861e4ce3 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -71,7 +71,8 @@ class MapKVStore final : public KVStore { struct Equal { inline bool operator()(const std::string& a, const std::string& b) const { - return a.size() == b.size() && StringEqualNoCase(a.c_str(), b.c_str()); + return a.size() == b.size() && + StringEqualNoCaseN(a.data(), b.data(), b.size()); } };