Skip to content

Commit ec44965

Browse files
anonrigtargos
authored andcommittedJun 1, 2024
src: simplify node modules traverse path
PR-URL: #53061 Reviewed-By: Daniel Lemire <daniel@lemire.me> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent cfcde78 commit ec44965

File tree

2 files changed

+18
-34
lines changed

2 files changed

+18
-34
lines changed
 

‎src/node_modules.cc

+15-31
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,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

‎src/node_modules.h

+3-3
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)
Please sign in to comment.