Skip to content

Commit

Permalink
Allows bytesTotal to be null for progress events
Browse files Browse the repository at this point in the history
Co-Authored-By: Marius Kleidl <[email protected]>
  • Loading branch information
Acconut committed Nov 13, 2024
1 parent 0f558f1 commit e359095
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export interface UploadOptions {
fingerprint: (file: UploadInput, options: UploadOptions) => Promise<string | null>
uploadSize: number | null

onProgress: ((bytesSent: number, bytesTotal: number) => void) | null
onChunkComplete: ((chunkSize: number, bytesAccepted: number, bytesTotal: number) => void) | null
onProgress: ((bytesSent: number, bytesTotal: number | null) => void) | null
onChunkComplete:
| ((chunkSize: number, bytesAccepted: number, bytesTotal: number | null) => void)
| null
onSuccess: (() => void) | null
onError: ((error: Error | DetailedError) => void) | null
onShouldRetry:
Expand Down
20 changes: 4 additions & 16 deletions lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,10 @@ export default class BaseUpload {
* data may not have been accepted by the server yet.
*
* @param {number} bytesSent Number of bytes sent to the server.
* @param {number} bytesTotal Total number of bytes to be sent to the server.
* @param {number|null} bytesTotal Total number of bytes to be sent to the server.
* @api private
*/
_emitProgress(bytesSent: number, bytesTotal: number): void {
_emitProgress(bytesSent: number, bytesTotal: number | null): void {
if (typeof this.options.onProgress === 'function') {
this.options.onProgress(bytesSent, bytesTotal)
}
Expand All @@ -554,10 +554,10 @@ export default class BaseUpload {
* @param {number} chunkSize Size of the chunk that was accepted by the server.
* @param {number} bytesAccepted Total number of bytes that have been
* accepted by the server.
* @param {number} bytesTotal Total number of bytes to be sent to the server.
* @param {number|null} bytesTotal Total number of bytes to be sent to the server.
* @api private
*/
_emitChunkComplete(chunkSize: number, bytesAccepted: number, bytesTotal: number): void {
_emitChunkComplete(chunkSize: number, bytesAccepted: number, bytesTotal: number | null): void {
if (typeof this.options.onChunkComplete === 'function') {
this.options.onChunkComplete(chunkSize, bytesAccepted, bytesTotal)
}
Expand Down Expand Up @@ -818,10 +818,6 @@ export default class BaseUpload {
let end = this._offset + this.options.chunkSize

req.setProgressHandler((bytesSent) => {
if (this._size === null) {
this._emitError(new Error('tus: Expected _size to be set'))
return
}
this._emitProgress(start + bytesSent, this._size)
})

Expand Down Expand Up @@ -872,10 +868,6 @@ export default class BaseUpload {
if (this.options.protocol === PROTOCOL_IETF_DRAFT_03) {
req.setHeader('Upload-Complete', done ? '?1' : '?0')
}
if (this._size === null) {
this._emitError(new Error('tus: Expected _size to be set'))
return
}
this._emitProgress(this._offset, this._size)
return this._sendRequest(req, value)
})
Expand All @@ -894,10 +886,6 @@ export default class BaseUpload {
return
}

if (this._size === null) {
this._emitError(new Error('tus: Expected _size to be set'))
return
}
this._emitProgress(offset, this._size)
this._emitChunkComplete(offset - this._offset, offset, this._size)

Expand Down

0 comments on commit e359095

Please sign in to comment.