fix(method-override): add duplex option when body is a ReadableStream#5022
Open
LeSingh1 wants to merge 1 commit into
Open
fix(method-override): add duplex option when body is a ReadableStream#5022LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
The query override branch rebuilds the request from c.req.raw.body, which is a ReadableStream. The Fetch spec requires duplex: 'half' on RequestInit when the body is a stream; undici (Node.js native fetch) enforces this and throws TypeError: duplex option is required when sending a body. The form and header branches consume the body into a FormData or URLSearchParams before constructing the new Request, so they are unaffected. Only the query branch passes the raw stream directly. Adds a regression test for a body-bearing POST with query override.
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.
The
queryoverride branch passesc.req.raw.body(aReadableStream) directly tonew Request()without settingduplex: 'half'. The Fetch spec requires this option whenever the request body is a stream, and undici (the native fetch implementation in Node.js) enforces it with:This means any HTML form that submits to a route using query-based method override will get a 500, because forms always send a body.
The
formandheaderbranches are not affected. They consume the body intoFormDataorURLSearchParamsbefore constructing the new request, so the stream is already resolved by the timenew Request()is called.The fix spreads
{ duplex: 'half' }into theRequestInitonly whenbodyis non-null, and casts toRequestInitbecause the TypeScript lib types do not yet includeduplex.Added a regression test: a
POST ?_method=DELETEwith aapplication/x-www-form-urlencodedbody. The existing test for the query branch only used a bodyless POST.Fixes part of #5010.