index.js 674 B

12345678910111213141516171819202122
  1. 'use strict';
  2. const decompressTar = require('decompress-tar');
  3. const fileType = require('file-type');
  4. const isStream = require('is-stream');
  5. const seekBzip = require('seek-bzip');
  6. const unbzip2Stream = require('unbzip2-stream');
  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. if (Buffer.isBuffer(input) && (!fileType(input) || fileType(input).ext !== 'bz2')) {
  12. return Promise.resolve([]);
  13. }
  14. if (Buffer.isBuffer(input)) {
  15. return decompressTar()(seekBzip.decode(input));
  16. }
  17. return decompressTar()(input.pipe(unbzip2Stream()));
  18. };