diff --git a/lib/upload.js b/lib/upload.js index e2577758..4365388f 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -73,6 +73,9 @@ class Upload { // The offset of the remote upload before the latest attempt was started. this._offsetBeforeRetry = 0; + + // The remote upload resource is deferred + this._deferred = false; } static terminate(url, options, cb) { @@ -401,6 +404,7 @@ class Upload { this._setupXHR(xhr); if (this.options.uploadLengthDeferred) { xhr.setRequestHeader("Upload-Defer-Length", 1); + this._deferred = true; } else { xhr.setRequestHeader("Upload-Length", this._size); } @@ -470,8 +474,13 @@ class Upload { return; } + let deferLength = parseInt(xhr.getResponseHeader("Upload-Defer-Length"), 10); + if (deferLength === 1) { + this._deferred = true; + } + let length = parseInt(xhr.getResponseHeader("Upload-Length"), 10); - if (isNaN(length) && !this.options.uploadLengthDeferred) { + if (isNaN(length) && !this._deferred) { this._emitXhrError(xhr, new Error("tus: invalid or missing length value")); return; } @@ -581,10 +590,11 @@ class Upload { return; } - if (this.options.uploadLengthDeferred) { - if (complete) { + if (this.options.uploadLengthDeferred || this._deferred) { + if (!this.options.uploadLengthDeferred || complete) { this._size = this._offset + (value && value.size ? value.size : 0); xhr.setRequestHeader("Upload-Length", this._size); + this._deferred = false; } }