Skip to content

fix: buffered body copies, blob content-type, and generator finally - #54

Open
johnstonmatt wants to merge 3 commits into
mainfrom
FUNC-1146/middleware/buffered-body-and-generator-fixes
Open

johnstonmatt wants to merge 3 commits into
mainfrom
FUNC-1146/middleware/buffered-body-and-generator-fixes

Conversation

@johnstonmatt

Copy link
Copy Markdown
Collaborator

Summary

Three fixes in the core, one commit each.

Buffered body reads (bufferRequest): every read form goes through a fresh Response over the one cached read.

 bufferRequest(req)
   cache = req.arrayBuffer()             # read once, as before
-  arrayBuffer() → cache                 # one buffer shared by every reader
-  bytes()       → view over cache
-  blob()        → new Blob([cache])     # type is ""
-  text()        → TextDecoder(cache)
+  each read     → new Response(cache, { headers: req.headers }).<read>()
+                  # the constructor copies the bytes; each form decodes as Request's does
  • A middleware that writes into or transfers the bytes it read no longer changes what later layers read. Zeroing a read used to make the handler's text() return NULs, and transferring it made text() return "".
  • blob().type comes from the Content-Type, as on a native Request.

Generator finally (defineMiddleware):

 downstream throws err
   recovered = gen.throw(err)
+  if the generator is still suspended: gen.return()   # runs `finally`
   if recovered.value is a Response: return it
   rethrow err

A catch that recovers with yield new Response(...) now has its finally run, as the other paths that receive a yielded Response already do.

Evidence

Each commit adds the vitest cases that fail before its fix.

  • Before:

    • gives each reader its own bytes: writing into one read leaves later reads intact: the handler reads "\0\0\0\0\0\0\0" instead of {"a":1}
    • gives each reader its own buffer: detaching one read leaves later reads intact: the handler reads ""
    • blob() carries the content-type, as a native Request does: "" instead of "application/json"
    • runs `finally` when a try/catch recovers a downstream throw by yielding a Response: cleanup log is []

    After: all 128 tests pass; tsc, ESLint and Prettier are clean.

Outside the suite, property-based tests driving the built dist in Node also pass:

  • Every buffered read form, in random order and through clone(), matches a fresh native Request over 5,000 cases, including readers that write into or transfer their bytes.
  • Random stacks of plain and generator middleware and composites match a model of the documented semantics over 5,000 cases, plus 3,000 cases of 2 to 5 interleaved concurrent requests.

Standalone reproductions of all three pass on Node, Bun and Deno. The Workers suite runs in CI.

Merge Danger

Door: two-way

No API or type changes; reverting restores the previous behavior.

Blast Radius: request bodies

  • Each buffered read allocates its own copy of the body, where arrayBuffer() used to return the cache itself. The extra memory per read is the body's size, and only while the caller holds it.
  • req.arrayBuffer() returns a different buffer on each call, so code comparing buffers by identity across reads would see a change.
  • Every read form now depends on new Response(bytes, { headers }). formData() already did, so every supported host already runs it.
  • blob() has a non-empty type when the request has a Content-Type. Code that relied on "" sees the real type.

@johnstonmatt
johnstonmatt requested review from a team as code owners November 25, 2026 11:11
@pkg-pr-new

pkg-pr-new Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@supabase/middleware@54

commit: d42c999

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.
@johnstonmatt
johnstonmatt force-pushed the FUNC-1146/middleware/buffered-body-and-generator-fixes branch from a26a858 to d42c999 Compare November 25, 2026 11:13

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.

1 participant