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
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
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
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
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
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
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).
> --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nodejs+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Oscar Torrente Artero

unread,
Sep 9, 2013, 2:23:57 PM9/9/13
to nod...@googlegroups.com
Ooh, thanks a lot !!! And sorry for this so silly question...

Maybe a explanation like this in online documentation could help to understand these details and could avoid you to repeat these things.

Thanks!!

greelgorke

unread,
Sep 10, 2013, 3:04:51 AM9/10/13
to nod...@googlegroups.com
ah that was my missing puzzle piece
Reply all
Reply to author
Forward
0 new messages