@cosyte/mllp 0.1.6 → 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 +24 -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 +23 -3
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
|
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
|
|
108
|
-
_accumulator = Buffer.allocUnsafe(
|
|
109
|
-
/** Number of
|
|
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.
|
|
287
|
+
this._discard();
|
|
161
288
|
this._byteOffset = 0;
|
|
162
289
|
this._wsStart = 0;
|
|
163
290
|
this._wsCount = 0;
|
|
@@ -244,7 +371,7 @@ var FrameReader = class {
|
|
|
244
371
|
);
|
|
245
372
|
this._frameStartOffset = this._byteOffset;
|
|
246
373
|
this._state = "READING_PAYLOAD";
|
|
247
|
-
this.
|
|
374
|
+
this._readPayload(byte);
|
|
248
375
|
return;
|
|
249
376
|
}
|
|
250
377
|
const snippet = Buffer.from([byte]);
|
|
@@ -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.
|
|
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
|
-
/**
|
|
499
|
+
/** Add one byte to the current frame, starting a fresh block when this one is full. */
|
|
380
500
|
_appendByte(byte) {
|
|
381
|
-
if (this.
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
this.
|
|
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.
|
|
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
|
|
395
|
-
this.
|
|
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 = [];
|
|
@@ -700,7 +832,10 @@ function assertUsableDhParameters(dhParameters) {
|
|
|
700
832
|
createSecureContext({ dhparam: dhParameters });
|
|
701
833
|
} catch (err) {
|
|
702
834
|
throw new MllpTlsConfigurationError(MLLP_TLS_DH_PARAMETERS_REJECTED, {
|
|
703
|
-
cause: err instanceof Error ? err :
|
|
835
|
+
cause: err instanceof Error ? err : (
|
|
836
|
+
/* v8 ignore next -- createSecureContext throws only Error */
|
|
837
|
+
new Error(String(err))
|
|
838
|
+
)
|
|
704
839
|
});
|
|
705
840
|
}
|
|
706
841
|
}
|
|
@@ -718,7 +853,10 @@ function resolveTlsCipherPolicy(opts, side) {
|
|
|
718
853
|
createSecureContext({ ciphers });
|
|
719
854
|
} catch (err) {
|
|
720
855
|
throw new MllpTlsConfigurationError(MLLP_TLS_CIPHER_LIST_REJECTED, {
|
|
721
|
-
cause: err instanceof Error ? err :
|
|
856
|
+
cause: err instanceof Error ? err : (
|
|
857
|
+
/* v8 ignore next -- createSecureContext throws only Error */
|
|
858
|
+
new Error(String(err))
|
|
859
|
+
)
|
|
722
860
|
});
|
|
723
861
|
}
|
|
724
862
|
}
|
|
@@ -808,6 +946,17 @@ var Connection = class extends EventEmitter {
|
|
|
808
946
|
_warningsTruncated = false;
|
|
809
947
|
_onWarningFn = null;
|
|
810
948
|
_drainPromise = null;
|
|
949
|
+
/** Resolves {@link Connection._leftDraining}; called on the transition out of `DRAINING`. */
|
|
950
|
+
_leaveDraining;
|
|
951
|
+
/**
|
|
952
|
+
* Settles when the connection leaves `DRAINING` by any route: the drain finishing, the drain
|
|
953
|
+
* timeout, a `destroy()`, or the peer closing. A connection enters `DRAINING` at most once, so
|
|
954
|
+
* one promise per connection is enough. The drain races it, so a `close()` never outlives the
|
|
955
|
+
* drain it waits on.
|
|
956
|
+
*/
|
|
957
|
+
_leftDraining = new Promise((resolve) => {
|
|
958
|
+
this._leaveDraining = resolve;
|
|
959
|
+
});
|
|
811
960
|
/**
|
|
812
961
|
* beforeClose hook, no-op default that resolves immediately.
|
|
813
962
|
*
|
|
@@ -884,7 +1033,9 @@ var Connection = class extends EventEmitter {
|
|
|
884
1033
|
* Notify the connection that the transport handshake has completed.
|
|
885
1034
|
*
|
|
886
1035
|
* Transitions `CONNECTING → CONNECTED` and emits the `'connect'` event.
|
|
887
|
-
* Called externally by Server/Client after the socket connects.
|
|
1036
|
+
* Called externally by Server/Client after the socket connects. In any other state it does
|
|
1037
|
+
* nothing: a connection closed before its socket connected stays `CLOSED` and emits no
|
|
1038
|
+
* `'connect'`, and a second call on a `CONNECTED` connection emits no second one.
|
|
888
1039
|
*
|
|
889
1040
|
* @param remoteAddress - Remote peer IP address, or `null` if unavailable.
|
|
890
1041
|
* @param remotePort - Remote peer port, or `null` if unavailable.
|
|
@@ -897,6 +1048,7 @@ var Connection = class extends EventEmitter {
|
|
|
897
1048
|
* ```
|
|
898
1049
|
*/
|
|
899
1050
|
notifyConnect(remoteAddress, remotePort) {
|
|
1051
|
+
if (this._state !== "CONNECTING") return;
|
|
900
1052
|
this._remoteAddress = remoteAddress;
|
|
901
1053
|
this._remotePort = remotePort;
|
|
902
1054
|
this._connectedAt = /* @__PURE__ */ new Date();
|
|
@@ -935,6 +1087,10 @@ var Connection = class extends EventEmitter {
|
|
|
935
1087
|
* then `DRAINING → DISCONNECTED` once the hook resolves, or `DRAINING → CLOSED`
|
|
936
1088
|
* if the drain timeout elapses first.
|
|
937
1089
|
* - If `DRAINING`: waits for the existing drain to complete (idempotent).
|
|
1090
|
+
*
|
|
1091
|
+
* Every pending `close()` resolves as soon as the connection leaves `DRAINING`, by whatever
|
|
1092
|
+
* route: the hook resolving, the drain timeout, a `destroy()`, or the peer closing. A
|
|
1093
|
+
* `destroy()` during a long drain therefore ends the wait at once, not at the drain timeout.
|
|
938
1094
|
* - If `CLOSED` or `DISCONNECTED`: no-op.
|
|
939
1095
|
*
|
|
940
1096
|
* @param opts.drainTimeoutMs - Override drain timeout (default: `opts.drainTimeoutMs ?? 30_000`).
|
|
@@ -969,23 +1125,29 @@ var Connection = class extends EventEmitter {
|
|
|
969
1125
|
*/
|
|
970
1126
|
async _drainWithTimeout(timeoutMs) {
|
|
971
1127
|
const drainPromise = this.beforeClose(timeoutMs);
|
|
1128
|
+
let handle;
|
|
972
1129
|
const timeoutPromise = new Promise((resolve) => {
|
|
973
|
-
|
|
1130
|
+
handle = setTimeout(() => {
|
|
974
1131
|
resolve("timeout");
|
|
975
1132
|
}, timeoutMs);
|
|
976
1133
|
handle.unref();
|
|
977
1134
|
});
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
1135
|
+
try {
|
|
1136
|
+
const result = await Promise.race([
|
|
1137
|
+
drainPromise.then(() => "done"),
|
|
1138
|
+
timeoutPromise,
|
|
1139
|
+
this._leftDraining
|
|
1140
|
+
]);
|
|
1141
|
+
if (this._state !== "DRAINING") return;
|
|
1142
|
+
if (result === "timeout") {
|
|
981
1143
|
this._transition("CLOSED", "drain timeout");
|
|
982
1144
|
this._transport.destroy();
|
|
983
|
-
}
|
|
984
|
-
} else {
|
|
985
|
-
if (this._state === "DRAINING") {
|
|
1145
|
+
} else {
|
|
986
1146
|
this._transition("DISCONNECTED");
|
|
987
1147
|
this._transport.close();
|
|
988
1148
|
}
|
|
1149
|
+
} finally {
|
|
1150
|
+
clearTimeout(handle);
|
|
989
1151
|
}
|
|
990
1152
|
}
|
|
991
1153
|
/**
|
|
@@ -1057,6 +1219,7 @@ var Connection = class extends EventEmitter {
|
|
|
1057
1219
|
this._dispatchContained(() => {
|
|
1058
1220
|
this.emit("stateChange", event);
|
|
1059
1221
|
});
|
|
1222
|
+
if (from === "DRAINING") this._leaveDraining("left");
|
|
1060
1223
|
if (to === "DISCONNECTED") {
|
|
1061
1224
|
this._dispatchContained(() => {
|
|
1062
1225
|
this.emit("disconnect", Object.freeze({ connectionId: this.connectionId }));
|
|
@@ -1231,17 +1394,22 @@ var UNSAFE_FIELD_SEPARATORS = /* @__PURE__ */ new Set([
|
|
|
1231
1394
|
function isMshSegmentAt(buf, i) {
|
|
1232
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]);
|
|
1233
1396
|
}
|
|
1234
|
-
function
|
|
1235
|
-
let
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
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
|
+
};
|
|
1240
1407
|
}
|
|
1241
1408
|
function findMshSegment(buf) {
|
|
1409
|
+
const segmentEnd = segmentEnds(buf);
|
|
1242
1410
|
let segStart = 0;
|
|
1243
1411
|
while (segStart < buf.length) {
|
|
1244
|
-
const end = segmentEnd(
|
|
1412
|
+
const end = segmentEnd(segStart);
|
|
1245
1413
|
if (isMshSegmentAt(buf, segStart)) return { start: segStart, end };
|
|
1246
1414
|
let next = end;
|
|
1247
1415
|
while (next < buf.length && (buf[next] === SEGMENT_SEPARATOR_CR || buf[next] === SEGMENT_SEPARATOR_LF)) {
|
|
@@ -1266,10 +1434,11 @@ function isBatchEnvelopeSegmentAt(buf, i) {
|
|
|
1266
1434
|
return false;
|
|
1267
1435
|
}
|
|
1268
1436
|
function containsBatchOrExtraMessage(buf) {
|
|
1437
|
+
const segmentEnd = segmentEnds(buf);
|
|
1269
1438
|
let mshCount = 0;
|
|
1270
1439
|
let segStart = 0;
|
|
1271
1440
|
while (segStart < buf.length) {
|
|
1272
|
-
const end = segmentEnd(
|
|
1441
|
+
const end = segmentEnd(segStart);
|
|
1273
1442
|
if (isMshSegmentAt(buf, segStart)) {
|
|
1274
1443
|
mshCount++;
|
|
1275
1444
|
if (mshCount > 1) return true;
|
|
@@ -1358,11 +1527,21 @@ var DEFAULT_FIELD_SEPARATOR = "|";
|
|
|
1358
1527
|
var ENCODING_CHAR_SUBSTITUTE = "#";
|
|
1359
1528
|
var UNSAFE_DELIMITER = /[\r\n\v\x1c]/;
|
|
1360
1529
|
function rawAckUncorrelatable(payload) {
|
|
1361
|
-
|
|
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;
|
|
1362
1541
|
if (msh === null) return true;
|
|
1363
1542
|
const controlId = msh.fields[9];
|
|
1364
1543
|
if (controlId === void 0 || controlId === "") return true;
|
|
1365
|
-
return
|
|
1544
|
+
return inbound.batch();
|
|
1366
1545
|
}
|
|
1367
1546
|
function downgradePositiveAck(code) {
|
|
1368
1547
|
if (code === "AA") return "AE";
|
|
@@ -1370,8 +1549,11 @@ function downgradePositiveAck(code) {
|
|
|
1370
1549
|
return code;
|
|
1371
1550
|
}
|
|
1372
1551
|
function buildRawAck(payload, code) {
|
|
1373
|
-
|
|
1374
|
-
|
|
1552
|
+
return buildRawAckFrom(readInbound(payload), code);
|
|
1553
|
+
}
|
|
1554
|
+
function buildRawAckFrom(inbound, code) {
|
|
1555
|
+
const { msh } = inbound;
|
|
1556
|
+
if ((code === "AA" || code === "CA") && uncorrelatable(inbound)) {
|
|
1375
1557
|
code = downgradePositiveAck(code);
|
|
1376
1558
|
}
|
|
1377
1559
|
const newControlId = randomUUID().replace(/-/g, "").substring(0, 20);
|
|
@@ -2132,6 +2314,7 @@ var MllpServer = class extends EventEmitter {
|
|
|
2132
2314
|
return Object.freeze({
|
|
2133
2315
|
subjectCN: asCn(cert.subject?.CN),
|
|
2134
2316
|
issuerCN: asCn(cert.issuer?.CN),
|
|
2317
|
+
/* v8 ignore next -- Node.js always reports valid_to for a parsed certificate */
|
|
2135
2318
|
validTo: cert.valid_to ?? null,
|
|
2136
2319
|
// Verification outcome, under 'WANT' a certificate can be present yet
|
|
2137
2320
|
// UNVERIFIED (see PeerCertificateSummary.authorized JSDoc).
|
|
@@ -2155,18 +2338,21 @@ var MllpServer = class extends EventEmitter {
|
|
|
2155
2338
|
*/
|
|
2156
2339
|
async _sendCommitAck(payload, meta, conn) {
|
|
2157
2340
|
const handler = this._opts.onMessage;
|
|
2158
|
-
const
|
|
2341
|
+
const inbound = readInbound(payload);
|
|
2342
|
+
const selectCode = this._ackModeSelector(inbound, conn);
|
|
2159
2343
|
if (handler === void 0) {
|
|
2160
2344
|
this._dispatchAck(
|
|
2161
2345
|
conn,
|
|
2162
|
-
|
|
2346
|
+
buildRawAckFrom(inbound, this._resolveAutoAckPositive(inbound, meta, conn, selectCode))
|
|
2163
2347
|
);
|
|
2164
2348
|
return;
|
|
2165
2349
|
}
|
|
2166
2350
|
let code;
|
|
2351
|
+
let settled;
|
|
2167
2352
|
try {
|
|
2168
2353
|
await handler(payload, meta, conn);
|
|
2169
|
-
|
|
2354
|
+
settled = readInbound(payload);
|
|
2355
|
+
code = this._resolveAutoAckPositive(settled, meta, conn, selectCode);
|
|
2170
2356
|
} catch (err) {
|
|
2171
2357
|
const nack = resolveNackCode(err);
|
|
2172
2358
|
code = selectCode(nack);
|
|
@@ -2179,7 +2365,8 @@ var MllpServer = class extends EventEmitter {
|
|
|
2179
2365
|
})
|
|
2180
2366
|
);
|
|
2181
2367
|
}
|
|
2182
|
-
|
|
2368
|
+
settled ??= readInbound(payload);
|
|
2369
|
+
this._dispatchAck(conn, buildRawAckFrom(settled, code));
|
|
2183
2370
|
}
|
|
2184
2371
|
/**
|
|
2185
2372
|
* Resolve the auto-ACK code for a message the server would otherwise acknowledge
|
|
@@ -2208,10 +2395,9 @@ var MllpServer = class extends EventEmitter {
|
|
|
2208
2395
|
* `selectCode` puts the resolved disposition in the half of HL7 Table 0008 the inbound
|
|
2209
2396
|
* asked for; it is the identity for a message that asked for nothing.
|
|
2210
2397
|
*/
|
|
2211
|
-
_resolveAutoAckPositive(
|
|
2398
|
+
_resolveAutoAckPositive(inbound, meta, conn, selectCode) {
|
|
2212
2399
|
const discarded = meta.warnings.some((w) => w.code === "MLLP_TRAILING_BYTES");
|
|
2213
|
-
|
|
2214
|
-
if (!discarded && !uncorrelatable) return selectCode("AA");
|
|
2400
|
+
if (!discarded && !uncorrelatable(inbound)) return selectCode("AA");
|
|
2215
2401
|
const reason = discarded ? "discarded-bytes" : "uncorrelatable-inbound";
|
|
2216
2402
|
const code = selectCode("AE");
|
|
2217
2403
|
this._emitContained(
|
|
@@ -2252,9 +2438,9 @@ var MllpServer = class extends EventEmitter {
|
|
|
2252
2438
|
* Downgrading a message the handler committed would make a discharged sender resend a
|
|
2253
2439
|
* message already in storage.
|
|
2254
2440
|
*/
|
|
2255
|
-
_ackModeSelector(
|
|
2441
|
+
_ackModeSelector(inbound, conn) {
|
|
2256
2442
|
const identity = (disposition) => disposition;
|
|
2257
|
-
const msh =
|
|
2443
|
+
const { msh } = inbound;
|
|
2258
2444
|
if (msh === null) return identity;
|
|
2259
2445
|
const accept = readAcceptCondition(msh);
|
|
2260
2446
|
if (accept.kind === "unrecognised") {
|
|
@@ -2262,7 +2448,7 @@ var MllpServer = class extends EventEmitter {
|
|
|
2262
2448
|
return identity;
|
|
2263
2449
|
}
|
|
2264
2450
|
if (accept.kind === "null") return identity;
|
|
2265
|
-
if (
|
|
2451
|
+
if (inbound.batch()) return identity;
|
|
2266
2452
|
return (disposition) => acceptAckRequested(accept, disposition) ? acceptModeCounterpart(disposition) : disposition;
|
|
2267
2453
|
}
|
|
2268
2454
|
/**
|
|
@@ -3053,7 +3239,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3053
3239
|
* the Correlator is timer-free. Cleared on close/destroy.
|
|
3054
3240
|
*/
|
|
3055
3241
|
_ackSweepTimer = null;
|
|
3056
|
-
// ── Reconnect state
|
|
3242
|
+
// ── Reconnect state: backoff, the retry strategy and the reconnect cycle ─────────
|
|
3057
3243
|
_autoReconnect;
|
|
3058
3244
|
_initialDelayMs;
|
|
3059
3245
|
_maxDelayMs;
|
|
@@ -3076,7 +3262,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3076
3262
|
_backoffTimer = null;
|
|
3077
3263
|
/** Last delay (ms) used by the strategy. Surfaced via RetryContext.lastDelayMs. */
|
|
3078
3264
|
_lastDelayMs = 0;
|
|
3079
|
-
// ── Backpressure
|
|
3265
|
+
// ── Backpressure and pipeline state: the high-water mark and the in-flight set ───
|
|
3080
3266
|
/** Count cap; `Number.POSITIVE_INFINITY` when only bytes configured. */
|
|
3081
3267
|
_hwmCount;
|
|
3082
3268
|
/** Byte cap; `Number.POSITIVE_INFINITY` when only count configured. */
|
|
@@ -3125,7 +3311,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3125
3311
|
* @internal
|
|
3126
3312
|
*/
|
|
3127
3313
|
_reconnectFactory = null;
|
|
3128
|
-
// ──
|
|
3314
|
+
// ── Observability counters for getStats() ─────────────────────────────────
|
|
3129
3315
|
/** Total successful `conn.send()` flushes since construction. */
|
|
3130
3316
|
_sentTotal = 0;
|
|
3131
3317
|
/** Total ACKs resolved since construction. */
|
|
@@ -3391,6 +3577,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3391
3577
|
ackCode: ctx.ackCode,
|
|
3392
3578
|
controlIdBytes: ctx.controlIdBytes,
|
|
3393
3579
|
elapsedSinceSendMs: ctx.elapsedSinceSendMs,
|
|
3580
|
+
/* v8 ignore next -- send() refuses before any warning when no connection is attached, so one always is here */
|
|
3394
3581
|
connectionId: this._connection?.connectionId ?? conn?.connectionId,
|
|
3395
3582
|
timestamp: /* @__PURE__ */ new Date()
|
|
3396
3583
|
});
|
|
@@ -3494,7 +3681,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3494
3681
|
this._correlator = new Correlator({
|
|
3495
3682
|
mode: this._correlateByControlId ? "controlId" : "fifo",
|
|
3496
3683
|
ackTimeoutMs: this._ackTimeoutMs,
|
|
3497
|
-
//
|
|
3684
|
+
// pipeline:false collapses the in-flight set to at most one.
|
|
3498
3685
|
maxInFlight: this._pipeline ? Number.POSITIVE_INFINITY : 1,
|
|
3499
3686
|
onWarning: (code, ctx) => {
|
|
3500
3687
|
this._aggregatedWarningsByCode[code] = (this._aggregatedWarningsByCode[code] ?? 0) + 1;
|
|
@@ -3504,6 +3691,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3504
3691
|
message: ackDiagnosticMessage(code),
|
|
3505
3692
|
controlIdBytes: ctx.controlIdBytes,
|
|
3506
3693
|
elapsedSinceSendMs: ctx.elapsedSinceSendMs,
|
|
3694
|
+
/* v8 ignore next -- correlator callbacks run only after attach has stored this connection */
|
|
3507
3695
|
connectionId: this._connection?.connectionId ?? conn.connectionId,
|
|
3508
3696
|
timestamp: /* @__PURE__ */ new Date()
|
|
3509
3697
|
});
|
|
@@ -3520,6 +3708,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3520
3708
|
safeEmitError(
|
|
3521
3709
|
this,
|
|
3522
3710
|
Object.freeze({
|
|
3711
|
+
/* v8 ignore next -- correlator callbacks run only after attach has stored this connection */
|
|
3523
3712
|
connectionId: this._connection?.connectionId ?? conn.connectionId,
|
|
3524
3713
|
error: err,
|
|
3525
3714
|
// The peer's MSA-2 byte LENGTH. The bytes themselves used to be
|
|
@@ -3535,6 +3724,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3535
3724
|
new MllpTimeoutError(`ACK timeout after ${elapsedMs}ms`, {
|
|
3536
3725
|
messageControlIdBytes: entry.controlId?.length,
|
|
3537
3726
|
elapsedMs,
|
|
3727
|
+
/* v8 ignore next -- only flushed sends can time out, so sentAt is set */
|
|
3538
3728
|
sentAt: entry.sentAt ?? 0
|
|
3539
3729
|
})
|
|
3540
3730
|
);
|
|
@@ -3566,6 +3756,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3566
3756
|
safeEmitError(
|
|
3567
3757
|
this,
|
|
3568
3758
|
Object.freeze({
|
|
3759
|
+
/* v8 ignore next -- correlator callbacks run only after attach has stored this connection */
|
|
3569
3760
|
connectionId: this._connection?.connectionId ?? conn.connectionId,
|
|
3570
3761
|
error: err instanceof Error ? err : new Error(String(err))
|
|
3571
3762
|
})
|
|
@@ -3744,6 +3935,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3744
3935
|
safeEmitError(
|
|
3745
3936
|
this,
|
|
3746
3937
|
Object.freeze({
|
|
3938
|
+
/* v8 ignore next -- a disconnect always arrives from an attached connection, so one is stored */
|
|
3747
3939
|
connectionId: this._connection?.connectionId ?? "<none>",
|
|
3748
3940
|
error: this._lastError
|
|
3749
3941
|
})
|
|
@@ -3761,6 +3953,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
3761
3953
|
this._emitContained(
|
|
3762
3954
|
"reconnecting",
|
|
3763
3955
|
Object.freeze({
|
|
3956
|
+
/* v8 ignore next -- a disconnect always arrives from an attached connection, so one is stored */
|
|
3764
3957
|
connectionId: this._connection?.connectionId ?? "<none>",
|
|
3765
3958
|
attempt: this._attempt,
|
|
3766
3959
|
delayMs: delay
|
|
@@ -4006,7 +4199,7 @@ var MllpClient = class extends EventEmitter {
|
|
|
4006
4199
|
*
|
|
4007
4200
|
* Emits a frozen `'ack'` event on every successful match.
|
|
4008
4201
|
*
|
|
4009
|
-
*
|
|
4202
|
+
* **Enhanced acknowledgement mode**
|
|
4010
4203
|
*
|
|
4011
4204
|
* A message whose MSH-15 or MSH-16 is not null asks for the two-part protocol of HL7
|
|
4012
4205
|
* v2.5.1 §2.9, and on a client correlating by control ID this method delivers it: the
|
|
@@ -4884,6 +5077,7 @@ function recordWarning(into, warning, offsetBase = 0) {
|
|
|
4884
5077
|
function classifyFrame(exchange, payload, frameStart, chunks, deviations, finish) {
|
|
4885
5078
|
const canonical = encodeFrame(payload, {
|
|
4886
5079
|
allowDelimiterBytesInPayload: true,
|
|
5080
|
+
/* v8 ignore next 3 -- no delivered payload holds VT or FS, so the encoder never warns here */
|
|
4887
5081
|
onWarning: (w) => {
|
|
4888
5082
|
recordWarning(deviations, w, frameStart + 1);
|
|
4889
5083
|
}
|
|
@@ -4955,7 +5149,7 @@ async function runDifferential(options = {}) {
|
|
|
4955
5149
|
}
|
|
4956
5150
|
|
|
4957
5151
|
// src/index.ts
|
|
4958
|
-
var VERSION = "0.1.
|
|
5152
|
+
var VERSION = "0.1.8";
|
|
4959
5153
|
|
|
4960
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 };
|
|
4961
5155
|
//# sourceMappingURL=index.mjs.map
|