Skip to content

Commit c261f0c

Browse files
committed
fixup! src: avoid URL parsing in GetPackageScopeConfig
1 parent 241534e commit c261f0c

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎src/node_modules.cc‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,14 @@ void BindingData::GetPackageScopeConfig(
591591
return;
592592
}
593593

594-
// Walking one string avoids a URL parse per directory level.
594+
// Avoids a URL parse per level.
595595
std::string& path = *file_path;
596596
static constexpr std::string_view kPackageJSON = "package.json";
597597
size_t root_end = 0;
598598
#ifdef _WIN32
599599
static constexpr std::string_view kNodeModulesPackagePath =
600600
"\\node_modules\\package.json";
601-
// Do not walk above the drive ("C:") or the UNC host ("\\host").
601+
// Stop at the drive ("C:") or UNC host ("\\host").
602602
root_end = path.starts_with("\\\\") ? path.find('\\', 2) : 2;
603603
#else
604604
static constexpr std::string_view kNodeModulesPackagePath =
@@ -607,13 +607,12 @@ void BindingData::GetPackageScopeConfig(
607607

608608
while (!path.ends_with(kNodeModulesPackagePath)) {
609609
#ifdef _WIN32
610-
// Cache keys must match the namespaced paths ReadPackageJSON() inserts.
611-
// TODO(araujogui): This creates a V8 string per level. Add a string
612-
// ToNamespacedPath() overload if it shows up in profiles.
610+
// Cache keys are namespaced.
611+
// TODO(araujogui): Add a std::string ToNamespacedPath() overload.
613612
BufferValue file_path_buf(realm->isolate(),
614613
String::NewFromUtf8(realm->isolate(),
615614
path.c_str(),
616-
NewStringType::kInternalized,
615+
NewStringType::kNormal,
617616
path.size())
618617
.ToLocalChecked());
619618
ToNamespacedPath(realm->env(), &file_path_buf);
@@ -638,7 +637,13 @@ void BindingData::GetPackageScopeConfig(
638637
if (dir_end <= root_end) {
639638
break;
640639
}
641-
path.resize(path.rfind(kPathSeparator, dir_end - 1) + 1);
640+
const size_t parent_end = path.rfind(kPathSeparator, dir_end - 1);
641+
// Drive-relative, from "file:///C:foo/x.js". The URL walk threw here.
642+
if (parent_end == std::string::npos) {
643+
url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt);
644+
return;
645+
}
646+
path.resize(parent_end + 1);
642647
path += kPackageJSON;
643648
}
644649

‎test/parallel/test-find-package-json.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ describe('findPackageJSON', () => { // Throws when no arguments are provided
4848
);
4949
});
5050

51+
it('should reject a drive-relative file URL', { skip: !common.isWindows }, () => {
52+
assert.throws(
53+
() => findPackageJSON('pkg', 'file:///C:foo/x.js'),
54+
{ code: 'ERR_INVALID_URL' },
55+
);
56+
});
57+
5158
it('should be able to crawl up (CJS)', () => {
5259
const pathToMod = fixtures.path('packages/nested/sub-pkg-cjs/index.js');
5360
const parentPkg = require(pathToMod);

0 commit comments

Comments
 (0)