You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Per oven-sh/bun#1381 the BunJS runtime does not support .clone()
The withContent() middleware relies on .clone().json() to throw an error if the body is not parsed as JSON (and subsequently as formData). Since .clone() does not properly clone the request body, all inbuilt parsings fail, resulting in the post body being returned by .text()
Example Router Code
router.post('/path',withContent,req=>{returntypeofreq.content})// "string" no matter if the post body is JSON or formData
Request Details
This code is executed on BunJS runtime bun run app.js
Steps to Reproduce
Steps to reproduce the behavior:
Post a request to an endpoint that's defined as above running on BunJS
Expected Behavior
withContent should parse the post body, but it can't because there is none after .clone()
Actual Behavior
the .catch() is executed every time because .json() and .formData() are being passed empty data due to .clone() failing to clone the post body
Environment (please complete the following information):
Environment: Bun
itty-router Version: >4.2
Additional Context
Maybe this is just something that goes into the ity-router documentation. I'm only reporting it because itty-router is so often linked with BunJS, it seems relevant to be published somewhere that this is a known limitation.
I ended up writing my own withContent middleware, but it's cast more in the approach of the pre-v4.2 to parsing post body. It's not itty, but it works.
constwithContent=asynct=>{if(!t.body)thrownewStatusError(400,"No body in the request");constcontentType=t.headers.get('Content-Type');try{if(contentType.includes('json')){t.content=awaitt.json();}elseif(contentType.includes('form-data')){t.content=Object.fromEntries([...(awaitt.formData()).entries()]);}else{t.content=awaitt.text();if(contentType.includes("x-www-form-urlencoded")){t.content=Object.fromEntries(newURLSearchParams(t.content).entries());}}}catch(e){thrownewStatusError(400,e.message);}};
The text was updated successfully, but these errors were encountered:
Fantastic writeup - I'm considering adding environment-specific exports (e.g. itty-router/bun) for overrides that may require more verbosity... or just forcing withContent to check/honor content-type settings so it doesn't attempt more than one...
Describe the Issue
Per oven-sh/bun#1381 the BunJS runtime does not support .clone()
The withContent() middleware relies on .clone().json() to throw an error if the body is not parsed as JSON (and subsequently as formData). Since .clone() does not properly clone the request body, all inbuilt parsings fail, resulting in the post body being returned by .text()
Example Router Code
Request Details
This code is executed on BunJS runtime
bun run app.js
Steps to Reproduce
Steps to reproduce the behavior:
Expected Behavior
withContent should parse the post body, but it can't because there is none after .clone()
Actual Behavior
the .catch() is executed every time because .json() and .formData() are being passed empty data due to .clone() failing to clone the post body
Environment (please complete the following information):
Additional Context
Maybe this is just something that goes into the ity-router documentation. I'm only reporting it because itty-router is so often linked with BunJS, it seems relevant to be published somewhere that this is a known limitation.
I ended up writing my own withContent middleware, but it's cast more in the approach of the pre-v4.2 to parsing post body. It's not itty, but it works.
The text was updated successfully, but these errors were encountered: