@argos-ci/api-client 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +54 -30
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -39,7 +39,7 @@ function isNetworkError(error) {
|
|
|
39
39
|
return errorMessages.has(message);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
// ../../node_modules/.pnpm/p-retry@7.
|
|
42
|
+
// ../../node_modules/.pnpm/p-retry@7.1.0/node_modules/p-retry/index.js
|
|
43
43
|
function validateRetries(retries) {
|
|
44
44
|
if (typeof retries === "number") {
|
|
45
45
|
if (retries < 0) {
|
|
@@ -80,43 +80,57 @@ var AbortError = class extends Error {
|
|
|
80
80
|
this.message = message;
|
|
81
81
|
}
|
|
82
82
|
};
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
return Object.freeze({
|
|
86
|
-
error,
|
|
87
|
-
attemptNumber,
|
|
88
|
-
retriesLeft
|
|
89
|
-
});
|
|
90
|
-
};
|
|
91
|
-
function calculateDelay(attempt, options) {
|
|
83
|
+
function calculateDelay(retriesConsumed, options) {
|
|
84
|
+
const attempt = Math.max(1, retriesConsumed + 1);
|
|
92
85
|
const random = options.randomize ? Math.random() + 1 : 1;
|
|
93
|
-
let timeout = Math.round(random *
|
|
86
|
+
let timeout = Math.round(random * options.minTimeout * options.factor ** (attempt - 1));
|
|
94
87
|
timeout = Math.min(timeout, options.maxTimeout);
|
|
95
88
|
return timeout;
|
|
96
89
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
normalizedError = new TypeError(`Non-error was thrown: "${normalizedError}". You should only throw errors.`);
|
|
90
|
+
function calculateRemainingTime(start, max) {
|
|
91
|
+
if (!Number.isFinite(max)) {
|
|
92
|
+
return max;
|
|
101
93
|
}
|
|
94
|
+
return max - (performance.now() - start);
|
|
95
|
+
}
|
|
96
|
+
async function onAttemptFailure({ error, attemptNumber, retriesConsumed, startTime, options }) {
|
|
97
|
+
const normalizedError = error instanceof Error ? error : new TypeError(`Non-error was thrown: "${error}". You should only throw errors.`);
|
|
102
98
|
if (normalizedError instanceof AbortError) {
|
|
103
99
|
throw normalizedError.originalError;
|
|
104
100
|
}
|
|
105
|
-
|
|
101
|
+
const retriesLeft = Number.isFinite(options.retries) ? Math.max(0, options.retries - retriesConsumed) : options.retries;
|
|
102
|
+
const maxRetryTime = options.maxRetryTime ?? Number.POSITIVE_INFINITY;
|
|
103
|
+
const context = Object.freeze({
|
|
104
|
+
error: normalizedError,
|
|
105
|
+
attemptNumber,
|
|
106
|
+
retriesLeft,
|
|
107
|
+
retriesConsumed
|
|
108
|
+
});
|
|
109
|
+
await options.onFailedAttempt(context);
|
|
110
|
+
if (calculateRemainingTime(startTime, maxRetryTime) <= 0) {
|
|
106
111
|
throw normalizedError;
|
|
107
112
|
}
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (currentTime - startTime >= maxRetryTime || attemptNumber >= options.retries + 1 || !await options.shouldRetry(context)) {
|
|
113
|
+
const consumeRetry = await options.shouldConsumeRetry(context);
|
|
114
|
+
const remainingTime = calculateRemainingTime(startTime, maxRetryTime);
|
|
115
|
+
if (remainingTime <= 0 || retriesLeft <= 0) {
|
|
112
116
|
throw normalizedError;
|
|
113
117
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
118
|
+
if (normalizedError instanceof TypeError && !isNetworkError(normalizedError)) {
|
|
119
|
+
if (consumeRetry) {
|
|
120
|
+
throw normalizedError;
|
|
121
|
+
}
|
|
122
|
+
options.signal?.throwIfAborted();
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
if (!await options.shouldRetry(context)) {
|
|
117
126
|
throw normalizedError;
|
|
118
127
|
}
|
|
119
|
-
|
|
128
|
+
if (!consumeRetry) {
|
|
129
|
+
options.signal?.throwIfAborted();
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
const delayTime = calculateDelay(retriesConsumed, options);
|
|
133
|
+
const finalDelay = Math.min(delayTime, remainingTime);
|
|
120
134
|
if (finalDelay > 0) {
|
|
121
135
|
await new Promise((resolve, reject) => {
|
|
122
136
|
const onAbort = () => {
|
|
@@ -135,6 +149,7 @@ async function onAttemptFailure(error, attemptNumber, options, startTime, maxRet
|
|
|
135
149
|
});
|
|
136
150
|
}
|
|
137
151
|
options.signal?.throwIfAborted();
|
|
152
|
+
return true;
|
|
138
153
|
}
|
|
139
154
|
async function pRetry(input, options = {}) {
|
|
140
155
|
options = { ...options };
|
|
@@ -146,23 +161,24 @@ async function pRetry(input, options = {}) {
|
|
|
146
161
|
options.factor ??= 2;
|
|
147
162
|
options.minTimeout ??= 1e3;
|
|
148
163
|
options.maxTimeout ??= Number.POSITIVE_INFINITY;
|
|
164
|
+
options.maxRetryTime ??= Number.POSITIVE_INFINITY;
|
|
149
165
|
options.randomize ??= false;
|
|
150
166
|
options.onFailedAttempt ??= () => {
|
|
151
167
|
};
|
|
152
168
|
options.shouldRetry ??= () => true;
|
|
169
|
+
options.shouldConsumeRetry ??= () => true;
|
|
153
170
|
validateNumberOption("factor", options.factor, { min: 0, allowInfinity: false });
|
|
154
171
|
validateNumberOption("minTimeout", options.minTimeout, { min: 0, allowInfinity: false });
|
|
155
172
|
validateNumberOption("maxTimeout", options.maxTimeout, { min: 0, allowInfinity: true });
|
|
156
|
-
|
|
157
|
-
validateNumberOption("maxRetryTime", resolvedMaxRetryTime, { min: 0, allowInfinity: true });
|
|
173
|
+
validateNumberOption("maxRetryTime", options.maxRetryTime, { min: 0, allowInfinity: true });
|
|
158
174
|
if (!(options.factor > 0)) {
|
|
159
175
|
options.factor = 1;
|
|
160
176
|
}
|
|
161
177
|
options.signal?.throwIfAborted();
|
|
162
178
|
let attemptNumber = 0;
|
|
163
|
-
|
|
164
|
-
const
|
|
165
|
-
while (
|
|
179
|
+
let retriesConsumed = 0;
|
|
180
|
+
const startTime = performance.now();
|
|
181
|
+
while (Number.isFinite(options.retries) ? retriesConsumed <= options.retries : true) {
|
|
166
182
|
attemptNumber++;
|
|
167
183
|
try {
|
|
168
184
|
options.signal?.throwIfAborted();
|
|
@@ -170,7 +186,15 @@ async function pRetry(input, options = {}) {
|
|
|
170
186
|
options.signal?.throwIfAborted();
|
|
171
187
|
return result;
|
|
172
188
|
} catch (error) {
|
|
173
|
-
await onAttemptFailure(
|
|
189
|
+
if (await onAttemptFailure({
|
|
190
|
+
error,
|
|
191
|
+
attemptNumber,
|
|
192
|
+
retriesConsumed,
|
|
193
|
+
startTime,
|
|
194
|
+
options
|
|
195
|
+
})) {
|
|
196
|
+
retriesConsumed++;
|
|
197
|
+
}
|
|
174
198
|
}
|
|
175
199
|
}
|
|
176
200
|
throw new Error("Retry attempts exhausted without throwing an error.");
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@argos-ci/api-client",
|
|
3
3
|
"description": "Argos API Client library.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.13.0",
|
|
5
5
|
"author": "Smooth Code",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -45,12 +45,12 @@
|
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@types/debug": "^4.1.12",
|
|
47
47
|
"@types/node": "catalog:",
|
|
48
|
-
"openapi-typescript": "^7.
|
|
49
|
-
"p-retry": "^7.
|
|
48
|
+
"openapi-typescript": "^7.10.1",
|
|
49
|
+
"p-retry": "^7.1.0"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"debug": "^4.4.3",
|
|
53
|
-
"openapi-fetch": "^0.
|
|
53
|
+
"openapi-fetch": "^0.15.0"
|
|
54
54
|
},
|
|
55
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "7b770d9d0c621980319bd5a195413f96c54d5752"
|
|
56
56
|
}
|