Browse Source

[BREAKING] ES2015ify

v1.x
Sindre Sorhus 8 years ago
parent
commit
51eb3920f4
  1. 3
      .editorconfig
  2. 12
      .jshintrc
  3. 5
      .travis.yml
  4. 11
      index.js
  5. 9
      package.json
  6. 10
      readme.md
  7. 10
      test/server.js
  8. 47
      test/test.js

3
.editorconfig

@ -10,6 +10,3 @@ insert_final_newline = true
[{package.json,*.yml}]
indent_style = space
indent_size = 2
[*.md]
trim_trailing_whitespace = false

12
.jshintrc

@ -1,12 +0,0 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"curly": true,
"immed": true,
"newcap": true,
"noarg": true,
"undef": true,
"unused": "vars",
"strict": true
}

5
.travis.yml

@ -1,5 +1,4 @@
language: node_js
node_js:
- 'iojs'
- '0.12'
- '0.10'
- '6'
- '4'

11
index.js

@ -1,9 +1,10 @@
'use strict';
var zlib = require('zlib');
const zlib = require('zlib');
module.exports = function (res) {
module.exports = res => {
// TODO: use Array#includes when targeting Node.js 6
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
var unzip = zlib.createUnzip();
const unzip = zlib.createUnzip();
unzip.httpVersion = res.httpVersion;
unzip.headers = res.headers;
@ -15,7 +16,7 @@ module.exports = function (res) {
unzip.statusMessage = res.statusMessage;
unzip.socket = res.socket;
unzip.once('error', function (err) {
unzip.once('error', err => {
if (err.code === 'Z_BUF_ERROR') {
res.emit('end');
return;
@ -24,7 +25,7 @@ module.exports = function (res) {
res.emit('error', err);
});
res.on('close', function () {
res.on('close', () => {
unzip.emit('close');
});

9
package.json

@ -17,10 +17,10 @@
}
],
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "node test/test.js"
"test": "tape && xo"
},
"files": [
"index.js"
@ -37,7 +37,8 @@
"stream"
],
"devDependencies": {
"concat-stream": "^1.5.0",
"tape": "^4.0.0"
"get-stream": "^2.3.0",
"tape": "^4.0.0",
"xo": "*"
}
}

10
readme.md

@ -2,7 +2,7 @@
> Unzip a HTTP response if needed
Unzips the response from `http.request` if it's gzipped/deflated, otherwise just passes it through.
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.
## Install
@ -15,10 +15,10 @@ $ npm install --save unzip-response
## Usage
```js
var http = require('http');
var unzipResponse = require('unzip-response');
const http = require('http');
const unzipResponse = require('unzip-response');
http.get('http://sindresorhus.com', function (res) {
http.get('http://sindresorhus.com', res => {
res = unzipResponse(res);
});
```
@ -26,4 +26,4 @@ http.get('http://sindresorhus.com', function (res) {
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)

10
test/server.js

@ -1,21 +1,21 @@
'use strict';
var http = require('http');
const http = require('http');
exports.host = 'localhost';
exports.port = 6765;
exports.createServer = function (port) {
var host = exports.host;
exports.createServer = port => {
const host = exports.host;
port = port || exports.port;
var s = http.createServer(function (req, resp) {
const s = http.createServer((req, resp) => {
s.emit(req.url, req, resp);
});
s.host = host;
s.port = port;
s.url = 'http://' + host + ':' + port;
s.url = `http://${host}:${port}`;
s.protocol = 'http';
return s;

47
test/test.js

@ -1,18 +1,19 @@
'use strict';
var http = require('http');
var zlib = require('zlib');
var test = require('tape');
var concatStream = require('concat-stream');
var fn = require('../');
var server = require('./server.js');
var s = server.createServer();
var fixture = 'Compressible response content.\n';
const http = require('http');
const zlib = require('zlib');
const test = require('tape');
const getStream = require('get-stream');
const fn = require('../');
const server = require('./server.js');
s.on('/', function (req, res) {
const s = server.createServer();
const fixture = 'Compressible response content.\n';
s.on('/', (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Encoding', 'gzip');
zlib.gzip(fixture, function (err, data) {
zlib.gzip(fixture, (err, data) => {
if (err) {
throw err;
}
@ -21,11 +22,11 @@ s.on('/', function (req, res) {
});
});
s.on('/missing-data', function (req, res) {
s.on('/missing-data', (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Encoding', 'gzip');
zlib.gzip(fixture, function (err, data) {
zlib.gzip(fixture, (err, data) => {
if (err) {
throw err;
}
@ -34,14 +35,14 @@ s.on('/missing-data', function (req, res) {
});
});
test('setup', function (t) {
s.listen(s.port, function () {
test('setup', t => {
s.listen(s.port, () => {
t.end();
});
});
test('unzip content', function (t) {
http.get(s.url, function (res) {
test('unzip content', t => {
http.get(s.url, res => {
res = fn(res);
t.ok(typeof res.httpVersion === 'string');
@ -49,15 +50,15 @@ test('unzip content', function (t) {
res.setEncoding('utf8');
res.pipe(concatStream(function (data) {
getStream(res).then(data => {
t.equal(data, fixture);
t.end();
}));
});
}).on('err', t.error.bind(t));
});
test('ignore missing data', function (t) {
http.get(s.url + '/missing-data', function (res) {
test('ignore missing data', t => {
http.get(`${s.url}/missing-data`, res => {
res = fn(res);
t.ok(typeof res.httpVersion === 'string');
@ -65,14 +66,14 @@ test('ignore missing data', function (t) {
res.setEncoding('utf8');
res.pipe(concatStream(function (data) {
getStream(res).then(data => {
t.equal(data, fixture);
t.end();
}));
});
}).on('err', t.error.bind(t));
});
test('cleanup', function (t) {
test('cleanup', t => {
s.close();
t.end();
});

Loading…
Cancel
Save