Skip to content

Commit 2572d92

Browse files
committed
Rename to decompress-response and other tweaks
Fixes #7
1 parent 1a84047 commit 2572d92

File tree

6 files changed

+50
-51
lines changed

6 files changed

+50
-51
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
* text=auto
2+
*.js text eol=lf

index.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ const PassThrough = require('stream').PassThrough;
33
const zlib = require('zlib');
44

55
module.exports = res => {
6-
// TODO: use Array#includes when targeting Node.js 6
6+
// TODO: Use Array#includes when targeting Node.js 6
77
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) === -1) {
88
return res;
99
}
1010

1111
const unzip = zlib.createUnzip();
1212
const stream = new PassThrough();
1313

14-
stream.httpVersion = res.httpVersion;
14+
// https://nodejs.org/api/http.html#http_class_http_incomingmessage
15+
stream.destroy = res.destroy.bind(res);
16+
stream.setTimeout = res.setTimeout.bind(res);
17+
stream.socket = res.socket;
1518
stream.headers = res.headers;
16-
stream.rawHeaders = res.rawHeaders;
1719
stream.trailers = res.trailers;
18-
stream.rawTrailers = res.rawTrailers;
19-
stream.setTimeout = res.setTimeout.bind(res);
20+
stream.rawHeaders = res.rawHeaders;
2021
stream.statusCode = res.statusCode;
22+
stream.httpVersion = res.httpVersion;
23+
stream.rawTrailers = res.rawTrailers;
2124
stream.statusMessage = res.statusMessage;
22-
stream.socket = res.socket;
2325

2426
unzip.on('error', err => {
2527
if (err.code === 'Z_BUF_ERROR') {

package.json

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "unzip-response",
2+
"name": "decompress-response",
33
"version": "3.0.0",
4-
"description": "Unzip a HTTP response if needed",
4+
"description": "Decompress a HTTP response if needed",
55
"license": "MIT",
6-
"repository": "sindresorhus/unzip-response",
6+
"repository": "sindresorhus/decompress-response",
77
"maintainers": [
88
{
99
"name": "Sindre Sorhus",
@@ -26,24 +26,26 @@
2626
"index.js"
2727
],
2828
"keywords": [
29+
"decompress",
30+
"response",
2931
"http",
30-
"unzip",
32+
"https",
3133
"zlib",
3234
"gzip",
35+
"zip",
3336
"deflate",
37+
"unzip",
38+
"ungzip",
3439
"incoming",
3540
"message",
36-
"response",
37-
"stream"
41+
"stream",
42+
"compressed"
3843
],
3944
"devDependencies": {
4045
"ava": "*",
41-
"get-stream": "^2.3.0",
46+
"get-stream": "^3.0.0",
4247
"pify": "^2.3.0",
4348
"rfpify": "^1.0.0",
4449
"xo": "*"
45-
},
46-
"xo": {
47-
"esnext": true
4850
}
4951
}

readme.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
# unzip-response [![Build Status](https://travis-ci.org/sindresorhus/unzip-response.svg?branch=master)](https://travis-ci.org/sindresorhus/unzip-response)
1+
# decompress-response [![Build Status](https://travis-ci.org/sindresorhus/decompress-response.svg?branch=master)](https://travis-ci.org/sindresorhus/decompress-response)
22

3-
> Unzip a HTTP response if needed
3+
> Decompress a HTTP response if needed
44
5-
Unzips the response from [`http.request`](https://nodejs.org/api/http.html#http_http_request_options_callback) if it's gzipped/deflated, otherwise just passes it through.
5+
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 or deflated, otherwise just passes it through.
66

77

88
## Install
99

1010
```
11-
$ npm install --save unzip-response
11+
$ npm install decompress-response
1212
```
1313

1414

1515
## Usage
1616

1717
```js
1818
const http = require('http');
19-
const unzipResponse = require('unzip-response');
19+
const decompressResponse = require('decompress-response');
2020

21-
http.get('http://sindresorhus.com', res => {
22-
res = unzipResponse(res);
21+
http.get('http://sindresorhus.com', response => {
22+
response = decompressResponse(response);
2323
});
2424
```
2525

test/helpers/server.js

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
'use strict';
2-
const http = require('http');
3-
const pify = require('pify');
1+
import http from 'http';
2+
import pify from 'pify';
43

5-
exports.host = 'localhost';
6-
exports.port = 6765;
7-
8-
exports.createServer = port => {
9-
const host = exports.host;
10-
11-
port = port || exports.port;
4+
export const host = 'localhost';
5+
export const port = 6765;
126

7+
export function createServer(givenPort = port) {
138
const s = http.createServer((req, resp) => {
149
s.emit(req.url, req, resp);
1510
});
1611

1712
s.host = host;
18-
s.port = port;
19-
s.url = `http://${host}:${port}`;
13+
s.port = givenPort;
14+
s.url = `http://${host}:${givenPort}`;
2015
s.protocol = 'http';
21-
2216
s.listen = pify(s.listen);
2317
s.close = pify(s.close);
2418

2519
return s;
26-
};
20+
}

test/test.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import test from 'ava';
44
import getStream from 'get-stream';
55
import pify from 'pify';
66
import rfpify from 'rfpify';
7-
import m from '../';
8-
import {createServer} from './helpers/server.js';
7+
import m from '..';
8+
import {createServer} from './helpers/server';
99

1010
const zlibP = pify(zlib);
1111
const httpGetP = rfpify(http.get);
@@ -18,29 +18,33 @@ test.before('setup', async () => {
1818

1919
s.on('/', async (req, res) => {
2020
res.statusCode = 200;
21-
res.setHeader('Content-Type', 'text/plain');
22-
res.setHeader('Content-Encoding', 'gzip');
21+
res.setHeader('content-type', 'text/plain');
22+
res.setHeader('content-encoding', 'gzip');
2323
res.end(await zlibP.gzip(fixture));
2424
});
2525

2626
s.on('/deflate', async (req, res) => {
2727
res.statusCode = 200;
28-
res.setHeader('Content-Encodingnt-Type', 'text/plain');
29-
res.setHeader('Content-Encoding', 'deflate');
28+
res.setHeader('content-encoding-type', 'text/plain');
29+
res.setHeader('content-encoding', 'deflate');
3030
res.end(await zlibP.deflate(fixture));
3131
});
3232

3333
s.on('/missing-data', async (req, res) => {
3434
res.statusCode = 200;
35-
res.setHeader('Content-Encodingnt-Type', 'text/plain');
36-
res.setHeader('Content-Encoding', 'gzip');
35+
res.setHeader('content-encoding-type', 'text/plain');
36+
res.setHeader('content-encoding', 'gzip');
3737
res.end((await zlibP.gzip(fixture)).slice(0, -1));
3838
});
3939

4040
await s.listen(s.port);
4141
});
4242

43-
test('unzip gzipped content', async t => {
43+
test.after('cleanup', async () => {
44+
await s.close();
45+
});
46+
47+
test('decompress gzipped content', async t => {
4448
const res = m(await httpGetP(s.url));
4549

4650
t.is(typeof res.httpVersion, 'string');
@@ -51,7 +55,7 @@ test('unzip gzipped content', async t => {
5155
t.is(await getStream(res), fixture);
5256
});
5357

54-
test('unzip deflated content', async t => {
58+
test('decompress deflated content', async t => {
5559
const res = m(await httpGetP(`${s.url}/deflate`));
5660

5761
t.is(typeof res.httpVersion, 'string');
@@ -72,7 +76,3 @@ test('ignore missing data', async t => {
7276

7377
t.is(await getStream(res), fixture);
7478
});
75-
76-
test.after('cleanup', async () => {
77-
await s.close();
78-
});

0 commit comments

Comments
 (0)