diff --git a/src/text-encoding.ts b/src/text-encoding.ts index b1269763..d9940048 100644 --- a/src/text-encoding.ts +++ b/src/text-encoding.ts @@ -406,7 +406,10 @@ function getGbkEncodeMap(): Map { if (gbkEncodeMap !== undefined) return gbkEncodeMap const map = new Map() for (let byte = 0; byte <= 0x7f; byte += 1) map.set(String.fromCharCode(byte), byte) - map.set('€', 0x80) + // GBK has a legacy single-byte euro at 0x80, but WHATWG/ICU (Node 24.21's + // ICU 78, and current Chromium) reject it as invalid under `fatal: true`. + // The canonical two-byte GBK spelling decodes everywhere. + map.set('€', 0xa2e3) for (let lead = 0x81; lead <= 0xfe; lead += 1) { for (let trail = 0x40; trail <= 0xfe; trail += 1) { if (trail === 0x7f) continue diff --git a/tests/text-encoding.spec.ts b/tests/text-encoding.spec.ts index f3dbcc1a..2f5e9bd5 100644 --- a/tests/text-encoding.spec.ts +++ b/tests/text-encoding.spec.ts @@ -35,6 +35,12 @@ describe('text encoding', () => { expect(decoded).toEqual({ content: text, encoding }) }) + it('encodes the euro sign with the two-byte GBK mapping', () => { + const bytes = encodeText('€', 'gbk') + expect(bytes.toString('hex')).toBe('a2e3') + expect(decodeTextBytes(bytes)).toEqual({ content: '€', encoding: 'gbk' }) + }) + it('detects BOM-less UTF-16 script text, including an odd truncated read', () => { const text = '@echo off\r\necho 中文测试\r\n' const little = Buffer.from(text, 'utf16le')