diff --git a/src/node_file.cc b/src/node_file.cc index 5a50aacb1b939d..f77cc88fcb4246 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3125,55 +3125,6 @@ static void GetFormatOfExtensionlessFile( return args.GetReturnValue().Set(EXTENSIONLESS_FORMAT_JAVASCRIPT); } -#ifdef _WIN32 -std::wstring ConvertToWideString(const std::string& str) { - int size_needed = MultiByteToWideChar( - CP_UTF8, 0, &str[0], static_cast(str.size()), nullptr, 0); - std::wstring wstrTo(size_needed, 0); - MultiByteToWideChar(CP_UTF8, - 0, - &str[0], - static_cast(str.size()), - &wstrTo[0], - size_needed); - return wstrTo; -} - -#define BufferValueToPath(str) \ - std::filesystem::path(ConvertToWideString(str.ToString())) - -std::string ConvertWideToUTF8(const std::wstring& wstr) { - if (wstr.empty()) return std::string(); - - int size_needed = WideCharToMultiByte(CP_UTF8, - 0, - &wstr[0], - static_cast(wstr.size()), - nullptr, - 0, - nullptr, - nullptr); - std::string strTo(size_needed, 0); - WideCharToMultiByte(CP_UTF8, - 0, - &wstr[0], - static_cast(wstr.size()), - &strTo[0], - size_needed, - nullptr, - nullptr); - return strTo; -} - -#define PathToString(path) ConvertWideToUTF8(path.wstring()); - -#else // _WIN32 - -#define BufferValueToPath(str) std::filesystem::path(str.ToStringView()); -#define PathToString(path) path.native(); - -#endif // _WIN32 - static void CpSyncCheckPaths(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); diff --git a/src/node_modules.cc b/src/node_modules.cc index 94ed9bc4b3c157..4678061a9321e3 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -337,14 +337,13 @@ void BindingData::GetNearestParentPackageJSON( bool slashCheck = path_value.ToStringView().ends_with(kPathSeparator); ToNamespacedPath(realm->env(), &path_value); - - std::string path_value_str = path_value.ToString(); + std::filesystem::path path = BufferValueToPath(path_value); if (slashCheck) { - path_value_str.push_back(kPathSeparator); + path += kPathSeparator; } auto package_json = - TraverseParent(realm, std::filesystem::path(path_value_str)); + TraverseParent(realm, path); if (package_json != nullptr) { args.GetReturnValue().Set(package_json->Serialize(realm)); @@ -363,14 +362,12 @@ void BindingData::GetNearestParentPackageJSONType( bool slashCheck = path_value.ToStringView().ends_with(kPathSeparator); ToNamespacedPath(realm->env(), &path_value); - - std::string path_value_str = path_value.ToString(); + std::filesystem::path path = BufferValueToPath(path_value); if (slashCheck) { - path_value_str.push_back(kPathSeparator); + path += kPathSeparator; } - auto package_json = - TraverseParent(realm, std::filesystem::path(path_value_str)); + auto package_json = TraverseParent(realm, path); if (package_json == nullptr) { return; diff --git a/src/util.cc b/src/util.cc index a372eb4d88ca1d..531a5330c2cb74 100644 --- a/src/util.cc +++ b/src/util.cc @@ -885,4 +885,43 @@ v8::Maybe GetValidFileMode(Environment* env, return v8::Just(mode); } +#ifdef _WIN32 +std::wstring ConvertToWideString(const std::string& str) { + int size_needed = MultiByteToWideChar( + CP_UTF8, 0, &str[0], static_cast(str.size()), nullptr, 0); + std::wstring wstrTo(size_needed, 0); + MultiByteToWideChar(CP_UTF8, + 0, + &str[0], + static_cast(str.size()), + &wstrTo[0], + size_needed); + return wstrTo; +} + +std::string ConvertWideToUTF8(const std::wstring& wstr) { + if (wstr.empty()) return std::string(); + + int size_needed = WideCharToMultiByte(CP_UTF8, + 0, + &wstr[0], + static_cast(wstr.size()), + nullptr, + 0, + nullptr, + nullptr); + std::string strTo(size_needed, 0); + WideCharToMultiByte(CP_UTF8, + 0, + &wstr[0], + static_cast(wstr.size()), + &strTo[0], + size_needed, + nullptr, + nullptr); + return strTo; +} + +#endif // _WIN32 + } // namespace node diff --git a/src/util.h b/src/util.h index b1f316eebc7199..d32c71cd70aee8 100644 --- a/src/util.h +++ b/src/util.h @@ -49,6 +49,11 @@ #include #include #include +#ifdef _WIN32 +#include +#else +#include +#endif #ifdef __GNUC__ #define MUST_USE_RESULT __attribute__((warn_unused_result)) @@ -1013,6 +1018,23 @@ v8::Maybe GetValidFileMode(Environment* env, // case insensitive. inline bool IsWindowsBatchFile(const char* filename); +#ifdef _WIN32 +std::wstring ConvertToWideString(const std::string& str); + +#define BufferValueToPath(str) \ + std::filesystem::path(ConvertToWideString(str.ToString())) + +std::string ConvertWideToUTF8(const std::wstring& wstr); + +#define PathToString(path) ConvertWideToUTF8(path.wstring()); + +#else // _WIN32 + +#define BufferValueToPath(str) std::filesystem::path(str.ToStringView()); +#define PathToString(path) path.native(); + +#endif // _WIN32 + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS