Comparing version 1.2.2 to 2.0.0
52
index.js
@@ -8,2 +8,4 @@ 'use strict'; | ||
var assign = require('object-assign'); | ||
var read = require('read-all-stream'); | ||
var timeout = require('timed-out'); | ||
@@ -49,9 +51,10 @@ module.exports = function (url, opts, cb) { | ||
fn.get(arg, function (res) { | ||
var req = fn.get(arg, function (response) { | ||
var statusCode = response.statusCode; | ||
var res = response; | ||
// redirect | ||
if (res.statusCode < 400 && res.statusCode >= 300 && res.headers.location) { | ||
res.destroy(); | ||
if (statusCode < 400 && statusCode >= 300 && res.headers.location) { | ||
if (++redirectCount > 10) { | ||
cb(new Error('Redirected 10 times. Aborting.')); | ||
cb(new Error('Redirected 10 times. Aborting.'), undefined, res); | ||
return; | ||
@@ -64,8 +67,2 @@ } | ||
if (res.statusCode < 200 || res.statusCode > 299) { | ||
res.destroy(); | ||
cb(res.statusCode); | ||
return; | ||
} | ||
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) { | ||
@@ -77,2 +74,11 @@ var unzip = zlib.createUnzip(); | ||
if (statusCode < 200 || statusCode > 299) { | ||
read(res, encoding, function (error, data) { | ||
var err = error || new Error('Couldn\'t connect to ' + url + '.'); | ||
err.code = statusCode; | ||
cb(err, data, response); | ||
}); | ||
return; | ||
} | ||
// pipe the response to the proxy if in proxy mode | ||
@@ -84,22 +90,8 @@ if (proxy) { | ||
res.once('error', cb); | ||
read(res, encoding, cb, response); | ||
}).once('error', cb); | ||
var chunks = []; | ||
var len = 0; | ||
res.on('data', function (chunk) { | ||
chunks.push(chunk); | ||
len += chunk.length; | ||
}); | ||
res.once('end', function () { | ||
var data = Buffer.concat(chunks, len); | ||
if (encoding !== null) { | ||
data = data.toString(encoding || 'utf8'); | ||
} | ||
cb(null, data, res); | ||
}); | ||
}).once('error', cb); | ||
if (opts.timeout) { | ||
timeout(req, opts.timeout); | ||
} | ||
}; | ||
@@ -106,0 +98,0 @@ |
{ | ||
"name": "got", | ||
"version": "1.2.2", | ||
"version": "2.0.0", | ||
"description": "Simplified HTTP/HTTPS requests", | ||
@@ -34,3 +34,5 @@ "license": "MIT", | ||
"dependencies": { | ||
"object-assign": "^1.0.0" | ||
"object-assign": "^2.0.0", | ||
"read-all-stream": "^0.1.0", | ||
"timed-out": "^1.0.0" | ||
}, | ||
@@ -37,0 +39,0 @@ "devDependencies": { |
@@ -60,4 +60,14 @@ # got [![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) | ||
##### options.timeout | ||
Type: `number` | ||
Milliseconds after which the request will be aborted and an error event with `ETIMEDOUT` code will be emitted. | ||
##### callback(err, data, response) | ||
###### err | ||
`Error` object with HTTP status code as `code` property. | ||
###### data | ||
@@ -64,0 +74,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4751
89
3
80
+ Addedread-all-stream@^0.1.0
+ Addedtimed-out@^1.0.0
+ Addedobject-assign@2.1.1(transitive)
+ Addedread-all-stream@0.1.2(transitive)
+ Addedtimed-out@1.0.0(transitive)
- Removedobject-assign@1.0.0(transitive)
Updatedobject-assign@^2.0.0