bun icon indicating copy to clipboard operation
bun copied to clipboard

Support streaming in `fetch`

Open noway opened this issue 2 years ago • 1 comments

What version of Bun is running?

0.5.6

What platform is your computer?

Darwin 22.2.0 arm64 arm

What steps can reproduce the bug?

Run repro script:

curl https://gist.githubusercontent.com/noway/c169c8befb9e279a1494b79da103b8a0/raw/b90b626f4afbc3c978a2342844ebe7189e8104c8/bun-fetch-get-reader-streaming-bug.sh | bash

Alternatively, go to https://gist.github.com/noway/c169c8befb9e279a1494b79da103b8a0 and copy-paste the script into Terminal.

What is the expected behavior?

I expect this output:

node: value 1


node: value 2


node: value 3


node: value 4


node: value 5


node: value

bun: value 1


bun: value 2


bun: value 3


bun: value 4


bun: value 5


bun: value

What do you see instead?

I see this output instead:

node: value 1


node: value 2


node: value 3


node: value 4


node: value 5


node: value

bun: value 1
2
3
4
5


bun: value

Additional information

It seems like bun's fetch's getReader doesn't support streaming response from a web server. Bun's fetch simply waits for the whole response before returning it in await reader.read().

noway avatar Feb 18 '23 11:02 noway

Yes, fetch doesn’t stream yet. We haven’t implemented this.

Jarred-Sumner avatar Feb 18 '23 11:02 Jarred-Sumner

@noway did you find any work-around (non-fetch) for this?

folke avatar Mar 04 '23 17:03 folke

@folke no I gave up

noway avatar Mar 06 '23 02:03 noway

@noway did you find any work-around (non-fetch) for this?

My workaround is to use curl:

Bun.spawn({
    stdout: 'pipe',
    cmd: [
      'curl',
      '--silent',
      '--no-buffer',
      '--request',
      'POST',
      '--url',
      url,
      '--header',
      "'Content-Type: application/json'",
      '--data',
      JSON.stringify({}),
    ],
  })

patlux avatar Aug 11 '23 16:08 patlux

Fixed in Bun v0.8.0! 🎉

image

bun.sh/blog/bun-v0.8.0

noway avatar Aug 24 '23 16:08 noway