@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/dist/index.d.cts CHANGED
@@ -269,9 +269,17 @@ declare class FrameReader {
269
269
  private readonly _opts;
270
270
  private readonly _maxFrameSize;
271
271
  private _state;
272
- /** Reusable accumulator buffer, grown by doubling as needed. */
272
+ /** Reusable 4 KiB block the current frame's latest bytes are gathered in. */
273
273
  private _accumulator;
274
- /** Number of payload bytes written to accumulator in current frame. */
274
+ /** Number of the current frame's bytes in `_accumulator`. */
275
+ private _blockUsed;
276
+ /**
277
+ * The current frame's earlier bytes, in order, once it outgrew one block: full blocks, and
278
+ * exact copies of runs longer than a block. Dropped with the frame, so a reader holds no more
279
+ * than the frame it is part way through.
280
+ */
281
+ private _parts;
282
+ /** Number of payload bytes in the current frame, `_parts` and `_accumulator` together. */
275
283
  private _writePos;
276
284
  /** Monotonic absolute stream byte offset across the entire connection lifetime. */
277
285
  private _byteOffset;
@@ -300,6 +308,30 @@ declare class FrameReader {
300
308
  * ```
301
309
  */
302
310
  push(chunk: Buffer): void;
311
+ /** The byte-at-a-time reading, for a chunk that is not a byte array. */
312
+ private _pushEachByte;
313
+ /**
314
+ * How many more payload bytes the size cap admits. A byte is admitted while
315
+ * `writePos < maxFrameSizeBytes`, so a fractional cap admits up to its ceiling, a cap already
316
+ * reached admits none, and a `NaN` cap, which that comparison never refuses, admits any number.
317
+ */
318
+ private _room;
319
+ /**
320
+ * Append the ordinary payload bytes `chunk[start, stop)`, none of them FS or VT, in one copy.
321
+ * The size cap holds as it did byte by byte: every byte it admits is appended, and the first it
322
+ * refuses throws at that byte's offset.
323
+ */
324
+ private _appendRun;
325
+ /** Refuse the payload byte at the current offset: the frame has reached its size cap. */
326
+ private _throwTooLarge;
327
+ /**
328
+ * Add `src[start, end)` to the current frame: into the block while it has room; then the full
329
+ * block becomes a part, a run of a block or more is copied as one part of its own, and what is
330
+ * left starts a fresh block.
331
+ */
332
+ private _gather;
333
+ /** Forget the current frame's bytes: the block is kept for the next frame, the parts dropped. */
334
+ private _discard;
303
335
  /**
304
336
  * Clear internal accumulator state and reset byte offset to 0.
305
337
  *
@@ -316,13 +348,18 @@ declare class FrameReader {
316
348
  private _scanForVt;
317
349
  private _readPayload;
318
350
  private _expectCr;
319
- /** Grow the internal accumulator by doubling and write one byte. */
351
+ /** Add one byte to the current frame, starting a fresh block when this one is full. */
320
352
  private _appendByte;
321
353
  /**
322
354
  * Copy accumulated payload bytes into a new Buffer and deliver via `onFrame`.
323
355
  * The copy isolates callers from internal accumulator reuse.
324
356
  */
325
357
  private _deliverFrame;
358
+ /**
359
+ * Deliver `payload` via `onFrame` with this frame's start offset and warnings, the frame's
360
+ * bytes forgotten first.
361
+ */
362
+ private _deliver;
326
363
  /** Emit a warning via the `onWarning` callback, swallowing any handler exceptions. */
327
364
  private _emitWarning;
328
365
  }
@@ -2404,6 +2441,7 @@ declare function createStarterServer(opts: StarterServerOptions): Promise<MllpSe
2404
2441
  *
2405
2442
  * @packageDocumentation
2406
2443
  */
2444
+
2407
2445
  /**
2408
2446
  * HL7 Table 0008, Acknowledgment Code. A **stable public API**.
2409
2447
  *
package/dist/index.d.ts CHANGED
@@ -269,9 +269,17 @@ declare class FrameReader {
269
269
  private readonly _opts;
270
270
  private readonly _maxFrameSize;
271
271
  private _state;
272
- /** Reusable accumulator buffer, grown by doubling as needed. */
272
+ /** Reusable 4 KiB block the current frame's latest bytes are gathered in. */
273
273
  private _accumulator;
274
- /** Number of payload bytes written to accumulator in current frame. */
274
+ /** Number of the current frame's bytes in `_accumulator`. */
275
+ private _blockUsed;
276
+ /**
277
+ * The current frame's earlier bytes, in order, once it outgrew one block: full blocks, and
278
+ * exact copies of runs longer than a block. Dropped with the frame, so a reader holds no more
279
+ * than the frame it is part way through.
280
+ */
281
+ private _parts;
282
+ /** Number of payload bytes in the current frame, `_parts` and `_accumulator` together. */
275
283
  private _writePos;
276
284
  /** Monotonic absolute stream byte offset across the entire connection lifetime. */
277
285
  private _byteOffset;
@@ -300,6 +308,30 @@ declare class FrameReader {
300
308
  * ```
301
309
  */
302
310
  push(chunk: Buffer): void;
311
+ /** The byte-at-a-time reading, for a chunk that is not a byte array. */
312
+ private _pushEachByte;
313
+ /**
314
+ * How many more payload bytes the size cap admits. A byte is admitted while
315
+ * `writePos < maxFrameSizeBytes`, so a fractional cap admits up to its ceiling, a cap already
316
+ * reached admits none, and a `NaN` cap, which that comparison never refuses, admits any number.
317
+ */
318
+ private _room;
319
+ /**
320
+ * Append the ordinary payload bytes `chunk[start, stop)`, none of them FS or VT, in one copy.
321
+ * The size cap holds as it did byte by byte: every byte it admits is appended, and the first it
322
+ * refuses throws at that byte's offset.
323
+ */
324
+ private _appendRun;
325
+ /** Refuse the payload byte at the current offset: the frame has reached its size cap. */
326
+ private _throwTooLarge;
327
+ /**
328
+ * Add `src[start, end)` to the current frame: into the block while it has room; then the full
329
+ * block becomes a part, a run of a block or more is copied as one part of its own, and what is
330
+ * left starts a fresh block.
331
+ */
332
+ private _gather;
333
+ /** Forget the current frame's bytes: the block is kept for the next frame, the parts dropped. */
334
+ private _discard;
303
335
  /**
304
336
  * Clear internal accumulator state and reset byte offset to 0.
305
337
  *
@@ -316,13 +348,18 @@ declare class FrameReader {
316
348
  private _scanForVt;
317
349
  private _readPayload;
318
350
  private _expectCr;
319
- /** Grow the internal accumulator by doubling and write one byte. */
351
+ /** Add one byte to the current frame, starting a fresh block when this one is full. */
320
352
  private _appendByte;
321
353
  /**
322
354
  * Copy accumulated payload bytes into a new Buffer and deliver via `onFrame`.
323
355
  * The copy isolates callers from internal accumulator reuse.
324
356
  */
325
357
  private _deliverFrame;
358
+ /**
359
+ * Deliver `payload` via `onFrame` with this frame's start offset and warnings, the frame's
360
+ * bytes forgotten first.
361
+ */
362
+ private _deliver;
326
363
  /** Emit a warning via the `onWarning` callback, swallowing any handler exceptions. */
327
364
  private _emitWarning;
328
365
  }
@@ -2404,6 +2441,7 @@ declare function createStarterServer(opts: StarterServerOptions): Promise<MllpSe
2404
2441
  *
2405
2442
  * @packageDocumentation
2406
2443
  */
2444
+
2407
2445
  /**
2408
2446
  * HL7 Table 0008, Acknowledgment Code. A **stable public API**.
2409
2447
  *
package/dist/index.mjs CHANGED
@@ -67,26 +67,17 @@ var DEFAULT_MAX_FRAME_SIZE = 16 * 1024 * 1024;
67
67
  function encodeFrame(payload, opts) {
68
68
  const allow = opts?.allowDelimiterBytesInPayload === true;
69
69
  const onWarning = opts?.onWarning;
70
- for (let i = 0; i < payload.length; i++) {
71
- const byte = payload[i];
72
- if (byte === void 0) break;
73
- if (byte === VT || byte === FS) {
74
- const code = byte === VT ? "MLLP_PAYLOAD_CONTAINS_VT" : "MLLP_PAYLOAD_CONTAINS_FS";
75
- if (!allow) {
76
- throw new MllpFramingError(code, i, Buffer.from([byte]));
77
- }
78
- if (onWarning !== void 0) {
79
- const byteHex = byte === VT ? "0x0B" : "0x1C";
80
- const warning = createWarning(
81
- code,
82
- i,
83
- `Payload contains ${byte === VT ? "VT" : "FS"} byte (${byteHex}) at offset ${i}; frame will be ambiguous`
84
- );
85
- try {
86
- onWarning(warning);
87
- } catch {
88
- }
89
- }
70
+ if (!(payload instanceof Uint8Array)) {
71
+ scanEachByte(payload, allow, onWarning);
72
+ } else if (!allow || onWarning !== void 0) {
73
+ let vt = payload.indexOf(VT);
74
+ let fs = payload.indexOf(FS);
75
+ while (vt !== -1 || fs !== -1) {
76
+ const isVt = fs === -1 || vt !== -1 && vt < fs;
77
+ const i = isVt ? vt : fs;
78
+ delimiterAt(isVt ? VT : FS, i, allow, onWarning);
79
+ if (isVt) vt = payload.indexOf(VT, i + 1);
80
+ else fs = payload.indexOf(FS, i + 1);
90
81
  }
91
82
  }
92
83
  const frame = Buffer.allocUnsafe(payload.length + 3);
@@ -96,17 +87,50 @@ function encodeFrame(payload, opts) {
96
87
  frame[payload.length + 2] = CR;
97
88
  return frame;
98
89
  }
90
+ function scanEachByte(payload, allow, onWarning) {
91
+ for (let i = 0; i < payload.length; i++) {
92
+ const byte = payload[i];
93
+ if (byte === void 0) break;
94
+ if (byte === VT || byte === FS) delimiterAt(byte, i, allow, onWarning);
95
+ }
96
+ }
97
+ function delimiterAt(byte, i, allow, onWarning) {
98
+ const code = byte === VT ? "MLLP_PAYLOAD_CONTAINS_VT" : "MLLP_PAYLOAD_CONTAINS_FS";
99
+ if (!allow) {
100
+ throw new MllpFramingError(code, i, Buffer.from([byte]));
101
+ }
102
+ if (onWarning !== void 0) {
103
+ const byteHex = byte === VT ? "0x0B" : "0x1C";
104
+ const warning = createWarning(
105
+ code,
106
+ i,
107
+ `Payload contains ${byte === VT ? "VT" : "FS"} byte (${byteHex}) at offset ${i}; frame will be ambiguous`
108
+ );
109
+ try {
110
+ onWarning(warning);
111
+ } catch {
112
+ }
113
+ }
114
+ }
99
115
 
100
116
  // src/framing/decoder.ts
101
- var INITIAL_ACCUMULATOR_SIZE = 4096;
117
+ var BLOCK_SIZE = 4096;
102
118
  var EMPTY_SNIPPET = Buffer.alloc(0);
103
119
  var FrameReader = class {
104
120
  _opts;
105
121
  _maxFrameSize;
106
122
  _state = "SCANNING_FOR_VT";
107
- /** Reusable accumulator buffer, grown by doubling as needed. */
108
- _accumulator = Buffer.allocUnsafe(INITIAL_ACCUMULATOR_SIZE);
109
- /** Number of payload bytes written to accumulator in current frame. */
123
+ /** Reusable 4 KiB block the current frame's latest bytes are gathered in. */
124
+ _accumulator = Buffer.allocUnsafe(BLOCK_SIZE);
125
+ /** Number of the current frame's bytes in `_accumulator`. */
126
+ _blockUsed = 0;
127
+ /**
128
+ * The current frame's earlier bytes, in order, once it outgrew one block: full blocks, and
129
+ * exact copies of runs longer than a block. Dropped with the frame, so a reader holds no more
130
+ * than the frame it is part way through.
131
+ */
132
+ _parts = [];
133
+ /** Number of payload bytes in the current frame, `_parts` and `_accumulator` together. */
110
134
  _writePos = 0;
111
135
  /** Monotonic absolute stream byte offset across the entire connection lifetime. */
112
136
  _byteOffset = 0;
@@ -138,12 +162,115 @@ var FrameReader = class {
138
162
  * ```
139
163
  */
140
164
  push(chunk) {
165
+ if (!(chunk instanceof Uint8Array)) {
166
+ this._pushEachByte(chunk);
167
+ return;
168
+ }
169
+ const end = chunk.length;
170
+ let nextFs = -2;
171
+ let nextVt = -2;
172
+ let i = 0;
173
+ while (i < end) {
174
+ if (this._state === "READING_PAYLOAD") {
175
+ if (nextFs !== -1 && nextFs < i) nextFs = chunk.indexOf(FS, i);
176
+ if (nextVt !== -1 && nextVt < i) nextVt = chunk.indexOf(VT, i);
177
+ let stop = nextFs === -1 ? end : nextFs;
178
+ if (nextVt !== -1 && nextVt < stop) stop = nextVt;
179
+ if (stop > i) {
180
+ if (this._writePos === 0 && stop === nextFs && stop + 1 < end && chunk[stop + 1] === CR && stop - i <= this._room()) {
181
+ const payload = Buffer.from(chunk.subarray(i, stop));
182
+ this._byteOffset += stop - i + 1;
183
+ this._state = "EXPECTING_CR";
184
+ this._deliver(payload);
185
+ this._state = "SCANNING_FOR_VT";
186
+ this._byteOffset++;
187
+ i = stop + 2;
188
+ continue;
189
+ }
190
+ this._appendRun(chunk, i, stop);
191
+ i = stop;
192
+ continue;
193
+ }
194
+ }
195
+ this._processByte(chunk[i]);
196
+ i++;
197
+ this._byteOffset++;
198
+ }
199
+ }
200
+ /** The byte-at-a-time reading, for a chunk that is not a byte array. */
201
+ _pushEachByte(chunk) {
141
202
  for (let i = 0; i < chunk.length; i++, this._byteOffset++) {
142
203
  const byte = chunk[i];
143
204
  if (byte === void 0) break;
144
205
  this._processByte(byte);
145
206
  }
146
207
  }
208
+ /**
209
+ * How many more payload bytes the size cap admits. A byte is admitted while
210
+ * `writePos < maxFrameSizeBytes`, so a fractional cap admits up to its ceiling, a cap already
211
+ * reached admits none, and a `NaN` cap, which that comparison never refuses, admits any number.
212
+ */
213
+ _room() {
214
+ const room = this._maxFrameSize - this._writePos;
215
+ if (Number.isNaN(room)) return Infinity;
216
+ return room > 0 ? Math.ceil(room) : 0;
217
+ }
218
+ /**
219
+ * Append the ordinary payload bytes `chunk[start, stop)`, none of them FS or VT, in one copy.
220
+ * The size cap holds as it did byte by byte: every byte it admits is appended, and the first it
221
+ * refuses throws at that byte's offset.
222
+ */
223
+ _appendRun(chunk, start, stop) {
224
+ const length = stop - start;
225
+ const take = Math.min(length, this._room());
226
+ if (take > 0) {
227
+ this._gather(chunk, start, start + take);
228
+ this._byteOffset += take;
229
+ }
230
+ if (take < length) this._throwTooLarge();
231
+ }
232
+ /** Refuse the payload byte at the current offset: the frame has reached its size cap. */
233
+ _throwTooLarge() {
234
+ throw new MllpFramingError(
235
+ "MLLP_FRAME_TOO_LARGE",
236
+ this._byteOffset,
237
+ EMPTY_SNIPPET,
238
+ `Frame payload exceeded maxFrameSizeBytes (${this._maxFrameSize}) at offset ${this._byteOffset}`
239
+ );
240
+ }
241
+ /**
242
+ * Add `src[start, end)` to the current frame: into the block while it has room; then the full
243
+ * block becomes a part, a run of a block or more is copied as one part of its own, and what is
244
+ * left starts a fresh block.
245
+ */
246
+ _gather(src, start, end) {
247
+ let at = start;
248
+ const fill = Math.min(end - at, BLOCK_SIZE - this._blockUsed);
249
+ if (fill > 0) {
250
+ this._accumulator.set(src.subarray(at, at + fill), this._blockUsed);
251
+ this._blockUsed += fill;
252
+ at += fill;
253
+ }
254
+ if (at < end) {
255
+ this._parts.push(this._accumulator);
256
+ this._accumulator = Buffer.allocUnsafe(BLOCK_SIZE);
257
+ const rest = end - at;
258
+ if (rest >= BLOCK_SIZE) {
259
+ this._parts.push(Buffer.from(src.subarray(at, end)));
260
+ this._blockUsed = 0;
261
+ } else {
262
+ this._accumulator.set(src.subarray(at, end), 0);
263
+ this._blockUsed = rest;
264
+ }
265
+ }
266
+ this._writePos += end - start;
267
+ }
268
+ /** Forget the current frame's bytes: the block is kept for the next frame, the parts dropped. */
269
+ _discard() {
270
+ this._writePos = 0;
271
+ this._blockUsed = 0;
272
+ if (this._parts.length > 0) this._parts = [];
273
+ }
147
274
  /**
148
275
  * Clear internal accumulator state and reset byte offset to 0.
149
276
  *
@@ -157,7 +284,7 @@ var FrameReader = class {
157
284
  */
158
285
  reset() {
159
286
  this._state = "SCANNING_FOR_VT";
160
- this._writePos = 0;
287
+ this._discard();
161
288
  this._byteOffset = 0;
162
289
  this._wsStart = 0;
163
290
  this._wsCount = 0;
@@ -273,18 +400,11 @@ var FrameReader = class {
273
400
  this._byteOffset,
274
401
  `Unexpected VT (0x0B) mid-payload at offset ${this._byteOffset}; discarding ${this._writePos} accumulated bytes`
275
402
  );
276
- this._writePos = 0;
403
+ this._discard();
277
404
  this._state = "READING_PAYLOAD";
278
405
  return;
279
406
  }
280
- if (this._writePos >= this._maxFrameSize) {
281
- throw new MllpFramingError(
282
- "MLLP_FRAME_TOO_LARGE",
283
- this._byteOffset,
284
- EMPTY_SNIPPET,
285
- `Frame payload exceeded maxFrameSizeBytes (${this._maxFrameSize}) at offset ${this._byteOffset}`
286
- );
287
- }
407
+ if (this._writePos >= this._maxFrameSize) this._throwTooLarge();
288
408
  this._appendByte(byte);
289
409
  }
290
410
  _expectCr(byte) {
@@ -376,23 +496,35 @@ var FrameReader = class {
376
496
  `Expected CR (0x0D) after FS, got 0x${byte.toString(16).padStart(2, "0")} at offset ${this._byteOffset}`
377
497
  );
378
498
  }
379
- /** Grow the internal accumulator by doubling and write one byte. */
499
+ /** Add one byte to the current frame, starting a fresh block when this one is full. */
380
500
  _appendByte(byte) {
381
- if (this._writePos >= this._accumulator.length) {
382
- const newSize = this._accumulator.length * 2;
383
- const grown = Buffer.allocUnsafe(newSize);
384
- this._accumulator.subarray(0, this._writePos).copy(grown);
385
- this._accumulator = grown;
501
+ if (this._blockUsed === BLOCK_SIZE) {
502
+ this._parts.push(this._accumulator);
503
+ this._accumulator = Buffer.allocUnsafe(BLOCK_SIZE);
504
+ this._blockUsed = 0;
386
505
  }
387
- this._accumulator[this._writePos++] = byte;
506
+ this._accumulator[this._blockUsed++] = byte;
507
+ this._writePos++;
388
508
  }
389
509
  /**
390
510
  * Copy accumulated payload bytes into a new Buffer and deliver via `onFrame`.
391
511
  * The copy isolates callers from internal accumulator reuse.
392
512
  */
393
513
  _deliverFrame() {
394
- const payload = Buffer.from(this._accumulator.subarray(0, this._writePos));
395
- this._writePos = 0;
514
+ const block = this._accumulator.subarray(0, this._blockUsed);
515
+ if (this._parts.length === 0) {
516
+ this._deliver(Buffer.from(block));
517
+ return;
518
+ }
519
+ this._parts.push(block);
520
+ this._deliver(Buffer.concat(this._parts, this._writePos));
521
+ }
522
+ /**
523
+ * Deliver `payload` via `onFrame` with this frame's start offset and warnings, the frame's
524
+ * bytes forgotten first.
525
+ */
526
+ _deliver(payload) {
527
+ this._discard();
396
528
  const frameStart = this._frameStartOffset;
397
529
  const frameWarnings = this._frameWarnings;
398
530
  this._frameWarnings = [];
@@ -1262,17 +1394,22 @@ var UNSAFE_FIELD_SEPARATORS = /* @__PURE__ */ new Set([
1262
1394
  function isMshSegmentAt(buf, i) {
1263
1395
  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]);
1264
1396
  }
1265
- function segmentEnd(buf, i) {
1266
- let end = i;
1267
- while (end < buf.length && buf[end] !== SEGMENT_SEPARATOR_CR && buf[end] !== SEGMENT_SEPARATOR_LF) {
1268
- end++;
1269
- }
1270
- return end;
1397
+ function segmentEnds(buf) {
1398
+ let cr = -2;
1399
+ let lf = -2;
1400
+ return (i) => {
1401
+ if (cr !== -1 && cr < i) cr = buf.indexOf(SEGMENT_SEPARATOR_CR, i);
1402
+ if (lf !== -1 && lf < i) lf = buf.indexOf(SEGMENT_SEPARATOR_LF, i);
1403
+ const atCr = cr === -1 ? buf.length : cr;
1404
+ const atLf = lf === -1 ? buf.length : lf;
1405
+ return atCr < atLf ? atCr : atLf;
1406
+ };
1271
1407
  }
1272
1408
  function findMshSegment(buf) {
1409
+ const segmentEnd = segmentEnds(buf);
1273
1410
  let segStart = 0;
1274
1411
  while (segStart < buf.length) {
1275
- const end = segmentEnd(buf, segStart);
1412
+ const end = segmentEnd(segStart);
1276
1413
  if (isMshSegmentAt(buf, segStart)) return { start: segStart, end };
1277
1414
  let next = end;
1278
1415
  while (next < buf.length && (buf[next] === SEGMENT_SEPARATOR_CR || buf[next] === SEGMENT_SEPARATOR_LF)) {
@@ -1297,10 +1434,11 @@ function isBatchEnvelopeSegmentAt(buf, i) {
1297
1434
  return false;
1298
1435
  }
1299
1436
  function containsBatchOrExtraMessage(buf) {
1437
+ const segmentEnd = segmentEnds(buf);
1300
1438
  let mshCount = 0;
1301
1439
  let segStart = 0;
1302
1440
  while (segStart < buf.length) {
1303
- const end = segmentEnd(buf, segStart);
1441
+ const end = segmentEnd(segStart);
1304
1442
  if (isMshSegmentAt(buf, segStart)) {
1305
1443
  mshCount++;
1306
1444
  if (mshCount > 1) return true;
@@ -1389,11 +1527,21 @@ var DEFAULT_FIELD_SEPARATOR = "|";
1389
1527
  var ENCODING_CHAR_SUBSTITUTE = "#";
1390
1528
  var UNSAFE_DELIMITER = /[\r\n\v\x1c]/;
1391
1529
  function rawAckUncorrelatable(payload) {
1392
- const msh = readMshSegment(payload);
1530
+ return uncorrelatable(readInbound(payload));
1531
+ }
1532
+ function readInbound(payload) {
1533
+ let batch;
1534
+ return {
1535
+ msh: readMshSegment(payload),
1536
+ batch: () => batch ??= containsBatchOrExtraMessage(payload)
1537
+ };
1538
+ }
1539
+ function uncorrelatable(inbound) {
1540
+ const { msh } = inbound;
1393
1541
  if (msh === null) return true;
1394
1542
  const controlId = msh.fields[9];
1395
1543
  if (controlId === void 0 || controlId === "") return true;
1396
- return containsBatchOrExtraMessage(payload);
1544
+ return inbound.batch();
1397
1545
  }
1398
1546
  function downgradePositiveAck(code) {
1399
1547
  if (code === "AA") return "AE";
@@ -1401,8 +1549,11 @@ function downgradePositiveAck(code) {
1401
1549
  return code;
1402
1550
  }
1403
1551
  function buildRawAck(payload, code) {
1404
- const msh = readMshSegment(payload);
1405
- if ((code === "AA" || code === "CA") && rawAckUncorrelatable(payload)) {
1552
+ return buildRawAckFrom(readInbound(payload), code);
1553
+ }
1554
+ function buildRawAckFrom(inbound, code) {
1555
+ const { msh } = inbound;
1556
+ if ((code === "AA" || code === "CA") && uncorrelatable(inbound)) {
1406
1557
  code = downgradePositiveAck(code);
1407
1558
  }
1408
1559
  const newControlId = randomUUID().replace(/-/g, "").substring(0, 20);
@@ -2187,18 +2338,21 @@ var MllpServer = class extends EventEmitter {
2187
2338
  */
2188
2339
  async _sendCommitAck(payload, meta, conn) {
2189
2340
  const handler = this._opts.onMessage;
2190
- const selectCode = this._ackModeSelector(payload, conn);
2341
+ const inbound = readInbound(payload);
2342
+ const selectCode = this._ackModeSelector(inbound, conn);
2191
2343
  if (handler === void 0) {
2192
2344
  this._dispatchAck(
2193
2345
  conn,
2194
- buildRawAck(payload, this._resolveAutoAckPositive(payload, meta, conn, selectCode))
2346
+ buildRawAckFrom(inbound, this._resolveAutoAckPositive(inbound, meta, conn, selectCode))
2195
2347
  );
2196
2348
  return;
2197
2349
  }
2198
2350
  let code;
2351
+ let settled;
2199
2352
  try {
2200
2353
  await handler(payload, meta, conn);
2201
- code = this._resolveAutoAckPositive(payload, meta, conn, selectCode);
2354
+ settled = readInbound(payload);
2355
+ code = this._resolveAutoAckPositive(settled, meta, conn, selectCode);
2202
2356
  } catch (err) {
2203
2357
  const nack = resolveNackCode(err);
2204
2358
  code = selectCode(nack);
@@ -2211,7 +2365,8 @@ var MllpServer = class extends EventEmitter {
2211
2365
  })
2212
2366
  );
2213
2367
  }
2214
- this._dispatchAck(conn, buildRawAck(payload, code));
2368
+ settled ??= readInbound(payload);
2369
+ this._dispatchAck(conn, buildRawAckFrom(settled, code));
2215
2370
  }
2216
2371
  /**
2217
2372
  * Resolve the auto-ACK code for a message the server would otherwise acknowledge
@@ -2240,10 +2395,9 @@ var MllpServer = class extends EventEmitter {
2240
2395
  * `selectCode` puts the resolved disposition in the half of HL7 Table 0008 the inbound
2241
2396
  * asked for; it is the identity for a message that asked for nothing.
2242
2397
  */
2243
- _resolveAutoAckPositive(payload, meta, conn, selectCode) {
2398
+ _resolveAutoAckPositive(inbound, meta, conn, selectCode) {
2244
2399
  const discarded = meta.warnings.some((w) => w.code === "MLLP_TRAILING_BYTES");
2245
- const uncorrelatable = rawAckUncorrelatable(payload);
2246
- if (!discarded && !uncorrelatable) return selectCode("AA");
2400
+ if (!discarded && !uncorrelatable(inbound)) return selectCode("AA");
2247
2401
  const reason = discarded ? "discarded-bytes" : "uncorrelatable-inbound";
2248
2402
  const code = selectCode("AE");
2249
2403
  this._emitContained(
@@ -2284,9 +2438,9 @@ var MllpServer = class extends EventEmitter {
2284
2438
  * Downgrading a message the handler committed would make a discharged sender resend a
2285
2439
  * message already in storage.
2286
2440
  */
2287
- _ackModeSelector(payload, conn) {
2441
+ _ackModeSelector(inbound, conn) {
2288
2442
  const identity = (disposition) => disposition;
2289
- const msh = readMshSegment(payload);
2443
+ const { msh } = inbound;
2290
2444
  if (msh === null) return identity;
2291
2445
  const accept = readAcceptCondition(msh);
2292
2446
  if (accept.kind === "unrecognised") {
@@ -2294,7 +2448,7 @@ var MllpServer = class extends EventEmitter {
2294
2448
  return identity;
2295
2449
  }
2296
2450
  if (accept.kind === "null") return identity;
2297
- if (containsBatchOrExtraMessage(payload)) return identity;
2451
+ if (inbound.batch()) return identity;
2298
2452
  return (disposition) => acceptAckRequested(accept, disposition) ? acceptModeCounterpart(disposition) : disposition;
2299
2453
  }
2300
2454
  /**
@@ -4995,7 +5149,7 @@ async function runDifferential(options = {}) {
4995
5149
  }
4996
5150
 
4997
5151
  // src/index.ts
4998
- var VERSION = "0.1.7";
5152
+ var VERSION = "0.1.8";
4999
5153
 
5000
5154
  export { ATNA_CIPHER_LIST, ATNA_CIPHER_SUITES, Connection, FrameReader, MLLP_BIND_ALL_INTERFACES, MLLP_DIFF_PEER_UNPARSEABLE, MLLP_TLS_CIPHER_LIST_REJECTED, MLLP_TLS_CIPHER_OPTION_CONFLICT, MLLP_TLS_DH_PARAMETERS_REJECTED, MLLP_TLS_VERIFY_DISABLED, MllpAckError, MllpApplicationAckError, MllpBackpressureError, MllpClient, MllpCommitRejectedError, MllpConnectionError, MllpDifferentialConfigurationError, MllpFramingError, MllpNeverDeliveredError, MllpServer, MllpTimeoutError, MllpTlsConfigurationError, MllpUnknownFateError, NetTransport, TLS13_DEFAULT_CIPHER_SUITES, TlsTransport, VERSION, ackDiagnosticMessage, ackModeDiagnosticMessage, buildRawAck, canonicalAcknowledgement, canonicalExchanges, createClient, createServer, createStarterClient, createStarterServer, createWarning, differentialConfigurationMessage, encodeFrame, isTlsProtocolError, isTlsVerificationErrorCode, isTransientConnectionError, rawAckUncorrelatable, readNegotiatedTlsParameters, resolveDifferentialPeer, resolveNackCode, resolveTlsCipherPolicy, runDifferential, tlsConfigurationMessage };
5001
5155
  //# sourceMappingURL=index.mjs.map