Skip to content

Commit bb09b4d

Browse files
dario-piotrowiczaduh95
authored andcommitted
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 9e6054e commit bb09b4d

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
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
@@ -398,6 +398,7 @@ void BindingData::GetNearestParentPackageJSONType(
398398
args.GetReturnValue().Set(value);
399399
}
400400

401+
template <bool return_only_type>
401402
void BindingData::GetPackageScopeConfig(
402403
const FunctionCallbackInfo<Value>& args) {
403404
CHECK_GE(args.Length(), 1);
@@ -436,7 +437,15 @@ void BindingData::GetPackageScopeConfig(
436437
error_context.specifier = resolved.ToString();
437438
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
438439
if (package_json != nullptr) {
439-
return args.GetReturnValue().Set(package_json->Serialize(realm));
440+
if constexpr (return_only_type) {
441+
Local<Value> value;
442+
if (ToV8Value(realm->context(), package_json->type).ToLocal(&value)) {
443+
args.GetReturnValue().Set(value);
444+
}
445+
return;
446+
} else {
447+
return args.GetReturnValue().Set(package_json->Serialize(realm));
448+
}
440449
}
441450

442451
auto last_href = std::string(package_json_url->get_href());
@@ -454,6 +463,12 @@ void BindingData::GetPackageScopeConfig(
454463
}
455464
}
456465

466+
if constexpr (return_only_type) {
467+
return;
468+
}
469+
470+
// If the package.json could not be found return a string containing a path
471+
// to the non-existent package.json file in the initial requested location
457472
auto package_json_url_as_path =
458473
url::FileURLToPath(realm->env(), *package_json_url);
459474
CHECK(package_json_url_as_path);
@@ -526,7 +541,9 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
526541
target,
527542
"getNearestParentPackageJSON",
528543
GetNearestParentPackageJSON);
529-
SetMethod(isolate, target, "getPackageScopeConfig", GetPackageScopeConfig);
544+
SetMethod(
545+
isolate, target, "getPackageScopeConfig", GetPackageScopeConfig<false>);
546+
SetMethod(isolate, target, "getPackageType", GetPackageScopeConfig<true>);
530547
SetMethod(isolate, target, "enableCompileCache", EnableCompileCache);
531548
SetMethod(isolate, target, "getCompileCacheDir", GetCompileCacheDir);
532549
SetMethod(isolate, target, "flushCompileCache", FlushCompileCache);
@@ -559,7 +576,8 @@ void BindingData::RegisterExternalReferences(
559576
registry->Register(ReadPackageJSON);
560577
registry->Register(GetNearestParentPackageJSONType);
561578
registry->Register(GetNearestParentPackageJSON);
562-
registry->Register(GetPackageScopeConfig);
579+
registry->Register(GetPackageScopeConfig<false>);
580+
registry->Register(GetPackageScopeConfig<true>);
563581
registry->Register(EnableCompileCache);
564582
registry->Register(GetCompileCacheDir);
565583
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ export interface ModulesBinding {
2626
getNearestRawParentPackageJSON(origin: URL['pathname']): [ReturnType<JSON['stringify']>, DeserializedPackageConfig['path']] | undefined
2727
getNearestParentPackageJSONType(path: string): PackageConfig['type']
2828
getPackageScopeConfig(path: string): SerializedPackageConfig | undefined
29-
getPackageJSONScripts(): string | undefined
29+
getPackageType(path: string): PackageConfig['type'] | undefined
30+
enableCompileCache(path?: string): { status: number, message?: string, directory?: string }
31+
getCompileCacheDir(): string | undefined
32+
flushCompileCache(keepDeserializedCache?: boolean): void
3033
}

0 commit comments

Comments
 (0)
close