Skip to content

feat: export bufferRequestInPlace for frameworks that keep the same Request - #66

Open
martijn00 wants to merge 4 commits into
supabase:mainfrom
martijn00:feat/buffer-request-in-place
Open

martijn00 wants to merge 4 commits into
supabase:mainfrom
martijn00:feat/buffer-request-in-place

Conversation

@martijn00

Copy link
Copy Markdown

Stacked on #54. This branch contains #54's three commits so the shared readers carry its per-read copies and blob() content type. The change for this PR is the last commit, 31f98eb. Once #54 merges, the diff shrinks to that commit.

What

  • New export bufferRequestInPlace(req). It redefines arrayBuffer, bytes, blob, text, json, formData and clone as non-enumerable own properties of the request and returns the same request. It is a no-op without a body, after the body was used, and when called twice.
  • bufferRequest and bufferRequestInPlace share one private bufferedReaders factory, so the in-place variant gets the same per-read copies and content-type handling. clone() on an in-place request returns a bufferRequest proxy over the same cache.
  • bufferRequest's TSDoc now says when to use which.

Why

bufferRequest returns a proxy, which works when the caller can hand the proxy on (Hono's c.req.raw, H3's event.req). TanStack Start, SvelteKit (event.request) and React Router (request in loaders and actions) hand the route the same Request object their middleware saw. In a bridge for those, an entry that reads the body consumes the stream and the route's own request.json() throws. Every bridge currently carries its own copy of this code, which has to track bufferRequest's behavior.

Closes #63

Testing

  • src/core/runtime.test.ts: every read form on the same object, a stack and then the route reading the same request, form bodies, per-read copies, clone(), and the no-op cases.
  • test/runtimes/workers: the built dist buffers a workerd Request in place, runs a stack that reads the body, and reads it again from the same object.

Ran locally: pnpm format:check, pnpm lint, pnpm typecheck, pnpm typecheck:min, pnpm build, pnpm typecheck:consumer, pnpm smoke, pnpm check-exports, pnpm test (135 tests), pnpm test:workers, pnpm test:bun and the JSR dry run. I did not run pnpm test:deno (no Deno locally).

Alternatives

  • An option on bufferRequest ({ inPlace: true }) instead of a second export. A separate name made the different return contract (same object versus a proxy) clearer.
  • Returning void like the existing bridge copies. Returning the request lets it be used inline.

Prior art: better-supabase bridges/shared.ts (bufferInPlace).

🤖 Generated with Claude Code

johnstonmatt and others added 4 commits November 25, 2026 07:13
bufferRequest handed every arrayBuffer() caller the same cached ArrayBuffer, and bytes() a view over it, so a middleware that wrote into its read changed the body every later layer saw, and one that transferred it left the handler reading an empty body. Each read now goes through a fresh Response over the cached bytes, whose constructor copies them.
The buffered request built its blob from the raw bytes, so blob().type was always empty where a native Request takes it from the Content-Type, and forwarding `await req.blob()` dropped it. blob() now reads through the same Response as the other forms, which applies the request's headers.
When a downstream layer threw and the middleware's catch recovered with `yield new Response(...)`, the runtime returned that response without closing the generator, so its finally block never ran. It now calls gen.return() when gen.throw() leaves the generator suspended, as the other paths that receive a yielded Response already do.
…equest

bufferRequest returns a proxy, which only helps when the caller can hand it
on. TanStack Start, SvelteKit and React Router give the route the same
Request their middleware saw. bufferRequestInPlace redefines the body
readers on the request itself, sharing the readers bufferRequest uses.

Closes supabase#63

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export an in-place bufferRequest for frameworks that hand the route the same Request

2 participants