@npora/request 1.5.0 → 1.6.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 +8 -2
- package/dist/index.cjs +111 -45
- package/dist/index.d.cts +26 -1
- package/dist/index.d.ts +26 -1
- package/dist/index.js +111 -45
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -158,6 +158,10 @@ const cache = cachePlugin({
|
|
|
158
158
|
})
|
|
159
159
|
```
|
|
160
160
|
|
|
161
|
+
The default memory store keeps up to 1,000 entries using LRU eviction and
|
|
162
|
+
removes expired entries when read. Set `maxEntries` to tune the bound, use `0`
|
|
163
|
+
to disable storage, or use `Infinity` for an explicitly unbounded store.
|
|
164
|
+
|
|
161
165
|
Built-in plugins:
|
|
162
166
|
|
|
163
167
|
- `retryPlugin()`
|
|
@@ -176,7 +180,8 @@ Protect a failing upstream after retries are exhausted:
|
|
|
176
180
|
```ts
|
|
177
181
|
const breaker = circuitBreakerPlugin({
|
|
178
182
|
failureThreshold: 5,
|
|
179
|
-
resetTimeout: 30000
|
|
183
|
+
resetTimeout: 30000,
|
|
184
|
+
maxCircuits: 1000
|
|
180
185
|
})
|
|
181
186
|
|
|
182
187
|
const request = createClient()
|
|
@@ -186,7 +191,8 @@ const request = createClient()
|
|
|
186
191
|
|
|
187
192
|
Circuits are isolated by request origin by default. Open circuits reject with
|
|
188
193
|
the stable `CIRCUIT_OPEN` error code and permit a bounded half-open probe after
|
|
189
|
-
the recovery window.
|
|
194
|
+
the recovery window. Inactive circuit state is retained with LRU eviction;
|
|
195
|
+
active requests are never evicted and can temporarily exceed `maxCircuits`.
|
|
190
196
|
|
|
191
197
|
Inject a structured logger when request correlation and timing are needed:
|
|
192
198
|
|
package/dist/index.cjs
CHANGED
|
@@ -1869,13 +1869,35 @@ function defaultShouldRefresh(error) {
|
|
|
1869
1869
|
|
|
1870
1870
|
// src/plugins/cachePlugin.ts
|
|
1871
1871
|
var MemoryCacheStore = class {
|
|
1872
|
-
constructor() {
|
|
1872
|
+
constructor(options = {}) {
|
|
1873
1873
|
this.entries = /* @__PURE__ */ new Map();
|
|
1874
|
+
this.maxEntries = normalizeMaxEntries(options.maxEntries);
|
|
1874
1875
|
}
|
|
1875
1876
|
get(key) {
|
|
1876
|
-
|
|
1877
|
+
const entry = this.entries.get(key);
|
|
1878
|
+
if (!entry) {
|
|
1879
|
+
return void 0;
|
|
1880
|
+
}
|
|
1881
|
+
if (!Number.isFinite(entry.expiresAt) || Date.now() > entry.expiresAt) {
|
|
1882
|
+
this.entries.delete(key);
|
|
1883
|
+
return void 0;
|
|
1884
|
+
}
|
|
1885
|
+
this.entries.delete(key);
|
|
1886
|
+
this.entries.set(key, entry);
|
|
1887
|
+
return entry;
|
|
1877
1888
|
}
|
|
1878
1889
|
set(key, entry) {
|
|
1890
|
+
if (this.maxEntries === 0) {
|
|
1891
|
+
return;
|
|
1892
|
+
}
|
|
1893
|
+
this.entries.delete(key);
|
|
1894
|
+
while (this.entries.size >= this.maxEntries) {
|
|
1895
|
+
const oldestKey = this.entries.keys().next().value;
|
|
1896
|
+
if (oldestKey === void 0) {
|
|
1897
|
+
break;
|
|
1898
|
+
}
|
|
1899
|
+
this.entries.delete(oldestKey);
|
|
1900
|
+
}
|
|
1879
1901
|
this.entries.set(key, entry);
|
|
1880
1902
|
}
|
|
1881
1903
|
delete(key) {
|
|
@@ -1896,7 +1918,9 @@ var DEFAULT_VARY_HEADERS = [
|
|
|
1896
1918
|
"accept-language"
|
|
1897
1919
|
];
|
|
1898
1920
|
function cachePlugin(options = {}) {
|
|
1899
|
-
const store = options.store ?? new MemoryCacheStore(
|
|
1921
|
+
const store = options.store ?? new MemoryCacheStore({
|
|
1922
|
+
maxEntries: options.maxEntries
|
|
1923
|
+
});
|
|
1900
1924
|
const cacheHits = /* @__PURE__ */ new WeakSet();
|
|
1901
1925
|
const leaders = /* @__PURE__ */ new WeakMap();
|
|
1902
1926
|
const completedRecords = /* @__PURE__ */ new WeakMap();
|
|
@@ -2236,6 +2260,15 @@ function cloneCacheValue(value) {
|
|
|
2236
2260
|
return value;
|
|
2237
2261
|
}
|
|
2238
2262
|
}
|
|
2263
|
+
function normalizeMaxEntries(value) {
|
|
2264
|
+
if (value === void 0) {
|
|
2265
|
+
return 1e3;
|
|
2266
|
+
}
|
|
2267
|
+
if (!Number.isFinite(value)) {
|
|
2268
|
+
return value > 0 ? Number.POSITIVE_INFINITY : 0;
|
|
2269
|
+
}
|
|
2270
|
+
return Math.max(0, Math.floor(value));
|
|
2271
|
+
}
|
|
2239
2272
|
|
|
2240
2273
|
// src/plugins/circuitBreakerPlugin.ts
|
|
2241
2274
|
function circuitBreakerPlugin(options = {}) {
|
|
@@ -2267,7 +2300,11 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2267
2300
|
const key = normalizeKey(
|
|
2268
2301
|
requestOptions?.key ?? normalized.createKey(requestContext.config)
|
|
2269
2302
|
);
|
|
2270
|
-
const record = getCircuit(
|
|
2303
|
+
const record = getCircuit(
|
|
2304
|
+
circuits,
|
|
2305
|
+
key,
|
|
2306
|
+
normalized.maxCircuits
|
|
2307
|
+
);
|
|
2271
2308
|
if (record.state === "open" && Date.now() - record.openedAt >= normalized.resetTimeout) {
|
|
2272
2309
|
transition(record, key, "half-open", normalized);
|
|
2273
2310
|
}
|
|
@@ -2281,10 +2318,12 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2281
2318
|
if (probe) {
|
|
2282
2319
|
record.probes += 1;
|
|
2283
2320
|
}
|
|
2321
|
+
record.activeRequests += 1;
|
|
2284
2322
|
admissions.set(requestContext, {
|
|
2285
2323
|
key,
|
|
2286
2324
|
generation: record.generation,
|
|
2287
|
-
probe
|
|
2325
|
+
probe,
|
|
2326
|
+
record
|
|
2288
2327
|
});
|
|
2289
2328
|
});
|
|
2290
2329
|
context.hooks.onSettled(async (requestContext) => {
|
|
@@ -2293,42 +2332,48 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2293
2332
|
return;
|
|
2294
2333
|
}
|
|
2295
2334
|
admissions.delete(requestContext);
|
|
2335
|
+
const record = admission.record;
|
|
2296
2336
|
if (!active) {
|
|
2337
|
+
record.activeRequests = Math.max(0, record.activeRequests - 1);
|
|
2297
2338
|
return;
|
|
2298
2339
|
}
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2340
|
+
try {
|
|
2341
|
+
if (circuits.get(admission.key) !== record || record.generation !== admission.generation) {
|
|
2342
|
+
return;
|
|
2343
|
+
}
|
|
2344
|
+
if (admission.probe) {
|
|
2345
|
+
record.probes = Math.max(0, record.probes - 1);
|
|
2346
|
+
}
|
|
2347
|
+
if (!requestContext.error && requestContext.response) {
|
|
2348
|
+
record.failures = 0;
|
|
2349
|
+
if (admission.probe && record.state === "half-open") {
|
|
2350
|
+
record.successes += 1;
|
|
2351
|
+
if (record.successes >= normalized.successThreshold) {
|
|
2352
|
+
transition(record, admission.key, "closed", normalized);
|
|
2353
|
+
}
|
|
2312
2354
|
}
|
|
2355
|
+
return;
|
|
2313
2356
|
}
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
}
|
|
2323
|
-
if (admission.probe && record.state === "half-open") {
|
|
2324
|
-
transition(record, admission.key, "open", normalized);
|
|
2325
|
-
return;
|
|
2326
|
-
}
|
|
2327
|
-
if (record.state === "closed") {
|
|
2328
|
-
record.failures += 1;
|
|
2329
|
-
if (record.failures >= normalized.failureThreshold) {
|
|
2357
|
+
const counted = await normalized.shouldCountFailure(
|
|
2358
|
+
requestContext.error,
|
|
2359
|
+
requestContext.config
|
|
2360
|
+
);
|
|
2361
|
+
if (!active || circuits.get(admission.key) !== record || record.generation !== admission.generation || !counted) {
|
|
2362
|
+
return;
|
|
2363
|
+
}
|
|
2364
|
+
if (admission.probe && record.state === "half-open") {
|
|
2330
2365
|
transition(record, admission.key, "open", normalized);
|
|
2366
|
+
return;
|
|
2367
|
+
}
|
|
2368
|
+
if (record.state === "closed") {
|
|
2369
|
+
record.failures += 1;
|
|
2370
|
+
if (record.failures >= normalized.failureThreshold) {
|
|
2371
|
+
transition(record, admission.key, "open", normalized);
|
|
2372
|
+
}
|
|
2331
2373
|
}
|
|
2374
|
+
} finally {
|
|
2375
|
+
record.activeRequests = Math.max(0, record.activeRequests - 1);
|
|
2376
|
+
trimCircuits(circuits, normalized.maxCircuits);
|
|
2332
2377
|
}
|
|
2333
2378
|
});
|
|
2334
2379
|
return () => {
|
|
@@ -2354,6 +2399,7 @@ function normalizeOptions(options) {
|
|
|
2354
2399
|
options.halfOpenMaxRequests,
|
|
2355
2400
|
1
|
|
2356
2401
|
),
|
|
2402
|
+
maxCircuits: normalizePositiveInteger(options.maxCircuits, 1e3),
|
|
2357
2403
|
createKey: options.createKey ?? createDefaultKey,
|
|
2358
2404
|
shouldCountFailure: options.shouldCountFailure ?? defaultShouldCountFailure,
|
|
2359
2405
|
onStateChange: options.onStateChange
|
|
@@ -2393,21 +2439,41 @@ function createDefaultKey(config) {
|
|
|
2393
2439
|
}
|
|
2394
2440
|
return "default";
|
|
2395
2441
|
}
|
|
2396
|
-
function getCircuit(circuits, key) {
|
|
2442
|
+
function getCircuit(circuits, key, maxCircuits) {
|
|
2397
2443
|
let record = circuits.get(key);
|
|
2398
|
-
if (
|
|
2399
|
-
|
|
2400
|
-
state: "closed",
|
|
2401
|
-
failures: 0,
|
|
2402
|
-
successes: 0,
|
|
2403
|
-
openedAt: 0,
|
|
2404
|
-
probes: 0,
|
|
2405
|
-
generation: 0
|
|
2406
|
-
};
|
|
2444
|
+
if (record) {
|
|
2445
|
+
circuits.delete(key);
|
|
2407
2446
|
circuits.set(key, record);
|
|
2408
|
-
|
|
2447
|
+
return record;
|
|
2448
|
+
}
|
|
2449
|
+
trimCircuits(circuits, maxCircuits - 1);
|
|
2450
|
+
record = {
|
|
2451
|
+
state: "closed",
|
|
2452
|
+
failures: 0,
|
|
2453
|
+
successes: 0,
|
|
2454
|
+
openedAt: 0,
|
|
2455
|
+
probes: 0,
|
|
2456
|
+
generation: 0,
|
|
2457
|
+
activeRequests: 0
|
|
2458
|
+
};
|
|
2459
|
+
circuits.set(key, record);
|
|
2409
2460
|
return record;
|
|
2410
2461
|
}
|
|
2462
|
+
function trimCircuits(circuits, maxCircuits) {
|
|
2463
|
+
while (circuits.size > maxCircuits) {
|
|
2464
|
+
let removableKey;
|
|
2465
|
+
for (const [key, record] of circuits) {
|
|
2466
|
+
if (record.activeRequests === 0) {
|
|
2467
|
+
removableKey = key;
|
|
2468
|
+
break;
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
if (removableKey === void 0) {
|
|
2472
|
+
return;
|
|
2473
|
+
}
|
|
2474
|
+
circuits.delete(removableKey);
|
|
2475
|
+
}
|
|
2476
|
+
}
|
|
2411
2477
|
function transition(record, key, state, options) {
|
|
2412
2478
|
if (record.state === state) {
|
|
2413
2479
|
if (state === "open") {
|
package/dist/index.d.cts
CHANGED
|
@@ -637,8 +637,19 @@ interface CacheStore {
|
|
|
637
637
|
delete(key: string): MaybePromise<void>;
|
|
638
638
|
clear(): MaybePromise<void>;
|
|
639
639
|
}
|
|
640
|
+
interface MemoryCacheStoreOptions {
|
|
641
|
+
/**
|
|
642
|
+
* Maximum entries retained by the in-memory LRU store.
|
|
643
|
+
* Use `Infinity` for no practical limit.
|
|
644
|
+
*
|
|
645
|
+
* @default 1000
|
|
646
|
+
*/
|
|
647
|
+
maxEntries?: number;
|
|
648
|
+
}
|
|
640
649
|
declare class MemoryCacheStore implements CacheStore {
|
|
641
650
|
private readonly entries;
|
|
651
|
+
private readonly maxEntries;
|
|
652
|
+
constructor(options?: MemoryCacheStoreOptions);
|
|
642
653
|
get(key: string): CacheEntry | undefined;
|
|
643
654
|
set(key: string, entry: CacheEntry): void;
|
|
644
655
|
delete(key: string): void;
|
|
@@ -663,6 +674,13 @@ interface CachePluginOptions {
|
|
|
663
674
|
* @default an isolated MemoryCacheStore
|
|
664
675
|
*/
|
|
665
676
|
store?: CacheStore;
|
|
677
|
+
/**
|
|
678
|
+
* Maximum entries retained by the default MemoryCacheStore.
|
|
679
|
+
* Ignored when `store` is provided.
|
|
680
|
+
*
|
|
681
|
+
* @default 1000
|
|
682
|
+
*/
|
|
683
|
+
maxEntries?: number;
|
|
666
684
|
/**
|
|
667
685
|
* Share one network operation between concurrent equivalent requests.
|
|
668
686
|
*
|
|
@@ -700,6 +718,13 @@ interface CircuitBreakerPluginOptions {
|
|
|
700
718
|
* @default 1
|
|
701
719
|
*/
|
|
702
720
|
halfOpenMaxRequests?: number;
|
|
721
|
+
/**
|
|
722
|
+
* Maximum inactive isolation states retained by this plugin instance.
|
|
723
|
+
* Active requests are never evicted and may temporarily exceed the limit.
|
|
724
|
+
*
|
|
725
|
+
* @default 1000
|
|
726
|
+
*/
|
|
727
|
+
maxCircuits?: number;
|
|
703
728
|
/**
|
|
704
729
|
* Create the default isolation key for a request.
|
|
705
730
|
*
|
|
@@ -743,4 +768,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
|
743
768
|
|
|
744
769
|
declare function uploadPlugin(): Plugin;
|
|
745
770
|
|
|
746
|
-
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, type CircuitBreakerOptions, type CircuitBreakerPlugin, type CircuitBreakerPluginOptions, type CircuitBreakerStateChange, type CircuitState, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, MockAdapter, type MockAdapterOptions, type MockHandler, type MockReply, type MockReplyHandler, type MockRequestMatcher, type MockResponseOptions, type MockRoute, type MockURLMatcher, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, circuitBreakerPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
|
771
|
+
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, type CircuitBreakerOptions, type CircuitBreakerPlugin, type CircuitBreakerPluginOptions, type CircuitBreakerStateChange, type CircuitState, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, type MemoryCacheStoreOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type MockReply, type MockReplyHandler, type MockRequestMatcher, type MockResponseOptions, type MockRoute, type MockURLMatcher, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, circuitBreakerPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -637,8 +637,19 @@ interface CacheStore {
|
|
|
637
637
|
delete(key: string): MaybePromise<void>;
|
|
638
638
|
clear(): MaybePromise<void>;
|
|
639
639
|
}
|
|
640
|
+
interface MemoryCacheStoreOptions {
|
|
641
|
+
/**
|
|
642
|
+
* Maximum entries retained by the in-memory LRU store.
|
|
643
|
+
* Use `Infinity` for no practical limit.
|
|
644
|
+
*
|
|
645
|
+
* @default 1000
|
|
646
|
+
*/
|
|
647
|
+
maxEntries?: number;
|
|
648
|
+
}
|
|
640
649
|
declare class MemoryCacheStore implements CacheStore {
|
|
641
650
|
private readonly entries;
|
|
651
|
+
private readonly maxEntries;
|
|
652
|
+
constructor(options?: MemoryCacheStoreOptions);
|
|
642
653
|
get(key: string): CacheEntry | undefined;
|
|
643
654
|
set(key: string, entry: CacheEntry): void;
|
|
644
655
|
delete(key: string): void;
|
|
@@ -663,6 +674,13 @@ interface CachePluginOptions {
|
|
|
663
674
|
* @default an isolated MemoryCacheStore
|
|
664
675
|
*/
|
|
665
676
|
store?: CacheStore;
|
|
677
|
+
/**
|
|
678
|
+
* Maximum entries retained by the default MemoryCacheStore.
|
|
679
|
+
* Ignored when `store` is provided.
|
|
680
|
+
*
|
|
681
|
+
* @default 1000
|
|
682
|
+
*/
|
|
683
|
+
maxEntries?: number;
|
|
666
684
|
/**
|
|
667
685
|
* Share one network operation between concurrent equivalent requests.
|
|
668
686
|
*
|
|
@@ -700,6 +718,13 @@ interface CircuitBreakerPluginOptions {
|
|
|
700
718
|
* @default 1
|
|
701
719
|
*/
|
|
702
720
|
halfOpenMaxRequests?: number;
|
|
721
|
+
/**
|
|
722
|
+
* Maximum inactive isolation states retained by this plugin instance.
|
|
723
|
+
* Active requests are never evicted and may temporarily exceed the limit.
|
|
724
|
+
*
|
|
725
|
+
* @default 1000
|
|
726
|
+
*/
|
|
727
|
+
maxCircuits?: number;
|
|
703
728
|
/**
|
|
704
729
|
* Create the default isolation key for a request.
|
|
705
730
|
*
|
|
@@ -743,4 +768,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
|
743
768
|
|
|
744
769
|
declare function uploadPlugin(): Plugin;
|
|
745
770
|
|
|
746
|
-
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, type CircuitBreakerOptions, type CircuitBreakerPlugin, type CircuitBreakerPluginOptions, type CircuitBreakerStateChange, type CircuitState, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, MockAdapter, type MockAdapterOptions, type MockHandler, type MockReply, type MockReplyHandler, type MockRequestMatcher, type MockResponseOptions, type MockRoute, type MockURLMatcher, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, circuitBreakerPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
|
771
|
+
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, type CircuitBreakerOptions, type CircuitBreakerPlugin, type CircuitBreakerPluginOptions, type CircuitBreakerStateChange, type CircuitState, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, type MemoryCacheStoreOptions, MockAdapter, type MockAdapterOptions, type MockHandler, type MockReply, type MockReplyHandler, type MockRequestMatcher, type MockResponseOptions, type MockRoute, type MockURLMatcher, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, circuitBreakerPlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
package/dist/index.js
CHANGED
|
@@ -1829,13 +1829,35 @@ function defaultShouldRefresh(error) {
|
|
|
1829
1829
|
|
|
1830
1830
|
// src/plugins/cachePlugin.ts
|
|
1831
1831
|
var MemoryCacheStore = class {
|
|
1832
|
-
constructor() {
|
|
1832
|
+
constructor(options = {}) {
|
|
1833
1833
|
this.entries = /* @__PURE__ */ new Map();
|
|
1834
|
+
this.maxEntries = normalizeMaxEntries(options.maxEntries);
|
|
1834
1835
|
}
|
|
1835
1836
|
get(key) {
|
|
1836
|
-
|
|
1837
|
+
const entry = this.entries.get(key);
|
|
1838
|
+
if (!entry) {
|
|
1839
|
+
return void 0;
|
|
1840
|
+
}
|
|
1841
|
+
if (!Number.isFinite(entry.expiresAt) || Date.now() > entry.expiresAt) {
|
|
1842
|
+
this.entries.delete(key);
|
|
1843
|
+
return void 0;
|
|
1844
|
+
}
|
|
1845
|
+
this.entries.delete(key);
|
|
1846
|
+
this.entries.set(key, entry);
|
|
1847
|
+
return entry;
|
|
1837
1848
|
}
|
|
1838
1849
|
set(key, entry) {
|
|
1850
|
+
if (this.maxEntries === 0) {
|
|
1851
|
+
return;
|
|
1852
|
+
}
|
|
1853
|
+
this.entries.delete(key);
|
|
1854
|
+
while (this.entries.size >= this.maxEntries) {
|
|
1855
|
+
const oldestKey = this.entries.keys().next().value;
|
|
1856
|
+
if (oldestKey === void 0) {
|
|
1857
|
+
break;
|
|
1858
|
+
}
|
|
1859
|
+
this.entries.delete(oldestKey);
|
|
1860
|
+
}
|
|
1839
1861
|
this.entries.set(key, entry);
|
|
1840
1862
|
}
|
|
1841
1863
|
delete(key) {
|
|
@@ -1856,7 +1878,9 @@ var DEFAULT_VARY_HEADERS = [
|
|
|
1856
1878
|
"accept-language"
|
|
1857
1879
|
];
|
|
1858
1880
|
function cachePlugin(options = {}) {
|
|
1859
|
-
const store = options.store ?? new MemoryCacheStore(
|
|
1881
|
+
const store = options.store ?? new MemoryCacheStore({
|
|
1882
|
+
maxEntries: options.maxEntries
|
|
1883
|
+
});
|
|
1860
1884
|
const cacheHits = /* @__PURE__ */ new WeakSet();
|
|
1861
1885
|
const leaders = /* @__PURE__ */ new WeakMap();
|
|
1862
1886
|
const completedRecords = /* @__PURE__ */ new WeakMap();
|
|
@@ -2196,6 +2220,15 @@ function cloneCacheValue(value) {
|
|
|
2196
2220
|
return value;
|
|
2197
2221
|
}
|
|
2198
2222
|
}
|
|
2223
|
+
function normalizeMaxEntries(value) {
|
|
2224
|
+
if (value === void 0) {
|
|
2225
|
+
return 1e3;
|
|
2226
|
+
}
|
|
2227
|
+
if (!Number.isFinite(value)) {
|
|
2228
|
+
return value > 0 ? Number.POSITIVE_INFINITY : 0;
|
|
2229
|
+
}
|
|
2230
|
+
return Math.max(0, Math.floor(value));
|
|
2231
|
+
}
|
|
2199
2232
|
|
|
2200
2233
|
// src/plugins/circuitBreakerPlugin.ts
|
|
2201
2234
|
function circuitBreakerPlugin(options = {}) {
|
|
@@ -2227,7 +2260,11 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2227
2260
|
const key = normalizeKey(
|
|
2228
2261
|
requestOptions?.key ?? normalized.createKey(requestContext.config)
|
|
2229
2262
|
);
|
|
2230
|
-
const record = getCircuit(
|
|
2263
|
+
const record = getCircuit(
|
|
2264
|
+
circuits,
|
|
2265
|
+
key,
|
|
2266
|
+
normalized.maxCircuits
|
|
2267
|
+
);
|
|
2231
2268
|
if (record.state === "open" && Date.now() - record.openedAt >= normalized.resetTimeout) {
|
|
2232
2269
|
transition(record, key, "half-open", normalized);
|
|
2233
2270
|
}
|
|
@@ -2241,10 +2278,12 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2241
2278
|
if (probe) {
|
|
2242
2279
|
record.probes += 1;
|
|
2243
2280
|
}
|
|
2281
|
+
record.activeRequests += 1;
|
|
2244
2282
|
admissions.set(requestContext, {
|
|
2245
2283
|
key,
|
|
2246
2284
|
generation: record.generation,
|
|
2247
|
-
probe
|
|
2285
|
+
probe,
|
|
2286
|
+
record
|
|
2248
2287
|
});
|
|
2249
2288
|
});
|
|
2250
2289
|
context.hooks.onSettled(async (requestContext) => {
|
|
@@ -2253,42 +2292,48 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2253
2292
|
return;
|
|
2254
2293
|
}
|
|
2255
2294
|
admissions.delete(requestContext);
|
|
2295
|
+
const record = admission.record;
|
|
2256
2296
|
if (!active) {
|
|
2297
|
+
record.activeRequests = Math.max(0, record.activeRequests - 1);
|
|
2257
2298
|
return;
|
|
2258
2299
|
}
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2300
|
+
try {
|
|
2301
|
+
if (circuits.get(admission.key) !== record || record.generation !== admission.generation) {
|
|
2302
|
+
return;
|
|
2303
|
+
}
|
|
2304
|
+
if (admission.probe) {
|
|
2305
|
+
record.probes = Math.max(0, record.probes - 1);
|
|
2306
|
+
}
|
|
2307
|
+
if (!requestContext.error && requestContext.response) {
|
|
2308
|
+
record.failures = 0;
|
|
2309
|
+
if (admission.probe && record.state === "half-open") {
|
|
2310
|
+
record.successes += 1;
|
|
2311
|
+
if (record.successes >= normalized.successThreshold) {
|
|
2312
|
+
transition(record, admission.key, "closed", normalized);
|
|
2313
|
+
}
|
|
2272
2314
|
}
|
|
2315
|
+
return;
|
|
2273
2316
|
}
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
}
|
|
2283
|
-
if (admission.probe && record.state === "half-open") {
|
|
2284
|
-
transition(record, admission.key, "open", normalized);
|
|
2285
|
-
return;
|
|
2286
|
-
}
|
|
2287
|
-
if (record.state === "closed") {
|
|
2288
|
-
record.failures += 1;
|
|
2289
|
-
if (record.failures >= normalized.failureThreshold) {
|
|
2317
|
+
const counted = await normalized.shouldCountFailure(
|
|
2318
|
+
requestContext.error,
|
|
2319
|
+
requestContext.config
|
|
2320
|
+
);
|
|
2321
|
+
if (!active || circuits.get(admission.key) !== record || record.generation !== admission.generation || !counted) {
|
|
2322
|
+
return;
|
|
2323
|
+
}
|
|
2324
|
+
if (admission.probe && record.state === "half-open") {
|
|
2290
2325
|
transition(record, admission.key, "open", normalized);
|
|
2326
|
+
return;
|
|
2327
|
+
}
|
|
2328
|
+
if (record.state === "closed") {
|
|
2329
|
+
record.failures += 1;
|
|
2330
|
+
if (record.failures >= normalized.failureThreshold) {
|
|
2331
|
+
transition(record, admission.key, "open", normalized);
|
|
2332
|
+
}
|
|
2291
2333
|
}
|
|
2334
|
+
} finally {
|
|
2335
|
+
record.activeRequests = Math.max(0, record.activeRequests - 1);
|
|
2336
|
+
trimCircuits(circuits, normalized.maxCircuits);
|
|
2292
2337
|
}
|
|
2293
2338
|
});
|
|
2294
2339
|
return () => {
|
|
@@ -2314,6 +2359,7 @@ function normalizeOptions(options) {
|
|
|
2314
2359
|
options.halfOpenMaxRequests,
|
|
2315
2360
|
1
|
|
2316
2361
|
),
|
|
2362
|
+
maxCircuits: normalizePositiveInteger(options.maxCircuits, 1e3),
|
|
2317
2363
|
createKey: options.createKey ?? createDefaultKey,
|
|
2318
2364
|
shouldCountFailure: options.shouldCountFailure ?? defaultShouldCountFailure,
|
|
2319
2365
|
onStateChange: options.onStateChange
|
|
@@ -2353,21 +2399,41 @@ function createDefaultKey(config) {
|
|
|
2353
2399
|
}
|
|
2354
2400
|
return "default";
|
|
2355
2401
|
}
|
|
2356
|
-
function getCircuit(circuits, key) {
|
|
2402
|
+
function getCircuit(circuits, key, maxCircuits) {
|
|
2357
2403
|
let record = circuits.get(key);
|
|
2358
|
-
if (
|
|
2359
|
-
|
|
2360
|
-
state: "closed",
|
|
2361
|
-
failures: 0,
|
|
2362
|
-
successes: 0,
|
|
2363
|
-
openedAt: 0,
|
|
2364
|
-
probes: 0,
|
|
2365
|
-
generation: 0
|
|
2366
|
-
};
|
|
2404
|
+
if (record) {
|
|
2405
|
+
circuits.delete(key);
|
|
2367
2406
|
circuits.set(key, record);
|
|
2368
|
-
|
|
2407
|
+
return record;
|
|
2408
|
+
}
|
|
2409
|
+
trimCircuits(circuits, maxCircuits - 1);
|
|
2410
|
+
record = {
|
|
2411
|
+
state: "closed",
|
|
2412
|
+
failures: 0,
|
|
2413
|
+
successes: 0,
|
|
2414
|
+
openedAt: 0,
|
|
2415
|
+
probes: 0,
|
|
2416
|
+
generation: 0,
|
|
2417
|
+
activeRequests: 0
|
|
2418
|
+
};
|
|
2419
|
+
circuits.set(key, record);
|
|
2369
2420
|
return record;
|
|
2370
2421
|
}
|
|
2422
|
+
function trimCircuits(circuits, maxCircuits) {
|
|
2423
|
+
while (circuits.size > maxCircuits) {
|
|
2424
|
+
let removableKey;
|
|
2425
|
+
for (const [key, record] of circuits) {
|
|
2426
|
+
if (record.activeRequests === 0) {
|
|
2427
|
+
removableKey = key;
|
|
2428
|
+
break;
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
if (removableKey === void 0) {
|
|
2432
|
+
return;
|
|
2433
|
+
}
|
|
2434
|
+
circuits.delete(removableKey);
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2371
2437
|
function transition(record, key, state, options) {
|
|
2372
2438
|
if (record.state === state) {
|
|
2373
2439
|
if (state === "open") {
|