Skip to content

Commit

Permalink
fs,url: move FromNamespacedPath to node_url
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Oct 9, 2023
1 parent 5f628fe commit a8cbc30
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
24 changes: 5 additions & 19 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 @@ -1813,7 1800,7 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
!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 @@ -2915,8 2902,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& 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<ada::url_aggregator>(
std::string("./") package_config_main, &package_json_url.value());
Expand All @@ -2931,7 2917,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 @@ -2966,7 2952,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 @@ -525,6 525,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(const 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 a8cbc30

Please sign in to comment.