From b7fa0238be2dfbcbbe5db994d5130be65a9331df Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 21 Mar 2023 13:25:44 +0100 Subject: [PATCH] @uppy/tus: do not auto-open sockets, clean them up on abort Co-authored-by: Artur Paikin Co-authored-by: Antoine du Hamel --- packages/@uppy/tus/src/index.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/@uppy/tus/src/index.js b/packages/@uppy/tus/src/index.js index ec3b588d8..707136e1e 100644 --- a/packages/@uppy/tus/src/index.js +++ b/packages/@uppy/tus/src/index.js @@ -496,7 +496,7 @@ export default class Tus extends BasePlugin { return new Promise((resolve, reject) => { const token = file.serverToken const host = getSocketHost(file.remote.companionUrl) - const socket = new Socket({ target: `${host}/api/${token}` }) + const socket = new Socket({ target: `${host}/api/${token}`, autoOpen: false }) this.uploaderSockets[file.id] = socket this.uploaderEvents[file.id] = new EventTracker(this.uppy) @@ -519,8 +519,10 @@ export default class Tus extends BasePlugin { // resume a queued upload to make it skip the queue. queuedRequest.abort() queuedRequest = this.requests.run(() => { + socket.open() socket.send('resume', {}) - return () => {} + + return () => socket.close() }) } }) @@ -545,8 +547,10 @@ export default class Tus extends BasePlugin { socket.send('pause', {}) } queuedRequest = this.requests.run(() => { + socket.open() socket.send('resume', {}) - return () => {} + + return () => socket.close() }) }) @@ -607,15 +611,17 @@ export default class Tus extends BasePlugin { queuedRequest = this.requests.run(() => { if (file.isPaused) { socket.send('pause', {}) + } else { + socket.open() } - // Don't do anything here, the caller will take care of cancelling the upload itself + // Just close the socket here, the caller will take care of cancelling the upload itself // using resetUploaderReferences(). This is because resetUploaderReferences() has to be // called when this request is still in the queue, and has not been started yet, too. At // that point this cancellation function is not going to be called. // Also, we need to remove the request from the queue _without_ destroying everything // related to this upload to handle pauses. - return () => {} + return () => socket.close() }) }) }