Event "end" in a ReadStream object seems not working if properties "start" and "end" properties are used in the object specified as second parameter in fs.createReadStream()
1,190 views
Skip to first unread message
Oscar Torrente Artero
unread,
Sep 5, 2013, 9:35:57 PM9/5/13
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to nod...@googlegroups.com
I mean...this code works perfectly:
var fs = require("fs") var readable = fs.createReadStream("file.txt") readable.on("end", function () { console.log("End") }) readable.on("error", function (err) { console.log(err) }) readable.on("readable", function () { var chunk = readable.read(); console.log(chunk.toString()) })
But this not:
var fs = require("fs") var readable = fs.createReadStream("file.txt",{start:0, end:1}) readable.on("end", function () { console.log("End") }) readable.on("error", function (err) { console.log(err) }) readable.on("readable", function () { var chunk = readable.read(); console.log(chunk.toString()) })
I see two related problems: -It seems "readable" event is emitted twice (why??!!) -As a consequenca, the second time "readable" event is emitted, chunk is null because there's nothing to read, so method toString() crashes. I thought that just after the first time "readable" event is emitted, then the "end" event would be emitted too, but it doesn't.
I'm sure I am doing something wrong, but I really can't see it.
Thanks a lot
greelgorke
unread,
Sep 6, 2013, 5:29:50 AM9/6/13
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to nod...@googlegroups.com
1. if you check chunk for null before printing, everything is working fine.
readable.on("readable", function () { var chunk = readable.read(); console.log(chunk != null ? chunk.toString() : 'null') })
2. you could read on a single event till null.
readable.on("readable", function () { var chunk; while ( (chunk = readable.read()) != null) console.log(chunk.toString()) })
chunk == null tells you, that the buffer of the stream is empty now and you should wait till the next 'readable' or the 'end'
Oscar Torrente Artero
unread,
Sep 6, 2013, 1:36:18 PM9/6/13
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to nod...@googlegroups.com
Yes, it's true what you say, but I think it a little more tricky it should be.
*Why there are two "readable" events emitted?
*Why "end" event isn't emitted when there aren't more data to read?
*Why is there such an inconsistence between the two codes I've exposed?
Thanks a lot for your patiente.
Oscar Torrente Artero
unread,
Sep 8, 2013, 2:35:30 PM9/8/13
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to nod...@googlegroups.com
Well, I insist. If you try this code:
var fs = require("fs") var readable = fs.createReadStream("file.txt") readable.on("end", function () { console.log("End") })
readable.on("readable", function () { var chunk = readable.read(1); console.log(chunk.toString()) })
you will see "readable" event is emitted twice (!!??) when I think it should be emitted many times as until buffer is drained (namely, many times as bytes are inside "file.txt"), shouldn't??!!
greelgorke
unread,
Sep 9, 2013, 3:38:48 AM9/9/13
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to nod...@googlegroups.com
may be you should place an issue on github?
Isaac Schlueter
unread,
Sep 9, 2013, 1:12:00 PM9/9/13
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to nodejs
Working as designed.
The 'end' event doesn't happen until you read PAST the last chunk. If
there's no data in the buffer, then that read() returns null, and then
'end' is emitted.
The reason for this is to prevent 'end' being emitted before you have
a chance to attach a listener, on zero-length readable streams (ie,
streams that end without *ever* having any data).