Skip to content

Commit 2389947

Browse files
committed
fs,url: refactor FileURLToPath method
1 parent 67c5b8a commit 2389947

3 files changed

Lines changed: 63 additions & 86 deletions

File tree

src/node_file.cc

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,45 +2899,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
28992899
CHECK(args[0]->IsString());
29002900

29012901
Environment* env = Environment::GetCurrent(args);
2902+
auto isolate = env->isolate();
29022903

2903-
Utf8Value utf8_package_json_url(env->isolate(), args[0].As<String>());
2904+
Utf8Value utf8_package_json_url(isolate, args[0]);
29042905
auto package_json_url =
29052906
ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView());
29062907

29072908
if (!package_json_url) {
2908-
env->isolate()->ThrowException(
2909-
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
2910-
2909+
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
29112910
return;
29122911
}
29132912

29142913
ada::result<ada::url_aggregator> file_path_url;
2915-
std::string initial_file_path;
2914+
std::optional<std::string> initial_file_path;
29162915
std::string file_path;
29172916

2918-
if (args.Length() >= 2 && !args[1]->IsNullOrUndefined() &&
2919-
args[1]->IsString()) {
2920-
std::string package_config_main =
2921-
Utf8Value(env->isolate(), args[1].As<String>()).ToString();
2917+
if (args.Length() >= 2 && args[1]->IsString()) {
2918+
auto package_config_main = Utf8Value(isolate, args[1]).ToString();
29222919

29232920
file_path_url = ada::parse<ada::url_aggregator>(
29242921
std::string("./") + package_config_main, &package_json_url.value());
29252922

29262923
if (!file_path_url) {
2927-
env->isolate()->ThrowException(
2928-
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
2929-
2924+
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
29302925
return;
29312926
}
29322927

2933-
if (!node::url::FileURLToPath(
2934-
env, file_path_url.value(), initial_file_path))
2928+
initial_file_path = node::url::FileURLToPath(env, *file_path_url);
2929+
if (!initial_file_path.has_value()) {
29352930
return;
2931+
}
29362932

2937-
FromNamespacedPath(&initial_file_path);
2933+
FromNamespacedPath(&initial_file_path.value());
29382934

29392935
for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
2940-
file_path = initial_file_path + std::string(legacy_main_extensions[i]);
2936+
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
29412937

29422938
switch (FilePathIsFile(env, file_path)) {
29432939
case BindingData::FilePathIsFileReturnType::kIsFile:
@@ -2960,21 +2956,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29602956
ada::parse<ada::url_aggregator>("./index", &package_json_url.value());
29612957

29622958
if (!file_path_url) {
2963-
env->isolate()->ThrowException(
2964-
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
2965-
2959+
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
29662960
return;
29672961
}
29682962

2969-
if (!node::url::FileURLToPath(env, file_path_url.value(), initial_file_path))
2963+
initial_file_path = node::url::FileURLToPath(env, *file_path_url);
2964+
if (!initial_file_path.has_value()) {
29702965
return;
2966+
}
29712967

2972-
FromNamespacedPath(&initial_file_path);
2968+
FromNamespacedPath(&initial_file_path.value());
29732969

29742970
for (int i = legacy_main_extensions_with_main_end;
29752971
i < legacy_main_extensions_package_fallback_end;
29762972
i++) {
2977-
file_path = initial_file_path + std::string(legacy_main_extensions[i]);
2973+
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
29782974

29792975
switch (FilePathIsFile(env, file_path)) {
29802976
case BindingData::FilePathIsFileReturnType::kIsFile:
@@ -2991,39 +2987,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29912987
}
29922988
}
29932989

2994-
std::string module_path;
2995-
std::string module_base;
2990+
std::optional<std::string> module_path =
2991+
node::url::FileURLToPath(env, *package_json_url);
2992+
std::optional<std::string> module_base;
29962993

2997-
if (!node::url::FileURLToPath(env, package_json_url.value(), module_path))
2994+
if (!module_path.has_value()) {
29982995
return;
2996+
}
29992997

3000-
if (args.Length() >= 3 && !args[2]->IsNullOrUndefined() &&
3001-
args[2]->IsString()) {
3002-
Utf8Value utf8_base_path(env->isolate(), args[2].As<String>());
2998+
if (args.Length() >= 3 && args[2]->IsString()) {
2999+
Utf8Value utf8_base_path(isolate, args[2]);
30033000
auto base_url =
30043001
ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView());
30053002

30063003
if (!base_url) {
3007-
env->isolate()->ThrowException(
3008-
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
3009-
3004+
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
30103005
return;
30113006
}
30123007

3013-
if (!node::url::FileURLToPath(env, base_url.value(), module_base)) return;
3008+
module_base = node::url::FileURLToPath(env, *base_url);
3009+
if (!module_base.has_value()) {
3010+
return;
3011+
}
30143012
} else {
3015-
std::string err_arg_message =
3016-
"The \"base\" argument must be of type string or an instance of URL.";
3017-
env->isolate()->ThrowException(
3018-
ERR_INVALID_ARG_TYPE(env->isolate(), err_arg_message.c_str()));
3013+
THROW_ERR_INVALID_ARG_TYPE(
3014+
isolate,
3015+
"The \"base\" argument must be of type string or an instance of URL.");
30193016
return;
30203017
}
30213018

3022-
env->isolate()->ThrowException(
3023-
ERR_MODULE_NOT_FOUND(env->isolate(),
3024-
"Cannot find package '%s' imported from %s",
3025-
module_path,
3026-
module_base));
3019+
THROW_ERR_MODULE_NOT_FOUND(isolate,
3020+
"Cannot find package '%s' imported from %s",
3021+
*module_path,
3022+
*module_base);
30273023
}
30283024

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

src/node_url.cc

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -428,16 +428,11 @@ std::string FromFilePath(const std::string_view file_path) {
428428
return ada::href_from_file(escaped_file_path);
429429
}
430430

431-
bool FileURLToPath(Environment* env,
432-
const ada::url_aggregator& file_url,
433-
/* The linter can't detect the assign for result_file_path
434-
So we need to ignore since it suggest to put const */
435-
// NOLINTNEXTLINE(runtime/references)
436-
std::string& result_file_path) {
431+
std::optional<std::string> FileURLToPath(Environment* env,
432+
const ada::url_aggregator& file_url) {
437433
if (file_url.type != ada::scheme::FILE) {
438-
env->isolate()->ThrowException(ERR_INVALID_URL_SCHEME(env->isolate()));
439-
440-
return false;
434+
THROW_ERR_INVALID_URL_SCHEME(env->isolate());
435+
return std::nullopt;
441436
}
442437

443438
std::string_view pathname = file_url.get_pathname();
@@ -470,11 +465,10 @@ bool FileURLToPath(Environment* env,
470465

471466
if (!is_slash && !is_forward_slash) continue;
472467

473-
env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH(
474-
env->isolate(),
475-
"File URL path must not include encoded \\ or / characters"));
476-
477-
return false;
468+
THROW_ERR_INVALID_FILE_URL_PATH(
469+
env->isolate(),
470+
"File URL path must not include encoded \\ or / characters");
471+
return std::nullopt;
478472
}
479473

480474
std::string_view hostname = file_url.get_hostname();
@@ -488,37 +482,29 @@ bool FileURLToPath(Environment* env,
488482
// about percent encoding because the URL parser will have
489483
// already taken care of that for us. Note that this only
490484
// causes IDNs with an appropriate `xn--` prefix to be decoded.
491-
result_file_path =
492-
"\\\\" + ada::unicode::to_unicode(hostname) + decoded_pathname;
493-
494-
return true;
485+
return "\\\\" + ada::unicode::to_unicode(hostname) + decoded_pathname;
495486
}
496487

497488
char letter = decoded_pathname[1] | 0x20;
498489
char sep = decoded_pathname[2];
499490

500491
// a..z A..Z
501492
if (letter < 'a' || letter > 'z' || sep != ':') {
502-
env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH(
503-
env->isolate(), "File URL path must be absolute"));
504-
505-
return false;
493+
THROW_ERR_INVALID_FILE_URL_PATH(env->isolate(),
494+
"File URL path must be absolute");
495+
return std::nullopt;
506496
}
507497

508-
result_file_path = decoded_pathname.substr(1);
509-
510-
return true;
498+
return decoded_pathname.substr(1);
511499
#else // _WIN32
512500
std::string_view hostname = file_url.get_hostname();
513501

514502
if (hostname.size() > 0) {
515-
std::string error_message =
516-
std::string("File URL host must be \"localhost\" or empty on ") +
517-
std::string(per_process::metadata.platform);
518-
env->isolate()->ThrowException(
519-
ERR_INVALID_FILE_URL_HOST(env->isolate(), error_message.c_str()));
520-
521-
return false;
503+
THROW_ERR_INVALID_FILE_URL_HOST(
504+
env->isolate(),
505+
"File URL host must be \"localhost\" or empty on ",
506+
std::string(per_process::metadata.platform));
507+
return std::nullopt;
522508
}
523509

524510
size_t first_percent = std::string::npos;
@@ -530,17 +516,14 @@ bool FileURLToPath(Environment* env,
530516
}
531517

532518
if (pathname[i + 1] == '2' && (pathname[i + 2] | 0x20) == 102) {
533-
env->isolate()->ThrowException(ERR_INVALID_FILE_URL_PATH(
519+
THROW_ERR_INVALID_FILE_URL_PATH(
534520
env->isolate(),
535-
"File URL path must not include encoded / characters"));
536-
537-
return false;
521+
"File URL path must not include encoded / characters");
522+
return std::nullopt;
538523
}
539524
}
540525

541-
result_file_path = ada::unicode::percent_decode(pathname, first_percent);
542-
543-
return true;
526+
return ada::unicode::percent_decode(pathname, first_percent);
544527
#endif // _WIN32
545528
}
546529

src/node_url.h

Lines changed: 4 additions & 6 deletions
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(const 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)