refactor(pipe) create

This commit is contained in:
coderaiser 2014-11-01 05:15:28 -04:00
parent a6fd6bda2d
commit ba880af169

View file

@ -27,7 +27,6 @@
*/
function create(read, write, options, callback) {
var gzip,
isFsWrite = write instanceof fs.WriteStream,
isStrRead = type.string(read),
isStrWrite = type.string(write),
isFunc = type.function(options),
@ -48,15 +47,12 @@
start : o.range.start,
end : o.range.end,
});
if (isStrRead)
read = fs.createReadStream(read, optionsRead);
if (isStrWrite) {
if (isStrWrite)
write = fs.createWriteStream(write);
isFsWrite = true;
}
if (o.gzip || o.gunzip) {
if (o.gzip)
@ -68,18 +64,7 @@
read = read.pipe(gzip);
}
on('error', write, callback);
on('error', read, callback);
Util.exec.if(!isFsWrite, function() {
read.pipe(write, {
end: !o.notEnd
});
on('end', read, callback);
}, function(callback) {
on('open', write, callback);
});
pipe([read, write], options, callback);
}
function on(event, emitter, callback) {
@ -111,19 +96,24 @@
});
}
function pipe(streams, callback) {
function pipe(streams, options, callback) {
var main,
read = streams[0];
Util.checkArgs(arguments, ['streams', 'callback']);
if (!callback)
callback = options;
streams.forEach(function(stream) {
on('error', stream, callback);
if (!main)
main = stream;
else
main = main.pipe(stream);
main = main.pipe(stream, {
end: !options.notEnd
});
});
on('end', read, callback);