Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Upload-Length when appropriate #744

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ class BaseUpload {

// An array of upload URLs which are used for uploading the different
// parts, if the parallelUploads option is used.
this._parallelUploadUrls = null
this._parallelUploadUrls = null
Vija02 marked this conversation as resolved.
Show resolved Hide resolved

// The remote upload resource's length is deferred
this._deferred = false
Vija02 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -586,6 +589,7 @@ class BaseUpload {

if (this.options.uploadLengthDeferred) {
req.setHeader('Upload-Defer-Length', '1')
this._deferred = true
Vija02 marked this conversation as resolved.
Show resolved Hide resolved
} else {
req.setHeader('Upload-Length', `${this._size}`)
}
Expand Down Expand Up @@ -704,9 +708,13 @@ class BaseUpload {
return
}

const deferLength = Number.parseInt(res.getHeader("Upload-Defer-Length"), 10)
this._deferred = deferLength === 1

const length = Number.parseInt(res.getHeader('Upload-Length'), 10)
if (
Number.isNaN(length) &&
!this._deferred &&
!this.options.uploadLengthDeferred &&
this.options.protocol === PROTOCOL_TUS_V1
) {
Expand Down Expand Up @@ -821,9 +829,10 @@ class BaseUpload {
// If the upload length is deferred, the upload size was not specified during
// upload creation. So, if the file reader is done reading, we know the total
// upload size and can tell the tus server.
if (this.options.uploadLengthDeferred && done) {
if (this._deferred && (!this.options.uploadLengthDeferred || done)) {
this._size = this._offset + valueSize
req.setHeader('Upload-Length', `${this._size}`)
this._deferred = false
}

// The specified uploadSize might not match the actual amount of data that a source
Expand Down
Loading