@cosyte/mllp 0.1.6 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,34 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.9
4
+ ### Patch Changes
5
+
6
+ - b3f843c: Add the npm keywords `client` and `server` so the package is easier to find in npm search.
7
+
8
+ ## 0.1.8
9
+ ### Patch Changes
10
+
11
+ - e4792e9: Faster framing and acknowledgement, with every decoded frame, framing error, encoded frame and ACK byte unchanged. `FrameReader` now finds frame boundaries with a native search and copies a payload in bulk rather than a byte at a time, and keeps a frame's bytes only until the frame is delivered, so a receiver no longer holds the size of the largest message it ever read (up to the 16 MiB frame limit) for the life of the connection. `encodeFrame` checks a payload for `VT` and `FS` with a native search. The server's auto-ACK path reads a message's MSH header once on arrival, and once more after an `onMessage` handler settles (a handler may rewrite its payload in place, and the ACK answers what it left), instead of about four times.
12
+
13
+ ## 0.1.7
14
+ ### Patch Changes
15
+
16
+ - 772368f: New `@cosyte/mllp/arbitraries` entry point: fast-check generators for MLLP, so you can property-test your own code on generated frames and byte streams. `payload()` gives payloads the encoder accepts (any bytes, text, or synthetic HL7 v2 messages); `frame()` gives a canonical frame with its payload; `stream()` gives canonical frames back to back, split into random reads, with the payloads a reader must deliver; `messyStream()` gives the framing quirks real senders produce, limited to the tolerances you name, with the payloads and warning codes a reader must report; `hostileInput()` gives random, framing-heavy and mangled streams in random reads; and `chunked(bytes)` splits bytes of your own into random reads. Every generated value is synthetic and random, and a failing case reports the value as it was generated even if your code changed its bytes. `fast-check` (4.x) is an optional peer dependency: install it to use this entry point. The main entry point never loads it, so nothing changes for code that only imports `@cosyte/mllp`.
17
+ - b75745f: `close()` now resolves as soon as its connections are gone. A second `MllpServer.close()` with a
18
+ shorter `drainTimeoutMs` already destroyed a straggling connection at its own deadline, but its
19
+ promise, and the first call's, stayed pending until the first call's longer drain timed out. The
20
+ same held for `Connection.close()`: a `destroy()` or a peer close during a slow drain ended the
21
+ connection at once, yet `close()` waited out the drain timeout anyway. Every pending `close()` now
22
+ resolves the moment its connection leaves `DRAINING`, by whatever route, so a shutdown with a short
23
+ budget ends within that budget.
24
+ - 945a934: The documentation follows our docs-content standard: goal-named how-to guides for sending and receiving messages, tolerating framing quirks and building ACKs with `@cosyte/hl7`; a reference page of every warning and error code; troubleshooting; testing and performance pages; and an API reference generated from the doc comments. Every runnable sample is type-checked and run against the built package. The doc comments in the published types now use bold lines where they used Markdown headings. No API or behavior changes.
25
+ - b75745f: `Connection.notifyConnect()` on a connection that was already closed kept it `CLOSED` and emitted
26
+ no `stateChange`, yet still emitted `'connect'` and recorded a peer address and connect time, so a
27
+ subscriber could count a connection that never opened. It now does nothing unless the connection is
28
+ `CONNECTING`: a closed connection emits nothing, and a connect reported twice emits `'connect'`
29
+ once.
30
+ - 772368f: With `allowMissingLeadingVt` on, a stray `FS` where a frame was due (a sender repeating its `FS CR` trailer) is now read as an empty frame, with `MLLP_MISSING_LEADING_VT` and `MLLP_EMPTY_PAYLOAD`, instead of arriving as the first byte of the next message's payload, where it broke that message and could not be framed again. A frame that opens without a `VT` is now also held to `maxFrameSizeBytes` from its first byte.
31
+
3
32
  ## 0.1.6
4
33
  ### Patch Changes
5
34
 
package/README.md CHANGED
@@ -73,7 +73,10 @@ pnpm add @cosyte/hl7
73
73
 
