Skip to content

In Bun, l'opzione unix in fetch() ti consente di inviare richieste HTTP tramite un socket di dominio unix.

ts
const unix = "/var/run/docker.sock";

const response = await fetch("http://localhost/info", { unix });

const body = await response.json();
console.log(body); // { ... }

L'opzione unix è una stringa che specifica il percorso del file locale a un socket di dominio unix. La funzione fetch() userà il socket per inviare la richiesta al server invece di usare una connessione di rete TCP. Anche https è supportato usando il protocollo https:// nell'URL invece di http://.

Per inviare una richiesta POST a un endpoint API tramite un socket di dominio unix:

ts
const response = await fetch("https://hostname/a/path", {
  unix: "/var/run/path/to/unix.sock",
  method: "POST",
  body: JSON.stringify({ message: "Ciao da Bun!" }),
  headers: {
    "Content-Type": "application/json",
  },
});

const body = await response.json();

Bun a cura di www.bunjs.com.cn