bun icon indicating copy to clipboard operation
bun copied to clipboard

Pass `Request` object to `Request` constructor doesn't work with `ReadableStream` body

Open bogeychan opened this issue 1 year ago • 1 comments

What version of Bun is running?

1.0.15

What platform is your computer?

No response

What steps can reproduce the bug?

Code

// index.mjs

const body = new ReadableStream({
	start(controller) {
		controller.enqueue(new TextEncoder().encode('yay'))
		controller.close()
	}
})

const request = new Request('http://localhost', {
	body,
	method: 'POST',
	duplex: 'half'
})
const req = new Request(request)

console.log(await req.text())

Run

bun run index.mjs

What is the expected behavior?

Console output

yay

What do you see instead?

Console output


Additional information

The same code works as expected in Node.js (v18.16.0 & v21.4.0), run node index.mjs

This works already in Bun:

const request = new Request('http://localhost', { body: 'yay', method: 'POST' })
const req = new Request(request)

console.log(await req.text()) // "yay"

I noticed that the following code does not work in Bun. Is this intended? (same behavior; should print yay as above)

const server = Bun.serve({
	port: 8080,
	async fetch(request) {
		const req = new Request(request)
		return new Response(await req.text())
	}
})

const res = await fetch('http://localhost:8080', {
	method: 'POST',
	body: 'yay'
})

console.log(await res.text())

server.stop()

bogeychan avatar Dec 06 '23 22:12 bogeychan

I'm being blocked by this. The implementation of effect rpc (https://github.com/Effect-TS/effect) uses ReadableStream to stream the reponse to the client. I'm seeing the same issue, the response body is empty on the client.

evelant avatar Feb 10 '24 00:02 evelant