@chaos-maker/core 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/dist/chaos-maker.cjs +3 -3
- package/dist/chaos-maker.js +1473 -1056
- package/dist/chaos-maker.umd.js +3 -3
- package/dist/types/ChaosMaker.d.ts +8 -0
- package/dist/types/builder.d.ts +33 -6
- package/dist/types/config.d.ts +75 -5
- package/dist/types/events.d.ts +11 -1
- package/dist/types/index.d.ts +4 -3
- package/dist/types/interceptors/domAssailant.d.ts +1 -1
- package/dist/types/interceptors/networkFetch.d.ts +1 -1
- package/dist/types/interceptors/networkXHR.d.ts +1 -1
- package/dist/types/interceptors/websocket.d.ts +30 -0
- package/dist/types/prng.d.ts +15 -0
- package/dist/types/utils.d.ts +14 -2
- package/package.json +5 -4
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { UiConfig } from '../config';
|
|
2
2
|
import { ChaosEventEmitter } from '../events';
|
|
3
|
-
export declare function attachDomAssailant(config: UiConfig, emitter?: ChaosEventEmitter): MutationObserver;
|
|
3
|
+
export declare function attachDomAssailant(config: UiConfig, emitter?: ChaosEventEmitter, random?: () => number): MutationObserver;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { NetworkConfig } from '../config';
|
|
2
2
|
import { ChaosEventEmitter } from '../events';
|
|
3
|
-
export declare function patchFetch(originalFetch: typeof window.fetch, config: NetworkConfig, emitter?: ChaosEventEmitter): (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
3
|
+
export declare function patchFetch(originalFetch: typeof window.fetch, config: NetworkConfig, emitter?: ChaosEventEmitter, random?: () => number, counters?: Map<object, number>): (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { NetworkConfig } from '../config';
|
|
2
2
|
import { ChaosEventEmitter } from '../events';
|
|
3
|
-
export declare function patchXHR(originalXhrSend: (body?: Document | XMLHttpRequestBodyInit) => void, config: NetworkConfig, emitter?: ChaosEventEmitter): (this: XMLHttpRequest, body?: Document | XMLHttpRequestBodyInit) => void;
|
|
3
|
+
export declare function patchXHR(originalXhrSend: (body?: Document | XMLHttpRequestBodyInit) => void, config: NetworkConfig, emitter?: ChaosEventEmitter, random?: () => number, counters?: Map<object, number>): (this: XMLHttpRequest, body?: Document | XMLHttpRequestBodyInit) => void;
|
|
4
4
|
export declare function patchXHROpen(originalXhrOpen: (method: string, url: string | URL) => void): (this: XMLHttpRequest, method: string, url: string | URL) => void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebSocket chaos interceptor.
|
|
3
|
+
*
|
|
4
|
+
* Design decisions (see V2_PHASE3_WEBSOCKET_PLAN.md §4, §9):
|
|
5
|
+
* - Patch `window.WebSocket` with a wrapper constructor. Real socket is returned
|
|
6
|
+
* so `instanceof WebSocket` continues to work. `.send` is overridden on the
|
|
7
|
+
* instance; inbound messages are intercepted via a listener installed *before*
|
|
8
|
+
* user code runs.
|
|
9
|
+
* - Ordering of primitives on a matched message: drop → corrupt → delay.
|
|
10
|
+
* A dropped message short-circuits the remaining primitives.
|
|
11
|
+
* - Counting for drop/delay/corrupt is per-rule, per-message. Counting for
|
|
12
|
+
* close is per-rule, per-connection.
|
|
13
|
+
* - On `stop()`, the interceptor flips a `running` flag and cancels every
|
|
14
|
+
* pending timer. Any already-wrapped socket is disarmed in place so its
|
|
15
|
+
* patched `.send` and inbound listener become no-ops; pending delay timers
|
|
16
|
+
* emit `websocket:drop` with `detail.reason: 'stop-during-delay'`; pending
|
|
17
|
+
* close timers silently cancel (they never fired a close event).
|
|
18
|
+
* - Close chaos clears pending-delay timers for the socket before closing.
|
|
19
|
+
* - Binary corruption runs `truncate` / `empty` natively; `malformed-json` and
|
|
20
|
+
* `wrong-type` emit `applied: false` with `reason: 'incompatible-payload-type'`.
|
|
21
|
+
*/
|
|
22
|
+
import { WebSocketConfig } from '../config';
|
|
23
|
+
import { ChaosEventEmitter } from '../events';
|
|
24
|
+
export interface WebSocketPatchHandle {
|
|
25
|
+
/** Wrapped WebSocket constructor suitable for `window.WebSocket = …`. */
|
|
26
|
+
readonly Wrapped: typeof WebSocket;
|
|
27
|
+
/** Clear all pending delay timers and emit drop events for them. Call on ChaosMaker.stop(). */
|
|
28
|
+
uninstall(): void;
|
|
29
|
+
}
|
|
30
|
+
export declare function patchWebSocket(OriginalWebSocket: typeof WebSocket, config: WebSocketConfig, emitter: ChaosEventEmitter, random: () => number, counters: Map<object, number>): WebSocketPatchHandle;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a random seed using Math.random().
|
|
3
|
+
* Returns a 32-bit integer.
|
|
4
|
+
*/
|
|
5
|
+
export declare function generateSeed(): number;
|
|
6
|
+
/**
|
|
7
|
+
* Create a seedable random number generator.
|
|
8
|
+
* If no seed is provided, one is auto-generated.
|
|
9
|
+
*
|
|
10
|
+
* @returns An object with the `random` function and the `seed` used.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createPrng(seed?: number): {
|
|
13
|
+
random: () => number;
|
|
14
|
+
seed: number;
|
|
15
|
+
};
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
import type { CorruptionStrategy } from './config';
|
|
2
|
-
export declare function shouldApplyChaos(probability: number): boolean;
|
|
1
|
+
import type { CorruptionStrategy, RequestCountingOptions } from './config';
|
|
2
|
+
export declare function shouldApplyChaos(probability: number, random?: () => number): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Increment the per-rule request counter and return the new count.
|
|
5
|
+
* Keyed by the rule object reference so the same counter is shared across
|
|
6
|
+
* fetch and XHR interceptors for a given config entry.
|
|
7
|
+
*/
|
|
8
|
+
export declare function incrementCounter(rule: object, counters: Map<object, number>): number;
|
|
9
|
+
/**
|
|
10
|
+
* Given a rule with optional counting fields and the current (already-incremented)
|
|
11
|
+
* request count for that rule, return whether the chaos should fire this request.
|
|
12
|
+
* Returns `true` when no counting field is set (counting is always opt-in).
|
|
13
|
+
*/
|
|
14
|
+
export declare function checkCountingCondition(rule: RequestCountingOptions, count: number): boolean;
|
|
3
15
|
export declare function matchUrl(url: string, pattern: string): boolean;
|
|
4
16
|
export declare function corruptText(text: string, strategy: CorruptionStrategy): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chaos-maker/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A lightweight, framework-agnostic toolkit for injecting chaos into web applications to test frontend resilience",
|
|
6
6
|
"keywords": [
|
|
@@ -52,12 +52,13 @@
|
|
|
52
52
|
"@vitejs/plugin-basic-ssl": "^1.1.0",
|
|
53
53
|
"jsdom": "^27.0.1",
|
|
54
54
|
"typescript": "^5.4.5",
|
|
55
|
-
"vite": "^
|
|
56
|
-
"vitest": "^
|
|
55
|
+
"vite": "^6.4.2",
|
|
56
|
+
"vitest": "^3.2.4"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"dev": "vite build --watch",
|
|
60
60
|
"build": "vite build && tsc -p tsconfig.build.json",
|
|
61
|
-
"test": "vitest"
|
|
61
|
+
"test": "vitest run",
|
|
62
|
+
"test:watch": "vitest"
|
|
62
63
|
}
|
|
63
64
|
}
|