Skip to Content
Reference@streamsy/serve

Bun

Reference for the host exports in @streamsy/serve/bun. They provide a scoped Bun HTTP listener.

import * as Bun from "@streamsy/serve/bun";
import type { Duration, Effect, Layer } from "effect";
import type { Etag, HttpPlatform, HttpServer, HttpServerError } from "effect/http";
import type { BunServices } from "@effect/platform-bun";

Bun.listener is the HTTP listener Layer of a Streamsy host on Bun. Provide it to Serve.layer. See Run a process server.

Listener

Bun.listener

declare function listener(
  options?: Bun.ListenerOptions,
): Layer.Layer<
  Etag.Generator | HttpPlatform.HttpPlatform | HttpServer.HttpServer | BunServices.BunServices,
  HttpServerError.ServeError,
  never
>;

Bun.listener binds the port when its Layer builds, and closes the server when the scope of the Layer closes. When the bind fails, the Layer fails with HttpServerError.ServeError. The Layer requires no services.

The listener binds port 3000 on 127.0.0.1 unless the options set others. Without gracefulShutdownTimeout, the listener turns off the preemptive shutdown of the platform server, and its scope finalizer waits for in-flight requests to end. With gracefulShutdownTimeout, the platform server stops waiting after that time.

The default idleTimeout is 0. Bun otherwise closes an idle connection after 10 seconds, which would end a parked long poll.

Options

Bun.ListenerOptions

interface ListenerOptions {
  readonly port?: number;
  readonly hostname?: string;
  readonly idleTimeout?: number;
  readonly gracefulShutdownTimeout?: Duration.Input;
}

Bun.ListenerOptions holds the options of Bun.listener.

FieldDefaultMeaning
port3000The port to bind
hostname127.0.0.1The address to bind
idleTimeout0The idle timeout of a connection, in seconds. 0 turns it off
gracefulShutdownTimeoutUnsetThe longest wait for in-flight requests during shutdown

Defaults

Bun.DEFAULT_PORT

declare const DEFAULT_PORT: 3000;

Bun.DEFAULT_PORT is the default port of Bun.listener: 3000.

Bun.DEFAULT_HOSTNAME

declare const DEFAULT_HOSTNAME: "127.0.0.1";

Bun.DEFAULT_HOSTNAME is the default bind address of Bun.listener: 127.0.0.1.

On this page