@lingda-ai/dsh-plugin-vault 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +117 -2
- package/bin/dsh-vault.mjs +189 -0
- package/cordis.patch.yml +7 -0
- package/icon.svg +7 -0
- package/index.js +84 -0
- package/lib/client.js +369 -0
- package/lib/route.js +70 -0
- package/lib/scope.js +125 -0
- package/lib/tool.js +60 -0
- package/lib/vault.js +268 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +83 -4
package/lib/vault.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 机库的活:备份、校验、对比、回滚。
|
|
3
|
+
*
|
|
4
|
+
* 四个动作都围绕同一份 manifest,且都不碰"备份目录之外的任何东西":
|
|
5
|
+
* create 读 home 下的选中文件 → 复制进 <vault>/<id>/files/ → 写 manifest
|
|
6
|
+
* verify 重新算 hash,告诉你"备份里那份还在不在、当前文件有没有变"
|
|
7
|
+
* diff 当前状态 vs 某个备份:改了哪些、少了哪些、多了哪些
|
|
8
|
+
* restore 先给当前状态拍一份新备份,再按 manifest 逐文件写回
|
|
9
|
+
*
|
|
10
|
+
* 为什么 restore 要做 pre-backup:
|
|
11
|
+
* 回滚是唯一会覆盖用户文件的动作。哪怕用户点错了备份,也得能退回来。
|
|
12
|
+
*/
|
|
13
|
+
import {
|
|
14
|
+
copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync,
|
|
15
|
+
} from 'node:fs'
|
|
16
|
+
import { createHash } from 'node:crypto'
|
|
17
|
+
import { dirname, join } from 'node:path'
|
|
18
|
+
import { selectFiles, isSecretName, defaultVaultDir } from './scope.js'
|
|
19
|
+
|
|
20
|
+
export const MANIFEST = 'manifest.json'
|
|
21
|
+
|
|
22
|
+
function sha256(buffer) {
|
|
23
|
+
return createHash('sha256').update(buffer).digest('hex')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function stampOf(at) {
|
|
27
|
+
const d = new Date(at)
|
|
28
|
+
const pad = (n) => String(n).padStart(2, '0')
|
|
29
|
+
return `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}-${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 备份内文件路径必须相对、且不含 ..,否则拒绝(防 manifest 被改坏导致越界写)。 */
|
|
33
|
+
export function safeRelative(path) {
|
|
34
|
+
if (typeof path !== 'string' || path.length === 0) return null
|
|
35
|
+
if (path.startsWith('/') || path.includes('\\')) return null
|
|
36
|
+
const parts = path.split('/')
|
|
37
|
+
if (parts.some((part) => part === '..' || part === '' || part === '.')) return null
|
|
38
|
+
return path
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createVault(options = {}) {
|
|
42
|
+
const home = options.home ?? process.env.DSH_HOME ?? ''
|
|
43
|
+
const vaultDir = options.vaultDir ?? defaultVaultDir(home || undefined)
|
|
44
|
+
const now = options.now ?? (() => Date.now())
|
|
45
|
+
const keep = options.keep ?? 10
|
|
46
|
+
|
|
47
|
+
function list() {
|
|
48
|
+
let names
|
|
49
|
+
try {
|
|
50
|
+
names = readdirSync(vaultDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name)
|
|
51
|
+
} catch {
|
|
52
|
+
return []
|
|
53
|
+
}
|
|
54
|
+
const items = []
|
|
55
|
+
for (const id of names.sort().reverse()) {
|
|
56
|
+
const manifest = readManifest(id)
|
|
57
|
+
if (manifest === null) {
|
|
58
|
+
items.push({ id, broken: true })
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
items.push({
|
|
62
|
+
id,
|
|
63
|
+
createdAt: manifest.createdAt,
|
|
64
|
+
files: manifest.totals?.files ?? manifest.items?.length ?? 0,
|
|
65
|
+
bytes: manifest.totals?.bytes ?? 0,
|
|
66
|
+
note: manifest.note ?? null,
|
|
67
|
+
pre: manifest.pre === true,
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
return items
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function readManifest(id) {
|
|
74
|
+
try {
|
|
75
|
+
return JSON.parse(readFileSync(join(vaultDir, id, MANIFEST), 'utf8'))
|
|
76
|
+
} catch {
|
|
77
|
+
return null
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 建一份备份。
|
|
83
|
+
* @returns {{ok:true, id:string, dir:string, files:number, bytes:number, skipped:string[]}
|
|
84
|
+
* |{ok:false, error:string}}
|
|
85
|
+
*/
|
|
86
|
+
function create({ note = null, include, exclude } = {}) {
|
|
87
|
+
if (typeof home !== 'string' || home.length === 0) return { ok: false, error: '没有 DSH_HOME,不知道该备哪里' }
|
|
88
|
+
if (!existsSync(home)) return { ok: false, error: `目录不存在:${home}` }
|
|
89
|
+
|
|
90
|
+
const paths = selectFiles({ home, include, exclude })
|
|
91
|
+
if (paths.length === 0) return { ok: false, error: '没有匹配到任何可备份文件(检查 include 配置)' }
|
|
92
|
+
|
|
93
|
+
const at = now()
|
|
94
|
+
let id = stampOf(at)
|
|
95
|
+
let suffix = 1
|
|
96
|
+
while (existsSync(join(vaultDir, id))) {
|
|
97
|
+
suffix += 1
|
|
98
|
+
id = `${stampOf(at)}-${suffix}`
|
|
99
|
+
}
|
|
100
|
+
const dir = join(vaultDir, id)
|
|
101
|
+
mkdirSync(join(dir, 'files'), { recursive: true })
|
|
102
|
+
|
|
103
|
+
const items = []
|
|
104
|
+
const skipped = []
|
|
105
|
+
let bytes = 0
|
|
106
|
+
for (const path of paths) {
|
|
107
|
+
const safe = safeRelative(path)
|
|
108
|
+
if (safe === null) {
|
|
109
|
+
skipped.push(path)
|
|
110
|
+
continue
|
|
111
|
+
}
|
|
112
|
+
if (isSecretName(safe.split('/').pop() ?? '')) {
|
|
113
|
+
skipped.push(path)
|
|
114
|
+
continue
|
|
115
|
+
}
|
|
116
|
+
let buffer
|
|
117
|
+
try {
|
|
118
|
+
buffer = readFileSync(join(home, safe))
|
|
119
|
+
} catch {
|
|
120
|
+
skipped.push(path)
|
|
121
|
+
continue
|
|
122
|
+
}
|
|
123
|
+
const target = join(dir, 'files', safe)
|
|
124
|
+
mkdirSync(dirname(target), { recursive: true })
|
|
125
|
+
copyFileSync(join(home, safe), target)
|
|
126
|
+
const hash = sha256(buffer)
|
|
127
|
+
items.push({ path: safe, bytes: buffer.length, sha256: hash, mtimeMs: statSync(join(home, safe)).mtimeMs })
|
|
128
|
+
bytes += buffer.length
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const manifest = {
|
|
132
|
+
v: 1,
|
|
133
|
+
id,
|
|
134
|
+
createdAt: at,
|
|
135
|
+
home,
|
|
136
|
+
note,
|
|
137
|
+
pre: false,
|
|
138
|
+
items,
|
|
139
|
+
skipped,
|
|
140
|
+
totals: { files: items.length, bytes },
|
|
141
|
+
host: { node: process.version, platform: process.platform },
|
|
142
|
+
excludedPolicy: '不备份密钥:credentials / *token* / *secret* / *.key / .env',
|
|
143
|
+
}
|
|
144
|
+
writeFileSync(join(dir, MANIFEST), JSON.stringify(manifest, null, 2), 'utf8')
|
|
145
|
+
prune()
|
|
146
|
+
return { ok: true, id, dir, files: items.length, bytes, skipped }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function prune() {
|
|
150
|
+
const items = list().filter((item) => !item.broken)
|
|
151
|
+
if (items.length <= keep) return []
|
|
152
|
+
const drop = items.slice(keep)
|
|
153
|
+
for (const item of drop) {
|
|
154
|
+
try {
|
|
155
|
+
rmSync(join(vaultDir, item.id), { recursive: true, force: true })
|
|
156
|
+
} catch {}
|
|
157
|
+
}
|
|
158
|
+
return drop.map((item) => item.id)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 校验:备份里的文件是否还在、哈希是否与 manifest 一致;
|
|
163
|
+
* 同时给出当前 home 里这些文件的状态(same / changed / missing)。
|
|
164
|
+
*/
|
|
165
|
+
function verify(id) {
|
|
166
|
+
const manifest = readManifest(id)
|
|
167
|
+
if (manifest === null) return { ok: false, error: `找不到备份 ${id}` }
|
|
168
|
+
const items = manifest.items.map((item) => {
|
|
169
|
+
let backupOk = false
|
|
170
|
+
try {
|
|
171
|
+
backupOk = sha256(readFileSync(join(vaultDir, id, 'files', item.path))) === item.sha256
|
|
172
|
+
} catch {
|
|
173
|
+
backupOk = false
|
|
174
|
+
}
|
|
175
|
+
let current = 'missing'
|
|
176
|
+
try {
|
|
177
|
+
const buffer = readFileSync(join(home, item.path))
|
|
178
|
+
current = sha256(buffer) === item.sha256 ? 'same' : 'changed'
|
|
179
|
+
} catch {
|
|
180
|
+
current = 'missing'
|
|
181
|
+
}
|
|
182
|
+
return { path: item.path, bytes: item.bytes, backupOk, current }
|
|
183
|
+
})
|
|
184
|
+
const summary = {
|
|
185
|
+
total: items.length,
|
|
186
|
+
backupIntact: items.filter((item) => item.backupOk).length,
|
|
187
|
+
currentSame: items.filter((item) => item.current === 'same').length,
|
|
188
|
+
currentChanged: items.filter((item) => item.current === 'changed').length,
|
|
189
|
+
currentMissing: items.filter((item) => item.current === 'missing').length,
|
|
190
|
+
}
|
|
191
|
+
return { ok: true, id, createdAt: manifest.createdAt, items, summary }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** 当前状态 vs 备份:改了什么、少了什么、多了什么。 */
|
|
195
|
+
function diff(id) {
|
|
196
|
+
const manifest = readManifest(id)
|
|
197
|
+
if (manifest === null) return { ok: false, error: `找不到备份 ${id}` }
|
|
198
|
+
const known = new Set(manifest.items.map((item) => item.path))
|
|
199
|
+
const currentPaths = selectFiles({ home })
|
|
200
|
+
const changed = []
|
|
201
|
+
const missing = []
|
|
202
|
+
for (const item of manifest.items) {
|
|
203
|
+
try {
|
|
204
|
+
const buffer = readFileSync(join(home, item.path))
|
|
205
|
+
if (sha256(buffer) !== item.sha256) changed.push(item.path)
|
|
206
|
+
} catch {
|
|
207
|
+
missing.push(item.path)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const added = currentPaths.filter((path) => !known.has(path))
|
|
211
|
+
return { ok: true, id, changed, missing, added, total: manifest.items.length }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 回滚。默认先给当前状态拍一份(pre=true)再写回,写回前逐条校验 manifest 完整性。
|
|
216
|
+
*/
|
|
217
|
+
function restore(id, { dryRun = false, preBackup = true } = {}) {
|
|
218
|
+
const manifest = readManifest(id)
|
|
219
|
+
if (manifest === null) return { ok: false, error: `找不到备份 ${id}` }
|
|
220
|
+
|
|
221
|
+
const planned = []
|
|
222
|
+
for (const item of manifest.items) {
|
|
223
|
+
const safe = safeRelative(item.path)
|
|
224
|
+
if (safe === null) return { ok: false, error: `备份里有不安全的路径:${item.path}` }
|
|
225
|
+
let intact = false
|
|
226
|
+
try {
|
|
227
|
+
intact = sha256(readFileSync(join(vaultDir, id, 'files', safe))) === item.sha256
|
|
228
|
+
} catch {
|
|
229
|
+
intact = false
|
|
230
|
+
}
|
|
231
|
+
if (!intact) return { ok: false, error: `备份文件损坏或不一致:${safe}(先跑 verify)` }
|
|
232
|
+
planned.push({ item, safe })
|
|
233
|
+
}
|
|
234
|
+
if (dryRun) return { ok: true, dryRun: true, willRestore: planned.map((entry) => entry.safe) }
|
|
235
|
+
|
|
236
|
+
let pre = null
|
|
237
|
+
if (preBackup) {
|
|
238
|
+
const created = create({ note: `restore 前自动备份(准备回到 ${id})` })
|
|
239
|
+
if (created.ok) pre = created.id
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const restored = []
|
|
243
|
+
const failed = []
|
|
244
|
+
for (const { item, safe } of planned) {
|
|
245
|
+
try {
|
|
246
|
+
const target = join(home, safe)
|
|
247
|
+
mkdirSync(dirname(target), { recursive: true })
|
|
248
|
+
copyFileSync(join(vaultDir, id, 'files', safe), target)
|
|
249
|
+
restored.push({ path: safe, bytes: item.bytes })
|
|
250
|
+
} catch (error) {
|
|
251
|
+
failed.push({ path: safe, error: error instanceof Error ? error.message : String(error) })
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return { ok: failed.length === 0, id, preBackupId: pre, restored, failed }
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function remove(id) {
|
|
258
|
+
if (readManifest(id) === null) return { ok: false, error: `找不到备份 ${id}` }
|
|
259
|
+
try {
|
|
260
|
+
rmSync(join(vaultDir, id), { recursive: true, force: true })
|
|
261
|
+
return { ok: true }
|
|
262
|
+
} catch (error) {
|
|
263
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) }
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return { home, vaultDir, create, list, verify, diff, restore, remove, readManifest }
|
|
268
|
+
}
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,85 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingda-ai/dsh-plugin-vault",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "机库:DSH 配置备份与回滚。装插件、改 profile 前拍一份快照,改坏了用一条命令退回上一次能用的状态。只备份配置,绝不备份任何密钥。",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"dsh",
|
|
7
|
+
"dsh-plugin",
|
|
8
|
+
"deepseek-harness",
|
|
9
|
+
"backup",
|
|
10
|
+
"rollback",
|
|
11
|
+
"snapshot",
|
|
12
|
+
"config",
|
|
13
|
+
"disaster-recovery"
|
|
14
|
+
],
|
|
15
|
+
"license": "Apache-2.0",
|
|
16
|
+
"author": "LingDaAi",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/lingda-ai/dsh-plugin-vault.git"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://gitee.com/linzhengtong/dsh-plugin-vault#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://gitee.com/linzhengtong/dsh-plugin-vault/issues"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "index.js",
|
|
27
|
+
"bin": {
|
|
28
|
+
"dsh-vault": "./bin/dsh-vault.mjs"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": "./index.js",
|
|
32
|
+
"./client": "./lib/client.js",
|
|
33
|
+
"./scope": "./lib/scope.js",
|
|
34
|
+
"./vault": "./lib/vault.js",
|
|
35
|
+
"./locale/*.json": "./locale/*.json",
|
|
36
|
+
"./icon": "./icon.svg",
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"index.js",
|
|
41
|
+
"bin",
|
|
42
|
+
"lib",
|
|
43
|
+
"cordis.patch.yml",
|
|
44
|
+
"locale/*.json",
|
|
45
|
+
"icon.svg",
|
|
46
|
+
"README.md",
|
|
47
|
+
"docs/*.md"
|
|
48
|
+
],
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=22.6"
|
|
51
|
+
},
|
|
52
|
+
"dsh": {
|
|
53
|
+
"bundle": {
|
|
54
|
+
"patch": "./cordis.patch.yml"
|
|
55
|
+
},
|
|
56
|
+
"client": {
|
|
57
|
+
"platform": "web",
|
|
58
|
+
"inject": [
|
|
59
|
+
"@deepseek-ai/dsh-client-ui-sidebar-right"
|
|
60
|
+
]
|
|
61
|
+
},
|
|
62
|
+
"catalog": {
|
|
63
|
+
"category": "other",
|
|
64
|
+
"summary": {
|
|
65
|
+
"en": "Hangar keeps snapshots of your DSH config — profiles, patch layers, settings — so a bad upgrade is one command away from the last working state. Credentials are never copied. Restore always backs up the current state first.",
|
|
66
|
+
"zh": "机库给 DSH 配置拍快照(profile、补丁层、settings),改坏了用一条命令退回上一次能用的状态。凭据永不进备份;回滚前会先把当前状态再备一份。"
|
|
67
|
+
},
|
|
68
|
+
"capabilities": [
|
|
69
|
+
"config-backup",
|
|
70
|
+
"rollback",
|
|
71
|
+
"integrity-check",
|
|
72
|
+
"local-endpoint",
|
|
73
|
+
"dsh-client-ui-sidebar-right"
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
"publishConfig": {
|
|
78
|
+
"access": "public"
|
|
79
|
+
},
|
|
80
|
+
"scripts": {
|
|
81
|
+
"test": "node --test test/*.test.mjs",
|
|
82
|
+
"check": "node scripts/check-listing.mjs",
|
|
83
|
+
"verify": "node scripts/verify-real-host.mjs"
|
|
84
|
+
}
|
|
85
|
+
}
|