Bun では、fetch() の unix オプションを使用して、unix ドメインソケット 経由で HTTP リクエストを送信できます。
ts
const unix = "/var/run/docker.sock";
const response = await fetch("http://localhost/info", { unix });
const body = await response.json();
console.log(body); // { ... }unix オプションは、unix ドメインソケットへのローカルファイルパスを指定する文字列です。fetch() 関数は、TCP ネットワーク接続を使用する代わりに、ソケットを使用してサーバーにリクエストを送信します。https も、URL で http:// プロトコルの代わりに https:// プロトコルを使用することでサポートされています。
unix ドメインソケット経由で API エンドポイントに POST リクエストを送信するには:
ts
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();