dsh-plugin-mesh 0.5.7 → 0.5.8
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/AGENTS.md +9 -2
- package/README.md +68 -6
- package/client/client.js +270 -30
- package/cordis.patch.yml +4 -0
- package/lib/_selftest-at-mention-host.mjs +377 -0
- package/lib/_selftest-at-mention.mjs +168 -0
- package/lib/_selftest-prompt.mjs +7 -0
- package/lib/_selftest-reply-route.mjs +49 -1
- package/lib/_selftest-update.mjs +9 -0
- package/lib/_selftest-upgrade.mjs +619 -0
- package/lib/_selftest-workbench.mjs +130 -20
- package/lib/at-mention.js +109 -0
- package/lib/index.js +501 -98
- package/lib/prompt.js +5 -1
- package/lib/update.js +3 -0
- package/lib/upgrade.js +635 -0
- package/llms.txt +3 -1
- package/package.json +7 -4
- package/scripts/check-tdz.mjs +1 -1
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 一键升级的自测:校验、解包、差异分类、原子替换与回滚。
|
|
3
|
+
*
|
|
4
|
+
* 这一块最贵的 bug 不是"装不上",而是**装一半**:磁盘被覆盖了、新代码加载
|
|
5
|
+
* 不了,用户下次重启 DSH 就崩。所以断言的重点是:
|
|
6
|
+
* · 任何失败路径下,旧目录一个字节都不能变;
|
|
7
|
+
* · 只有「可加载检查通过」才允许 rename 覆盖;
|
|
8
|
+
* · 只改客户端时不能要求重启(否则等于白折腾)。
|
|
9
|
+
*
|
|
10
|
+
* 全部无网络:fetch 是注入的假实现,tar 包在测试里现做。
|
|
11
|
+
*
|
|
12
|
+
* node lib/_selftest-upgrade.mjs
|
|
13
|
+
*/
|
|
14
|
+
import { strict as assert } from "node:assert";
|
|
15
|
+
import { createHash } from "node:crypto";
|
|
16
|
+
import {
|
|
17
|
+
existsSync,
|
|
18
|
+
mkdirSync,
|
|
19
|
+
mkdtempSync,
|
|
20
|
+
readFileSync,
|
|
21
|
+
readdirSync,
|
|
22
|
+
rmSync,
|
|
23
|
+
statSync,
|
|
24
|
+
writeFileSync,
|
|
25
|
+
} from "node:fs";
|
|
26
|
+
import { tmpdir } from "node:os";
|
|
27
|
+
import { dirname, join } from "node:path";
|
|
28
|
+
import { gzipSync } from "node:zlib";
|
|
29
|
+
import {
|
|
30
|
+
PACKAGE_NAME,
|
|
31
|
+
collectChanges,
|
|
32
|
+
downloadTarball,
|
|
33
|
+
extractTarball,
|
|
34
|
+
fetchVersionMeta,
|
|
35
|
+
installUpdate,
|
|
36
|
+
parseIntegrity,
|
|
37
|
+
pruneBackups,
|
|
38
|
+
requestHostReload,
|
|
39
|
+
requestHostRestart,
|
|
40
|
+
trustedControlRequest,
|
|
41
|
+
verifyBuffer,
|
|
42
|
+
writeEntries,
|
|
43
|
+
} from "./upgrade.js";
|
|
44
|
+
|
|
45
|
+
let passed = 0;
|
|
46
|
+
let failed = 0;
|
|
47
|
+
function test(name, fn) {
|
|
48
|
+
try {
|
|
49
|
+
fn();
|
|
50
|
+
passed += 1;
|
|
51
|
+
console.log(` ✓ ${name}`);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
failed += 1;
|
|
54
|
+
console.log(` ✗ ${name}`);
|
|
55
|
+
console.log(` ${err?.message ?? err}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function testAsync(name, fn) {
|
|
60
|
+
try {
|
|
61
|
+
await fn();
|
|
62
|
+
passed += 1;
|
|
63
|
+
console.log(` ✓ ${name}`);
|
|
64
|
+
} catch (err) {
|
|
65
|
+
failed += 1;
|
|
66
|
+
console.log(` ✗ ${name}`);
|
|
67
|
+
console.log(` ${err?.message ?? err}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ── tar 夹具 ─────────────────────────────────────────────
|
|
72
|
+
|
|
73
|
+
function tarBlock(name, data, { mode = 0o644, type = "0" } = {}) {
|
|
74
|
+
const header = Buffer.alloc(512);
|
|
75
|
+
header.write(name, 0, 100, "utf8");
|
|
76
|
+
header.write(`${(mode & 0o7777).toString(8).padStart(7, "0")}\0`, 100, 8, "ascii");
|
|
77
|
+
header.write("0000000\0", 108, 8);
|
|
78
|
+
header.write("0000000\0", 116, 8);
|
|
79
|
+
header.write(`${data.length.toString(8).padStart(11, "0")}\0`, 124, 12, "ascii");
|
|
80
|
+
header.write("00000000000\0", 136, 12);
|
|
81
|
+
header.write(" ", 148, 8); // 校验和字段先留空格
|
|
82
|
+
header.write(type, 156, 1);
|
|
83
|
+
let sum = 0;
|
|
84
|
+
for (const b of header) sum += b;
|
|
85
|
+
header.write(`${sum.toString(8).padStart(6, "0")}\0 `, 148, 8, "ascii");
|
|
86
|
+
const padded = Buffer.alloc(Math.ceil(data.length / 512) * 512);
|
|
87
|
+
data.copy(padded);
|
|
88
|
+
return Buffer.concat([header, padded]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function makeTarGz(files) {
|
|
92
|
+
const chunks = [];
|
|
93
|
+
for (const file of files) {
|
|
94
|
+
const data = file.dir ? Buffer.alloc(0) : Buffer.from(file.content, "utf8");
|
|
95
|
+
chunks.push(tarBlock(file.name, data, { mode: file.mode ?? (file.dir ? 0o755 : 0o644), type: file.dir ? "5" : "0" }));
|
|
96
|
+
}
|
|
97
|
+
chunks.push(Buffer.alloc(1024));
|
|
98
|
+
return gzipSync(Buffer.concat(chunks));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function integrityOf(buffer) {
|
|
102
|
+
return `sha512-${createHash("sha512").update(buffer).digest("base64")}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function jsonRes(body, status = 200) {
|
|
106
|
+
return { ok: status === 200, status, headers: { get: () => null }, json: async () => body };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function bufRes(buffer, status = 200) {
|
|
110
|
+
return {
|
|
111
|
+
ok: status === 200,
|
|
112
|
+
status,
|
|
113
|
+
headers: { get: (name) => (String(name).toLowerCase() === "content-length" ? String(buffer.length) : null) },
|
|
114
|
+
arrayBuffer: async () => buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function tempDir() {
|
|
119
|
+
return mkdtempSync(join(tmpdir(), "mesh-upgrade-test-"));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function seedPlugin(dir, { version = "0.5.7", lib = "old", client = "old" } = {}) {
|
|
123
|
+
mkdirSync(join(dir, "lib"), { recursive: true });
|
|
124
|
+
mkdirSync(join(dir, "client"), { recursive: true });
|
|
125
|
+
writeFileSync(join(dir, "package.json"), JSON.stringify({ name: PACKAGE_NAME, version }, null, 2));
|
|
126
|
+
writeFileSync(join(dir, "lib", "index.js"), `export const marker = ${JSON.stringify(lib)};\n`);
|
|
127
|
+
writeFileSync(join(dir, "client", "client.js"), `// ${client}\n`);
|
|
128
|
+
return dir;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function npmTarball({ version = "0.5.8", lib = "new", client = "new" } = {}) {
|
|
132
|
+
return makeTarGz([
|
|
133
|
+
{ name: "package/", dir: true },
|
|
134
|
+
{ name: "package/package.json", content: JSON.stringify({ name: PACKAGE_NAME, version }, null, 2) },
|
|
135
|
+
{ name: "package/lib/index.js", content: `export const marker = ${JSON.stringify(lib)};\n` },
|
|
136
|
+
{ name: "package/client/client.js", content: `// ${client}\n` },
|
|
137
|
+
]);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
console.log("\n── 完整性校验 ──");
|
|
141
|
+
|
|
142
|
+
test("sha512 integrity 能验过;内容改一字节就失败", () => {
|
|
143
|
+
const buf = Buffer.from("hello mesh");
|
|
144
|
+
const integrity = integrityOf(buf);
|
|
145
|
+
assert.deepEqual(parseIntegrity(integrity)?.algorithm, "sha512");
|
|
146
|
+
assert.equal(verifyBuffer(buf, integrity), true);
|
|
147
|
+
assert.equal(verifyBuffer(Buffer.from("hello mesa"), integrity), false);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("不认识的 integrity 一律不通过(不能把「没有哈希」当成通过)", () => {
|
|
151
|
+
for (const bad of ["", "sha512", "md5-abc", "sha512-***", null, undefined]) {
|
|
152
|
+
assert.equal(parseIntegrity(bad), null, JSON.stringify(bad));
|
|
153
|
+
assert.equal(verifyBuffer(Buffer.from("x"), bad), false, JSON.stringify(bad));
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
console.log("\n── 解包 ──");
|
|
158
|
+
|
|
159
|
+
test("npm 包的 package/ 前缀能 strip 掉,目录与文件都还原", () => {
|
|
160
|
+
const entries = extractTarball(npmTarball(), { stripComponents: 1 });
|
|
161
|
+
const paths = entries.map((e) => e.path).sort();
|
|
162
|
+
assert.deepEqual(paths, ["client/client.js", "lib/index.js", "package.json"]);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("带 ../ 的路径直接拒绝(不解出目录穿越)", () => {
|
|
166
|
+
const evil = makeTarGz([{ name: "package/../../etc/passwd", content: "x" }]);
|
|
167
|
+
assert.throws(() => extractTarball(evil, { stripComponents: 1 }), /不安全路径/);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("writeEntries 再兜一层:不接受写到目标目录之外", () => {
|
|
171
|
+
const dir = tempDir();
|
|
172
|
+
try {
|
|
173
|
+
assert.throws(() => writeEntries(dir, [{ path: "../evil.txt", dir: false, mode: 0o644, data: Buffer.from("x") }]), /目录之外/);
|
|
174
|
+
} finally {
|
|
175
|
+
rmSync(dir, { recursive: true, force: true });
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("写盘保留可执行位(脚本类文件不会丢权限)", () => {
|
|
180
|
+
const dir = tempDir();
|
|
181
|
+
try {
|
|
182
|
+
const entries = extractTarball(makeTarGz([{ name: "package/run.sh", content: "#!/bin/sh\n", mode: 0o755 }]), { stripComponents: 1 });
|
|
183
|
+
writeEntries(dir, entries);
|
|
184
|
+
const mode = statSync(join(dir, "run.sh")).mode & 0o777;
|
|
185
|
+
assert.equal(mode, 0o755);
|
|
186
|
+
} finally {
|
|
187
|
+
rmSync(dir, { recursive: true, force: true });
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
console.log("\n── 差异分类:该重启还是该重载 ──");
|
|
192
|
+
|
|
193
|
+
test("只改 client → 重载界面就够,不要求重启", () => {
|
|
194
|
+
const root = tempDir();
|
|
195
|
+
try {
|
|
196
|
+
const oldDir = seedPlugin(join(root, "old"));
|
|
197
|
+
const newDir = seedPlugin(join(root, "new"), { version: "0.5.8", lib: "old", client: "new" });
|
|
198
|
+
const r = collectChanges(oldDir, newDir);
|
|
199
|
+
assert.equal(r.client, true);
|
|
200
|
+
assert.equal(r.host, false);
|
|
201
|
+
assert.equal(r.reloadRequired, true);
|
|
202
|
+
assert.equal(r.restartRequired, false);
|
|
203
|
+
} finally {
|
|
204
|
+
rmSync(root, { recursive: true, force: true });
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("改了 lib → 必须重启", () => {
|
|
209
|
+
const root = tempDir();
|
|
210
|
+
try {
|
|
211
|
+
const oldDir = seedPlugin(join(root, "old"));
|
|
212
|
+
const newDir = seedPlugin(join(root, "new"), { version: "0.5.8", lib: "new", client: "old" });
|
|
213
|
+
const r = collectChanges(oldDir, newDir);
|
|
214
|
+
assert.equal(r.host, true);
|
|
215
|
+
assert.equal(r.restartRequired, true);
|
|
216
|
+
assert.equal(r.reloadRequired, false);
|
|
217
|
+
} finally {
|
|
218
|
+
rmSync(root, { recursive: true, force: true });
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test("只改了版本号 → 建议重启(否则进程里的 VERSION 一直是旧的)", () => {
|
|
223
|
+
const root = tempDir();
|
|
224
|
+
try {
|
|
225
|
+
const oldDir = seedPlugin(join(root, "old"));
|
|
226
|
+
const newDir = seedPlugin(join(root, "new"), { version: "0.5.8", lib: "old", client: "old" });
|
|
227
|
+
const r = collectChanges(oldDir, newDir);
|
|
228
|
+
assert.equal(r.host, false);
|
|
229
|
+
assert.equal(r.client, false);
|
|
230
|
+
assert.equal(r.packageJson, true);
|
|
231
|
+
assert.equal(r.restartRequired, true);
|
|
232
|
+
assert.equal(r.reloadRequired, false);
|
|
233
|
+
} finally {
|
|
234
|
+
rmSync(root, { recursive: true, force: true });
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test("只改文档/脚本 → 既不需要重启也不需要重载", () => {
|
|
239
|
+
const root = tempDir();
|
|
240
|
+
try {
|
|
241
|
+
const oldDir = seedPlugin(join(root, "old"));
|
|
242
|
+
const newDir = seedPlugin(join(root, "new"), { lib: "old", client: "old" });
|
|
243
|
+
writeFileSync(join(newDir, "README.md"), "new docs\n");
|
|
244
|
+
const r = collectChanges(oldDir, newDir);
|
|
245
|
+
assert.equal(r.restartRequired, false);
|
|
246
|
+
assert.equal(r.reloadRequired, false);
|
|
247
|
+
assert.equal(r.other, true);
|
|
248
|
+
} finally {
|
|
249
|
+
rmSync(root, { recursive: true, force: true });
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
console.log("\n── 版本元数据 / 下载 ──");
|
|
254
|
+
|
|
255
|
+
const META_URL = "https://registry.npmjs.org/dsh-plugin-mesh/0.5.8";
|
|
256
|
+
const MIRROR_META_URL = "https://registry.npmmirror.com/dsh-plugin-mesh/0.5.8";
|
|
257
|
+
const TARBALL_URL = "https://registry.npmjs.org/dsh-plugin-mesh/-/dsh-plugin-mesh-0.5.8.tgz";
|
|
258
|
+
|
|
259
|
+
await testAsync("官方源成功 → 拿到 tarball 与 integrity", async () => {
|
|
260
|
+
const tar = npmTarball();
|
|
261
|
+
const meta = {
|
|
262
|
+
version: "0.5.8",
|
|
263
|
+
dist: { tarball: TARBALL_URL, integrity: integrityOf(tar) },
|
|
264
|
+
};
|
|
265
|
+
const r = await fetchVersionMeta({
|
|
266
|
+
version: "0.5.8",
|
|
267
|
+
fetchImpl: async (url) => (url === META_URL ? jsonRes(meta) : jsonRes({ error: "no" }, 404)),
|
|
268
|
+
});
|
|
269
|
+
assert.equal(r.ok, true);
|
|
270
|
+
assert.equal(r.tarball, TARBALL_URL);
|
|
271
|
+
assert.equal(r.integrity, integrityOf(tar));
|
|
272
|
+
assert.equal(r.source, "https://registry.npmjs.org");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
await testAsync("官方源 500 → 镜像兜底;两边都挂则逐条报出原因", async () => {
|
|
276
|
+
const tar = npmTarball();
|
|
277
|
+
const ok = await fetchVersionMeta({
|
|
278
|
+
version: "0.5.8",
|
|
279
|
+
fetchImpl: async (url) => {
|
|
280
|
+
if (url === META_URL) return jsonRes({ error: "boom" }, 500);
|
|
281
|
+
return jsonRes({ version: "0.5.8", dist: { tarball: TARBALL_URL, integrity: integrityOf(tar) } });
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
assert.equal(ok.ok, true);
|
|
285
|
+
assert.equal(ok.source, "https://registry.npmmirror.com");
|
|
286
|
+
|
|
287
|
+
const bad = await fetchVersionMeta({
|
|
288
|
+
version: "0.5.8",
|
|
289
|
+
fetchImpl: async (url) => {
|
|
290
|
+
if (url === META_URL) throw new Error("ENOTFOUND");
|
|
291
|
+
return jsonRes({}, 502);
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
assert.equal(bad.ok, false);
|
|
295
|
+
assert.equal(bad.failures.length, 2, JSON.stringify(bad.failures));
|
|
296
|
+
assert.ok(bad.failures[0].includes("ENOTFOUND"));
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
await testAsync("镜像滞后(返回别的版本)/ 缺 integrity → 不算成功", async () => {
|
|
300
|
+
const stale = await fetchVersionMeta({
|
|
301
|
+
version: "0.5.8",
|
|
302
|
+
bases: ["https://registry.npmmirror.com"],
|
|
303
|
+
fetchImpl: async () => jsonRes({ version: "0.5.7", dist: { tarball: TARBALL_URL, integrity: "sha512-AAAA" } }),
|
|
304
|
+
});
|
|
305
|
+
assert.equal(stale.ok, false);
|
|
306
|
+
const noIntegrity = await fetchVersionMeta({
|
|
307
|
+
version: "0.5.8",
|
|
308
|
+
bases: ["https://registry.npmjs.org"],
|
|
309
|
+
fetchImpl: async () => jsonRes({ version: "0.5.8", dist: { tarball: TARBALL_URL } }),
|
|
310
|
+
});
|
|
311
|
+
assert.equal(noIntegrity.ok, false);
|
|
312
|
+
assert.ok(noIntegrity.error.includes("integrity"), noIntegrity.error);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
await testAsync("下载校验:内容与 integrity 不一致就丢弃", async () => {
|
|
316
|
+
const tar = npmTarball();
|
|
317
|
+
const good = await downloadTarball({
|
|
318
|
+
tarball: TARBALL_URL,
|
|
319
|
+
integrity: integrityOf(tar),
|
|
320
|
+
fetchImpl: async () => bufRes(tar),
|
|
321
|
+
});
|
|
322
|
+
assert.equal(good.ok, true);
|
|
323
|
+
|
|
324
|
+
const tampered = Buffer.from(tar);
|
|
325
|
+
tampered[tampered.length - 1] ^= 0xff;
|
|
326
|
+
const bad = await downloadTarball({
|
|
327
|
+
tarball: TARBALL_URL,
|
|
328
|
+
integrity: integrityOf(tar),
|
|
329
|
+
fetchImpl: async () => bufRes(tampered),
|
|
330
|
+
});
|
|
331
|
+
assert.equal(bad.ok, false);
|
|
332
|
+
assert.ok(bad.error.includes("校验失败"), bad.error);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
await testAsync("下载超过上限 / HTTP 错误 → 明确拒绝", async () => {
|
|
336
|
+
const tooBig = await downloadTarball({
|
|
337
|
+
tarball: TARBALL_URL,
|
|
338
|
+
integrity: "sha512-AAAA",
|
|
339
|
+
maxBytes: 8,
|
|
340
|
+
fetchImpl: async () => bufRes(Buffer.from("0123456789")),
|
|
341
|
+
});
|
|
342
|
+
assert.equal(tooBig.ok, false);
|
|
343
|
+
assert.ok(tooBig.error.includes("太大"), tooBig.error);
|
|
344
|
+
|
|
345
|
+
const http = await downloadTarball({
|
|
346
|
+
tarball: TARBALL_URL,
|
|
347
|
+
integrity: "sha512-AAAA",
|
|
348
|
+
fetchImpl: async () => jsonRes({}, 404),
|
|
349
|
+
});
|
|
350
|
+
assert.equal(http.ok, false);
|
|
351
|
+
assert.ok(http.error.includes("404"), http.error);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
console.log("\n── 安装:失败必须保留旧版 ──");
|
|
355
|
+
|
|
356
|
+
/** 把安装流程要用的假 fetch 组装好。 */
|
|
357
|
+
function fakeUpgradeFetch({ version = "0.5.8", tar = npmTarball({ version }), badTar = null } = {}) {
|
|
358
|
+
return async (url) => {
|
|
359
|
+
if (url === META_URL) {
|
|
360
|
+
const meta = { version, dist: { tarball: TARBALL_URL, integrity: integrityOf(tar) } };
|
|
361
|
+
return jsonRes(meta);
|
|
362
|
+
}
|
|
363
|
+
if (url === TARBALL_URL) return bufRes(badTar ?? tar);
|
|
364
|
+
if (url === MIRROR_META_URL) return jsonRes({ error: "not found" }, 404);
|
|
365
|
+
throw new Error(`unexpected url: ${url}`);
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
async function withPlugin(fn) {
|
|
370
|
+
const root = tempDir();
|
|
371
|
+
try {
|
|
372
|
+
const pluginDir = seedPlugin(join(root, "node_modules", PACKAGE_NAME));
|
|
373
|
+
const markerPath = join(root, "upgrade.json");
|
|
374
|
+
return await fn({ root, pluginDir, markerPath });
|
|
375
|
+
} finally {
|
|
376
|
+
rmSync(root, { recursive: true, force: true });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
await testAsync("成功:替换文件、留备份、写待重启标记", () =>
|
|
381
|
+
withPlugin(async ({ pluginDir, markerPath }) => {
|
|
382
|
+
const r = await installUpdate({
|
|
383
|
+
version: "0.5.8",
|
|
384
|
+
current: "0.5.7",
|
|
385
|
+
pluginDir,
|
|
386
|
+
markerPath,
|
|
387
|
+
fetchImpl: fakeUpgradeFetch(),
|
|
388
|
+
checkLoadable: async () => ({ ok: true }),
|
|
389
|
+
now: 1700000000000,
|
|
390
|
+
});
|
|
391
|
+
assert.equal(r.ok, true, r.error);
|
|
392
|
+
assert.equal(r.installed, true);
|
|
393
|
+
assert.equal(r.restartRequired, true);
|
|
394
|
+
assert.ok(r.backupDir && existsSync(r.backupDir), "旧版备份应存在");
|
|
395
|
+
assert.equal(JSON.parse(readFileSync(join(pluginDir, "package.json"), "utf8")).version, "0.5.8");
|
|
396
|
+
assert.equal(readFileSync(join(pluginDir, "lib", "index.js"), "utf8"), 'export const marker = "new";\n');
|
|
397
|
+
const marker = JSON.parse(readFileSync(markerPath, "utf8"));
|
|
398
|
+
assert.equal(marker.version, "0.5.8");
|
|
399
|
+
assert.equal(marker.restartRequired, true);
|
|
400
|
+
assert.equal(readdirSync(dirname(pluginDir)).filter((n) => n.startsWith(".mesh-upgrade-staging-")).length, 0, "staging 要清掉");
|
|
401
|
+
}));
|
|
402
|
+
|
|
403
|
+
await testAsync("只改客户端:安装成功但 restartRequired=false", () =>
|
|
404
|
+
withPlugin(async ({ pluginDir }) => {
|
|
405
|
+
const tar = npmTarball({ client: "new", lib: "old" });
|
|
406
|
+
const r = await installUpdate({
|
|
407
|
+
version: "0.5.8",
|
|
408
|
+
current: "0.5.7",
|
|
409
|
+
pluginDir,
|
|
410
|
+
fetchImpl: fakeUpgradeFetch({ tar }),
|
|
411
|
+
checkLoadable: async () => ({ ok: true }),
|
|
412
|
+
now: 1700000000001,
|
|
413
|
+
});
|
|
414
|
+
assert.equal(r.ok, true, r.error);
|
|
415
|
+
assert.equal(r.restartRequired, false);
|
|
416
|
+
assert.equal(r.reloadRequired, true);
|
|
417
|
+
}));
|
|
418
|
+
|
|
419
|
+
await testAsync("可加载检查失败:旧版原封不动,不留备份/staging", () =>
|
|
420
|
+
withPlugin(async ({ pluginDir, root }) => {
|
|
421
|
+
const before = readFileSync(join(pluginDir, "lib", "index.js"), "utf8");
|
|
422
|
+
const r = await installUpdate({
|
|
423
|
+
version: "0.5.8",
|
|
424
|
+
current: "0.5.7",
|
|
425
|
+
pluginDir,
|
|
426
|
+
fetchImpl: fakeUpgradeFetch(),
|
|
427
|
+
checkLoadable: async () => ({ ok: false, error: "Cannot find package '@deepseek-ai/dsh-brand'" }),
|
|
428
|
+
now: 1700000000002,
|
|
429
|
+
});
|
|
430
|
+
assert.equal(r.ok, false);
|
|
431
|
+
assert.equal(r.stage, "loadable");
|
|
432
|
+
assert.ok(r.error.includes("不可加载"), r.error);
|
|
433
|
+
assert.equal(readFileSync(join(pluginDir, "lib", "index.js"), "utf8"), before);
|
|
434
|
+
assert.equal(readdirSync(dirname(pluginDir)).filter((n) => n.startsWith(".mesh-upgrade-backup-")).length, 0, "不该产生备份(还没交换)");
|
|
435
|
+
assert.equal(readdirSync(dirname(pluginDir)).filter((n) => n.startsWith(".mesh-upgrade-staging-")).length, 0, "staging 要清掉");
|
|
436
|
+
}));
|
|
437
|
+
|
|
438
|
+
await testAsync("哈希不符:不落地、不备份,报下载阶段失败", () =>
|
|
439
|
+
withPlugin(async ({ pluginDir, root }) => {
|
|
440
|
+
const tar = npmTarball();
|
|
441
|
+
const bad = Buffer.from(tar);
|
|
442
|
+
bad[bad.length - 1] ^= 0xff;
|
|
443
|
+
const r = await installUpdate({
|
|
444
|
+
version: "0.5.8",
|
|
445
|
+
current: "0.5.7",
|
|
446
|
+
pluginDir,
|
|
447
|
+
fetchImpl: fakeUpgradeFetch({ tar, badTar: bad }),
|
|
448
|
+
checkLoadable: async () => ({ ok: true }),
|
|
449
|
+
now: 1700000000003,
|
|
450
|
+
});
|
|
451
|
+
assert.equal(r.ok, false);
|
|
452
|
+
assert.equal(r.stage, "download");
|
|
453
|
+
assert.ok(r.error.includes("校验失败"), r.error);
|
|
454
|
+
assert.equal(readdirSync(dirname(pluginDir)).filter((n) => n.startsWith(".mesh-upgrade-")).length, 0);
|
|
455
|
+
}));
|
|
456
|
+
|
|
457
|
+
await testAsync("目标版本不比当前新 / 版本号非法:直接拒绝,不碰磁盘", () =>
|
|
458
|
+
withPlugin(async ({ pluginDir }) => {
|
|
459
|
+
const same = await installUpdate({ version: "0.5.7", current: "0.5.7", pluginDir, fetchImpl: fakeUpgradeFetch() });
|
|
460
|
+
assert.equal(same.ok, false);
|
|
461
|
+
assert.equal(same.stage, "input");
|
|
462
|
+
const older = await installUpdate({ version: "0.5.6", current: "0.5.7", pluginDir, fetchImpl: fakeUpgradeFetch() });
|
|
463
|
+
assert.equal(older.ok, false);
|
|
464
|
+
const weird = await installUpdate({ version: "latest", current: "0.5.7", pluginDir, fetchImpl: fakeUpgradeFetch() });
|
|
465
|
+
assert.equal(weird.ok, false);
|
|
466
|
+
}));
|
|
467
|
+
|
|
468
|
+
await testAsync("包身份不符(版本/名字对不上):拒绝覆盖", () =>
|
|
469
|
+
withPlugin(async ({ pluginDir }) => {
|
|
470
|
+
const tar = npmTarball({ version: "9.9.9" });
|
|
471
|
+
const r = await installUpdate({
|
|
472
|
+
version: "0.5.8",
|
|
473
|
+
current: "0.5.7",
|
|
474
|
+
pluginDir,
|
|
475
|
+
fetchImpl: fakeUpgradeFetch({ tar }),
|
|
476
|
+
checkLoadable: async () => ({ ok: true }),
|
|
477
|
+
now: 1700000000004,
|
|
478
|
+
});
|
|
479
|
+
assert.equal(r.ok, false);
|
|
480
|
+
assert.equal(r.stage, "validate");
|
|
481
|
+
assert.equal(JSON.parse(readFileSync(join(pluginDir, "package.json"), "utf8")).version, "0.5.7");
|
|
482
|
+
}));
|
|
483
|
+
|
|
484
|
+
await testAsync("内容完全一样:报 up-to-date,不覆盖、不备份", () =>
|
|
485
|
+
withPlugin(async ({ pluginDir, root }) => {
|
|
486
|
+
// 旧版内容与"新版"一致,仅版本号相同才无差异;这里让旧版也标记 new
|
|
487
|
+
writeFileSync(join(pluginDir, "lib", "index.js"), 'export const marker = "new";\n');
|
|
488
|
+
writeFileSync(join(pluginDir, "client", "client.js"), "// new\n");
|
|
489
|
+
writeFileSync(join(pluginDir, "package.json"), JSON.stringify({ name: PACKAGE_NAME, version: "0.5.8" }, null, 2));
|
|
490
|
+
const r = await installUpdate({
|
|
491
|
+
version: "0.5.8",
|
|
492
|
+
current: "0.5.7",
|
|
493
|
+
pluginDir,
|
|
494
|
+
fetchImpl: fakeUpgradeFetch(),
|
|
495
|
+
checkLoadable: async () => ({ ok: true }),
|
|
496
|
+
now: 1700000000005,
|
|
497
|
+
});
|
|
498
|
+
assert.equal(r.ok, true, r.error);
|
|
499
|
+
assert.equal(r.installed, false);
|
|
500
|
+
assert.equal(r.upToDate, true);
|
|
501
|
+
assert.equal(readdirSync(dirname(pluginDir)).filter((n) => n.startsWith(".mesh-upgrade-backup-")).length, 0);
|
|
502
|
+
}));
|
|
503
|
+
|
|
504
|
+
console.log("\n── 备份清理 / 请求守卫 / 重启入口 ──");
|
|
505
|
+
|
|
506
|
+
test("只保留最近 3 份本工具创建的备份", () => {
|
|
507
|
+
const root = tempDir();
|
|
508
|
+
try {
|
|
509
|
+
for (let i = 1; i <= 5; i += 1) mkdirSync(join(root, `.mesh-upgrade-backup-0.5.${i}-${i}`));
|
|
510
|
+
mkdirSync(join(root, "unrelated"));
|
|
511
|
+
pruneBackups(root, 3);
|
|
512
|
+
const left = readdirSync(root).filter((n) => n.startsWith(".mesh-upgrade-backup-")).sort();
|
|
513
|
+
assert.equal(left.length, 3, left.join(","));
|
|
514
|
+
assert.ok(readdirSync(root).includes("unrelated"), "不相干的目录不能删");
|
|
515
|
+
} finally {
|
|
516
|
+
rmSync(root, { recursive: true, force: true });
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
test("控制请求只认同源回环:转发头 / 非回环 / Origin 不符全部拒绝", () => {
|
|
521
|
+
const req = (over = {}) => ({
|
|
522
|
+
socket: { remoteAddress: "127.0.0.1" },
|
|
523
|
+
headers: { host: "127.0.0.1:43120", origin: "http://127.0.0.1:43120" },
|
|
524
|
+
...over,
|
|
525
|
+
});
|
|
526
|
+
assert.equal(trustedControlRequest(req()), true);
|
|
527
|
+
assert.equal(trustedControlRequest(req({ socket: { remoteAddress: "192.168.1.9" } })), false);
|
|
528
|
+
assert.equal(
|
|
529
|
+
trustedControlRequest(req({ headers: { host: "127.0.0.1:43120", origin: "http://127.0.0.1:43120", "x-forwarded-for": "1.2.3.4" } })),
|
|
530
|
+
false,
|
|
531
|
+
);
|
|
532
|
+
assert.equal(trustedControlRequest(req({ headers: { host: "127.0.0.1:43120" } })), false);
|
|
533
|
+
assert.equal(
|
|
534
|
+
trustedControlRequest(req({ headers: { host: "127.0.0.1:43120", origin: "http://evil.example" } })),
|
|
535
|
+
false,
|
|
536
|
+
);
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
await testAsync("有 desktopSettingsController 就调它的 restart(并触发 afterResponse)", async () => {
|
|
540
|
+
let called = 0;
|
|
541
|
+
const ctx = {
|
|
542
|
+
get: (name) =>
|
|
543
|
+
name === "desktopSettingsController"
|
|
544
|
+
? {
|
|
545
|
+
restart: () => {
|
|
546
|
+
called += 1;
|
|
547
|
+
return { afterResponse: () => (called += 1) };
|
|
548
|
+
},
|
|
549
|
+
}
|
|
550
|
+
: undefined,
|
|
551
|
+
};
|
|
552
|
+
const r = await requestHostRestart(ctx);
|
|
553
|
+
assert.equal(r.ok, true);
|
|
554
|
+
assert.equal(r.mode, "desktop");
|
|
555
|
+
assert.equal(called, 2, "restart 与 afterResponse 都要被调用");
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
test("有 desktopSettingsController.reloadRenderer 就调它(内置重载入口)", () => {
|
|
559
|
+
let called = 0;
|
|
560
|
+
const ctx = {
|
|
561
|
+
get: (name) =>
|
|
562
|
+
name === "desktopSettingsController"
|
|
563
|
+
? {
|
|
564
|
+
reloadRenderer: () => {
|
|
565
|
+
called += 1;
|
|
566
|
+
return { afterResponse: () => (called += 1) };
|
|
567
|
+
},
|
|
568
|
+
}
|
|
569
|
+
: undefined,
|
|
570
|
+
};
|
|
571
|
+
const r = requestHostReload(ctx);
|
|
572
|
+
assert.equal(r.ok, true);
|
|
573
|
+
assert.equal(r.mode, "desktop");
|
|
574
|
+
assert.equal(called, 2, "reloadRenderer 与 afterResponse 都要被调用");
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
test("没有内置重载入口 → mode:self(由页面自己 reload,不假装做了事)", () => {
|
|
578
|
+
const r = requestHostReload({ get: () => undefined });
|
|
579
|
+
assert.equal(r.ok, false);
|
|
580
|
+
assert.equal(r.mode, "self");
|
|
581
|
+
assert.ok(r.error.includes("页面自身重载"), r.error);
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
await testAsync("没有桌面控制器但有 web 端口 → 退到 dshmarket 的自重启(同源回环请求)", async () => {
|
|
585
|
+
let seen = null;
|
|
586
|
+
const ctx = { get: () => undefined, webServer: { port: 43120 } };
|
|
587
|
+
const r = await requestHostRestart(ctx, {
|
|
588
|
+
fetchImpl: async (url, init) => {
|
|
589
|
+
seen = { url, init };
|
|
590
|
+
return { ok: true, status: 200, text: async () => "{}" };
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
assert.equal(r.ok, true);
|
|
594
|
+
assert.equal(r.mode, "market");
|
|
595
|
+
assert.equal(seen.url, "http://127.0.0.1:43120/dsh-market/api/v1/restart");
|
|
596
|
+
assert.equal(seen.init.headers.host, "127.0.0.1:43120");
|
|
597
|
+
assert.equal(seen.init.headers.origin, "http://127.0.0.1:43120");
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
await testAsync("市场自重启也不可用时:说清两条路都失败,并给手动重启", async () => {
|
|
601
|
+
const ctx = { get: () => undefined, webServer: { port: 43120 } };
|
|
602
|
+
const r = await requestHostRestart(ctx, {
|
|
603
|
+
fetchImpl: async () => ({ ok: false, status: 404, text: async () => "not found" }),
|
|
604
|
+
});
|
|
605
|
+
assert.equal(r.ok, false);
|
|
606
|
+
assert.equal(r.mode, "manual");
|
|
607
|
+
assert.ok(r.error.includes("404"), r.error);
|
|
608
|
+
assert.ok(r.error.includes("手动重启"), r.error);
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
await testAsync("既没有桌面控制器、也没有 web 端口 → 直接说手动重启(不假装成功)", async () => {
|
|
612
|
+
const r = await requestHostRestart({ get: () => undefined });
|
|
613
|
+
assert.equal(r.ok, false);
|
|
614
|
+
assert.equal(r.mode, "manual");
|
|
615
|
+
assert.ok(r.error.includes("手动重启"), r.error);
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
console.log(`\n${failed === 0 ? "全部通过" : "有失败"}:${passed} 通过 / ${failed} 失败`);
|
|
619
|
+
process.exit(failed === 0 ? 0 : 1);
|