document-bytes 1.6.3 → 1.8.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/README.md CHANGED
@@ -53,6 +53,35 @@ To run a single test file, pass its path to vitest directly, e.g. `pnpm exec vit
53
53
  | `image/png-decode` | `decodePng` (PNG bytes → raw pixels, handling every PNG colour type including indexed/palette via its own PLTE/tRNS lookup), `RawImage` |
54
54
  | `image/png-filter` | `filterScanlines`, `unfilterScanlines` (the five PNG scanline filters) |
55
55
  | `image/jpeg-info` | `readJpegInfo` (JPEG header reader: dimensions, components, progressive flag — no sample decoding) |
56
+ | `text/decode` | `decodeText`, `tryDecodeText`, `detectByteOrderMark`, `isProbablyText`, `UndecodableTextError`, `TEXT_DECODE_CHUNK_CODE_UNITS` (bytes → text with the character encoding worked out from the bytes; see [Text decoding](#text-decoding) below) |
57
+
58
+ ## Text decoding
59
+
60
+ `decodeText` turns bytes into text without being told which character encoding they hold. It exists because the plain-text formats in this family used to assume UTF-8 and refuse everything else, which turns away Excel's own "CSV (Comma delimited)" export (the Windows ANSI code page) and anything Notepad saved as "Unicode" or PowerShell 5.1 wrote with a redirection operator (UTF-16 with a byte order mark).
61
+
62
+ The encoding is settled in a fixed order, and the result says which step settled it:
63
+
64
+ | Step | `source` | `confidence` | What decides it |
65
+ | ---- | ---------- | -------------------------------- | ----------------------------------------------------------------------------- |
66
+ | 1 | `declared` | `certain` | The caller's own `encoding` option, which skips detection entirely |
67
+ | 2 | `bom` | `certain` | A byte order mark: UTF-8, UTF-16LE/BE, UTF-32LE/BE |
68
+ | 3 | `detected` | `low` | The NUL interleave a mark-less UTF-16 file holding Latin-script text produces |
69
+ | 4 | `utf8` | `certain` for ASCII, else `high` | The bytes satisfy UTF-8's own validity rules |
70
+ | 5 | `detected` | `low` | windows-1252, behind a check that the bytes read as Western text |
71
+
72
+ Anything guessed (`confidence: "low"`) also carries a `warnings` entry naming what was guessed and on what evidence, so a guess can be shown as a guess rather than presented as fact. windows-1252 has a character for all but five of its byte values, so trying it and seeing whether it fails proves nothing: the plausibility check, and reporting the result as a guess, are what stand in for a test that cannot exist.
73
+
74
+ Bytes that are not text at all are refused with an `UndecodableTextError` before any guess is made, since without that gate windows-1252 would read a PNG as a page of mojibake instead of refusing it. `isProbablyText` is that judgement on its own, for a caller whose real question is whether bytes are text: a NUL byte, or more than one C0 control byte other than tab, line feed, form feed and carriage return per 64 bytes, says binary.
75
+
76
+ `detectByteOrderMark` is step 2 above on its own, for a caller that has to settle the encoding before it can safely read anything past the mark — an XML document's own encoding declaration is the case that occurs: the declaration lives inside the document, in whichever encoding the document turns out to be, so a mark has to be ruled out, or read, before that declaration can even be located. It returns the same encoding and mark length `decodeText` itself would settle on from a mark, or `undefined` when the bytes carry none of the five it recognises.
77
+
78
+ Everything except UTF-8 is decoded by this package rather than through `TextDecoder`. `TextDecoder`'s legacy single-byte and UTF-16 support comes from the host's ICU build, which a Node binary compiled with small-icu does not carry, and the [Encoding Standard](https://encoding.spec.whatwg.org/#names-and-labels) has no UTF-32 at all, so delegating would make the same bytes decode differently, or not at all, depending on where the code runs. The `pnpm test:workers` suite decodes windows-1252, UTF-16 and UTF-32 inside a real `workerd` isolate for exactly that reason, and one unit test checks the whole windows-1252 table, all 256 bytes, against the platform's own decoder wherever the platform has one.
79
+
80
+ Three limits are worth knowing rather than discovering:
81
+
82
+ - UTF-16 holding CJK text has no NUL interleave to see, so without a byte order mark it is not detected. Pass `encoding` for it.
83
+ - A legacy CJK or Cyrillic code page is refused rather than guessed at, since nothing distinguishes those from one another without a statistical model this package deliberately does not carry.
84
+ - A truncated UTF-8 sequence is genuinely indistinguishable from windows-1252 text containing the same characters, so it decodes as the latter, with a `low` confidence and a warning.
56
85
 
57
86
  ## Conventions
58
87