Skip to content

Commit

Permalink
Always allow uploading Upload-Defer-Length resources (tus#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatuska committed Feb 21, 2020
1 parent e424441 commit 1bdc5fe
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 1bdc5fe

Please sign in to comment.