Skip to content

Commit a540689

Browse files
dario-piotrowiczRafaelGSS
authored andcommitted
module: improve getPackageType performance
PR-URL: #57599 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ulises Gascón <ulisesgascongonzalez@gmail.com>
1 parent 6adbbe2 commit a540689

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

lib/internal/modules/package_json_reader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ function getPackageScopeConfig(resolved) {
174174
* @param {URL} url - The URL to get the package type for.
175175
*/
176176
function getPackageType(url) {
177-
// TODO(@anonrig): Write a C++ function that returns only "type".
178-
return getPackageScopeConfig(url).type;
177+
const type = modulesBinding.getPackageType(`${url}`);
178+
return type ?? 'none';
179179
}
180180

181181
const invalidPackageNameRegEx = /^\.|%|\\/;

src/node_modules.cc

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ void BindingData::GetNearestParentPackageJSONType(
404404
}
405405
}
406406

407+
template <bool return_only_type>
407408
void BindingData::GetPackageScopeConfig(
408409
const FunctionCallbackInfo<Value>& args) {
409410
CHECK_GE(args.Length(), 1);
@@ -442,7 +443,15 @@ void BindingData::GetPackageScopeConfig(
442443
error_context.specifier = resolved.ToString();
443444
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
444445
if (package_json != nullptr) {
445-
return args.GetReturnValue().Set(package_json->Serialize(realm));
446+
if constexpr (return_only_type) {
447+
Local<Value> value;
448+
if (ToV8Value(realm->context(), package_json->type).ToLocal(&value)) {
449+
args.GetReturnValue().Set(value);
450+
}
451+
return;
452+
} else {
453+
return args.GetReturnValue().Set(package_json->Serialize(realm));
454+
}
446455
}
447456

448457
auto last_href = std::string(package_json_url->get_href());
@@ -460,6 +469,12 @@ void BindingData::GetPackageScopeConfig(
460469
}
461470
}
462471

472+
if constexpr (return_only_type) {
473+
return;
474+
}
475+
476+
// If the package.json could not be found return a string containing a path
477+
// to the non-existent package.json file in the initial requested location
463478
auto package_json_url_as_path =
464479
url::FileURLToPath(realm->env(), *package_json_url);
465480
CHECK(package_json_url_as_path);
@@ -604,7 +619,9 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
604619
target,
605620
"getNearestParentPackageJSON",
606621
GetNearestParentPackageJSON);
607-
SetMethod(isolate, target, "getPackageScopeConfig", GetPackageScopeConfig);
622+
SetMethod(
623+
isolate, target, "getPackageScopeConfig", GetPackageScopeConfig<false>);
624+
SetMethod(isolate, target, "getPackageType", GetPackageScopeConfig<true>);
608625
SetMethod(isolate, target, "enableCompileCache", EnableCompileCache);
609626
SetMethod(isolate, target, "getCompileCacheDir", GetCompileCacheDir);
610627
SetMethod(isolate, target, "flushCompileCache", FlushCompileCache);
@@ -658,7 +675,8 @@ void BindingData::RegisterExternalReferences(
658675
registry->Register(ReadPackageJSON);
659676
registry->Register(GetNearestParentPackageJSONType);
660677
registry->Register(GetNearestParentPackageJSON);
661-
registry->Register(GetPackageScopeConfig);
678+
registry->Register(GetPackageScopeConfig<false>);
679+
registry->Register(GetPackageScopeConfig<true>);
662680
registry->Register(EnableCompileCache);
663681
registry->Register(GetCompileCacheDir);
664682
registry->Register(FlushCompileCache);

src/node_modules.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class BindingData : public SnapshotableObject {
5959
const v8::FunctionCallbackInfo<v8::Value>& args);
6060
static void GetNearestParentPackageJSONType(
6161
const v8::FunctionCallbackInfo<v8::Value>& args);
62+
template <bool return_only_type>
6263
static void GetPackageScopeConfig(
6364
const v8::FunctionCallbackInfo<v8::Value>& args);
6465
static void GetPackageJSONScripts(

typings/internalBinding/modules.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface ModulesBinding {
2525
getNearestParentPackageJSONType(path: string): PackageConfig['type']
2626
getNearestParentPackageJSON(path: string): SerializedPackageConfig | undefined
2727
getPackageScopeConfig(path: string): SerializedPackageConfig | undefined
28+
getPackageType(path: string): PackageConfig['type'] | undefined
2829
enableCompileCache(path?: string): { status: number, message?: string, directory?: string }
2930
getCompileCacheDir(): string | undefined
3031
flushCompileCache(keepDeserializedCache?: boolean): void

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy