@indra211/exios 1.0.0 → 2.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/README.md +58 -14
- package/dist/index.cjs +939 -395
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +74 -8
- package/dist/index.js +940 -395
- package/dist/index.node.cjs +2545 -0
- package/dist/index.node.js +2515 -0
- package/package.json +62 -4
package/dist/index.cjs
CHANGED
|
@@ -34,6 +34,8 @@ var ExiosError = class _ExiosError extends Error {
|
|
|
34
34
|
static ERR_CANCELED = "ERR_CANCELED";
|
|
35
35
|
static ERR_BAD_OPTION_VALUE = "ERR_BAD_OPTION_VALUE";
|
|
36
36
|
static ERR_CIRCUIT_OPEN = "ERR_CIRCUIT_OPEN";
|
|
37
|
+
static ERR_NOT_SUPPORT = "ERR_NOT_SUPPORT";
|
|
38
|
+
static ERR_FR_TOO_MANY_REDIRECTS = "ERR_FR_TOO_MANY_REDIRECTS";
|
|
37
39
|
code;
|
|
38
40
|
config;
|
|
39
41
|
request;
|
|
@@ -94,6 +96,111 @@ function isCancel(v) {
|
|
|
94
96
|
return v instanceof CanceledError || isExiosError(v) && v.code === ExiosError.ERR_CANCELED;
|
|
95
97
|
}
|
|
96
98
|
|
|
99
|
+
// src/adapters/registry.ts
|
|
100
|
+
var registry = /* @__PURE__ */ new Map();
|
|
101
|
+
function registerAdapter(name, entry) {
|
|
102
|
+
registry.set(name, entry);
|
|
103
|
+
}
|
|
104
|
+
function isAdapterAvailable(name, config) {
|
|
105
|
+
const entry = registry.get(name);
|
|
106
|
+
return !!entry && entry.isAvailable(config);
|
|
107
|
+
}
|
|
108
|
+
function resolveAdapter(config) {
|
|
109
|
+
const list = config.adapter === void 0 ? [] : Array.isArray(config.adapter) ? config.adapter : [config.adapter];
|
|
110
|
+
for (const item of list) {
|
|
111
|
+
if (typeof item === "function") return item;
|
|
112
|
+
const entry = registry.get(item);
|
|
113
|
+
if (entry && entry.isAvailable(config)) return entry.adapter;
|
|
114
|
+
}
|
|
115
|
+
const names = list.map((a) => typeof a === "function" ? "custom" : a);
|
|
116
|
+
throw new ExiosError(
|
|
117
|
+
`Adapter ${names.join(", ") || "(none)"} is not available in this environment`,
|
|
118
|
+
ExiosError.ERR_NOT_SUPPORT,
|
|
119
|
+
config
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/core/headers.ts
|
|
124
|
+
var HEADER_BUCKETS = [
|
|
125
|
+
"common",
|
|
126
|
+
"get",
|
|
127
|
+
"post",
|
|
128
|
+
"put",
|
|
129
|
+
"patch",
|
|
130
|
+
"delete",
|
|
131
|
+
"head",
|
|
132
|
+
"options"
|
|
133
|
+
];
|
|
134
|
+
function normalizeHeaders(h) {
|
|
135
|
+
const out = {};
|
|
136
|
+
if (!h) return out;
|
|
137
|
+
if (typeof Headers !== "undefined" && h instanceof Headers) {
|
|
138
|
+
h.forEach((v, k) => {
|
|
139
|
+
out[k.toLowerCase()] = v;
|
|
140
|
+
});
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
for (const [k, v] of Object.entries(h)) {
|
|
144
|
+
if (v === null || v === void 0 || v === false) continue;
|
|
145
|
+
out[k.toLowerCase()] = String(v);
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
function flattenHeaders(h, method) {
|
|
150
|
+
if (!h) return {};
|
|
151
|
+
if (typeof Headers !== "undefined" && h instanceof Headers)
|
|
152
|
+
return normalizeHeaders(h);
|
|
153
|
+
const src = h;
|
|
154
|
+
const isBucket2 = (k) => HEADER_BUCKETS.includes(k.toLowerCase());
|
|
155
|
+
const bucket = (name) => {
|
|
156
|
+
const key = Object.keys(src).find((k) => k.toLowerCase() === name);
|
|
157
|
+
const v = key === void 0 ? void 0 : src[key];
|
|
158
|
+
return v && typeof v === "object" ? v : void 0;
|
|
159
|
+
};
|
|
160
|
+
const top = {};
|
|
161
|
+
for (const [k, v] of Object.entries(src)) {
|
|
162
|
+
if (isBucket2(k)) continue;
|
|
163
|
+
Object.assign(top, normalizeHeaders({ [k]: v }));
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
...normalizeHeaders(bucket("common")),
|
|
167
|
+
...normalizeHeaders(bucket(method)),
|
|
168
|
+
...top
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function finalizeHeaders(h, method) {
|
|
172
|
+
if (!h || typeof h !== "object") return {};
|
|
173
|
+
if (typeof Headers !== "undefined" && h instanceof Headers)
|
|
174
|
+
return normalizeHeaders(h);
|
|
175
|
+
const src = h;
|
|
176
|
+
const isBucket2 = (k) => HEADER_BUCKETS.includes(k.toLowerCase());
|
|
177
|
+
const out = {};
|
|
178
|
+
for (const name of ["common", method]) {
|
|
179
|
+
for (const [k, v] of Object.entries(src)) {
|
|
180
|
+
if (k.toLowerCase() === name && v && typeof v === "object")
|
|
181
|
+
Object.assign(out, normalizeHeaders(v));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const [k, v] of Object.entries(src)) {
|
|
185
|
+
if (isBucket2(k)) continue;
|
|
186
|
+
const lk = k.toLowerCase();
|
|
187
|
+
if (v === null || v === void 0 || v === false) delete out[lk];
|
|
188
|
+
else out[lk] = String(v);
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
function headersToObject(h) {
|
|
193
|
+
const out = {};
|
|
194
|
+
h.forEach((v, k) => {
|
|
195
|
+
if (k.toLowerCase() !== "set-cookie") out[k.toLowerCase()] = v;
|
|
196
|
+
});
|
|
197
|
+
if (h.has("set-cookie")) {
|
|
198
|
+
const cookies = h.getSetCookie?.();
|
|
199
|
+
out["set-cookie"] = cookies ?? h.get("set-cookie");
|
|
200
|
+
}
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
|
|
97
204
|
// src/core/buildURL.ts
|
|
98
205
|
var ABSOLUTE_URL = /^([a-z][a-z\d+\-.]*:)?\/\//i;
|
|
99
206
|
function encode(s) {
|
|
@@ -174,7 +281,10 @@ function isPlainObject2(v) {
|
|
|
174
281
|
return proto === Object.prototype || proto === null;
|
|
175
282
|
}
|
|
176
283
|
function isPassThrough(data) {
|
|
177
|
-
return typeof FormData !== "undefined" && data instanceof FormData || typeof Blob !== "undefined" && data instanceof Blob || data instanceof ArrayBuffer || ArrayBuffer.isView(data) || typeof ReadableStream !== "undefined" && data instanceof ReadableStream || data instanceof URLSearchParams;
|
|
284
|
+
return typeof FormData !== "undefined" && data instanceof FormData || typeof Blob !== "undefined" && data instanceof Blob || data instanceof ArrayBuffer || ArrayBuffer.isView(data) || typeof ReadableStream !== "undefined" && data instanceof ReadableStream || data instanceof URLSearchParams || isNodeStream(data);
|
|
285
|
+
}
|
|
286
|
+
function isNodeStream(data) {
|
|
287
|
+
return typeof data === "object" && data !== null && typeof data.pipe === "function" && typeof data.on === "function";
|
|
178
288
|
}
|
|
179
289
|
function defaultTransformRequest(data, headers) {
|
|
180
290
|
if (data === void 0 || data === null) return void 0;
|
|
@@ -228,309 +338,285 @@ function runTransforms(fns, config, data, headers, status) {
|
|
|
228
338
|
return data;
|
|
229
339
|
}
|
|
230
340
|
|
|
231
|
-
// src/
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
function
|
|
341
|
+
// src/adapters/shared.ts
|
|
342
|
+
function basicAuth(auth) {
|
|
343
|
+
const bytes = new TextEncoder().encode(`${auth.username}:${auth.password}`);
|
|
344
|
+
let bin = "";
|
|
345
|
+
for (const b of bytes) bin += String.fromCharCode(b);
|
|
346
|
+
return `Basic ${btoa(bin)}`;
|
|
347
|
+
}
|
|
348
|
+
function bodyLength(body) {
|
|
349
|
+
if (typeof body === "string") return new TextEncoder().encode(body).byteLength;
|
|
350
|
+
if (typeof Blob !== "undefined" && body instanceof Blob) return body.size;
|
|
351
|
+
if (body instanceof ArrayBuffer) return body.byteLength;
|
|
352
|
+
if (ArrayBuffer.isView(body)) return body.byteLength;
|
|
353
|
+
if (typeof URLSearchParams !== "undefined" && body instanceof URLSearchParams)
|
|
354
|
+
return new TextEncoder().encode(body.toString()).byteLength;
|
|
355
|
+
return void 0;
|
|
356
|
+
}
|
|
357
|
+
var isWebStream = (v) => typeof ReadableStream !== "undefined" && v instanceof ReadableStream;
|
|
358
|
+
function isSameOrigin(url) {
|
|
359
|
+
const loc = globalThis.location;
|
|
360
|
+
if (!loc) return false;
|
|
361
|
+
try {
|
|
362
|
+
return new URL(url, loc.href).origin === loc.origin;
|
|
363
|
+
} catch {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
function readCookie(name) {
|
|
368
|
+
const doc = globalThis.document;
|
|
369
|
+
const cookie = doc?.cookie;
|
|
370
|
+
if (!cookie) return void 0;
|
|
371
|
+
for (const part of cookie.split(";")) {
|
|
372
|
+
const i = part.indexOf("=");
|
|
373
|
+
if (i < 0 || part.slice(0, i).trim() !== name) continue;
|
|
374
|
+
try {
|
|
375
|
+
return decodeURIComponent(part.slice(i + 1).trim());
|
|
376
|
+
} catch {
|
|
377
|
+
return part.slice(i + 1).trim();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return void 0;
|
|
381
|
+
}
|
|
382
|
+
function applyXsrf(config, url, headers) {
|
|
383
|
+
if (!globalThis.document) return;
|
|
384
|
+
const opt = config.withXSRFToken;
|
|
385
|
+
const decision = typeof opt === "function" ? opt(config) : opt;
|
|
386
|
+
if (decision === false) return;
|
|
387
|
+
if (decision !== true && !isSameOrigin(url)) return;
|
|
388
|
+
const name = config.xsrfHeaderName?.toLowerCase();
|
|
389
|
+
if (!name || !config.xsrfCookieName || name in headers) return;
|
|
390
|
+
const value = readCookie(config.xsrfCookieName);
|
|
391
|
+
if (value !== void 0) headers[name] = value;
|
|
392
|
+
}
|
|
393
|
+
function prepareRequest(config) {
|
|
394
|
+
const url = buildURL(
|
|
395
|
+
buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls),
|
|
396
|
+
config.params,
|
|
397
|
+
config.paramsSerializer
|
|
398
|
+
);
|
|
399
|
+
const headers = { ...config.headers };
|
|
400
|
+
if (config.auth) headers.authorization = basicAuth(config.auth);
|
|
401
|
+
applyXsrf(config, url, headers);
|
|
402
|
+
const hasBody = config.method !== "get" && config.method !== "head";
|
|
403
|
+
const body = hasBody ? runTransforms(config.transformRequest, config, config.data, headers) : void 0;
|
|
404
|
+
const length = bodyLength(body);
|
|
405
|
+
const maxBody = config.maxBodyLength ?? -1;
|
|
406
|
+
if (maxBody > -1 && length !== void 0 && length > maxBody) {
|
|
407
|
+
throw new ExiosError(
|
|
408
|
+
`Request body larger than maxBodyLength limit`,
|
|
409
|
+
ExiosError.ERR_BAD_REQUEST,
|
|
410
|
+
config
|
|
411
|
+
);
|
|
412
|
+
}
|
|
258
413
|
return {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
head: {},
|
|
266
|
-
options: {}
|
|
414
|
+
url,
|
|
415
|
+
method: config.method.toUpperCase(),
|
|
416
|
+
headers,
|
|
417
|
+
body,
|
|
418
|
+
replayable: !isOneShotBody(body),
|
|
419
|
+
length
|
|
267
420
|
};
|
|
268
421
|
}
|
|
269
|
-
function
|
|
422
|
+
function decodeText(bytes, config) {
|
|
423
|
+
return new TextDecoder(config.responseEncoding ?? "utf-8").decode(bytes);
|
|
424
|
+
}
|
|
425
|
+
function assertEncoding(config) {
|
|
426
|
+
const enc = config.responseEncoding;
|
|
427
|
+
if (enc === void 0) return;
|
|
428
|
+
try {
|
|
429
|
+
new TextDecoder(enc);
|
|
430
|
+
} catch {
|
|
431
|
+
throw new ExiosError(
|
|
432
|
+
`Unsupported responseEncoding: ${enc}`,
|
|
433
|
+
ExiosError.ERR_BAD_OPTION_VALUE,
|
|
434
|
+
config
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
function finishResponse(config, raw) {
|
|
439
|
+
const response = {
|
|
440
|
+
data: raw.data,
|
|
441
|
+
status: raw.status,
|
|
442
|
+
statusText: raw.statusText,
|
|
443
|
+
headers: raw.headers,
|
|
444
|
+
config,
|
|
445
|
+
request: raw.request,
|
|
446
|
+
meta: { attempts: 0 }
|
|
447
|
+
};
|
|
448
|
+
try {
|
|
449
|
+
response.data = runTransforms(
|
|
450
|
+
config.transformResponse,
|
|
451
|
+
config,
|
|
452
|
+
raw.data,
|
|
453
|
+
raw.headers,
|
|
454
|
+
raw.status
|
|
455
|
+
);
|
|
456
|
+
} catch (err) {
|
|
457
|
+
cancelBody(raw.data);
|
|
458
|
+
if (err instanceof ExiosError) {
|
|
459
|
+
err.request ??= raw.request;
|
|
460
|
+
err.response ??= response;
|
|
461
|
+
err.status ??= response.status;
|
|
462
|
+
}
|
|
463
|
+
throw err;
|
|
464
|
+
}
|
|
465
|
+
return response;
|
|
466
|
+
}
|
|
467
|
+
function createProgress(cb, kind, total) {
|
|
468
|
+
let loaded = 0;
|
|
469
|
+
let reported = -1;
|
|
470
|
+
const start = Date.now();
|
|
471
|
+
const emit = (bytes, event) => {
|
|
472
|
+
if (!cb) return;
|
|
473
|
+
const elapsed = (Date.now() - start) / 1e3;
|
|
474
|
+
const rate = elapsed > 0 ? loaded / elapsed : void 0;
|
|
475
|
+
const e = {
|
|
476
|
+
loaded,
|
|
477
|
+
total,
|
|
478
|
+
progress: total ? loaded / total : void 0,
|
|
479
|
+
bytes,
|
|
480
|
+
rate,
|
|
481
|
+
estimated: rate && total ? (total - loaded) / rate : void 0
|
|
482
|
+
};
|
|
483
|
+
if (kind === "upload") e.upload = true;
|
|
484
|
+
else e.download = true;
|
|
485
|
+
if (event !== void 0) e.event = event;
|
|
486
|
+
reported = loaded;
|
|
487
|
+
cb(e);
|
|
488
|
+
};
|
|
270
489
|
return {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
Accept: "application/json, text/plain, */*"
|
|
275
|
-
}),
|
|
276
|
-
transformRequest: [defaultTransformRequest],
|
|
277
|
-
transformResponse: [defaultTransformResponse],
|
|
278
|
-
validateStatus: (s) => s >= 200 && s < 300,
|
|
279
|
-
maxContentLength: -1,
|
|
280
|
-
maxBodyLength: -1,
|
|
281
|
-
transitional: { silentJSONParsing: true, clarifyTimeoutError: false },
|
|
282
|
-
retry: {
|
|
283
|
-
...DEFAULT_RETRY,
|
|
284
|
-
methods: [...DEFAULT_RETRY.methods],
|
|
285
|
-
statusCodes: [...DEFAULT_RETRY.statusCodes]
|
|
490
|
+
add(n, event) {
|
|
491
|
+
loaded += n;
|
|
492
|
+
emit(n, event);
|
|
286
493
|
},
|
|
287
|
-
|
|
494
|
+
done() {
|
|
495
|
+
if (reported !== loaded) emit(0);
|
|
496
|
+
}
|
|
288
497
|
};
|
|
289
498
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const code = o.error?.code;
|
|
299
|
-
if (code === ExiosError.ERR_CANCELED) return false;
|
|
300
|
-
if (code !== void 0 && FAILURE_CODES.has(code)) return true;
|
|
301
|
-
const status = o.response?.status ?? o.error?.response?.status;
|
|
302
|
-
if (status === void 0) return false;
|
|
303
|
-
return status >= 500 || status === 408 || status === 429;
|
|
304
|
-
}
|
|
305
|
-
var CircuitBreaker = class {
|
|
306
|
-
constructor(key, cfg, now = Date.now) {
|
|
307
|
-
this.key = key;
|
|
308
|
-
this.cfg = cfg;
|
|
309
|
-
this.now = now;
|
|
499
|
+
var isNodeStream2 = (v) => typeof v === "object" && v !== null && typeof v.pipe === "function" && typeof v.destroy === "function";
|
|
500
|
+
function cancelBody(data) {
|
|
501
|
+
try {
|
|
502
|
+
if (isWebStream(data)) {
|
|
503
|
+
if (!data.locked) data.cancel().catch(() => {
|
|
504
|
+
});
|
|
505
|
+
} else if (isNodeStream2(data)) data.destroy();
|
|
506
|
+
} catch {
|
|
310
507
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
508
|
+
}
|
|
509
|
+
function abortError(signal, config, request) {
|
|
510
|
+
const r = signal?.reason;
|
|
511
|
+
if (r instanceof CanceledError) {
|
|
512
|
+
r.config ??= config;
|
|
513
|
+
r.request ??= request;
|
|
514
|
+
return r;
|
|
515
|
+
}
|
|
516
|
+
return new CanceledError("canceled", config, request);
|
|
517
|
+
}
|
|
518
|
+
function isOneShotBody(body) {
|
|
519
|
+
return isWebStream(body) || isNodeStream2(body);
|
|
520
|
+
}
|
|
521
|
+
function whenBodyDone(data, fn) {
|
|
522
|
+
let called = false;
|
|
523
|
+
const once = () => {
|
|
524
|
+
if (!called) {
|
|
525
|
+
called = true;
|
|
526
|
+
fn();
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
if (isNodeStream2(data)) {
|
|
530
|
+
data.once("close", once);
|
|
531
|
+
return data;
|
|
325
532
|
}
|
|
326
|
-
|
|
327
|
-
|
|
533
|
+
if (!isWebStream(data)) {
|
|
534
|
+
once();
|
|
535
|
+
return data;
|
|
328
536
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
if (this.probes.size >= this.cfg.halfOpenMaxRequests) {
|
|
344
|
-
throw new CircuitOpenError(this.key, this.now(), config);
|
|
345
|
-
}
|
|
346
|
-
const probe = {
|
|
347
|
-
probe: true,
|
|
348
|
-
done: false,
|
|
349
|
-
epoch: this.epoch,
|
|
350
|
-
at: this.now()
|
|
351
|
-
};
|
|
352
|
-
this.probes.add(probe);
|
|
353
|
-
return probe;
|
|
354
|
-
}
|
|
355
|
-
return { probe: false, done: false, epoch: this.epoch, at: this.now() };
|
|
356
|
-
}
|
|
357
|
-
/** Called before each retry: a non-probe ticket may not continue unless closed. */
|
|
358
|
-
check(ticket) {
|
|
359
|
-
if (this._state === "closed" || ticket.probe && ticket.epoch === this.epoch)
|
|
360
|
-
return;
|
|
361
|
-
throw new CircuitOpenError(
|
|
362
|
-
this.key,
|
|
363
|
-
this._state === "open" ? this.retryAt() : this.now()
|
|
364
|
-
);
|
|
365
|
-
}
|
|
366
|
-
record(ticket, failed) {
|
|
367
|
-
if (ticket.done) return;
|
|
368
|
-
ticket.done = true;
|
|
369
|
-
if (ticket.epoch !== this.epoch) return;
|
|
370
|
-
if (ticket.probe) {
|
|
371
|
-
this.probes.delete(ticket);
|
|
372
|
-
if (this._state !== "half-open") return;
|
|
373
|
-
if (failed) return this.transition("open");
|
|
374
|
-
if (++this.probeSuccesses >= this.cfg.successThreshold)
|
|
375
|
-
this.transition("closed");
|
|
376
|
-
return;
|
|
377
|
-
}
|
|
378
|
-
if (this._state !== "closed") return;
|
|
379
|
-
if (this.cfg.windowMs !== void 0) {
|
|
380
|
-
const t = this.now();
|
|
381
|
-
this.samples.push({ t, failed });
|
|
382
|
-
if (failed) this.windowFailures++;
|
|
383
|
-
let i = 0;
|
|
384
|
-
while (i < this.samples.length && t - this.samples[i].t > this.cfg.windowMs) {
|
|
385
|
-
if (this.samples[i].failed) this.windowFailures--;
|
|
386
|
-
i++;
|
|
387
|
-
}
|
|
388
|
-
if (i > 0) this.samples.splice(0, i);
|
|
389
|
-
if (this.samples.length >= this.cfg.minRequests && this.windowFailures / this.samples.length >= this.cfg.failureRateThreshold) {
|
|
390
|
-
this.transition("open");
|
|
537
|
+
const reader = data.getReader();
|
|
538
|
+
return new ReadableStream({
|
|
539
|
+
async pull(controller) {
|
|
540
|
+
try {
|
|
541
|
+
const r = await reader.read();
|
|
542
|
+
if (r.done) {
|
|
543
|
+
once();
|
|
544
|
+
controller.close();
|
|
545
|
+
} else controller.enqueue(r.value);
|
|
546
|
+
} catch (err) {
|
|
547
|
+
once();
|
|
548
|
+
controller.error(err);
|
|
391
549
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
397
|
-
/** No outcome (cancel): frees a probe slot, never changes state. */
|
|
398
|
-
release(ticket) {
|
|
399
|
-
if (ticket.done) return;
|
|
400
|
-
ticket.done = true;
|
|
401
|
-
if (ticket.probe && ticket.epoch === this.epoch) this.probes.delete(ticket);
|
|
402
|
-
}
|
|
403
|
-
transition(to) {
|
|
404
|
-
const from = this._state;
|
|
405
|
-
this._state = to;
|
|
406
|
-
this.epoch++;
|
|
407
|
-
this.probes = /* @__PURE__ */ new Set();
|
|
408
|
-
this.windowFailures = 0;
|
|
409
|
-
this.probeSuccesses = 0;
|
|
410
|
-
this.consecutive = 0;
|
|
411
|
-
this.samples = [];
|
|
412
|
-
if (to === "open") this.openedAt = this.now();
|
|
413
|
-
try {
|
|
414
|
-
this.cfg.onStateChange?.(this.key, from, to);
|
|
415
|
-
} catch {
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
var BreakerRegistry = class {
|
|
420
|
-
constructor(getConfig, now = Date.now) {
|
|
421
|
-
this.getConfig = getConfig;
|
|
422
|
-
this.now = now;
|
|
423
|
-
}
|
|
424
|
-
getConfig;
|
|
425
|
-
now;
|
|
426
|
-
breakers = /* @__PURE__ */ new Map();
|
|
427
|
-
get(key) {
|
|
428
|
-
const cfg = this.getConfig();
|
|
429
|
-
if (cfg === false) return null;
|
|
430
|
-
let b = this.breakers.get(key);
|
|
431
|
-
if (!b) {
|
|
432
|
-
b = new CircuitBreaker(key, cfg, this.now);
|
|
433
|
-
this.breakers.set(key, b);
|
|
550
|
+
},
|
|
551
|
+
cancel(reason) {
|
|
552
|
+
once();
|
|
553
|
+
return reader.cancel(reason);
|
|
434
554
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
state(key) {
|
|
438
|
-
return this.breakers.get(key)?.state ?? "closed";
|
|
439
|
-
}
|
|
440
|
-
reset(key) {
|
|
441
|
-
if (key === void 0) this.breakers.clear();
|
|
442
|
-
else this.breakers.delete(key);
|
|
443
|
-
}
|
|
444
|
-
};
|
|
555
|
+
});
|
|
556
|
+
}
|
|
445
557
|
|
|
446
|
-
// src/
|
|
447
|
-
var
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
"
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
for (const [k, v] of Object.entries(h)) {
|
|
467
|
-
if (v === null || v === void 0 || v === false) continue;
|
|
468
|
-
out[k.toLowerCase()] = String(v);
|
|
558
|
+
// src/adapters/fetch/uploadStream.ts
|
|
559
|
+
var CHUNK = 64 * 1024;
|
|
560
|
+
var streamSupport;
|
|
561
|
+
function supportsRequestStreams() {
|
|
562
|
+
if (streamSupport !== void 0) return streamSupport;
|
|
563
|
+
if (typeof globalThis.document !== "undefined")
|
|
564
|
+
return streamSupport = false;
|
|
565
|
+
try {
|
|
566
|
+
let duplexAccessed = false;
|
|
567
|
+
const hasContentType = new Request("http://localhost/", {
|
|
568
|
+
body: new ReadableStream(),
|
|
569
|
+
method: "POST",
|
|
570
|
+
get duplex() {
|
|
571
|
+
duplexAccessed = true;
|
|
572
|
+
return "half";
|
|
573
|
+
}
|
|
574
|
+
}).headers.has("content-type");
|
|
575
|
+
streamSupport = duplexAccessed && !hasContentType;
|
|
576
|
+
} catch {
|
|
577
|
+
streamSupport = false;
|
|
469
578
|
}
|
|
470
|
-
return
|
|
579
|
+
return streamSupport;
|
|
471
580
|
}
|
|
472
|
-
function
|
|
473
|
-
|
|
474
|
-
if (typeof Headers !== "undefined" && h instanceof Headers)
|
|
475
|
-
return normalizeHeaders(h);
|
|
476
|
-
const src = h;
|
|
477
|
-
const isBucket2 = (k) => HEADER_BUCKETS.includes(k.toLowerCase());
|
|
478
|
-
const bucket = (name) => {
|
|
479
|
-
const key = Object.keys(src).find((k) => k.toLowerCase() === name);
|
|
480
|
-
const v = key === void 0 ? void 0 : src[key];
|
|
481
|
-
return v && typeof v === "object" ? v : void 0;
|
|
482
|
-
};
|
|
483
|
-
const top = {};
|
|
484
|
-
for (const [k, v] of Object.entries(src)) {
|
|
485
|
-
if (isBucket2(k)) continue;
|
|
486
|
-
Object.assign(top, normalizeHeaders({ [k]: v }));
|
|
487
|
-
}
|
|
581
|
+
async function bodyBytes(body) {
|
|
582
|
+
const res = new Response(body);
|
|
488
583
|
return {
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
...top
|
|
584
|
+
bytes: new Uint8Array(await res.arrayBuffer()),
|
|
585
|
+
contentType: res.headers.get("content-type") ?? void 0
|
|
492
586
|
};
|
|
493
587
|
}
|
|
494
|
-
function
|
|
495
|
-
if (
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
for (const [k, v] of Object.entries(src)) {
|
|
508
|
-
if (isBucket2(k)) continue;
|
|
509
|
-
const lk = k.toLowerCase();
|
|
510
|
-
if (v === null || v === void 0 || v === false) delete out[lk];
|
|
511
|
-
else out[lk] = String(v);
|
|
588
|
+
function countingStream(source, progress) {
|
|
589
|
+
if (isWebStream(source)) {
|
|
590
|
+
return source.pipeThrough(
|
|
591
|
+
new TransformStream({
|
|
592
|
+
transform(chunk, controller) {
|
|
593
|
+
controller.enqueue(chunk);
|
|
594
|
+
progress.add(chunk.byteLength);
|
|
595
|
+
},
|
|
596
|
+
flush() {
|
|
597
|
+
progress.done();
|
|
598
|
+
}
|
|
599
|
+
})
|
|
600
|
+
);
|
|
512
601
|
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
602
|
+
let offset = 0;
|
|
603
|
+
return new ReadableStream({
|
|
604
|
+
pull(controller) {
|
|
605
|
+
if (offset >= source.byteLength) {
|
|
606
|
+
progress.done();
|
|
607
|
+
controller.close();
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
const chunk = source.subarray(offset, offset + CHUNK);
|
|
611
|
+
offset += chunk.byteLength;
|
|
612
|
+
controller.enqueue(chunk);
|
|
613
|
+
progress.add(chunk.byteLength);
|
|
614
|
+
}
|
|
519
615
|
});
|
|
520
|
-
if (h.has("set-cookie")) {
|
|
521
|
-
const cookies = h.getSetCookie?.();
|
|
522
|
-
out["set-cookie"] = cookies ?? h.get("set-cookie");
|
|
523
|
-
}
|
|
524
|
-
return out;
|
|
525
616
|
}
|
|
526
617
|
|
|
527
|
-
// src/fetch/linkStream.ts
|
|
528
|
-
|
|
529
|
-
function cancelBody(data) {
|
|
530
|
-
if (isStream(data) && !data.locked) data.cancel().catch(() => {
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
function linkStream(body, signal, abortError) {
|
|
618
|
+
// src/adapters/fetch/linkStream.ts
|
|
619
|
+
function linkStream(body, signal, abortError2) {
|
|
534
620
|
if (!signal) return body;
|
|
535
621
|
const reader = body.getReader();
|
|
536
622
|
let detach = () => {
|
|
@@ -545,7 +631,7 @@ function linkStream(body, signal, abortError) {
|
|
|
545
631
|
start(controller) {
|
|
546
632
|
const onAbort = () => {
|
|
547
633
|
detach();
|
|
548
|
-
safe(() => controller.error(
|
|
634
|
+
safe(() => controller.error(abortError2()));
|
|
549
635
|
reader.cancel(signal.reason).catch(() => {
|
|
550
636
|
});
|
|
551
637
|
};
|
|
@@ -566,7 +652,7 @@ function linkStream(body, signal, abortError) {
|
|
|
566
652
|
} else safe(() => controller.enqueue(value));
|
|
567
653
|
} catch (err) {
|
|
568
654
|
detach();
|
|
569
|
-
safe(() => controller.error(signal.aborted ?
|
|
655
|
+
safe(() => controller.error(signal.aborted ? abortError2() : err));
|
|
570
656
|
}
|
|
571
657
|
},
|
|
572
658
|
cancel(reason) {
|
|
@@ -576,9 +662,9 @@ function linkStream(body, signal, abortError) {
|
|
|
576
662
|
});
|
|
577
663
|
}
|
|
578
664
|
|
|
579
|
-
// src/fetch/readBody.ts
|
|
580
|
-
function convert(res,
|
|
581
|
-
switch (
|
|
665
|
+
// src/adapters/fetch/readBody.ts
|
|
666
|
+
function convert(res, config, bytes) {
|
|
667
|
+
switch (config.responseType) {
|
|
582
668
|
case "blob":
|
|
583
669
|
return new Blob([bytes], { type: res.headers.get("content-type") ?? "" });
|
|
584
670
|
case "arraybuffer":
|
|
@@ -586,7 +672,7 @@ function convert(res, type, bytes) {
|
|
|
586
672
|
case "formData":
|
|
587
673
|
return new Response(bytes, { headers: res.headers }).formData();
|
|
588
674
|
default:
|
|
589
|
-
return
|
|
675
|
+
return decodeText(bytes, config);
|
|
590
676
|
}
|
|
591
677
|
}
|
|
592
678
|
async function readBody(res, config, onProgress = config.onDownloadProgress) {
|
|
@@ -602,7 +688,7 @@ async function readBody(res, config, onProgress = config.onDownloadProgress) {
|
|
|
602
688
|
case "formData":
|
|
603
689
|
return res.formData();
|
|
604
690
|
default:
|
|
605
|
-
return res.
|
|
691
|
+
return res.arrayBuffer().then((b) => decodeText(new Uint8Array(b), config));
|
|
606
692
|
}
|
|
607
693
|
}
|
|
608
694
|
const lengthHeader = Number(res.headers.get("content-length"));
|
|
@@ -620,7 +706,7 @@ async function readBody(res, config, onProgress = config.onDownloadProgress) {
|
|
|
620
706
|
const reader = res.body.getReader();
|
|
621
707
|
const chunks = [];
|
|
622
708
|
let loaded = 0;
|
|
623
|
-
const
|
|
709
|
+
const progress = createProgress(onProgress, "download", total);
|
|
624
710
|
for (; ; ) {
|
|
625
711
|
const { done, value } = await reader.read();
|
|
626
712
|
if (done) break;
|
|
@@ -631,31 +717,19 @@ async function readBody(res, config, onProgress = config.onDownloadProgress) {
|
|
|
631
717
|
throw tooLarge();
|
|
632
718
|
}
|
|
633
719
|
chunks.push(value);
|
|
634
|
-
|
|
635
|
-
const elapsed = (Date.now() - start) / 1e3;
|
|
636
|
-
const rate = elapsed > 0 ? loaded / elapsed : void 0;
|
|
637
|
-
const ev = {
|
|
638
|
-
loaded,
|
|
639
|
-
total,
|
|
640
|
-
progress: total ? loaded / total : void 0,
|
|
641
|
-
bytes: value.byteLength,
|
|
642
|
-
rate,
|
|
643
|
-
estimated: rate && total ? (total - loaded) / rate : void 0,
|
|
644
|
-
download: true
|
|
645
|
-
};
|
|
646
|
-
onProgress(ev);
|
|
647
|
-
}
|
|
720
|
+
progress.add(value.byteLength);
|
|
648
721
|
}
|
|
722
|
+
progress.done();
|
|
649
723
|
const bytes = new Uint8Array(loaded);
|
|
650
724
|
let offset = 0;
|
|
651
725
|
for (const c of chunks) {
|
|
652
726
|
bytes.set(c, offset);
|
|
653
727
|
offset += c.byteLength;
|
|
654
728
|
}
|
|
655
|
-
return convert(res,
|
|
729
|
+
return convert(res, config, bytes);
|
|
656
730
|
}
|
|
657
731
|
|
|
658
|
-
// src/
|
|
732
|
+
// src/adapters/signals.ts
|
|
659
733
|
function combineSignals(...signals) {
|
|
660
734
|
const controller = new AbortController();
|
|
661
735
|
const active = signals.filter((s) => !!s);
|
|
@@ -702,40 +776,38 @@ function attemptSignal(user, timeoutMs) {
|
|
|
702
776
|
};
|
|
703
777
|
}
|
|
704
778
|
|
|
705
|
-
// src/fetch/
|
|
706
|
-
function
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
config
|
|
738
|
-
);
|
|
779
|
+
// src/adapters/fetch/index.ts
|
|
780
|
+
async function fetchAdapter(config) {
|
|
781
|
+
const { url, headers, body, length } = prepareRequest(config);
|
|
782
|
+
let userError;
|
|
783
|
+
const guard = (cb) => cb && ((ev) => {
|
|
784
|
+
try {
|
|
785
|
+
cb(ev);
|
|
786
|
+
} catch (err) {
|
|
787
|
+
userError ??= { err };
|
|
788
|
+
throw err;
|
|
789
|
+
}
|
|
790
|
+
});
|
|
791
|
+
let sendBody = body;
|
|
792
|
+
let uploadDone;
|
|
793
|
+
if (config.onUploadProgress && body !== void 0) {
|
|
794
|
+
const streaming = supportsRequestStreams();
|
|
795
|
+
if (isWebStream(body)) {
|
|
796
|
+
const progress = createProgress(guard(config.onUploadProgress), "upload", length);
|
|
797
|
+
if (streaming) sendBody = countingStream(body, progress);
|
|
798
|
+
else uploadDone = () => progress.done();
|
|
799
|
+
} else {
|
|
800
|
+
const { bytes, contentType } = await bodyBytes(body);
|
|
801
|
+
if (contentType && !headers["content-type"]) headers["content-type"] = contentType;
|
|
802
|
+
const progress = createProgress(guard(config.onUploadProgress), "upload", bytes.byteLength);
|
|
803
|
+
if (streaming) sendBody = countingStream(bytes, progress);
|
|
804
|
+
else {
|
|
805
|
+
sendBody = bytes;
|
|
806
|
+
uploadDone = () => {
|
|
807
|
+
progress.add(bytes.byteLength);
|
|
808
|
+
progress.done();
|
|
809
|
+
};
|
|
810
|
+
}
|
|
739
811
|
}
|
|
740
812
|
}
|
|
741
813
|
const sig = attemptSignal(config.signal, config.timeout ?? 0);
|
|
@@ -744,13 +816,12 @@ async function performAttempt(config) {
|
|
|
744
816
|
...config.fetchOptions,
|
|
745
817
|
method: config.method.toUpperCase(),
|
|
746
818
|
headers,
|
|
747
|
-
body,
|
|
819
|
+
body: sendBody,
|
|
748
820
|
credentials: config.withCredentials ? "include" : config.fetchOptions?.credentials ?? "same-origin",
|
|
749
821
|
signal: sig.signal
|
|
750
822
|
};
|
|
751
823
|
if (config.redirect) init.redirect = config.redirect;
|
|
752
|
-
if (
|
|
753
|
-
init.duplex = "half";
|
|
824
|
+
if (isWebStream(sendBody)) init.duplex = "half";
|
|
754
825
|
let request;
|
|
755
826
|
try {
|
|
756
827
|
request = new Request(url, init);
|
|
@@ -764,29 +835,22 @@ async function performAttempt(config) {
|
|
|
764
835
|
throw e;
|
|
765
836
|
}
|
|
766
837
|
if (config.signal?.aborted)
|
|
767
|
-
throw
|
|
768
|
-
|
|
769
|
-
const progress = config.onDownloadProgress ? (ev) => {
|
|
770
|
-
try {
|
|
771
|
-
config.onDownloadProgress(ev);
|
|
772
|
-
} catch (err) {
|
|
773
|
-
userError = { err };
|
|
774
|
-
throw err;
|
|
775
|
-
}
|
|
776
|
-
} : void 0;
|
|
838
|
+
throw abortError(config.signal, config, request);
|
|
839
|
+
const progress = guard(config.onDownloadProgress);
|
|
777
840
|
let res;
|
|
778
841
|
let data;
|
|
779
842
|
try {
|
|
780
843
|
res = await (config.fetch ?? globalThis.fetch)(request);
|
|
844
|
+
uploadDone?.();
|
|
781
845
|
data = await readBody(res, config, progress);
|
|
782
846
|
} catch (err) {
|
|
783
|
-
if (userError
|
|
847
|
+
if (userError) throw userError.err;
|
|
784
848
|
if (isExios(err)) {
|
|
785
849
|
err.request ??= request;
|
|
786
850
|
throw err;
|
|
787
851
|
}
|
|
788
852
|
if (config.signal?.aborted)
|
|
789
|
-
throw
|
|
853
|
+
throw abortError(config.signal, config, request);
|
|
790
854
|
if (sig.timedOut()) {
|
|
791
855
|
const code = config.transitional?.clarifyTimeoutError ? ExiosError.ETIMEDOUT : ExiosError.ECONNABORTED;
|
|
792
856
|
throw new ExiosError(
|
|
@@ -809,37 +873,16 @@ async function performAttempt(config) {
|
|
|
809
873
|
data = linkStream(
|
|
810
874
|
data,
|
|
811
875
|
config.signal,
|
|
812
|
-
() =>
|
|
876
|
+
() => abortError(config.signal, config, request)
|
|
813
877
|
);
|
|
814
878
|
}
|
|
815
|
-
|
|
816
|
-
const response = {
|
|
817
|
-
data,
|
|
879
|
+
return finishResponse(config, {
|
|
818
880
|
status: res.status,
|
|
819
881
|
statusText: res.statusText,
|
|
820
|
-
headers:
|
|
821
|
-
|
|
822
|
-
request
|
|
823
|
-
|
|
824
|
-
};
|
|
825
|
-
try {
|
|
826
|
-
response.data = runTransforms(
|
|
827
|
-
config.transformResponse,
|
|
828
|
-
config,
|
|
829
|
-
data,
|
|
830
|
-
resHeaders,
|
|
831
|
-
res.status
|
|
832
|
-
);
|
|
833
|
-
} catch (err) {
|
|
834
|
-
cancelBody(data);
|
|
835
|
-
if (isExios(err)) {
|
|
836
|
-
err.request ??= request;
|
|
837
|
-
err.response ??= response;
|
|
838
|
-
err.status ??= response.status;
|
|
839
|
-
}
|
|
840
|
-
throw err;
|
|
841
|
-
}
|
|
842
|
-
return response;
|
|
882
|
+
headers: headersToObject(res.headers),
|
|
883
|
+
data,
|
|
884
|
+
request
|
|
885
|
+
});
|
|
843
886
|
} finally {
|
|
844
887
|
sig.cleanup();
|
|
845
888
|
}
|
|
@@ -848,9 +891,467 @@ function isExios(e) {
|
|
|
848
891
|
return e instanceof ExiosError;
|
|
849
892
|
}
|
|
850
893
|
|
|
851
|
-
// src/
|
|
852
|
-
function
|
|
853
|
-
|
|
894
|
+
// src/adapters/fetch/register.ts
|
|
895
|
+
function registerFetchAdapter() {
|
|
896
|
+
registerAdapter("fetch", {
|
|
897
|
+
adapter: fetchAdapter,
|
|
898
|
+
isAvailable: (config) => typeof (config.fetch ?? globalThis.fetch) === "function"
|
|
899
|
+
});
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// src/adapters/xhr/index.ts
|
|
903
|
+
function parseHeaders(raw) {
|
|
904
|
+
const out = {};
|
|
905
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
906
|
+
const i = line.indexOf(":");
|
|
907
|
+
if (i <= 0) continue;
|
|
908
|
+
const key = line.slice(0, i).trim().toLowerCase();
|
|
909
|
+
const value = line.slice(i + 1).trim();
|
|
910
|
+
out[key] = key in out ? `${out[key]}, ${value}` : value;
|
|
911
|
+
}
|
|
912
|
+
return out;
|
|
913
|
+
}
|
|
914
|
+
function xhrAdapter(config) {
|
|
915
|
+
if (isWebStream(config.data)) {
|
|
916
|
+
return new Response(config.data).blob().then((blob) => xhrAdapter({ ...config, data: blob }));
|
|
917
|
+
}
|
|
918
|
+
if (config.responseType === "stream") {
|
|
919
|
+
if (isAdapterAvailable("fetch", config)) return fetchAdapter(config);
|
|
920
|
+
return Promise.reject(
|
|
921
|
+
new ExiosError(
|
|
922
|
+
"responseType 'stream' needs fetch, which is not available",
|
|
923
|
+
ExiosError.ERR_NOT_SUPPORT,
|
|
924
|
+
config
|
|
925
|
+
)
|
|
926
|
+
);
|
|
927
|
+
}
|
|
928
|
+
return new Promise((resolve, reject) => {
|
|
929
|
+
let prepared;
|
|
930
|
+
try {
|
|
931
|
+
prepared = prepareRequest(config);
|
|
932
|
+
} catch (e) {
|
|
933
|
+
return reject(e);
|
|
934
|
+
}
|
|
935
|
+
const { url, method, headers, body } = prepared;
|
|
936
|
+
const xhr = new XMLHttpRequest();
|
|
937
|
+
const signal = config.signal;
|
|
938
|
+
let settled = false;
|
|
939
|
+
let userError;
|
|
940
|
+
const cleanup = () => signal?.removeEventListener("abort", onAbort);
|
|
941
|
+
const fail = (err) => {
|
|
942
|
+
if (settled) return;
|
|
943
|
+
settled = true;
|
|
944
|
+
cleanup();
|
|
945
|
+
reject(err);
|
|
946
|
+
};
|
|
947
|
+
const onAbort = () => {
|
|
948
|
+
xhr.abort();
|
|
949
|
+
fail(abortError(signal, config, xhr));
|
|
950
|
+
};
|
|
951
|
+
const guard = (cb) => cb && ((e) => {
|
|
952
|
+
try {
|
|
953
|
+
cb(e);
|
|
954
|
+
} catch (err) {
|
|
955
|
+
userError ??= { err };
|
|
956
|
+
xhr.abort();
|
|
957
|
+
fail(err);
|
|
958
|
+
}
|
|
959
|
+
});
|
|
960
|
+
try {
|
|
961
|
+
xhr.open(method, url, true);
|
|
962
|
+
} catch (err) {
|
|
963
|
+
const e = new ExiosError(
|
|
964
|
+
err instanceof Error ? err.message : String(err),
|
|
965
|
+
ExiosError.ERR_BAD_REQUEST,
|
|
966
|
+
config,
|
|
967
|
+
xhr
|
|
968
|
+
);
|
|
969
|
+
e.cause = err;
|
|
970
|
+
return reject(e);
|
|
971
|
+
}
|
|
972
|
+
xhr.timeout = config.timeout ?? 0;
|
|
973
|
+
xhr.withCredentials = !!config.withCredentials;
|
|
974
|
+
const type = config.responseType;
|
|
975
|
+
xhr.responseType = type === "blob" || type === "formData" ? "blob" : "arraybuffer";
|
|
976
|
+
try {
|
|
977
|
+
for (const [k, v] of Object.entries(headers)) {
|
|
978
|
+
if (k === "content-type" && body instanceof FormData) continue;
|
|
979
|
+
xhr.setRequestHeader(k, v);
|
|
980
|
+
}
|
|
981
|
+
} catch (err) {
|
|
982
|
+
const e = new ExiosError(
|
|
983
|
+
err instanceof Error ? err.message : String(err),
|
|
984
|
+
ExiosError.ERR_BAD_REQUEST,
|
|
985
|
+
config,
|
|
986
|
+
xhr
|
|
987
|
+
);
|
|
988
|
+
e.cause = err;
|
|
989
|
+
return reject(e);
|
|
990
|
+
}
|
|
991
|
+
const max = config.maxContentLength ?? -1;
|
|
992
|
+
const onDownload = guard(config.onDownloadProgress);
|
|
993
|
+
if (onDownload || max > -1) {
|
|
994
|
+
let progress;
|
|
995
|
+
let seen = 0;
|
|
996
|
+
xhr.onprogress = (ev) => {
|
|
997
|
+
if (max > -1 && ev.loaded > max) {
|
|
998
|
+
xhr.abort();
|
|
999
|
+
return fail(
|
|
1000
|
+
new ExiosError(
|
|
1001
|
+
`maxContentLength size of ${max} exceeded`,
|
|
1002
|
+
ExiosError.ERR_BAD_RESPONSE,
|
|
1003
|
+
config,
|
|
1004
|
+
xhr
|
|
1005
|
+
)
|
|
1006
|
+
);
|
|
1007
|
+
}
|
|
1008
|
+
progress ??= createProgress(
|
|
1009
|
+
onDownload,
|
|
1010
|
+
"download",
|
|
1011
|
+
ev.lengthComputable ? ev.total : void 0
|
|
1012
|
+
);
|
|
1013
|
+
progress.add(ev.loaded - seen, ev);
|
|
1014
|
+
seen = ev.loaded;
|
|
1015
|
+
};
|
|
1016
|
+
xhr.onload = () => progress?.done();
|
|
1017
|
+
}
|
|
1018
|
+
const onUpload = guard(config.onUploadProgress);
|
|
1019
|
+
if (onUpload && body !== void 0) {
|
|
1020
|
+
let progress;
|
|
1021
|
+
let seen = 0;
|
|
1022
|
+
xhr.upload.onprogress = (ev) => {
|
|
1023
|
+
progress ??= createProgress(
|
|
1024
|
+
onUpload,
|
|
1025
|
+
"upload",
|
|
1026
|
+
ev.lengthComputable ? ev.total : void 0
|
|
1027
|
+
);
|
|
1028
|
+
progress.add(ev.loaded - seen, ev);
|
|
1029
|
+
seen = ev.loaded;
|
|
1030
|
+
};
|
|
1031
|
+
xhr.upload.onload = () => progress?.done();
|
|
1032
|
+
}
|
|
1033
|
+
xhr.ontimeout = () => fail(
|
|
1034
|
+
new ExiosError(
|
|
1035
|
+
config.timeoutErrorMessage ?? `timeout of ${config.timeout}ms exceeded`,
|
|
1036
|
+
config.transitional?.clarifyTimeoutError ? ExiosError.ETIMEDOUT : ExiosError.ECONNABORTED,
|
|
1037
|
+
config,
|
|
1038
|
+
xhr
|
|
1039
|
+
)
|
|
1040
|
+
);
|
|
1041
|
+
xhr.onerror = () => {
|
|
1042
|
+
const e = new ExiosError("Network Error", ExiosError.ERR_NETWORK, config, xhr);
|
|
1043
|
+
fail(e);
|
|
1044
|
+
};
|
|
1045
|
+
xhr.onloadend = () => {
|
|
1046
|
+
if (settled || userError) return;
|
|
1047
|
+
if (xhr.readyState !== 4 || xhr.status === 0) return;
|
|
1048
|
+
void (async () => {
|
|
1049
|
+
try {
|
|
1050
|
+
const resHeaders = parseHeaders(xhr.getAllResponseHeaders());
|
|
1051
|
+
let data = xhr.response;
|
|
1052
|
+
if (type === "formData") {
|
|
1053
|
+
data = await new Response(data, {
|
|
1054
|
+
headers: { "content-type": resHeaders["content-type"] ?? "" }
|
|
1055
|
+
}).formData();
|
|
1056
|
+
} else if (type !== "blob" && type !== "arraybuffer") {
|
|
1057
|
+
data = decodeText(new Uint8Array(data), config);
|
|
1058
|
+
}
|
|
1059
|
+
const response = finishResponse(config, {
|
|
1060
|
+
status: xhr.status,
|
|
1061
|
+
statusText: xhr.statusText,
|
|
1062
|
+
headers: resHeaders,
|
|
1063
|
+
data,
|
|
1064
|
+
request: xhr
|
|
1065
|
+
});
|
|
1066
|
+
if (settled) return;
|
|
1067
|
+
settled = true;
|
|
1068
|
+
cleanup();
|
|
1069
|
+
resolve(response);
|
|
1070
|
+
} catch (err) {
|
|
1071
|
+
fail(err);
|
|
1072
|
+
}
|
|
1073
|
+
})();
|
|
1074
|
+
};
|
|
1075
|
+
if (signal?.aborted) return onAbort();
|
|
1076
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1077
|
+
xhr.send(body ?? null);
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
// src/adapters/xhr/register.ts
|
|
1082
|
+
function registerXhrAdapter() {
|
|
1083
|
+
registerAdapter("xhr", {
|
|
1084
|
+
adapter: xhrAdapter,
|
|
1085
|
+
isAvailable: () => typeof XMLHttpRequest !== "undefined"
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
// src/cancel/CancelToken.ts
|
|
1090
|
+
var CancelToken = class _CancelToken {
|
|
1091
|
+
reason;
|
|
1092
|
+
promise;
|
|
1093
|
+
listeners = [];
|
|
1094
|
+
constructor(executor) {
|
|
1095
|
+
if (typeof executor !== "function")
|
|
1096
|
+
throw new TypeError("executor must be a function.");
|
|
1097
|
+
let resolve;
|
|
1098
|
+
this.promise = new Promise((r) => resolve = r);
|
|
1099
|
+
executor((message, config, request) => {
|
|
1100
|
+
if (this.reason) return;
|
|
1101
|
+
this.reason = new CanceledError(message ?? "canceled", config, request);
|
|
1102
|
+
resolve(this.reason);
|
|
1103
|
+
const ls = this.listeners;
|
|
1104
|
+
this.listeners = [];
|
|
1105
|
+
for (const l of ls) l(this.reason);
|
|
1106
|
+
});
|
|
1107
|
+
}
|
|
1108
|
+
throwIfRequested() {
|
|
1109
|
+
if (this.reason) throw this.reason;
|
|
1110
|
+
}
|
|
1111
|
+
subscribe(listener) {
|
|
1112
|
+
if (this.reason) listener(this.reason);
|
|
1113
|
+
else this.listeners.push(listener);
|
|
1114
|
+
}
|
|
1115
|
+
unsubscribe(listener) {
|
|
1116
|
+
this.listeners = this.listeners.filter((l) => l !== listener);
|
|
1117
|
+
}
|
|
1118
|
+
/** An AbortSignal that aborts with the reason; call `signal.unsubscribe()` when done with it. */
|
|
1119
|
+
toAbortSignal() {
|
|
1120
|
+
const ac = new AbortController();
|
|
1121
|
+
const abort = (reason) => ac.abort(reason);
|
|
1122
|
+
this.subscribe(abort);
|
|
1123
|
+
return Object.assign(ac.signal, { unsubscribe: () => this.unsubscribe(abort) });
|
|
1124
|
+
}
|
|
1125
|
+
static source() {
|
|
1126
|
+
let cancel;
|
|
1127
|
+
const token = new _CancelToken((c) => cancel = c);
|
|
1128
|
+
return { token, cancel };
|
|
1129
|
+
}
|
|
1130
|
+
};
|
|
1131
|
+
|
|
1132
|
+
// src/core/defaults.ts
|
|
1133
|
+
var DEFAULT_RETRY = Object.freeze({
|
|
1134
|
+
retries: 3,
|
|
1135
|
+
methods: Object.freeze([
|
|
1136
|
+
"get",
|
|
1137
|
+
"head",
|
|
1138
|
+
"options",
|
|
1139
|
+
"put",
|
|
1140
|
+
"delete"
|
|
1141
|
+
]),
|
|
1142
|
+
statusCodes: Object.freeze([408, 429, 500, 502, 503, 504]),
|
|
1143
|
+
retryOnNetworkError: true,
|
|
1144
|
+
baseDelayMs: 300,
|
|
1145
|
+
maxDelayMs: 3e4,
|
|
1146
|
+
factor: 2,
|
|
1147
|
+
jitter: "full",
|
|
1148
|
+
respectRetryAfter: true
|
|
1149
|
+
});
|
|
1150
|
+
var DEFAULT_BREAKER = Object.freeze({
|
|
1151
|
+
failureThreshold: 5,
|
|
1152
|
+
minRequests: 10,
|
|
1153
|
+
failureRateThreshold: 0.5,
|
|
1154
|
+
resetTimeoutMs: 3e4,
|
|
1155
|
+
halfOpenMaxRequests: 1,
|
|
1156
|
+
successThreshold: 1
|
|
1157
|
+
});
|
|
1158
|
+
function createHeaderBuckets(common = {}) {
|
|
1159
|
+
return {
|
|
1160
|
+
common,
|
|
1161
|
+
get: {},
|
|
1162
|
+
post: {},
|
|
1163
|
+
put: {},
|
|
1164
|
+
patch: {},
|
|
1165
|
+
delete: {},
|
|
1166
|
+
head: {},
|
|
1167
|
+
options: {}
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
function createDefaults() {
|
|
1171
|
+
return {
|
|
1172
|
+
adapter: ["xhr", "http", "fetch"],
|
|
1173
|
+
xsrfCookieName: "XSRF-TOKEN",
|
|
1174
|
+
xsrfHeaderName: "X-XSRF-TOKEN",
|
|
1175
|
+
maxRedirects: 21,
|
|
1176
|
+
decompress: true,
|
|
1177
|
+
timeout: 0,
|
|
1178
|
+
responseType: "json",
|
|
1179
|
+
headers: createHeaderBuckets({
|
|
1180
|
+
Accept: "application/json, text/plain, */*"
|
|
1181
|
+
}),
|
|
1182
|
+
transformRequest: [defaultTransformRequest],
|
|
1183
|
+
transformResponse: [defaultTransformResponse],
|
|
1184
|
+
validateStatus: (s) => s >= 200 && s < 300,
|
|
1185
|
+
maxContentLength: -1,
|
|
1186
|
+
maxBodyLength: -1,
|
|
1187
|
+
transitional: { silentJSONParsing: true, clarifyTimeoutError: false },
|
|
1188
|
+
retry: {
|
|
1189
|
+
...DEFAULT_RETRY,
|
|
1190
|
+
methods: [...DEFAULT_RETRY.methods],
|
|
1191
|
+
statusCodes: [...DEFAULT_RETRY.statusCodes]
|
|
1192
|
+
},
|
|
1193
|
+
breaker: { ...DEFAULT_BREAKER }
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
// src/resilience/breaker.ts
|
|
1198
|
+
var FAILURE_CODES = /* @__PURE__ */ new Set([
|
|
1199
|
+
ExiosError.ERR_NETWORK,
|
|
1200
|
+
ExiosError.ECONNABORTED,
|
|
1201
|
+
ExiosError.ETIMEDOUT
|
|
1202
|
+
]);
|
|
1203
|
+
function defaultIsFailure(o) {
|
|
1204
|
+
const code = o.error?.code;
|
|
1205
|
+
if (code === ExiosError.ERR_CANCELED) return false;
|
|
1206
|
+
if (code !== void 0 && FAILURE_CODES.has(code)) return true;
|
|
1207
|
+
const status = o.response?.status ?? o.error?.response?.status;
|
|
1208
|
+
if (status === void 0) return false;
|
|
1209
|
+
return status >= 500 || status === 408 || status === 429;
|
|
1210
|
+
}
|
|
1211
|
+
var CircuitBreaker = class {
|
|
1212
|
+
constructor(key, cfg, now = Date.now) {
|
|
1213
|
+
this.key = key;
|
|
1214
|
+
this.cfg = cfg;
|
|
1215
|
+
this.now = now;
|
|
1216
|
+
}
|
|
1217
|
+
key;
|
|
1218
|
+
cfg;
|
|
1219
|
+
now;
|
|
1220
|
+
_state = "closed";
|
|
1221
|
+
epoch = 0;
|
|
1222
|
+
openedAt = 0;
|
|
1223
|
+
consecutive = 0;
|
|
1224
|
+
samples = [];
|
|
1225
|
+
probes = /* @__PURE__ */ new Set();
|
|
1226
|
+
windowFailures = 0;
|
|
1227
|
+
probeSuccesses = 0;
|
|
1228
|
+
/** Stored state; open -> half-open happens lazily on `acquire` (no timers). */
|
|
1229
|
+
get state() {
|
|
1230
|
+
return this._state;
|
|
1231
|
+
}
|
|
1232
|
+
retryAt() {
|
|
1233
|
+
return this.openedAt + this.cfg.resetTimeoutMs;
|
|
1234
|
+
}
|
|
1235
|
+
acquire(config) {
|
|
1236
|
+
if (this._state === "open") {
|
|
1237
|
+
if (this.now() < this.retryAt())
|
|
1238
|
+
throw new CircuitOpenError(this.key, this.retryAt(), config);
|
|
1239
|
+
this.transition("half-open");
|
|
1240
|
+
}
|
|
1241
|
+
if (this._state === "half-open") {
|
|
1242
|
+
const cutoff = this.now() - this.cfg.resetTimeoutMs;
|
|
1243
|
+
for (const p of this.probes) {
|
|
1244
|
+
if (p.at <= cutoff) {
|
|
1245
|
+
this.transition("open");
|
|
1246
|
+
throw new CircuitOpenError(this.key, this.retryAt(), config);
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
if (this.probes.size >= this.cfg.halfOpenMaxRequests) {
|
|
1250
|
+
throw new CircuitOpenError(this.key, this.now(), config);
|
|
1251
|
+
}
|
|
1252
|
+
const probe = {
|
|
1253
|
+
probe: true,
|
|
1254
|
+
done: false,
|
|
1255
|
+
epoch: this.epoch,
|
|
1256
|
+
at: this.now()
|
|
1257
|
+
};
|
|
1258
|
+
this.probes.add(probe);
|
|
1259
|
+
return probe;
|
|
1260
|
+
}
|
|
1261
|
+
return { probe: false, done: false, epoch: this.epoch, at: this.now() };
|
|
1262
|
+
}
|
|
1263
|
+
/** Called before each retry: a non-probe ticket may not continue unless closed. */
|
|
1264
|
+
check(ticket) {
|
|
1265
|
+
if (this._state === "closed" || ticket.probe && ticket.epoch === this.epoch)
|
|
1266
|
+
return;
|
|
1267
|
+
throw new CircuitOpenError(
|
|
1268
|
+
this.key,
|
|
1269
|
+
this._state === "open" ? this.retryAt() : this.now()
|
|
1270
|
+
);
|
|
1271
|
+
}
|
|
1272
|
+
record(ticket, failed) {
|
|
1273
|
+
if (ticket.done) return;
|
|
1274
|
+
ticket.done = true;
|
|
1275
|
+
if (ticket.epoch !== this.epoch) return;
|
|
1276
|
+
if (ticket.probe) {
|
|
1277
|
+
this.probes.delete(ticket);
|
|
1278
|
+
if (this._state !== "half-open") return;
|
|
1279
|
+
if (failed) return this.transition("open");
|
|
1280
|
+
if (++this.probeSuccesses >= this.cfg.successThreshold)
|
|
1281
|
+
this.transition("closed");
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
if (this._state !== "closed") return;
|
|
1285
|
+
if (this.cfg.windowMs !== void 0) {
|
|
1286
|
+
const t = this.now();
|
|
1287
|
+
this.samples.push({ t, failed });
|
|
1288
|
+
if (failed) this.windowFailures++;
|
|
1289
|
+
let i = 0;
|
|
1290
|
+
while (i < this.samples.length && t - this.samples[i].t > this.cfg.windowMs) {
|
|
1291
|
+
if (this.samples[i].failed) this.windowFailures--;
|
|
1292
|
+
i++;
|
|
1293
|
+
}
|
|
1294
|
+
if (i > 0) this.samples.splice(0, i);
|
|
1295
|
+
if (this.samples.length >= this.cfg.minRequests && this.windowFailures / this.samples.length >= this.cfg.failureRateThreshold) {
|
|
1296
|
+
this.transition("open");
|
|
1297
|
+
}
|
|
1298
|
+
return;
|
|
1299
|
+
}
|
|
1300
|
+
this.consecutive = failed ? this.consecutive + 1 : 0;
|
|
1301
|
+
if (this.consecutive >= this.cfg.failureThreshold) this.transition("open");
|
|
1302
|
+
}
|
|
1303
|
+
/** No outcome (cancel): frees a probe slot, never changes state. */
|
|
1304
|
+
release(ticket) {
|
|
1305
|
+
if (ticket.done) return;
|
|
1306
|
+
ticket.done = true;
|
|
1307
|
+
if (ticket.probe && ticket.epoch === this.epoch) this.probes.delete(ticket);
|
|
1308
|
+
}
|
|
1309
|
+
transition(to) {
|
|
1310
|
+
const from = this._state;
|
|
1311
|
+
this._state = to;
|
|
1312
|
+
this.epoch++;
|
|
1313
|
+
this.probes = /* @__PURE__ */ new Set();
|
|
1314
|
+
this.windowFailures = 0;
|
|
1315
|
+
this.probeSuccesses = 0;
|
|
1316
|
+
this.consecutive = 0;
|
|
1317
|
+
this.samples = [];
|
|
1318
|
+
if (to === "open") this.openedAt = this.now();
|
|
1319
|
+
try {
|
|
1320
|
+
this.cfg.onStateChange?.(this.key, from, to);
|
|
1321
|
+
} catch {
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
};
|
|
1325
|
+
var BreakerRegistry = class {
|
|
1326
|
+
constructor(getConfig, now = Date.now) {
|
|
1327
|
+
this.getConfig = getConfig;
|
|
1328
|
+
this.now = now;
|
|
1329
|
+
}
|
|
1330
|
+
getConfig;
|
|
1331
|
+
now;
|
|
1332
|
+
breakers = /* @__PURE__ */ new Map();
|
|
1333
|
+
get(key) {
|
|
1334
|
+
const cfg = this.getConfig();
|
|
1335
|
+
if (cfg === false) return null;
|
|
1336
|
+
let b = this.breakers.get(key);
|
|
1337
|
+
if (!b) {
|
|
1338
|
+
b = new CircuitBreaker(key, cfg, this.now);
|
|
1339
|
+
this.breakers.set(key, b);
|
|
1340
|
+
}
|
|
1341
|
+
return b;
|
|
1342
|
+
}
|
|
1343
|
+
state(key) {
|
|
1344
|
+
return this.breakers.get(key)?.state ?? "closed";
|
|
1345
|
+
}
|
|
1346
|
+
reset(key) {
|
|
1347
|
+
if (key === void 0) this.breakers.clear();
|
|
1348
|
+
else this.breakers.delete(key);
|
|
1349
|
+
}
|
|
1350
|
+
};
|
|
1351
|
+
|
|
1352
|
+
// src/resilience/backoff.ts
|
|
1353
|
+
function computeDelay(attempt, cfg, random = Math.random) {
|
|
1354
|
+
const capped = Math.min(
|
|
854
1355
|
cfg.maxDelayMs,
|
|
855
1356
|
cfg.baseDelayMs * cfg.factor ** (attempt - 1)
|
|
856
1357
|
);
|
|
@@ -870,7 +1371,9 @@ function parseRetryAfter(value, now = Date.now()) {
|
|
|
870
1371
|
var NEVER_RETRY = /* @__PURE__ */ new Set([
|
|
871
1372
|
"ERR_CANCELED",
|
|
872
1373
|
"ERR_CIRCUIT_OPEN",
|
|
873
|
-
"ERR_BAD_OPTION_VALUE"
|
|
1374
|
+
"ERR_BAD_OPTION_VALUE",
|
|
1375
|
+
"ERR_NOT_SUPPORT",
|
|
1376
|
+
"ERR_FR_TOO_MANY_REDIRECTS"
|
|
874
1377
|
]);
|
|
875
1378
|
var RETRYABLE_ERRORS = /* @__PURE__ */ new Set(["ERR_NETWORK", "ECONNABORTED", "ETIMEDOUT"]);
|
|
876
1379
|
function defaultShouldRetry(o, cfg, config) {
|
|
@@ -882,10 +1385,10 @@ function defaultShouldRetry(o, cfg, config) {
|
|
|
882
1385
|
}
|
|
883
1386
|
function defaultSleep(ms, signal) {
|
|
884
1387
|
return new Promise((resolve, reject) => {
|
|
885
|
-
if (signal?.aborted) return reject(
|
|
1388
|
+
if (signal?.aborted) return reject(abortError(signal));
|
|
886
1389
|
const onAbort = () => {
|
|
887
1390
|
clearTimeout(timer);
|
|
888
|
-
reject(
|
|
1391
|
+
reject(abortError(signal));
|
|
889
1392
|
};
|
|
890
1393
|
const timer = setTimeout(() => {
|
|
891
1394
|
signal?.removeEventListener("abort", onAbort);
|
|
@@ -898,7 +1401,7 @@ async function runWithRetry(attempt, cfg, config, hooks) {
|
|
|
898
1401
|
const sleep = hooks.sleep ?? defaultSleep;
|
|
899
1402
|
const now = hooks.now ?? Date.now;
|
|
900
1403
|
const start = now();
|
|
901
|
-
const
|
|
1404
|
+
const isStream = isOneShotBody(config.data);
|
|
902
1405
|
let attempts = 0;
|
|
903
1406
|
for (; ; ) {
|
|
904
1407
|
attempts++;
|
|
@@ -909,7 +1412,7 @@ async function runWithRetry(attempt, cfg, config, hooks) {
|
|
|
909
1412
|
if (!isExiosError(e)) throw e;
|
|
910
1413
|
outcome = { error: e };
|
|
911
1414
|
}
|
|
912
|
-
if (cfg === false || attempts > cfg.retries ||
|
|
1415
|
+
if (cfg === false || attempts > cfg.retries || isStream)
|
|
913
1416
|
return { outcome, attempts };
|
|
914
1417
|
if (outcome.response && outcome.response.status >= 200 && outcome.response.status < 300)
|
|
915
1418
|
return { outcome, attempts };
|
|
@@ -935,7 +1438,7 @@ async function runWithRetry(attempt, cfg, config, hooks) {
|
|
|
935
1438
|
return { outcome, attempts };
|
|
936
1439
|
}
|
|
937
1440
|
cancelBody(outcome.response?.data);
|
|
938
|
-
if (hooks.signal?.aborted) throw
|
|
1441
|
+
if (hooks.signal?.aborted) throw abortError(hooks.signal);
|
|
939
1442
|
cfg.onRetry?.({ ...outcome, attempt: attempts, delayMs: delay });
|
|
940
1443
|
await sleep(delay, hooks.signal);
|
|
941
1444
|
hooks.beforeRetry?.();
|
|
@@ -958,19 +1461,56 @@ function defaultBreakerKey(config) {
|
|
|
958
1461
|
return full;
|
|
959
1462
|
}
|
|
960
1463
|
}
|
|
961
|
-
async function dispatchRequest(config,
|
|
962
|
-
|
|
1464
|
+
async function dispatchRequest(config, registry2) {
|
|
1465
|
+
const token = config.cancelToken;
|
|
1466
|
+
if (token) {
|
|
1467
|
+
if (token.reason) {
|
|
1468
|
+
token.reason.config ??= config;
|
|
1469
|
+
throw token.reason;
|
|
1470
|
+
}
|
|
1471
|
+
const tokenSignal = token.toAbortSignal();
|
|
1472
|
+
const joined = combineSignals(config.signal, tokenSignal);
|
|
1473
|
+
const release = () => {
|
|
1474
|
+
joined.cleanup();
|
|
1475
|
+
tokenSignal.unsubscribe();
|
|
1476
|
+
};
|
|
1477
|
+
let deferred = false;
|
|
1478
|
+
try {
|
|
1479
|
+
const response = await dispatchRequest(
|
|
1480
|
+
{ ...config, signal: joined.signal, cancelToken: void 0 },
|
|
1481
|
+
registry2
|
|
1482
|
+
);
|
|
1483
|
+
if (isOneShotBody(response.data)) {
|
|
1484
|
+
response.data = whenBodyDone(response.data, release);
|
|
1485
|
+
deferred = true;
|
|
1486
|
+
}
|
|
1487
|
+
return response;
|
|
1488
|
+
} catch (e) {
|
|
1489
|
+
if (isExiosError(e)) e.config ??= config;
|
|
1490
|
+
throw e;
|
|
1491
|
+
} finally {
|
|
1492
|
+
if (!deferred) release();
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
if (config.signal?.aborted) throw abortError(config.signal, config);
|
|
963
1496
|
const bcfg = config.breaker;
|
|
964
|
-
const breaker = bcfg === false ? null :
|
|
1497
|
+
const breaker = bcfg === false ? null : registry2.get(bcfg.key ? bcfg.key(config) : defaultBreakerKey(config));
|
|
965
1498
|
const ticket = breaker?.acquire(config);
|
|
966
1499
|
let attempts = 0;
|
|
967
1500
|
try {
|
|
1501
|
+
let adapter;
|
|
1502
|
+
try {
|
|
1503
|
+
adapter = resolveAdapter(config);
|
|
1504
|
+
} catch (e) {
|
|
1505
|
+
if (isExiosError(e)) e.config ??= config;
|
|
1506
|
+
throw e;
|
|
1507
|
+
}
|
|
968
1508
|
let result;
|
|
969
1509
|
try {
|
|
970
1510
|
result = await runWithRetry(
|
|
971
1511
|
() => {
|
|
972
1512
|
attempts++;
|
|
973
|
-
return
|
|
1513
|
+
return adapter(config);
|
|
974
1514
|
},
|
|
975
1515
|
ticket?.probe ? false : config.retry,
|
|
976
1516
|
// half-open probes get exactly one attempt
|
|
@@ -1223,6 +1763,7 @@ function resolveConfig(merged) {
|
|
|
1223
1763
|
};
|
|
1224
1764
|
}
|
|
1225
1765
|
function finalizeConfig(config) {
|
|
1766
|
+
assertEncoding(config);
|
|
1226
1767
|
const method = (config.method ?? "get").toLowerCase();
|
|
1227
1768
|
return {
|
|
1228
1769
|
...config,
|
|
@@ -1440,7 +1981,10 @@ function createInstance(defaults) {
|
|
|
1440
1981
|
}
|
|
1441
1982
|
|
|
1442
1983
|
// src/index.ts
|
|
1984
|
+
registerXhrAdapter();
|
|
1985
|
+
registerFetchAdapter();
|
|
1443
1986
|
var statics = {
|
|
1987
|
+
CancelToken,
|
|
1444
1988
|
ExiosError,
|
|
1445
1989
|
CanceledError,
|
|
1446
1990
|
CircuitOpenError,
|