Created
February 18, 2023 11:47
-
-
Save noway/c169c8befb9e279a1494b79da103b8a0 to your computer and use it in GitHub Desktop.
A script to reproduce fetch's `getReader` streaming bug in bun.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TIMESTAMP=$(date +%s) | |
mkdir "bun-fetch-get-reader-streaming-bug-$TIMESTAMP/" | |
cd "bun-fetch-get-reader-streaming-bug-$TIMESTAMP/" | |
cat <<EOT > package.json | |
{ "dependencies": { "express": "^4.18.2" }, "type": "module" } | |
EOT | |
cat <<EOT > index.js | |
import express from "express"; | |
import { spawn } from "child_process"; | |
const port = 3003; | |
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); | |
const child = spawn("node", ["fetch.js"]); | |
child.stdout.on("data", (data) => { | |
console.log("node: " + data); | |
}); | |
child.stderr.on("data", (data) => { | |
console.error("node: " + data); | |
}); | |
child.on("close", (code) => { | |
const child = spawn("bun", ["run", "fetch.js"]); | |
child.stdout.on("data", (data) => { | |
console.log("bun: " + data); | |
}); | |
child.stderr.on("data", (data) => { | |
console.error("bun: " + data); | |
}); | |
child.on("close", (code) => {}); | |
}); | |
}); | |
EOT | |
cat <<EOT > fetch.js | |
async function fetchStreaming() { | |
const res = await fetch("http://localhost:3003", {}); | |
if (!res.body) { | |
console.log("No body") | |
return | |
} | |
const reader = res.body.getReader(); | |
const decoder = new TextDecoder("utf-8"); | |
while (true) { | |
const { done, value } = await reader.read(); | |
console.log("value", decoder.decode(value)) | |
if (done) { | |
break; | |
} | |
} | |
} | |
fetchStreaming() | |
EOT | |
bun install | |
node index.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment