3
3
#include " base_object-inl.h"
4
4
#include " node_errors.h"
5
5
#include " node_external_reference.h"
6
- #include " node_process-inl.h"
7
6
#include " node_url.h"
8
7
#include " permission/permission.h"
9
8
#include " permission/permission_base.h"
@@ -33,14 +32,6 @@ using v8::String;
33
32
using v8::Undefined;
34
33
using v8::Value;
35
34
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
-
44
35
void BindingData::MemoryInfo (MemoryTracker* tracker) const {
45
36
// Do nothing
46
37
}
@@ -287,18 +278,16 @@ void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {
287
278
}
288
279
289
280
const 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;
291
283
auto env = realm->env ();
292
- auto root_separator_index = check_path.find_first_of (kPathSeparator );
293
- size_t separator_index = 0 ;
294
284
const bool is_permissions_enabled = env->permission ()->enabled ();
295
285
296
286
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 ();
299
288
300
289
// We don't need to try "/"
301
- if (check_path. empty () ) {
290
+ if (current_path. parent_path () == current_path ) {
302
291
break ;
303
292
}
304
293
@@ -308,26 +297,17 @@ const BindingData::PackageConfig* BindingData::TraverseParent(
308
297
!env->permission ()->is_granted (
309
298
env,
310
299
permission::PermissionScope::kFileSystemRead ,
311
- std::string (check_path) + kPathSeparator ))) {
312
- return nullptr ;
313
- }
314
-
315
- // 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 ())) {
300
+ current_path.string ()))) {
320
301
return nullptr ;
321
302
}
322
303
323
- auto package_json = GetPackageJSON (
324
- realm,
325
- std::string (check_path) + kPathSeparator + " package.json" ,
326
- nullptr );
304
+ auto package_json_path = current_path / " package.json" ;
305
+ auto package_json =
306
+ GetPackageJSON (realm, package_json_path.string (), nullptr );
327
307
if (package_json != nullptr ) {
328
308
return package_json;
329
309
}
330
- } while (separator_index > root_separator_index );
310
+ } while (true );
331
311
332
312
return nullptr ;
333
313
}
@@ -339,7 +319,8 @@ void BindingData::GetNearestParentPackageJSON(
339
319
340
320
Realm* realm = Realm::GetCurrent (args);
341
321
Utf8Value path_value (realm->isolate (), args[0 ]);
342
- auto package_json = TraverseParent (realm, path_value.ToStringView ());
322
+ auto package_json =
323
+ TraverseParent (realm, std::filesystem::path (path_value.ToString ()));
343
324
344
325
if (package_json != nullptr ) {
345
326
args.GetReturnValue ().Set (package_json->Serialize (realm));
@@ -353,7 +334,8 @@ void BindingData::GetNearestParentPackageJSONType(
353
334
354
335
Realm* realm = Realm::GetCurrent (args);
355
336
Utf8Value path (realm->isolate (), args[0 ]);
356
- auto package_json = TraverseParent (realm, path.ToStringView ());
337
+ auto package_json =
338
+ TraverseParent (realm, std::filesystem::path (path.ToString ()));
357
339
358
340
if (package_json == nullptr ) {
359
341
return ;
@@ -390,10 +372,7 @@ void BindingData::GetPackageScopeConfig(
390
372
// TODO(@anonrig): Rewrite this function and avoid calling URL parser.
391
373
while (true ) {
392
374
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 ) {
375
+ if (pathname.ends_with (node_modules_package_path)) {
397
376
break ;
398
377
}
399
378
0 commit comments