74
74
  Node.js 22 or later, tested on 22, 24, and 26 (`engines.node` is `>=22.0.0`). The package ships
75
75
  dual ESM (`import`) and CommonJS (`require`) builds, each with its own type declarations, and has
76
- zero runtime dependencies.
76
+ zero runtime dependencies. The optional `@cosyte/mllp/arbitraries` entry point, fast-check
77
+ generators for
78
+ [property-testing your own code](https://github.com/cosyte/mllp/blob/main/docs-content/property-test-your-code.md),
79
+ needs `fast-check` installed beside it; the main entry point never loads it.
77
80
 
78
81
  ## Usage
79
82
 
@@ -239,13 +242,14 @@ The full reference is on [docs.cosyte.com/mllp](https://docs.cosyte.com/mllp); s
239
242
  is the one place a whole message is parsed. [PHI and safety](#phi-and-safety) sets all of that out
240
243
  in full. For actual parsing, pair this with [`@cosyte/hl7`](https://github.com/cosyte/hl7), which
241
244
  is exactly what that subpath does.
242
- - **Differentially verified against freely available engines**: Mirth Connect from NextGen, and the
243
- Google Cloud Healthcare MLLP adapter. **Epic and Cerner are not part of that harness**, and no
244
- claim about either is made here. `runDifferential` ships in the published artifact, so you can aim
245
+ - **Framing checked against canonical frames for freely available engines**: Mirth Connect from
246
+ NextGen, and the Google Cloud Healthcare MLLP adapter. The frames are written from the
247
+ specification, not captured from a running engine, and no live engine runs in our CI. **Epic and
248
+ Cerner are not part of that harness**, and no claim about either is made here. `runDifferential` ships in the published artifact, so you can aim
245
249
  it at your own test or staging endpoint and get a per-exchange frame-parity and `MSA-2` correlation
246
250
  report for the engine you actually integrate with.
247
- - **Known gaps, stated rather than hidden.** No batch acknowledgment, no MLLP Release 2, no queue or
248
- replay of unacknowledged messages, no clinical acceptance decision, and no PKI. The
251
+ - **Known gaps, stated rather than hidden.** No batch acknowledgment, no MLLP Release 2, no persistent
252
+ queue of unacknowledged messages, no clinical acceptance decision, and no PKI. The
249
253
  [known limitations](https://github.com/cosyte/mllp/blob/main/docs-content/limitations.md) page is
250
254
  the full list. Read it before you depend on this.
251
255
 
@@ -255,16 +259,20 @@ The full reference lives at [docs.cosyte.com/mllp](https://docs.cosyte.com/mllp)
255
259
  repository's [`docs-content/`](https://github.com/cosyte/mllp/tree/main/docs-content). Start with
256
260
  these pages:
257
261
 
258
- - [Framing and tolerance](https://github.com/cosyte/mllp/blob/main/docs-content/framing.md): the
259
- wire format, the opt-in tolerance flags, the stable warning codes, and what throws.
262
+ - [Send messages](https://github.com/cosyte/mllp/blob/main/docs-content/send-messages.md) and
263
+ [Receive and acknowledge messages](https://github.com/cosyte/mllp/blob/main/docs-content/receive-messages.md): the two everyday tasks.
260
264
  - [ACKs and the commit contract](https://github.com/cosyte/mllp/blob/main/docs-content/acks.md):
261
265
  the code auto-ACK selects, correlation on the client, enhanced mode, and the ack-from-hl7 builder.
262
- - [Connection, reconnect, and backpressure](https://github.com/cosyte/mllp/blob/main/docs-content/reliability.md):
266
+ - [Framing and tolerance](https://github.com/cosyte/mllp/blob/main/docs-content/framing.md): the
267
+ wire format, the opt-in tolerance flags, the stable warning codes, and what throws.
268
+ - [Connection, reconnect and backpressure](https://github.com/cosyte/mllp/blob/main/docs-content/reliability.md):
263
269
  the six-state connection machine, backoff, dead-peer detection, and what `close()` does.
264
- - [MLLPS / TLS](https://github.com/cosyte/mllp/blob/main/docs-content/tls.md): mutual TLS,
270
+ - [Secure a link with TLS](https://github.com/cosyte/mllp/blob/main/docs-content/tls.md): mutual TLS,
265
271
  verification, bind safety, the TLS 1.2 floor, and the typed failure modes.
266
- - [Testing and verification](https://github.com/cosyte/mllp/blob/main/docs-content/testing.md): the
267
- in-memory transport, and the differential harness for the engine you integrate with.
272
+ - [Test your code and verify an engine](https://github.com/cosyte/mllp/blob/main/docs-content/testing.md):
273
+ the in-memory transport, and the differential harness for the engine you integrate with.
274
+ - [Warning and error codes](https://github.com/cosyte/mllp/blob/main/docs-content/warning-and-error-codes.md)
275
+ and [Troubleshooting](https://github.com/cosyte/mllp/blob/main/docs-content/troubleshooting.md): every stable code, and what to do when a link fails.
268
276
  - [Known limitations and non-goals](https://github.com/cosyte/mllp/blob/main/docs-content/limitations.md):
269
277
  what not to trust this transport to do.
270
278
 
@@ -276,6 +284,9 @@ agreement on the approach before a pull request. The organization's
276
284
  [support page](https://github.com/cosyte/.github/blob/main/SUPPORT.md) covers questions that span our
277
285
  libraries.
278
286
 
287
+ Need an MLLP integration built, or hosted and run for you? See our
288
+ [integration services](https://cosyte.com/integration-services).
289
+
279
290
  ## Security
280
291
 
281
292
  Report a vulnerability privately, as the
@@ -291,10 +302,12 @@ footnote.
291
302
  **What it does with your payload.** It moves the buffer you hand it to the peer and hands the peer's
292
303
  bytes back to your handler. It holds a message in memory only while that message is in flight: from
293
304
  `send()` until its acknowledgment is correlated, or until `close()` drains it or reports it
294
- unresolved. On the receive side your handler is given a copy, and the decoder then reuses its
295
- internal accumulator for the next frame rather than zeroing it, so those bytes stay in that private
296
- buffer until a later frame overwrites them, for as long as that reader lives. That is buffer reuse
297
- rather than storage, and no public API hands the buffer out, but it is not a scrub.
305
+ unresolved. On the receive side your handler is given a copy. A frame that arrived across reads was
306
+ gathered in the decoder's internal accumulator, which is never zeroed: a frame of up to 4 KiB stays
307
+ in that private buffer until a later frame overwrites it, for as long as that reader lives, and the
308
+ buffer that held a larger one is dropped once the frame is delivered and left to the garbage
309
+ collector. That is buffer reuse rather than storage, and no public API hands the buffer out, but it
310
+ is not a scrub.
298
311
 
299
312
  **The reads the transport performs, and where they stop.** The transport does not parse HL7, but it
300
313
  is not blind to the payload either, and the bound is worth more to you than the slogan. To answer a
@@ -68,26 +68,17 @@ function createWarning(code, byteOffset, message) {
68
68
  function encodeFrame(payload, opts) {
69
69
  const allow = opts?.allowDelimiterBytesInPayload === true;
70
70
  const onWarning = opts?.onWarning;
71
- for (let i = 0; i < payload.length; i++) {
72
- const byte = payload[i];
73
- if (byte === void 0) break;
74
- if (byte === VT || byte === FS) {
75
- const code = byte === VT ? "MLLP_PAYLOAD_CONTAINS_VT" : "MLLP_PAYLOAD_CONTAINS_FS";
76
- if (!allow) {
77
- throw new MllpFramingError(code, i, Buffer.from([byte]));
78
- }
79
- if (onWarning !== void 0) {
80
- const byteHex = byte === VT ? "0x0B" : "0x1C";
81
- const warning = createWarning(
82
- code,
83
- i,
84
- `Payload contains ${byte === VT ? "VT" : "FS"} byte (${byteHex}) at offset ${i}; frame will be ambiguous`
85
- );
86
- try {
87
- onWarning(warning);
88
- } catch {
89
- }
90
- }
71
+ if (!(payload instanceof Uint8Array)) {
72
+ scanEachByte(payload, allow, onWarning);
73
+ } else if (!allow || onWarning !== void 0) {
74
+ let vt = payload.indexOf(VT);
75
+ let fs = payload.indexOf(FS);
76
+ while (vt !== -1 || fs !== -1) {
77
+ const isVt = fs === -1 || vt !== -1 && vt < fs;
78
+ const i = isVt ? vt : fs;
79
+ delimiterAt(isVt ? VT : FS, i, allow, onWarning);
80
+ if (isVt) vt = payload.indexOf(VT, i + 1);
81
+ else fs = payload.indexOf(FS, i + 1);
91
82
  }
92
83
  }
93
84
  const frame = Buffer.allocUnsafe(payload.length + 3);
@@ -97,6 +88,31 @@ function encodeFrame(payload, opts) {
97
88
  frame[payload.length + 2] = CR;
98
89
  return frame;
99
90
  }
91
+ function scanEachByte(payload, allow, onWarning) {
92
+ for (let i = 0; i < payload.length; i++) {
93
+ const byte = payload[i];
94
+ if (byte === void 0) break;
95
+ if (byte === VT || byte === FS) delimiterAt(byte, i, allow, onWarning);
96
+ }
97
+ }
98
+ function delimiterAt(byte, i, allow, onWarning) {
99
+ const code = byte === VT ? "MLLP_PAYLOAD_CONTAINS_VT" : "MLLP_PAYLOAD_CONTAINS_FS";
100
+ if (!allow) {
101
+ throw new MllpFramingError(code, i, Buffer.from([byte]));
102
+ }
103
+ if (onWarning !== void 0) {
104
+ const byteHex = byte === VT ? "0x0B" : "0x1C";
105
+ const warning = createWarning(
106
+ code,
107
+ i,
108
+ `Payload contains ${byte === VT ? "VT" : "FS"} byte (${byteHex}) at offset ${i}; frame will be ambiguous`
109
+ );
110
+ try {
111
+ onWarning(warning);
112
+ } catch {
113
+ }
114
+ }
115
+ }
100
116
 
101
117
  // src/internal/control-id.ts
102
118
  var ASCII_M = 77;
@@ -117,17 +133,22 @@ var UNSAFE_FIELD_SEPARATORS = /* @__PURE__ */ new Set([
117
133
  function isMshSegmentAt(buf, i) {
118
134
  return i + 3 < buf.length && buf[i] === ASCII_M && buf[i + 1] === ASCII_S && buf[i + 2] === ASCII_H && !UNSAFE_FIELD_SEPARATORS.has(buf[i + 3]);
119
135
  }
120
- function segmentEnd(buf, i) {
121
- let end = i;
122
- while (end < buf.length && buf[end] !== SEGMENT_SEPARATOR_CR && buf[end] !== SEGMENT_SEPARATOR_LF) {
123
- end++;
124
- }
125
- return end;
136
+ function segmentEnds(buf) {
137
+ let cr = -2;
138
+ let lf = -2;
139
+ return (i) => {
140
+ if (cr !== -1 && cr < i) cr = buf.indexOf(SEGMENT_SEPARATOR_CR, i);
141
+ if (lf !== -1 && lf < i) lf = buf.indexOf(SEGMENT_SEPARATOR_LF, i);
142
+ const atCr = cr === -1 ? buf.length : cr;
143
+ const atLf = lf === -1 ? buf.length : lf;
144
+ return atCr < atLf ? atCr : atLf;
145
+ };
126
146
  }
127
147
  function findMshSegment(buf) {
148
+ const segmentEnd = segmentEnds(buf);
128
149
  let segStart = 0;
129
150
  while (segStart < buf.length) {
130
- const end = segmentEnd(buf, segStart);
151
+ const end = segmentEnd(segStart);
131
152
  if (isMshSegmentAt(buf, segStart)) return { start: segStart, end };
132
153
  let next = end;
133
154
  while (next < buf.length && (buf[next] === SEGMENT_SEPARATOR_CR || buf[next] === SEGMENT_SEPARATOR_LF)) {