/** * Bun implementation of the Effect `HttpServer`. * * `make` creates a scoped HTTP server from `Bun.serve`, converting Bun * `Request` values into `HttpServerRequest` values and Effect * `HttpServerResponse` values back into Web `Response` values. The server * supports streaming bodies, multipart requests, file responses through * `BunHttpPlatform`, and WebSocket upgrades. `Bun.serve` cannot hand over the * connection for other protocols, so other upgrades fail. This module also * provides layers for the server alone, the Bun HTTP support services, the * combined server, configurable server options, and a test server with an * HTTP client. * * @stability unstable * @since 4.0.0 */ import type { ServerWebSocket } from "bun"; import * as Config from "effect/Config"; import type { ConfigError } from "effect/Config"; import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import type * as FileSystem from "effect/FileSystem"; import * as Etag from "effect/http/Etag"; import type { HttpClient } from "effect/http/HttpClient"; import type { HttpPlatform } from "effect/http/HttpPlatform"; import * as Server from "effect/http/HttpServer"; import * as Error from "effect/http/HttpServerError"; import * as ServerRequest from "effect/http/HttpServerRequest"; import * as ServerResponse from "effect/http/HttpServerResponse"; import * as Layer from "effect/Layer"; import * as NetAddress from "effect/net/NetAddress"; import type * as Path from "effect/Path"; import * as Scope from "effect/Scope"; import * as Socket from "effect/socket/Socket"; import * as BunServices from "./BunServices.ts"; /** * Bun serve options accepted by the HTTP server, extended with typed route definitions. * * @stability unstable * @category options * @since 4.0.0 */ export type ServeOptions = (Bun.Serve.UnixServeOptions | Bun.Serve.HostnamePortServeOptions) & { readonly routes?: Bun.Serve.Routes; }; /** * WebSocket tuning options forwarded to `Bun.serve`'s `websocket` handler. * * **Details** * * The lifecycle handlers (`open`, `message`, `close`, ...) are managed by the * server and cannot be overridden; everything else — such as * `perMessageDeflate` compression, payload limits, and idle timeouts — passes * through, e.g. * `BunHttpServer.layer({ port: 3000, websocket: { perMessageDeflate: true } })`. * * The `compressionThreshold` option controls the minimum message size in bytes * that is compressed when per-message deflate is negotiated. It defaults to * 1024, matching the default threshold of Node's `ws` server. * * @stability unstable * @category options * @since 4.0.0 */ export type WebSocketOptions = Omit, "open" | "message" | "close" | "drain" | "ping" | "pong" | "data" | "binaryType"> & { /** * The minimum message size in bytes that is compressed when per-message * deflate is negotiated. * * @default 1024 */ readonly compressionThreshold?: number | undefined; }; /** * Creates a scoped Bun `HttpServer` from `Bun.serve` options, stopping the server on scope finalization with optional graceful shutdown settings. * * **Details** * * `idleTimeout` defaults to `0`, disabling Bun's 10 second idle timeout, so * quiet response streams and slow uploads are bounded only by the `Stream` * and `Effect` timeouts in your handlers or middleware. An explicit value is * passed to Bun unchanged. * * **Gotchas** * * Without an idle timeout, Bun also keeps idle keep-alive connections and * clients that stopped reading a response open, and handler timeouts cannot * catch either. Directly exposed servers should set `idleTimeout` (seconds, * at most 255) or sit behind a proxy with its own idle timeout. * * @stability unstable * @category constructors * @since 4.0.0 */ export declare const make: (options: ServeOptions & { readonly disablePreemptiveShutdown?: boolean | undefined; readonly gracefulShutdownTimeout?: Duration.Input | undefined; readonly websocket?: WebSocketOptions | undefined; }) => Effect.Effect<{ readonly serve: { (effect: Effect.Effect): Effect.Effect | Scope.Scope>; >(effect: Effect.Effect, middleware: import("effect/http/HttpMiddleware").HttpMiddleware.Applied): Effect.Effect, ServerRequest.HttpServerRequest> | Scope.Scope>; }; readonly address: NetAddress.SocketAddress; }, Error.ServeError, Scope.Scope>; /** * Layer that provides only `HttpServer` by constructing a scoped Bun server from the supplied serve options. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerServer: (options: ServeOptions & { readonly disablePreemptiveShutdown?: boolean | undefined; readonly gracefulShutdownTimeout?: Duration.Input | undefined; readonly websocket?: WebSocketOptions | undefined; }) => Layer.Layer; /** * Layer that provides Bun HTTP support services: `HttpPlatform`, weak ETag generation, and `BunServices`. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerHttpServices: Layer.Layer; /** * Layer that provides a Bun `HttpServer` together with the Bun HTTP platform, ETag generator, and Bun services. * * **Details** * * `idleTimeout` defaults to `0` (disabled); see `make`. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layer: (options: ServeOptions & { readonly disablePreemptiveShutdown?: boolean | undefined; readonly gracefulShutdownTimeout?: Duration.Input | undefined; readonly websocket?: WebSocketOptions | undefined; }) => Layer.Layer; /** * Layer that starts a Bun HTTP server on an ephemeral port for tests. * * @stability unstable * @category testing * @since 4.0.0 */ export declare const layerTest: Layer.Layer; /** * Creates the Bun HTTP server and support-services layer from configurable serve options. * * @stability unstable * @category layers * @since 4.0.0 */ export declare const layerConfig: (options: Config.Wrap & { readonly disablePreemptiveShutdown?: boolean | undefined; readonly gracefulShutdownTimeout?: Duration.Input | undefined; readonly websocket?: WebSocketOptions | undefined; }>) => Layer.Layer; interface WebSocketContext { readonly deferred: Deferred.Deferred>; readonly buffer: Array; closeError: Socket.SocketError | undefined; run: (_: Uint8Array | string) => void; onClose: (error: Socket.SocketError) => void; } export {}; //# sourceMappingURL=BunHttpServer.d.ts.map