@indra211/exios 2.0.0 → 3.0.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 +93 -0
- package/dist/index.cjs +237 -11
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +113 -8
- package/dist/index.js +237 -11
- package/dist/index.node.cjs +237 -11
- package/dist/index.node.js +237 -11
- package/package.json +11 -3
package/dist/index.node.cjs
CHANGED
|
@@ -34,6 +34,36 @@ __export(cjs_node_exports, {
|
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(cjs_node_exports);
|
|
36
36
|
|
|
37
|
+
// src/core/redact.ts
|
|
38
|
+
var REDACTED = "[REDACTED]";
|
|
39
|
+
var SENSITIVE_HEADERS = /* @__PURE__ */ new Set([
|
|
40
|
+
"authorization",
|
|
41
|
+
"proxy-authorization",
|
|
42
|
+
"cookie",
|
|
43
|
+
"set-cookie",
|
|
44
|
+
"x-api-key",
|
|
45
|
+
"x-auth-token",
|
|
46
|
+
"x-xsrf-token",
|
|
47
|
+
"x-csrf-token"
|
|
48
|
+
]);
|
|
49
|
+
function redactHeaders(headers) {
|
|
50
|
+
if (!headers || typeof headers !== "object") return headers;
|
|
51
|
+
const out = {};
|
|
52
|
+
for (const [k, v] of Object.entries(headers))
|
|
53
|
+
out[k] = SENSITIVE_HEADERS.has(k.toLowerCase()) ? REDACTED : v;
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
function redactConfig(config) {
|
|
57
|
+
if (!config || typeof config !== "object") return config;
|
|
58
|
+
const c = { ...config };
|
|
59
|
+
if (c.headers && typeof c.headers === "object") c.headers = redactHeaders(c.headers);
|
|
60
|
+
if (c.auth) c.auth = { username: c.auth.username, password: REDACTED };
|
|
61
|
+
if (c.proxy && typeof c.proxy === "object" && c.proxy.auth)
|
|
62
|
+
c.proxy = { ...c.proxy, auth: { username: c.proxy.auth.username, password: REDACTED } };
|
|
63
|
+
if (c.data !== void 0) c.data = REDACTED;
|
|
64
|
+
return c;
|
|
65
|
+
}
|
|
66
|
+
|
|
37
67
|
// src/errors/ExiosError.ts
|
|
38
68
|
var ExiosError = class _ExiosError extends Error {
|
|
39
69
|
static ERR_BAD_REQUEST = "ERR_BAD_REQUEST";
|
|
@@ -63,13 +93,14 @@ var ExiosError = class _ExiosError extends Error {
|
|
|
63
93
|
this.response = response;
|
|
64
94
|
this.status = response?.status;
|
|
65
95
|
}
|
|
96
|
+
/** Log-safe: credentials, sensitive headers (Authorization, Cookie, API keys) and the body are redacted. */
|
|
66
97
|
toJSON() {
|
|
67
98
|
return {
|
|
68
99
|
message: this.message,
|
|
69
100
|
name: this.name,
|
|
70
101
|
code: this.code,
|
|
71
102
|
status: this.status,
|
|
72
|
-
config: this.config,
|
|
103
|
+
config: redactConfig(this.config),
|
|
73
104
|
meta: this.meta,
|
|
74
105
|
stack: this.stack
|
|
75
106
|
};
|
|
@@ -1743,14 +1774,16 @@ function defaultIsFailure(o) {
|
|
|
1743
1774
|
return status >= 500 || status === 408 || status === 429;
|
|
1744
1775
|
}
|
|
1745
1776
|
var CircuitBreaker = class {
|
|
1746
|
-
constructor(key, cfg, now = Date.now) {
|
|
1777
|
+
constructor(key, cfg, now = Date.now, onTransition) {
|
|
1747
1778
|
this.key = key;
|
|
1748
1779
|
this.cfg = cfg;
|
|
1749
1780
|
this.now = now;
|
|
1781
|
+
this.onTransition = onTransition;
|
|
1750
1782
|
}
|
|
1751
1783
|
key;
|
|
1752
1784
|
cfg;
|
|
1753
1785
|
now;
|
|
1786
|
+
onTransition;
|
|
1754
1787
|
_state = "closed";
|
|
1755
1788
|
epoch = 0;
|
|
1756
1789
|
openedAt = 0;
|
|
@@ -1854,22 +1887,25 @@ var CircuitBreaker = class {
|
|
|
1854
1887
|
this.cfg.onStateChange?.(this.key, from, to);
|
|
1855
1888
|
} catch {
|
|
1856
1889
|
}
|
|
1890
|
+
this.onTransition?.(this.key, from, to);
|
|
1857
1891
|
}
|
|
1858
1892
|
};
|
|
1859
1893
|
var BreakerRegistry = class {
|
|
1860
|
-
constructor(getConfig, now = Date.now) {
|
|
1894
|
+
constructor(getConfig, now = Date.now, onTransition) {
|
|
1861
1895
|
this.getConfig = getConfig;
|
|
1862
1896
|
this.now = now;
|
|
1897
|
+
this.onTransition = onTransition;
|
|
1863
1898
|
}
|
|
1864
1899
|
getConfig;
|
|
1865
1900
|
now;
|
|
1901
|
+
onTransition;
|
|
1866
1902
|
breakers = /* @__PURE__ */ new Map();
|
|
1867
1903
|
get(key) {
|
|
1868
1904
|
const cfg = this.getConfig();
|
|
1869
1905
|
if (cfg === false) return null;
|
|
1870
1906
|
let b = this.breakers.get(key);
|
|
1871
1907
|
if (!b) {
|
|
1872
|
-
b = new CircuitBreaker(key, cfg, this.now);
|
|
1908
|
+
b = new CircuitBreaker(key, cfg, this.now, this.onTransition);
|
|
1873
1909
|
this.breakers.set(key, b);
|
|
1874
1910
|
}
|
|
1875
1911
|
return b;
|
|
@@ -1974,6 +2010,7 @@ async function runWithRetry(attempt, cfg, config, hooks) {
|
|
|
1974
2010
|
cancelBody(outcome.response?.data);
|
|
1975
2011
|
if (hooks.signal?.aborted) throw abortError(hooks.signal);
|
|
1976
2012
|
cfg.onRetry?.({ ...outcome, attempt: attempts, delayMs: delay });
|
|
2013
|
+
hooks.onRetry?.({ ...outcome, attempt: attempts, delayMs: delay });
|
|
1977
2014
|
await sleep(delay, hooks.signal);
|
|
1978
2015
|
hooks.beforeRetry?.();
|
|
1979
2016
|
}
|
|
@@ -1995,7 +2032,7 @@ function defaultBreakerKey(config) {
|
|
|
1995
2032
|
return full;
|
|
1996
2033
|
}
|
|
1997
2034
|
}
|
|
1998
|
-
async function dispatchRequest(config, registry2) {
|
|
2035
|
+
async function dispatchRequest(config, registry2, observer) {
|
|
1999
2036
|
const token = config.cancelToken;
|
|
2000
2037
|
if (token) {
|
|
2001
2038
|
if (token.reason) {
|
|
@@ -2012,7 +2049,8 @@ async function dispatchRequest(config, registry2) {
|
|
|
2012
2049
|
try {
|
|
2013
2050
|
const response = await dispatchRequest(
|
|
2014
2051
|
{ ...config, signal: joined.signal, cancelToken: void 0 },
|
|
2015
|
-
registry2
|
|
2052
|
+
registry2,
|
|
2053
|
+
observer
|
|
2016
2054
|
);
|
|
2017
2055
|
if (isOneShotBody(response.data)) {
|
|
2018
2056
|
response.data = whenBodyDone(response.data, release);
|
|
@@ -2031,6 +2069,7 @@ async function dispatchRequest(config, registry2) {
|
|
|
2031
2069
|
const breaker = bcfg === false ? null : registry2.get(bcfg.key ? bcfg.key(config) : defaultBreakerKey(config));
|
|
2032
2070
|
const ticket = breaker?.acquire(config);
|
|
2033
2071
|
let attempts = 0;
|
|
2072
|
+
let responseTimeMs;
|
|
2034
2073
|
try {
|
|
2035
2074
|
let adapter;
|
|
2036
2075
|
try {
|
|
@@ -2043,15 +2082,31 @@ async function dispatchRequest(config, registry2) {
|
|
|
2043
2082
|
try {
|
|
2044
2083
|
result = await runWithRetry(
|
|
2045
2084
|
() => {
|
|
2046
|
-
attempts
|
|
2047
|
-
|
|
2085
|
+
const n = ++attempts;
|
|
2086
|
+
const t0 = Date.now();
|
|
2087
|
+
const failed = (e) => {
|
|
2088
|
+
responseTimeMs = Date.now() - t0;
|
|
2089
|
+
if (isExiosError(e)) observer?.onAttemptError?.(n, e);
|
|
2090
|
+
throw e;
|
|
2091
|
+
};
|
|
2092
|
+
let result2;
|
|
2093
|
+
try {
|
|
2094
|
+
result2 = Promise.resolve(adapter(config));
|
|
2095
|
+
} catch (e) {
|
|
2096
|
+
return failed(e);
|
|
2097
|
+
}
|
|
2098
|
+
return result2.then((r) => {
|
|
2099
|
+
responseTimeMs = Date.now() - t0;
|
|
2100
|
+
return r;
|
|
2101
|
+
}, failed);
|
|
2048
2102
|
},
|
|
2049
2103
|
ticket?.probe ? false : config.retry,
|
|
2050
2104
|
// half-open probes get exactly one attempt
|
|
2051
2105
|
config,
|
|
2052
2106
|
{
|
|
2053
2107
|
signal: config.signal,
|
|
2054
|
-
beforeRetry: breaker && ticket ? () => breaker.check(ticket) : void 0
|
|
2108
|
+
beforeRetry: breaker && ticket ? () => breaker.check(ticket) : void 0,
|
|
2109
|
+
onRetry: observer?.onRetry
|
|
2055
2110
|
}
|
|
2056
2111
|
);
|
|
2057
2112
|
} catch (e) {
|
|
@@ -2070,6 +2125,7 @@ async function dispatchRequest(config, registry2) {
|
|
|
2070
2125
|
if (outcome.error) throw outcome.error;
|
|
2071
2126
|
const response = outcome.response;
|
|
2072
2127
|
response.meta.attempts = attempts;
|
|
2128
|
+
if (responseTimeMs !== void 0) response.meta.responseTimeMs = responseTimeMs;
|
|
2073
2129
|
const { status } = response;
|
|
2074
2130
|
if (config.validateStatus && !config.validateStatus(status)) {
|
|
2075
2131
|
const err = new ExiosError(
|
|
@@ -2088,6 +2144,53 @@ async function dispatchRequest(config, registry2) {
|
|
|
2088
2144
|
}
|
|
2089
2145
|
}
|
|
2090
2146
|
|
|
2147
|
+
// src/core/events.ts
|
|
2148
|
+
var EventHub = class {
|
|
2149
|
+
listeners = /* @__PURE__ */ new Map();
|
|
2150
|
+
on(name, listener) {
|
|
2151
|
+
if (typeof listener !== "function")
|
|
2152
|
+
throw new TypeError(`exios: listener for "${name}" must be a function`);
|
|
2153
|
+
let set = this.listeners.get(name);
|
|
2154
|
+
if (!set) this.listeners.set(name, set = /* @__PURE__ */ new Set());
|
|
2155
|
+
set.add(listener);
|
|
2156
|
+
return () => this.off(name, listener);
|
|
2157
|
+
}
|
|
2158
|
+
once(name, listener) {
|
|
2159
|
+
const off = this.on(name, (e) => {
|
|
2160
|
+
off();
|
|
2161
|
+
listener(e);
|
|
2162
|
+
});
|
|
2163
|
+
return off;
|
|
2164
|
+
}
|
|
2165
|
+
off(name, listener) {
|
|
2166
|
+
const set = this.listeners.get(name);
|
|
2167
|
+
if (!set) return;
|
|
2168
|
+
if (listener === void 0) set.clear();
|
|
2169
|
+
else set.delete(listener);
|
|
2170
|
+
if (set.size === 0) this.listeners.delete(name);
|
|
2171
|
+
}
|
|
2172
|
+
has(name) {
|
|
2173
|
+
return this.listeners.has(name);
|
|
2174
|
+
}
|
|
2175
|
+
listenerCount(name) {
|
|
2176
|
+
return this.listeners.get(name)?.size ?? 0;
|
|
2177
|
+
}
|
|
2178
|
+
/** Build the payload only when someone listens: `emit('x', () => ({ ... }))`. */
|
|
2179
|
+
emit(name, payload) {
|
|
2180
|
+
const set = this.listeners.get(name);
|
|
2181
|
+
if (!set) return;
|
|
2182
|
+
const event = payload();
|
|
2183
|
+
for (const l of [...set]) {
|
|
2184
|
+
try {
|
|
2185
|
+
l(event);
|
|
2186
|
+
} catch (err) {
|
|
2187
|
+
if (typeof console !== "undefined")
|
|
2188
|
+
console.error(`[exios] "${name}" listener threw:`, err);
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
};
|
|
2193
|
+
|
|
2091
2194
|
// src/core/InterceptorManager.ts
|
|
2092
2195
|
var InterceptorManager = class {
|
|
2093
2196
|
handlers = [];
|
|
@@ -2324,7 +2427,27 @@ function assertRequestBreaker(b) {
|
|
|
2324
2427
|
}
|
|
2325
2428
|
}
|
|
2326
2429
|
|
|
2430
|
+
// src/core/requestId.ts
|
|
2431
|
+
var seq = 0;
|
|
2432
|
+
function createRequestId() {
|
|
2433
|
+
const c = globalThis.crypto;
|
|
2434
|
+
if (typeof c?.randomUUID === "function") {
|
|
2435
|
+
try {
|
|
2436
|
+
return c.randomUUID();
|
|
2437
|
+
} catch {
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
seq = (seq + 1) % Number.MAX_SAFE_INTEGER;
|
|
2441
|
+
return `${Date.now().toString(36)}-${seq.toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
|
|
2442
|
+
}
|
|
2443
|
+
|
|
2327
2444
|
// src/core/Exios.ts
|
|
2445
|
+
var CIRCUIT_EVENTS = {
|
|
2446
|
+
open: "circuitOpen",
|
|
2447
|
+
"half-open": "circuitHalfOpen",
|
|
2448
|
+
closed: "circuitClose"
|
|
2449
|
+
};
|
|
2450
|
+
var TIMEOUT_CODES = /* @__PURE__ */ new Set(["ECONNABORTED", "ETIMEDOUT"]);
|
|
2328
2451
|
var isThenable = (v) => typeof v === "object" && v !== null && typeof v.then === "function";
|
|
2329
2452
|
var Exios = class {
|
|
2330
2453
|
defaults;
|
|
@@ -2334,10 +2457,18 @@ var Exios = class {
|
|
|
2334
2457
|
};
|
|
2335
2458
|
breaker;
|
|
2336
2459
|
registry;
|
|
2460
|
+
events = new EventHub();
|
|
2337
2461
|
constructor(defaults) {
|
|
2338
2462
|
this.defaults = defaults;
|
|
2339
2463
|
this.registry = new BreakerRegistry(
|
|
2340
|
-
() => resolveBreaker(this.defaults.breaker)
|
|
2464
|
+
() => resolveBreaker(this.defaults.breaker),
|
|
2465
|
+
Date.now,
|
|
2466
|
+
(key, from, to) => this.events.emit(CIRCUIT_EVENTS[to], () => ({
|
|
2467
|
+
key,
|
|
2468
|
+
from,
|
|
2469
|
+
to,
|
|
2470
|
+
timestamp: Date.now()
|
|
2471
|
+
}))
|
|
2341
2472
|
);
|
|
2342
2473
|
this.breaker = {
|
|
2343
2474
|
state: (key) => this.registry.state(key),
|
|
@@ -2355,7 +2486,7 @@ var Exios = class {
|
|
|
2355
2486
|
synchronous = synchronous && h.synchronous;
|
|
2356
2487
|
requestChain.unshift(h);
|
|
2357
2488
|
});
|
|
2358
|
-
const dispatch = (c) =>
|
|
2489
|
+
const dispatch = (c) => this.dispatch(finalizeConfig(c));
|
|
2359
2490
|
let promise;
|
|
2360
2491
|
if (synchronous) {
|
|
2361
2492
|
promise = this.runSync(requestChain, resolved, dispatch);
|
|
@@ -2373,6 +2504,98 @@ var Exios = class {
|
|
|
2373
2504
|
});
|
|
2374
2505
|
return promise;
|
|
2375
2506
|
}
|
|
2507
|
+
/** Subscribe to a lifecycle event; returns a function that unsubscribes. */
|
|
2508
|
+
on(name, listener) {
|
|
2509
|
+
return this.events.on(name, listener);
|
|
2510
|
+
}
|
|
2511
|
+
/** Like `on`, but the listener runs at most once. */
|
|
2512
|
+
once(name, listener) {
|
|
2513
|
+
return this.events.once(name, listener);
|
|
2514
|
+
}
|
|
2515
|
+
/** Remove one listener, or every listener of `name` when `listener` is omitted. */
|
|
2516
|
+
off(name, listener) {
|
|
2517
|
+
this.events.off(name, listener);
|
|
2518
|
+
}
|
|
2519
|
+
/** Ids, correlation header, timing, error context and lifecycle events around dispatchRequest. */
|
|
2520
|
+
async dispatch(config) {
|
|
2521
|
+
const requestId = config.requestId ??= createRequestId();
|
|
2522
|
+
const { correlationId } = config;
|
|
2523
|
+
const header = config.correlationIdHeader?.toLowerCase();
|
|
2524
|
+
if (header && config.headers[header] === void 0)
|
|
2525
|
+
config.headers = { ...config.headers, [header]: correlationId ?? requestId };
|
|
2526
|
+
const hub = this.events;
|
|
2527
|
+
const url = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls);
|
|
2528
|
+
const base = () => ({
|
|
2529
|
+
requestId,
|
|
2530
|
+
correlationId,
|
|
2531
|
+
method: config.method,
|
|
2532
|
+
url,
|
|
2533
|
+
config,
|
|
2534
|
+
timestamp: Date.now()
|
|
2535
|
+
});
|
|
2536
|
+
const start = Date.now();
|
|
2537
|
+
const observer = {
|
|
2538
|
+
onRetry: (o) => hub.emit("retry", () => ({
|
|
2539
|
+
...base(),
|
|
2540
|
+
attempt: o.attempt,
|
|
2541
|
+
delayMs: o.delayMs,
|
|
2542
|
+
error: o.error,
|
|
2543
|
+
status: o.response?.status ?? o.error?.response?.status
|
|
2544
|
+
})),
|
|
2545
|
+
onAttemptError: (attempt, error) => {
|
|
2546
|
+
if (TIMEOUT_CODES.has(error.code ?? ""))
|
|
2547
|
+
hub.emit("timeout", () => ({
|
|
2548
|
+
...base(),
|
|
2549
|
+
attempt,
|
|
2550
|
+
timeoutMs: config.timeout ?? 0,
|
|
2551
|
+
error
|
|
2552
|
+
}));
|
|
2553
|
+
}
|
|
2554
|
+
};
|
|
2555
|
+
hub.emit("request", base);
|
|
2556
|
+
try {
|
|
2557
|
+
const response = await dispatchRequest(config, this.registry, observer);
|
|
2558
|
+
const durationMs = Date.now() - start;
|
|
2559
|
+
const meta = response.meta;
|
|
2560
|
+
meta.requestId = requestId;
|
|
2561
|
+
if (correlationId !== void 0) meta.correlationId = correlationId;
|
|
2562
|
+
meta.durationMs = durationMs;
|
|
2563
|
+
hub.emit("response", () => ({
|
|
2564
|
+
...base(),
|
|
2565
|
+
response,
|
|
2566
|
+
status: response.status,
|
|
2567
|
+
attempts: meta.attempts,
|
|
2568
|
+
durationMs,
|
|
2569
|
+
responseTimeMs: meta.responseTimeMs ?? durationMs
|
|
2570
|
+
}));
|
|
2571
|
+
return response;
|
|
2572
|
+
} catch (e) {
|
|
2573
|
+
const durationMs = Date.now() - start;
|
|
2574
|
+
let attempts = 0;
|
|
2575
|
+
if (isExiosError(e)) {
|
|
2576
|
+
const meta = e.meta;
|
|
2577
|
+
meta.requestId ??= requestId;
|
|
2578
|
+
if (correlationId !== void 0) meta.correlationId ??= correlationId;
|
|
2579
|
+
meta.method ??= config.method;
|
|
2580
|
+
meta.url ??= url;
|
|
2581
|
+
meta.durationMs ??= durationMs;
|
|
2582
|
+
attempts = meta.attempts;
|
|
2583
|
+
}
|
|
2584
|
+
if (isCancel(e)) {
|
|
2585
|
+
hub.emit("canceled", () => ({ ...base(), error: e, attempts, durationMs }));
|
|
2586
|
+
} else {
|
|
2587
|
+
hub.emit("error", () => ({
|
|
2588
|
+
...base(),
|
|
2589
|
+
error: e,
|
|
2590
|
+
code: isExiosError(e) ? e.code : void 0,
|
|
2591
|
+
status: isExiosError(e) ? e.status ?? e.response?.status : void 0,
|
|
2592
|
+
attempts,
|
|
2593
|
+
durationMs
|
|
2594
|
+
}));
|
|
2595
|
+
}
|
|
2596
|
+
throw e;
|
|
2597
|
+
}
|
|
2598
|
+
}
|
|
2376
2599
|
/** Runs every request interceptor in this tick with promise-chain semantics, then dispatches. */
|
|
2377
2600
|
runSync(chain, config, dispatch) {
|
|
2378
2601
|
let value = config;
|
|
@@ -2450,6 +2673,9 @@ var Exios = class {
|
|
|
2450
2673
|
};
|
|
2451
2674
|
var METHODS = [
|
|
2452
2675
|
"request",
|
|
2676
|
+
"on",
|
|
2677
|
+
"once",
|
|
2678
|
+
"off",
|
|
2453
2679
|
"getUri",
|
|
2454
2680
|
"get",
|
|
2455
2681
|
"delete",
|