@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 +29 -0
- package/README.md +29 -16
- package/dist/ack-from-hl7/index.cjs +48 -27
- package/dist/ack-from-hl7/index.cjs.map +1 -1
- package/dist/ack-from-hl7/index.d.cts +1 -1
- package/dist/ack-from-hl7/index.d.ts +1 -1
- package/dist/ack-from-hl7/index.mjs +48 -27
- package/dist/ack-from-hl7/index.mjs.map +1 -1
- package/dist/arbitraries/index.cjs +282 -0
- package/dist/arbitraries/index.cjs.map +1 -0
- package/dist/arbitraries/index.d.cts +245 -0
- package/dist/arbitraries/index.d.ts +245 -0
- package/dist/arbitraries/index.mjs +271 -0
- package/dist/arbitraries/index.mjs.map +1 -0
- package/dist/index.cjs +276 -82
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +74 -103
- package/dist/index.d.ts +74 -103
- package/dist/index.mjs +276 -82
- package/dist/index.mjs.map +1 -1
- package/dist/registry-CyGU_l8N.d.cts +86 -0
- package/dist/registry-CyGU_l8N.d.ts +86 -0
- package/package.json +26 -4
package/dist/index.cjs
CHANGED
|
@@ -69,26 +69,17 @@ var DEFAULT_MAX_FRAME_SIZE = 16 * 1024 * 1024;
|
|
|
69
69
|
function encodeFrame(payload, opts) {
|
|
70
70
|
const allow = opts?.allowDelimiterBytesInPayload === true;
|
|
71
71
|
const onWarning = opts?.onWarning;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
code,
|
|
84
|
-
i,
|
|
85
|
-
`Payload contains ${byte === VT ? "VT" : "FS"} byte (${byteHex}) at offset ${i}; frame will be ambiguous`
|
|
86
|
-
);
|
|
87
|
-
try {
|
|
88
|
-
onWarning(warning);
|
|
89
|
-
} catch {
|
|
90
|
-
}
|
|
91
|
-
}
|
|
72
|
+
if (!(payload instanceof Uint8Array)) {
|
|
73
|
+
scanEachByte(payload, allow, onWarning);
|
|
74
|
+
} else if (!allow || onWarning !== void 0) {
|
|
75
|
+
let vt = payload.indexOf(VT);
|
|
76
|
+
let fs = payload.indexOf(FS);
|
|
77
|
+
while (vt !== -1 || fs !== -1) {
|
|
78
|
+
const isVt = fs === -1 || vt !== -1 && vt < fs;
|
|
79
|
+
const i = isVt ? vt : fs;
|
|
80
|
+
delimiterAt(isVt ? VT : FS, i, allow, onWarning);
|
|
81
|
+
if (isVt) vt = payload.indexOf(VT, i + 1);
|
|
82
|
+
else fs = payload.indexOf(FS, i + 1);
|
|
92
83
|
}
|
|
93
84
|
}
|
|
94
85
|
const frame = Buffer.allocUnsafe(payload.length + 3);
|
|
@@ -98,17 +89,50 @@ function encodeFrame(payload, opts) {
|
|
|
98
89
|
frame[payload.length + 2] = CR;
|
|
99
90
|
return frame;
|
|
100
91
|
}
|
|
92
|
+
function scanEachByte(payload, allow, onWarning) {
|
|
93
|
+
for (let i = 0; i < payload.length; i++) {
|
|
94
|
+
const byte = payload[i];
|
|
95
|
+
if (byte === void 0) break;
|
|
96
|
+
if (byte === VT || byte === FS) delimiterAt(byte, i, allow, onWarning);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function delimiterAt(byte, i, allow, onWarning) {
|
|
100
|
+
const code = byte === VT ? "MLLP_PAYLOAD_CONTAINS_VT" : "MLLP_PAYLOAD_CONTAINS_FS";
|
|
101
|
+
if (!allow) {
|
|
102
|
+
throw new MllpFramingError(code, i, Buffer.from([byte]));
|
|
103
|
+
}
|
|
104
|
+
if (onWarning !== void 0) {
|
|
105
|
+
const byteHex = byte === VT ? "0x0B" : "0x1C";
|
|
106
|
+
const warning = createWarning(
|
|
107
|
+
code,
|
|
108
|
+
i,
|
|
109
|
+
`Payload contains ${byte === VT ? "VT" : "FS"} byte (${byteHex}) at offset ${i}; frame will be ambiguous`
|
|
110
|
+
);
|
|
111
|
+
try {
|
|
112
|
+
onWarning(warning);
|
|
113
|
+
} catch {
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
101
117
|
|
|
102
118
|
// src/framing/decoder.ts
|
|
103
|
-
var
|
|
119
|
+
var BLOCK_SIZE = 4096;
|
|
104
120
|
var EMPTY_SNIPPET = Buffer.alloc(0);
|
|
105
121
|
var FrameReader = class {
|
|
106
122
|
_opts;
|
|
107
123
|
_maxFrameSize;
|
|
108
124
|
_state = "SCANNING_FOR_VT";
|
|
109
|
-
/** Reusable
|
|
110
|
-
_accumulator = Buffer.allocUnsafe(
|
|
111
|
-
/** Number of
|
|
125
|
+
/** Reusable 4 KiB block the current frame's latest bytes are gathered in. */
|
|
126
|
+
_accumulator = Buffer.allocUnsafe(BLOCK_SIZE);
|
|
127
|
+
/** Number of the current frame's bytes in `_accumulator`. */
|
|
128
|
+
_blockUsed = 0;
|
|
129
|
+
/**
|
|
130
|
+
* The current frame's earlier bytes, in order, once it outgrew one block: full blocks, and
|
|
131
|
+
* exact copies of runs longer than a block. Dropped with the frame, so a reader holds no more
|
|
132
|
+
* than the frame it is part way through.
|
|
133
|
+
*/
|
|
134
|
+
_parts = [];
|
|
135
|
+
/** Number of payload bytes in the current frame, `_parts` and `_accumulator` together. */
|
|
112
136
|
_writePos = 0;
|
|
113
137
|
/** Monotonic absolute stream byte offset across the entire connection lifetime. */
|
|
114
138
|
_byteOffset = 0;
|
|
@@ -140,12 +164,115 @@ var FrameReader = class {
|
|
|
140
164
|
* ```
|
|
141
165
|
*/
|
|
142
166
|
push(chunk) {
|
|
167
|
+
if (!(chunk instanceof Uint8Array)) {
|
|
168
|
+
this._pushEachByte(chunk);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const end = chunk.length;
|
|
172
|
+
let nextFs = -2;
|
|
173
|
+
let nextVt = -2;
|
|
174
|
+
let i = 0;
|
|
175
|
+
while (i < end) {
|
|
176
|
+
if (this._state === "READING_PAYLOAD") {
|
|
177
|
+
if (nextFs !== -1 && nextFs < i) nextFs = chunk.indexOf(FS, i);
|
|
178
|
+
if (nextVt !== -1 && nextVt < i) nextVt = chunk.indexOf(VT, i);
|
|
179
|
+
let stop = nextFs === -1 ? end : nextFs;
|
|
180
|
+
if (nextVt !== -1 && nextVt < stop) stop = nextVt;
|
|
181
|
+
if (stop > i) {
|
|
182
|
+
if (this._writePos === 0 && stop === nextFs && stop + 1 < end && chunk[stop + 1] === CR && stop - i <= this._room()) {
|
|
183
|
+
const payload = Buffer.from(chunk.subarray(i, stop));
|
|
184
|
+
this._byteOffset += stop - i + 1;
|
|
185
|
+
this._state = "EXPECTING_CR";
|
|
186
|
+
this._deliver(payload);
|
|
187
|
+
this._state = "SCANNING_FOR_VT";
|
|
188
|
+
this._byteOffset++;
|
|
189
|
+
i = stop + 2;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
this._appendRun(chunk, i, stop);
|
|
193
|
+
i = stop;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
this._processByte(chunk[i]);
|
|
198
|
+
i++;
|
|
199
|
+
this._byteOffset++;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/** The byte-at-a-time reading, for a chunk that is not a byte array. */
|
|
203
|
+
_pushEachByte(chunk) {
|
|
143
204
|
for (let i = 0; i < chunk.length; i++, this._byteOffset++) {
|
|
144
205
|
const byte = chunk[i];
|
|
145
206
|
if (byte === void 0) break;
|
|
146
207
|
this._processByte(byte);
|
|
147
208
|
}
|
|
148
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* How many more payload bytes the size cap admits. A byte is admitted while
|
|
212
|
+
* `writePos < maxFrameSizeBytes`, so a fractional cap admits up to its ceiling, a cap already
|
|
213
|
+
* reached admits none, and a `NaN` cap, which that comparison never refuses, admits any number.
|
|
214
|
+
*/
|
|
215
|
+
_room() {
|
|
216
|
+
const room = this._maxFrameSize - this._writePos;
|
|
217
|
+
if (Number.isNaN(room)) return Infinity;
|
|
218
|
+
return room > 0 ? Math.ceil(room) : 0;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Append the ordinary payload bytes `chunk[start, stop)`, none of them FS or VT, in one copy.
|
|
222
|
+
* The size cap holds as it did byte by byte: every byte it admits is appended, and the first it
|
|
223
|
+
* refuses throws at that byte's offset.
|
|
224
|
+
*/
|
|
225
|
+
_appendRun(chunk, start, stop) {
|
|
226
|
+
const length = stop - start;
|
|
227
|
+
const take = Math.min(length, this._room());
|
|
228
|
+
if (take > 0) {
|
|
229
|
+
this._gather(chunk, start, start + take);
|
|
230
|
+
this._byteOffset += take;
|
|
231
|
+
}
|
|
232
|
+
if (take < length) this._throwTooLarge();
|
|
233
|
+
}
|
|
234
|
+
/** Refuse the payload byte at the current offset: the frame has reached its size cap. */
|
|
235
|
+
_throwTooLarge() {
|
|
236
|
+
throw new MllpFramingError(
|
|
237
|
+
"MLLP_FRAME_TOO_LARGE",
|
|
238
|
+
this._byteOffset,
|
|
239
|
+
EMPTY_SNIPPET,
|
|
240
|
+
`Frame payload exceeded maxFrameSizeBytes (${this._maxFrameSize}) at offset ${this._byteOffset}`
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Add `src[start, end)` to the current frame: into the block while it has room; then the full
|
|
245
|
+
* block becomes a part, a run of a block or more is copied as one part of its own, and what is
|
|
246
|
+
* left starts a fresh block.
|
|
247
|
+
*/
|
|
248
|
+
_gather(src, start, end) {
|
|
249
|
+
let at = start;
|
|
250
|
+
const fill = Math.min(end - at, BLOCK_SIZE - this._blockUsed);
|
|
251
|
+
if (fill > 0) {
|
|
252
|
+
this._accumulator.set(src.subarray(at, at + fill), this._blockUsed);
|
|
253
|
+
this._blockUsed += fill;
|
|
254
|
+
at += fill;
|
|
255
|
+
}
|
|
256
|
+
if (at < end) {
|
|
257
|
+
this._parts.push(this._accumulator);
|
|
258
|
+
this._accumulator = Buffer.allocUnsafe(BLOCK_SIZE);
|
|
259
|
+
const rest = end - at;
|
|
260
|
+
if (rest >= BLOCK_SIZE) {
|
|
261
|
+
this._parts.push(Buffer.from(src.subarray(at, end)));
|
|
262
|
+
this._blockUsed = 0;
|
|
263
|
+
} else {
|
|
264
|
+
this._accumulator.set(src.subarray(at, end), 0);
|
|
265
|
+
this._blockUsed = rest;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
this._writePos += end - start;
|
|
269
|
+
}
|
|
270
|
+
/** Forget the current frame's bytes: the block is kept for the next frame, the parts dropped. */
|
|
271
|
+
_discard() {
|
|
272
|
+
this._writePos = 0;
|
|
273
|
+
this._blockUsed = 0;
|
|
274
|
+
if (this._parts.length > 0) this._parts = [];
|
|
275
|
+
}
|
|
149
276
|
/**
|
|
150
277
|
* Clear internal accumulator state and reset byte offset to 0.
|
|
151
278
|
*
|
|
@@ -159,7 +286,7 @@ var FrameReader = class {
|
|
|
159
286
|
*/
|
|
160
287
|
reset() {
|
|
161
288
|
this._state = "SCANNING_FOR_VT";
|
|
162
|
-
this.
|
|
289
|
+
this._discard();
|
|
163
290
|
this._byteOffset = 0;
|
|
164
291
|
this._wsStart = 0;
|
|
165
292
|
this._wsCount = 0;
|
|
@@ -246,7 +373,7 @@ var FrameReader = class {
|
|
|
246
373
|
);
|
|
247
374
|
this._frameStartOffset = this._byteOffset;
|
|
248
375
|
this._state = "READING_PAYLOAD";
|
|
249
|
-
this.
|
|
376
|
+
this._readPayload(byte);
|
|
250
377
|
return;
|
|
251
378
|
}
|
|
252
379
|
const snippet = Buffer.from([byte]);
|
|
@@ -275,18 +402,11 @@ var FrameReader = class {
|
|
|
275
402
|
this._byteOffset,
|
|
276
403
|
`Unexpected VT (0x0B) mid-payload at offset ${this._byteOffset}; discarding ${this._writePos} accumulated bytes`
|
|
277
404
|
);
|
|
278
|
-
this.
|
|
405
|
+
this._discard();
|
|
279
406
|
this._state = "READING_PAYLOAD";
|
|
280
407
|
return;
|
|
281
408
|
}
|
|
282
|
-
if (this._writePos >= this._maxFrameSize)
|
|
283
|
-
throw new MllpFramingError(
|
|
284
|
-
"MLLP_FRAME_TOO_LARGE",
|
|
285
|
-
this._byteOffset,
|
|
286
|
-
EMPTY_SNIPPET,
|
|
287
|
-
`Frame payload exceeded maxFrameSizeBytes (${this._maxFrameSize}) at offset ${this._byteOffset}`
|
|
288
|
-
);
|
|
289
|
-
}
|
|
409
|
+
if (this._writePos >= this._maxFrameSize) this._throwTooLarge();
|
|
290
410
|
this._appendByte(byte);
|
|
291
411
|
}
|
|
292
412
|
_expectCr(byte) {
|
|
@@ -378,23 +498,35 @@ var FrameReader = class {
|
|
|
378
498
|
`Expected CR (0x0D) after FS, got 0x${byte.toString(16).padStart(2, "0")} at offset ${this._byteOffset}`
|
|
379
499
|
);
|
|
380
500
|
}
|
|
381
|
-
/**
|
|
501
|
+
/** Add one byte to the current frame, starting a fresh block when this one is full. */
|
|
382
502
|
_appendByte(byte) {
|
|
383
|
-
if (this.
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
this.
|
|
387
|
-
this._accumulator = grown;
|
|
503
|
+
if (this._blockUsed === BLOCK_SIZE) {
|
|
504
|
+
this._parts.push(this._accumulator);
|
|
505
|
+
this._accumulator = Buffer.allocUnsafe(BLOCK_SIZE);
|
|
506
|
+
this._blockUsed = 0;
|
|
388
507
|
}
|
|
389
|
-
this._accumulator[this.
|
|
508
|
+
this._accumulator[this._blockUsed++] = byte;
|
|
509
|
+
this._writePos++;
|
|
390
510
|
}
|
|
391
511
|
/**
|
|
392
512
|
* Copy accumulated payload bytes into a new Buffer and deliver via `onFrame`.
|
|
393
513
|
* The copy isolates callers from internal accumulator reuse.
|
|
394
514
|
*/
|
|
395
515
|
_deliverFrame() {
|
|
396
|
-
const
|
|
397
|
-
this.
|
|
516
|
+
const block = this._accumulator.subarray(0, this._blockUsed);
|
|
517
|
+
if (this._parts.length === 0) {
|
|
518
|
+
this._deliver(Buffer.from(block));
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
this._parts.push(block);
|
|
522
|
+
this._deliver(Buffer.concat(this._parts, this._writePos));
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Deliver `payload` via `onFrame` with this frame's start offset and warnings, the frame's
|
|
526
|
+
* bytes forgotten first.
|
|
527
|
+
*/
|
|
528
|
+
_deliver(payload) {
|
|
529
|
+
this._discard();
|
|
398
530
|
const frameStart = this._frameStartOffset;
|
|
399
531
|
const frameWarnings = this._frameWarnings;
|
|
400
532
|
this._frameWarnings = [];
|
|
@@ -702,7 +834,10 @@ function assertUsableDhParameters(dhParameters) {
|
|
|
702
834
|
tls.createSecureContext({ dhparam: dhParameters });
|
|
703
835
|
} catch (err) {
|
|
704
836
|
throw new MllpTlsConfigurationError(MLLP_TLS_DH_PARAMETERS_REJECTED, {
|
|
705
|
-
cause: err instanceof Error ? err :
|
|
837
|
+
cause: err instanceof Error ? err : (
|
|
838
|
+
/* v8 ignore next -- createSecureContext throws only Error */
|
|
839
|
+
new Error(String(err))
|
|
840
|
+
)
|
|
706
841
|
});
|
|
707
842
|
}
|
|
708
843
|
}
|
|
@@ -720,7 +855,10 @@ function resolveTlsCipherPolicy(opts, side) {
|
|
|
720
855
|
tls.createSecureContext({ ciphers });
|
|
721
856
|
} catch (err) {
|
|
722
857
|
throw new MllpTlsConfigurationError(MLLP_TLS_CIPHER_LIST_REJECTED, {
|
|
723
|
-
cause: err instanceof Error ? err :
|
|
858
|
+
cause: err instanceof Error ? err : (
|
|
859
|
+
/* v8 ignore next -- createSecureContext throws only Error */
|
|
860
|
+
new Error(String(err))
|
|
861
|
+
)
|
|
724
862
|
});
|
|
725
863
|
}
|
|
726
864
|
}
|
|
@@ -810,6 +948,17 @@ var Connection = class extends events.EventEmitter {
|
|
|
810
948
|
_warningsTruncated = false;
|
|
811
949
|
_onWarningFn = null;
|
|
812
950
|
_drainPromise = null;
|
|
951
|
+
/** Resolves {@link Connection._leftDraining}; called on the transition out of `DRAINING`. */
|
|
952
|
+
_leaveDraining;
|
|
953
|
+
/**
|
|
954
|
+
* Settles when the connection leaves `DRAINING` by any route: the drain finishing, the drain
|
|
955
|
+
* timeout, a `destroy()`, or the peer closing. A connection enters `DRAINING` at most once, so
|
|
956
|
+
* one promise per connection is enough. The drain races it, so a `close()` never outlives the
|
|
957
|
+
* drain it waits on.
|
|
958
|
+
*/
|
|
959
|
+
_leftDraining = new Promise((resolve) => {
|
|
960
|
+
this._leaveDraining = resolve;
|
|
961
|
+
});
|
|
813
962
|
/**
|
|
814
963
|
* beforeClose hook, no-op default that resolves immediately.
|
|
815
964
|
*
|
|
@@ -886,7 +1035,9 @@ var Connection = class extends events.EventEmitter {
|
|
|
886
1035
|
* Notify the connection that the transport handshake has completed.
|
|
887
1036
|
*
|
|
888
1037
|
* Transitions `CONNECTING → CONNECTED` and emits the `'connect'` event.
|
|
889
|
-
* Called externally by Server/Client after the socket connects.
|
|
1038
|
+
* Called externally by Server/Client after the socket connects. In any other state it does
|
|
1039
|
+
* nothing: a connection closed before its socket connected stays `CLOSED` and emits no
|
|
1040
|
+
* `'connect'`, and a second call on a `CONNECTED` connection emits no second one.
|
|
890
1041
|
*
|
|
891
1042
|
* @param remoteAddress - Remote peer IP address, or `null` if unavailable.
|
|
892
1043
|
* @param remotePort - Remote peer port, or `null` if unavailable.
|
|
@@ -899,6 +1050,7 @@ var Connection = class extends events.EventEmitter {
|
|
|
899
1050
|
* ```
|
|
900
1051
|
*/
|
|
901
1052
|
notifyConnect(remoteAddress, remotePort) {
|
|
1053
|
+
if (this._state !== "CONNECTING") return;
|
|
902
1054
|
this._remoteAddress = remoteAddress;
|
|
903
1055
|
this._remotePort = remotePort;
|
|
904
1056
|
this._connectedAt = /* @__PURE__ */ new Date();
|
|
@@ -937,6 +1089,10 @@ var Connection = class extends events.EventEmitter {
|
|
|
937
1089
|
* then `DRAINING → DISCONNECTED` once the hook resolves, or `DRAINING → CLOSED`
|
|
938
1090
|
* if the drain timeout elapses first.
|
|
939
1091
|
* - If `DRAINING`: waits for the existing drain to complete (idempotent).
|
|
1092
|
+
*
|
|
1093
|
+
* Every pending `close()` resolves as soon as the connection leaves `DRAINING`, by whatever
|
|
1094
|
+
* route: the hook resolving, the drain timeout, a `destroy()`, or the peer closing. A
|
|
1095
|
+
* `destroy()` during a long drain therefore ends the wait at once, not at the drain timeout.
|
|
940
1096
|
* - If `CLOSED` or `DISCONNECTED`: no-op.
|
|
941
1097
|
*
|
|
942
1098
|
* @param opts.drainTimeoutMs - Override drain timeout (default: `opts.drainTimeoutMs ?? 30_000`).
|
|
@@ -971,23 +1127,29 @@ var Connection = class extends events.EventEmitter {
|
|
|
971
1127
|
*/
|
|
972
1128
|
async _drainWithTimeout(timeoutMs) {
|
|
973
1129
|
const drainPromise = this.beforeClose(timeoutMs);
|
|
1130
|
+
let handle;
|
|
974
1131
|
const timeoutPromise = new Promise((resolve) => {
|
|
975
|
-
|
|
1132
|
+
handle = setTimeout(() => {
|
|
976
1133
|
resolve("timeout");
|
|
977
1134
|
}, timeoutMs);
|
|
978
1135
|
handle.unref();
|
|
979
1136
|
});
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
1137
|
+
try {
|
|
1138
|
+
const result = await Promise.race([
|
|
1139
|
+
drainPromise.then(() => "done"),
|
|
1140
|
+
timeoutPromise,
|
|
1141
|
+
this._leftDraining
|
|
1142
|
+
]);
|
|
1143
|
+
if (this._state !== "DRAINING") return;
|
|
1144
|
+
if (result === "timeout") {
|
|
983
1145
|
this._transition("CLOSED", "drain timeout");
|
|
984
1146
|
this._transport.destroy();
|
|
985
|
-
}
|
|
986
|
-
} else {
|
|
987
|
-
if (this._state === "DRAINING") {
|
|
1147
|
+
} else {
|
|
988
1148
|
this._transition("DISCONNECTED");
|
|
989
1149
|
this._transport.close();
|
|
990
1150
|
}
|
|
1151
|
+
} finally {
|
|
1152
|
+
clearTimeout(handle);
|
|
991
1153
|
}
|
|
992
1154
|
}
|
|
993
1155
|
/**
|
|
@@ -1059,6 +1221,7 @@ var Connection = class extends events.EventEmitter {
|
|
|
1059
1221
|
this._dispatchContained(() => {
|
|
1060
1222
|
this.emit("stateChange", event);
|
|
1061
1223
|
});
|
|
1224
|
+
if (from === "DRAINING") this._leaveDraining("left");
|
|
1062
1225
|
if (to === "DISCONNECTED") {
|
|
1063
1226
|
this._dispatchContained(() => {
|
|
1064
1227
|
this.emit("disconnect", Object.freeze({ connectionId: this.connectionId }));
|
|
@@ -1233,17 +1396,22 @@ var UNSAFE_FIELD_SEPARATORS = /* @__PURE__ */ new Set([
|
|
|
1233
1396
|
function isMshSegmentAt(buf, i) {
|
|
1234
1397
|
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]);
|
|
1235
1398
|
}
|
|
1236
|
-
function
|
|
1237
|
-
let
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1399
|
+
function segmentEnds(buf) {
|
|
1400
|
+
let cr = -2;
|
|
1401
|
+
let lf = -2;
|
|
1402
|
+
return (i) => {
|
|
1403
|
+
if (cr !== -1 && cr < i) cr = buf.indexOf(SEGMENT_SEPARATOR_CR, i);
|
|
1404
|
+
if (lf !== -1 && lf < i) lf = buf.indexOf(SEGMENT_SEPARATOR_LF, i);
|
|
1405
|
+
const atCr = cr === -1 ? buf.length : cr;
|
|
1406
|
+
const atLf = lf === -1 ? buf.length : lf;
|
|
1407
|
+
return atCr < atLf ? atCr : atLf;
|
|
1408
|
+
};
|
|
1242
1409
|
}
|
|
1243
1410
|
function findMshSegment(buf) {
|
|
1411
|
+
const segmentEnd = segmentEnds(buf);
|
|
1244
1412
|
let segStart = 0;
|
|
1245
1413
|
while (segStart < buf.length) {
|
|
1246
|
-
const end = segmentEnd(
|
|
1414
|
+
const end = segmentEnd(segStart);
|
|
1247
1415
|
if (isMshSegmentAt(buf, segStart)) return { start: segStart, end };
|
|
1248
1416
|
let next = end;
|
|
1249
1417
|
while (next < buf.length && (buf[next] === SEGMENT_SEPARATOR_CR || buf[next] === SEGMENT_SEPARATOR_LF)) {
|
|
@@ -1268,10 +1436,11 @@ function isBatchEnvelopeSegmentAt(buf, i) {
|
|
|
1268
1436
|
return false;
|
|
1269
1437
|
}
|
|
1270
1438
|
function containsBatchOrExtraMessage(buf) {
|
|
1439
|
+
const segmentEnd = segmentEnds(buf);
|
|
1271
1440
|
let mshCount = 0;
|
|
1272
1441
|
let segStart = 0;
|
|
1273
1442
|
while (segStart < buf.length) {
|
|
1274
|
-
const end = segmentEnd(
|
|
1443
|
+
const end = segmentEnd(segStart);
|
|
1275
1444
|
if (isMshSegmentAt(buf, segStart)) {
|
|
1276
1445
|
mshCount++;
|
|
1277
1446
|
if (mshCount > 1) return true;
|
|
@@ -1360,11 +1529,21 @@ var DEFAULT_FIELD_SEPARATOR = "|";
|
|
|
1360
1529
|
var ENCODING_CHAR_SUBSTITUTE = "#";
|
|
1361
1530
|
var UNSAFE_DELIMITER = /[\r\n\v\x1c]/;
|
|
1362
1531
|
function rawAckUncorrelatable(payload) {
|
|
1363
|
-
|
|
1532
|
+
return uncorrelatable(readInbound(payload));
|
|
1533
|
+
}
|
|
1534
|
+
function readInbound(payload) {
|
|
1535
|
+
let batch;
|
|
1536
|
+
return {
|
|
1537
|
+
msh: readMshSegment(payload),
|
|
1538
|
+
batch: () => batch ??= containsBatchOrExtraMessage(payload)
|
|
1539
|
+
};
|
|
1540
|
+
}
|
|
1541
|
+
function uncorrelatable(inbound) {
|
|
1542
|
+
const { msh } = inbound;
|
|
1364
1543
|
if (msh === null) return true;
|
|
1365
1544
|
const controlId = msh.fields[9];
|
|
1366
1545
|
if (controlId === void 0 || controlId === "") return true;
|
|
1367
|
-
return
|
|
1546
|
+
return inbound.batch();
|
|
1368
1547
|
}
|
|
1369
1548
|
function downgradePositiveAck(code) {
|
|
1370
1549
|
if (code === "AA") return "AE";
|
|
@@ -1372,8 +1551,11 @@ function downgradePositiveAck(code) {
|
|
|
1372
1551
|
return code;
|
|
1373
1552
|
}
|
|
1374
1553
|
function buildRawAck(payload, code) {
|
|
1375
|
-
|
|
1376
|
-
|
|
1554
|
+
return buildRawAckFrom(readInbound(payload), code);
|
|
1555
|
+
}
|
|
1556
|
+
function buildRawAckFrom(inbound, code) {
|
|
1557
|
+
const { msh } = inbound;
|
|
1558
|
+
if ((code === "AA" || code === "CA") && uncorrelatable(inbound)) {
|
|
1377
1559
|
code = downgradePositiveAck(code);
|
|
1378
1560
|
}
|
|
1379
1561
|
const newControlId = crypto.randomUUID().replace(/-/g, "").substring(0, 20);
|
|
@@ -2134,6 +2316,7 @@ var MllpServer = class extends events.EventEmitter {
|
|
|
2134
2316
|
return Object.freeze({
|
|
2135
2317
|
subjectCN: asCn(cert.subject?.CN),
|
|
2136
2318
|
issuerCN: asCn(cert.issuer?.CN),
|
|
2319
|
+
/* v8 ignore next -- Node.js always reports valid_to for a parsed certificate */
|
|
2137
2320
|
validTo: cert.valid_to ?? null,
|
|
2138
2321
|
// Verification outcome, under 'WANT' a certificate can be present yet
|
|
2139
2322
|
// UNVERIFIED (see PeerCertificateSummary.authorized JSDoc).
|
|
@@ -2157,18 +2340,21 @@ var MllpServer = class extends events.EventEmitter {
|
|
|
2157
2340
|
*/
|
|
2158
2341
|
async _sendCommitAck(payload, meta, conn) {
|
|
2159
2342
|
const handler = this._opts.onMessage;
|
|
2160
|
-
const
|
|
2343
|
+
const inbound = readInbound(payload);
|
|
2344
|
+
const selectCode = this._ackModeSelector(inbound, conn);
|
|
2161
2345
|
if (handler === void 0) {
|
|
2162
2346
|
this._dispatchAck(
|
|
2163
2347
|
conn,
|
|
2164
|
-
|
|
2348
|
+
buildRawAckFrom(inbound, this._resolveAutoAckPositive(inbound, meta, conn, selectCode))
|
|
2165
2349
|
);
|
|
2166
2350
|
return;
|
|
2167
2351
|
}
|
|
2168
2352
|
let code;
|
|
2353
|
+
let settled;
|
|
2169
2354
|
try {
|
|
2170
2355
|
await handler(payload, meta, conn);
|
|
2171
|
-
|
|
2356
|
+
settled = readInbound(payload);
|
|
2357
|
+
code = this._resolveAutoAckPositive(settled, meta, conn, selectCode);
|
|
2172
2358
|
} catch (err) {
|
|
2173
2359
|
const nack = resolveNackCode(err);
|
|
2174
2360
|
code = selectCode(nack);
|
|
@@ -2181,7 +2367,8 @@ var MllpServer = class extends events.EventEmitter {
|
|
|
2181
2367
|
})
|
|
2182
2368
|
);
|
|
2183
2369
|
}
|
|
2184
|
-
|
|
2370
|
+
settled ??= readInbound(payload);
|
|
2371
|
+
this._dispatchAck(conn, buildRawAckFrom(settled, code));
|
|
2185
2372
|
}
|
|
2186
2373
|
/**
|
|
2187
2374
|
* Resolve the auto-ACK code for a message the server would otherwise acknowledge
|
|
@@ -2210,10 +2397,9 @@ var MllpServer = class extends events.EventEmitter {
|
|
|
2210
2397
|
* `selectCode` puts the resolved disposition in the half of HL7 Table 0008 the inbound
|
|
2211
2398
|
* asked for; it is the identity for a message that asked for nothing.
|
|
2212
2399
|
*/
|
|
2213
|
-
_resolveAutoAckPositive(
|
|
2400
|
+
_resolveAutoAckPositive(inbound, meta, conn, selectCode) {
|
|
2214
2401
|
const discarded = meta.warnings.some((w) => w.code === "MLLP_TRAILING_BYTES");
|
|
2215
|
-
|
|
2216
|
-
if (!discarded && !uncorrelatable) return selectCode("AA");
|
|
2402
|
+
if (!discarded && !uncorrelatable(inbound)) return selectCode("AA");
|
|
2217
2403
|
const reason = discarded ? "discarded-bytes" : "uncorrelatable-inbound";
|
|
2218
2404
|
const code = selectCode("AE");
|
|
2219
2405
|
this._emitContained(
|
|
@@ -2254,9 +2440,9 @@ var MllpServer = class extends events.EventEmitter {
|
|
|
2254
2440
|
* Downgrading a message the handler committed would make a discharged sender resend a
|
|
2255
2441
|
* message already in storage.
|
|
2256
2442
|
*/
|
|
2257
|
-
_ackModeSelector(
|
|
2443
|
+
_ackModeSelector(inbound, conn) {
|
|
2258
2444
|
const identity = (disposition) => disposition;
|
|
2259
|
-
const msh =
|
|
2445
|
+
const { msh } = inbound;
|
|
2260
2446
|
if (msh === null) return identity;
|
|
2261
2447
|
const accept = readAcceptCondition(msh);
|
|
2262
2448
|
if (accept.kind === "unrecognised") {
|
|
@@ -2264,7 +2450,7 @@ var MllpServer = class extends events.EventEmitter {
|
|
|
2264
2450
|
return identity;
|
|
2265
2451
|
}
|
|
2266
2452
|
if (accept.kind === "null") return identity;
|
|
2267
|
-
if (
|
|
2453
|
+
if (inbound.batch()) return identity;
|
|
2268
2454
|
return (disposition) => acceptAckRequested(accept, disposition) ? acceptModeCounterpart(disposition) : disposition;
|
|
2269
2455
|
}
|
|
2270
2456
|
/**
|
|
@@ -3055,7 +3241,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3055
3241
|
* the Correlator is timer-free. Cleared on close/destroy.
|
|
3056
3242
|
*/
|
|
3057
3243
|
_ackSweepTimer = null;
|
|
3058
|
-
// ── Reconnect state
|
|
3244
|
+
// ── Reconnect state: backoff, the retry strategy and the reconnect cycle ─────────
|
|
3059
3245
|
_autoReconnect;
|
|
3060
3246
|
_initialDelayMs;
|
|
3061
3247
|
_maxDelayMs;
|
|
@@ -3078,7 +3264,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3078
3264
|
_backoffTimer = null;
|
|
3079
3265
|
/** Last delay (ms) used by the strategy. Surfaced via RetryContext.lastDelayMs. */
|
|
3080
3266
|
_lastDelayMs = 0;
|
|
3081
|
-
// ── Backpressure
|
|
3267
|
+
// ── Backpressure and pipeline state: the high-water mark and the in-flight set ───
|
|
3082
3268
|
/** Count cap; `Number.POSITIVE_INFINITY` when only bytes configured. */
|
|
3083
3269
|
_hwmCount;
|
|
3084
3270
|
/** Byte cap; `Number.POSITIVE_INFINITY` when only count configured. */
|
|
@@ -3127,7 +3313,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3127
3313
|
* @internal
|
|
3128
3314
|
*/
|
|
3129
3315
|
_reconnectFactory = null;
|
|
3130
|
-
// ──
|
|
3316
|
+
// ── Observability counters for getStats() ─────────────────────────────────
|
|
3131
3317
|
/** Total successful `conn.send()` flushes since construction. */
|
|
3132
3318
|
_sentTotal = 0;
|
|
3133
3319
|
/** Total ACKs resolved since construction. */
|
|
@@ -3393,6 +3579,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3393
3579
|
ackCode: ctx.ackCode,
|
|
3394
3580
|
controlIdBytes: ctx.controlIdBytes,
|
|
3395
3581
|
elapsedSinceSendMs: ctx.elapsedSinceSendMs,
|
|
3582
|
+
/* v8 ignore next -- send() refuses before any warning when no connection is attached, so one always is here */
|
|
3396
3583
|
connectionId: this._connection?.connectionId ?? conn?.connectionId,
|
|
3397
3584
|
timestamp: /* @__PURE__ */ new Date()
|
|
3398
3585
|
});
|
|
@@ -3496,7 +3683,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3496
3683
|
this._correlator = new Correlator({
|
|
3497
3684
|
mode: this._correlateByControlId ? "controlId" : "fifo",
|
|
3498
3685
|
ackTimeoutMs: this._ackTimeoutMs,
|
|
3499
|
-
//
|
|
3686
|
+
// pipeline:false collapses the in-flight set to at most one.
|
|
3500
3687
|
maxInFlight: this._pipeline ? Number.POSITIVE_INFINITY : 1,
|
|
3501
3688
|
onWarning: (code, ctx) => {
|
|
3502
3689
|
this._aggregatedWarningsByCode[code] = (this._aggregatedWarningsByCode[code] ?? 0) + 1;
|
|
@@ -3506,6 +3693,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3506
3693
|
message: ackDiagnosticMessage(code),
|
|
3507
3694
|
controlIdBytes: ctx.controlIdBytes,
|
|
3508
3695
|
elapsedSinceSendMs: ctx.elapsedSinceSendMs,
|
|
3696
|
+
/* v8 ignore next -- correlator callbacks run only after attach has stored this connection */
|
|
3509
3697
|
connectionId: this._connection?.connectionId ?? conn.connectionId,
|
|
3510
3698
|
timestamp: /* @__PURE__ */ new Date()
|
|
3511
3699
|
});
|
|
@@ -3522,6 +3710,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3522
3710
|
safeEmitError(
|
|
3523
3711
|
this,
|
|
3524
3712
|
Object.freeze({
|
|
3713
|
+
/* v8 ignore next -- correlator callbacks run only after attach has stored this connection */
|
|
3525
3714
|
connectionId: this._connection?.connectionId ?? conn.connectionId,
|
|
3526
3715
|
error: err,
|
|
3527
3716
|
// The peer's MSA-2 byte LENGTH. The bytes themselves used to be
|
|
@@ -3537,6 +3726,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3537
3726
|
new MllpTimeoutError(`ACK timeout after ${elapsedMs}ms`, {
|
|
3538
3727
|
messageControlIdBytes: entry.controlId?.length,
|
|
3539
3728
|
elapsedMs,
|
|
3729
|
+
/* v8 ignore next -- only flushed sends can time out, so sentAt is set */
|
|
3540
3730
|
sentAt: entry.sentAt ?? 0
|
|
3541
3731
|
})
|
|
3542
3732
|
);
|
|
@@ -3568,6 +3758,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3568
3758
|
safeEmitError(
|
|
3569
3759
|
this,
|
|
3570
3760
|
Object.freeze({
|
|
3761
|
+
/* v8 ignore next -- correlator callbacks run only after attach has stored this connection */
|
|
3571
3762
|
connectionId: this._connection?.connectionId ?? conn.connectionId,
|
|
3572
3763
|
error: err instanceof Error ? err : new Error(String(err))
|
|
3573
3764
|
})
|
|
@@ -3746,6 +3937,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3746
3937
|
safeEmitError(
|
|
3747
3938
|
this,
|
|
3748
3939
|
Object.freeze({
|
|
3940
|
+
/* v8 ignore next -- a disconnect always arrives from an attached connection, so one is stored */
|
|
3749
3941
|
connectionId: this._connection?.connectionId ?? "<none>",
|
|
3750
3942
|
error: this._lastError
|
|
3751
3943
|
})
|
|
@@ -3763,6 +3955,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
3763
3955
|
this._emitContained(
|
|
3764
3956
|
"reconnecting",
|
|
3765
3957
|
Object.freeze({
|
|
3958
|
+
/* v8 ignore next -- a disconnect always arrives from an attached connection, so one is stored */
|
|
3766
3959
|
connectionId: this._connection?.connectionId ?? "<none>",
|
|
3767
3960
|
attempt: this._attempt,
|
|
3768
3961
|
delayMs: delay
|
|
@@ -4008,7 +4201,7 @@ var MllpClient = class extends events.EventEmitter {
|
|
|
4008
4201
|
*
|
|
4009
4202
|
* Emits a frozen `'ack'` event on every successful match.
|
|
4010
4203
|
*
|
|
4011
|
-
*
|
|
4204
|
+
* **Enhanced acknowledgement mode**
|
|
4012
4205
|
*
|
|
4013
4206
|
* A message whose MSH-15 or MSH-16 is not null asks for the two-part protocol of HL7
|
|
4014
4207
|
* v2.5.1 §2.9, and on a client correlating by control ID this method delivers it: the
|
|
@@ -4886,6 +5079,7 @@ function recordWarning(into, warning, offsetBase = 0) {
|
|
|
4886
5079
|
function classifyFrame(exchange, payload, frameStart, chunks, deviations, finish) {
|
|
4887
5080
|
const canonical = encodeFrame(payload, {
|
|
4888
5081
|
allowDelimiterBytesInPayload: true,
|
|
5082
|
+
/* v8 ignore next 3 -- no delivered payload holds VT or FS, so the encoder never warns here */
|
|
4889
5083
|
onWarning: (w) => {
|
|
4890
5084
|
recordWarning(deviations, w, frameStart + 1);
|
|
4891
5085
|
}
|
|
@@ -4957,7 +5151,7 @@ async function runDifferential(options = {}) {
|
|
|
4957
5151
|
}
|
|
4958
5152
|
|
|
4959
5153
|
// src/index.ts
|
|
4960
|
-
var VERSION = "0.1.
|
|
5154
|
+
var VERSION = "0.1.9";
|
|
4961
5155
|
|
|
4962
5156
|
exports.ATNA_CIPHER_LIST = ATNA_CIPHER_LIST;
|
|
4963
5157
|
exports.ATNA_CIPHER_SUITES = ATNA_CIPHER_SUITES;
|