@hyperttp/transport-bun
v0.2.2
Published
High-performance HTTP transport for Bun with native fetch optimizations
Maintainers
Readme
BunTransport
BunTransport is a network transport implementation for HyperTransport, designed to run in the Bun runtime.
It wraps fetch and adds:
- concurrency limiting
- request timeouts via
AbortSignal - automatic cookie handling
- header normalization
keepalivesupport- access to Bun
tlsoptions
Interface
/**
* @ru Общий интерфейс для реализации сетевых транспортов.
* @en Unified interface for building runtime-specific network transports.
*/
export interface HyperTransport {
execute(req: TransportRequest): Promise<TransportResponse>;
close?(): Promise<void>;
destroy?(): Promise<void>;
}BunTransport implements this interface and can be used as a runtime-specific HTTP transport in client libraries.
Features
fetch-based implementation withsignalsupportredirect: "manual"- automatic merging of cookies from responses and subsequent requests
- cookie cache per domain
- request concurrency limiting
- request timeout support
- automatic
User-Agentinjection from config (if not provided manually) - compatibility with
TransportRequest/TransportResponse
Installation
BunTransport is designed for Bun, so the project must run in the Bun runtime.
bun installUsage
import { BunTransport } from "./BunTransport";
const transport = new BunTransport({
network: {
timeout: 10_000,
maxConcurrent: 32,
userAgent: "Hyperttp/1.0",
keepAliveTimeout: 30_000,
rejectUnauthorized: true,
},
});
const response = await transport.execute({
url: "https://example.com",
method: "GET",
headers: {},
body: null,
});
console.log(response.status);
console.log(await response.text());Configuration
BunTransport reads configuration from HttpClientOptions.network.
Supported options:
network?: {
maxConcurrent?: number;
timeout?: number;
userAgent?: string;
keepAliveTimeout?: number;
rejectUnauthorized?: boolean;
}Option behavior
maxConcurrent— maximum number of concurrent active requests.timeout— global request timeout in milliseconds.userAgent— injected intoUser-Agentheader if not explicitly set.keepAliveTimeout— enableskeepalivemode when provided.rejectUnauthorized— passed to Bunfetchviatlsconfiguration.
Cookies
The transport supports persistent cookie storage and reuse between requests.
How it works
- When the server returns
Set-Cookie, cookies are stored per domain. - On subsequent requests to the same domain, cookies are automatically attached to the
Cookieheader. - If the user provides their own
Cookieheader, it is merged with stored cookies.
Example
await transport.execute({
url: "https://example.com/login",
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ login: "demo", password: "secret" }),
});
const res = await transport.execute({
url: "https://example.com/profile",
method: "GET",
headers: {},
body: null,
});Concurrency limiting
If maxConcurrent > 0, incoming requests are queued until a slot becomes available.
This is useful for:
- preventing server overload
- limiting runtime resource usage
- avoiding traffic spikes
Timeout & cancellation
The transport uses AbortSignal:
- if
req.signalis provided, it is respected - if
timeoutis set,AbortSignal.timeout(...)is created - if both exist, they are combined using
AbortSignal.any(...)
If a request is aborted, the signal reason (or a default AbortError) is thrown.
Response API
BunTransportResponse wraps the native Response and provides:
statusurlbodytext()json()headers
Body behavior
If body exists, a dump() method may be attached to it, allowing safe stream consumption and resource cleanup.
Internal utilities
fastGetHostname(url: string): string
Fast hostname extraction from a URL without full parsing.
normalizeHeaderValue(name, value)
Normalizes header values:
Cookieheaders are joined using;- other arrays are joined using
,
normalizeCookieHeader(value)
Converts cookie header values into a normalized string.
throwIfAborted(signal)
Checks whether an operation has been aborted and throws if so.
Shutdown
BunTransport supports resource cleanup:
await transport.close();
await transport.destroy();Both methods clear:
- pending request queue
- cookie jar
- cookie cache
Notes
- Designed specifically for the Bun runtime, not Node.js.
- Uses
redirect: "manual"for fetch requests. - Minimal interference with request data; only headers, cookies, and signals are normalized or extended.
TransportRequest.bodyis treated asBodyInit | null.
License
MIT
