| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | const zlib = require('zlib'); |
| 3 | const decompressTar = require('decompress-tar'); |
| 4 | const fileType = require('file-type'); |
| 5 | const isStream = require('is-stream'); |
| 6 | |
| 7 | module.exports = () => input => { |
| 8 | if (!Buffer.isBuffer(input) && !isStream(input)) { |
| 9 | return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`)); |
| 10 | } |
| 11 | |
| 12 | if (Buffer.isBuffer(input) && (!fileType(input) || fileType(input).ext !== 'gz')) { |
| 13 | return Promise.resolve([]); |
| 14 | } |
| 15 | |
| 16 | const unzip = zlib.createGunzip(); |
| 17 | const result = decompressTar()(unzip); |
| 18 | |
| 19 | if (Buffer.isBuffer(input)) { |
| 20 | unzip.end(input); |
| 21 | } else { |
| 22 | input.pipe(unzip); |
| 23 | } |
| 24 | |
| 25 | return result; |
| 26 | }; |