calculate-retry-delay.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.retryAfterStatusCodes = void 0;
  4. exports.retryAfterStatusCodes = new Set([413, 429, 503]);
  5. const calculateRetryDelay = ({ attemptCount, retryOptions, error, retryAfter }) => {
  6. if (attemptCount > retryOptions.limit) {
  7. return 0;
  8. }
  9. const hasMethod = retryOptions.methods.includes(error.options.method);
  10. const hasErrorCode = retryOptions.errorCodes.includes(error.code);
  11. const hasStatusCode = error.response && retryOptions.statusCodes.includes(error.response.statusCode);
  12. if (!hasMethod || (!hasErrorCode && !hasStatusCode)) {
  13. return 0;
  14. }
  15. if (error.response) {
  16. if (retryAfter) {
  17. if (retryOptions.maxRetryAfter === undefined || retryAfter > retryOptions.maxRetryAfter) {
  18. return 0;
  19. }
  20. return retryAfter;
  21. }
  22. if (error.response.statusCode === 413) {
  23. return 0;
  24. }
  25. }
  26. const noise = Math.random() * 100;
  27. return ((2 ** (attemptCount - 1)) * 1000) + noise;
  28. };
  29. exports.default = calculateRetryDelay;