Benjamin, The readable.end event is not actually a guarantee that the entire operation is completed, only that the "end" signal is being propagated to the writable. It could still take some time for the writable stream to finish writing all the data it's been given.
In v0.10, writable streams all have a "finish" event, which tells you that the writable stream has received the end signal, and all of its data has been flushed. Prior to that, well, you kind of have to know what sort of stream you were dealing with. Most emit "close" at some point, but that's not guaranteed. There's no way to be sure in the general case. (One of many reasons for the streams refactor in 0.10.) If you are using pipe(), there's no need to call resume(). The pipe() call will make the stream flow for you. (Actually, prior to 0.10, readable streams started out in a free-flowing state, and pause() was not guaranteed to actually prevent "data" events.) On Fri, May 3, 2013 at 7:15 AM, Benjamin Pasero <[email protected]> wrote: > So taking your feedback using node 0.8, this is what it has to look like? > > var readS = fs.createReadStream("fileA.txt"); > var writeS = fs.createWriteStream("fileB.txt"); > > readS.on("end", function() { > // Operation done > }); > > readS.on("error", function() { > // Operation error > }); > > writeS.on("error", function() { > // Operation error > }); > > readS.resumse(); > > Right? > > On Friday, May 3, 2013 10:44:39 AM UTC+2, Benjamin Pasero wrote: >> >> Hi, >> >> I am missing a good example for how to adopt stream.pipe for my old >> util.pump code when it comes to replacing a file with the contents of >> another file. As of today, I am using code similar to this: >> >> var readS = fs.createReadStream("fileA.txt"); >> var writeS = fs.createWriteStream("fileB.txt"); >> util.pump(readS, writeS, function(error) >> // Operation done >> }); >> >> Now with stream.pipe I am uncertain if I am using it correctly. I am >> wondering if I have to call resume() or not before piping and I also wonder >> how to register a callback when the operation is fully completed. My attempt >> is this: >> >> var readS = fs.createReadStream("fileA.txt"); >> var writeS = fs.createWriteStream("fileB.txt"); >> readS.pipe(writeS); >> >> readS.on("end", function() { >> // Operation done >> }); >> >> I would really appreciate if someone could tell me if this is the right >> way of doing it. As a related question, I am wondering if I should open the >> file with "w" or "r+". However, I think "w" is correct because I am >> replacing the file contents. >> >> Thanks, >> Ben > > -- > -- > 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 [email protected] > To unsubscribe from this group, send email to > [email protected] > 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 [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > -- -- 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 [email protected] To unsubscribe from this group, send email to [email protected] 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 [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
