@chaos-maker/core 0.4.0 → 0.5.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 +91 -9
- package/dist/chaos-config.schema.json +1314 -0
- package/dist/chaos-config.schema.notes.md +32 -0
- package/dist/chaos-maker.cjs +23 -5
- package/dist/chaos-maker.js +3476 -2247
- package/dist/chaos-maker.umd.js +23 -5
- package/dist/sw.js +1 -1
- package/dist/sw.mjs +808 -454
- package/dist/types/ChaosMaker.d.ts +41 -0
- package/dist/types/builder.d.ts +24 -0
- package/dist/types/config.d.ts +82 -14
- package/dist/types/debug.d.ts +58 -0
- package/dist/types/errors.d.ts +14 -2
- package/dist/types/events.d.ts +59 -1
- package/dist/types/groups.d.ts +71 -0
- package/dist/types/index.d.ts +30 -5
- package/dist/types/interceptors/domAssailant.d.ts +2 -1
- package/dist/types/interceptors/eventSource.d.ts +2 -1
- package/dist/types/interceptors/networkFetch.d.ts +2 -1
- package/dist/types/interceptors/networkXHR.d.ts +2 -1
- package/dist/types/interceptors/websocket.d.ts +2 -1
- package/dist/types/presets.d.ts +63 -2
- package/dist/types/sw-bridge-source.d.ts +3 -1
- package/dist/types/sw.d.ts +7 -0
- package/dist/types/utils.d.ts +46 -1
- package/dist/types/validation-deprecation.d.ts +7 -0
- package/dist/types/validation-format.d.ts +10 -0
- package/dist/types/validation-strip.d.ts +11 -0
- package/dist/types/validation-types.d.ts +34 -0
- package/dist/types/validation.d.ts +52 -0
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -49,14 +49,82 @@ chaos.stop(); // restores original fetch/XHR
|
|
|
49
49
|
|
|
50
50
|
### Presets
|
|
51
51
|
|
|
52
|
+
Presets are reusable bundles of rules. Drop them into a config by name with the `presets` field, and the engine merges them at construction time.
|
|
53
|
+
|
|
52
54
|
```ts
|
|
53
|
-
import { ChaosMaker
|
|
55
|
+
import { ChaosMaker } from '@chaos-maker/core';
|
|
54
56
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
+
const chaos = new ChaosMaker({
|
|
58
|
+
presets: ['slow-api'],
|
|
59
|
+
network: {
|
|
60
|
+
failures: [{ urlPattern: '/api/checkout', statusCode: 500, probability: 1 }],
|
|
61
|
+
},
|
|
62
|
+
});
|
|
57
63
|
chaos.start();
|
|
58
64
|
```
|
|
59
65
|
|
|
66
|
+
**Built-in catalog**
|
|
67
|
+
|
|
68
|
+
| camelCase name | Kebab alias | Behavior |
|
|
69
|
+
| ----------------------- | --------------- | ----------------------------------------------------------------- |
|
|
70
|
+
| `slowNetwork` | `slow-api` | 2000ms latency on every request |
|
|
71
|
+
| `flakyConnection` | `flaky-api` | 5% aborts plus 3000ms latency on 10% of requests |
|
|
72
|
+
| `offlineMode` | `offline-mode` | Force CORS failure on every request |
|
|
73
|
+
| `unstableApi` | `high-latency` | 10% failures + 20% 1000ms latency, scoped to `/api/` |
|
|
74
|
+
| `degradedUi` | | 20% disable buttons, 10% hide links |
|
|
75
|
+
| `unreliableWebSocket` | | 10% drops, 500ms inbound delay, 5% inbound truncation |
|
|
76
|
+
| `unreliableEventStream` | | 5% drops, 200ms delay, 2% close after 2000ms |
|
|
77
|
+
|
|
78
|
+
Kebab-case aliases (`slow-api`, `flaky-api`, `offline-mode`, `high-latency`) are registry-only. They resolve via `presets: ['slow-api']` and `new PresetRegistry().get('slow-api')`. They are NOT keys on the legacy `presets` record export — `presets['slow-api']` is `undefined` by design. Use the camelCase key (`presets.slowNetwork`) when reading from the record.
|
|
79
|
+
|
|
80
|
+
**Custom presets**
|
|
81
|
+
|
|
82
|
+
Register your own bundle inline via `customPresets`. Names collide fail-fast against built-ins and against each other.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
new ChaosMaker({
|
|
86
|
+
customPresets: {
|
|
87
|
+
'team-flow': {
|
|
88
|
+
network: {
|
|
89
|
+
failures: [{ urlPattern: '/checkout', statusCode: 503, probability: 1 }],
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
presets: ['team-flow'],
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Custom preset values may carry only rule arrays plus the optional `groups` field — `presets`, `customPresets`, `seed`, and `debug` are rejected at validation. Dependency chains are out of scope.
|
|
98
|
+
|
|
99
|
+
**Builder helper**
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { ChaosConfigBuilder } from '@chaos-maker/core';
|
|
103
|
+
|
|
104
|
+
const config = new ChaosConfigBuilder()
|
|
105
|
+
.usePreset('slow-api')
|
|
106
|
+
.failRequests('/api/checkout', 500, 1)
|
|
107
|
+
.build();
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Validation**
|
|
111
|
+
|
|
112
|
+
Unknown preset names, chain attempts, forbidden subfields, duplicate registrations, and group-name collisions across preset+user all surface as `ChaosConfigError` at construction time, never at runtime.
|
|
113
|
+
|
|
114
|
+
**Mutability**
|
|
115
|
+
|
|
116
|
+
Built-in preset configs are deep-frozen — `presets.slowNetwork.network!.latencies![0].delayMs = 1` throws. Your own custom presets passed via `customPresets` are NOT frozen — keep treating them as your data. The engine takes a deep clone at expansion, so any tweaks you make after construction are not observed.
|
|
117
|
+
|
|
118
|
+
**Legacy spread**
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { presets } from '@chaos-maker/core';
|
|
122
|
+
|
|
123
|
+
new ChaosMaker({ ...presets.slowNetwork, network: { failures: [{ urlPattern: '/api', statusCode: 500, probability: 1 }] } });
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Still supported for migration. Prefer the declarative `presets:` field for new code.
|
|
127
|
+
|
|
60
128
|
### Config Builder
|
|
61
129
|
|
|
62
130
|
```ts
|
|
@@ -191,22 +259,36 @@ Event types: `network:failure`, `network:latency`, `network:abort`, `network:cor
|
|
|
191
259
|
|
|
192
260
|
## Config Validation
|
|
193
261
|
|
|
194
|
-
All configs are validated with Zod in strict mode. Unknown keys are rejected. Invalid values throw `ChaosConfigError` with
|
|
262
|
+
All configs are validated with Zod in strict mode. Unknown keys are rejected by default. Invalid values throw `ChaosConfigError` whose `issues` is a `ValidationIssue[]` with structured `path` / `code` / `ruleType` / `message` / `expected` / `received`.
|
|
195
263
|
|
|
196
264
|
```ts
|
|
197
|
-
import {
|
|
265
|
+
import { validateChaosConfig, ChaosConfigError } from '@chaos-maker/core';
|
|
198
266
|
|
|
199
267
|
try {
|
|
200
|
-
|
|
268
|
+
validateChaosConfig({
|
|
269
|
+
network: { failures: [{ urlPattern: '', statusCode: 999, probability: 2 }] },
|
|
270
|
+
});
|
|
201
271
|
} catch (e) {
|
|
202
272
|
if (e instanceof ChaosConfigError) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
273
|
+
for (const issue of e.issues) {
|
|
274
|
+
console.log(issue.path, issue.code, issue.message);
|
|
275
|
+
}
|
|
276
|
+
// legacy v0.4.x string array still available:
|
|
277
|
+
console.log(e.messages);
|
|
206
278
|
}
|
|
207
279
|
}
|
|
208
280
|
```
|
|
209
281
|
|
|
282
|
+
`validateChaosConfig(input, opts?)` accepts:
|
|
283
|
+
|
|
284
|
+
- `unknownFields: 'reject' | 'warn' | 'ignore'` — strict by default. `'warn'` and `'ignore'` strip unknowns from the returned config; `'warn'` emits exactly one aggregated `console.warn` per call.
|
|
285
|
+
- `customValidators: Partial<Record<RuleType, (rule, ctx) => ValidationIssue[] | void>>` — run extra checks per rule type.
|
|
286
|
+
- `onDeprecation: (issue) => void` — receive `ValidationIssue` events for deprecated fields (rails only in v0.5.0).
|
|
287
|
+
|
|
288
|
+
A JSON Schema artifact ships at `node_modules/@chaos-maker/core/dist/chaos-config.schema.json` for IDE / `"$schema"` autocomplete plus a sidecar `chaos-config.schema.notes.md` listing parity caveats. The artifact is a tooling approximation — runtime canonical validation is always Zod via `validateChaosConfig`.
|
|
289
|
+
|
|
290
|
+
See the [Rule Validation concept page](https://chaos-maker-dev.github.io/chaos-maker/concepts/validation/) for the full pipeline, brand semantics, and migration notes.
|
|
291
|
+
|
|
210
292
|
## Service Worker chaos
|
|
211
293
|
|
|
212
294
|
Chaos applies to SW-originated fetches via the `@chaos-maker/core/sw` subpath. Zod + UI + builder are excluded from this bundle so it stays small enough for production SW deploys.
|