@npora/request 1.5.0 → 1.7.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 +28 -2
- package/dist/index.cjs +384 -48
- package/dist/index.d.cts +90 -2
- package/dist/index.d.ts +90 -2
- package/dist/index.js +383 -48
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -116,6 +116,7 @@ import {
|
|
|
116
116
|
authPlugin,
|
|
117
117
|
cachePlugin,
|
|
118
118
|
circuitBreakerPlugin,
|
|
119
|
+
concurrencyPlugin,
|
|
119
120
|
createClient,
|
|
120
121
|
retryPlugin
|
|
121
122
|
} from '@npora/request'
|
|
@@ -129,6 +130,7 @@ const request = createClient()
|
|
|
129
130
|
maxElapsedTime: 10000
|
|
130
131
|
}))
|
|
131
132
|
.use(circuitBreakerPlugin())
|
|
133
|
+
.use(concurrencyPlugin({ maxConcurrent: 20 }))
|
|
132
134
|
.use(cache)
|
|
133
135
|
.use(authPlugin({
|
|
134
136
|
token: () => accessToken,
|
|
@@ -158,11 +160,16 @@ const cache = cachePlugin({
|
|
|
158
160
|
})
|
|
159
161
|
```
|
|
160
162
|
|
|
163
|
+
The default memory store keeps up to 1,000 entries using LRU eviction and
|
|
164
|
+
removes expired entries when read. Set `maxEntries` to tune the bound, use `0`
|
|
165
|
+
to disable storage, or use `Infinity` for an explicitly unbounded store.
|
|
166
|
+
|
|
161
167
|
Built-in plugins:
|
|
162
168
|
|
|
163
169
|
- `retryPlugin()`
|
|
164
170
|
- `cachePlugin()`
|
|
165
171
|
- `circuitBreakerPlugin()`
|
|
172
|
+
- `concurrencyPlugin()`
|
|
166
173
|
- `authPlugin()`
|
|
167
174
|
- `loggerPlugin()`
|
|
168
175
|
- `uploadPlugin()`
|
|
@@ -176,7 +183,8 @@ Protect a failing upstream after retries are exhausted:
|
|
|
176
183
|
```ts
|
|
177
184
|
const breaker = circuitBreakerPlugin({
|
|
178
185
|
failureThreshold: 5,
|
|
179
|
-
resetTimeout: 30000
|
|
186
|
+
resetTimeout: 30000,
|
|
187
|
+
maxCircuits: 1000
|
|
180
188
|
})
|
|
181
189
|
|
|
182
190
|
const request = createClient()
|
|
@@ -186,7 +194,24 @@ const request = createClient()
|
|
|
186
194
|
|
|
187
195
|
Circuits are isolated by request origin by default. Open circuits reject with
|
|
188
196
|
the stable `CIRCUIT_OPEN` error code and permit a bounded half-open probe after
|
|
189
|
-
the recovery window.
|
|
197
|
+
the recovery window. Inactive circuit state is retained with LRU eviction;
|
|
198
|
+
active requests are never evicted and can temporarily exceed `maxCircuits`.
|
|
199
|
+
|
|
200
|
+
Bound concurrent logical requests and queue short bursts before adapter I/O:
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
const concurrency = concurrencyPlugin({
|
|
204
|
+
maxConcurrent: 20,
|
|
205
|
+
maxQueue: 200,
|
|
206
|
+
queueTimeout: 5000
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
const request = createClient().use(concurrency)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Limits are isolated by resolved request origin. Queued requests are admitted
|
|
213
|
+
in FIFO order and remain abortable through their request signal. Full queues
|
|
214
|
+
and expired queue waits reject with the stable `CONCURRENCY_LIMIT` error code.
|
|
190
215
|
|
|
191
216
|
Inject a structured logger when request correlation and timing are needed:
|
|
192
217
|
|
|
@@ -263,6 +288,7 @@ Stable request error codes:
|
|
|
263
288
|
- `ABORT_ERROR`
|
|
264
289
|
- `PARSER_ERROR`
|
|
265
290
|
- `CIRCUIT_OPEN`
|
|
291
|
+
- `CONCURRENCY_LIMIT`
|
|
266
292
|
|
|
267
293
|
## Documentation
|
|
268
294
|
|
package/dist/index.cjs
CHANGED
|
@@ -30,6 +30,7 @@ __export(index_exports, {
|
|
|
30
30
|
authPlugin: () => authPlugin,
|
|
31
31
|
cachePlugin: () => cachePlugin,
|
|
32
32
|
circuitBreakerPlugin: () => circuitBreakerPlugin,
|
|
33
|
+
concurrencyPlugin: () => concurrencyPlugin,
|
|
33
34
|
createClient: () => createClient,
|
|
34
35
|
downloadPlugin: () => downloadPlugin,
|
|
35
36
|
loggerPlugin: () => loggerPlugin,
|
|
@@ -1869,13 +1870,35 @@ function defaultShouldRefresh(error) {
|
|
|
1869
1870
|
|
|
1870
1871
|
// src/plugins/cachePlugin.ts
|
|
1871
1872
|
var MemoryCacheStore = class {
|
|
1872
|
-
constructor() {
|
|
1873
|
+
constructor(options = {}) {
|
|
1873
1874
|
this.entries = /* @__PURE__ */ new Map();
|
|
1875
|
+
this.maxEntries = normalizeMaxEntries(options.maxEntries);
|
|
1874
1876
|
}
|
|
1875
1877
|
get(key) {
|
|
1876
|
-
|
|
1878
|
+
const entry = this.entries.get(key);
|
|
1879
|
+
if (!entry) {
|
|
1880
|
+
return void 0;
|
|
1881
|
+
}
|
|
1882
|
+
if (!Number.isFinite(entry.expiresAt) || Date.now() > entry.expiresAt) {
|
|
1883
|
+
this.entries.delete(key);
|
|
1884
|
+
return void 0;
|
|
1885
|
+
}
|
|
1886
|
+
this.entries.delete(key);
|
|
1887
|
+
this.entries.set(key, entry);
|
|
1888
|
+
return entry;
|
|
1877
1889
|
}
|
|
1878
1890
|
set(key, entry) {
|
|
1891
|
+
if (this.maxEntries === 0) {
|
|
1892
|
+
return;
|
|
1893
|
+
}
|
|
1894
|
+
this.entries.delete(key);
|
|
1895
|
+
while (this.entries.size >= this.maxEntries) {
|
|
1896
|
+
const oldestKey = this.entries.keys().next().value;
|
|
1897
|
+
if (oldestKey === void 0) {
|
|
1898
|
+
break;
|
|
1899
|
+
}
|
|
1900
|
+
this.entries.delete(oldestKey);
|
|
1901
|
+
}
|
|
1879
1902
|
this.entries.set(key, entry);
|
|
1880
1903
|
}
|
|
1881
1904
|
delete(key) {
|
|
@@ -1896,7 +1919,9 @@ var DEFAULT_VARY_HEADERS = [
|
|
|
1896
1919
|
"accept-language"
|
|
1897
1920
|
];
|
|
1898
1921
|
function cachePlugin(options = {}) {
|
|
1899
|
-
const store = options.store ?? new MemoryCacheStore(
|
|
1922
|
+
const store = options.store ?? new MemoryCacheStore({
|
|
1923
|
+
maxEntries: options.maxEntries
|
|
1924
|
+
});
|
|
1900
1925
|
const cacheHits = /* @__PURE__ */ new WeakSet();
|
|
1901
1926
|
const leaders = /* @__PURE__ */ new WeakMap();
|
|
1902
1927
|
const completedRecords = /* @__PURE__ */ new WeakMap();
|
|
@@ -2236,6 +2261,15 @@ function cloneCacheValue(value) {
|
|
|
2236
2261
|
return value;
|
|
2237
2262
|
}
|
|
2238
2263
|
}
|
|
2264
|
+
function normalizeMaxEntries(value) {
|
|
2265
|
+
if (value === void 0) {
|
|
2266
|
+
return 1e3;
|
|
2267
|
+
}
|
|
2268
|
+
if (!Number.isFinite(value)) {
|
|
2269
|
+
return value > 0 ? Number.POSITIVE_INFINITY : 0;
|
|
2270
|
+
}
|
|
2271
|
+
return Math.max(0, Math.floor(value));
|
|
2272
|
+
}
|
|
2239
2273
|
|
|
2240
2274
|
// src/plugins/circuitBreakerPlugin.ts
|
|
2241
2275
|
function circuitBreakerPlugin(options = {}) {
|
|
@@ -2267,7 +2301,11 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2267
2301
|
const key = normalizeKey(
|
|
2268
2302
|
requestOptions?.key ?? normalized.createKey(requestContext.config)
|
|
2269
2303
|
);
|
|
2270
|
-
const record = getCircuit(
|
|
2304
|
+
const record = getCircuit(
|
|
2305
|
+
circuits,
|
|
2306
|
+
key,
|
|
2307
|
+
normalized.maxCircuits
|
|
2308
|
+
);
|
|
2271
2309
|
if (record.state === "open" && Date.now() - record.openedAt >= normalized.resetTimeout) {
|
|
2272
2310
|
transition(record, key, "half-open", normalized);
|
|
2273
2311
|
}
|
|
@@ -2281,10 +2319,12 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2281
2319
|
if (probe) {
|
|
2282
2320
|
record.probes += 1;
|
|
2283
2321
|
}
|
|
2322
|
+
record.activeRequests += 1;
|
|
2284
2323
|
admissions.set(requestContext, {
|
|
2285
2324
|
key,
|
|
2286
2325
|
generation: record.generation,
|
|
2287
|
-
probe
|
|
2326
|
+
probe,
|
|
2327
|
+
record
|
|
2288
2328
|
});
|
|
2289
2329
|
});
|
|
2290
2330
|
context.hooks.onSettled(async (requestContext) => {
|
|
@@ -2293,42 +2333,48 @@ function circuitBreakerPlugin(options = {}) {
|
|
|
2293
2333
|
return;
|
|
2294
2334
|
}
|
|
2295
2335
|
admissions.delete(requestContext);
|
|
2336
|
+
const record = admission.record;
|
|
2296
2337
|
if (!active) {
|
|
2338
|
+
record.activeRequests = Math.max(0, record.activeRequests - 1);
|
|
2297
2339
|
return;
|
|
2298
2340
|
}
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2341
|
+
try {
|
|
2342
|
+
if (circuits.get(admission.key) !== record || record.generation !== admission.generation) {
|
|
2343
|
+
return;
|
|
2344
|
+
}
|
|
2345
|
+
if (admission.probe) {
|
|
2346
|
+
record.probes = Math.max(0, record.probes - 1);
|
|
2347
|
+
}
|
|
2348
|
+
if (!requestContext.error && requestContext.response) {
|
|
2349
|
+
record.failures = 0;
|
|
2350
|
+
if (admission.probe && record.state === "half-open") {
|
|
2351
|
+
record.successes += 1;
|
|
2352
|
+
if (record.successes >= normalized.successThreshold) {
|
|
2353
|
+
transition(record, admission.key, "closed", normalized);
|
|
2354
|
+
}
|
|
2312
2355
|
}
|
|
2356
|
+
return;
|
|
2313
2357
|
}
|
|
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) {
|
|
2358
|
+
const counted = await normalized.shouldCountFailure(
|
|
2359
|
+
requestContext.error,
|
|
2360
|
+
requestContext.config
|
|
2361
|
+
);
|
|
2362
|
+
if (!active || circuits.get(admission.key) !== record || record.generation !== admission.generation || !counted) {
|
|
2363
|
+
return;
|
|
2364
|
+
}
|
|
2365
|
+
if (admission.probe && record.state === "half-open") {
|
|
2330
2366
|
transition(record, admission.key, "open", normalized);
|
|
2367
|
+
return;
|
|
2331
2368
|
}
|
|
2369
|
+
if (record.state === "closed") {
|
|
2370
|
+
record.failures += 1;
|
|
2371
|
+
if (record.failures >= normalized.failureThreshold) {
|
|
2372
|
+
transition(record, admission.key, "open", normalized);
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
} finally {
|
|
2376
|
+
record.activeRequests = Math.max(0, record.activeRequests - 1);
|
|
2377
|
+
trimCircuits(circuits, normalized.maxCircuits);
|
|
2332
2378
|
}
|
|
2333
2379
|
});
|
|
2334
2380
|
return () => {
|
|
@@ -2354,6 +2400,7 @@ function normalizeOptions(options) {
|
|
|
2354
2400
|
options.halfOpenMaxRequests,
|
|
2355
2401
|
1
|
|
2356
2402
|
),
|
|
2403
|
+
maxCircuits: normalizePositiveInteger(options.maxCircuits, 1e3),
|
|
2357
2404
|
createKey: options.createKey ?? createDefaultKey,
|
|
2358
2405
|
shouldCountFailure: options.shouldCountFailure ?? defaultShouldCountFailure,
|
|
2359
2406
|
onStateChange: options.onStateChange
|
|
@@ -2393,21 +2440,41 @@ function createDefaultKey(config) {
|
|
|
2393
2440
|
}
|
|
2394
2441
|
return "default";
|
|
2395
2442
|
}
|
|
2396
|
-
function getCircuit(circuits, key) {
|
|
2443
|
+
function getCircuit(circuits, key, maxCircuits) {
|
|
2397
2444
|
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
|
-
};
|
|
2445
|
+
if (record) {
|
|
2446
|
+
circuits.delete(key);
|
|
2407
2447
|
circuits.set(key, record);
|
|
2408
|
-
|
|
2448
|
+
return record;
|
|
2449
|
+
}
|
|
2450
|
+
trimCircuits(circuits, maxCircuits - 1);
|
|
2451
|
+
record = {
|
|
2452
|
+
state: "closed",
|
|
2453
|
+
failures: 0,
|
|
2454
|
+
successes: 0,
|
|
2455
|
+
openedAt: 0,
|
|
2456
|
+
probes: 0,
|
|
2457
|
+
generation: 0,
|
|
2458
|
+
activeRequests: 0
|
|
2459
|
+
};
|
|
2460
|
+
circuits.set(key, record);
|
|
2409
2461
|
return record;
|
|
2410
2462
|
}
|
|
2463
|
+
function trimCircuits(circuits, maxCircuits) {
|
|
2464
|
+
while (circuits.size > maxCircuits) {
|
|
2465
|
+
let removableKey;
|
|
2466
|
+
for (const [key, record] of circuits) {
|
|
2467
|
+
if (record.activeRequests === 0) {
|
|
2468
|
+
removableKey = key;
|
|
2469
|
+
break;
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
if (removableKey === void 0) {
|
|
2473
|
+
return;
|
|
2474
|
+
}
|
|
2475
|
+
circuits.delete(removableKey);
|
|
2476
|
+
}
|
|
2477
|
+
}
|
|
2411
2478
|
function transition(record, key, state, options) {
|
|
2412
2479
|
if (record.state === state) {
|
|
2413
2480
|
if (state === "open") {
|
|
@@ -2464,6 +2531,274 @@ function defaultShouldCountFailure(error) {
|
|
|
2464
2531
|
return error.code === "HTTP_ERROR" && (error.status === 429 || error.status !== void 0 && error.status >= 500);
|
|
2465
2532
|
}
|
|
2466
2533
|
|
|
2534
|
+
// src/plugins/concurrencyPlugin.ts
|
|
2535
|
+
function concurrencyPlugin(options = {}) {
|
|
2536
|
+
const normalized = normalizeOptions2(options);
|
|
2537
|
+
const records = /* @__PURE__ */ new Map();
|
|
2538
|
+
return {
|
|
2539
|
+
name: "concurrency",
|
|
2540
|
+
getState(key) {
|
|
2541
|
+
const record = records.get(normalizeKey2(key));
|
|
2542
|
+
return {
|
|
2543
|
+
active: record?.active ?? 0,
|
|
2544
|
+
queued: record?.queue.length ?? 0
|
|
2545
|
+
};
|
|
2546
|
+
},
|
|
2547
|
+
install(context) {
|
|
2548
|
+
const admissions = /* @__PURE__ */ new WeakMap();
|
|
2549
|
+
let active = true;
|
|
2550
|
+
context.hooks.onRequest(async (requestContext) => {
|
|
2551
|
+
const requestOptions = resolveExtensionConfig(
|
|
2552
|
+
requestContext.config,
|
|
2553
|
+
"concurrency"
|
|
2554
|
+
);
|
|
2555
|
+
if (requestOptions?.enabled === false) {
|
|
2556
|
+
return;
|
|
2557
|
+
}
|
|
2558
|
+
const key = normalizeKey2(
|
|
2559
|
+
requestOptions?.key ?? normalized.createKey(requestContext.config)
|
|
2560
|
+
);
|
|
2561
|
+
const record = getRecord(records, key, normalized.maxKeys);
|
|
2562
|
+
if (record.active < normalized.maxConcurrent) {
|
|
2563
|
+
record.active += 1;
|
|
2564
|
+
admissions.set(requestContext, {
|
|
2565
|
+
key,
|
|
2566
|
+
record
|
|
2567
|
+
});
|
|
2568
|
+
return;
|
|
2569
|
+
}
|
|
2570
|
+
if (requestContext.config.signal?.aborted) {
|
|
2571
|
+
throw createAbortError3(
|
|
2572
|
+
requestContext.config.signal.reason,
|
|
2573
|
+
requestContext.config
|
|
2574
|
+
);
|
|
2575
|
+
}
|
|
2576
|
+
if (record.queue.length >= normalized.maxQueue) {
|
|
2577
|
+
throw createLimitError(
|
|
2578
|
+
"Concurrency queue is full",
|
|
2579
|
+
requestContext.config
|
|
2580
|
+
);
|
|
2581
|
+
}
|
|
2582
|
+
const timeout = normalizeDuration2(
|
|
2583
|
+
requestOptions?.queueTimeout,
|
|
2584
|
+
normalized.queueTimeout
|
|
2585
|
+
);
|
|
2586
|
+
await enqueue(record, requestContext, timeout);
|
|
2587
|
+
if (!active) {
|
|
2588
|
+
throw createRemovedError(requestContext.config);
|
|
2589
|
+
}
|
|
2590
|
+
});
|
|
2591
|
+
context.hooks.onSettled((requestContext) => {
|
|
2592
|
+
const admission = admissions.get(requestContext);
|
|
2593
|
+
if (!admission) {
|
|
2594
|
+
return;
|
|
2595
|
+
}
|
|
2596
|
+
admissions.delete(requestContext);
|
|
2597
|
+
if (!active || records.get(admission.key) !== admission.record) {
|
|
2598
|
+
return;
|
|
2599
|
+
}
|
|
2600
|
+
releaseNext(admission.record, (entry) => {
|
|
2601
|
+
admissions.set(entry.context, admission);
|
|
2602
|
+
entry.resolve();
|
|
2603
|
+
});
|
|
2604
|
+
if (admission.record.active === 0 && admission.record.queue.length === 0) {
|
|
2605
|
+
touchRecord(records, admission.key, admission.record);
|
|
2606
|
+
trimRecords(records, normalized.maxKeys);
|
|
2607
|
+
}
|
|
2608
|
+
});
|
|
2609
|
+
return () => {
|
|
2610
|
+
active = false;
|
|
2611
|
+
for (const record of records.values()) {
|
|
2612
|
+
for (const entry of record.queue.splice(0)) {
|
|
2613
|
+
entry.cleanup();
|
|
2614
|
+
entry.reject(createRemovedError(entry.config));
|
|
2615
|
+
}
|
|
2616
|
+
}
|
|
2617
|
+
records.clear();
|
|
2618
|
+
};
|
|
2619
|
+
}
|
|
2620
|
+
};
|
|
2621
|
+
}
|
|
2622
|
+
function enqueue(record, context, timeout) {
|
|
2623
|
+
const { config } = context;
|
|
2624
|
+
const signal = config.signal;
|
|
2625
|
+
if (signal?.aborted) {
|
|
2626
|
+
return Promise.reject(createAbortError3(signal.reason, config));
|
|
2627
|
+
}
|
|
2628
|
+
if (timeout <= 0) {
|
|
2629
|
+
return Promise.reject(
|
|
2630
|
+
createLimitError("Concurrency queue wait timed out", config)
|
|
2631
|
+
);
|
|
2632
|
+
}
|
|
2633
|
+
return new Promise((resolve, reject) => {
|
|
2634
|
+
let timer;
|
|
2635
|
+
let settled = false;
|
|
2636
|
+
const remove = () => {
|
|
2637
|
+
const index = record.queue.indexOf(entry);
|
|
2638
|
+
if (index !== -1) {
|
|
2639
|
+
record.queue.splice(index, 1);
|
|
2640
|
+
}
|
|
2641
|
+
};
|
|
2642
|
+
const cleanup = () => {
|
|
2643
|
+
if (timer !== void 0) {
|
|
2644
|
+
clearTimeout(timer);
|
|
2645
|
+
}
|
|
2646
|
+
signal?.removeEventListener("abort", onAbort);
|
|
2647
|
+
};
|
|
2648
|
+
const rejectOnce = (error) => {
|
|
2649
|
+
if (settled) {
|
|
2650
|
+
return;
|
|
2651
|
+
}
|
|
2652
|
+
settled = true;
|
|
2653
|
+
cleanup();
|
|
2654
|
+
reject(error);
|
|
2655
|
+
};
|
|
2656
|
+
const resolveOnce = () => {
|
|
2657
|
+
if (settled) {
|
|
2658
|
+
return;
|
|
2659
|
+
}
|
|
2660
|
+
settled = true;
|
|
2661
|
+
cleanup();
|
|
2662
|
+
resolve();
|
|
2663
|
+
};
|
|
2664
|
+
const onAbort = () => {
|
|
2665
|
+
remove();
|
|
2666
|
+
rejectOnce(createAbortError3(signal?.reason, config));
|
|
2667
|
+
};
|
|
2668
|
+
const entry = {
|
|
2669
|
+
context,
|
|
2670
|
+
config,
|
|
2671
|
+
resolve: resolveOnce,
|
|
2672
|
+
reject: rejectOnce,
|
|
2673
|
+
cleanup
|
|
2674
|
+
};
|
|
2675
|
+
record.queue.push(entry);
|
|
2676
|
+
signal?.addEventListener("abort", onAbort, {
|
|
2677
|
+
once: true
|
|
2678
|
+
});
|
|
2679
|
+
if (Number.isFinite(timeout)) {
|
|
2680
|
+
timer = setTimeout(() => {
|
|
2681
|
+
remove();
|
|
2682
|
+
rejectOnce(
|
|
2683
|
+
createLimitError("Concurrency queue wait timed out", config)
|
|
2684
|
+
);
|
|
2685
|
+
}, timeout);
|
|
2686
|
+
}
|
|
2687
|
+
});
|
|
2688
|
+
}
|
|
2689
|
+
function releaseNext(record, grant) {
|
|
2690
|
+
const next = record.queue.shift();
|
|
2691
|
+
if (next) {
|
|
2692
|
+
next.cleanup();
|
|
2693
|
+
grant(next);
|
|
2694
|
+
return;
|
|
2695
|
+
}
|
|
2696
|
+
record.active = Math.max(0, record.active - 1);
|
|
2697
|
+
}
|
|
2698
|
+
function getRecord(records, key, maxKeys) {
|
|
2699
|
+
const existing = records.get(key);
|
|
2700
|
+
if (existing) {
|
|
2701
|
+
touchRecord(records, key, existing);
|
|
2702
|
+
return existing;
|
|
2703
|
+
}
|
|
2704
|
+
trimRecords(records, Math.max(0, maxKeys - 1));
|
|
2705
|
+
const record = {
|
|
2706
|
+
active: 0,
|
|
2707
|
+
queue: []
|
|
2708
|
+
};
|
|
2709
|
+
records.set(key, record);
|
|
2710
|
+
return record;
|
|
2711
|
+
}
|
|
2712
|
+
function touchRecord(records, key, record) {
|
|
2713
|
+
records.delete(key);
|
|
2714
|
+
records.set(key, record);
|
|
2715
|
+
}
|
|
2716
|
+
function trimRecords(records, maxKeys) {
|
|
2717
|
+
if (records.size <= maxKeys) {
|
|
2718
|
+
return;
|
|
2719
|
+
}
|
|
2720
|
+
for (const [key, record] of records) {
|
|
2721
|
+
if (records.size <= maxKeys) {
|
|
2722
|
+
return;
|
|
2723
|
+
}
|
|
2724
|
+
if (record.active === 0 && record.queue.length === 0) {
|
|
2725
|
+
records.delete(key);
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
}
|
|
2729
|
+
function normalizeOptions2(options) {
|
|
2730
|
+
return {
|
|
2731
|
+
maxConcurrent: normalizePositiveInteger2(options.maxConcurrent, 10),
|
|
2732
|
+
maxQueue: normalizeNonNegativeInteger(options.maxQueue, 1e3),
|
|
2733
|
+
queueTimeout: normalizeDuration2(options.queueTimeout, 3e4),
|
|
2734
|
+
maxKeys: normalizePositiveInteger2(options.maxKeys, 1e3),
|
|
2735
|
+
createKey: options.createKey ?? createDefaultKey2
|
|
2736
|
+
};
|
|
2737
|
+
}
|
|
2738
|
+
function normalizePositiveInteger2(value, fallback) {
|
|
2739
|
+
if (value === void 0 || !Number.isFinite(value)) {
|
|
2740
|
+
return fallback;
|
|
2741
|
+
}
|
|
2742
|
+
return Math.max(1, Math.floor(value));
|
|
2743
|
+
}
|
|
2744
|
+
function normalizeNonNegativeInteger(value, fallback) {
|
|
2745
|
+
if (value === void 0 || !Number.isFinite(value)) {
|
|
2746
|
+
return fallback;
|
|
2747
|
+
}
|
|
2748
|
+
return Math.max(0, Math.floor(value));
|
|
2749
|
+
}
|
|
2750
|
+
function normalizeDuration2(value, fallback) {
|
|
2751
|
+
if (value === void 0) {
|
|
2752
|
+
return fallback;
|
|
2753
|
+
}
|
|
2754
|
+
if (!Number.isFinite(value)) {
|
|
2755
|
+
return value > 0 ? Number.POSITIVE_INFINITY : 0;
|
|
2756
|
+
}
|
|
2757
|
+
return Math.max(0, value);
|
|
2758
|
+
}
|
|
2759
|
+
function normalizeKey2(key) {
|
|
2760
|
+
const normalized = String(key).trim();
|
|
2761
|
+
return normalized || "default";
|
|
2762
|
+
}
|
|
2763
|
+
function createDefaultKey2(config) {
|
|
2764
|
+
try {
|
|
2765
|
+
return new URL(config.url).origin;
|
|
2766
|
+
} catch {
|
|
2767
|
+
}
|
|
2768
|
+
try {
|
|
2769
|
+
if (config.baseURL) {
|
|
2770
|
+
return new URL(config.url, config.baseURL).origin;
|
|
2771
|
+
}
|
|
2772
|
+
} catch {
|
|
2773
|
+
}
|
|
2774
|
+
return "default";
|
|
2775
|
+
}
|
|
2776
|
+
function createLimitError(message, config) {
|
|
2777
|
+
return new RequestError(message, {
|
|
2778
|
+
code: "CONCURRENCY_LIMIT",
|
|
2779
|
+
config
|
|
2780
|
+
});
|
|
2781
|
+
}
|
|
2782
|
+
function createAbortError3(reason, config) {
|
|
2783
|
+
return new RequestError(
|
|
2784
|
+
"Request aborted while waiting for concurrency permit",
|
|
2785
|
+
{
|
|
2786
|
+
code: "ABORT_ERROR",
|
|
2787
|
+
config,
|
|
2788
|
+
cause: reason
|
|
2789
|
+
}
|
|
2790
|
+
);
|
|
2791
|
+
}
|
|
2792
|
+
function createRemovedError(config) {
|
|
2793
|
+
return new RequestError(
|
|
2794
|
+
"Concurrency plugin removed while request was queued",
|
|
2795
|
+
{
|
|
2796
|
+
code: "ABORT_ERROR",
|
|
2797
|
+
config
|
|
2798
|
+
}
|
|
2799
|
+
);
|
|
2800
|
+
}
|
|
2801
|
+
|
|
2467
2802
|
// src/plugins/xhrTransport.ts
|
|
2468
2803
|
function xhrRequest(config, options = {}) {
|
|
2469
2804
|
let request;
|
|
@@ -2513,7 +2848,7 @@ function xhrRequest(config, options = {}) {
|
|
|
2513
2848
|
};
|
|
2514
2849
|
const onSignalAbort = () => {
|
|
2515
2850
|
abortWith(
|
|
2516
|
-
|
|
2851
|
+
createAbortError4(signal?.reason, config)
|
|
2517
2852
|
);
|
|
2518
2853
|
};
|
|
2519
2854
|
xhr.onload = () => {
|
|
@@ -2532,7 +2867,7 @@ function xhrRequest(config, options = {}) {
|
|
|
2532
2867
|
};
|
|
2533
2868
|
xhr.onabort = () => {
|
|
2534
2869
|
rejectOnce(
|
|
2535
|
-
|
|
2870
|
+
createAbortError4(signal?.reason, config)
|
|
2536
2871
|
);
|
|
2537
2872
|
};
|
|
2538
2873
|
xhr.onprogress = createProgressHandler(
|
|
@@ -2690,7 +3025,7 @@ function parseHeaders(value) {
|
|
|
2690
3025
|
}
|
|
2691
3026
|
return headers;
|
|
2692
3027
|
}
|
|
2693
|
-
function
|
|
3028
|
+
function createAbortError4(reason, config) {
|
|
2694
3029
|
if (reason instanceof RequestError) {
|
|
2695
3030
|
return new RequestError(reason.message, {
|
|
2696
3031
|
code: reason.code,
|
|
@@ -3394,6 +3729,7 @@ function uploadPlugin() {
|
|
|
3394
3729
|
authPlugin,
|
|
3395
3730
|
cachePlugin,
|
|
3396
3731
|
circuitBreakerPlugin,
|
|
3732
|
+
concurrencyPlugin,
|
|
3397
3733
|
createClient,
|
|
3398
3734
|
downloadPlugin,
|
|
3399
3735
|
loggerPlugin,
|