@cosyte/astm 0.0.11 → 0.0.13

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.mjs CHANGED
@@ -1385,7 +1385,8 @@ var AstmSerializeError = class extends Error {
1385
1385
  /**
1386
1386
  * Stable discriminant. `ASTM_EMIT_UNENCODABLE_VALUE` for a `CR`/`LF` in a value;
1387
1387
  * `ASTM_EMIT_INVALID_DELIMITERS` for a delimiter set that cannot be emitted
1388
- * reversibly.
1388
+ * reversibly; `ASTM_EMIT_TYPE_LETTER_COLLISION` for a record whose own type
1389
+ * letter the set being emitted with would escape away.
1389
1390
  */
1390
1391
  code;
1391
1392
  /** 0-based ordinal of the record within the message, when known. */
@@ -1449,10 +1450,24 @@ function serializeAstmRecord(record, d = CANONICAL_DELIMITERS) {
1449
1450
  return serializeRecordChecked(record, d);
1450
1451
  }
1451
1452
  function serializeRecordChecked(record, d) {
1453
+ return assertTypeLetterSurvives(serializeRecordText(record, d), record);
1454
+ }
1455
+ function serializeRecordText(record, d) {
1452
1456
  if (record.type === "M" || record.type === "S") return serializeVerbatimRecord(record, d);
1453
1457
  if (record.type === "H") return serializeHeader(record, d);
1454
1458
  return record.fields.map((f) => encodeField(f.repeats, d, record.recordIndex)).join(d.field);
1455
1459
  }
1460
+ function recordTypeLetter(record) {
1461
+ return record.type === "unsupported" ? record.rawType : record.type;
1462
+ }
1463
+ function assertTypeLetterSurvives(text, record) {
1464
+ if (text.charAt(0) === recordTypeLetter(record)) return text;
1465
+ throw new AstmSerializeError(
1466
+ "A record's type letter is escaped away by the delimiters being emitted with, so the record would read back as a different record. The record's index is on the error; the letter itself is not quoted, because on an unsupported record it is a byte off the wire.",
1467
+ record.recordIndex,
1468
+ "ASTM_EMIT_TYPE_LETTER_COLLISION"
1469
+ );
1470
+ }
1456
1471
  function serializeVerbatimRecord(record, d) {
1457
1472
  if (recoversVerbatim(record, d)) return record.rawLine;
1458
1473
  return record.fields.map((f) => encodeField(f.repeats, d, record.recordIndex)).join(d.field);
@@ -2227,7 +2242,9 @@ var AstmFrameEncodeError = class extends Error {
2227
2242
  /**
2228
2243
  * Position of the offending character within that record, when applicable:
2229
2244
  * its index in the string, never the character itself and never its code
2230
- * point. Enough to find it in the caller's own data.
2245
+ * point. Enough to find it in the caller's own data. For a record supplied as
2246
+ * a `Uint8Array` this is the offending byte's index, which is the same
2247
+ * position: a record string is a byte string, so the two indices coincide.
2231
2248
  */
2232
2249
  characterIndex;
2233
2250
  /** @internal */
@@ -2257,6 +2274,20 @@ function toBytes(input, recordIndex) {
2257
2274
  }
2258
2275
  return out;
2259
2276
  }
2277
+ var RESERVED_STRUCTURE_BYTES = /* @__PURE__ */ new Set([STX, ETB, ETX]);
2278
+ function assertFramable(bytes, recordIndex) {
2279
+ for (let i = 0; i < bytes.length; i += 1) {
2280
+ const byte = bytes[i];
2281
+ if (byte !== void 0 && RESERVED_STRUCTURE_BYTES.has(byte)) {
2282
+ throw new AstmFrameEncodeError(
2283
+ "A record contains a byte the framing layer reads as structure (STX, ETB or ETX). A frame has no escape sequence for one, so it cannot be carried inside a frame: remove it from the record before framing.",
2284
+ recordIndex,
2285
+ "ASTM_FRAME_RESERVED_BYTE",
2286
+ i
2287
+ );
2288
+ }
2289
+ }
2290
+ }
2260
2291
  function encodeFrame(text, frameNumber, isFinal) {
2261
2292
  const term = isFinal ? ETX : ETB;
2262
2293
  const bytes = [STX, FN_ZERO + frameNumber, ...text, term];
@@ -2278,6 +2309,7 @@ function composeAstmFrames(records, options = {}) {
2278
2309
  recordIndex
2279
2310
  );
2280
2311
  }
2312
+ assertFramable(bytes, recordIndex);
2281
2313
  for (let offset = 0; offset < bytes.length; offset += MAX_FRAME_TEXT) {
2282
2314
  const chunk = bytes.subarray(offset, offset + MAX_FRAME_TEXT);
2283
2315
  const isFinal = offset + MAX_FRAME_TEXT >= bytes.length;
@@ -2313,7 +2345,7 @@ function concatBytes3(chunks) {
2313
2345
  }
2314
2346
 
2315
2347
  // src/index.ts
2316
- var VERSION = "0.0.11";
2348
+ var VERSION = "0.0.13";
2317
2349
 
2318
2350
  export { ALL_ASTM_WARNING_CODES, AMBIGUOUS_CODES, ACK as ASTM_ACK, ENQ as ASTM_ENQ, EOT as ASTM_EOT, NAK as ASTM_NAK, AstmAmbiguousStreamError, AstmFrameEncodeError, AstmFrameStrictError, AstmParseError, AstmProfileDefinitionError, AstmSerializeError, AstmStrictError, CANONICAL_DELIMITERS, FATAL_CODES, FRAME_WARNING_CODES, LIVD_WARNING_CODES, LTP_WARNING_CODES, SAFETY_CRITICAL_CODES, TOLERABLE_CODES, VERSION, WARNING_CODES, ambiguousMessageKind, ambiguousValueSplit, applyAstmProfile, applyAstmProfileToWarnings, applyLivd, astmDateToLocalISO, astmProfiles, attachComments, buildAstmMessage, classifyMessage, comments, commentsFor, composeAstmFrames, computeChecksum, decodeAstmFrames, decodeEscapes, deepFreeze, defineAstmProfile, defineLivdCatalog, delimitersRedeclared, detectFraming, encodeComponent, fieldScalar, fieldsUnseparated, frameBadChecksum, frameOversize, frameSequenceGap, frameUnterminated, getAstmProfile, getDefaultAstmProfile, interpretAbnormalFlag, interpretResultStatus, isNonStandard, isSafetyCriticalCode, listAstmProfiles, livdAmbiguousMapping, livdUnmappedCode, lookupLivdForRecord, ltpAmbiguousTransport, ltpFrameRejected, ltpInitialState, ltpReduce, ltpUnexpectedEvent, messages, nonStandardDelimiters, orders, orphanComment, parseAstmDate, parseAstmRecords, parseChecksumHex, parseFramedAstm, parseReferenceRange, partialTimestamp, patient, primaryCode, profileQuirkApplied, query, readDelimiters, recognizeUniversalTestId, resolveProfileTransport, results, serializeAstmRecord, serializeAstmRecords, serializeField, serializeFramedAstm, setDefaultAstmProfile, splitEscapeAware, toChecksumHex, tokenizeHeader, tokenizeRecord, undefinedAbnormalFlag, undefinedResultStatus, uninterpretedQueryStatus, unitsAbsent, unknownEscapeSequence, unknownRecordType, unpairedEscapeCharacter, unparseableReferenceRange, unreadableRedeclaration };
2319
2351
  //# sourceMappingURL=index.mjs.map