@cosyte/mllp 0.1.13 → 0.1.14

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.cjs CHANGED
@@ -1530,9 +1530,12 @@ function segmentEnds(buf) {
1530
1530
  return atCr < atLf ? atCr : atLf;
1531
1531
  };
1532
1532
  }
1533
+ function firstSegmentStart(buf) {
1534
+ return buf[0] === 239 && buf[1] === 187 && buf[2] === 191 ? 3 : 0;
1535
+ }
1533
1536
  function findMshSegment(buf) {
1534
1537
  const segmentEnd = segmentEnds(buf);
1535
- let segStart = 0;
1538
+ let segStart = firstSegmentStart(buf);
1536
1539
  while (segStart < buf.length) {
1537
1540
  const end = segmentEnd(segStart);
1538
1541
  if (isMshSegmentAt(buf, segStart)) return { start: segStart, end };
@@ -1561,7 +1564,7 @@ function isBatchEnvelopeSegmentAt(buf, i) {
1561
1564
  function containsBatchOrExtraMessage(buf) {
1562
1565
  const segmentEnd = segmentEnds(buf);
1563
1566
  let mshCount = 0;
1564
- let segStart = 0;
1567
+ let segStart = firstSegmentStart(buf);
1565
1568
  while (segStart < buf.length) {
1566
1569
  const end = segmentEnd(segStart);
1567
1570
  if (isMshSegmentAt(buf, segStart)) {
@@ -1602,7 +1605,7 @@ function readMsaSegment(buf) {
1602
1605
  if (msh === null) return null;
1603
1606
  const fieldSep = msh.fieldSep.charCodeAt(0);
1604
1607
  const end = buf.length;
1605
- let segStart = 0;
1608
+ let segStart = firstSegmentStart(buf);
1606
1609
  while (segStart < end) {
1607
1610
  let segEnd = segStart;
1608
1611
  while (segEnd < end && buf[segEnd] !== SEGMENT_SEPARATOR_CR && buf[segEnd] !== SEGMENT_SEPARATOR_LF) {
@@ -1830,7 +1833,10 @@ var ACK_MODE_MESSAGES = Object.freeze({
1830
1833
  MLLP_ACK_COMMIT_ALREADY_REPORTED: "A further commit-accept acknowledgement arrived for a send whose commit disposition had already been reported; it was surfaced and neither re-reported nor allowed to restart the application-acknowledgement wait. The send is still pending.",
1831
1834
  MLLP_ACK_MSA1_ABSENT: "An acknowledgement matched to an enhanced-mode send carried no MSA-1 acknowledgement code, so it was not classified into a mode and the send was left pending. Field values are withheld; msa1Bytes gives the field's length.",
1832
1835
  MLLP_ACK_MSA1_UNCLASSIFIABLE: "An acknowledgement matched to an enhanced-mode send carried an MSA-1 value outside HL7 Table 0008, so it was not classified into a mode and the send was left pending. Field values are withheld; msa1Bytes gives the field's length.",
1833
- MLLP_ACK_SEND_ALREADY_DISPOSED: "An acknowledgement arrived for a send that had already been settled or failed; it was dropped and that outcome was left unchanged. Field values are withheld: a control ID is payload content and this diagnostic goes to a log."
1836
+ MLLP_ACK_SEND_ALREADY_DISPOSED: "An acknowledgement arrived for a send that had already been settled or failed; it was dropped and that outcome was left unchanged. Field values are withheld: a control ID is payload content and this diagnostic goes to a log.",
1837
+ // Stryker disable StringLiteral: a module-level initializer runs once, when the module is first loaded, so per-test coverage credits it to whichever test loaded the module first rather than to the tests that assert this value (test/client/fifo-unreadable-ack.test.ts asserts it literally).
1838
+ MLLP_ACK_FIFO_MSA1_UNREADABLE: "A client correlating acknowledgements in order settled its oldest pending send on a frame with no readable MSA-1 acknowledgement code, so the peer has not acknowledged that message in HL7 terms. The send resolved with the frame, or rejected when fifoUnreadableAck is 'reject'. Set correlateByControlId to match acknowledgements by MSA-2 instead. Field values are withheld; msa1Bytes gives the field's length."
1839
+ // Stryker restore StringLiteral
1834
1840
  });
1835
1841
  function ackModeDiagnosticMessage(code) {
1836
1842
  return ACK_MODE_MESSAGES[code];
@@ -2842,6 +2848,7 @@ var Correlator = class {
2842
2848
  onCommitReported: opts.onCommitReported,
2843
2849
  onCommitRejected: opts.onCommitRejected,
2844
2850
  onApplicationAckTimeout: opts.onApplicationAckTimeout,
2851
+ onUnreadableAck: opts.onUnreadableAck,
2845
2852
  now: opts.now ?? Date.now
2846
2853
  };
2847
2854
  }
@@ -2906,6 +2913,9 @@ var Correlator = class {
2906
2913
  *
2907
2914
  * - FIFO: returns first pending entry by insertion order; entry is
2908
2915
  * removed from live store. Caller calls `entry.resolve(ackPayload)`.
2916
+ * A frame with no readable MSA-1 is reported as
2917
+ * `MLLP_ACK_FIFO_MSA1_UNREADABLE` first, and when `onUnreadableAck` is
2918
+ * wired it is handed the entry instead and `null` is returned.
2909
2919
  * - controlId: keyed lookup by `controlIdFromAck`. Live-store hit returns
2910
2920
  * the entry; graveyard hit fires `MLLP_ACK_AFTER_TIMEOUT` warning
2911
2921
  * and returns `null`; otherwise fires `onUnmatchedAck` with the id's
@@ -2932,7 +2942,24 @@ var Correlator = class {
2932
2942
  this._pending.delete(entry.key);
2933
2943
  this._queueBytes -= entry.byteCount;
2934
2944
  if (entry.sentAt !== null) this._inFlight -= 1;
2935
- return entry;
2945
+ const classification = classifyMsa1(payload);
2946
+ if (classification.kind === "code") return entry;
2947
+ const elapsedSinceSendMs = entry.sentAt === null ? 0 : this._opts.now() - entry.sentAt;
2948
+ this._ackModeWarn("MLLP_ACK_FIFO_MSA1_UNREADABLE", {
2949
+ msa1Bytes: classification.byteLength,
2950
+ ackCode: null,
2951
+ controlIdBytes: null,
2952
+ byteOffset: byteOffsetFromAck,
2953
+ elapsedSinceSendMs
2954
+ });
2955
+ if (this._opts.onUnreadableAck === void 0) return entry;
2956
+ this._opts.onUnreadableAck(
2957
+ entry,
2958
+ classification.byteLength,
2959
+ byteOffsetFromAck,
2960
+ elapsedSinceSendMs
2961
+ );
2962
+ return null;
2936
2963
  }
2937
2964
  if (controlIdFromAck === null) {
2938
2965
  if (this._opts.onUnmatchedAck !== void 0) {
@@ -3279,6 +3306,30 @@ var MllpApplicationAckError = class _MllpApplicationAckError extends Error {
3279
3306
  }
3280
3307
  }
3281
3308
  };
3309
+ var MllpUnreadableAckError = class _MllpUnreadableAckError extends Error {
3310
+ name = "MllpUnreadableAckError";
3311
+ /** Byte length of the MSA-1 field read, `0` when the frame has no `MSH` or no `MSA`. */
3312
+ msa1Bytes;
3313
+ /** Stream byte offset of the frame that settled the send. */
3314
+ byteOffset;
3315
+ /** Milliseconds between the send's write-flush and that frame. */
3316
+ elapsedMs;
3317
+ /**
3318
+ * Construct an unreadable-acknowledgement error.
3319
+ *
3320
+ * @param message - Human-readable error message. Structural facts only, never field content.
3321
+ * @param opts - Failure context (MSA-1 byte length, stream offset, elapsed time).
3322
+ */
3323
+ constructor(message, opts) {
3324
+ super(message);
3325
+ this.msa1Bytes = opts.msa1Bytes;
3326
+ this.byteOffset = opts.byteOffset;
3327
+ this.elapsedMs = opts.elapsedMs;
3328
+ if (Error.captureStackTrace) {
3329
+ Error.captureStackTrace(this, _MllpUnreadableAckError);
3330
+ }
3331
+ }
3332
+ };
3282
3333
  var MllpCommitRejectedError = class _MllpCommitRejectedError extends Error {
3283
3334
  name = "MllpCommitRejectedError";
3284
3335
  /** The negative accept-mode code the peer sent. */
@@ -3491,6 +3542,8 @@ var MllpClient = class extends events.EventEmitter {
3491
3542
  _applicationAckTimeoutMs;
3492
3543
  /** controlId-mode flag. `false` → FIFO. */
3493
3544
  _correlateByControlId;
3545
+ /** Whether a FIFO send settled on a frame with no readable MSA-1 rejects rather than resolves. */
3546
+ _rejectUnreadableAck;
3494
3547
  /** Unified ACK correlator. Built during `_attachConnection`. */
3495
3548
  _correlator = null;
3496
3549
  /**
@@ -3610,6 +3663,12 @@ var MllpClient = class extends events.EventEmitter {
3610
3663
  "ClientOptions.connectTimeoutMs must be an integer from 1 to 2147483647, or left unset for no bound"
3611
3664
  );
3612
3665
  }
3666
+ const fifoUnreadableAck = opts.fifoUnreadableAck;
3667
+ if (fifoUnreadableAck !== void 0 && fifoUnreadableAck !== "warn" && fifoUnreadableAck !== "reject") {
3668
+ throw new TypeError(
3669
+ "ClientOptions.fifoUnreadableAck must be 'warn' or 'reject', or left unset for 'warn'"
3670
+ );
3671
+ }
3613
3672
  const tls = copyTlsOptions(opts.tls);
3614
3673
  checkCredentialMix(tls);
3615
3674
  this._pfxRefusal = typeof tls === "object" ? pfxRefusal(tls.pfx, tls.passphrase) : void 0;
@@ -3618,6 +3677,7 @@ var MllpClient = class extends events.EventEmitter {
3618
3677
  this._ackTimeoutMs = opts.ackTimeoutMs ?? 3e4;
3619
3678
  this._applicationAckTimeoutMs = opts.applicationAckTimeoutMs;
3620
3679
  this._correlateByControlId = opts.correlateByControlId === true;
3680
+ this._rejectUnreadableAck = fifoUnreadableAck === "reject";
3621
3681
  this._autoReconnect = opts.autoReconnect === true;
3622
3682
  this._initialDelayMs = opts.initialDelayMs ?? 100;
3623
3683
  this._maxDelayMs = opts.maxDelayMs ?? 3e4;
@@ -4109,7 +4169,18 @@ var MllpClient = class extends events.EventEmitter {
4109
4169
  },
4110
4170
  onAckModeWarning: (code, ctx) => {
4111
4171
  this._emitAckModeWarning(code, ctx, conn);
4112
- }
4172
+ },
4173
+ ...this._rejectUnreadableAck ? {
4174
+ onUnreadableAck: (entry, msa1Bytes, byteOffset, elapsedMs) => {
4175
+ entry.reject(
4176
+ new MllpUnreadableAckError(
4177
+ "the frame that settled this send in order carried no readable MSA-1, so the peer has not acknowledged it in HL7 terms",
4178
+ { msa1Bytes, byteOffset, elapsedMs }
4179
+ )
4180
+ );
4181
+ this._maybeEmitDrain();
4182
+ }
4183
+ } : {}
4113
4184
  });
4114
4185
  }
4115
4186
  if (this._ackSweepTimer === null) {
@@ -4542,6 +4613,12 @@ var MllpClient = class extends events.EventEmitter {
4542
4613
  * client's). The clock starts at the underlying `write()` flush callback, NOT at
4543
4614
  * the `send()` call.
4544
4615
  * - `MllpConnectionError({ phase: 'send' })` if the client is not connected.
4616
+ * - `MllpUnreadableAckError` with `fifoUnreadableAck: 'reject'`, when the frame that settles
4617
+ * this send in order carries no readable MSA-1.
4618
+ *
4619
+ * Resolving is not acceptance: read MSA-1 off the acknowledgement you are handed. In FIFO
4620
+ * mode a frame with no readable MSA-1 still resolves by default, with the warning
4621
+ * `MLLP_ACK_FIFO_MSA1_UNREADABLE`.
4545
4622
  *
4546
4623
  * Emits a frozen `'ack'` event on every successful match.
4547
4624
  *
@@ -5199,6 +5276,7 @@ async function createStarterClient(opts) {
5199
5276
  pipeline: opts.pipeline ?? true,
5200
5277
  highWaterMark: opts.highWaterMark ?? 64,
5201
5278
  onBackpressure: opts.onBackpressure ?? "reject",
5279
+ fifoUnreadableAck: opts.fifoUnreadableAck ?? "warn",
5202
5280
  ...opts.drainTimeoutMs !== void 0 ? { drainTimeoutMs: opts.drainTimeoutMs } : {},
5203
5281
  ...opts.framing !== void 0 ? { framing: opts.framing } : {},
5204
5282
  ...opts.retryStrategy !== void 0 ? { retryStrategy: opts.retryStrategy } : {},
@@ -5526,7 +5604,7 @@ async function runDifferential(options = {}) {
5526
5604
  }
5527
5605
 
5528
5606
  // src/index.ts
5529
- var VERSION = "0.1.13";
5607
+ var VERSION = "0.1.14";
5530
5608
 
5531
5609
  exports.ATNA_CIPHER_LIST = ATNA_CIPHER_LIST;
5532
5610
  exports.ATNA_CIPHER_SUITES = ATNA_CIPHER_SUITES;
@@ -5557,6 +5635,7 @@ exports.MllpServer = MllpServer;
5557
5635
  exports.MllpTimeoutError = MllpTimeoutError;
5558
5636
  exports.MllpTlsConfigurationError = MllpTlsConfigurationError;
5559
5637
  exports.MllpUnknownFateError = MllpUnknownFateError;
5638
+ exports.MllpUnreadableAckError = MllpUnreadableAckError;
5560
5639
  exports.NetTransport = NetTransport;
5561
5640
  exports.TLS13_DEFAULT_CIPHER_SUITES = TLS13_DEFAULT_CIPHER_SUITES;
5562
5641
  exports.TlsTransport = TlsTransport;