decompress-response
Advanced tools
Comparing version 5.0.0 to 6.0.0
48
index.js
'use strict'; | ||
const { | ||
pipeline: streamPipeline, | ||
PassThrough: PassThroughStream | ||
} = require('stream'); | ||
const {Transform, PassThrough} = require('stream'); | ||
const zlib = require('zlib'); | ||
const mimicResponse = require('mimic-response'); | ||
const decompressResponse = response => { | ||
module.exports = response => { | ||
const contentEncoding = (response.headers['content-encoding'] || '').toLowerCase(); | ||
@@ -19,25 +16,44 @@ | ||
if (isBrotli && typeof zlib.createBrotliDecompress !== 'function') { | ||
response.destroy(new Error('Brotli is not supported on Node.js < 12')); | ||
return response; | ||
} | ||
const decompress = isBrotli ? zlib.createBrotliDecompress() : zlib.createUnzip(); | ||
const stream = new PassThroughStream(); | ||
let isEmpty = true; | ||
decompress.on('error', error => { | ||
// Ignore empty response | ||
if (error.code === 'Z_BUF_ERROR') { | ||
stream.end(); | ||
const checker = new Transform({ | ||
transform(data, _encoding, callback) { | ||
isEmpty = false; | ||
callback(null, data); | ||
}, | ||
flush(callback) { | ||
callback(); | ||
} | ||
}); | ||
const finalStream = new PassThrough({ | ||
autoDestroy: false, | ||
destroy(error, callback) { | ||
response.destroy(); | ||
callback(error); | ||
} | ||
}); | ||
const decompressStream = isBrotli ? zlib.createBrotliDecompress() : zlib.createUnzip(); | ||
decompressStream.once('error', error => { | ||
if (isEmpty && !response.readable) { | ||
finalStream.end(); | ||
return; | ||
} | ||
stream.emit('error', error); | ||
finalStream.destroy(error); | ||
}); | ||
const finalStream = streamPipeline(response, decompress, stream, () => {}); | ||
mimicResponse(response, finalStream); | ||
response.pipe(checker).pipe(decompressStream).pipe(finalStream); | ||
return finalStream; | ||
}; | ||
module.exports = decompressResponse; |
{ | ||
"name": "decompress-response", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Decompress a HTTP response if needed", | ||
"license": "MIT", | ||
"repository": "sindresorhus/decompress-response", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
@@ -40,12 +41,17 @@ "engines": { | ||
"dependencies": { | ||
"mimic-response": "^2.0.0" | ||
"mimic-response": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^12.7.1", | ||
"@types/node": "^14.0.1", | ||
"ava": "^2.2.0", | ||
"get-stream": "^5.0.0", | ||
"pify": "^4.0.1", | ||
"tsd": "^0.10.0", | ||
"xo": "^0.25.3" | ||
"pify": "^5.0.0", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.30.0" | ||
}, | ||
"xo": { | ||
"rules": { | ||
"@typescript-eslint/prefer-readonly-parameter-types": "off" | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# decompress-response [![Build Status](https://travis-ci.org/sindresorhus/decompress-response.svg?branch=master)](https://travis-ci.org/sindresorhus/decompress-response) | ||
# decompress-response [![Build Status](https://travis-ci.com/sindresorhus/decompress-response.svg?branch=master)](https://travis-ci.com/sindresorhus/decompress-response) | ||
@@ -9,3 +9,2 @@ > Decompress a HTTP response if needed | ||
## Install | ||
@@ -17,3 +16,2 @@ | ||
## Usage | ||
@@ -30,3 +28,2 @@ | ||
## API | ||
@@ -44,3 +41,2 @@ | ||
--- | ||
@@ -47,0 +43,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5472
61
49
+ Addedmimic-response@3.1.0(transitive)
- Removedmimic-response@2.1.0(transitive)
Updatedmimic-response@^3.1.0