Skip to content

Commit 46649e7

Browse files
module: improve getPackageType performance
1 parent f95e43e commit 46649e7

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

lib/internal/modules/package_json_reader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ function getPackageScopeConfig(resolved) {
185185
* @param {URL} url - The URL to get the package type for.
186186
*/
187187
function getPackageType(url) {
188-
// TODO(@anonrig): Write a C++ function that returns only "type".
189-
return getPackageScopeConfig(url).type;
188+
const type = modulesBinding.getPackageType(`${url}`);
189+
return type ?? 'none';
190190
}
191191

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

src/node_modules.cc

Lines changed: 19 additions & 2 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,6 +443,13 @@ void BindingData::GetPackageScopeConfig(
442443
error_context.specifier = resolved.ToString();
443444
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
444445
if (package_json != nullptr) {
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+
}
445453
return args.GetReturnValue().Set(package_json->Serialize(realm));
446454
}
447455

@@ -460,6 +468,12 @@ void BindingData::GetPackageScopeConfig(
460468
}
461469
}
462470

471+
if constexpr (return_only_type) {
472+
return;
473+
}
474+
475+
// If the package.json could not be found return a string containing a path
476+
// to the non-existent package.json file in the initial requested location
463477
auto package_json_url_as_path =
464478
url::FileURLToPath(realm->env(), *package_json_url);
465479
CHECK(package_json_url_as_path);
@@ -604,7 +618,9 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
604618
target,
605619
"getNearestParentPackageJSON",
606620
GetNearestParentPackageJSON);
607-
SetMethod(isolate, target, "getPackageScopeConfig", GetPackageScopeConfig);
621+
SetMethod(
622+
isolate, target, "getPackageScopeConfig", GetPackageScopeConfig<false>);
623+
SetMethod(isolate, target, "getPackageType", GetPackageScopeConfig<true>);
608624
SetMethod(isolate, target, "enableCompileCache", EnableCompileCache);
609625
SetMethod(isolate, target, "getCompileCacheDir", GetCompileCacheDir);
610626
SetMethod(isolate, target, "flushCompileCache", FlushCompileCache);
@@ -658,7 +674,8 @@ void BindingData::RegisterExternalReferences(
658674
registry->Register(ReadPackageJSON);
659675
registry->Register(GetNearestParentPackageJSONType);
660676
registry->Register(GetNearestParentPackageJSON);
661-
registry->Register(GetPackageScopeConfig);
677+
registry->Register(GetPackageScopeConfig<false>);
678+
registry->Register(GetPackageScopeConfig<true>);
662679
registry->Register(EnableCompileCache);
663680
registry->Register(GetCompileCacheDir);
664681
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(

0 commit comments

Comments
 (0)