冲突只有两处上下文搬迁,均在 src/index.ts: - 导入块:保留 dev 的 fs-batch / active-worktree 两行(含其注释),其后追加 PR 的 text-encoding 导入。 - fs.write:保留 dev 的写路由新形状(每请求唯一临时名 + flag:'wx'), 并把 PR 的 encodeText(content, encoding) 接进去(encoding 仍在重命名前 用 encodingOfFile(path) 从磁盘上的原文件探测)。 一并修掉证据实测的截断误判:读上限(readLimit / 64KiB 嗅探窗口)落在多字节 字符中间时,残留的部分字节会被 GBK 探测当成一个合法双字节字,导致 > 上限的 UTF-8 文件整段乱码、保存还可能按错编码落盘(Node v26 实测:三字节汉字缺 2 字节时 utf8=fatal 失败、gbk=成功)。现 decodeTextBytes 接受 truncated 标记, 在截断且尾部确为不完整 UTF-8 序列时按 UTF-8 处理(内容仍沿用原先的 U+FFFD 渲染);完整但不合法的尾部序列照旧交给传统编码探测。测试补齐:单测(三字节 / 四字节截断、完整非法序列不被误裁、>64KiB 嗅探边界)+ 路由级(GBK round-trip、 512KiB readLimit 截断的中文 UTF-8 读取)。 Refs #523
95 lines
3.8 KiB
TypeScript
95 lines
3.8 KiB
TypeScript
import { afterAll, describe, expect, it } from 'vitest'
|
|
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { tmpdir } from 'node:os'
|
|
import {
|
|
decodeTextBytes,
|
|
encodeText,
|
|
encodingOfFile,
|
|
type TextEncoding,
|
|
} from '../src/text-encoding.ts'
|
|
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-encoding-'))
|
|
|
|
const ROUND_TRIPS: [TextEncoding, string][] = [
|
|
['utf8', '中文 test'],
|
|
['utf8-bom', '中文 test'],
|
|
['utf16le-bom', '中文 test'],
|
|
['utf16be-bom', '中文 test'],
|
|
['utf32le-bom', '中文 😀'],
|
|
['utf32be-bom', '中文 😀'],
|
|
['gbk', '中文测试 €'],
|
|
['gb18030', '中文 😀 𠀀'],
|
|
]
|
|
|
|
afterAll(() => {
|
|
rmSync(root, { recursive: true, force: true })
|
|
})
|
|
|
|
describe('text encoding', () => {
|
|
it.each(ROUND_TRIPS)('round-trips %s', (encoding, text) => {
|
|
const decoded = decodeTextBytes(encodeText(text, encoding))
|
|
expect(decoded).toEqual({ content: text, encoding })
|
|
})
|
|
|
|
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')
|
|
expect(decodeTextBytes(little)).toEqual({ content: text, encoding: 'utf16le' })
|
|
expect(decodeTextBytes(little.subarray(0, little.length - 1))?.encoding).toBe('utf16le')
|
|
|
|
const big = Buffer.from(little)
|
|
big.swap16()
|
|
expect(decodeTextBytes(big)).toEqual({ content: text, encoding: 'utf16be' })
|
|
expect(decodeTextBytes(big.subarray(0, big.length - 1))?.encoding).toBe('utf16be')
|
|
})
|
|
|
|
it('keeps NUL-heavy non-Unicode data binary', () => {
|
|
expect(decodeTextBytes(Buffer.from([0x89, 0x50, 0x00, 0x01, 0x02, 0x03]))).toBeNull()
|
|
})
|
|
|
|
it('keeps a capped UTF-8 read UTF-8 when the cap cuts a character', () => {
|
|
// A read cap stops wherever it lands, and the partial tail of a CJK
|
|
// character is exactly what the GBK probe accepts: two of the three bytes
|
|
// decode as one legacy glyph, so a >cap UTF-8 file used to open as
|
|
// mojibake. The cap must not vote on the encoding.
|
|
const cjk = Buffer.from('中文测试内容', 'utf8')
|
|
const cutCjk = cjk.subarray(0, cjk.length - 2)
|
|
expect(decodeTextBytes(cutCjk, true)).toEqual({ content: cutCjk.toString('utf8'), encoding: 'utf8' })
|
|
// Without the truncation flag the same bytes fall through to the legacy
|
|
// probe — that fall-through is what the flag exists to suppress.
|
|
expect(decodeTextBytes(cutCjk)?.encoding).toBe('gbk')
|
|
|
|
const emoji = Buffer.from('中文😀', 'utf8')
|
|
const cutEmoji = emoji.subarray(0, emoji.length - 2)
|
|
expect(decodeTextBytes(cutEmoji, true)?.encoding).toBe('utf8')
|
|
|
|
// A complete (invalid) sequence at the tail is real evidence about the
|
|
// encoding, so the legacy probe still wins there: 0xC0 0x80 is an
|
|
// overlong UTF-8 pair the GBK decoder accepts.
|
|
expect(decodeTextBytes(Buffer.from([0x41, 0xc0, 0x80]), true)?.encoding).toBe('gbk')
|
|
})
|
|
|
|
it('sniffs a > window UTF-8 file whose window boundary cuts a character', async () => {
|
|
const path = join(root, 'big-utf8.txt')
|
|
// The 64 KiB sniff window ends inside a three-byte character; the save
|
|
// side must still recognize the file as UTF-8.
|
|
writeFileSync(path, Buffer.from(`${'a'.repeat(64 * 1024 - 2)}中文`, 'utf8'))
|
|
await expect(encodingOfFile(path)).resolves.toBe('utf8')
|
|
})
|
|
|
|
it('detects the on-disk encoding used for the next save', async () => {
|
|
const path = join(root, 'legacy.cmd')
|
|
writeFileSync(path, encodeText('@echo off\r\necho 中文\r\n', 'gbk'))
|
|
await expect(encodingOfFile(path)).resolves.toBe('gbk')
|
|
})
|
|
|
|
it('defaults new files to UTF-8', async () => {
|
|
await expect(encodingOfFile(join(root, 'missing.txt'))).resolves.toBe('utf8')
|
|
})
|
|
|
|
it('refuses silently lossy GBK writes', () => {
|
|
expect(() => encodeText('中文😀', 'gbk')).toThrow(/GBK/)
|
|
})
|
|
})
|