Skip to content

Commit bd17978

Browse files
committed
src: simplify node modules traverse path
1 parent a6d54f1 commit bd17978

File tree

2 files changed

+17
-38
lines changed

2 files changed

+17
-38
lines changed

src/node_modules.cc

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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;
3332
using v8::Undefined;
3433
using 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-
4435
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
4536
// Do nothing
4637
}
@@ -287,18 +278,16 @@ void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {
287278
}
288279

289280
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;
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,17 @@ const BindingData::PackageConfig* BindingData::TraverseParent(
308297
!env->permission()->is_granted(
309298
env,
310299
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()))) {
320301
return nullptr;
321302
}
322303

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);
327307
if (package_json != nullptr) {
328308
return package_json;
329309
}
330-
} while (separator_index > root_separator_index);
310+
} while (true);
331311

332312
return nullptr;
333313
}
@@ -339,7 +319,8 @@ void BindingData::GetNearestParentPackageJSON(
339319

340320
Realm* realm = Realm::GetCurrent(args);
341321
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()));
343324

344325
if (package_json != nullptr) {
345326
args.GetReturnValue().Set(package_json->Serialize(realm));
@@ -353,7 +334,8 @@ void BindingData::GetNearestParentPackageJSONType(
353334

354335
Realm* realm = Realm::GetCurrent(args);
355336
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()));
357339

358340
if (package_json == nullptr) {
359341
return;
@@ -390,10 +372,7 @@ void BindingData::GetPackageScopeConfig(
390372
// TODO(@anonrig): Rewrite this function and avoid calling URL parser.
391373
while (true) {
392374
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)) {
397376
break;
398377
}
399378

src/node_modules.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef SRC_NODE_MODULES_H_
22
#define SRC_NODE_MODULES_H_
33

4-
#include "v8-function-callback.h"
54
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
65

76
#include "node.h"
@@ -11,6 +10,7 @@
1110
#include "v8-fast-api-calls.h"
1211
#include "v8.h"
1312

13+
#include <filesystem>
1414
#include <optional>
1515
#include <string>
1616
#include <string_view>
@@ -80,8 +80,8 @@ class BindingData : public SnapshotableObject {
8080
Realm* realm,
8181
std::string_view path,
8282
ErrorContext* error_context = nullptr);
83-
static const PackageConfig* TraverseParent(Realm* realm,
84-
std::string_view check_path);
83+
static const PackageConfig* TraverseParent(
84+
Realm* realm, const std::filesystem::path& check_path);
8585
};
8686

8787
} // namespace modules

0 commit comments

Comments
 (0)