pax_global_header00006660000000000000000000000064136574035670014532gustar00rootroot0000000000000052 comment=279d53e59fae44eec709e1891d9cf8c2d863f6f3 decompress-response-6.0.0/000077500000000000000000000000001365740356700155355ustar00rootroot00000000000000decompress-response-6.0.0/.editorconfig000066400000000000000000000002571365740356700202160ustar00rootroot00000000000000root = 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/.gitattributes000066400000000000000000000000231365740356700204230ustar00rootroot00000000000000* text=auto eol=lf decompress-response-6.0.0/.github/000077500000000000000000000000001365740356700170755ustar00rootroot00000000000000decompress-response-6.0.0/.github/funding.yml000066400000000000000000000001751365740356700212550ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus custom: https://sindresorhus.com/donate tidelift: npm/decompress-response decompress-response-6.0.0/.github/security.md000066400000000000000000000002631365740356700212670ustar00rootroot00000000000000# 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/.gitignore000066400000000000000000000000271365740356700175240ustar00rootroot00000000000000node_modules yarn.lock decompress-response-6.0.0/.npmrc000066400000000000000000000000231365740356700166500ustar00rootroot00000000000000package-lock=false decompress-response-6.0.0/.travis.yml000066400000000000000000000000661365740356700176500ustar00rootroot00000000000000language: node_js node_js: - '14' - '12' - '10' decompress-response-6.0.0/index.d.ts000066400000000000000000000010361365740356700174360ustar00rootroot00000000000000/// 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.js000066400000000000000000000024501365740356700172030ustar00rootroot00000000000000'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.ts000066400000000000000000000003221365740356700204100ustar00rootroot00000000000000import * as http from 'http'; import {expectType} from 'tsd'; import decompressResponse = require('.'); http.get('localhost', response => { expectType(decompressResponse(response)); }); decompress-response-6.0.0/license000066400000000000000000000021351365740356700171030ustar00rootroot00000000000000MIT 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.json000066400000000000000000000017311365740356700200250ustar00rootroot00000000000000{ "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.md000066400000000000000000000027441365740356700173230ustar00rootroot00000000000000# decompress-response [![Build Status](https://travis-ci.com/sindresorhus/decompress-response.svg?branch=master)](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. ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
decompress-response-6.0.0/test/000077500000000000000000000000001365740356700165145ustar00rootroot00000000000000decompress-response-6.0.0/test/_server.js000066400000000000000000000007441365740356700205240ustar00rootroot00000000000000import 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.js000066400000000000000000000064211365740356700200340ustar00rootroot00000000000000import 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(); });