Skip to content

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 네트워크 연결을 사용하는 대신 소켓을 사용하여 서버에 요청을 보냅니다. URL 에서 http:// 대신 https:// 프로토콜을 사용하면 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();

Bun by www.bunjs.com.cn 편집