@zukhruf/chaos 0.0.0-stage → 0.8.225

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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +244 -2
  3. package/dist/channel.d.ts +32 -0
  4. package/dist/channel.d.ts.map +1 -0
  5. package/dist/channel.js +62 -0
  6. package/dist/channel.js.map +1 -0
  7. package/dist/direction.d.ts +24 -0
  8. package/dist/direction.d.ts.map +1 -0
  9. package/dist/direction.js +130 -0
  10. package/dist/direction.js.map +1 -0
  11. package/dist/index.d.ts +9 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +8 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/proxy.d.ts +28 -0
  16. package/dist/proxy.d.ts.map +1 -0
  17. package/dist/proxy.js +125 -0
  18. package/dist/proxy.js.map +1 -0
  19. package/dist/toxic.d.ts +29 -0
  20. package/dist/toxic.d.ts.map +1 -0
  21. package/dist/toxic.js +2 -0
  22. package/dist/toxic.js.map +1 -0
  23. package/dist/toxics/latency.d.ts +19 -0
  24. package/dist/toxics/latency.d.ts.map +1 -0
  25. package/dist/toxics/latency.js +48 -0
  26. package/dist/toxics/latency.js.map +1 -0
  27. package/dist/toxics/reset-peer.d.ts +18 -0
  28. package/dist/toxics/reset-peer.d.ts.map +1 -0
  29. package/dist/toxics/reset-peer.js +21 -0
  30. package/dist/toxics/reset-peer.js.map +1 -0
  31. package/dist/toxics/slicer.d.ts +24 -0
  32. package/dist/toxics/slicer.d.ts.map +1 -0
  33. package/dist/toxics/slicer.js +53 -0
  34. package/dist/toxics/slicer.js.map +1 -0
  35. package/dist/toxics/stall-after.d.ts +20 -0
  36. package/dist/toxics/stall-after.d.ts.map +1 -0
  37. package/dist/toxics/stall-after.js +41 -0
  38. package/dist/toxics/stall-after.js.map +1 -0
  39. package/dist/toxics/timeout.d.ts +19 -0
  40. package/dist/toxics/timeout.d.ts.map +1 -0
  41. package/dist/toxics/timeout.js +42 -0
  42. package/dist/toxics/timeout.js.map +1 -0
  43. package/dist/toxics/vanish.d.ts +15 -0
  44. package/dist/toxics/vanish.d.ts.map +1 -0
  45. package/dist/toxics/vanish.js +23 -0
  46. package/dist/toxics/vanish.js.map +1 -0
  47. package/dist/toxics/wait.d.ts +11 -0
  48. package/dist/toxics/wait.d.ts.map +1 -0
  49. package/dist/toxics/wait.js +22 -0
  50. package/dist/toxics/wait.js.map +1 -0
  51. package/package.json +51 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ezzabuzaid
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 CHANGED
@@ -1,3 +1,245 @@
1
- # Temporary Holding Version
1
+ # @zukhruf/chaos
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > **In build (backlog #6078).** The API on paper was approved on 2026-11-28, and it is built as the paper shows, with `Link.signal` added on 2026-11-28 ([ADR 0004](./docs/adr/0004-a-toxic-is-a-strategy-for-one-stream-of-each-connection-and-add-gives-a-disposable.md)); no consumer has moved yet. Each decision is an accepted ADR, and each detail that the review decided is in [Decided in the review](#decided-in-the-review).
4
+
5
+ Put faults between a client of a test and a real server. A `Proxy` forwards each connection to its upstream, the real server. A `Server` of `@zukhruf/net` serves it on a free port, and the client connects to that port in place of the server's. `proxy.add(toxic)` puts a fault on one stream of each connection, also of each connection that is open: a latency, a partition, a reset, a stall after a count of bytes, a side that vanishes. The model is [Toxiproxy](https://github.com/Shopify/toxiproxy), in the process of the test ([ADR 0001](./docs/adr/0001-a-tool-package-on-net-holds-the-faults-that-a-proxy-injects-between-a-client-and-a-real-server.md)).
6
+
7
+ It is a tool package: only tests and tools import it.
8
+
9
+ The words in these documents have one meaning each. See the glossary in [CONTEXT.md](./CONTEXT.md).
10
+
11
+ ## Use it
12
+
13
+ ```sh
14
+ npm install --save-dev @zukhruf/chaos
15
+ ```
16
+
17
+ ```ts
18
+ import { Proxy, Timeout } from '@zukhruf/chaos';
19
+ import { Server, TcpEndpoint } from '@zukhruf/net';
20
+
21
+ await using proxy = new Proxy(
22
+ { host: database.host, port: database.port },
23
+ { clock: globalThis },
24
+ );
25
+ await using hop = await new Server(proxy.forward, {
26
+ allowHalfOpen: true,
27
+ }).serve(new TcpEndpoint());
28
+ const client = connectTo({ host: hop.address.address, port: hop.address.port });
29
+
30
+ using _up = proxy.add(new Timeout({ stream: 'upstream', timeout: 0 }));
31
+ using _down = proxy.add(new Timeout({ stream: 'downstream', timeout: 0 }));
32
+ // The network is partitioned: no byte passes, and the proxy closes no socket.
33
+ ```
34
+
35
+ Declare the proxy before the serving. `await using` then closes the serving first and disposes the proxy last ([ADR 0003](./docs/adr/0003-a-proxy-is-a-handler-that-a-server-serves-and-it-forwards-each-connection-to-its-upstream.md)).
36
+
37
+ ## Forward to a real server
38
+
39
+ `new Proxy(upstream, { clock })` says where to forward. `proxy.forward` is the handler of a `Server`. For each accepted socket, the proxy connects to the upstream, and forwards each direction through a `pipeline` of `node:stream/promises`.
40
+
41
+ - When the connect to the upstream fails, the proxy destroys the accepted socket, as Toxiproxy closes the client.
42
+ - `allowHalfOpen: true` on the `Server` lets a client's end reach the upstream as an end.
43
+ - `clock` is required: the toxics wait on it (root ADR 0004). An app gives `globalThis`, and a test can give the clock of `@sinonjs/fake-timers`.
44
+ - The disposal of the proxy destroys each socket that it opened to the upstream and waits for its `close`.
45
+
46
+ ## Add a fault and remove it
47
+
48
+ `proxy.add(toxic)` gives a `Disposable`. The toxic acts on its stream of each open connection and of each later one, until its disposal ([ADR 0004](./docs/adr/0004-a-toxic-is-a-strategy-for-one-stream-of-each-connection-and-add-gives-a-disposable.md)).
49
+
50
+ | Call | What happens underneath |
51
+ | -------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
52
+ | `using fault = proxy.add(toxic)` | Each connection gets a stage for the toxic on its `stream`, after the stages of the earlier adds. |
53
+ | `fault[Symbol.dispose]()`, or the end of the scope | The stage stops. What it holds goes on with no more delay. What it dropped stays dropped. Later chunks pass with no change. |
54
+ | two toxics on one stream | They act in the order of their adds. |
55
+ | a toxic on both streams | Two adds, one for each stream, as in Toxiproxy. |
56
+
57
+ `upstream` is the stream from the client to the server. `downstream` is the stream from the server to the client.
58
+
59
+ ## Choose a fault
60
+
61
+ | Fault | Toxic |
62
+ | --------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
63
+ | A slow link | `new Latency({ stream, latency })`: each chunk and the end arrive `latency` ms late. |
64
+ | A partition | `new Timeout({ stream, timeout: 0 })` on both streams: chunks drop, and the proxy closes no socket. |
65
+ | No answer, then a close | `new Timeout({ stream, timeout })`: chunks drop, and the connection closes after `timeout` ms. |
66
+ | A reset | `new ResetPeer({ stream, timeout })`: at the first chunk, after `timeout` ms, a TCP reset. |
67
+ | Small reads | `new Slicer({ stream, averageSize, sizeVariation, delay })` |
68
+ | A stall in the middle of an answer | `new StallAfter({ stream: 'downstream', bytes })`: after `bytes` bytes, it holds all; `reached` settles then. |
69
+ | A machine that is gone | `new Vanish({ stream })` on both streams: no byte, no end and no error reaches the other side. |
70
+ | A server that accepts and never answers | Not a toxic: `new Server(() => {}).serve(new TcpEndpoint())` of `@zukhruf/net`. |
71
+
72
+ `Latency`, `Timeout`, `ResetPeer` and `Slicer` are Toxiproxy's toxics `latency`, `timeout`, `reset_peer` and `slicer`. `StallAfter` and `Vanish` are our own: Toxiproxy's `limit_data` closes the connection, and its README lists no toxic that holds the close of a side ([ADR 0005](./docs/adr/0005-the-toxics-are-latency-timeout-reset-peer-and-slicer-after-toxiproxy-and-stall-after-and-vanish-are-our-own.md)).
73
+
74
+ ## Write a toxic
75
+
76
+ `Toxic` is an interface. A host writes its own toxic with `stream` and `pipe(chunks, link)`: `pipe` takes the chunks of one stream of one connection and gives the chunks that reach the other side.
77
+
78
+ ```ts
79
+ import { once } from 'node:events';
80
+
81
+ import type { Link, Toxic } from '@zukhruf/chaos';
82
+
83
+ /** Drops every chunk after the first. */
84
+ class FirstChunkOnly implements Toxic {
85
+ readonly stream = 'downstream';
86
+ async *pipe(chunks: AsyncIterable<Buffer>, { signal }: Link) {
87
+ for await (const chunk of chunks) {
88
+ yield chunk;
89
+ break;
90
+ }
91
+ if (!signal.aborted) await once(signal, 'abort');
92
+ }
93
+ }
94
+ ```
95
+
96
+ - The end of `chunks` goes to the other side after the last chunk that `pipe` gave. A `pipe` that never returns holds the end.
97
+ - An error of the sending socket is a throw of `chunks`. A `pipe` that does not catch it ends the connection.
98
+ - `link.clock` is the clock of the proxy. `link.reset()` ends both sockets with a TCP reset. `link.close()` destroys both sockets.
99
+ - `link.signal` aborts at the removal of the toxic, and at the disposal of the proxy, as Toxiproxy's `stub.Interrupt`. `chunks` then ends, and `pipe` gives what it holds at once and returns. Each chunk that `pipe` did not take goes on. A `pipe` that ignores the signal holds its stream after the removal ([ADR 0004](./docs/adr/0004-a-toxic-is-a-strategy-for-one-stream-of-each-connection-and-add-gives-a-disposable.md)).
100
+
101
+ ## Errors
102
+
103
+ The package has no error class of its own.
104
+
105
+ | Error | When |
106
+ | ----------------------------- | --------------------------------------------------------------------------- |
107
+ | none: the client sees a close | The connect to the upstream failed. The proxy destroys the accepted socket. |
108
+ | `ECONNRESET` at the client | A `ResetPeer`, or a reset of the upstream that no toxic catches. |
109
+
110
+ ## What the package does not do
111
+
112
+ - **Listen.** A `Server` of `@zukhruf/net` listens and keeps the accepted sockets.
113
+ - **HTTP faults** (a status, a body, a header). The faults are in the bytes. An HTTP fault is a handler of `@zukhruf/net/http`.
114
+ - **Random faults.** No toxic has a toxicity or a jitter now: a fault happens each time, so a test is stable.
115
+ - **A proxy in another process, with an HTTP API.** That is Toxiproxy.
116
+ - **Later, on the map:** `limit_data`, `bandwidth`, `slow_close`, `jitter`, an upstream on a pipe (testing's `EngineProxy`).
117
+
118
+ ## Who uses it
119
+
120
+ | Consumer | How | Record |
121
+ | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------ | -------------- |
122
+ | mutex subjects mariadb, mysql, postgres, sqlserver (`partitionable`) | `Timeout` with `timeout: 0` on both streams, today `proxyTo` | backlog #6078 |
123
+ | testing database tests mariadb, mysql, postgres, sqlserver | `Latency` on `upstream`, today `slowProxy` | backlog #6078 |
124
+ | mutex `sqlserver-store.vanished-holder.test.ts` | `Vanish` on both streams, today `vanishingHop` | mutex ADR 0035 |
125
+ | elt `sql-server-request.test.ts`, `sql-server-source.test.ts` (later, after the release) | `StallAfter` on `downstream`, today `relay` | backlog #6078 |
126
+
127
+ ## The same concepts in other systems
128
+
129
+ | Concept | Other systems |
130
+ | ------------------------ | -------------------------------------------------------------------------------------------------------- |
131
+ | A proxy with toxics | [Toxiproxy](https://github.com/Shopify/toxiproxy#toxics): proxies, toxics, `stream` |
132
+ | Faults in a program | [Polly Simmy](https://www.pollydocs.org/chaos/) chaos strategies of .NET |
133
+ | A network fault in Linux | `tc qdisc ... netem delay` and `loss`, [netem(8)](https://man7.org/linux/man-pages/man8/tc-netem.8.html) |
134
+
135
+ ## Why not a package of npm
136
+
137
+ No npm package does the job (probe of 2026-11-28 on Node.js 26). `toxy` 0.3.16 (archived) and `@fetchkit/chaos-proxy` 3.4.3, the successor of `chaos-proxy`, are HTTP proxies: a raw TCP `PING` through each came back as `HTTP/1.1 400 Bad Request`, so neither carries the protocol of a database driver. Neither has faults for one direction, a slicer or a stall after a count of bytes, and `@fetchkit/chaos-proxy` cannot listen on port 0.
138
+
139
+ ## API
140
+
141
+ ```ts
142
+ import type { Socket } from 'node:net';
143
+
144
+ import type { Clock } from '@zukhruf/async';
145
+ import type { Handler } from '@zukhruf/net';
146
+
147
+ export type Stream = 'upstream' | 'downstream';
148
+
149
+ /** What a toxic gets for one stream of one connection. */
150
+ export interface Link {
151
+ readonly clock: Clock;
152
+ /**
153
+ * Aborts at the removal of the toxic, and at the disposal of the proxy
154
+ * (Toxiproxy's `stub.Interrupt`): `chunks` then ends, `pipe` gives what it
155
+ * holds at once and returns, and each chunk that it did not take goes on.
156
+ */
157
+ readonly signal: AbortSignal;
158
+ /** Ends both sockets with a TCP reset. */
159
+ reset(): void;
160
+ /** Destroys both sockets. */
161
+ close(): void;
162
+ }
163
+
164
+ /** A fault on one stream of each connection of a proxy. */
165
+ export interface Toxic {
166
+ readonly stream: Stream;
167
+ pipe(chunks: AsyncIterable<Buffer>, link: Link): AsyncIterable<Buffer>;
168
+ }
169
+
170
+ export interface Upstream {
171
+ /** A host name or an IP address. */
172
+ readonly host: string;
173
+ readonly port: number;
174
+ }
175
+
176
+ export class Proxy implements AsyncDisposable {
177
+ constructor(upstream: Upstream, options: { readonly clock: Clock });
178
+ /** The handler that a Server of @zukhruf/net serves. */
179
+ readonly forward: Handler;
180
+ /** Puts the toxic on its stream of each open and each later connection. */
181
+ add(toxic: Toxic): Disposable;
182
+ /** Destroys each socket to the upstream, and waits for each close. */
183
+ [Symbol.asyncDispose](): Promise<void>;
184
+ }
185
+
186
+ export class Latency implements Toxic {
187
+ constructor(options: { readonly stream: Stream; readonly latency: number });
188
+ }
189
+
190
+ export class Timeout implements Toxic {
191
+ /** timeout 0: drop until the removal, and never close. */
192
+ constructor(options: { readonly stream: Stream; readonly timeout: number });
193
+ }
194
+
195
+ export class ResetPeer implements Toxic {
196
+ constructor(options: { readonly stream: Stream; readonly timeout: number });
197
+ }
198
+
199
+ export class Slicer implements Toxic {
200
+ constructor(options: {
201
+ readonly stream: Stream;
202
+ readonly averageSize: number;
203
+ readonly sizeVariation: number;
204
+ /** Milliseconds between two pieces. */
205
+ readonly delay: number;
206
+ });
207
+ }
208
+
209
+ export class StallAfter implements Toxic {
210
+ constructor(options: { readonly stream: Stream; readonly bytes: number });
211
+ /** Settles when a connection has passed `bytes` bytes on the stream. */
212
+ readonly reached: Promise<void>;
213
+ }
214
+
215
+ export class Vanish implements Toxic {
216
+ constructor(options: { readonly stream: Stream });
217
+ }
218
+ ```
219
+
220
+ ## Decided in the review
221
+
222
+ Each item is a detail that a caller sees. Ezz approved each proposal on 2026-11-28 ("Approve as written"; item 12, the name `Proxy`: "Proxy"), and each follows the leader that it names. The npm candidates are probed before the build.
223
+
224
+ 1. **The type of the upstream.** Proposal: `{ host, port }`, where `host` can be a host name, as Toxiproxy's `upstream` is `host:port`. A `net.SocketAddress` refuses a host name (`ERR_INVALID_ADDRESS` for `localhost`, Node 26.13.1), and a Docker host can be a name. An upstream on a pipe (a `PipeAddress`) is later, for `EngineProxy`.
225
+ 2. **The clock is an option of `Proxy`, not of each toxic.** Proposal: one required `clock` for the proxy, given to each toxic through `link.clock`, so a test gives it once (root ADR 0004). Toxiproxy's toxics read the time of Go.
226
+ 3. **The name of the handler.** Proposal: `proxy.forward`, a field that holds a function, so `new Server(proxy.forward)` needs no `bind`. Toxiproxy has no such object: its proxy listens itself.
227
+ 4. **`stream` is required, with no default.** Toxiproxy's default is `downstream`. Proposal: required, because the copies fault `upstream` (`slowProxy`), `downstream` (elt) and both (`proxyTo`, `vanishingHop`), and a silent default faults one half of a partition.
228
+ 5. **The options of each toxic are one object, with Toxiproxy's attribute names in camel case.** Proposal: `{ stream, latency }`, `{ stream, timeout }`, `{ stream, averageSize, sizeVariation, delay }`, `{ stream, bytes }`. The record writes `Timeout (0 = partition)`, which could also read as `new Timeout(0)`.
229
+ 6. **Milliseconds for each time.** Toxiproxy's `slicer` takes `delay` in microseconds, and its other toxics take milliseconds. Proposal: milliseconds for each, as `setTimeout` and the `Clock` of `@zukhruf/async`.
230
+ 7. **No `jitter` and no `toxicity` now.** Toxiproxy has both. Proposal: later, when a consumer needs one; each takes a source of random numbers as an input then.
231
+ 8. **What `StallAfter.reached` gives.** elt's `stallAfter` resolves with the bytes that the client had sent. Proposal: `Promise<void>`; a count of bytes is a fact of the connection, not of the toxic. elt's move decides it.
232
+ 9. **The count of `StallAfter` is for each connection, and it acts on later connections too.** elt counts over all connections and lets a later connection pass. Proposal: Toxiproxy's model, one state for each link; the move of elt's two tests proves it is enough, or names the gap.
233
+ 10. **npm candidates.** Probed on 2026-11-28: `toxy` and `@fetchkit/chaos-proxy` are HTTP proxies and cannot carry raw TCP, so the package builds its own proxy.
234
+ 11. **The `link.reset()` side.** Toxiproxy's `reset_peer` closes its stub ([reset_peer.go](https://github.com/Shopify/toxiproxy/blob/main/toxics/reset_peer.go)). Proposal: `resetAndDestroy()` on both sockets, so the client and the server each see `ECONNRESET`.
235
+ 12. **The class name `Proxy` hides the global `Proxy` of JavaScript** in each module that imports it. The record names it `class Proxy(upstream)`, after Toxiproxy's proxy. Proposal: keep the name, because a test file seldom uses the global `Proxy`; the other choice is `TcpProxy`, which names the transport as Go names `TCPConn`.
236
+
237
+ ## Documentation
238
+
239
+ **Decisions**
240
+
241
+ - [ADR 0001](./docs/adr/0001-a-tool-package-on-net-holds-the-faults-that-a-proxy-injects-between-a-client-and-a-real-server.md): a tool package on net holds the faults that a proxy injects between a client and a real server.
242
+ - [ADR 0002](./docs/adr/0002-the-name-is-chaos.md): the name is chaos.
243
+ - [ADR 0003](./docs/adr/0003-a-proxy-is-a-handler-that-a-server-serves-and-it-forwards-each-connection-to-its-upstream.md): a proxy is a handler that a server serves, and it forwards each connection to its upstream.
244
+ - [ADR 0004](./docs/adr/0004-a-toxic-is-a-strategy-for-one-stream-of-each-connection-and-add-gives-a-disposable.md): a toxic is a strategy for one stream of each connection, and add gives a disposable.
245
+ - [ADR 0005](./docs/adr/0005-the-toxics-are-latency-timeout-reset-peer-and-slicer-after-toxiproxy-and-stall-after-and-vanish-are-our-own.md): the toxics are Latency, Timeout, ResetPeer and Slicer after Toxiproxy, and StallAfter and Vanish are our own.
@@ -0,0 +1,32 @@
1
+ /** What a channel carries: a chunk, or how its stream ended. */
2
+ export type Item = {
3
+ readonly kind: 'chunk';
4
+ readonly chunk: Buffer;
5
+ /** Resolves the send of the chunk. */
6
+ readonly taken: () => void;
7
+ } | {
8
+ readonly kind: 'end';
9
+ } | {
10
+ readonly kind: 'error';
11
+ readonly error: unknown;
12
+ };
13
+ /**
14
+ * A queue between two stages of one stream. A send resolves when a reader
15
+ * takes its chunk, so a stage that stops taking stops the stage before it,
16
+ * and at last the read of the socket (Go's unbuffered channel, as Toxiproxy's
17
+ * stubs). The end and the error stay in the queue when a reader sees them, so
18
+ * a stage that its removal stops before it gave them on leaves them for the
19
+ * stage after it.
20
+ */
21
+ export declare class Channel {
22
+ #private;
23
+ send(chunk: Buffer): Promise<void>;
24
+ end(): void;
25
+ fail(error: unknown): void;
26
+ /** The next item, or `undefined` when `signal` aborts first. */
27
+ take(): Promise<Item>;
28
+ take(signal: AbortSignal): Promise<Item | undefined>;
29
+ /** Moves each item that waits here to `other`, in order. */
30
+ moveTo(other: Channel): void;
31
+ }
32
+ //# sourceMappingURL=channel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,MAAM,MAAM,IAAI,GACZ;IACE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,sCAAsC;IACtC,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC;CAC5B,GACD;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAA;CAAE,GACxB;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC;AAIxD;;;;;;;GAOG;AACH,qBAAa,OAAO;;IAIlB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAIjC;IAED,GAAG,IAAI,IAAI,CAEV;IAED,IAAI,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAEzB;IAED,gEAAgE;IAChE,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACtB,IAAI,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,CAAC;IAmBrD,4DAA4D;IAC5D,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAE3B;CAgBF"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * A queue between two stages of one stream. A send resolves when a reader
3
+ * takes its chunk, so a stage that stops taking stops the stage before it,
4
+ * and at last the read of the socket (Go's unbuffered channel, as Toxiproxy's
5
+ * stubs). The end and the error stay in the queue when a reader sees them, so
6
+ * a stage that its removal stops before it gave them on leaves them for the
7
+ * stage after it.
8
+ */
9
+ export class Channel {
10
+ #items = [];
11
+ #readers = [];
12
+ send(chunk) {
13
+ const { promise, resolve } = Promise.withResolvers();
14
+ this.#put({ kind: 'chunk', chunk, taken: resolve });
15
+ return promise;
16
+ }
17
+ end() {
18
+ this.#put({ kind: 'end' });
19
+ }
20
+ fail(error) {
21
+ this.#put({ kind: 'error', error });
22
+ }
23
+ take(signal) {
24
+ if (signal?.aborted)
25
+ return Promise.resolve(undefined);
26
+ const item = this.#items[0];
27
+ if (item !== undefined)
28
+ return Promise.resolve(this.#taken(item));
29
+ const { promise, resolve } = Promise.withResolvers();
30
+ const onAbort = () => {
31
+ this.#readers.splice(this.#readers.indexOf(reader), 1);
32
+ resolve(undefined);
33
+ };
34
+ const reader = (next) => {
35
+ signal?.removeEventListener('abort', onAbort);
36
+ resolve(next);
37
+ };
38
+ this.#readers.push(reader);
39
+ signal?.addEventListener('abort', onAbort, { once: true });
40
+ return promise;
41
+ }
42
+ /** Moves each item that waits here to `other`, in order. */
43
+ moveTo(other) {
44
+ for (const item of this.#items.splice(0))
45
+ other.#put(item);
46
+ }
47
+ #put(item) {
48
+ this.#items.push(item);
49
+ const reader = this.#readers.shift();
50
+ if (reader !== undefined)
51
+ reader(this.#taken(this.#items[0]));
52
+ }
53
+ /** Takes a chunk off the queue; leaves the end and the error. */
54
+ #taken(item) {
55
+ if (item.kind === 'chunk') {
56
+ this.#items.shift();
57
+ item.taken();
58
+ }
59
+ return item;
60
+ }
61
+ }
62
+ //# sourceMappingURL=channel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel.js","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAaA;;;;;;;GAOG;AACH,MAAM,OAAO,OAAO;IACT,MAAM,GAAW,EAAE,CAAC;IACpB,QAAQ,GAAa,EAAE,CAAC;IAEjC,IAAI,CAAC,KAAa;QAChB,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,aAAa,EAAQ,CAAC;QAC3D,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;QACpD,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,GAAG;QACD,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;IAC7B,CAAC;IAED,IAAI,CAAC,KAAc;QACjB,IAAI,CAAC,IAAI,CAAC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CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACvB,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACrC,IAAI,MAAM,KAAK,SAAS;YAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC;IACjE,CAAC;IAED,iEAAiE;IACjE,MAAM,CAAC,IAAU;QACf,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;YAC1B,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;YACpB,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
@@ -0,0 +1,24 @@
1
+ import type { Socket } from 'node:net';
2
+ import type { Clock } from '@zukhruf/async';
3
+ import type { Stream, Toxic } from './toxic.ts';
4
+ /** What a connection gives each stage of its streams. */
5
+ export interface Sockets {
6
+ readonly clock: Clock;
7
+ reset(): void;
8
+ close(): void;
9
+ }
10
+ /**
11
+ * One stream of one connection: the chunks of the socket that sends, through
12
+ * a stage for each toxic of the stream in the order of their adds, into the
13
+ * socket that receives. Toxiproxy's `ToxicLink`.
14
+ */
15
+ export declare class Direction {
16
+ #private;
17
+ readonly stream: Stream;
18
+ constructor(stream: Stream, sockets: Sockets, disposal: AbortSignal);
19
+ /** Forwards from `from` to `to`; the read of `from` is a generator, so its error reaches the toxics and does not destroy `to` (ADR 0003). */
20
+ run(from: Socket, to: Socket): void;
21
+ add(key: object, toxic: Toxic): void;
22
+ remove(key: object): void;
23
+ }
24
+ //# sourceMappingURL=direction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"direction.d.ts","sourceRoot":"","sources":["../src/direction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGvC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAG5C,OAAO,KAAK,EAAQ,MAAM,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEtD,yDAAyD;AACzD,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,KAAK,IAAI,IAAI,CAAC;IACd,KAAK,IAAI,IAAI,CAAC;CACf;AA2DD;;;;GAIG;AACH,qBAAa,SAAS;;IACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAOxB,YAAY,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAIlE;IAED,6IAA6I;IAC7I,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI,CAQlC;IAED,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAUnC;IAED,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAExB;CAoCF"}
@@ -0,0 +1,130 @@
1
+ import { pipeline } from 'node:stream/promises';
2
+ import { Channel } from './channel.js';
3
+ /** One toxic on one stream of one connection: Toxiproxy's `ToxicStub`. */
4
+ class Stage {
5
+ key;
6
+ input = new Channel();
7
+ output;
8
+ #removal = new AbortController();
9
+ signal;
10
+ constructor(key, output, disposal) {
11
+ this.key = key;
12
+ this.output = output;
13
+ this.signal = AbortSignal.any([this.#removal.signal, disposal]);
14
+ }
15
+ remove() {
16
+ this.#removal.abort();
17
+ }
18
+ /**
19
+ * Resolves `true` when the stage stopped at its removal and the stream goes
20
+ * on without it. An end or an error that the toxic saw is still in the
21
+ * input then, so it goes on with the chunks that the toxic did not take.
22
+ */
23
+ async run(toxic, link) {
24
+ try {
25
+ for await (const chunk of toxic.pipe(this.#chunks(), link))
26
+ await this.output.send(chunk);
27
+ }
28
+ catch (error) {
29
+ if (!this.signal.aborted || error !== this.signal.reason) {
30
+ this.output.fail(error);
31
+ return false;
32
+ }
33
+ }
34
+ if (this.signal.aborted)
35
+ return true;
36
+ this.output.end();
37
+ return false;
38
+ }
39
+ async *#chunks() {
40
+ for (;;) {
41
+ const item = await this.input.take(this.signal);
42
+ if (item === undefined)
43
+ return;
44
+ if (item.kind === 'chunk') {
45
+ yield item.chunk;
46
+ continue;
47
+ }
48
+ if (item.kind === 'end')
49
+ return;
50
+ throw item.error;
51
+ }
52
+ }
53
+ }
54
+ /**
55
+ * One stream of one connection: the chunks of the socket that sends, through
56
+ * a stage for each toxic of the stream in the order of their adds, into the
57
+ * socket that receives. Toxiproxy's `ToxicLink`.
58
+ */
59
+ export class Direction {
60
+ stream;
61
+ #sockets;
62
+ #disposal;
63
+ #tail = new Channel();
64
+ #head = { output: this.#tail };
65
+ #stages = [];
66
+ constructor(stream, sockets, disposal) {
67
+ this.stream = stream;
68
+ this.#sockets = sockets;
69
+ this.#disposal = disposal;
70
+ }
71
+ /** Forwards from `from` to `to`; the read of `from` is a generator, so its error reaches the toxics and does not destroy `to` (ADR 0003). */
72
+ run(from, to) {
73
+ pipeline(async function* () {
74
+ yield* from.iterator({ destroyOnReturn: false });
75
+ }, (source) => this.#flow(source, to), to).catch(ignore);
76
+ }
77
+ add(key, toxic) {
78
+ const stage = new Stage(key, this.#tail, this.#disposal);
79
+ (this.#stages.at(-1) ?? this.#head).output = stage.input;
80
+ this.#stages.push(stage);
81
+ const { clock, reset, close } = this.#sockets;
82
+ void stage
83
+ .run(toxic, { clock, signal: stage.signal, reset, close })
84
+ .then((removed) => {
85
+ if (removed)
86
+ this.#unlink(stage);
87
+ });
88
+ }
89
+ remove(key) {
90
+ for (const stage of this.#stages)
91
+ if (stage.key === key)
92
+ stage.remove();
93
+ }
94
+ /** Toxiproxy's `RemoveToxic`: the chunks that the stage did not take go on, and the stage before sends past it. */
95
+ #unlink(stage) {
96
+ const index = this.#stages.indexOf(stage);
97
+ stage.input.moveTo(stage.output);
98
+ (this.#stages[index - 1] ?? this.#head).output = stage.output;
99
+ this.#stages.splice(index, 1);
100
+ }
101
+ /** An error that reaches the end of the stages resets `to`, so its peer sees `ECONNRESET` as at the side that failed. */
102
+ async *#flow(source, to) {
103
+ void this.#read(source);
104
+ for (;;) {
105
+ const item = await this.#tail.take();
106
+ if (item.kind === 'chunk') {
107
+ yield item.chunk;
108
+ continue;
109
+ }
110
+ if (item.kind === 'end')
111
+ return;
112
+ if (!to.destroyed)
113
+ to.resetAndDestroy();
114
+ throw item.error;
115
+ }
116
+ }
117
+ async #read(source) {
118
+ try {
119
+ for await (const chunk of source)
120
+ await this.#head.output.send(chunk);
121
+ this.#head.output.end();
122
+ }
123
+ catch (error) {
124
+ this.#head.output.fail(error);
125
+ }
126
+ }
127
+ }
128
+ /** A failed pipeline has destroyed the socket that receives; the other stream ends by itself. */
129
+ function ignore() { }
130
+ //# sourceMappingURL=direction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"direction.js","sourceRoot":"","sources":["../src/direction.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAIhD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAevC,0EAA0E;AAC1E,MAAM,KAAK;IACA,GAAG,CAAS;IACZ,KAAK,GAAG,IAAI,OAAO,EAAE,CAAC;IAC/B,MAAM,CAAU;IACP,QAAQ,GAAG,IAAI,eAAe,EAAE,CAAC;IACjC,MAAM,CAAc;IAE7B,YAAY,GAAW,EAAE,MAAe,EAAE,QAAqB;QAC7D,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;IAClE,CAAC;IAED,MAAM;QACJ,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;IAED;;;;OAIG;IACH,KAAK,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MAAM;gBAAE,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACtE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC;QAC1B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;CACF;AAED,iGAAiG;AACjG,SAAS,MAAM,KAAU,CAAC"}
@@ -0,0 +1,9 @@
1
+ export { Proxy, type ProxyOptions, type Upstream } from './proxy.ts';
2
+ export type { Link, Stream, Toxic } from './toxic.ts';
3
+ export { Latency, type LatencyOptions } from './toxics/latency.ts';
4
+ export { ResetPeer, type ResetPeerOptions } from './toxics/reset-peer.ts';
5
+ export { Slicer, type SlicerOptions } from './toxics/slicer.ts';
6
+ export { StallAfter, type StallAfterOptions } from './toxics/stall-after.ts';
7
+ export { Timeout, type TimeoutOptions } from './toxics/timeout.ts';
8
+ export { Vanish, type VanishOptions } from './toxics/vanish.ts';
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AACrE,YAAY,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,SAAS,EAAE,KAAK,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,KAAK,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { Proxy } from './proxy.js';
2
+ export { Latency } from './toxics/latency.js';
3
+ export { ResetPeer } from './toxics/reset-peer.js';
4
+ export { Slicer } from './toxics/slicer.js';
5
+ export { StallAfter } from './toxics/stall-after.js';
6
+ export { Timeout } from './toxics/timeout.js';
7
+ export { Vanish } from './toxics/vanish.js';
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAoC,MAAM,YAAY,CAAC;AAErE,OAAO,EAAE,OAAO,EAAuB,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,SAAS,EAAyB,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,MAAM,EAAsB,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,UAAU,EAA0B,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAuB,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAAE,MAAM,EAAsB,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { Clock } from '@zukhruf/async';
2
+ import type { Handler } from '@zukhruf/net';
3
+ import type { Toxic } from './toxic.ts';
4
+ /** The real server that a proxy forwards to. Toxiproxy's `upstream`. */
5
+ export interface Upstream {
6
+ /** A host name or an IP address. */
7
+ readonly host: string;
8
+ readonly port: number;
9
+ }
10
+ export interface ProxyOptions {
11
+ /** The clock that the toxics wait on: `globalThis`, or a fake clock in a test (root ADR 0004). */
12
+ readonly clock: Clock;
13
+ }
14
+ /**
15
+ * Forwards each connection that a `Server` of `@zukhruf/net` accepts to its
16
+ * upstream, through its toxics. Toxiproxy's proxy, without the listen.
17
+ */
18
+ export declare class Proxy implements AsyncDisposable {
19
+ #private;
20
+ constructor(upstream: Upstream, { clock }: ProxyOptions);
21
+ /** The handler that a `Server` of `@zukhruf/net` serves. */
22
+ readonly forward: Handler;
23
+ /** Puts the toxic on its stream of each open and each later connection. */
24
+ add(toxic: Toxic): Disposable;
25
+ /** Destroys each socket to the upstream, and waits for each close. */
26
+ [Symbol.asyncDispose](): Promise<void>;
27
+ }
28
+ //# sourceMappingURL=proxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAG5C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,wEAAwE;AACxE,MAAM,WAAW,QAAQ;IACvB,oCAAoC;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,YAAY;IAC3B,kGAAkG;IAClG,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;CACvB;AAOD;;;GAGG;AACH,qBAAa,KAAM,YAAW,eAAe;;IAQ3C,YAAY,QAAQ,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,EAAE,YAAY,EAGtD;IAED,4DAA4D;IAC5D,QAAQ,CAAC,OAAO,EAAE,OAAO,CAEvB;IAEF,2EAA2E;IAC3E,GAAG,CAAC,KAAK,EAAE,KAAK,GAAG,UAAU,CAY5B;IAED,sEAAsE;IAChE,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,CAK3C;CAkCF"}