@cosyte/mllp 0.1.13 → 0.1.15
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/CHANGELOG.md +16 -0
- package/README.md +13 -8
- package/dist/ack-from-hl7/index.cjs +5 -2
- package/dist/ack-from-hl7/index.cjs.map +1 -1
- package/dist/ack-from-hl7/index.d.cts +2 -1
- package/dist/ack-from-hl7/index.d.ts +2 -1
- package/dist/ack-from-hl7/index.mjs +5 -2
- package/dist/ack-from-hl7/index.mjs.map +1 -1
- package/dist/arbitraries/index.cjs +6 -15
- package/dist/arbitraries/index.cjs.map +1 -1
- package/dist/arbitraries/index.d.cts +5 -4
- package/dist/arbitraries/index.d.ts +5 -4
- package/dist/arbitraries/index.mjs +6 -15
- package/dist/arbitraries/index.mjs.map +1 -1
- package/dist/index.cjs +364 -69
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +260 -46
- package/dist/index.d.ts +260 -46
- package/dist/index.mjs +364 -70
- package/dist/index.mjs.map +1 -1
- package/dist/testing/index.cjs.map +1 -1
- package/dist/testing/index.d.cts +2 -2
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.mjs.map +1 -1
- package/package.json +5 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/arbitraries/bytes.ts","../../src/arbitraries/messy.ts","../../src/arbitraries/hostile.ts"],"names":["fc","payloadBytes","frame","cuts","stream","payload"],"mappings":";;;;;;;;;AAcO,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,UAAA,GAAgC,CAAC,EAAA,EAAM,CAAA,EAAM,IAAI,EAAE,CAAA;AAczD,SAAS,SAAA,CAA4B,OAAU,IAAA,EAA0B;AAC9E,EAAA,MAAM,QAAA,GAAW,KAAK,KAAK,CAAA;AAC3B,EAAA,MAAA,CAAO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G,SAAA,EAAW,GAAG;AAAA,MAE9C,GAAA,CAAI,CAAC,CAAC,KAAA,EAAO,KAAK,MAAM,UAAA,CAAW,CAAC,GAAG,KAAA,CAAM,KAAK,CAAA,EAAG,KAAK,EAAE,KAAA,CAAM,CAAA,EAAG,SAAS,CAAC;AAAA,GACpF;AACA,EAAA,OAAO,KAAA,CAAM,KAAA,CAAM,CAAC,IAAA,KAAS;AAC3B,IAAA,MAAM,MAAA,GAAS,MAAA,CAAO,IAAA,CAAK,IAAI,CAAA;AAC/B,IAAA,OAAO,IAAA,CAAK,MAAA,CAAO,MAAA,EAAQ,OAAO,CAAA,CAAE,GAAA,CAAI,CAAC,EAAA,KAAO,eAAA,CAAgB,KAAA,CAAM,MAAA,EAAQ,EAAE,CAAC,CAAC,CAAA;AAAA,EACpF,CAAC,CAAA;AACH","file":"index.cjs","sourcesContent":["/**\n * Payloads, canonical frames, canonical byte streams and chunk splits: the valid half of\n * `@cosyte/mllp/arbitraries`.\n *\n * The framing bytes are written here from the MLLP specification (HL7 v2 Appendix C), not imported\n * from the codec, so a generated frame is an independent statement of what the wire carries and a\n * test that compares it with `encodeFrame` checks something.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\n/** Start of a frame, `0x0B`. */\nexport const VT = 0x0b;\n/** End of a frame's payload, `0x1C`. */\nexport const FS = 0x1c;\n/** Carriage return, `0x0D`: the byte after `FS`, and the HL7 segment separator. */\nexport const CR = 0x0d;\n/** Line feed, `0x0A`. */\nexport const LF = 0x0a;\n/** The bytes a lenient reader skips before a frame: space, tab, line feed and carriage return. */\nexport const WHITESPACE: readonly number[] = [0x20, 0x09, LF, CR];\n\n/**\n * Make a generated value safe from code that changes it. fast-check hands a property the value\n * itself the first time and a clone each time after, and reports the value itself when the\n * property fails. So a copy is taken here, before any code under test sees the value: every clone\n * is made from that copy, and the value prints as that copy, so a failure reports the case as it\n * was generated, whatever the code under test did to its bytes.\n *\n * @example\n * ```ts\n * const bytes = cloneable(Buffer.from(\"A\"), (b) => Buffer.from(b));\n * ```\n */\nexport function cloneable<T extends object>(value: T, copy: (value: T) => T): T {\n const pristine = copy(value);\n Object.defineProperty(value, fc.cloneMethod, {\n value: () => cloneable(copy(pristine), copy),\n });\n Object.defineProperty(value, fc.toStringMethod, { value: () => fc.stringify(pristine) });\n return value;\n}\n\n/** A copy of a list of buffers, each buffer copied too. */\nfunction copyBuffers(buffers: readonly Buffer[]): Buffer[] {\n return buffers.map((buffer) => Buffer.from(buffer));\n}\n\n/** A cloneable `Buffer` of the given bytes. */\nfunction cloneableBuffer(bytes: Uint8Array | readonly number[]): Buffer {\n return cloneable(Buffer.from(bytes), (buffer) => Buffer.from(buffer));\n}\n\n/** What a generated payload holds. */\nexport type PayloadContent = \"bytes\" | \"text\" | \"hl7\";\n\n/** Options for {@link payload}, and for the payloads inside frames and streams. */\nexport interface PayloadOptions {\n /**\n * What the payload holds.\n *\n * - `\"bytes\"` (the default): any byte except `VT` (`0x0B`) and `FS` (`0x1C`), the two bytes the\n * encoder refuses. `CR` and the other control bytes are in.\n * - `\"text\"`: printable ASCII with carriage returns, the shape of HL7 v2 text.\n * - `\"hl7\"`: a synthetic HL7 v2 message: an `MSH` segment with a generated control id in MSH-10,\n * then up to four more segments, each ended by a carriage return. `minLength` and `maxLength`\n * do not apply.\n */\n readonly content?: PayloadContent;\n /** The fewest bytes in a `\"bytes\"` or `\"text\"` payload. Default `1`. */\n readonly minLength?: number;\n /** The most bytes in a `\"bytes\"` or `\"text\"` payload. Default `256`. */\n readonly maxLength?: number;\n}\n\n/** Any byte except the two the encoder refuses in a payload. */\nconst PAYLOAD_BYTE = fc.integer({ min: 0, max: 0xff }).filter((b) => b !== VT && b !== FS);\n\n/** A printable ASCII byte, or a carriage return now and then. */\nconst TEXT_BYTE = fc.oneof(\n { weight: 15, arbitrary: fc.integer({ min: 0x20, max: 0x7e }) },\n { weight: 1, arbitrary: fc.constant(CR) },\n);\n\n/** A short run of letters and digits: the content of one synthetic HL7 field. */\nfunction word(maxLength: number): fc.Arbitrary<string> {\n return fc.string({\n unit: fc.constantFrom(...\"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"),\n maxLength,\n });\n}\n\n/** The message types a synthetic HL7 payload carries in MSH-9. */\nconst MESSAGE_TYPES = [\n \"ADT^A01\",\n \"ADT^A04\",\n \"ADT^A08\",\n \"ORM^O01\",\n \"ORU^R01\",\n \"SIU^S12\",\n \"MDM^T02\",\n \"DFT^P03\",\n \"ACK\",\n];\n\n/** The segments after MSH in a synthetic HL7 payload. None of them carries a patient. */\nconst SEGMENT_NAMES = [\"EVN\", \"PV1\", \"ORC\", \"OBR\", \"OBX\", \"NTE\", \"ZXX\"];\n\n/** One synthetic segment after MSH: a name, then fields of letters and digits with components. */\nconst SEGMENT = fc\n .tuple(\n fc.constantFrom(...SEGMENT_NAMES),\n fc.array(fc.array(word(8), { minLength: 1, maxLength: 3 }), { maxLength: 6 }),\n )\n .map(([name, fields]) => [name, ...fields.map((components) => components.join(\"^\"))].join(\"|\"));\n\n/** A synthetic HL7 v2 message, each segment ended by a carriage return. */\nconst HL7_TEXT = fc\n .record({\n sender: word(6),\n receiver: word(6),\n when: fc.date({\n min: new Date(\"2000-01-01T00:00:00Z\"),\n max: new Date(\"2039-12-31T23:59:59Z\"),\n noInvalidDate: true,\n }),\n type: fc.constantFrom(...MESSAGE_TYPES),\n controlId: word(20).filter((id) => id.length > 0),\n segments: fc.array(SEGMENT, { maxLength: 4 }),\n })\n .map(({ sender, receiver, when, type, controlId, segments }) => {\n const ts = when.toISOString().replace(/\\D/gu, \"\").slice(0, 14);\n const msh = `MSH|^~\\\\&|${sender}|FAC|${receiver}|FAC|${ts}||${type}|${controlId}|P|2.5`;\n return [msh, ...segments].map((segment) => `${segment}\\r`).join(\"\");\n });\n\n/**\n * The bytes of one payload, as a plain array, before it becomes a `Buffer`.\n *\n * @example\n * ```ts\n * const bytes = fc.sample(payloadBytes({ content: \"text\" }), 1)[0];\n * ```\n */\nexport function payloadBytes(options: PayloadOptions = {}): fc.Arbitrary<number[]> {\n const content = options.content ?? \"bytes\";\n if (content === \"hl7\") return HL7_TEXT.map((text) => [...Buffer.from(text, \"latin1\")]);\n return fc.array(content === \"text\" ? TEXT_BYTE : PAYLOAD_BYTE, {\n minLength: options.minLength ?? 1,\n maxLength: options.maxLength ?? 256,\n });\n}\n\n/**\n * A payload the encoder accepts: a `Buffer` with no `VT` (`0x0B`) and no `FS` (`0x1C`) in it.\n *\n * Framed with `encodeFrame` and read back with a `FrameReader`, every one comes back byte for\n * byte. With the default `minLength` of 1 it also comes back with no warning; an empty payload\n * comes back with `MLLP_EMPTY_PAYLOAD`.\n *\n * @param options - What the payload holds and how long it is.\n * @returns An arbitrary of payload buffers.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { encodeFrame } from \"@cosyte/mllp\";\n * import { payload } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(fc.property(payload(), (p) => encodeFrame(p).length === p.length + 3));\n * ```\n */\nexport function payload(options: PayloadOptions = {}): fc.Arbitrary<Buffer> {\n return payloadBytes(options).map(cloneableBuffer);\n}\n\n/** A canonical frame and the payload it carries. */\nexport interface GeneratedFrame {\n /** The payload: what a reader must deliver for these bytes. */\n readonly payload: Buffer;\n /** The frame as it goes on the wire: `VT`, the payload, `FS`, `CR`. */\n readonly bytes: Buffer;\n}\n\n/** The canonical frame of a payload, written byte by byte. */\nfunction frameBytes(payloadBytes: readonly number[]): number[] {\n return [VT, ...payloadBytes, FS, CR];\n}\n\n/** Copy a frame, both buffers. */\nfunction copyFrame(frame: GeneratedFrame): GeneratedFrame {\n return { payload: Buffer.from(frame.payload), bytes: Buffer.from(frame.bytes) };\n}\n\n/**\n * A canonical MLLP frame, `VT` + payload + `FS` + `CR`, with the payload it carries.\n *\n * The bytes are written from the MLLP specification, not by `encodeFrame`, so they are an\n * independent check on the encoder: `encodeFrame(f.payload)` equals `f.bytes`.\n *\n * @param options - The payload's options, as for {@link payload}.\n * @returns An arbitrary of frames.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { encodeFrame } from \"@cosyte/mllp\";\n * import { frame } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(fc.property(frame(), (f) => encodeFrame(f.payload).equals(f.bytes)));\n * ```\n */\nexport function frame(options: PayloadOptions = {}): fc.Arbitrary<GeneratedFrame> {\n return payloadBytes(options).map((bytes) =>\n cloneable<GeneratedFrame>(\n { payload: Buffer.from(bytes), bytes: Buffer.from(frameBytes(bytes)) },\n copyFrame,\n ),\n );\n}\n\n/** Options for {@link chunked}. */\nexport interface ChunkOptions {\n /** The most places the bytes are cut. Default `16`. Two cuts in one place leave an empty chunk. */\n readonly maxCuts?: number;\n}\n\n/**\n * Cut `bytes` at the given offsets, sorted; repeated offsets leave empty chunks.\n *\n * @example\n * ```ts\n * cutAt(Buffer.from(\"ABC\"), [2, 1]); // A, B, C\n * ```\n */\nexport function cutAt(bytes: Buffer, cuts: readonly number[]): Buffer[] {\n const sorted = [...cuts].sort((a, b) => a - b);\n const chunks: Buffer[] = [];\n let start = 0;\n for (const cut of sorted) {\n chunks.push(Buffer.from(bytes.subarray(start, cut)));\n start = cut;\n }\n chunks.push(Buffer.from(bytes.subarray(start)));\n return chunks;\n}\n\n/**\n * A cloneable list of chunks.\n *\n * @example\n * ```ts\n * const chunks = cloneableChunks([Buffer.from(\"A\")]);\n * ```\n */\nexport function cloneableChunks(chunks: Buffer[]): Buffer[] {\n return cloneable(chunks, copyBuffers);\n}\n\n/**\n * The cut offsets for a run of `length` bytes.\n *\n * @example\n * ```ts\n * const at = fc.sample(cuts(10), 1)[0];\n * ```\n */\nexport function cuts(length: number, options: ChunkOptions = {}): fc.Arbitrary<number[]> {\n return fc.array(fc.integer({ min: 0, max: length }), { maxLength: options.maxCuts ?? 16 });\n}\n\n/**\n * The given bytes split into chunks at random places, the way a socket may deliver them: a frame\n * across many reads, many frames in one read, and now and then an empty read. Concatenated, the\n * chunks are always the bytes given. A case shrinks toward one chunk.\n *\n * @param bytes - The bytes to split.\n * @param options - How many cuts at most.\n * @returns An arbitrary of chunk lists.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { chunked } from \"@cosyte/mllp/arbitraries\";\n *\n * const bytes = Buffer.from(\"ABCDEF\");\n * fc.assert(fc.property(chunked(bytes), (chunks) => Buffer.concat(chunks).equals(bytes)));\n * ```\n */\nexport function chunked(bytes: Buffer, options: ChunkOptions = {}): fc.Arbitrary<Buffer[]> {\n const source = Buffer.from(bytes);\n return cuts(source.length, options).map((at) => cloneableChunks(cutAt(source, at)));\n}\n\n/** Options for {@link stream}. */\nexport interface StreamOptions extends PayloadOptions, ChunkOptions {\n /** The fewest frames in the stream. Default `1`. */\n readonly minFrames?: number;\n /** The most frames in the stream. Default `5`. */\n readonly maxFrames?: number;\n}\n\n/** A canonical byte stream: frames back to back, as chunks, with the payloads it carries. */\nexport interface GeneratedStream {\n /** The payloads in order: what a reader must deliver for this stream. */\n readonly payloads: readonly Buffer[];\n /** The whole stream: each payload's canonical frame, back to back. */\n readonly bytes: Buffer;\n /** The same bytes split at random places, the reads a socket may deliver. */\n readonly chunks: readonly Buffer[];\n}\n\n/**\n * Copy a stream, every buffer in it.\n *\n * @example\n * ```ts\n * const copy = copyStream(generated);\n * ```\n */\nexport function copyStream<T extends GeneratedStream>(stream: T): T {\n return {\n ...stream,\n payloads: copyBuffers(stream.payloads),\n bytes: Buffer.from(stream.bytes),\n chunks: copyBuffers(stream.chunks),\n };\n}\n\n/**\n * A canonical MLLP byte stream: one to five frames back to back, split into chunks at random\n * places, with the payloads a reader must deliver from it, in order and with no warning.\n *\n * @param options - The payloads' options, how many frames, and how many cuts at most.\n * @returns An arbitrary of streams.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader } from \"@cosyte/mllp\";\n * import { stream } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(\n * fc.property(stream(), (s) => {\n * const got: Buffer[] = [];\n * const reader = new FrameReader({ onFrame: (p) => got.push(p) });\n * for (const chunk of s.chunks) reader.push(chunk);\n * return got.length === s.payloads.length;\n * }),\n * );\n * ```\n */\nexport function stream(options: StreamOptions = {}): fc.Arbitrary<GeneratedStream> {\n return fc\n .array(payloadBytes(options), {\n minLength: options.minFrames ?? 1,\n maxLength: options.maxFrames ?? 5,\n })\n .chain((payloads) => {\n const bytes = Buffer.from(payloads.flatMap(frameBytes));\n return cuts(bytes.length, options).map((at) =>\n cloneable<GeneratedStream>(\n {\n payloads: payloads.map((p) => Buffer.from(p)),\n bytes,\n chunks: cutAt(bytes, at),\n },\n copyStream,\n ),\n );\n });\n}\n","/**\n * Messy streams a lenient reader accepts: the framing quirks real senders produce, each with the\n * payload and the warnings a `FrameReader` must report for it.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\nimport type { WarningCode } from \"../framing/registry.js\";\n\nimport {\n CR,\n FS,\n LF,\n VT,\n WHITESPACE,\n cloneable,\n copyStream,\n cutAt,\n cuts,\n payloadBytes,\n type GeneratedStream,\n type StreamOptions,\n} from \"./bytes.js\";\n\n/**\n * Options for {@link messyStream}: the payloads, the frames and the cuts as for a canonical stream,\n * and the tolerances of the reader the stream is meant for. A quirk whose tolerance is `false` is\n * never generated, so pass the same flags your `FrameReader` or server has. Each defaults to `true`.\n */\nexport interface MessyStreamOptions extends StreamOptions {\n /** Generate `FS` with no `CR` after it. */\n readonly allowFsOnly?: boolean;\n /** Generate `FS` then `LF` in place of `FS` then `CR`. */\n readonly allowLfAfterFs?: boolean;\n /** Generate a frame that opens with no `VT`. */\n readonly allowMissingLeadingVt?: boolean;\n /** Generate spaces, tabs, line feeds and carriage returns before a frame. */\n readonly allowLeadingWhitespace?: boolean;\n}\n\n/** A messy stream, with what a reader with the matching tolerances must deliver for it. */\nexport interface MessyStream extends GeneratedStream {\n /**\n * The warning codes each delivered frame must carry, in the order they are raised, one list per\n * payload. A frame with no quirk has an empty list.\n */\n readonly warnings: readonly (readonly WarningCode[])[];\n}\n\n/** How a frame opens. */\ntype Opening = \"vt\" | \"whitespace\" | \"no-vt\" | \"whitespace-no-vt\";\n/** What sits between the opening and the payload. */\ntype Body = \"plain\" | \"empty\" | \"discarded\";\n/** How a frame ends. */\ntype Ending = \"cr\" | \"lf\" | \"fs-only\" | \"stray\";\n\n/** One frame's choices, before the stream fixes them up so that they read as intended. */\ninterface FrameChoice {\n readonly opening: Opening;\n readonly body: Body;\n readonly ending: Ending;\n readonly whitespace: readonly number[];\n readonly discarded: readonly number[];\n readonly payload: readonly number[];\n readonly stray: number;\n}\n\n/** The parts of a messy stream built so far. */\ninterface Built {\n readonly bytes: number[];\n readonly payloads: Buffer[];\n readonly warnings: WarningCode[][];\n}\n\n/** The openings, bodies and endings the given tolerances accept. */\nfunction quirks(options: MessyStreamOptions): {\n openings: Opening[];\n endings: Ending[];\n} {\n const ws = options.allowLeadingWhitespace !== false;\n const noVt = options.allowMissingLeadingVt !== false;\n const openings: Opening[] = [\"vt\"];\n if (ws) openings.push(\"whitespace\");\n if (noVt) openings.push(\"no-vt\");\n if (ws && noVt) openings.push(\"whitespace-no-vt\");\n const endings: Ending[] = [\"cr\"];\n if (options.allowLfAfterFs !== false) endings.push(\"lf\");\n if (options.allowFsOnly !== false) endings.push(\"fs-only\", \"stray\");\n return { openings, endings };\n}\n\n/** One frame's random choices, within the given tolerances. */\nfunction frameChoice(options: MessyStreamOptions): fc.Arbitrary<FrameChoice> {\n const { openings, endings } = quirks(options);\n return fc.record({\n opening: fc.constantFrom(...openings),\n body: fc.constantFrom<Body>(\"plain\", \"empty\", \"discarded\"),\n ending: fc.constantFrom(...endings),\n whitespace: fc.array(fc.constantFrom(...WHITESPACE), { minLength: 1, maxLength: 4 }),\n discarded: payloadBytes({ ...options, minLength: 0, maxLength: 16 }),\n payload: payloadBytes(options),\n // After FS, any byte but CR and LF (which end the frame properly) and VT (which opens the next).\n stray: fc.integer({ min: 0, max: 0xff }).filter((b) => b !== CR && b !== LF && b !== VT),\n });\n}\n\n/**\n * Append one frame to the stream, fixing its choices up where they would read otherwise:\n *\n * - a frame after an `fs-only` ending opens with its `VT`, which is what ends the frame before;\n * - a frame with no `VT` cannot open on whitespace a reader skips, so such a payload gets its `VT`;\n * - a frame with no `VT` has nothing to discard, since discarding starts at a `VT`;\n * - the last frame does not end `fs-only`, which only the next frame's `VT` completes.\n */\nfunction append(built: Built, choice: FrameChoice, mustOpenWithVt: boolean, last: boolean): void {\n const payload = choice.body === \"empty\" ? [] : [...choice.payload];\n let opening = mustOpenWithVt ? \"vt\" : choice.opening;\n const noVt = opening === \"no-vt\" || opening === \"whitespace-no-vt\";\n const first = payload[0];\n if (\n noVt &&\n (choice.body === \"discarded\" || (first !== undefined && WHITESPACE.includes(first)))\n ) {\n opening = opening === \"no-vt\" ? \"vt\" : \"whitespace\";\n }\n const ending = last && choice.ending === \"fs-only\" ? \"cr\" : choice.ending;\n\n const warnings: WarningCode[] = [];\n if (opening === \"whitespace\" || opening === \"whitespace-no-vt\") {\n built.bytes.push(...choice.whitespace);\n warnings.push(\"MLLP_LEADING_WHITESPACE\");\n }\n if (opening === \"no-vt\" || opening === \"whitespace-no-vt\") {\n warnings.push(\"MLLP_MISSING_LEADING_VT\");\n } else if (!mustOpenWithVt) {\n built.bytes.push(VT);\n }\n if (choice.body === \"discarded\") {\n built.bytes.push(...choice.discarded, VT);\n warnings.push(\"MLLP_TRAILING_BYTES\");\n }\n built.bytes.push(...payload, FS);\n if (payload.length === 0) warnings.push(\"MLLP_EMPTY_PAYLOAD\");\n if (ending === \"cr\") built.bytes.push(CR);\n else if (ending === \"lf\") {\n built.bytes.push(LF);\n warnings.push(\"MLLP_LF_AFTER_FS\");\n } else {\n // `fs-only` is ended by the next frame's VT, which that frame writes as its opening.\n if (ending === \"stray\") built.bytes.push(choice.stray);\n else built.bytes.push(VT);\n warnings.push(\"MLLP_FS_WITHOUT_CR\");\n }\n built.payloads.push(Buffer.from(payload));\n built.warnings.push(warnings);\n}\n\n/** Build a messy stream's bytes, payloads and warnings from its frames' choices. */\nfunction buildMessy(choices: readonly FrameChoice[]): Built {\n const built: Built = { bytes: [], payloads: [], warnings: [] };\n let mustOpenWithVt = false;\n choices.forEach((choice, index) => {\n const last = index === choices.length - 1;\n append(built, choice, mustOpenWithVt, last);\n mustOpenWithVt = !last && choice.ending === \"fs-only\";\n });\n return built;\n}\n\n/**\n * A messy MLLP byte stream a lenient reader accepts, split into chunks at random places, with the\n * payloads it must deliver and the warnings each must carry.\n *\n * Each frame may open with spaces, tabs, line feeds or carriage returns before its `VT`, or with no\n * `VT` at all; carry an empty payload; carry bytes before a second `VT` that the reader discards\n * (`MLLP_TRAILING_BYTES`, the payload delivered is what follows that `VT`); and end with `FS` and\n * `LF`, with `FS` and the next frame's `VT`, or with `FS` and a stray byte the reader drops. Give it\n * the tolerances your reader has, and it generates only what that reader accepts.\n *\n * @param options - The payloads, frames, cuts and tolerances.\n * @returns An arbitrary of messy streams.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader } from \"@cosyte/mllp\";\n * import { messyStream } from \"@cosyte/mllp/arbitraries\";\n *\n * const tolerances = { allowFsOnly: true, allowLfAfterFs: true, allowLeadingWhitespace: true };\n * fc.assert(\n * fc.property(messyStream({ ...tolerances, allowMissingLeadingVt: false }), (s) => {\n * const got: Buffer[] = [];\n * const reader = new FrameReader({ ...tolerances, onFrame: (p) => got.push(p) });\n * for (const chunk of s.chunks) reader.push(chunk);\n * return got.length === s.payloads.length;\n * }),\n * );\n * ```\n */\nexport function messyStream(options: MessyStreamOptions = {}): fc.Arbitrary<MessyStream> {\n return fc\n .array(frameChoice(options), {\n minLength: options.minFrames ?? 1,\n maxLength: options.maxFrames ?? 5,\n })\n .chain((choices) => {\n const built = buildMessy(choices);\n const bytes = Buffer.from(built.bytes);\n return cuts(bytes.length, options).map((at) =>\n cloneable<MessyStream>(\n {\n payloads: built.payloads.map((p) => Buffer.from(p)),\n warnings: built.warnings.map((codes) => [...codes]),\n bytes,\n chunks: cutAt(bytes, at),\n },\n copyStream,\n ),\n );\n });\n}\n","/**\n * Hostile byte streams: what a reader may meet from a broken sender, a port scanner or an attacker.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\nimport { CR, FS, LF, VT, WHITESPACE, cloneableChunks, cutAt, cuts } from \"./bytes.js\";\nimport { messyStream } from \"./messy.js\";\n\n/** Options for {@link hostileInput}. */\nexport interface HostileInputOptions {\n /** The most bytes in the stream. Default `512`. */\n readonly maxLength?: number;\n /** The most places the stream is cut into chunks. Default `16`. */\n readonly maxCuts?: number;\n}\n\n/** A byte that is a framing byte or whitespace one time in two, and any byte otherwise. */\nconst FRAMING_HEAVY_BYTE = fc.oneof(\n fc.constantFrom(VT, FS, CR, LF, ...WHITESPACE),\n fc.integer({ min: 0, max: 0xff }),\n);\n\n/** One edit to a stream. `at` is taken modulo the stream's length at the time. */\ninterface Edit {\n readonly op: \"insert\" | \"delete\" | \"replace\" | \"truncate\" | \"repeat\";\n readonly at: number;\n readonly byte: number;\n}\n\nconst EDIT: fc.Arbitrary<Edit> = fc.record({\n op: fc.constantFrom<Edit[\"op\"]>(\"insert\", \"delete\", \"replace\", \"truncate\", \"repeat\"),\n at: fc.nat(),\n byte: FRAMING_HEAVY_BYTE,\n});\n\n/** Apply edits to a stream's bytes, in order. */\nfunction applyEdits(bytes: readonly number[], edits: readonly Edit[]): number[] {\n const out = [...bytes];\n for (const { op, at, byte } of edits) {\n const i = at % (out.length + 1);\n if (op === \"insert\") out.splice(i, 0, byte);\n else if (op === \"truncate\") out.length = i;\n else if (op === \"repeat\") out.splice(i, 0, ...out.slice(i, i + 8));\n else if (i < out.length) {\n if (op === \"delete\") out.splice(i, 1);\n else out[i] = byte;\n }\n }\n return out;\n}\n\n/**\n * A hostile MLLP byte stream as the chunks a socket delivers: random bytes, bytes thick with\n * `VT`, `FS`, `CR`, `LF` and whitespace, and messy streams with bytes inserted, deleted, replaced,\n * repeated and cut off. A reader may refuse one, but it must not hang, crash or throw anything it\n * does not document.\n *\n * @param options - The most bytes, and the most cuts.\n * @returns An arbitrary of chunk lists.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader, MllpFramingError } from \"@cosyte/mllp\";\n * import { hostileInput } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(\n * fc.property(hostileInput(), (chunks) => {\n * const reader = new FrameReader({ onFrame: () => {} });\n * try {\n * for (const chunk of chunks) reader.push(chunk);\n * } catch (err) {\n * if (!(err instanceof MllpFramingError)) throw err;\n * }\n * }),\n * );\n * ```\n */\nexport function hostileInput(options: HostileInputOptions = {}): fc.Arbitrary<Buffer[]> {\n const maxLength = options.maxLength ?? 512;\n const bytes = fc.oneof(\n fc.array(fc.integer({ min: 0, max: 0xff }), { maxLength }),\n fc.array(FRAMING_HEAVY_BYTE, { maxLength }),\n fc\n .tuple(\n messyStream({ content: \"text\", maxLength: 32 }),\n fc.array(EDIT, { minLength: 1, maxLength: 4 }),\n )\n .map(([messy, edits]) => applyEdits([...messy.bytes], edits).slice(0, maxLength)),\n );\n return bytes.chain((list) => {\n const buffer = Buffer.from(list);\n return cuts(buffer.length, options).map((at) => cloneableChunks(cutAt(buffer, at)));\n });\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/arbitraries/bytes.ts","../../src/arbitraries/messy.ts","../../src/arbitraries/hostile.ts"],"names":["fc","payloadBytes","frame","cuts","stream","payload"],"mappings":";;;;;;;;;AAcO,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,UAAA,GAAgC,CAAC,EAAA,EAAM,CAAA,EAAM,IAAI,EAAE,CAAA;AAczD,SAAS,SAAA,CAA4B,OAAU,IAAA,EAA0B;AAC9E,EAAA,MAAM,QAAA,GAAW,KAAK,KAAK,CAAA;AAC3B,EAAA,MAAA,CAAO,cAAA,CAAe,KAAA,EAAOA,mBAAA,CAAG,WAAA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AACvC,EAAA,MAAM,QAAQA,mBAAAA,CAAG,KAAA;AAAA,IACfA,mBAAAA,CAAG,KAAA,CAAMA,mBAAAA,CAAG,OAAA,CAAQ,EAAE,GAAA,EAAK,CAAA,EAAG,GAAA,EAAK,GAAA,EAAM,CAAA,EAAG,EAAE,WAAW,CAAA;AAAA,IACzDA,mBAAAA,CAAG,KAAA,CAAM,kBAAA,EAAoB,EAAE,WAAW,CAAA;AAAA,IAC1CA,mBAAAA,CACG,KAAA;AAAA,MACC,YAAY,EAAE,OAAA,EAAS,MAAA,EAAQ,SAAA,EAAW,IAAI,CAAA;AAAA,MAC9CA,mBAAAA,CAAG,MAAM,IAAA,EAAM,EAAE,WAAW,CAAA,EAAG,SAAA,EAAW,GAAG;AAAA,MAE9C,GAAA,CAAI,CAAC,CAAC,KAAA,EAAO,KAAK,MAAM,UAAA,CAAW,CAAC,GAAG,KAAA,CAAM,KAAK,CAAA,EAAG,KAAK,EAAE,KAAA,CAAM,CAAA,EAAG,SAAS,CAAC;AAAA,GACpF;AACA,EAAA,OAAO,KAAA,CAAM,KAAA,CAAM,CAAC,IAAA,KAAS;AAC3B,IAAA,MAAM,MAAA,GAAS,MAAA,CAAO,IAAA,CAAK,IAAI,CAAA;AAC/B,IAAA,OAAO,IAAA,CAAK,MAAA,CAAO,MAAA,EAAQ,OAAO,CAAA,CAAE,GAAA,CAAI,CAAC,EAAA,KAAO,eAAA,CAAgB,KAAA,CAAM,MAAA,EAAQ,EAAE,CAAC,CAAC,CAAA;AAAA,EACpF,CAAC,CAAA;AACH","file":"index.cjs","sourcesContent":["/**\n * Payloads, canonical frames, canonical byte streams and chunk splits: the valid half of\n * `@cosyte/mllp/arbitraries`.\n *\n * The framing bytes are written here from the MLLP specification (HL7 v2 Appendix C), not imported\n * from the codec, so a generated frame is an independent statement of what the wire carries and a\n * test that compares it with `encodeFrame` checks something.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\n/** Start of a frame, `0x0B`. */\nexport const VT = 0x0b;\n/** End of a frame's payload, `0x1C`. */\nexport const FS = 0x1c;\n/** Carriage return, `0x0D`: the byte after `FS`, and the HL7 segment separator. */\nexport const CR = 0x0d;\n/** Line feed, `0x0A`. */\nexport const LF = 0x0a;\n/** The bytes a lenient reader skips before a frame: space, tab, line feed and carriage return. */\nexport const WHITESPACE: readonly number[] = [0x20, 0x09, LF, CR];\n\n/**\n * Make a generated value safe from code that changes it. fast-check hands a property the value\n * itself the first time and a clone each time after, and reports the value itself when the\n * property fails. So a copy is taken here, before any code under test sees the value: every clone\n * is made from that copy, and the value prints as that copy, so a failure reports the case as it\n * was generated, whatever the code under test did to its bytes.\n *\n * @example\n * ```ts\n * const bytes = cloneable(Buffer.from(\"A\"), (b) => Buffer.from(b));\n * ```\n */\nexport function cloneable<T extends object>(value: T, copy: (value: T) => T): T {\n const pristine = copy(value);\n Object.defineProperty(value, fc.cloneMethod, {\n value: () => cloneable(copy(pristine), copy),\n });\n Object.defineProperty(value, fc.toStringMethod, { value: () => fc.stringify(pristine) });\n return value;\n}\n\n/** A copy of a list of buffers, each buffer copied too. */\nfunction copyBuffers(buffers: readonly Buffer[]): Buffer[] {\n return buffers.map((buffer) => Buffer.from(buffer));\n}\n\n/** A cloneable `Buffer` of the given bytes. */\nfunction cloneableBuffer(bytes: Uint8Array | readonly number[]): Buffer {\n return cloneable(Buffer.from(bytes), (buffer) => Buffer.from(buffer));\n}\n\n/** What a generated payload holds. */\nexport type PayloadContent = \"bytes\" | \"text\" | \"hl7\";\n\n/** Options for {@link payload}, and for the payloads inside frames and streams. */\nexport interface PayloadOptions {\n /**\n * What the payload holds.\n *\n * - `\"bytes\"` (the default): any byte except `VT` (`0x0B`) and `FS` (`0x1C`), the two bytes the\n * encoder refuses. `CR` and the other control bytes are in.\n * - `\"text\"`: printable ASCII with carriage returns, the shape of HL7 v2 text.\n * - `\"hl7\"`: a synthetic HL7 v2 message: an `MSH` segment with a generated control id in MSH-10,\n * then up to four more segments, each ended by a carriage return. `minLength` and `maxLength`\n * do not apply.\n */\n readonly content?: PayloadContent;\n /** The fewest bytes in a `\"bytes\"` or `\"text\"` payload. Default `1`. */\n readonly minLength?: number;\n /** The most bytes in a `\"bytes\"` or `\"text\"` payload. Default `256`. */\n readonly maxLength?: number;\n}\n\n/** Any byte except the two the encoder refuses in a payload. */\nconst PAYLOAD_BYTE = fc.integer({ min: 0, max: 0xff }).filter((b) => b !== VT && b !== FS);\n\n/** A printable ASCII byte, or a carriage return now and then. */\nconst TEXT_BYTE = fc.oneof(\n { weight: 15, arbitrary: fc.integer({ min: 0x20, max: 0x7e }) },\n { weight: 1, arbitrary: fc.constant(CR) },\n);\n\n/** A short run of letters and digits: the content of one synthetic HL7 field. */\nfunction word(maxLength: number): fc.Arbitrary<string> {\n return fc.string({\n unit: fc.constantFrom(...\"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"),\n maxLength,\n });\n}\n\n/** The message types a synthetic HL7 payload carries in MSH-9. */\nconst MESSAGE_TYPES = [\n \"ADT^A01\",\n \"ADT^A04\",\n \"ADT^A08\",\n \"ORM^O01\",\n \"ORU^R01\",\n \"SIU^S12\",\n \"MDM^T02\",\n \"DFT^P03\",\n \"ACK\",\n];\n\n/** The segments after MSH in a synthetic HL7 payload. None of them carries a patient. */\nconst SEGMENT_NAMES = [\"EVN\", \"PV1\", \"ORC\", \"OBR\", \"OBX\", \"NTE\", \"ZXX\"];\n\n/** One synthetic segment after MSH: a name, then fields of letters and digits with components. */\nconst SEGMENT = fc\n .tuple(\n fc.constantFrom(...SEGMENT_NAMES),\n fc.array(fc.array(word(8), { minLength: 1, maxLength: 3 }), { maxLength: 6 }),\n )\n .map(([name, fields]) => [name, ...fields.map((components) => components.join(\"^\"))].join(\"|\"));\n\n/** A synthetic HL7 v2 message, each segment ended by a carriage return. */\nconst HL7_TEXT = fc\n .record({\n sender: word(6),\n receiver: word(6),\n when: fc.date({\n min: new Date(\"2000-01-01T00:00:00Z\"),\n max: new Date(\"2039-12-31T23:59:59Z\"),\n noInvalidDate: true,\n }),\n type: fc.constantFrom(...MESSAGE_TYPES),\n controlId: word(20).filter((id) => id.length > 0),\n segments: fc.array(SEGMENT, { maxLength: 4 }),\n })\n .map(({ sender, receiver, when, type, controlId, segments }) => {\n const ts = when.toISOString().replace(/\\D/gu, \"\").slice(0, 14);\n const msh = `MSH|^~\\\\&|${sender}|FAC|${receiver}|FAC|${ts}||${type}|${controlId}|P|2.5`;\n return [msh, ...segments].map((segment) => `${segment}\\r`).join(\"\");\n });\n\n/**\n * The bytes of one payload, as a plain array, before it becomes a `Buffer`.\n *\n * @example\n * ```ts\n * const bytes = fc.sample(payloadBytes({ content: \"text\" }), 1)[0];\n * ```\n */\nexport function payloadBytes(options: PayloadOptions = {}): fc.Arbitrary<number[]> {\n const content = options.content ?? \"bytes\";\n if (content === \"hl7\") return HL7_TEXT.map((text) => [...Buffer.from(text, \"latin1\")]);\n return fc.array(content === \"text\" ? TEXT_BYTE : PAYLOAD_BYTE, {\n minLength: options.minLength ?? 1,\n maxLength: options.maxLength ?? 256,\n });\n}\n\n/**\n * A payload the encoder accepts: a `Buffer` with no `VT` (`0x0B`) and no `FS` (`0x1C`) in it.\n *\n * Framed with `encodeFrame` and read back with a `FrameReader`, every one comes back byte for\n * byte. With the default `minLength` of 1 it also comes back with no warning; an empty payload\n * comes back with `MLLP_EMPTY_PAYLOAD`.\n *\n * @param options - What the payload holds and how long it is.\n * @returns An arbitrary of payload buffers.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { encodeFrame } from \"@cosyte/mllp\";\n * import { payload } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(fc.property(payload(), (p) => encodeFrame(p).length === p.length + 3));\n * ```\n */\nexport function payload(options: PayloadOptions = {}): fc.Arbitrary<Buffer> {\n return payloadBytes(options).map(cloneableBuffer);\n}\n\n/** A canonical frame and the payload it carries. */\nexport interface GeneratedFrame {\n /** The payload: what a reader must deliver for these bytes. */\n readonly payload: Buffer;\n /** The frame as it goes on the wire: `VT`, the payload, `FS`, `CR`. */\n readonly bytes: Buffer;\n}\n\n/** The canonical frame of a payload, written byte by byte. */\nfunction frameBytes(payloadBytes: readonly number[]): number[] {\n return [VT, ...payloadBytes, FS, CR];\n}\n\n/** Copy a frame, both buffers. */\nfunction copyFrame(frame: GeneratedFrame): GeneratedFrame {\n return { payload: Buffer.from(frame.payload), bytes: Buffer.from(frame.bytes) };\n}\n\n/**\n * A canonical MLLP frame, `VT` + payload + `FS` + `CR`, with the payload it carries.\n *\n * The bytes are written from the MLLP specification, not by `encodeFrame`, so they are an\n * independent check on the encoder: `encodeFrame(f.payload)` equals `f.bytes`.\n *\n * @param options - The payload's options, as for {@link payload}.\n * @returns An arbitrary of frames.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { encodeFrame } from \"@cosyte/mllp\";\n * import { frame } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(fc.property(frame(), (f) => encodeFrame(f.payload).equals(f.bytes)));\n * ```\n */\nexport function frame(options: PayloadOptions = {}): fc.Arbitrary<GeneratedFrame> {\n return payloadBytes(options).map((bytes) =>\n cloneable<GeneratedFrame>(\n { payload: Buffer.from(bytes), bytes: Buffer.from(frameBytes(bytes)) },\n copyFrame,\n ),\n );\n}\n\n/** Options for {@link chunked}. */\nexport interface ChunkOptions {\n /** The most places the bytes are cut. Default `16`. Two cuts in one place leave an empty chunk. */\n readonly maxCuts?: number;\n}\n\n/**\n * Cut `bytes` at the given offsets, sorted; repeated offsets leave empty chunks.\n *\n * @example\n * ```ts\n * cutAt(Buffer.from(\"ABC\"), [2, 1]); // A, B, C\n * ```\n */\nexport function cutAt(bytes: Buffer, cuts: readonly number[]): Buffer[] {\n const sorted = [...cuts].sort((a, b) => a - b);\n const chunks: Buffer[] = [];\n let start = 0;\n for (const cut of sorted) {\n chunks.push(Buffer.from(bytes.subarray(start, cut)));\n start = cut;\n }\n chunks.push(Buffer.from(bytes.subarray(start)));\n return chunks;\n}\n\n/**\n * A cloneable list of chunks.\n *\n * @example\n * ```ts\n * const chunks = cloneableChunks([Buffer.from(\"A\")]);\n * ```\n */\nexport function cloneableChunks(chunks: Buffer[]): Buffer[] {\n return cloneable(chunks, copyBuffers);\n}\n\n/**\n * The cut offsets for a run of `length` bytes.\n *\n * @example\n * ```ts\n * const at = fc.sample(cuts(10), 1)[0];\n * ```\n */\nexport function cuts(length: number, options: ChunkOptions = {}): fc.Arbitrary<number[]> {\n return fc.array(fc.integer({ min: 0, max: length }), { maxLength: options.maxCuts ?? 16 });\n}\n\n/**\n * The given bytes split into chunks at random places, the way a socket may deliver them: a frame\n * across many reads, many frames in one read, and now and then an empty read. Concatenated, the\n * chunks are always the bytes given. A case shrinks toward one chunk.\n *\n * @param bytes - The bytes to split.\n * @param options - How many cuts at most.\n * @returns An arbitrary of chunk lists.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { chunked } from \"@cosyte/mllp/arbitraries\";\n *\n * const bytes = Buffer.from(\"ABCDEF\");\n * fc.assert(fc.property(chunked(bytes), (chunks) => Buffer.concat(chunks).equals(bytes)));\n * ```\n */\nexport function chunked(bytes: Buffer, options: ChunkOptions = {}): fc.Arbitrary<Buffer[]> {\n const source = Buffer.from(bytes);\n return cuts(source.length, options).map((at) => cloneableChunks(cutAt(source, at)));\n}\n\n/** Options for {@link stream}. */\nexport interface StreamOptions extends PayloadOptions, ChunkOptions {\n /** The fewest frames in the stream. Default `1`. */\n readonly minFrames?: number;\n /** The most frames in the stream. Default `5`. */\n readonly maxFrames?: number;\n}\n\n/** A canonical byte stream: frames back to back, as chunks, with the payloads it carries. */\nexport interface GeneratedStream {\n /** The payloads in order: what a reader must deliver for this stream. */\n readonly payloads: readonly Buffer[];\n /** The whole stream: each payload's canonical frame, back to back. */\n readonly bytes: Buffer;\n /** The same bytes split at random places, the reads a socket may deliver. */\n readonly chunks: readonly Buffer[];\n}\n\n/**\n * Copy a stream, every buffer in it.\n *\n * @example\n * ```ts\n * const copy = copyStream(generated);\n * ```\n */\nexport function copyStream<T extends GeneratedStream>(stream: T): T {\n return {\n ...stream,\n payloads: copyBuffers(stream.payloads),\n bytes: Buffer.from(stream.bytes),\n chunks: copyBuffers(stream.chunks),\n };\n}\n\n/**\n * A canonical MLLP byte stream: one to five frames back to back, split into chunks at random\n * places, with the payloads a reader must deliver from it, in order and with no warning.\n *\n * @param options - The payloads' options, how many frames, and how many cuts at most.\n * @returns An arbitrary of streams.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader } from \"@cosyte/mllp\";\n * import { stream } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(\n * fc.property(stream(), (s) => {\n * const got: Buffer[] = [];\n * const reader = new FrameReader({ onFrame: (p) => got.push(p) });\n * for (const chunk of s.chunks) reader.push(chunk);\n * return got.length === s.payloads.length;\n * }),\n * );\n * ```\n */\nexport function stream(options: StreamOptions = {}): fc.Arbitrary<GeneratedStream> {\n return fc\n .array(payloadBytes(options), {\n minLength: options.minFrames ?? 1,\n maxLength: options.maxFrames ?? 5,\n })\n .chain((payloads) => {\n const bytes = Buffer.from(payloads.flatMap(frameBytes));\n return cuts(bytes.length, options).map((at) =>\n cloneable<GeneratedStream>(\n {\n payloads: payloads.map((p) => Buffer.from(p)),\n bytes,\n chunks: cutAt(bytes, at),\n },\n copyStream,\n ),\n );\n });\n}\n","/**\n * Messy streams a lenient reader accepts: the framing quirks real senders produce, each with the\n * payload and the warnings a `FrameReader` must report for it.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\nimport type { WarningCode } from \"../framing/registry.js\";\n\nimport {\n CR,\n FS,\n LF,\n VT,\n WHITESPACE,\n cloneable,\n copyStream,\n cutAt,\n cuts,\n payloadBytes,\n type GeneratedStream,\n type StreamOptions,\n} from \"./bytes.js\";\n\n/**\n * Options for {@link messyStream}: the payloads, the frames and the cuts as for a canonical stream,\n * and the tolerances of the reader the stream is meant for. A quirk whose tolerance is `false` is\n * never generated, so pass the same flags your `FrameReader` or server has. Each defaults to `true`.\n */\nexport interface MessyStreamOptions extends StreamOptions {\n /** Generate `FS` with no `CR` after it. */\n readonly allowFsOnly?: boolean;\n /** Generate `FS` then `LF` in place of `FS` then `CR`. */\n readonly allowLfAfterFs?: boolean;\n /** Generate a frame that opens with no `VT`. */\n readonly allowMissingLeadingVt?: boolean;\n /** Generate spaces, tabs, line feeds and carriage returns before a frame. */\n readonly allowLeadingWhitespace?: boolean;\n}\n\n/** A messy stream, with what a reader with the matching tolerances must deliver for it. */\nexport interface MessyStream extends GeneratedStream {\n /**\n * The warning codes each delivered frame must carry, in the order they are raised, one list per\n * payload. A frame with no quirk has an empty list.\n */\n readonly warnings: readonly (readonly WarningCode[])[];\n}\n\n/** How a frame opens. */\ntype Opening = \"vt\" | \"whitespace\" | \"no-vt\" | \"whitespace-no-vt\";\n/** How a frame ends. */\ntype Ending = \"cr\" | \"lf\" | \"fs-only\";\n\n/** One frame's choices, before the stream fixes them up so that they read as intended. */\ninterface FrameChoice {\n readonly opening: Opening;\n /** Whether the frame carries no payload. */\n readonly empty: boolean;\n readonly ending: Ending;\n readonly whitespace: readonly number[];\n readonly payload: readonly number[];\n}\n\n/** The parts of a messy stream built so far. */\ninterface Built {\n readonly bytes: number[];\n readonly payloads: Buffer[];\n readonly warnings: WarningCode[][];\n}\n\n/** The openings, bodies and endings the given tolerances accept. */\nfunction quirks(options: MessyStreamOptions): {\n openings: Opening[];\n endings: Ending[];\n} {\n const ws = options.allowLeadingWhitespace !== false;\n const noVt = options.allowMissingLeadingVt !== false;\n const openings: Opening[] = [\"vt\"];\n if (ws) openings.push(\"whitespace\");\n if (noVt) openings.push(\"no-vt\");\n if (ws && noVt) openings.push(\"whitespace-no-vt\");\n const endings: Ending[] = [\"cr\"];\n if (options.allowLfAfterFs !== false) endings.push(\"lf\");\n if (options.allowFsOnly !== false) endings.push(\"fs-only\");\n return { openings, endings };\n}\n\n/** One frame's random choices, within the given tolerances. */\nfunction frameChoice(options: MessyStreamOptions): fc.Arbitrary<FrameChoice> {\n const { openings, endings } = quirks(options);\n return fc.record({\n opening: fc.constantFrom(...openings),\n empty: fc.boolean(),\n ending: fc.constantFrom(...endings),\n whitespace: fc.array(fc.constantFrom(...WHITESPACE), { minLength: 1, maxLength: 4 }),\n payload: payloadBytes(options),\n });\n}\n\n/**\n * Append one frame to the stream, fixing its choices up where they would read otherwise:\n *\n * - a frame after an `fs-only` ending opens with its `VT`, which is what ends the frame before;\n * - a frame with no `VT` cannot open on whitespace a reader skips, so such a payload gets its `VT`;\n * - the last frame does not end `fs-only`, which only the next frame's `VT` completes.\n */\nfunction append(built: Built, choice: FrameChoice, mustOpenWithVt: boolean, last: boolean): void {\n const payload = choice.empty ? [] : [...choice.payload];\n let opening = mustOpenWithVt ? \"vt\" : choice.opening;\n const noVt = opening === \"no-vt\" || opening === \"whitespace-no-vt\";\n // An empty payload has no first byte, and `includes(undefined)` is false.\n if (noVt && WHITESPACE.includes(payload[0] as number)) {\n opening = opening === \"no-vt\" ? \"vt\" : \"whitespace\";\n }\n const ending = last && choice.ending === \"fs-only\" ? \"cr\" : choice.ending;\n\n const warnings: WarningCode[] = [];\n if (opening === \"whitespace\" || opening === \"whitespace-no-vt\") {\n built.bytes.push(...choice.whitespace);\n warnings.push(\"MLLP_LEADING_WHITESPACE\");\n }\n if (opening === \"no-vt\" || opening === \"whitespace-no-vt\") {\n warnings.push(\"MLLP_MISSING_LEADING_VT\");\n } else if (!mustOpenWithVt) {\n built.bytes.push(VT);\n }\n built.bytes.push(...payload, FS);\n if (payload.length === 0) warnings.push(\"MLLP_EMPTY_PAYLOAD\");\n if (ending === \"cr\") built.bytes.push(CR);\n else if (ending === \"lf\") {\n built.bytes.push(LF);\n warnings.push(\"MLLP_LF_AFTER_FS\");\n } else {\n // `fs-only` is ended by the next frame's VT, which that frame writes as its opening.\n built.bytes.push(VT);\n warnings.push(\"MLLP_FS_WITHOUT_CR\");\n }\n built.payloads.push(Buffer.from(payload));\n built.warnings.push(warnings);\n}\n\n/** Build a messy stream's bytes, payloads and warnings from its frames' choices. */\nfunction buildMessy(choices: readonly FrameChoice[]): Built {\n const built: Built = { bytes: [], payloads: [], warnings: [] };\n let mustOpenWithVt = false;\n choices.forEach((choice, index) => {\n const last = index === choices.length - 1;\n append(built, choice, mustOpenWithVt, last);\n mustOpenWithVt = !last && choice.ending === \"fs-only\";\n });\n return built;\n}\n\n/**\n * A messy MLLP byte stream a lenient reader accepts, split into chunks at random places, with the\n * payloads it must deliver and the warnings each must carry.\n *\n * Each frame may open with spaces, tabs, line feeds or carriage returns before its `VT`, or with no\n * `VT` at all; carry an empty payload; and end with `FS` and `LF`, or with `FS` and the next frame's\n * `VT`. Give it the tolerances your reader has, and it generates only what that reader accepts. It\n * never puts a `VT` inside a frame, nor an `FS` with anything but `CR`, `LF` or the next `VT` after\n * it: a reader refuses such a frame whole (`MLLP_PAYLOAD_CONTAINS_VT`, `MLLP_PAYLOAD_CONTAINS_FS`),\n * so no reader accepts it.\n *\n * @param options - The payloads, frames, cuts and tolerances.\n * @returns An arbitrary of messy streams.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader } from \"@cosyte/mllp\";\n * import { messyStream } from \"@cosyte/mllp/arbitraries\";\n *\n * const tolerances = { allowFsOnly: true, allowLfAfterFs: true, allowLeadingWhitespace: true };\n * fc.assert(\n * fc.property(messyStream({ ...tolerances, allowMissingLeadingVt: false }), (s) => {\n * const got: Buffer[] = [];\n * const reader = new FrameReader({ ...tolerances, onFrame: (p) => got.push(p) });\n * for (const chunk of s.chunks) reader.push(chunk);\n * return got.length === s.payloads.length;\n * }),\n * );\n * ```\n */\nexport function messyStream(options: MessyStreamOptions = {}): fc.Arbitrary<MessyStream> {\n return fc\n .array(frameChoice(options), {\n minLength: options.minFrames ?? 1,\n maxLength: options.maxFrames ?? 5,\n })\n .chain((choices) => {\n const built = buildMessy(choices);\n const bytes = Buffer.from(built.bytes);\n return cuts(bytes.length, options).map((at) =>\n cloneable<MessyStream>(\n {\n payloads: built.payloads.map((p) => Buffer.from(p)),\n warnings: built.warnings.map((codes) => [...codes]),\n bytes,\n chunks: cutAt(bytes, at),\n },\n copyStream,\n ),\n );\n });\n}\n","/**\n * Hostile byte streams: what a reader may meet from a broken sender, a port scanner or an attacker.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\nimport { CR, FS, LF, VT, WHITESPACE, cloneableChunks, cutAt, cuts } from \"./bytes.js\";\nimport { messyStream } from \"./messy.js\";\n\n/** Options for {@link hostileInput}. */\nexport interface HostileInputOptions {\n /** The most bytes in the stream. Default `512`. */\n readonly maxLength?: number;\n /** The most places the stream is cut into chunks. Default `16`. */\n readonly maxCuts?: number;\n}\n\n/** A byte that is a framing byte or whitespace one time in two, and any byte otherwise. */\nconst FRAMING_HEAVY_BYTE = fc.oneof(\n fc.constantFrom(VT, FS, CR, LF, ...WHITESPACE),\n fc.integer({ min: 0, max: 0xff }),\n);\n\n/** One edit to a stream. `at` is taken modulo the stream's length at the time. */\ninterface Edit {\n readonly op: \"insert\" | \"delete\" | \"replace\" | \"truncate\" | \"repeat\";\n readonly at: number;\n readonly byte: number;\n}\n\nconst EDIT: fc.Arbitrary<Edit> = fc.record({\n op: fc.constantFrom<Edit[\"op\"]>(\"insert\", \"delete\", \"replace\", \"truncate\", \"repeat\"),\n at: fc.nat(),\n byte: FRAMING_HEAVY_BYTE,\n});\n\n/** Apply edits to a stream's bytes, in order. */\nfunction applyEdits(bytes: readonly number[], edits: readonly Edit[]): number[] {\n const out = [...bytes];\n for (const { op, at, byte } of edits) {\n const i = at % (out.length + 1);\n if (op === \"insert\") out.splice(i, 0, byte);\n else if (op === \"truncate\") out.length = i;\n else if (op === \"repeat\") out.splice(i, 0, ...out.slice(i, i + 8));\n else if (i < out.length) {\n if (op === \"delete\") out.splice(i, 1);\n else out[i] = byte;\n }\n }\n return out;\n}\n\n/**\n * A hostile MLLP byte stream as the chunks a socket delivers: random bytes, bytes thick with\n * `VT`, `FS`, `CR`, `LF` and whitespace, and messy streams with bytes inserted, deleted, replaced,\n * repeated and cut off. A reader may refuse one, but it must not hang, crash or throw anything it\n * does not document.\n *\n * @param options - The most bytes, and the most cuts.\n * @returns An arbitrary of chunk lists.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader, MllpFramingError } from \"@cosyte/mllp\";\n * import { hostileInput } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(\n * fc.property(hostileInput(), (chunks) => {\n * const reader = new FrameReader({ onFrame: () => {} });\n * try {\n * for (const chunk of chunks) reader.push(chunk);\n * } catch (err) {\n * if (!(err instanceof MllpFramingError)) throw err;\n * }\n * }),\n * );\n * ```\n */\nexport function hostileInput(options: HostileInputOptions = {}): fc.Arbitrary<Buffer[]> {\n const maxLength = options.maxLength ?? 512;\n const bytes = fc.oneof(\n fc.array(fc.integer({ min: 0, max: 0xff }), { maxLength }),\n fc.array(FRAMING_HEAVY_BYTE, { maxLength }),\n fc\n .tuple(\n messyStream({ content: \"text\", maxLength: 32 }),\n fc.array(EDIT, { minLength: 1, maxLength: 4 }),\n )\n .map(([messy, edits]) => applyEdits([...messy.bytes], edits).slice(0, maxLength)),\n );\n return bytes.chain((list) => {\n const buffer = Buffer.from(list);\n return cuts(buffer.length, options).map((at) => cloneableChunks(cutAt(buffer, at)));\n });\n}\n"]}
|
|
@@ -174,10 +174,11 @@ interface MessyStream extends GeneratedStream {
|
|
|
174
174
|
* payloads it must deliver and the warnings each must carry.
|
|
175
175
|
*
|
|
176
176
|
* Each frame may open with spaces, tabs, line feeds or carriage returns before its `VT`, or with no
|
|
177
|
-
* `VT` at all; carry an empty payload;
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
177
|
+
* `VT` at all; carry an empty payload; and end with `FS` and `LF`, or with `FS` and the next frame's
|
|
178
|
+
* `VT`. Give it the tolerances your reader has, and it generates only what that reader accepts. It
|
|
179
|
+
* never puts a `VT` inside a frame, nor an `FS` with anything but `CR`, `LF` or the next `VT` after
|
|
180
|
+
* it: a reader refuses such a frame whole (`MLLP_PAYLOAD_CONTAINS_VT`, `MLLP_PAYLOAD_CONTAINS_FS`),
|
|
181
|
+
* so no reader accepts it.
|
|
181
182
|
*
|
|
182
183
|
* @param options - The payloads, frames, cuts and tolerances.
|
|
183
184
|
* @returns An arbitrary of messy streams.
|
|
@@ -174,10 +174,11 @@ interface MessyStream extends GeneratedStream {
|
|
|
174
174
|
* payloads it must deliver and the warnings each must carry.
|
|
175
175
|
*
|
|
176
176
|
* Each frame may open with spaces, tabs, line feeds or carriage returns before its `VT`, or with no
|
|
177
|
-
* `VT` at all; carry an empty payload;
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
177
|
+
* `VT` at all; carry an empty payload; and end with `FS` and `LF`, or with `FS` and the next frame's
|
|
178
|
+
* `VT`. Give it the tolerances your reader has, and it generates only what that reader accepts. It
|
|
179
|
+
* never puts a `VT` inside a frame, nor an `FS` with anything but `CR`, `LF` or the next `VT` after
|
|
180
|
+
* it: a reader refuses such a frame whole (`MLLP_PAYLOAD_CONTAINS_VT`, `MLLP_PAYLOAD_CONTAINS_FS`),
|
|
181
|
+
* so no reader accepts it.
|
|
181
182
|
*
|
|
182
183
|
* @param options - The payloads, frames, cuts and tolerances.
|
|
183
184
|
* @returns An arbitrary of messy streams.
|
|
@@ -144,28 +144,24 @@ function quirks(options) {
|
|
|
144
144
|
if (ws && noVt) openings.push("whitespace-no-vt");
|
|
145
145
|
const endings = ["cr"];
|
|
146
146
|
if (options.allowLfAfterFs !== false) endings.push("lf");
|
|
147
|
-
if (options.allowFsOnly !== false) endings.push("fs-only"
|
|
147
|
+
if (options.allowFsOnly !== false) endings.push("fs-only");
|
|
148
148
|
return { openings, endings };
|
|
149
149
|
}
|
|
150
150
|
function frameChoice(options) {
|
|
151
151
|
const { openings, endings } = quirks(options);
|
|
152
152
|
return fc.record({
|
|
153
153
|
opening: fc.constantFrom(...openings),
|
|
154
|
-
|
|
154
|
+
empty: fc.boolean(),
|
|
155
155
|
ending: fc.constantFrom(...endings),
|
|
156
156
|
whitespace: fc.array(fc.constantFrom(...WHITESPACE), { minLength: 1, maxLength: 4 }),
|
|
157
|
-
|
|
158
|
-
payload: payloadBytes(options),
|
|
159
|
-
// After FS, any byte but CR and LF (which end the frame properly) and VT (which opens the next).
|
|
160
|
-
stray: fc.integer({ min: 0, max: 255 }).filter((b) => b !== CR && b !== LF && b !== VT)
|
|
157
|
+
payload: payloadBytes(options)
|
|
161
158
|
});
|
|
162
159
|
}
|
|
163
160
|
function append(built, choice, mustOpenWithVt, last) {
|
|
164
|
-
const payload2 = choice.
|
|
161
|
+
const payload2 = choice.empty ? [] : [...choice.payload];
|
|
165
162
|
let opening = mustOpenWithVt ? "vt" : choice.opening;
|
|
166
163
|
const noVt = opening === "no-vt" || opening === "whitespace-no-vt";
|
|
167
|
-
|
|
168
|
-
if (noVt && (choice.body === "discarded" || first !== void 0 && WHITESPACE.includes(first))) {
|
|
164
|
+
if (noVt && WHITESPACE.includes(payload2[0])) {
|
|
169
165
|
opening = opening === "no-vt" ? "vt" : "whitespace";
|
|
170
166
|
}
|
|
171
167
|
const ending = last && choice.ending === "fs-only" ? "cr" : choice.ending;
|
|
@@ -179,10 +175,6 @@ function append(built, choice, mustOpenWithVt, last) {
|
|
|
179
175
|
} else if (!mustOpenWithVt) {
|
|
180
176
|
built.bytes.push(VT);
|
|
181
177
|
}
|
|
182
|
-
if (choice.body === "discarded") {
|
|
183
|
-
built.bytes.push(...choice.discarded, VT);
|
|
184
|
-
warnings.push("MLLP_TRAILING_BYTES");
|
|
185
|
-
}
|
|
186
178
|
built.bytes.push(...payload2, FS);
|
|
187
179
|
if (payload2.length === 0) warnings.push("MLLP_EMPTY_PAYLOAD");
|
|
188
180
|
if (ending === "cr") built.bytes.push(CR);
|
|
@@ -190,8 +182,7 @@ function append(built, choice, mustOpenWithVt, last) {
|
|
|
190
182
|
built.bytes.push(LF);
|
|
191
183
|
warnings.push("MLLP_LF_AFTER_FS");
|
|
192
184
|
} else {
|
|
193
|
-
|
|
194
|
-
else built.bytes.push(VT);
|
|
185
|
+
built.bytes.push(VT);
|
|
195
186
|
warnings.push("MLLP_FS_WITHOUT_CR");
|
|
196
187
|
}
|
|
197
188
|
built.payloads.push(Buffer.from(payload2));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/arbitraries/bytes.ts","../../src/arbitraries/messy.ts","../../src/arbitraries/hostile.ts"],"names":["payloadBytes","frame","cuts","stream","fc","payload"],"mappings":";;;AAcO,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,UAAA,GAAgC,CAAC,EAAA,EAAM,CAAA,EAAM,IAAI,EAAE,CAAA;AAczD,SAAS,SAAA,CAA4B,OAAU,IAAA,EAA0B;AAC9E,EAAA,MAAM,QAAA,GAAW,KAAK,KAAK,CAAA;AAC3B,EAAA,MAAA,CAAO,cAAA,CAAe,KAAA,EAAO,EAAA,CAAG,WAAA,EAAa;AAAA,IAC3C,OAAO,MAAM,SAAA,CAAU,IAAA,CAAK,QAAQ,GAAG,IAAI;AAAA,GAC5C,CAAA;AACD,EAAA,MAAA,CAAO,cAAA,CAAe,KAAA,EAAO,EAAA,CAAG,cAAA,EAAgB,EAAE,KAAA,EAAO,MAAM,EAAA,CAAG,SAAA,CAAU,QAAQ,CAAA,EAAG,CAAA;AACvF,EAAA,OAAO,KAAA;AACT;AAGA,SAAS,YAAY,OAAA,EAAsC;AACzD,EAAA,OAAO,QAAQ,GAAA,CAAI,CAAC,WAAW,MAAA,CAAO,IAAA,CAAK,MAAM,CAAC,CAAA;AACpD;AAGA,SAAS,gBAAgB,KAAA,EAA+C;AACtE,EAAA,OAAO,SAAA,CAAU,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,EAAG,CAAC,MAAA,KAAW,MAAA,CAAO,IAAA,C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;AACA,EAAA,OAAO,GAAA;AACT;AA4BO,SAAS,YAAA,CAAa,OAAA,GAA+B,EAAC,EAA2B;AACtF,EAAA,MAAM,SAAA,GAAY,QAAQ,SAAA,IAAa,GAAA;AACvC,EAAA,MAAM,QAAQA,EAAAA,CAAG,KAAA;AAAA,IACfA,EAAAA,CAAG,KAAA,CAAMA,EAAAA,CAAG,OAAA,CAAQ,EAAE,GAAA,EAAK,CAAA,EAAG,GAAA,EAAK,GAAA,EAAM,CAAA,EAAG,EAAE,WAAW,CAAA;AAAA,IACzDA,EAAAA,CAAG,KAAA,CAAM,kBAAA,EAAoB,EAAE,WAAW,CAAA;AAAA,IAC1CA,EAAAA,CACG,KAAA;AAAA,MACC,YAAY,EAAE,OAAA,EAAS,MAAA,EAAQ,SAAA,EAAW,IAAI,CAAA;AAAA,MAC9CA,EAAAA,CAAG,MAAM,IAAA,EAAM,EAAE,WAAW,CAAA,EAAG,SAAA,EAAW,GAAG;AAAA,MAE9C,GAAA,CAAI,CAAC,CAAC,KAAA,EAAO,KAAK,MAAM,UAAA,CAAW,CAAC,GAAG,KAAA,CAAM,KAAK,CAAA,EAAG,KAAK,EAAE,KAAA,CAAM,CAAA,EAAG,SAAS,CAAC;AAAA,GACpF;AACA,EAAA,OAAO,KAAA,CAAM,KAAA,CAAM,CAAC,IAAA,KAAS;AAC3B,IAAA,MAAM,MAAA,GAAS,MAAA,CAAO,IAAA,CAAK,IAAI,CAAA;AAC/B,IAAA,OAAO,IAAA,CAAK,MAAA,CAAO,MAAA,EAAQ,OAAO,CAAA,CAAE,GAAA,CAAI,CAAC,EAAA,KAAO,eAAA,CAAgB,KAAA,CAAM,MAAA,EAAQ,EAAE,CAAC,CAAC,CAAA;AAAA,EACpF,CAAC,CAAA;AACH","file":"index.mjs","sourcesContent":["/**\n * Payloads, canonical frames, canonical byte streams and chunk splits: the valid half of\n * `@cosyte/mllp/arbitraries`.\n *\n * The framing bytes are written here from the MLLP specification (HL7 v2 Appendix C), not imported\n * from the codec, so a generated frame is an independent statement of what the wire carries and a\n * test that compares it with `encodeFrame` checks something.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\n/** Start of a frame, `0x0B`. */\nexport const VT = 0x0b;\n/** End of a frame's payload, `0x1C`. */\nexport const FS = 0x1c;\n/** Carriage return, `0x0D`: the byte after `FS`, and the HL7 segment separator. */\nexport const CR = 0x0d;\n/** Line feed, `0x0A`. */\nexport const LF = 0x0a;\n/** The bytes a lenient reader skips before a frame: space, tab, line feed and carriage return. */\nexport const WHITESPACE: readonly number[] = [0x20, 0x09, LF, CR];\n\n/**\n * Make a generated value safe from code that changes it. fast-check hands a property the value\n * itself the first time and a clone each time after, and reports the value itself when the\n * property fails. So a copy is taken here, before any code under test sees the value: every clone\n * is made from that copy, and the value prints as that copy, so a failure reports the case as it\n * was generated, whatever the code under test did to its bytes.\n *\n * @example\n * ```ts\n * const bytes = cloneable(Buffer.from(\"A\"), (b) => Buffer.from(b));\n * ```\n */\nexport function cloneable<T extends object>(value: T, copy: (value: T) => T): T {\n const pristine = copy(value);\n Object.defineProperty(value, fc.cloneMethod, {\n value: () => cloneable(copy(pristine), copy),\n });\n Object.defineProperty(value, fc.toStringMethod, { value: () => fc.stringify(pristine) });\n return value;\n}\n\n/** A copy of a list of buffers, each buffer copied too. */\nfunction copyBuffers(buffers: readonly Buffer[]): Buffer[] {\n return buffers.map((buffer) => Buffer.from(buffer));\n}\n\n/** A cloneable `Buffer` of the given bytes. */\nfunction cloneableBuffer(bytes: Uint8Array | readonly number[]): Buffer {\n return cloneable(Buffer.from(bytes), (buffer) => Buffer.from(buffer));\n}\n\n/** What a generated payload holds. */\nexport type PayloadContent = \"bytes\" | \"text\" | \"hl7\";\n\n/** Options for {@link payload}, and for the payloads inside frames and streams. */\nexport interface PayloadOptions {\n /**\n * What the payload holds.\n *\n * - `\"bytes\"` (the default): any byte except `VT` (`0x0B`) and `FS` (`0x1C`), the two bytes the\n * encoder refuses. `CR` and the other control bytes are in.\n * - `\"text\"`: printable ASCII with carriage returns, the shape of HL7 v2 text.\n * - `\"hl7\"`: a synthetic HL7 v2 message: an `MSH` segment with a generated control id in MSH-10,\n * then up to four more segments, each ended by a carriage return. `minLength` and `maxLength`\n * do not apply.\n */\n readonly content?: PayloadContent;\n /** The fewest bytes in a `\"bytes\"` or `\"text\"` payload. Default `1`. */\n readonly minLength?: number;\n /** The most bytes in a `\"bytes\"` or `\"text\"` payload. Default `256`. */\n readonly maxLength?: number;\n}\n\n/** Any byte except the two the encoder refuses in a payload. */\nconst PAYLOAD_BYTE = fc.integer({ min: 0, max: 0xff }).filter((b) => b !== VT && b !== FS);\n\n/** A printable ASCII byte, or a carriage return now and then. */\nconst TEXT_BYTE = fc.oneof(\n { weight: 15, arbitrary: fc.integer({ min: 0x20, max: 0x7e }) },\n { weight: 1, arbitrary: fc.constant(CR) },\n);\n\n/** A short run of letters and digits: the content of one synthetic HL7 field. */\nfunction word(maxLength: number): fc.Arbitrary<string> {\n return fc.string({\n unit: fc.constantFrom(...\"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"),\n maxLength,\n });\n}\n\n/** The message types a synthetic HL7 payload carries in MSH-9. */\nconst MESSAGE_TYPES = [\n \"ADT^A01\",\n \"ADT^A04\",\n \"ADT^A08\",\n \"ORM^O01\",\n \"ORU^R01\",\n \"SIU^S12\",\n \"MDM^T02\",\n \"DFT^P03\",\n \"ACK\",\n];\n\n/** The segments after MSH in a synthetic HL7 payload. None of them carries a patient. */\nconst SEGMENT_NAMES = [\"EVN\", \"PV1\", \"ORC\", \"OBR\", \"OBX\", \"NTE\", \"ZXX\"];\n\n/** One synthetic segment after MSH: a name, then fields of letters and digits with components. */\nconst SEGMENT = fc\n .tuple(\n fc.constantFrom(...SEGMENT_NAMES),\n fc.array(fc.array(word(8), { minLength: 1, maxLength: 3 }), { maxLength: 6 }),\n )\n .map(([name, fields]) => [name, ...fields.map((components) => components.join(\"^\"))].join(\"|\"));\n\n/** A synthetic HL7 v2 message, each segment ended by a carriage return. */\nconst HL7_TEXT = fc\n .record({\n sender: word(6),\n receiver: word(6),\n when: fc.date({\n min: new Date(\"2000-01-01T00:00:00Z\"),\n max: new Date(\"2039-12-31T23:59:59Z\"),\n noInvalidDate: true,\n }),\n type: fc.constantFrom(...MESSAGE_TYPES),\n controlId: word(20).filter((id) => id.length > 0),\n segments: fc.array(SEGMENT, { maxLength: 4 }),\n })\n .map(({ sender, receiver, when, type, controlId, segments }) => {\n const ts = when.toISOString().replace(/\\D/gu, \"\").slice(0, 14);\n const msh = `MSH|^~\\\\&|${sender}|FAC|${receiver}|FAC|${ts}||${type}|${controlId}|P|2.5`;\n return [msh, ...segments].map((segment) => `${segment}\\r`).join(\"\");\n });\n\n/**\n * The bytes of one payload, as a plain array, before it becomes a `Buffer`.\n *\n * @example\n * ```ts\n * const bytes = fc.sample(payloadBytes({ content: \"text\" }), 1)[0];\n * ```\n */\nexport function payloadBytes(options: PayloadOptions = {}): fc.Arbitrary<number[]> {\n const content = options.content ?? \"bytes\";\n if (content === \"hl7\") return HL7_TEXT.map((text) => [...Buffer.from(text, \"latin1\")]);\n return fc.array(content === \"text\" ? TEXT_BYTE : PAYLOAD_BYTE, {\n minLength: options.minLength ?? 1,\n maxLength: options.maxLength ?? 256,\n });\n}\n\n/**\n * A payload the encoder accepts: a `Buffer` with no `VT` (`0x0B`) and no `FS` (`0x1C`) in it.\n *\n * Framed with `encodeFrame` and read back with a `FrameReader`, every one comes back byte for\n * byte. With the default `minLength` of 1 it also comes back with no warning; an empty payload\n * comes back with `MLLP_EMPTY_PAYLOAD`.\n *\n * @param options - What the payload holds and how long it is.\n * @returns An arbitrary of payload buffers.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { encodeFrame } from \"@cosyte/mllp\";\n * import { payload } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(fc.property(payload(), (p) => encodeFrame(p).length === p.length + 3));\n * ```\n */\nexport function payload(options: PayloadOptions = {}): fc.Arbitrary<Buffer> {\n return payloadBytes(options).map(cloneableBuffer);\n}\n\n/** A canonical frame and the payload it carries. */\nexport interface GeneratedFrame {\n /** The payload: what a reader must deliver for these bytes. */\n readonly payload: Buffer;\n /** The frame as it goes on the wire: `VT`, the payload, `FS`, `CR`. */\n readonly bytes: Buffer;\n}\n\n/** The canonical frame of a payload, written byte by byte. */\nfunction frameBytes(payloadBytes: readonly number[]): number[] {\n return [VT, ...payloadBytes, FS, CR];\n}\n\n/** Copy a frame, both buffers. */\nfunction copyFrame(frame: GeneratedFrame): GeneratedFrame {\n return { payload: Buffer.from(frame.payload), bytes: Buffer.from(frame.bytes) };\n}\n\n/**\n * A canonical MLLP frame, `VT` + payload + `FS` + `CR`, with the payload it carries.\n *\n * The bytes are written from the MLLP specification, not by `encodeFrame`, so they are an\n * independent check on the encoder: `encodeFrame(f.payload)` equals `f.bytes`.\n *\n * @param options - The payload's options, as for {@link payload}.\n * @returns An arbitrary of frames.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { encodeFrame } from \"@cosyte/mllp\";\n * import { frame } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(fc.property(frame(), (f) => encodeFrame(f.payload).equals(f.bytes)));\n * ```\n */\nexport function frame(options: PayloadOptions = {}): fc.Arbitrary<GeneratedFrame> {\n return payloadBytes(options).map((bytes) =>\n cloneable<GeneratedFrame>(\n { payload: Buffer.from(bytes), bytes: Buffer.from(frameBytes(bytes)) },\n copyFrame,\n ),\n );\n}\n\n/** Options for {@link chunked}. */\nexport interface ChunkOptions {\n /** The most places the bytes are cut. Default `16`. Two cuts in one place leave an empty chunk. */\n readonly maxCuts?: number;\n}\n\n/**\n * Cut `bytes` at the given offsets, sorted; repeated offsets leave empty chunks.\n *\n * @example\n * ```ts\n * cutAt(Buffer.from(\"ABC\"), [2, 1]); // A, B, C\n * ```\n */\nexport function cutAt(bytes: Buffer, cuts: readonly number[]): Buffer[] {\n const sorted = [...cuts].sort((a, b) => a - b);\n const chunks: Buffer[] = [];\n let start = 0;\n for (const cut of sorted) {\n chunks.push(Buffer.from(bytes.subarray(start, cut)));\n start = cut;\n }\n chunks.push(Buffer.from(bytes.subarray(start)));\n return chunks;\n}\n\n/**\n * A cloneable list of chunks.\n *\n * @example\n * ```ts\n * const chunks = cloneableChunks([Buffer.from(\"A\")]);\n * ```\n */\nexport function cloneableChunks(chunks: Buffer[]): Buffer[] {\n return cloneable(chunks, copyBuffers);\n}\n\n/**\n * The cut offsets for a run of `length` bytes.\n *\n * @example\n * ```ts\n * const at = fc.sample(cuts(10), 1)[0];\n * ```\n */\nexport function cuts(length: number, options: ChunkOptions = {}): fc.Arbitrary<number[]> {\n return fc.array(fc.integer({ min: 0, max: length }), { maxLength: options.maxCuts ?? 16 });\n}\n\n/**\n * The given bytes split into chunks at random places, the way a socket may deliver them: a frame\n * across many reads, many frames in one read, and now and then an empty read. Concatenated, the\n * chunks are always the bytes given. A case shrinks toward one chunk.\n *\n * @param bytes - The bytes to split.\n * @param options - How many cuts at most.\n * @returns An arbitrary of chunk lists.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { chunked } from \"@cosyte/mllp/arbitraries\";\n *\n * const bytes = Buffer.from(\"ABCDEF\");\n * fc.assert(fc.property(chunked(bytes), (chunks) => Buffer.concat(chunks).equals(bytes)));\n * ```\n */\nexport function chunked(bytes: Buffer, options: ChunkOptions = {}): fc.Arbitrary<Buffer[]> {\n const source = Buffer.from(bytes);\n return cuts(source.length, options).map((at) => cloneableChunks(cutAt(source, at)));\n}\n\n/** Options for {@link stream}. */\nexport interface StreamOptions extends PayloadOptions, ChunkOptions {\n /** The fewest frames in the stream. Default `1`. */\n readonly minFrames?: number;\n /** The most frames in the stream. Default `5`. */\n readonly maxFrames?: number;\n}\n\n/** A canonical byte stream: frames back to back, as chunks, with the payloads it carries. */\nexport interface GeneratedStream {\n /** The payloads in order: what a reader must deliver for this stream. */\n readonly payloads: readonly Buffer[];\n /** The whole stream: each payload's canonical frame, back to back. */\n readonly bytes: Buffer;\n /** The same bytes split at random places, the reads a socket may deliver. */\n readonly chunks: readonly Buffer[];\n}\n\n/**\n * Copy a stream, every buffer in it.\n *\n * @example\n * ```ts\n * const copy = copyStream(generated);\n * ```\n */\nexport function copyStream<T extends GeneratedStream>(stream: T): T {\n return {\n ...stream,\n payloads: copyBuffers(stream.payloads),\n bytes: Buffer.from(stream.bytes),\n chunks: copyBuffers(stream.chunks),\n };\n}\n\n/**\n * A canonical MLLP byte stream: one to five frames back to back, split into chunks at random\n * places, with the payloads a reader must deliver from it, in order and with no warning.\n *\n * @param options - The payloads' options, how many frames, and how many cuts at most.\n * @returns An arbitrary of streams.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader } from \"@cosyte/mllp\";\n * import { stream } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(\n * fc.property(stream(), (s) => {\n * const got: Buffer[] = [];\n * const reader = new FrameReader({ onFrame: (p) => got.push(p) });\n * for (const chunk of s.chunks) reader.push(chunk);\n * return got.length === s.payloads.length;\n * }),\n * );\n * ```\n */\nexport function stream(options: StreamOptions = {}): fc.Arbitrary<GeneratedStream> {\n return fc\n .array(payloadBytes(options), {\n minLength: options.minFrames ?? 1,\n maxLength: options.maxFrames ?? 5,\n })\n .chain((payloads) => {\n const bytes = Buffer.from(payloads.flatMap(frameBytes));\n return cuts(bytes.length, options).map((at) =>\n cloneable<GeneratedStream>(\n {\n payloads: payloads.map((p) => Buffer.from(p)),\n bytes,\n chunks: cutAt(bytes, at),\n },\n copyStream,\n ),\n );\n });\n}\n","/**\n * Messy streams a lenient reader accepts: the framing quirks real senders produce, each with the\n * payload and the warnings a `FrameReader` must report for it.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\nimport type { WarningCode } from \"../framing/registry.js\";\n\nimport {\n CR,\n FS,\n LF,\n VT,\n WHITESPACE,\n cloneable,\n copyStream,\n cutAt,\n cuts,\n payloadBytes,\n type GeneratedStream,\n type StreamOptions,\n} from \"./bytes.js\";\n\n/**\n * Options for {@link messyStream}: the payloads, the frames and the cuts as for a canonical stream,\n * and the tolerances of the reader the stream is meant for. A quirk whose tolerance is `false` is\n * never generated, so pass the same flags your `FrameReader` or server has. Each defaults to `true`.\n */\nexport interface MessyStreamOptions extends StreamOptions {\n /** Generate `FS` with no `CR` after it. */\n readonly allowFsOnly?: boolean;\n /** Generate `FS` then `LF` in place of `FS` then `CR`. */\n readonly allowLfAfterFs?: boolean;\n /** Generate a frame that opens with no `VT`. */\n readonly allowMissingLeadingVt?: boolean;\n /** Generate spaces, tabs, line feeds and carriage returns before a frame. */\n readonly allowLeadingWhitespace?: boolean;\n}\n\n/** A messy stream, with what a reader with the matching tolerances must deliver for it. */\nexport interface MessyStream extends GeneratedStream {\n /**\n * The warning codes each delivered frame must carry, in the order they are raised, one list per\n * payload. A frame with no quirk has an empty list.\n */\n readonly warnings: readonly (readonly WarningCode[])[];\n}\n\n/** How a frame opens. */\ntype Opening = \"vt\" | \"whitespace\" | \"no-vt\" | \"whitespace-no-vt\";\n/** What sits between the opening and the payload. */\ntype Body = \"plain\" | \"empty\" | \"discarded\";\n/** How a frame ends. */\ntype Ending = \"cr\" | \"lf\" | \"fs-only\" | \"stray\";\n\n/** One frame's choices, before the stream fixes them up so that they read as intended. */\ninterface FrameChoice {\n readonly opening: Opening;\n readonly body: Body;\n readonly ending: Ending;\n readonly whitespace: readonly number[];\n readonly discarded: readonly number[];\n readonly payload: readonly number[];\n readonly stray: number;\n}\n\n/** The parts of a messy stream built so far. */\ninterface Built {\n readonly bytes: number[];\n readonly payloads: Buffer[];\n readonly warnings: WarningCode[][];\n}\n\n/** The openings, bodies and endings the given tolerances accept. */\nfunction quirks(options: MessyStreamOptions): {\n openings: Opening[];\n endings: Ending[];\n} {\n const ws = options.allowLeadingWhitespace !== false;\n const noVt = options.allowMissingLeadingVt !== false;\n const openings: Opening[] = [\"vt\"];\n if (ws) openings.push(\"whitespace\");\n if (noVt) openings.push(\"no-vt\");\n if (ws && noVt) openings.push(\"whitespace-no-vt\");\n const endings: Ending[] = [\"cr\"];\n if (options.allowLfAfterFs !== false) endings.push(\"lf\");\n if (options.allowFsOnly !== false) endings.push(\"fs-only\", \"stray\");\n return { openings, endings };\n}\n\n/** One frame's random choices, within the given tolerances. */\nfunction frameChoice(options: MessyStreamOptions): fc.Arbitrary<FrameChoice> {\n const { openings, endings } = quirks(options);\n return fc.record({\n opening: fc.constantFrom(...openings),\n body: fc.constantFrom<Body>(\"plain\", \"empty\", \"discarded\"),\n ending: fc.constantFrom(...endings),\n whitespace: fc.array(fc.constantFrom(...WHITESPACE), { minLength: 1, maxLength: 4 }),\n discarded: payloadBytes({ ...options, minLength: 0, maxLength: 16 }),\n payload: payloadBytes(options),\n // After FS, any byte but CR and LF (which end the frame properly) and VT (which opens the next).\n stray: fc.integer({ min: 0, max: 0xff }).filter((b) => b !== CR && b !== LF && b !== VT),\n });\n}\n\n/**\n * Append one frame to the stream, fixing its choices up where they would read otherwise:\n *\n * - a frame after an `fs-only` ending opens with its `VT`, which is what ends the frame before;\n * - a frame with no `VT` cannot open on whitespace a reader skips, so such a payload gets its `VT`;\n * - a frame with no `VT` has nothing to discard, since discarding starts at a `VT`;\n * - the last frame does not end `fs-only`, which only the next frame's `VT` completes.\n */\nfunction append(built: Built, choice: FrameChoice, mustOpenWithVt: boolean, last: boolean): void {\n const payload = choice.body === \"empty\" ? [] : [...choice.payload];\n let opening = mustOpenWithVt ? \"vt\" : choice.opening;\n const noVt = opening === \"no-vt\" || opening === \"whitespace-no-vt\";\n const first = payload[0];\n if (\n noVt &&\n (choice.body === \"discarded\" || (first !== undefined && WHITESPACE.includes(first)))\n ) {\n opening = opening === \"no-vt\" ? \"vt\" : \"whitespace\";\n }\n const ending = last && choice.ending === \"fs-only\" ? \"cr\" : choice.ending;\n\n const warnings: WarningCode[] = [];\n if (opening === \"whitespace\" || opening === \"whitespace-no-vt\") {\n built.bytes.push(...choice.whitespace);\n warnings.push(\"MLLP_LEADING_WHITESPACE\");\n }\n if (opening === \"no-vt\" || opening === \"whitespace-no-vt\") {\n warnings.push(\"MLLP_MISSING_LEADING_VT\");\n } else if (!mustOpenWithVt) {\n built.bytes.push(VT);\n }\n if (choice.body === \"discarded\") {\n built.bytes.push(...choice.discarded, VT);\n warnings.push(\"MLLP_TRAILING_BYTES\");\n }\n built.bytes.push(...payload, FS);\n if (payload.length === 0) warnings.push(\"MLLP_EMPTY_PAYLOAD\");\n if (ending === \"cr\") built.bytes.push(CR);\n else if (ending === \"lf\") {\n built.bytes.push(LF);\n warnings.push(\"MLLP_LF_AFTER_FS\");\n } else {\n // `fs-only` is ended by the next frame's VT, which that frame writes as its opening.\n if (ending === \"stray\") built.bytes.push(choice.stray);\n else built.bytes.push(VT);\n warnings.push(\"MLLP_FS_WITHOUT_CR\");\n }\n built.payloads.push(Buffer.from(payload));\n built.warnings.push(warnings);\n}\n\n/** Build a messy stream's bytes, payloads and warnings from its frames' choices. */\nfunction buildMessy(choices: readonly FrameChoice[]): Built {\n const built: Built = { bytes: [], payloads: [], warnings: [] };\n let mustOpenWithVt = false;\n choices.forEach((choice, index) => {\n const last = index === choices.length - 1;\n append(built, choice, mustOpenWithVt, last);\n mustOpenWithVt = !last && choice.ending === \"fs-only\";\n });\n return built;\n}\n\n/**\n * A messy MLLP byte stream a lenient reader accepts, split into chunks at random places, with the\n * payloads it must deliver and the warnings each must carry.\n *\n * Each frame may open with spaces, tabs, line feeds or carriage returns before its `VT`, or with no\n * `VT` at all; carry an empty payload; carry bytes before a second `VT` that the reader discards\n * (`MLLP_TRAILING_BYTES`, the payload delivered is what follows that `VT`); and end with `FS` and\n * `LF`, with `FS` and the next frame's `VT`, or with `FS` and a stray byte the reader drops. Give it\n * the tolerances your reader has, and it generates only what that reader accepts.\n *\n * @param options - The payloads, frames, cuts and tolerances.\n * @returns An arbitrary of messy streams.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader } from \"@cosyte/mllp\";\n * import { messyStream } from \"@cosyte/mllp/arbitraries\";\n *\n * const tolerances = { allowFsOnly: true, allowLfAfterFs: true, allowLeadingWhitespace: true };\n * fc.assert(\n * fc.property(messyStream({ ...tolerances, allowMissingLeadingVt: false }), (s) => {\n * const got: Buffer[] = [];\n * const reader = new FrameReader({ ...tolerances, onFrame: (p) => got.push(p) });\n * for (const chunk of s.chunks) reader.push(chunk);\n * return got.length === s.payloads.length;\n * }),\n * );\n * ```\n */\nexport function messyStream(options: MessyStreamOptions = {}): fc.Arbitrary<MessyStream> {\n return fc\n .array(frameChoice(options), {\n minLength: options.minFrames ?? 1,\n maxLength: options.maxFrames ?? 5,\n })\n .chain((choices) => {\n const built = buildMessy(choices);\n const bytes = Buffer.from(built.bytes);\n return cuts(bytes.length, options).map((at) =>\n cloneable<MessyStream>(\n {\n payloads: built.payloads.map((p) => Buffer.from(p)),\n warnings: built.warnings.map((codes) => [...codes]),\n bytes,\n chunks: cutAt(bytes, at),\n },\n copyStream,\n ),\n );\n });\n}\n","/**\n * Hostile byte streams: what a reader may meet from a broken sender, a port scanner or an attacker.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\nimport { CR, FS, LF, VT, WHITESPACE, cloneableChunks, cutAt, cuts } from \"./bytes.js\";\nimport { messyStream } from \"./messy.js\";\n\n/** Options for {@link hostileInput}. */\nexport interface HostileInputOptions {\n /** The most bytes in the stream. Default `512`. */\n readonly maxLength?: number;\n /** The most places the stream is cut into chunks. Default `16`. */\n readonly maxCuts?: number;\n}\n\n/** A byte that is a framing byte or whitespace one time in two, and any byte otherwise. */\nconst FRAMING_HEAVY_BYTE = fc.oneof(\n fc.constantFrom(VT, FS, CR, LF, ...WHITESPACE),\n fc.integer({ min: 0, max: 0xff }),\n);\n\n/** One edit to a stream. `at` is taken modulo the stream's length at the time. */\ninterface Edit {\n readonly op: \"insert\" | \"delete\" | \"replace\" | \"truncate\" | \"repeat\";\n readonly at: number;\n readonly byte: number;\n}\n\nconst EDIT: fc.Arbitrary<Edit> = fc.record({\n op: fc.constantFrom<Edit[\"op\"]>(\"insert\", \"delete\", \"replace\", \"truncate\", \"repeat\"),\n at: fc.nat(),\n byte: FRAMING_HEAVY_BYTE,\n});\n\n/** Apply edits to a stream's bytes, in order. */\nfunction applyEdits(bytes: readonly number[], edits: readonly Edit[]): number[] {\n const out = [...bytes];\n for (const { op, at, byte } of edits) {\n const i = at % (out.length + 1);\n if (op === \"insert\") out.splice(i, 0, byte);\n else if (op === \"truncate\") out.length = i;\n else if (op === \"repeat\") out.splice(i, 0, ...out.slice(i, i + 8));\n else if (i < out.length) {\n if (op === \"delete\") out.splice(i, 1);\n else out[i] = byte;\n }\n }\n return out;\n}\n\n/**\n * A hostile MLLP byte stream as the chunks a socket delivers: random bytes, bytes thick with\n * `VT`, `FS`, `CR`, `LF` and whitespace, and messy streams with bytes inserted, deleted, replaced,\n * repeated and cut off. A reader may refuse one, but it must not hang, crash or throw anything it\n * does not document.\n *\n * @param options - The most bytes, and the most cuts.\n * @returns An arbitrary of chunk lists.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader, MllpFramingError } from \"@cosyte/mllp\";\n * import { hostileInput } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(\n * fc.property(hostileInput(), (chunks) => {\n * const reader = new FrameReader({ onFrame: () => {} });\n * try {\n * for (const chunk of chunks) reader.push(chunk);\n * } catch (err) {\n * if (!(err instanceof MllpFramingError)) throw err;\n * }\n * }),\n * );\n * ```\n */\nexport function hostileInput(options: HostileInputOptions = {}): fc.Arbitrary<Buffer[]> {\n const maxLength = options.maxLength ?? 512;\n const bytes = fc.oneof(\n fc.array(fc.integer({ min: 0, max: 0xff }), { maxLength }),\n fc.array(FRAMING_HEAVY_BYTE, { maxLength }),\n fc\n .tuple(\n messyStream({ content: \"text\", maxLength: 32 }),\n fc.array(EDIT, { minLength: 1, maxLength: 4 }),\n )\n .map(([messy, edits]) => applyEdits([...messy.bytes], edits).slice(0, maxLength)),\n );\n return bytes.chain((list) => {\n const buffer = Buffer.from(list);\n return cuts(buffer.length, options).map((at) => cloneableChunks(cutAt(buffer, at)));\n });\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/arbitraries/bytes.ts","../../src/arbitraries/messy.ts","../../src/arbitraries/hostile.ts"],"names":["payloadBytes","frame","cuts","stream","fc","payload"],"mappings":";;;AAcO,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,EAAA,GAAK,EAAA;AAEX,IAAM,UAAA,GAAgC,CAAC,EAAA,EAAM,CAAA,EAAM,IAAI,EAAE,CAAA;AAczD,SAAS,SAAA,CAA4B,OAAU,IAAA,EAA0B;AAC9E,EAAA,MAAM,QAAA,GAAW,KAAK,KAAK,CAAA;AAC3B,EAAA,MAAA,CAAO,cAAA,CAAe,KAAA,EAAO,EAAA,CAAG,WAAA,EAAa;AAAA,IAC3C,OAAO,MAAM,SAAA,CAAU,IAAA,CAAK,QAAQ,GAAG,IAAI;AAAA,GAC5C,CAAA;AACD,EAAA,MAAA,CAAO,cAAA,CAAe,KAAA,EAAO,EAAA,CAAG,cAAA,EAAgB,EAAE,KAAA,EAAO,MAAM,EAAA,CAAG,SAAA,CAAU,QAAQ,CAAA,EAAG,CAAA;AACvF,EAAA,OAAO,KAAA;AACT;AAGA,SAAS,YAAY,OAAA,EAAsC;AACzD,EAAA,OAAO,QAAQ,GAAA,CAAI,CAAC,WAAW,MAAA,CAAO,IAAA,CAAK,MAAM,CAAC,CAAA;AACpD;AAGA,SAAS,gBAAgB,KAAA,EAA+C;AACtE,EAAA,OAAO,SAAA,CAAU,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,EAAG,CAAC,MAAA,KAAW,MAAA,CAAO,IAAA,CAAK,MAAM,CAAC,CAAA;AACtE;AAyBA,IAAM,eAAe,EAAA,CAAG,OAAA,CAAQ,EAAE,GAAA,EAAK,GAAG,GAAA,EAAK,GAAA,EAAM,CAAA,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,IAAA,CAAK,IAAI,CAAA;AAC/B,IAAA,OAAO,IAAA,CAAK,MAAA,CAAO,MAAA,EAAQ,OAAO,CAAA,CAAE,GAAA,CAAI,CAAC,EAAA,KAAO,eAAA,CAAgB,KAAA,CAAM,MAAA,EAAQ,EAAE,CAAC,CAAC,CAAA;AAAA,EACpF,CAAC,CAAA;AACH","file":"index.mjs","sourcesContent":["/**\n * Payloads, canonical frames, canonical byte streams and chunk splits: the valid half of\n * `@cosyte/mllp/arbitraries`.\n *\n * The framing bytes are written here from the MLLP specification (HL7 v2 Appendix C), not imported\n * from the codec, so a generated frame is an independent statement of what the wire carries and a\n * test that compares it with `encodeFrame` checks something.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\n/** Start of a frame, `0x0B`. */\nexport const VT = 0x0b;\n/** End of a frame's payload, `0x1C`. */\nexport const FS = 0x1c;\n/** Carriage return, `0x0D`: the byte after `FS`, and the HL7 segment separator. */\nexport const CR = 0x0d;\n/** Line feed, `0x0A`. */\nexport const LF = 0x0a;\n/** The bytes a lenient reader skips before a frame: space, tab, line feed and carriage return. */\nexport const WHITESPACE: readonly number[] = [0x20, 0x09, LF, CR];\n\n/**\n * Make a generated value safe from code that changes it. fast-check hands a property the value\n * itself the first time and a clone each time after, and reports the value itself when the\n * property fails. So a copy is taken here, before any code under test sees the value: every clone\n * is made from that copy, and the value prints as that copy, so a failure reports the case as it\n * was generated, whatever the code under test did to its bytes.\n *\n * @example\n * ```ts\n * const bytes = cloneable(Buffer.from(\"A\"), (b) => Buffer.from(b));\n * ```\n */\nexport function cloneable<T extends object>(value: T, copy: (value: T) => T): T {\n const pristine = copy(value);\n Object.defineProperty(value, fc.cloneMethod, {\n value: () => cloneable(copy(pristine), copy),\n });\n Object.defineProperty(value, fc.toStringMethod, { value: () => fc.stringify(pristine) });\n return value;\n}\n\n/** A copy of a list of buffers, each buffer copied too. */\nfunction copyBuffers(buffers: readonly Buffer[]): Buffer[] {\n return buffers.map((buffer) => Buffer.from(buffer));\n}\n\n/** A cloneable `Buffer` of the given bytes. */\nfunction cloneableBuffer(bytes: Uint8Array | readonly number[]): Buffer {\n return cloneable(Buffer.from(bytes), (buffer) => Buffer.from(buffer));\n}\n\n/** What a generated payload holds. */\nexport type PayloadContent = \"bytes\" | \"text\" | \"hl7\";\n\n/** Options for {@link payload}, and for the payloads inside frames and streams. */\nexport interface PayloadOptions {\n /**\n * What the payload holds.\n *\n * - `\"bytes\"` (the default): any byte except `VT` (`0x0B`) and `FS` (`0x1C`), the two bytes the\n * encoder refuses. `CR` and the other control bytes are in.\n * - `\"text\"`: printable ASCII with carriage returns, the shape of HL7 v2 text.\n * - `\"hl7\"`: a synthetic HL7 v2 message: an `MSH` segment with a generated control id in MSH-10,\n * then up to four more segments, each ended by a carriage return. `minLength` and `maxLength`\n * do not apply.\n */\n readonly content?: PayloadContent;\n /** The fewest bytes in a `\"bytes\"` or `\"text\"` payload. Default `1`. */\n readonly minLength?: number;\n /** The most bytes in a `\"bytes\"` or `\"text\"` payload. Default `256`. */\n readonly maxLength?: number;\n}\n\n/** Any byte except the two the encoder refuses in a payload. */\nconst PAYLOAD_BYTE = fc.integer({ min: 0, max: 0xff }).filter((b) => b !== VT && b !== FS);\n\n/** A printable ASCII byte, or a carriage return now and then. */\nconst TEXT_BYTE = fc.oneof(\n { weight: 15, arbitrary: fc.integer({ min: 0x20, max: 0x7e }) },\n { weight: 1, arbitrary: fc.constant(CR) },\n);\n\n/** A short run of letters and digits: the content of one synthetic HL7 field. */\nfunction word(maxLength: number): fc.Arbitrary<string> {\n return fc.string({\n unit: fc.constantFrom(...\"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"),\n maxLength,\n });\n}\n\n/** The message types a synthetic HL7 payload carries in MSH-9. */\nconst MESSAGE_TYPES = [\n \"ADT^A01\",\n \"ADT^A04\",\n \"ADT^A08\",\n \"ORM^O01\",\n \"ORU^R01\",\n \"SIU^S12\",\n \"MDM^T02\",\n \"DFT^P03\",\n \"ACK\",\n];\n\n/** The segments after MSH in a synthetic HL7 payload. None of them carries a patient. */\nconst SEGMENT_NAMES = [\"EVN\", \"PV1\", \"ORC\", \"OBR\", \"OBX\", \"NTE\", \"ZXX\"];\n\n/** One synthetic segment after MSH: a name, then fields of letters and digits with components. */\nconst SEGMENT = fc\n .tuple(\n fc.constantFrom(...SEGMENT_NAMES),\n fc.array(fc.array(word(8), { minLength: 1, maxLength: 3 }), { maxLength: 6 }),\n )\n .map(([name, fields]) => [name, ...fields.map((components) => components.join(\"^\"))].join(\"|\"));\n\n/** A synthetic HL7 v2 message, each segment ended by a carriage return. */\nconst HL7_TEXT = fc\n .record({\n sender: word(6),\n receiver: word(6),\n when: fc.date({\n min: new Date(\"2000-01-01T00:00:00Z\"),\n max: new Date(\"2039-12-31T23:59:59Z\"),\n noInvalidDate: true,\n }),\n type: fc.constantFrom(...MESSAGE_TYPES),\n controlId: word(20).filter((id) => id.length > 0),\n segments: fc.array(SEGMENT, { maxLength: 4 }),\n })\n .map(({ sender, receiver, when, type, controlId, segments }) => {\n const ts = when.toISOString().replace(/\\D/gu, \"\").slice(0, 14);\n const msh = `MSH|^~\\\\&|${sender}|FAC|${receiver}|FAC|${ts}||${type}|${controlId}|P|2.5`;\n return [msh, ...segments].map((segment) => `${segment}\\r`).join(\"\");\n });\n\n/**\n * The bytes of one payload, as a plain array, before it becomes a `Buffer`.\n *\n * @example\n * ```ts\n * const bytes = fc.sample(payloadBytes({ content: \"text\" }), 1)[0];\n * ```\n */\nexport function payloadBytes(options: PayloadOptions = {}): fc.Arbitrary<number[]> {\n const content = options.content ?? \"bytes\";\n if (content === \"hl7\") return HL7_TEXT.map((text) => [...Buffer.from(text, \"latin1\")]);\n return fc.array(content === \"text\" ? TEXT_BYTE : PAYLOAD_BYTE, {\n minLength: options.minLength ?? 1,\n maxLength: options.maxLength ?? 256,\n });\n}\n\n/**\n * A payload the encoder accepts: a `Buffer` with no `VT` (`0x0B`) and no `FS` (`0x1C`) in it.\n *\n * Framed with `encodeFrame` and read back with a `FrameReader`, every one comes back byte for\n * byte. With the default `minLength` of 1 it also comes back with no warning; an empty payload\n * comes back with `MLLP_EMPTY_PAYLOAD`.\n *\n * @param options - What the payload holds and how long it is.\n * @returns An arbitrary of payload buffers.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { encodeFrame } from \"@cosyte/mllp\";\n * import { payload } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(fc.property(payload(), (p) => encodeFrame(p).length === p.length + 3));\n * ```\n */\nexport function payload(options: PayloadOptions = {}): fc.Arbitrary<Buffer> {\n return payloadBytes(options).map(cloneableBuffer);\n}\n\n/** A canonical frame and the payload it carries. */\nexport interface GeneratedFrame {\n /** The payload: what a reader must deliver for these bytes. */\n readonly payload: Buffer;\n /** The frame as it goes on the wire: `VT`, the payload, `FS`, `CR`. */\n readonly bytes: Buffer;\n}\n\n/** The canonical frame of a payload, written byte by byte. */\nfunction frameBytes(payloadBytes: readonly number[]): number[] {\n return [VT, ...payloadBytes, FS, CR];\n}\n\n/** Copy a frame, both buffers. */\nfunction copyFrame(frame: GeneratedFrame): GeneratedFrame {\n return { payload: Buffer.from(frame.payload), bytes: Buffer.from(frame.bytes) };\n}\n\n/**\n * A canonical MLLP frame, `VT` + payload + `FS` + `CR`, with the payload it carries.\n *\n * The bytes are written from the MLLP specification, not by `encodeFrame`, so they are an\n * independent check on the encoder: `encodeFrame(f.payload)` equals `f.bytes`.\n *\n * @param options - The payload's options, as for {@link payload}.\n * @returns An arbitrary of frames.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { encodeFrame } from \"@cosyte/mllp\";\n * import { frame } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(fc.property(frame(), (f) => encodeFrame(f.payload).equals(f.bytes)));\n * ```\n */\nexport function frame(options: PayloadOptions = {}): fc.Arbitrary<GeneratedFrame> {\n return payloadBytes(options).map((bytes) =>\n cloneable<GeneratedFrame>(\n { payload: Buffer.from(bytes), bytes: Buffer.from(frameBytes(bytes)) },\n copyFrame,\n ),\n );\n}\n\n/** Options for {@link chunked}. */\nexport interface ChunkOptions {\n /** The most places the bytes are cut. Default `16`. Two cuts in one place leave an empty chunk. */\n readonly maxCuts?: number;\n}\n\n/**\n * Cut `bytes` at the given offsets, sorted; repeated offsets leave empty chunks.\n *\n * @example\n * ```ts\n * cutAt(Buffer.from(\"ABC\"), [2, 1]); // A, B, C\n * ```\n */\nexport function cutAt(bytes: Buffer, cuts: readonly number[]): Buffer[] {\n const sorted = [...cuts].sort((a, b) => a - b);\n const chunks: Buffer[] = [];\n let start = 0;\n for (const cut of sorted) {\n chunks.push(Buffer.from(bytes.subarray(start, cut)));\n start = cut;\n }\n chunks.push(Buffer.from(bytes.subarray(start)));\n return chunks;\n}\n\n/**\n * A cloneable list of chunks.\n *\n * @example\n * ```ts\n * const chunks = cloneableChunks([Buffer.from(\"A\")]);\n * ```\n */\nexport function cloneableChunks(chunks: Buffer[]): Buffer[] {\n return cloneable(chunks, copyBuffers);\n}\n\n/**\n * The cut offsets for a run of `length` bytes.\n *\n * @example\n * ```ts\n * const at = fc.sample(cuts(10), 1)[0];\n * ```\n */\nexport function cuts(length: number, options: ChunkOptions = {}): fc.Arbitrary<number[]> {\n return fc.array(fc.integer({ min: 0, max: length }), { maxLength: options.maxCuts ?? 16 });\n}\n\n/**\n * The given bytes split into chunks at random places, the way a socket may deliver them: a frame\n * across many reads, many frames in one read, and now and then an empty read. Concatenated, the\n * chunks are always the bytes given. A case shrinks toward one chunk.\n *\n * @param bytes - The bytes to split.\n * @param options - How many cuts at most.\n * @returns An arbitrary of chunk lists.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { chunked } from \"@cosyte/mllp/arbitraries\";\n *\n * const bytes = Buffer.from(\"ABCDEF\");\n * fc.assert(fc.property(chunked(bytes), (chunks) => Buffer.concat(chunks).equals(bytes)));\n * ```\n */\nexport function chunked(bytes: Buffer, options: ChunkOptions = {}): fc.Arbitrary<Buffer[]> {\n const source = Buffer.from(bytes);\n return cuts(source.length, options).map((at) => cloneableChunks(cutAt(source, at)));\n}\n\n/** Options for {@link stream}. */\nexport interface StreamOptions extends PayloadOptions, ChunkOptions {\n /** The fewest frames in the stream. Default `1`. */\n readonly minFrames?: number;\n /** The most frames in the stream. Default `5`. */\n readonly maxFrames?: number;\n}\n\n/** A canonical byte stream: frames back to back, as chunks, with the payloads it carries. */\nexport interface GeneratedStream {\n /** The payloads in order: what a reader must deliver for this stream. */\n readonly payloads: readonly Buffer[];\n /** The whole stream: each payload's canonical frame, back to back. */\n readonly bytes: Buffer;\n /** The same bytes split at random places, the reads a socket may deliver. */\n readonly chunks: readonly Buffer[];\n}\n\n/**\n * Copy a stream, every buffer in it.\n *\n * @example\n * ```ts\n * const copy = copyStream(generated);\n * ```\n */\nexport function copyStream<T extends GeneratedStream>(stream: T): T {\n return {\n ...stream,\n payloads: copyBuffers(stream.payloads),\n bytes: Buffer.from(stream.bytes),\n chunks: copyBuffers(stream.chunks),\n };\n}\n\n/**\n * A canonical MLLP byte stream: one to five frames back to back, split into chunks at random\n * places, with the payloads a reader must deliver from it, in order and with no warning.\n *\n * @param options - The payloads' options, how many frames, and how many cuts at most.\n * @returns An arbitrary of streams.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader } from \"@cosyte/mllp\";\n * import { stream } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(\n * fc.property(stream(), (s) => {\n * const got: Buffer[] = [];\n * const reader = new FrameReader({ onFrame: (p) => got.push(p) });\n * for (const chunk of s.chunks) reader.push(chunk);\n * return got.length === s.payloads.length;\n * }),\n * );\n * ```\n */\nexport function stream(options: StreamOptions = {}): fc.Arbitrary<GeneratedStream> {\n return fc\n .array(payloadBytes(options), {\n minLength: options.minFrames ?? 1,\n maxLength: options.maxFrames ?? 5,\n })\n .chain((payloads) => {\n const bytes = Buffer.from(payloads.flatMap(frameBytes));\n return cuts(bytes.length, options).map((at) =>\n cloneable<GeneratedStream>(\n {\n payloads: payloads.map((p) => Buffer.from(p)),\n bytes,\n chunks: cutAt(bytes, at),\n },\n copyStream,\n ),\n );\n });\n}\n","/**\n * Messy streams a lenient reader accepts: the framing quirks real senders produce, each with the\n * payload and the warnings a `FrameReader` must report for it.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\nimport type { WarningCode } from \"../framing/registry.js\";\n\nimport {\n CR,\n FS,\n LF,\n VT,\n WHITESPACE,\n cloneable,\n copyStream,\n cutAt,\n cuts,\n payloadBytes,\n type GeneratedStream,\n type StreamOptions,\n} from \"./bytes.js\";\n\n/**\n * Options for {@link messyStream}: the payloads, the frames and the cuts as for a canonical stream,\n * and the tolerances of the reader the stream is meant for. A quirk whose tolerance is `false` is\n * never generated, so pass the same flags your `FrameReader` or server has. Each defaults to `true`.\n */\nexport interface MessyStreamOptions extends StreamOptions {\n /** Generate `FS` with no `CR` after it. */\n readonly allowFsOnly?: boolean;\n /** Generate `FS` then `LF` in place of `FS` then `CR`. */\n readonly allowLfAfterFs?: boolean;\n /** Generate a frame that opens with no `VT`. */\n readonly allowMissingLeadingVt?: boolean;\n /** Generate spaces, tabs, line feeds and carriage returns before a frame. */\n readonly allowLeadingWhitespace?: boolean;\n}\n\n/** A messy stream, with what a reader with the matching tolerances must deliver for it. */\nexport interface MessyStream extends GeneratedStream {\n /**\n * The warning codes each delivered frame must carry, in the order they are raised, one list per\n * payload. A frame with no quirk has an empty list.\n */\n readonly warnings: readonly (readonly WarningCode[])[];\n}\n\n/** How a frame opens. */\ntype Opening = \"vt\" | \"whitespace\" | \"no-vt\" | \"whitespace-no-vt\";\n/** How a frame ends. */\ntype Ending = \"cr\" | \"lf\" | \"fs-only\";\n\n/** One frame's choices, before the stream fixes them up so that they read as intended. */\ninterface FrameChoice {\n readonly opening: Opening;\n /** Whether the frame carries no payload. */\n readonly empty: boolean;\n readonly ending: Ending;\n readonly whitespace: readonly number[];\n readonly payload: readonly number[];\n}\n\n/** The parts of a messy stream built so far. */\ninterface Built {\n readonly bytes: number[];\n readonly payloads: Buffer[];\n readonly warnings: WarningCode[][];\n}\n\n/** The openings, bodies and endings the given tolerances accept. */\nfunction quirks(options: MessyStreamOptions): {\n openings: Opening[];\n endings: Ending[];\n} {\n const ws = options.allowLeadingWhitespace !== false;\n const noVt = options.allowMissingLeadingVt !== false;\n const openings: Opening[] = [\"vt\"];\n if (ws) openings.push(\"whitespace\");\n if (noVt) openings.push(\"no-vt\");\n if (ws && noVt) openings.push(\"whitespace-no-vt\");\n const endings: Ending[] = [\"cr\"];\n if (options.allowLfAfterFs !== false) endings.push(\"lf\");\n if (options.allowFsOnly !== false) endings.push(\"fs-only\");\n return { openings, endings };\n}\n\n/** One frame's random choices, within the given tolerances. */\nfunction frameChoice(options: MessyStreamOptions): fc.Arbitrary<FrameChoice> {\n const { openings, endings } = quirks(options);\n return fc.record({\n opening: fc.constantFrom(...openings),\n empty: fc.boolean(),\n ending: fc.constantFrom(...endings),\n whitespace: fc.array(fc.constantFrom(...WHITESPACE), { minLength: 1, maxLength: 4 }),\n payload: payloadBytes(options),\n });\n}\n\n/**\n * Append one frame to the stream, fixing its choices up where they would read otherwise:\n *\n * - a frame after an `fs-only` ending opens with its `VT`, which is what ends the frame before;\n * - a frame with no `VT` cannot open on whitespace a reader skips, so such a payload gets its `VT`;\n * - the last frame does not end `fs-only`, which only the next frame's `VT` completes.\n */\nfunction append(built: Built, choice: FrameChoice, mustOpenWithVt: boolean, last: boolean): void {\n const payload = choice.empty ? [] : [...choice.payload];\n let opening = mustOpenWithVt ? \"vt\" : choice.opening;\n const noVt = opening === \"no-vt\" || opening === \"whitespace-no-vt\";\n // An empty payload has no first byte, and `includes(undefined)` is false.\n if (noVt && WHITESPACE.includes(payload[0] as number)) {\n opening = opening === \"no-vt\" ? \"vt\" : \"whitespace\";\n }\n const ending = last && choice.ending === \"fs-only\" ? \"cr\" : choice.ending;\n\n const warnings: WarningCode[] = [];\n if (opening === \"whitespace\" || opening === \"whitespace-no-vt\") {\n built.bytes.push(...choice.whitespace);\n warnings.push(\"MLLP_LEADING_WHITESPACE\");\n }\n if (opening === \"no-vt\" || opening === \"whitespace-no-vt\") {\n warnings.push(\"MLLP_MISSING_LEADING_VT\");\n } else if (!mustOpenWithVt) {\n built.bytes.push(VT);\n }\n built.bytes.push(...payload, FS);\n if (payload.length === 0) warnings.push(\"MLLP_EMPTY_PAYLOAD\");\n if (ending === \"cr\") built.bytes.push(CR);\n else if (ending === \"lf\") {\n built.bytes.push(LF);\n warnings.push(\"MLLP_LF_AFTER_FS\");\n } else {\n // `fs-only` is ended by the next frame's VT, which that frame writes as its opening.\n built.bytes.push(VT);\n warnings.push(\"MLLP_FS_WITHOUT_CR\");\n }\n built.payloads.push(Buffer.from(payload));\n built.warnings.push(warnings);\n}\n\n/** Build a messy stream's bytes, payloads and warnings from its frames' choices. */\nfunction buildMessy(choices: readonly FrameChoice[]): Built {\n const built: Built = { bytes: [], payloads: [], warnings: [] };\n let mustOpenWithVt = false;\n choices.forEach((choice, index) => {\n const last = index === choices.length - 1;\n append(built, choice, mustOpenWithVt, last);\n mustOpenWithVt = !last && choice.ending === \"fs-only\";\n });\n return built;\n}\n\n/**\n * A messy MLLP byte stream a lenient reader accepts, split into chunks at random places, with the\n * payloads it must deliver and the warnings each must carry.\n *\n * Each frame may open with spaces, tabs, line feeds or carriage returns before its `VT`, or with no\n * `VT` at all; carry an empty payload; and end with `FS` and `LF`, or with `FS` and the next frame's\n * `VT`. Give it the tolerances your reader has, and it generates only what that reader accepts. It\n * never puts a `VT` inside a frame, nor an `FS` with anything but `CR`, `LF` or the next `VT` after\n * it: a reader refuses such a frame whole (`MLLP_PAYLOAD_CONTAINS_VT`, `MLLP_PAYLOAD_CONTAINS_FS`),\n * so no reader accepts it.\n *\n * @param options - The payloads, frames, cuts and tolerances.\n * @returns An arbitrary of messy streams.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader } from \"@cosyte/mllp\";\n * import { messyStream } from \"@cosyte/mllp/arbitraries\";\n *\n * const tolerances = { allowFsOnly: true, allowLfAfterFs: true, allowLeadingWhitespace: true };\n * fc.assert(\n * fc.property(messyStream({ ...tolerances, allowMissingLeadingVt: false }), (s) => {\n * const got: Buffer[] = [];\n * const reader = new FrameReader({ ...tolerances, onFrame: (p) => got.push(p) });\n * for (const chunk of s.chunks) reader.push(chunk);\n * return got.length === s.payloads.length;\n * }),\n * );\n * ```\n */\nexport function messyStream(options: MessyStreamOptions = {}): fc.Arbitrary<MessyStream> {\n return fc\n .array(frameChoice(options), {\n minLength: options.minFrames ?? 1,\n maxLength: options.maxFrames ?? 5,\n })\n .chain((choices) => {\n const built = buildMessy(choices);\n const bytes = Buffer.from(built.bytes);\n return cuts(bytes.length, options).map((at) =>\n cloneable<MessyStream>(\n {\n payloads: built.payloads.map((p) => Buffer.from(p)),\n warnings: built.warnings.map((codes) => [...codes]),\n bytes,\n chunks: cutAt(bytes, at),\n },\n copyStream,\n ),\n );\n });\n}\n","/**\n * Hostile byte streams: what a reader may meet from a broken sender, a port scanner or an attacker.\n *\n * @packageDocumentation\n */\n\nimport fc from \"fast-check\";\n\nimport { CR, FS, LF, VT, WHITESPACE, cloneableChunks, cutAt, cuts } from \"./bytes.js\";\nimport { messyStream } from \"./messy.js\";\n\n/** Options for {@link hostileInput}. */\nexport interface HostileInputOptions {\n /** The most bytes in the stream. Default `512`. */\n readonly maxLength?: number;\n /** The most places the stream is cut into chunks. Default `16`. */\n readonly maxCuts?: number;\n}\n\n/** A byte that is a framing byte or whitespace one time in two, and any byte otherwise. */\nconst FRAMING_HEAVY_BYTE = fc.oneof(\n fc.constantFrom(VT, FS, CR, LF, ...WHITESPACE),\n fc.integer({ min: 0, max: 0xff }),\n);\n\n/** One edit to a stream. `at` is taken modulo the stream's length at the time. */\ninterface Edit {\n readonly op: \"insert\" | \"delete\" | \"replace\" | \"truncate\" | \"repeat\";\n readonly at: number;\n readonly byte: number;\n}\n\nconst EDIT: fc.Arbitrary<Edit> = fc.record({\n op: fc.constantFrom<Edit[\"op\"]>(\"insert\", \"delete\", \"replace\", \"truncate\", \"repeat\"),\n at: fc.nat(),\n byte: FRAMING_HEAVY_BYTE,\n});\n\n/** Apply edits to a stream's bytes, in order. */\nfunction applyEdits(bytes: readonly number[], edits: readonly Edit[]): number[] {\n const out = [...bytes];\n for (const { op, at, byte } of edits) {\n const i = at % (out.length + 1);\n if (op === \"insert\") out.splice(i, 0, byte);\n else if (op === \"truncate\") out.length = i;\n else if (op === \"repeat\") out.splice(i, 0, ...out.slice(i, i + 8));\n else if (i < out.length) {\n if (op === \"delete\") out.splice(i, 1);\n else out[i] = byte;\n }\n }\n return out;\n}\n\n/**\n * A hostile MLLP byte stream as the chunks a socket delivers: random bytes, bytes thick with\n * `VT`, `FS`, `CR`, `LF` and whitespace, and messy streams with bytes inserted, deleted, replaced,\n * repeated and cut off. A reader may refuse one, but it must not hang, crash or throw anything it\n * does not document.\n *\n * @param options - The most bytes, and the most cuts.\n * @returns An arbitrary of chunk lists.\n * @example\n * ```ts\n * import fc from \"fast-check\";\n * import { FrameReader, MllpFramingError } from \"@cosyte/mllp\";\n * import { hostileInput } from \"@cosyte/mllp/arbitraries\";\n *\n * fc.assert(\n * fc.property(hostileInput(), (chunks) => {\n * const reader = new FrameReader({ onFrame: () => {} });\n * try {\n * for (const chunk of chunks) reader.push(chunk);\n * } catch (err) {\n * if (!(err instanceof MllpFramingError)) throw err;\n * }\n * }),\n * );\n * ```\n */\nexport function hostileInput(options: HostileInputOptions = {}): fc.Arbitrary<Buffer[]> {\n const maxLength = options.maxLength ?? 512;\n const bytes = fc.oneof(\n fc.array(fc.integer({ min: 0, max: 0xff }), { maxLength }),\n fc.array(FRAMING_HEAVY_BYTE, { maxLength }),\n fc\n .tuple(\n messyStream({ content: \"text\", maxLength: 32 }),\n fc.array(EDIT, { minLength: 1, maxLength: 4 }),\n )\n .map(([messy, edits]) => applyEdits([...messy.bytes], edits).slice(0, maxLength)),\n );\n return bytes.chain((list) => {\n const buffer = Buffer.from(list);\n return cuts(buffer.length, options).map((at) => cloneableChunks(cutAt(buffer, at)));\n });\n}\n"]}
|