diff --git a/demos/browser/demo.js b/demos/browser/demo.js index 3d35c772..6c8a1f22 100644 --- a/demos/browser/demo.js +++ b/demos/browser/demo.js @@ -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?`)) { diff --git a/docs/api.md b/docs/api.md index f5542f88..04e1eac7 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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. diff --git a/lib/upload.js b/lib/upload.js index d632bc94..e564de34 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -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, @@ -37,6 +40,8 @@ const defaultOptions = { urlStorage: null, fileReader: null, httpStack: null, + + protocol: PROTOCOL_TUS_V1, } class BaseUpload { @@ -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 @@ -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) } @@ -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 } @@ -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) }) @@ -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]) => {