mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00
module: improve getPackageType
performance
PR-URL: https://github.com/nodejs/node/pull/57599 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
This commit is contained in:
parent
d7a1565350
commit
8c254658bb
4 changed files with 25 additions and 5 deletions
|
@ -174,8 +174,8 @@ function getPackageScopeConfig(resolved) {
|
|||
* @param {URL} url - The URL to get the package type for.
|
||||
*/
|
||||
function getPackageType(url) {
|
||||
// TODO(@anonrig): Write a C++ function that returns only "type".
|
||||
return getPackageScopeConfig(url).type;
|
||||
const type = modulesBinding.getPackageType(`${url}`);
|
||||
return type ?? 'none';
|
||||
}
|
||||
|
||||
const invalidPackageNameRegEx = /^\.|%|\\/;
|
||||
|
|
|
@ -404,6 +404,7 @@ void BindingData::GetNearestParentPackageJSONType(
|
|||
}
|
||||
}
|
||||
|
||||
template <bool return_only_type>
|
||||
void BindingData::GetPackageScopeConfig(
|
||||
const FunctionCallbackInfo<Value>& args) {
|
||||
CHECK_GE(args.Length(), 1);
|
||||
|
@ -442,7 +443,15 @@ void BindingData::GetPackageScopeConfig(
|
|||
error_context.specifier = resolved.ToString();
|
||||
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
|
||||
if (package_json != nullptr) {
|
||||
return args.GetReturnValue().Set(package_json->Serialize(realm));
|
||||
if constexpr (return_only_type) {
|
||||
Local<Value> value;
|
||||
if (ToV8Value(realm->context(), package_json->type).ToLocal(&value)) {
|
||||
args.GetReturnValue().Set(value);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
return args.GetReturnValue().Set(package_json->Serialize(realm));
|
||||
}
|
||||
}
|
||||
|
||||
auto last_href = std::string(package_json_url->get_href());
|
||||
|
@ -460,6 +469,12 @@ void BindingData::GetPackageScopeConfig(
|
|||
}
|
||||
}
|
||||
|
||||
if constexpr (return_only_type) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the package.json could not be found return a string containing a path
|
||||
// to the non-existent package.json file in the initial requested location
|
||||
auto package_json_url_as_path =
|
||||
url::FileURLToPath(realm->env(), *package_json_url);
|
||||
CHECK(package_json_url_as_path);
|
||||
|
@ -604,7 +619,9 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
|
|||
target,
|
||||
"getNearestParentPackageJSON",
|
||||
GetNearestParentPackageJSON);
|
||||
SetMethod(isolate, target, "getPackageScopeConfig", GetPackageScopeConfig);
|
||||
SetMethod(
|
||||
isolate, target, "getPackageScopeConfig", GetPackageScopeConfig<false>);
|
||||
SetMethod(isolate, target, "getPackageType", GetPackageScopeConfig<true>);
|
||||
SetMethod(isolate, target, "enableCompileCache", EnableCompileCache);
|
||||
SetMethod(isolate, target, "getCompileCacheDir", GetCompileCacheDir);
|
||||
SetMethod(isolate, target, "flushCompileCache", FlushCompileCache);
|
||||
|
@ -658,7 +675,8 @@ void BindingData::RegisterExternalReferences(
|
|||
registry->Register(ReadPackageJSON);
|
||||
registry->Register(GetNearestParentPackageJSONType);
|
||||
registry->Register(GetNearestParentPackageJSON);
|
||||
registry->Register(GetPackageScopeConfig);
|
||||
registry->Register(GetPackageScopeConfig<false>);
|
||||
registry->Register(GetPackageScopeConfig<true>);
|
||||
registry->Register(EnableCompileCache);
|
||||
registry->Register(GetCompileCacheDir);
|
||||
registry->Register(FlushCompileCache);
|
||||
|
|
|
@ -59,6 +59,7 @@ class BindingData : public SnapshotableObject {
|
|||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetNearestParentPackageJSONType(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
template <bool return_only_type>
|
||||
static void GetPackageScopeConfig(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetPackageJSONScripts(
|
||||
|
|
1
typings/internalBinding/modules.d.ts
vendored
1
typings/internalBinding/modules.d.ts
vendored
|
@ -25,6 +25,7 @@ export interface ModulesBinding {
|
|||
getNearestParentPackageJSONType(path: string): PackageConfig['type']
|
||||
getNearestParentPackageJSON(path: string): SerializedPackageConfig | undefined
|
||||
getPackageScopeConfig(path: string): SerializedPackageConfig | undefined
|
||||
getPackageType(path: string): PackageConfig['type'] | undefined
|
||||
enableCompileCache(path?: string): { status: number, message?: string, directory?: string }
|
||||
getCompileCacheDir(): string | undefined
|
||||
flushCompileCache(keepDeserializedCache?: boolean): void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue