Skip to content

Commit f3ac20c

Browse files
authored
Merge pull request #1377 from eromano/master-2
Add upload timeout
2 parents ad4c96f + e6630ec commit f3ac20c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/client.js

+21
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,17 @@ Request.prototype.end = function(fn){
676676
this._end();
677677
};
678678

679+
Request.prototype._setUploadTimeout = function () {
680+
const self = this;
681+
682+
// upload timeout it's wokrs only if deadline timeout is off
683+
if (this._uploadTimeout && !this._uploadTimeoutTimer) {
684+
this._uploadTimeoutTimer = setTimeout(() => {
685+
self._timeoutError('Upload timeout of ', self._uploadTimeout, 'ETIMEDOUT');
686+
}, this._uploadTimeout);
687+
}
688+
};
689+
679690
Request.prototype._end = function() {
680691
if (this._aborted) return this.callback(Error("The request has been aborted even before .end() was called"));
681692

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

710721
// progress
711722
const handleProgress = (direction, e) => {
723+
712724
if (e.total > 0) {
713725
e.percent = e.loaded / e.total * 100;
726+
727+
if(e.percent === 100) {
728+
clearTimeout(self._uploadTimeoutTimer);
729+
}
714730
}
731+
715732
e.direction = direction;
716733
self.emit('progress', e);
717734
};
@@ -728,6 +745,10 @@ Request.prototype._end = function() {
728745
}
729746
}
730747

748+
if(xhr.upload){
749+
this._setUploadTimeout();
750+
}
751+
731752
// initiate request
732753
try {
733754
if (this.username && this.password) {

lib/request-base.js

+8
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ function mixin(obj) {
4646
RequestBase.prototype.clearTimeout = function _clearTimeout(){
4747
clearTimeout(this._timer);
4848
clearTimeout(this._responseTimeoutTimer);
49+
clearTimeout(this._uploadTimeoutTimer);
4950
delete this._timer;
5051
delete this._responseTimeoutTimer;
52+
delete this._uploadTimeoutTimer;
5153
return this;
5254
};
5355

@@ -107,6 +109,7 @@ RequestBase.prototype.serialize = function serialize(fn){
107109
*
108110
* - response timeout is time between sending request and receiving the first byte of the response. Includes DNS and connection time.
109111
* - 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.
112+
* - upload is the time since last bit of data was sent or received. This timeout works only if deadline timeout is off
110113
*
111114
* Value of 0 or false means no timeout.
112115
*
@@ -119,6 +122,7 @@ RequestBase.prototype.timeout = function timeout(options){
119122
if (!options || 'object' !== typeof options) {
120123
this._timeout = options;
121124
this._responseTimeout = 0;
125+
this._uploadTimeout = 0;
122126
return this;
123127
}
124128

@@ -130,6 +134,9 @@ RequestBase.prototype.timeout = function timeout(options){
130134
case 'response':
131135
this._responseTimeout = options.response;
132136
break;
137+
case 'upload':
138+
this._uploadTimeout = options.upload;
139+
break;
133140
default:
134141
console.warn("Unknown timeout option", option);
135142
}
@@ -700,4 +707,5 @@ RequestBase.prototype._setTimeouts = function() {
700707
self._timeoutError('Response timeout of ', self._responseTimeout, 'ETIMEDOUT');
701708
}, this._responseTimeout);
702709
}
710+
703711
};

0 commit comments

Comments
 (0)