Repository navigation
fix: buffered body copies, blob content-type, and generator finally - #54
Open
johnstonmatt wants to merge 3 commits into
Open
johnstonmatt wants to merge 3 commits into
johnstonmatt wants to merge 3 commits into
Conversation
commit: |
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
force-pushed
the
FUNC-1146/middleware/buffered-body-and-generator-fixes
branch
from
November 25, 2026 11:13
a26a858 to
d42c999
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three fixes in the core, one commit each.
Buffered body reads (
bufferRequest): every read form goes through a freshResponseover the one cached read.text()return NULs, and transferring it madetext()return"".blob().typecomes from the Content-Type, as on a nativeRequest.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 errA
catchthat recovers withyield new Response(...)now has itsfinallyrun, as the other paths that receive a yieldedResponsealready 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
distin Node also pass:clone(), matches a fresh nativeRequestover 5,000 cases, including readers that write into or transfer their bytes.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
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.new Response(bytes, { headers }).formData()already did, so every supported host already runs it.blob()has a non-emptytypewhen the request has a Content-Type. Code that relied on""sees the real type.