From a8cbc3023a04144d0fa469d3c2df20ae3c3b421d Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 9 Oct 2023 11:48:21 -0400 Subject: [PATCH] fs,url: move `FromNamespacedPath` to `node_url` --- src/node_file.cc | 24 +++++------------------- src/node_url.cc | 13 +++++++++++++ src/node_url.h | 1 + 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index c2f65ee3f170c8..0d7d2e04038d45 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -842,19 +842,6 @@ void AfterOpenFileHandle(uv_fs_t* req) { } } -// Reverse the logic applied by path.toNamespacedPath() to create a -// namespace-prefixed path. -void FromNamespacedPath(std::string* path) { -#ifdef _WIN32 - if (path->compare(0, 8, "\\\\?\\UNC\\", 8) == 0) { - *path = path->substr(8); - path->insert(0, "\\\\"); - } else if (path->compare(0, 4, "\\\\?\\", 4) == 0) { - *path = path->substr(4); - } -#endif -} - void AfterMkdirp(uv_fs_t* req) { FSReqBase* req_wrap = FSReqBase::from_req(req); FSReqAfterScope after(req_wrap, req); @@ -864,7 +851,7 @@ void AfterMkdirp(uv_fs_t* req) { std::string first_path(req_wrap->continuation_data()->first_path()); if (first_path.empty()) return req_wrap->Resolve(Undefined(req_wrap->env()->isolate())); - FromNamespacedPath(&first_path); + node::url::FromNamespacedPath(&first_path); Local path; Local error; if (!StringBytes::Encode(req_wrap->env()->isolate(), first_path.c_str(), @@ -1813,7 +1800,7 @@ static void MKDir(const FunctionCallbackInfo& args) { !req_wrap_sync.continuation_data()->first_path().empty()) { Local error; std::string first_path(req_wrap_sync.continuation_data()->first_path()); - FromNamespacedPath(&first_path); + node::url::FromNamespacedPath(&first_path); MaybeLocal path = StringBytes::Encode(env->isolate(), first_path.c_str(), UTF8, &error); @@ -2915,8 +2902,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { std::string file_path; if (args.Length() >= 2 && args[1]->IsString()) { - auto package_config_main = - Utf8Value(isolate, args[1]).ToString(); + auto package_config_main = Utf8Value(isolate, args[1]).ToString(); file_path_url = ada::parse( std::string("./") + package_config_main, &package_json_url.value()); @@ -2931,7 +2917,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { return; } - FromNamespacedPath(&initial_file_path.value()); + node::url::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]); @@ -2966,7 +2952,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { return; } - FromNamespacedPath(&initial_file_path.value()); + node::url::FromNamespacedPath(&initial_file_path.value()); for (int i = legacy_main_extensions_with_main_end; i < legacy_main_extensions_package_fallback_end; diff --git a/src/node_url.cc b/src/node_url.cc index 925fe076ec1953..5f6f63b601c560 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -525,6 +525,19 @@ std::optional FileURLToPath(Environment* env, #endif // _WIN32 } +// Reverse the logic applied by path.toNamespacedPath() to create a +// namespace-prefixed path. +void FromNamespacedPath(std::string* path) { +#ifdef _WIN32 + if (path->compare(0, 8, "\\\\?\\UNC\\", 8) == 0) { + *path = path->substr(8); + path->insert(0, "\\\\"); + } else if (path->compare(0, 4, "\\\\?\\", 4) == 0) { + *path = path->substr(4); + } +#endif +} + } // namespace url } // namespace node diff --git a/src/node_url.h b/src/node_url.h index 5680e3da0e568c..4bb14ea77e5f97 100644 --- a/src/node_url.h +++ b/src/node_url.h @@ -85,6 +85,7 @@ class BindingData : public SnapshotableObject { std::string FromFilePath(const std::string_view file_path); std::optional FileURLToPath(Environment* env, const ada::url_aggregator& file_url); +void FromNamespacedPath(std::string* path); } // namespace url