Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 20, 2019
1 parent 9748dd9 commit b2917dd
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- '6'
- '4'
- '10'
- '8'
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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);
Expand Down
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
`The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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.
96 changes: 45 additions & 51 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
```
Expand Down
118 changes: 59 additions & 59 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
});

0 comments on commit b2917dd

Please sign in to comment.