1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | exports.getRetryConfig = getRetryConfig;
|
16 | async function getRetryConfig(err) {
|
17 | let config = getConfig(err);
|
18 | if (!err || !err.config || (!config && !err.config.retry)) {
|
19 | return { shouldRetry: false };
|
20 | }
|
21 | config = config || {};
|
22 | config.currentRetryAttempt = config.currentRetryAttempt || 0;
|
23 | config.retry =
|
24 | config.retry === undefined || config.retry === null ? 3 : config.retry;
|
25 | config.httpMethodsToRetry = config.httpMethodsToRetry || [
|
26 | 'GET',
|
27 | 'HEAD',
|
28 | 'PUT',
|
29 | 'OPTIONS',
|
30 | 'DELETE',
|
31 | ];
|
32 | config.noResponseRetries =
|
33 | config.noResponseRetries === undefined || config.noResponseRetries === null
|
34 | ? 2
|
35 | : config.noResponseRetries;
|
36 | config.retryDelayMultiplier = config.retryDelayMultiplier
|
37 | ? config.retryDelayMultiplier
|
38 | : 2;
|
39 | config.timeOfFirstRequest = config.timeOfFirstRequest
|
40 | ? config.timeOfFirstRequest
|
41 | : Date.now();
|
42 | config.totalTimeout = config.totalTimeout
|
43 | ? config.totalTimeout
|
44 | : Number.MAX_SAFE_INTEGER;
|
45 | config.maxRetryDelay = config.maxRetryDelay
|
46 | ? config.maxRetryDelay
|
47 | : Number.MAX_SAFE_INTEGER;
|
48 |
|
49 |
|
50 | const retryRanges = [
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | [100, 199],
|
60 | [408, 408],
|
61 | [429, 429],
|
62 | [500, 599],
|
63 | ];
|
64 | config.statusCodesToRetry = config.statusCodesToRetry || retryRanges;
|
65 |
|
66 | err.config.retryConfig = config;
|
67 |
|
68 | const shouldRetryFn = config.shouldRetry || shouldRetryRequest;
|
69 | if (!(await shouldRetryFn(err))) {
|
70 | return { shouldRetry: false, config: err.config };
|
71 | }
|
72 | const delay = getNextRetryDelay(config);
|
73 |
|
74 | err.config.retryConfig.currentRetryAttempt += 1;
|
75 |
|
76 | const backoff = config.retryBackoff
|
77 | ? config.retryBackoff(err, delay)
|
78 | : new Promise(resolve => {
|
79 | setTimeout(resolve, delay);
|
80 | });
|
81 |
|
82 | if (config.onRetryAttempt) {
|
83 | config.onRetryAttempt(err);
|
84 | }
|
85 |
|
86 | await backoff;
|
87 | return { shouldRetry: true, config: err.config };
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function shouldRetryRequest(err) {
|
94 | var _a;
|
95 | const config = getConfig(err);
|
96 |
|
97 |
|
98 | if (err.name === 'AbortError' || ((_a = err.error) === null || _a === void 0 ? void 0 : _a.name) === 'AbortError') {
|
99 | return false;
|
100 | }
|
101 |
|
102 | if (!config || config.retry === 0) {
|
103 | return false;
|
104 | }
|
105 |
|
106 | if (!err.response &&
|
107 | (config.currentRetryAttempt || 0) >= config.noResponseRetries) {
|
108 | return false;
|
109 | }
|
110 |
|
111 | if (!err.config.method ||
|
112 | config.httpMethodsToRetry.indexOf(err.config.method.toUpperCase()) < 0) {
|
113 | return false;
|
114 | }
|
115 |
|
116 |
|
117 | if (err.response && err.response.status) {
|
118 | let isInRange = false;
|
119 | for (const [min, max] of config.statusCodesToRetry) {
|
120 | const status = err.response.status;
|
121 | if (status >= min && status <= max) {
|
122 | isInRange = true;
|
123 | break;
|
124 | }
|
125 | }
|
126 | if (!isInRange) {
|
127 | return false;
|
128 | }
|
129 | }
|
130 |
|
131 | config.currentRetryAttempt = config.currentRetryAttempt || 0;
|
132 | if (config.currentRetryAttempt >= config.retry) {
|
133 | return false;
|
134 | }
|
135 | return true;
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | function getConfig(err) {
|
142 | if (err && err.config && err.config.retryConfig) {
|
143 | return err.config.retryConfig;
|
144 | }
|
145 | return;
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | function getNextRetryDelay(config) {
|
154 | var _a;
|
155 |
|
156 |
|
157 | const retryDelay = config.currentRetryAttempt ? 0 : (_a = config.retryDelay) !== null && _a !== void 0 ? _a : 100;
|
158 |
|
159 | const calculatedDelay = retryDelay +
|
160 | ((Math.pow(config.retryDelayMultiplier, config.currentRetryAttempt) - 1) /
|
161 | 2) *
|
162 | 1000;
|
163 | const maxAllowableDelay = config.totalTimeout - (Date.now() - config.timeOfFirstRequest);
|
164 | return Math.min(calculatedDelay, maxAllowableDelay, config.maxRetryDelay);
|
165 | }
|
166 |
|
\ | No newline at end of file |