@@ -398,6 +398,7 @@ void BindingData::GetNearestParentPackageJSONType(
398398 args.GetReturnValue ().Set (value);
399399}
400400
401+ template <bool return_only_type>
401402void 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);
0 commit comments