Skip to content

Commit

Permalink
add upload timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
eromano committed May 17, 2018
1 parent 0f90861 commit 650b521
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/client.js
Expand Up @@ -709,6 +709,8 @@ Request.prototype._end = function() {

// progress
const handleProgress = (direction, e) => {

clearTimeout(self._uploadTimeoutTimer);
if (e.total > 0) {
e.percent = e.loaded / e.total * 100;
}
Expand All @@ -728,6 +730,15 @@ Request.prototype._end = function() {
}
}

if(xhr.upload){
// upload timeout it's wokrs only if deadline timeout is off
if (this._uploadTimeout && !this._uploadTimeoutTimer) {
this._uploadTimeoutTimer = setTimeout(() => {
self._timeoutError('Upload timeout of ', self._uploadTimeout, 'ETIMEDOUT');
}, this._uploadTimeout);
}
}

// initiate request
try {
if (this.username && this.password) {
Expand Down
8 changes: 8 additions & 0 deletions lib/request-base.js
Expand Up @@ -46,8 +46,10 @@ function mixin(obj) {
RequestBase.prototype.clearTimeout = function _clearTimeout(){
clearTimeout(this._timer);
clearTimeout(this._responseTimeoutTimer);
clearTimeout(this._uploadTimeoutTimer);
delete this._timer;
delete this._responseTimeoutTimer;
delete this._uploadTimeoutTimer;
return this;
};

Expand Down Expand Up @@ -107,6 +109,7 @@ RequestBase.prototype.serialize = function serialize(fn){
*
* - response timeout is time between sending request and receiving the first byte of the response. Includes DNS and connection time.
* - deadline is the time from start of the request to receiving response body in full. If the deadline is too short large files may not load at all on slow connections.
* - upload is the time since last bit of data was sent or received. This timeout works only if deadline timeout is off
*
* Value of 0 or false means no timeout.
*
Expand All @@ -119,6 +122,7 @@ RequestBase.prototype.timeout = function timeout(options){
if (!options || 'object' !== typeof options) {
this._timeout = options;
this._responseTimeout = 0;
this._uploadTimeout = 0;
return this;
}

Expand All @@ -130,6 +134,9 @@ RequestBase.prototype.timeout = function timeout(options){
case 'response':
this._responseTimeout = options.response;
break;
case 'upload':
this._uploadTimeout = options.upload;
break;
default:
console.warn("Unknown timeout option", option);
}
Expand Down Expand Up @@ -692,4 +699,5 @@ RequestBase.prototype._setTimeouts = function() {
self._timeoutError('Response timeout of ', self._responseTimeout, 'ETIMEDOUT');
}, this._responseTimeout);
}

};

0 comments on commit 650b521

Please sign in to comment.