bun
bun copied to clipboard
Support streaming in `fetch`
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().
Yes, fetch doesn’t stream yet. We haven’t implemented this.
@noway did you find any work-around (non-fetch) for this?
@folke no I gave up
@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({}),
],
})