-
-
Save anonymous/88b5877dcb8983be4aa6 to your computer and use it in GitHub Desktop.
Create stream with on(open) callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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