Comparing version 3.3.1 to 4.0.0
396
index.js
'use strict'; | ||
var EventEmitter = require('events').EventEmitter; | ||
var http = require('http'); | ||
var https = require('https'); | ||
var urlLib = require('url'); | ||
var util = require('util'); | ||
var zlib = require('zlib'); | ||
var querystring = require('querystring'); | ||
var objectAssign = require('object-assign'); | ||
var infinityAgent = require('infinity-agent'); | ||
var duplexify = require('duplexify'); | ||
@@ -17,244 +15,219 @@ var isStream = require('is-stream'); | ||
var isRedirect = require('is-redirect'); | ||
var NestedErrorStacks = require('nested-error-stacks'); | ||
var pinkiePromise = require('pinkie-promise'); | ||
var unzipResponse = require('unzip-response'); | ||
var createErrorClass = require('create-error-class'); | ||
function GotError(message, nested) { | ||
NestedErrorStacks.call(this, message, nested); | ||
objectAssign(this, nested, {nested: this.nested}); | ||
} | ||
function requestAsEventEmitter(opts) { | ||
opts = opts || {}; | ||
util.inherits(GotError, NestedErrorStacks); | ||
GotError.prototype.name = 'GotError'; | ||
var ee = new EventEmitter(); | ||
var redirectCount = 0; | ||
function got(url, opts, cb) { | ||
if (typeof url !== 'string' && typeof url !== 'object') { | ||
throw new GotError('Parameter `url` must be a string or object, not ' + typeof url); | ||
} | ||
var get = function (opts) { | ||
var fn = opts.protocol === 'https:' ? https : http; | ||
if (typeof opts === 'function') { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
var req = fn.request(opts, function (res) { | ||
var statusCode = res.statusCode; | ||
if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) { | ||
res.resume(); | ||
opts = objectAssign( | ||
{ | ||
protocol: 'http:' | ||
}, | ||
typeof url === 'string' ? urlLib.parse(prependHttp(url)) : url, | ||
opts | ||
); | ||
if (++redirectCount > 10) { | ||
ee.emit('error', new got.MaxRedirectsError(statusCode, opts), null, res); | ||
return; | ||
} | ||
opts.headers = objectAssign({ | ||
'user-agent': 'https://github.com/sindresorhus/got', | ||
'accept-encoding': 'gzip,deflate' | ||
}, lowercaseKeys(opts.headers)); | ||
var redirectUrl = urlLib.resolve(urlLib.format(opts), res.headers.location); | ||
var redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl)); | ||
if (opts.pathname) { | ||
opts.path = opts.pathname; | ||
} | ||
ee.emit('redirect', res, redirectOpts); | ||
if (opts.query) { | ||
if (typeof opts.query !== 'string') { | ||
opts.query = querystring.stringify(opts.query); | ||
get(redirectOpts); | ||
return; | ||
} | ||
ee.emit('response', unzipResponse(res)); | ||
}).once('error', function (err) { | ||
ee.emit('error', new got.RequestError(err, opts)); | ||
}); | ||
if (opts.timeout) { | ||
timedOut(req, opts.timeout); | ||
} | ||
opts.path = opts.pathname + '?' + opts.query; | ||
delete opts.query; | ||
} | ||
setImmediate(ee.emit.bind(ee), 'request', req); | ||
}; | ||
var encoding = opts.encoding; | ||
var body = opts.body; | ||
var json = opts.json; | ||
var timeout = opts.timeout; | ||
var proxy; | ||
var redirectCount = 0; | ||
get(opts); | ||
return ee; | ||
} | ||
delete opts.encoding; | ||
delete opts.body; | ||
delete opts.json; | ||
delete opts.timeout; | ||
function asCallback(opts, cb) { | ||
var ee = requestAsEventEmitter(opts); | ||
if (json) { | ||
opts.headers.accept = opts.headers.accept || 'application/json'; | ||
} | ||
if (body) { | ||
if (typeof body !== 'string' && !Buffer.isBuffer(body) && !isStream.readable(body)) { | ||
throw new GotError('options.body must be a ReadableStream, string or Buffer'); | ||
ee.on('request', function (req) { | ||
if (isStream.readable(opts.body)) { | ||
opts.body.pipe(req); | ||
opts.body = undefined; | ||
return; | ||
} | ||
opts.method = opts.method || 'POST'; | ||
req.end(opts.body); | ||
}); | ||
if (!opts.headers['content-length'] && !opts.headers['transfer-encoding'] && !isStream.readable(body)) { | ||
var length = typeof body === 'string' ? Buffer.byteLength(body) : body.length; | ||
opts.headers['content-length'] = length; | ||
} | ||
} | ||
ee.on('response', function (res) { | ||
readAllStream(res, opts.encoding, function (err, data) { | ||
if (err) { | ||
cb(new got.ReadError(err, opts), null, res); | ||
return; | ||
} | ||
opts.method = opts.method || 'GET'; | ||
var statusCode = res.statusCode; | ||
// returns a proxy stream to the response | ||
// if no callback has been provided | ||
if (!cb) { | ||
proxy = duplexify(); | ||
if (statusCode < 200 || statusCode > 299) { | ||
err = new got.HTTPError(statusCode, opts); | ||
} | ||
// forward errors on the stream | ||
cb = function (err, data, response) { | ||
proxy.emit('error', err, data, response); | ||
}; | ||
} | ||
if (opts.json && statusCode !== 204) { | ||
try { | ||
data = JSON.parse(data); | ||
} catch (e) { | ||
err = new got.ParseError(e, opts); | ||
} | ||
} | ||
if (proxy && json) { | ||
throw new GotError('got can not be used as stream when options.json is used'); | ||
} | ||
cb(err, data, res); | ||
}); | ||
}); | ||
function get(opts, cb) { | ||
var fn = opts.protocol === 'https:' ? https : http; | ||
var url = urlLib.format(opts); | ||
ee.on('error', cb); | ||
} | ||
if (opts.agent === undefined) { | ||
opts.agent = infinityAgent[fn === https ? 'https' : 'http'].globalAgent; | ||
function asPromise(opts) { | ||
return new pinkiePromise(function (resolve, reject) { | ||
asCallback(opts, function (err, data, response) { | ||
response.body = data; | ||
if (process.version.indexOf('v0.10') === 0 && fn === https && ( | ||
typeof opts.ca !== 'undefined' || | ||
typeof opts.cert !== 'undefined' || | ||
typeof opts.ciphers !== 'undefined' || | ||
typeof opts.key !== 'undefined' || | ||
typeof opts.passphrase !== 'undefined' || | ||
typeof opts.pfx !== 'undefined' || | ||
typeof opts.rejectUnauthorized !== 'undefined')) { | ||
opts.agent = new infinityAgent.https.Agent({ | ||
ca: opts.ca, | ||
cert: opts.cert, | ||
ciphers: opts.ciphers, | ||
key: opts.key, | ||
passphrase: opts.passphrase, | ||
pfx: opts.pfx, | ||
rejectUnauthorized: opts.rejectUnauthorized | ||
}); | ||
if (err) { | ||
err.response = response; | ||
reject(err); | ||
return; | ||
} | ||
} | ||
var req = fn.request(opts, function (response) { | ||
var statusCode = response.statusCode; | ||
var res = response; | ||
resolve(response); | ||
}); | ||
}); | ||
} | ||
// auto-redirect only for GET and HEAD methods | ||
if (isRedirect(statusCode) && 'location' in res.headers && (opts.method === 'GET' || opts.method === 'HEAD')) { | ||
// discard response | ||
res.resume(); | ||
function asStream(opts) { | ||
var proxy = duplexify(); | ||
if (++redirectCount > 10) { | ||
cb(new GotError('Redirected 10 times. Aborting.'), undefined, res); | ||
return; | ||
} | ||
if (opts.json) { | ||
throw new Error('got can not be used as stream when options.json is used'); | ||
} | ||
var redirectUrl = urlLib.resolve(url, res.headers.location); | ||
var redirectOpts = objectAssign({}, opts, urlLib.parse(redirectUrl)); | ||
if (opts.body) { | ||
proxy.write = function () { | ||
throw new Error('got\'s stream is not writable when options.body is used'); | ||
}; | ||
} | ||
if (opts.agent === infinityAgent.http.globalAgent && redirectOpts.protocol === 'https:' && opts.protocol === 'http:') { | ||
redirectOpts.agent = undefined; | ||
} | ||
var ee = requestAsEventEmitter(opts); | ||
if (proxy) { | ||
proxy.emit('redirect', res, redirectOpts); | ||
} | ||
ee.on('request', function (req) { | ||
proxy.emit('request', req); | ||
get(redirectOpts, cb); | ||
return; | ||
} | ||
if (isStream.readable(opts.body)) { | ||
opts.body.pipe(req); | ||
return; | ||
} | ||
if (proxy) { | ||
proxy.emit('response', res); | ||
} | ||
if (opts.body) { | ||
req.end(opts.body); | ||
return; | ||
} | ||
if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) { | ||
res = res.pipe(zlib.createUnzip()); | ||
} | ||
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') { | ||
proxy.setWritable(req); | ||
return; | ||
} | ||
if (statusCode < 200 || statusCode > 299) { | ||
readAllStream(res, encoding, function (err, data) { | ||
err = new GotError(opts.method + ' ' + url + ' response code is ' + statusCode + ' (' + http.STATUS_CODES[statusCode] + ')', err); | ||
err.code = statusCode; | ||
req.end(); | ||
}); | ||
if (data && json) { | ||
try { | ||
data = JSON.parse(data); | ||
} catch (e) { | ||
err = new GotError('Parsing ' + url + ' response failed', new GotError(e.message, err)); | ||
} | ||
} | ||
ee.on('response', function (res) { | ||
proxy.setReadable(res); | ||
proxy.emit('response', res); | ||
}); | ||
cb(err, data, response); | ||
}); | ||
ee.on('redirect', proxy.emit.bind(proxy, 'redirect')); | ||
return; | ||
} | ||
return proxy; | ||
} | ||
// pipe the response to the proxy if in proxy mode | ||
if (proxy) { | ||
proxy.setReadable(res); | ||
return; | ||
} | ||
function normalizeArguments(url, opts) { | ||
if (typeof url !== 'string' && typeof url !== 'object') { | ||
throw new Error('Parameter `url` must be a string or object, not ' + typeof url); | ||
} | ||
readAllStream(res, encoding, function (err, data) { | ||
if (err) { | ||
err = new GotError('Reading ' + url + ' response failed', err); | ||
} else if (json && statusCode !== 204) { | ||
// only parse json if the option is enabled, and the response | ||
// is not a 204 (empty reponse) | ||
try { | ||
data = JSON.parse(data); | ||
} catch (e) { | ||
err = new GotError('Parsing ' + url + ' response failed', e); | ||
} | ||
} | ||
opts = objectAssign( | ||
{protocol: 'http:', path: ''}, | ||
typeof url === 'string' ? urlLib.parse(prependHttp(url)) : url, | ||
opts | ||
); | ||
cb(err, data, response); | ||
}); | ||
}).once('error', function (err) { | ||
cb(new GotError('Request to ' + url + ' failed', err)); | ||
}); | ||
opts.headers = objectAssign({ | ||
'user-agent': 'https://github.com/sindresorhus/got', | ||
'accept-encoding': 'gzip,deflate' | ||
}, lowercaseKeys(opts.headers)); | ||
if (timeout) { | ||
timedOut(req, timeout); | ||
var query = opts.query; | ||
if (query) { | ||
if (typeof query !== 'string') { | ||
opts.query = querystring.stringify(query); | ||
} | ||
if (!proxy) { | ||
if (isStream.readable(body)) { | ||
body.pipe(req); | ||
} else { | ||
req.end(body); | ||
} | ||
opts.path = opts.path.split('?')[0] + '?' + opts.query; | ||
delete opts.query; | ||
} | ||
return; | ||
if (opts.json) { | ||
opts.headers.accept = opts.headers.accept || 'application/json'; | ||
} | ||
var body = opts.body; | ||
if (body) { | ||
if (typeof body !== 'string' && !Buffer.isBuffer(body) && !isStream.readable(body)) { | ||
throw new Error('options.body must be a ReadableStream, string or Buffer'); | ||
} | ||
if (body) { | ||
proxy.write = function () { | ||
throw new Error('got\'s stream is not writable when options.body is used'); | ||
}; | ||
opts.method = opts.method || 'POST'; | ||
if (isStream.readable(body)) { | ||
body.pipe(req); | ||
} else { | ||
req.end(body); | ||
} | ||
return; | ||
if (!opts.headers['content-length'] && !opts.headers['transfer-encoding'] && !isStream.readable(body)) { | ||
var length = typeof body === 'string' ? Buffer.byteLength(body) : body.length; | ||
opts.headers['content-length'] = length; | ||
} | ||
} | ||
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') { | ||
proxy.setWritable(req); | ||
return; | ||
} | ||
opts.method = opts.method || 'GET'; | ||
req.end(); | ||
return opts; | ||
} | ||
function got(url, opts, cb) { | ||
if (typeof opts === 'function') { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
get(opts, cb); | ||
opts = normalizeArguments(url, opts); | ||
return proxy; | ||
if (cb) { | ||
asCallback(opts, cb); | ||
return; | ||
} | ||
return asPromise(opts); | ||
} | ||
[ | ||
var helpers = [ | ||
'get', | ||
@@ -266,3 +239,5 @@ 'post', | ||
'delete' | ||
].forEach(function (el) { | ||
]; | ||
helpers.forEach(function (el) { | ||
got[el] = function (url, opts, cb) { | ||
@@ -278,2 +253,41 @@ if (typeof opts === 'function') { | ||
got.stream = function (url, opts) { | ||
return asStream(normalizeArguments(url, opts)); | ||
}; | ||
helpers.forEach(function (el) { | ||
got.stream[el] = function (url, opts) { | ||
return got.stream(url, objectAssign({}, opts, {method: el.toUpperCase()})); | ||
}; | ||
}); | ||
function stdError(error, opts) { | ||
objectAssign(this, { | ||
message: error.message, | ||
code: error.code, | ||
host: opts.host, | ||
hostname: opts.hostname, | ||
method: opts.method, | ||
path: opts.path | ||
}); | ||
} | ||
got.RequestError = createErrorClass('RequestError', stdError); | ||
got.ReadError = createErrorClass('ReadError', stdError); | ||
got.ParseError = createErrorClass('ParseError', stdError); | ||
got.HTTPError = createErrorClass('HTTPError', function (statusCode, opts) { | ||
stdError.call(this, {}, opts); | ||
this.statusCode = statusCode; | ||
this.statusMessage = http.STATUS_CODES[this.statusCode]; | ||
this.message = 'Response code ' + this.statusCode + ' (' + this.statusMessage + ')'; | ||
}); | ||
got.MaxRedirectsError = createErrorClass('MaxRedirectsError', function (statusCode, opts) { | ||
stdError.call(this, {}, opts); | ||
this.statusCode = statusCode; | ||
this.statusMessage = http.STATUS_CODES[this.statusCode]; | ||
this.message = 'Redirected 10 times. Aborting.'; | ||
}); | ||
module.exports = got; |
{ | ||
"name": "got", | ||
"version": "3.3.1", | ||
"version": "4.0.0", | ||
"description": "Simplified HTTP/HTTPS requests", | ||
@@ -23,4 +23,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "tap test/test-*.js", | ||
"coverage": "istanbul cover tape --report html -- test/test-*.js" | ||
"test": "tap test/test-*.js" | ||
}, | ||
@@ -46,12 +45,13 @@ "files": [ | ||
"dependencies": { | ||
"create-error-class": "^2.0.0", | ||
"duplexify": "^3.2.0", | ||
"infinity-agent": "^2.0.0", | ||
"is-redirect": "^1.0.0", | ||
"is-stream": "^1.0.0", | ||
"lowercase-keys": "^1.0.0", | ||
"nested-error-stacks": "^1.0.0", | ||
"object-assign": "^3.0.0", | ||
"pinkie-promise": "^1.0.0", | ||
"prepend-http": "^1.0.0", | ||
"read-all-stream": "^3.0.0", | ||
"timed-out": "^2.0.0" | ||
"timed-out": "^2.0.0", | ||
"unzip-response": "^1.0.0" | ||
}, | ||
@@ -58,0 +58,0 @@ "devDependencies": { |
@@ -15,7 +15,6 @@ <h1 align="center"> | ||
It supports following redirects, streams, automagically handling gzip/deflate and some convenience options. | ||
It supports following redirects, Promises, streams, automagically handling gzip/deflate and some convenience options. | ||
Created because [`request`](https://github.com/mikeal/request) is bloated *(several megabytes!)* and slow. | ||
Created because [`request`](https://github.com/mikeal/request) is bloated *(several megabytes!)*. | ||
## Install | ||
@@ -39,8 +38,14 @@ | ||
// Promise mode | ||
got('todomvc.com') | ||
.then(function (res) { | ||
console.log(res.body); | ||
}) | ||
.catch(console.error); | ||
// Stream mode | ||
got('todomvc.com').pipe(fs.createWriteStream('index.html')); | ||
got.stream('todomvc.com').pipe(fs.createWriteStream('index.html')); | ||
// For POST, PUT and PATCH methods got returns a WritableStream | ||
fs.createReadStream('index.html').pipe(got.post('todomvc.com')); | ||
// For POST, PUT and PATCH methods got.stream returns a WritableStream | ||
fs.createReadStream('index.html').pipe(got.stream.post('todomvc.com')); | ||
``` | ||
@@ -107,12 +112,6 @@ | ||
###### agent | ||
##### callback(error, data, response) | ||
[http.Agent](http://nodejs.org/api/http.html#http_class_http_agent) instance. | ||
Function to be called, when error or data recieved. If omitted - Promise will be returned. | ||
If `undefined` - [`infinity-agent`](https://github.com/floatdrop/infinity-agent) will be used to backport Agent class from Node.js core. | ||
To use default [globalAgent](http://nodejs.org/api/http.html#http_http_globalagent) just pass `null`. | ||
##### callback(error, data, response) | ||
###### error | ||
@@ -132,2 +131,6 @@ | ||
##### .on('request', request) | ||
`request` event to get the request object of the request. | ||
##### .on('response', response) | ||
@@ -145,6 +148,3 @@ | ||
###### response | ||
The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage). | ||
#### got.get(url, [options], [callback]) | ||
@@ -159,3 +159,26 @@ #### got.post(url, [options], [callback]) | ||
## Errors | ||
Each Error contains (if available) `host`, `hostname`, `method` and `path` properties to make debug easier. | ||
#### got.RequestError | ||
Happens, when making request failed. Should contain `code` property with error class code (like `ECONNREFUSED`). | ||
#### got.ReadError | ||
Happens, when reading from response stream failed. | ||
#### got.ParseError | ||
Happens, when `json` option is enabled and `JSON.parse` failed. | ||
#### got.HTTPError | ||
Happens, when server response code is not 2xx. Contains `statusCode` and `statusMessage`. | ||
#### got.MaxRedirectsError | ||
Happens, when server redirects you more than 10 times. | ||
## Proxy | ||
@@ -194,8 +217,18 @@ | ||
## Node 0.10.x | ||
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 of application and (in rare cases) deadlocks. To avoid this you can set it manually: | ||
```js | ||
require('http').globalAgent.maxSockets = Infinity; | ||
require('https').globalAgent.maxSockets = Infinity; | ||
``` | ||
This should only ever be done if you have Node version 0.10.x and at the top-level application layer. | ||
## Related | ||
- [gh-got](https://github.com/sindresorhus/gh-got) - Convenience wrapper for interacting with the GitHub API | ||
- [got-promise](https://github.com/floatdrop/got-promise) - Promise wrapper | ||
## Created by | ||
@@ -202,0 +235,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
14209
229
237
11
3
+ Addedcreate-error-class@^2.0.0
+ Addedpinkie-promise@^1.0.0
+ Addedunzip-response@^1.0.0
+ Addedcapture-stack-trace@1.0.2(transitive)
+ Addedcreate-error-class@2.0.1(transitive)
+ Addedpinkie@1.0.0(transitive)
+ Addedpinkie-promise@1.0.0(transitive)
+ Addedunzip-response@1.0.2(transitive)
- Removedinfinity-agent@^2.0.0
- Removednested-error-stacks@^1.0.0
- Removedinfinity-agent@2.0.3(transitive)
- Removednested-error-stacks@1.0.2(transitive)