@@ -2869,45 +2869,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
28692869 CHECK (args[0 ]->IsString ());
28702870
28712871 Environment* env = Environment::GetCurrent (args);
2872+ auto isolate = env->isolate ();
28722873
2873- Utf8Value utf8_package_json_url (env-> isolate () , args[0 ]. As <String>() );
2874+ Utf8Value utf8_package_json_url (isolate, args[0 ]);
28742875 auto package_json_url =
28752876 ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView ());
28762877
28772878 if (!package_json_url) {
2878- env->isolate ()->ThrowException (
2879- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2880-
2879+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
28812880 return ;
28822881 }
28832882
28842883 ada::result<ada::url_aggregator> file_path_url;
2885- std::string initial_file_path;
2884+ std::optional<std:: string> initial_file_path;
28862885 std::string file_path;
28872886
2888- if (args.Length () >= 2 && !args[1 ]->IsNullOrUndefined () &&
2889- args[1 ]->IsString ()) {
2890- std::string package_config_main =
2891- Utf8Value (env->isolate (), args[1 ].As <String>()).ToString ();
2887+ if (args.Length () >= 2 && args[1 ]->IsString ()) {
2888+ auto package_config_main = Utf8Value (isolate, args[1 ]).ToString ();
28922889
28932890 file_path_url = ada::parse<ada::url_aggregator>(
28942891 std::string (" ./" ) + package_config_main, &package_json_url.value ());
28952892
28962893 if (!file_path_url) {
2897- env->isolate ()->ThrowException (
2898- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2899-
2894+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
29002895 return ;
29012896 }
29022897
2903- if (! node::url::FileURLToPath (
2904- env, file_path_url. value (), initial_file_path))
2898+ initial_file_path = node::url::FileURLToPath (env, *file_path_url);
2899+ if (!initial_file_path. has_value ()) {
29052900 return ;
2901+ }
29062902
2907- FromNamespacedPath (&initial_file_path);
2903+ FromNamespacedPath (&initial_file_path. value () );
29082904
29092905 for (int i = 0 ; i < legacy_main_extensions_with_main_end; i++) {
2910- file_path = initial_file_path + std::string (legacy_main_extensions[i]);
2906+ file_path = * initial_file_path + std::string (legacy_main_extensions[i]);
29112907
29122908 switch (FilePathIsFile (env, file_path)) {
29132909 case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2930,21 +2926,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29302926 ada::parse<ada::url_aggregator>(" ./index" , &package_json_url.value ());
29312927
29322928 if (!file_path_url) {
2933- env->isolate ()->ThrowException (
2934- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2935-
2929+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
29362930 return ;
29372931 }
29382932
2939- if (!node::url::FileURLToPath (env, file_path_url.value (), initial_file_path))
2933+ initial_file_path = node::url::FileURLToPath (env, *file_path_url);
2934+ if (!initial_file_path.has_value ()) {
29402935 return ;
2936+ }
29412937
2942- FromNamespacedPath (&initial_file_path);
2938+ FromNamespacedPath (&initial_file_path. value () );
29432939
29442940 for (int i = legacy_main_extensions_with_main_end;
29452941 i < legacy_main_extensions_package_fallback_end;
29462942 i++) {
2947- file_path = initial_file_path + std::string (legacy_main_extensions[i]);
2943+ file_path = * initial_file_path + std::string (legacy_main_extensions[i]);
29482944
29492945 switch (FilePathIsFile (env, file_path)) {
29502946 case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2961,39 +2957,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29612957 }
29622958 }
29632959
2964- std::string module_path;
2965- std::string module_base;
2960+ std::optional<std::string> module_path =
2961+ node::url::FileURLToPath (env, *package_json_url);
2962+ std::optional<std::string> module_base;
29662963
2967- if (!node::url::FileURLToPath (env, package_json_url. value (), module_path))
2964+ if (!module_path. has_value ()) {
29682965 return ;
2966+ }
29692967
2970- if (args.Length () >= 3 && !args[2 ]->IsNullOrUndefined () &&
2971- args[2 ]->IsString ()) {
2972- Utf8Value utf8_base_path (env->isolate (), args[2 ].As <String>());
2968+ if (args.Length () >= 3 && args[2 ]->IsString ()) {
2969+ Utf8Value utf8_base_path (isolate, args[2 ]);
29732970 auto base_url =
29742971 ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView ());
29752972
29762973 if (!base_url) {
2977- env->isolate ()->ThrowException (
2978- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2979-
2974+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
29802975 return ;
29812976 }
29822977
2983- if (!node::url::FileURLToPath (env, base_url.value (), module_base)) return ;
2978+ module_base = node::url::FileURLToPath (env, *base_url);
2979+ if (!module_base.has_value ()) {
2980+ return ;
2981+ }
29842982 } else {
2985- std::string err_arg_message =
2986- " The \" base\" argument must be of type string or an instance of URL." ;
2987- env->isolate ()->ThrowException (
2988- ERR_INVALID_ARG_TYPE (env->isolate (), err_arg_message.c_str ()));
2983+ THROW_ERR_INVALID_ARG_TYPE (
2984+ isolate,
2985+ " The \" base\" argument must be of type string or an instance of URL." );
29892986 return ;
29902987 }
29912988
2992- env->isolate ()->ThrowException (
2993- ERR_MODULE_NOT_FOUND (env->isolate (),
2994- " Cannot find package '%s' imported from %s" ,
2995- module_path,
2996- module_base));
2989+ THROW_ERR_MODULE_NOT_FOUND (isolate,
2990+ " Cannot find package '%s' imported from %s" ,
2991+ *module_path,
2992+ *module_base);
29972993}
29982994
29992995void BindingData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments