mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
refactor(pipe) create
This commit is contained in:
parent
a6fd6bda2d
commit
ba880af169
1 changed files with 9 additions and 19 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue