Skip to content

Instantly share code, notes, and snippets.

Created October 8, 2012 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/88b5877dcb8983be4aa6 to your computer and use it in GitHub Desktop.
Save anonymous/88b5877dcb8983be4aa6 to your computer and use it in GitHub Desktop.
Create stream with on(open) callback
var fs = require('fs');
var createStream = function(type, path, erred, opened) {
var StreamType = (type === 'read')
? 'createReadStream'
: 'createWriteStream';
if (typeof(opened) !== 'function') {
opened = erred;
};
var stream = fs[StreamType](path);
stream.on('error', erred.bind(stream));
stream.on('open', opened.bind(stream, null, stream));
return stream;
};
var createReadStream = createStream.bind(this, 'read');
var createWriteStream = createStream.bind(this, 'write');
var copy = function(from, to, cb){
createReadStream(from, cb, function(err, fromStream) {
createWriteStream(to, cb, function(err, toStream) {
fromStream.pipe(toStream);
fromStream.on('end', cb);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment