bun icon indicating copy to clipboard operation
bun copied to clipboard

Node incompatibility `req.on`

Open 9OP opened this issue 1 year ago • 2 comments

What version of Bun is running?

1.0.2+37edd5a6e389265738e89265bcbdf2999cb81a49

What platform is your computer?

Darwin 22.4.0 x86_64 i386

What steps can reproduce the bug?

Context: I was trying to get raw binary data from a client using fetch API. It seems to work in Node but not in Bun.


Server code:

import Koa from "koa";
import Router from "koa-router";

const app = new Koa({ proxy: true });
const router = new Router();

router.post("/", async (ctx) => {
  const rawBinaryData = [];
  ctx.req.on("data", (chunk) => {
    rawBinaryData.push(chunk);
  });
  ctx.req.on("end", () => {
    const binaryData = Buffer.concat(rawBinaryData);
    ctx.status = 200;
    console.log(binaryData);
  });
});
app.use(router.routes());
app.listen(8080, "127.0.0.1");

client code:

const dataToSend = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
await fetch("http://127.0.0.1:8080/", {
  body: dataToSend,
  met

Response in node:

> $ node server.js
<Buffer 48 65 6c 6c 6f>

Response in bun:

> $ bun server.js

What is the expected behavior?

Get the same result as Node. Get the binary data from the request object.

What do you see instead?

Instead no data binary data is being received.

Additional information

No response

9OP avatar Sep 17 '23 18:09 9OP

I will try to replicate with Hono, it might be a Koa issue.

9OP avatar Sep 18 '23 10:09 9OP

same issue, with nodejs directly:

const http = require('http');

const port = process.env.PORT || 9091

const server = http.createServer({
  insecureHTTPParser: true
}, (req, res) => {
  // Handle requests here
  console.log("req received", req.url);
  req.on("data", (chunk) => {
    console.log('chunk received', chunk);
  });
  req.on("end", (chunk) => {
    console.log('END chunk received', chunk);
  });

        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Hello, HTTP!');
});
server.listen(port, process.env.HOST_IP, () => {
  console.log(`host started on port ${port}`)
});

Curl:

curl -X POST -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2"}' https://your-url

songz avatar May 10 '24 14:05 songz