在 Bun 中,fetch()
中的 unix
選項讓您可以透過 Unix 域套接字 傳送 HTTP 要求。
const unix = "/var/run/docker.sock";
const response = await fetch("https://127.0.0.1/info", { unix });
const body = await response.json();
console.log(body); // { ... }
unix
選項是一個字串,用於指定 Unix 域套接字的本機檔案路徑。fetch()
函式將使用套接字將要求傳送至伺服器,而非使用 TCP 網路連線。https
也支援在 URL 中使用 https://
協定,而非 http://
。
若要透過 Unix 域套接字將 POST
要求傳送至 API 端點
const response = await fetch("https://hostname/a/path", {
unix: "/var/run/path/to/unix.sock",
method: "POST",
body: JSON.stringify({ message: "Hello from Bun!" }),
headers: {
"Content-Type": "application/json",
},
});
const body = await response.json();