Skip to main content

Import

Examples

Single middleware

Pipeline composition

API

middlewarePipeline.use(middleware)

Add a middleware handler to the pipeline. Returns: this

middlewarePipeline.useFor(pattern, )

Add a middleware handler that only runs for matching URL patterns. Returns: this

middlewarePipeline.onTeardown(cb)

Register a cleanup callback that runs once per request after each execute()/handle() response body closes, is canceled, or errors. Bodyless, locked, or already-read responses and handler/middleware exceptions clean up before the call resolves. Returns: this

middlewarePipeline.compose()

Compose all registered middleware into a single handler function. Returns: MiddlewareHandler

middlewarePipeline.execute(req, env, executionCtx, adapter)

Execute the pipeline for an incoming request. Returns: Promise<Response>

middlewarePipeline.handle(req, handler)

Run the middleware pipeline with a final request handler. Unlike execute, which returns a 404 when no middleware responds, handle invokes the given handler as the terminal step so middleware can add headers, validate auth, etc. before the handler runs. Returns: Promise<Response>

middlewarePipeline.teardown()

Drain and discard all registered teardown callbacks. Unlike the per-request cleanup run by execute() / handle(), this clears callbacks so they never run again. Returns: Promise<void>

middlewarePipeline.getMiddleware()

List registered middleware with metadata. Returns: Array<{ name?: string; order?: number }>

Type Reference

CorsOptions

Options accepted by cors.

RateLimitOptions

Options accepted by rate limit.

MemoryRateLimitStoreOptions

Options accepted by the in-memory rate limit store.

LoggerOptions

Options accepted by logger.

TimeoutOptions

Options accepted by timeout.

Exports

Functions

Classes

Types