Skip to content

BunHttpServer

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. 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.

8 exports Added in v4.0.0 Source

Constructors

make

Added in v4.0.0 Source

Creates a scoped Bun HttpServer from Bun.serve options, stopping the server on scope finalization with optional graceful shutdown settings.

Signature

declare const make: <R extends string>(
  ...args: [options: any]
) => Effect<
  {
    readonly address: Address;
    readonly serve: {
      <E, R>(
        effect: Effect<HttpServerResponse, E, R>,
      ): Effect<void, never, Scope | Exclude<R, HttpServerRequest>>;
      <E, R, App extends Effect<HttpServerResponse, any, any>>(
        effect: Effect<HttpServerResponse, E, R>,
        middleware: Applied<App, E, R>,
      ): Effect<void, never, Scope | Exclude<R, HttpServerRequest>>;
    };
  },
  never,
  Scope
>;

Layers

layer

Added in v4.0.0 Source

Layer that provides a Bun HttpServer together with the Bun HTTP platform, ETag generator, and Bun services.

Signature

declare function layer<R extends string>(
  options: any,
): Layer<HttpPlatform | Generator | BunServices | HttpServer>;

layerConfig

Added in v4.0.0 Source

Creates the Bun HTTP server and support-services layer from configurable serve options.

Signature

declare function layerConfig<R extends string>(
  options: Config<any>,
): Layer<HttpPlatform | Generator | FileSystem | Path | HttpServer, ConfigError>;

Layer that provides Bun HTTP support services: HttpPlatform, weak ETag generation, and BunServices.

Signature

declare const layerHttpServices: Layer.Layer<
  HttpPlatform | Etag.Generator | BunServices.BunServices
>;

layerServer

Added in v4.0.0 Source

Layer that provides only HttpServer by constructing a scoped Bun server from the supplied serve options.

Signature

declare const layerServer: <R extends string>(
  options: ServeOptions<R> & {
    readonly disablePreemptiveShutdown?: boolean;
    readonly gracefulShutdownTimeout?: Duration.Input;
    readonly websocket?: WebSocketOptions;
  },
) => Layer.Layer<Server.HttpServer>;

Options

ServeOptions type

Added in v4.0.0 Source

Bun serve options accepted by the HTTP server, extended with typed route definitions.

Signature

type ServeOptions<R extends string> =
  | Bun.Serve.UnixServeOptions<WebSocketContext>
  | (Bun.Serve.HostnamePortServeOptions<WebSocketContext> & {
      readonly routes?: Bun.Serve.Routes<WebSocketContext, R>;
    });

WebSocketOptions type

Added in v4.0.0 Source

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 } }).

Signature

type WebSocketOptions = Omit<
  Bun.WebSocketHandler<WebSocketContext>,
  "open" | "message" | "close" | "drain" | "ping" | "pong" | "data" | "binaryType"
>;

Testing

layerTest

Added in v4.0.0 Source

Layer that starts a Bun HTTP server on an ephemeral port for tests.

Signature

declare const layerTest: Layer.Layer<
  Server.HttpServer | HttpPlatform | FileSystem.FileSystem | Etag.Generator | Path.Path | HttpClient
>;