req-race 0.1.0 → 0.2.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/package.json +1 -1
- package/src/index.d.ts +9 -2
- package/src/index.js +34 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "req-race",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Automated stale-fetch race condition, out-of-order response chaos, and double-submit mutation tester for browsers, Playwright, and CI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
package/src/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type ChaosMode = "reverse-order" | "jitter" | "throttle-mutations";
|
|
1
|
+
export type ChaosMode = "reverse-order" | "jitter" | "throttle-mutations" | "timeout";
|
|
2
2
|
|
|
3
3
|
export interface RaceStats {
|
|
4
4
|
totalRequests: number;
|
|
@@ -6,6 +6,7 @@ export interface RaceStats {
|
|
|
6
6
|
abortedCount: number;
|
|
7
7
|
unabortedStaleCount: number;
|
|
8
8
|
duplicateMutations: number;
|
|
9
|
+
timeouts: number;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
export interface RaceLogEntry {
|
|
@@ -24,6 +25,7 @@ export interface RaceChaosOptions {
|
|
|
24
25
|
target?: { fetch: typeof fetch };
|
|
25
26
|
mode?: ChaosMode;
|
|
26
27
|
delayMs?: number;
|
|
28
|
+
timeoutMs?: number;
|
|
27
29
|
minDelayMs?: number;
|
|
28
30
|
maxDelayMs?: number;
|
|
29
31
|
doubleSubmitWindowMs?: number;
|
|
@@ -39,9 +41,14 @@ export interface RaceChaosController {
|
|
|
39
41
|
resetStats: () => void;
|
|
40
42
|
}
|
|
41
43
|
|
|
44
|
+
export interface RaceSafeFetchOptions {
|
|
45
|
+
normalizeKey?: (url: string, method: string) => string;
|
|
46
|
+
dedupeMutations?: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
42
49
|
export function enableFetchRaceChaos(options?: RaceChaosOptions): RaceChaosController;
|
|
43
50
|
export function createRaceSafeFetch(
|
|
44
51
|
fetchImpl?: typeof fetch,
|
|
45
|
-
|
|
52
|
+
optionsOrNormalizeKey?: ((url: string, method: string) => string) | RaceSafeFetchOptions
|
|
46
53
|
): typeof fetch;
|
|
47
54
|
export function formatRaceReport(stats: RaceStats): string;
|
package/src/index.js
CHANGED
|
@@ -48,6 +48,7 @@ export function enableFetchRaceChaos(options = {}) {
|
|
|
48
48
|
target = typeof globalThis !== "undefined" ? globalThis : null,
|
|
49
49
|
mode = "reverse-order",
|
|
50
50
|
delayMs = 1000,
|
|
51
|
+
timeoutMs = 1500,
|
|
51
52
|
minDelayMs = 100,
|
|
52
53
|
maxDelayMs = 900,
|
|
53
54
|
doubleSubmitWindowMs = 500,
|
|
@@ -72,6 +73,7 @@ export function enableFetchRaceChaos(options = {}) {
|
|
|
72
73
|
abortedCount: 0,
|
|
73
74
|
unabortedStaleCount: 0,
|
|
74
75
|
duplicateMutations: 0,
|
|
76
|
+
timeouts: 0,
|
|
75
77
|
};
|
|
76
78
|
|
|
77
79
|
let seq = 0;
|
|
@@ -154,6 +156,13 @@ export function enableFetchRaceChaos(options = {}) {
|
|
|
154
156
|
}
|
|
155
157
|
|
|
156
158
|
try {
|
|
159
|
+
if (mode === "timeout") {
|
|
160
|
+
await sleep(timeoutMs, signal);
|
|
161
|
+
stats.timeouts++;
|
|
162
|
+
onRaceDetected?.({ type: "timeout", method, key, url, timeoutMs });
|
|
163
|
+
throw new DOMException(`Request timed out after ${timeoutMs}ms`, "TimeoutError");
|
|
164
|
+
}
|
|
165
|
+
|
|
157
166
|
const response = await originalFetch(input, init);
|
|
158
167
|
|
|
159
168
|
// In reverse-order mode, if a newer request arrived while this one was in flight,
|
|
@@ -215,6 +224,7 @@ export function enableFetchRaceChaos(options = {}) {
|
|
|
215
224
|
stats.abortedCount = 0;
|
|
216
225
|
stats.unabortedStaleCount = 0;
|
|
217
226
|
stats.duplicateMutations = 0;
|
|
227
|
+
stats.timeouts = 0;
|
|
218
228
|
logs.length = 0;
|
|
219
229
|
recentMutations.clear();
|
|
220
230
|
},
|
|
@@ -223,15 +233,23 @@ export function enableFetchRaceChaos(options = {}) {
|
|
|
223
233
|
|
|
224
234
|
/**
|
|
225
235
|
* Create a race-safe fetch wrapper that automatically aborts previous in-flight
|
|
226
|
-
* GET/HEAD requests for the same normalized key
|
|
236
|
+
* GET/HEAD requests for the same normalized key, and optionally deduplicates
|
|
237
|
+
* concurrent identical mutations (POST/PUT/PATCH/DELETE).
|
|
227
238
|
* @param {typeof fetch} [fetchImpl]
|
|
228
|
-
* @param {(url: string, method: string) => string} [
|
|
239
|
+
* @param {((url: string, method: string) => string) | { normalizeKey?: (url: string, method: string) => string, dedupeMutations?: boolean }} [optionsOrNormalizeKey]
|
|
229
240
|
*/
|
|
230
241
|
export function createRaceSafeFetch(
|
|
231
242
|
fetchImpl = (...args) => globalThis.fetch(...args),
|
|
232
|
-
|
|
243
|
+
optionsOrNormalizeKey = {}
|
|
233
244
|
) {
|
|
245
|
+
const isFn = typeof optionsOrNormalizeKey === "function";
|
|
246
|
+
const normalizeKey = isFn
|
|
247
|
+
? optionsOrNormalizeKey
|
|
248
|
+
: optionsOrNormalizeKey.normalizeKey || defaultNormalizeKey;
|
|
249
|
+
const dedupeMutations = isFn ? false : Boolean(optionsOrNormalizeKey.dedupeMutations);
|
|
250
|
+
|
|
234
251
|
const controllers = new Map();
|
|
252
|
+
const inFlightMutations = new Map();
|
|
235
253
|
|
|
236
254
|
return async function raceSafeFetch(input, init = {}) {
|
|
237
255
|
const url = extractUrl(input);
|
|
@@ -263,6 +281,19 @@ export function createRaceSafeFetch(
|
|
|
263
281
|
}
|
|
264
282
|
}
|
|
265
283
|
|
|
284
|
+
if (dedupeMutations && MUTATION_METHODS.has(method)) {
|
|
285
|
+
const existing = inFlightMutations.get(key);
|
|
286
|
+
if (existing) return existing;
|
|
287
|
+
|
|
288
|
+
const promise = Promise.resolve(fetchImpl(input, init)).finally(() => {
|
|
289
|
+
if (inFlightMutations.get(key) === promise) {
|
|
290
|
+
inFlightMutations.delete(key);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
inFlightMutations.set(key, promise);
|
|
294
|
+
return promise;
|
|
295
|
+
}
|
|
296
|
+
|
|
266
297
|
return fetchImpl(input, init);
|
|
267
298
|
};
|
|
268
299
|
}
|