33#include " base_object-inl.h"
44#include " node_errors.h"
55#include " node_external_reference.h"
6- #include " node_process-inl.h"
76#include " node_url.h"
87#include " permission/permission.h"
98#include " permission/permission_base.h"
@@ -33,14 +32,6 @@ using v8::String;
3332using v8::Undefined;
3433using v8::Value;
3534
36- #ifdef __POSIX__
37- constexpr char kPathSeparator = ' /' ;
38- constexpr std::string_view kNodeModules = " /node_modules" ;
39- #else
40- constexpr char kPathSeparator = ' \\ ' ;
41- constexpr std::string_view kNodeModules = " \\ node_modules" ;
42- #endif
43-
4435void BindingData::MemoryInfo (MemoryTracker* tracker) const {
4536 // Do nothing
4637}
@@ -287,18 +278,16 @@ void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {
287278}
288279
289280const BindingData::PackageConfig* BindingData::TraverseParent (
290- Realm* realm, std::string_view check_path) {
281+ Realm* realm, const std::filesystem::path& check_path) {
282+ std::filesystem::path current_path = check_path;
291283 auto env = realm->env ();
292- auto root_separator_index = check_path.find_first_of (kPathSeparator );
293- size_t separator_index = 0 ;
294284 const bool is_permissions_enabled = env->permission ()->enabled ();
295285
296286 do {
297- separator_index = check_path.find_last_of (kPathSeparator );
298- check_path = check_path.substr (0 , separator_index);
287+ current_path = current_path.parent_path ();
299288
300289 // We don't need to try "/"
301- if (check_path. empty () ) {
290+ if (current_path. parent_path () == current_path ) {
302291 break ;
303292 }
304293
@@ -308,26 +297,22 @@ const BindingData::PackageConfig* BindingData::TraverseParent(
308297 !env->permission ()->is_granted (
309298 env,
310299 permission::PermissionScope::kFileSystemRead ,
311- std::string (check_path) + kPathSeparator ))) {
300+ current_path. generic_string () ))) {
312301 return nullptr ;
313302 }
314303
315304 // Check if the path ends with `/node_modules`
316- if (check_path.size () >= kNodeModules .size () &&
317- std::equal (check_path.end () - kNodeModules .size (),
318- check_path.end (),
319- kNodeModules .begin ())) {
305+ if (current_path.generic_string ().ends_with (" /node_modules" )) {
320306 return nullptr ;
321307 }
322308
323- auto package_json = GetPackageJSON (
324- realm,
325- std::string (check_path) + kPathSeparator + " package.json" ,
326- nullptr );
309+ auto package_json_path = current_path / " package.json" ;
310+ auto package_json =
311+ GetPackageJSON (realm, package_json_path.string (), nullptr );
327312 if (package_json != nullptr ) {
328313 return package_json;
329314 }
330- } while (separator_index > root_separator_index );
315+ } while (true );
331316
332317 return nullptr ;
333318}
@@ -339,7 +324,8 @@ void BindingData::GetNearestParentPackageJSON(
339324
340325 Realm* realm = Realm::GetCurrent (args);
341326 Utf8Value path_value (realm->isolate (), args[0 ]);
342- auto package_json = TraverseParent (realm, path_value.ToStringView ());
327+ auto package_json =
328+ TraverseParent (realm, std::filesystem::path (path_value.ToString ()));
343329
344330 if (package_json != nullptr ) {
345331 args.GetReturnValue ().Set (package_json->Serialize (realm));
@@ -353,7 +339,8 @@ void BindingData::GetNearestParentPackageJSONType(
353339
354340 Realm* realm = Realm::GetCurrent (args);
355341 Utf8Value path (realm->isolate (), args[0 ]);
356- auto package_json = TraverseParent (realm, path.ToStringView ());
342+ auto package_json =
343+ TraverseParent (realm, std::filesystem::path (path.ToString ()));
357344
358345 if (package_json == nullptr ) {
359346 return ;
@@ -390,10 +377,7 @@ void BindingData::GetPackageScopeConfig(
390377 // TODO(@anonrig): Rewrite this function and avoid calling URL parser.
391378 while (true ) {
392379 auto pathname = package_json_url->get_pathname ();
393- if (pathname.size () >= node_modules_package_path.size () &&
394- pathname.compare (pathname.size () - node_modules_package_path.size (),
395- node_modules_package_path.size (),
396- node_modules_package_path) == 0 ) {
380+ if (pathname.ends_with (node_modules_package_path)) {
397381 break ;
398382 }
399383
0 commit comments