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