oven-sh / bun

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

`ReadableStream` used as `fetch` `body` is `null` in `Bun.serve` #10655

Open TomasHubelbauer opened 5 months ago

TomasHubelbauer commented 5 months ago

What version of Bun is running?

1.1.5

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

https://github.com/TomasHubelbauer/bun-passthrough-server

const textDecoder = new TextDecoder();

const server = Bun.serve({
  async fetch(request) {
    const url = new URL(request.url);
    switch (url.pathname) {
      case "/produce": {
        // @ts-expect-error Bun types?
        return new Response(async function* () {
          while (true) {
            const line = new Date().toISOString();
            yield line + "\n";
            console.log("Produced:", line);
            await Bun.sleep(1000);
          }
        });
      }
      case "/consume": {
        // @ts-expect-error Bun types?
        for await (const chunk of request.body) {
          console.log("Consumed:", textDecoder.decode(chunk));
        }

        return new Response();
      }
    }

    throw new Error("Not Found");
  },
});

const response = await fetch(`http://localhost:${server.port}/produce`);
await fetch(`http://localhost:${server.port}/consume`, {
  method: "POST",
  body: response.body,
});

What is the expected behavior?

For every "Produced" line, there should be a corresponding "Consumed" line.

What do you see instead?

request.body is null in the /consume switch branch.

Additional information

No response

TomasHubelbauer commented 5 months ago

I guess this is a duplicate of #7206? I am not sure. I would expect this to work OOTB, even without duplex but I guess that's not what the fetch standard dictates.

guest271314 commented 3 months ago

duplex: "half" (HTTP/2) is not supported in bun. Yes, this is a duplicate of https://github.com/oven-sh/bun/issues/7206.