var stream = fs.createReadStream(from)
.pipe(fs.createWriteStream(to))
stream.on('error', cb)
stream.on('end', cb)
}
Take a look at the source for Stream.pipe and you'll see what I'm talking about; It wraps the 'error' event callback and emits only once, then does its own cleanup (you'll have to call destroy yourself, although I think maybe pipe should do this in its cleanup...).
source.on('error', onerror);
dest.on('error', onerror);
// remove all the event listeners that were added.
function cleanup() {
source.removeListener('data', ondata);
dest.removeListener('drain', ondrain);
source.removeListener('end', onend);
source.removeListener('close', onclose);
source.removeListener('error', onerror);
dest.removeListener('error', onerror);
source.removeListener('end', cleanup);
source.removeListener('close', cleanup);
dest.removeListener('end', cleanup);
dest.removeListener('close', cleanup);
}
Cheers,
Adam Crabtree