Browse Source

@uppy/tus: wait for user promise on beforeRequest (#3712)

Fixes: https://github.com/transloadit/uppy/issues/3710
Co-authored-by: Merlijn Vos <merlijn@soverin.net>
Antoine du Hamel 2 năm trước cách đây
mục cha
commit
77aacf198c
1 tập tin đã thay đổi với 12 bổ sung3 xóa
  1. 12 3
      packages/@uppy/tus/src/index.js

+ 12 - 3
packages/@uppy/tus/src/index.js

@@ -212,8 +212,9 @@ export default 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,17 @@ export default class Tus extends BasePlugin {
             done()
             return () => {}
           })
-          return p
+          // 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 undefined
+        return userProvidedPromise
       }
 
       uploadOptions.onError = (err) => {