Comparing version 4.2.0 to 5.0.0
70
index.js
@@ -12,3 +12,3 @@ 'use strict'; | ||
var timedOut = require('timed-out'); | ||
var prependHttp = require('prepend-http'); | ||
var urlParseLax = require('url-parse-lax'); | ||
var lowercaseKeys = require('lowercase-keys'); | ||
@@ -23,2 +23,7 @@ var isRedirect = require('is-redirect'); | ||
function backoff(iter) { | ||
var noise = Math.random() * 1000; | ||
return (1 << iter) * 1000 + noise; | ||
} | ||
function requestAsEventEmitter(opts) { | ||
@@ -29,2 +34,3 @@ opts = opts || {}; | ||
var redirectCount = 0; | ||
var retryCount = 0; | ||
@@ -36,2 +42,3 @@ var get = function (opts) { | ||
var statusCode = res.statusCode; | ||
if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) { | ||
@@ -54,4 +61,14 @@ res.resume(); | ||
ee.emit('response', unzipResponse(res)); | ||
}).once('error', function (err) { | ||
// do not write ee.bind(...) instead of function - it will break gzip in Node.js 0.10 | ||
setImmediate(function () { | ||
ee.emit('response', typeof unzipResponse === 'function' ? unzipResponse(res) : res); | ||
}); | ||
}); | ||
req.once('error', function (err) { | ||
if (retryCount < opts.retries) { | ||
setTimeout(get, backoff(retryCount++), opts); | ||
return; | ||
} | ||
ee.emit('error', new got.RequestError(err, opts)); | ||
@@ -86,2 +103,4 @@ }); | ||
readAllStream(res, opts.encoding, function (err, data) { | ||
var statusCode = res.statusCode; | ||
if (err) { | ||
@@ -92,4 +111,2 @@ cb(new got.ReadError(err, opts), null, res); | ||
var statusCode = res.statusCode; | ||
if (statusCode < 200 || statusCode > 299) { | ||
@@ -170,7 +187,9 @@ err = new got.HTTPError(statusCode, opts); | ||
ee.on('response', function (res) { | ||
var statusCode = res.statusCode; | ||
proxy.setReadable(res); | ||
var statusCode = res.statusCode; | ||
if (statusCode < 200 || statusCode > 299) { | ||
proxy.emit('error', new got.HTTPError(statusCode, opts), null, res); | ||
return; | ||
} | ||
@@ -193,5 +212,13 @@ | ||
if (typeof url === 'string') { | ||
url = urlParseLax(url); | ||
if (url.auth) { | ||
throw new Error('Basic authentication must be done with auth option'); | ||
} | ||
} | ||
opts = objectAssign( | ||
{protocol: 'http:', path: ''}, | ||
typeof url === 'string' ? urlLib.parse(prependHttp(url)) : url, | ||
{protocol: 'http:', path: '', retries: 5}, | ||
url, | ||
opts | ||
@@ -242,15 +269,8 @@ ); | ||
// check for unix domain socket | ||
if (opts.hostname === 'unix') { | ||
// extract socket path and request path | ||
var matches = /(.+)\:(.+)/.exec(opts.path); | ||
if (matches) { | ||
var socketPath = matches[1]; | ||
var path = matches[2]; | ||
// make http.request use unix domain socket | ||
// instead of host:port combination | ||
opts.socketPath = socketPath; | ||
opts.path = path; | ||
opts.socketPath = matches[1]; | ||
opts.path = matches[2]; | ||
opts.host = null; | ||
@@ -269,10 +289,12 @@ } | ||
opts = normalizeArguments(url, opts); | ||
if (cb) { | ||
asCallback(opts, cb); | ||
asCallback(normalizeArguments(url, opts), cb); | ||
return null; | ||
} | ||
return asPromise(opts); | ||
try { | ||
return asPromise(normalizeArguments(url, opts)); | ||
} catch (error) { | ||
return PinkiePromise.reject(error); | ||
} | ||
} | ||
@@ -300,3 +322,7 @@ | ||
got.stream = function (url, opts) { | ||
got.stream = function (url, opts, cb) { | ||
if (cb || typeof opts === 'function') { | ||
throw new Error('callback can not be used with stream mode'); | ||
} | ||
return asStream(normalizeArguments(url, opts)); | ||
@@ -303,0 +329,0 @@ }; |
{ | ||
"name": "got", | ||
"version": "4.2.0", | ||
"version": "5.0.0", | ||
"description": "Simplified HTTP/HTTPS requests", | ||
@@ -22,4 +22,8 @@ "license": "MIT", | ||
}, | ||
"browser": { | ||
"unzip-response": false | ||
}, | ||
"scripts": { | ||
"test": "xo && tap test/test-*.js" | ||
"test": "xo && nyc ava", | ||
"coveralls": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
@@ -52,18 +56,26 @@ "files": [ | ||
"node-status-codes": "^1.0.0", | ||
"object-assign": "^3.0.0", | ||
"object-assign": "^4.0.1", | ||
"parse-json": "^2.1.0", | ||
"pinkie-promise": "^1.0.0", | ||
"prepend-http": "^1.0.0", | ||
"read-all-stream": "^3.0.0", | ||
"timed-out": "^2.0.0", | ||
"unzip-response": "^1.0.0" | ||
"unzip-response": "^1.0.0", | ||
"url-parse-lax": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "git+https://github.com/sindresorhus/ava.git#7cebc1099", | ||
"coveralls": "^2.11.4", | ||
"get-port": "^1.0.0", | ||
"into-stream": "^2.0.0", | ||
"istanbul": "^0.3.13", | ||
"nyc": "^3.2.2", | ||
"pem": "^1.4.4", | ||
"tap": "^1.0.0", | ||
"pify": "^2.2.0", | ||
"tempfile": "^1.1.1", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"ignores": [ | ||
"test/**" | ||
] | ||
} | ||
} |
@@ -11,7 +11,7 @@ <h1 align="center"> | ||
[![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) | ||
[![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) [![Coverage Status](https://coveralls.io/repos/sindresorhus/got/badge.svg?service=github&branch=master)](https://coveralls.io/github/sindresorhus/got?branch=master) | ||
A nicer interface to the built-in [`http`](http://nodejs.org/api/http.html) module. | ||
It supports following redirects, promises, streams, automagically handling gzip/deflate and some convenience options. | ||
It supports following redirects, promises, streams, retries, automagically handling gzip/deflate and some convenience options. | ||
@@ -31,7 +31,7 @@ Created because [`request`](https://github.com/mikeal/request) is bloated *(several megabytes!)*. | ||
```js | ||
var got = require('got'); | ||
const got = require('got'); | ||
// Callback mode | ||
got('todomvc.com', function (err, data, res) { | ||
console.log(data); | ||
got('todomvc.com', (error, body, response) => { | ||
console.log(body); | ||
//=> '<!doctype html> ...' | ||
@@ -41,10 +41,6 @@ }); | ||
// Promise mode | ||
got('todomvc.com') | ||
.then(function (res) { | ||
console.log(res.body); | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
console.error(err.response && err.response.body); | ||
}); | ||
got('todomvc.com').then(response => { | ||
console.log(response.body); | ||
//=> '<!doctype html> ...' | ||
}); | ||
@@ -58,2 +54,3 @@ // Stream mode | ||
### API | ||
@@ -67,3 +64,2 @@ | ||
*Required* | ||
Type: `string`, `object` | ||
@@ -93,3 +89,3 @@ | ||
If `body` is a plain Object, it will be stringified with [`querystring.stringify`](https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options) and sent as `application/x-www-form-urlencoded`. | ||
If `body` is a plain object, it will be stringified with [`querystring.stringify`](https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options) and sent as `application/x-www-form-urlencoded`. | ||
@@ -124,5 +120,13 @@ ###### encoding | ||
###### retries | ||
Type: `number` | ||
Default: `5` | ||
Number of request retries when network errors happens. | ||
##### callback(error, data, response) | ||
Function to be called, when error or data received. If omitted, a promise will be returned. | ||
Function to be called when error or data are received. If omitted, a promise will be returned. | ||
@@ -153,7 +157,7 @@ ###### error | ||
`redirect` event to get the response object of a redirect. Second argument is options for the next request to the redirect location. | ||
`redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location. | ||
##### .on('error', error, body, response) | ||
`error` event emitted in case of protocol error (like `ENOTFOUND` etc.) or status error (4xx or 5xx). Second argument is body of server response in case of status error. Third argument is response object. | ||
`error` event emitted in case of protocol error (like `ENOTFOUND` etc.) or status error (4xx or 5xx). The second argument is the body of the server response in case of status error. The third argument is response object. | ||
@@ -170,6 +174,9 @@ | ||
## Errors | ||
Each error contains (if available) `host`, `hostname`, `method` and `path` properties to make debug easier. | ||
Each error contains (if available) `host`, `hostname`, `method` and `path` properties to make debugging easier. | ||
In Promise mode, the `response` is attached to the error. | ||
#### got.RequestError | ||
@@ -196,3 +203,3 @@ | ||
## Proxy | ||
## Proxies | ||
@@ -202,4 +209,4 @@ You can use the [`tunnel`](https://github.com/koichik/node-tunnel) module with the `agent` option to work with proxies: | ||
```js | ||
var got = require('got'); | ||
var tunnel = require('tunnel'); | ||
const got = require('got'); | ||
const tunnel = require('tunnel'); | ||
@@ -212,7 +219,24 @@ got('todomvc.com', { | ||
}) | ||
}, function () {}); | ||
}, () => {}); | ||
``` | ||
### Unix Domain Sockets | ||
## Cookies | ||
You can use the [`cookie`](https://github.com/jshttp/cookie) module to include cookies in a request: | ||
```js | ||
const got = require('got'); | ||
const cookie = require('cookie'); | ||
got('google.com', { | ||
headers: { | ||
cookie: cookie.serialize('foo', 'bar') | ||
} | ||
}); | ||
``` | ||
## Unix Domain Sockets | ||
Requests can also be sent via [unix domain sockets](http://serverfault.com/questions/124517/whats-the-difference-between-unix-socket-and-tcp-ip-socket). Use the following URL scheme: `PROTOCOL://unix:SOCKET:PATH`. | ||
@@ -224,4 +248,2 @@ | ||
Example: | ||
```js | ||
@@ -234,11 +256,5 @@ got('http://unix:/var/run/docker.sock:/containers/json'); | ||
Use-cases: | ||
- [Docker API](https://docs.docker.com/articles/basics/#bind-docker-to-another-host-port-or-a-unix-socket) (/var/run/docker.sock) | ||
- [fleet API](https://coreos.com/fleet/docs/latest/deployment-and-configuration.html#api) (/var/run/fleet.sock) | ||
## Tip | ||
It's a good idea to set the `'user-agent'` header so the provider can more easily see how their resource is used. By default it's the URL to this repo. | ||
It's a good idea to set the `'user-agent'` header so the provider can more easily see how their resource is used. By default, it's the URL to this repo. | ||
@@ -256,3 +272,3 @@ ```js | ||
## Node 0.10.x | ||
## Node.js 0.10.x | ||
@@ -259,0 +275,0 @@ It is a known issue with old good Node 0.10.x [`http.Agent`](https://nodejs.org/docs/v0.10.39/api/http.html#http_class_http_agent) and `agent.maxSockets`, which is set to `5`. This can cause low performance and in rare cases deadlocks. To avoid this you can set it manually: |
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
18631
278
285
9
+ Addedurl-parse-lax@^1.0.0
+ Addedobject-assign@4.1.1(transitive)
+ Addedurl-parse-lax@1.0.0(transitive)
- Removedprepend-http@^1.0.0
- Removedobject-assign@3.0.0(transitive)
Updatedobject-assign@^4.0.1