@@ -2901,45 +2901,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2901
2901
CHECK (args[0 ]->IsString ());
2902
2902
2903
2903
Environment* env = Environment::GetCurrent (args);
2904
+ auto isolate = env->isolate ();
2904
2905
2905
- Utf8Value utf8_package_json_url (env-> isolate () , args[0 ]. As <String>() );
2906
+ Utf8Value utf8_package_json_url (isolate, args[0 ]);
2906
2907
auto package_json_url =
2907
2908
ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView ());
2908
2909
2909
2910
if (!package_json_url) {
2910
- env->isolate ()->ThrowException (
2911
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2912
-
2911
+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
2913
2912
return ;
2914
2913
}
2915
2914
2916
2915
ada::result<ada::url_aggregator> file_path_url;
2917
- std::string initial_file_path;
2916
+ std::optional<std:: string> initial_file_path;
2918
2917
std::string file_path;
2919
2918
2920
- if (args.Length () >= 2 && !args[1 ]->IsNullOrUndefined () &&
2921
- args[1 ]->IsString ()) {
2922
- std::string package_config_main =
2923
- Utf8Value (env->isolate (), args[1 ].As <String>()).ToString ();
2919
+ if (args.Length () >= 2 && args[1 ]->IsString ()) {
2920
+ auto package_config_main = Utf8Value (isolate, args[1 ]).ToString ();
2924
2921
2925
2922
file_path_url = ada::parse<ada::url_aggregator>(
2926
2923
std::string (" ./" ) + package_config_main, &package_json_url.value ());
2927
2924
2928
2925
if (!file_path_url) {
2929
- env->isolate ()->ThrowException (
2930
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2931
-
2926
+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
2932
2927
return ;
2933
2928
}
2934
2929
2935
- if (! node::url::FileURLToPath (
2936
- env, file_path_url. value (), initial_file_path))
2930
+ initial_file_path = node::url::FileURLToPath (env, *file_path_url);
2931
+ if (!initial_file_path. has_value ()) {
2937
2932
return ;
2933
+ }
2938
2934
2939
- FromNamespacedPath (&initial_file_path);
2935
+ FromNamespacedPath (&initial_file_path. value () );
2940
2936
2941
2937
for (int i = 0 ; i < legacy_main_extensions_with_main_end; i++) {
2942
- file_path = initial_file_path + std::string (legacy_main_extensions[i]);
2938
+ file_path = * initial_file_path + std::string (legacy_main_extensions[i]);
2943
2939
2944
2940
switch (FilePathIsFile (env, file_path)) {
2945
2941
case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2962,21 +2958,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2962
2958
ada::parse<ada::url_aggregator>(" ./index" , &package_json_url.value ());
2963
2959
2964
2960
if (!file_path_url) {
2965
- env->isolate ()->ThrowException (
2966
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2967
-
2961
+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
2968
2962
return ;
2969
2963
}
2970
2964
2971
- if (!node::url::FileURLToPath (env, file_path_url.value (), initial_file_path))
2965
+ initial_file_path = node::url::FileURLToPath (env, *file_path_url);
2966
+ if (!initial_file_path.has_value ()) {
2972
2967
return ;
2968
+ }
2973
2969
2974
- FromNamespacedPath (&initial_file_path);
2970
+ FromNamespacedPath (&initial_file_path. value () );
2975
2971
2976
2972
for (int i = legacy_main_extensions_with_main_end;
2977
2973
i < legacy_main_extensions_package_fallback_end;
2978
2974
i++) {
2979
- file_path = initial_file_path + std::string (legacy_main_extensions[i]);
2975
+ file_path = * initial_file_path + std::string (legacy_main_extensions[i]);
2980
2976
2981
2977
switch (FilePathIsFile (env, file_path)) {
2982
2978
case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2993,39 +2989,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2993
2989
}
2994
2990
}
2995
2991
2996
- std::string module_path;
2997
- std::string module_base;
2992
+ std::optional<std::string> module_path =
2993
+ node::url::FileURLToPath (env, *package_json_url);
2994
+ std::optional<std::string> module_base;
2998
2995
2999
- if (!node::url::FileURLToPath (env, package_json_url. value (), module_path))
2996
+ if (!module_path. has_value ()) {
3000
2997
return ;
2998
+ }
3001
2999
3002
- if (args.Length () >= 3 && !args[2 ]->IsNullOrUndefined () &&
3003
- args[2 ]->IsString ()) {
3004
- Utf8Value utf8_base_path (env->isolate (), args[2 ].As <String>());
3000
+ if (args.Length () >= 3 && args[2 ]->IsString ()) {
3001
+ Utf8Value utf8_base_path (isolate, args[2 ]);
3005
3002
auto base_url =
3006
3003
ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView ());
3007
3004
3008
3005
if (!base_url) {
3009
- env->isolate ()->ThrowException (
3010
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
3011
-
3006
+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
3012
3007
return ;
3013
3008
}
3014
3009
3015
- if (!node::url::FileURLToPath (env, base_url.value (), module_base)) return ;
3010
+ module_base = node::url::FileURLToPath (env, *base_url);
3011
+ if (!module_base.has_value ()) {
3012
+ return ;
3013
+ }
3016
3014
} else {
3017
- std::string err_arg_message =
3018
- " The \" base\" argument must be of type string or an instance of URL." ;
3019
- env->isolate ()->ThrowException (
3020
- ERR_INVALID_ARG_TYPE (env->isolate (), err_arg_message.c_str ()));
3015
+ THROW_ERR_INVALID_ARG_TYPE (
3016
+ isolate,
3017
+ " The \" base\" argument must be of type string or an instance of URL." );
3021
3018
return ;
3022
3019
}
3023
3020
3024
- env->isolate ()->ThrowException (
3025
- ERR_MODULE_NOT_FOUND (env->isolate (),
3026
- " Cannot find package '%s' imported from %s" ,
3027
- module_path,
3028
- module_base));
3021
+ THROW_ERR_MODULE_NOT_FOUND (isolate,
3022
+ " Cannot find package '%s' imported from %s" ,
3023
+ *module_path,
3024
+ *module_base);
3029
3025
}
3030
3026
3031
3027
void BindingData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments