@@ -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+
473505void 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);
0 commit comments