mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
src: cache missing package.json files in the C++ package config cache
PR-URL: https://github.com/nodejs/node/pull/60425 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
c1aa9f49cd
commit
bb9e4e0784
4 files changed
+128
-89
No files matched your search
+54
-12
@@ -98,15 +98,27 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
|
||||
|
||||
auto cache_entry = binding_data->package_configs_.find(path.data());
|
||||
if (cache_entry != binding_data->package_configs_.end()) {
|
||||
return &cache_entry->second;
|
||||
auto& cache_value = cache_entry->second;
|
||||
if (cache_value) {
|
||||
return &*cache_value;
|
||||
}
|
||||
|
||||
// If we have a cache entry without a value, we've already
|
||||
// attempted to open and read this path and couldn't (it most
|
||||
// likely doesn't exist)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PackageConfig package_config{};
|
||||
package_config.file_path = path;
|
||||
// No need to exclude BOM since simdjson will skip it.
|
||||
if (ReadFileSync(&package_config.raw_json, path.data()) < 0) {
|
||||
// Add `nullopt` to the package config cache so that we don't
|
||||
// need to open and attempt to read this path again
|
||||
binding_data->package_configs_.insert({std::string(path), std::nullopt});
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
simdjson::ondemand::document document;
|
||||
simdjson::ondemand::object main_object;
|
||||
simdjson::error_code error =
|
||||
@@ -239,7 +251,7 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON(
|
||||
auto cached = binding_data->package_configs_.insert(
|
||||
{std::string(path), std::move(package_config)});
|
||||
|
||||
return &cached.first->second;
|
||||
return &*cached.first->second;
|
||||
}
|
||||
|
||||
void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {
|
||||
@@ -322,6 +334,40 @@ const BindingData::PackageConfig* BindingData::TraverseParent(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::filesystem::path BindingData::NormalizePath(
|
||||
Realm* realm, BufferValue* path_value) {
|
||||
// Check if the path has a trailing slash. If so, add it after
|
||||
// ToNamespacedPath() as it will be deleted by ToNamespacedPath()
|
||||
bool slashCheck = path_value->ToStringView().ends_with(kPathSeparator);
|
||||
|
||||
ToNamespacedPath(realm->env(), path_value);
|
||||
|
||||
auto path = path_value->ToPath();
|
||||
|
||||
if (slashCheck) {
|
||||
path /= "";
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
void BindingData::GetNearestParentPackageJSON(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
CHECK_GE(args.Length(), 1);
|
||||
CHECK(args[0]->IsString());
|
||||
|
||||
Realm* realm = Realm::GetCurrent(args);
|
||||
BufferValue path_value(realm->isolate(), args[0]);
|
||||
|
||||
auto path = NormalizePath(realm, &path_value);
|
||||
|
||||
auto package_json = TraverseParent(realm, path);
|
||||
|
||||
if (package_json != nullptr) {
|
||||
args.GetReturnValue().Set(package_json->Serialize(realm));
|
||||
}
|
||||
}
|
||||
|
||||
void BindingData::GetNearestParentPackageJSONType(
|
||||
const FunctionCallbackInfo<Value>& args) {
|
||||
CHECK_GE(args.Length(), 1);
|
||||
@@ -329,17 +375,8 @@ void BindingData::GetNearestParentPackageJSONType(
|
||||
|
||||
Realm* realm = Realm::GetCurrent(args);
|
||||
BufferValue path_value(realm->isolate(), args[0]);
|
||||
// Check if the path has a trailing slash. If so, add it after
|
||||
// ToNamespacedPath() as it will be deleted by ToNamespacedPath()
|
||||
bool slashCheck = path_value.ToStringView().ends_with(kPathSeparator);
|
||||
|
||||
ToNamespacedPath(realm->env(), &path_value);
|
||||
|
||||
auto path = path_value.ToPath();
|
||||
|
||||
if (slashCheck) {
|
||||
path /= "";
|
||||
}
|
||||
auto path = NormalizePath(realm, &path_value);
|
||||
|
||||
auto package_json = TraverseParent(realm, path);
|
||||
|
||||
@@ -640,6 +677,10 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
|
||||
target,
|
||||
"getNearestParentPackageJSONType",
|
||||
GetNearestParentPackageJSONType);
|
||||
SetMethod(isolate,
|
||||
target,
|
||||
"getNearestParentPackageJSON",
|
||||
GetNearestParentPackageJSON);
|
||||
SetMethod(
|
||||
isolate, target, "getPackageScopeConfig", GetPackageScopeConfig<false>);
|
||||
SetMethod(isolate, target, "getPackageType", GetPackageScopeConfig<true>);
|
||||
@@ -696,6 +737,7 @@ void BindingData::RegisterExternalReferences(
|
||||
ExternalReferenceRegistry* registry) {
|
||||
registry->Register(ReadPackageJSON);
|
||||
registry->Register(GetNearestParentPackageJSONType);
|
||||
registry->Register(GetNearestParentPackageJSON);
|
||||
registry->Register(GetPackageScopeConfig<false>);
|
||||
registry->Register(GetPackageScopeConfig<true>);
|
||||
registry->Register(EnableCompileCache);
|
||||
|
||||
Reference in new issue
Block a user