@@ -2904,45 +2904,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2904
2904
CHECK (args[0 ]->IsString ());
2905
2905
2906
2906
Environment* env = Environment::GetCurrent (args);
2907
+ auto isolate = env->isolate ();
2907
2908
2908
- Utf8Value utf8_package_json_url (env-> isolate () , args[0 ]. As <String>() );
2909
+ Utf8Value utf8_package_json_url (isolate, args[0 ]);
2909
2910
auto package_json_url =
2910
2911
ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView ());
2911
2912
2912
2913
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" );
2916
2915
return ;
2917
2916
}
2918
2917
2919
2918
ada::result<ada::url_aggregator> file_path_url;
2920
- std::string initial_file_path;
2919
+ std::optional<std:: string> initial_file_path;
2921
2920
std::string file_path;
2922
2921
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 ();
2927
2924
2928
2925
file_path_url = ada::parse<ada::url_aggregator>(
2929
2926
std::string (" ./" ) + package_config_main, &package_json_url.value ());
2930
2927
2931
2928
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" );
2935
2930
return ;
2936
2931
}
2937
2932
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 ()) {
2940
2935
return ;
2936
+ }
2941
2937
2942
- FromNamespacedPath (&initial_file_path);
2938
+ FromNamespacedPath (&initial_file_path. value () );
2943
2939
2944
2940
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]);
2946
2942
2947
2943
switch (FilePathIsFile (env, file_path)) {
2948
2944
case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2965,21 +2961,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2965
2961
ada::parse<ada::url_aggregator>(" ./index" , &package_json_url.value ());
2966
2962
2967
2963
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" );
2971
2965
return ;
2972
2966
}
2973
2967
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 ()) {
2975
2970
return ;
2971
+ }
2976
2972
2977
- FromNamespacedPath (&initial_file_path);
2973
+ FromNamespacedPath (&initial_file_path. value () );
2978
2974
2979
2975
for (int i = legacy_main_extensions_with_main_end;
2980
2976
i < legacy_main_extensions_package_fallback_end;
2981
2977
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]);
2983
2979
2984
2980
switch (FilePathIsFile (env, file_path)) {
2985
2981
case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2996,39 +2992,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2996
2992
}
2997
2993
}
2998
2994
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;
3001
2998
3002
- if (!node::url::FileURLToPath (env, package_json_url. value (), module_path))
2999
+ if (!module_path. has_value ()) {
3003
3000
return ;
3001
+ }
3004
3002
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 ]);
3008
3005
auto base_url =
3009
3006
ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView ());
3010
3007
3011
3008
if (!base_url) {
3012
- env->isolate ()->ThrowException (
3013
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
3014
-
3009
+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
3015
3010
return ;
3016
3011
}
3017
3012
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
+ }
3019
3017
} 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." );
3024
3021
return ;
3025
3022
}
3026
3023
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);
3032
3028
}
3033
3029
3034
3030
void BindingData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments