Skip to content

Instantly share code, notes, and snippets.

@noway
Created February 18, 2023 11:00
Show Gist options
  • Save noway/7097e86dc5650a762b156a63e43ff6d7 to your computer and use it in GitHub Desktop.
Save noway/7097e86dc5650a762b156a63e43ff6d7 to your computer and use it in GitHub Desktop.
A script to reproduce streaming response bug in bun that's affecting express.js.
#!/bin/bash
TIMESTAMP=$(date +%s)
mkdir "bun-express-streaming-response-bug-$TIMESTAMP/"
cd "bun-express-streaming-response-bug-$TIMESTAMP/"
cat <<EOT > package.json
{ "dependencies": { "express": "^4.18.2" }, "type": "module" }
EOT
cat <<EOT > index.js
import express from "express";
const port = process.argv[2];
const app = express();
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
app.get("/", async (req, res) => {
res.set({ "transfer-encoding": "chunked" });
res.write("1\n");
await sleep(300);
res.write("2\n");
await sleep(300);
res.write("3\n");
await sleep(300);
res.write("4\n");
await sleep(300);
res.write("5\n");
await sleep(300);
res.end();
});
app.listen(port, () => {
console.log("Example app listening on port " + port);
});
EOT
bun install
bun run index.js 3001 & node index.js 3002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment