From 24302467b6030cf30ad1bbba98b4f1fb8aebba87 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 10 May 2022 16:39:16 +0200 Subject: [PATCH 1/2] @uppy/tus: wait for user promise on beforeRequest Fixes: https://github.com/transloadit/uppy/issues/3710 --- packages/@uppy/tus/src/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/@uppy/tus/src/index.js b/packages/@uppy/tus/src/index.js index a7ca86eaa4..378896f1ca 100644 --- a/packages/@uppy/tus/src/index.js +++ b/packages/@uppy/tus/src/index.js @@ -212,8 +212,9 @@ module.exports = class Tus extends BasePlugin { const xhr = req.getUnderlyingObject() xhr.withCredentials = !!opts.withCredentials + let userProvidedPromise if (typeof opts.onBeforeRequest === 'function') { - opts.onBeforeRequest(req) + userProvidedPromise = opts.onBeforeRequest(req) } if (hasProperty(queuedRequest, 'shouldBeRequeued')) { @@ -229,9 +230,9 @@ module.exports = class Tus extends BasePlugin { done() return () => {} }) - return p + return Promise.all([p, userProvidedPromise]) } - return undefined + return userProvidedPromise } uploadOptions.onError = (err) => { From 774ea714bd3884255dcf0cb1997e6bb7a41445dc Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 17 May 2022 17:08:33 +0200 Subject: [PATCH 2/2] Update packages/@uppy/tus/src/index.js Co-authored-by: Merlijn Vos --- packages/@uppy/tus/src/index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/@uppy/tus/src/index.js b/packages/@uppy/tus/src/index.js index 378896f1ca..164f41ec65 100644 --- a/packages/@uppy/tus/src/index.js +++ b/packages/@uppy/tus/src/index.js @@ -230,6 +230,14 @@ module.exports = class Tus extends BasePlugin { done() return () => {} }) + // If the request has been requeued because it was rate limited by the + // remote server, we want to wait for `RateLimitedQueue` to dispatch + // the re-try request. + // Therefore we create a promise that the queue will resolve when + // enough time has elapsed to expect not to be rate-limited again. + // This means we can hold the Tus retry here with a `Promise.all`, + // together with the returned value of the user provided + // `onBeforeRequest` option callback (in case it returns a promise). return Promise.all([p, userProvidedPromise]) } return userProvidedPromise