@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/dist/proxy.js ADDED
@@ -0,0 +1,125 @@
1
+ import { once } from 'node:events';
2
+ import { connect } from 'node:net';
3
+ import { Direction } from './direction.js';
4
+ /**
5
+ * Forwards each connection that a `Server` of `@zukhruf/net` accepts to its
6
+ * upstream, through its toxics. Toxiproxy's proxy, without the listen.
7
+ */
8
+ export class Proxy {
9
+ #upstream;
10
+ #clock;
11
+ #added = [];
12
+ #connections = new Set();
13
+ #sockets = new Set();
14
+ #disposal = new AbortController();
15
+ constructor(upstream, { clock }) {
16
+ this.#upstream = upstream;
17
+ this.#clock = clock;
18
+ }
19
+ /** The handler that a `Server` of `@zukhruf/net` serves. */
20
+ forward = (client) => {
21
+ void this.#forward(client);
22
+ };
23
+ /** Puts the toxic on its stream of each open and each later connection. */
24
+ add(toxic) {
25
+ const added = { toxic };
26
+ this.#added.push(added);
27
+ for (const connection of this.#connections)
28
+ connection.add(added);
29
+ return {
30
+ [Symbol.dispose]: () => {
31
+ const index = this.#added.indexOf(added);
32
+ if (index === -1)
33
+ return;
34
+ this.#added.splice(index, 1);
35
+ for (const connection of this.#connections)
36
+ connection.remove(added);
37
+ },
38
+ };
39
+ }
40
+ /** Destroys each socket to the upstream, and waits for each close. */
41
+ async [Symbol.asyncDispose]() {
42
+ this.#disposal.abort();
43
+ const closes = [...this.#sockets].map(closed);
44
+ for (const socket of this.#sockets)
45
+ socket.destroy();
46
+ await Promise.all(closes);
47
+ }
48
+ async #forward(client) {
49
+ if (this.#disposal.signal.aborted) {
50
+ client.destroy();
51
+ return;
52
+ }
53
+ const server = connect({
54
+ host: this.#upstream.host,
55
+ port: this.#upstream.port,
56
+ allowHalfOpen: true,
57
+ });
58
+ server.on('error', ignore);
59
+ this.#sockets.add(server);
60
+ server.once('close', () => this.#sockets.delete(server));
61
+ try {
62
+ await once(server, 'connect', { signal: this.#disposal.signal });
63
+ }
64
+ catch {
65
+ client.destroy();
66
+ return;
67
+ }
68
+ const connection = new Connection(client, server, this.#clock, this.#disposal.signal);
69
+ this.#connections.add(connection);
70
+ void Promise.all([closed(client), closed(server)]).then(() => this.#connections.delete(connection));
71
+ for (const added of this.#added)
72
+ connection.add(added);
73
+ connection.run();
74
+ }
75
+ }
76
+ /** A client socket, its socket to the upstream, and a direction for each stream. */
77
+ class Connection {
78
+ #client;
79
+ #server;
80
+ #upstream;
81
+ #downstream;
82
+ constructor(client, server, clock, disposal) {
83
+ this.#client = client;
84
+ this.#server = server;
85
+ const sockets = {
86
+ clock,
87
+ reset: () => {
88
+ for (const socket of [client, server])
89
+ if (!socket.destroyed)
90
+ socket.resetAndDestroy();
91
+ },
92
+ close: () => {
93
+ client.destroy();
94
+ server.destroy();
95
+ },
96
+ };
97
+ this.#upstream = new Direction('upstream', sockets, disposal);
98
+ this.#downstream = new Direction('downstream', sockets, disposal);
99
+ }
100
+ add(added) {
101
+ this.#direction(added.toxic).add(added, added.toxic);
102
+ }
103
+ remove(added) {
104
+ this.#direction(added.toxic).remove(added);
105
+ }
106
+ run() {
107
+ this.#upstream.run(this.#client, this.#server);
108
+ this.#downstream.run(this.#server, this.#client);
109
+ }
110
+ #direction({ stream }) {
111
+ return stream === 'upstream' ? this.#upstream : this.#downstream;
112
+ }
113
+ }
114
+ /** Resolves at the `close` of the socket, also after an `error`. */
115
+ function closed(socket) {
116
+ return new Promise((resolve) => {
117
+ if (socket.closed)
118
+ resolve();
119
+ else
120
+ socket.once('close', () => resolve());
121
+ });
122
+ }
123
+ /** An error of the socket to the upstream reaches the toxics through its read; this listener keeps it from ending the process. */
124
+ function ignore() { }
125
+ //# sourceMappingURL=proxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAe,OAAO,EAAE,MAAM,UAAU,CAAC;AAKhD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAoB3C;;;GAGG;AACH,MAAM,OAAO,KAAK;IACP,SAAS,CAAW;IACpB,MAAM,CAAQ;IACd,MAAM,GAAY,EAAE,CAAC;IACrB,YAAY,GAAG,IAAI,GAAG,EAAc,CAAC;IACrC,QAAQ,GAAG,IAAI,GAAG,EAAU,CAAC;IAC7B,SAAS,GAAG,IAAI,eAAe,EAAE,CAAC;IAE3C,YAAY,QAAkB,EAAE,EAAE,KAAK,EAAgB;QACrD,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,4DAA4D;IACnD,OAAO,GAAY,CAAC,MAAM,EAAE,EAAE;QACrC,KAAK,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC7B,CAAC,CAAC;IAEF,2EAA2E;IAC3E,GAAG,CAAC,KAAY;QACd,MAAM,KAAK,GAAU,EAAE,KAAK,EAAE,CAAC;QAC/B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACxB,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,YAAY;YAAE,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QAClE,OAAO;YACL,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE;gBACrB,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;gBACzC,IAAI,KAAK,KAAK,CAAC,CAAC;oBAAE,OAA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/async';
2
+ /** `upstream` is from the client to the server, `downstream` from the server to the client. Toxiproxy's `stream`. */
3
+ export type Stream = 'upstream' | 'downstream';
4
+ /** What a toxic gets for one stream of one connection. Toxiproxy's link and its stub. */
5
+ export interface Link {
6
+ readonly clock: Clock;
7
+ /**
8
+ * Aborts at the removal of the toxic, and at the disposal of the proxy
9
+ * (Toxiproxy's `stub.Interrupt`): `chunks` then ends, `pipe` gives what it
10
+ * holds at once and returns, and each chunk that it did not take goes on.
11
+ */
12
+ readonly signal: AbortSignal;
13
+ /** Ends both sockets with a TCP reset. */
14
+ reset(): void;
15
+ /** Destroys both sockets. */
16
+ close(): void;
17
+ }
18
+ /** A fault on one stream of each connection of a proxy (Strategy, GoF). */
19
+ export interface Toxic {
20
+ readonly stream: Stream;
21
+ /**
22
+ * Gets the chunks of one stream of one connection, and gives the chunks
23
+ * that reach the other side. Its return sends the end on; a `pipe` that
24
+ * never returns holds the end. A throw of `chunks` is an error of the
25
+ * socket that sends; a `pipe` that does not catch it ends the connection.
26
+ */
27
+ pipe(chunks: AsyncIterable<Buffer>, link: Link): AsyncIterable<Buffer>;
28
+ }
29
+ //# sourceMappingURL=toxic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toxic.d.ts","sourceRoot":"","sources":["../src/toxic.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAE5C,qHAAqH;AACrH,MAAM,MAAM,MAAM,GAAG,UAAU,GAAG,YAAY,CAAC;AAE/C,yFAAyF;AACzF,MAAM,WAAW,IAAI;IACnB,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,0CAA0C;IAC1C,KAAK,IAAI,IAAI,CAAC;IACd,6BAA6B;IAC7B,KAAK,IAAI,IAAI,CAAC;CACf;AAED,2EAA2E;AAC3E,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB;;;;;OAKG;IACH,IAAI,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;CACxE"}
package/dist/toxic.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=toxic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toxic.js","sourceRoot":"","sources":["../src/toxic.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ import type { Link, Stream, Toxic } from '../toxic.ts';
2
+ export interface LatencyOptions {
3
+ readonly stream: Stream;
4
+ /** Milliseconds. */
5
+ readonly latency: number;
6
+ }
7
+ /**
8
+ * Each chunk and the end reach the other side `latency` ms after they reached
9
+ * the toxic, in their order. Toxiproxy's `latency`, which reads ahead and
10
+ * waits for each chunk from its own arrival; unlike Toxiproxy, the end waits
11
+ * too (ADR 0005).
12
+ */
13
+ export declare class Latency implements Toxic {
14
+ #private;
15
+ readonly stream: Stream;
16
+ constructor({ stream, latency }: LatencyOptions);
17
+ pipe(chunks: AsyncIterable<Buffer>, link: Link): AsyncGenerator<Buffer>;
18
+ }
19
+ //# sourceMappingURL=latency.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"latency.d.ts","sourceRoot":"","sources":["../../src/toxics/latency.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGvD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,oBAAoB;IACpB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AASD;;;;;GAKG;AACH,qBAAa,OAAQ,YAAW,KAAK;;IACnC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAGxB,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,cAAc,EAG9C;IAEM,IAAI,CACT,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,IAAI,EAAE,IAAI,GACT,cAAc,CAAC,MAAM,CAAC,CA4BxB;CACF"}
@@ -0,0 +1,48 @@
1
+ import { wait } from './wait.js';
2
+ /**
3
+ * Each chunk and the end reach the other side `latency` ms after they reached
4
+ * the toxic, in their order. Toxiproxy's `latency`, which reads ahead and
5
+ * waits for each chunk from its own arrival; unlike Toxiproxy, the end waits
6
+ * too (ADR 0005).
7
+ */
8
+ export class Latency {
9
+ stream;
10
+ #latency;
11
+ constructor({ stream, latency }) {
12
+ this.stream = stream;
13
+ this.#latency = latency;
14
+ }
15
+ async *pipe(chunks, link) {
16
+ const arrivals = [];
17
+ let arrived = Promise.withResolvers();
18
+ const now = () => link.clock.performance.now();
19
+ void (async () => {
20
+ try {
21
+ for await (const chunk of chunks) {
22
+ arrivals.push({ at: now(), kind: 'chunk', chunk });
23
+ arrived.resolve();
24
+ }
25
+ arrivals.push({ at: now(), kind: 'end' });
26
+ }
27
+ catch (error) {
28
+ arrivals.push({ at: now(), kind: 'error', error });
29
+ }
30
+ arrived.resolve();
31
+ })();
32
+ for (;;) {
33
+ if (arrivals.length === 0) {
34
+ arrived = Promise.withResolvers();
35
+ await arrived.promise;
36
+ continue;
37
+ }
38
+ const arrival = arrivals.shift();
39
+ await wait(arrival.at + this.#latency - now(), link);
40
+ if (arrival.kind === 'end')
41
+ return;
42
+ if (arrival.kind === 'error')
43
+ throw arrival.error;
44
+ yield arrival.chunk;
45
+ }
46
+ }
47
+ }
48
+ //# sourceMappingURL=latency.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"latency.js","sourceRoot":"","sources":["../../src/toxics/latency.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAejC;;;;;GAKG;AACH,MAAM,OAAO,OAAO;IACT,MAAM,CAAS;IACf,QAAQ,CAAS;IAE1B,YAAY,EAA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YAClC,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,GAAG,IAAI,CAAC,QAAQ,GAAG,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC;YACrD,IAAI,OAAO,CAAC,IAAI,KAAK,KAAK;gBAAE,OAAO;YACnC,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO;gBAAE,MAAM,OAAO,CAAC,KAAK,CAAC;YAClD,MAAM,OAAO,CAAC,KAAK,CAAC;QACtB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,18 @@
1
+ import type { Link, Stream, Toxic } from '../toxic.ts';
2
+ export interface ResetPeerOptions {
3
+ readonly stream: Stream;
4
+ /** Milliseconds from the first chunk to the reset. */
5
+ readonly timeout: number;
6
+ }
7
+ /**
8
+ * At the first chunk, or at the end, it waits `timeout` ms and ends the
9
+ * connection with a TCP reset; the chunk drops. Toxiproxy's `reset_peer`,
10
+ * which takes the end as it takes a chunk.
11
+ */
12
+ export declare class ResetPeer implements Toxic {
13
+ #private;
14
+ readonly stream: Stream;
15
+ constructor({ stream, timeout }: ResetPeerOptions);
16
+ pipe(chunks: AsyncIterable<Buffer>, link: Link): AsyncGenerator<Buffer>;
17
+ }
18
+ //# sourceMappingURL=reset-peer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reset-peer.d.ts","sourceRoot":"","sources":["../../src/toxics/reset-peer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGvD,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,sDAAsD;IACtD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;;GAIG;AACH,qBAAa,SAAU,YAAW,KAAK;;IACrC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAGxB,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,gBAAgB,EAGhD;IAGM,IAAI,CACT,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,IAAI,EAAE,IAAI,GACT,cAAc,CAAC,MAAM,CAAC,CAGxB;CACF"}
@@ -0,0 +1,21 @@
1
+ import { wait } from './wait.js';
2
+ /**
3
+ * At the first chunk, or at the end, it waits `timeout` ms and ends the
4
+ * connection with a TCP reset; the chunk drops. Toxiproxy's `reset_peer`,
5
+ * which takes the end as it takes a chunk.
6
+ */
7
+ export class ResetPeer {
8
+ stream;
9
+ #timeout;
10
+ constructor({ stream, timeout }) {
11
+ this.stream = stream;
12
+ this.#timeout = timeout;
13
+ }
14
+ // eslint-disable-next-line require-yield -- the first chunk drops
15
+ async *pipe(chunks, link) {
16
+ await chunks[Symbol.asyncIterator]().next();
17
+ if (await wait(this.#timeout, link))
18
+ link.reset();
19
+ }
20
+ }
21
+ //# sourceMappingURL=reset-peer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reset-peer.js","sourceRoot":"","sources":["../../src/toxics/reset-peer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAQjC;;;;GAIG;AACH,MAAM,OAAO,SAAS;IACX,MAAM,CAAS;IACf,QAAQ,CAAS;IAE1B,YAAY,EAAE,MAAM,EAAE,OAAO,EAAoB;QAC/C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,kEAAkE;IAClE,KAAK,CAAC,CAAC,IAAI,CACT,MAA6B,EAC7B,IAAU;QAEV,MAAM,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC;QAC5C,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC;YAAE,IAAI,CAAC,KAAK,EAAE,CAAC;IACpD,CAAC;CACF"}
@@ -0,0 +1,24 @@
1
+ import type { Link, Stream, Toxic } from '../toxic.ts';
2
+ export interface SlicerOptions {
3
+ readonly stream: Stream;
4
+ /** Bytes; more than 0. */
5
+ readonly averageSize: number;
6
+ /** Bytes; from 0 to `averageSize`. */
7
+ readonly sizeVariation: number;
8
+ /** Milliseconds after each piece. */
9
+ readonly delay: number;
10
+ }
11
+ /**
12
+ * Splits each chunk into pieces of about `averageSize` bytes, and waits
13
+ * `delay` after each piece. Toxiproxy's `slicer`: it halves a chunk until a
14
+ * piece is at most `averageSize + sizeVariation`, and moves each cut by a
15
+ * random amount below `sizeVariation`; at the removal, the rest of the chunk
16
+ * goes on in one piece.
17
+ */
18
+ export declare class Slicer implements Toxic {
19
+ #private;
20
+ readonly stream: Stream;
21
+ constructor({ stream, averageSize, sizeVariation, delay }: SlicerOptions);
22
+ pipe(chunks: AsyncIterable<Buffer>, link: Link): AsyncGenerator<Buffer>;
23
+ }
24
+ //# sourceMappingURL=slicer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slicer.d.ts","sourceRoot":"","sources":["../../src/toxics/slicer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGvD,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,0BAA0B;IAC1B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,sCAAsC;IACtC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,qCAAqC;IACrC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;GAMG;AACH,qBAAa,MAAO,YAAW,KAAK;;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAKxB,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,KAAK,EAAE,EAAE,aAAa,EAWvE;IAEM,IAAI,CACT,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,IAAI,EAAE,IAAI,GACT,cAAc,CAAC,MAAM,CAAC,CAaxB;CAaF"}
@@ -0,0 +1,53 @@
1
+ import { wait } from './wait.js';
2
+ /**
3
+ * Splits each chunk into pieces of about `averageSize` bytes, and waits
4
+ * `delay` after each piece. Toxiproxy's `slicer`: it halves a chunk until a
5
+ * piece is at most `averageSize + sizeVariation`, and moves each cut by a
6
+ * random amount below `sizeVariation`; at the removal, the rest of the chunk
7
+ * goes on in one piece.
8
+ */
9
+ export class Slicer {
10
+ stream;
11
+ #averageSize;
12
+ #sizeVariation;
13
+ #delay;
14
+ constructor({ stream, averageSize, sizeVariation, delay }) {
15
+ if (!(averageSize > 0))
16
+ throw new RangeError(`averageSize must be more than 0: ${averageSize}`);
17
+ if (!(sizeVariation >= 0 && sizeVariation <= averageSize))
18
+ throw new RangeError(`sizeVariation must be from 0 to averageSize: ${sizeVariation}`);
19
+ this.stream = stream;
20
+ this.#averageSize = averageSize;
21
+ this.#sizeVariation = sizeVariation;
22
+ this.#delay = delay;
23
+ }
24
+ async *pipe(chunks, link) {
25
+ for await (const chunk of chunks) {
26
+ const cuts = this.#cuts(0, chunk.length);
27
+ for (let start = 0, index = 0; index < cuts.length; index++) {
28
+ const end = cuts[index];
29
+ yield chunk.subarray(start, end);
30
+ start = end;
31
+ if (!(await wait(this.#delay, link))) {
32
+ if (start < chunk.length)
33
+ yield chunk.subarray(start);
34
+ return;
35
+ }
36
+ }
37
+ }
38
+ }
39
+ /** The end of each piece of `[start, end)`: Toxiproxy's `chunk`. */
40
+ #cuts(start, end) {
41
+ if (end - start - this.#averageSize <= this.#sizeVariation)
42
+ return [end];
43
+ let middle = start + Math.floor((end - start) / 2);
44
+ if (this.#sizeVariation > 0)
45
+ middle +=
46
+ Math.floor(Math.random() * this.#sizeVariation * 2) -
47
+ this.#sizeVariation;
48
+ if (middle <= start || middle >= end)
49
+ return [end];
50
+ return [...this.#cuts(start, middle), ...this.#cuts(middle, end)];
51
+ }
52
+ }
53
+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K,GAAG,GAAG,CAAC;gBACZ,IAAI,CAAC,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,EAAE,CAAC;oBACrC,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM;wBAAE,MAAM,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;oBACtD,OAAO;gBACT,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,oEAAoE;IACpE,KAAK,CAAC,KAAa,EAAE,GAAW;QAC9B,IAAI,GAAG,GAAG,KAAK,GAAG,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,cAAc;YAAE,OAAO,CAAC,GAAG,CAAC,CAAC;QACzE,IAAI,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QACnD,IAAI,IAAI,CAAC,cAAc,GAAG,CAAC;YACzB,MAAM;gBACJ,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,cAAc,GAAG,CAAC,CAAC;oBACnD,IAAI,CAAC,cAAc,CAAC;QACxB,IAAI,MAAM,IAAI,KAAK,IAAI,MAAM,IAAI,GAAG;YAAE,OAAO,CAAC,GAAG,CAAC,CAAC;QACnD,OAAO,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;IACpE,CAAC;CACF"}
@@ -0,0 +1,20 @@
1
+ import type { Link, Stream, Toxic } from '../toxic.ts';
2
+ export interface StallAfterOptions {
3
+ readonly stream: Stream;
4
+ /** The bytes that pass on each connection before the stall. */
5
+ readonly bytes: number;
6
+ }
7
+ /**
8
+ * Passes `bytes` bytes of each connection, then holds each later chunk and
9
+ * the end, and keeps the connection open; at the removal, what it holds goes
10
+ * on. Our own fault: Toxiproxy's `limit_data` closes the connection.
11
+ */
12
+ export declare class StallAfter implements Toxic {
13
+ #private;
14
+ readonly stream: Stream;
15
+ constructor({ stream, bytes }: StallAfterOptions);
16
+ /** Settles when a connection has passed `bytes` bytes on the stream. */
17
+ get reached(): Promise<void>;
18
+ pipe(chunks: AsyncIterable<Buffer>, { signal }: Link): AsyncGenerator<Buffer>;
19
+ }
20
+ //# sourceMappingURL=stall-after.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stall-after.d.ts","sourceRoot":"","sources":["../../src/toxics/stall-after.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEvD,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,+DAA+D;IAC/D,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED;;;;GAIG;AACH,qBAAa,UAAW,YAAW,KAAK;;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAIxB,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,iBAAiB,EAG/C;IAED,wEAAwE;IACxE,IAAI,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAE3B;IAEM,IAAI,CACT,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,EAAE,MAAM,EAAE,EAAE,IAAI,GACf,cAAc,CAAC,MAAM,CAAC,CAexB;CACF"}
@@ -0,0 +1,41 @@
1
+ import { once } from 'node:events';
2
+ import { Latch } from '@zukhruf/async';
3
+ /**
4
+ * Passes `bytes` bytes of each connection, then holds each later chunk and
5
+ * the end, and keeps the connection open; at the removal, what it holds goes
6
+ * on. Our own fault: Toxiproxy's `limit_data` closes the connection.
7
+ */
8
+ export class StallAfter {
9
+ stream;
10
+ #bytes;
11
+ #reached = new Latch();
12
+ constructor({ stream, bytes }) {
13
+ this.stream = stream;
14
+ this.#bytes = bytes;
15
+ }
16
+ /** Settles when a connection has passed `bytes` bytes on the stream. */
17
+ get reached() {
18
+ return this.#reached.wait();
19
+ }
20
+ async *pipe(chunks, { signal }) {
21
+ const iterator = chunks[Symbol.asyncIterator]();
22
+ let left = this.#bytes;
23
+ let held;
24
+ while (left > 0) {
25
+ const next = await iterator.next();
26
+ if (next.done)
27
+ return;
28
+ const chunk = next.value;
29
+ if (chunk.length > left)
30
+ held = chunk.subarray(left);
31
+ yield chunk.subarray(0, left);
32
+ left -= Math.min(left, chunk.length);
33
+ }
34
+ this.#reached.open();
35
+ if (!signal.aborted)
36
+ await once(signal, 'abort');
37
+ if (held !== undefined)
38
+ yield held;
39
+ }
40
+ }
41
+ //# sourceMappingURL=stall-after.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stall-after.js","sourceRoot":"","sources":["../../src/toxics/stall-after.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEnC,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAUvC;;;;GAIG;AACH,MAAM,OAAO,UAAU;IACZ,MAAM,CAAS;IACf,MAAM,CAAS;IACf,QAAQ,GAAG,IAAI,KAAK,EAAE,CAAC;IAEhC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAqB;QAC9C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,wEAAwE;IACxE,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,CAAC,IAAI,CACT,MAA6B,EAC7B,EAAE,MAAM,EAAQ;QAEhB,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC;QAChD,IAAI,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC;QACvB,IAAI,IAAwB,CAAC;QAC7B,OAAO,IAAI,GAAG,CAAC,EAAE,CAAC;YAChB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,IAAI,IAAI,CAAC,IAAI;gBAAE,OAAO;YACtB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;YACzB,IAAI,KAAK,CAAC,MAAM,GAAG,IAAI;gBAAE,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YACrD,MAAM,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;YAC9B,IAAI,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;QACvC,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;QACrB,IAAI,CAAC,MAAM,CAAC,OAAO;YAAE,MAAM,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACjD,IAAI,IAAI,KAAK,SAAS;YAAE,MAAM,IAAI,CAAC;IACrC,CAAC;CACF"}
@@ -0,0 +1,19 @@
1
+ import type { Link, Stream, Toxic } from '../toxic.ts';
2
+ export interface TimeoutOptions {
3
+ readonly stream: Stream;
4
+ /** Milliseconds. 0: drop until the removal, and never close. */
5
+ readonly timeout: number;
6
+ }
7
+ /**
8
+ * Drops each chunk. After `timeout` ms with no chunk, it closes the
9
+ * connection; each dropped chunk starts the wait again, as Toxiproxy's
10
+ * `timeout` makes a new `time.After` for each chunk. An end or a close of a
11
+ * peer still goes through.
12
+ */
13
+ export declare class Timeout implements Toxic {
14
+ #private;
15
+ readonly stream: Stream;
16
+ constructor({ stream, timeout }: TimeoutOptions);
17
+ pipe(chunks: AsyncIterable<Buffer>, { clock, close }: Link): AsyncGenerator<Buffer>;
18
+ }
19
+ //# sourceMappingURL=timeout.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../../src/toxics/timeout.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGvD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,gEAAgE;IAChE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;GAKG;AACH,qBAAa,OAAQ,YAAW,KAAK;;IACnC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAGxB,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,cAAc,EAG9C;IAGM,IAAI,CACT,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,GACrB,cAAc,CAAC,MAAM,CAAC,CAqBxB;CACF"}
@@ -0,0 +1,42 @@
1
+ import { wait } from './wait.js';
2
+ /**
3
+ * Drops each chunk. After `timeout` ms with no chunk, it closes the
4
+ * connection; each dropped chunk starts the wait again, as Toxiproxy's
5
+ * `timeout` makes a new `time.After` for each chunk. An end or a close of a
6
+ * peer still goes through.
7
+ */
8
+ export class Timeout {
9
+ stream;
10
+ #timeout;
11
+ constructor({ stream, timeout }) {
12
+ this.stream = stream;
13
+ this.#timeout = timeout;
14
+ }
15
+ // eslint-disable-next-line require-yield -- each chunk drops
16
+ async *pipe(chunks, { clock, close }) {
17
+ const iterator = chunks[Symbol.asyncIterator]();
18
+ for (;;) {
19
+ const next = iterator.next();
20
+ if (this.#timeout === 0) {
21
+ if ((await next).done)
22
+ return;
23
+ continue;
24
+ }
25
+ const stop = new AbortController();
26
+ const timedOut = wait(this.#timeout, { clock, signal: stop.signal });
27
+ try {
28
+ const first = await Promise.race([next, timedOut]);
29
+ if (first === true) {
30
+ close();
31
+ return;
32
+ }
33
+ if (first !== false && first.done)
34
+ return;
35
+ }
36
+ finally {
37
+ stop.abort();
38
+ }
39
+ }
40
+ }
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5C,CAAC;oBAAS,CAAC;gBACT,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,15 @@
1
+ import type { Link, Stream, Toxic } from '../toxic.ts';
2
+ export interface VanishOptions {
3
+ readonly stream: Stream;
4
+ }
5
+ /**
6
+ * Drops each chunk, the end and the error of its input: the other side never
7
+ * learns that this side ended, as a gateway for a machine that is gone. Our
8
+ * own fault (mutex ADR 0035): a `Timeout` of 0 still forwards the end.
9
+ */
10
+ export declare class Vanish implements Toxic {
11
+ readonly stream: Stream;
12
+ constructor({ stream }: VanishOptions);
13
+ pipe(chunks: AsyncIterable<Buffer>, { signal }: Link): AsyncGenerator<Buffer>;
14
+ }
15
+ //# sourceMappingURL=vanish.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vanish.d.ts","sourceRoot":"","sources":["../../src/toxics/vanish.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEvD,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED;;;;GAIG;AACH,qBAAa,MAAO,YAAW,KAAK;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB,YAAY,EAAE,MAAM,EAAE,EAAE,aAAa,EAEpC;IAGM,IAAI,CACT,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC,EAC7B,EAAE,MAAM,EAAE,EAAE,IAAI,GACf,cAAc,CAAC,MAAM,CAAC,CAMxB;CACF"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Drops each chunk, the end and the error of its input: the other side never
3
+ * learns that this side ended, as a gateway for a machine that is gone. Our
4
+ * own fault (mutex ADR 0035): a `Timeout` of 0 still forwards the end.
5
+ */
6
+ export class Vanish {
7
+ stream;
8
+ constructor({ stream }) {
9
+ this.stream = stream;
10
+ }
11
+ // eslint-disable-next-line require-yield -- each chunk drops
12
+ async *pipe(chunks, { signal }) {
13
+ try {
14
+ for await (const _chunk of chunks)
15
+ ;
16
+ // eslint-disable-next-line zukhruf/no-silent-catch -- the fault: no error of the side that is gone reaches the other side
17
+ }
18
+ catch { }
19
+ if (!signal.aborted)
20
+ await new Promise(() => { });
21
+ }
22
+ }
23
+ //# sourceMappingURL=vanish.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vanish.js","sourceRoot":"","sources":["../../src/toxics/vanish.ts"],"names":[],"mappings":"AAMA;;;;GAIG;AACH,MAAM,OAAO,MAAM;IACR,MAAM,CAAS;IAExB,YAAY,EAAE,MAAM,EAAiB;QACnC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAED,6DAA6D;IAC7D,KAAK,CAAC,CAAC,IAAI,CACT,MAA6B,EAC7B,EAAE,MAAM,EAAQ;QAEhB,IAAI,CAAC;YACH,IAAI,KAAK,EAAE,MAAM,MAAM,IAAI,MAAM;gBAAC,CAAC;YACnC,0HAA0H;QAC5H,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QACV,IAAI,CAAC,MAAM,CAAC,OAAO;YAAE,MAAM,IAAI,OAAO,CAAQ,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IAC1D,CAAC;CACF"}
@@ -0,0 +1,11 @@
1
+ import type { Clock } from '@zukhruf/async';
2
+ /**
3
+ * Waits `ms` on `clock`: resolves `true` at its end, and `false` at once when
4
+ * `signal` aborts, so a toxic gives what it holds with no more delay. A wait
5
+ * of 0 sets no timer, as Go's `time.After(0)`.
6
+ */
7
+ export declare function wait(ms: number, { clock, signal }: {
8
+ readonly clock: Clock;
9
+ readonly signal: AbortSignal;
10
+ }): Promise<boolean>;
11
+ //# sourceMappingURL=wait.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wait.d.ts","sourceRoot":"","sources":["../../src/toxics/wait.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAE5C;;;;GAIG;AACH,wBAAsB,IAAI,CACxB,EAAE,EAAE,MAAM,EACV,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAA;CAAE,GACzE,OAAO,CAAC,OAAO,CAAC,CAUlB"}
@@ -0,0 +1,22 @@
1
+ import { delay } from '@zukhruf/async';
2
+ /**
3
+ * Waits `ms` on `clock`: resolves `true` at its end, and `false` at once when
4
+ * `signal` aborts, so a toxic gives what it holds with no more delay. A wait
5
+ * of 0 sets no timer, as Go's `time.After(0)`.
6
+ */
7
+ export async function wait(ms, { clock, signal }) {
8
+ if (signal.aborted)
9
+ return false;
10
+ if (ms <= 0)
11
+ return true;
12
+ try {
13
+ await delay(ms, { clock, signal });
14
+ return true;
15
+ }
16
+ catch (error) {
17
+ if (error === signal.reason)
18
+ return false;
19
+ throw error;
20
+ }
21
+ }
22
+ //# sourceMappingURL=wait.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wait.js","sourceRoot":"","sources":["../../src/toxics/wait.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAGvC;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,IAAI,CACxB,EAAU,EACV,EAAE,KAAK,EAAE,MAAM,EAA2D;IAE1E,IAAI,MAAM,CAAC,OAAO;QAAE,OAAO,KAAK,CAAC;IACjC,IAAI,EAAE,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IACzB,IAAI,CAAC;QACH,MAAM,KAAK,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;QACnC,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,KAAK,MAAM,CAAC,MAAM;YAAE,OAAO,KAAK,CAAC;QAC1C,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}