Skip to content
// EFFECT DAYS 2026

HttpEffect unstable

Runs Effect HTTP server handlers at platform boundaries.

This module turns effects that produce HttpServerResponse values into Web Request handlers and other server adapters. It also applies middleware, converts failures into responses, runs hooks before a response is sent, and manages request scopes for streamed responses.

12 exports Since v4.0.0 Source

Combinators

toHandled

unstable
Since v4.0.0

Runs an HTTP server effect, sends the produced response with the supplied handler, and converts failures into HTTP responses. On handler failure, the request scope closes with the original cause. If the response body was transferred with scopeTransferToStream, the scope closes with the body's exit once both the request and the body have finished. If the send handler returns an HttpServerResponse, such as the 101 response of an upgraded request, middleware observes it in place of the handler's response.

Gotchas

self, the middleware and handleResponse run uninterruptibly, so an interrupt, which server adapters send on client disconnect and shutdown, takes effect once the response is handled. Wrap self in Effect.interruptible to let it be interrupted while producing the response.

Signature

declare function toHandled<E, R, EH, RH>(self: Effect<HttpServerResponse, E, R>, handleResponse: (request: HttpServerRequest, response: HttpServerResponse) => Effect<unknown, EH, RH>, middleware?: HttpMiddleware): Effect<void, never, HttpServerRequest | Exclude<R, Scope> | Exclude<RH, Scope>>

Converting

Since v4.0.0

Adapts a Web Request handler into an HTTP server effect for the current HttpServerRequest.

Signature

declare function fromWebHandler(handler: (request: Request) => Promise<Response>): Effect<HttpServerResponse, HttpServerError, HttpServerRequest>
Since v4.0.0

Converts an HTTP server effect into a Web Request handler using an empty base context.

Gotchas

The effect runs uninterruptibly by default, so aborting the request's signal does not stop it before it produces a response. Wrap the effect in Effect.interruptible to let an abort interrupt it.

Signature

declare const toWebHandler: <E>(self: Effect.Effect<HttpServerResponse, E, HttpServerRequest | Scope.Scope>, middleware?: HttpMiddleware) => (request: Request, context?: Context.Context<never>) => Promise<globalThis.Response>
Since v4.0.0

Builds a Web Request handler for an HTTP server effect using a layer to provide its services, returning the handler with a dispose function.

Details

The layer is built immediately, so the cost is paid when the handler is created rather than on the first request. A layer that performs asynchronous work while building may still be in progress when the first request arrives, in which case that request waits for the build to finish. If the build fails, every request rejects with the build error.

Gotchas

The effect runs uninterruptibly by default, so aborting the request's signal does not stop it before it produces a response. Wrap the effect in Effect.interruptible to let an abort interrupt it.

Signature

declare function toWebHandlerLayer<E, R, Provided, LE, ReqR = Exclude<R, Scope | HttpServerRequest | Provided>>(self: Effect<HttpServerResponse, E, R>, layer: Layer<Provided, LE>, options?: {
readonly memoMap?: MemoMap;
readonly middleware?: HttpMiddleware;
}): {
readonly dispose: () => Promise<void>;
readonly handler: [ReqR] extends [never] ? (request: Request, context?: Context<never>) => Promise<Response> : (request: Request, context: Context<ReqR>) => Promise<Response>;
}
Since v4.0.0

Builds a Web Request handler from a layer and handler factory, returning the handler with a dispose function for the layer scope.

Details

Every call to dispose returns the same promise, which resolves once the layer resources have been released.

The layer is built immediately, so the cost is paid when the handler is created rather than on the first request. A layer that performs asynchronous work while building may still be in progress when the first request arrives, in which case that request waits for the build to finish. If the build fails, every request rejects with the build error.

Gotchas

The effect runs uninterruptibly by default, so aborting the request's signal does not stop it before it produces a response. Wrap the effect in Effect.interruptible to let an abort interrupt it.

Signature

declare function toWebHandlerLayerWith<E, Provided, LE, R, ReqR = Exclude<R, Scope | HttpServerRequest | Provided>>(layer: Layer<Provided, LE>, options: {
readonly memoMap?: MemoMap;
readonly middleware?: HttpMiddleware;
readonly toHandler: (context: Context<Provided>) => Effect<Effect<HttpServerResponse, E, R>, LE>;
}): {
readonly dispose: () => Promise<void>;
readonly handler: [ReqR] extends [never] ? (request: Request, context?: Context<never>) => Promise<Response> : (request: Request, context: Context<ReqR>) => Promise<Response>;
}
Since v4.0.0

Converts an HTTP server effect into a Web Request handler using the supplied base context and optional middleware.

Gotchas

The effect runs uninterruptibly by default, so aborting the request's signal does not stop it before it produces a response. Wrap the effect in Effect.interruptible to let an abort interrupt it.

Signature

declare function toWebHandlerWith<Provided, R = never, ReqR = Exclude<R, Scope | HttpServerRequest | Provided>>(context: Context<Provided>): <E>(self: Effect<HttpServerResponse, E, R>, middleware?: HttpMiddleware) => [ReqR] extends [never] ? (request: Request, context?: Context<never>) => Promise<Response> : (request: Request, context: Context<ReqR>) => Promise<Response>

Handlers

Registers an additional pre-response handler for the current HTTP server request.

Signature

declare function appendPreResponseHandler(handler: PreResponseHandler): Effect<void, never, HttpServerRequest>

PreResponseHandler

type unstable
Since v4.0.0

Function run with the current request and response just before the response is sent, allowing the response to be replaced or failing with HttpServerError.

Signature

type PreResponseHandler = (request: HttpServerRequest, response: HttpServerResponse) => Effect.Effect<HttpServerResponse, HttpServerError>

Runs an effect after registering a pre-response handler for the current HTTP server request.

Signature

declare const withPreResponseHandler: {
(handler: PreResponseHandler): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, HttpServerRequest | R>;
<A, E, R>(self: Effect<A, E, R>, handler: PreResponseHandler): Effect<A, E, HttpServerRequest | R>;
}

Resource Management

Since v4.0.0

Disables automatic closing for an HTTP request scope.

When to use

Use when the host must keep the scope created by toHandled open past the response and its body, for example while background work still uses request resources.

Details

Only the close that runs when the request finishes is disabled. A body transferred with scopeTransferToStream still closes the scope when it exits. If the body is not transferred anywhere, whether in the handler, middleware or handleResponse, the scope stays open until the host closes it with Scope.close and its chosen exit.

Gotchas

Without scopeTransferToStream, finishing or aborting the body does not release request resources. The host must close the scope on every path, including failures and interruption, or those resources can leak.

Signature

declare function scopeDisableClose(scope: Scope): void
Since v4.0.0

Returns a streaming server response that closes the request scope with the body stream's exit.

Details

For a request scope created by toHandled, the scope closes once both the body stream has exited and the request has finished, including middleware and tracing. If the body exits first, for example because the request signal was already aborted, the close waits for the request. Any other scope closes as soon as the body stream exits.

Responses without a stream body are returned unchanged and the request scope keeps its usual close.

Signature

declare function scopeTransferToStream(response: HttpServerResponse): HttpServerResponse

Unsafe

Registers a pre-response handler for the HTTP server request that owns the supplied fiber context. Does nothing outside a server request.

Signature

declare const appendPreResponseHandlerUnsafe: (context: Context.Context<never>, handler: PreResponseHandler) => void