@@ -398,6 +398,7 @@ void BindingData::GetNearestParentPackageJSONType(
398
398
args.GetReturnValue ().Set (value);
399
399
}
400
400
401
+ template <bool return_only_type>
401
402
void BindingData::GetPackageScopeConfig (
402
403
const FunctionCallbackInfo<Value>& args) {
403
404
CHECK_GE (args.Length (), 1 );
@@ -436,7 +437,15 @@ void BindingData::GetPackageScopeConfig(
436
437
error_context.specifier = resolved.ToString ();
437
438
auto package_json = GetPackageJSON (realm, *file_url, &error_context);
438
439
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
+ }
440
449
}
441
450
442
451
auto last_href = std::string (package_json_url->get_href ());
@@ -454,6 +463,12 @@ void BindingData::GetPackageScopeConfig(
454
463
}
455
464
}
456
465
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
457
472
auto package_json_url_as_path =
458
473
url::FileURLToPath (realm->env (), *package_json_url);
459
474
CHECK (package_json_url_as_path);
@@ -526,7 +541,9 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
526
541
target,
527
542
" getNearestParentPackageJSON" ,
528
543
GetNearestParentPackageJSON);
529
- SetMethod (isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig);
544
+ SetMethod (
545
+ isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig<false >);
546
+ SetMethod (isolate, target, " getPackageType" , GetPackageScopeConfig<true >);
530
547
SetMethod (isolate, target, " enableCompileCache" , EnableCompileCache);
531
548
SetMethod (isolate, target, " getCompileCacheDir" , GetCompileCacheDir);
532
549
SetMethod (isolate, target, " flushCompileCache" , FlushCompileCache);
@@ -559,7 +576,8 @@ void BindingData::RegisterExternalReferences(
559
576
registry->Register (ReadPackageJSON);
560
577
registry->Register (GetNearestParentPackageJSONType);
561
578
registry->Register (GetNearestParentPackageJSON);
562
- registry->Register (GetPackageScopeConfig);
579
+ registry->Register (GetPackageScopeConfig<false >);
580
+ registry->Register (GetPackageScopeConfig<true >);
563
581
registry->Register (EnableCompileCache);
564
582
registry->Register (GetCompileCacheDir);
565
583
registry->Register (FlushCompileCache);
0 commit comments