oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.87k stars 2.74k forks source link

Express post request with transfer-encoding chuncked has no body #8638

Open co-sic opened 8 months ago

co-sic commented 8 months ago

What version of Bun is running?

1.0.25, 1.1.8

What platform is your computer?

Linux 6.5.0-15-generic x86_64 x86_64

What steps can reproduce the bug?

I'm running express and apollo-server (latest versions) and i am having the problem that the body of an incomming post request is empty in certain situations. I narrowed it down to the "transfer-encoding": "chuncked" header beeing present and/or the 'content-lenght' header beeing not present. If tried the same failing request and send it through a proxy first (ngrok) and then it worked. You can see the diff of the requests here https://www.diffchecker.com/0AdUpa8R/ (Left side is failing, right side is working).

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

Electroid commented 8 months ago

Thanks for reporting, mind sharing some code to reproduce this issue?

co-sic commented 8 months ago

Hey @Electroid i added a example here to reproduce this: https://github.com/co-sic/bun-playground/tree/cosmo-bun-apollo-server

ambergristle commented 8 months ago

Hi! I've potentially run into a similar issue.

I use Docker to build a Next.js (14.0.3) app running on Bun (oven/bun:1.0-slim). I use App Router to GET some data from a third party (Marketstack), and return it to the client.

The third party service responds with chunk-encoded data. When deployed (on Railway, via Docker) large (e.g., ~400 rows) responses fail on json parse (SyntaxError: Unexpected end of JSON). Small (~30 rows) responses work ok both locally and in deployment. Locally (via Docker), the app mostly has no issue; I've been able to repro a couple times, but I'm not sure how. Deploying with Node instead of Bun seems to work regardless of response size.

I added a log of the response before json parse, and those (larger) responses that fail log a size of 0kb, while responses that are (ultimately) parsed ok have a non-zero size.

Source code from the jshttp lib seems to make its way into the error logs (if left uncaught).

I'm wondering whether this has anything to do with:

Here's a permalink to the failing point in my repo. I'm currently working on a more contained/easily-reproducible example.

ambergristle commented 8 months ago

Locally (via Docker), the app mostly has no issue; I've been able to repro a couple times, but I'm not sure how. Deploying with Node instead of Bun seems to work regardless of response size.

I just discovered that using bun --bun run dev locally is breaking. Works fine (locally) with bun dev and bun run dev.

  const response = await fetch(url)
    .then(async (res) => {
      if (res.ok) {
        return res.json()
      }
    })

// results in:

 ⨯ SyntaxError: Unexpected end of JSON input
    at json (native)
    at <anonymous> (:20:28)
    at <anonymous> (:18:55)
    at processTicksAndRejections (:61:39)
 ⨯ SyntaxError: Unexpected end of JSON input
    at json (native)
    at <anonymous> (:20:28)
    at <anonymous> (:18:55)
    at processTicksAndRejections (:61:39)
digest: "1774337577"
co-sic commented 8 months ago

Hey @Electroid any chance on getting a fix (or even workaround) for this soon? Sorry for the ping, but this is currently blocking and i need to consider to maybe move my bun production service back to node again if it will take a longer time to fix.

co-sic commented 5 months ago

I just check and the issue is still there with bun v1.1.8

RobinVanCauter commented 4 months ago

We've seen this issue as well while building an express backend using bun as the runtime for an existing client (so we can't change the way it's performing its requests).

So def. still an issue with Bun v1.1.12

When ran through node it works fine.

vitalyavolyn commented 2 weeks ago

Still an issue in bun 1.1.29 :( I have to use node for now

vitalyavolyn commented 1 week ago

Here's a tiny repro repo: https://github.com/vitalyavolyn/bun-chunked-repro

Jarred-Sumner commented 1 week ago

I believe this PR will fix it - #14384, but it's not going to be easy to merge