Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sveltekit (using vite): cannot read body #6638

Open
Paultje52 opened this issue Oct 21, 2023 · 10 comments
Open

Sveltekit (using vite): cannot read body #6638

Paultje52 opened this issue Oct 21, 2023 · 10 comments
Labels
bug Something isn't working needs investigate Needs to be investigated to find the root cause

Comments

@Paultje52
Copy link

Paultje52 commented Oct 21, 2023

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.

@Paultje52 Paultje52 added the bug Something isn't working label Oct 21, 2023
@Electroid Electroid added the needs investigate Needs to be investigated to find the root cause label Oct 30, 2023
@fiffty
Copy link

fiffty commented Nov 30, 2023

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:

  • Calling fetch() from the browser never worked and always hangs
  • Calling fetch() from node randomly sometimes works, but mostly hangs
  • Running curl -X POST from local bash showed a more reliable pattern where the first time it would hang, but subsequent requests would all succeed.

@kncfreight
Copy link

kncfreight commented Dec 7, 2023

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
Copy link
Author

Paultje52 commented Dec 10, 2023

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.

  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
Copy link

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

@kncfreight
Copy link

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
Copy link

Same error for me but with more details:

  • when I do bun --bun run dev :
    const body = await request.json(); hang indefinitely
    console.log(request) give me that :
Request (0 KB) {
  method: "POST",
  url: "http://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": "95f4b791d7001a70305297e09d7a9606d4538d23e09530477a73cd1e32b342e7",
    "x-newreleases-timestamp": "1703090236",
  }
  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],
  }
}
  • When I run bun --bun run vite build with @catdadcode/svelte-adapter-bun and then bun ./build/index.js :
    const body = await request.json(); crash immediately with :
    image
    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": "199",
    "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": "1834d76ac4d9b861a8547a710f70e3edbfeb972c19b6588ffe6c1692479a0d90",
    "x-newreleases-timestamp": "1703092199",
  }
}
  • when I do npm run dev :
    const body = await request.json(); work perfectly
    console.log(request) give me that :
Request {
  [Symbol(realm)]: { settingsObject: { baseUrl: undefined } },
  [Symbol(state)]: {
    method: 'POST',
    localURLsOnly: false,
    unsafeRequest: false,
    body: { stream: undefined, source: null, length: null },
    client: { baseUrl: undefined },
    reservedClient: null,
    replacesClientId: '',
    window: 'client',
    keepalive: false,
    serviceWorkers: 'all',
    initiator: '',
    destination: '',
    priority: null,
    origin: 'client',
    policyContainer: 'client',
    referrer: 'client',
    referrerPolicy: '',
    mode: 'cors',
    useCORSPreflightFlag: true,
    credentials: 'same-origin',
    useCredentials: false,
    cache: 'default',
    redirect: 'follow',
    integrity: '',
    cryptoGraphicsNonceMetadata: '',
    parserMetadata: '',
    reloadNavigation: false,
    historyNavigation: false,
    userActivation: false,
    taintedOrigin: false,
    redirectCount: 0,
    responseTainting: 'basic',
    preventNoCacheCacheControlHeaderModification: false,
    done: false,
    timingAllowFailed: false,
    headersList: HeadersList {
      cookies: null,
      [Symbol(headers map)]: [Map],
      [Symbol(headers map sorted)]: null
    },
    urlList: [ [URL] ],
    url: URL {
      href: 'http://test.REDACTED/hooks/release',
      origin: 'http://test.REDACTED',
      protocol: 'http:',
      username: '',
      password: '',
      host: 'test.REDACTED',
      hostname: 'test.REDACTED',
      port: '',
      pathname: '/hooks/release',
      search: '',
      searchParams: URLSearchParams {},
      hash: ''
    }
  },
  [Symbol(signal)]: AbortSignal { aborted: false },
  [Symbol(headers)]: HeadersList {
    cookies: null,
    [Symbol(headers map)]: Map(11) {
      'host' => [Object],
      'user-agent' => [Object],
      'content-length' => [Object],
      'accept-encoding' => [Object],
      'authorization' => [Object],
      'content-type' => [Object],
      'x-forwarded-for' => [Object],
      'x-forwarded-host' => [Object],
      'x-forwarded-proto' => [Object],
      'x-newreleases-signature' => [Object],
      'x-newreleases-timestamp' => [Object]
    },
    [Symbol(headers map sorted)]: null
  }
}
  • When I run vite build with @sveltejs/adapter-node and then node ./build/index.js :
    const body = await request.json(); work perfectly
    console.log(request) give me that :
Request {
  [Symbol(realm)]: { settingsObject: { baseUrl: undefined } },
  [Symbol(state)]: {
    method: 'POST',
    localURLsOnly: false,
    unsafeRequest: false,
    body: { stream: undefined, source: null, length: null },
    client: { baseUrl: undefined },
    reservedClient: null,
    replacesClientId: '',
    window: 'client',
    keepalive: false,
    serviceWorkers: 'all',
    initiator: '',
    destination: '',
    priority: null,
    origin: 'client',
    policyContainer: 'client',
    referrer: 'client',
    referrerPolicy: '',
    mode: 'cors',
    useCORSPreflightFlag: true,
    credentials: 'same-origin',
    useCredentials: false,
    cache: 'default',
    redirect: 'follow',
    integrity: '',
    cryptoGraphicsNonceMetadata: '',
    parserMetadata: '',
    reloadNavigation: false,
    historyNavigation: false,
    userActivation: false,
    taintedOrigin: false,
    redirectCount: 0,
    responseTainting: 'basic',
    preventNoCacheCacheControlHeaderModification: false,
    done: false,
    timingAllowFailed: false,
    headersList: HeadersList {
      cookies: null,
      [Symbol(headers map)]: [Map],
      [Symbol(headers map sorted)]: null
    },
    urlList: [ [URL] ],
    url: URL {
      href: 'https://test.REDACTED/hooks/release',
      origin: 'https://test.REDACTED',
      protocol: 'https:',
      username: '',
      password: '',
      host: 'test.REDACTED',
      hostname: 'test.REDACTED',
      port: '',
      pathname: '/hooks/release',
      search: '',
      searchParams: URLSearchParams {},
      hash: ''
    }
  },
  [Symbol(signal)]: AbortSignal { aborted: false },
  [Symbol(headers)]: HeadersList {
    cookies: null,
    [Symbol(headers map)]: Map(11) {
      'host' => [Object],
      'user-agent' => [Object],
      'content-length' => [Object],
      'accept-encoding' => [Object],
      'authorization' => [Object],
      'content-type' => [Object],
      'x-forwarded-for' => [Object],
      'x-forwarded-host' => [Object],
      'x-forwarded-proto' => [Object],
      'x-newreleases-signature' => [Object],
      'x-newreleases-timestamp' => [Object]
    },
    [Symbol(headers map sorted)]: null
  }
}

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
Copy link

cfouche3005 commented Dec 21, 2023

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
Copy link

524c commented Dec 23, 2023

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
Copy link

524c commented Dec 23, 2023

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
Copy link

Has there been any progress on this topic? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs investigate Needs to be investigated to find the root cause
Projects
None yet
Development

No branches or pull requests

6 participants