Skip to content

Commit db309a6

Browse files
committed
fs,url: refactor FileURLToPath method
1 parent 4e9f4aa commit db309a6

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
@@ -2901,45 +2901,41 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29012901
CHECK(args[0]->IsString());
29022902

29032903
Environment* env = Environment::GetCurrent(args);
2904+
auto isolate = env->isolate();
29042905

2905-
Utf8Value utf8_package_json_url(env->isolate(), args[0].As<String>());
2906+
Utf8Value utf8_package_json_url(isolate, args[0]);
29062907
auto package_json_url =
29072908
ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView());
29082909

29092910
if (!package_json_url) {
2910-
env->isolate()->ThrowException(
2911-
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
2912-
2911+
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
29132912
return;
29142913
}
29152914

29162915
ada::result<ada::url_aggregator> file_path_url;
2917-
std::string initial_file_path;
2916+
std::optional<std::string> initial_file_path;
29182917
std::string file_path;
29192918

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

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

29282925
if (!file_path_url) {
2929-
env->isolate()->ThrowException(
2930-
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
2931-
2926+
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
29322927
return;
29332928
}
29342929

2935-
if (!node::url::FileURLToPath(
2936-
env, file_path_url.value(), initial_file_path))
2930+
initial_file_path = node::url::FileURLToPath(env, *file_path_url);
2931+
if (!initial_file_path.has_value()) {
29372932
return;
2933+
}
29382934

2939-
FromNamespacedPath(&initial_file_path);
2935+
FromNamespacedPath(&initial_file_path.value());
29402936

29412937
for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
2942-
file_path = initial_file_path + std::string(legacy_main_extensions[i]);
2938+
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
29432939

29442940
switch (FilePathIsFile(env, file_path)) {
29452941
case BindingData::FilePathIsFileReturnType::kIsFile:
@@ -2962,21 +2958,21 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29622958
ada::parse<ada::url_aggregator>("./index", &package_json_url.value());
29632959

29642960
if (!file_path_url) {
2965-
env->isolate()->ThrowException(
2966-
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
2967-
2961+
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
29682962
return;
29692963
}
29702964

2971-
if (!node::url::FileURLToPath(env, file_path_url.value(), initial_file_path))
2965+
initial_file_path = node::url::FileURLToPath(env, *file_path_url);
2966+
if (!initial_file_path.has_value()) {
29722967
return;
2968+
}
29732969

2974-
FromNamespacedPath(&initial_file_path);
2970+
FromNamespacedPath(&initial_file_path.value());
29752971

29762972
for (int i = legacy_main_extensions_with_main_end;
29772973
i < legacy_main_extensions_package_fallback_end;
29782974
i++) {
2979-
file_path = initial_file_path + std::string(legacy_main_extensions[i]);
2975+
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
29802976

29812977
switch (FilePathIsFile(env, file_path)) {
29822978
case BindingData::FilePathIsFileReturnType::kIsFile:
@@ -2993,39 +2989,39 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29932989
}
29942990
}
29952991

2996-
std::string module_path;
2997-
std::string module_base;
2992+
std::optional<std::string> module_path =
2993+
node::url::FileURLToPath(env, *package_json_url);
2994+
std::optional<std::string> module_base;
29982995

2999-
if (!node::url::FileURLToPath(env, package_json_url.value(), module_path))
2996+
if (!module_path.has_value()) {
30002997
return;
2998+
}
30012999

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

30083005
if (!base_url) {
3009-
env->isolate()->ThrowException(
3010-
ERR_INVALID_URL(env->isolate(), "Invalid URL"));
3011-
3006+
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
30123007
return;
30133008
}
30143009

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

3024-
env->isolate()->ThrowException(
3025-
ERR_MODULE_NOT_FOUND(env->isolate(),
3026-
"Cannot find package '%s' imported from %s",
3027-
module_path,
3028-
module_base));
3021+
THROW_ERR_MODULE_NOT_FOUND(isolate,
3022+
"Cannot find package '%s' imported from %s",
3023+
*module_path,
3024+
*module_base);
30293025
}
30303026

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

src/node_url.cc

+21-38
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(
468+
THROW_ERR_INVALID_FILE_URL_PATH(
474469
env->isolate(),
475-
"File URL path must not include encoded \\ or / characters"));
476-
477-
return false;
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

+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(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)