From a6fd6bda2db39a8915b59fb020b20e7bafc37338 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Sat, 1 Nov 2014 05:05:38 -0400 Subject: [PATCH] feature(pipe) add all --- lib/server/packer.js | 33 +++++++++++++++++++++++++-------- lib/server/pipe.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/lib/server/packer.js b/lib/server/packer.js index d003d95b..8eb1c8a2 100644 --- a/lib/server/packer.js +++ b/lib/server/packer.js @@ -6,6 +6,7 @@ path = require('path'), fs = require('fs'), + zlib = require('zlib'), tryRequire = require(DIR + 'tryRequire'), @@ -17,7 +18,16 @@ exports.pack = function(from, to, callback) { isDir(from, function(is) { - var stream, dir, name, + var dir, name, + + optionsDir = { path: from, type: 'Directory' }, + optionsTar = { noProprietary: true }, + + streamDir, + streamTar, + streamZip = zlib.createGzip(), + streamFile, + isStr = Util.type.string(to), options = { gzip: true @@ -26,18 +36,25 @@ if (!is || !fstream || !tar) { pipe(from, to, options, callback); } else { - stream = fstream - .Reader({ path: from, type: 'Directory' }) - .on('error', callback) - .pipe(tar.Pack({ noProprietary: true })); + streamDir = fstream.Reader(optionsDir); + streamTar = tar.Pack(optionsTar); - if (isStr) { + if (!isStr) { + streamFile = to; + } else { dir = path.dirname(to); - name = path.basename(to); + name = path.basename(to, '.gz'); to = dir + path.sep + name + '.tar.gz'; + + streamFile = fs.createWriteStream(to); } - pipe(stream, to, options, callback); + pipe.all([ + streamDir, + streamTar, + streamZip, + streamFile + ], callback); } }); }; diff --git a/lib/server/pipe.js b/lib/server/pipe.js index 8a60ae61..61f9f2af 100644 --- a/lib/server/pipe.js +++ b/lib/server/pipe.js @@ -8,7 +8,8 @@ type = Util.type; module.exports = create; - module.exports.getBody = getBody; + module.exports.getBody = getBody; + module.exports.all = all; /** * create pipe @@ -94,6 +95,40 @@ emitter.on(event, callback); } + function all(streams, callback) { + var n, write, isFSWrite; + + Util.checkArgs(arguments, ['streams', 'callback']); + + n = streams.length - 1; + write = streams[n]; + isFSWrite = write instanceof fs.WriteStream; + + Util.exec.if(!isFSWrite, function() { + pipe(streams, callback); + }, function(callback) { + write.on('open', callback); + }); + } + + function pipe(streams, callback) { + var main, + read = streams[0]; + + Util.checkArgs(arguments, ['streams', 'callback']); + + streams.forEach(function(stream) { + on('error', stream, callback); + + if (!main) + main = stream; + else + main = main.pipe(stream); + }); + + on('end', read, callback); + } + /** * get body of readStream *