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,22 @@ 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 ))) {
300
+ current_path. generic_string () ))) {
312
301
return nullptr ;
313
302
}
314
303
315
304
// 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" )) {
320
306
return nullptr ;
321
307
}
322
308
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 );
327
312
if (package_json != nullptr ) {
328
313
return package_json;
329
314
}
330
- } while (separator_index > root_separator_index );
315
+ } while (true );
331
316
332
317
return nullptr ;
333
318
}
@@ -339,7 +324,8 @@ void BindingData::GetNearestParentPackageJSON(
339
324
340
325
Realm* realm = Realm::GetCurrent (args);
341
326
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 ()));
343
329
344
330
if (package_json != nullptr ) {
345
331
args.GetReturnValue ().Set (package_json->Serialize (realm));
@@ -353,7 +339,8 @@ void BindingData::GetNearestParentPackageJSONType(
353
339
354
340
Realm* realm = Realm::GetCurrent (args);
355
341
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 ()));
357
344
358
345
if (package_json == nullptr ) {
359
346
return ;
@@ -390,10 +377,7 @@ void BindingData::GetPackageScopeConfig(
390
377
// TODO(@anonrig): Rewrite this function and avoid calling URL parser.
391
378
while (true ) {
392
379
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)) {
397
381
break ;
398
382
}
399
383
0 commit comments