@uppy/tus: do not auto-open sockets, clean them up on abort

Co-authored-by: Artur Paikin <artur@arturpaikin.com>
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Antoine du Hamel 2023-03-21 13:25:44 +01:00
parent a825b43451
commit b7fa0238be
No known key found for this signature in database
GPG key ID: 21D900FFDB233756

View file

@ -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()
})
})
}