@@ -2899,45 +2899,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
28992899 CHECK (args[0 ]->IsString ());
29002900
29012901 Environment* env = Environment::GetCurrent (args);
2902+ auto isolate = env->isolate ();
29022903
2903- Utf8Value utf8_package_json_url (env-> isolate () , args[0 ]. As <String>() );
2904+ Utf8Value utf8_package_json_url (isolate, args[0 ]);
29042905 auto package_json_url =
29052906 ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView ());
29062907
29072908 if (!package_json_url) {
2908- env->isolate ()->ThrowException (
2909- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2910-
2909+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
29112910 return ;
29122911 }
29132912
29142913 ada::result<ada::url_aggregator> file_path_url;
2915- std::string initial_file_path;
2914+ std::optional<std:: string> initial_file_path;
29162915 std::string file_path;
29172916
2918- if (args.Length () >= 2 && !args[1 ]->IsNullOrUndefined () &&
2919- args[1 ]->IsString ()) {
2920- std::string package_config_main =
2921- Utf8Value (env->isolate (), args[1 ].As <String>()).ToString ();
2917+ if (args.Length () >= 2 && args[1 ]->IsString ()) {
2918+ auto package_config_main = Utf8Value (isolate, args[1 ]).ToString ();
29222919
29232920 file_path_url = ada::parse<ada::url_aggregator>(
29242921 std::string (" ./" ) + package_config_main, &package_json_url.value ());
29252922
29262923 if (!file_path_url) {
2927- env->isolate ()->ThrowException (
2928- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2929-
2924+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
29302925 return ;
29312926 }
29322927
2933- if (! node::url::FileURLToPath (
2934- env, file_path_url. value (), initial_file_path))
2928+ initial_file_path = node::url::FileURLToPath (env, *file_path_url);
2929+ if (!initial_file_path. has_value ()) {
29352930 return ;
2931+ }
29362932
2937- FromNamespacedPath (&initial_file_path);
2933+ FromNamespacedPath (&initial_file_path. value () );
29382934
29392935 for (int i = 0 ; i < legacy_main_extensions_with_main_end; i++) {
2940- file_path = initial_file_path + std::string (legacy_main_extensions[i]);
2936+ file_path = * initial_file_path + std::string (legacy_main_extensions[i]);
29412937
29422938 switch (FilePathIsFile (env, file_path)) {
29432939 case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2960,21 +2956,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29602956 ada::parse<ada::url_aggregator>(" ./index" , &package_json_url.value ());
29612957
29622958 if (!file_path_url) {
2963- env->isolate ()->ThrowException (
2964- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2965-
2959+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
29662960 return ;
29672961 }
29682962
2969- if (!node::url::FileURLToPath (env, file_path_url.value (), initial_file_path))
2963+ initial_file_path = node::url::FileURLToPath (env, *file_path_url);
2964+ if (!initial_file_path.has_value ()) {
29702965 return ;
2966+ }
29712967
2972- FromNamespacedPath (&initial_file_path);
2968+ FromNamespacedPath (&initial_file_path. value () );
29732969
29742970 for (int i = legacy_main_extensions_with_main_end;
29752971 i < legacy_main_extensions_package_fallback_end;
29762972 i++) {
2977- file_path = initial_file_path + std::string (legacy_main_extensions[i]);
2973+ file_path = * initial_file_path + std::string (legacy_main_extensions[i]);
29782974
29792975 switch (FilePathIsFile (env, file_path)) {
29802976 case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2991,39 +2987,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29912987 }
29922988 }
29932989
2994- std::string module_path;
2995- std::string module_base;
2990+ std::optional<std::string> module_path =
2991+ node::url::FileURLToPath (env, *package_json_url);
2992+ std::optional<std::string> module_base;
29962993
2997- if (!node::url::FileURLToPath (env, package_json_url. value (), module_path))
2994+ if (!module_path. has_value ()) {
29982995 return ;
2996+ }
29992997
3000- if (args.Length () >= 3 && !args[2 ]->IsNullOrUndefined () &&
3001- args[2 ]->IsString ()) {
3002- Utf8Value utf8_base_path (env->isolate (), args[2 ].As <String>());
2998+ if (args.Length () >= 3 && args[2 ]->IsString ()) {
2999+ Utf8Value utf8_base_path (isolate, args[2 ]);
30033000 auto base_url =
30043001 ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView ());
30053002
30063003 if (!base_url) {
3007- env->isolate ()->ThrowException (
3008- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
3009-
3004+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
30103005 return ;
30113006 }
30123007
3013- if (!node::url::FileURLToPath (env, base_url.value (), module_base)) return ;
3008+ module_base = node::url::FileURLToPath (env, *base_url);
3009+ if (!module_base.has_value ()) {
3010+ return ;
3011+ }
30143012 } else {
3015- std::string err_arg_message =
3016- " The \" base\" argument must be of type string or an instance of URL." ;
3017- env->isolate ()->ThrowException (
3018- ERR_INVALID_ARG_TYPE (env->isolate (), err_arg_message.c_str ()));
3013+ THROW_ERR_INVALID_ARG_TYPE (
3014+ isolate,
3015+ " The \" base\" argument must be of type string or an instance of URL." );
30193016 return ;
30203017 }
30213018
3022- env->isolate ()->ThrowException (
3023- ERR_MODULE_NOT_FOUND (env->isolate (),
3024- " Cannot find package '%s' imported from %s" ,
3025- module_path,
3026- module_base));
3019+ THROW_ERR_MODULE_NOT_FOUND (isolate,
3020+ " Cannot find package '%s' imported from %s" ,
3021+ *module_path,
3022+ *module_base);
30273023}
30283024
30293025void BindingData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments