@mark1russell7/rigor-proxy
v0.4.2
Published
A TCP proxy that applies a seeded fault plan to TCP, HTTP and WebSocket streams, and records each fault.
Maintainers
Readme
@mark1russell7/rigor-proxy
This package holds a TCP proxy that puts the faults of a plan into the streams between clients and a server. It records each fault that it applies.
A fault plan is data of @mark1russell7/rigor-chaos. A seed fixes the plan before the run. Thus a record keeps the plan, and a later run applies the same faults at the same points.
A proxy
import { faultPlan } from "@mark1russell7/rigor-chaos";
import { startFaultProxy } from "@mark1russell7/rigor-proxy";
const plan = faultPlan({ connections: 10, perConnection: 2, kinds: { split: 2, delay: 1, reset: 1 }, within: { frame: 20 } }, "drill");
const proxy = await startFaultProxy({ target: { port: 8080 }, plan, protocol: "websocket" });
// The client under test connects to proxy.port.
await proxy.close();- The proxy numbers the connections from 0, in the order that it accepts them. Connection
igets the faults of connectioniof the plan. recordsholds each fault that occurred: its time, its connection, its direction, its kind, and its byte and frame position.connectionsholds the summary of each connection that closed: how it ended, the bytes of each direction, and the faults that did not occur.onFaultandonCloseget the same data when it occurs.close()destroys each open connection. Its promise resolves after each connection closes, so thenconnectionshas the summary of each connection.clocksets the clock of the times and the timers. A test givesfakeClock()of@mark1russell7/algebra/clock, and it moves the time itself.
Windows
The proxy applies each fault of a window to each connection that is open at a time in the window. Thus a window of a plan makes an outage for each client, also for a client that keeps its connection open.
originis the time of the clock at the start of the plan. The windows count from it. The default is the time when the proxy starts.- Give the time of the start of your case as
origin. Then the windows of the plan and the samples of your case use the same times. - The record of a window fault has
window: true, and itsindexis the position in the listwindowsof the plan. - The summary of each connection has
opened, the start of the connection in ms from the start of the plan. - The faults of windows are not in
missed, because a window fault does not belong to one connection.
Frames
A fault with a frame point counts frames, and split, reorder and duplicate change one frame. The framers of protocol set what a frame is.
| protocol | A frame |
|---|---|
| tcp | Each read of the socket. |
| http | The head of a request or a response, each read of a body with a length, or each chunk of a chunked body. |
| websocket | The same as http. After the 101 response, each WebSocket frame. |
- A framer does not change a byte. When it cannot read the stream, it gives each read as one frame.
- The proxy works on TCP. It cannot read a TLS stream, thus a TLS stream has only
tcpframes. framersreplaces the framers ofprotocolwith your framers.
An answer
An answer puts an HTTP/1.1 response from the plan in place of the server. The proxy writes it to the client, and ends the connection with a FIN.
| Point | What the client and the server see |
|---|---|
| up, { frame: 0 } | The server does not get the request. The client gets the answer. |
| down, { frame: 0 } | The server gets the request. The client gets the answer in place of the response of the server. |
| down, a later frame | The client gets a part of the response of the server, then the answer. This stream is not valid HTTP. |
- The proxy writes the fields in their order, then
content-lengthandconnection: close. A 204 or a 304 has nocontent-length. - After an answer, no byte of the server goes to the client. The summary of the connection says
cut. answerBytesgives the bytes of an answer.
The pure parts
The framers (tcpFramers, httpFramers, webSocketFramer), the applier (applierOf) and answerBytes read no clock and write no socket. A test gives them bytes and times, and it examines the writes that they give back.
The entry @mark1russell7/rigor-proxy/applier has these parts and no module of Node.js. A browser can load it, for example to show the writes of a plan.
connectionOf holds the two appliers of one connection and the rules between the two directions. The proxy uses these rules, and a browser can use them with no socket:
- A stall stops the reads of its direction until the latest end of the stalls of that direction.
- A cut ends the two appliers. Each write that waits goes out, and then each target gets a FIN.
- A reset closes the connection at once. Each write with a time after the reset is lost.
- After an answer, no write of the server goes to the client.
read, tick and end give a step: the records, the writes of each direction, the pauses, the close and the lost writes.
The frame codec of RFC 6455 is also a pure part. readWebSocketFrame reads the first frame of some bytes, with its bits, its mask and its payload with no mask. encodeWebSocketFrame writes a frame, with the shortest form of the length. WEBSOCKET_OPCODES names the opcodes. The scripted server of @mark1russell7/rigor-testkit/http uses this codec for its WebSocket routes.
import { faultPlan } from "@mark1russell7/rigor-chaos";
import { applierOf, connectionOf } from "@mark1russell7/rigor-proxy/applier";
const plan = faultPlan({ connections: 1, perConnection: 2, kinds: { delay: 1, cut: 1 }, within: { frame: 8 } }, 7);
const applier = applierOf(plan, 0, "up");
const step = applier.push(new TextEncoder().encode("frame 0"), 0);
// step.sends has the bytes and the time of each write. step.records has each fault that the frame started.
const connection = connectionOf(plan, 0);
const both = connection.read("up", new TextEncoder().encode("frame 0"), 0);
// both.sends.up and both.sends.down have the writes of each direction. both.close is the close, if any.REASONS has the reason of each HTTP status, equal to STATUS_CODES of node:http in Node.js 24. Thus answerBytes gives the same bytes in each runtime.
Tests
The tests with real sockets measure time, thus only CI starts them, in the workflow proxy-net.yml. pnpm test starts only the tests of the pure parts.
