pi-charset 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/CHANGELOG.zh-CN.md +9 -0
- package/docs/INSTALL.md +2 -2
- package/docs/zh-CN/INSTALL.md +2 -2
- package/package.json +1 -1
- package/src/encoding/classify.ts +31 -3
- package/src/resolve.ts +34 -1
- package/src/version.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
|
7
7
|
|
|
8
8
|
---
|
|
9
9
|
|
|
10
|
+
## [0.1.4] - 2026-10-28
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- Validate the body of UTF-8 BOM files with strict UTF-8 checking; a BOM followed by invalid or truncated sequences now classifies as UNKNOWN and is rejected for writing instead of being treated as valid UTF-8.
|
|
15
|
+
- Validate UTF-16 BOM bodies: odd byte lengths and unpaired surrogates classify as UNKNOWN instead of being loss-decoded on read and written back with dropped or replaced units.
|
|
16
|
+
- `force: true` no longer bypasses write rejection for files whose own BOM contradicts their bytes, and writes to UNKNOWN files containing U+FFFD are rejected to avoid baking replacement characters into damaged files.
|
|
17
|
+
- UTF-16 forced conversions now emit the target endianness's BOM (LE<->BE) instead of dropping it.
|
|
18
|
+
|
|
10
19
|
## [0.1.3] - 2026-10-28
|
|
11
20
|
|
|
12
21
|
### Changed
|
package/CHANGELOG.zh-CN.md
CHANGED
|
@@ -6,6 +6,15 @@
|
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
+
## [0.1.4] - 2026-10-28
|
|
10
|
+
|
|
11
|
+
### 修复
|
|
12
|
+
|
|
13
|
+
- 带 UTF-8 BOM 的文件改为对 BOM 后的完整字节做严格 UTF-8 校验:BOM 后含非法或截断序列时判为 UNKNOWN 并拒写,不再当作合法 UTF-8。
|
|
14
|
+
- 校验 UTF-16 BOM 正文:奇数字节长度与孤立代理项判为 UNKNOWN,不再在读取时按有损解码、写回时丢码元或写入替换字符。
|
|
15
|
+
- `force: true` 不再能绕过“文件自身 BOM 与字节矛盾”的拒写;向 UNKNOWN 文件写入含 U+FFFD 的内容时直接拒写,避免把替换字符固化进损坏文件。
|
|
16
|
+
- UTF-16 强制转换写出目标端序的 BOM(LE<->BE),不再丢失 BOM。
|
|
17
|
+
|
|
9
18
|
## [0.1.3] - 2026-10-28
|
|
10
19
|
|
|
11
20
|
### 调整
|
package/docs/INSTALL.md
CHANGED
|
@@ -35,7 +35,7 @@ pi update --extensions
|
|
|
35
35
|
pi install git:github.com/ethan-blue/pi-charset
|
|
36
36
|
|
|
37
37
|
# Pin a specific release tag
|
|
38
|
-
pi install git:github.com/ethan-blue/pi-charset@v0.1.
|
|
38
|
+
pi install git:github.com/ethan-blue/pi-charset@v0.1.4 -l
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
When installing from a `git:` source, Pi clones the repository under `.pi/git/github.com/ethan-blue/pi-charset` (or `~/.pi/agent/git/...`) and runs `npm install` automatically.
|
|
@@ -93,7 +93,7 @@ Restart Pi after creating `.pi-charset.json`.
|
|
|
93
93
|
|
|
94
94
|
## 4. Post-Install Verification
|
|
95
95
|
|
|
96
|
-
1. Run `/charset-status` in Pi to confirm `pi-charset v0.1.
|
|
96
|
+
1. Run `/charset-status` in Pi to confirm `pi-charset v0.1.4` and the active mode or `.pi-charset.json` path.
|
|
97
97
|
2. Edit a Chinese comment in a GBK file, then run `/charset-scan` (or `node <PKG_DIR>/tools/scan-encoding.mjs <file>`) and verify the file remains `cjk/GBK`.
|
|
98
98
|
3. Optionally scan for pre-existing damaged files (`U+FFFD` or `锟斤拷`):
|
|
99
99
|
```text
|
package/docs/zh-CN/INSTALL.md
CHANGED
|
@@ -35,7 +35,7 @@ pi update --extensions
|
|
|
35
35
|
pi install git:github.com/ethan-blue/pi-charset
|
|
36
36
|
|
|
37
37
|
# 指定版本标签安装
|
|
38
|
-
pi install git:github.com/ethan-blue/pi-charset@v0.1.
|
|
38
|
+
pi install git:github.com/ethan-blue/pi-charset@v0.1.4 -l
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
使用 `git:` 源安装时,Pi 会将仓库克隆至 `.pi/git/github.com/ethan-blue/pi-charset`(或 `~/.pi/agent/git/...`)并自动执行 `npm install`。
|
|
@@ -93,7 +93,7 @@ node <PKG_DIR>/tools/scan-encoding.mjs . --init
|
|
|
93
93
|
|
|
94
94
|
## 4. 安装后验证
|
|
95
95
|
|
|
96
|
-
1. **状态检查**:在 Pi 会话中输入 `/charset-status`,确认显示 `pi-charset v0.1.
|
|
96
|
+
1. **状态检查**:在 Pi 会话中输入 `/charset-status`,确认显示 `pi-charset v0.1.4` 及生效的模式或 `.pi-charset.json` 路径。
|
|
97
97
|
2. **读写验证**:修改工程内一个 GBK 文件的中文注释,执行 `/charset-scan`(或 `node <PKG_DIR>/tools/scan-encoding.mjs <文件路径>`),确认编码仍为 `cjk/GBK`。
|
|
98
98
|
3. **历史受损文件排查(可选)**:
|
|
99
99
|
```text
|
package/package.json
CHANGED
package/src/encoding/classify.ts
CHANGED
|
@@ -102,6 +102,25 @@ function normalizeCandidates(list: string[] | undefined): string[] {
|
|
|
102
102
|
return out.length ? out : [...DEFAULT_AUTO_CANDIDATES];
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
/** Check that a UTF-16 byte body is well-formed: even length and no unpaired surrogate code units. */
|
|
106
|
+
function isValidUtf16Body(body: Buffer, littleEndian: boolean): boolean {
|
|
107
|
+
if (body.length % 2 !== 0) return false;
|
|
108
|
+
const unitAt = (i: number) =>
|
|
109
|
+
littleEndian ? body[i]! | (body[i + 1]! << 8) : (body[i]! << 8) | body[i + 1]!;
|
|
110
|
+
for (let i = 0; i < body.length; i += 2) {
|
|
111
|
+
const unit = unitAt(i);
|
|
112
|
+
if (unit >= 0xd800 && unit <= 0xdbff) {
|
|
113
|
+
if (i + 4 > body.length) return false;
|
|
114
|
+
const next = unitAt(i + 2);
|
|
115
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) return false;
|
|
116
|
+
i += 2;
|
|
117
|
+
} else if (unit >= 0xdc00 && unit <= 0xdfff) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
|
|
105
124
|
function configVerdict(b: Buffer, enc: string, reason: "force" | "undecidable"): Verdict {
|
|
106
125
|
return {
|
|
107
126
|
kind: "config",
|
|
@@ -120,14 +139,23 @@ export function classifyBuffer(B: Buffer, cfg: ClassifyConfig | null = null): Ve
|
|
|
120
139
|
return { kind: "utf8", encoding: "UTF-8", ambiguous: false, hasFffd: false, candidates: [], exhaustive: true, bom: false };
|
|
121
140
|
}
|
|
122
141
|
// Check UTF-32 BOM before UTF-16LE (FF FE) and classify UTF-32 as binary.
|
|
123
|
-
if (startsWith(B, [0xef, 0xbb, 0xbf]))
|
|
142
|
+
if (startsWith(B, [0xef, 0xbb, 0xbf])) {
|
|
143
|
+
if (!isValidUtf8Strict(B.subarray(3)))
|
|
144
|
+
return { kind: "unknown", encoding: "UNKNOWN", ambiguous: false, hasFffd: true, candidates: [], exhaustive: true, bom: true };
|
|
124
145
|
return { kind: "utf8-bom", encoding: "UTF-8", ambiguous: false, hasFffd: false, candidates: [], exhaustive: true, bom: true };
|
|
146
|
+
}
|
|
125
147
|
if (startsWith(B, [0xff, 0xfe, 0x00, 0x00]) || startsWith(B, [0x00, 0x00, 0xfe, 0xff]))
|
|
126
148
|
return { kind: "binary", encoding: "BINARY", ambiguous: false, hasFffd: false, candidates: [], exhaustive: true, bom: false };
|
|
127
|
-
if (startsWith(B, [0xff, 0xfe]))
|
|
149
|
+
if (startsWith(B, [0xff, 0xfe])) {
|
|
150
|
+
if (!isValidUtf16Body(B.subarray(2), true))
|
|
151
|
+
return { kind: "unknown", encoding: "UNKNOWN", ambiguous: false, hasFffd: true, candidates: [], exhaustive: true, bom: true };
|
|
128
152
|
return { kind: "utf16le", encoding: "UTF-16LE", ambiguous: false, hasFffd: false, candidates: [], exhaustive: true, bom: true };
|
|
129
|
-
|
|
153
|
+
}
|
|
154
|
+
if (startsWith(B, [0xfe, 0xff])) {
|
|
155
|
+
if (!isValidUtf16Body(B.subarray(2), false))
|
|
156
|
+
return { kind: "unknown", encoding: "UNKNOWN", ambiguous: false, hasFffd: true, candidates: [], exhaustive: true, bom: true };
|
|
130
157
|
return { kind: "utf16be", encoding: "UTF-16BE", ambiguous: false, hasFffd: false, candidates: [], exhaustive: true, bom: true };
|
|
158
|
+
}
|
|
131
159
|
|
|
132
160
|
if (containsNul(B))
|
|
133
161
|
return { kind: "binary", encoding: "BINARY", ambiguous: false, hasFffd: false, candidates: [], exhaustive: true, bom: false };
|
package/src/resolve.ts
CHANGED
|
@@ -182,6 +182,36 @@ export async function resolveWritePlan(
|
|
|
182
182
|
};
|
|
183
183
|
}
|
|
184
184
|
if (verdict.kind === "unknown") {
|
|
185
|
+
if (rule.force && verdict.bom) {
|
|
186
|
+
// A BOM is the file's own encoding declaration; a body that fails to decode under it means the
|
|
187
|
+
// on-disk bytes are damaged. force declares an encoding for undecidable files, not a repair tool.
|
|
188
|
+
return {
|
|
189
|
+
encoding: null,
|
|
190
|
+
rule,
|
|
191
|
+
kind: "unknown",
|
|
192
|
+
verdict,
|
|
193
|
+
addBom: "",
|
|
194
|
+
reject:
|
|
195
|
+
`拒绝写入 ${absFilePath}:文件带 BOM 但按该编码校验失败(磁盘字节已损坏或被截断)。` +
|
|
196
|
+
`"force": true 不能覆盖文件自身的 BOM 声明;请先修复文件或人工确认实际编码后重试。`,
|
|
197
|
+
warnings,
|
|
198
|
+
transcoding: false,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (rule.force && content.includes("\ufffd")) {
|
|
202
|
+
return {
|
|
203
|
+
encoding: null,
|
|
204
|
+
rule,
|
|
205
|
+
kind: "unknown",
|
|
206
|
+
verdict,
|
|
207
|
+
addBom: "",
|
|
208
|
+
reject:
|
|
209
|
+
`拒写 ${absFilePath}:待写入内容含 U+FFFD(替换字符),写入将把原始损坏字节永久固化为替换字符。` +
|
|
210
|
+
`请先核对文件真实内容后再修改。`,
|
|
211
|
+
warnings,
|
|
212
|
+
transcoding: false,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
185
215
|
if (rule.force) {
|
|
186
216
|
warnings.push(`force=true 跳过 unknown 判定,按 ${targetFromConfig} 写出`);
|
|
187
217
|
return {
|
|
@@ -260,7 +290,10 @@ export async function resolveWritePlan(
|
|
|
260
290
|
rule,
|
|
261
291
|
kind: verdict.kind,
|
|
262
292
|
verdict,
|
|
263
|
-
|
|
293
|
+
// Cross-endian conversion (LE<->BE) must carry the target encoding's BOM; other targets carry no BOM.
|
|
294
|
+
addBom: forceConvert
|
|
295
|
+
? (encoding.toUpperCase() === "UTF-16LE" ? "utf16le" : encoding.toUpperCase() === "UTF-16BE" ? "utf16be" : "")
|
|
296
|
+
: (encoding === "UTF-16LE" ? "utf16le" : "utf16be"),
|
|
264
297
|
reject: null,
|
|
265
298
|
warnings,
|
|
266
299
|
transcoding: forceConvert,
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Synchronized with package.json version.
|
|
2
|
-
export const PLUGIN_VERSION = "0.1.
|
|
2
|
+
export const PLUGIN_VERSION = "0.1.4";
|