@koishi-ce/plugin-explorer 1.2.5 → 1.2.7
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/dist/{dist-DuNPX8oL.js → dist-2ck6Lu1l.js} +1 -1
- package/dist/{dist-Ch8tr6dT.js → dist-B4uepvx7.js} +1 -1
- package/dist/{dist-Blh4OPYn.js → dist-BQewsVT5.js} +1 -1
- package/dist/{dist-Cacwo_YK.js → dist-Bj8MxUlR.js} +1 -1
- package/dist/dist-C2lVT9bn.js +13 -0
- package/dist/{dist-C0gMr32h.js → dist-CMLFB1R0.js} +1 -1
- package/dist/{dist-_hmoJos2.js → dist-CT3KxkoU.js} +1 -1
- package/dist/{dist-TVIFRf9R.js → dist-CVDBysnv.js} +1 -1
- package/dist/{dist-s0Ar-UF_.js → dist-Cs4wIxAc.js} +1 -1
- package/dist/{dist-C9jnKDsH.js → dist-DJhDB1Oo.js} +1 -1
- package/dist/{dist-C10CZ8tl.js → dist-DgF9sBz5.js} +1 -1
- package/dist/{dist-DMBczCvn.js → dist-DiPK7Ui7.js} +1 -1
- package/dist/{dist-DY-hzjgX.js → dist-DmS5Ra5k.js} +1 -1
- package/dist/{dist-Hie5K0FP.js → dist-Dv58_4ys.js} +1 -1
- package/dist/index.js +2 -2
- package/dist/rolldown-runtime-BMBA7GOT.js +1 -0
- package/dist/style.css +1 -1
- package/lib/index.d.ts +4 -0
- package/lib/index.mjs +103 -23
- package/package.json +8 -8
- package/src/index.ts +167 -21
- package/dist/dist-DvIabug0.js +0 -13
- package/dist/rolldown-runtime-CGnMp29N.js +0 -1
- package/src/encoding.test.ts +0 -131
- package/src/index.test.ts +0 -206
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var e=Object.defineProperty,t=(t,n)=>{let r={};for(var i in t)e(r,i,{get:t[i],enumerable:!0});return n||e(r,Symbol.toStringTag,{value:`Module`}),r};export{t};
|
package/src/encoding.test.ts
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
-
// Copyright (c) 2019-present Shigma and Koishijs contributors.
|
|
3
|
-
// Copyright (c) 2026-present Koishi-CE contributors.
|
|
4
|
-
|
|
5
|
-
import { describe, expect, it } from "bun:test";
|
|
6
|
-
import {
|
|
7
|
-
detectEncoding,
|
|
8
|
-
HEAD_SIZE,
|
|
9
|
-
readHead,
|
|
10
|
-
} from "./encoding.ts";
|
|
11
|
-
|
|
12
|
-
/** 构造指定长度的可重复字节序列。 */
|
|
13
|
-
function repeat(pattern: Buffer, times: number) {
|
|
14
|
-
const buffer = Buffer.allocUnsafe(pattern.length * times);
|
|
15
|
-
for (let i = 0; i < times; i++) {
|
|
16
|
-
pattern.copy(buffer, i * pattern.length);
|
|
17
|
-
}
|
|
18
|
-
return buffer;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const gb18030 = Buffer.from([
|
|
22
|
-
// "这是" 的 GBK/GB18030 双字节编码
|
|
23
|
-
0xd5, 0xe2, 0xca, 0xc7,
|
|
24
|
-
]);
|
|
25
|
-
const shiftJis = Buffer.from([
|
|
26
|
-
// "テスト" 的 Shift_JIS 双字节编码
|
|
27
|
-
0x83, 0x65, 0x83, 0x58, 0x83, 0x67,
|
|
28
|
-
]);
|
|
29
|
-
|
|
30
|
-
describe("readHead", () => {
|
|
31
|
-
it("短于阈值时返回原 buffer 本身", () => {
|
|
32
|
-
const buffer = Buffer.from("hello world");
|
|
33
|
-
expect(readHead(buffer)).toBe(buffer);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("超长时返回不拷贝的头部视图", () => {
|
|
37
|
-
const buffer = Buffer.alloc(HEAD_SIZE * 3, 0x61);
|
|
38
|
-
const head = readHead(buffer);
|
|
39
|
-
expect(head.length).toBe(HEAD_SIZE);
|
|
40
|
-
// 视图共享底层内存:改原 buffer 可见
|
|
41
|
-
buffer[0] = 0x62;
|
|
42
|
-
expect(head[0]).toBe(0x62);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe("detectEncoding", () => {
|
|
47
|
-
it("无 BOM 的合法 UTF-8 判为 UTF-8", () => {
|
|
48
|
-
expect(detectEncoding(Buffer.from("hello world"))).toBe(
|
|
49
|
-
"UTF-8",
|
|
50
|
-
);
|
|
51
|
-
expect(
|
|
52
|
-
detectEncoding(
|
|
53
|
-
repeat(Buffer.from("这是中文测试文本。"), 500),
|
|
54
|
-
),
|
|
55
|
-
).toBe("UTF-8");
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it("UTF-8 BOM 优先于统计探测", () => {
|
|
59
|
-
const buffer = Buffer.concat([
|
|
60
|
-
Buffer.from([0xef, 0xbb, 0xbf]),
|
|
61
|
-
Buffer.from("这是中文", "utf8"),
|
|
62
|
-
]);
|
|
63
|
-
expect(detectEncoding(buffer)).toBe("UTF-8");
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("UTF-16LE BOM 判为 UTF-16LE", () => {
|
|
67
|
-
const buffer = Buffer.concat([
|
|
68
|
-
Buffer.from([0xff, 0xfe]),
|
|
69
|
-
Buffer.from("中文测试内容", "utf16le"),
|
|
70
|
-
]);
|
|
71
|
-
expect(detectEncoding(buffer)).toBe("UTF-16LE");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("UTF-16BE BOM 判为 UTF-16BE", () => {
|
|
75
|
-
const buffer = Buffer.concat([
|
|
76
|
-
Buffer.from([0xfe, 0xff, 0x4e, 0x2d]),
|
|
77
|
-
Buffer.alloc(64, 0x00),
|
|
78
|
-
]);
|
|
79
|
-
expect(detectEncoding(buffer)).toBe("UTF-16BE");
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("UTF-32LE/BE BOM 判为 UTF-32 系列", () => {
|
|
83
|
-
expect(
|
|
84
|
-
detectEncoding(
|
|
85
|
-
Buffer.concat([
|
|
86
|
-
Buffer.from([0xff, 0xfe, 0x00, 0x00]),
|
|
87
|
-
Buffer.alloc(64, 0x41),
|
|
88
|
-
]),
|
|
89
|
-
),
|
|
90
|
-
).toBe("UTF-32LE");
|
|
91
|
-
expect(
|
|
92
|
-
detectEncoding(
|
|
93
|
-
Buffer.concat([
|
|
94
|
-
Buffer.from([0x00, 0x00, 0xfe, 0xff]),
|
|
95
|
-
Buffer.alloc(64, 0x41),
|
|
96
|
-
]),
|
|
97
|
-
),
|
|
98
|
-
).toBe("UTF-32BE");
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("GB18030 回落 chardet 统计探测", () => {
|
|
102
|
-
expect(detectEncoding(repeat(gb18030, 400))).toBe(
|
|
103
|
-
"GB18030",
|
|
104
|
-
);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it("Shift_JIS 回落 chardet 统计探测", () => {
|
|
108
|
-
expect(detectEncoding(repeat(shiftJis, 400))).toBe(
|
|
109
|
-
"Shift_JIS",
|
|
110
|
-
);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it("仅头部采样:尾部乱码不影响首选编码", () => {
|
|
114
|
-
// 头部为 GB18030 文本,尾部追加非 UTF-8 噪声
|
|
115
|
-
const buffer = Buffer.concat([
|
|
116
|
-
repeat(gb18030, 2000),
|
|
117
|
-
Buffer.alloc(1024 * 1024, 0xff),
|
|
118
|
-
]);
|
|
119
|
-
expect(detectEncoding(buffer)).toBe("GB18030");
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it("超长文件不整体解码,尾部非法字节不触发统计探测", () => {
|
|
123
|
-
// 头部 9 MB 全为合法 UTF-8(超出单次校验上限,只校验头部),
|
|
124
|
-
// 尾部落在校验范围外的非法字节因此不影响判定
|
|
125
|
-
const buffer = Buffer.concat([
|
|
126
|
-
Buffer.alloc(9 * 1024 * 1024, 0x61),
|
|
127
|
-
Buffer.from([0xff, 0xfe, 0xfd]),
|
|
128
|
-
]);
|
|
129
|
-
expect(detectEncoding(buffer)).toBe("UTF-8");
|
|
130
|
-
});
|
|
131
|
-
});
|
package/src/index.test.ts
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
-
// Copyright (c) 2019-present Shigma and Koishijs contributors.
|
|
3
|
-
// Copyright (c) 2026-present Koishi-CE contributors.
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
afterAll,
|
|
7
|
-
beforeAll,
|
|
8
|
-
describe,
|
|
9
|
-
expect,
|
|
10
|
-
it,
|
|
11
|
-
} from "bun:test";
|
|
12
|
-
import { existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
13
|
-
import {
|
|
14
|
-
mkdir,
|
|
15
|
-
readFile,
|
|
16
|
-
symlink,
|
|
17
|
-
writeFile,
|
|
18
|
-
} from "node:fs/promises";
|
|
19
|
-
import { tmpdir } from "node:os";
|
|
20
|
-
import { join } from "node:path";
|
|
21
|
-
import { Console, type Entry } from "@koishi-ce/console";
|
|
22
|
-
import { App, type Plugin } from "@koishi-ce/koishi";
|
|
23
|
-
import explorer, {
|
|
24
|
-
type Entry as TreeEntry,
|
|
25
|
-
} from "./index.ts";
|
|
26
|
-
|
|
27
|
-
/** 控制台服务桩:仅实现入口登记所需的最小面。 */
|
|
28
|
-
class FakeConsole extends Console {
|
|
29
|
-
protected resolveEntry(
|
|
30
|
-
_files: Entry.Files,
|
|
31
|
-
_key: string,
|
|
32
|
-
): string[] {
|
|
33
|
-
return [];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** 在系统临时目录构造文件管理场景(含被忽略的隐藏文件与 node_modules)。 */
|
|
38
|
-
async function createScenario() {
|
|
39
|
-
const root = mkdtempSync(
|
|
40
|
-
join(tmpdir(), "explorer-test-"),
|
|
41
|
-
);
|
|
42
|
-
await writeFile(
|
|
43
|
-
join(root, "b.txt"),
|
|
44
|
-
"hello world",
|
|
45
|
-
"utf8",
|
|
46
|
-
);
|
|
47
|
-
// PNG 魔数 + IHDR chunk 头(file-type 依此识别 MIME)
|
|
48
|
-
await writeFile(
|
|
49
|
-
join(root, "a.png"),
|
|
50
|
-
Buffer.from([
|
|
51
|
-
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00,
|
|
52
|
-
0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00,
|
|
53
|
-
0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00,
|
|
54
|
-
0x00, 0x00,
|
|
55
|
-
]),
|
|
56
|
-
);
|
|
57
|
-
await mkdir(join(root, "sub"));
|
|
58
|
-
await writeFile(
|
|
59
|
-
join(root, "sub", "c.txt"),
|
|
60
|
-
"nested",
|
|
61
|
-
"utf8",
|
|
62
|
-
);
|
|
63
|
-
await mkdir(join(root, "node_modules"));
|
|
64
|
-
await writeFile(
|
|
65
|
-
join(root, "node_modules", "x.txt"),
|
|
66
|
-
"ignored",
|
|
67
|
-
);
|
|
68
|
-
await writeFile(join(root, ".hidden"), "ignored");
|
|
69
|
-
// win32 无特权环境下用 junction 目录链接充当符号链接
|
|
70
|
-
await symlink(
|
|
71
|
-
join(root, "sub"),
|
|
72
|
-
join(root, "link"),
|
|
73
|
-
"junction",
|
|
74
|
-
);
|
|
75
|
-
return root;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const root = await createScenario();
|
|
79
|
-
|
|
80
|
-
const app = new App();
|
|
81
|
-
|
|
82
|
-
app.plugin(
|
|
83
|
-
FakeConsole as unknown as Plugin.Constructor<App>,
|
|
84
|
-
);
|
|
85
|
-
app.plugin(explorer, { root });
|
|
86
|
-
|
|
87
|
-
/** 以假客户端身份调用指定 RPC 监听器。 */
|
|
88
|
-
async function call<K extends keyof Console["listeners"]>(
|
|
89
|
-
event: K,
|
|
90
|
-
...args: unknown[]
|
|
91
|
-
) {
|
|
92
|
-
const listener = app.console.listeners[event];
|
|
93
|
-
if (!listener)
|
|
94
|
-
throw new Error(`listener ${String(event)} not found`);
|
|
95
|
-
return listener.callback.call(
|
|
96
|
-
{} as never,
|
|
97
|
-
...(args as never[]),
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const service = () => app.get("console.services.explorer");
|
|
102
|
-
|
|
103
|
-
beforeAll(() => app.start());
|
|
104
|
-
|
|
105
|
-
afterAll(async () => {
|
|
106
|
-
await app.stop();
|
|
107
|
-
rmSync(root, { recursive: true, force: true });
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
describe("explorer 文件管理服务", () => {
|
|
111
|
-
it("文件树遍历:目录优先、按名排序、忽略隐藏与 node_modules", async () => {
|
|
112
|
-
const entries = (await service()?.get()) as TreeEntry[];
|
|
113
|
-
const names = entries.map((entry) => entry.name);
|
|
114
|
-
// 目录排最前,其余按字母序;ignored 项不出现
|
|
115
|
-
expect(names).toEqual([
|
|
116
|
-
"sub",
|
|
117
|
-
"a.png",
|
|
118
|
-
"b.txt",
|
|
119
|
-
"link",
|
|
120
|
-
]);
|
|
121
|
-
const sub = entries[0];
|
|
122
|
-
expect(sub?.type).toBe("directory");
|
|
123
|
-
expect(
|
|
124
|
-
sub?.children?.map((child) => child.name),
|
|
125
|
-
).toEqual(["c.txt"]);
|
|
126
|
-
// junction 链接识别为符号链接并给出目标
|
|
127
|
-
const link = entries[3];
|
|
128
|
-
expect(link?.type).toBe("symlink");
|
|
129
|
-
expect(link?.target).toContain("sub");
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
it("read 返回文本文件的 base64 与编码探测", async () => {
|
|
133
|
-
const file = (await call("explorer/read", "b.txt")) as {
|
|
134
|
-
base64: string;
|
|
135
|
-
encoding?: string;
|
|
136
|
-
mime?: string;
|
|
137
|
-
};
|
|
138
|
-
expect(
|
|
139
|
-
Buffer.from(file.base64, "base64").toString("utf8"),
|
|
140
|
-
).toBe("hello world");
|
|
141
|
-
expect(file.encoding).toBeTruthy();
|
|
142
|
-
// 文本文件不携带 mime
|
|
143
|
-
expect(file.mime).toBeUndefined();
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it("read 返回二进制文件的 MIME 探测", async () => {
|
|
147
|
-
const file = (await call("explorer/read", "a.png")) as {
|
|
148
|
-
base64: string;
|
|
149
|
-
encoding?: string;
|
|
150
|
-
mime?: string;
|
|
151
|
-
};
|
|
152
|
-
expect(file.mime).toBe("image/png");
|
|
153
|
-
// 已识别出 MIME 的文件不做编码探测(客户端只消费 mime)
|
|
154
|
-
expect(file.encoding).toBeUndefined();
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it("write 支持文本与 base64 二进制写入", async () => {
|
|
158
|
-
await call("explorer/write", "sub/new.txt", "新内容");
|
|
159
|
-
expect(
|
|
160
|
-
await readFile(join(root, "sub", "new.txt"), "utf8"),
|
|
161
|
-
).toBe("新内容");
|
|
162
|
-
|
|
163
|
-
const buffer = Buffer.from([1, 2, 3, 4]);
|
|
164
|
-
await call(
|
|
165
|
-
"explorer/write",
|
|
166
|
-
"sub/new.bin",
|
|
167
|
-
buffer.toString("base64"),
|
|
168
|
-
true,
|
|
169
|
-
);
|
|
170
|
-
expect(
|
|
171
|
-
await readFile(join(root, "sub", "new.bin")),
|
|
172
|
-
).toEqual(buffer);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it("mkdir / rename / remove 执行实际文件操作", async () => {
|
|
176
|
-
await call("explorer/mkdir", "made");
|
|
177
|
-
expect(existsSync(join(root, "made"))).toBe(true);
|
|
178
|
-
|
|
179
|
-
await call("explorer/rename", "made", "renamed");
|
|
180
|
-
expect(existsSync(join(root, "renamed"))).toBe(true);
|
|
181
|
-
|
|
182
|
-
await call("explorer/remove", "renamed");
|
|
183
|
-
expect(existsSync(join(root, "renamed"))).toBe(false);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
it("explorer/refresh 与 get 的缓存复用语义", async () => {
|
|
187
|
-
const svc = service();
|
|
188
|
-
// 非强制调用复用进行中的遍历任务(task 字段不更换)
|
|
189
|
-
await svc?.get();
|
|
190
|
-
const before = svc?.task;
|
|
191
|
-
await svc?.get();
|
|
192
|
-
expect(svc?.task).toBe(before);
|
|
193
|
-
// 文件变动后强制刷新得到新树
|
|
194
|
-
await writeFile(join(root, "z.txt"), "tail");
|
|
195
|
-
const fresh = (await svc?.get(true)) as TreeEntry[];
|
|
196
|
-
expect(fresh.map((entry) => entry.name)).toContain(
|
|
197
|
-
"z.txt",
|
|
198
|
-
);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it("read 不存在的文件时抛错", async () => {
|
|
202
|
-
expect(
|
|
203
|
-
call("explorer/read", "not-exist.txt"),
|
|
204
|
-
).rejects.toThrow();
|
|
205
|
-
});
|
|
206
|
-
});
|