Skip to content

fix(readBody, readRawBody): handle raw body as buffer#366

Merged
pi0 merged 4 commits intoh3js:mainfrom
johannschopplich:fix/read-body-buffer
Mar 28, 2023
Merged

fix(readBody, readRawBody): handle raw body as buffer#366
pi0 merged 4 commits intoh3js:mainfrom
johannschopplich:fix/read-body-buffer

Conversation

@johannschopplich
Copy link
Copy Markdown
Contributor

@johannschopplich johannschopplich commented Mar 24, 2023

I'm not sure if this implementation is enough. For my use-case in nuxt-api-party:

// https://github.com/johannschopplich/nuxt-api-party/blob/main/src/runtime/server.ts#L26
let _body = await readBody<EndpointFetchOptions>(event)

// Inconsistent `readBody` behavior in some Nitro presets
// https://github.com/unjs/nitro/issues/912
if (Buffer.isBuffer(_body))
  _body = destr((_body as Buffer).toString())

… this change fixed handling Buffer requests, e.g. when sending POST requests in a Cloudflare Nitro worker.

@pi0 pi0 changed the title fix: handle Buffer in readBody fix(readBody): handle raw body as buffer Mar 28, 2023
@pi0 pi0 changed the title fix(readBody): handle raw body as buffer fix(readBody, readRawBody): handle raw body as buffer Mar 28, 2023
@pi0
Copy link
Copy Markdown
Member

pi0 commented Mar 28, 2023

Thanks for the PR. I have pushed couple of changes to handle it also for readRawBody consistently + tests

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 28, 2023

Codecov Report

Merging #366 (cb7a486) into main (ba64f60) will increase coverage by 0.19%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #366      +/-   ##
==========================================
+ Coverage   74.57%   74.77%   +0.19%     
==========================================
  Files          26       26              
  Lines        2407     2406       -1     
  Branches      380      381       +1     
==========================================
+ Hits         1795     1799       +4     
+ Misses        612      607       -5     
Impacted Files Coverage Δ
src/utils/body.ts 92.62% <100.00%> (+4.00%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@pi0 pi0 merged commit 74a2e6d into h3js:main Mar 28, 2023
@pi0
Copy link
Copy Markdown
Member

pi0 commented Mar 28, 2023

Related fix 19d133d

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.

readBody needs to handle Buffer

2 participants