From 542baa6adceeb1f1630739dd6d6e097fe1c41b97 Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Thu, 19 Oct 2023 14:13:39 +0700 Subject: [PATCH 1/4] move default retry logic on to defaultOptions to allow user to reuse it inside custom `onShouldRetry` option --- lib/upload.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/upload.js b/lib/upload.js index b38a2aa8..6df5f4e5 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -23,7 +23,10 @@ const defaultOptions = { addRequestId: false, onBeforeRequest: null, onAfterResponse: null, - onShouldRetry: null, + onShouldRetry: (err, retryAttempt, options) => { + const status = err.originalResponse ? err.originalResponse.getStatus() : 0 + return (!inStatusCategory(status, 400) || status === 409 || status === 423) && isOnline() + }, chunkSize: Infinity, retryDelays: [0, 1000, 3000, 5000], @@ -1026,8 +1029,7 @@ function shouldRetry(err, retryAttempt, options) { return options.onShouldRetry(err, retryAttempt, options) } - const status = err.originalResponse ? err.originalResponse.getStatus() : 0 - return (!inStatusCategory(status, 400) || status === 409 || status === 423) && isOnline() + return defaultOptions.onShouldRetry(err, retryAttempt, options); } /** From 472245610d29d4e57e453b9535aa937ff71f5dc5 Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Thu, 19 Oct 2023 14:19:25 +0700 Subject: [PATCH 2/4] correct onShouldRetry as it'll never be called with just an error --- lib/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 3ddee0f0..5e866729 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -33,7 +33,7 @@ interface UploadOptions { onSuccess?: (() => void) | null onError?: ((error: Error | DetailedError) => void) | null onShouldRetry?: - | ((error: Error | DetailedError, retryAttempt: number, options: UploadOptions) => boolean) + | ((error: DetailedError, retryAttempt: number, options: UploadOptions) => boolean) | null onUploadUrlAvailable?: (() => void) | null From e511047028b8b8c437eb04a45ca98c6daf035b3e Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Thu, 19 Oct 2023 17:19:04 +0700 Subject: [PATCH 3/4] remove unused parameters causing build error --- lib/upload.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/upload.js b/lib/upload.js index 6df5f4e5..e7decc53 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -23,7 +23,7 @@ const defaultOptions = { addRequestId: false, onBeforeRequest: null, onAfterResponse: null, - onShouldRetry: (err, retryAttempt, options) => { + onShouldRetry: (err) => { const status = err.originalResponse ? err.originalResponse.getStatus() : 0 return (!inStatusCategory(status, 400) || status === 409 || status === 423) && isOnline() }, From 34cef8a741d2b40f5d6d486b2057040d29d2f9ba Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Tue, 24 Oct 2023 14:35:07 +0700 Subject: [PATCH 4/4] move default on should retry into it's own function. --- lib/upload.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/upload.js b/lib/upload.js index e7decc53..61289da4 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -23,10 +23,7 @@ const defaultOptions = { addRequestId: false, onBeforeRequest: null, onAfterResponse: null, - onShouldRetry: (err) => { - const status = err.originalResponse ? err.originalResponse.getStatus() : 0 - return (!inStatusCategory(status, 400) || status === 409 || status === 423) && isOnline() - }, + onShouldRetry: defaultOnShouldRetry, chunkSize: Infinity, retryDelays: [0, 1000, 3000, 5000], @@ -1003,7 +1000,7 @@ function isOnline() { /** * Checks whether or not it is ok to retry a request. - * @param {Error} err the error returned from the last request + * @param {Error|DetailedError} err the error returned from the last request * @param {number} retryAttempt the number of times the request has already been retried * @param {object} options tus Upload options * @@ -1029,7 +1026,17 @@ function shouldRetry(err, retryAttempt, options) { return options.onShouldRetry(err, retryAttempt, options) } - return defaultOptions.onShouldRetry(err, retryAttempt, options); + return defaultOnShouldRetry(err) +} + +/** + * determines if the request should be retried. Will only retry if not a status 4xx except a 409 or 423 + * @param {DetailedError} err + * @returns {boolean} + */ +function defaultOnShouldRetry(err) { + const status = err.originalResponse ? err.originalResponse.getStatus() : 0 + return (!inStatusCategory(status, 400) || status === 409 || status === 423) && isOnline() } /**