itchlib 1.0.0 → 1.0.1
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/CHANGELOG.md +4 -0
- package/package.json +1 -1
- package/src/http.js +6 -3
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/http.js
CHANGED
|
@@ -4,6 +4,7 @@ const DEFAULT_TIMEOUT_MS = 15_000;
|
|
|
4
4
|
const DEFAULT_ATTEMPTS = 3;
|
|
5
5
|
const DEFAULT_MAX_RETRY_DELAY_MS = 30_000;
|
|
6
6
|
const RETRYABLE_STATUS = new Set([408, 429]);
|
|
7
|
+
const RETRYABLE_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);
|
|
7
8
|
|
|
8
9
|
function retryDelay(response, attempt) {
|
|
9
10
|
const retryAfter = response.headers?.get?.("retry-after");
|
|
@@ -26,7 +27,7 @@ export class ItchTransport {
|
|
|
26
27
|
fetch: fetchImpl = globalThis.fetch,
|
|
27
28
|
timeout = DEFAULT_TIMEOUT_MS,
|
|
28
29
|
headers = {},
|
|
29
|
-
userAgent = "itchlib/0.1
|
|
30
|
+
userAgent = "itchlib/1.0.1",
|
|
30
31
|
maxRetryDelay = DEFAULT_MAX_RETRY_DELAY_MS,
|
|
31
32
|
} = {}) {
|
|
32
33
|
if (typeof fetchImpl !== "function") throw new TypeError("A Fetch API implementation is required");
|
|
@@ -43,6 +44,8 @@ export class ItchTransport {
|
|
|
43
44
|
async request(url, options = {}) {
|
|
44
45
|
const timeout = options.timeout ?? this.timeout;
|
|
45
46
|
const { timeout: _timeout, maxRetryDelay: _maxRetryDelay, ...fetchOptions } = options;
|
|
47
|
+
const method = String(fetchOptions.method || "GET").toUpperCase();
|
|
48
|
+
const canRetry = RETRYABLE_METHODS.has(method);
|
|
46
49
|
let lastError;
|
|
47
50
|
|
|
48
51
|
for (let attempt = 1; attempt <= DEFAULT_ATTEMPTS; attempt += 1) {
|
|
@@ -63,7 +66,7 @@ export class ItchTransport {
|
|
|
63
66
|
headers: { ...this.headers, ...fetchOptions.headers },
|
|
64
67
|
signal: controller.signal,
|
|
65
68
|
});
|
|
66
|
-
if (!isRetryableStatus(response.status) || attempt === DEFAULT_ATTEMPTS) return response;
|
|
69
|
+
if (!canRetry || !isRetryableStatus(response.status) || attempt === DEFAULT_ATTEMPTS) return response;
|
|
67
70
|
const delay = retryDelay(response, attempt);
|
|
68
71
|
if (delay > this.maxRetryDelay) return response;
|
|
69
72
|
try {
|
|
@@ -89,7 +92,7 @@ export class ItchTransport {
|
|
|
89
92
|
code: "ITCH_NETWORK_ERROR",
|
|
90
93
|
cause,
|
|
91
94
|
});
|
|
92
|
-
if (attempt === DEFAULT_ATTEMPTS) throw lastError;
|
|
95
|
+
if (!canRetry || attempt === DEFAULT_ATTEMPTS) throw lastError;
|
|
93
96
|
await new Promise((resolve) =>
|
|
94
97
|
setTimeout(resolve, Math.round(500 * 2 ** (attempt - 1) * (0.8 + Math.random() * 0.4))),
|
|
95
98
|
);
|