Files
rea/src/domain/strictUtf8.ts
morluto cabd56ea71 refactor(domain): give strict UTF-8 decoding one owner with an explicit BOM policy (#10728)
* refactor(domain): give strict UTF-8 decoding one owner with an explicit BOM policy

* fix(domain): keep the leading-BOM behaviour every migrated UTF-8 decoder had

* refactor(domain): read the domain layer's strict UTF-8 through the BOM-policy owner (#10803)

Twelve domain decoders built their own fatal TextDecoder with ignoreBOM: true. They now use strictUtf8Decoder("preserve"), which keeps that behaviour: each reads bytes where a leading U+FEFF is content (binary-format names and fields, JSON and XML read byte-for-byte, recorded bodies, registry seeds whose kept bytes are re-emitted). The counted baseline drops from 74 to 62.

* refactor(artifacts): read artifact readers' strict UTF-8 through the BOM-policy owner (#10871)

Fourteen artifact reader modules (17 sites) built their own fatal TextDecoder. They now take it from strictUtf8Decoder with the policy each had: every one kept a leading BOM (ignoreBOM: true), so they use "preserve"; JavaScriptArtifactFiles keeps it for JavaScript and HTML entries and strips it otherwise, as before. The strict-utf8-decoder baseline drops those 17 sites.

* refactor(domain): drop TextDecoder imports left by the strict UTF-8 migration

* refactor(application): read MCP registration metadata through the strict UTF-8 owner

* refactor(formats): read binary-format readers' strict UTF-8 through the BOM-policy owner (#10903)

* refactor(formats): read binary-format readers' strict UTF-8 through the BOM-policy owner

* refactor(formats): drop the model readers' unused TextDecoder imports

* refactor(browser): read browser capture and fetch strict UTF-8 through the BOM-policy owner (#10935)

* refactor(browser): read browser capture and fetch strict UTF-8 through the BOM-policy owner

* refactor(native): read LLDB and Mach-O name strict UTF-8 through the BOM-policy owner (#10936)

* refactor(native): read LLDB and Mach-O name strict UTF-8 through the BOM-policy owner

* refactor(process): read MCP and NDJSON transport frames' strict UTF-8 through the BOM-policy owner (#10937)

* refactor(process): read MCP and NDJSON transport frames' strict UTF-8 through the BOM-policy owner

* refactor(application): read JSON and file inputs' strict UTF-8 through the BOM-policy owner (#10938)

* refactor(application): read JSON and file inputs' strict UTF-8 through the BOM-policy owner

* refactor(providers): read IDA, Electron, Ghidra and reference strict UTF-8 through the BOM-policy owner (#10939)

* refactor(providers): read IDA, Electron, Ghidra and reference strict UTF-8 through the BOM-policy owner

* refactor(evm): read the hex bytecode carrier's strict UTF-8 through the owner and refuse new local decoders (#10940)

* refactor(evm): read the hex bytecode carrier's strict UTF-8 through the BOM-policy owner

* chore(architecture): match local strict UTF-8 decoders in any option order
2026-11-15 19:54:39 +08:00

36 lines
1.5 KiB
TypeScript

import { isUtf8 } from "node:buffer";
/**
* What a strict UTF-8 decode does with one leading byte order mark.
*
* - `strip-signature` drops it as an encoding signature. This is the WHATWG
* decoder default, so it is what a plain `{ fatal: true }` decoder did.
* - `preserve` keeps it as U+FEFF, so the text matches the caller's bytes and
* offsets (`ignoreBOM: true`). Use it where a leading U+FEFF is content.
*
* Changing a site's policy changes what its consumers see (for example an
* exact-value secret match or a JSON parser that rejects U+FEFF), so a change
* needs a regression test at that consumer.
*/
export type Utf8BomPolicy = "preserve" | "strip-signature";
/**
* A fatal UTF-8 decoder with an explicit BOM policy. It throws a TypeError on
* malformed bytes. Callers that pass `{ stream: true }` need their own
* instance, so this returns a new decoder each call.
*/
export const strictUtf8Decoder = (bom: Utf8BomPolicy): TextDecoder =>
new TextDecoder("utf-8", { fatal: true, ignoreBOM: bom === "preserve" });
const preserving = strictUtf8Decoder("preserve");
const stripping = strictUtf8Decoder("strip-signature");
/** Decode complete UTF-8 bytes; malformed input throws a TypeError. */
export const decodeStrictUtf8 = (
bytes: Uint8Array,
bom: Utf8BomPolicy,
): string => (bom === "preserve" ? preserving : stripping).decode(bytes);
/** Whether the bytes are well-formed UTF-8, without building the string. */
export const isStrictUtf8 = (bytes: Uint8Array): boolean => isUtf8(bytes);