|
1 | | -const through = require('through2').obj, |
2 | | - unzip = require('unzipper'), |
3 | | - Vinyl = require('vinyl'); |
| 1 | +const Stream = require('stream') |
| 2 | + |
| 3 | +const through = require('through2').obj |
| 4 | + |
| 5 | +const unzip = require('unzipper') |
| 6 | + |
| 7 | +const Vinyl = require('vinyl') |
4 | 8 |
|
5 | 9 | module.exports = (options = {}) => { |
6 | | - |
7 | | - function transform(file, enc, callback){ |
| 10 | + function transform (file, enc, callback) { |
8 | 11 | if (file.isNull()) { |
9 | | - this.push(file); |
10 | | - return callback(); |
| 12 | + this.push(file) |
| 13 | + return callback() |
11 | 14 | } |
12 | 15 |
|
13 | | - const opts = {}; |
14 | | - opts.filter = options.filter || (() => true); |
15 | | - opts.keepEmpty = options.keepEmpty || false; |
| 16 | + let stream = new Stream.PassThrough() |
16 | 17 |
|
17 | | - file.pipe(unzip.Parse()) |
| 18 | + if (file.isBuffer() && !file.pipe) { |
| 19 | + stream.end(file.contents) |
| 20 | + } else { |
| 21 | + stream = file |
| 22 | + } |
| 23 | + |
| 24 | + const opts = {} |
| 25 | + opts.filter = options.filter || (() => true) |
| 26 | + opts.keepEmpty = options.keepEmpty || false |
| 27 | + |
| 28 | + stream.pipe(unzip.Parse()) |
18 | 29 | .on('entry', entry => { |
19 | | - const chunks = []; |
20 | | - if(!opts.filter(entry)){ |
21 | | - entry.autodrain(); |
22 | | - return; |
| 30 | + const chunks = [] |
| 31 | + if (!opts.filter(entry)) { |
| 32 | + entry.autodrain() |
| 33 | + return |
23 | 34 | } |
24 | | - |
| 35 | + |
25 | 36 | entry.pipe(through((chunk, enc, cb) => { |
26 | | - chunks.push(chunk); |
27 | | - cb(); |
| 37 | + chunks.push(chunk) |
| 38 | + cb() |
28 | 39 | }, cb => { |
29 | | - if(entry.type === 'File' && (chunks.length > 0 || opts.keepEmpty)){ |
| 40 | + if (entry.type === 'File' && (chunks.length > 0 || opts.keepEmpty)) { |
30 | 41 | this.push(new Vinyl({ |
31 | | - cwd : "./", |
32 | | - path : entry.path, |
| 42 | + cwd: './', |
| 43 | + path: entry.path, |
33 | 44 | contents: Buffer.concat(chunks) |
34 | | - })); |
| 45 | + })) |
35 | 46 | } |
36 | | - cb(); |
37 | | - })); |
38 | | - }).on('close', callback); |
| 47 | + cb() |
| 48 | + })) |
| 49 | + }).on('close', callback) |
39 | 50 | } |
40 | | - return through(transform); |
41 | | -}; |
| 51 | + return through(transform) |
| 52 | +} |
0 commit comments