nodejs / undici

An HTTP/1.1 client, written from scratch for Node.js
https://nodejs.github.io/undici
MIT License
6.14k stars 531 forks source link

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

Open JerrysShan opened 6 months ago

JerrysShan commented 6 months ago

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
});
ronag commented 6 months ago

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.

JerrysShan commented 6 months ago

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.

mcollina commented 6 months ago

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

@KhafraDev wdyt?

KhafraDev commented 6 months ago

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

KhafraDev commented 6 months ago

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

Radiergummi commented 3 weeks ago

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.