Skip to content

Commit

Permalink
Add support for IETF resumable upload draft (#609)
Browse files Browse the repository at this point in the history
* Add support for IETF resumable upload draft

* Fix linting

* Update to draft -03

* Add documentation
  • Loading branch information
Acconut authored Mar 18, 2024
1 parent f9e1e5f commit d2506ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions demos/browser/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function startUpload() {
filename: file.name,
filetype: file.type,
},
protocol: 'ietf-draft-01',
onError(error) {
if (error.originalRequest) {
if (window.confirm(`Failed because: ${error}\nDo you want to retry?`)) {
Expand Down
6 changes: 6 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ interface SliceResult {
}
```

#### protocol

_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.Upload(file, options)

The constructor for the `tus.Upload` class. The upload will not be started automatically, use `start` to do so.
Expand Down
29 changes: 27 additions & 2 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import DetailedError from './error.js'
import { log } from './logger.js'
import uuid from './uuid.js'

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

const defaultOptions = {
endpoint: null,

Expand Down Expand Up @@ -37,6 +40,8 @@ const defaultOptions = {
urlStorage: null,
fileReader: null,
httpStack: null,

protocol: PROTOCOL_TUS_V1,
}

class BaseUpload {
Expand Down Expand Up @@ -171,6 +176,11 @@ class BaseUpload {
return
}

if (![PROTOCOL_TUS_V1, PROTOCOL_IETF_DRAFT_03].includes(this.options.protocol)) {
this._emitError(new Error(`tus: unsupported protocol ${this.options.protocol}`))
return
}

if (!this.options.endpoint && !this.options.uploadUrl && !this.url) {
this._emitError(new Error('tus: neither an endpoint or an upload URL is provided'))
return
Expand Down Expand Up @@ -585,6 +595,9 @@ class BaseUpload {
this._offset = 0
promise = this._addChunkToRequest(req)
} else {
if (this.options.protocol === PROTOCOL_IETF_DRAFT_03) {
req.setHeader('Upload-Complete', '?0')
}
promise = this._sendRequest(req, null)
}

Expand Down Expand Up @@ -683,7 +696,11 @@ class BaseUpload {
}

const length = parseInt(res.getHeader('Upload-Length'), 10)
if (Number.isNaN(length) && !this.options.uploadLengthDeferred) {
if (
Number.isNaN(length) &&
!this.options.uploadLengthDeferred &&
this.options.protocol === PROTOCOL_TUS_V1
) {
this._emitHttpError(req, res, 'tus: invalid or missing length value')
return
}
Expand Down Expand Up @@ -810,6 +827,10 @@ class BaseUpload {
if (value === null) {
return this._sendRequest(req)
}

if (this.options.protocol === PROTOCOL_IETF_DRAFT_03) {
req.setHeader('Upload-Complete', done ? '?1' : '?0')
}
this._emitProgress(this._offset, this._size)
return this._sendRequest(req, value)
})
Expand Down Expand Up @@ -941,7 +962,11 @@ function inStatusCategory(status, category) {
function openRequest(method, url, options) {
const req = options.httpStack.createRequest(method, url)

req.setHeader('Tus-Resumable', '1.0.0')
if (options.protocol === PROTOCOL_IETF_DRAFT_03) {
req.setHeader('Upload-Draft-Interop-Version', '5')
} else {
req.setHeader('Tus-Resumable', '1.0.0')
}
const headers = options.headers || {}

Object.entries(headers).forEach(([name, value]) => {
Expand Down

0 comments on commit d2506ee

Please sign in to comment.