|
|
@ -2,6 +2,38 @@ |
|
|
|
const PassThrough = require('stream').PassThrough; |
|
|
|
const zlib = require('zlib'); |
|
|
|
|
|
|
|
// We define these manually to ensure they're always copied
|
|
|
|
// even if they would move up the prototype chain
|
|
|
|
// https://nodejs.org/api/http.html#http_class_http_incomingmessage
|
|
|
|
const knownProps = [ |
|
|
|
'destroy', |
|
|
|
'setTimeout', |
|
|
|
'socket', |
|
|
|
'headers', |
|
|
|
'trailers', |
|
|
|
'rawHeaders', |
|
|
|
'statusCode', |
|
|
|
'httpVersion', |
|
|
|
'httpVersionMinor', |
|
|
|
'httpVersionMajor', |
|
|
|
'rawTrailers', |
|
|
|
'statusMessage' |
|
|
|
]; |
|
|
|
|
|
|
|
const copyProps = (fromStream, toStream) => { |
|
|
|
const toProps = Object.keys(toStream); |
|
|
|
const fromProps = new Set(Object.keys(fromStream).concat(knownProps)); |
|
|
|
|
|
|
|
for (const prop of fromProps) { |
|
|
|
// Don't overwrite existing properties
|
|
|
|
if (toProps.indexOf(prop) !== -1) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
toStream[prop] = typeof prop === 'function' ? fromStream[prop].bind(fromStream) : fromStream[prop]; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = res => { |
|
|
|
// TODO: Use Array#includes when targeting Node.js 6
|
|
|
|
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) === -1) { |
|
|
@ -11,17 +43,7 @@ module.exports = res => { |
|
|
|
const unzip = zlib.createUnzip(); |
|
|
|
const stream = new PassThrough(); |
|
|
|
|
|
|
|
// https://nodejs.org/api/http.html#http_class_http_incomingmessage
|
|
|
|
stream.destroy = res.destroy.bind(res); |
|
|
|
stream.setTimeout = res.setTimeout.bind(res); |
|
|
|
stream.socket = res.socket; |
|
|
|
stream.headers = res.headers; |
|
|
|
stream.trailers = res.trailers; |
|
|
|
stream.rawHeaders = res.rawHeaders; |
|
|
|
stream.statusCode = res.statusCode; |
|
|
|
stream.httpVersion = res.httpVersion; |
|
|
|
stream.rawTrailers = res.rawTrailers; |
|
|
|
stream.statusMessage = res.statusMessage; |
|
|
|
copyProps(res, stream); |
|
|
|
|
|
|
|
unzip.on('error', err => { |
|
|
|
if (err.code === 'Z_BUF_ERROR') { |
|
|
|