From b7a9c3ef49b7ef081096d8e85171134f7c5b82e0 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 11 May 2014 01:39:26 +0200 Subject: [PATCH] add gzip/deflate support --- index.js | 13 ++++++++++++- license | 21 +++++++++++++++++++++ readme.md | 10 +++++++--- test.js | 8 ++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 license diff --git a/index.js b/index.js index b5975cd..a5ed370 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ var urlLib = require('url'); var http = require('http'); var https = require('https'); +var zlib = require('zlib'); var assign = require('object-assign'); module.exports = function (url, opts, cb) { @@ -15,7 +16,11 @@ module.exports = function (url, opts, cb) { cb = cb || function () {}; opts = opts || {}; - opts.headers = opts.headers || {'user-agent': 'https://github.com/sindresorhus/got'}; + + opts.headers = assign({ + 'user-agent': 'https://github.com/sindresorhus/got', + 'accept-encoding': 'gzip,deflate' + }, opts.headers || {}); var parsedUrl = urlLib.parse(url); var fn = parsedUrl.protocol === 'https:' ? https : http; @@ -43,6 +48,12 @@ module.exports = function (url, opts, cb) { return; } + if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) { + var unzip = zlib.createUnzip(); + res.pipe(unzip); + res = unzip; + } + res.setEncoding('utf8'); res.on('data', function (data) { diff --git a/license b/license new file mode 100644 index 0000000..654d0bf --- /dev/null +++ b/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +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: + +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. diff --git a/readme.md b/readme.md index 0f08c56..b50df07 100644 --- a/readme.md +++ b/readme.md @@ -2,12 +2,16 @@ > Simplified HTTP/HTTPS requests -A nicer interface to the built-in [`http`](http://nodejs.org/api/http.html) module that also follows redirects. Use [request](https://github.com/mikeal/request) if you need more. +A nicer interface to the built-in [`http`](http://nodejs.org/api/http.html) module. + +It also supports following redirects and automagically handling gzip/deflate. + +Use [request](https://github.com/mikeal/request) if you need more. ## Install -```bash +```sh $ npm install --save got ``` @@ -48,4 +52,4 @@ Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_op ## License -[MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com) +MIT © [Sindre Sorhus](http://sindresorhus.com) diff --git a/test.js b/test.js index 22b8eb5..0dda9bd 100644 --- a/test.js +++ b/test.js @@ -42,3 +42,11 @@ it('should support optional options', function (done) { done(); }); }); + +it('should support gzip', function (done) { + got('http://sindresorhus.com', function (err, data) { + assert(!err, err); + assert(/^/.test(data)); + done(); + }); +});