Repository navigation
Preserve U+FEFF when Wtf8Decoder reads payload text - #65579
5 (tydavid5) wants to merge 2 commits into
Conversation
Fixes microsoft#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.
There was a problem hiding this comment.
🟡 Changes recommended
The new signature references the unavailable global TextDecoderOptions, causing type-checking to fail under the package’s ES2022 and Node-only configuration.
1 open issue
What changed in this PR
Addresses #65458 by preserving U+FEFF in API payload strings, including literal and template values and text following lone surrogates.
Changes:
- Defaults
Wtf8Decoderto preserve U+FEFF while retaining explicitignoreBOM: falsebehavior. - Simplifies the WASM transport to use the new default.
- Adds regression tests for U+FEFF positions and explicit opt-out.
| File | Description |
|---|---|
packages/typescript/test/wtf8.test.ts |
Tests U+FEFF preservation and opt-out behavior. |
packages/typescript/src/wasm/transport.ts |
Removes the now-redundant decoder option. |
packages/typescript/src/api/node/wtf8.ts |
Adds constructor defaults that preserve payload U+FEFF. |
Review effort: Balanced
| } | ||
|
|
||
| export class Wtf8Decoder extends TextDecoder { | ||
| constructor(label: string | undefined = "utf-8", options?: TextDecoderOptions) { |
There was a problem hiding this comment.
Fixed. The constructor no longer names the TextDecoderOptions global. It now takes ConstructorParameters[1], which resolves under the package's ES2022 and Node-only configuration and keeps the same accepted options, including the explicit ignoreBOM: false opt-out. Verified by running the package type-check: it fails on the old signature with TS2552 Cannot find name TextDecoderOptions and passes with this change. The wtf8 tests still pass, 9 of 9.
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 microsoft#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.

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.