From 642ddcbb10f00dffebf831a9e181fe59dc01098a Mon Sep 17 00:00:00 2001 From: 5 <93879550+tydavid5@users.noreply.github.com> Date: Thu, 19 Nov 2026 12:55:34 +0100 Subject: [PATCH 1/2] Preserve U+FEFF when Wtf8Decoder reads payload text Fixes #65458 Wtf8Decoder extends TextDecoder without ignoreBOM, so the underlying decoder drops U+FEFF at the start of every segment it decodes. A string or template value that starts with U+FEFF, or that has U+FEFF immediately after a lone surrogate, came back from the API without that character, while a U+FEFF in the middle of a segment survived. The constructor now defaults ignoreBOM to true, so every payload decoder built from this class keeps the character, and an explicit ignoreBOM: false still opts out. The WASM transport's explicit option is now redundant, so it just constructs the decoder with no arguments. This cannot change how a real file-level BOM is handled. The Go filesystem reader already strips a UTF-8 BOM from source text before the compiler sees the file, and the client decoder only ever sees payload strings from the server's string table, never raw file bytes. The new tests cover the reported cases plus a trailing U+FEFF and the explicit opt-out. Verification: the issue repro gave leading [78], only [], afterSurrogate [d800] and template [74, 61, 69, 6c] before the fix, and all five give the expected code points after. The new tests pass, and with the fix reverted they fail with actual [120], expected [65279, 120]. Fifteen edge cases were checked by hand, including U+FEFF after a lone surrogate, a trailing U+FEFF, streaming decode, and the explicit opt-out. --- packages/typescript/src/api/node/wtf8.ts | 6 +++++ packages/typescript/src/wasm/transport.ts | 2 +- packages/typescript/test/wtf8.test.ts | 28 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/typescript/src/api/node/wtf8.ts b/packages/typescript/src/api/node/wtf8.ts index f73ac298e35f6..7d8caf475a228 100644 --- a/packages/typescript/src/api/node/wtf8.ts +++ b/packages/typescript/src/api/node/wtf8.ts @@ -119,6 +119,12 @@ export function encodeWtf8(text: string): Uint8Array { } export class Wtf8Decoder extends TextDecoder { + constructor(label: string | undefined = "utf-8", options?: TextDecoderOptions) { + // U+FEFF inside a payload is data. TextDecoder otherwise drops it at the + // start of each decoded segment, including the segment after a lone surrogate. + super(label, { ignoreBOM: true, ...options }); + } + override decode(input?: DecodeInput, options?: DecodeOptions): string { if (input == null) { return super.decode(undefined, options); diff --git a/packages/typescript/src/wasm/transport.ts b/packages/typescript/src/wasm/transport.ts index 1882dde34e979..4235608baf0e9 100644 --- a/packages/typescript/src/wasm/transport.ts +++ b/packages/typescript/src/wasm/transport.ts @@ -84,7 +84,7 @@ type Frame = RequestFrame | CallFrame; const encoder = new TextEncoder(); const decoder = new TextDecoder(); -const fileContentDecoder = new Wtf8Decoder("utf-8", { ignoreBOM: true }); +const fileContentDecoder = new Wtf8Decoder(); /** * API transport backed by an in-process TypeScript WebAssembly reactor. diff --git a/packages/typescript/test/wtf8.test.ts b/packages/typescript/test/wtf8.test.ts index 94480e2fd1c1a..292d5794f7bf5 100644 --- a/packages/typescript/test/wtf8.test.ts +++ b/packages/typescript/test/wtf8.test.ts @@ -87,6 +87,34 @@ describe("Wtf8Decoder", { concurrency }, () => { [0xD83E, 0xDD80, 0xD7FF, 0xD800, 0xD801, 0xDC00, 0xD83E, 0xDD80], ); }); + + test("preserves U+FEFF at the start of a decoded segment", () => { + const decoder = new Wtf8Decoder(); + const cases: readonly [string, string, readonly number[]][] = [ + ["leading", "\uFEFFx", [0xFEFF, 0x78]], + ["only", "\uFEFF", [0xFEFF]], + ["after a lone surrogate", "\uD800\uFEFF", [0xD800, 0xFEFF]], + ["middle", "a\uFEFFb", [0x61, 0xFEFF, 0x62]], + ["trailing", "x\uFEFF", [0x78, 0xFEFF]], + ["template text", "\uFEFFtail", [0xFEFF, 0x74, 0x61, 0x69, 0x6C]], + ]; + + for (const [name, text, expected] of cases) { + assert.deepStrictEqual( + Array.from(decoder.decode(encodeWtf8(text)), c => c.codePointAt(0)), + expected, + name, + ); + } + }); + + test("explicit ignoreBOM: false still opts out", () => { + const decoder = new Wtf8Decoder("utf-8", { ignoreBOM: false }); + assert.deepStrictEqual( + Array.from(decoder.decode(encodeWtf8("\uFEFFx")), c => c.codePointAt(0)), + [0x78], + ); + }); }); describe("Msgpack strings", { concurrency }, () => { From 3fd8d1f33d61a5ee648cfc626422e2035bfae663 Mon Sep 17 00:00:00 2001 From: 5 <93879550+tydavid5@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:04:47 +0100 Subject: [PATCH 2/2] Derive Wtf8Decoder options from the TextDecoder constructor The constructor named the TextDecoderOptions global, which does not exist under this package's ES2022 and Node-only configuration, so type-checking the package failed with Cannot find name TextDecoderOptions. The options type is now taken from the constructor itself, which keeps the same accepted options and the explicit ignoreBOM false opt-out. Fixes the Copilot review on #65579. Verified by running the package type-check: it fails on the old signature with TS2552 and passes with this change. The wtf8 tests still pass, 9 of 9. --- packages/typescript/src/api/node/wtf8.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typescript/src/api/node/wtf8.ts b/packages/typescript/src/api/node/wtf8.ts index 7d8caf475a228..92ef2c24b9d30 100644 --- a/packages/typescript/src/api/node/wtf8.ts +++ b/packages/typescript/src/api/node/wtf8.ts @@ -119,7 +119,7 @@ export function encodeWtf8(text: string): Uint8Array { } export class Wtf8Decoder extends TextDecoder { - constructor(label: string | undefined = "utf-8", options?: TextDecoderOptions) { + constructor(label: string | undefined = "utf-8", options?: ConstructorParameters[1]) { // U+FEFF inside a payload is data. TextDecoder otherwise drops it at the // start of each decoded segment, including the segment after a lone surrogate. super(label, { ignoreBOM: true, ...options });