|
@@ -215,6 +215,23 @@ module.exports = class Tus extends BasePlugin {
|
|
|
if (typeof opts.onBeforeRequest === 'function') {
|
|
|
opts.onBeforeRequest(req)
|
|
|
}
|
|
|
+
|
|
|
+ if (Object.hasOwn(queuedRequest, 'shouldBeRequeued')) {
|
|
|
+ if (!queuedRequest.shouldBeRequeued) return Promise.reject()
|
|
|
+ let done
|
|
|
+ const p = new Promise((res) => { // eslint-disable-line promise/param-names
|
|
|
+ done = res
|
|
|
+ })
|
|
|
+ queuedRequest = this.requests.run(() => {
|
|
|
+ if (file.isPaused) {
|
|
|
+ queuedRequest.abort()
|
|
|
+ }
|
|
|
+ done()
|
|
|
+ return () => {}
|
|
|
+ })
|
|
|
+ return p
|
|
|
+ }
|
|
|
+ return undefined
|
|
|
}
|
|
|
|
|
|
uploadOptions.onError = (err) => {
|
|
@@ -259,7 +276,7 @@ module.exports = class Tus extends BasePlugin {
|
|
|
resolve(upload)
|
|
|
}
|
|
|
|
|
|
- uploadOptions.onShouldRetry = (err, retryAttempt, options) => {
|
|
|
+ uploadOptions.onShouldRetry = (err) => {
|
|
|
const status = err?.originalResponse?.getStatus()
|
|
|
if (status === 429) {
|
|
|
// HTTP 429 Too Many Requests => to avoid the whole download to fail, pause all requests.
|
|
@@ -270,8 +287,6 @@ module.exports = class Tus extends BasePlugin {
|
|
|
}
|
|
|
this.requests.rateLimit(next.value)
|
|
|
}
|
|
|
- queuedRequest.abort()
|
|
|
- queuedRequest = this.requests.run(qRequest)
|
|
|
} else if (status > 400 && status < 500 && status !== 409) {
|
|
|
// HTTP 4xx, the server won't send anything, it's doesn't make sense to retry
|
|
|
return false
|
|
@@ -283,19 +298,20 @@ module.exports = class Tus extends BasePlugin {
|
|
|
this.requests.resume()
|
|
|
}, { once: true })
|
|
|
}
|
|
|
- queuedRequest.abort()
|
|
|
- queuedRequest = this.requests.run(qRequest)
|
|
|
- } else {
|
|
|
- // For a non-4xx error, we can re-queue the request.
|
|
|
- setTimeout(() => {
|
|
|
- queuedRequest.abort()
|
|
|
- queuedRequest = this.requests.run(qRequest)
|
|
|
- }, options.retryDelays[retryAttempt])
|
|
|
}
|
|
|
- // Aborting the timeout set by tus-js-client to not short-circuit the rate limiting.
|
|
|
- // eslint-disable-next-line no-underscore-dangle
|
|
|
- queueMicrotask(() => clearTimeout(queuedRequest._retryTimeout))
|
|
|
- // We need to return true here so tus-js-client increments the retryAttempt and do not emit an error event.
|
|
|
+ queuedRequest.abort()
|
|
|
+ queuedRequest = {
|
|
|
+ shouldBeRequeued: true,
|
|
|
+ abort () {
|
|
|
+ this.shouldBeRequeued = false
|
|
|
+ },
|
|
|
+ done () {
|
|
|
+ throw new Error('Cannot mark a queued request as done: this indicates a bug')
|
|
|
+ },
|
|
|
+ fn () {
|
|
|
+ throw new Error('Cannot run a queued request: this indicates a bug')
|
|
|
+ },
|
|
|
+ }
|
|
|
return true
|
|
|
}
|
|
|
|
|
@@ -325,6 +341,7 @@ module.exports = class Tus extends BasePlugin {
|
|
|
this.uploaders[file.id] = upload
|
|
|
this.uploaderEvents[file.id] = new EventTracker(this.uppy)
|
|
|
|
|
|
+ // eslint-disable-next-line prefer-const
|
|
|
qRequest = () => {
|
|
|
if (!file.isPaused) {
|
|
|
upload.start()
|
|
@@ -355,14 +372,13 @@ module.exports = class Tus extends BasePlugin {
|
|
|
})
|
|
|
|
|
|
this.onPause(file.id, (isPaused) => {
|
|
|
+ queuedRequest.abort()
|
|
|
if (isPaused) {
|
|
|
// Remove this file from the queue so another file can start in its place.
|
|
|
- queuedRequest.abort()
|
|
|
upload.abort()
|
|
|
} else {
|
|
|
// Resuming an upload should be queued, else you could pause and then
|
|
|
// resume a queued upload to make it skip the queue.
|
|
|
- queuedRequest.abort()
|
|
|
queuedRequest = this.requests.run(qRequest)
|
|
|
}
|
|
|
})
|