Repository navigation
Expand file tree
/
Copy pathplugin-updater.cjs
More file actions
563 lines (550 loc) · 32.3 KB
/
Copy pathplugin-updater.cjs
File metadata and controls
563 lines (550 loc) · 32.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
"use strict";
const fs = require("fs");
const path = require("path");
const os = require("os");
const { execFile, spawn } = require("child_process");
const crypto = require("crypto");
const { isDeepStrictEqual } = require("util");
const releaseChannel = require("./plugin-release.cjs");
const { resolvePluginStorage } = require("./plugin-storage.cjs");
const PLUGIN_ID = "zcode-token-usage-statusbar@zusage-marketplace";
const INTERVAL_MS = 60 * 60 * 1000;
function compareVersions(a, b) {
const parse = (v) => /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(v || "");
const aa = parse(a), bb = parse(b);
if (!aa || !bb) throw new Error("插件版本号缺失或不是语义版本,无法确认更新状态");
for (let i = 1; i <= 3; i++) {
if (Number(aa[i]) !== Number(bb[i])) return Number(aa[i]) > Number(bb[i]) ? 1 : -1;
}
if (!aa[4] && !bb[4]) return 0;
if (!aa[4]) return 1;
if (!bb[4]) return -1;
const ap = aa[4].split("."), bp = bb[4].split(".");
for (let i = 0; i < Math.max(ap.length, bp.length); i++) {
if (ap[i] === bp[i]) continue;
if (ap[i] === undefined) return -1;
if (bp[i] === undefined) return 1;
const an = /^\d+$/.test(ap[i]), bn = /^\d+$/.test(bp[i]);
if (an && bn) return Number(ap[i]) > Number(bp[i]) ? 1 : -1;
if (an !== bn) return an ? -1 : 1;
return ap[i] > bp[i] ? 1 : -1;
}
return 0;
}
function readJson(file, fallback) {
try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch (e) { return fallback; }
}
function readConfig(file) {
try {
const value = JSON.parse(fs.readFileSync(file, "utf8"));
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
} catch (e) { return e.code === "ENOENT" ? {} : null; }
}
function writeJson(file, value) {
const tmp = file + "." + process.pid + ".tmp";
try {
fs.writeFileSync(tmp, JSON.stringify(value, null, 2) + "\n");
fs.renameSync(tmp, file);
} finally {
try { fs.unlinkSync(tmp); } catch (e) { }
}
}
function runFile(command, args, options, input, onSpawn) {
return new Promise((resolve, reject) => {
const child = execFile(command, args, options, (error, stdout, stderr) => {
let data;
try { data = JSON.parse(stdout); } catch (e) { }
if (error) {
const detail = data && data.diagnostics && data.diagnostics.map((d) => d.message).join("; ");
reject(new Error(detail || String(stderr || error.message).trim().slice(0, 1000)));
} else if (!data) reject(new Error("插件管理器没有返回有效 JSON"));
else resolve(data);
});
if (onSpawn) {
try { if (child.pid) onSpawn(child.pid); }
catch (e) { child.kill(); reject(e); }
}
if (input !== undefined) { child.stdin.on("error", () => {}); child.stdin.end(input); }
});
}
async function updateConfig(runtime, changes, python, defaults) {
const result = await runFile(python, [path.join(__dirname, "runtime_lock.py"), "--update-config", runtime], {
windowsHide: true, timeout: 15000, maxBuffer: 1024 * 1024,
}, JSON.stringify({ changes, defaults }));
if (result.ok !== true || typeof result.changed !== "boolean") throw new Error("配置更新没有返回有效确认");
return result;
}
function readState(file, currentTime) {
const value = readJson(file, {});
const saved = value && typeof value === "object" && !Array.isArray(value) ? value : {};
const timestamp = (v) => Number.isSafeInteger(v) && v >= 0 && v <= currentTime ? v : 0;
let pendingVersion = "";
try { if (typeof saved.pendingVersion === "string") { compareVersions(saved.pendingVersion, saved.pendingVersion); pendingVersion = saved.pendingVersion; } } catch (e) { }
return { checkedAt: timestamp(saved.checkedAt), lastAttempt: timestamp(saved.lastAttempt),
pendingSync: saved.pendingSync === true, pendingVersion,
pendingPid: Number.isSafeInteger(saved.pendingPid) && saved.pendingPid > 0 ? saved.pendingPid : 0,
pendingRelease: saved.pendingRelease === undefined || saved.pendingRelease === null ? null : saved.pendingRelease };
}
function legacyLockBusy(runtime) {
// An already-running wx updater cannot honor v2 locks. Respect its live owner,
// never unlink its file, and retire old updater processes by restarting ZCode.
const file = path.join(runtime, "plugin-update.lock");
try {
const owner = readJson(file, {});
if (owner && Number.isSafeInteger(owner.pid) && owner.pid > 0) {
try { process.kill(owner.pid, 0); return true; }
catch (e) { return e.code !== "ESRCH"; }
}
return Date.now() - fs.statSync(file).mtimeMs < 600000;
} catch (e) { if (e.code === "ENOENT") return false; throw e; }
}
function acquireUpdateLock(runtime, python, helper) {
return new Promise((resolve, reject) => {
const child = spawn(python, [helper, "--hold", path.join(runtime, "plugin-update-v2.lock")], {
windowsHide: true, stdio: ["pipe", "pipe", "pipe"],
});
let settled = false, output = "", detail = "";
const timer = setTimeout(() => fail(new Error("插件更新锁未返回就绪状态")), 10000);
const fail = (error) => {
if (settled) return;
settled = true; clearTimeout(timer); child.stdin.destroy(); child.kill(); reject(error);
};
child.stdin.on("error", () => {});
child.once("error", fail);
child.stderr.on("data", (chunk) => { detail = (detail + chunk).slice(0, 1000); });
child.once("close", (code) => {
if (!settled) fail(new Error(detail.trim() || `插件更新锁进程提前退出 (${code})`));
});
child.stdout.on("data", (chunk) => {
if (settled) return;
output += chunk;
if (output.length > 1000) { fail(new Error("插件更新锁返回无效 JSON")); return; }
if (!output.includes("\n")) return;
let data;
try { data = JSON.parse(output.trim()); } catch (e) { fail(new Error("插件更新锁返回无效 JSON")); return; }
if (!data || typeof data.locked !== "boolean" || Object.keys(data).length !== 1) {
fail(new Error("插件更新锁返回无效 JSON")); return;
}
settled = true; clearTimeout(timer);
if (!data.locked) { child.stdin.end(); resolve(null); return; }
let releasing;
resolve(() => {
if (!releasing) releasing = new Promise((done) => {
if (child.exitCode !== null || child.signalCode !== null) { done(); return; }
child.once("close", done); child.stdin.end();
});
return releasing;
});
});
});
}
// 渠道身份只比 kind/repository/source;flavor 由 source 可推导,不参与比较,
// 这样旧版本持久化的更新意图(无 flavor 字段)在新代码下仍然等价,不会误判来源变化。
function sameChannel(a, b) {
return !!a && !!b && a.kind === b.kind && (a.repository || "") === (b.repository || "") &&
isDeepStrictEqual(a.source === undefined ? null : a.source, b.source === undefined ? null : b.source);
}
function channelFlavor(channel) {
// 持久化意图的渠道形态:注册为 GitCode git 来源即镜像渠道,其余远端一律按 GitHub 校验。
return !!(channel && channel.source && channel.source.source === "git" &&
releaseChannel.gitcodeRepository(channel.source.url)) ? "gitcode" : "github";
}
function verifyCommittedRelease(record, expected, currentChannel) {
const release = expected && expected.release, manifest = expected && expected.manifest;
if (!release || !manifest || !expected.channel || !sameChannel(currentChannel, expected.channel)) throw new Error("Release 更新意图无效或市场来源已变化,停止运行时同步");
const flavor = expected.channel.flavor || channelFlavor(expected.channel);
const validated = flavor === "gitcode"
? releaseChannel.validateGitcodeRelease(release)
: releaseChannel.validateRelease({ tag_name: release.tag, draft: false, prerelease: false,
assets: [release.archive, release.manifest] }, release.repository);
if (!validated || validated.version !== release.version || expected.channel.kind !== "release" ||
flavor !== validated.flavor ||
!releaseChannel.sameRepository(expected.channel.repository, release.repository)) throw new Error("Release 更新意图无效,停止运行时同步");
releaseChannel.validateManifest(manifest, validated);
if (record.version !== release.version || !record.source || !releaseChannel.sameAssetUrl(record.source.url, release.archive.browser_download_url) ||
record.source.sha256 !== manifest.asset.sha256 || record.source.source !== "url" || record.source.type !== "zip" ||
record.source.path !== "plugins/zcode-token-usage-statusbar" ||
(record.source.stripRoot !== undefined && record.source.stripRoot !== false) || record.source.headers !== undefined) throw new Error("官方安装结果与选定 Release 不一致,停止运行时同步");
}
function verifyRuntimeSync(options) {
const { runtime, pluginRoot, home = os.homedir(), env = process.env } = options;
const readObject = (file, missing) => {
let value;
try { value = JSON.parse(fs.readFileSync(file, "utf8")); }
catch (error) { if (error.code === "ENOENT") return missing; throw error; }
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("同步验收记录不是 JSON 对象: " + file);
return value;
};
const root = fs.realpathSync(pluginRoot);
const manifest = readObject(path.join(root, ".zcode-plugin", "plugin.json"), null);
if (!manifest || manifest.name !== PLUGIN_ID.split("@")[0]) throw new Error("同步插件清单身份无效");
compareVersions(manifest.version, manifest.version);
const pending = readObject(path.join(runtime, "plugin-update.json"), null);
const hasPending = pending && (pending.pendingSync === true || (pending.pendingRelease !== undefined && pending.pendingRelease !== null) || pending.pendingPid);
if (legacyLockBusy(runtime)) throw new Error("旧更新进程仍在运行,停止运行时同步");
if (pending && pending.pendingPid) {
if (!Number.isSafeInteger(pending.pendingPid) || pending.pendingPid <= 0) throw new Error("安装进程记录无效");
try { process.kill(pending.pendingPid, 0); throw new Error("插件安装进程仍在运行,停止运行时同步"); }
catch (error) { if (error.code !== "ESRCH") throw error; }
}
const storage = resolvePluginStorage(home, env);
if (fs.existsSync(path.join(storage, "marketplaces", ".zusage-marketplace.transaction.json"))) throw new Error("官方市场事务尚未完成,停止运行时同步");
const inventory = readObject(path.join(storage, "installed_plugins.json"), null);
if (inventory && !Array.isArray(inventory.plugins)) throw new Error("官方安装记录形状无效");
const records = inventory ? inventory.plugins.filter((row) => row && row.id === PLUGIN_ID && row.scope === "user") : [];
if (!records.length) {
if (hasPending || inventory) throw new Error("同步所需用户插件安装记录不存在");
return { ok: true, pluginRoot: root, version: manifest.version, identity: { record: null, channel: null, pending, manifest } };
}
if (records.length !== 1) throw new Error("同步所需用户插件安装记录不唯一");
const record = records[0];
if (record.enabled === false || typeof record.installPath !== "string" || fs.realpathSync(record.installPath) !== root || record.version !== manifest.version) throw new Error("同步插件与官方安装记录不一致或已禁用");
const selected = releaseChannel.readChannel(home, storage);
const currentChannel = { kind: selected.kind, repository: selected.repository || "", source: selected.marketplace ? selected.marketplace.source : null };
if (hasPending) {
if (pending.pendingSync !== true) throw new Error("待同步意图不完整,停止运行时同步");
if (pending.pendingVersion && compareVersions(record.version, pending.pendingVersion) <= 0) throw new Error("插件安装尚未提交新版,停止运行时同步");
if (pending.pendingRelease !== undefined && pending.pendingRelease !== null) verifyCommittedRelease(record, pending.pendingRelease, currentChannel);
else if (currentChannel.kind === "release") throw new Error("历史更新意图缺少 Release 校验信息,停止运行时同步");
}
return { ok: true, pluginRoot: root, version: manifest.version, identity: { record, channel: currentChannel, pending, manifest } };
}
function listOverview(data) {
// The official CLI answers different shapes by flag: plain `plugins list --json`
// returns a top-level array of installed entries (no scope field, per-entry
// diagnostics), while `--available` returns { installed, available, diagnostics }.
// Accept both so an absent plugin is a real absence, never a shape mismatch.
if (Array.isArray(data)) {
const diagnostics = [];
for (const entry of data) {
if (entry && entry.id === PLUGIN_ID && Array.isArray(entry.diagnostics)) diagnostics.push(...entry.diagnostics);
}
return { installed: data, available: [], diagnostics };
}
if (data && typeof data === "object") {
const installed = Array.isArray(data.installed) ? data.installed : null;
const available = Array.isArray(data.available) ? data.available : null;
// An envelope without either list is a shape we do not know; an empty list
// is a real absence. Never let shape drift masquerade as "not installed".
if (!installed && !available) throw new Error("插件管理器返回了无法识别的插件列表");
return { installed: installed || [], available: available || [],
diagnostics: Array.isArray(data.diagnostics) ? data.diagnostics : [] };
}
throw new Error("插件管理器返回了无法识别的插件列表");
}
function createUpdater(options) {
const { runtime, home, resourcesPath, executable, notify = () => {}, now = Date.now } = options;
const configFile = path.join(runtime, "config.json");
const stateFile = path.join(runtime, "plugin-update.json");
const environment = options.env || process.env;
let storageRoot = null, localError = "";
const storage = () => {
const root = resolvePluginStorage(home, environment);
if (storageRoot && storageRoot !== root) throw new Error("更新期间插件存储目录已变化,请重新启动更新器");
storageRoot = root;
return root;
};
const bundle = path.join(resourcesPath, "glm", "zcode.cjs");
const markerFile = path.join(runtime, ".plugin-restart-required");
const saved = readState(stateFile, now());
let state = { phase: "idle", supported: false, auto: false, currentVersion: "", latestVersion: "",
...saved, restartRequired: fs.existsSync(markerFile), skipped: [], error: "", channel: "", channelHost: "", releaseUrl: "", progress: null };
let busy = false;
let selectedRelease = null, selectedChannel = null;
const channel = () => {
const source = options.channel ? options.channel() : releaseChannel.readChannel(home, storage());
return { kind: source.kind, flavor: source.flavor || "", repository: source.repository || "", source: source.marketplace ? source.marketplace.source : null };
};
const fetchRelease = options.fetchRelease || releaseChannel.fetchBytes;
const prepareRelease = options.prepareRelease || ((release, onProgress) => releaseChannel.downloadRelease(release, python(), fetchRelease, undefined, onProgress));
const readReleaseCatalog = options.readReleaseCatalog || (() => {
const root = path.join(storage(), "marketplaces");
if (fs.existsSync(path.join(root, ".zusage-marketplace.transaction.json"))) throw new Error("官方插件市场事务尚未完成,请稍后重试");
return readJson(path.join(root, "zusage-marketplace", "marketplace.json"), null);
});
const python = () => process.env.ZUSAGE_PLUGIN_PYTHON || (readConfig(configFile) || {}).python_path || (process.platform === "win32" ? "python" : "python3");
const helper = path.join(__dirname, "runtime_lock.py");
const lock = options.acquireLock || (() => acquireUpdateLock(runtime, python(), helper));
const cli = options.cli || ((args, onSpawn) => runFile(executable, [bundle, "plugins", ...args, "--json"], {
cwd: home, env: { ...environment, HOME: home, USERPROFILE: home, ELECTRON_RUN_AS_NODE: "1", ZCODE_STORAGE_DIR: path.dirname(storage()) }, windowsHide: true,
timeout: 120000, maxBuffer: 4 * 1024 * 1024,
}, undefined, onSpawn));
const sync = options.sync || ((root) => runFile(python(), [
path.join(root, "overlay_install.py"), "sync",
], { cwd: home, env: { ...process.env, ZUSAGE_PLUGIN_RUNTIME: runtime },
windowsHide: true, timeout: 30000, maxBuffer: 1024 * 1024 }));
function installedRecord() {
const inventory = readJson(path.join(storage(), "installed_plugins.json"), {});
const records = inventory && inventory.plugins;
return Array.isArray(records) ? records.find((p) => p && p.id === PLUGIN_ID && p.scope === "user"
&& typeof p.installPath === "string" && p.installPath && typeof p.version === "string") : null;
}
function refreshLocal() {
const config = readConfig(configFile);
state.auto = !!config && (!Object.prototype.hasOwnProperty.call(config, "plugin_auto_update") || config.plugin_auto_update === true);
let record = null;
localError = "";
try { record = installedRecord(); } catch (error) { localError = error.message; }
state.supported = !!(record && record.installPath && (options.cli || fs.existsSync(bundle)));
state.currentVersion = record && record.version || "";
state.restartRequired = fs.existsSync(markerFile);
return record;
}
function publish() { state.restartRequired = fs.existsSync(markerFile); notify({ ...state, busy }); }
function load() { Object.assign(state, readState(stateFile, now())); }
function save() {
writeJson(stateFile, { checkedAt: state.checkedAt, lastAttempt: state.lastAttempt,
pendingSync: state.pendingSync, pendingVersion: state.pendingVersion, pendingPid: state.pendingPid, pendingRelease: state.pendingRelease });
}
function diagnostics(data) {
const errors = (data.diagnostics || []).filter((d) => d.severity === "error" && (!d.pluginId || d.pluginId === PLUGIN_ID));
if (errors.length) throw new Error(errors.map((d) => d.message).join("; "));
}
async function check(auto = false, install = false) {
if (busy) { if (!auto) publish(); return; }
refreshLocal();
if (!state.supported) { state.phase = localError ? "error" : "unavailable"; state.error = localError; publish(); return; }
busy = true;
// 手动检查要立刻有反馈:取锁、恢复待处理意图等前置步骤可能耗时数秒,不能让面板毫无动静。
if (!auto) { state.phase = "checking"; state.error = ""; publish(); }
let release;
try {
if (legacyLockBusy(runtime)) { if (!auto) throw new Error("另一个更新进程仍在运行,请稍后重试"); return; }
release = await lock();
if (!release || legacyLockBusy(runtime)) { if (!auto) throw new Error("另一个更新进程仍在运行,请稍后重试"); return; }
load();
if (state.pendingSync && await recoverPending()) return;
refreshLocal();
if (auto && (!state.auto || (state.lastAttempt && now() - state.lastAttempt < INTERVAL_MS))) return;
state.phase = "checking"; state.error = ""; publish();
state.lastAttempt = now();
save();
const source = channel();
state.channel = source.kind; state.channelHost = source.flavor === "gitcode" ? "gitcode" : source.flavor === "github" ? "github" : "";
state.releaseUrl = ""; selectedRelease = null; selectedChannel = source;
if (source.kind === "release" && source.flavor === "gitcode") {
// GitCode 渠道:宿主从注册 ref 拉取的渠道清单是唯一版本源,不调 GitCode API。
const market = await cli(["marketplace", "update", "zusage-marketplace"]);
diagnostics(market);
if (!isDeepStrictEqual(channel(), source)) throw new Error("更新期间市场来源已变化");
selectedRelease = releaseChannel.gitcodeReleaseFromCatalog(readReleaseCatalog(), source.repository);
} else if (source.kind === "release") {
selectedRelease = await releaseChannel.checkRelease(source.repository, compareVersions, fetchRelease);
}
if (selectedRelease) {
state.releaseUrl = selectedRelease.url;
state.latestVersion = selectedRelease.version;
const record = installedRecord();
if (!record) throw new Error("用户级插件安装记录不存在");
state.currentVersion = record.version;
const newer = compareVersions(state.latestVersion, record.version) > 0;
state.phase = newer ? "available" : "current"; state.checkedAt = now(); save(); publish();
if ((auto || install) && newer) {
const overview = listOverview(await cli(["list"])); diagnostics(overview);
const installed = (overview.installed || []).find((p) => p && p.id === PLUGIN_ID && (!p.scope || p.scope === "user"));
if (!installed) throw new Error("用户插件未注册,不能安装 Release");
if (installed.enabled !== false) await installUpdate(auto);
}
} else {
const market = await cli(["marketplace", "update", "zusage-marketplace"]);
diagnostics(market);
const overview = listOverview(await cli(["list", "--available"]));
diagnostics(overview);
if (!isDeepStrictEqual(channel(), source)) throw new Error("更新期间市场来源已变化");
const installed = (overview.installed || []).find((p) => p && p.id === PLUGIN_ID && (!p.scope || p.scope === "user"));
const available = (overview.available || []).find((p) => p && p.id === PLUGIN_ID);
if (!installed || !available) throw new Error("未找到已安装插件或市场中的插件,请检查市场来源");
state.currentVersion = installed.version || "";
state.latestVersion = available.version || "";
const newer = compareVersions(state.latestVersion, state.currentVersion) > 0;
state.phase = newer ? "available" : "current";
state.checkedAt = now(); save(); publish();
if ((auto || install) && newer && installed.enabled !== false) await installUpdate(auto);
}
} catch (e) {
state.phase = "error"; state.error = e.message; state.progress = null;
} finally {
if (release) await release();
busy = false; publish();
}
}
function clearPending() {
state.pendingSync = false; state.pendingVersion = ""; state.pendingPid = 0; state.pendingRelease = null;
save();
}
async function recoverPending() {
// Legacy pendingSync meant an acknowledged CLI commit. New intents are saved
// before launch and must prove a version change in official user inventory.
// A CLI orphaned by parent shutdown can still be writing inventory/runtime.
// Wait even after its version commit; normal completion clears its PID.
if (state.pendingPid) {
try { process.kill(state.pendingPid, 0); state.phase = "updating"; state.progress = { stage: "install" }; return true; }
catch (e) { if (e.code !== "ESRCH") { state.phase = "updating"; state.progress = { stage: "install" }; return true; } }
state.pendingPid = 0;
}
const record = installedRecord();
if (!record) throw new Error("更新恢复所需的安装记录不存在");
if (!state.pendingVersion || compareVersions(record.version, state.pendingVersion) > 0) {
if (!state.pendingRelease && channel().kind === "release") throw new Error("历史更新意图缺少 Release 校验信息,停止运行时同步");
await syncInstalled(); return true;
}
state.lastAttempt = 0;
clearPending();
state.phase = "idle";
return false;
}
async function installUpdate(auto) {
let record = refreshLocal();
if (!record) throw new Error("插件已卸载,更新已取消");
if (auto && !state.auto) return;
if (!isDeepStrictEqual(channel(), selectedChannel)) throw new Error("更新期间市场来源已变化");
compareVersions(record.version, record.version);
let downloaded = null;
// 中途放弃(插件被卸载/禁用、自动更新被关闭、版本已不再更新)时退回「发现新版本」,
// 不能让 downloading 阶段与进度条停在面板上。
const abandon = () => { state.phase = "available"; state.progress = null; };
try {
if (selectedRelease) {
const currentChannel = channel();
if (!isDeepStrictEqual(currentChannel, selectedChannel) || currentChannel.kind !== "release" || !releaseChannel.sameRepository(currentChannel.repository, selectedRelease.repository)) throw new Error("更新期间市场来源已变化");
state.phase = "downloading"; state.progress = { stage: "download", loaded: 0, total: selectedRelease.archive.size || 0 }; publish();
let lastPublished = 0;
downloaded = await prepareRelease(selectedRelease, (progress) => {
state.progress = progress;
// 下载字节进度按 ~150ms 节流推送;阶段切换与下载完成一律立即推送。
const complete = progress.stage !== "download" || (progress.total && progress.loaded >= progress.total);
if (complete || now() - lastPublished >= 150) { lastPublished = now(); publish(); }
});
releaseChannel.validateManifest(downloaded.manifest, selectedRelease);
state.progress = { stage: "verify" }; publish();
const market = await cli(["marketplace", "update", "zusage-marketplace"]); diagnostics(market);
if (!isDeepStrictEqual(channel(), currentChannel)) throw new Error("更新期间市场来源已变化");
record = refreshLocal();
if (!record) throw new Error("插件已卸载,更新已取消");
if (auto && !state.auto) { abandon(); return; }
if (compareVersions(selectedRelease.version, record.version) <= 0) { abandon(); return; }
const overview = listOverview(await cli(["list"])); diagnostics(overview);
const installed = (overview.installed || []).find((p) => p && p.id === PLUGIN_ID && (!p.scope || p.scope === "user"));
if (!installed) throw new Error("用户插件已卸载,Release 安装已取消");
if (installed.enabled === false) { abandon(); return; }
if (!isDeepStrictEqual(channel(), currentChannel)) throw new Error("更新期间市场来源已变化");
record = refreshLocal();
if (!record) throw new Error("插件已卸载,更新已取消");
if ((auto && !state.auto) || compareVersions(selectedRelease.version, record.version) <= 0) { abandon(); return; }
}
state.phase = "updating"; state.progress = { stage: "install" };
state.pendingSync = true; state.pendingVersion = record.version; state.pendingPid = 0;
state.pendingRelease = selectedRelease ? { release: selectedRelease, manifest: downloaded.manifest, channel: channel() } : null;
save(); publish();
const command = selectedRelease ? ["install", PLUGIN_ID, "--scope", "user"] : ["update", PLUGIN_ID];
try {
let result;
try { result = await cli(command, (pid) => { state.pendingPid = pid; save(); }); }
finally { state.pendingPid = 0; save(); }
diagnostics(result);
if (result.ok !== true) throw new Error("插件管理器未确认更新成功");
} catch (e) {
const committed = installedRecord();
if (committed && compareVersions(committed.version, state.pendingVersion) > 0) {
if (selectedRelease) verifyReleaseCommit(committed);
await syncInstalled(); return;
}
state.lastAttempt = 0; clearPending(); throw e;
}
const committed = installedRecord();
if (!committed || compareVersions(committed.version, state.pendingVersion) <= 0) {
state.lastAttempt = 0; clearPending(); throw new Error("插件管理器未提交新版安装记录");
}
if (selectedRelease) verifyReleaseCommit(committed);
await syncInstalled();
} finally { if (downloaded && downloaded.cleanup) downloaded.cleanup(); }
}
function verifyReleaseCommit(record) {
verifyCommittedRelease(record, state.pendingRelease, channel());
}
async function recordRestart(report) {
if (report.restartRecorded === true) return;
const changed = Array.isArray(report.changed) ? report.changed : [];
const cold = changed.filter((f) => f === "inject-main.cjs" || f === "plugin-updater.cjs" || f === "plugin-release.cjs" || f === "plugin-storage.cjs" || f === "quota-client.cjs"
|| ((readConfig(configFile) || {}).hot_reload === false && (f === "overlay.js" || f === "zusage.py")));
if (!cold.length) return;
const hashes = {};
for (const name of cold) {
try { hashes[name] = crypto.createHash("sha256").update(fs.readFileSync(path.join(runtime, name))).digest("hex"); }
catch (e) { if (e.code !== "ENOENT") throw e; }
}
await runFile(python(), [helper, "--record-restart", runtime], {
windowsHide: true, timeout: 15000, maxBuffer: 1024 * 1024,
}, JSON.stringify({ hashes, changed, version: state.currentVersion }));
}
async function syncInstalled() {
// 更新器只信任宿主安装记录,不接收页面提供的脚本路径。
const record = installedRecord();
if (!record || !record.installPath) throw new Error("更新后的安装记录不存在");
if (state.pendingRelease !== null) verifyReleaseCommit(record);
state.phase = "updating"; state.progress = { stage: "sync" }; publish();
const report = await sync(record.installPath);
if (!report || typeof report !== "object" || Array.isArray(report)) throw new Error("运行时同步没有返回有效报告");
state.currentVersion = record.version;
// Current packages persist the marker themselves. This also covers old
// packages' changed reports, before clearing durable pending coordination.
await recordRestart(report);
clearPending();
state.skipped = Array.isArray(report.preserved) ? report.preserved : [];
state.restartRequired = fs.existsSync(markerFile);
state.phase = "updated"; state.error = ""; state.progress = null;
}
async function request(req) {
if (!req) return;
if (req.action === "auto" && typeof req.enabled === "boolean") {
try {
await updateConfig(runtime, { plugin_auto_update: req.enabled }, python());
refreshLocal(); state.error = "";
publish();
if (req.enabled) await check(true);
} catch (e) { state.phase = "error"; state.error = e.message; publish(); }
} else if (req.action === "check") await check();
else if (req.action === "update") {
// 手动更新也先刷新版本,避免过期的“有新版”状态触发重装或降级。
await check(false, true);
}
}
async function tick() {
refreshLocal();
if (localError) { state.phase = "error"; state.error = localError; }
publish();
if (busy) return;
load();
if (state.supported && (state.pendingSync || (state.auto && (!state.lastAttempt || now() - state.lastAttempt >= INTERVAL_MS)))) await check(true);
}
refreshLocal();
return { request, tick, status: () => ({ ...state, busy }) };
}
async function runAutoCommand(argv) {
if (argv[0] !== "--auto") throw new Error("仅支持 --auto 后台更新模式");
const values = {};
for (let i = 1; i < argv.length; i += 2) {
if (!["--resources", "--runtime"].includes(argv[i]) || !argv[i + 1]) throw new Error("无效自动更新参数");
values[argv[i]] = argv[i + 1];
}
if (!values["--resources"] || !values["--runtime"]) throw new Error("缺少客户端或运行时目录");
const updater = createUpdater({ runtime: path.resolve(values["--runtime"]), home: os.homedir(),
resourcesPath: path.resolve(values["--resources"]), executable: process.execPath });
await updater.tick();
const state = updater.status();
process.stdout.write(JSON.stringify(state) + "\n");
if (state.phase === "error") process.exitCode = 1;
}
module.exports = { createUpdater, compareVersions, INTERVAL_MS, PLUGIN_ID, runAutoCommand, updateConfig, verifyRuntimeSync, listOverview };
if (require.main === module) {
const argv = process.argv.slice(2);
if (argv[0] === "--verify-runtime-sync") {
try {
if (argv.length !== 3) throw new Error("同步验收需要运行时与插件路径");
process.stdout.write(JSON.stringify(verifyRuntimeSync({ runtime: path.resolve(argv[1]), pluginRoot: path.resolve(argv[2]) })) + "\n");
} catch (error) { console.error("[zusage-plugin] 同步验收失败:", error.message); process.exitCode = 1; }
} else runAutoCommand(argv).catch((e) => {
console.error("[zusage-plugin] 自动更新失败:", e.message);
process.exitCode = 1;
});
}