From 46649e7534e3c9234978a30b557754b9c355c14d Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 24 Mar 2025 23:34:22 +0000 Subject: [PATCH 1/3] module: improve `getPackageType` performance --- lib/internal/modules/package_json_reader.js | 4 ++-- src/node_modules.cc | 21 +++++++++++++++++++-- src/node_modules.h | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/internal/modules/package_json_reader.js b/lib/internal/modules/package_json_reader.js index 6e1da13fdc479f..497cdb789632c3 100644 --- a/lib/internal/modules/package_json_reader.js +++ b/lib/internal/modules/package_json_reader.js @@ -185,8 +185,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 = /^\.|%|\\/; diff --git a/src/node_modules.cc b/src/node_modules.cc index e362663053526f..c337617ff860c0 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -404,6 +404,7 @@ void BindingData::GetNearestParentPackageJSONType( } } +template void BindingData::GetPackageScopeConfig( const FunctionCallbackInfo& args) { CHECK_GE(args.Length(), 1); @@ -442,6 +443,13 @@ void BindingData::GetPackageScopeConfig( error_context.specifier = resolved.ToString(); auto package_json = GetPackageJSON(realm, *file_url, &error_context); if (package_json != nullptr) { + if constexpr (return_only_type) { + Local value; + if (ToV8Value(realm->context(), package_json->type).ToLocal(&value)) { + args.GetReturnValue().Set(value); + } + return; + } return args.GetReturnValue().Set(package_json->Serialize(realm)); } @@ -460,6 +468,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 +618,9 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data, target, "getNearestParentPackageJSON", GetNearestParentPackageJSON); - SetMethod(isolate, target, "getPackageScopeConfig", GetPackageScopeConfig); + SetMethod( + isolate, target, "getPackageScopeConfig", GetPackageScopeConfig); + SetMethod(isolate, target, "getPackageType", GetPackageScopeConfig); SetMethod(isolate, target, "enableCompileCache", EnableCompileCache); SetMethod(isolate, target, "getCompileCacheDir", GetCompileCacheDir); SetMethod(isolate, target, "flushCompileCache", FlushCompileCache); @@ -658,7 +674,8 @@ void BindingData::RegisterExternalReferences( registry->Register(ReadPackageJSON); registry->Register(GetNearestParentPackageJSONType); registry->Register(GetNearestParentPackageJSON); - registry->Register(GetPackageScopeConfig); + registry->Register(GetPackageScopeConfig); + registry->Register(GetPackageScopeConfig); registry->Register(EnableCompileCache); registry->Register(GetCompileCacheDir); registry->Register(FlushCompileCache); diff --git a/src/node_modules.h b/src/node_modules.h index 17909b2270454b..eb2900d8f83852 100644 --- a/src/node_modules.h +++ b/src/node_modules.h @@ -59,6 +59,7 @@ class BindingData : public SnapshotableObject { const v8::FunctionCallbackInfo& args); static void GetNearestParentPackageJSONType( const v8::FunctionCallbackInfo& args); + template static void GetPackageScopeConfig( const v8::FunctionCallbackInfo& args); static void GetPackageJSONScripts( From bd1545192c776bcc66963448f4336bef94b3b133 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Tue, 25 Mar 2025 15:24:04 +0000 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: James M Snell --- src/node_modules.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/node_modules.cc b/src/node_modules.cc index c337617ff860c0..8b1031cd19e220 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -449,8 +449,9 @@ void BindingData::GetPackageScopeConfig( args.GetReturnValue().Set(value); } return; + } else { + return args.GetReturnValue().Set(package_json->Serialize(realm)); } - return args.GetReturnValue().Set(package_json->Serialize(realm)); } auto last_href = std::string(package_json_url->get_href()); From d242ecffd7f45e5c6091fe07581525be192af7d5 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Fri, 28 Mar 2025 20:19:55 +0000 Subject: [PATCH 3/3] add `getPackageType` to typings as well --- typings/internalBinding/modules.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/typings/internalBinding/modules.d.ts b/typings/internalBinding/modules.d.ts index e19c662ded3379..0b1d0e2938319f 100644 --- a/typings/internalBinding/modules.d.ts +++ b/typings/internalBinding/modules.d.ts @@ -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