Skip to content

Commit

Permalink
Merge pull request #1377 from eromano/master-2
Browse files Browse the repository at this point in the history
Add upload timeout
  • Loading branch information
kornelski authored Jan 2, 2019
2 parents ad4c96f + e6630ec commit f3ac20c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,17 @@ Request.prototype.end = function(fn){
this._end();
};

Request.prototype._setUploadTimeout = function () {
const self = this;

// 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);
}
};

Request.prototype._end = function() {
if (this._aborted) return this.callback(Error("The request has been aborted even before .end() was called"));

Expand Down Expand Up @@ -709,9 +720,15 @@ Request.prototype._end = function() {

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

if (e.total > 0) {
e.percent = e.loaded / e.total * 100;

if(e.percent === 100) {
clearTimeout(self._uploadTimeoutTimer);
}
}

e.direction = direction;
self.emit('progress', e);
};
Expand All @@ -728,6 +745,10 @@ Request.prototype._end = function() {
}
}

if(xhr.upload){
this._setUploadTimeout();
}

// initiate request
try {
if (this.username && this.password) {
Expand Down
8 changes: 8 additions & 0 deletions lib/request-base.js
Original file line number Diff line number Diff line change
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 @@ -700,4 +707,5 @@ RequestBase.prototype._setTimeouts = function() {
self._timeoutError('Response timeout of ', self._responseTimeout, 'ETIMEDOUT');
}, this._responseTimeout);
}

};

0 comments on commit f3ac20c

Please sign in to comment.