Skip to content

Commit

Permalink
Support IETF draft -04 and -05 (#735)
Browse files Browse the repository at this point in the history
* Support IETF draft -04 and -05

* Fix formatting
  • Loading branch information
Acconut authored Nov 2, 2024
1 parent d85895b commit f20ef3c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ interface SliceResult {

_Default value:_ `'tus-v1'`

tus-js-client uses the [tus v1.0.0 upload protocol](https://tus.io/protocols/resumable-upload) by default. It also includes experimental support for [the draft of Resumable Uploads For HTTP](https://datatracker.ietf.org/doc/draft-ietf-httpbis-resumable-upload/) developed in the HTTP working group of the IETF. By setting the `protocol` option to `'ietf-draft-03'`, tus-js-client will use the protocol as defined in the draft version 03. Please be aware that this feature is experimental and that this option might change in breaking ways in non-major releases.
tus-js-client uses the [tus v1.0.0 upload protocol](https://tus.io/protocols/resumable-upload) by default. It also includes experimental support for [the draft of Resumable Uploads For HTTP](https://datatracker.ietf.org/doc/draft-ietf-httpbis-resumable-upload/) developed in the HTTP working group of the IETF. By setting the `protocol` option to `'ietf-draft-03'` or `'ietf-draft-05'`, tus-js-client will use the protocol as defined in the draft version 03 or 05 respectively. Please be aware that this feature is experimental and that this option might change in breaking ways in non-major releases.

## tus.Upload(file, options)

Expand Down
25 changes: 21 additions & 4 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import uuid from './uuid.js'

const PROTOCOL_TUS_V1 = 'tus-v1'
const PROTOCOL_IETF_DRAFT_03 = 'ietf-draft-03'
const PROTOCOL_IETF_DRAFT_05 = 'ietf-draft-05'

const defaultOptions = {
endpoint: null,
Expand Down Expand Up @@ -176,7 +177,11 @@ class BaseUpload {
return
}

if (![PROTOCOL_TUS_V1, PROTOCOL_IETF_DRAFT_03].includes(this.options.protocol)) {
if (
![PROTOCOL_TUS_V1, PROTOCOL_IETF_DRAFT_03, PROTOCOL_IETF_DRAFT_05].includes(
this.options.protocol,
)
) {
this._emitError(new Error(`tus: unsupported protocol ${this.options.protocol}`))
return
}
Expand Down Expand Up @@ -596,7 +601,10 @@ class BaseUpload {
this._offset = 0
promise = this._addChunkToRequest(req)
} else {
if (this.options.protocol === PROTOCOL_IETF_DRAFT_03) {
if (
this.options.protocol === PROTOCOL_IETF_DRAFT_03 ||
this.options.protocol === PROTOCOL_IETF_DRAFT_05
) {
req.setHeader('Upload-Complete', '?0')
}
promise = this._sendRequest(req, null)
Expand Down Expand Up @@ -791,7 +799,11 @@ class BaseUpload {
this._emitProgress(start + bytesSent, this._size)
})

req.setHeader('Content-Type', 'application/offset+octet-stream')
if (this.options.protocol === PROTOCOL_TUS_V1) {
req.setHeader('Content-Type', 'application/offset+octet-stream')
} else if (this.options.protocol === PROTOCOL_IETF_DRAFT_05) {
req.setHeader('Content-Type', 'application/partial-upload')
}

// The specified chunkSize may be Infinity or the calcluated end position
// may exceed the file's size. In both cases, we limit the end position to
Expand Down Expand Up @@ -832,7 +844,10 @@ class BaseUpload {
return this._sendRequest(req)
}

if (this.options.protocol === PROTOCOL_IETF_DRAFT_03) {
if (
this.options.protocol === PROTOCOL_IETF_DRAFT_03 ||
this.options.protocol === PROTOCOL_IETF_DRAFT_05
) {
req.setHeader('Upload-Complete', done ? '?1' : '?0')
}
this._emitProgress(this._offset, this._size)
Expand Down Expand Up @@ -968,6 +983,8 @@ function openRequest(method, url, options) {

if (options.protocol === PROTOCOL_IETF_DRAFT_03) {
req.setHeader('Upload-Draft-Interop-Version', '5')
} else if (options.protocol === PROTOCOL_IETF_DRAFT_05) {
req.setHeader('Upload-Draft-Interop-Version', '6')
} else {
req.setHeader('Tus-Resumable', '1.0.0')
}
Expand Down

0 comments on commit f20ef3c

Please sign in to comment.