@indra211/exios 3.1.2 → 3.3.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 +144 -1
- package/dist/index.cjs +256 -4
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +92 -3
- package/dist/index.js +257 -4
- package/dist/index.node.cjs +256 -4
- package/dist/index.node.js +257 -4
- package/dist/otel.cjs +199 -0
- package/dist/otel.d.cts +79 -0
- package/dist/otel.d.ts +79 -0
- package/dist/otel.js +168 -0
- package/dist/testing.cjs +275 -0
- package/dist/testing.d.cts +87 -0
- package/dist/testing.d.ts +87 -0
- package/dist/testing.js +244 -0
- package/package.json +36 -1
package/dist/index.js
CHANGED
|
@@ -27,6 +27,51 @@ function redactConfig(config) {
|
|
|
27
27
|
if (c.data !== void 0) c.data = REDACTED;
|
|
28
28
|
return c;
|
|
29
29
|
}
|
|
30
|
+
var SENSITIVE_QUERY_PARAMS = /* @__PURE__ */ new Set([
|
|
31
|
+
"access_token",
|
|
32
|
+
"api_key",
|
|
33
|
+
"apikey",
|
|
34
|
+
"auth",
|
|
35
|
+
"client_secret",
|
|
36
|
+
"id_token",
|
|
37
|
+
"key",
|
|
38
|
+
"password",
|
|
39
|
+
"refresh_token",
|
|
40
|
+
"secret",
|
|
41
|
+
"sig",
|
|
42
|
+
"signature",
|
|
43
|
+
"token",
|
|
44
|
+
"awsaccesskeyid",
|
|
45
|
+
"x-amz-credential",
|
|
46
|
+
"x-amz-security-token",
|
|
47
|
+
"x-amz-signature",
|
|
48
|
+
"x-goog-credential",
|
|
49
|
+
"x-goog-signature"
|
|
50
|
+
]);
|
|
51
|
+
function redactURL(url) {
|
|
52
|
+
const absolute = /^[a-z][a-z\d+\-.]*:\/\//i.test(url);
|
|
53
|
+
let u;
|
|
54
|
+
try {
|
|
55
|
+
u = absolute ? new URL(url) : new URL(url, "http://exios.invalid");
|
|
56
|
+
} catch {
|
|
57
|
+
const q2 = url.indexOf("?");
|
|
58
|
+
return q2 === -1 ? url : url.slice(0, q2);
|
|
59
|
+
}
|
|
60
|
+
u.username = "";
|
|
61
|
+
u.password = "";
|
|
62
|
+
let changed = false;
|
|
63
|
+
const params = new URLSearchParams(u.search);
|
|
64
|
+
for (const k of new Set(params.keys()))
|
|
65
|
+
if (SENSITIVE_QUERY_PARAMS.has(k.toLowerCase())) {
|
|
66
|
+
params.set(k, REDACTED);
|
|
67
|
+
changed = true;
|
|
68
|
+
}
|
|
69
|
+
if (changed) u.search = params.toString().replaceAll("%5BREDACTED%5D", REDACTED);
|
|
70
|
+
if (absolute) return u.href;
|
|
71
|
+
const q = url.indexOf("?");
|
|
72
|
+
const path = q === -1 ? url : url.slice(0, q);
|
|
73
|
+
return changed ? path + u.search : url;
|
|
74
|
+
}
|
|
30
75
|
|
|
31
76
|
// src/errors/ExiosError.ts
|
|
32
77
|
var ExiosError = class _ExiosError extends Error {
|
|
@@ -1574,6 +1619,8 @@ async function dispatchRequest(config, registry2, observer) {
|
|
|
1574
1619
|
breaker.record(ticket, (bcfg.isFailure ?? defaultIsFailure)(outcome));
|
|
1575
1620
|
if (outcome.error) throw outcome.error;
|
|
1576
1621
|
const response = outcome.response;
|
|
1622
|
+
response.meta ??= { attempts };
|
|
1623
|
+
response.config ??= config;
|
|
1577
1624
|
response.meta.attempts = attempts;
|
|
1578
1625
|
if (responseTimeMs !== void 0) response.meta.responseTimeMs = responseTimeMs;
|
|
1579
1626
|
const { status } = response;
|
|
@@ -1956,6 +2003,74 @@ var InflightRequests = class {
|
|
|
1956
2003
|
}
|
|
1957
2004
|
};
|
|
1958
2005
|
|
|
2006
|
+
// src/core/plugins.ts
|
|
2007
|
+
var HOOKS = ["setup", "beforeRequest", "afterResponse", "onError", "onCancel"];
|
|
2008
|
+
function invalid(message) {
|
|
2009
|
+
throw new ExiosError(`ERR_BAD_OPTION_VALUE: ${message}`, ExiosError.ERR_BAD_OPTION_VALUE);
|
|
2010
|
+
}
|
|
2011
|
+
var PluginRegistry = class {
|
|
2012
|
+
list = [];
|
|
2013
|
+
get size() {
|
|
2014
|
+
return this.list.length;
|
|
2015
|
+
}
|
|
2016
|
+
add(plugin, instance) {
|
|
2017
|
+
if (typeof plugin !== "object" || plugin === null) invalid(`plugin must be an object (got ${String(plugin)})`);
|
|
2018
|
+
if (typeof plugin.name !== "string" || !plugin.name) invalid("plugin.name must be a non-empty string");
|
|
2019
|
+
for (const h of HOOKS)
|
|
2020
|
+
if (plugin[h] !== void 0 && typeof plugin[h] !== "function")
|
|
2021
|
+
invalid(`plugin "${plugin.name}": ${h} must be a function`);
|
|
2022
|
+
if (this.list.some((r) => r.plugin.name === plugin.name))
|
|
2023
|
+
invalid(`a plugin named "${plugin.name}" is already registered`);
|
|
2024
|
+
const entry = { plugin };
|
|
2025
|
+
const teardown = plugin.setup?.(instance);
|
|
2026
|
+
if (typeof teardown === "function") entry.teardown = teardown;
|
|
2027
|
+
this.list = [...this.list, entry];
|
|
2028
|
+
return () => void this.remove(plugin);
|
|
2029
|
+
}
|
|
2030
|
+
remove(nameOrPlugin) {
|
|
2031
|
+
const entry = this.list.find(
|
|
2032
|
+
(r) => typeof nameOrPlugin === "string" ? r.plugin.name === nameOrPlugin : r.plugin === nameOrPlugin
|
|
2033
|
+
);
|
|
2034
|
+
if (!entry) return false;
|
|
2035
|
+
this.list = this.list.filter((r) => r !== entry);
|
|
2036
|
+
entry.teardown?.();
|
|
2037
|
+
return true;
|
|
2038
|
+
}
|
|
2039
|
+
/** Run `final` wrapped in every plugin's hooks. */
|
|
2040
|
+
run(config, final) {
|
|
2041
|
+
const plugins = this.list.map((r) => r.plugin);
|
|
2042
|
+
const layer = async (i, cfg) => {
|
|
2043
|
+
if (i === plugins.length) return final(cfg);
|
|
2044
|
+
const p = plugins[i];
|
|
2045
|
+
const c = p.beforeRequest ? await p.beforeRequest(cfg) ?? cfg : cfg;
|
|
2046
|
+
let response;
|
|
2047
|
+
try {
|
|
2048
|
+
response = await layer(i + 1, c);
|
|
2049
|
+
} catch (err) {
|
|
2050
|
+
if (isCancel(err)) {
|
|
2051
|
+
if (p.onCancel) {
|
|
2052
|
+
try {
|
|
2053
|
+
p.onCancel(err, c);
|
|
2054
|
+
} catch (hookErr) {
|
|
2055
|
+
report(p.name, hookErr);
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
throw err;
|
|
2059
|
+
}
|
|
2060
|
+
if (!p.onError) throw err;
|
|
2061
|
+
const recovered = await p.onError(err, c);
|
|
2062
|
+
if (recovered == null) throw err;
|
|
2063
|
+
return recovered;
|
|
2064
|
+
}
|
|
2065
|
+
return p.afterResponse ? await p.afterResponse(response, c) ?? response : response;
|
|
2066
|
+
};
|
|
2067
|
+
return layer(0, config);
|
|
2068
|
+
}
|
|
2069
|
+
};
|
|
2070
|
+
function report(name, err) {
|
|
2071
|
+
if (typeof console !== "undefined") console.error(`[exios] plugin "${name}" onCancel failed:`, err);
|
|
2072
|
+
}
|
|
2073
|
+
|
|
1959
2074
|
// src/core/requestCache.ts
|
|
1960
2075
|
var SAFE_METHODS = Object.freeze(["get", "head"]);
|
|
1961
2076
|
var MemoryCache = class {
|
|
@@ -2005,7 +2120,21 @@ function hash(str) {
|
|
|
2005
2120
|
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
|
|
2006
2121
|
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
|
|
2007
2122
|
}
|
|
2008
|
-
var
|
|
2123
|
+
var KEY_IGNORED_HEADERS = /* @__PURE__ */ new Set([
|
|
2124
|
+
"cache-control",
|
|
2125
|
+
"pragma",
|
|
2126
|
+
"traceparent",
|
|
2127
|
+
"tracestate",
|
|
2128
|
+
"baggage",
|
|
2129
|
+
"b3",
|
|
2130
|
+
"x-b3-traceid",
|
|
2131
|
+
"x-b3-spanid",
|
|
2132
|
+
"x-b3-parentspanid",
|
|
2133
|
+
"x-b3-sampled",
|
|
2134
|
+
"x-b3-flags",
|
|
2135
|
+
"uber-trace-id",
|
|
2136
|
+
"sentry-trace"
|
|
2137
|
+
]);
|
|
2009
2138
|
function requestTarget(config) {
|
|
2010
2139
|
const url = buildURL(
|
|
2011
2140
|
buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls),
|
|
@@ -2018,7 +2147,7 @@ function defaultRequestKey(config, scope = "cache") {
|
|
|
2018
2147
|
const skip = config.correlationIdHeader?.toLowerCase();
|
|
2019
2148
|
const parts = [];
|
|
2020
2149
|
for (const k of Object.keys(config.headers).sort())
|
|
2021
|
-
if (k !== skip && !
|
|
2150
|
+
if (k !== skip && !KEY_IGNORED_HEADERS.has(k)) parts.push(`${k}:${config.headers[k]}`);
|
|
2022
2151
|
if (config.auth) parts.push(`auth:${config.auth.username}:${config.auth.password}`);
|
|
2023
2152
|
parts.push(`rt:${config.responseType ?? ""}`);
|
|
2024
2153
|
parts.push(
|
|
@@ -2186,6 +2315,9 @@ var Exios = class {
|
|
|
2186
2315
|
events = new EventHub();
|
|
2187
2316
|
memoryCache = new MemoryCache();
|
|
2188
2317
|
inflight = new InflightRequests();
|
|
2318
|
+
plugins = new PluginRegistry();
|
|
2319
|
+
/** The callable instance wrapping this context (given to plugin `setup`). */
|
|
2320
|
+
instance;
|
|
2189
2321
|
/** The instance cache: `defaults.cache.provider`, or the built-in in-memory cache. */
|
|
2190
2322
|
cache;
|
|
2191
2323
|
constructor(defaults) {
|
|
@@ -2260,7 +2392,11 @@ var Exios = class {
|
|
|
2260
2392
|
synchronous = synchronous && h.synchronous;
|
|
2261
2393
|
requestChain.unshift(h);
|
|
2262
2394
|
});
|
|
2263
|
-
const dispatch = (c) =>
|
|
2395
|
+
const dispatch = (c) => {
|
|
2396
|
+
const finalized = finalizeConfig(c);
|
|
2397
|
+
if (this.plugins.size === 0) return this.dispatch(finalized);
|
|
2398
|
+
return this.plugins.run(finalized, (pc) => this.dispatch(finalizeConfig(pc)));
|
|
2399
|
+
};
|
|
2264
2400
|
let promise;
|
|
2265
2401
|
if (synchronous) {
|
|
2266
2402
|
promise = this.runSync(requestChain, resolved, dispatch);
|
|
@@ -2278,6 +2414,14 @@ var Exios = class {
|
|
|
2278
2414
|
});
|
|
2279
2415
|
return promise;
|
|
2280
2416
|
}
|
|
2417
|
+
/** Register a plugin; returns a function that ejects it. Plugins are not inherited by `create()`. */
|
|
2418
|
+
use(plugin) {
|
|
2419
|
+
return this.plugins.add(plugin, this.instance);
|
|
2420
|
+
}
|
|
2421
|
+
/** Remove a plugin by name or by object (its `setup` teardown runs). True if it was registered. */
|
|
2422
|
+
eject(nameOrPlugin) {
|
|
2423
|
+
return this.plugins.remove(nameOrPlugin);
|
|
2424
|
+
}
|
|
2281
2425
|
/** Subscribe to a lifecycle event; returns a function that unsubscribes. */
|
|
2282
2426
|
on(name, listener) {
|
|
2283
2427
|
return this.events.on(name, listener);
|
|
@@ -2581,6 +2725,8 @@ var Exios = class {
|
|
|
2581
2725
|
};
|
|
2582
2726
|
var METHODS = [
|
|
2583
2727
|
"request",
|
|
2728
|
+
"use",
|
|
2729
|
+
"eject",
|
|
2584
2730
|
"on",
|
|
2585
2731
|
"once",
|
|
2586
2732
|
"off",
|
|
@@ -2645,6 +2791,7 @@ function createInstance(defaults) {
|
|
|
2645
2791
|
set: (v) => context.defaults = v,
|
|
2646
2792
|
enumerable: true
|
|
2647
2793
|
});
|
|
2794
|
+
context.instance = instance;
|
|
2648
2795
|
instance.interceptors = context.interceptors;
|
|
2649
2796
|
instance.breaker = context.breaker;
|
|
2650
2797
|
instance.cache = context.cache;
|
|
@@ -2652,6 +2799,109 @@ function createInstance(defaults) {
|
|
|
2652
2799
|
return instance;
|
|
2653
2800
|
}
|
|
2654
2801
|
|
|
2802
|
+
// src/core/internals.ts
|
|
2803
|
+
var INTERNALS = /* @__PURE__ */ Symbol.for("@indra211/exios/internals");
|
|
2804
|
+
var internals = {
|
|
2805
|
+
abortError,
|
|
2806
|
+
buildFullPath,
|
|
2807
|
+
buildURL,
|
|
2808
|
+
createRequestId,
|
|
2809
|
+
finishResponse,
|
|
2810
|
+
prepareRequest,
|
|
2811
|
+
redactURL
|
|
2812
|
+
};
|
|
2813
|
+
|
|
2814
|
+
// src/core/logger.ts
|
|
2815
|
+
var RANK = { debug: 10, info: 20, warn: 30, error: 40, silent: 99 };
|
|
2816
|
+
function createLoggerPlugin(options = {}) {
|
|
2817
|
+
const min = RANK[options.level ?? "info"];
|
|
2818
|
+
if (min === void 0) throw new TypeError(`createLoggerPlugin: unknown level ${String(options.level)}`);
|
|
2819
|
+
const target = options.logger ?? (typeof console !== "undefined" ? console : void 0);
|
|
2820
|
+
const write = (level, message, fields) => {
|
|
2821
|
+
if (RANK[level] < min) return;
|
|
2822
|
+
if (options.log) return options.log(level, message, fields);
|
|
2823
|
+
const fn = target?.[level];
|
|
2824
|
+
if (typeof fn === "function") fn.call(target, message, fields);
|
|
2825
|
+
};
|
|
2826
|
+
const base = (e) => {
|
|
2827
|
+
const url = redactURL(e.url);
|
|
2828
|
+
const fields = {
|
|
2829
|
+
label: `${e.method.toUpperCase()} ${url}`,
|
|
2830
|
+
event: "request",
|
|
2831
|
+
requestId: e.requestId,
|
|
2832
|
+
method: e.method.toUpperCase(),
|
|
2833
|
+
url
|
|
2834
|
+
};
|
|
2835
|
+
if (e.correlationId !== void 0) fields.correlationId = e.correlationId;
|
|
2836
|
+
if (options.metadata && e.metadata) fields.metadata = e.metadata;
|
|
2837
|
+
return fields;
|
|
2838
|
+
};
|
|
2839
|
+
const entry = (level, msg, f) => {
|
|
2840
|
+
const { label, ...fields } = f;
|
|
2841
|
+
write(level, `[exios] ${msg(label)}`, fields);
|
|
2842
|
+
};
|
|
2843
|
+
return {
|
|
2844
|
+
name: options.name ?? "logger",
|
|
2845
|
+
setup(instance) {
|
|
2846
|
+
if (min === RANK.silent) return;
|
|
2847
|
+
const offs = [
|
|
2848
|
+
instance.on("request", (e) => {
|
|
2849
|
+
const f = base(e);
|
|
2850
|
+
if (options.headers) f.headers = redactHeaders(e.config.headers);
|
|
2851
|
+
entry("debug", (l) => `\u2192 ${l}`, f);
|
|
2852
|
+
}),
|
|
2853
|
+
instance.on("response", (e) => {
|
|
2854
|
+
const f = { ...base(e), event: "response", status: e.status, attempts: e.attempts, durationMs: e.durationMs };
|
|
2855
|
+
const retries = e.attempts > 1 ? `, ${e.attempts} attempts` : "";
|
|
2856
|
+
entry(e.status >= 400 ? "warn" : "info", (l) => `\u2190 ${e.status} ${l} (${e.durationMs}ms${retries})`, f);
|
|
2857
|
+
}),
|
|
2858
|
+
instance.on("retry", (e) => {
|
|
2859
|
+
const reason = e.status !== void 0 ? `status ${e.status}` : e.error?.code ?? "error";
|
|
2860
|
+
const f = { ...base(e), event: "retry", attempt: e.attempt, delayMs: e.delayMs };
|
|
2861
|
+
if (e.status !== void 0) f.status = e.status;
|
|
2862
|
+
if (e.error?.code) f.code = e.error.code;
|
|
2863
|
+
entry("warn", (l) => `\u21BB ${l} attempt ${e.attempt} failed (${reason}), retrying in ${e.delayMs}ms`, f);
|
|
2864
|
+
}),
|
|
2865
|
+
instance.on("timeout", (e) => {
|
|
2866
|
+
const f = { ...base(e), event: "timeout", attempt: e.attempt, timeoutMs: e.timeoutMs };
|
|
2867
|
+
if (e.error.code) f.code = e.error.code;
|
|
2868
|
+
entry("warn", (l) => `\u23F1 ${l} ${e.kind === "total" ? "total timeout" : "timeout"} of ${e.timeoutMs}ms`, f);
|
|
2869
|
+
}),
|
|
2870
|
+
instance.on("error", (e) => {
|
|
2871
|
+
const f = { ...base(e), event: "error", attempts: e.attempts, durationMs: e.durationMs };
|
|
2872
|
+
if (e.status !== void 0) f.status = e.status;
|
|
2873
|
+
if (e.code !== void 0) f.code = e.code;
|
|
2874
|
+
const what = e.status !== void 0 ? `${e.status}` : e.code ?? "failed";
|
|
2875
|
+
entry("error", (l) => `\u2715 ${l} ${what} (${e.durationMs}ms)`, f);
|
|
2876
|
+
}),
|
|
2877
|
+
instance.on("canceled", (e) => {
|
|
2878
|
+
entry("debug", (l) => `\u2298 ${l} canceled`, { ...base(e), event: "canceled", attempts: e.attempts, durationMs: e.durationMs });
|
|
2879
|
+
}),
|
|
2880
|
+
...["circuitOpen", "circuitHalfOpen", "circuitClose"].map(
|
|
2881
|
+
(name) => instance.on(name, (e) => {
|
|
2882
|
+
const fields = { event: name, key: e.key, from: e.from, to: e.to };
|
|
2883
|
+
write(name === "circuitOpen" ? "warn" : "info", `[exios] circuit ${e.key} ${e.from} \u2192 ${e.to}`, fields);
|
|
2884
|
+
})
|
|
2885
|
+
)
|
|
2886
|
+
];
|
|
2887
|
+
return () => {
|
|
2888
|
+
for (const off of offs) off();
|
|
2889
|
+
};
|
|
2890
|
+
},
|
|
2891
|
+
// Cache hits and deduplicated followers make no network request, so they emit no events.
|
|
2892
|
+
afterResponse(response, config) {
|
|
2893
|
+
const source = response.meta.cached ? "cached" : response.meta.deduped ? "deduped" : void 0;
|
|
2894
|
+
if (!source || RANK.debug < min) return;
|
|
2895
|
+
const c = response.config ?? config;
|
|
2896
|
+
const url = redactURL(buildFullPath(c.baseURL, c.url, c.allowAbsoluteUrls));
|
|
2897
|
+
const fields = { event: "cache", source, method: c.method.toUpperCase(), url, status: response.status };
|
|
2898
|
+
if (c.requestId !== void 0) fields.requestId = c.requestId;
|
|
2899
|
+
if (c.correlationId !== void 0) fields.correlationId = c.correlationId;
|
|
2900
|
+
write("debug", `[exios] \u26A1 ${fields.method} ${url} ${source} (${response.status})`, fields);
|
|
2901
|
+
}
|
|
2902
|
+
};
|
|
2903
|
+
}
|
|
2904
|
+
|
|
2655
2905
|
// src/index.ts
|
|
2656
2906
|
registerXhrAdapter();
|
|
2657
2907
|
registerFetchAdapter();
|
|
@@ -2664,12 +2914,14 @@ var statics = {
|
|
|
2664
2914
|
isCancel,
|
|
2665
2915
|
mergeConfig,
|
|
2666
2916
|
toFormData,
|
|
2667
|
-
MemoryCache
|
|
2917
|
+
MemoryCache,
|
|
2918
|
+
createLoggerPlugin
|
|
2668
2919
|
};
|
|
2669
2920
|
var exios = Object.assign(
|
|
2670
2921
|
createInstance(createDefaults()),
|
|
2671
2922
|
statics
|
|
2672
2923
|
);
|
|
2924
|
+
Object.defineProperty(exios, INTERNALS, { value: internals });
|
|
2673
2925
|
var create = exios.create;
|
|
2674
2926
|
var index_default = exios;
|
|
2675
2927
|
export {
|
|
@@ -2679,6 +2931,7 @@ export {
|
|
|
2679
2931
|
ExiosError,
|
|
2680
2932
|
MemoryCache,
|
|
2681
2933
|
create,
|
|
2934
|
+
createLoggerPlugin,
|
|
2682
2935
|
index_default as default,
|
|
2683
2936
|
isCancel,
|
|
2684
2937
|
isExiosError,
|
package/dist/index.node.cjs
CHANGED
|
@@ -63,6 +63,51 @@ function redactConfig(config) {
|
|
|
63
63
|
if (c.data !== void 0) c.data = REDACTED;
|
|
64
64
|
return c;
|
|
65
65
|
}
|
|
66
|
+
var SENSITIVE_QUERY_PARAMS = /* @__PURE__ */ new Set([
|
|
67
|
+
"access_token",
|
|
68
|
+
"api_key",
|
|
69
|
+
"apikey",
|
|
70
|
+
"auth",
|
|
71
|
+
"client_secret",
|
|
72
|
+
"id_token",
|
|
73
|
+
"key",
|
|
74
|
+
"password",
|
|
75
|
+
"refresh_token",
|
|
76
|
+
"secret",
|
|
77
|
+
"sig",
|
|
78
|
+
"signature",
|
|
79
|
+
"token",
|
|
80
|
+
"awsaccesskeyid",
|
|
81
|
+
"x-amz-credential",
|
|
82
|
+
"x-amz-security-token",
|
|
83
|
+
"x-amz-signature",
|
|
84
|
+
"x-goog-credential",
|
|
85
|
+
"x-goog-signature"
|
|
86
|
+
]);
|
|
87
|
+
function redactURL(url) {
|
|
88
|
+
const absolute = /^[a-z][a-z\d+\-.]*:\/\//i.test(url);
|
|
89
|
+
let u;
|
|
90
|
+
try {
|
|
91
|
+
u = absolute ? new URL(url) : new URL(url, "http://exios.invalid");
|
|
92
|
+
} catch {
|
|
93
|
+
const q2 = url.indexOf("?");
|
|
94
|
+
return q2 === -1 ? url : url.slice(0, q2);
|
|
95
|
+
}
|
|
96
|
+
u.username = "";
|
|
97
|
+
u.password = "";
|
|
98
|
+
let changed = false;
|
|
99
|
+
const params = new URLSearchParams(u.search);
|
|
100
|
+
for (const k of new Set(params.keys()))
|
|
101
|
+
if (SENSITIVE_QUERY_PARAMS.has(k.toLowerCase())) {
|
|
102
|
+
params.set(k, REDACTED);
|
|
103
|
+
changed = true;
|
|
104
|
+
}
|
|
105
|
+
if (changed) u.search = params.toString().replaceAll("%5BREDACTED%5D", REDACTED);
|
|
106
|
+
if (absolute) return u.href;
|
|
107
|
+
const q = url.indexOf("?");
|
|
108
|
+
const path = q === -1 ? url : url.slice(0, q);
|
|
109
|
+
return changed ? path + u.search : url;
|
|
110
|
+
}
|
|
66
111
|
|
|
67
112
|
// src/errors/ExiosError.ts
|
|
68
113
|
var ExiosError = class _ExiosError extends Error {
|
|
@@ -2158,6 +2203,8 @@ async function dispatchRequest(config, registry2, observer) {
|
|
|
2158
2203
|
breaker.record(ticket, (bcfg.isFailure ?? defaultIsFailure)(outcome));
|
|
2159
2204
|
if (outcome.error) throw outcome.error;
|
|
2160
2205
|
const response = outcome.response;
|
|
2206
|
+
response.meta ??= { attempts };
|
|
2207
|
+
response.config ??= config;
|
|
2161
2208
|
response.meta.attempts = attempts;
|
|
2162
2209
|
if (responseTimeMs !== void 0) response.meta.responseTimeMs = responseTimeMs;
|
|
2163
2210
|
const { status } = response;
|
|
@@ -2540,6 +2587,74 @@ var InflightRequests = class {
|
|
|
2540
2587
|
}
|
|
2541
2588
|
};
|
|
2542
2589
|
|
|
2590
|
+
// src/core/plugins.ts
|
|
2591
|
+
var HOOKS = ["setup", "beforeRequest", "afterResponse", "onError", "onCancel"];
|
|
2592
|
+
function invalid(message) {
|
|
2593
|
+
throw new ExiosError(`ERR_BAD_OPTION_VALUE: ${message}`, ExiosError.ERR_BAD_OPTION_VALUE);
|
|
2594
|
+
}
|
|
2595
|
+
var PluginRegistry = class {
|
|
2596
|
+
list = [];
|
|
2597
|
+
get size() {
|
|
2598
|
+
return this.list.length;
|
|
2599
|
+
}
|
|
2600
|
+
add(plugin, instance) {
|
|
2601
|
+
if (typeof plugin !== "object" || plugin === null) invalid(`plugin must be an object (got ${String(plugin)})`);
|
|
2602
|
+
if (typeof plugin.name !== "string" || !plugin.name) invalid("plugin.name must be a non-empty string");
|
|
2603
|
+
for (const h of HOOKS)
|
|
2604
|
+
if (plugin[h] !== void 0 && typeof plugin[h] !== "function")
|
|
2605
|
+
invalid(`plugin "${plugin.name}": ${h} must be a function`);
|
|
2606
|
+
if (this.list.some((r) => r.plugin.name === plugin.name))
|
|
2607
|
+
invalid(`a plugin named "${plugin.name}" is already registered`);
|
|
2608
|
+
const entry = { plugin };
|
|
2609
|
+
const teardown = plugin.setup?.(instance);
|
|
2610
|
+
if (typeof teardown === "function") entry.teardown = teardown;
|
|
2611
|
+
this.list = [...this.list, entry];
|
|
2612
|
+
return () => void this.remove(plugin);
|
|
2613
|
+
}
|
|
2614
|
+
remove(nameOrPlugin) {
|
|
2615
|
+
const entry = this.list.find(
|
|
2616
|
+
(r) => typeof nameOrPlugin === "string" ? r.plugin.name === nameOrPlugin : r.plugin === nameOrPlugin
|
|
2617
|
+
);
|
|
2618
|
+
if (!entry) return false;
|
|
2619
|
+
this.list = this.list.filter((r) => r !== entry);
|
|
2620
|
+
entry.teardown?.();
|
|
2621
|
+
return true;
|
|
2622
|
+
}
|
|
2623
|
+
/** Run `final` wrapped in every plugin's hooks. */
|
|
2624
|
+
run(config, final) {
|
|
2625
|
+
const plugins = this.list.map((r) => r.plugin);
|
|
2626
|
+
const layer = async (i, cfg) => {
|
|
2627
|
+
if (i === plugins.length) return final(cfg);
|
|
2628
|
+
const p = plugins[i];
|
|
2629
|
+
const c = p.beforeRequest ? await p.beforeRequest(cfg) ?? cfg : cfg;
|
|
2630
|
+
let response;
|
|
2631
|
+
try {
|
|
2632
|
+
response = await layer(i + 1, c);
|
|
2633
|
+
} catch (err) {
|
|
2634
|
+
if (isCancel(err)) {
|
|
2635
|
+
if (p.onCancel) {
|
|
2636
|
+
try {
|
|
2637
|
+
p.onCancel(err, c);
|
|
2638
|
+
} catch (hookErr) {
|
|
2639
|
+
report(p.name, hookErr);
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2642
|
+
throw err;
|
|
2643
|
+
}
|
|
2644
|
+
if (!p.onError) throw err;
|
|
2645
|
+
const recovered = await p.onError(err, c);
|
|
2646
|
+
if (recovered == null) throw err;
|
|
2647
|
+
return recovered;
|
|
2648
|
+
}
|
|
2649
|
+
return p.afterResponse ? await p.afterResponse(response, c) ?? response : response;
|
|
2650
|
+
};
|
|
2651
|
+
return layer(0, config);
|
|
2652
|
+
}
|
|
2653
|
+
};
|
|
2654
|
+
function report(name, err) {
|
|
2655
|
+
if (typeof console !== "undefined") console.error(`[exios] plugin "${name}" onCancel failed:`, err);
|
|
2656
|
+
}
|
|
2657
|
+
|
|
2543
2658
|
// src/core/requestCache.ts
|
|
2544
2659
|
var SAFE_METHODS = Object.freeze(["get", "head"]);
|
|
2545
2660
|
var MemoryCache = class {
|
|
@@ -2589,7 +2704,21 @@ function hash(str) {
|
|
|
2589
2704
|
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
|
|
2590
2705
|
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
|
|
2591
2706
|
}
|
|
2592
|
-
var
|
|
2707
|
+
var KEY_IGNORED_HEADERS = /* @__PURE__ */ new Set([
|
|
2708
|
+
"cache-control",
|
|
2709
|
+
"pragma",
|
|
2710
|
+
"traceparent",
|
|
2711
|
+
"tracestate",
|
|
2712
|
+
"baggage",
|
|
2713
|
+
"b3",
|
|
2714
|
+
"x-b3-traceid",
|
|
2715
|
+
"x-b3-spanid",
|
|
2716
|
+
"x-b3-parentspanid",
|
|
2717
|
+
"x-b3-sampled",
|
|
2718
|
+
"x-b3-flags",
|
|
2719
|
+
"uber-trace-id",
|
|
2720
|
+
"sentry-trace"
|
|
2721
|
+
]);
|
|
2593
2722
|
function requestTarget(config) {
|
|
2594
2723
|
const url = buildURL(
|
|
2595
2724
|
buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls),
|
|
@@ -2602,7 +2731,7 @@ function defaultRequestKey(config, scope = "cache") {
|
|
|
2602
2731
|
const skip = config.correlationIdHeader?.toLowerCase();
|
|
2603
2732
|
const parts = [];
|
|
2604
2733
|
for (const k of Object.keys(config.headers).sort())
|
|
2605
|
-
if (k !== skip && !
|
|
2734
|
+
if (k !== skip && !KEY_IGNORED_HEADERS.has(k)) parts.push(`${k}:${config.headers[k]}`);
|
|
2606
2735
|
if (config.auth) parts.push(`auth:${config.auth.username}:${config.auth.password}`);
|
|
2607
2736
|
parts.push(`rt:${config.responseType ?? ""}`);
|
|
2608
2737
|
parts.push(
|
|
@@ -2770,6 +2899,9 @@ var Exios = class {
|
|
|
2770
2899
|
events = new EventHub();
|
|
2771
2900
|
memoryCache = new MemoryCache();
|
|
2772
2901
|
inflight = new InflightRequests();
|
|
2902
|
+
plugins = new PluginRegistry();
|
|
2903
|
+
/** The callable instance wrapping this context (given to plugin `setup`). */
|
|
2904
|
+
instance;
|
|
2773
2905
|
/** The instance cache: `defaults.cache.provider`, or the built-in in-memory cache. */
|
|
2774
2906
|
cache;
|
|
2775
2907
|
constructor(defaults) {
|
|
@@ -2844,7 +2976,11 @@ var Exios = class {
|
|
|
2844
2976
|
synchronous = synchronous && h.synchronous;
|
|
2845
2977
|
requestChain.unshift(h);
|
|
2846
2978
|
});
|
|
2847
|
-
const dispatch = (c) =>
|
|
2979
|
+
const dispatch = (c) => {
|
|
2980
|
+
const finalized = finalizeConfig(c);
|
|
2981
|
+
if (this.plugins.size === 0) return this.dispatch(finalized);
|
|
2982
|
+
return this.plugins.run(finalized, (pc) => this.dispatch(finalizeConfig(pc)));
|
|
2983
|
+
};
|
|
2848
2984
|
let promise;
|
|
2849
2985
|
if (synchronous) {
|
|
2850
2986
|
promise = this.runSync(requestChain, resolved, dispatch);
|
|
@@ -2862,6 +2998,14 @@ var Exios = class {
|
|
|
2862
2998
|
});
|
|
2863
2999
|
return promise;
|
|
2864
3000
|
}
|
|
3001
|
+
/** Register a plugin; returns a function that ejects it. Plugins are not inherited by `create()`. */
|
|
3002
|
+
use(plugin) {
|
|
3003
|
+
return this.plugins.add(plugin, this.instance);
|
|
3004
|
+
}
|
|
3005
|
+
/** Remove a plugin by name or by object (its `setup` teardown runs). True if it was registered. */
|
|
3006
|
+
eject(nameOrPlugin) {
|
|
3007
|
+
return this.plugins.remove(nameOrPlugin);
|
|
3008
|
+
}
|
|
2865
3009
|
/** Subscribe to a lifecycle event; returns a function that unsubscribes. */
|
|
2866
3010
|
on(name, listener) {
|
|
2867
3011
|
return this.events.on(name, listener);
|
|
@@ -3165,6 +3309,8 @@ var Exios = class {
|
|
|
3165
3309
|
};
|
|
3166
3310
|
var METHODS = [
|
|
3167
3311
|
"request",
|
|
3312
|
+
"use",
|
|
3313
|
+
"eject",
|
|
3168
3314
|
"on",
|
|
3169
3315
|
"once",
|
|
3170
3316
|
"off",
|
|
@@ -3229,6 +3375,7 @@ function createInstance(defaults) {
|
|
|
3229
3375
|
set: (v) => context.defaults = v,
|
|
3230
3376
|
enumerable: true
|
|
3231
3377
|
});
|
|
3378
|
+
context.instance = instance;
|
|
3232
3379
|
instance.interceptors = context.interceptors;
|
|
3233
3380
|
instance.breaker = context.breaker;
|
|
3234
3381
|
instance.cache = context.cache;
|
|
@@ -3236,6 +3383,109 @@ function createInstance(defaults) {
|
|
|
3236
3383
|
return instance;
|
|
3237
3384
|
}
|
|
3238
3385
|
|
|
3386
|
+
// src/core/internals.ts
|
|
3387
|
+
var INTERNALS = /* @__PURE__ */ Symbol.for("@indra211/exios/internals");
|
|
3388
|
+
var internals = {
|
|
3389
|
+
abortError,
|
|
3390
|
+
buildFullPath,
|
|
3391
|
+
buildURL,
|
|
3392
|
+
createRequestId,
|
|
3393
|
+
finishResponse,
|
|
3394
|
+
prepareRequest,
|
|
3395
|
+
redactURL
|
|
3396
|
+
};
|
|
3397
|
+
|
|
3398
|
+
// src/core/logger.ts
|
|
3399
|
+
var RANK = { debug: 10, info: 20, warn: 30, error: 40, silent: 99 };
|
|
3400
|
+
function createLoggerPlugin(options = {}) {
|
|
3401
|
+
const min = RANK[options.level ?? "info"];
|
|
3402
|
+
if (min === void 0) throw new TypeError(`createLoggerPlugin: unknown level ${String(options.level)}`);
|
|
3403
|
+
const target = options.logger ?? (typeof console !== "undefined" ? console : void 0);
|
|
3404
|
+
const write = (level, message, fields) => {
|
|
3405
|
+
if (RANK[level] < min) return;
|
|
3406
|
+
if (options.log) return options.log(level, message, fields);
|
|
3407
|
+
const fn = target?.[level];
|
|
3408
|
+
if (typeof fn === "function") fn.call(target, message, fields);
|
|
3409
|
+
};
|
|
3410
|
+
const base = (e) => {
|
|
3411
|
+
const url = redactURL(e.url);
|
|
3412
|
+
const fields = {
|
|
3413
|
+
label: `${e.method.toUpperCase()} ${url}`,
|
|
3414
|
+
event: "request",
|
|
3415
|
+
requestId: e.requestId,
|
|
3416
|
+
method: e.method.toUpperCase(),
|
|
3417
|
+
url
|
|
3418
|
+
};
|
|
3419
|
+
if (e.correlationId !== void 0) fields.correlationId = e.correlationId;
|
|
3420
|
+
if (options.metadata && e.metadata) fields.metadata = e.metadata;
|
|
3421
|
+
return fields;
|
|
3422
|
+
};
|
|
3423
|
+
const entry = (level, msg, f) => {
|
|
3424
|
+
const { label, ...fields } = f;
|
|
3425
|
+
write(level, `[exios] ${msg(label)}`, fields);
|
|
3426
|
+
};
|
|
3427
|
+
return {
|
|
3428
|
+
name: options.name ?? "logger",
|
|
3429
|
+
setup(instance) {
|
|
3430
|
+
if (min === RANK.silent) return;
|
|
3431
|
+
const offs = [
|
|
3432
|
+
instance.on("request", (e) => {
|
|
3433
|
+
const f = base(e);
|
|
3434
|
+
if (options.headers) f.headers = redactHeaders(e.config.headers);
|
|
3435
|
+
entry("debug", (l) => `\u2192 ${l}`, f);
|
|
3436
|
+
}),
|
|
3437
|
+
instance.on("response", (e) => {
|
|
3438
|
+
const f = { ...base(e), event: "response", status: e.status, attempts: e.attempts, durationMs: e.durationMs };
|
|
3439
|
+
const retries = e.attempts > 1 ? `, ${e.attempts} attempts` : "";
|
|
3440
|
+
entry(e.status >= 400 ? "warn" : "info", (l) => `\u2190 ${e.status} ${l} (${e.durationMs}ms${retries})`, f);
|
|
3441
|
+
}),
|
|
3442
|
+
instance.on("retry", (e) => {
|
|
3443
|
+
const reason = e.status !== void 0 ? `status ${e.status}` : e.error?.code ?? "error";
|
|
3444
|
+
const f = { ...base(e), event: "retry", attempt: e.attempt, delayMs: e.delayMs };
|
|
3445
|
+
if (e.status !== void 0) f.status = e.status;
|
|
3446
|
+
if (e.error?.code) f.code = e.error.code;
|
|
3447
|
+
entry("warn", (l) => `\u21BB ${l} attempt ${e.attempt} failed (${reason}), retrying in ${e.delayMs}ms`, f);
|
|
3448
|
+
}),
|
|
3449
|
+
instance.on("timeout", (e) => {
|
|
3450
|
+
const f = { ...base(e), event: "timeout", attempt: e.attempt, timeoutMs: e.timeoutMs };
|
|
3451
|
+
if (e.error.code) f.code = e.error.code;
|
|
3452
|
+
entry("warn", (l) => `\u23F1 ${l} ${e.kind === "total" ? "total timeout" : "timeout"} of ${e.timeoutMs}ms`, f);
|
|
3453
|
+
}),
|
|
3454
|
+
instance.on("error", (e) => {
|
|
3455
|
+
const f = { ...base(e), event: "error", attempts: e.attempts, durationMs: e.durationMs };
|
|
3456
|
+
if (e.status !== void 0) f.status = e.status;
|
|
3457
|
+
if (e.code !== void 0) f.code = e.code;
|
|
3458
|
+
const what = e.status !== void 0 ? `${e.status}` : e.code ?? "failed";
|
|
3459
|
+
entry("error", (l) => `\u2715 ${l} ${what} (${e.durationMs}ms)`, f);
|
|
3460
|
+
}),
|
|
3461
|
+
instance.on("canceled", (e) => {
|
|
3462
|
+
entry("debug", (l) => `\u2298 ${l} canceled`, { ...base(e), event: "canceled", attempts: e.attempts, durationMs: e.durationMs });
|
|
3463
|
+
}),
|
|
3464
|
+
...["circuitOpen", "circuitHalfOpen", "circuitClose"].map(
|
|
3465
|
+
(name) => instance.on(name, (e) => {
|
|
3466
|
+
const fields = { event: name, key: e.key, from: e.from, to: e.to };
|
|
3467
|
+
write(name === "circuitOpen" ? "warn" : "info", `[exios] circuit ${e.key} ${e.from} \u2192 ${e.to}`, fields);
|
|
3468
|
+
})
|
|
3469
|
+
)
|
|
3470
|
+
];
|
|
3471
|
+
return () => {
|
|
3472
|
+
for (const off of offs) off();
|
|
3473
|
+
};
|
|
3474
|
+
},
|
|
3475
|
+
// Cache hits and deduplicated followers make no network request, so they emit no events.
|
|
3476
|
+
afterResponse(response, config) {
|
|
3477
|
+
const source = response.meta.cached ? "cached" : response.meta.deduped ? "deduped" : void 0;
|
|
3478
|
+
if (!source || RANK.debug < min) return;
|
|
3479
|
+
const c = response.config ?? config;
|
|
3480
|
+
const url = redactURL(buildFullPath(c.baseURL, c.url, c.allowAbsoluteUrls));
|
|
3481
|
+
const fields = { event: "cache", source, method: c.method.toUpperCase(), url, status: response.status };
|
|
3482
|
+
if (c.requestId !== void 0) fields.requestId = c.requestId;
|
|
3483
|
+
if (c.correlationId !== void 0) fields.correlationId = c.correlationId;
|
|
3484
|
+
write("debug", `[exios] \u26A1 ${fields.method} ${url} ${source} (${response.status})`, fields);
|
|
3485
|
+
}
|
|
3486
|
+
};
|
|
3487
|
+
}
|
|
3488
|
+
|
|
3239
3489
|
// src/index.ts
|
|
3240
3490
|
registerXhrAdapter();
|
|
3241
3491
|
registerFetchAdapter();
|
|
@@ -3248,12 +3498,14 @@ var statics = {
|
|
|
3248
3498
|
isCancel,
|
|
3249
3499
|
mergeConfig,
|
|
3250
3500
|
toFormData,
|
|
3251
|
-
MemoryCache
|
|
3501
|
+
MemoryCache,
|
|
3502
|
+
createLoggerPlugin
|
|
3252
3503
|
};
|
|
3253
3504
|
var exios = Object.assign(
|
|
3254
3505
|
createInstance(createDefaults()),
|
|
3255
3506
|
statics
|
|
3256
3507
|
);
|
|
3508
|
+
Object.defineProperty(exios, INTERNALS, { value: internals });
|
|
3257
3509
|
var create = exios.create;
|
|
3258
3510
|
var src_default = exios;
|
|
3259
3511
|
|