@@ -2869,45 +2869,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2869
2869
CHECK (args[0 ]->IsString ());
2870
2870
2871
2871
Environment* env = Environment::GetCurrent (args);
2872
+ auto isolate = env->isolate ();
2872
2873
2873
- Utf8Value utf8_package_json_url (env-> isolate () , args[0 ]. As <String>() );
2874
+ Utf8Value utf8_package_json_url (isolate, args[0 ]);
2874
2875
auto package_json_url =
2875
2876
ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView ());
2876
2877
2877
2878
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" );
2881
2880
return ;
2882
2881
}
2883
2882
2884
2883
ada::result<ada::url_aggregator> file_path_url;
2885
- std::string initial_file_path;
2884
+ std::optional<std:: string> initial_file_path;
2886
2885
std::string file_path;
2887
2886
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 ();
2892
2889
2893
2890
file_path_url = ada::parse<ada::url_aggregator>(
2894
2891
std::string (" ./" ) + package_config_main, &package_json_url.value ());
2895
2892
2896
2893
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" );
2900
2895
return ;
2901
2896
}
2902
2897
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 ()) {
2905
2900
return ;
2901
+ }
2906
2902
2907
- FromNamespacedPath (&initial_file_path);
2903
+ FromNamespacedPath (&initial_file_path. value () );
2908
2904
2909
2905
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]);
2911
2907
2912
2908
switch (FilePathIsFile (env, file_path)) {
2913
2909
case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2930,21 +2926,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2930
2926
ada::parse<ada::url_aggregator>(" ./index" , &package_json_url.value ());
2931
2927
2932
2928
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" );
2936
2930
return ;
2937
2931
}
2938
2932
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 ()) {
2940
2935
return ;
2936
+ }
2941
2937
2942
- FromNamespacedPath (&initial_file_path);
2938
+ FromNamespacedPath (&initial_file_path. value () );
2943
2939
2944
2940
for (int i = legacy_main_extensions_with_main_end;
2945
2941
i < legacy_main_extensions_package_fallback_end;
2946
2942
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]);
2948
2944
2949
2945
switch (FilePathIsFile (env, file_path)) {
2950
2946
case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2961,39 +2957,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2961
2957
}
2962
2958
}
2963
2959
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;
2966
2963
2967
- if (!node::url::FileURLToPath (env, package_json_url. value (), module_path))
2964
+ if (!module_path. has_value ()) {
2968
2965
return ;
2966
+ }
2969
2967
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 ]);
2973
2970
auto base_url =
2974
2971
ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView ());
2975
2972
2976
2973
if (!base_url) {
2977
- env->isolate ()->ThrowException (
2978
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2979
-
2974
+ THROW_ERR_INVALID_URL (isolate, " Invalid URL" );
2980
2975
return ;
2981
2976
}
2982
2977
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
+ }
2984
2982
} 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." );
2989
2986
return ;
2990
2987
}
2991
2988
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);
2997
2993
}
2998
2994
2999
2995
void BindingData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments