dsh-plugin-yanyan-toolkit 0.0.0-stage → 1.2.0
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 +30 -0
- package/LICENSE +29 -0
- package/README.i18n.yaml +6 -0
- package/README.md +65 -2
- package/README.zh.md +66 -0
- package/cordis.patch.yml +35 -0
- package/icon.svg +8 -0
- package/lib/client.js +26 -0
- package/lib/index.js +44 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +79 -4
- package/packages/dsh-plugin-yanyan-subagent/README.md +72 -0
- package/packages/dsh-plugin-yanyan-subagent/README.zh.md +72 -0
- package/packages/dsh-plugin-yanyan-subagent/cordis.patch.yml +8 -0
- package/packages/dsh-plugin-yanyan-subagent/icon.svg +8 -0
- package/packages/dsh-plugin-yanyan-subagent/lib/client.js +1250 -0
- package/packages/dsh-plugin-yanyan-subagent/lib/index.js +951 -0
- package/packages/dsh-plugin-yanyan-subagent/lib/legacy-hidden-ids.json +9 -0
- package/packages/dsh-plugin-yanyan-subagent/lib/log-rewrite.js +382 -0
- package/packages/dsh-plugin-yanyan-subagent/lib/zstd-frames.js +81 -0
- package/packages/dsh-plugin-yanyan-subagent/locale/en.json +1 -0
- package/packages/dsh-plugin-yanyan-subagent/locale/zh.json +1 -0
- package/packages/dsh-plugin-yanyan-subagent/package.json +32 -0
- package/packages/dsh-plugin-yanyan-update/README.md +24 -0
- package/packages/dsh-plugin-yanyan-update/README.zh.md +24 -0
- package/packages/dsh-plugin-yanyan-update/cordis.patch.yml +7 -0
- package/packages/dsh-plugin-yanyan-update/icon.svg +5 -0
- package/packages/dsh-plugin-yanyan-update/lib/client.js +400 -0
- package/packages/dsh-plugin-yanyan-update/lib/index.js +840 -0
- package/packages/dsh-plugin-yanyan-update/locale/en.json +1 -0
- package/packages/dsh-plugin-yanyan-update/locale/zh.json +1 -0
- package/packages/dsh-plugin-yanyan-update/package.json +32 -0
- package/scripts/cleanup-ghost-subagents.mjs +241 -0
|
@@ -0,0 +1,840 @@
|
|
|
1
|
+
// dsh-plugin-yanyan-update — Host 半区。
|
|
2
|
+
//
|
|
3
|
+
// 职责:补齐官方 @linxin666/dsh-client-ui-plugin-manager 唯一没有覆盖的缺口——
|
|
4
|
+
// 它只检查 source.kind === 'npm' 且 isDirectRegistrySpec 的行(见
|
|
5
|
+
// dsh-web-0.4.16/packages/dsh-plugin-manager/src/host/routes.ts:603)。本插件额外覆盖
|
|
6
|
+
// 本地 link: / file: 目录安装的插件(多数是 git 仓库)与 registry 线上源,并提供逐行一键更新。
|
|
7
|
+
//
|
|
8
|
+
// 形态照抄 dsh-plugin-wechat-session/lib/index.js 的
|
|
9
|
+
// respond / readBody / apply / ctx.effect + ctx.webServer.register。
|
|
10
|
+
//
|
|
11
|
+
// 约束:纯 JavaScript,模块顶层不得访问 document / window / ctx;apply 只在被调用时才
|
|
12
|
+
// 执行。所有 git / fetch 都带超时并逐行 try/catch 隔离,单行异常不拖垮整体检查。
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { execFileSync } from 'node:child_process';
|
|
17
|
+
|
|
18
|
+
export const name = 'dsh-plugin-yanyan-update';
|
|
19
|
+
export const VERSION = '1.0.0';
|
|
20
|
+
export const inject = ['webServer', 'connection'];
|
|
21
|
+
export const ROUTE_BASE = '/__dsh-plugin-yanyan-update';
|
|
22
|
+
|
|
23
|
+
const DEFAULTS = {
|
|
24
|
+
checkOnOpen: false,
|
|
25
|
+
includeLocal: true,
|
|
26
|
+
includeRegistry: true,
|
|
27
|
+
registryUrl: 'https://registry.npmjs.org',
|
|
28
|
+
timeoutMs: 12000,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const FORBIDDEN_BODY = {
|
|
32
|
+
ok: false,
|
|
33
|
+
error: 'forbidden',
|
|
34
|
+
message: '请求未通过 DSH 准入裁决。',
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const BODY_LIMIT = 64 * 1024;
|
|
38
|
+
|
|
39
|
+
// ───────────────── 配置 ─────────────────
|
|
40
|
+
// 整段覆盖、不深合并:只认 DEFAULTS 列出来的键,缺省键逐个补默认值。
|
|
41
|
+
export function normalizeConfig(config) {
|
|
42
|
+
const c = config && typeof config === 'object' ? config : {};
|
|
43
|
+
// 整段覆盖、不深合并:先铺默认值,再用 config 的自有键浅覆盖(保留额外键,
|
|
44
|
+
// 但不对嵌套对象递归合并),最后对已知键做类型规范化。
|
|
45
|
+
const out = {};
|
|
46
|
+
for (const key of Object.keys(DEFAULTS)) out[key] = DEFAULTS[key];
|
|
47
|
+
for (const key of Object.keys(c)) out[key] = c[key];
|
|
48
|
+
out.checkOnOpen = Boolean(out.checkOnOpen);
|
|
49
|
+
out.includeLocal = Boolean(out.includeLocal);
|
|
50
|
+
out.includeRegistry = Boolean(out.includeRegistry);
|
|
51
|
+
out.registryUrl = typeof out.registryUrl === 'string' && out.registryUrl.trim()
|
|
52
|
+
? out.registryUrl.trim().replace(/\/+$/, '')
|
|
53
|
+
: DEFAULTS.registryUrl;
|
|
54
|
+
let t = Number(out.timeoutMs);
|
|
55
|
+
if (!Number.isFinite(t)) t = DEFAULTS.timeoutMs; // 非法值回落默认
|
|
56
|
+
else t = Math.min(120000, Math.max(1000, Math.round(t))); // 限制 1000..120000
|
|
57
|
+
out.timeoutMs = t;
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// ───────────────── semver 大于比较 ─────────────────
|
|
62
|
+
function parseVersion(v) {
|
|
63
|
+
if (typeof v !== 'string') return null;
|
|
64
|
+
let s = v.trim();
|
|
65
|
+
if (!s) return null;
|
|
66
|
+
if (/^[vV]/.test(s)) s = s.slice(1); // 允许前导 v
|
|
67
|
+
const plus = s.indexOf('+'); // 忽略 build metadata
|
|
68
|
+
if (plus >= 0) s = s.slice(0, plus);
|
|
69
|
+
let core = s;
|
|
70
|
+
let pre = '';
|
|
71
|
+
const dash = s.indexOf('-');
|
|
72
|
+
if (dash >= 0) {
|
|
73
|
+
core = s.slice(0, dash);
|
|
74
|
+
pre = s.slice(dash + 1);
|
|
75
|
+
}
|
|
76
|
+
const segs = core.split('.');
|
|
77
|
+
if (segs.length === 0 || segs.some((seg) => !/^\d+$/.test(seg))) return null;
|
|
78
|
+
return { nums: segs.map(Number), pre };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function comparePre(a, b) {
|
|
82
|
+
const as = a.split('.');
|
|
83
|
+
const bs = b.split('.');
|
|
84
|
+
const len = Math.max(as.length, bs.length);
|
|
85
|
+
for (let i = 0; i < len; i++) {
|
|
86
|
+
const x = i < as.length ? as[i] : '0';
|
|
87
|
+
const y = i < bs.length ? bs[i] : '0';
|
|
88
|
+
const xn = /^\d+$/.test(x) ? Number(x) : x;
|
|
89
|
+
const yn = /^\d+$/.test(y) ? Number(y) : y;
|
|
90
|
+
if (typeof xn === 'number' && typeof yn === 'number') {
|
|
91
|
+
if (xn > yn) return 1;
|
|
92
|
+
if (xn < yn) return -1;
|
|
93
|
+
} else {
|
|
94
|
+
const xs = String(xn);
|
|
95
|
+
const ys = String(yn);
|
|
96
|
+
if (xs > ys) return 1; // 非数字段按字符串比较
|
|
97
|
+
if (xs < ys) return -1;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return 0;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function gt(a, b) {
|
|
104
|
+
const A = parseVersion(a);
|
|
105
|
+
const B = parseVersion(b);
|
|
106
|
+
if (!A || !B) return false; // 非法 / 缺失版本
|
|
107
|
+
const len = Math.max(A.nums.length, B.nums.length);
|
|
108
|
+
for (let i = 0; i < len; i++) {
|
|
109
|
+
const x = i < A.nums.length ? A.nums[i] : 0;
|
|
110
|
+
const y = i < B.nums.length ? B.nums[i] : 0;
|
|
111
|
+
if (typeof x === 'number' && typeof y === 'number') {
|
|
112
|
+
if (x > y) return true;
|
|
113
|
+
if (x < y) return false;
|
|
114
|
+
} else {
|
|
115
|
+
const xs = String(x);
|
|
116
|
+
const ys = String(y);
|
|
117
|
+
if (xs > ys) return true; // 非数字段按字符串比较
|
|
118
|
+
if (xs < ys) return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// 数字段全相等:预发布视为小于同版本正式版。
|
|
122
|
+
if (!A.pre && !B.pre) return false;
|
|
123
|
+
if (!A.pre && B.pre) return true;
|
|
124
|
+
if (A.pre && !B.pre) return false;
|
|
125
|
+
return comparePre(A.pre, B.pre) > 0;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ───────────────── spec 分类 ─────────────────
|
|
129
|
+
export function classifySpec(name, spec) {
|
|
130
|
+
const n = typeof name === 'string' ? name : '';
|
|
131
|
+
const s = typeof spec === 'string' ? spec : '';
|
|
132
|
+
// ① link: / file: → 本地目录(去掉前缀后原样保留路径,解析交给调用方)
|
|
133
|
+
if (/^link:/.test(s) || /^file:/.test(s)) {
|
|
134
|
+
return { kind: 'local', name: n, spec: s, dir: s.replace(/^(link:|file:)/, '') };
|
|
135
|
+
}
|
|
136
|
+
// ② github: / git+ / git: / 含 github.com 的 URL → github
|
|
137
|
+
const gitPrefix = /^(github:|git\+|git:)/i.test(s);
|
|
138
|
+
const githubUrl = /github\.com/i.test(s) &&
|
|
139
|
+
(/^https?:\/\//i.test(s) || /^git@/i.test(s) || /^ssh:/i.test(s) || gitPrefix);
|
|
140
|
+
if (gitPrefix || githubUrl) {
|
|
141
|
+
return { kind: 'github', name: n, spec: s };
|
|
142
|
+
}
|
|
143
|
+
// ③ 官方包与自身 → skip(官方随 DSH 本体升级;禁自更新避免把正在服务的路由一起移除)
|
|
144
|
+
if (/^@deepseek-ai\//.test(n) || n === '@linxin666/dsh-session-archive' ||
|
|
145
|
+
/^dsh-plugin-yanyan-/.test(n) || n === 'dsh-plugin-yanyan-update') {
|
|
146
|
+
return { kind: 'skip', name: n, spec: s };
|
|
147
|
+
}
|
|
148
|
+
// ④ 其余(纯 name@version / ^x.y.z / 普通包名)→ registry
|
|
149
|
+
return { kind: 'registry', name: n, spec: s };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ───────────────── HTTP 工具 ─────────────────
|
|
153
|
+
function respond(res, status, body) {
|
|
154
|
+
const payload = JSON.stringify(body);
|
|
155
|
+
res.writeHead(status, {
|
|
156
|
+
'content-type': 'application/json; charset=utf-8',
|
|
157
|
+
'cache-control': 'no-store',
|
|
158
|
+
'content-length': Buffer.byteLength(payload),
|
|
159
|
+
});
|
|
160
|
+
res.end(payload);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function readBody(req, limit = BODY_LIMIT) {
|
|
164
|
+
return new Promise((resolve) => {
|
|
165
|
+
const chunks = [];
|
|
166
|
+
let size = 0;
|
|
167
|
+
let settled = false;
|
|
168
|
+
const finish = (value) => {
|
|
169
|
+
if (settled) return;
|
|
170
|
+
settled = true;
|
|
171
|
+
resolve(value);
|
|
172
|
+
};
|
|
173
|
+
req.on('data', (chunk) => {
|
|
174
|
+
if (settled) return;
|
|
175
|
+
size += chunk.length;
|
|
176
|
+
if (size > limit) {
|
|
177
|
+
finish({ ok: false, reason: 'body-too-large' });
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
chunks.push(chunk);
|
|
181
|
+
});
|
|
182
|
+
req.on('end', () => {
|
|
183
|
+
if (settled) return;
|
|
184
|
+
try {
|
|
185
|
+
finish({ ok: true, text: Buffer.concat(chunks).toString('utf8') });
|
|
186
|
+
} catch (error) {
|
|
187
|
+
finish({ ok: false, reason: 'invalid-body' });
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
req.on('error', () => finish({ ok: false, reason: 'request-error' }));
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ───────────────── profile 读取 ─────────────────
|
|
195
|
+
// 优先宿主发布的 profileContext.dir;回退 DSH_HOME/profiles/desktop,
|
|
196
|
+
// 再遍历 profiles/* 找含 package.json 的目录。
|
|
197
|
+
function resolveProfileDir(ctx) {
|
|
198
|
+
let dir = null;
|
|
199
|
+
try {
|
|
200
|
+
const pc = ctx.get('profileContext');
|
|
201
|
+
if (pc && typeof pc.dir === 'string' && pc.dir) dir = pc.dir;
|
|
202
|
+
} catch (error) {
|
|
203
|
+
dir = null;
|
|
204
|
+
}
|
|
205
|
+
if (dir) return dir;
|
|
206
|
+
const home = process.env.DSH_HOME;
|
|
207
|
+
if (!home) return null;
|
|
208
|
+
const direct = path.join(home, 'profiles', 'desktop');
|
|
209
|
+
try {
|
|
210
|
+
if (fs.existsSync(path.join(direct, 'package.json'))) return direct;
|
|
211
|
+
} catch (error) {
|
|
212
|
+
// 继续遍历
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
const profilesRoot = path.join(home, 'profiles');
|
|
216
|
+
const entries = fs.readdirSync(profilesRoot, { withFileTypes: true });
|
|
217
|
+
for (const ent of entries) {
|
|
218
|
+
if (!ent.isDirectory()) continue;
|
|
219
|
+
const cand = path.join(profilesRoot, ent.name);
|
|
220
|
+
try {
|
|
221
|
+
if (fs.existsSync(path.join(cand, 'package.json'))) return cand;
|
|
222
|
+
} catch (error) {
|
|
223
|
+
// 忽略单个候选
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
} catch (error) {
|
|
227
|
+
// 忽略
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function readDependencies(profileDir) {
|
|
233
|
+
const raw = fs.readFileSync(path.join(profileDir, 'package.json'), 'utf8');
|
|
234
|
+
const pkg = JSON.parse(raw);
|
|
235
|
+
return pkg && pkg.dependencies && typeof pkg.dependencies === 'object'
|
|
236
|
+
? pkg.dependencies
|
|
237
|
+
: {};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function readProfile(ctx) {
|
|
241
|
+
const profileDir = resolveProfileDir(ctx);
|
|
242
|
+
if (!profileDir) return { error: 'profileDir-unavailable', message: '无法定位当前 profile 目录。' };
|
|
243
|
+
let deps;
|
|
244
|
+
try {
|
|
245
|
+
deps = readDependencies(profileDir);
|
|
246
|
+
} catch (error) {
|
|
247
|
+
return { error: 'profile-read-failed', message: String((error && error.message) || error) };
|
|
248
|
+
}
|
|
249
|
+
return { profileDir, deps };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// fs.realpathSync 找它自己或向上最多 3 层含 .git 的根。
|
|
253
|
+
function findGitRoot(startDir) {
|
|
254
|
+
let real;
|
|
255
|
+
try {
|
|
256
|
+
real = fs.realpathSync(startDir);
|
|
257
|
+
} catch (error) {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
let cur = real;
|
|
261
|
+
for (let i = 0; i <= 3; i++) {
|
|
262
|
+
try {
|
|
263
|
+
if (fs.existsSync(path.join(cur, '.git'))) return cur;
|
|
264
|
+
} catch (error) {
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
const parent = path.dirname(cur);
|
|
268
|
+
if (parent === cur) break;
|
|
269
|
+
cur = parent;
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function git(cwd, args, timeoutMs) {
|
|
275
|
+
return execFileSync('git', args, {
|
|
276
|
+
cwd,
|
|
277
|
+
encoding: 'utf8',
|
|
278
|
+
timeout: Math.max(1000, Math.min(timeoutMs, 30000)),
|
|
279
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
280
|
+
windowsHide: true,
|
|
281
|
+
}).trim();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async function fetchLatest(pkgName, cfg) {
|
|
285
|
+
const url = cfg.registryUrl + '/' + encodeURIComponent(pkgName) + '/latest';
|
|
286
|
+
const resp = await fetch(url, { signal: AbortSignal.timeout(cfg.timeoutMs) });
|
|
287
|
+
if (!resp || !resp.ok) throw new Error('registry HTTP ' + (resp && resp.status));
|
|
288
|
+
const data = await resp.json();
|
|
289
|
+
const version = String((data && data.version) || '');
|
|
290
|
+
if (!version) throw new Error('registry 未返回 version');
|
|
291
|
+
return version;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// ───────────────── 逐行检查 ─────────────────
|
|
295
|
+
async function checkLocal(pkgName, dir, profileDir, cfg) {
|
|
296
|
+
const absDir = path.isAbsolute(dir) ? dir : path.join(profileDir, dir);
|
|
297
|
+
const gitRoot = findGitRoot(absDir);
|
|
298
|
+
if (!gitRoot) {
|
|
299
|
+
return { id: pkgName, kind: 'local', hasUpdate: false, reason: 'localNoGit', dir };
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
const head = git(gitRoot, ['rev-parse', 'HEAD'], cfg.timeoutMs);
|
|
303
|
+
const branch = git(gitRoot, ['rev-parse', '--abbrev-ref', 'HEAD'], cfg.timeoutMs);
|
|
304
|
+
if (!branch || branch === 'HEAD') {
|
|
305
|
+
return { id: pkgName, kind: 'local', hasUpdate: false, reason: 'detachedHead', dir };
|
|
306
|
+
}
|
|
307
|
+
// fetch 失败不阻断:仍可用本地已经存在的 origin/<branch> 引用比较。
|
|
308
|
+
try {
|
|
309
|
+
git(gitRoot, ['fetch', 'origin', branch], cfg.timeoutMs);
|
|
310
|
+
} catch (error) {
|
|
311
|
+
// 网络不可用时退化为本地已有远端引用
|
|
312
|
+
}
|
|
313
|
+
const remote = git(gitRoot, ['rev-parse', 'origin/' + branch], cfg.timeoutMs);
|
|
314
|
+
const short = (sha) => String(sha || '').slice(0, 7);
|
|
315
|
+
return {
|
|
316
|
+
id: pkgName,
|
|
317
|
+
current: short(head),
|
|
318
|
+
latest: short(remote),
|
|
319
|
+
kind: 'local',
|
|
320
|
+
hasUpdate: head !== remote,
|
|
321
|
+
dir,
|
|
322
|
+
branch,
|
|
323
|
+
};
|
|
324
|
+
} catch (error) {
|
|
325
|
+
return {
|
|
326
|
+
id: pkgName,
|
|
327
|
+
kind: 'local',
|
|
328
|
+
hasUpdate: false,
|
|
329
|
+
dir,
|
|
330
|
+
error: String((error && error.message) || error),
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async function checkRegistry(pkgName, profileDir, cfg) {
|
|
336
|
+
let current = '';
|
|
337
|
+
try {
|
|
338
|
+
const pkgPath = path.join(profileDir, 'node_modules', pkgName, 'package.json');
|
|
339
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
340
|
+
current = String((pkg && pkg.version) || '');
|
|
341
|
+
} catch (error) {
|
|
342
|
+
return {
|
|
343
|
+
id: pkgName,
|
|
344
|
+
kind: 'registry',
|
|
345
|
+
hasUpdate: false,
|
|
346
|
+
error: '读取已装版本失败:' + String((error && error.message) || error),
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
if (!current) {
|
|
350
|
+
return { id: pkgName, kind: 'registry', hasUpdate: false, error: '已装版本为空' };
|
|
351
|
+
}
|
|
352
|
+
let latest = '';
|
|
353
|
+
try {
|
|
354
|
+
latest = await fetchLatest(pkgName, cfg);
|
|
355
|
+
} catch (error) {
|
|
356
|
+
return {
|
|
357
|
+
id: pkgName,
|
|
358
|
+
kind: 'registry',
|
|
359
|
+
current,
|
|
360
|
+
hasUpdate: false,
|
|
361
|
+
error: String((error && error.message) || error),
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
return { id: pkgName, current, latest, kind: 'registry', hasUpdate: gt(latest, current) };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
async function checkAll(ctx, cfg) {
|
|
368
|
+
const prof = readProfile(ctx);
|
|
369
|
+
if (prof.error) return { updates: [], skipped: 0, rows: [], error: prof.message };
|
|
370
|
+
const rows = [];
|
|
371
|
+
let skipped = 0;
|
|
372
|
+
for (const pkgName of Object.keys(prof.deps)) {
|
|
373
|
+
let cls;
|
|
374
|
+
try {
|
|
375
|
+
cls = classifySpec(pkgName, prof.deps[pkgName]);
|
|
376
|
+
} catch (error) {
|
|
377
|
+
skipped += 1;
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (cls.kind === 'skip') {
|
|
381
|
+
skipped += 1;
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (cls.kind === 'local' && !cfg.includeLocal) {
|
|
385
|
+
skipped += 1;
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
if (cls.kind === 'registry' && !cfg.includeRegistry) {
|
|
389
|
+
skipped += 1;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
let row;
|
|
393
|
+
try {
|
|
394
|
+
if (cls.kind === 'local') {
|
|
395
|
+
row = await checkLocal(pkgName, cls.dir, prof.profileDir, cfg);
|
|
396
|
+
} else if (cls.kind === 'registry') {
|
|
397
|
+
row = await checkRegistry(pkgName, prof.profileDir, cfg);
|
|
398
|
+
} else if (cls.kind === 'github') {
|
|
399
|
+
row = { id: pkgName, kind: 'github', hasUpdate: false, reason: 'githubUnsupported' };
|
|
400
|
+
} else {
|
|
401
|
+
skipped += 1;
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
} catch (error) {
|
|
405
|
+
row = {
|
|
406
|
+
id: pkgName,
|
|
407
|
+
kind: cls.kind,
|
|
408
|
+
hasUpdate: false,
|
|
409
|
+
error: String((error && error.message) || error),
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
if (row) rows.push(row);
|
|
413
|
+
}
|
|
414
|
+
return { updates: rows.filter((r) => r.hasUpdate === true), skipped, rows };
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// ───────────────── 逐行更新 ─────────────────
|
|
418
|
+
function isValidId(id, deps) {
|
|
419
|
+
if (typeof id !== 'string') return false;
|
|
420
|
+
const s = id.trim();
|
|
421
|
+
if (!s) return false;
|
|
422
|
+
if (s.indexOf('..') >= 0) return false;
|
|
423
|
+
if (s.indexOf('\\') >= 0) return false;
|
|
424
|
+
const slashes = (s.match(/\//g) || []).length;
|
|
425
|
+
// 除 scoped 包名的单个 @scope/ 外,禁止任何斜杠(防路径穿越 / 任意 spec)。
|
|
426
|
+
const isScoped = s.charAt(0) === '@' && slashes === 1;
|
|
427
|
+
if (slashes > 0 && !isScoped) return false;
|
|
428
|
+
// 必须在本次 dependencies 名单里。
|
|
429
|
+
return Object.prototype.hasOwnProperty.call(deps, s);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function isProtected(id) {
|
|
433
|
+
return /^dsh-plugin-yanyan-/.test(id) ||
|
|
434
|
+
id === '@linxin666/dsh-session-archive' ||
|
|
435
|
+
/^@deepseek-ai\//.test(id);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// 兼容三种返回形态:host 服务直接返回 ChangeResult;remote proxy 包成 { ok, value };
|
|
439
|
+
// 官方 updateBundle 返回 PluginUpdateResult(结果在 .result)。
|
|
440
|
+
function changeOf(outcome) {
|
|
441
|
+
if (!outcome || typeof outcome !== 'object') return outcome;
|
|
442
|
+
if (outcome.value && typeof outcome.value === 'object') return outcome.value;
|
|
443
|
+
if (outcome.result && typeof outcome.result === 'object') return outcome.result;
|
|
444
|
+
return outcome;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// 官方管理器把失败折叠进返回值(application: 'failed' / 'cancelled'),而不是抛错;
|
|
448
|
+
// 部分形态只带 error.code(如 already-installed / incompatible),同样视为失败。
|
|
449
|
+
function nativeManagerFailure(outcome) {
|
|
450
|
+
if (!outcome || typeof outcome !== 'object') return false;
|
|
451
|
+
if (outcome.ok === false) return true;
|
|
452
|
+
const c = changeOf(outcome);
|
|
453
|
+
const app = c.application || outcome.application;
|
|
454
|
+
if (app === 'failed' || app === 'cancelled') return true;
|
|
455
|
+
const err = c.error || outcome.error;
|
|
456
|
+
if (err && typeof err.code === 'string' && err.code) return true;
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// 读取官方管理器拒绝码:gateway.ts 用 verdict.error?.code 判定 already-installed。
|
|
461
|
+
function refusalCode(outcome) {
|
|
462
|
+
if (!outcome || typeof outcome !== 'object') return '';
|
|
463
|
+
const c = changeOf(outcome);
|
|
464
|
+
const err = c.error || outcome.error;
|
|
465
|
+
const code = err && err.code;
|
|
466
|
+
return typeof code === 'string' ? code : '';
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// 解析官方 pluginManager.updateBundle 的返回值 PluginUpdateResult:
|
|
470
|
+
// { from, to, step, result: ChangeResult, restore?: ChangeResult }。
|
|
471
|
+
// ChangeResult.application: applied / restart-required / overridden / failed / cancelled。
|
|
472
|
+
// 官方在 HMR 下「安装失败或取消会把被移除的版本重新装回」并写入 restore,
|
|
473
|
+
// 因此本函数只读结果、绝不在本包层补做 removeBundle。
|
|
474
|
+
export function parseUpdateResult(outcome) {
|
|
475
|
+
const base = { ok: false, updated: false, restartRequired: false, restored: false };
|
|
476
|
+
if (!outcome || typeof outcome !== 'object') {
|
|
477
|
+
return Object.assign({}, base, {
|
|
478
|
+
error: 'update-failed',
|
|
479
|
+
message: '更新未返回有效结果,请到插件页确认该插件安装状态。',
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
// 兼容 remote proxy 把 PluginUpdateResult 包在 { ok, value } 里的形态。
|
|
483
|
+
const src = outcome.value && typeof outcome.value === 'object' ? outcome.value : outcome;
|
|
484
|
+
const r = src.result && typeof src.result === 'object' ? src.result : {};
|
|
485
|
+
const app = r.application || src.application;
|
|
486
|
+
const step = src.step;
|
|
487
|
+
const successful = app === 'applied' || app === 'restart-required' || app === 'overridden';
|
|
488
|
+
// 官方成功态 step 恒为 install;兼容个别 proxy 省略 step 的情形。
|
|
489
|
+
// outcome.ok === false 是 proxy 层显式失败(此时本包也从未 remove,原版本必在)。
|
|
490
|
+
if (successful && outcome.ok !== false && (!step || step === 'install')) {
|
|
491
|
+
return {
|
|
492
|
+
ok: true,
|
|
493
|
+
updated: true,
|
|
494
|
+
restartRequired: app !== 'applied',
|
|
495
|
+
restored: true,
|
|
496
|
+
message: app === 'applied'
|
|
497
|
+
? '更新完成,已即时生效。'
|
|
498
|
+
: '更新完成,重启 DSH 服务后生效。',
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
// 失败:有 restore 字段则以其结果为准;无 restore 时,官方拒绝在 check 阶段
|
|
502
|
+
// 或非 HMR 覆盖安装失败,原安装均未被移除,一律视为已保留(本包也从不在此分支 remove)。
|
|
503
|
+
const restore = src.restore;
|
|
504
|
+
const restoredByOfficial = restore && typeof restore === 'object'
|
|
505
|
+
? !nativeManagerFailure(restore)
|
|
506
|
+
: true;
|
|
507
|
+
const reason = r.error && (r.error.diagnostic || r.error.code)
|
|
508
|
+
? String(r.error.diagnostic || r.error.code)
|
|
509
|
+
: String(app || step || '未知原因');
|
|
510
|
+
return Object.assign({}, base, {
|
|
511
|
+
error: app === 'cancelled' ? 'update-cancelled' : 'update-failed',
|
|
512
|
+
restored: restoredByOfficial,
|
|
513
|
+
message: restoredByOfficial
|
|
514
|
+
? '更新未完成(' + reason + '),原版本已保留,请稍后重试。'
|
|
515
|
+
: '更新未完成(' + reason + '),请到插件页确认该插件是否缺失,如已缺失请手动重装。',
|
|
516
|
+
detail: JSON.stringify(r.error || {}),
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
async function updateLocal(id, cls, profileDir, cfg) {
|
|
521
|
+
const base = { ok: false, updated: false, restartRequired: false, id };
|
|
522
|
+
const dir = cls.dir || '';
|
|
523
|
+
const absDir = path.isAbsolute(dir) ? dir : path.join(profileDir, dir);
|
|
524
|
+
const gitRoot = findGitRoot(absDir);
|
|
525
|
+
if (!gitRoot) {
|
|
526
|
+
return Object.assign({}, base, { error: 'localNoGit', message: '本地源码,无法自动更新' });
|
|
527
|
+
}
|
|
528
|
+
try {
|
|
529
|
+
const branch = git(gitRoot, ['rev-parse', '--abbrev-ref', 'HEAD'], cfg.timeoutMs);
|
|
530
|
+
if (!branch || branch === 'HEAD') {
|
|
531
|
+
return Object.assign({}, base, { error: 'detachedHead', message: '当前处于游离 HEAD,请手动更新。' });
|
|
532
|
+
}
|
|
533
|
+
git(gitRoot, ['fetch', 'origin', branch], cfg.timeoutMs);
|
|
534
|
+
const head = git(gitRoot, ['rev-parse', 'HEAD'], cfg.timeoutMs);
|
|
535
|
+
const remote = git(gitRoot, ['rev-parse', 'origin/' + branch], cfg.timeoutMs);
|
|
536
|
+
if (head === remote) {
|
|
537
|
+
return { ok: true, updated: false, restartRequired: false, id,
|
|
538
|
+
reason: 'already-latest', message: '已是最新版本,无需更新。' };
|
|
539
|
+
}
|
|
540
|
+
git(gitRoot, ['merge', '--ff-only', 'origin/' + branch], cfg.timeoutMs);
|
|
541
|
+
return { ok: true, updated: true, restartRequired: true, id,
|
|
542
|
+
message: '更新完成,重启 DSH 服务后生效。' };
|
|
543
|
+
} catch (error) {
|
|
544
|
+
return Object.assign({}, base, {
|
|
545
|
+
error: 'git-update-failed',
|
|
546
|
+
message: String((error && error.message) || error),
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// io 为可选注入点,仅供单测替换 IO(network / fs / manager 来源),生产调用省略。
|
|
552
|
+
// 单测据此完全不触碰真实 pluginManager、npm registry 与 profile 文件。
|
|
553
|
+
export async function updateRegistry(ctx, id, cfg, deps, io) {
|
|
554
|
+
const ioRef = io || {};
|
|
555
|
+
const fetchLatestFn = ioRef.fetchLatest || fetchLatest;
|
|
556
|
+
const resolveProfileDirFn = ioRef.resolveProfileDir || resolveProfileDir;
|
|
557
|
+
const readDependenciesFn = ioRef.readDependencies || readDependencies;
|
|
558
|
+
const base = { ok: false, updated: false, restartRequired: false, id };
|
|
559
|
+
// 已查证官方契约(dsh-web-0.4.16 ... gateway.ts 与 app.asar):
|
|
560
|
+
// pluginManager.updateBundle(name, version, options) 内建原子回滚;
|
|
561
|
+
// installBundle(spec, { enabled, requestId }) / removeBundle(name) 仅在降级时使用。
|
|
562
|
+
let manager = Object.prototype.hasOwnProperty.call(ioRef, 'manager')
|
|
563
|
+
? ioRef.manager
|
|
564
|
+
: null;
|
|
565
|
+
if (manager === null) {
|
|
566
|
+
try {
|
|
567
|
+
manager = ctx.get('pluginManager');
|
|
568
|
+
} catch (error) {
|
|
569
|
+
manager = null;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
const hasUpdate = manager && typeof manager.updateBundle === 'function';
|
|
573
|
+
const hasTwoPhase = manager && typeof manager.removeBundle === 'function' &&
|
|
574
|
+
typeof manager.installBundle === 'function';
|
|
575
|
+
if (!hasUpdate && !hasTwoPhase) {
|
|
576
|
+
// 找不到或形态不明时绝不乱调,退化为明确的手动更新提示;此时绝未动过安装状态。
|
|
577
|
+
return Object.assign({}, base, {
|
|
578
|
+
error: 'native-manager-unavailable',
|
|
579
|
+
restored: true,
|
|
580
|
+
message: '未找到可用的官方插件管理器,原版本未改动,请手动更新该插件。',
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
let latest;
|
|
584
|
+
try {
|
|
585
|
+
latest = await fetchLatestFn(id, cfg);
|
|
586
|
+
} catch (error) {
|
|
587
|
+
return Object.assign({}, base, {
|
|
588
|
+
error: 'registry-fetch-failed',
|
|
589
|
+
restored: true,
|
|
590
|
+
message: '无法读取最新版本:' + String((error && error.message) || error),
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
const previousSpec = deps[id];
|
|
594
|
+
const restoreSpec = id + '@' + previousSpec;
|
|
595
|
+
const targetSpec = id + '@' + latest;
|
|
596
|
+
const requestId = 'yanyan-update-' + id + '-' + Date.now() + '-' +
|
|
597
|
+
Math.random().toString(36).slice(2, 8);
|
|
598
|
+
|
|
599
|
+
// 首选:直接复用官方 updateBundle。HMR 下安装失败/取消时官方自动把移除的版本
|
|
600
|
+
// 重新装回(写入 restore 字段);非 HMR 直接覆盖安装,根本不先卸载。
|
|
601
|
+
// 因此本包绝不自行 removeBundle,从根子上杜绝「已卸载且没装回」。
|
|
602
|
+
if (hasUpdate) {
|
|
603
|
+
let outcome;
|
|
604
|
+
try {
|
|
605
|
+
outcome = await manager.updateBundle(id, latest, { requestId });
|
|
606
|
+
} catch (error) {
|
|
607
|
+
// 调用本身抛错时本包并未卸载任何东西,原安装仍在。
|
|
608
|
+
return Object.assign({}, base, {
|
|
609
|
+
error: 'update-failed',
|
|
610
|
+
restored: true,
|
|
611
|
+
message: '官方更新调用异常(' + String((error && error.message) || error) +
|
|
612
|
+
'),原版本未被卸载,请稍后重试。',
|
|
613
|
+
detail: String((error && error.message) || error),
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
return Object.assign({}, base, parseUpdateResult(outcome), {
|
|
617
|
+
id,
|
|
618
|
+
spec: targetSpec,
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// 降级:旧版 DSH 无 updateBundle。对齐官方 gateway.ts 的 runNativeUpdate:
|
|
623
|
+
// 先尝试覆盖式 installBundle(targetSpec),仅当官方明确以 already-installed 拒绝时,
|
|
624
|
+
// 才不得不进入 removeBundle + installBundle 两阶段。任何失败都 ensureRestore 装回原 spec。
|
|
625
|
+
let removed = false;
|
|
626
|
+
const ensureRestore = async () => {
|
|
627
|
+
if (!removed) return true; // 从未卸载,原安装必然保留
|
|
628
|
+
let ok = false;
|
|
629
|
+
for (let attempt = 0; attempt < 2 && !ok; attempt += 1) {
|
|
630
|
+
try {
|
|
631
|
+
const rr = await manager.installBundle(restoreSpec, { enabled: true });
|
|
632
|
+
ok = !nativeManagerFailure(rr);
|
|
633
|
+
} catch (error2) {
|
|
634
|
+
ok = false;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
if (ok) return true;
|
|
638
|
+
// 管理器返回被拒也可能是「已装回」的假阴性,再查 profile 依赖兜底二次核验。
|
|
639
|
+
try {
|
|
640
|
+
const after = ioRef.profileDeps || null;
|
|
641
|
+
if (after && Object.prototype.hasOwnProperty.call(after, id)) return true;
|
|
642
|
+
const dir = resolveProfileDirFn(ctx);
|
|
643
|
+
const depsAfter = dir ? readDependenciesFn(dir) : null;
|
|
644
|
+
if (depsAfter && Object.prototype.hasOwnProperty.call(depsAfter, id)) return true;
|
|
645
|
+
} catch (error3) {
|
|
646
|
+
// 无法确认时如实报告未装回,交由用户手动重装。
|
|
647
|
+
}
|
|
648
|
+
return false;
|
|
649
|
+
};
|
|
650
|
+
const failResult = (err, restored) => Object.assign({}, base, {
|
|
651
|
+
error: 'update-failed',
|
|
652
|
+
restored: Boolean(restored),
|
|
653
|
+
message: restored
|
|
654
|
+
? '更新失败,已装回原版本,请稍后重试。'
|
|
655
|
+
: '更新失败,未能装回原版本,请到插件页手动重新安装该插件。',
|
|
656
|
+
detail: String((err && err.message) || err),
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
try {
|
|
660
|
+
const first = await manager.installBundle(targetSpec, { enabled: true });
|
|
661
|
+
if (refusalCode(first) !== 'already-installed') {
|
|
662
|
+
if (nativeManagerFailure(first)) {
|
|
663
|
+
// 覆盖安装失败,但从未卸载,原版本仍在;文案据此如实说明「未被卸载」。
|
|
664
|
+
return Object.assign({}, base, {
|
|
665
|
+
error: 'update-failed',
|
|
666
|
+
restored: true,
|
|
667
|
+
message: '更新失败,原版本未被卸载,请稍后重试。',
|
|
668
|
+
detail: String((first && first.error &&
|
|
669
|
+
(first.error.diagnostic || first.error.code)) || '覆盖安装被拒绝'),
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
return { ok: true, updated: true, restartRequired: true, id, spec: targetSpec,
|
|
673
|
+
message: '更新完成,重启 DSH 服务后生效。' };
|
|
674
|
+
}
|
|
675
|
+
// 官方以 already-installed 拒绝,进入两阶段;此后任何异常都必须装回原版本。
|
|
676
|
+
const r1 = await manager.removeBundle(id);
|
|
677
|
+
if (nativeManagerFailure(r1)) {
|
|
678
|
+
return failResult(new Error('removeBundle 被拒绝:' + JSON.stringify(r1)), true);
|
|
679
|
+
}
|
|
680
|
+
removed = true;
|
|
681
|
+
const r2 = await manager.installBundle(targetSpec, { enabled: true });
|
|
682
|
+
if (nativeManagerFailure(r2)) {
|
|
683
|
+
const restored = await ensureRestore();
|
|
684
|
+
return failResult(new Error('installBundle 被拒绝:' + JSON.stringify(r2)), restored);
|
|
685
|
+
}
|
|
686
|
+
return { ok: true, updated: true, restartRequired: true, id, spec: targetSpec,
|
|
687
|
+
message: '更新完成,重启 DSH 服务后生效。' };
|
|
688
|
+
} catch (error) {
|
|
689
|
+
const restored = await ensureRestore();
|
|
690
|
+
return failResult(error, restored);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// ───────────────── 路由 handler ─────────────────
|
|
695
|
+
function makeHealthHandler(ctx, cfg, fence) {
|
|
696
|
+
return function healthHandler(req, res) {
|
|
697
|
+
if (!fence(req)) {
|
|
698
|
+
respond(res, 403, FORBIDDEN_BODY);
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
const method = String(req.method || 'GET').toUpperCase();
|
|
702
|
+
if (method !== 'GET' && method !== 'POST') {
|
|
703
|
+
respond(res, 405, { ok: false, error: 'method-not-allowed', message: '只接受 GET 或 POST 请求。' });
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
respond(res, 200, { ok: true, name, version: VERSION });
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function makeCheckHandler(ctx, cfg, fence) {
|
|
711
|
+
return async function checkHandler(req, res) {
|
|
712
|
+
if (!fence(req)) {
|
|
713
|
+
respond(res, 403, FORBIDDEN_BODY);
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
const method = String(req.method || 'GET').toUpperCase();
|
|
717
|
+
if (method !== 'POST') {
|
|
718
|
+
respond(res, 405, { ok: false, error: 'method-not-allowed', message: '只接受 POST 请求。' });
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
try {
|
|
722
|
+
const result = await checkAll(ctx, cfg);
|
|
723
|
+
respond(res, 200, {
|
|
724
|
+
ok: true,
|
|
725
|
+
updates: result.updates,
|
|
726
|
+
skipped: result.skipped,
|
|
727
|
+
checkOnOpen: cfg.checkOnOpen,
|
|
728
|
+
rows: result.rows,
|
|
729
|
+
error: result.error || undefined,
|
|
730
|
+
});
|
|
731
|
+
} catch (error) {
|
|
732
|
+
respond(res, 200, {
|
|
733
|
+
ok: true,
|
|
734
|
+
updates: [],
|
|
735
|
+
skipped: 0,
|
|
736
|
+
error: String((error && error.message) || error),
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function makeUpdateHandler(ctx, cfg, fence) {
|
|
743
|
+
return async function updateHandler(req, res) {
|
|
744
|
+
if (!fence(req)) {
|
|
745
|
+
respond(res, 403, FORBIDDEN_BODY);
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
const method = String(req.method || 'GET').toUpperCase();
|
|
749
|
+
if (method !== 'POST') {
|
|
750
|
+
respond(res, 405, { ok: false, error: 'method-not-allowed', message: '只接受 POST 请求。' });
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
let payload;
|
|
754
|
+
try {
|
|
755
|
+
const read = await readBody(req);
|
|
756
|
+
payload = read.ok ? JSON.parse(read.text || '{}') : {};
|
|
757
|
+
} catch (error) {
|
|
758
|
+
payload = {};
|
|
759
|
+
}
|
|
760
|
+
const id = payload && typeof payload.id === 'string' ? payload.id.trim() : '';
|
|
761
|
+
const base = { ok: false, updated: false, restartRequired: false, id };
|
|
762
|
+
// 统一的 200 错误体便于前端逐行处理,异常不外抛。
|
|
763
|
+
const prof = readProfile(ctx);
|
|
764
|
+
if (prof.error) {
|
|
765
|
+
respond(res, 200, Object.assign({}, base, { error: prof.error, message: prof.message }));
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
if (!isValidId(id, prof.deps)) {
|
|
769
|
+
respond(res, 200, Object.assign({}, base, {
|
|
770
|
+
error: 'invalid-id',
|
|
771
|
+
message: '插件标识不合法或不在已安装名单中。',
|
|
772
|
+
}));
|
|
773
|
+
return;
|
|
774
|
+
}
|
|
775
|
+
if (isProtected(id)) {
|
|
776
|
+
respond(res, 200, Object.assign({}, base, {
|
|
777
|
+
error: 'forbidden-self-update',
|
|
778
|
+
message: '该插件不允许在此更新。',
|
|
779
|
+
}));
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
let cls;
|
|
783
|
+
try {
|
|
784
|
+
cls = classifySpec(id, prof.deps[id]);
|
|
785
|
+
} catch (error) {
|
|
786
|
+
cls = { kind: 'skip', name: id, spec: prof.deps[id] };
|
|
787
|
+
}
|
|
788
|
+
let result;
|
|
789
|
+
try {
|
|
790
|
+
if (cls.kind === 'local') {
|
|
791
|
+
result = await updateLocal(id, cls, prof.profileDir, cfg);
|
|
792
|
+
} else if (cls.kind === 'registry') {
|
|
793
|
+
result = await updateRegistry(ctx, id, cfg, prof.deps);
|
|
794
|
+
} else if (cls.kind === 'github') {
|
|
795
|
+
result = Object.assign({}, base, {
|
|
796
|
+
error: 'unsupported',
|
|
797
|
+
message: '该来源暂不支持一键更新,请到源码目录手动更新。',
|
|
798
|
+
});
|
|
799
|
+
} else {
|
|
800
|
+
result = Object.assign({}, base, { error: 'skip', message: '该插件不需要在此更新。' });
|
|
801
|
+
}
|
|
802
|
+
} catch (error) {
|
|
803
|
+
result = Object.assign({}, base, {
|
|
804
|
+
error: 'update-exception',
|
|
805
|
+
message: String((error && error.message) || error),
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
respond(res, 200, result);
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// ───────────────── apply ─────────────────
|
|
813
|
+
export function apply(ctx, config) {
|
|
814
|
+
const cfg = normalizeConfig(config);
|
|
815
|
+
// 统一准入:仅当 requestRejection 返回 undefined 时放行(不自建信任例外)。
|
|
816
|
+
const fence = (req) => {
|
|
817
|
+
try {
|
|
818
|
+
return ctx.connection.requestRejection(req) === undefined;
|
|
819
|
+
} catch (error) {
|
|
820
|
+
return false;
|
|
821
|
+
}
|
|
822
|
+
};
|
|
823
|
+
const routes = [
|
|
824
|
+
{ path: ROUTE_BASE + '/health', handler: makeHealthHandler(ctx, cfg, fence) },
|
|
825
|
+
{ path: ROUTE_BASE + '/check', handler: makeCheckHandler(ctx, cfg, fence) },
|
|
826
|
+
{ path: ROUTE_BASE + '/update', handler: makeUpdateHandler(ctx, cfg, fence) },
|
|
827
|
+
];
|
|
828
|
+
// register 的返回值直接作为 effect 的清理函数,禁用 / HMR 时路由自动撤销。
|
|
829
|
+
for (const route of routes) {
|
|
830
|
+
ctx.effect(
|
|
831
|
+
() => ctx.webServer.register({ kind: 'exact', path: route.path, handler: route.handler }),
|
|
832
|
+
'dsh-plugin-yanyan-update: route ' + route.path,
|
|
833
|
+
);
|
|
834
|
+
}
|
|
835
|
+
try {
|
|
836
|
+
ctx.logger?.info?.('[dsh-plugin-yanyan-update] host routes ready');
|
|
837
|
+
} catch (error) {
|
|
838
|
+
// 日志服务不可用时不影响路由注册
|
|
839
|
+
}
|
|
840
|
+
}
|