@@ -404,6 +404,7 @@ void BindingData::GetNearestParentPackageJSONType(
404404 }
405405}
406406
407+ template <bool return_only_type>
407408void BindingData::GetPackageScopeConfig (
408409 const FunctionCallbackInfo<Value>& args) {
409410 CHECK_GE (args.Length (), 1 );
@@ -442,7 +443,15 @@ void BindingData::GetPackageScopeConfig(
442443 error_context.specifier = resolved.ToString ();
443444 auto package_json = GetPackageJSON (realm, *file_url, &error_context);
444445 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+ }
446455 }
447456
448457 auto last_href = std::string (package_json_url->get_href ());
@@ -460,6 +469,12 @@ void BindingData::GetPackageScopeConfig(
460469 }
461470 }
462471
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
463478 auto package_json_url_as_path =
464479 url::FileURLToPath (realm->env (), *package_json_url);
465480 CHECK (package_json_url_as_path);
@@ -604,7 +619,9 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
604619 target,
605620 " getNearestParentPackageJSON" ,
606621 GetNearestParentPackageJSON);
607- SetMethod (isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig);
622+ SetMethod (
623+ isolate, target, " getPackageScopeConfig" , GetPackageScopeConfig<false >);
624+ SetMethod (isolate, target, " getPackageType" , GetPackageScopeConfig<true >);
608625 SetMethod (isolate, target, " enableCompileCache" , EnableCompileCache);
609626 SetMethod (isolate, target, " getCompileCacheDir" , GetCompileCacheDir);
610627 SetMethod (isolate, target, " flushCompileCache" , FlushCompileCache);
@@ -658,7 +675,8 @@ void BindingData::RegisterExternalReferences(
658675 registry->Register (ReadPackageJSON);
659676 registry->Register (GetNearestParentPackageJSONType);
660677 registry->Register (GetNearestParentPackageJSON);
661- registry->Register (GetPackageScopeConfig);
678+ registry->Register (GetPackageScopeConfig<false >);
679+ registry->Register (GetPackageScopeConfig<true >);
662680 registry->Register (EnableCompileCache);
663681 registry->Register (GetCompileCacheDir);
664682 registry->Register (FlushCompileCache);
0 commit comments