unjs / h3

⚡️ Minimal H(TTP) framework built for high performance and portability
https://h3.unjs.io/
MIT License
3.69k stars 219 forks source link

Using `readBody` with Nuxt internal routing fails to parse URLSearchParams #887

Closed phoenix-ru closed 1 month ago

phoenix-ru commented 1 month ago

Environment

Reproduction

https://github.com/phoenix-ru/h3-urlsearchparams-reproduction

Describe the bug

Users of @sidebase/nuxt-auth are experiencing the following problem with our module, described here:

ERROR  [nuxt] [request error] [unhandled] [500] The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of URLSearchParams

We managed to pinpoint the problem to this piece of code: https://github.com/sidebase/nuxt-auth/blob/a0704470583c5fea47c26c14b8673f8bb6a39a5f/src/runtime/composables/authjs/useAuth.ts#L120-L133

It works well when being called using HTTP, but fails when calling Nuxt endpoints from within a server. As you know,$fetch calls are translated to function calls in server code.

Upon some investigation, I was able to find the exact culprit - it is the readRawBody method and its Buffer.from return statement: https://github.com/unjs/h3/blob/112fa338ef5bbde6207f3317d2ef51f0e52a59fa/src/utils/body.ts#L93

This is what happens:

I fixed it locally and verified the patch, I will submit a PR shortly.

Additional context

No response

Logs

No response

pi0 commented 1 month ago

fixed on edge and next release thanks.