@alequet/dsh-patchkeeper 0.0.0-stage → 0.5.3
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/LICENSE +21 -0
- package/README.md +67 -2
- package/bin/cli.mjs +749 -0
- package/client.js +472 -0
- package/cordis.patch.yml +6 -0
- package/index.js +176 -0
- package/package.json +41 -4
package/client.js
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-patchkeeper 客户端 UI(免构建手写 bundle,形态参照 dsh-meme/dshmarket)。
|
|
3
|
+
*
|
|
4
|
+
* 槽位:
|
|
5
|
+
* plugins.detail.badge —— 失配插件详情页标题旁的「补丁未加载」徽章
|
|
6
|
+
* plugins.detail.section —— 失配插件详情页底部的适配卡片
|
|
7
|
+
* plugins.bundle.config (key=dsh-patchkeeper) —— patchkeeper 自己详情页的补丁管理台
|
|
8
|
+
* shell.overlay —— 开机绿泡泡(正向确认)/ 常驻红黄牌警告
|
|
9
|
+
*
|
|
10
|
+
* 数据来源:Node 侧 /dsh-patchkeeper-api(GET 清单 / POST 操作)。
|
|
11
|
+
*/
|
|
12
|
+
window.__ModuleLoader__.load({
|
|
13
|
+
id: 'dsh-patchkeeper',
|
|
14
|
+
factory: (require) => {
|
|
15
|
+
const React = require('react')
|
|
16
|
+
const h = React.createElement
|
|
17
|
+
const API = '/dsh-patchkeeper-api'
|
|
18
|
+
|
|
19
|
+
// 需要在详情页挂牌的异常状态(disabled 是用户主动关的,不算异常)
|
|
20
|
+
const BAD_STATUSES = ['version-mismatch', 'conflict', 'apply-failed', 'missing-target']
|
|
21
|
+
|
|
22
|
+
async function fetchList() {
|
|
23
|
+
try {
|
|
24
|
+
const res = await fetch(API)
|
|
25
|
+
if (!res.ok) return null
|
|
26
|
+
return await res.json()
|
|
27
|
+
} catch {
|
|
28
|
+
return null
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function postAction(action, name, extra) {
|
|
33
|
+
const res = await fetch(API, {
|
|
34
|
+
method: 'POST',
|
|
35
|
+
headers: { 'Content-Type': 'application/json' },
|
|
36
|
+
body: JSON.stringify({ action, name, ...(extra || {}) }),
|
|
37
|
+
})
|
|
38
|
+
return res.json()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 取某个插件的异常补丁记录(无异常 → null → 不渲染)
|
|
42
|
+
function useMismatch(subject) {
|
|
43
|
+
const [tick, setTick] = React.useState(0)
|
|
44
|
+
const [data, setData] = React.useState(null)
|
|
45
|
+
React.useEffect(() => {
|
|
46
|
+
let live = true
|
|
47
|
+
fetchList().then((s) => live && setData(s))
|
|
48
|
+
return () => {
|
|
49
|
+
live = false
|
|
50
|
+
}
|
|
51
|
+
}, [tick])
|
|
52
|
+
const pkgName = subject && subject.kind === 'bundle' ? subject.pkg && subject.pkg.name : null
|
|
53
|
+
const record =
|
|
54
|
+
pkgName && data && Array.isArray(data.patches)
|
|
55
|
+
? data.patches.find((r) => r.name === pkgName && BAD_STATUSES.includes(r.status)) || null
|
|
56
|
+
: null
|
|
57
|
+
return { record, refresh: () => setTick((t) => t + 1) }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const badgeStyle = {
|
|
61
|
+
display: 'inline-block',
|
|
62
|
+
marginLeft: 8,
|
|
63
|
+
padding: '1px 8px',
|
|
64
|
+
borderRadius: 8,
|
|
65
|
+
fontSize: 12,
|
|
66
|
+
lineHeight: '18px',
|
|
67
|
+
color: '#8a5a00',
|
|
68
|
+
background: 'rgba(250, 173, 20, 0.18)',
|
|
69
|
+
border: '1px solid rgba(250, 173, 20, 0.45)',
|
|
70
|
+
verticalAlign: 'middle',
|
|
71
|
+
whiteSpace: 'nowrap',
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function MismatchBadge(props) {
|
|
75
|
+
const { record } = useMismatch(props.subject)
|
|
76
|
+
if (!record) return null
|
|
77
|
+
return h('span', { style: badgeStyle, title: `补丁版本 ${record.version},已装版本 ${record.installedVersion || '未知'}` }, '补丁未加载')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const cardStyle = {
|
|
81
|
+
margin: '12px 0',
|
|
82
|
+
padding: '12px 14px',
|
|
83
|
+
borderRadius: 10,
|
|
84
|
+
border: '1px solid rgba(250, 173, 20, 0.45)',
|
|
85
|
+
background: 'rgba(250, 173, 20, 0.08)',
|
|
86
|
+
fontSize: 13,
|
|
87
|
+
lineHeight: 1.7,
|
|
88
|
+
}
|
|
89
|
+
const btnStyle = {
|
|
90
|
+
marginRight: 8,
|
|
91
|
+
marginTop: 8,
|
|
92
|
+
padding: '4px 12px',
|
|
93
|
+
borderRadius: 6,
|
|
94
|
+
border: '1px solid rgba(250, 173, 20, 0.6)',
|
|
95
|
+
background: 'transparent',
|
|
96
|
+
color: 'inherit',
|
|
97
|
+
cursor: 'pointer',
|
|
98
|
+
fontSize: 13,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function MismatchSection(props) {
|
|
102
|
+
const { record, refresh } = useMismatch(props.subject)
|
|
103
|
+
const [busy, setBusy] = React.useState('')
|
|
104
|
+
const [msg, setMsg] = React.useState(null)
|
|
105
|
+
if (!record) return null
|
|
106
|
+
|
|
107
|
+
const run = async (action) => {
|
|
108
|
+
setBusy(action)
|
|
109
|
+
setMsg(null)
|
|
110
|
+
try {
|
|
111
|
+
const j = await postAction(action, record.name)
|
|
112
|
+
if (j.error) {
|
|
113
|
+
setMsg(`失败:${j.error}`)
|
|
114
|
+
} else if (action === 'adapt') {
|
|
115
|
+
const r = j.result && j.result.results && j.result.results[0]
|
|
116
|
+
const st = r && r.status
|
|
117
|
+
setMsg(
|
|
118
|
+
st === 'applied-now'
|
|
119
|
+
? '已强行打上旧补丁(有 fuzz 风险),重启 DSH 后生效;若插件异常请在管理台禁用。'
|
|
120
|
+
: st === 'applied'
|
|
121
|
+
? '补丁其实已处于应用状态。'
|
|
122
|
+
: `未能应用(${st || '未知'}),请手动合并 rejects/ 后用「以当前代码重建补丁」。`,
|
|
123
|
+
)
|
|
124
|
+
} else if (action === 'extract') {
|
|
125
|
+
const r = j.result && j.result.results && j.result.results[0]
|
|
126
|
+
setMsg(r && r.error ? `重建失败:${r.error}` : '已按当前代码重建补丁并入库,重启 DSH 后自动生效。')
|
|
127
|
+
} else {
|
|
128
|
+
setMsg('已回退该插件的补丁。')
|
|
129
|
+
}
|
|
130
|
+
} catch (err) {
|
|
131
|
+
setMsg(`请求失败:${err && err.message ? err.message : err}`)
|
|
132
|
+
} finally {
|
|
133
|
+
setBusy('')
|
|
134
|
+
refresh()
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const statusText = {
|
|
139
|
+
'version-mismatch': `插件已升级到 ${record.installedVersion || '?'},补丁是按 ${record.version} 做的,为避免加载失败本次未打。`,
|
|
140
|
+
conflict: '补丁与当前代码冲突,未能应用。',
|
|
141
|
+
'apply-failed': '补丁应用失败,reject 详情见数据目录 rejects/。',
|
|
142
|
+
'missing-target': '补丁目标插件当前未安装。',
|
|
143
|
+
}[record.status] || record.status
|
|
144
|
+
|
|
145
|
+
return h('section', { style: cardStyle }, [
|
|
146
|
+
h('div', { key: 't', style: { fontWeight: 600 } }, '⚠ 本地补丁未加载'),
|
|
147
|
+
h('div', { key: 'd' }, statusText),
|
|
148
|
+
h('div', { key: 'b' }, [
|
|
149
|
+
h(
|
|
150
|
+
'button',
|
|
151
|
+
{ key: 'adapt', style: btnStyle, disabled: !!busy, onClick: () => run('adapt') },
|
|
152
|
+
busy === 'adapt' ? '适配中…' : '尝试重新适配(force)',
|
|
153
|
+
),
|
|
154
|
+
h(
|
|
155
|
+
'button',
|
|
156
|
+
{ key: 'extract', style: btnStyle, disabled: !!busy, onClick: () => run('extract') },
|
|
157
|
+
busy === 'extract' ? '重建中…' : '以当前代码重建补丁',
|
|
158
|
+
),
|
|
159
|
+
]),
|
|
160
|
+
msg ? h('div', { key: 'm', style: { marginTop: 8, opacity: 0.85 } }, msg) : null,
|
|
161
|
+
])
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ---------- patchkeeper 自己的详情页:补丁管理台 ----------
|
|
165
|
+
|
|
166
|
+
function statusOf(p, bootTs) {
|
|
167
|
+
if (p.mounted && p.mountedAt && bootTs && Date.parse(p.mountedAt) > bootTs) return ['✓ 已挂载·重启后生效', '#d29922']
|
|
168
|
+
return statusMeta[p.status] || [p.status, '#8b8b8b']
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const statusMeta = {
|
|
172
|
+
applied: ['· 挂载中', '#2ea043'],
|
|
173
|
+
'applied-now': ['✓ 刚挂载', '#2ea043'],
|
|
174
|
+
'not-applied': ['○ 未挂载', '#8b8b8b'],
|
|
175
|
+
disabled: ['⏸ 已禁用', '#8b8b8b'],
|
|
176
|
+
'version-mismatch': ['⊘ 版本不匹配', '#d29922'],
|
|
177
|
+
conflict: ['✗ 冲突', '#dc3c3c'],
|
|
178
|
+
'apply-failed': ['✗ 挂载失败', '#dc3c3c'],
|
|
179
|
+
'missing-target': ['✗ 目标缺失', '#dc3c3c'],
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const toggleStyle = (on) => ({
|
|
183
|
+
width: 36,
|
|
184
|
+
height: 20,
|
|
185
|
+
borderRadius: 10,
|
|
186
|
+
border: 'none',
|
|
187
|
+
cursor: 'pointer',
|
|
188
|
+
background: on ? '#2ea043' : 'rgba(128,128,128,0.4)',
|
|
189
|
+
position: 'relative',
|
|
190
|
+
flexShrink: 0,
|
|
191
|
+
transition: 'background 0.15s',
|
|
192
|
+
})
|
|
193
|
+
const knobStyle = (on) => ({
|
|
194
|
+
position: 'absolute',
|
|
195
|
+
top: 2,
|
|
196
|
+
left: on ? 18 : 2,
|
|
197
|
+
width: 16,
|
|
198
|
+
height: 16,
|
|
199
|
+
borderRadius: 8,
|
|
200
|
+
background: '#fff',
|
|
201
|
+
transition: 'left 0.15s',
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
// 常驻红字维测:失败状态必须给出足够的诊断信息
|
|
205
|
+
function diagOf(p) {
|
|
206
|
+
switch (p.status) {
|
|
207
|
+
case 'version-mismatch':
|
|
208
|
+
return `未挂载:补丁按 v${p.version} 制作,当前安装 v${p.installedVersion || '?'},版本闸已拦截(防止旧补丁打崩新代码)。点「尝试重新适配」或改补丁后 remount。`
|
|
209
|
+
case 'conflict':
|
|
210
|
+
return `挂载失败:补丁与当前代码冲突${p.lastError ? `(${p.lastError})` : ''},reject 详情见数据目录 rejects/。`
|
|
211
|
+
case 'apply-failed':
|
|
212
|
+
return `挂载失败${p.lastError ? `:${p.lastError}` : '(原因见 state.json)'}`
|
|
213
|
+
case 'missing-target':
|
|
214
|
+
return '目标插件未安装,补丁悬空。'
|
|
215
|
+
default:
|
|
216
|
+
return null
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function PatchRow({ p, busy, op, bootTs, onToggle, onSaveNote }) {
|
|
221
|
+
const [note, setNote] = React.useState(p.note || '')
|
|
222
|
+
const [dirty, setDirty] = React.useState(false)
|
|
223
|
+
const meta = statusOf(p, bootTs)
|
|
224
|
+
const diag = diagOf(p)
|
|
225
|
+
return h(
|
|
226
|
+
'div',
|
|
227
|
+
{
|
|
228
|
+
style: {
|
|
229
|
+
padding: '10px 0',
|
|
230
|
+
borderBottom: '1px solid rgba(128,128,128,0.2)',
|
|
231
|
+
display: 'flex',
|
|
232
|
+
gap: 12,
|
|
233
|
+
alignItems: 'flex-start',
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
h(
|
|
237
|
+
'button',
|
|
238
|
+
{
|
|
239
|
+
style: toggleStyle(p.enabled),
|
|
240
|
+
title: p.enabled ? '点击禁用(恢复原插件、清除副本)' : '启用(创建副本并挂载)',
|
|
241
|
+
disabled: busy,
|
|
242
|
+
onClick: () => onToggle(p),
|
|
243
|
+
},
|
|
244
|
+
h('span', { style: knobStyle(p.enabled) }),
|
|
245
|
+
),
|
|
246
|
+
h('div', { style: { flex: 1, minWidth: 0 } }, [
|
|
247
|
+
h('div', { key: 'n', style: { fontWeight: 600, fontSize: 13 } }, [
|
|
248
|
+
`${p.name} `,
|
|
249
|
+
h('span', { key: 'v', style: { fontWeight: 400, opacity: 0.7, fontSize: 12 } }, `补丁 ${p.version} / 已装 ${p.installedVersion || '?'}`),
|
|
250
|
+
]),
|
|
251
|
+
h('div', { key: 's', style: { fontSize: 12, color: meta[1], marginTop: 2 } }, meta[0] + (p.files?.length ? ` · ${p.files.length} 个文件` : '')),
|
|
252
|
+
p.files?.length
|
|
253
|
+
? h('div', { key: 'f', style: { fontSize: 11, opacity: 0.6, marginTop: 2, wordBreak: 'break-all' } }, p.files.join('、'))
|
|
254
|
+
: null,
|
|
255
|
+
diag ? h('div', { key: 'diag', style: { fontSize: 12, color: '#dc3c3c', marginTop: 4, lineHeight: 1.6 } }, diag) : null,
|
|
256
|
+
p.risk ? h('div', { key: 'risk', style: { fontSize: 12, color: '#d29922', marginTop: 4, lineHeight: 1.6 } }, `⚠ 禁用风险:${p.risk}`) : null,
|
|
257
|
+
op
|
|
258
|
+
? h('div', { key: 'op', style: { fontSize: 12, fontWeight: 600, marginTop: 4, color: op.ok ? '#2ea043' : '#dc3c3c' } }, op.text)
|
|
259
|
+
: null,
|
|
260
|
+
h('div', { key: 'note', style: { display: 'flex', gap: 6, marginTop: 6 } }, [
|
|
261
|
+
h('input', {
|
|
262
|
+
key: 'i',
|
|
263
|
+
value: note,
|
|
264
|
+
placeholder: '补丁效果说明(点击填写…)',
|
|
265
|
+
onChange: (e) => {
|
|
266
|
+
setNote(e.target.value)
|
|
267
|
+
setDirty(true)
|
|
268
|
+
},
|
|
269
|
+
style: {
|
|
270
|
+
flex: 1,
|
|
271
|
+
fontSize: 12,
|
|
272
|
+
padding: '3px 8px',
|
|
273
|
+
borderRadius: 6,
|
|
274
|
+
border: '1px solid rgba(128,128,128,0.35)',
|
|
275
|
+
background: 'transparent',
|
|
276
|
+
color: 'inherit',
|
|
277
|
+
},
|
|
278
|
+
}),
|
|
279
|
+
dirty
|
|
280
|
+
? h(
|
|
281
|
+
'button',
|
|
282
|
+
{
|
|
283
|
+
key: 'sv',
|
|
284
|
+
style: { ...btnStyle, marginTop: 0, padding: '2px 10px', fontSize: 12 },
|
|
285
|
+
disabled: busy,
|
|
286
|
+
onClick: () => onSaveNote(p, note).then(() => setDirty(false)),
|
|
287
|
+
},
|
|
288
|
+
'保存',
|
|
289
|
+
)
|
|
290
|
+
: null,
|
|
291
|
+
]),
|
|
292
|
+
]),
|
|
293
|
+
)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function PatchPanel() {
|
|
297
|
+
const [data, setData] = React.useState(null)
|
|
298
|
+
const [busy, setBusy] = React.useState(false)
|
|
299
|
+
const [hint, setHint] = React.useState(null)
|
|
300
|
+
const [ops, setOps] = React.useState({}) // 每个补丁最近一次操作的结果反馈
|
|
301
|
+
const refresh = () => fetchList().then(setData)
|
|
302
|
+
React.useEffect(() => {
|
|
303
|
+
refresh()
|
|
304
|
+
}, [])
|
|
305
|
+
if (!data) return h('div', { style: { padding: 8, opacity: 0.6 } }, '补丁清单加载中…')
|
|
306
|
+
const patches = data.patches || []
|
|
307
|
+
|
|
308
|
+
const setOp = (name, ok, text) => setOps((m) => ({ ...m, [name]: { ok, text } }))
|
|
309
|
+
|
|
310
|
+
const act = async (action, p, extra) => {
|
|
311
|
+
setBusy(true)
|
|
312
|
+
setHint(null)
|
|
313
|
+
try {
|
|
314
|
+
const j = await postAction(action, p.name, extra)
|
|
315
|
+
if (j.error) {
|
|
316
|
+
setOp(p.name, false, `操作失败:${j.error}`)
|
|
317
|
+
} else {
|
|
318
|
+
const r = j.result && j.result.results && j.result.results[0]
|
|
319
|
+
const live = j.reload === 'reloaded'
|
|
320
|
+
const suffix = live ? '(已热重载,即时生效)' : '(后端代码重启 DSH 后切换)'
|
|
321
|
+
if (action === 'enable') {
|
|
322
|
+
if (r && (r.status === 'applied-now' || r.status === 'applied')) setOp(p.name, true, `✓ 已启用:副本已创建并挂载${suffix}`)
|
|
323
|
+
else setOp(p.name, false, `✗ 启用失败:${(r && (r.error || r.status)) || '未知原因'}`)
|
|
324
|
+
} else if (action === 'disable') {
|
|
325
|
+
if (r && (r.status === 'reverted' || r.status === 'not-applied')) setOp(p.name, true, `✓ 已禁用:恢复原插件、副本已清除${suffix}`)
|
|
326
|
+
else setOp(p.name, false, `✗ 禁用失败:${(r && (r.error || r.status)) || '未知原因'}`)
|
|
327
|
+
} else if (action === 'note') {
|
|
328
|
+
setOp(p.name, true, '✓ 说明已保存')
|
|
329
|
+
}
|
|
330
|
+
if (j.list) setData(j.list)
|
|
331
|
+
}
|
|
332
|
+
} catch (err) {
|
|
333
|
+
setOp(p.name, false, `请求失败:${err && err.message ? err.message : err}`)
|
|
334
|
+
} finally {
|
|
335
|
+
setBusy(false)
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return h('div', { style: { fontSize: 13 } }, [
|
|
340
|
+
data.alarm
|
|
341
|
+
? h('div', { key: 'alarm', style: { ...cardStyle, borderColor: 'rgba(220,60,60,0.5)', background: 'rgba(220,60,60,0.1)', fontWeight: 600 } }, data.alarm)
|
|
342
|
+
: null,
|
|
343
|
+
patches.length
|
|
344
|
+
? h('div', { key: 'rows' }, patches.map((p) =>
|
|
345
|
+
h(PatchRow, {
|
|
346
|
+
key: p.name,
|
|
347
|
+
p,
|
|
348
|
+
busy,
|
|
349
|
+
op: ops[p.name],
|
|
350
|
+
bootTs: data.bootTs,
|
|
351
|
+
onToggle: (pp) => {
|
|
352
|
+
if (pp.enabled && pp.risk && !window.confirm(`禁用风险:${pp.risk}\n\n确认禁用?`)) return
|
|
353
|
+
act(pp.enabled ? 'disable' : 'enable', pp)
|
|
354
|
+
},
|
|
355
|
+
onSaveNote: (pp, note) => act('note', pp, { note }),
|
|
356
|
+
}),
|
|
357
|
+
))
|
|
358
|
+
: h('div', { key: 'empty', style: { opacity: 0.6, padding: '8px 0' } }, '补丁库为空——改了插件代码后跑 extract 入库。'),
|
|
359
|
+
hint ? h('div', { key: 'hint', style: { marginTop: 8, fontSize: 12, opacity: 0.75 } }, hint) : null,
|
|
360
|
+
h('div', { key: 'ft', style: { marginTop: 10, fontSize: 11, opacity: 0.55 } },
|
|
361
|
+
'补丁库:~/.dsh/patch-manager/patches(git 管理)· 副本挂载于运行期,原版插件永远干净'),
|
|
362
|
+
])
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// ---------- 开机 toast ----------
|
|
366
|
+
|
|
367
|
+
function BootToast() {
|
|
368
|
+
const [info, setInfo] = React.useState(null)
|
|
369
|
+
const [gone, setGone] = React.useState(false)
|
|
370
|
+
React.useEffect(() => {
|
|
371
|
+
let live = true
|
|
372
|
+
fetchList().then((s) => {
|
|
373
|
+
if (!live || !s) return
|
|
374
|
+
if (s.alarm) {
|
|
375
|
+
setInfo({ level: 'alarm', text: s.alarm })
|
|
376
|
+
return
|
|
377
|
+
}
|
|
378
|
+
const patches = Array.isArray(s.patches) ? s.patches : []
|
|
379
|
+
const bootTs = s.bootTs || 0
|
|
380
|
+
const effective = patches.filter((r) => r.mounted && r.mountedAt && Date.parse(r.mountedAt) <= bootTs).length
|
|
381
|
+
const pending = patches.filter((r) => r.mounted && (!r.mountedAt || Date.parse(r.mountedAt) > bootTs)).length
|
|
382
|
+
const bad = patches.filter((r) => BAD_STATUSES.includes(r.status)).length
|
|
383
|
+
if (bad) setInfo({ level: 'warn', text: `补丁管家:${bad} 个补丁未挂载,请到插件管理页处理` })
|
|
384
|
+
else if (pending) setInfo({ level: 'ok', text: `补丁管家:${pending} 个补丁已挂载(本轮启动的冻结快照更早,重启后生效)` })
|
|
385
|
+
else if (effective) setInfo({ level: 'ok', text: `补丁管家:${effective} 个补丁生效中 ✓` })
|
|
386
|
+
})
|
|
387
|
+
return () => {
|
|
388
|
+
live = false
|
|
389
|
+
}
|
|
390
|
+
}, [])
|
|
391
|
+
React.useEffect(() => {
|
|
392
|
+
if (info && info.level === 'ok') {
|
|
393
|
+
const t = setTimeout(() => setGone(true), 6000)
|
|
394
|
+
return () => clearTimeout(t)
|
|
395
|
+
}
|
|
396
|
+
}, [info])
|
|
397
|
+
if (!info || gone) return null
|
|
398
|
+
const palette = {
|
|
399
|
+
ok: { bg: 'rgba(46, 160, 67, 0.95)', fg: '#fff' },
|
|
400
|
+
warn: { bg: 'rgba(250, 173, 20, 0.95)', fg: '#3a2a00' },
|
|
401
|
+
alarm: { bg: 'rgba(220, 60, 60, 0.95)', fg: '#fff' },
|
|
402
|
+
}[info.level]
|
|
403
|
+
return h(
|
|
404
|
+
'div',
|
|
405
|
+
{
|
|
406
|
+
style: {
|
|
407
|
+
position: 'fixed',
|
|
408
|
+
top: 16,
|
|
409
|
+
right: 16,
|
|
410
|
+
zIndex: 99999,
|
|
411
|
+
padding: '10px 14px',
|
|
412
|
+
borderRadius: 10,
|
|
413
|
+
background: palette.bg,
|
|
414
|
+
color: palette.fg,
|
|
415
|
+
fontSize: 13,
|
|
416
|
+
lineHeight: 1.5,
|
|
417
|
+
maxWidth: 360,
|
|
418
|
+
boxShadow: '0 4px 16px rgba(0,0,0,0.25)',
|
|
419
|
+
display: 'flex',
|
|
420
|
+
gap: 10,
|
|
421
|
+
alignItems: 'flex-start',
|
|
422
|
+
},
|
|
423
|
+
},
|
|
424
|
+
h('span', { style: { flex: 1 } }, info.text),
|
|
425
|
+
h(
|
|
426
|
+
'button',
|
|
427
|
+
{
|
|
428
|
+
style: { background: 'none', border: 'none', color: palette.fg, cursor: 'pointer', fontSize: 14, padding: 0 },
|
|
429
|
+
onClick: () => setGone(true),
|
|
430
|
+
},
|
|
431
|
+
'×',
|
|
432
|
+
),
|
|
433
|
+
)
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function apply(ctx) {
|
|
437
|
+
const slots = ctx.slots ?? (typeof ctx.get === 'function' ? ctx.get('slots') : null)
|
|
438
|
+
if (!slots || typeof slots.inject !== 'function') return
|
|
439
|
+
// slots.inject:槽被宿主声明时才注册,未声明时安全静默(宿主版本差异不炸 UI)
|
|
440
|
+
slots.inject('plugins.detail.badge', () => {
|
|
441
|
+
try {
|
|
442
|
+
return slots.register({ name: 'plugins.detail.badge', id: 'patchkeeper-mismatch', order: 100 }, MismatchBadge)
|
|
443
|
+
} catch {
|
|
444
|
+
return () => {}
|
|
445
|
+
}
|
|
446
|
+
})
|
|
447
|
+
slots.inject('plugins.detail.section', () => {
|
|
448
|
+
try {
|
|
449
|
+
return slots.register({ name: 'plugins.detail.section', id: 'patchkeeper-mismatch', order: 100 }, MismatchSection)
|
|
450
|
+
} catch {
|
|
451
|
+
return () => {}
|
|
452
|
+
}
|
|
453
|
+
})
|
|
454
|
+
slots.inject('plugins.bundle.config', () => {
|
|
455
|
+
try {
|
|
456
|
+
return slots.register({ name: 'plugins.bundle.config', key: '@alequet/dsh-patchkeeper' }, PatchPanel)
|
|
457
|
+
} catch {
|
|
458
|
+
return () => {}
|
|
459
|
+
}
|
|
460
|
+
})
|
|
461
|
+
slots.inject('shell.overlay', () => {
|
|
462
|
+
try {
|
|
463
|
+
return slots.register({ name: 'shell.overlay', id: 'patchkeeper-boot-toast', order: 100 }, BootToast)
|
|
464
|
+
} catch {
|
|
465
|
+
return () => {}
|
|
466
|
+
}
|
|
467
|
+
})
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
return { name: 'dsh-patchkeeper', inject: ['slots'], apply }
|
|
471
|
+
},
|
|
472
|
+
})
|
package/cordis.patch.yml
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-patchkeeper — cordis 插件壳(常驻挂载模式)。
|
|
3
|
+
*
|
|
4
|
+
* 生命周期(0.5.0 起改为常驻):
|
|
5
|
+
* DSH 启动时先用 createRuntimeResolution 冻结包路径快照,再加载插件——
|
|
6
|
+
* 所以补丁必须在进程启动前就挂在磁盘上,「退出时回退」会让补丁永远赶不上快照。
|
|
7
|
+
* 挂载常驻:启动时只做保底清除(崩溃残留/升级替换的修复)+ 版本匹配检查补挂。
|
|
8
|
+
* 升级安全由结构保证:原版完整备份在数据目录 shadows/orig/,node_modules 条目只是
|
|
9
|
+
* 一根软链,pnpm/安装器随时可以用新包替换它(保底清除会识别并丢弃过时备份)。
|
|
10
|
+
*
|
|
11
|
+
* UI 通道:注册 /dsh-patchkeeper-api
|
|
12
|
+
* GET → 实时清单(插件管理页的徽章/提示卡/管理台取数)
|
|
13
|
+
* POST {action, name} → adapt / extract / revert / enable / disable / note
|
|
14
|
+
*/
|
|
15
|
+
import { spawnSync } from 'node:child_process'
|
|
16
|
+
import path from 'node:path'
|
|
17
|
+
import { fileURLToPath } from 'node:url'
|
|
18
|
+
|
|
19
|
+
export const name = 'dsh-patchkeeper'
|
|
20
|
+
// 本进程(≈冻结快照)时刻:快照之后挂载的补丁要下次启动才进运行代码
|
|
21
|
+
const BOOT_TS = Date.now()
|
|
22
|
+
// cordis 规矩:访问 ctx 上的服务必须先在这里声明;声明后服务不可用时仍为 null(参照 dsh-meme 同款写法)
|
|
23
|
+
export const inject = ['webServer']
|
|
24
|
+
|
|
25
|
+
const CLI = path.resolve(path.dirname(fileURLToPath(import.meta.url)), 'bin', 'cli.mjs')
|
|
26
|
+
const ROUTE = '/dsh-patchkeeper-api'
|
|
27
|
+
|
|
28
|
+
function runCli(logger, args, timeoutMs = 180_000) {
|
|
29
|
+
let res
|
|
30
|
+
try {
|
|
31
|
+
res = spawnSync(process.execPath, [CLI, ...args, '--json'], {
|
|
32
|
+
encoding: 'utf8',
|
|
33
|
+
timeout: timeoutMs,
|
|
34
|
+
})
|
|
35
|
+
} catch (err) {
|
|
36
|
+
logger.warn(`patchkeeper ${args[0]} 执行失败: ${err.message}`)
|
|
37
|
+
return null
|
|
38
|
+
}
|
|
39
|
+
if (res.status !== 0 && res.status !== 2) {
|
|
40
|
+
logger.warn(`patchkeeper ${args[0]} 异常退出 (${res.status}): ${(res.stderr || '').slice(0, 500)}`)
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
return JSON.parse(res.stdout)
|
|
45
|
+
} catch {
|
|
46
|
+
logger.warn(`patchkeeper ${args[0]} 输出无法解析: ${(res.stdout || '').slice(0, 300)}`)
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function applyPatches(logger) {
|
|
52
|
+
const out = runCli(logger, ['apply'])
|
|
53
|
+
if (!out) return
|
|
54
|
+
const applied = out.results.filter((r) => r.status === 'applied-now')
|
|
55
|
+
const skipped = out.results.filter((r) => r.status === 'version-mismatch')
|
|
56
|
+
const bad = out.results.filter((r) => ['conflict', 'missing-target', 'apply-failed'].includes(r.status))
|
|
57
|
+
if (applied.length) {
|
|
58
|
+
logger.info(`已为 ${applied.length} 个插件热打补丁: ${applied.map((r) => r.name).join(', ')}`)
|
|
59
|
+
}
|
|
60
|
+
if (skipped.length) {
|
|
61
|
+
logger.info(
|
|
62
|
+
`${skipped.length} 个补丁因版本不匹配未加载: ${skipped.map((r) => `${r.name} 补丁${r.patchVersion}/已装${r.installedVersion}`).join(', ')}——请到插件管理页处理`,
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
for (const r of bad) {
|
|
66
|
+
logger.warn(`补丁未打上: ${r.name} [${r.status}](补丁版本 ${r.patchVersion} / 已装 ${r.installedVersion ?? '无'})`)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 挂载变更后尝试借插件管理器的机器即时生效:
|
|
71
|
+
// 刷新 resolution 快照 + HMR 重载。任一环节缺席/失败就退回「重启后生效」。
|
|
72
|
+
async function tryLiveReload(ctx, logger) {
|
|
73
|
+
try {
|
|
74
|
+
const hmr = typeof ctx.get === 'function' ? ctx.get('hmr') : null
|
|
75
|
+
const packages = typeof ctx.get === 'function' ? ctx.get('pluginPackages') : null
|
|
76
|
+
if (!hmr || typeof hmr.runExclusive !== 'function' || typeof hmr.reconcileProfile !== 'function') return 'restart-required'
|
|
77
|
+
if (packages?.resolution !== undefined && typeof packages.refresh === 'function') {
|
|
78
|
+
try {
|
|
79
|
+
await packages.refresh()
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (!String(err?.name || err).includes('ResolutionRestartRequired')) throw err
|
|
82
|
+
return 'restart-required'
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
await hmr.runExclusive(() => hmr.reconcileProfile([]))
|
|
86
|
+
return 'reloaded'
|
|
87
|
+
} catch (err) {
|
|
88
|
+
logger.warn(`热重载未完成(重启后生效): ${err?.message || err}`)
|
|
89
|
+
return 'restart-required'
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ---- UI HTTP 通道 ----
|
|
94
|
+
|
|
95
|
+
function sendJson(res, code, obj) {
|
|
96
|
+
res.writeHead(code, { 'Content-Type': 'application/json; charset=utf-8' })
|
|
97
|
+
res.end(JSON.stringify(obj))
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function readBody(req) {
|
|
101
|
+
return new Promise((resolve) => {
|
|
102
|
+
let data = ''
|
|
103
|
+
req.on('data', (c) => {
|
|
104
|
+
data += c
|
|
105
|
+
if (data.length > 64 * 1024) req.destroy()
|
|
106
|
+
})
|
|
107
|
+
req.on('end', () => resolve(data))
|
|
108
|
+
req.on('error', () => resolve(''))
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function registerApi(ctx, logger) {
|
|
113
|
+
const webServer = ctx.webServer ?? ctx.get('webServer')
|
|
114
|
+
if (!webServer) {
|
|
115
|
+
logger.info('webServer 服务不可用,插件管理页提示卡不生效(补丁功能本身不受影响)')
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
ctx.effect(() =>
|
|
119
|
+
webServer.register({
|
|
120
|
+
kind: 'prefix',
|
|
121
|
+
path: ROUTE,
|
|
122
|
+
async handler(req, res) {
|
|
123
|
+
try {
|
|
124
|
+
if (req.method === 'GET') {
|
|
125
|
+
const out = runCli(logger, ['list'])
|
|
126
|
+
return sendJson(res, 200, { ...(out || { patches: [] }), bootTs: BOOT_TS })
|
|
127
|
+
}
|
|
128
|
+
if (req.method === 'POST') {
|
|
129
|
+
let payload = {}
|
|
130
|
+
try {
|
|
131
|
+
payload = JSON.parse(await readBody(req))
|
|
132
|
+
} catch {
|
|
133
|
+
return sendJson(res, 400, { error: 'bad json' })
|
|
134
|
+
}
|
|
135
|
+
const { action, name: pkgName, note } = payload
|
|
136
|
+
if (!pkgName || typeof pkgName !== 'string') return sendJson(res, 400, { error: 'missing name' })
|
|
137
|
+
let out
|
|
138
|
+
if (action === 'adapt') out = runCli(logger, ['apply', '--force', '--pkg', pkgName])
|
|
139
|
+
else if (action === 'extract') out = runCli(logger, ['extract', '--pkg', pkgName])
|
|
140
|
+
else if (action === 'revert') out = runCli(logger, ['revert', '--pkg', pkgName])
|
|
141
|
+
else if (action === 'enable') out = runCli(logger, ['enable', '--pkg', pkgName])
|
|
142
|
+
else if (action === 'disable') out = runCli(logger, ['disable', '--pkg', pkgName])
|
|
143
|
+
else if (action === 'note') out = runCli(logger, ['note', '--pkg', pkgName, String(payload.note ?? '')])
|
|
144
|
+
else return sendJson(res, 400, { error: 'unknown action' })
|
|
145
|
+
if (!out) return sendJson(res, 500, { error: 'cli failed' })
|
|
146
|
+
// 挂载类操作成功后,借插件管理器的 HMR 机器尝试即时生效
|
|
147
|
+
let reload = null
|
|
148
|
+
if (['enable', 'disable', 'adapt', 'revert'].includes(action)) {
|
|
149
|
+
reload = await tryLiveReload(ctx, logger)
|
|
150
|
+
}
|
|
151
|
+
// 操作后附上一份最新清单,前端直接刷新
|
|
152
|
+
const list = runCli(logger, ['list'])
|
|
153
|
+
return sendJson(res, 200, { action, result: out, reload, list })
|
|
154
|
+
}
|
|
155
|
+
sendJson(res, 405, { error: 'method not allowed' })
|
|
156
|
+
} catch (err) {
|
|
157
|
+
sendJson(res, 500, { error: String(err?.message || err) })
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
}),
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function apply(ctx, config = {}) {
|
|
165
|
+
if (config.enabled === false) return
|
|
166
|
+
const logger = ctx.logger('patchkeeper')
|
|
167
|
+
|
|
168
|
+
// 启动:保底清除 + 版本匹配补挂。挂载是常驻的——本进程的冻结快照已在用它们。
|
|
169
|
+
applyPatches(logger)
|
|
170
|
+
|
|
171
|
+
// UI 通道
|
|
172
|
+
registerApi(ctx, logger)
|
|
173
|
+
|
|
174
|
+
// 注意:退出时【不要】revert。冻结快照机制要求补丁在下次进程启动前就在磁盘上;
|
|
175
|
+
// 常驻挂载下,原版由 shadows/orig/ 备份保护,升级器替换软链后启动清理会自动归位。
|
|
176
|
+
}
|