index.js 628 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. const PassThrough = require('stream').PassThrough;
  3. const zlib = require('zlib');
  4. const mimicResponse = require('mimic-response');
  5. module.exports = response => {
  6. // TODO: Use Array#includes when targeting Node.js 6
  7. if (['gzip', 'deflate'].indexOf(response.headers['content-encoding']) === -1) {
  8. return response;
  9. }
  10. const unzip = zlib.createUnzip();
  11. const stream = new PassThrough();
  12. mimicResponse(response, stream);
  13. unzip.on('error', err => {
  14. if (err.code === 'Z_BUF_ERROR') {
  15. stream.end();
  16. return;
  17. }
  18. stream.emit('error', err);
  19. });
  20. response.pipe(unzip).pipe(stream);
  21. return stream;
  22. };