wreck
Advanced tools
Comparing version 5.2.0 to 5.3.0
// Load modules | ||
var Events = require('events'); | ||
var Url = require('url'); | ||
@@ -20,11 +21,22 @@ var Http = require('http'); | ||
exports.agents = { | ||
https: new Https.Agent({ maxSockets: Infinity }), | ||
http: new Http.Agent({ maxSockets: Infinity }), | ||
httpsAllowUnauthorized: new Https.Agent({ maxSockets: Infinity, rejectUnauthorized: false }) | ||
// new instance is exported as module.exports | ||
internals.Client = function () { | ||
Events.EventEmitter.call(this); | ||
this.agents = { | ||
https: new Https.Agent({ maxSockets: Infinity }), | ||
http: new Http.Agent({ maxSockets: Infinity }), | ||
httpsAllowUnauthorized: new Https.Agent({ maxSockets: Infinity, rejectUnauthorized: false }) | ||
}; | ||
}; | ||
Hoek.inherits(internals.Client, Events.EventEmitter); | ||
exports.request = function (method, url, options, callback, _trace) { | ||
internals.Client.prototype.request = function (method, url, options, callback, _trace) { | ||
var self = this; | ||
options = options || {}; | ||
@@ -62,3 +74,3 @@ | ||
if (options.rejectUnauthorized !== undefined && uri.protocol === 'https:') { | ||
uri.agent = options.rejectUnauthorized ? exports.agents.https : exports.agents.httpsAllowUnauthorized; | ||
uri.agent = options.rejectUnauthorized ? this.agents.https : this.agents.httpsAllowUnauthorized; | ||
} | ||
@@ -69,3 +81,3 @@ else if (options.agent || options.agent === false) { | ||
else { | ||
uri.agent = uri.protocol === 'https:' ? exports.agents.https : exports.agents.http; | ||
uri.agent = uri.protocol === 'https:' ? this.agents.https : this.agents.http; | ||
} | ||
@@ -86,6 +98,2 @@ | ||
if (!callback || err) { | ||
if (res) { | ||
res.destroy(); | ||
} | ||
req.abort(); | ||
@@ -98,2 +106,3 @@ } | ||
clearTimeout(timeoutId); | ||
self.emit('response', err, req, res, _trace); | ||
@@ -150,3 +159,3 @@ if (callback) { | ||
return exports.request(redirectMethod, location, redirectOptions, finish, _trace); | ||
return self.request(redirectMethod, location, redirectOptions, finish, _trace); | ||
}; | ||
@@ -186,2 +195,23 @@ | ||
// Custom abort method to detect early aborts | ||
var _abort = req.abort; | ||
var aborted = false; | ||
req.abort = function () { | ||
if (!aborted && !req.res && !req.socket) { | ||
process.nextTick(function () { | ||
// Fake an ECONNRESET error | ||
var error = new Error('socket hang up'); | ||
error.code = 'ECONNRESET'; | ||
finish(error); | ||
}); | ||
} | ||
aborted = true; | ||
return _abort.call(req); | ||
}; | ||
// Finalize request | ||
@@ -197,3 +227,3 @@ | ||
exports.read = function (res, options, callback) { | ||
internals.Client.prototype.read = function (res, options, callback) { | ||
@@ -298,3 +328,3 @@ options = options || {}; | ||
exports.toReadableStream = function (payload, encoding) { | ||
internals.Client.prototype.toReadableStream = function (payload, encoding) { | ||
@@ -307,3 +337,3 @@ return new Payload(payload, encoding); | ||
exports.parseCacheControl = function (field) { | ||
internals.Client.prototype.parseCacheControl = function (field) { | ||
@@ -346,23 +376,23 @@ /* | ||
exports.get = function (uri, options, callback) { | ||
internals.Client.prototype.get = function (uri, options, callback) { | ||
return internals.shortcutWrap('GET', uri, options, callback); | ||
return this._shortcutWrap('GET', uri, options, callback); | ||
}; | ||
exports.post = function (uri, options, callback) { | ||
internals.Client.prototype.post = function (uri, options, callback) { | ||
return internals.shortcutWrap('POST', uri, options, callback); | ||
return this._shortcutWrap('POST', uri, options, callback); | ||
}; | ||
exports.put = function (uri, options, callback) { | ||
internals.Client.prototype.put = function (uri, options, callback) { | ||
return internals.shortcutWrap('PUT', uri, options, callback); | ||
return this._shortcutWrap('PUT', uri, options, callback); | ||
}; | ||
exports.delete = function (uri, options, callback) { | ||
internals.Client.prototype.delete = function (uri, options, callback) { | ||
return internals.shortcutWrap('DELETE', uri, options, callback); | ||
return this._shortcutWrap('DELETE', uri, options, callback); | ||
}; | ||
@@ -372,3 +402,3 @@ | ||
// Wrapper so that shortcut can be optimized with required params | ||
internals.shortcutWrap = function (method, uri /* [options], callback */) { | ||
internals.Client.prototype._shortcutWrap = function (method, uri /* [options], callback */) { | ||
@@ -378,10 +408,12 @@ var options = (typeof arguments[2] === 'function' ? {} : arguments[2]); | ||
return internals.shortcut(method, uri, options, callback); | ||
return this._shortcut(method, uri, options, callback); | ||
}; | ||
internals.shortcut = function (method, uri, options, callback) { | ||
internals.Client.prototype._shortcut = function (method, uri, options, callback) { | ||
return exports.request(method, uri, options, function (err, res) { | ||
var self = this; | ||
return this.request(method, uri, options, function (err, res) { | ||
if (err) { | ||
@@ -391,3 +423,3 @@ return callback(err); | ||
exports.read(res, options, function (err, payload) { | ||
self.read(res, options, function (err, payload) { | ||
@@ -419,1 +451,4 @@ if (payload instanceof Buffer) { | ||
}; | ||
module.exports = new internals.Client(); |
{ | ||
"name": "wreck", | ||
"description": "HTTP Client Utilities", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"repository": "git://github.com/hapijs/wreck", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -7,5 +7,6 @@ ![wreck Logo](https://raw.github.com/hapijs/wreck/master/images/wreck.png) | ||
Lead Maintainer: [Wyatt Preul](https://github.com/wpreul) | ||
Lead Maintainer: [Wyatt Preul](https://github.com/geek) | ||
## Usage | ||
### Basic | ||
@@ -54,3 +55,2 @@ ```javascript | ||
### `request(method, uri, [options, [callback]])` | ||
@@ -190,4 +190,4 @@ | ||
`httpsAllowUnauthorized` which is an `https` agent with `rejectUnauthorized` set to true. All agents have `maxSockets` | ||
configured to `Infinity`. They are each instances of the node.js [Agent](http://nodejs.org/api/http.html#http_class_http_agent) | ||
and expose the standard properties. | ||
configured to `Infinity`. They are each instances of the node.js | ||
[Agent](http://nodejs.org/api/http.html#http_class_http_agent) and expose the standard properties. | ||
@@ -201,1 +201,12 @@ For example, the following code demonstrates changing `maxSockets` on the `http` agent. | ||
``` | ||
### Events | ||
#### `response` | ||
The response event is always emitted for any request that *wreck* makes. The handler should accept the following | ||
arguments `(error, request, response)`. This event is useful for logging all requests that go through *wreck*. The | ||
error and response arguments can be undefined depending on if an error occurs. Please be aware that if multiple | ||
modules are depending on the same cached *wreck* module that this event can fire for each request made across all | ||
modules. |
@@ -214,3 +214,3 @@ // Load modules | ||
Wreck.request('get', 'https://google.com', { rejectUnauthorized: true, secureProtocol: 'SSLv3_method' }, function (err, res) { | ||
Wreck.request('get', 'https://google.com', { rejectUnauthorized: true, secureProtocol: 'SSLv23_method' }, function (err, res) { | ||
@@ -671,2 +671,24 @@ expect(err).to.not.exist(); | ||
it('in-progress requests can be aborted', function (done) { | ||
var wreck; | ||
var server = Http.createServer(function (req, res) { | ||
res.writeHead(200); | ||
res.end(); | ||
wreck.abort(); | ||
}); | ||
server.listen(0, function () { | ||
wreck = Wreck.request('get', 'http://localhost:' + server.address().port, {}, function (err) { | ||
expect(err).to.exist(); | ||
expect(err.code).to.equal('ECONNRESET'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('uses agent option', function (done) { | ||
@@ -1502,1 +1524,75 @@ | ||
}); | ||
describe('Events', function () { | ||
it('emits response event when wreck is finished', function (done) { | ||
Wreck.once('response', function (err, req, res) { | ||
expect(err).to.not.exist(); | ||
expect(req).to.exist(); | ||
expect(res).to.exist(); | ||
done(); | ||
}); | ||
var server = Http.createServer(function (req, res) { | ||
res.writeHead(200); | ||
res.end('ok'); | ||
}); | ||
server.listen(0, function () { | ||
Wreck.put('http://localhost:' + server.address().port, function (err, res, payload) { | ||
expect(err).to.not.exist(); | ||
expect(res.statusCode).to.equal(200); | ||
expect(payload).to.equal('ok'); | ||
server.close(); | ||
}); | ||
}); | ||
}); | ||
it('response event includes error when it occurs', function (done) { | ||
Wreck.once('response', function (err, req, res) { | ||
expect(err).to.exist(); | ||
expect(req).to.exist(); | ||
expect(res).to.not.exist(); | ||
done(); | ||
}); | ||
Wreck.get('http://0', function (err) { | ||
expect(err).to.exist(); | ||
}); | ||
}); | ||
it('multiple requests execute the same response handler', function (done) { | ||
var count = 0; | ||
var handler = function (err, req, res) { | ||
expect(err).to.exist(); | ||
expect(req).to.exist(); | ||
expect(res).to.not.exist(); | ||
count++; | ||
}; | ||
Wreck.on('response', handler); | ||
Wreck.get('http://0', function (err) { | ||
expect(err).to.exist(); | ||
Wreck.get('http://0', function (err) { | ||
expect(err).to.exist(); | ||
expect(count).to.equal(2); | ||
Wreck.removeListener('response', handler); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
108065
1496
209