diff --git a/.editorconfig b/.editorconfig index 98a761d..1c6314a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,6 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[{package.json,*.yml}] +[*.yml] indent_style = space indent_size = 2 diff --git a/.gitattributes b/.gitattributes index 391f0a4..6313b56 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -* text=auto -*.js text eol=lf +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index 3c3629e..239ecff 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +yarn.lock diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/.travis.yml b/.travis.yml index 97519af..f3fa8cd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ language: node_js node_js: - - '6' - - '4' + - '10' + - '8' diff --git a/index.js b/index.js index 8da4d40..897d093 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,16 @@ 'use strict'; -const PassThrough = require('stream').PassThrough; +const {PassThrough} = require('stream'); const zlib = require('zlib'); const mimicResponse = require('mimic-response'); module.exports = response => { - // TODO: Use Array#includes when targeting Node.js 6 - if (['gzip', 'deflate', 'br'].indexOf(response.headers['content-encoding']) === -1) { + const contentEncoding = response.headers['content-encoding']; + + if (!['gzip', 'deflate', 'br'].includes(contentEncoding)) { return response; } - const isBrotli = response.headers['content-encoding'] === 'br'; - + const isBrotli = contentEncoding === 'br'; if (isBrotli && typeof zlib.createBrotliDecompress !== 'function') { return response; } @@ -20,14 +20,14 @@ module.exports = response => { mimicResponse(response, stream); - decompress.on('error', err => { + decompress.on('error', error => { // Ignore empty response - if (err.code === 'Z_BUF_ERROR') { + if (error.code === 'Z_BUF_ERROR') { stream.end(); return; } - stream.emit('error', err); + stream.emit('error', error); }); response.pipe(decompress).pipe(stream); diff --git a/license b/license index 32a16ce..e7af2f7 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -`The MIT License (MIT) +MIT License Copyright (c) Sindre Sorhus (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: +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 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. +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. diff --git a/package.json b/package.json index 3574dc2..9018536 100644 --- a/package.json +++ b/package.json @@ -1,53 +1,47 @@ { - "name": "decompress-response", - "version": "3.3.0", - "description": "Decompress a HTTP response if needed", - "license": "MIT", - "repository": "sindresorhus/decompress-response", - "maintainers": [ - { - "name": "Sindre Sorhus", - "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" - }, - { - "name": "Vsevolod Strukchinsky", - "email": "floatdrop@gmail.com", - "url": "github.com/floatdrop" - } - ], - "engines": { - "node": ">=4" - }, - "scripts": { - "test": "xo && ava" - }, - "files": [ - "index.js" - ], - "keywords": [ - "decompress", - "response", - "http", - "https", - "zlib", - "gzip", - "zip", - "deflate", - "unzip", - "ungzip", - "incoming", - "message", - "stream", - "compressed" - ], - "dependencies": { - "mimic-response": "^1.0.0" - }, - "devDependencies": { - "ava": "*", - "get-stream": "^3.0.0", - "pify": "^3.0.0", - "xo": "*" - } + "name": "decompress-response", + "version": "3.3.0", + "description": "Decompress a HTTP response if needed", + "license": "MIT", + "repository": "sindresorhus/decompress-response", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=8" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "index.js" + ], + "keywords": [ + "decompress", + "response", + "http", + "https", + "zlib", + "gzip", + "zip", + "deflate", + "unzip", + "ungzip", + "incoming", + "message", + "stream", + "compressed", + "brotli" + ], + "dependencies": { + "mimic-response": "^2.0.0" + }, + "devDependencies": { + "ava": "^1.1.0", + "get-stream": "^4.1.0", + "pify": "^4.0.1", + "xo": "^0.24.0" + } } diff --git a/readme.md b/readme.md index 0216623..ca6582a 100644 --- a/readme.md +++ b/readme.md @@ -20,7 +20,7 @@ $ npm install decompress-response const http = require('http'); const decompressResponse = require('decompress-response'); -http.get('http://sindresorhus.com', response => { +http.get('https://sindresorhus.com', response => { response = decompressResponse(response); }); ``` diff --git a/test/test.js b/test/test.js index 486bb7c..7cbc843 100644 --- a/test/test.js +++ b/test/test.js @@ -3,7 +3,7 @@ import zlib from 'zlib'; import test from 'ava'; import getStream from 'get-stream'; import pify from 'pify'; -import m from '..'; +import decompressResponse from '..'; import {createServer} from './helpers/server'; const zlibP = pify(zlib); @@ -15,32 +15,32 @@ let s; test.before('setup', async () => { s = createServer(); - s.on('/', async (req, res) => { - res.statusCode = 200; - res.setHeader('content-type', 'text/plain'); - res.setHeader('content-encoding', 'gzip'); - res.end(await zlibP.gzip(fixture)); + s.on('/', async (request, response) => { + response.statusCode = 200; + response.setHeader('content-type', 'text/plain'); + response.setHeader('content-encoding', 'gzip'); + response.end(await zlibP.gzip(fixture)); }); - s.on('/deflate', async (req, res) => { - res.statusCode = 200; - res.setHeader('content-encoding-type', 'text/plain'); - res.setHeader('content-encoding', 'deflate'); - res.end(await zlibP.deflate(fixture)); + s.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)); }); - s.on('/brotli', async (req, res) => { - res.statusCode = 200; - res.setHeader('content-type', 'text/plain'); - res.setHeader('content-encoding', 'br'); - res.end(await zlibP.brotliCompress(fixture)); + s.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)); }); - s.on('/missing-data', async (req, res) => { - res.statusCode = 200; - res.setHeader('content-encoding-type', 'text/plain'); - res.setHeader('content-encoding', 'gzip'); - res.end((await zlibP.gzip(fixture)).slice(0, -1)); + s.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 s.listen(s.port); @@ -51,67 +51,67 @@ test.after('cleanup', async () => { }); test('decompress gzipped content', async t => { - const res = m(await httpGetP(s.url)); - - t.truthy(res.destroy); - t.truthy(res.setTimeout); - t.truthy(res.socket); - t.truthy(res.headers); - t.truthy(res.trailers); - t.truthy(res.rawHeaders); - t.truthy(res.statusCode); - t.truthy(res.httpVersion); - t.truthy(res.httpVersionMinor); - t.truthy(res.httpVersionMajor); - t.truthy(res.rawTrailers); - t.truthy(res.statusMessage); - - res.setEncoding('utf8'); - - t.is(await getStream(res), fixture); + const response = decompressResponse(await httpGetP(s.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); }); test('decompress deflated content', async t => { - const res = m(await httpGetP(`${s.url}/deflate`)); + const response = decompressResponse(await httpGetP(`${s.url}/deflate`)); - t.is(typeof res.httpVersion, 'string'); - t.truthy(res.headers); + t.is(typeof response.httpVersion, 'string'); + t.truthy(response.headers); - res.setEncoding('utf8'); + response.setEncoding('utf8'); - t.is(await getStream(res), fixture); + t.is(await getStream(response), fixture); }); if (typeof zlib.brotliCompress === 'function') { test('decompress brotli content', async t => { - const res = m(await httpGetP(`${s.url}/brotli`)); + const response = decompressResponse(await httpGetP(`${s.url}/brotli`)); - t.is(typeof res.httpVersion, 'string'); - t.truthy(res.headers); + t.is(typeof response.httpVersion, 'string'); + t.truthy(response.headers); - res.setEncoding('utf8'); + response.setEncoding('utf8'); - t.is(await getStream(res), fixture); + t.is(await getStream(response), fixture); }); } test('ignore missing data', async t => { - const res = m(await httpGetP(`${s.url}/missing-data`)); + const response = decompressResponse(await httpGetP(`${s.url}/missing-data`)); - t.is(typeof res.httpVersion, 'string'); - t.truthy(res.headers); + t.is(typeof response.httpVersion, 'string'); + t.truthy(response.headers); - res.setEncoding('utf8'); + response.setEncoding('utf8'); - t.is(await getStream(res), fixture); + t.is(await getStream(response), fixture); }); test('preserves custom properties on the stream', async t => { - let res = await httpGetP(s.url); - res.customProp = '🦄'; - res = m(res); + let response = await httpGetP(s.url); + response.customProp = '🦄'; + response = decompressResponse(response); - t.is(res.customProp, '🦄'); + t.is(response.customProp, '🦄'); - res.destroy(); + response.destroy(); });