@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.
@@ -62,26 +62,17 @@ function createWarning(code, byteOffset, message) {
62
62
  function encodeFrame(payload, opts) {
63
63
  const allow = opts?.allowDelimiterBytesInPayload === true;
64
64
  const onWarning = opts?.onWarning;
65
- for (let i = 0; i < payload.length; i++) {
66
- const byte = payload[i];
67
- if (byte === void 0) break;
68
- if (byte === VT || byte === FS) {
69
- const code = byte === VT ? "MLLP_PAYLOAD_CONTAINS_VT" : "MLLP_PAYLOAD_CONTAINS_FS";
70
- if (!allow) {
71
- throw new MllpFramingError(code, i, Buffer.from([byte]));
72
- }
73
- if (onWarning !== void 0) {
74
- const byteHex = byte === VT ? "0x0B" : "0x1C";
75
- const warning = createWarning(
76
- code,
77
- i,
78
- `Payload contains ${byte === VT ? "VT" : "FS"} byte (${byteHex}) at offset ${i}; frame will be ambiguous`
79
- );
80
- try {
81
- onWarning(warning);
82
- } catch {
83
- }
84
- }
65
+ if (!(payload instanceof Uint8Array)) {
66
+ scanEachByte(payload, allow, onWarning);
67
+ } else if (!allow || onWarning !== void 0) {
68
+ let vt = payload.indexOf(VT);
69
+ let fs = payload.indexOf(FS);
70
+ while (vt !== -1 || fs !== -1) {
71
+ const isVt = fs === -1 || vt !== -1 && vt < fs;
72
+ const i = isVt ? vt : fs;
73
+ delimiterAt(isVt ? VT : FS, i, allow, onWarning);
74
+ if (isVt) vt = payload.indexOf(VT, i + 1);
75
+ else fs = payload.indexOf(FS, i + 1);
85
76
  }
86
77
  }
87
78
  const frame = Buffer.allocUnsafe(payload.length + 3);
@@ -91,6 +82,31 @@ function encodeFrame(payload, opts) {
91
82
  frame[payload.length + 2] = CR;
92
83
  return frame;
93
84
  }
85
+ function scanEachByte(payload, allow, onWarning) {
86
+ for (let i = 0; i < payload.length; i++) {
87
+ const byte = payload[i];
88
+ if (byte === void 0) break;
89
+ if (byte === VT || byte === FS) delimiterAt(byte, i, allow, onWarning);
90
+ }
91
+ }
92
+ function delimiterAt(byte, i, allow, onWarning) {
93
+ const code = byte === VT ? "MLLP_PAYLOAD_CONTAINS_VT" : "MLLP_PAYLOAD_CONTAINS_FS";
94
+ if (!allow) {
95
+ throw new MllpFramingError(code, i, Buffer.from([byte]));
96
+ }
97
+ if (onWarning !== void 0) {
98
+ const byteHex = byte === VT ? "0x0B" : "0x1C";
99
+ const warning = createWarning(
100
+ code,
101
+ i,
102
+ `Payload contains ${byte === VT ? "VT" : "FS"} byte (${byteHex}) at offset ${i}; frame will be ambiguous`
103
+ );
104
+ try {
105
+ onWarning(warning);
106
+ } catch {
107
+ }
108
+ }
109
+ }
94
110
 
95
111
  // src/internal/control-id.ts
96
112
  var ASCII_M = 77;
@@ -111,17 +127,22 @@ var UNSAFE_FIELD_SEPARATORS = /* @__PURE__ */ new Set([
111
127
  function isMshSegmentAt(buf, i) {
112
128
  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]);
113
129
  }
114
- function segmentEnd(buf, i) {
115
- let end = i;
116
- while (end < buf.length && buf[end] !== SEGMENT_SEPARATOR_CR && buf[end] !== SEGMENT_SEPARATOR_LF) {
117
- end++;
118
- }
119
- return end;
130
+ function segmentEnds(buf) {
131
+ let cr = -2;
132
+ let lf = -2;
133
+ return (i) => {
134
+ if (cr !== -1 && cr < i) cr = buf.indexOf(SEGMENT_SEPARATOR_CR, i);
135
+ if (lf !== -1 && lf < i) lf = buf.indexOf(SEGMENT_SEPARATOR_LF, i);
136
+ const atCr = cr === -1 ? buf.length : cr;
137
+ const atLf = lf === -1 ? buf.length : lf;
138
+ return atCr < atLf ? atCr : atLf;
139
+ };
120
140
  }
121
141
  function findMshSegment(buf) {
142
+ const segmentEnd = segmentEnds(buf);
122
143
  let segStart = 0;
123
144
  while (segStart < buf.length) {
124
- const end = segmentEnd(buf, segStart);
145
+ const end = segmentEnd(segStart);
125
146
  if (isMshSegmentAt(buf, segStart)) return { start: segStart, end };
126
147
  let next = end;
127
148
  while (next < buf.length && (buf[next] === SEGMENT_SEPARATOR_CR || buf[next] === SEGMENT_SEPARATOR_LF)) {