fix(encoding): (多AI) 重构 encoding.ts 以整合并改进检测机制#1426
Open
cyfung1031 wants to merge 6 commits intomainfrom
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
该 PR 主要围绕脚本内容的“读取 + 编码检测 + 解码”流程做统一与重构:将安装页原先独立的编码检测/解码逻辑整合到 src/pkg/utils/encoding.ts 的 readBlobContent,并显著调整编码检测优先级以更稳健地保护 UTF-8(尤其是 GitHub Release 场景中无 charset、非 ASCII 内容出现在后段的 userscript)。
Changes:
- 安装页改为统一调用
readBlobContent解码脚本内容,避免多套检测逻辑并存导致行为不一致。 - 重构
encoding.ts:增强 Content-Type charset 解析、UTF-32 解码实现,并将检测优先级调整为 header charset → BOM → UTF-16/32 启发式 → UTF-8 严格校验/近似保留 → chardet legacy 回退。 - 大幅扩充
encoding.test.ts,覆盖 UTF-8/16/32、GitHub Release 回归场景、多语言与大文件采样等边界。
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/pkg/utils/encoding.ts | 重构编码检测与解码流程(charset 解析、UTF-32、UTF-8 采样校验、legacy 回退策略)。 |
| src/pkg/utils/encoding.test.ts | 新增/扩充大量回归与边界测试,验证重构后的检测优先级与解码正确性。 |
| src/pages/install/App.tsx | 安装页脚本下载后统一改用 readBlobContent 解码,移除手写 detect/decode/fallback 流程。 |
Collaborator
Author
|
可以合并 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#1115 https://github.com/caigg188/LDStatusPro/releases/download/v3.5.2.4/LDStatusPro.user.js
#1425 https://github.com/dongdevcom/video-speed-controller/releases/download/v2.0.1/video-speed-controller.user.js
问题说明
修复 #1425。
原有编码处理逻辑中,安装页会自行调用
detectEncoding与bytesDecode来解码下载后的脚本内容,而encoding.ts中也已经存在readBlobContent这一套较完整的 Blob/File 读取与编码检测流程。两套逻辑并存,容易造成不同入口的解码行为不一致,也使后续维护和扩展编码检测规则变得困难。在 #1425 中,通过 GitHub Release URL 安装 userscript 时,响应内容可能没有明确提供
charset,例如只有application/octet-stream。当脚本前段主要是 ASCII,而越南语、Emoji 等 UTF-8 字符出现在较靠后位置时,旧检测方式容易在样本不足的情况下把实际 UTF-8 内容误判为其他编码,导致安装页显示乱码。修改内容
1. 整合安装页的脚本解码流程
将
src/pages/install/App.tsx中原本手动执行的:detectEncoding(chunksAll, contentType)bytesDecode(encode, chunksAll)改为统一调用
readBlobContent。这样安装页不再维护独立的编码检测与回退逻辑,而是复用
encoding.ts中统一的 Blob 内容读取机制,减少重复实现,并确保不同入口的解码行为一致。2. 重构
encoding.ts的检测优先级本次将编码检测流程整理为更明确的优先级:
Content-Type charset这样可以优先保护现代脚本中最常见的 UTF-8 内容,避免在没有 charset 的情况下过早依赖 chardet,降低 UTF-8 被误判为 legacy 编码的概率。
3. 改进
Content-Type charset解析增强
parseCharsetFromContentType的解析能力:charset = UTF-8这类带空格的写法charset参数4. 改进 UTF-32 解码实现
重构
decodeUTF32:DataView按端序读取 code pointbyteOffset的Uint8Array.subarrayString.fromCodePoint,避免大文件一次性展开过多参数同时
bytesDecode会先规范化 charset 大小写,使UTF-32LE、UTF-32BE等写法也能被正确处理。5. 改进大文件 UTF-8 检测策略
对于较大的内容,新增采样范围检测逻辑:
这可以覆盖 GitHub Release userscript 的常见情况:文件前段多为 metadata 或 ASCII 代码,真正的多语言文本或 Emoji 可能出现在文件后部。
6. 改进 legacy 编码回退逻辑
当 UTF-8 判断失败后,才进入 chardet-based legacy 检测,并改进以下行为:
windows-1252这能在保护 UTF-8 的同时,继续兼容 GBK、Big5、Shift_JIS 等 legacy 编码脚本。
7. 增加回归测试与边界测试
新增并扩充
encoding.test.ts,覆盖:Content-Type charset解析影响范围
本次主要影响
encoding.ts的编码检测与 Blob 内容读取逻辑,以及安装页读取远程 userscript 时的解码流程。预期改善:
风险说明
编码检测本身仍然依赖一定启发式判断,极端混合编码或伪文本二进制内容仍可能存在误判风险。本次通过更明确的检测优先级、UTF-8 严格校验、启发式结果验证和更完整的测试覆盖来降低风险。
测试
已增加并调整
src/pkg/utils/encoding.test.ts相关测试,覆盖本次重构涉及的主要检测路径、回归场景与边界情况。