Skip to content

Commit 759229d

Browse files
module: improve getPackageType performance
1 parent ffc1cf6 commit 759229d

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

lib/internal/modules/package_json_reader.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ 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+
return modulesBinding.getPackageType(`${url}`);
190189
}
191190

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

src/node_modules.cc

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,23 +404,22 @@ void BindingData::GetNearestParentPackageJSONType(
404404
}
405405
}
406406

407-
void BindingData::GetPackageScopeConfig(
408-
const FunctionCallbackInfo<Value>& args) {
407+
const BindingData::PackageConfig* BindingData::FindPackageScopeConfig(
408+
Realm* realm, const FunctionCallbackInfo<Value>& args) {
409409
CHECK_GE(args.Length(), 1);
410410
CHECK(args[0]->IsString());
411-
412-
Realm* realm = Realm::GetCurrent(args);
413411
Utf8Value resolved(realm->isolate(), args[0]);
414412
auto package_json_url_base = ada::parse(resolved.ToStringView());
415413
if (!package_json_url_base) {
416414
url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt);
417-
return;
415+
return nullptr;
418416
}
417+
419418
auto package_json_url =
420419
ada::parse("./package.json", &package_json_url_base.value());
421420
if (!package_json_url) {
422421
url::ThrowInvalidURL(realm->env(), "./package.json", resolved.ToString());
423-
return;
422+
return nullptr;
424423
}
425424

426425
std::string_view node_modules_package_path = "/node_modules/package.json";
@@ -437,20 +436,20 @@ void BindingData::GetPackageScopeConfig(
437436
auto file_url = url::FileURLToPath(realm->env(), *package_json_url);
438437
if (!file_url) {
439438
url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt);
440-
return;
439+
return nullptr;
441440
}
442441
error_context.specifier = resolved.ToString();
443442
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
444443
if (package_json != nullptr) {
445-
return args.GetReturnValue().Set(package_json->Serialize(realm));
444+
return package_json;
446445
}
447446

448447
auto last_href = std::string(package_json_url->get_href());
449448
auto last_pathname = std::string(package_json_url->get_pathname());
450449
package_json_url = ada::parse("../package.json", &package_json_url.value());
451450
if (!package_json_url) {
452451
url::ThrowInvalidURL(realm->env(), "../package.json", last_href);
453-
return;
452+
return nullptr;
454453
}
455454

456455
// Terminates at root where ../package.json equals ../../package.json
@@ -460,6 +459,23 @@ void BindingData::GetPackageScopeConfig(
460459
}
461460
}
462461

462+
return nullptr;
463+
}
464+
465+
void BindingData::GetPackageScopeConfig(
466+
const FunctionCallbackInfo<Value>& args) {
467+
Realm* realm = Realm::GetCurrent(args);
468+
469+
auto package_json = FindPackageScopeConfig(realm, args);
470+
if (package_json != nullptr) {
471+
return args.GetReturnValue().Set(package_json->Serialize(realm));
472+
}
473+
474+
Utf8Value resolved(realm->isolate(), args[0]);
475+
auto package_json_url_base = ada::parse(resolved.ToStringView());
476+
auto package_json_url =
477+
ada::parse("./package.json", &package_json_url_base.value());
478+
463479
auto package_json_url_as_path =
464480
url::FileURLToPath(realm->env(), *package_json_url);
465481
CHECK(package_json_url_as_path);
@@ -470,6 +486,22 @@ void BindingData::GetPackageScopeConfig(
470486
}
471487
}
472488

489+
void BindingData::GetPackageType(const FunctionCallbackInfo<Value>& args) {
490+
Realm* realm = Realm::GetCurrent(args);
491+
492+
auto package_scope = FindPackageScopeConfig(realm, args);
493+
494+
std::string resultType = "none";
495+
496+
if (package_scope != nullptr) {
497+
resultType = package_scope->type;
498+
}
499+
500+
return args.GetReturnValue().Set(
501+
String::NewFromUtf8(realm->isolate(), resultType.c_str())
502+
.ToLocalChecked());
503+
}
504+
473505
void FlushCompileCache(const FunctionCallbackInfo<Value>& args) {
474506
Isolate* isolate = args.GetIsolate();
475507
Local<Context> context = isolate->GetCurrentContext();
@@ -605,6 +637,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
605637
"getNearestParentPackageJSON",
606638
GetNearestParentPackageJSON);
607639
SetMethod(isolate, target, "getPackageScopeConfig", GetPackageScopeConfig);
640+
SetMethod(isolate, target, "getPackageType", GetPackageType);
608641
SetMethod(isolate, target, "enableCompileCache", EnableCompileCache);
609642
SetMethod(isolate, target, "getCompileCacheDir", GetCompileCacheDir);
610643
SetMethod(isolate, target, "flushCompileCache", FlushCompileCache);
@@ -659,6 +692,7 @@ void BindingData::RegisterExternalReferences(
659692
registry->Register(GetNearestParentPackageJSONType);
660693
registry->Register(GetNearestParentPackageJSON);
661694
registry->Register(GetPackageScopeConfig);
695+
registry->Register(GetPackageType);
662696
registry->Register(EnableCompileCache);
663697
registry->Register(GetCompileCacheDir);
664698
registry->Register(FlushCompileCache);

src/node_modules.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ class BindingData : public SnapshotableObject {
5959
const v8::FunctionCallbackInfo<v8::Value>& args);
6060
static void GetNearestParentPackageJSONType(
6161
const v8::FunctionCallbackInfo<v8::Value>& args);
62+
static const BindingData::PackageConfig* FindPackageScopeConfig(
63+
Realm* realm, const v8::FunctionCallbackInfo<v8::Value>& args);
6264
static void GetPackageScopeConfig(
6365
const v8::FunctionCallbackInfo<v8::Value>& args);
66+
static void GetPackageType(
67+
const v8::FunctionCallbackInfo<v8::Value>& args);
6468
static void GetPackageJSONScripts(
6569
const v8::FunctionCallbackInfo<v8::Value>& args);
6670

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']
2829
enableCompileCache(path?: string): { status: number, message?: string, directory?: string }
2930
getCompileCacheDir(): string | undefined
3031
flushCompileCache(keepDeserializedCache?: boolean): void

0 commit comments

Comments
 (0)