UNPKG

box-node-sdk

Version:

Official SDK for Box Platform APIs

79 lines 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BoxRetryStrategy = void 0; const utils_1 = require("../internal/utils"); class BoxRetryStrategy { maxAttempts = 5; retryRandomizationFactor = 0.5; retryBaseInterval = 1; maxRetriesOnException = 2; constructor(fields) { if (fields.maxAttempts !== undefined) { this.maxAttempts = fields.maxAttempts; } if (fields.retryRandomizationFactor !== undefined) { this.retryRandomizationFactor = fields.retryRandomizationFactor; } if (fields.retryBaseInterval !== undefined) { this.retryBaseInterval = fields.retryBaseInterval; } if (fields.maxRetriesOnException !== undefined) { this.maxRetriesOnException = fields.maxRetriesOnException; } } /** * @param {FetchOptions} fetchOptions * @param {FetchResponse} fetchResponse * @param {number} attemptNumber * @returns {Promise<boolean>} */ async shouldRetry(fetchOptions, fetchResponse, attemptNumber) { if (fetchResponse.status == 0) { return attemptNumber <= this.maxRetriesOnException; } const isSuccessful = fetchResponse.status >= 200 && fetchResponse.status < 400; const retryAfterHeader = 'Retry-After' in fetchResponse.headers ? fetchResponse.headers['Retry-After'] : void 0; const isAcceptedWithRetryAfter = fetchResponse.status == 202 && !(retryAfterHeader == void 0); if (attemptNumber >= this.maxAttempts) { return false; } if (isAcceptedWithRetryAfter) { return true; } if (fetchResponse.status >= 500) { return true; } if (fetchResponse.status == 429) { return true; } if (fetchResponse.status == 401 && !(fetchOptions.auth == void 0)) { await fetchOptions.auth.refreshToken(fetchOptions.networkSession); return true; } if (isSuccessful) { return false; } return false; } /** * @param {FetchOptions} fetchOptions * @param {FetchResponse} fetchResponse * @param {number} attemptNumber * @returns {number} */ retryAfter(fetchOptions, fetchResponse, attemptNumber) { const retryAfterHeader = 'Retry-After' in fetchResponse.headers ? fetchResponse.headers['Retry-After'] : void 0; if (!(retryAfterHeader == void 0)) { return parseFloat(retryAfterHeader); } const randomization = (0, utils_1.random)(1 - this.retryRandomizationFactor, 1 + this.retryRandomizationFactor); const exponential = 2 ** attemptNumber; return exponential * this.retryBaseInterval * randomization; } } exports.BoxRetryStrategy = BoxRetryStrategy; //# sourceMappingURL=retries.js.map