-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
fs.ReadStream does not emit data events #12099
Comments
createReadStream seems to work because it gets to the "on open" log, i would like to see further debugging of the data from the createReadStream to see what data it is getting. If you aren't using the node fs functions mkdir and readdir then you should use buns api file I/O (https://bun.sh/docs/api/file-io) |
I tried using // const inp = Bun.file("/dev/cu.usbmodem1201")
const inp = Bun.file("/dev/random")
const stream = await inp.stream()
console.log(`chunk size is ${inp.size}`) // chunk size is Infinity
let chunkCount = 0
for await (const chunk of stream) {
console.log(chunk.length); // 262144
if (++chunkCount > 3) {
break
}
} |
does this still reproduce for you on Bun 1.1.27? (you may need to run |
Yes, this still reproduces on Bun 1.1.27. The |
Here's a reproducer using a fifo. // fifo-writer.mjs
import * as fs from "node:fs"
const outp = fs.createWriteStream("my-fifo")
let i = 1
setInterval(() => {
outp.write(`Message ${i++}\n`)
}, 10) // fifo-reader.mjs
import * as fs from "node:fs"
const inp = fs.createReadStream("my-fifo")
let chunkCount = 1
inp.on("data", function (chunk) {
console.log(`${chunkCount++}: ${chunk.toString().trim()}`)
if (chunkCount > 5) {
console.log("Closing file now due to chunk count...")
inp.close()
}
})
inp.on("open", function () {
console.log("on open")
})
inp.on("close", function () {
console.log("on close")
}) Running the writer in the background, try the reader with node (twice) and with bun: $ mkfifo my-fifo
$ bun fifo-writer.mjs &
[1] 50572
$ node fifo-reader.mjs
on open
1: Message 1
2: Message 2
3: Message 3
4: Message 4
5: Message 5
Closing file now due to chunk count...
on close
$ node fifo-reader.mjs
on open
1: Message 198
2: Message 199
3: Message 200
4: Message 201
5: Message 202
Closing file now due to chunk count...
on close
$ bun fifo-reader.mjs # hangs, interrupt with Ctrl-C
on open
^C |
What version of Bun is running?
1.1.16
What platform is your computer?
Darwin 23.5.0 arm64 arm
What steps can reproduce the bug?
A Raspberry Pi Pico is connected to a Mac USB port. The Pico shows up as a character special device at
/dev/cu.usbmodem1201
and sends a 29-byte line every 250ms or so, which I would like to read with Bun.With the script
read-test.js
below, the behavior of Bun is different from the behavior of Node.What is the expected behavior?
With Node.js v20.11.1, several data events are received before exiting. The device writes 29-character lines about 250ms apart.
What do you see instead?
Bun runs without reporting an error, but no data events are emitted.
Additional information
The Pi Pico streams data from a sensor. My goal here is to read each line from the USB-attached Pi Pico and process the line immediately. Eventually, the script needs to run on Windows, Mac, and Linux.
Changing the script to read from the Mac's
/dev/random
, Bun has some data events, but the behavior is still different from Node.js. Bun evidently uses a larger read buffer. Is there a way to set a read timeout for Bun, so that it does not wait for the read buffer to fill up before emitting a data event?The text was updated successfully, but these errors were encountered: