Skip to content

Commit 650b521

Browse files
committed
add upload timeout
1 parent 0f90861 commit 650b521

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/client.js

+11
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ Request.prototype._end = function() {
709709

710710
// progress
711711
const handleProgress = (direction, e) => {
712+
713+
clearTimeout(self._uploadTimeoutTimer);
712714
if (e.total > 0) {
713715
e.percent = e.loaded / e.total * 100;
714716
}
@@ -728,6 +730,15 @@ Request.prototype._end = function() {
728730
}
729731
}
730732

733+
if(xhr.upload){
734+
// upload timeout it's wokrs only if deadline timeout is off
735+
if (this._uploadTimeout && !this._uploadTimeoutTimer) {
736+
this._uploadTimeoutTimer = setTimeout(() => {
737+
self._timeoutError('Upload timeout of ', self._uploadTimeout, 'ETIMEDOUT');
738+
}, this._uploadTimeout);
739+
}
740+
}
741+
731742
// initiate request
732743
try {
733744
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
}
@@ -692,4 +699,5 @@ RequestBase.prototype._setTimeouts = function() {
692699
self._timeoutError('Response timeout of ', self._responseTimeout, 'ETIMEDOUT');
693700
}, this._responseTimeout);
694701
}
702+
695703
};

0 commit comments

Comments
 (0)