@indra211/exios 3.0.0 → 3.1.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/README.md +95 -2
- package/dist/index.cjs +409 -11
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +105 -4
- package/dist/index.js +410 -11
- package/dist/index.node.cjs +409 -11
- package/dist/index.node.js +410 -11
- package/package.json +1 -1
package/dist/index.node.js
CHANGED
|
@@ -164,8 +164,8 @@ function serializeParams(params, opts2 = {}) {
|
|
|
164
164
|
return parts.join("&");
|
|
165
165
|
}
|
|
166
166
|
function buildURL(url, params, serializer) {
|
|
167
|
-
const
|
|
168
|
-
if (
|
|
167
|
+
const hash2 = url.indexOf("#");
|
|
168
|
+
if (hash2 !== -1) url = url.slice(0, hash2);
|
|
169
169
|
if (params === void 0 || params === null) return url;
|
|
170
170
|
let serialized;
|
|
171
171
|
if (typeof serializer === "function") serialized = serializer(params);
|
|
@@ -2285,7 +2285,14 @@ var isPlainObject3 = (v) => {
|
|
|
2285
2285
|
return p === Object.prototype || p === null;
|
|
2286
2286
|
};
|
|
2287
2287
|
var isBucket = (k) => HEADER_BUCKETS.includes(k.toLowerCase());
|
|
2288
|
-
var SHALLOW_MERGE = /* @__PURE__ */ new Set([
|
|
2288
|
+
var SHALLOW_MERGE = /* @__PURE__ */ new Set([
|
|
2289
|
+
"auth",
|
|
2290
|
+
"transitional",
|
|
2291
|
+
"fetchOptions",
|
|
2292
|
+
"metadata",
|
|
2293
|
+
"cache",
|
|
2294
|
+
"dedupe"
|
|
2295
|
+
]);
|
|
2289
2296
|
function setCI(target, k, v) {
|
|
2290
2297
|
const lk = k.toLowerCase();
|
|
2291
2298
|
for (const existing of Object.keys(target)) {
|
|
@@ -2391,6 +2398,210 @@ function assertRequestBreaker(b) {
|
|
|
2391
2398
|
}
|
|
2392
2399
|
}
|
|
2393
2400
|
|
|
2401
|
+
// src/core/dedupe.ts
|
|
2402
|
+
var InflightRequests = class {
|
|
2403
|
+
flights = /* @__PURE__ */ new Map();
|
|
2404
|
+
get size() {
|
|
2405
|
+
return this.flights.size;
|
|
2406
|
+
}
|
|
2407
|
+
/**
|
|
2408
|
+
* Join the flight for `key`, starting it with `start(signal)` when there is none. `start` receives a
|
|
2409
|
+
* signal owned by the flight, never a caller's. Resolves with `leader: true` for the caller that started it.
|
|
2410
|
+
*/
|
|
2411
|
+
join(key, config, start) {
|
|
2412
|
+
const token = config.cancelToken;
|
|
2413
|
+
if (token?.reason) {
|
|
2414
|
+
token.reason.config ??= config;
|
|
2415
|
+
return Promise.reject(token.reason);
|
|
2416
|
+
}
|
|
2417
|
+
const tokenSignal = token?.toAbortSignal();
|
|
2418
|
+
const signals = [config.signal, tokenSignal].filter((s) => !!s);
|
|
2419
|
+
const aborted = signals.find((s) => s.aborted);
|
|
2420
|
+
if (aborted) {
|
|
2421
|
+
tokenSignal?.unsubscribe();
|
|
2422
|
+
return Promise.reject(abortError(aborted, config));
|
|
2423
|
+
}
|
|
2424
|
+
let flight = this.flights.get(key);
|
|
2425
|
+
const leader = !flight;
|
|
2426
|
+
if (!flight) {
|
|
2427
|
+
const controller = new AbortController();
|
|
2428
|
+
const f2 = { controller, subscribers: 0, promise: void 0 };
|
|
2429
|
+
f2.promise = start(controller.signal).finally(() => {
|
|
2430
|
+
if (this.flights.get(key) === f2) this.flights.delete(key);
|
|
2431
|
+
});
|
|
2432
|
+
this.flights.set(key, f2);
|
|
2433
|
+
flight = f2;
|
|
2434
|
+
}
|
|
2435
|
+
const f = flight;
|
|
2436
|
+
f.subscribers++;
|
|
2437
|
+
return new Promise((resolve, reject) => {
|
|
2438
|
+
let settled = false;
|
|
2439
|
+
const detach = () => {
|
|
2440
|
+
settled = true;
|
|
2441
|
+
for (const s of signals) s.removeEventListener("abort", onAbort);
|
|
2442
|
+
tokenSignal?.unsubscribe();
|
|
2443
|
+
};
|
|
2444
|
+
const onAbort = (ev) => {
|
|
2445
|
+
if (settled) return;
|
|
2446
|
+
detach();
|
|
2447
|
+
const reason = abortError(ev.target, config);
|
|
2448
|
+
reject(reason);
|
|
2449
|
+
if (--f.subscribers === 0) {
|
|
2450
|
+
if (this.flights.get(key) === f) this.flights.delete(key);
|
|
2451
|
+
f.controller.abort(reason);
|
|
2452
|
+
}
|
|
2453
|
+
};
|
|
2454
|
+
for (const s of signals) s.addEventListener("abort", onAbort, { once: true });
|
|
2455
|
+
f.promise.then(
|
|
2456
|
+
(response) => {
|
|
2457
|
+
if (settled) return;
|
|
2458
|
+
detach();
|
|
2459
|
+
f.subscribers--;
|
|
2460
|
+
resolve({ response, leader });
|
|
2461
|
+
},
|
|
2462
|
+
(err) => {
|
|
2463
|
+
if (settled) return;
|
|
2464
|
+
detach();
|
|
2465
|
+
f.subscribers--;
|
|
2466
|
+
reject(err);
|
|
2467
|
+
}
|
|
2468
|
+
);
|
|
2469
|
+
});
|
|
2470
|
+
}
|
|
2471
|
+
};
|
|
2472
|
+
|
|
2473
|
+
// src/core/requestCache.ts
|
|
2474
|
+
var SAFE_METHODS = Object.freeze(["get", "head"]);
|
|
2475
|
+
var MemoryCache = class {
|
|
2476
|
+
map = /* @__PURE__ */ new Map();
|
|
2477
|
+
maxEntries;
|
|
2478
|
+
constructor(options = {}) {
|
|
2479
|
+
const max = options.maxEntries ?? 100;
|
|
2480
|
+
if (!Number.isInteger(max) || max < 1) bad("maxEntries", max, "an integer >= 1");
|
|
2481
|
+
this.maxEntries = max;
|
|
2482
|
+
}
|
|
2483
|
+
get size() {
|
|
2484
|
+
return this.map.size;
|
|
2485
|
+
}
|
|
2486
|
+
get(key) {
|
|
2487
|
+
const entry = this.map.get(key);
|
|
2488
|
+
if (!entry) return void 0;
|
|
2489
|
+
this.map.delete(key);
|
|
2490
|
+
if (entry.expiresAt <= Date.now()) return void 0;
|
|
2491
|
+
this.map.set(key, entry);
|
|
2492
|
+
return entry;
|
|
2493
|
+
}
|
|
2494
|
+
set(key, entry) {
|
|
2495
|
+
this.map.delete(key);
|
|
2496
|
+
this.map.set(key, entry);
|
|
2497
|
+
while (this.map.size > this.maxEntries)
|
|
2498
|
+
this.map.delete(this.map.keys().next().value);
|
|
2499
|
+
}
|
|
2500
|
+
delete(key) {
|
|
2501
|
+
this.map.delete(key);
|
|
2502
|
+
}
|
|
2503
|
+
clear() {
|
|
2504
|
+
this.map.clear();
|
|
2505
|
+
}
|
|
2506
|
+
keys() {
|
|
2507
|
+
return [...this.map.keys()];
|
|
2508
|
+
}
|
|
2509
|
+
};
|
|
2510
|
+
function hash(str) {
|
|
2511
|
+
let h1 = 3735928559;
|
|
2512
|
+
let h2 = 1103547991;
|
|
2513
|
+
for (let i = 0; i < str.length; i++) {
|
|
2514
|
+
const ch = str.charCodeAt(i);
|
|
2515
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
2516
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
2517
|
+
}
|
|
2518
|
+
h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909);
|
|
2519
|
+
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
|
|
2520
|
+
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
|
|
2521
|
+
}
|
|
2522
|
+
var CACHE_DIRECTIVE_HEADERS = /* @__PURE__ */ new Set(["cache-control", "pragma"]);
|
|
2523
|
+
function requestTarget(config) {
|
|
2524
|
+
const url = buildURL(
|
|
2525
|
+
buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls),
|
|
2526
|
+
config.params,
|
|
2527
|
+
config.paramsSerializer
|
|
2528
|
+
);
|
|
2529
|
+
return `${(config.method ?? "get").toUpperCase()} ${url}`;
|
|
2530
|
+
}
|
|
2531
|
+
function defaultRequestKey(config) {
|
|
2532
|
+
const skip = config.correlationIdHeader?.toLowerCase();
|
|
2533
|
+
const parts = [];
|
|
2534
|
+
for (const k of Object.keys(config.headers).sort())
|
|
2535
|
+
if (k !== skip && !CACHE_DIRECTIVE_HEADERS.has(k)) parts.push(`${k}:${config.headers[k]}`);
|
|
2536
|
+
if (config.auth) parts.push(`auth:${config.auth.username}:${config.auth.password}`);
|
|
2537
|
+
parts.push(`rt:${config.responseType ?? ""}`);
|
|
2538
|
+
return `${requestTarget(config)} #${hash(parts.join("\n"))}`;
|
|
2539
|
+
}
|
|
2540
|
+
function keyURL(key) {
|
|
2541
|
+
const sp = key.indexOf(" ");
|
|
2542
|
+
const h = key.lastIndexOf(" #");
|
|
2543
|
+
return sp > 0 && h > sp ? key.slice(sp + 1, h) : void 0;
|
|
2544
|
+
}
|
|
2545
|
+
function keyFor(config, opts2) {
|
|
2546
|
+
if (!opts2.methods.includes(config.method)) return void 0;
|
|
2547
|
+
if (config.responseType === "stream") return void 0;
|
|
2548
|
+
if (opts2.key) return opts2.key(config);
|
|
2549
|
+
if (config.data !== void 0) return void 0;
|
|
2550
|
+
return defaultRequestKey(config);
|
|
2551
|
+
}
|
|
2552
|
+
function checkMethods(name, m) {
|
|
2553
|
+
if (!Array.isArray(m)) bad(name, m, "an array");
|
|
2554
|
+
return m.map((x) => String(x).toLowerCase());
|
|
2555
|
+
}
|
|
2556
|
+
function resolveDedupe(v) {
|
|
2557
|
+
if (v === void 0 || v === false) return false;
|
|
2558
|
+
if (v === true) return { methods: [...SAFE_METHODS] };
|
|
2559
|
+
if (typeof v !== "object" || v === null) bad("dedupe", v, "a boolean or an object");
|
|
2560
|
+
if (v.key !== void 0 && typeof v.key !== "function") bad("dedupe.key", v.key, "a function");
|
|
2561
|
+
return { methods: checkMethods("dedupe.methods", v.methods ?? SAFE_METHODS), key: v.key };
|
|
2562
|
+
}
|
|
2563
|
+
function resolveCache(v) {
|
|
2564
|
+
if (v === void 0 || v === false) return false;
|
|
2565
|
+
const o = v === true ? {} : v;
|
|
2566
|
+
if (typeof o !== "object" || o === null) bad("cache", v, "a boolean or an object");
|
|
2567
|
+
const ttl = o.ttl ?? 6e4;
|
|
2568
|
+
if (typeof ttl !== "number" || !(ttl > 0) || !Number.isFinite(ttl))
|
|
2569
|
+
bad("cache.ttl", ttl, "a finite number > 0");
|
|
2570
|
+
if (o.key !== void 0 && typeof o.key !== "function") bad("cache.key", o.key, "a function");
|
|
2571
|
+
const p = o.provider;
|
|
2572
|
+
if (p !== void 0 && (typeof p !== "object" || p === null || typeof p.get !== "function" || typeof p.set !== "function"))
|
|
2573
|
+
bad("cache.provider", p, "an object with get/set/delete/clear");
|
|
2574
|
+
return {
|
|
2575
|
+
ttl,
|
|
2576
|
+
methods: checkMethods("cache.methods", o.methods ?? SAFE_METHODS),
|
|
2577
|
+
key: o.key,
|
|
2578
|
+
provider: p,
|
|
2579
|
+
respectCacheControl: o.respectCacheControl ?? true
|
|
2580
|
+
};
|
|
2581
|
+
}
|
|
2582
|
+
function parseCacheControl(value) {
|
|
2583
|
+
const out = /* @__PURE__ */ new Map();
|
|
2584
|
+
const raw = Array.isArray(value) ? value.join(",") : value;
|
|
2585
|
+
if (typeof raw !== "string") return out;
|
|
2586
|
+
for (const part of raw.split(",")) {
|
|
2587
|
+
const [k, v] = part.split("=", 2).map((s) => s.trim());
|
|
2588
|
+
if (k) out.set(k.toLowerCase(), v === void 0 ? true : v.replace(/^"|"$/g, ""));
|
|
2589
|
+
}
|
|
2590
|
+
return out;
|
|
2591
|
+
}
|
|
2592
|
+
function cloneData(data) {
|
|
2593
|
+
if (data === null || typeof data !== "object") return { ok: true, value: data };
|
|
2594
|
+
if (typeof structuredClone !== "function") return { ok: false, value: data };
|
|
2595
|
+
try {
|
|
2596
|
+
return { ok: true, value: structuredClone(data) };
|
|
2597
|
+
} catch {
|
|
2598
|
+
return { ok: false, value: data };
|
|
2599
|
+
}
|
|
2600
|
+
}
|
|
2601
|
+
function reportCacheError(op, err) {
|
|
2602
|
+
if (typeof console !== "undefined") console.error(`[exios] cache provider ${op} failed:`, err);
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2394
2605
|
// src/core/requestId.ts
|
|
2395
2606
|
var seq = 0;
|
|
2396
2607
|
function createRequestId() {
|
|
@@ -2422,6 +2633,10 @@ var Exios = class {
|
|
|
2422
2633
|
breaker;
|
|
2423
2634
|
registry;
|
|
2424
2635
|
events = new EventHub();
|
|
2636
|
+
memoryCache = new MemoryCache();
|
|
2637
|
+
inflight = new InflightRequests();
|
|
2638
|
+
/** The instance cache: `defaults.cache.provider`, or the built-in in-memory cache. */
|
|
2639
|
+
cache;
|
|
2425
2640
|
constructor(defaults) {
|
|
2426
2641
|
this.defaults = defaults;
|
|
2427
2642
|
this.registry = new BreakerRegistry(
|
|
@@ -2438,6 +2653,50 @@ var Exios = class {
|
|
|
2438
2653
|
state: (key) => this.registry.state(key),
|
|
2439
2654
|
reset: (key) => this.registry.reset(key)
|
|
2440
2655
|
};
|
|
2656
|
+
const provider = () => {
|
|
2657
|
+
const c = this.defaults.cache;
|
|
2658
|
+
return typeof c === "object" && c?.provider || this.memoryCache;
|
|
2659
|
+
};
|
|
2660
|
+
this.cache = {
|
|
2661
|
+
clear: async () => {
|
|
2662
|
+
await provider().clear();
|
|
2663
|
+
},
|
|
2664
|
+
delete: async (key) => {
|
|
2665
|
+
await provider().delete(key);
|
|
2666
|
+
},
|
|
2667
|
+
key: (cfg = {}) => {
|
|
2668
|
+
const resolved = finalizeConfig(resolveConfig(mergeConfig(this.defaults, cfg)));
|
|
2669
|
+
const c = resolveCache(resolved.cache) || resolveCache(true);
|
|
2670
|
+
return c ? keyFor(resolved, c) : void 0;
|
|
2671
|
+
},
|
|
2672
|
+
invalidate: async (match) => {
|
|
2673
|
+
const p = provider();
|
|
2674
|
+
if (typeof p.keys !== "function")
|
|
2675
|
+
throw new ExiosError(
|
|
2676
|
+
"cache.invalidate needs a cache provider with keys()",
|
|
2677
|
+
ExiosError.ERR_NOT_SUPPORT
|
|
2678
|
+
);
|
|
2679
|
+
let test;
|
|
2680
|
+
if (typeof match === "function") test = match;
|
|
2681
|
+
else if (match instanceof RegExp) test = (k) => match.test(k);
|
|
2682
|
+
else {
|
|
2683
|
+
const target = requestTarget({ ...this.defaults, url: match, params: void 0 });
|
|
2684
|
+
const prefix = target.slice(target.indexOf(" ") + 1);
|
|
2685
|
+
test = (k) => {
|
|
2686
|
+
const u = keyURL(k);
|
|
2687
|
+
return u !== void 0 && (u === prefix || u.startsWith(prefix + "/") || u.startsWith(prefix + "?"));
|
|
2688
|
+
};
|
|
2689
|
+
}
|
|
2690
|
+
let n = 0;
|
|
2691
|
+
for (const k of [...await p.keys()]) {
|
|
2692
|
+
if (test(k)) {
|
|
2693
|
+
await p.delete(k);
|
|
2694
|
+
n++;
|
|
2695
|
+
}
|
|
2696
|
+
}
|
|
2697
|
+
return n;
|
|
2698
|
+
}
|
|
2699
|
+
};
|
|
2441
2700
|
}
|
|
2442
2701
|
async request(configOrUrl, config) {
|
|
2443
2702
|
const cfg = typeof configOrUrl === "string" ? { ...config, url: configOrUrl } : { ...configOrUrl };
|
|
@@ -2480,13 +2739,104 @@ var Exios = class {
|
|
|
2480
2739
|
off(name, listener) {
|
|
2481
2740
|
this.events.off(name, listener);
|
|
2482
2741
|
}
|
|
2483
|
-
/**
|
|
2742
|
+
/**
|
|
2743
|
+
* After request interceptors: ids → cache read → dedupe → network → cache write.
|
|
2744
|
+
* Cache hits and deduplicated followers make no network request and emit no lifecycle events.
|
|
2745
|
+
*/
|
|
2484
2746
|
async dispatch(config) {
|
|
2485
|
-
const
|
|
2486
|
-
|
|
2747
|
+
const start = Date.now();
|
|
2748
|
+
config.requestId ??= createRequestId();
|
|
2749
|
+
const totalTimeout = config.totalTimeout ?? 0;
|
|
2750
|
+
if (typeof totalTimeout !== "number" || !(totalTimeout >= 0) || !Number.isFinite(totalTimeout))
|
|
2751
|
+
bad("totalTimeout", totalTimeout, "a non-negative finite number");
|
|
2752
|
+
const cacheCfg = resolveCache(config.cache);
|
|
2753
|
+
const dedupeCfg = resolveDedupe(config.dedupe);
|
|
2754
|
+
const cacheKey = cacheCfg ? keyFor(config, cacheCfg) : void 0;
|
|
2755
|
+
const dedupeKey = dedupeCfg ? keyFor(config, dedupeCfg) : void 0;
|
|
2756
|
+
const provider = cacheCfg ? cacheCfg.provider ?? this.memoryCache : void 0;
|
|
2757
|
+
const reqCC = cacheCfg && cacheCfg.respectCacheControl ? parseCacheControl(config.headers["cache-control"]) : void 0;
|
|
2758
|
+
if (cacheKey !== void 0 && provider && !reqCC?.has("no-cache") && !reqCC?.has("no-store")) {
|
|
2759
|
+
let entry;
|
|
2760
|
+
try {
|
|
2761
|
+
entry = await provider.get(cacheKey);
|
|
2762
|
+
} catch (e) {
|
|
2763
|
+
reportCacheError("get", e);
|
|
2764
|
+
}
|
|
2765
|
+
if (entry && entry.expiresAt > Date.now()) {
|
|
2766
|
+
const copy2 = cloneData(entry.data);
|
|
2767
|
+
return this.localResponse(config, start, {
|
|
2768
|
+
data: copy2.value,
|
|
2769
|
+
status: entry.status,
|
|
2770
|
+
statusText: entry.statusText,
|
|
2771
|
+
headers: { ...entry.headers },
|
|
2772
|
+
request: null,
|
|
2773
|
+
meta: { attempts: 0, cached: true }
|
|
2774
|
+
});
|
|
2775
|
+
}
|
|
2776
|
+
}
|
|
2487
2777
|
const header = config.correlationIdHeader?.toLowerCase();
|
|
2488
2778
|
if (header && config.headers[header] === void 0)
|
|
2489
|
-
config.headers = { ...config.headers, [header]: correlationId ?? requestId };
|
|
2779
|
+
config.headers = { ...config.headers, [header]: config.correlationId ?? config.requestId };
|
|
2780
|
+
const network = async (c) => {
|
|
2781
|
+
const response2 = await this.network(c, start, totalTimeout);
|
|
2782
|
+
if (cacheKey !== void 0 && provider && cacheCfg && !reqCC?.has("no-store"))
|
|
2783
|
+
this.store(provider, cacheKey, response2, cacheCfg);
|
|
2784
|
+
return response2;
|
|
2785
|
+
};
|
|
2786
|
+
if (dedupeKey === void 0) return network(config);
|
|
2787
|
+
const { response, leader } = await this.inflight.join(
|
|
2788
|
+
dedupeKey,
|
|
2789
|
+
config,
|
|
2790
|
+
(signal) => network({ ...config, signal, cancelToken: void 0 })
|
|
2791
|
+
);
|
|
2792
|
+
if (leader) return response;
|
|
2793
|
+
const copy = cloneData(response.data);
|
|
2794
|
+
return this.localResponse(config, start, {
|
|
2795
|
+
...response,
|
|
2796
|
+
data: copy.value,
|
|
2797
|
+
headers: { ...response.headers },
|
|
2798
|
+
meta: { attempts: response.meta.attempts, deduped: true }
|
|
2799
|
+
});
|
|
2800
|
+
}
|
|
2801
|
+
/** A response made without a network request of its own (cache hit, deduplicated follower). */
|
|
2802
|
+
localResponse(config, start, r) {
|
|
2803
|
+
r.meta.requestId = config.requestId;
|
|
2804
|
+
if (config.correlationId !== void 0) r.meta.correlationId = config.correlationId;
|
|
2805
|
+
r.meta.durationMs = Date.now() - start;
|
|
2806
|
+
return { ...r, config };
|
|
2807
|
+
}
|
|
2808
|
+
/** Store a successful response; provider failures are reported, never thrown. */
|
|
2809
|
+
store(provider, key, response, cfg) {
|
|
2810
|
+
if (response.status < 200 || response.status >= 300) return;
|
|
2811
|
+
let ttl = cfg.ttl;
|
|
2812
|
+
if (cfg.respectCacheControl) {
|
|
2813
|
+
const cc = parseCacheControl(response.headers["cache-control"]);
|
|
2814
|
+
if (cc.has("no-store") || cc.has("no-cache")) return;
|
|
2815
|
+
const maxAge = Number(cc.get("max-age"));
|
|
2816
|
+
if (cc.has("max-age") && Number.isFinite(maxAge)) ttl = Math.min(ttl, maxAge * 1e3);
|
|
2817
|
+
}
|
|
2818
|
+
if (ttl <= 0) return;
|
|
2819
|
+
const copy = cloneData(response.data);
|
|
2820
|
+
if (!copy.ok) return;
|
|
2821
|
+
const now = Date.now();
|
|
2822
|
+
try {
|
|
2823
|
+
const r = provider.set(key, {
|
|
2824
|
+
status: response.status,
|
|
2825
|
+
statusText: response.statusText,
|
|
2826
|
+
headers: { ...response.headers },
|
|
2827
|
+
data: copy.value,
|
|
2828
|
+
storedAt: now,
|
|
2829
|
+
expiresAt: now + ttl
|
|
2830
|
+
});
|
|
2831
|
+
if (isThenable(r)) Promise.resolve(r).catch((e) => reportCacheError("set", e));
|
|
2832
|
+
} catch (e) {
|
|
2833
|
+
reportCacheError("set", e);
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
/** totalTimeout, timing, error context and lifecycle events around dispatchRequest. */
|
|
2837
|
+
async network(config, start, totalTimeout) {
|
|
2838
|
+
const requestId = config.requestId;
|
|
2839
|
+
const { correlationId, metadata } = config;
|
|
2490
2840
|
const hub = this.events;
|
|
2491
2841
|
const url = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls);
|
|
2492
2842
|
const base = () => ({
|
|
@@ -2495,9 +2845,9 @@ var Exios = class {
|
|
|
2495
2845
|
method: config.method,
|
|
2496
2846
|
url,
|
|
2497
2847
|
config,
|
|
2848
|
+
metadata,
|
|
2498
2849
|
timestamp: Date.now()
|
|
2499
2850
|
});
|
|
2500
|
-
const start = Date.now();
|
|
2501
2851
|
const observer = {
|
|
2502
2852
|
onRetry: (o) => hub.emit("retry", () => ({
|
|
2503
2853
|
...base(),
|
|
@@ -2510,15 +2860,55 @@ var Exios = class {
|
|
|
2510
2860
|
if (TIMEOUT_CODES.has(error.code ?? ""))
|
|
2511
2861
|
hub.emit("timeout", () => ({
|
|
2512
2862
|
...base(),
|
|
2863
|
+
kind: "attempt",
|
|
2513
2864
|
attempt,
|
|
2514
2865
|
timeoutMs: config.timeout ?? 0,
|
|
2515
2866
|
error
|
|
2516
2867
|
}));
|
|
2517
2868
|
}
|
|
2518
2869
|
};
|
|
2870
|
+
let timer;
|
|
2871
|
+
let totalFired = false;
|
|
2872
|
+
let joined;
|
|
2873
|
+
let dispatchConfig = config;
|
|
2874
|
+
if (totalTimeout > 0) {
|
|
2875
|
+
const ac = new AbortController();
|
|
2876
|
+
timer = setTimeout(() => {
|
|
2877
|
+
totalFired = true;
|
|
2878
|
+
ac.abort(new Error("totalTimeout"));
|
|
2879
|
+
}, totalTimeout);
|
|
2880
|
+
joined = combineSignals(config.signal, ac.signal);
|
|
2881
|
+
dispatchConfig = { ...config, signal: joined.signal };
|
|
2882
|
+
}
|
|
2883
|
+
let deferCleanup = false;
|
|
2519
2884
|
hub.emit("request", base);
|
|
2520
2885
|
try {
|
|
2521
|
-
|
|
2886
|
+
let response;
|
|
2887
|
+
try {
|
|
2888
|
+
response = await dispatchRequest(dispatchConfig, this.registry, observer);
|
|
2889
|
+
} catch (e) {
|
|
2890
|
+
if (!totalFired || config.signal?.aborted || !isCancel(e)) throw e;
|
|
2891
|
+
const err = new ExiosError(
|
|
2892
|
+
`total timeout of ${totalTimeout}ms exceeded`,
|
|
2893
|
+
config.transitional?.clarifyTimeoutError ? ExiosError.ETIMEDOUT : ExiosError.ECONNABORTED,
|
|
2894
|
+
config,
|
|
2895
|
+
e.request
|
|
2896
|
+
);
|
|
2897
|
+
err.cause = e;
|
|
2898
|
+
err.meta.attempts = e.meta.attempts;
|
|
2899
|
+
hub.emit("timeout", () => ({
|
|
2900
|
+
...base(),
|
|
2901
|
+
kind: "total",
|
|
2902
|
+
attempt: err.meta.attempts,
|
|
2903
|
+
timeoutMs: totalTimeout,
|
|
2904
|
+
error: err
|
|
2905
|
+
}));
|
|
2906
|
+
throw err;
|
|
2907
|
+
}
|
|
2908
|
+
if (joined && isOneShotBody(response.data)) {
|
|
2909
|
+
response.data = whenBodyDone(response.data, joined.cleanup);
|
|
2910
|
+
deferCleanup = true;
|
|
2911
|
+
}
|
|
2522
2912
|
const durationMs = Date.now() - start;
|
|
2523
2913
|
const meta = response.meta;
|
|
2524
2914
|
meta.requestId = requestId;
|
|
@@ -2558,6 +2948,9 @@ var Exios = class {
|
|
|
2558
2948
|
}));
|
|
2559
2949
|
}
|
|
2560
2950
|
throw e;
|
|
2951
|
+
} finally {
|
|
2952
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
2953
|
+
if (joined && !deferCleanup) joined.cleanup();
|
|
2561
2954
|
}
|
|
2562
2955
|
}
|
|
2563
2956
|
/** Runs every request interceptor in this tick with promise-chain semantics, then dispatches. */
|
|
@@ -2670,7 +3063,10 @@ function detachDefaults(d) {
|
|
|
2670
3063
|
"transitional",
|
|
2671
3064
|
"fetchOptions",
|
|
2672
3065
|
"auth",
|
|
2673
|
-
"breaker"
|
|
3066
|
+
"breaker",
|
|
3067
|
+
"metadata",
|
|
3068
|
+
"cache",
|
|
3069
|
+
"dedupe"
|
|
2674
3070
|
]) {
|
|
2675
3071
|
if (isPlainObject4(out[k])) out[k] = { ...out[k] };
|
|
2676
3072
|
}
|
|
@@ -2700,6 +3096,7 @@ function createInstance(defaults) {
|
|
|
2700
3096
|
});
|
|
2701
3097
|
instance.interceptors = context.interceptors;
|
|
2702
3098
|
instance.breaker = context.breaker;
|
|
3099
|
+
instance.cache = context.cache;
|
|
2703
3100
|
instance.create = (cfg) => createInstance(detachDefaults(mergeConfig(context.defaults, cfg)));
|
|
2704
3101
|
return instance;
|
|
2705
3102
|
}
|
|
@@ -2715,7 +3112,8 @@ var statics = {
|
|
|
2715
3112
|
isExiosError,
|
|
2716
3113
|
isCancel,
|
|
2717
3114
|
mergeConfig,
|
|
2718
|
-
toFormData
|
|
3115
|
+
toFormData,
|
|
3116
|
+
MemoryCache
|
|
2719
3117
|
};
|
|
2720
3118
|
var exios = Object.assign(
|
|
2721
3119
|
createInstance(createDefaults()),
|
|
@@ -2732,6 +3130,7 @@ export {
|
|
|
2732
3130
|
CanceledError,
|
|
2733
3131
|
CircuitOpenError,
|
|
2734
3132
|
ExiosError,
|
|
3133
|
+
MemoryCache,
|
|
2735
3134
|
create,
|
|
2736
3135
|
node_default as default,
|
|
2737
3136
|
isCancel,
|
package/package.json
CHANGED