index.js 655 B

1234567891011121314151617181920212223242526
  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. module.exports = () => input => {
  7. if (!Buffer.isBuffer(input) && !isStream(input)) {
  8. return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`));
  9. }
  10. if (Buffer.isBuffer(input) && (!fileType(input) || fileType(input).ext !== 'gz')) {
  11. return Promise.resolve([]);
  12. }
  13. const unzip = zlib.createGunzip();
  14. const result = decompressTar()(unzip);
  15. if (Buffer.isBuffer(input)) {
  16. unzip.end(input);
  17. } else {
  18. input.pipe(unzip);
  19. }
  20. return result;
  21. };