@uppy/tus
Advanced tools
Comparing version 1.0.2 to 1.1.0
189
lib/index.js
@@ -1,8 +0,8 @@ | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _class, _temp; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } | ||
@@ -20,8 +20,11 @@ var _require = require('@uppy/core'), | ||
var emitSocketProgress = require('@uppy/utils/lib/emitSocketProgress'); | ||
var getSocketHost = require('@uppy/utils/lib/getSocketHost'); | ||
var settle = require('@uppy/utils/lib/settle'); | ||
var limitPromises = require('@uppy/utils/lib/limitPromises'); | ||
// Extracted from https://github.com/tus/tus-js-client/blob/master/lib/upload.js#L13 | ||
var limitPromises = require('@uppy/utils/lib/limitPromises'); // Extracted from https://github.com/tus/tus-js-client/blob/master/lib/upload.js#L13 | ||
// excepted we removed 'fingerprint' key to avoid adding more dependencies | ||
var tusDefaultOptions = { | ||
@@ -41,3 +44,2 @@ endpoint: '', | ||
retryDelays: null | ||
/** | ||
@@ -47,3 +49,6 @@ * Create a wrapper around an event emitter with a `remove` method to remove | ||
*/ | ||
};function createEventTracker(emitter) { | ||
}; | ||
function createEventTracker(emitter) { | ||
var events = []; | ||
@@ -59,3 +64,2 @@ return { | ||
fn = _ref[1]; | ||
emitter.off(event, fn); | ||
@@ -66,3 +70,2 @@ }); | ||
} | ||
/** | ||
@@ -72,15 +75,17 @@ * Tus resumable file uploader | ||
*/ | ||
module.exports = function (_Plugin) { | ||
_inherits(Tus, _Plugin); | ||
module.exports = (_temp = _class = | ||
/*#__PURE__*/ | ||
function (_Plugin) { | ||
_inheritsLoose(Tus, _Plugin); | ||
function Tus(uppy, opts) { | ||
_classCallCheck(this, Tus); | ||
var _this; | ||
var _this = _possibleConstructorReturn(this, _Plugin.call(this, uppy, opts)); | ||
_this = _Plugin.call(this, uppy, opts) || this; | ||
_this.type = 'uploader'; | ||
_this.id = 'Tus'; | ||
_this.title = 'Tus'; | ||
_this.title = 'Tus'; // set default options | ||
// set default options | ||
var defaultOptions = { | ||
@@ -91,8 +96,7 @@ resume: true, | ||
limit: 0, | ||
retryDelays: [0, 1000, 3000, 5000] | ||
retryDelays: [0, 1000, 3000, 5000] // merge default options with the ones set by user | ||
// merge default options with the ones set by user | ||
};_this.opts = _extends({}, defaultOptions, opts); | ||
}; | ||
_this.opts = _extends({}, defaultOptions, opts); // Simultaneous upload limiting is shared across all uploads with this plugin. | ||
// Simultaneous upload limiting is shared across all uploads with this plugin. | ||
if (typeof _this.opts.limit === 'number' && _this.opts.limit !== 0) { | ||
@@ -109,10 +113,12 @@ _this.limitUploads = limitPromises(_this.opts.limit); | ||
_this.uploaderSockets = Object.create(null); | ||
_this.handleResetProgress = _this.handleResetProgress.bind(_this); | ||
_this.handleUpload = _this.handleUpload.bind(_this); | ||
_this.handleResetProgress = _this.handleResetProgress.bind(_assertThisInitialized(_this)); | ||
_this.handleUpload = _this.handleUpload.bind(_assertThisInitialized(_this)); | ||
return _this; | ||
} | ||
Tus.prototype.handleResetProgress = function handleResetProgress() { | ||
var _proto = Tus.prototype; | ||
_proto.handleResetProgress = function handleResetProgress() { | ||
var files = _extends({}, this.uppy.getState().files); | ||
Object.keys(files).forEach(function (fileID) { | ||
@@ -122,10 +128,13 @@ // Only clone the file object if it has a Tus `uploadUrl` attached. | ||
var tusState = _extends({}, files[fileID].tus); | ||
delete tusState.uploadUrl; | ||
files[fileID] = _extends({}, files[fileID], { tus: tusState }); | ||
files[fileID] = _extends({}, files[fileID], { | ||
tus: tusState | ||
}); | ||
} | ||
}); | ||
this.uppy.setState({ files: files }); | ||
}; | ||
this.uppy.setState({ | ||
files: files | ||
}); | ||
} | ||
/** | ||
@@ -135,5 +144,5 @@ * Clean up all references for a file's upload: the tus.Upload instance, | ||
*/ | ||
; | ||
Tus.prototype.resetUploaderReferences = function resetUploaderReferences(fileID) { | ||
_proto.resetUploaderReferences = function resetUploaderReferences(fileID) { | ||
if (this.uploaders[fileID]) { | ||
@@ -143,2 +152,3 @@ this.uploaders[fileID].abort(); | ||
} | ||
if (this.uploaderEvents[fileID]) { | ||
@@ -148,2 +158,3 @@ this.uploaderEvents[fileID].remove(); | ||
} | ||
if (this.uploaderSockets[fileID]) { | ||
@@ -153,4 +164,3 @@ this.uploaderSockets[fileID].close(); | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -164,13 +174,11 @@ * Create a new Tus upload | ||
*/ | ||
; | ||
Tus.prototype.upload = function upload(file, current, total) { | ||
_proto.upload = function upload(file, current, total) { | ||
var _this2 = this; | ||
this.resetUploaderReferences(file.id); | ||
this.resetUploaderReferences(file.id); // Create a new tus upload | ||
// Create a new tus upload | ||
return new Promise(function (resolve, reject) { | ||
var optsTus = _extends({}, tusDefaultOptions, _this2.opts, | ||
// Install file-specific upload overrides. | ||
var optsTus = _extends({}, tusDefaultOptions, _this2.opts, // Install file-specific upload overrides. | ||
file.tus || {}); | ||
@@ -180,6 +188,9 @@ | ||
_this2.uppy.log(err); | ||
_this2.uppy.emit('upload-error', file, err); | ||
err.message = 'Failed because: ' + err.message; | ||
err.message = "Failed because: " + err.message; | ||
_this2.resetUploaderReferences(file.id); | ||
reject(err); | ||
@@ -190,2 +201,3 @@ }; | ||
_this2.onReceiveUploadUrl(file, upload.url); | ||
_this2.uppy.emit('upload-progress', file, { | ||
@@ -210,2 +222,3 @@ uploader: _this2, | ||
_this2.resetUploaderReferences(file.id); | ||
resolve(upload); | ||
@@ -218,10 +231,10 @@ }; | ||
} | ||
}; | ||
}; // tusd uses metadata fields 'filetype' and 'filename' | ||
// tusd uses metadata fields 'filetype' and 'filename' | ||
var meta = _extends({}, file.meta); | ||
copyProp(meta, 'type', 'filetype'); | ||
copyProp(meta, 'name', 'filename'); | ||
optsTus.metadata = meta; | ||
var upload = new tus.Upload(file.data, optsTus); | ||
@@ -233,3 +246,4 @@ _this2.uploaders[file.id] = upload; | ||
_this2.resetUploaderReferences(file.id); | ||
resolve('upload ' + targetFileID + ' was removed'); | ||
resolve("upload " + targetFileID + " was removed"); | ||
}); | ||
@@ -251,3 +265,4 @@ | ||
_this2.resetUploaderReferences(file.id); | ||
resolve('upload ' + file.id + ' was canceled'); | ||
resolve("upload " + file.id + " was canceled"); | ||
}); | ||
@@ -259,2 +274,3 @@ | ||
} | ||
upload.start(); | ||
@@ -269,3 +285,3 @@ }); | ||
Tus.prototype.uploadRemote = function uploadRemote(file, current, total) { | ||
_proto.uploadRemote = function uploadRemote(file, current, total) { | ||
var _this3 = this; | ||
@@ -275,4 +291,3 @@ | ||
var opts = _extends({}, this.opts, | ||
// Install file-specific upload overrides. | ||
var opts = _extends({}, this.opts, // Install file-specific upload overrides. | ||
file.tus || {}); | ||
@@ -282,2 +297,3 @@ | ||
_this3.uppy.log(file.remote.url); | ||
if (file.serverToken) { | ||
@@ -290,2 +306,3 @@ return _this3.connectToServerSocket(file).then(function () { | ||
_this3.uppy.emit('upload-started', file); | ||
var Client = file.remote.providerOptions.provider ? Provider : RequestClient; | ||
@@ -300,3 +317,6 @@ var client = new Client(_this3.uppy, file.remote.providerOptions); | ||
})).then(function (res) { | ||
_this3.uppy.setFileState(file.id, { serverToken: res.token }); | ||
_this3.uppy.setFileState(file.id, { | ||
serverToken: res.token | ||
}); | ||
file = _this3.uppy.getFile(file.id); | ||
@@ -314,3 +334,3 @@ return file; | ||
Tus.prototype.connectToServerSocket = function connectToServerSocket(file) { | ||
_proto.connectToServerSocket = function connectToServerSocket(file) { | ||
var _this4 = this; | ||
@@ -321,3 +341,5 @@ | ||
var host = getSocketHost(file.remote.companionUrl); | ||
var socket = new Socket({ target: host + '/api/' + token }); | ||
var socket = new Socket({ | ||
target: host + "/api/" + token | ||
}); | ||
_this4.uploaderSockets[file.id] = socket; | ||
@@ -328,3 +350,3 @@ _this4.uploaderEvents[file.id] = createEventTracker(_this4.uppy); | ||
socket.send('pause', {}); | ||
resolve('upload ' + file.id + ' was removed'); | ||
resolve("upload " + file.id + " was removed"); | ||
}); | ||
@@ -348,2 +370,3 @@ | ||
} | ||
socket.send('resume', {}); | ||
@@ -369,13 +392,15 @@ }); | ||
}); | ||
socket.on('error', function (errData) { | ||
var message = errData.error.message; | ||
var error = _extends(new Error(message), { cause: errData.error }); | ||
var error = _extends(new Error(message), { | ||
cause: errData.error | ||
}); // If the remote retry optimisation should not be used, | ||
// close the socket—this will tell companion to clear state and delete the file. | ||
// If the remote retry optimisation should not be used, | ||
// close the socket—this will tell companion to clear state and delete the file. | ||
if (!_this4.opts.useFastRemoteRetry) { | ||
_this4.resetUploaderReferences(file.id); | ||
// Remove the serverToken so that a new one will be created for the retry. | ||
_this4.resetUploaderReferences(file.id); // Remove the serverToken so that a new one will be created for the retry. | ||
_this4.uppy.setFileState(file.id, { | ||
@@ -387,5 +412,5 @@ serverToken: null | ||
_this4.uppy.emit('upload-error', file, error); | ||
reject(error); | ||
}); | ||
socket.on('success', function (data) { | ||
@@ -397,8 +422,9 @@ var uploadResp = { | ||
_this4.uppy.emit('upload-success', file, uploadResp); | ||
_this4.resetUploaderReferences(file.id); | ||
resolve(); | ||
}); | ||
}); | ||
}; | ||
} | ||
/** | ||
@@ -408,8 +434,8 @@ * Store the uploadUrl on the file options, so that when Golden Retriever | ||
*/ | ||
; | ||
_proto.onReceiveUploadUrl = function onReceiveUploadUrl(file, uploadURL) { | ||
var currentFile = this.uppy.getFile(file.id); | ||
if (!currentFile) return; // Only do the update if we didn't have an upload URL yet. | ||
Tus.prototype.onReceiveUploadUrl = function onReceiveUploadUrl(file, uploadURL) { | ||
var currentFile = this.uppy.getFile(file.id); | ||
if (!currentFile) return; | ||
// Only do the update if we didn't have an upload URL yet. | ||
if (!currentFile.tus || currentFile.tus.uploadUrl !== uploadURL) { | ||
@@ -425,3 +451,3 @@ this.uppy.log('[Tus] Storing upload url'); | ||
Tus.prototype.onFileRemove = function onFileRemove(fileID, cb) { | ||
_proto.onFileRemove = function onFileRemove(fileID, cb) { | ||
this.uploaderEvents[fileID].on('file-removed', function (file) { | ||
@@ -432,3 +458,3 @@ if (fileID === file.id) cb(file.id); | ||
Tus.prototype.onPause = function onPause(fileID, cb) { | ||
_proto.onPause = function onPause(fileID, cb) { | ||
this.uploaderEvents[fileID].on('upload-pause', function (targetFileID, isPaused) { | ||
@@ -442,3 +468,3 @@ if (fileID === targetFileID) { | ||
Tus.prototype.onRetry = function onRetry(fileID, cb) { | ||
_proto.onRetry = function onRetry(fileID, cb) { | ||
this.uploaderEvents[fileID].on('upload-retry', function (targetFileID) { | ||
@@ -451,3 +477,3 @@ if (fileID === targetFileID) { | ||
Tus.prototype.onRetryAll = function onRetryAll(fileID, cb) { | ||
_proto.onRetryAll = function onRetryAll(fileID, cb) { | ||
var _this5 = this; | ||
@@ -461,3 +487,3 @@ | ||
Tus.prototype.onPauseAll = function onPauseAll(fileID, cb) { | ||
_proto.onPauseAll = function onPauseAll(fileID, cb) { | ||
var _this6 = this; | ||
@@ -471,3 +497,3 @@ | ||
Tus.prototype.onCancelAll = function onCancelAll(fileID, cb) { | ||
_proto.onCancelAll = function onCancelAll(fileID, cb) { | ||
var _this7 = this; | ||
@@ -481,3 +507,3 @@ | ||
Tus.prototype.onResumeAll = function onResumeAll(fileID, cb) { | ||
_proto.onResumeAll = function onResumeAll(fileID, cb) { | ||
var _this8 = this; | ||
@@ -491,3 +517,3 @@ | ||
Tus.prototype.uploadFiles = function uploadFiles(files) { | ||
_proto.uploadFiles = function uploadFiles(files) { | ||
var _this9 = this; | ||
@@ -507,18 +533,19 @@ | ||
_this9.uppy.emit('upload-started', file); | ||
return _this9.uploadRemote.bind(_this9, file, current, total); | ||
} else { | ||
_this9.uppy.emit('upload-started', file); | ||
return _this9.upload.bind(_this9, file, current, total); | ||
} | ||
}); | ||
var promises = actions.map(function (action) { | ||
var limitedAction = _this9.limitUploads(action); | ||
return limitedAction(); | ||
}); | ||
return settle(promises); | ||
}; | ||
Tus.prototype.handleUpload = function handleUpload(fileIDs) { | ||
_proto.handleUpload = function handleUpload(fileIDs) { | ||
var _this10 = this; | ||
@@ -535,3 +562,2 @@ | ||
}); | ||
return this.uploadFiles(filesToUpload).then(function () { | ||
@@ -542,3 +568,3 @@ return null; | ||
Tus.prototype.install = function install() { | ||
_proto.install = function install() { | ||
this.uppy.setState({ | ||
@@ -550,3 +576,2 @@ capabilities: _extends({}, this.uppy.getState().capabilities, { | ||
this.uppy.addUploader(this.handleUpload); | ||
this.uppy.on('reset-progress', this.handleResetProgress); | ||
@@ -559,3 +584,3 @@ | ||
Tus.prototype.uninstall = function uninstall() { | ||
_proto.uninstall = function uninstall() { | ||
this.uppy.setState({ | ||
@@ -574,2 +599,2 @@ capabilities: _extends({}, this.uppy.getState().capabilities, { | ||
return Tus; | ||
}(Plugin); | ||
}(Plugin), _class.VERSION = "1.1.0", _temp); |
{ | ||
"name": "@uppy/tus", | ||
"description": "Resumable uploads for Uppy using Tus.io", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -25,8 +25,8 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@uppy/companion-client": "1.0.1", | ||
"@uppy/utils": "1.0.2", | ||
"@uppy/companion-client": "1.1.0", | ||
"@uppy/utils": "1.1.0", | ||
"tus-js-client": "^1.8.0-0" | ||
}, | ||
"devDependencies": { | ||
"@uppy/core": "1.0.2" | ||
"@uppy/core": "1.1.0" | ||
}, | ||
@@ -36,3 +36,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "5c9e1a836df858254657a59ab8bd4b90035cf8b4" | ||
"gitHead": "28d235fe2fb57d87a399c20883fd6590aa49f4f4" | ||
} |
@@ -51,2 +51,4 @@ const { Plugin } = require('@uppy/core') | ||
module.exports = class Tus extends Plugin { | ||
static VERSION = require('../package.json').version | ||
constructor (uppy, opts) { | ||
@@ -260,10 +262,7 @@ super(uppy, opts) | ||
return file | ||
}) | ||
.then((file) => { | ||
}).then((file) => { | ||
return this.connectToServerSocket(file) | ||
}) | ||
.then(() => { | ||
}).then(() => { | ||
resolve() | ||
}) | ||
.catch((err) => { | ||
}).catch((err) => { | ||
reject(new Error(err)) | ||
@@ -270,0 +269,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
869
33275
+ Added@uppy/companion-client@1.1.0(transitive)
+ Added@uppy/utils@1.1.0(transitive)
- Removed@uppy/companion-client@1.0.1(transitive)
- Removed@uppy/utils@1.0.2(transitive)
Updated@uppy/companion-client@1.1.0
Updated@uppy/utils@1.1.0