@argos-ci/api-client 0.31.1 → 0.31.3

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.
Files changed (2) hide show
  1. package/dist/index.mjs +54 -3
  2. package/package.json +7 -3
package/dist/index.mjs CHANGED
@@ -6,6 +6,17 @@ const debug = createDebug("@argos-ci/api-client");
6
6
  //#endregion
7
7
  //#region src/fetch.ts
8
8
  const DEFAULT_TIMEOUT = 3e4;
9
+ const DEFAULT_MIN_TIMEOUT = 1e3;
10
+ /**
11
+ * A request the API rejects with `429 Too Many Requests` is retried up to
12
+ * `MAX_RATE_LIMIT_RETRIES` times, apart from the retries for server errors,
13
+ * waiting at most `MAX_RATE_LIMIT_DELAY` before each retry whatever
14
+ * `Retry-After` asks. The API counts requests in fixed 5-minute windows, so
15
+ * five waits of a minute see a request through a whole window, and rate
16
+ * limiting never holds a request up for more than five minutes.
17
+ */
18
+ const MAX_RATE_LIMIT_RETRIES = 5;
19
+ const MAX_RATE_LIMIT_DELAY = 6e4;
9
20
  var APIError = class extends Error {
10
21
  /**
11
22
  * HTTP status code of the response that triggered the error, when available.
@@ -47,16 +58,56 @@ async function createRequestFactory(request, timeout) {
47
58
  });
48
59
  };
49
60
  }
61
+ /**
62
+ * Get how long to wait before retrying a `429 Too Many Requests` response: its
63
+ * `Retry-After`, or exponential backoff from `minTimeout` when the header is
64
+ * missing or invalid. The API sends a number of seconds; any other value, an
65
+ * HTTP date included, counts as invalid.
66
+ */
67
+ function getRateLimitDelay(response, minTimeout, retry) {
68
+ const retryAfter = response.headers.get("retry-after")?.trim() ?? "";
69
+ const delay = /^\d+$/.test(retryAfter) ? Number(retryAfter) * 1e3 : minTimeout * 2 ** retry;
70
+ return Math.min(delay, MAX_RATE_LIMIT_DELAY);
71
+ }
72
+ /**
73
+ * Wait for `ms`, or reject with the signal's reason as soon as it aborts.
74
+ */
75
+ function wait(ms, signal) {
76
+ return new Promise((resolve, reject) => {
77
+ signal.throwIfAborted();
78
+ const onAbort = () => {
79
+ clearTimeout(timer);
80
+ reject(signal.reason);
81
+ };
82
+ const timer = setTimeout(() => {
83
+ signal.removeEventListener("abort", onAbort);
84
+ resolve();
85
+ }, ms);
86
+ signal.addEventListener("abort", onAbort, { once: true });
87
+ });
88
+ }
50
89
  async function apiFetch(input, options = {}) {
51
90
  input.signal.throwIfAborted();
52
91
  const fetchImpl = options.fetch ?? fetch;
92
+ const minTimeout = options.minTimeout ?? DEFAULT_MIN_TIMEOUT;
53
93
  const createRequest = await createRequestFactory(input, options.timeout ?? DEFAULT_TIMEOUT);
54
- return pRetry(async (attemptNumber) => {
55
- const response = await fetchImpl(createRequest(attemptNumber - 1));
94
+ let retryAttempt = 0;
95
+ let rateLimitRetries = 0;
96
+ const send = () => fetchImpl(createRequest(retryAttempt++));
97
+ return pRetry(async () => {
98
+ let response = await send();
99
+ while (response.status === 429 && rateLimitRetries < MAX_RATE_LIMIT_RETRIES) {
100
+ const delay = getRateLimitDelay(response, minTimeout, rateLimitRetries);
101
+ debug(`API request rate limited, retrying in ${delay}ms (${MAX_RATE_LIMIT_RETRIES - rateLimitRetries} left)`);
102
+ rateLimitRetries++;
103
+ await response.body?.cancel();
104
+ await wait(delay, input.signal);
105
+ response = await send();
106
+ }
56
107
  if (response.status >= 500) throw new APIError(`Internal Server Error (${response.status})`);
57
108
  return response;
58
109
  }, {
59
- minTimeout: options.minTimeout,
110
+ minTimeout,
60
111
  retries: options.retries ?? 3,
61
112
  shouldRetry: () => !input.signal.aborted,
62
113
  onFailedAttempt: (context) => {
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.31.1",
4
+ "version": "0.31.3",
5
5
  "author": "Smooth Code",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -14,9 +14,13 @@
14
14
  "url": "https://github.com/argos-ci/argos-javascript/issues"
15
15
  },
16
16
  "keywords": [
17
+ "argos",
18
+ "argos-ci",
17
19
  "api",
20
+ "api-client",
18
21
  "openapi",
19
- "argos"
22
+ "rest-api",
23
+ "visual-testing"
20
24
  ],
21
25
  "publishConfig": {
22
26
  "access": "public"
@@ -54,5 +58,5 @@
54
58
  "openapi-fetch": "^0.17.0",
55
59
  "p-retry": "^8.0.1"
56
60
  },
57
- "gitHead": "4f0edc08d8549534dd3a682bbe034ebcb604bddf"
61
+ "gitHead": "810aab42c890851ea19ba96f49c4f167a5f2e3df"
58
62
  }