diff --git a/src/node_file.cc b/src/node_file.cc index 1f9147c456c0bc..a96e37d414d2af 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -2869,45 +2869,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { CHECK(args[0]->IsString()); Environment* env = Environment::GetCurrent(args); + auto isolate = env->isolate(); - Utf8Value utf8_package_json_url(env->isolate(), args[0].As()); + Utf8Value utf8_package_json_url(isolate, args[0]); auto package_json_url = ada::parse(utf8_package_json_url.ToStringView()); if (!package_json_url) { - env->isolate()->ThrowException( - ERR_INVALID_URL(env->isolate(), "Invalid URL")); - + THROW_ERR_INVALID_URL(isolate, "Invalid URL"); return; } ada::result file_path_url; - std::string initial_file_path; + std::optional initial_file_path; std::string file_path; - if (args.Length() >= 2 && !args[1]->IsNullOrUndefined() && - args[1]->IsString()) { - std::string package_config_main = - Utf8Value(env->isolate(), args[1].As()).ToString(); + if (args.Length() >= 2 && args[1]->IsString()) { + auto package_config_main = Utf8Value(isolate, args[1]).ToString(); file_path_url = ada::parse( std::string("./") + package_config_main, &package_json_url.value()); if (!file_path_url) { - env->isolate()->ThrowException( - ERR_INVALID_URL(env->isolate(), "Invalid URL")); - + THROW_ERR_INVALID_URL(isolate, "Invalid URL"); return; } - if (!node::url::FileURLToPath( - env, file_path_url.value(), initial_file_path)) + initial_file_path = node::url::FileURLToPath(env, *file_path_url); + if (!initial_file_path.has_value()) { return; + } - FromNamespacedPath(&initial_file_path); + FromNamespacedPath(&initial_file_path.value()); for (int i = 0; i < legacy_main_extensions_with_main_end; i++) { - file_path = initial_file_path + std::string(legacy_main_extensions[i]); + file_path = *initial_file_path + std::string(legacy_main_extensions[i]); switch (FilePathIsFile(env, file_path)) { case BindingData::FilePathIsFileReturnType::kIsFile: @@ -2930,21 +2926,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { ada::parse("./index", &package_json_url.value()); if (!file_path_url) { - env->isolate()->ThrowException( - ERR_INVALID_URL(env->isolate(), "Invalid URL")); - + THROW_ERR_INVALID_URL(isolate, "Invalid URL"); return; } - if (!node::url::FileURLToPath(env, file_path_url.value(), initial_file_path)) + initial_file_path = node::url::FileURLToPath(env, *file_path_url); + if (!initial_file_path.has_value()) { return; + } - FromNamespacedPath(&initial_file_path); + FromNamespacedPath(&initial_file_path.value()); for (int i = legacy_main_extensions_with_main_end; i < legacy_main_extensions_package_fallback_end; i++) { - file_path = initial_file_path + std::string(legacy_main_extensions[i]); + file_path = *initial_file_path + std::string(legacy_main_extensions[i]); switch (FilePathIsFile(env, file_path)) { case BindingData::FilePathIsFileReturnType::kIsFile: @@ -2961,39 +2957,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { } } - std::string module_path; - std::string module_base; + std::optional module_path = + node::url::FileURLToPath(env, *package_json_url); + std::optional module_base; - if (!node::url::FileURLToPath(env, package_json_url.value(), module_path)) + if (!module_path.has_value()) { return; + } - if (args.Length() >= 3 && !args[2]->IsNullOrUndefined() && - args[2]->IsString()) { - Utf8Value utf8_base_path(env->isolate(), args[2].As()); + if (args.Length() >= 3 && args[2]->IsString()) { + Utf8Value utf8_base_path(isolate, args[2]); auto base_url = ada::parse(utf8_base_path.ToStringView()); if (!base_url) { - env->isolate()->ThrowException( - ERR_INVALID_URL(env->isolate(), "Invalid URL")); - + THROW_ERR_INVALID_URL(isolate, "Invalid URL"); return; } - if (!node::url::FileURLToPath(env, base_url.value(), module_base)) return; + module_base = node::url::FileURLToPath(env, *base_url); + if (!module_base.has_value()) { + return; + } } else { - std::string err_arg_message = - "The \"base\" argument must be of type string or an instance of URL."; - env->isolate()->ThrowException( - ERR_INVALID_ARG_TYPE(env->isolate(), err_arg_message.c_str())); + THROW_ERR_INVALID_ARG_TYPE( + isolate, + "The \"base\" argument must be of type string or an instance of URL."); return; } - env->isolate()->ThrowException( - ERR_MODULE_NOT_FOUND(env->isolate(), - "Cannot find package '%s' imported from %s", - module_path, - module_base)); + THROW_ERR_MODULE_NOT_FOUND(isolate, + "Cannot find package '%s' imported from %s", + *module_path, + *module_base); } void BindingData::MemoryInfo(MemoryTracker* tracker) const { diff --git a/src/node_url.cc b/src/node_url.cc index dec5d640fc2b8f..7677c281cdade9 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -437,16 +437,11 @@ std::string FromFilePath(std::string_view file_path) { return ada::href_from_file(escaped_file_path); } -bool FileURLToPath(Environment* env, - const ada::url_aggregator& file_url, - /* The linter can't detect the assign for result_file_path - So we need to ignore since it suggest to put const */ - // NOLINTNEXTLINE(runtime/references) - std::string& result_file_path) { +std::optional FileURLToPath(Environment* env, + const ada::url_aggregator& file_url) { if (file_url.type != ada::scheme::FILE) { - env->isolate()->ThrowException(ERR_INVALID_URL_SCHEME(env->isolate())); - - return false; + THROW_ERR_INVALID_URL_SCHEME(env->isolate()); + return std::nullopt; } std::string_view pathname = file_url.get_pathname(); @@ -479,11 +474,10 @@ bool FileURLToPath(Environment* env, if (!is_slash && !is_forward_slash) continue; - env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH( + THROW_ERR_INVALID_FILE_URL_PATH( env->isolate(), - "File URL path must not include encoded \\ or / characters")); - - return false; + "File URL path must not include encoded \\ or / characters"); + return std::nullopt; } std::string_view hostname = file_url.get_hostname(); @@ -497,10 +491,7 @@ bool FileURLToPath(Environment* env, // about percent encoding because the URL parser will have // already taken care of that for us. Note that this only // causes IDNs with an appropriate `xn--` prefix to be decoded. - result_file_path = - "\\\\" + ada::unicode::to_unicode(hostname) + decoded_pathname; - - return true; + return "\\\\" + ada::unicode::to_unicode(hostname) + decoded_pathname; } char letter = decoded_pathname[1] | 0x20; @@ -508,26 +499,21 @@ bool FileURLToPath(Environment* env, // a..z A..Z if (letter < 'a' || letter > 'z' || sep != ':') { - env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH( - env->isolate(), "File URL path must be absolute")); - - return false; + THROW_ERR_INVALID_FILE_URL_PATH(env->isolate(), + "File URL path must be absolute"); + return std::nullopt; } - result_file_path = decoded_pathname.substr(1); - - return true; + return decoded_pathname.substr(1); #else // _WIN32 std::string_view hostname = file_url.get_hostname(); if (hostname.size() > 0) { - std::string error_message = - std::string("File URL host must be \"localhost\" or empty on ") + - std::string(per_process::metadata.platform); - env->isolate()->ThrowException( - ERR_INVALID_FILE_URL_HOST(env->isolate(), error_message.c_str())); - - return false; + THROW_ERR_INVALID_FILE_URL_HOST( + env->isolate(), + "File URL host must be \"localhost\" or empty on ", + std::string(per_process::metadata.platform)); + return std::nullopt; } size_t first_percent = std::string::npos; @@ -539,17 +525,14 @@ bool FileURLToPath(Environment* env, } if (pathname[i + 1] == '2' && (pathname[i + 2] | 0x20) == 102) { - env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH( + THROW_ERR_INVALID_FILE_URL_PATH( env->isolate(), - "File URL path must not include encoded / characters")); - - return false; + "File URL path must not include encoded / characters"); + return std::nullopt; } } - result_file_path = ada::unicode::percent_decode(pathname, first_percent); - - return true; + return ada::unicode::percent_decode(pathname, first_percent); #endif // _WIN32 } diff --git a/src/node_url.h b/src/node_url.h index 8da7722f1f6ef8..a0f595934a1265 100644 --- a/src/node_url.h +++ b/src/node_url.h @@ -12,6 +12,7 @@ #include "v8-fast-api-calls.h" #include "v8.h" +#include #include namespace node { @@ -82,12 +83,9 @@ class BindingData : public SnapshotableObject { }; std::string FromFilePath(std::string_view file_path); -bool FileURLToPath(Environment* env, - const ada::url_aggregator& file_url, - /* The linter can't detect the assign for result_file_path - So we need to ignore since it suggest to put const */ - // NOLINTNEXTLINE(runtime/references) - std::string& result_file_path); +std::optional FileURLToPath(Environment* env, + const ada::url_aggregator& file_url); + } // namespace url } // namespace node