gunzip-maybe.js 556 B

12345678910111213141516171819202122
  1. var duplex = require('mississippi').duplex
  2. var through = require('mississippi').through
  3. var zlib = require('zlib')
  4. function hasGzipHeader (c) {
  5. return c[0] === 0x1F && c[1] === 0x8B && c[2] === 0x08
  6. }
  7. module.exports = gunzip
  8. function gunzip () {
  9. var stream = duplex()
  10. var peeker = through(function (chunk, enc, cb) {
  11. var newStream = hasGzipHeader(chunk)
  12. ? zlib.createGunzip()
  13. : through()
  14. stream.setReadable(newStream)
  15. stream.setWritable(newStream)
  16. stream.write(chunk)
  17. })
  18. stream.setWritable(peeker)
  19. return stream
  20. }