index.js 875 B

1234567891011121314151617181920212223242526272829303132
  1. var zlib = require('zlib')
  2. var peek = require('peek-stream')
  3. var through = require('through2')
  4. var pumpify = require('pumpify')
  5. var isGzip = require('is-gzip')
  6. var isDeflate = require('is-deflate')
  7. var isCompressed = function (data) {
  8. if (isGzip(data)) return 1
  9. if (isDeflate(data)) return 2
  10. return 0
  11. }
  12. var gunzip = function (maxRecursion) {
  13. if (maxRecursion === undefined) maxRecursion = 3
  14. return peek({newline: false, maxBuffer: 10}, function (data, swap) {
  15. if (maxRecursion < 0) return swap(new Error('Maximum recursion reached'))
  16. switch (isCompressed(data)) {
  17. case 1:
  18. swap(null, pumpify(zlib.createGunzip(), gunzip(maxRecursion - 1)))
  19. break
  20. case 2:
  21. swap(null, pumpify(zlib.createInflate(), gunzip(maxRecursion - 1)))
  22. break
  23. default:
  24. swap(null, through())
  25. }
  26. })
  27. }
  28. module.exports = gunzip