pi-charset 0.1.2 → 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 +16 -0
- package/CHANGELOG.zh-CN.md +16 -0
- package/README.md +8 -8
- package/README.zh-CN.md +8 -8
- package/docs/INSTALL.md +16 -14
- package/docs/zh-CN/INSTALL.md +16 -14
- 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,22 @@ 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
|
+
|
|
19
|
+
## [0.1.3] - 2026-10-28
|
|
20
|
+
|
|
21
|
+
### Changed
|
|
22
|
+
|
|
23
|
+
- Configure `.github/workflows/release.yml` for tokenless npm OIDC Trusted Publishing with Sigstore provenance attestations and automatic `--latest` GitHub Release promotion.
|
|
24
|
+
- Update installation guides and badges to recommend `pi install npm:pi-charset` and `pi update --extensions` as the primary installation and upgrade path.
|
|
25
|
+
|
|
10
26
|
## [0.1.2] - 2026-10-28
|
|
11
27
|
|
|
12
28
|
### Added
|
package/CHANGELOG.zh-CN.md
CHANGED
|
@@ -6,6 +6,22 @@
|
|
|
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
|
+
|
|
18
|
+
## [0.1.3] - 2026-10-28
|
|
19
|
+
|
|
20
|
+
### 调整
|
|
21
|
+
|
|
22
|
+
- 将 `.github/workflows/release.yml` 切换为无密钥 npm OIDC Trusted Publishing(附带 Sigstore Provenance 溯源签名)并自动标记最新 GitHub Release。
|
|
23
|
+
- 更新安装说明与实时版本徽章,将 `pi install npm:pi-charset` 与 `pi update --extensions` 作为首选安装与一键升级路径。
|
|
24
|
+
|
|
9
25
|
## [0.1.2] - 2026-10-28
|
|
10
26
|
|
|
11
27
|
### 新增
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# pi-charset
|
|
2
2
|
|
|
3
3
|
[](https://github.com/ethan-blue/pi-charset/actions/workflows/ci.yml)
|
|
4
|
-
[](https://www.npmjs.com/package/pi-charset)
|
|
5
5
|
[](./package.json)
|
|
6
6
|
[](./LICENSE)
|
|
7
7
|
|
|
@@ -58,17 +58,17 @@ Files are classified in memory in a fixed order:
|
|
|
58
58
|
|
|
59
59
|
## Quick Start
|
|
60
60
|
|
|
61
|
-
### 1. Installation
|
|
61
|
+
### 1. Installation & Updates
|
|
62
62
|
|
|
63
63
|
```bash
|
|
64
|
-
# Install from
|
|
65
|
-
pi install
|
|
64
|
+
# Install globally from npm (recommended; omit -l for global, add -l for project .pi/settings.json)
|
|
65
|
+
pi install npm:pi-charset
|
|
66
66
|
|
|
67
|
-
#
|
|
68
|
-
pi
|
|
67
|
+
# Upgrade to the latest release at any time
|
|
68
|
+
pi update --extensions
|
|
69
69
|
|
|
70
|
-
# Or install from
|
|
71
|
-
pi install /
|
|
70
|
+
# Or install directly from Git
|
|
71
|
+
pi install git:github.com/ethan-blue/pi-charset -l
|
|
72
72
|
```
|
|
73
73
|
|
|
74
74
|
### 2. Scaffold or Create `.pi-charset.json`
|
package/README.zh-CN.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# pi-charset
|
|
2
2
|
|
|
3
3
|
[](https://github.com/ethan-blue/pi-charset/actions/workflows/ci.yml)
|
|
4
|
-
[](https://www.npmjs.com/package/pi-charset)
|
|
5
5
|
[](./package.json)
|
|
6
6
|
[](./LICENSE)
|
|
7
7
|
|
|
@@ -58,17 +58,17 @@
|
|
|
58
58
|
|
|
59
59
|
## 快速开始
|
|
60
60
|
|
|
61
|
-
### 1.
|
|
61
|
+
### 1. 安装与升级
|
|
62
62
|
|
|
63
63
|
```bash
|
|
64
|
-
# 从
|
|
65
|
-
pi install
|
|
64
|
+
# 从 npm 全局安装(推荐;加 -l 则仅写入当前工程 .pi/settings.json)
|
|
65
|
+
pi install npm:pi-charset
|
|
66
66
|
|
|
67
|
-
#
|
|
68
|
-
pi
|
|
67
|
+
# 日常一键升级至最新发布版
|
|
68
|
+
pi update --extensions
|
|
69
69
|
|
|
70
|
-
#
|
|
71
|
-
pi install /
|
|
70
|
+
# 或从 Git 仓库安装
|
|
71
|
+
pi install git:github.com/ethan-blue/pi-charset -l
|
|
72
72
|
```
|
|
73
73
|
|
|
74
74
|
### 2. 脚手架生成或手动编写 `.pi-charset.json`
|
package/docs/INSTALL.md
CHANGED
|
@@ -13,31 +13,33 @@ English | [简体中文](./zh-CN/INSTALL.md)
|
|
|
13
13
|
| Runtime Dependencies | `iconv-lite`, `micromatch` (no Python required) |
|
|
14
14
|
| ripgrep | Automatically resolved from `~/.pi/agent/bin/rg(.exe)`, `rg` on `PATH`, or `@vscode/ripgrep` |
|
|
15
15
|
|
|
16
|
-
## 2. Installation
|
|
16
|
+
## 2. Installation & Updates
|
|
17
17
|
|
|
18
|
-
### Option A: Install from
|
|
18
|
+
### Option A: Install from npm (Recommended)
|
|
19
19
|
|
|
20
20
|
```powershell
|
|
21
|
+
# Global user-level install (writes to ~/.pi/agent/settings.json)
|
|
22
|
+
pi install npm:pi-charset
|
|
23
|
+
|
|
21
24
|
# Project-level install (writes to <project>/.pi/settings.json)
|
|
22
|
-
|
|
23
|
-
pi install git:github.com/ethan-blue/pi-charset -l
|
|
25
|
+
pi install npm:pi-charset -l
|
|
24
26
|
|
|
25
|
-
#
|
|
27
|
+
# Upgrade installed packages to the latest release at any time
|
|
28
|
+
pi update --extensions
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Option B: Install from Git
|
|
32
|
+
|
|
33
|
+
```powershell
|
|
34
|
+
# Unpinned Git install (tracks main via pi update --extensions)
|
|
26
35
|
pi install git:github.com/ethan-blue/pi-charset
|
|
27
36
|
|
|
28
37
|
# Pin a specific release tag
|
|
29
|
-
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
|
|
30
39
|
```
|
|
31
40
|
|
|
32
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.
|
|
33
42
|
|
|
34
|
-
### Option B: Install from npm (When Published)
|
|
35
|
-
|
|
36
|
-
```powershell
|
|
37
|
-
pi install npm:pi-charset -l
|
|
38
|
-
pi install npm:pi-charset@0.1.2 -l
|
|
39
|
-
```
|
|
40
|
-
|
|
41
43
|
### Option C: Install from a Local Directory (Offline / Intranet)
|
|
42
44
|
|
|
43
45
|
`pi install <local-path>` records the absolute path in `settings.json` without running `npm install`. When copying a source tree without `node_modules`, install production dependencies first:
|
|
@@ -91,7 +93,7 @@ Restart Pi after creating `.pi-charset.json`.
|
|
|
91
93
|
|
|
92
94
|
## 4. Post-Install Verification
|
|
93
95
|
|
|
94
|
-
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.
|
|
95
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`.
|
|
96
98
|
3. Optionally scan for pre-existing damaged files (`U+FFFD` or `锟斤拷`):
|
|
97
99
|
```text
|
package/docs/zh-CN/INSTALL.md
CHANGED
|
@@ -13,31 +13,33 @@
|
|
|
13
13
|
| 运行时依赖 | `iconv-lite`、`micromatch`(无 Python 依赖) |
|
|
14
14
|
| ripgrep | 自动查找 `~/.pi/agent/bin/rg(.exe)`、系统 `PATH` 中的 `rg` 或 `@vscode/ripgrep` |
|
|
15
15
|
|
|
16
|
-
## 2.
|
|
16
|
+
## 2. 安装与升级
|
|
17
17
|
|
|
18
|
-
### 方式 A:从
|
|
18
|
+
### 方式 A:从 npm 官方源安装(推荐)
|
|
19
19
|
|
|
20
20
|
```powershell
|
|
21
|
+
# 用户级全局安装(写入 ~/.pi/agent/settings.json,所有工程自动生效)
|
|
22
|
+
pi install npm:pi-charset
|
|
23
|
+
|
|
21
24
|
# 项目级安装(写入当前工程 .pi/settings.json)
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
pi install npm:pi-charset -l
|
|
26
|
+
|
|
27
|
+
# 日常一键升级已安装扩展至最新版本
|
|
28
|
+
pi update --extensions
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 方式 B:从 Git 仓库安装
|
|
24
32
|
|
|
25
|
-
|
|
33
|
+
```powershell
|
|
34
|
+
# 不锁版本安装(后续可通过 pi update --extensions 跟踪最新提交)
|
|
26
35
|
pi install git:github.com/ethan-blue/pi-charset
|
|
27
36
|
|
|
28
37
|
# 指定版本标签安装
|
|
29
|
-
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
|
|
30
39
|
```
|
|
31
40
|
|
|
32
41
|
使用 `git:` 源安装时,Pi 会将仓库克隆至 `.pi/git/github.com/ethan-blue/pi-charset`(或 `~/.pi/agent/git/...`)并自动执行 `npm install`。
|
|
33
42
|
|
|
34
|
-
### 方式 B:从 npm 安装(发布至 npm 后可用)
|
|
35
|
-
|
|
36
|
-
```powershell
|
|
37
|
-
pi install npm:pi-charset -l
|
|
38
|
-
pi install npm:pi-charset@0.1.2 -l
|
|
39
|
-
```
|
|
40
|
-
|
|
41
43
|
### 方式 C:从本地目录安装(离线或内网环境)
|
|
42
44
|
|
|
43
45
|
`pi install <本地路径>` 仅将绝对路径写入 `settings.json`,不会自动执行 `npm install`。若拷贝的是不含 `node_modules` 的源码目录,需先安装运行时依赖:
|
|
@@ -91,7 +93,7 @@ node <PKG_DIR>/tools/scan-encoding.mjs . --init
|
|
|
91
93
|
|
|
92
94
|
## 4. 安装后验证
|
|
93
95
|
|
|
94
|
-
1. **状态检查**:在 Pi 会话中输入 `/charset-status`,确认显示 `pi-charset v0.1.
|
|
96
|
+
1. **状态检查**:在 Pi 会话中输入 `/charset-status`,确认显示 `pi-charset v0.1.4` 及生效的模式或 `.pi-charset.json` 路径。
|
|
95
97
|
2. **读写验证**:修改工程内一个 GBK 文件的中文注释,执行 `/charset-scan`(或 `node <PKG_DIR>/tools/scan-encoding.mjs <文件路径>`),确认编码仍为 `cjk/GBK`。
|
|
96
98
|
3. **历史受损文件排查(可选)**:
|
|
97
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";
|