Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3173,29 +3173,6 @@ static void GetFormatOfExtensionlessFile(
#define BufferValueToPath(str) \
std::filesystem::path(ConvertToWideString(str.ToString(), CP_UTF8))

std::string ConvertWideToUTF8(const std::wstring& wstr) {
if (wstr.empty()) return std::string();

int size_needed = WideCharToMultiByte(CP_UTF8,
0,
&wstr[0],
static_cast<int>(wstr.size()),
nullptr,
0,
nullptr,
nullptr);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8,
0,
&wstr[0],
static_cast<int>(wstr.size()),
&strTo[0],
size_needed,
nullptr,
nullptr);
return strTo;
}

#define PathToString(path) ConvertWideToUTF8(path.wstring());

#else // _WIN32
Expand Down
33 changes: 23 additions & 10 deletions src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,22 +296,35 @@ const BindingData::PackageConfig* BindingData::TraverseParent(

// Stop the search when the process doesn't have permissions
// to walk upwards
if (is_permissions_enabled &&
!env->permission()->is_granted(
env,
permission::PermissionScope::kFileSystemRead,
current_path.generic_string())) [[unlikely]] {
return nullptr;
if (is_permissions_enabled) {
std::string generic_path;
#ifdef _WIN32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that the amount of ifdefs sprinkled everywhere is a bit cluttering; it looks like all new usages of these are on a std::filesystem::path, so technically they are just repeating what PathToString() does, I think we can just put PathToString to util.h and use that instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I think usage of generic_string vs string complicates it a bit, but let me see what I can do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about db29a1d? I tried to unify things as much as I could.

generic_path = ConvertWideToUTF8(current_path.generic_wstring());
#else // _WIN32
generic_path = current_path.generic_string();
#endif // _WIN32
//
if (!env->permission()->is_granted(
env, permission::PermissionScope::kFileSystemRead, generic_path))
[[unlikely]] {
return nullptr;
}
}

// Check if the path ends with `/node_modules`
if (current_path.generic_string().ends_with("/node_modules")) {
if (current_path.filename() == "node_modules") {
return nullptr;
}

auto package_json_path = current_path / "package.json";
auto package_json =
GetPackageJSON(realm, package_json_path.string(), nullptr);

std::string package_json_path_str;
#ifdef _WIN32
package_json_path_str = ConvertWideToUTF8(package_json_path.wstring());
#else // _WIN32
package_json_path_str = package_json_path.string();
#endif // _WIN32
auto package_json = GetPackageJSON(realm, package_json_path_str, nullptr);
if (package_json != nullptr) {
return package_json;
}
Expand Down Expand Up @@ -341,7 +354,7 @@ void BindingData::GetNearestParentPackageJSONType(
std::filesystem::path path;

#ifdef _WIN32
std::wstring wide_path = ConvertToWideString(path_value_str, GetACP());
std::wstring wide_path = ConvertToWideString(path_value_str, CP_UTF8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here - I think we can just create StringToPath and let BufferValueToPath use it, and here it can just use StringToPath

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by db29a1d. I also dropped the code_point argument from the util method to make sure we don't use GetACP() by accident.

path = std::filesystem::path(wide_path);
#else
path = std::filesystem::path(path_value_str);
Expand Down
23 changes: 23 additions & 0 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,29 @@ inline std::wstring ConvertToWideString(const std::string& str,
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<int>(wstr.size()),
nullptr,
0,
nullptr,
nullptr);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8,
0,
&wstr[0],
static_cast<int>(wstr.size()),
&strTo[0],
size_needed,
nullptr,
nullptr);
return strTo;
}
#endif // _WIN32

inline v8::MaybeLocal<v8::Object> NewDictionaryInstance(
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ class JSONOutputStream final : public v8::OutputStream {
// case insensitive.
inline bool IsWindowsBatchFile(const char* filename);
inline std::wstring ConvertToWideString(const std::string& str, UINT code_page);
inline std::string ConvertWideToUTF8(const std::wstring& wstr);
#endif // _WIN32

// A helper to create a new instance of the dictionary template.
Expand Down
Loading