Skip to content

fix(cloudflare): pass raw body instead of parsing it - #629

Merged
pi0 merged 1 commit into
nitrojs:mainfrom
brendonmatos:main
Nov 7, 2022
Merged

fix(cloudflare): pass raw body instead of parsing it#629
pi0 merged 1 commit into
nitrojs:mainfrom
brendonmatos:main

Conversation

@brendonmatos

Copy link
Copy Markdown
Contributor

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

In cases where it is necessary to read the body of the raw request, the cloudflare deploy was always parsing the body even using useRawBody.
Implementations like the stripe's webhook that needs to read the raw body would break.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@pi0

pi0 commented Nov 3, 2022

Copy link
Copy Markdown
Member

Hi @brendonmatos. Thanks for PR. Would you please provide a reproduction or a better example? We might fix the useRequestBody utility instead as it respects the mime type to support blob and formdata as well (.txt() only works for text and json)

@brendonmatos

brendonmatos commented Nov 3, 2022

Copy link
Copy Markdown
Contributor Author

In my case the useRequestBody was doing its job correctly.

The useRawBody returns the parsed body instead of raw when the localCall is invoked with the parsed body (it wasn't clear to me how or why this happens).
https://github.com/unjs/nitro/blob/9a799c921e3ed4db3245df186599062757d5f058/src/runtime/entries/cloudflare.ts#L25-L34

To support blobs and formdata an option can be to pass the buffer forward using cloudflare's event.request.arrayBuffer() and converting to buffer, like this:

body = Buffer.from(await event.request.arrayBuffer())

@pi0 What do you think about this?

@pi0

pi0 commented Nov 3, 2022

Copy link
Copy Markdown
Member

Buffer for workers is supported via lots of unenv polyfills. If possible, it would be better to use arrayBuffer directly and handle it from h3 utility. (We already depend on Buffer.concat here)

Seems a good idea!

@brendonmatos

Copy link
Copy Markdown
Contributor Author

thanks for the path you gave me!
the behaviour was implemented in both entries

@barisbora

Copy link
Copy Markdown

@pi0

pi0 commented Nov 7, 2022

Copy link
Copy Markdown
Member

Thanks!

@pi0

pi0 commented Nov 7, 2022

Copy link
Copy Markdown
Member

Can we pass CF records too?

Another enhancement, but sure we shall pass them to event.context.cf

@pi0 pi0 changed the title fix(cloudflare): raw body instead parsing (unjs#589) fix(cloudflare): pass raw body instead of parsing it Nov 7, 2022
@pi0
pi0 merged commit 837f894 into nitrojs:main Nov 7, 2022
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.

3 participants