Skip to content

Commit 8c25465

Browse files
module: improve getPackageType performance
PR-URL: #57599 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ulises Gascón <[email protected]>
1 parent d7a1565 commit 8c25465

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)