@cosyte/mllp 0.1.7 → 0.1.8

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,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.8
4
+ ### Patch Changes
5
+
6
+ - 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.
7
+
3
8
  ## 0.1.7
4
9
  ### Patch Changes
5
10
 
package/README.md CHANGED
@@ -302,10 +302,12 @@ footnote.
302
302
  **What it does with your payload.** It moves the buffer you hand it to the peer and hands the peer's
303
303
  bytes back to your handler. It holds a message in memory only while that message is in flight: from
304
304
  `send()` until its acknowledgment is correlated, or until `close()` drains it or reports it
305
- unresolved. On the receive side your handler is given a copy, and the decoder then reuses its
306
- internal accumulator for the next frame rather than zeroing it, so those bytes stay in that private
307
- buffer until a later frame overwrites them, for as long as that reader lives. That is buffer reuse
308
- 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.
309
311
 
310
312
  **The reads the transport performs, and where they stop.** The transport does not parse HL7, but it
311
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)) {