Skip to content

Commit b4db32e

Browse files
anonrigtargos
authored andcommitted
fs,url: refactor FileURLToPath method
PR-URL: #50090 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent 4345ee2 commit b4db32e

File tree

3 files changed

+62
-85
lines changed

3 files changed

+62
-85
lines changed

src/node_file.cc

+37-41
Original file line numberDiff line numberDiff line change
@@ -2869,45 +2869,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
28692869
CHECK(args[0]->IsString());
28702870

28712871
Environment* env = Environment::GetCurrent(args);
2872+
auto isolate = env->isolate();
28722873

2873-
Utf8Value utf8_package_json_url(env->isolate(), args[0].As<String>());
2874+
Utf8Value utf8_package_json_url(isolate, args[0]);
28742875
auto package_json_url =
28752876
ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView());
28762877

28772878
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");
28812880
return;
28822881
}
28832882

28842883
ada::result<ada::url_aggregator> file_path_url;
2885-
std::string initial_file_path;
2884+
std::optional<std::string> initial_file_path;
28862885
std::string file_path;
28872886

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();
28922889

28932890
file_path_url = ada::parse<ada::url_aggregator>(
28942891
std::string("./") + package_config_main, &package_json_url.value());
28952892

28962893
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");
29002895
return;
29012896
}
29022897

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()) {
29052900
return;
2901+
}
29062902

2907-
FromNamespacedPath(&initial_file_path);
2903+
FromNamespacedPath(&initial_file_path.value());
29082904

29092905
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]);
29112907

29122908
switch (FilePathIsFile(env, file_path)) {
29132909
case BindingData::FilePathIsFileReturnType::kIsFile:
@@ -2930,21 +2926,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29302926
ada::parse<ada::url_aggregator>("./index", &package_json_url.value());
29312927

29322928
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");
29362930
return;
29372931
}
29382932

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()) {
29402935
return;
2936+
}
29412937

2942-
FromNamespacedPath(&initial_file_path);
2938+
FromNamespacedPath(&initial_file_path.value());
29432939

29442940
for (int i = legacy_main_extensions_with_main_end;
29452941
i < legacy_main_extensions_package_fallback_end;
29462942
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]);
29482944

29492945
switch (FilePathIsFile(env, file_path)) {
29502946
case BindingData::FilePathIsFileReturnType::kIsFile:
@@ -2961,39 +2957,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29612957
}
29622958
}
29632959

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;
29662963

2967-
if (!node::url::FileURLToPath(env, package_json_url.value(), module_path))
2964+
if (!module_path.has_value()) {
29682965
return;
2966+
}
29692967

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]);
29732970
auto base_url =
29742971
ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView());
29752972

29762973
if (!base_url) {
2977-
env->isolate()->ThrowException(
2978-
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
2979-
2974+
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
29802975
return;
29812976
}
29822977

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+
}
29842982
} 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.");
29892986
return;
29902987
}
29912988

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);
29972993
}
29982994

