document-bytes 1.9.0 → 1.10.0

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
@@ -2187,11 +2187,11 @@ function pointerCodePoint(table, pointer) {
2187
2187
  if (codeUnit !== REPLACEMENT_CHARACTER_CODE_UNIT) return Number.isNaN(codeUnit) ? void 0 : codeUnit;
2188
2188
  return table.astral.get(pointer);
2189
2189
  }
2190
- function malformed(label, detail) {
2190
+ function malformed$1(label, detail) {
2191
2191
  throw new UndecodableTextError("malformed", `${label} ${detail}`);
2192
2192
  }
2193
- function truncated(label) {
2194
- malformed(label, "ends with an incomplete multi-byte sequence");
2193
+ function truncated$1(label) {
2194
+ malformed$1(label, "ends with an incomplete multi-byte sequence");
2195
2195
  }
2196
2196
  /**
2197
2197
  * Big5's own four hardcoded pointer exceptions where a single lead/trail byte pair decodes to *two* Unicode code points rather than one — combining-diacritic spellings of vowels HKSCS adds that Big5's own index, limited like every WHATWG index to single code points, cannot hold directly (https://encoding.spec.whatwg.org/#big5-decoder, the table immediately under "If there is a row in the table below whose first column is pointer"). Checked before the general {@link BIG5} lookup, exactly as the standard's own decoder algorithm does.
@@ -2238,16 +2238,16 @@ function decodeShiftJis(bytes) {
2238
2238
  continue;
2239
2239
  }
2240
2240
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(JIS0208, pointer);
2241
- if (codePoint === void 0) malformed("Shift_JIS", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2241
+ if (codePoint === void 0) malformed$1("Shift_JIS", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2242
2242
  appendCodePoint(units, codePoint);
2243
2243
  continue;
2244
2244
  }
2245
2245
  if (byte <= ASCII_BYTE_MAX || byte === 128) units.push(byte);
2246
2246
  else if (byte >= 161 && byte <= 223) appendCodePoint(units, 65216 + byte);
2247
2247
  else if (byte <= 159 || byte >= 224 && byte <= 252) leading = byte;
2248
- else malformed("Shift_JIS", `has no character for byte 0x${byte.toString(16)}`);
2248
+ else malformed$1("Shift_JIS", `has no character for byte 0x${byte.toString(16)}`);
2249
2249
  }
2250
- if (leading !== 0) truncated("Shift_JIS");
2250
+ if (leading !== 0) truncated$1("Shift_JIS");
2251
2251
  return fromCodeUnits(units);
2252
2252
  }
2253
2253
  /**
@@ -2279,15 +2279,15 @@ function decodeEucJp(bytes) {
2279
2279
  const usedJis0212 = jis0212;
2280
2280
  jis0212 = false;
2281
2281
  const codePoint = byte >= 161 && byte <= 254 ? pointerCodePoint(usedJis0212 ? JIS0212 : JIS0208, (leadingByte - 161) * 94 + byte - 161) : void 0;
2282
- if (codePoint === void 0) malformed("EUC-JP", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2282
+ if (codePoint === void 0) malformed$1("EUC-JP", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2283
2283
  appendCodePoint(units, codePoint);
2284
2284
  continue;
2285
2285
  }
2286
2286
  if (byte <= ASCII_BYTE_MAX) units.push(byte);
2287
2287
  else if (byte === 142 || byte === 143 || byte >= 161 && byte <= 254) leading = byte;
2288
- else malformed("EUC-JP", `has no character for byte 0x${byte.toString(16)}`);
2288
+ else malformed$1("EUC-JP", `has no character for byte 0x${byte.toString(16)}`);
2289
2289
  }
2290
- if (leading !== 0) truncated("EUC-JP");
2290
+ if (leading !== 0) truncated$1("EUC-JP");
2291
2291
  return fromCodeUnits(units);
2292
2292
  }
2293
2293
  /**
@@ -2306,15 +2306,15 @@ function decodeEucKr(bytes) {
2306
2306
  const leadingByte = leading;
2307
2307
  leading = 0;
2308
2308
  const codePoint = byte >= 65 && byte <= 254 ? pointerCodePoint(EUC_KR, (leadingByte - 129) * 190 + byte - 65) : void 0;
2309
- if (codePoint === void 0) malformed("EUC-KR", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2309
+ if (codePoint === void 0) malformed$1("EUC-KR", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2310
2310
  appendCodePoint(units, codePoint);
2311
2311
  continue;
2312
2312
  }
2313
2313
  if (byte <= ASCII_BYTE_MAX) units.push(byte);
2314
2314
  else if (byte >= 129 && byte <= 254) leading = byte;
2315
- else malformed("EUC-KR", `has no character for byte 0x${byte.toString(16)}`);
2315
+ else malformed$1("EUC-KR", `has no character for byte 0x${byte.toString(16)}`);
2316
2316
  }
2317
- if (leading !== 0) truncated("EUC-KR");
2317
+ if (leading !== 0) truncated$1("EUC-KR");
2318
2318
  return fromCodeUnits(units);
2319
2319
  }
2320
2320
  /**
@@ -2341,15 +2341,15 @@ function decodeBig5(bytes) {
2341
2341
  continue;
2342
2342
  }
2343
2343
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(BIG5, pointer);
2344
- if (codePoint === void 0) malformed("Big5", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2344
+ if (codePoint === void 0) malformed$1("Big5", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2345
2345
  appendCodePoint(units, codePoint);
2346
2346
  continue;
2347
2347
  }
2348
2348
  if (byte <= ASCII_BYTE_MAX) units.push(byte);
2349
2349
  else if (byte >= 129 && byte <= 254) leading = byte;
2350
- else malformed("Big5", `has no character for byte 0x${byte.toString(16)}`);
2350
+ else malformed$1("Big5", `has no character for byte 0x${byte.toString(16)}`);
2351
2351
  }
2352
- if (leading !== 0) truncated("Big5");
2352
+ if (leading !== 0) truncated$1("Big5");
2353
2353
  return fromCodeUnits(units);
2354
2354
  }
2355
2355
  /**
@@ -2389,7 +2389,7 @@ function decodeGb18030(bytes) {
2389
2389
  let third = 0;
2390
2390
  for (const byte of bytes) {
2391
2391
  if (third !== 0) {
2392
- if (byte < 48 || byte > 57) malformed("gb18030", `has no character for four-byte sequence 0x${first.toString(16)} 0x${second.toString(16)} 0x${third.toString(16)} 0x${byte.toString(16)}: fourth byte is not 0x30-0x39`);
2392
+ if (byte < 48 || byte > 57) malformed$1("gb18030", `has no character for four-byte sequence 0x${first.toString(16)} 0x${second.toString(16)} 0x${third.toString(16)} 0x${byte.toString(16)}: fourth byte is not 0x30-0x39`);
2393
2393
  const codePoint = gb18030RangesCodePoint((first - 129) * (1260 * GB18030_SECOND_FOURTH_BYTE_COUNT) + (second - 48) * 1260 + (third - 129) * GB18030_SECOND_FOURTH_BYTE_COUNT + (byte - 48));
2394
2394
  const failedFirst = first;
2395
2395
  const failedSecond = second;
@@ -2397,7 +2397,7 @@ function decodeGb18030(bytes) {
2397
2397
  first = 0;
2398
2398
  second = 0;
2399
2399
  third = 0;
2400
- if (codePoint === void 0) malformed("gb18030", `has no character for four-byte sequence 0x${failedFirst.toString(16)} 0x${failedSecond.toString(16)} 0x${failedThird.toString(16)} 0x${byte.toString(16)}`);
2400
+ if (codePoint === void 0) malformed$1("gb18030", `has no character for four-byte sequence 0x${failedFirst.toString(16)} 0x${failedSecond.toString(16)} 0x${failedThird.toString(16)} 0x${byte.toString(16)}`);
2401
2401
  appendCodePoint(units, codePoint);
2402
2402
  continue;
2403
2403
  }
@@ -2406,7 +2406,7 @@ function decodeGb18030(bytes) {
2406
2406
  third = byte;
2407
2407
  continue;
2408
2408
  }
2409
- malformed("gb18030", `has no character for byte sequence 0x${first.toString(16)} 0x${second.toString(16)} 0x${byte.toString(16)}: third byte is not 0x81-0xFE`);
2409
+ malformed$1("gb18030", `has no character for byte sequence 0x${first.toString(16)} 0x${second.toString(16)} 0x${byte.toString(16)}: third byte is not 0x81-0xFE`);
2410
2410
  }
2411
2411
  if (first !== 0) {
2412
2412
  if (byte >= 48 && byte <= 57) {
@@ -2418,16 +2418,133 @@ function decodeGb18030(bytes) {
2418
2418
  const column = twoByteTrailColumn(byte, 128, 254);
2419
2419
  const pointer = column === void 0 ? void 0 : (leadingByte - 129) * 190 + column;
2420
2420
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(GB18030, pointer);
2421
- if (codePoint === void 0) malformed("gb18030", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2421
+ if (codePoint === void 0) malformed$1("gb18030", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2422
2422
  appendCodePoint(units, codePoint);
2423
2423
  continue;
2424
2424
  }
2425
2425
  if (byte <= ASCII_BYTE_MAX) units.push(byte);
2426
2426
  else if (byte === 128) units.push(8364);
2427
2427
  else if (byte <= 254) first = byte;
2428
- else malformed("gb18030", `has no character for byte 0x${byte.toString(16)}`);
2428
+ else malformed$1("gb18030", `has no character for byte 0x${byte.toString(16)}`);
2429
2429
  }
2430
- if (first !== 0) truncated("gb18030");
2430
+ if (first !== 0) truncated$1("gb18030");
2431
+ return fromCodeUnits(units);
2432
+ }
2433
+ //#endregion
2434
+ //#region src/text/decode-iso-2022-jp.ts
2435
+ function malformed(detail) {
2436
+ throw new UndecodableTextError("malformed", `ISO-2022-JP ${detail}`);
2437
+ }
2438
+ function truncated() {
2439
+ malformed("ends with an incomplete multi-byte or escape sequence");
2440
+ }
2441
+ /** The byte value that begins every ISO-2022-JP escape sequence, wherever this decoder is. */
2442
+ const ESCAPE = 27;
2443
+ /** JIS X 0201 Katakana's own byte range (0x21-0x5F), mapped directly onto the same halfwidth katakana Unicode block decode-dbcs.ts's Shift_JIS and EUC-JP decoders both compute from their own byte ranges (0xFF61 plus the byte's own offset from the range's first value). */
2444
+ const KATAKANA_FIRST_BYTE = 33;
2445
+ const KATAKANA_LAST_BYTE = 95;
2446
+ /** JIS X 0208's own index byte range (0x21-0x7E) for both the leading and trailing byte of a two-byte pointer. */
2447
+ const JIS0208_INDEX_BYTE_MIN = 33;
2448
+ const JIS0208_INDEX_BYTE_MAX = 126;
2449
+ const JIS0208_INDEX_COLUMNS = 94;
2450
+ /**
2451
+ * Whether `byte` is one this decoder's ASCII and Roman states pass through unchanged: any byte below 0x80 except the two Shift-Out/Shift-In control codes (0x0E, 0x0F) a different ISO-2022 variant uses for a switching mechanism this decoder does not support. No check for {@link ESCAPE} here: both callers (the "ascii" and "roman" cases below) already intercept it with their own `byte === ESCAPE` branch before ever reaching this function, so `byte` here is never that value in the first place.
2452
+ */
2453
+ function isPlainByte(byte) {
2454
+ return byte <= 127 && byte !== 14 && byte !== 15;
2455
+ }
2456
+ /**
2457
+ * Decodes bytes as ISO-2022-JP (https://encoding.spec.whatwg.org/#iso-2022-jp-decoder).
2458
+ *
2459
+ * A stateful decoder carrying one of four designated character sets across the whole input — ASCII, JIS X 0201 Roman (ASCII with 0x5C and 0x7E replaced by ¥ and ‾), JIS X 0201 Katakana (0x21-0x5F as halfwidth katakana), or JIS X 0208 (a two-byte pointer into {@link JIS0208}) — switched by a three-byte escape sequence: `ESC ( B` for ASCII, `ESC ( J` for Roman, `ESC ( I` for Katakana, and either `ESC $ @` or `ESC $ B` for JIS X 0208 (the Encoding Standard treats both the 1978 and 1983 JIS C 6226 designators identically, decoding through the one JIS0208 index either way). Starts in ASCII.
2460
+ *
2461
+ * One designator escape is refused even though it is syntactically well-formed: two in a row with no character decoded between them. The Encoding Standard's own decoder tracks this with a boolean (`ISO-2022-JP output`, reset to false by every character emitted, by every error, and by entering a JIS X 0208 pair's leading-byte state, set true by every successful designator switch) and, when a second switch arrives before that flag is reset, treats it as an error step rather than a silent state change — confirmed directly against a real browser engine (Node's own `TextDecoder`, which is ICU-backed and implements this algorithm): two designator escapes back to back decode the second one as U+FFFD, while a designator escape that follows a real character, or a partially-read JIS X 0208 pair, does not. This decoder throws where that reference decoder would emit U+FFFD, matching this module's throw-on-malformed house style.
2462
+ * @param bytes - The bytes to decode.
2463
+ * @returns The decoded text.
2464
+ * @throws UndecodableTextError When the bytes contain a byte, byte pair, or escape sequence ISO-2022-JP leaves undefined, a redundant designator escape, or end with an incomplete multi-byte or escape sequence.
2465
+ */
2466
+ function decodeIso2022Jp(bytes) {
2467
+ const units = [];
2468
+ let state = "ascii";
2469
+ let pending = 0;
2470
+ let output = false;
2471
+ for (const byte of bytes) switch (state) {
2472
+ case "ascii":
2473
+ if (byte === ESCAPE) {
2474
+ state = "escapeStart";
2475
+ continue;
2476
+ }
2477
+ output = false;
2478
+ if (!isPlainByte(byte)) malformed(`has no character for byte 0x${byte.toString(16)} in ASCII mode`);
2479
+ units.push(byte);
2480
+ continue;
2481
+ case "roman":
2482
+ if (byte === ESCAPE) {
2483
+ state = "escapeStart";
2484
+ continue;
2485
+ }
2486
+ output = false;
2487
+ if (byte === 92) {
2488
+ units.push(165);
2489
+ continue;
2490
+ }
2491
+ if (byte === 126) {
2492
+ units.push(8254);
2493
+ continue;
2494
+ }
2495
+ if (!isPlainByte(byte)) malformed(`has no character for byte 0x${byte.toString(16)} in Roman mode`);
2496
+ units.push(byte);
2497
+ continue;
2498
+ case "katakana":
2499
+ if (byte === ESCAPE) {
2500
+ state = "escapeStart";
2501
+ continue;
2502
+ }
2503
+ output = false;
2504
+ if (byte < KATAKANA_FIRST_BYTE || byte > KATAKANA_LAST_BYTE) malformed(`has no character for byte 0x${byte.toString(16)} in Katakana mode`);
2505
+ units.push(65344 + byte);
2506
+ continue;
2507
+ case "leadingByte":
2508
+ if (byte === ESCAPE) {
2509
+ state = "escapeStart";
2510
+ continue;
2511
+ }
2512
+ output = false;
2513
+ if (byte < JIS0208_INDEX_BYTE_MIN || byte > JIS0208_INDEX_BYTE_MAX) malformed(`has no character for byte 0x${byte.toString(16)} as a JIS X 0208 lead byte`);
2514
+ pending = byte;
2515
+ state = "trailingByte";
2516
+ continue;
2517
+ case "trailingByte": {
2518
+ const leadingByte = pending;
2519
+ state = "leadingByte";
2520
+ if (byte === ESCAPE) malformed(`has an incomplete JIS X 0208 two-byte sequence: lead byte 0x${leadingByte.toString(16)} was interrupted by an escape sequence`);
2521
+ if (byte < JIS0208_INDEX_BYTE_MIN || byte > JIS0208_INDEX_BYTE_MAX) malformed(`has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2522
+ const pointer = (leadingByte - JIS0208_INDEX_BYTE_MIN) * JIS0208_INDEX_COLUMNS + byte - JIS0208_INDEX_BYTE_MIN;
2523
+ const codePoint = pointerCodePoint(JIS0208, pointer);
2524
+ if (codePoint === void 0) malformed(`has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2525
+ units.push(codePoint);
2526
+ continue;
2527
+ }
2528
+ case "escapeStart":
2529
+ if (byte === 36 || byte === 40) {
2530
+ pending = byte;
2531
+ state = "escape";
2532
+ continue;
2533
+ }
2534
+ malformed(`has no supported designator starting with escape byte 0x${byte.toString(16)}`);
2535
+ continue;
2536
+ case "escape": {
2537
+ const leadingEscapeByte = pending;
2538
+ pending = 0;
2539
+ const nextState = leadingEscapeByte === 40 && byte === 66 ? "ascii" : leadingEscapeByte === 40 && byte === 74 ? "roman" : leadingEscapeByte === 40 && byte === 73 ? "katakana" : leadingEscapeByte === 36 && (byte === 64 || byte === 66) ? "leadingByte" : void 0;
2540
+ if (nextState === void 0) malformed(`has an unsupported designator escape sequence 0x1b 0x${leadingEscapeByte.toString(16)} 0x${byte.toString(16)}`);
2541
+ state = nextState;
2542
+ if (output) malformed("has a redundant designator escape sequence with no characters decoded since the previous one");
2543
+ output = true;
2544
+ continue;
2545
+ }
2546
+ }
2547
+ if (state === "trailingByte" || state === "escapeStart" || state === "escape") truncated();
2431
2548
  return fromCodeUnits(units);
2432
2549
  }
2433
2550
  //#endregion
@@ -2714,7 +2831,8 @@ const DECODERS = {
2714
2831
  "euc-kr": decodeEucKr,
2715
2832
  gbk: decodeGb18030,
2716
2833
  gb18030: decodeGb18030,
2717
- big5: decodeBig5
2834
+ big5: decodeBig5,
2835
+ "iso-2022-jp": decodeIso2022Jp
2718
2836
  };
2719
2837
  /** The text-versus-binary rule itself, over character codes: a NUL settles it outright, and every other C0 control outside {@link TEXTUAL_CONTROL_CODES} is allowed only up to one per {@link CODES_PER_PERMITTED_CONTROL}. Reading codes through an accessor rather than taking an array lets the identical rule run over a Uint8Array's bytes and over a decoded string's code units without copying either into one. */
2720
2838
  function codesAreTextual(codeAt, length) {
package/dist/index.d.cts CHANGED
@@ -70,6 +70,10 @@ declare function inflateTolerant(data: Uint8Array<ArrayBuffer>): InflateResult;
70
70
  */
71
71
  type DbcsEncodingLabel = "shift_jis" | "euc-jp" | "euc-kr" | "gbk" | "gb18030" | "big5";
72
72
  //#endregion
73
+ //#region src/text/decode-iso-2022-jp.d.ts
74
+ /** ISO-2022-JP's own label — its own union member, kept apart from {@link DbcsEncodingLabel} in decode-dbcs.ts, for the reason this file's own header comment gives. */
75
+ type Iso2022JpEncodingLabel = "iso-2022-jp";
76
+ //#endregion
73
77
  //#region src/text/legacy-single-byte-tables.d.ts
74
78
  /** A legacy single-byte encoding {@link decodeText} can decode when the caller declares it explicitly. Never produced by detection: nothing distinguishes one of these code pages from another, or from windows-1252, without the statistical model this module does not carry (see {@link TextEncodingLabel}). */
75
79
  type LegacySingleByteEncodingLabel = "ibm866" | "iso-8859-2" | "iso-8859-3" | "iso-8859-4" | "iso-8859-5" | "iso-8859-6" | "iso-8859-7" | "iso-8859-8" | "iso-8859-8-i" | "iso-8859-10" | "iso-8859-13" | "iso-8859-14" | "iso-8859-15" | "iso-8859-16" | "koi8-r" | "koi8-u" | "macintosh" | "windows-874" | "windows-1250" | "windows-1251" | "windows-1253" | "windows-1254" | "windows-1255" | "windows-1256" | "windows-1257" | "windows-1258" | "x-mac-cyrillic";
@@ -80,9 +84,9 @@ type LegacySingleByteEncodingLabel = "ibm866" | "iso-8859-2" | "iso-8859-3" | "i
80
84
  *
81
85
  * Detection stays deliberately bounded to `"utf-8"`, `"utf-16le"`, `"utf-16be"`, `"utf-32le"`, `"utf-32be"` and `"windows-1252"`: each of those is either self-identifying through a byte order mark, structurally checkable through its own validity rules, or guessable behind a stated plausibility test, and nothing distinguishes one legacy code page from another, or from windows-1252, without the statistical model this module does not carry. Bytes that look like an undetected legacy encoding are refused rather than guessed at.
82
86
  *
83
- * {@link DecodeTextOptions.encoding} can name more than detection can reach, though: every {@link LegacySingleByteEncodingLabel} the WHATWG Encoding Standard defines is also accepted, for a caller who already knows their file's code page rather than asking this module to guess it (ExaDev/documents.js#1361), and so is every {@link DbcsEncodingLabel} — the standard's legacy multi-byte and double-byte CJK encodings, Shift_JIS, EUC-JP, EUC-KR, GBK, gb18030 and Big5, decoded against decode-dbcs.ts's own generated index tables for the same reason (ExaDev/documents.js#1388). ISO-2022-JP, the one legacy CJK encoding that is a genuine escape-sequence state machine rather than a byte-width decoder, is tracked as its own follow-on to #1388 rather than bundled in here.
87
+ * {@link DecodeTextOptions.encoding} can name more than detection can reach, though: every {@link LegacySingleByteEncodingLabel} the WHATWG Encoding Standard defines is also accepted, for a caller who already knows their file's code page rather than asking this module to guess it (ExaDev/documents.js#1361), and so is every {@link DbcsEncodingLabel} and {@link Iso2022JpEncodingLabel} — the standard's legacy multi-byte and double-byte CJK encodings, decoded against decode-dbcs.ts's own generated index tables, and ISO-2022-JP's own escape-sequence state machine in decode-iso-2022-jp.ts, for the same reason (ExaDev/documents.js#1388).
84
88
  */
85
- type TextEncodingLabel = "utf-8" | "utf-16le" | "utf-16be" | "utf-32le" | "utf-32be" | "windows-1252" | LegacySingleByteEncodingLabel | DbcsEncodingLabel;
89
+ type TextEncodingLabel = "utf-8" | "utf-16le" | "utf-16be" | "utf-32le" | "utf-32be" | "windows-1252" | LegacySingleByteEncodingLabel | DbcsEncodingLabel | Iso2022JpEncodingLabel;
86
90
  /**
87
91
  * How {@link decodeText} arrived at the encoding it used.
88
92
  *
package/dist/index.d.ts CHANGED
@@ -70,6 +70,10 @@ declare function inflateTolerant(data: Uint8Array<ArrayBuffer>): InflateResult;
70
70
  */
71
71
  type DbcsEncodingLabel = "shift_jis" | "euc-jp" | "euc-kr" | "gbk" | "gb18030" | "big5";
72
72
  //#endregion
73
+ //#region src/text/decode-iso-2022-jp.d.ts
74
+ /** ISO-2022-JP's own label — its own union member, kept apart from {@link DbcsEncodingLabel} in decode-dbcs.ts, for the reason this file's own header comment gives. */
75
+ type Iso2022JpEncodingLabel = "iso-2022-jp";
76
+ //#endregion
73
77
  //#region src/text/legacy-single-byte-tables.d.ts
74
78
  /** A legacy single-byte encoding {@link decodeText} can decode when the caller declares it explicitly. Never produced by detection: nothing distinguishes one of these code pages from another, or from windows-1252, without the statistical model this module does not carry (see {@link TextEncodingLabel}). */
75
79
  type LegacySingleByteEncodingLabel = "ibm866" | "iso-8859-2" | "iso-8859-3" | "iso-8859-4" | "iso-8859-5" | "iso-8859-6" | "iso-8859-7" | "iso-8859-8" | "iso-8859-8-i" | "iso-8859-10" | "iso-8859-13" | "iso-8859-14" | "iso-8859-15" | "iso-8859-16" | "koi8-r" | "koi8-u" | "macintosh" | "windows-874" | "windows-1250" | "windows-1251" | "windows-1253" | "windows-1254" | "windows-1255" | "windows-1256" | "windows-1257" | "windows-1258" | "x-mac-cyrillic";
@@ -80,9 +84,9 @@ type LegacySingleByteEncodingLabel = "ibm866" | "iso-8859-2" | "iso-8859-3" | "i
80
84
  *
81
85
  * Detection stays deliberately bounded to `"utf-8"`, `"utf-16le"`, `"utf-16be"`, `"utf-32le"`, `"utf-32be"` and `"windows-1252"`: each of those is either self-identifying through a byte order mark, structurally checkable through its own validity rules, or guessable behind a stated plausibility test, and nothing distinguishes one legacy code page from another, or from windows-1252, without the statistical model this module does not carry. Bytes that look like an undetected legacy encoding are refused rather than guessed at.
82
86
  *
83
- * {@link DecodeTextOptions.encoding} can name more than detection can reach, though: every {@link LegacySingleByteEncodingLabel} the WHATWG Encoding Standard defines is also accepted, for a caller who already knows their file's code page rather than asking this module to guess it (ExaDev/documents.js#1361), and so is every {@link DbcsEncodingLabel} — the standard's legacy multi-byte and double-byte CJK encodings, Shift_JIS, EUC-JP, EUC-KR, GBK, gb18030 and Big5, decoded against decode-dbcs.ts's own generated index tables for the same reason (ExaDev/documents.js#1388). ISO-2022-JP, the one legacy CJK encoding that is a genuine escape-sequence state machine rather than a byte-width decoder, is tracked as its own follow-on to #1388 rather than bundled in here.
87
+ * {@link DecodeTextOptions.encoding} can name more than detection can reach, though: every {@link LegacySingleByteEncodingLabel} the WHATWG Encoding Standard defines is also accepted, for a caller who already knows their file's code page rather than asking this module to guess it (ExaDev/documents.js#1361), and so is every {@link DbcsEncodingLabel} and {@link Iso2022JpEncodingLabel} — the standard's legacy multi-byte and double-byte CJK encodings, decoded against decode-dbcs.ts's own generated index tables, and ISO-2022-JP's own escape-sequence state machine in decode-iso-2022-jp.ts, for the same reason (ExaDev/documents.js#1388).
84
88
  */
85
- type TextEncodingLabel = "utf-8" | "utf-16le" | "utf-16be" | "utf-32le" | "utf-32be" | "windows-1252" | LegacySingleByteEncodingLabel | DbcsEncodingLabel;
89
+ type TextEncodingLabel = "utf-8" | "utf-16le" | "utf-16be" | "utf-32le" | "utf-32be" | "windows-1252" | LegacySingleByteEncodingLabel | DbcsEncodingLabel | Iso2022JpEncodingLabel;
86
90
  /**
87
91
  * How {@link decodeText} arrived at the encoding it used.
88
92
  *
package/dist/index.js CHANGED
@@ -2186,11 +2186,11 @@ function pointerCodePoint(table, pointer) {
2186
2186
  if (codeUnit !== REPLACEMENT_CHARACTER_CODE_UNIT) return Number.isNaN(codeUnit) ? void 0 : codeUnit;
2187
2187
  return table.astral.get(pointer);
2188
2188
  }
2189
- function malformed(label, detail) {
2189
+ function malformed$1(label, detail) {
2190
2190
  throw new UndecodableTextError("malformed", `${label} ${detail}`);
2191
2191
  }
2192
- function truncated(label) {
2193
- malformed(label, "ends with an incomplete multi-byte sequence");
2192
+ function truncated$1(label) {
2193
+ malformed$1(label, "ends with an incomplete multi-byte sequence");
2194
2194
  }
2195
2195
  /**
2196
2196
  * Big5's own four hardcoded pointer exceptions where a single lead/trail byte pair decodes to *two* Unicode code points rather than one — combining-diacritic spellings of vowels HKSCS adds that Big5's own index, limited like every WHATWG index to single code points, cannot hold directly (https://encoding.spec.whatwg.org/#big5-decoder, the table immediately under "If there is a row in the table below whose first column is pointer"). Checked before the general {@link BIG5} lookup, exactly as the standard's own decoder algorithm does.
@@ -2237,16 +2237,16 @@ function decodeShiftJis(bytes) {
2237
2237
  continue;
2238
2238
  }
2239
2239
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(JIS0208, pointer);
2240
- if (codePoint === void 0) malformed("Shift_JIS", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2240
+ if (codePoint === void 0) malformed$1("Shift_JIS", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2241
2241
  appendCodePoint(units, codePoint);
2242
2242
  continue;
2243
2243
  }
2244
2244
  if (byte <= ASCII_BYTE_MAX || byte === 128) units.push(byte);
2245
2245
  else if (byte >= 161 && byte <= 223) appendCodePoint(units, 65216 + byte);
2246
2246
  else if (byte <= 159 || byte >= 224 && byte <= 252) leading = byte;
2247
- else malformed("Shift_JIS", `has no character for byte 0x${byte.toString(16)}`);
2247
+ else malformed$1("Shift_JIS", `has no character for byte 0x${byte.toString(16)}`);
2248
2248
  }
2249
- if (leading !== 0) truncated("Shift_JIS");
2249
+ if (leading !== 0) truncated$1("Shift_JIS");
2250
2250
  return fromCodeUnits(units);
2251
2251
  }
2252
2252
  /**
@@ -2278,15 +2278,15 @@ function decodeEucJp(bytes) {
2278
2278
  const usedJis0212 = jis0212;
2279
2279
  jis0212 = false;
2280
2280
  const codePoint = byte >= 161 && byte <= 254 ? pointerCodePoint(usedJis0212 ? JIS0212 : JIS0208, (leadingByte - 161) * 94 + byte - 161) : void 0;
2281
- if (codePoint === void 0) malformed("EUC-JP", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2281
+ if (codePoint === void 0) malformed$1("EUC-JP", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2282
2282
  appendCodePoint(units, codePoint);
2283
2283
  continue;
2284
2284
  }
2285
2285
  if (byte <= ASCII_BYTE_MAX) units.push(byte);
2286
2286
  else if (byte === 142 || byte === 143 || byte >= 161 && byte <= 254) leading = byte;
2287
- else malformed("EUC-JP", `has no character for byte 0x${byte.toString(16)}`);
2287
+ else malformed$1("EUC-JP", `has no character for byte 0x${byte.toString(16)}`);
2288
2288
  }
2289
- if (leading !== 0) truncated("EUC-JP");
2289
+ if (leading !== 0) truncated$1("EUC-JP");
2290
2290
  return fromCodeUnits(units);
2291
2291
  }
2292
2292
  /**
@@ -2305,15 +2305,15 @@ function decodeEucKr(bytes) {
2305
2305
  const leadingByte = leading;
2306
2306
  leading = 0;
2307
2307
  const codePoint = byte >= 65 && byte <= 254 ? pointerCodePoint(EUC_KR, (leadingByte - 129) * 190 + byte - 65) : void 0;
2308
- if (codePoint === void 0) malformed("EUC-KR", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2308
+ if (codePoint === void 0) malformed$1("EUC-KR", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2309
2309
  appendCodePoint(units, codePoint);
2310
2310
  continue;
2311
2311
  }
2312
2312
  if (byte <= ASCII_BYTE_MAX) units.push(byte);
2313
2313
  else if (byte >= 129 && byte <= 254) leading = byte;
2314
- else malformed("EUC-KR", `has no character for byte 0x${byte.toString(16)}`);
2314
+ else malformed$1("EUC-KR", `has no character for byte 0x${byte.toString(16)}`);
2315
2315
  }
2316
- if (leading !== 0) truncated("EUC-KR");
2316
+ if (leading !== 0) truncated$1("EUC-KR");
2317
2317
  return fromCodeUnits(units);
2318
2318
  }
2319
2319
  /**
@@ -2340,15 +2340,15 @@ function decodeBig5(bytes) {
2340
2340
  continue;
2341
2341
  }
2342
2342
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(BIG5, pointer);
2343
- if (codePoint === void 0) malformed("Big5", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2343
+ if (codePoint === void 0) malformed$1("Big5", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2344
2344
  appendCodePoint(units, codePoint);
2345
2345
  continue;
2346
2346
  }
2347
2347
  if (byte <= ASCII_BYTE_MAX) units.push(byte);
2348
2348
  else if (byte >= 129 && byte <= 254) leading = byte;
2349
- else malformed("Big5", `has no character for byte 0x${byte.toString(16)}`);
2349
+ else malformed$1("Big5", `has no character for byte 0x${byte.toString(16)}`);
2350
2350
  }
2351
- if (leading !== 0) truncated("Big5");
2351
+ if (leading !== 0) truncated$1("Big5");
2352
2352
  return fromCodeUnits(units);
2353
2353
  }
2354
2354
  /**
@@ -2388,7 +2388,7 @@ function decodeGb18030(bytes) {
2388
2388
  let third = 0;
2389
2389
  for (const byte of bytes) {
2390
2390
  if (third !== 0) {
2391
- if (byte < 48 || byte > 57) malformed("gb18030", `has no character for four-byte sequence 0x${first.toString(16)} 0x${second.toString(16)} 0x${third.toString(16)} 0x${byte.toString(16)}: fourth byte is not 0x30-0x39`);
2391
+ if (byte < 48 || byte > 57) malformed$1("gb18030", `has no character for four-byte sequence 0x${first.toString(16)} 0x${second.toString(16)} 0x${third.toString(16)} 0x${byte.toString(16)}: fourth byte is not 0x30-0x39`);
2392
2392
  const codePoint = gb18030RangesCodePoint((first - 129) * (1260 * GB18030_SECOND_FOURTH_BYTE_COUNT) + (second - 48) * 1260 + (third - 129) * GB18030_SECOND_FOURTH_BYTE_COUNT + (byte - 48));
2393
2393
  const failedFirst = first;
2394
2394
  const failedSecond = second;
@@ -2396,7 +2396,7 @@ function decodeGb18030(bytes) {
2396
2396
  first = 0;
2397
2397
  second = 0;
2398
2398
  third = 0;
2399
- if (codePoint === void 0) malformed("gb18030", `has no character for four-byte sequence 0x${failedFirst.toString(16)} 0x${failedSecond.toString(16)} 0x${failedThird.toString(16)} 0x${byte.toString(16)}`);
2399
+ if (codePoint === void 0) malformed$1("gb18030", `has no character for four-byte sequence 0x${failedFirst.toString(16)} 0x${failedSecond.toString(16)} 0x${failedThird.toString(16)} 0x${byte.toString(16)}`);
2400
2400
  appendCodePoint(units, codePoint);
2401
2401
  continue;
2402
2402
  }
@@ -2405,7 +2405,7 @@ function decodeGb18030(bytes) {
2405
2405
  third = byte;
2406
2406
  continue;
2407
2407
  }
2408
- malformed("gb18030", `has no character for byte sequence 0x${first.toString(16)} 0x${second.toString(16)} 0x${byte.toString(16)}: third byte is not 0x81-0xFE`);
2408
+ malformed$1("gb18030", `has no character for byte sequence 0x${first.toString(16)} 0x${second.toString(16)} 0x${byte.toString(16)}: third byte is not 0x81-0xFE`);
2409
2409
  }
2410
2410
  if (first !== 0) {
2411
2411
  if (byte >= 48 && byte <= 57) {
@@ -2417,16 +2417,133 @@ function decodeGb18030(bytes) {
2417
2417
  const column = twoByteTrailColumn(byte, 128, 254);
2418
2418
  const pointer = column === void 0 ? void 0 : (leadingByte - 129) * 190 + column;
2419
2419
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(GB18030, pointer);
2420
- if (codePoint === void 0) malformed("gb18030", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2420
+ if (codePoint === void 0) malformed$1("gb18030", `has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2421
2421
  appendCodePoint(units, codePoint);
2422
2422
  continue;
2423
2423
  }
2424
2424
  if (byte <= ASCII_BYTE_MAX) units.push(byte);
2425
2425
  else if (byte === 128) units.push(8364);
2426
2426
  else if (byte <= 254) first = byte;
2427
- else malformed("gb18030", `has no character for byte 0x${byte.toString(16)}`);
2427
+ else malformed$1("gb18030", `has no character for byte 0x${byte.toString(16)}`);
2428
2428
  }
2429
- if (first !== 0) truncated("gb18030");
2429
+ if (first !== 0) truncated$1("gb18030");
2430
+ return fromCodeUnits(units);
2431
+ }
2432
+ //#endregion
2433
+ //#region src/text/decode-iso-2022-jp.ts
2434
+ function malformed(detail) {
2435
+ throw new UndecodableTextError("malformed", `ISO-2022-JP ${detail}`);
2436
+ }
2437
+ function truncated() {
2438
+ malformed("ends with an incomplete multi-byte or escape sequence");
2439
+ }
2440
+ /** The byte value that begins every ISO-2022-JP escape sequence, wherever this decoder is. */
2441
+ const ESCAPE = 27;
2442
+ /** JIS X 0201 Katakana's own byte range (0x21-0x5F), mapped directly onto the same halfwidth katakana Unicode block decode-dbcs.ts's Shift_JIS and EUC-JP decoders both compute from their own byte ranges (0xFF61 plus the byte's own offset from the range's first value). */
2443
+ const KATAKANA_FIRST_BYTE = 33;
2444
+ const KATAKANA_LAST_BYTE = 95;
2445
+ /** JIS X 0208's own index byte range (0x21-0x7E) for both the leading and trailing byte of a two-byte pointer. */
2446
+ const JIS0208_INDEX_BYTE_MIN = 33;
2447
+ const JIS0208_INDEX_BYTE_MAX = 126;
2448
+ const JIS0208_INDEX_COLUMNS = 94;
2449
+ /**
2450
+ * Whether `byte` is one this decoder's ASCII and Roman states pass through unchanged: any byte below 0x80 except the two Shift-Out/Shift-In control codes (0x0E, 0x0F) a different ISO-2022 variant uses for a switching mechanism this decoder does not support. No check for {@link ESCAPE} here: both callers (the "ascii" and "roman" cases below) already intercept it with their own `byte === ESCAPE` branch before ever reaching this function, so `byte` here is never that value in the first place.
2451
+ */
2452
+ function isPlainByte(byte) {
2453
+ return byte <= 127 && byte !== 14 && byte !== 15;
2454
+ }
2455
+ /**
2456
+ * Decodes bytes as ISO-2022-JP (https://encoding.spec.whatwg.org/#iso-2022-jp-decoder).
2457
+ *
2458
+ * A stateful decoder carrying one of four designated character sets across the whole input — ASCII, JIS X 0201 Roman (ASCII with 0x5C and 0x7E replaced by ¥ and ‾), JIS X 0201 Katakana (0x21-0x5F as halfwidth katakana), or JIS X 0208 (a two-byte pointer into {@link JIS0208}) — switched by a three-byte escape sequence: `ESC ( B` for ASCII, `ESC ( J` for Roman, `ESC ( I` for Katakana, and either `ESC $ @` or `ESC $ B` for JIS X 0208 (the Encoding Standard treats both the 1978 and 1983 JIS C 6226 designators identically, decoding through the one JIS0208 index either way). Starts in ASCII.
2459
+ *
2460
+ * One designator escape is refused even though it is syntactically well-formed: two in a row with no character decoded between them. The Encoding Standard's own decoder tracks this with a boolean (`ISO-2022-JP output`, reset to false by every character emitted, by every error, and by entering a JIS X 0208 pair's leading-byte state, set true by every successful designator switch) and, when a second switch arrives before that flag is reset, treats it as an error step rather than a silent state change — confirmed directly against a real browser engine (Node's own `TextDecoder`, which is ICU-backed and implements this algorithm): two designator escapes back to back decode the second one as U+FFFD, while a designator escape that follows a real character, or a partially-read JIS X 0208 pair, does not. This decoder throws where that reference decoder would emit U+FFFD, matching this module's throw-on-malformed house style.
2461
+ * @param bytes - The bytes to decode.
2462
+ * @returns The decoded text.
2463
+ * @throws UndecodableTextError When the bytes contain a byte, byte pair, or escape sequence ISO-2022-JP leaves undefined, a redundant designator escape, or end with an incomplete multi-byte or escape sequence.
2464
+ */
2465
+ function decodeIso2022Jp(bytes) {
2466
+ const units = [];
2467
+ let state = "ascii";
2468
+ let pending = 0;
2469
+ let output = false;
2470
+ for (const byte of bytes) switch (state) {
2471
+ case "ascii":
2472
+ if (byte === ESCAPE) {
2473
+ state = "escapeStart";
2474
+ continue;
2475
+ }
2476
+ output = false;
2477
+ if (!isPlainByte(byte)) malformed(`has no character for byte 0x${byte.toString(16)} in ASCII mode`);
2478
+ units.push(byte);
2479
+ continue;
2480
+ case "roman":
2481
+ if (byte === ESCAPE) {
2482
+ state = "escapeStart";
2483
+ continue;
2484
+ }
2485
+ output = false;
2486
+ if (byte === 92) {
2487
+ units.push(165);
2488
+ continue;
2489
+ }
2490
+ if (byte === 126) {
2491
+ units.push(8254);
2492
+ continue;
2493
+ }
2494
+ if (!isPlainByte(byte)) malformed(`has no character for byte 0x${byte.toString(16)} in Roman mode`);
2495
+ units.push(byte);
2496
+ continue;
2497
+ case "katakana":
2498
+ if (byte === ESCAPE) {
2499
+ state = "escapeStart";
2500
+ continue;
2501
+ }
2502
+ output = false;
2503
+ if (byte < KATAKANA_FIRST_BYTE || byte > KATAKANA_LAST_BYTE) malformed(`has no character for byte 0x${byte.toString(16)} in Katakana mode`);
2504
+ units.push(65344 + byte);
2505
+ continue;
2506
+ case "leadingByte":
2507
+ if (byte === ESCAPE) {
2508
+ state = "escapeStart";
2509
+ continue;
2510
+ }
2511
+ output = false;
2512
+ if (byte < JIS0208_INDEX_BYTE_MIN || byte > JIS0208_INDEX_BYTE_MAX) malformed(`has no character for byte 0x${byte.toString(16)} as a JIS X 0208 lead byte`);
2513
+ pending = byte;
2514
+ state = "trailingByte";
2515
+ continue;
2516
+ case "trailingByte": {
2517
+ const leadingByte = pending;
2518
+ state = "leadingByte";
2519
+ if (byte === ESCAPE) malformed(`has an incomplete JIS X 0208 two-byte sequence: lead byte 0x${leadingByte.toString(16)} was interrupted by an escape sequence`);
2520
+ if (byte < JIS0208_INDEX_BYTE_MIN || byte > JIS0208_INDEX_BYTE_MAX) malformed(`has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2521
+ const pointer = (leadingByte - JIS0208_INDEX_BYTE_MIN) * JIS0208_INDEX_COLUMNS + byte - JIS0208_INDEX_BYTE_MIN;
2522
+ const codePoint = pointerCodePoint(JIS0208, pointer);
2523
+ if (codePoint === void 0) malformed(`has no character for lead byte 0x${leadingByte.toString(16)} trail byte 0x${byte.toString(16)}`);
2524
+ units.push(codePoint);
2525
+ continue;
2526
+ }
2527
+ case "escapeStart":
2528
+ if (byte === 36 || byte === 40) {
2529
+ pending = byte;
2530
+ state = "escape";
2531
+ continue;
2532
+ }
2533
+ malformed(`has no supported designator starting with escape byte 0x${byte.toString(16)}`);
2534
+ continue;
2535
+ case "escape": {
2536
+ const leadingEscapeByte = pending;
2537
+ pending = 0;
2538
+ const nextState = leadingEscapeByte === 40 && byte === 66 ? "ascii" : leadingEscapeByte === 40 && byte === 74 ? "roman" : leadingEscapeByte === 40 && byte === 73 ? "katakana" : leadingEscapeByte === 36 && (byte === 64 || byte === 66) ? "leadingByte" : void 0;
2539
+ if (nextState === void 0) malformed(`has an unsupported designator escape sequence 0x1b 0x${leadingEscapeByte.toString(16)} 0x${byte.toString(16)}`);
2540
+ state = nextState;
2541
+ if (output) malformed("has a redundant designator escape sequence with no characters decoded since the previous one");
2542
+ output = true;
2543
+ continue;
2544
+ }
2545
+ }
2546
+ if (state === "trailingByte" || state === "escapeStart" || state === "escape") truncated();
2430
2547
  return fromCodeUnits(units);
2431
2548
  }
2432
2549
  //#endregion
@@ -2713,7 +2830,8 @@ const DECODERS = {
2713
2830
  "euc-kr": decodeEucKr,
2714
2831
  gbk: decodeGb18030,
2715
2832
  gb18030: decodeGb18030,
2716
- big5: decodeBig5
2833
+ big5: decodeBig5,
2834
+ "iso-2022-jp": decodeIso2022Jp
2717
2835
  };
2718
2836
  /** The text-versus-binary rule itself, over character codes: a NUL settles it outright, and every other C0 control outside {@link TEXTUAL_CONTROL_CODES} is allowed only up to one per {@link CODES_PER_PERMITTED_CONTROL}. Reading codes through an accessor rather than taking an array lets the identical rule run over a Uint8Array's bytes and over a decoded string's code units without copying either into one. */
2719
2837
  function codesAreTextual(codeAt, length) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document-bytes",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "description": "Generic byte-level primitives (ByteWriter, ByteReader, CRC-32, deflate/inflate, base64) and PNG/JPEG image encoding/decoding with zero PDF knowledge — the shared utility package for the documents.js family.",
5
5
  "type": "module",
6
6
  "repository": {