在 Bun 中,`fetch()` 函式的 unix 選項允許你透過 Unix domain socket 傳送 HTTP 請求。
const unix = "/var/run/docker.sock";
const response = await fetch("https:///info", { unix });
const body = await response.json();
console.log(body); // { ... }
unix 選項是一個字串,用於指定 Unix domain socket 的本地檔案路徑。`fetch()` 函式將使用該 socket 向伺服器傳送請求,而不是使用 TCP 網路連線。透過在 URL 中使用 https:// 協議而不是 http://,也可以支援 https。
要透過 Unix domain socket 向 API 端點發送 POST 請求
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();