Tried to copy files using streams and pipe and got funny results. Let's 
consider following code (highlighted version 
https://gist.github.com/3850106 ):

    var copy = function(from, to, cb){  
      var fromStream = fs.createReadStream(from)
      fromStream.on('error', cb)
      var toStream = fs.createWriteStream(to)
      toStream.on('error', cb)
      fromStream.on('end', cb)
    }

    copy('non existing file a', 'non existing dir/file b', function(err){
      console.log(err)
    })

There are errors in both streams (non existing source file and no parent 
directory for destination file) - so, both of them will emit
'error' event, and the callback will be called twice with both errors.

    // Error will be reported twice:
    //
    // { [Error: ENOENT, open 'non existing file a'] errno: 34, 
    //    code: 'ENOENT', path: 'non existing file a' }
    // { [Error: ENOENT, open 'non existing dir/file b'] errno: 34, 
    //    code: 'ENOENT', path: 'non existing dir/file b' }

I found the solution by looking at the source of `util.pump` - it does it 
by wrapping callback into function that calls callback only once - for the 
fist error and ignoring others.

But maybe it would be nice to have a callback for `createXxxStream` or 
something like 'ready' or 'success' events to know that it created 
successfully?

-- 
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

Reply via email to