@fabianactor/dsh-lil-whale-group 1.8.14 → 1.8.15
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/lib/index.js +3 -2
- package/lib/text-clean.js +89 -0
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
// 按 cwd 会叫错人。cwd 只作退路,并在 plugin.log 留一行痕迹。
|
|
10
10
|
|
|
11
11
|
import { appendFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
12
|
+
import { printableText } from './text-clean.js'
|
|
12
13
|
import { join, dirname, basename } from 'node:path'
|
|
13
14
|
import { homedir } from 'node:os'
|
|
14
15
|
import { pathToFileURL, fileURLToPath } from 'node:url'
|
|
@@ -1672,7 +1673,7 @@ function sayPrivate(from, to, text, image = null, images = null) {
|
|
|
1672
1673
|
mkdirSync(DM_DIR, { recursive: true })
|
|
1673
1674
|
const list = normalizeImageList(image, images)
|
|
1674
1675
|
const record = {
|
|
1675
|
-
ts: new Date().toISOString(), from, to, text, kind: 'dm', channel: 'dm',
|
|
1676
|
+
ts: new Date().toISOString(), from, to, text: printableText(text), kind: 'dm', channel: 'dm',
|
|
1676
1677
|
...(list.length === 0 ? {} : (list.length === 1 ? { image: list[0] } : { images: list })),
|
|
1677
1678
|
}
|
|
1678
1679
|
appendFileSync(dmFileOf(from, to), JSON.stringify(record) + '\n', 'utf8')
|
|
@@ -1693,7 +1694,7 @@ function say(from, to, text, channel = 'group', s = '', toSeat = '', image = nul
|
|
|
1693
1694
|
// 只追加的文件删不掉一行,所以空正文绝不能落盘。
|
|
1694
1695
|
// 现场(2026-11-11 16:23):一条没有 text 的记录进了 messages.jsonl,
|
|
1695
1696
|
// 在 群.md 里显示成 undefined —— 一个空对象,读起来却像一句话。
|
|
1696
|
-
const body = typeof text === 'string' ? text.trim() : ''
|
|
1697
|
+
const body = typeof text === 'string' ? printableText(text.trim()) : ''
|
|
1697
1698
|
// 一条消息至少要有一件东西:一段话或一张图。两个都空,仍然是"空消息"。
|
|
1698
1699
|
if (body === '' && list.length === 0) throw new Error('群消息不能没有内容(text 和 image 都是空的,拒绝写入)')
|
|
1699
1700
|
// 2026-11-12 20:20 改:以前 `to === 'fabian'` 也算私聊,于是"在群里对 Fabian 说"这条路不存在了
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// 账本里的每个字节都要能被印出来(第四条鱼 #1014,2026-11-17)。
|
|
2
|
+
//
|
|
3
|
+
// 现场:一条群消息里 markdown 的 `b 被 PowerShell 双引号 here-string 当成了退格符(0x08),
|
|
4
|
+
// 落盘后显示成 "undles=" —— 一个"合法但少一个字符"的字段名,按字段读的脚本不会报错。
|
|
5
|
+
//
|
|
6
|
+
// 规则:C0 控制字符(除换行)与 DEL 一律换成可见占位 <0xNN>;换行保留(一条消息可以多行)。
|
|
7
|
+
//
|
|
8
|
+
// 2026-11-17 第四条鱼补两句(#1016):扫字节之前先问"它是不是本来就不是文本"——
|
|
9
|
+
// UTF-16LE 的 "no" 是 6e 00 6f 00,按 UTF-8 读就是一片 NUL,那不是坏字节,是读法不对。
|
|
10
|
+
// 所以这里既给字符级的 printableText,也给字节级的 encodingOf / scanBytes。
|
|
11
|
+
const UNPRINTABLE = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g;
|
|
12
|
+
|
|
13
|
+
export function printableText(value) {
|
|
14
|
+
if (typeof value !== 'string') return value;
|
|
15
|
+
return value.replace(
|
|
16
|
+
UNPRINTABLE,
|
|
17
|
+
(ch) => '<0x' + ch.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0') + '>',
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function countUnprintable(value) {
|
|
22
|
+
if (typeof value !== 'string') return 0;
|
|
23
|
+
const m = value.match(UNPRINTABLE);
|
|
24
|
+
return m ? m.length : 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 猜一个 Buffer 的编码。只为"扫之前先分清楚文本还是二进制/UTF-16"这一件事,
|
|
29
|
+
* 不是通用编码识别器:能认 BOM、能认"偶数位/奇数位大量 0x00"这两种最常见的情况。
|
|
30
|
+
*/
|
|
31
|
+
export function encodingOf(buf) {
|
|
32
|
+
if (!buf || buf.length === 0) return 'empty';
|
|
33
|
+
if (buf.length >= 3 && buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) return 'utf-8-bom';
|
|
34
|
+
if (buf.length >= 2 && buf[0] === 0xff && buf[1] === 0xfe) return 'utf-16le-bom';
|
|
35
|
+
if (buf.length >= 2 && buf[0] === 0xfe && buf[1] === 0xff) return 'utf-16be-bom';
|
|
36
|
+
const n = Math.min(buf.length, 512);
|
|
37
|
+
let pairs = 0;
|
|
38
|
+
let zerosEven = 0;
|
|
39
|
+
let zerosOdd = 0;
|
|
40
|
+
for (let i = 0; i + 1 < n; i += 2) {
|
|
41
|
+
pairs += 1;
|
|
42
|
+
if (buf[i] === 0) zerosEven += 1;
|
|
43
|
+
if (buf[i + 1] === 0) zerosOdd += 1;
|
|
44
|
+
}
|
|
45
|
+
if (pairs >= 2 && zerosOdd / pairs > 0.6) return 'utf-16le';
|
|
46
|
+
if (pairs >= 2 && zerosEven / pairs > 0.6) return 'utf-16be';
|
|
47
|
+
return 'utf-8';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 扫一段字节里有没有"不可打印字符"。
|
|
52
|
+
* 返回 { encoding, decoded, unprintable: [{ index, char, code }] };
|
|
53
|
+
* UTF-16 会先解码再扫——这样 NUL 是编码差异,不会被当成内容坏了。
|
|
54
|
+
*/
|
|
55
|
+
export function scanBytes(buf) {
|
|
56
|
+
const encoding = encodingOf(buf);
|
|
57
|
+
if (encoding === 'empty') return { encoding, decoded: '', unprintable: [] };
|
|
58
|
+
if (encoding === 'utf-16le-bom' || encoding === 'utf-16be-bom') {
|
|
59
|
+
// 用 Node 的 TextDecoder(buf 可能是 Buffer 或 Uint8Array)
|
|
60
|
+
try {
|
|
61
|
+
const view = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
62
|
+
const decoded = new TextDecoder(encoding === 'utf-16le-bom' ? 'utf-16le' : 'utf-16be').decode(view);
|
|
63
|
+
return { encoding, decoded, unprintable: occurrencesOf(decoded) };
|
|
64
|
+
} catch {
|
|
65
|
+
return { encoding, decoded: '', unprintable: [] };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (encoding === 'utf-16le' || encoding === 'utf-16be') {
|
|
69
|
+
try {
|
|
70
|
+
const view = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
71
|
+
const decoded = new TextDecoder(encoding === 'utf-16le' ? 'utf-16le' : 'utf-16be').decode(view);
|
|
72
|
+
return { encoding, decoded, unprintable: occurrencesOf(decoded) };
|
|
73
|
+
} catch {
|
|
74
|
+
return { encoding, decoded: '', unprintable: [] };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const raw = buf.toString ? buf.toString('utf8') : String(buf);
|
|
78
|
+
return { encoding, decoded: raw, unprintable: occurrencesOf(raw) };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function occurrencesOf(str) {
|
|
82
|
+
const out = [];
|
|
83
|
+
const re = new RegExp(UNPRINTABLE.source, 'g');
|
|
84
|
+
let m;
|
|
85
|
+
while ((m = re.exec(str)) !== null) {
|
|
86
|
+
out.push({ index: m.index, char: m[0], code: m[0].charCodeAt(0) });
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|