constructor

FetchSession.constructor

constructor FetchSession(

Referenced types

interface FetchSessionInit

  • keepAlive?: boolean | { idleTimeout: number; maxIdleSockets: number }

    Connection reuse. false closes every connection after its response. The limits do not apply to HTTP/3 connections.

  • proxy?: FetchProxyOption

    The proxy for the requests of this session.

  • tls?: BunFetchRequestInitTLS

    TLS options for the connections of this session. A request that passes its own tls uses that instead, as a whole.

    A checkServerIdentity given here runs once per connection, and the connection is then shared by the requests of this session.

  • unix?: string

    Send the requests of this session over a Unix socket.

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.