Skip to content

Commit

Permalink
fs,url: move FromNamespacedPath to node_url
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
anonrig authored and targos committed Nov 11, 2023
1 parent b4db32e commit 67cbe1b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
21 changes: 4 additions & 17 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Value> path;
Local<Value> error;
if (!StringBytes::Encode(req_wrap->env()->isolate(), first_path.c_str(),
Expand Down Expand Up @@ -1790,7 +1777,7 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
if (!req_wrap_sync.continuation_data()->first_path().empty()) {
Local<Value> error;
std::string first_path(req_wrap_sync.continuation_data()->first_path());
FromNamespacedPath(&first_path);
node::url::FromNamespacedPath(&first_path);
MaybeLocal<Value> path = StringBytes::Encode(env->isolate(),
first_path.c_str(),
UTF8, &error);
Expand Down Expand Up @@ -2900,7 +2887,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& 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]);
Expand Down Expand Up @@ -2935,7 +2922,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& 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;
Expand Down
13 changes: 13 additions & 0 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,19 @@ std::optional<std::string> 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
Expand Down
1 change: 1 addition & 0 deletions src/node_url.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class BindingData : public SnapshotableObject {
std::string FromFilePath(std::string_view file_path);
std::optional<std::string> FileURLToPath(Environment* env,
const ada::url_aggregator& file_url);
void FromNamespacedPath(std::string* path);

} // namespace url

Expand Down

0 comments on commit 67cbe1b

Please sign in to comment.