29992995
void BindingData::MemoryInfo(MemoryTracker* tracker) const {

src/node_url.cc

+21-38
Original file line numberDiff line numberDiff line change
@@ -437,16 +437,11 @@ std::string FromFilePath(std::string_view file_path) {
437437
return ada::href_from_file(escaped_file_path);
438438
}
439439

440-
bool FileURLToPath(Environment* env,
441-
const ada::url_aggregator& file_url,
442-
/* The linter can't detect the assign for result_file_path
443-
So we need to ignore since it suggest to put const */
444-
// NOLINTNEXTLINE(runtime/references)
445-
std::string& result_file_path) {
440+
std::optional<std::string> FileURLToPath(Environment* env,
441+
const ada::url_aggregator& file_url) {
446442
if (file_url.type != ada::scheme::FILE) {
447-
env->isolate()->ThrowException(ERR_INVALID_URL_SCHEME(env->isolate()));
448-
449-
return false;
443+
THROW_ERR_INVALID_URL_SCHEME(env->isolate());
444+
return std::nullopt;
450445
}
451446

452447
std::string_view pathname = file_url.get_pathname();
@@ -479,11 +474,10 @@ bool FileURLToPath(Environment* env,
479474

480475
if (!is_slash && !is_forward_slash) continue;
481476

482-
env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH(
477+
THROW_ERR_INVALID_FILE_URL_PATH(
483478
env->isolate(),
484-
"File URL path must not include encoded \\ or / characters"));
485-
486-
return false;
479+
"File URL path must not include encoded \\ or / characters");
480+
return std::nullopt;
487481
}
488482

489483
std::string_view hostname = file_url.get_hostname();
@@ -497,37 +491,29 @@ bool FileURLToPath(Environment* env,
497491
// about percent encoding because the URL parser will have
498492
// already taken care of that for us. Note that this only
499493
// causes IDNs with an appropriate `xn--` prefix to be decoded.
500-
result_file_path =
501-
"\\\\" + ada::unicode::to_unicode(hostname) + decoded_pathname;
502-
503-
return true;
494+
return "\\\\" + ada::unicode::to_unicode(hostname) + decoded_pathname;
504495
}
505496

506497
char letter = decoded_pathname[1] | 0x20;
507498
char sep = decoded_pathname[2];
508499

509500
// a..z A..Z
510501
if (letter < 'a' || letter > 'z' || sep != ':') {
511-
env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH(
512-
env->isolate(), "File URL path must be absolute"));
513-
514-
return false;
502+
THROW_ERR_INVALID_FILE_URL_PATH(env->isolate(),
503+
"File URL path must be absolute");
504+
return std::nullopt;
515505
}
516506

517-
result_file_path = decoded_pathname.substr(1);
518-
519-
return true;
507+
return decoded_pathname.substr(1);
520508
#else // _WIN32
521509
std::string_view hostname = file_url.get_hostname();
522510

523511
if (hostname.size() > 0) {
524-
std::string error_message =
525-
std::string("File URL host must be \"localhost\" or empty on ") +
526-
std::string(per_process::metadata.platform);
527-
env->isolate()->ThrowException(
528-
ERR_INVALID_FILE_URL_HOST(env->isolate(), error_message.c_str()));
529-
530-
return false;
512+
THROW_ERR_INVALID_FILE_URL_HOST(
513+
env->isolate(),
514+
"File URL host must be \"localhost\" or empty on ",
515+
std::string(per_process::metadata.platform));
516+
return std::nullopt;
531517
}
532518

533519
size_t first_percent = std::string::npos;
@@ -539,17 +525,14 @@ bool FileURLToPath(Environment* env,
539525
}
540526

541527
if (pathname[i + 1] == '2' && (pathname[i + 2] | 0x20) == 102) {
542-
env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH(
528+
THROW_ERR_INVALID_FILE_URL_PATH(
543529
env->isolate(),
544-
"File URL path must not include encoded / characters"));
545-
546-
return false;
530+
"File URL path must not include encoded / characters");
531+
return std::nullopt;
547532
}
548533
}
549534

550-
result_file_path = ada::unicode::percent_decode(pathname, first_percent);
551-
552-
return true;
535+
return ada::unicode::percent_decode(pathname, first_percent);
553536
#endif // _WIN32
554537
}
555538

src/node_url.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "v8-fast-api-calls.h"
1313
#include "v8.h"
1414

15+
#include <optional>
1516
#include <string>
1617

1718
namespace node {
@@ -82,12 +83,9 @@ class BindingData : public SnapshotableObject {
8283
};
8384

8485
std::string FromFilePath(std::string_view file_path);
85-
bool FileURLToPath(Environment* env,
86-
const ada::url_aggregator& file_url,
87-
/* The linter can't detect the assign for result_file_path
88-
So we need to ignore since it suggest to put const */
89-
// NOLINTNEXTLINE(runtime/references)
90-
std::string& result_file_path);
86+
std::optional<std::string> FileURLToPath(Environment* env,
87+
const ada::url_aggregator& file_url);
88+
9189
} // namespace url
9290

9391
} // namespace node

0 commit comments

Comments
 (0)