@@ -2904,45 +2904,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29042904 CHECK (args[0 ]->IsString ());
29052905
29062906 Environment* env = Environment::GetCurrent (args);
2907+ auto isolate = env->isolate ();
29072908
2908- Utf8Value utf8_package_json_url (env-> isolate () , args[0 ]. As <String>() );
2909+ Utf8Value utf8_package_json_url (isolate, args[0 ]);
29092910 auto package_json_url =
29102911 ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView ());
29112912
29122913 if (!package_json_url) {
2913- env->isolate ()->ThrowException (
2914- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2915-
2914+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
29162915 return ;
29172916 }
29182917
29192918 ada::result<ada::url_aggregator> file_path_url;
2920- std::string initial_file_path;
2919+ std::optional<std:: string> initial_file_path;
29212920 std::string file_path;
29222921
2923- if (args.Length () >= 2 && !args[1 ]->IsNullOrUndefined () &&
2924- args[1 ]->IsString ()) {
2925- std::string package_config_main =
2926- Utf8Value (env->isolate (), args[1 ].As <String>()).ToString ();
2922+ if (args.Length () >= 2 && args[1 ]->IsString ()) {
2923+ auto package_config_main = Utf8Value (isolate, args[1 ]).ToString ();
29272924
29282925 file_path_url = ada::parse<ada::url_aggregator>(
29292926 std::string (" ./" ) + package_config_main, &package_json_url.value ());
29302927
29312928 if (!file_path_url) {
2932- env->isolate ()->ThrowException (
2933- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2934-
2929+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
29352930 return ;
29362931 }
29372932
2938- if (! node::url::FileURLToPath (
2939- 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 = 0 ; i < legacy_main_extensions_with_main_end; i++) {
2945- file_path = initial_file_path + std::string (legacy_main_extensions[i]);
2941+ file_path = * initial_file_path + std::string (legacy_main_extensions[i]);
29462942
29472943 switch (FilePathIsFile (env, file_path)) {
29482944 case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2965,21 +2961,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29652961 ada::parse<ada::url_aggregator>(" ./index" , &package_json_url.value ());
29662962
29672963 if (!file_path_url) {
2968- env->isolate ()->ThrowException (
2969- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2970-
2964+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
29712965 return ;
29722966 }
29732967
2974- if (!node::url::FileURLToPath (env, file_path_url.value (), initial_file_path))
2968+ initial_file_path = node::url::FileURLToPath (env, *file_path_url);
2969+ if (!initial_file_path.has_value ()) {
29752970 return ;
2971+ }
29762972
2977- FromNamespacedPath (&initial_file_path);
2973+ FromNamespacedPath (&initial_file_path. value () );
29782974
29792975 for (int i = legacy_main_extensions_with_main_end;
29802976 i < legacy_main_extensions_package_fallback_end;
29812977 i++) {
2982- file_path = initial_file_path + std::string (legacy_main_extensions[i]);
2978+ file_path = * initial_file_path + std::string (legacy_main_extensions[i]);
29832979
29842980 switch (FilePathIsFile (env, file_path)) {
29852981 case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2996,39 +2992,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29962992 }
29972993 }
29982994
2999- std::string module_path;
3000- std::string module_base;
2995+ std::optional<std::string> module_path =
2996+ node::url::FileURLToPath (env, *package_json_url);
2997+ std::optional<std::string> module_base;
30012998
3002- if (!node::url::FileURLToPath (env, package_json_url. value (), module_path))
2999+ if (!module_path. has_value ()) {
30033000 return ;
3001+ }
30043002
3005- if (args.Length () >= 3 && !args[2 ]->IsNullOrUndefined () &&
3006- args[2 ]->IsString ()) {
3007- Utf8Value utf8_base_path (env->isolate (), args[2 ].As <String>());
3003+ if (args.Length () >= 3 && args[2 ]->IsString ()) {
3004+ Utf8Value utf8_base_path (isolate, args[2 ]);
30083005 auto base_url =
30093006 ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView ());
30103007
30113008 if (!base_url) {
3012- env->isolate ()->ThrowException (
3013- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
3014-
3009+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
30153010 return ;
30163011 }
30173012
3018- if (!node::url::FileURLToPath (env, base_url.value (), module_base)) return ;
3013+ module_base = node::url::FileURLToPath (env, *base_url);
3014+ if (!module_base.has_value ()) {
3015+ return ;
3016+ }
30193017 } else {
3020- std::string err_arg_message =
3021- " The \" base\" argument must be of type string or an instance of URL." ;
3022- env->isolate ()->ThrowException (
3023- ERR_INVALID_ARG_TYPE (env->isolate (), err_arg_message.c_str ()));
3018+ THROW_ERR_INVALID_ARG_TYPE (
3019+ isolate,
3020+ " The \" base\" argument must be of type string or an instance of URL." );
30243021 return ;
30253022 }
30263023
3027- env->isolate ()->ThrowException (
3028- ERR_MODULE_NOT_FOUND (env->isolate (),
3029- " Cannot find package '%s' imported from %s" ,
3030- module_path,
3031- module_base));
3024+ THROW_ERR_MODULE_NOT_FOUND (isolate,
3025+ " Cannot find package '%s' imported from %s" ,
3026+ *module_path,
3027+ *module_base);
30323028}
30333029
30343030void BindingData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments