undici icon indicating copy to clipboard operation
undici copied to clipboard

Is `fetch` API planned to support unix domain socket?

Open JerrysShan opened this issue 1 year ago • 6 comments

Many Node.js applications leverage Unix Domain Sockets for inter-process communication (IPC), providing a more efficient and secure way to communicate compared to traditional network sockets. Supporting the socketPath option in undici's fetch API could significantly benefit applications that rely on IPC, especially those dealing with microservices or local server communication.

it would be incredibly useful to have fetch requests directed to a Unix Domain Socket. This could look something like:

const response = await fetch('unix:///path/to/socket.sock/request-path', {
  method: 'GET'
  // additional options
});

JerrysShan avatar Mar 18 '24 09:03 JerrysShan

Unlikely. We are only doing web spec stuff. If you are doing backend development why are you using fetch? Use undici.request and then you can use unix sockets.

ronag avatar Mar 18 '24 09:03 ronag

Unlikely. We are only doing web spec stuff. If you are doing backend development why are you using fetch? Use undici.request and then you can use unix sockets.

Because the fetch API is familiar to most developers, we also use the same API as the web on the server side.

JerrysShan avatar Mar 18 '24 09:03 JerrysShan

I think supporting this should be easy enough and possibly just having the request pass the validation check.

@KhafraDev wdyt?

mcollina avatar Mar 18 '24 10:03 mcollina

I'm not opposed to it, as long as it doesn't add an option to fetch

KhafraDev avatar Mar 18 '24 15:03 KhafraDev

in the meantime, you can use Agent with fetch

import { fetch, Agent } from 'undici'

const resp = await fetch('http://localhost/version', {
  dispatcher: new Agent({
    connect: {
      socketPath: '/var/run/docker.sock'
    }
  })
})

console.log(await resp.text())

as a sidenote this is actually how people seem to fetch unix sockets in deno

KhafraDev avatar Mar 20 '24 15:03 KhafraDev

If adding an option to fetch is problematic for some reason, how about just handling requests to unix:// in Deno itself, forwarding the request to the socket? To avoid name resolution/SNI issues, users could include an explicit Host header with their request that would need to be evaluated prior to dispatching.
I know this isn't really elegant from the implementation perspective, but it'd solve the problem now without any API changes necessary, and works just like people would expect it to.

Radiergummi avatar Sep 08 '24 12:09 Radiergummi

Hi,

I'm surprised (and disapointed) that undici doesn't support, in an easy way, unix sockets. As suggested by @JerrysShan, having the ability to use fetch('unix://...') would be really great, pretty simple to implement and easy to use. It would also avoid to add undici as an external dependency. This is something I would expect from Node.js fetch implementation.

Hope we could see that one day.

Adrien

Bacto avatar Sep 09 '25 20:09 Bacto