@@ -404,6 +404,7 @@ void BindingData::GetNearestParentPackageJSONType(
404
404
}
405
405
}
406
406
407
+ template <bool return_only_type>
407
408
void BindingData::GetPackageScopeConfig (
408
409
const FunctionCallbackInfo<Value>& args) {
409
410
CHECK_GE (args.Length (), 1 );
@@ -442,7 +443,15 @@ void BindingData::GetPackageScopeConfig(
442
443
error_context.specifier = resolved.ToString ();
443
444
auto package_json = GetPackageJSON (realm, *file_url, &error_context);
444
445
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
+ }
446
455
}
447
456
448
457
auto last_href = std::string (package_json_url->get_href ());
@@ -460,6 +469,12 @@ void BindingData::GetPackageScopeConfig(
460
469
}
461
470
}
462
471
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
463
478
auto package_json_url_as_path =
464
479
url::FileURLToPath (realm->env (), *package_json_url);
465
480
CHECK (package_json_url_as_path);
@@ -604,7 +619,9 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
604
619
target,
605
620
" getNearestParentPackageJSON" ,
606
621
GetNearestParentPackageJSON);
607
- SetMethod (isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig);
622
+ SetMethod (
623
+ isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig<false >);
624
+ SetMethod (isolate, target, " getPackageType" , GetPackageScopeConfig<true >);
608
625
SetMethod (isolate, target, " enableCompileCache" , EnableCompileCache);
609
626
SetMethod (isolate, target, " getCompileCacheDir" , GetCompileCacheDir);
610
627
SetMethod (isolate, target, " flushCompileCache" , FlushCompileCache);
@@ -658,7 +675,8 @@ void BindingData::RegisterExternalReferences(
658
675
registry->Register (ReadPackageJSON);
659
676
registry->Register (GetNearestParentPackageJSONType);
660
677
registry->Register (GetNearestParentPackageJSON);
661
- registry->Register (GetPackageScopeConfig);
678
+ registry->Register (GetPackageScopeConfig<false >);
679
+ registry->Register (GetPackageScopeConfig<true >);
662
680
registry->Register (EnableCompileCache);
663
681
registry->Register (GetCompileCacheDir);
664
682
registry->Register (FlushCompileCache);
0 commit comments