class

FetchSession

class FetchSession

Connection settings shared by the fetch() calls that name it, and the keep-alive connection pool they share. Connections are never shared between two sessions, or between a session and plain fetch().

const session = new Bun.FetchSession({
  proxy: { url: "http://proxy.internal:8080", respectNoProxy: false },
  tls: { ca: await Bun.file("corp-ca.pem").text() },
});

const response = await session.fetch("https://example.com");
// the same request, as an option of the global fetch():
await fetch("https://example.com", { session });
  • readonly fetch: (input: string | URL | Request, init?: BunFetchRequestInit) => Promise<Response>

    fetch() with this session. The function is bound: hand it to anything that takes a fetch. A session in init does not replace this one.

    It has no preconnect, so where an option is typed typeof fetch (which in Bun includes fetch.preconnect), pass session.fetch as typeof fetch.

    const client = new SomeClient({ fetch: session.fetch });
  • close(): void;

    Close the idle connections in this session's pool. Requests in flight finish, and the session stays usable.