Skip to content

Commit dd093fe

Browse files
feedback and incorporate changes to path handling from rebase
1 parent 85426ca commit dd093fe

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
@@ -333,32 +333,32 @@ const BindingData::PackageConfig* BindingData::TraverseParent(
333333
return nullptr;
334334
}
335335

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

347-
ToNamespacedPath(realm->env(), &path_value);
342+
ToNamespacedPath(realm->env(), path_value);
343+
344+
auto path = path_value->ToPath();
348345

349-
std::string path_value_str = path_value.ToString();
350346
if (slashCheck) {
351-
path_value_str.push_back(kPathSeparator);
347+
path /= "";
352348
}
353349

354-
std::filesystem::path path;
350+
return path;
351+
}
355352

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

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

@@ -374,17 +374,8 @@ void BindingData::GetNearestParentPackageJSONType(
374374

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

385-
if (slashCheck) {
386-
path /= "";
387-
}
378+
auto path = NormalizePath(realm, &path_value);
388379

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

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)