pax_global_header 0000666 0000000 0000000 00000000064 13657403567 0014532 g ustar 00root root 0000000 0000000 52 comment=279d53e59fae44eec709e1891d9cf8c2d863f6f3
decompress-response-6.0.0/ 0000775 0000000 0000000 00000000000 13657403567 0015535 5 ustar 00root root 0000000 0000000 decompress-response-6.0.0/.editorconfig 0000664 0000000 0000000 00000000257 13657403567 0020216 0 ustar 00root root 0000000 0000000 root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space
indent_size = 2
decompress-response-6.0.0/.gitattributes 0000664 0000000 0000000 00000000023 13657403567 0020423 0 ustar 00root root 0000000 0000000 * text=auto eol=lf
decompress-response-6.0.0/.github/ 0000775 0000000 0000000 00000000000 13657403567 0017075 5 ustar 00root root 0000000 0000000 decompress-response-6.0.0/.github/funding.yml 0000664 0000000 0000000 00000000175 13657403567 0021255 0 ustar 00root root 0000000 0000000 github: sindresorhus
open_collective: sindresorhus
custom: https://sindresorhus.com/donate
tidelift: npm/decompress-response
decompress-response-6.0.0/.github/security.md 0000664 0000000 0000000 00000000263 13657403567 0021267 0 ustar 00root root 0000000 0000000 # Security Policy
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
decompress-response-6.0.0/.gitignore 0000664 0000000 0000000 00000000027 13657403567 0017524 0 ustar 00root root 0000000 0000000 node_modules
yarn.lock
decompress-response-6.0.0/.npmrc 0000664 0000000 0000000 00000000023 13657403567 0016650 0 ustar 00root root 0000000 0000000 package-lock=false
decompress-response-6.0.0/.travis.yml 0000664 0000000 0000000 00000000066 13657403567 0017650 0 ustar 00root root 0000000 0000000 language: node_js
node_js:
- '14'
- '12'
- '10'
decompress-response-6.0.0/index.d.ts 0000664 0000000 0000000 00000001036 13657403567 0017436 0 ustar 00root root 0000000 0000000 ///
import {IncomingMessage} from 'http';
/**
Decompress a HTTP response if needed.
@param response - The HTTP incoming stream with compressed data.
@returns The decompressed HTTP response stream.
@example
```
import {http} from 'http';
import decompressResponse = require('decompress-response');
http.get('https://sindresorhus.com', response => {
response = decompressResponse(response);
});
```
*/
declare function decompressResponse(response: IncomingMessage): IncomingMessage;
export = decompressResponse;
decompress-response-6.0.0/index.js 0000664 0000000 0000000 00000002450 13657403567 0017203 0 ustar 00root root 0000000 0000000 'use strict';
const {Transform, PassThrough} = require('stream');
const zlib = require('zlib');
const mimicResponse = require('mimic-response');
module.exports = response => {
const contentEncoding = (response.headers['content-encoding'] || '').toLowerCase();
if (!['gzip', 'deflate', 'br'].includes(contentEncoding)) {
return response;
}
// TODO: Remove this when targeting Node.js 12.
const isBrotli = contentEncoding === 'br';
if (isBrotli && typeof zlib.createBrotliDecompress !== 'function') {
response.destroy(new Error('Brotli is not supported on Node.js < 12'));
return response;
}
let isEmpty = true;
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;
}
finalStream.destroy(error);
});
mimicResponse(response, finalStream);
response.pipe(checker).pipe(decompressStream).pipe(finalStream);
return finalStream;
};
decompress-response-6.0.0/index.test-d.ts 0000664 0000000 0000000 00000000322 13657403567 0020410 0 ustar 00root root 0000000 0000000 import * as http from 'http';
import {expectType} from 'tsd';
import decompressResponse = require('.');
http.get('localhost', response => {
expectType(decompressResponse(response));
});
decompress-response-6.0.0/license 0000664 0000000 0000000 00000002135 13657403567 0017103 0 ustar 00root root 0000000 0000000 MIT License
Copyright (c) Sindre Sorhus (https://sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
decompress-response-6.0.0/package.json 0000664 0000000 0000000 00000001731 13657403567 0020025 0 ustar 00root root 0000000 0000000 {
"name": "decompress-response",
"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": "https://sindresorhus.com"
},
"engines": {
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"decompress",
"response",
"http",
"https",
"zlib",
"gzip",
"zip",
"deflate",
"unzip",
"ungzip",
"incoming",
"message",
"stream",
"compressed",
"brotli"
],
"dependencies": {
"mimic-response": "^3.1.0"
},
"devDependencies": {
"@types/node": "^14.0.1",
"ava": "^2.2.0",
"get-stream": "^5.0.0",
"pify": "^5.0.0",
"tsd": "^0.11.0",
"xo": "^0.30.0"
},
"xo": {
"rules": {
"@typescript-eslint/prefer-readonly-parameter-types": "off"
}
}
}
decompress-response-6.0.0/readme.md 0000664 0000000 0000000 00000002744 13657403567 0017323 0 ustar 00root root 0000000 0000000 # decompress-response [](https://travis-ci.com/sindresorhus/decompress-response)
> Decompress a HTTP response if needed
Decompresses the [response](https://nodejs.org/api/http.html#http_class_http_incomingmessage) from [`http.request`](https://nodejs.org/api/http.html#http_http_request_options_callback) if it's gzipped, deflated or compressed with Brotli, otherwise just passes it through.
Used by [`got`](https://github.com/sindresorhus/got).
## Install
```
$ npm install decompress-response
```
## Usage
```js
const http = require('http');
const decompressResponse = require('decompress-response');
http.get('https://sindresorhus.com', response => {
response = decompressResponse(response);
});
```
## API
### decompressResponse(response)
Returns the decompressed HTTP response stream.
#### response
Type: [`http.IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
The HTTP incoming stream with compressed data.
---
decompress-response-6.0.0/test/ 0000775 0000000 0000000 00000000000 13657403567 0016514 5 ustar 00root root 0000000 0000000 decompress-response-6.0.0/test/_server.js 0000664 0000000 0000000 00000000744 13657403567 0020524 0 ustar 00root root 0000000 0000000 import http from 'http';
import pify from 'pify';
export const host = 'localhost';
export const port = 6765;
export function createServer(givenPort = port) {
const server = http.createServer((request, response) => {
server.emit(request.url, request, response);
});
server.host = host;
server.port = givenPort;
server.url = `http://${host}:${givenPort}`;
server.protocol = 'http';
server.listen = pify(server.listen);
server.close = pify(server.close);
return server;
}
decompress-response-6.0.0/test/test.js 0000664 0000000 0000000 00000006421 13657403567 0020034 0 ustar 00root root 0000000 0000000 import http from 'http';
import zlib from 'zlib';
import test from 'ava';
import getStream from 'get-stream';
import pify from 'pify';
import {createServer} from './_server';
import decompressResponse from '..';
const zlibP = pify(zlib);
const httpGetP = pify(http.get, {errorFirst: false});
const fixture = 'Compressible response content.\n';
let server;
test.before('setup', async () => {
server = createServer();
server.on('/', async (request, response) => {
response.statusCode = 200;
response.setHeader('content-type', 'text/plain');
response.setHeader('content-encoding', 'gzip');
response.end(await zlibP.gzip(fixture));
});
server.on('/deflate', async (request, response) => {
response.statusCode = 200;
response.setHeader('content-encoding-type', 'text/plain');
response.setHeader('content-encoding', 'deflate');
response.end(await zlibP.deflate(fixture));
});
server.on('/brotli', async (request, response) => {
response.statusCode = 200;
response.setHeader('content-type', 'text/plain');
response.setHeader('content-encoding', 'br');
response.end(await zlibP.brotliCompress(fixture));
});
server.on('/missing-data', async (request, response) => {
response.statusCode = 200;
response.setHeader('content-encoding-type', 'text/plain');
response.setHeader('content-encoding', 'gzip');
response.end((await zlibP.gzip(fixture)).slice(0, -1));
});
await server.listen(server.port);
});
test.after('cleanup', async () => {
await server.close();
});
test('decompress gzipped content', async t => {
const response = decompressResponse(await httpGetP(server.url));
t.truthy(response.destroy);
t.truthy(response.setTimeout);
t.truthy(response.socket);
t.truthy(response.headers);
t.truthy(response.trailers);
t.truthy(response.rawHeaders);
t.truthy(response.statusCode);
t.truthy(response.httpVersion);
t.truthy(response.httpVersionMinor);
t.truthy(response.httpVersionMajor);
t.truthy(response.rawTrailers);
t.truthy(response.statusMessage);
response.setEncoding('utf8');
t.is(await getStream(response), fixture);
t.false(response.destroyed);
});
test('decompress deflated content', async t => {
const response = decompressResponse(await httpGetP(`${server.url}/deflate`));
t.is(typeof response.httpVersion, 'string');
t.truthy(response.headers);
response.setEncoding('utf8');
t.is(await getStream(response), fixture);
});
if (typeof zlib.brotliCompress === 'function') {
test('decompress brotli content', async t => {
const response = decompressResponse(await httpGetP(`${server.url}/brotli`));
t.is(typeof response.httpVersion, 'string');
t.truthy(response.headers);
response.setEncoding('utf8');
t.is(await getStream(response), fixture);
});
}
test('does not ignore missing data', async t => {
const response = decompressResponse(await httpGetP(`${server.url}/missing-data`));
t.is(typeof response.httpVersion, 'string');
t.truthy(response.headers);
response.setEncoding('utf8');
await t.throwsAsync(getStream(response), {
message: 'unexpected end of file',
code: 'Z_BUF_ERROR'
});
});
test('preserves custom properties on the stream', async t => {
let response = await httpGetP(server.url);
response.customProp = '🦄';
response = decompressResponse(response);
t.is(response.customProp, '🦄');
response.destroy();
});