oven-sh / bun

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

Sveltekit (using vite): cannot read body #6638

Open Paultje52 opened 11 months ago

Paultje52 commented 11 months ago

What version of Bun is running?

1.0.5 - 1.0.21

What platform is your computer?

Linux 5.4.0-156-generic x86_64 x86_64

What steps can reproduce the bug?

I have a sveltekit project (using vite). I'm running it using bun --bun run dev. Here is a zip file that reproduces the issue: sveltekit-bun-body-issue.zip

When using --bun to force vite to use bun's runtime, the promise of request.json() or request.text() isn't resolved.

export const POST = async (event) => {
  console.log(1);
  const body = await event.request.text();
  console.log(2);
  console.log("request body: ", body);
  // ... the rest is the same as before

  return new Response(
    JSON.stringify({
      success: true
    })
  );
};

I see the 1 in the console, but no the 2. Omitting --bun does work.

What is the expected behavior?

To read the body and resolve the promise

What do you see instead?

The promise doesn't get resolved, coursing the request to eventually time out. I don't know if the body is read successfully.

Additional information

When building and then running the project, the body is read successfully. It's just the vite dev that doesn't work.

fiffty commented 9 months ago

I am seeing the same thing. Tried await request.json() and await Bun.readableStreamToJSON(request.body as ReadableStream) which all behaved the same way. A few more things I've noticed:

kncfreight commented 9 months ago

Has there been any update on this topic? I have experienced this for the first time yesterday. Sadly I can't use all the nice Bun features if a POST request/response doesn't work with it.

Thank you for an update on this matter. :)

Paultje52 commented 9 months ago

I did some more investigating. The same result @fiffty describes also happens with request.body.getReader. When using the following code, the request hangs for the first time.

  const reader = event.request.body.getReader();
  const chunks = [];

  while(true) {
    const { value, done } = await reader.read();
    console.log({ value, done });

    if (done) {
      break;
    }

    chunks.push(...value);
  }

  console.log("request body: ", Buffer.from(chunks).toString());

There is just one console log.

{
  value: Buffer(26) [ 123, 13, 10, 32, 32, 32, 32, 34, 104, 101, 108, 108, 111, 34, 58, 32, 34, 119, 111, 114, 108, 100, 34, 13, 10, 125 ],
  done: false,
}

However, the second time the request does go through. This is the log of the second time.

{
  value: Buffer(26) [ 123, 13, 10, 32, 32, 32, 32, 34, 104, 101, 108, 108, 111, 34, 58, 32, 34, 119, 111, 114, 108, 100, 34, 13, 10, 125 ],
  done: false,
}
{
  value: undefined,
  done: true,
}
request body:  {
    "hello": "world"
}

That means that there is something wrong in Bun's ReadableStreamDefaultReader. Looking into the v1.0.4-v1.0.5 changes, I saw that 6301778 changed a couple of things regarding streams. I don't know if this is the reason why ReadableStreamReaders are broken, but maybe this is a helpfull nudge in the right direction @Electroid?

Temporary workarond ### Temporary workaround From my testing, this is a temporary workaround to still read the body. It's probably a really bad workaround that doesn't deal with things like compression, but it's at least something. ```ts export const POST = async (event) => { const reader = event.request.body.getReader(); const contentLength = Number(event.request.headers.get("content-length")); const chunks = []; while (true) { const { value, done } = await reader.read(); if (value) chunks.push(...value); if (done || chunks.length >= contentLength) break; } console.log("request body: ", Buffer.from(chunks).toString()); return new Response( JSON.stringify({ success: true }) ); }; ```
kncfreight commented 9 months ago

The new 17 update seems to have resolved this issue. It is now working fine for me.

kncfreight commented 9 months ago

Okay, somehow it is still not fixed. Yesterday evening everything worked fine without any workarounds. Now I am logging in to the same setup and it doesnt work anymore. Very strange.

cfouche3005 commented 9 months ago

Same error for me but with more details:

It seem that bun doesn't receive or process all of the request and give these error (this is just a guess I am not a expert)

cfouche3005 commented 9 months ago

Same issue with vite build with @sveltejs/adapter-node but this time I run bun ./build/index.js : const body = await request.json(); hang indefinitely console.log(request) give me that :

Request (0 KB) {
  method: "POST",
  url: "https://test.REDACTED/hooks/release",
  headers: Headers {
    "host": "test.REDACTED",
    "user-agent": "newreleases/1.18.1-be681b9",
    "content-length": "198",
    "accept-encoding": "gzip",
    "authorization": "Basic REDACTED",
    "content-type": "application/json; charset=utf-8",
    "x-forwarded-for": "REDACTED",
    "x-forwarded-host": "test.REDACTED",
    "x-forwarded-proto": "https",
    "x-newreleases-signature": "9221223e4fac56b7f1337aa8ba726f5a1a119073fc45108ebf108bc818dcf518",
    "x-newreleases-timestamp": "1703154047",
  }
  ReadableStream {
    locked: [Getter],
    cancel: [Function: cancel],
    getReader: [Function: getReader],
    pipeTo: [Function: pipeTo],
    pipeThrough: [Function: pipeThrough],
    tee: [Function: tee],
    values: [Function: values],
    [Symbol(Symbol.asyncIterator)]: [Function: lazyAsyncIterator],
  }
}
524c commented 9 months ago

Same problem. request.json() does not resolve it. I created a simple project to reproduce the problem, but I couldn't. It only occurs in my real project.

With the zip provided here I can't reproduce it either.

524c commented 9 months ago

In my case the problem occurs with macOS arm64:

  System:
    OS: macOS 14.2
    CPU: (10) arm64 Apple M2 Pro
    Memory: 120.92 MB / 32.00 GB
    Shell: 5.9 - /usr/local/bin/zsh
  Binaries:
    Node: 18.19.0 - ~/n/bin/node
    Yarn: 4.0.2 - ~/n/bin/yarn
    npm: 10.2.3 - ~/n/bin/npm
    bun: 1.0.18 - ~/.bun/bin/bun
  Browsers:
    Chrome: 120.0.6099.129
    Edge: 120.0.2210.89
    Safari: 17.2
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.1 => 3.0.1
    @sveltejs/adapter-node: ^2.0.1 => 2.0.1
    @sveltejs/kit: ^2.0.4 => 2.0.4
    @sveltejs/vite-plugin-svelte: ^3.0.1 => 3.0.1
    svelte: ^4.2.8 => 4.2.8
    vite: ^5.0.10 => 5.0.10
kncfreight commented 8 months ago

Has there been any progress on this topic? :)