Middleware: bufferRequest

Wrap a Request so its body can be read more than once.

A Fetch Request body is a single-use stream — the first reader of req.text() / req.json() / … locks out every later one. This returns a proxy that reads the underlying body at most once and caches the bytes, so a body-verifying middleware (e.g. a webhook signature check) and the handler can each read it, in any form:

The proxy is created once at the entry call and flows down the stack, so the cache is shared across every layer.

One deliberate limit: reading the raw req.body stream (e.g. handing the request to fetch() to forward it) bypasses the cache. By design this is a buffering model, not a streaming one — to forward the body, reconstruct it, e.g. new Request(req.url, { method: req.method, headers: req.headers, body: await req.arrayBuffer() }).

Applied automatically by defineMiddleware at the entry call, so a normal stack never calls this directly. It is public for the same reason seedContext and isContext are: a host embedding the engine (e.g. @supabase/server) can be the entry point itself, and an entry that seeds a context without buffering hands the whole stack below it a single-use body — the first reader silently locks out every later one. Buffer alongside seeding:

const upstream = isContext(arg) ? arg : seedContext(arg)
const request = isContext(arg) || !req.body ? req : bufferRequest(req)

Parameters