@vyriy/chaos 0.1.9

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vyriy contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # @vyriy/chaos
2
+
3
+ Random failure helper for local Vyriy development.
4
+
5
+ ## Purpose
6
+
7
+ This package injects development-only failures so callers can exercise error and timeout handling paths before real backend instability happens.
8
+
9
+ ## Install
10
+
11
+ With npm:
12
+
13
+ ```bash
14
+ npm install @vyriy/chaos
15
+ ```
16
+
17
+ With Yarn:
18
+
19
+ ```bash
20
+ yarn add @vyriy/chaos
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```ts
26
+ import { chaos } from '@vyriy/chaos';
27
+
28
+ await chaos({
29
+ enabled: process.env.CHAOS_ENABLED === 'true',
30
+ probability: 0.2,
31
+ strategy: 'random',
32
+ timeoutMs: 1500,
33
+ });
34
+ ```
35
+
36
+ ## API
37
+
38
+ - `chaos(options)` resolves when no fault should be injected.
39
+ - `chaos(options)` throws an `Error` or waits and throws a timeout when the random check matches.
40
+ - `defaultMessage` is the default non-timeout error message.
41
+ - `defaultProbability` is the default failure probability.
42
+ - `defaultTimeoutMs` is the default timeout duration in milliseconds.
package/chaos.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { Chaos } from './types.js';
2
+ export declare const defaultMessage = "Chaos error!";
3
+ export declare const defaultProbability = 0.1;
4
+ export declare const defaultTimeoutMs = 1000;
5
+ export declare const chaos: Chaos;
package/chaos.js ADDED
@@ -0,0 +1,34 @@
1
+ import { toError } from '@vyriy/error';
2
+ import { timeout } from '@vyriy/timeout';
3
+ export const defaultMessage = 'Chaos error!';
4
+ export const defaultProbability = 0.1;
5
+ export const defaultTimeoutMs = 1000;
6
+ const normalizeProbability = (probability) => {
7
+ if (!Number.isFinite(probability)) {
8
+ return defaultProbability;
9
+ }
10
+ if (probability <= 0) {
11
+ return 0;
12
+ }
13
+ if (probability >= 1) {
14
+ return 1;
15
+ }
16
+ return probability;
17
+ };
18
+ const selectStrategy = (strategy, random) => {
19
+ if (strategy !== 'random') {
20
+ return strategy;
21
+ }
22
+ return random() < 0.5 ? 'error' : 'timeout';
23
+ };
24
+ export const chaos = async (options = {}) => {
25
+ const { enabled = false, error = defaultMessage, probability = defaultProbability, random = Math.random, strategy = 'random', timeoutMs = defaultTimeoutMs, } = options;
26
+ if (!enabled || random() >= normalizeProbability(probability)) {
27
+ return;
28
+ }
29
+ if (selectStrategy(strategy, random) === 'timeout') {
30
+ await timeout(timeoutMs);
31
+ return;
32
+ }
33
+ throw toError(error);
34
+ };
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './chaos.js';
2
+ export type * from './types.js';
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './chaos.js';
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@vyriy/chaos",
3
+ "version": "0.1.9",
4
+ "description": "Random failure injection utility for Vyriy projects",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "dependencies": {
8
+ "@vyriy/error": "0.1.9",
9
+ "@vyriy/timeout": "0.1.9"
10
+ },
11
+ "license": "MIT",
12
+ "types": "./index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./index.d.ts",
16
+ "import": "./index.js",
17
+ "default": "./index.js"
18
+ },
19
+ "./chaos": {
20
+ "types": "./chaos.d.ts",
21
+ "import": "./chaos.js",
22
+ "default": "./chaos.js"
23
+ },
24
+ "./chaos.js": {
25
+ "types": "./chaos.d.ts",
26
+ "import": "./chaos.js",
27
+ "default": "./chaos.js"
28
+ },
29
+ "./index": {
30
+ "types": "./index.d.ts",
31
+ "import": "./index.js",
32
+ "default": "./index.js"
33
+ },
34
+ "./index.js": {
35
+ "types": "./index.d.ts",
36
+ "import": "./index.js",
37
+ "default": "./index.js"
38
+ }
39
+ }
40
+ }
package/types.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export type ChaosStrategy = 'error' | 'timeout' | 'random';
2
+ export type ChaosOptions = {
3
+ enabled?: boolean;
4
+ probability?: number;
5
+ strategy?: ChaosStrategy;
6
+ timeoutMs?: number;
7
+ error?: unknown;
8
+ random?: () => number;
9
+ };
10
+ export type Chaos = (options?: ChaosOptions) => Promise<void>;