superagent
Advanced tools
Comparing version 0.9.10 to 0.10.0
@@ -736,3 +736,3 @@ ;(function(){ | ||
this.on('end', function(){ | ||
self.callback(new Response(self.xhr)); | ||
self.callback(null, new Response(self.xhr)); | ||
}); | ||
@@ -749,4 +749,30 @@ } | ||
/** | ||
* Abort the request. | ||
* Set timeout to `ms`. | ||
* | ||
* @param {Number} ms | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
Request.prototype.timeout = function(ms){ | ||
this._timeout = ms; | ||
return this; | ||
}; | ||
/** | ||
* Clear previous timeout. | ||
* | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
Request.prototype.clearTimeout = function(){ | ||
this._timeout = 0; | ||
clearTimeout(this._timer); | ||
return this; | ||
}; | ||
/** | ||
* Abort the request, and clear potential timeout. | ||
* | ||
* @return {Request} | ||
@@ -761,2 +787,3 @@ * @api public | ||
this.aborted = true; | ||
this.clearTimeout(); | ||
return this; | ||
@@ -923,2 +950,18 @@ }; | ||
/** | ||
* Invoke the callback with `err` and `res` | ||
* and handle arity check. | ||
* | ||
* @param {Error} err | ||
* @param {Response} res | ||
* @api private | ||
*/ | ||
Request.prototype.callback = function(err, res){ | ||
var fn = this._callback; | ||
if (2 == fn.length) return fn(err, res); | ||
if (err) return this.emit('error', err); | ||
fn(res); | ||
}; | ||
/** | ||
* Initiate request, invoking callback `fn(res)` | ||
@@ -936,12 +979,23 @@ * with an instanceof `Response`. | ||
var query = this._query.join('&'); | ||
var timeout = this._timeout; | ||
var data = this._data; | ||
// store callback | ||
this.callback = fn || noop; | ||
this._callback = fn || noop; | ||
// state change | ||
xhr.onreadystatechange = function(){ | ||
if (4 == xhr.readyState) self.emit('end'); | ||
if (4 == xhr.readyState && 0 != xhr.status) self.emit('end'); | ||
}; | ||
// timeout | ||
if (timeout && !this._timer) { | ||
this._timer = setTimeout(function(){ | ||
var err = new Error('timeout of ' + timeout + 'ms exceeded'); | ||
err.timeout = timeout; | ||
self.callback(err); | ||
self.abort(); | ||
}, timeout); | ||
} | ||
// querystring | ||
@@ -948,0 +1002,0 @@ if (query) { |
@@ -5,3 +5,3 @@ { | ||
"description": "awesome http requests", | ||
"version": "0.9.10", | ||
"version": "0.10.0", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "http", |
0.10.0 / 2012-11-14 | ||
================== | ||
* add req.timeout(ms) support for the client | ||
0.9.10 / 2012-11-14 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -398,3 +398,3 @@ | ||
this.on('end', function(){ | ||
self.callback(new Response(self.xhr)); | ||
self.callback(null, new Response(self.xhr)); | ||
}); | ||
@@ -411,4 +411,30 @@ } | ||
/** | ||
* Abort the request. | ||
* Set timeout to `ms`. | ||
* | ||
* @param {Number} ms | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
Request.prototype.timeout = function(ms){ | ||
this._timeout = ms; | ||
return this; | ||
}; | ||
/** | ||
* Clear previous timeout. | ||
* | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
Request.prototype.clearTimeout = function(){ | ||
this._timeout = 0; | ||
clearTimeout(this._timer); | ||
return this; | ||
}; | ||
/** | ||
* Abort the request, and clear potential timeout. | ||
* | ||
* @return {Request} | ||
@@ -423,2 +449,3 @@ * @api public | ||
this.aborted = true; | ||
this.clearTimeout(); | ||
return this; | ||
@@ -585,2 +612,18 @@ }; | ||
/** | ||
* Invoke the callback with `err` and `res` | ||
* and handle arity check. | ||
* | ||
* @param {Error} err | ||
* @param {Response} res | ||
* @api private | ||
*/ | ||
Request.prototype.callback = function(err, res){ | ||
var fn = this._callback; | ||
if (2 == fn.length) return fn(err, res); | ||
if (err) return this.emit('error', err); | ||
fn(res); | ||
}; | ||
/** | ||
* Initiate request, invoking callback `fn(res)` | ||
@@ -598,12 +641,23 @@ * with an instanceof `Response`. | ||
var query = this._query.join('&'); | ||
var timeout = this._timeout; | ||
var data = this._data; | ||
// store callback | ||
this.callback = fn || noop; | ||
this._callback = fn || noop; | ||
// state change | ||
xhr.onreadystatechange = function(){ | ||
if (4 == xhr.readyState) self.emit('end'); | ||
if (4 == xhr.readyState && 0 != xhr.status) self.emit('end'); | ||
}; | ||
// timeout | ||
if (timeout && !this._timer) { | ||
this._timer = setTimeout(function(){ | ||
var err = new Error('timeout of ' + timeout + 'ms exceeded'); | ||
err.timeout = timeout; | ||
self.callback(err); | ||
self.abort(); | ||
}, timeout); | ||
} | ||
// querystring | ||
@@ -610,0 +664,0 @@ if (query) { |
@@ -572,10 +572,4 @@ /*! | ||
var fn = this._callback; | ||
// flag as finished so that | ||
// timeouts may be ignored | ||
this._finished = true; | ||
// invoke callback | ||
this.clearTimeout(); | ||
if (2 == fn.length) return fn(err, res); | ||
if (err) return this.emit('error', err); | ||
@@ -608,8 +602,7 @@ fn(res); | ||
this._timer = setTimeout(function(){ | ||
if (self._finished) return; | ||
var err = new Error('timeout of ' + timeout + 'ms exceeded'); | ||
err.timeout = timeout; | ||
self.callback(err); | ||
self._aborted = true; | ||
req.abort(); | ||
self.callback(err); | ||
}, timeout); | ||
@@ -616,0 +609,0 @@ } |
{ | ||
"name": "superagent", | ||
"version": "0.9.10", | ||
"version": "0.10.0", | ||
"description": "elegant & feature rich browser / node HTTP with a fluent API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
90608
3058