constructor
FetchSession.constructor
Referenced types
interface FetchSessionInit
- keepAlive?: boolean | { idleTimeout: number; maxIdleSockets: number }
Connection reuse.
falsecloses every connection after its response. The limits do not apply to HTTP/3 connections. - tls?: BunFetchRequestInitTLS
TLS options for the connections of this session. A request that passes its own
tlsuses that instead, as a whole.A
checkServerIdentitygiven here runs once per connection, and the connection is then shared by the requests of this session.
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 afetch. Asessionininitdoes not replace this one.It has no
preconnect, so where an option is typedtypeof fetch(which in Bun includesfetch.preconnect), passsession.fetch as typeof fetch.const client = new SomeClient({ fetch: session.fetch }); Close the idle connections in this session's pool. Requests in flight finish, and the session stays usable.