Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent readRawBody behavior in some nitro presets #912

Closed
Twitch0125 opened this issue Feb 6, 2023 · 6 comments
Closed

Inconsistent readRawBody behavior in some nitro presets #912

Twitch0125 opened this issue Feb 6, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@Twitch0125
Copy link

Twitch0125 commented Feb 6, 2023

Environment

nitropack: 2.1.1
node: 18.12.1
deno: 1.30.2

Reproduction

https://github.com/Twitch0125/nitro-inconsistent
(stackblitz works for node-server and cloudflare workers https://stackblitz.com/edit/nitro-inconsistent?file=readme.md)

  1. pnpm install
  2. pnpm run:node-server or pnpm run:deno or pnpm run:cloudflare

Describe the bug

It seems that some of the utils behave differently if they're in different environments. I'm not sure if this is actually an issue in Nitro, Unenv, or H3. Creating it for Nitro because it has the most stars so this might be seen by more people here.

When I try to get the raw body with getRawBody I'm seeing different return values if its running in Deno or Cloudflare Worker presets.

Given a handler that looks like this

export default eventHandler(async (event) => {
  const body = await readRawBody(event);
  return { body, type: typeof body };
});

I can send it this json payload

{ "test": "testing json" }

and I'll get 3 different responses

node:

{"body":"{\"test\":\"testing json\"}","type":"string"}

deno:

{"body":"blob:null/a9f116d7-5f24-4f78-a50f-aa6432394633","type":"string"}

cloudflare:

{"body":{"type":"Buffer","data":[123,34,116,101,115,116,34,58,34,116,101,115,116,105,110,103,32,106,115,111,110,34,125]},"type":"object"}

Additional context

Looking into the code a bit, it looks like readRawBody() is checking a event.node.req.body property and then returns whatever that is. I'd guess the issue is in whatever codebase is setting that body property.

Logs

No response

Copy link
Collaborator

atinux commented Mar 19, 2023

Could you confirm with latest version of Nitro @Twitch0125 ? 🙏

johannschopplich added a commit to johannschopplich/nuxt-api-party that referenced this issue Mar 19, 2023
@Twitch0125
Copy link
Author

Twitch0125 commented Mar 19, 2023

Updated to nitro 2.3.1 and I am still seeing this issue. The reproduction repo and stackblitz link were also updated

Copy link
Collaborator

atinux commented Mar 21, 2023

Seem related directly to h3: unjs/h3#363

@pi0
Copy link
Member

pi0 commented Mar 22, 2023

unjs/h3#363 will support Buffer for readBody but it might be safer option to choose.

The core of different types backs to the fact that raw input of providers is different (even for node for direct calls vs user fetch). While we might support to Buffer normalization more it adds more overhead.

@martinsotirov
Copy link

@pi0 Any ETA on this getting merged?

@pi0
Copy link
Member

pi0 commented Mar 28, 2023

This should be fixed via unjs/h3#366 and unjs/h3@19d133d

Please upgrade lockfile and ping me to reopen if either readBody or readRawBody (with second param encoding set to false) are not returning a Buffer type.

Thanks for reporting the issue ❤️

@pi0 pi0 closed this as completed Mar 28, 2023
johannschopplich added a commit to johannschopplich/nuxt-kql that referenced this issue May 23, 2023
johannschopplich added a commit to johannschopplich/nuxt-api-party that referenced this issue May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants