Skip to content

Commit 86c48ea

Browse files
feedback and incorporate changes to path handling from rebase
1 parent c8a584a commit 86c48ea

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

src/node_modules.cc

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -334,32 +334,32 @@ const BindingData::PackageConfig* BindingData::TraverseParent(
334334
return nullptr;
335335
}
336336

337-
void BindingData::GetNearestParentPackageJSON(
338-
const v8::FunctionCallbackInfo<v8::Value>& args) {
339-
CHECK_GE(args.Length(), 1);
340-
CHECK(args[0]->IsString());
341-
342-
Realm* realm = Realm::GetCurrent(args);
343-
BufferValue path_value(realm->isolate(), args[0]);
337+
const std::filesystem::path BindingData::NormalizePath(
338+
Realm* realm, BufferValue* path_value) {
344339
// Check if the path has a trailing slash. If so, add it after
345340
// ToNamespacedPath() as it will be deleted by ToNamespacedPath()
346-
bool slashCheck = path_value.ToStringView().ends_with(kPathSeparator);
341+
bool slashCheck = path_value->ToStringView().ends_with(kPathSeparator);
347342

348-
ToNamespacedPath(realm->env(), &path_value);
343+
ToNamespacedPath(realm->env(), path_value);
344+
345+
auto path = path_value->ToPath();
349346

350-
std::string path_value_str = path_value.ToString();
351347
if (slashCheck) {
352-
path_value_str.push_back(kPathSeparator);
348+
path /= "";
353349
}
354350

355-
std::filesystem::path path;
351+
return path;
352+
}
356353

357-
#ifdef _WIN32
358-
std::wstring wide_path = ConvertToWideString(path_value_str, GetACP());
359-
path = std::filesystem::path(wide_path);
360-
#else
361-
path = std::filesystem::path(path_value_str);
362-
#endif
354+
void BindingData::GetNearestParentPackageJSON(
355+
const v8::FunctionCallbackInfo<v8::Value>& args) {
356+
CHECK_GE(args.Length(), 1);
357+
CHECK(args[0]->IsString());
358+
359+
Realm* realm = Realm::GetCurrent(args);
360+
BufferValue path_value(realm->isolate(), args[0]);
361+
362+
auto path = NormalizePath(realm, &path_value);
363363

364364
auto package_json = TraverseParent(realm, path);
365365

@@ -375,17 +375,8 @@ void BindingData::GetNearestParentPackageJSONType(
375375

376376
Realm* realm = Realm::GetCurrent(args);
377377
BufferValue path_value(realm->isolate(), args[0]);
378-
// Check if the path has a trailing slash. If so, add it after
379-
// ToNamespacedPath() as it will be deleted by ToNamespacedPath()
380-
bool slashCheck = path_value.ToStringView().ends_with(kPathSeparator);
381-
382-
ToNamespacedPath(realm->env(), &path_value);
383-
384-
auto path = path_value.ToPath();
385378

386-
if (slashCheck) {
387-
path /= "";
388-
}
379+
auto path = NormalizePath(realm, &path_value);
389380

390381
auto package_json = TraverseParent(realm, path);
391382

src/node_modules.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class BindingData : public SnapshotableObject {
8383
std::unordered_map<std::string, std::optional<PackageConfig> >
8484
package_configs_;
8585
simdjson::ondemand::parser json_parser;
86+
static const std::filesystem::path NormalizePath(Realm* realm,
87+
BufferValue* path_value);
8688
// returns null on error
8789
static const PackageConfig* GetPackageJSON(
8890
Realm* realm,

0 commit comments

Comments
 (0)