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

Requests with Transfer-Encoding: chunked seems to be broken in Bun but not in Node #11621

Closed
shadone opened this issue Jun 5, 2024 · 5 comments · Fixed by #15579
Closed

Requests with Transfer-Encoding: chunked seems to be broken in Bun but not in Node #11621

shadone opened this issue Jun 5, 2024 · 5 comments · Fixed by #15579
Labels
bug Something isn't working needs triage

Comments

@shadone
Copy link

shadone commented Jun 5, 2024

What version of Bun is running?

1.1.12

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

  1. create index.js
const express = require('express')
const bodyParser = require('body-parser')
const multer = require('multer')

const app = express()
app.use(bodyParser.json())

const upload = multer({ storage: multer.memoryStorage() })

function handleRegistrations(req, res, next) {
	const product_id = req.body.product_id
	console.log(`### handleRegistrations: product_id=${product_id}`)
	res.status(200).json({ product_id })
}

app.post(
  "/registrations",
  upload.single("image"),
  handleRegistrations,
)

app.post("*", (req, res, next) => {
	console.log("Unhandled endpoint", req)
	res.status(404).send()
})

console.log("Listening on port 8080")
app.listen(8080)
  1. run the server with bun ./index.js

  2. make the request with Transfer-Encoding: chunked like this: curl -XPOST http://localhost:8080/registrations -F product_id=1234foobar567 -H 'Transfer-Encoding: chunked'

The server spits the following error:

Error: Unexpected end of form
    at _final (/Users/denis/tmp/node-multipart-form-data/node_modules/busboy/lib/types/multipart.js:588:13)
    at callFinal (node:stream:2778:23)
    at prefinish (node:stream:2801:23)
    at finishMaybe (node:stream:2808:25)
    at <anonymous> (node:stream:2741:76)
    at onend (node:stream:2088:21)
    at emit (native)
    at endReadableNT (node:stream:2396:27)
    at processTicksAndRejections (:12:39)

What is the expected behavior?

Run the same server with node instead of bun, no errors occur.

What do you see instead?

No response

Additional information

No response

@shadone shadone added bug Something isn't working needs triage labels Jun 5, 2024
@shadone
Copy link
Author

shadone commented Jun 5, 2024

I believe this could be the same or related issue as the following:
#4139
#9100
#4234
#8638

@shadone shadone changed the title Requests with Transfer-Encoding: chunked seems to be broken in Bun but not in Node1 Requests with Transfer-Encoding: chunked seems to be broken in Bun but not in Node Jun 5, 2024
@guest271314

This comment was marked as off-topic.

@shadone
Copy link
Author

shadone commented Jun 6, 2024

What happens when Bun.listen() is used to handle Transfer-Encoding: chunked?

I am not sure what you would like me to test with Bun.listen() - that seems to give raw tcp socket data which I wouldn't know how to read from Bun/TS code

However I tried Bun.serve() instead, maybe that will be helpful?

async function streamToString(stream) {
  const chunks = [];

  for await (const chunk of stream) {
    chunks.push(Buffer.from(chunk));
  }

  return Buffer.concat(chunks).toString("utf-8");
}

Bun.serve({
  port: 8080,
  async fetch(req) {
    console.log("req:")
    console.log(req);

    const result = await streamToString(req.body)
    console.log("body:")
    console.log(result);

    return new Response("Bun!");
  },
});

I sent this request with curl: curl -XPOST http://localhost:8080/registrations -F product_id=1234foobar567 -H 'Transfer-Encoding: chunked'

server logs:

$ bun ./index.js
req:
Request (0 KB) {
  method: "POST",
  url: "http://localhost:8080/registrations",
  headers: Headers {
    "host": "localhost:8080",
    "user-agent": "curl/8.6.0",
    "accept": "*/*",
    "transfer-encoding": "chunked",
    "content-type": "multipart/form-data; boundary=------------------------VA4meCsV7czVKCTz7jQ3NZ",
  }
}
body:
--------------------------VA4meCsV7czVKCTz7jQ3NZ
Content-Disposition: form-data; name="product_id"

1234foobar567
--------------------------VA4meCsV7czVKCTz7jQ3NZ--

@guest271314

This comment was marked as off-topic.

@guest271314

This comment was marked as off-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 triage
Projects
None yet
2 participants