document-bytes 1.10.1 → 2.0.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
@@ -2224,7 +2224,7 @@ function twoByteTrailColumn(byte, highBandStart, highBandEnd) {
2224
2224
  * @throws UndecodableTextError When the bytes contain a byte or byte pair Shift_JIS leaves undefined, or end with an incomplete two-byte sequence.
2225
2225
  */
2226
2226
  function decodeShiftJis(bytes) {
2227
- const units = [];
2227
+ const sink = { units: [] };
2228
2228
  let leading = 0;
2229
2229
  for (const byte of bytes) {
2230
2230
  if (leading !== 0) {
@@ -2234,21 +2234,21 @@ function decodeShiftJis(bytes) {
2234
2234
  const column = twoByteTrailColumn(byte, 128, 252);
2235
2235
  const pointer = column === void 0 ? void 0 : (leadingByte - leadingOffset) * 188 + column;
2236
2236
  if (pointer !== void 0 && pointer >= 8836 && pointer <= 10715) {
2237
- appendCodePoint(units, 48508 + pointer);
2237
+ appendCodePoint(sink, 48508 + pointer);
2238
2238
  continue;
2239
2239
  }
2240
2240
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(JIS0208, pointer);
2241
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
- appendCodePoint(units, codePoint);
2242
+ appendCodePoint(sink, codePoint);
2243
2243
  continue;
2244
2244
  }
2245
- if (byte <= ASCII_BYTE_MAX || byte === 128) units.push(byte);
2246
- else if (byte >= 161 && byte <= 223) appendCodePoint(units, 65216 + byte);
2245
+ if (byte <= ASCII_BYTE_MAX || byte === 128) sink.units.push(byte);
2246
+ else if (byte >= 161 && byte <= 223) appendCodePoint(sink, 65216 + byte);
2247
2247
  else if (byte <= 159 || byte >= 224 && byte <= 252) leading = byte;
2248
2248
  else malformed$1("Shift_JIS", `has no character for byte 0x${byte.toString(16)}`);
2249
2249
  }
2250
2250
  if (leading !== 0) truncated$1("Shift_JIS");
2251
- return fromCodeUnits(units);
2251
+ return fromCodeUnits(sink.units);
2252
2252
  }
2253
2253
  /**
2254
2254
  * Decodes bytes as EUC-JP (https://encoding.spec.whatwg.org/#euc-jp-decoder).
@@ -2259,13 +2259,13 @@ function decodeShiftJis(bytes) {
2259
2259
  * @throws UndecodableTextError When the bytes contain a byte or byte pair EUC-JP leaves undefined, or end with an incomplete multi-byte sequence.
2260
2260
  */
2261
2261
  function decodeEucJp(bytes) {
2262
- const units = [];
2262
+ const sink = { units: [] };
2263
2263
  let leading = 0;
2264
2264
  let jis0212 = false;
2265
2265
  for (const byte of bytes) {
2266
2266
  if (leading === 142 && byte >= 161 && byte <= 223) {
2267
2267
  leading = 0;
2268
- appendCodePoint(units, 65216 + byte);
2268
+ appendCodePoint(sink, 65216 + byte);
2269
2269
  continue;
2270
2270
  }
2271
2271
  if (leading === 143 && byte >= 161 && byte <= 254) {
@@ -2280,15 +2280,15 @@ function decodeEucJp(bytes) {
2280
2280
  jis0212 = false;
2281
2281
  const codePoint = byte >= 161 && byte <= 254 ? pointerCodePoint(usedJis0212 ? JIS0212 : JIS0208, (leadingByte - 161) * 94 + byte - 161) : void 0;
2282
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
- appendCodePoint(units, codePoint);
2283
+ appendCodePoint(sink, codePoint);
2284
2284
  continue;
2285
2285
  }
2286
- if (byte <= ASCII_BYTE_MAX) units.push(byte);
2286
+ if (byte <= ASCII_BYTE_MAX) sink.units.push(byte);
2287
2287
  else if (byte === 142 || byte === 143 || byte >= 161 && byte <= 254) leading = byte;
2288
2288
  else malformed$1("EUC-JP", `has no character for byte 0x${byte.toString(16)}`);
2289
2289
  }
2290
2290
  if (leading !== 0) truncated$1("EUC-JP");
2291
- return fromCodeUnits(units);
2291
+ return fromCodeUnits(sink.units);
2292
2292
  }
2293
2293
  /**
2294
2294
  * Decodes bytes as EUC-KR (https://encoding.spec.whatwg.org/#euc-kr-decoder).
@@ -2299,7 +2299,7 @@ function decodeEucJp(bytes) {
2299
2299
  * @throws UndecodableTextError When the bytes contain a byte or byte pair EUC-KR leaves undefined, or end with an incomplete two-byte sequence.
2300
2300
  */
2301
2301
  function decodeEucKr(bytes) {
2302
- const units = [];
2302
+ const sink = { units: [] };
2303
2303
  let leading = 0;
2304
2304
  for (const byte of bytes) {
2305
2305
  if (leading !== 0) {
@@ -2307,15 +2307,15 @@ function decodeEucKr(bytes) {
2307
2307
  leading = 0;
2308
2308
  const codePoint = byte >= 65 && byte <= 254 ? pointerCodePoint(EUC_KR, (leadingByte - 129) * 190 + byte - 65) : void 0;
2309
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
- appendCodePoint(units, codePoint);
2310
+ appendCodePoint(sink, codePoint);
2311
2311
  continue;
2312
2312
  }
2313
- if (byte <= ASCII_BYTE_MAX) units.push(byte);
2313
+ if (byte <= ASCII_BYTE_MAX) sink.units.push(byte);
2314
2314
  else if (byte >= 129 && byte <= 254) leading = byte;
2315
2315
  else malformed$1("EUC-KR", `has no character for byte 0x${byte.toString(16)}`);
2316
2316
  }
2317
2317
  if (leading !== 0) truncated$1("EUC-KR");
2318
- return fromCodeUnits(units);
2318
+ return fromCodeUnits(sink.units);
2319
2319
  }
2320
2320
  /**
2321
2321
  * Decodes bytes as Big5 (https://encoding.spec.whatwg.org/#big5-decoder).
@@ -2326,7 +2326,7 @@ function decodeEucKr(bytes) {
2326
2326
  * @throws UndecodableTextError When the bytes contain a byte or byte pair Big5 leaves undefined, or end with an incomplete two-byte sequence.
2327
2327
  */
2328
2328
  function decodeBig5(bytes) {
2329
- const units = [];
2329
+ const sink = { units: [] };
2330
2330
  let leading = 0;
2331
2331
  for (const byte of bytes) {
2332
2332
  if (leading !== 0) {
@@ -2336,21 +2336,21 @@ function decodeBig5(bytes) {
2336
2336
  const pointer = column === void 0 ? void 0 : (leadingByte - 129) * 157 + column;
2337
2337
  const doubled = pointer === void 0 ? void 0 : BIG5_DOUBLE_CODE_POINT_POINTERS.get(pointer);
2338
2338
  if (doubled !== void 0) {
2339
- appendCodePoint(units, doubled[0]);
2340
- appendCodePoint(units, doubled[1]);
2339
+ appendCodePoint(sink, doubled[0]);
2340
+ appendCodePoint(sink, doubled[1]);
2341
2341
  continue;
2342
2342
  }
2343
2343
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(BIG5, pointer);
2344
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
- appendCodePoint(units, codePoint);
2345
+ appendCodePoint(sink, codePoint);
2346
2346
  continue;
2347
2347
  }
2348
- if (byte <= ASCII_BYTE_MAX) units.push(byte);
2348
+ if (byte <= ASCII_BYTE_MAX) sink.units.push(byte);
2349
2349
  else if (byte >= 129 && byte <= 254) leading = byte;
2350
2350
  else malformed$1("Big5", `has no character for byte 0x${byte.toString(16)}`);
2351
2351
  }
2352
2352
  if (leading !== 0) truncated$1("Big5");
2353
- return fromCodeUnits(units);
2353
+ return fromCodeUnits(sink.units);
2354
2354
  }
2355
2355
  /**
2356
2356
  * The number of distinct values gb18030's own third byte (0x81-0xFE) can take, and the number of distinct values its own second and fourth bytes (0x30-0x39) can take — the two factors the Encoding Standard's own gb18030 decoder algorithm multiplies together (as `10 × 126` and `10 × 126 × 10`) to place each of the four bytes in a four-byte pointer's own positional value. Named so the arithmetic in {@link decodeGb18030} reads as the byte-range sizes it actually is, rather than as two bare products of unexplained digits.
@@ -2383,7 +2383,7 @@ function gb18030RangesCodePoint(pointer) {
2383
2383
  * @throws UndecodableTextError When the bytes contain a byte or byte sequence gb18030 leaves undefined, or end with an incomplete multi-byte sequence.
2384
2384
  */
2385
2385
  function decodeGb18030(bytes) {
2386
- const units = [];
2386
+ const sink = { units: [] };
2387
2387
  let first = 0;
2388
2388
  let second = 0;
2389
2389
  let third = 0;
@@ -2398,7 +2398,7 @@ function decodeGb18030(bytes) {
2398
2398
  second = 0;
2399
2399
  third = 0;
2400
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
- appendCodePoint(units, codePoint);
2401
+ appendCodePoint(sink, codePoint);
2402
2402
  continue;
2403
2403
  }
2404
2404
  if (second !== 0) {
@@ -2419,16 +2419,16 @@ function decodeGb18030(bytes) {
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
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
- appendCodePoint(units, codePoint);
2422
+ appendCodePoint(sink, codePoint);
2423
2423
  continue;
2424
2424
  }
2425
- if (byte <= ASCII_BYTE_MAX) units.push(byte);
2426
- else if (byte === 128) units.push(8364);
2425
+ if (byte <= ASCII_BYTE_MAX) sink.units.push(byte);
2426
+ else if (byte === 128) sink.units.push(8364);
2427
2427
  else if (byte <= 254) first = byte;
2428
2428
  else malformed$1("gb18030", `has no character for byte 0x${byte.toString(16)}`);
2429
2429
  }
2430
2430
  if (first !== 0) truncated$1("gb18030");
2431
- return fromCodeUnits(units);
2431
+ return fromCodeUnits(sink.units);
2432
2432
  }
2433
2433
  //#endregion
2434
2434
  //#region src/text/decode-iso-2022-jp.ts
@@ -2636,18 +2636,18 @@ const LOW_SURROGATE_MASK = 1023;
2636
2636
  */
2637
2637
  const TEXT_DECODE_CHUNK_CODE_UNITS = 32768;
2638
2638
  /**
2639
- * Splits `codePoint` into one or two UTF-16 code units and pushes them onto `units`: the code point itself below the supplementary plane, a surrogate pair biased by {@link SURROGATE_BITS} and {@link LOW_SURROGATE_MASK} above it. Shared by every decoder here that can produce a code point outside the Basic Multilingual Plane — {@link decodeUtf32} for UTF-32's own scalar values, and decode-dbcs.ts's decoders for the WHATWG index tables' own supplementary-plane entries (Big5's CJK Compatibility Ideographs Supplement pointers, gb18030's final algorithmic range) — so the split happens in exactly one place rather than once per caller.
2640
- * @param units - The code unit array to push onto, in place.
2639
+ * Splits `codePoint` into one or two UTF-16 code units and pushes them onto `sink.units`: the code point itself below the supplementary plane, a surrogate pair biased by {@link SURROGATE_BITS} and {@link LOW_SURROGATE_MASK} above it. Shared by every decoder here that can produce a code point outside the Basic Multilingual Plane — {@link decodeUtf32} for UTF-32's own scalar values, and decode-dbcs.ts's decoders for the WHATWG index tables' own supplementary-plane entries (Big5's CJK Compatibility Ideographs Supplement pointers, gb18030's final algorithmic range) — so the split happens in exactly one place rather than once per caller.
2640
+ * @param sink - The code unit accumulator to push onto, in place.
2641
2641
  * @param codePoint - A Unicode scalar value in the range 0 to {@link MAX_CODE_POINT}.
2642
2642
  */
2643
- function appendCodePoint(units, codePoint) {
2643
+ function appendCodePoint(sink, codePoint) {
2644
2644
  if (codePoint < SUPPLEMENTARY_PLANE_START) {
2645
- units.push(codePoint);
2645
+ sink.units.push(codePoint);
2646
2646
  return;
2647
2647
  }
2648
2648
  const supplementary = codePoint - SUPPLEMENTARY_PLANE_START;
2649
- units.push(HIGH_SURROGATE_START + (supplementary >> SURROGATE_BITS));
2650
- units.push(LOW_SURROGATE_START + (supplementary & LOW_SURROGATE_MASK));
2649
+ sink.units.push(HIGH_SURROGATE_START + (supplementary >> SURROGATE_BITS));
2650
+ sink.units.push(LOW_SURROGATE_START + (supplementary & LOW_SURROGATE_MASK));
2651
2651
  }
2652
2652
  /**
2653
2653
  * Converts a sequence of UTF-16 code units to a string in fixed-size chunks (see {@link TEXT_DECODE_CHUNK_CODE_UNITS}), shared by every decoder in this module and in decode-dbcs.ts so each collects code units into a plain array and calls this once at the end rather than concatenating a string one character at a time.
@@ -2752,13 +2752,13 @@ function tryDecodeUtf16(bytes, littleEndian) {
2752
2752
  function decodeUtf32(bytes, littleEndian) {
2753
2753
  if (bytes.byteLength % 4 !== 0) throw new UndecodableTextError("malformed", "UTF-32 text must hold a whole number of 32-bit code units");
2754
2754
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2755
- const units = [];
2755
+ const sink = { units: [] };
2756
2756
  for (let offset = 0; offset < bytes.byteLength; offset += 4) {
2757
2757
  const codePoint = view.getUint32(offset, littleEndian);
2758
2758
  if (codePoint > MAX_CODE_POINT || codePoint >= HIGH_SURROGATE_START && codePoint <= LOW_SURROGATE_END) throw new UndecodableTextError("malformed", "UTF-32 text must hold only Unicode scalar values");
2759
- appendCodePoint(units, codePoint);
2759
+ appendCodePoint(sink, codePoint);
2760
2760
  }
2761
- return fromCodeUnits(units);
2761
+ return fromCodeUnits(sink.units);
2762
2762
  }
2763
2763
  function decodeWindows1252(bytes) {
2764
2764
  const units = [];
package/dist/index.d.cts CHANGED
@@ -132,11 +132,17 @@ declare class UndecodableTextError extends Error {
132
132
  */
133
133
  declare const TEXT_DECODE_CHUNK_CODE_UNITS: number;
134
134
  /**
135
- * Splits `codePoint` into one or two UTF-16 code units and pushes them onto `units`: the code point itself below the supplementary plane, a surrogate pair biased by {@link SURROGATE_BITS} and {@link LOW_SURROGATE_MASK} above it. Shared by every decoder here that can produce a code point outside the Basic Multilingual Plane — {@link decodeUtf32} for UTF-32's own scalar values, and decode-dbcs.ts's decoders for the WHATWG index tables' own supplementary-plane entries (Big5's CJK Compatibility Ideographs Supplement pointers, gb18030's final algorithmic range) — so the split happens in exactly one place rather than once per caller.
136
- * @param units - The code unit array to push onto, in place.
135
+ * A code unit accumulator threaded by reference into {@link appendCodePoint}, rather than the bare mutable array it wraps: the array itself is owned entirely by whichever decoder loop builds it, appended to hundreds or thousands of times per call, and this wrapper's own `units` property is not itself a primitive or callback, which is what keeps it out of scope for exadev/prefer-readonly-object-param (see this package's own eslint.config.ts, and PackageLintOptions.preferReadonlyParams in the workspace's shared eslint.shared.ts) the same way a bare `number[]` parameter is squarely inside exadev/prefer-readonly-array-param's.
136
+ */
137
+ interface CodeUnitSink {
138
+ readonly units: number[];
139
+ }
140
+ /**
141
+ * Splits `codePoint` into one or two UTF-16 code units and pushes them onto `sink.units`: the code point itself below the supplementary plane, a surrogate pair biased by {@link SURROGATE_BITS} and {@link LOW_SURROGATE_MASK} above it. Shared by every decoder here that can produce a code point outside the Basic Multilingual Plane — {@link decodeUtf32} for UTF-32's own scalar values, and decode-dbcs.ts's decoders for the WHATWG index tables' own supplementary-plane entries (Big5's CJK Compatibility Ideographs Supplement pointers, gb18030's final algorithmic range) — so the split happens in exactly one place rather than once per caller.
142
+ * @param sink - The code unit accumulator to push onto, in place.
137
143
  * @param codePoint - A Unicode scalar value in the range 0 to {@link MAX_CODE_POINT}.
138
144
  */
139
- declare function appendCodePoint(units: number[], codePoint: number): void;
145
+ declare function appendCodePoint(sink: CodeUnitSink, codePoint: number): void;
140
146
  /**
141
147
  * Converts a sequence of UTF-16 code units to a string in fixed-size chunks (see {@link TEXT_DECODE_CHUNK_CODE_UNITS}), shared by every decoder in this module and in decode-dbcs.ts so each collects code units into a plain array and calls this once at the end rather than concatenating a string one character at a time.
142
148
  * @param units - The code units to convert, in order.
@@ -238,4 +244,4 @@ interface JpegInfo {
238
244
  }
239
245
  declare function readJpegInfo(bytes: Uint8Array<ArrayBuffer>): JpegInfo;
240
246
  //#endregion
241
- export { BASE64_ENCODE_CHUNK_CHARS, ByteOrderMark, ByteReader, ByteWriter, DecodeTextOptions, DecodedText, DecodedTextConfidence, DecodedTextSource, DeflateLevel, InflateResult, JpegInfo, MAX_INFLATE_OUTPUT_BYTES, PNG_MAX_DIMENSION, PNG_MAX_PIXELS, PngDecodeOptions, PngEncodeOptions, PngFilterType, RawImage, TEXT_DECODE_CHUNK_CODE_UNITS, TextEncodingLabel, UndecodableTextError, UndecodableTextReason, appendCodePoint, base64ToBytes, bytesToBase64, concatBytes, crc32, decodePng, decodeText, deflate, detectByteOrderMark, encodePng, exceedsMaxPixelCount, filterScanlines, fromCodeUnits, inflate, inflateTolerant, isAsciiWhitespace, isProbablyText, readJpegInfo, resolveEncodingLabel, tryDecodeText, unfilterScanlines };
247
+ export { BASE64_ENCODE_CHUNK_CHARS, ByteOrderMark, ByteReader, ByteWriter, CodeUnitSink, DecodeTextOptions, DecodedText, DecodedTextConfidence, DecodedTextSource, DeflateLevel, InflateResult, JpegInfo, MAX_INFLATE_OUTPUT_BYTES, PNG_MAX_DIMENSION, PNG_MAX_PIXELS, PngDecodeOptions, PngEncodeOptions, PngFilterType, RawImage, TEXT_DECODE_CHUNK_CODE_UNITS, TextEncodingLabel, UndecodableTextError, UndecodableTextReason, appendCodePoint, base64ToBytes, bytesToBase64, concatBytes, crc32, decodePng, decodeText, deflate, detectByteOrderMark, encodePng, exceedsMaxPixelCount, filterScanlines, fromCodeUnits, inflate, inflateTolerant, isAsciiWhitespace, isProbablyText, readJpegInfo, resolveEncodingLabel, tryDecodeText, unfilterScanlines };
package/dist/index.d.ts CHANGED
@@ -132,11 +132,17 @@ declare class UndecodableTextError extends Error {
132
132
  */
133
133
  declare const TEXT_DECODE_CHUNK_CODE_UNITS: number;
134
134
  /**
135
- * Splits `codePoint` into one or two UTF-16 code units and pushes them onto `units`: the code point itself below the supplementary plane, a surrogate pair biased by {@link SURROGATE_BITS} and {@link LOW_SURROGATE_MASK} above it. Shared by every decoder here that can produce a code point outside the Basic Multilingual Plane — {@link decodeUtf32} for UTF-32's own scalar values, and decode-dbcs.ts's decoders for the WHATWG index tables' own supplementary-plane entries (Big5's CJK Compatibility Ideographs Supplement pointers, gb18030's final algorithmic range) — so the split happens in exactly one place rather than once per caller.
136
- * @param units - The code unit array to push onto, in place.
135
+ * A code unit accumulator threaded by reference into {@link appendCodePoint}, rather than the bare mutable array it wraps: the array itself is owned entirely by whichever decoder loop builds it, appended to hundreds or thousands of times per call, and this wrapper's own `units` property is not itself a primitive or callback, which is what keeps it out of scope for exadev/prefer-readonly-object-param (see this package's own eslint.config.ts, and PackageLintOptions.preferReadonlyParams in the workspace's shared eslint.shared.ts) the same way a bare `number[]` parameter is squarely inside exadev/prefer-readonly-array-param's.
136
+ */
137
+ interface CodeUnitSink {
138
+ readonly units: number[];
139
+ }
140
+ /**
141
+ * Splits `codePoint` into one or two UTF-16 code units and pushes them onto `sink.units`: the code point itself below the supplementary plane, a surrogate pair biased by {@link SURROGATE_BITS} and {@link LOW_SURROGATE_MASK} above it. Shared by every decoder here that can produce a code point outside the Basic Multilingual Plane — {@link decodeUtf32} for UTF-32's own scalar values, and decode-dbcs.ts's decoders for the WHATWG index tables' own supplementary-plane entries (Big5's CJK Compatibility Ideographs Supplement pointers, gb18030's final algorithmic range) — so the split happens in exactly one place rather than once per caller.
142
+ * @param sink - The code unit accumulator to push onto, in place.
137
143
  * @param codePoint - A Unicode scalar value in the range 0 to {@link MAX_CODE_POINT}.
138
144
  */
139
- declare function appendCodePoint(units: number[], codePoint: number): void;
145
+ declare function appendCodePoint(sink: CodeUnitSink, codePoint: number): void;
140
146
  /**
141
147
  * Converts a sequence of UTF-16 code units to a string in fixed-size chunks (see {@link TEXT_DECODE_CHUNK_CODE_UNITS}), shared by every decoder in this module and in decode-dbcs.ts so each collects code units into a plain array and calls this once at the end rather than concatenating a string one character at a time.
142
148
  * @param units - The code units to convert, in order.
@@ -238,4 +244,4 @@ interface JpegInfo {
238
244
  }
239
245
  declare function readJpegInfo(bytes: Uint8Array<ArrayBuffer>): JpegInfo;
240
246
  //#endregion
241
- export { BASE64_ENCODE_CHUNK_CHARS, ByteOrderMark, ByteReader, ByteWriter, DecodeTextOptions, DecodedText, DecodedTextConfidence, DecodedTextSource, DeflateLevel, InflateResult, JpegInfo, MAX_INFLATE_OUTPUT_BYTES, PNG_MAX_DIMENSION, PNG_MAX_PIXELS, PngDecodeOptions, PngEncodeOptions, PngFilterType, RawImage, TEXT_DECODE_CHUNK_CODE_UNITS, TextEncodingLabel, UndecodableTextError, UndecodableTextReason, appendCodePoint, base64ToBytes, bytesToBase64, concatBytes, crc32, decodePng, decodeText, deflate, detectByteOrderMark, encodePng, exceedsMaxPixelCount, filterScanlines, fromCodeUnits, inflate, inflateTolerant, isAsciiWhitespace, isProbablyText, readJpegInfo, resolveEncodingLabel, tryDecodeText, unfilterScanlines };
247
+ export { BASE64_ENCODE_CHUNK_CHARS, ByteOrderMark, ByteReader, ByteWriter, CodeUnitSink, DecodeTextOptions, DecodedText, DecodedTextConfidence, DecodedTextSource, DeflateLevel, InflateResult, JpegInfo, MAX_INFLATE_OUTPUT_BYTES, PNG_MAX_DIMENSION, PNG_MAX_PIXELS, PngDecodeOptions, PngEncodeOptions, PngFilterType, RawImage, TEXT_DECODE_CHUNK_CODE_UNITS, TextEncodingLabel, UndecodableTextError, UndecodableTextReason, appendCodePoint, base64ToBytes, bytesToBase64, concatBytes, crc32, decodePng, decodeText, deflate, detectByteOrderMark, encodePng, exceedsMaxPixelCount, filterScanlines, fromCodeUnits, inflate, inflateTolerant, isAsciiWhitespace, isProbablyText, readJpegInfo, resolveEncodingLabel, tryDecodeText, unfilterScanlines };
package/dist/index.js CHANGED
@@ -2223,7 +2223,7 @@ function twoByteTrailColumn(byte, highBandStart, highBandEnd) {
2223
2223
  * @throws UndecodableTextError When the bytes contain a byte or byte pair Shift_JIS leaves undefined, or end with an incomplete two-byte sequence.
2224
2224
  */
2225
2225
  function decodeShiftJis(bytes) {
2226
- const units = [];
2226
+ const sink = { units: [] };
2227
2227
  let leading = 0;
2228
2228
  for (const byte of bytes) {
2229
2229
  if (leading !== 0) {
@@ -2233,21 +2233,21 @@ function decodeShiftJis(bytes) {
2233
2233
  const column = twoByteTrailColumn(byte, 128, 252);
2234
2234
  const pointer = column === void 0 ? void 0 : (leadingByte - leadingOffset) * 188 + column;
2235
2235
  if (pointer !== void 0 && pointer >= 8836 && pointer <= 10715) {
2236
- appendCodePoint(units, 48508 + pointer);
2236
+ appendCodePoint(sink, 48508 + pointer);
2237
2237
  continue;
2238
2238
  }
2239
2239
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(JIS0208, pointer);
2240
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
- appendCodePoint(units, codePoint);
2241
+ appendCodePoint(sink, codePoint);
2242
2242
  continue;
2243
2243
  }
2244
- if (byte <= ASCII_BYTE_MAX || byte === 128) units.push(byte);
2245
- else if (byte >= 161 && byte <= 223) appendCodePoint(units, 65216 + byte);
2244
+ if (byte <= ASCII_BYTE_MAX || byte === 128) sink.units.push(byte);
2245
+ else if (byte >= 161 && byte <= 223) appendCodePoint(sink, 65216 + byte);
2246
2246
  else if (byte <= 159 || byte >= 224 && byte <= 252) leading = byte;
2247
2247
  else malformed$1("Shift_JIS", `has no character for byte 0x${byte.toString(16)}`);
2248
2248
  }
2249
2249
  if (leading !== 0) truncated$1("Shift_JIS");
2250
- return fromCodeUnits(units);
2250
+ return fromCodeUnits(sink.units);
2251
2251
  }
2252
2252
  /**
2253
2253
  * Decodes bytes as EUC-JP (https://encoding.spec.whatwg.org/#euc-jp-decoder).
@@ -2258,13 +2258,13 @@ function decodeShiftJis(bytes) {
2258
2258
  * @throws UndecodableTextError When the bytes contain a byte or byte pair EUC-JP leaves undefined, or end with an incomplete multi-byte sequence.
2259
2259
  */
2260
2260
  function decodeEucJp(bytes) {
2261
- const units = [];
2261
+ const sink = { units: [] };
2262
2262
  let leading = 0;
2263
2263
  let jis0212 = false;
2264
2264
  for (const byte of bytes) {
2265
2265
  if (leading === 142 && byte >= 161 && byte <= 223) {
2266
2266
  leading = 0;
2267
- appendCodePoint(units, 65216 + byte);
2267
+ appendCodePoint(sink, 65216 + byte);
2268
2268
  continue;
2269
2269
  }
2270
2270
  if (leading === 143 && byte >= 161 && byte <= 254) {
@@ -2279,15 +2279,15 @@ function decodeEucJp(bytes) {
2279
2279
  jis0212 = false;
2280
2280
  const codePoint = byte >= 161 && byte <= 254 ? pointerCodePoint(usedJis0212 ? JIS0212 : JIS0208, (leadingByte - 161) * 94 + byte - 161) : void 0;
2281
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
- appendCodePoint(units, codePoint);
2282
+ appendCodePoint(sink, codePoint);
2283
2283
  continue;
2284
2284
  }
2285
- if (byte <= ASCII_BYTE_MAX) units.push(byte);
2285
+ if (byte <= ASCII_BYTE_MAX) sink.units.push(byte);
2286
2286
  else if (byte === 142 || byte === 143 || byte >= 161 && byte <= 254) leading = byte;
2287
2287
  else malformed$1("EUC-JP", `has no character for byte 0x${byte.toString(16)}`);
2288
2288
  }
2289
2289
  if (leading !== 0) truncated$1("EUC-JP");
2290
- return fromCodeUnits(units);
2290
+ return fromCodeUnits(sink.units);
2291
2291
  }
2292
2292
  /**
2293
2293
  * Decodes bytes as EUC-KR (https://encoding.spec.whatwg.org/#euc-kr-decoder).
@@ -2298,7 +2298,7 @@ function decodeEucJp(bytes) {
2298
2298
  * @throws UndecodableTextError When the bytes contain a byte or byte pair EUC-KR leaves undefined, or end with an incomplete two-byte sequence.
2299
2299
  */
2300
2300
  function decodeEucKr(bytes) {
2301
- const units = [];
2301
+ const sink = { units: [] };
2302
2302
  let leading = 0;
2303
2303
  for (const byte of bytes) {
2304
2304
  if (leading !== 0) {
@@ -2306,15 +2306,15 @@ function decodeEucKr(bytes) {
2306
2306
  leading = 0;
2307
2307
  const codePoint = byte >= 65 && byte <= 254 ? pointerCodePoint(EUC_KR, (leadingByte - 129) * 190 + byte - 65) : void 0;
2308
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
- appendCodePoint(units, codePoint);
2309
+ appendCodePoint(sink, codePoint);
2310
2310
  continue;
2311
2311
  }
2312
- if (byte <= ASCII_BYTE_MAX) units.push(byte);
2312
+ if (byte <= ASCII_BYTE_MAX) sink.units.push(byte);
2313
2313
  else if (byte >= 129 && byte <= 254) leading = byte;
2314
2314
  else malformed$1("EUC-KR", `has no character for byte 0x${byte.toString(16)}`);
2315
2315
  }
2316
2316
  if (leading !== 0) truncated$1("EUC-KR");
2317
- return fromCodeUnits(units);
2317
+ return fromCodeUnits(sink.units);
2318
2318
  }
2319
2319
  /**
2320
2320
  * Decodes bytes as Big5 (https://encoding.spec.whatwg.org/#big5-decoder).
@@ -2325,7 +2325,7 @@ function decodeEucKr(bytes) {
2325
2325
  * @throws UndecodableTextError When the bytes contain a byte or byte pair Big5 leaves undefined, or end with an incomplete two-byte sequence.
2326
2326
  */
2327
2327
  function decodeBig5(bytes) {
2328
- const units = [];
2328
+ const sink = { units: [] };
2329
2329
  let leading = 0;
2330
2330
  for (const byte of bytes) {
2331
2331
  if (leading !== 0) {
@@ -2335,21 +2335,21 @@ function decodeBig5(bytes) {
2335
2335
  const pointer = column === void 0 ? void 0 : (leadingByte - 129) * 157 + column;
2336
2336
  const doubled = pointer === void 0 ? void 0 : BIG5_DOUBLE_CODE_POINT_POINTERS.get(pointer);
2337
2337
  if (doubled !== void 0) {
2338
- appendCodePoint(units, doubled[0]);
2339
- appendCodePoint(units, doubled[1]);
2338
+ appendCodePoint(sink, doubled[0]);
2339
+ appendCodePoint(sink, doubled[1]);
2340
2340
  continue;
2341
2341
  }
2342
2342
  const codePoint = pointer === void 0 ? void 0 : pointerCodePoint(BIG5, pointer);
2343
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
- appendCodePoint(units, codePoint);
2344
+ appendCodePoint(sink, codePoint);
2345
2345
  continue;
2346
2346
  }
2347
- if (byte <= ASCII_BYTE_MAX) units.push(byte);
2347
+ if (byte <= ASCII_BYTE_MAX) sink.units.push(byte);
2348
2348
  else if (byte >= 129 && byte <= 254) leading = byte;
2349
2349
  else malformed$1("Big5", `has no character for byte 0x${byte.toString(16)}`);
2350
2350
  }
2351
2351
  if (leading !== 0) truncated$1("Big5");
2352
- return fromCodeUnits(units);
2352
+ return fromCodeUnits(sink.units);
2353
2353
  }
2354
2354
  /**
2355
2355
  * The number of distinct values gb18030's own third byte (0x81-0xFE) can take, and the number of distinct values its own second and fourth bytes (0x30-0x39) can take — the two factors the Encoding Standard's own gb18030 decoder algorithm multiplies together (as `10 × 126` and `10 × 126 × 10`) to place each of the four bytes in a four-byte pointer's own positional value. Named so the arithmetic in {@link decodeGb18030} reads as the byte-range sizes it actually is, rather than as two bare products of unexplained digits.
@@ -2382,7 +2382,7 @@ function gb18030RangesCodePoint(pointer) {
2382
2382
  * @throws UndecodableTextError When the bytes contain a byte or byte sequence gb18030 leaves undefined, or end with an incomplete multi-byte sequence.
2383
2383
  */
2384
2384
  function decodeGb18030(bytes) {
2385
- const units = [];
2385
+ const sink = { units: [] };
2386
2386
  let first = 0;
2387
2387
  let second = 0;
2388
2388
  let third = 0;
@@ -2397,7 +2397,7 @@ function decodeGb18030(bytes) {
2397
2397
  second = 0;
2398
2398
  third = 0;
2399
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
- appendCodePoint(units, codePoint);
2400
+ appendCodePoint(sink, codePoint);
2401
2401
  continue;
2402
2402
  }
2403
2403
  if (second !== 0) {
@@ -2418,16 +2418,16 @@ function decodeGb18030(bytes) {
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
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
- appendCodePoint(units, codePoint);
2421
+ appendCodePoint(sink, codePoint);
2422
2422
  continue;
2423
2423
  }
2424
- if (byte <= ASCII_BYTE_MAX) units.push(byte);
2425
- else if (byte === 128) units.push(8364);
2424
+ if (byte <= ASCII_BYTE_MAX) sink.units.push(byte);
2425
+ else if (byte === 128) sink.units.push(8364);
2426
2426
  else if (byte <= 254) first = byte;
2427
2427
  else malformed$1("gb18030", `has no character for byte 0x${byte.toString(16)}`);
2428
2428
  }
2429
2429
  if (first !== 0) truncated$1("gb18030");
2430
- return fromCodeUnits(units);
2430
+ return fromCodeUnits(sink.units);
2431
2431
  }
2432
2432
  //#endregion
2433
2433
  //#region src/text/decode-iso-2022-jp.ts
@@ -2635,18 +2635,18 @@ const LOW_SURROGATE_MASK = 1023;
2635
2635
  */
2636
2636
  const TEXT_DECODE_CHUNK_CODE_UNITS = 32768;
2637
2637
  /**
2638
- * Splits `codePoint` into one or two UTF-16 code units and pushes them onto `units`: the code point itself below the supplementary plane, a surrogate pair biased by {@link SURROGATE_BITS} and {@link LOW_SURROGATE_MASK} above it. Shared by every decoder here that can produce a code point outside the Basic Multilingual Plane — {@link decodeUtf32} for UTF-32's own scalar values, and decode-dbcs.ts's decoders for the WHATWG index tables' own supplementary-plane entries (Big5's CJK Compatibility Ideographs Supplement pointers, gb18030's final algorithmic range) — so the split happens in exactly one place rather than once per caller.
2639
- * @param units - The code unit array to push onto, in place.
2638
+ * Splits `codePoint` into one or two UTF-16 code units and pushes them onto `sink.units`: the code point itself below the supplementary plane, a surrogate pair biased by {@link SURROGATE_BITS} and {@link LOW_SURROGATE_MASK} above it. Shared by every decoder here that can produce a code point outside the Basic Multilingual Plane — {@link decodeUtf32} for UTF-32's own scalar values, and decode-dbcs.ts's decoders for the WHATWG index tables' own supplementary-plane entries (Big5's CJK Compatibility Ideographs Supplement pointers, gb18030's final algorithmic range) — so the split happens in exactly one place rather than once per caller.
2639
+ * @param sink - The code unit accumulator to push onto, in place.
2640
2640
  * @param codePoint - A Unicode scalar value in the range 0 to {@link MAX_CODE_POINT}.
2641
2641
  */
2642
- function appendCodePoint(units, codePoint) {
2642
+ function appendCodePoint(sink, codePoint) {
2643
2643
  if (codePoint < SUPPLEMENTARY_PLANE_START) {
2644
- units.push(codePoint);
2644
+ sink.units.push(codePoint);
2645
2645
  return;
2646
2646
  }
2647
2647
  const supplementary = codePoint - SUPPLEMENTARY_PLANE_START;
2648
- units.push(HIGH_SURROGATE_START + (supplementary >> SURROGATE_BITS));
2649
- units.push(LOW_SURROGATE_START + (supplementary & LOW_SURROGATE_MASK));
2648
+ sink.units.push(HIGH_SURROGATE_START + (supplementary >> SURROGATE_BITS));
2649
+ sink.units.push(LOW_SURROGATE_START + (supplementary & LOW_SURROGATE_MASK));
2650
2650
  }
2651
2651
  /**
2652
2652
  * Converts a sequence of UTF-16 code units to a string in fixed-size chunks (see {@link TEXT_DECODE_CHUNK_CODE_UNITS}), shared by every decoder in this module and in decode-dbcs.ts so each collects code units into a plain array and calls this once at the end rather than concatenating a string one character at a time.
@@ -2751,13 +2751,13 @@ function tryDecodeUtf16(bytes, littleEndian) {
2751
2751
  function decodeUtf32(bytes, littleEndian) {
2752
2752
  if (bytes.byteLength % 4 !== 0) throw new UndecodableTextError("malformed", "UTF-32 text must hold a whole number of 32-bit code units");
2753
2753
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2754
- const units = [];
2754
+ const sink = { units: [] };
2755
2755
  for (let offset = 0; offset < bytes.byteLength; offset += 4) {
2756
2756
  const codePoint = view.getUint32(offset, littleEndian);
2757
2757
  if (codePoint > MAX_CODE_POINT || codePoint >= HIGH_SURROGATE_START && codePoint <= LOW_SURROGATE_END) throw new UndecodableTextError("malformed", "UTF-32 text must hold only Unicode scalar values");
2758
- appendCodePoint(units, codePoint);
2758
+ appendCodePoint(sink, codePoint);
2759
2759
  }
2760
- return fromCodeUnits(units);
2760
+ return fromCodeUnits(sink.units);
2761
2761
  }
2762
2762
  function decodeWindows1252(bytes) {
2763
2763
  const units = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document-bytes",
3
- "version": "1.10.1",
3
+ "version": "2.0.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": {