dsh-workbench-host 0.0.0-stage → 0.7.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 +21 -0
- package/README.md +26 -2
- package/client/client.js +2177 -0
- package/cordis.patch.yml +5 -0
- package/lib/actions/action-output.js +25 -0
- package/lib/actions/actions.js +499 -0
- package/lib/actions/capabilities.js +299 -0
- package/lib/actions/task-targets.js +75 -0
- package/lib/core/db.d.ts +6 -0
- package/lib/core/db.js +72 -0
- package/lib/core/index.d.ts +4 -0
- package/lib/core/index.js +4 -0
- package/lib/core/repo.d.ts +43 -0
- package/lib/core/repo.js +116 -0
- package/lib/core/schema.d.ts +7 -0
- package/lib/core/schema.js +65 -0
- package/lib/index.js +411 -0
- package/lib/infra/audit.js +373 -0
- package/lib/infra/backup.js +263 -0
- package/lib/infra/network.js +466 -0
- package/lib/infra/search.js +83 -0
- package/lib/infra/zip.js +217 -0
- package/lib/sync/sync-conflicts.js +37 -0
- package/lib/sync/sync-incoming.js +56 -0
- package/lib/sync/sync-types.js +1 -0
- package/lib/sync/sync-values.js +22 -0
- package/lib/sync/sync.js +550 -0
- package/lib/types/host-types.js +1 -0
- package/package.json +61 -4
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
export const AUDIT_LIMITS = {
|
|
2
|
+
/** 保留期(天),可用 meta.audit.retentionDays 覆盖 */
|
|
3
|
+
retentionDays: 90,
|
|
4
|
+
/** 单字段文本上限 */
|
|
5
|
+
maxText: 2000,
|
|
6
|
+
/** 参数整体 JSON 上限 */
|
|
7
|
+
maxArgs: 12_000,
|
|
8
|
+
/** 结果摘要上限 */
|
|
9
|
+
maxResult: 4_000,
|
|
10
|
+
/** 默认页大小与上限 */
|
|
11
|
+
pageSize: 50,
|
|
12
|
+
maxPageSize: 200,
|
|
13
|
+
/** 每写入多少条做一次惰性清理 */
|
|
14
|
+
pruneEvery: 200,
|
|
15
|
+
};
|
|
16
|
+
/** 敏感键名:命中即脱敏。宁可多脱,不可漏脱。 */
|
|
17
|
+
const SECRET_KEY = /(pass(word|wd)?|pwd|token|secret|credential|authorization|cookie|api[_-]?key|access[_-]?key|private[_-]?key|vault|encrypted|seed[_-]?phrase)/i;
|
|
18
|
+
/** 只读 action:最后一段命中即视为读,默认不记。 */
|
|
19
|
+
const READ_SUFFIX = new Set([
|
|
20
|
+
"list",
|
|
21
|
+
"get",
|
|
22
|
+
"search",
|
|
23
|
+
"stats",
|
|
24
|
+
"statsreport",
|
|
25
|
+
"help",
|
|
26
|
+
"report",
|
|
27
|
+
"history",
|
|
28
|
+
"changelog",
|
|
29
|
+
"timeline",
|
|
30
|
+
"timelinerefactor",
|
|
31
|
+
"preview",
|
|
32
|
+
"tree",
|
|
33
|
+
"schema",
|
|
34
|
+
"nav",
|
|
35
|
+
"breakdown",
|
|
36
|
+
"export",
|
|
37
|
+
"data",
|
|
38
|
+
"config",
|
|
39
|
+
"configget",
|
|
40
|
+
"view",
|
|
41
|
+
]);
|
|
42
|
+
/** 从参数里挑一个「目标」,让一行日志能被人快速定位到记录。 */
|
|
43
|
+
const TARGET_KEYS = [
|
|
44
|
+
"id",
|
|
45
|
+
"ids",
|
|
46
|
+
"taskId",
|
|
47
|
+
"siteId",
|
|
48
|
+
"tagId",
|
|
49
|
+
"nodeId",
|
|
50
|
+
"tableId",
|
|
51
|
+
"titleKey",
|
|
52
|
+
"name",
|
|
53
|
+
"title",
|
|
54
|
+
];
|
|
55
|
+
export function isReadAction(action) {
|
|
56
|
+
const tail = String(action || "")
|
|
57
|
+
.toLowerCase()
|
|
58
|
+
.split(".")
|
|
59
|
+
.pop();
|
|
60
|
+
return READ_SUFFIX.has(tail || "");
|
|
61
|
+
}
|
|
62
|
+
/** 递归脱敏:敏感键名换 ***,长字符串截断,深度/宽度封顶。 */
|
|
63
|
+
export function redactValue(value, depth = 0) {
|
|
64
|
+
if (depth > 6)
|
|
65
|
+
return "[max-depth]";
|
|
66
|
+
if (value === null || value === undefined)
|
|
67
|
+
return value;
|
|
68
|
+
if (typeof value === "string") {
|
|
69
|
+
return value.length > AUDIT_LIMITS.maxText
|
|
70
|
+
? value.slice(0, AUDIT_LIMITS.maxText) + "…(" + value.length + " chars)"
|
|
71
|
+
: value;
|
|
72
|
+
}
|
|
73
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
74
|
+
return value;
|
|
75
|
+
if (typeof value === "bigint")
|
|
76
|
+
return String(value);
|
|
77
|
+
if (typeof value === "function")
|
|
78
|
+
return "[function]";
|
|
79
|
+
if (Array.isArray(value)) {
|
|
80
|
+
return value.slice(0, 50).map((item) => redactValue(item, depth + 1));
|
|
81
|
+
}
|
|
82
|
+
if (typeof value === "object") {
|
|
83
|
+
const out = {};
|
|
84
|
+
let count = 0;
|
|
85
|
+
for (const [key, item] of Object.entries(value)) {
|
|
86
|
+
if (count++ >= 60) {
|
|
87
|
+
out["…"] = "更多字段已省略";
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
out[key] = SECRET_KEY.test(key) ? "***" : redactValue(item, depth + 1);
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
return String(value);
|
|
95
|
+
}
|
|
96
|
+
/** 序列化脱敏后的值;超长时改为 truncated 包装,绝不写半截 JSON。 */
|
|
97
|
+
export function encodeAuditJson(value, limit = AUDIT_LIMITS.maxArgs) {
|
|
98
|
+
if (value === undefined)
|
|
99
|
+
return null;
|
|
100
|
+
let json;
|
|
101
|
+
try {
|
|
102
|
+
json = JSON.stringify(redactValue(value));
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return JSON.stringify("[unserializable]");
|
|
106
|
+
}
|
|
107
|
+
if (json === undefined || json === "null")
|
|
108
|
+
return null;
|
|
109
|
+
if (json.length <= limit)
|
|
110
|
+
return json;
|
|
111
|
+
return JSON.stringify({ truncated: true, chars: json.length, preview: json.slice(0, limit) });
|
|
112
|
+
}
|
|
113
|
+
export function auditTargetOf(args) {
|
|
114
|
+
if (!args || typeof args !== "object" || Array.isArray(args))
|
|
115
|
+
return null;
|
|
116
|
+
const obj = args;
|
|
117
|
+
for (const key of TARGET_KEYS) {
|
|
118
|
+
if (!Object.prototype.hasOwnProperty.call(obj, key))
|
|
119
|
+
continue;
|
|
120
|
+
const value = obj[key];
|
|
121
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
122
|
+
const text = String(value).trim();
|
|
123
|
+
if (text)
|
|
124
|
+
return key + "=" + text.slice(0, 120);
|
|
125
|
+
}
|
|
126
|
+
if (Array.isArray(value) && value.length) {
|
|
127
|
+
const text = value
|
|
128
|
+
.slice(0, 5)
|
|
129
|
+
.map((x) => String(x))
|
|
130
|
+
.join(",");
|
|
131
|
+
if (text)
|
|
132
|
+
return key + "=" + text.slice(0, 120);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
function getMeta(db, key) {
|
|
138
|
+
try {
|
|
139
|
+
const row = db.prepare("SELECT v FROM meta WHERE k = ?").get(key);
|
|
140
|
+
return row && row.v !== undefined && row.v !== null ? String(row.v) : "";
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return "";
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export function setAuditMeta(db, key, value) {
|
|
147
|
+
db.prepare("INSERT INTO meta(k, v) VALUES(?, ?) ON CONFLICT(k) DO UPDATE SET v = excluded.v").run(key, String(value));
|
|
148
|
+
}
|
|
149
|
+
/** 把毫秒时间戳或 YYYY-MM-DD 归一成毫秒;endOfDay 用于 until。 */
|
|
150
|
+
function toMillis(value, endOfDay = false) {
|
|
151
|
+
if (value === undefined || value === null || value === "")
|
|
152
|
+
return null;
|
|
153
|
+
if (typeof value === "number")
|
|
154
|
+
return Number.isFinite(value) ? value : null;
|
|
155
|
+
const text = String(value).trim();
|
|
156
|
+
if (/^\d+$/.test(text))
|
|
157
|
+
return Number(text);
|
|
158
|
+
const dateOnly = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
|
|
159
|
+
if (dateOnly) {
|
|
160
|
+
const ms = Date.UTC(Number(dateOnly[1]), Number(dateOnly[2]) - 1, Number(dateOnly[3]), endOfDay ? 23 : 0, endOfDay ? 59 : 0, endOfDay ? 59 : 0, endOfDay ? 999 : 0);
|
|
161
|
+
return Number.isFinite(ms) ? ms : null;
|
|
162
|
+
}
|
|
163
|
+
const parsed = Date.parse(text);
|
|
164
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
165
|
+
}
|
|
166
|
+
function clampInt(value, fallback, min, max) {
|
|
167
|
+
const n = Math.floor(Number(value));
|
|
168
|
+
if (!Number.isFinite(n))
|
|
169
|
+
return fallback;
|
|
170
|
+
return Math.max(min, Math.min(max, n));
|
|
171
|
+
}
|
|
172
|
+
function recordOf(row) {
|
|
173
|
+
return {
|
|
174
|
+
id: row.id,
|
|
175
|
+
at: row.at,
|
|
176
|
+
time: new Date(Number(row.at)).toISOString(),
|
|
177
|
+
actor: row.actor,
|
|
178
|
+
action: row.action,
|
|
179
|
+
domain: row.domain,
|
|
180
|
+
ok: Number(row.ok) === 1,
|
|
181
|
+
error: row.error,
|
|
182
|
+
target: row.target,
|
|
183
|
+
args: row.args,
|
|
184
|
+
result: row.result,
|
|
185
|
+
durationMs: Number(row.duration_ms),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
export class AuditLog {
|
|
189
|
+
db;
|
|
190
|
+
now;
|
|
191
|
+
writes = 0;
|
|
192
|
+
constructor(db, now) {
|
|
193
|
+
this.db = db;
|
|
194
|
+
this.now = now;
|
|
195
|
+
}
|
|
196
|
+
ensureSchema() {
|
|
197
|
+
this.db.exec(`
|
|
198
|
+
CREATE TABLE IF NOT EXISTS audit_log (
|
|
199
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
200
|
+
at INTEGER NOT NULL,
|
|
201
|
+
actor TEXT NOT NULL,
|
|
202
|
+
action TEXT NOT NULL,
|
|
203
|
+
domain TEXT NOT NULL,
|
|
204
|
+
ok INTEGER NOT NULL,
|
|
205
|
+
error TEXT,
|
|
206
|
+
target TEXT,
|
|
207
|
+
args TEXT,
|
|
208
|
+
result TEXT,
|
|
209
|
+
duration_ms INTEGER NOT NULL DEFAULT 0
|
|
210
|
+
);
|
|
211
|
+
CREATE INDEX IF NOT EXISTS idx_audit_at ON audit_log(at);
|
|
212
|
+
CREATE INDEX IF NOT EXISTS idx_audit_action ON audit_log(action);
|
|
213
|
+
CREATE INDEX IF NOT EXISTS idx_audit_ok ON audit_log(ok);
|
|
214
|
+
`);
|
|
215
|
+
}
|
|
216
|
+
/** 是否记录只读 action(默认关闭,meta.audit.recordReads=1 打开)。 */
|
|
217
|
+
recordsReads() {
|
|
218
|
+
const raw = getMeta(this.db, "audit.recordReads").trim().toLowerCase();
|
|
219
|
+
return raw === "1" || raw === "true" || raw === "yes" || raw === "on";
|
|
220
|
+
}
|
|
221
|
+
retentionDays() {
|
|
222
|
+
const raw = Number(getMeta(this.db, "audit.retentionDays"));
|
|
223
|
+
return Number.isFinite(raw) && raw > 0 ? Math.min(3650, Math.floor(raw)) : AUDIT_LIMITS.retentionDays;
|
|
224
|
+
}
|
|
225
|
+
/** 写一条审计;任何失败都吞掉并返回 null,绝不影响业务。 */
|
|
226
|
+
record(input) {
|
|
227
|
+
try {
|
|
228
|
+
const action = String(input.action || "").trim();
|
|
229
|
+
if (!action)
|
|
230
|
+
return null;
|
|
231
|
+
// 自举元 action 与审计自身不记
|
|
232
|
+
if (action.startsWith("audit.") || action.startsWith("actions.") || action.startsWith("capabilities.")) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
if (!input.force && isReadAction(action) && !this.recordsReads())
|
|
236
|
+
return null;
|
|
237
|
+
const at = Number.isFinite(Number(input.at)) ? Number(input.at) : this.now();
|
|
238
|
+
const error = input.error && String(input.error).length > AUDIT_LIMITS.maxText
|
|
239
|
+
? String(input.error).slice(0, AUDIT_LIMITS.maxText) + "…"
|
|
240
|
+
: input.error
|
|
241
|
+
? String(input.error)
|
|
242
|
+
: null;
|
|
243
|
+
const info = this.db
|
|
244
|
+
.prepare(`INSERT INTO audit_log(at, actor, action, domain, ok, error, target, args, result, duration_ms)
|
|
245
|
+
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
246
|
+
.run(at, String(input.actor || "other").slice(0, 40), action.slice(0, 120), String(input.domain || "").slice(0, 120), input.ok ? 1 : 0, error, auditTargetOf(input.args), encodeAuditJson(input.args, AUDIT_LIMITS.maxArgs), encodeAuditJson(input.result, AUDIT_LIMITS.maxResult), Math.max(0, Math.floor(Number(input.durationMs) || 0)));
|
|
247
|
+
this.writes += 1;
|
|
248
|
+
if (this.writes % AUDIT_LIMITS.pruneEvery === 0) {
|
|
249
|
+
try {
|
|
250
|
+
this.prune(this.retentionDays());
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
/* 惰性清理失败不影响写入 */
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return Number(info.lastInsertRowid);
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
list(query = {}) {
|
|
263
|
+
const where = [];
|
|
264
|
+
const params = [];
|
|
265
|
+
const action = query.action ? String(query.action).trim() : "";
|
|
266
|
+
if (action) {
|
|
267
|
+
where.push("action = ?");
|
|
268
|
+
params.push(action);
|
|
269
|
+
}
|
|
270
|
+
const prefix = query.actionPrefix ? String(query.actionPrefix).trim() : "";
|
|
271
|
+
if (prefix) {
|
|
272
|
+
where.push("action LIKE ?");
|
|
273
|
+
params.push(prefix.replace(/[%_]/g, (m) => "\\" + m) + "%");
|
|
274
|
+
}
|
|
275
|
+
const actor = query.actor ? String(query.actor).trim() : "";
|
|
276
|
+
if (actor) {
|
|
277
|
+
where.push("actor = ?");
|
|
278
|
+
params.push(actor);
|
|
279
|
+
}
|
|
280
|
+
const domain = query.domain ? String(query.domain).trim() : "";
|
|
281
|
+
if (domain) {
|
|
282
|
+
where.push("domain = ?");
|
|
283
|
+
params.push(domain);
|
|
284
|
+
}
|
|
285
|
+
if (query.ok !== undefined && query.ok !== null && query.ok !== "") {
|
|
286
|
+
const raw = String(query.ok).toLowerCase();
|
|
287
|
+
const ok = raw === "1" || raw === "true" || raw === "yes" ? 1 : raw === "0" || raw === "false" || raw === "no" ? 0 : null;
|
|
288
|
+
if (ok !== null) {
|
|
289
|
+
where.push("ok = ?");
|
|
290
|
+
params.push(ok);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const q = query.q ? String(query.q).trim() : "";
|
|
294
|
+
if (q) {
|
|
295
|
+
where.push("(action LIKE ? OR target LIKE ? OR error LIKE ?)");
|
|
296
|
+
const like = "%" + q.replace(/[%_]/g, (m) => "\\" + m) + "%";
|
|
297
|
+
params.push(like, like, like);
|
|
298
|
+
}
|
|
299
|
+
const target = query.target ? String(query.target).trim() : "";
|
|
300
|
+
if (target) {
|
|
301
|
+
where.push("target LIKE ?");
|
|
302
|
+
params.push("%" + target.replace(/[%_]/g, (m) => "\\" + m) + "%");
|
|
303
|
+
}
|
|
304
|
+
const since = toMillis(query.since);
|
|
305
|
+
if (since !== null) {
|
|
306
|
+
where.push("at >= ?");
|
|
307
|
+
params.push(since);
|
|
308
|
+
}
|
|
309
|
+
const until = toMillis(query.until, true);
|
|
310
|
+
if (until !== null) {
|
|
311
|
+
where.push("at <= ?");
|
|
312
|
+
params.push(until);
|
|
313
|
+
}
|
|
314
|
+
const whereSql = where.length ? " WHERE " + where.join(" AND ") : "";
|
|
315
|
+
const page = clampInt(query.page, 1, 1, 100_000);
|
|
316
|
+
const pageSize = clampInt(query.pageSize, AUDIT_LIMITS.pageSize, 1, AUDIT_LIMITS.maxPageSize);
|
|
317
|
+
const totalRow = this.db
|
|
318
|
+
.prepare("SELECT COUNT(*) AS n FROM audit_log" + whereSql)
|
|
319
|
+
.get(...params);
|
|
320
|
+
const total = Number(totalRow && totalRow.n ? totalRow.n : 0);
|
|
321
|
+
const rows = this.db
|
|
322
|
+
.prepare("SELECT * FROM audit_log" + whereSql + " ORDER BY at DESC, id DESC LIMIT ? OFFSET ?")
|
|
323
|
+
.all(...params, pageSize, (page - 1) * pageSize);
|
|
324
|
+
return {
|
|
325
|
+
records: rows.map(recordOf),
|
|
326
|
+
total,
|
|
327
|
+
returned: rows.length,
|
|
328
|
+
page,
|
|
329
|
+
pageSize,
|
|
330
|
+
pageable: true,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
get(id) {
|
|
334
|
+
const row = this.db.prepare("SELECT * FROM audit_log WHERE id = ?").get(Number(id));
|
|
335
|
+
return row ? recordOf(row) : null;
|
|
336
|
+
}
|
|
337
|
+
stats(rangeDays = 30) {
|
|
338
|
+
const days = clampInt(rangeDays, 30, 1, 3650);
|
|
339
|
+
const since = this.now() - days * 86_400_000;
|
|
340
|
+
const totals = this.db
|
|
341
|
+
.prepare("SELECT COUNT(*) AS n, COALESCE(SUM(CASE WHEN ok = 0 THEN 1 ELSE 0 END), 0) AS failed FROM audit_log WHERE at >= ?")
|
|
342
|
+
.get(since);
|
|
343
|
+
const byAction = this.db
|
|
344
|
+
.prepare(`SELECT action, COUNT(*) AS n, COALESCE(SUM(CASE WHEN ok = 0 THEN 1 ELSE 0 END), 0) AS failed
|
|
345
|
+
FROM audit_log WHERE at >= ? GROUP BY action ORDER BY n DESC, action ASC LIMIT 30`)
|
|
346
|
+
.all(since);
|
|
347
|
+
const byActor = this.db
|
|
348
|
+
.prepare(`SELECT actor, COUNT(*) AS n FROM audit_log WHERE at >= ? GROUP BY actor ORDER BY n DESC LIMIT 10`)
|
|
349
|
+
.all(since);
|
|
350
|
+
const byDay = this.db
|
|
351
|
+
.prepare(`SELECT strftime('%Y-%m-%d', at / 1000, 'unixepoch', 'localtime') AS day,
|
|
352
|
+
COUNT(*) AS n, COALESCE(SUM(CASE WHEN ok = 0 THEN 1 ELSE 0 END), 0) AS failed
|
|
353
|
+
FROM audit_log WHERE at >= ? GROUP BY day ORDER BY day DESC LIMIT 60`)
|
|
354
|
+
.all(since);
|
|
355
|
+
return {
|
|
356
|
+
rangeDays: days,
|
|
357
|
+
since,
|
|
358
|
+
total: Number(totals && totals.n ? totals.n : 0),
|
|
359
|
+
failed: Number(totals && totals.failed ? totals.failed : 0),
|
|
360
|
+
byAction: byAction.map((r) => ({ action: r.action, count: Number(r.n), failed: Number(r.failed) })),
|
|
361
|
+
byActor: byActor.map((r) => ({ actor: r.actor, count: Number(r.n) })),
|
|
362
|
+
byDay: byDay.map((r) => ({ day: r.day, count: Number(r.n), failed: Number(r.failed) })),
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
prune(retentionDays) {
|
|
366
|
+
const days = retentionDays === undefined || retentionDays === null || retentionDays === ""
|
|
367
|
+
? this.retentionDays()
|
|
368
|
+
: clampInt(retentionDays, this.retentionDays(), 1, 3650);
|
|
369
|
+
const cutoff = this.now() - days * 86_400_000;
|
|
370
|
+
const info = this.db.prepare("DELETE FROM audit_log WHERE at < ?").run(cutoff);
|
|
371
|
+
return { deleted: Number(info.changes || 0), retentionDays: days, cutoff };
|
|
372
|
+
}
|
|
373
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数据备份:宿主只做三件事——收集各业务插件导出的数据、打成 ZIP、按 manifest 路由回去。
|
|
3
|
+
* 各业务插件自己负责数据格式、解析与校验;宿主不解释 modules/<id>.json。
|
|
4
|
+
*/
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
import { zipCreate, zipRead } from "./zip.js";
|
|
7
|
+
export const BACKUP_FORMAT_VERSION = 1;
|
|
8
|
+
export class BackupError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
constructor(message, code = "invalid") {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "BackupError";
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function normalizeImportPolicy(value) {
|
|
17
|
+
return value === "skip" || value === "overwrite" || value === "newest" ? value : "newest";
|
|
18
|
+
}
|
|
19
|
+
function sha256(buf) {
|
|
20
|
+
return createHash("sha256").update(buf).digest("hex");
|
|
21
|
+
}
|
|
22
|
+
function stamp(d) {
|
|
23
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
24
|
+
return d.getFullYear() + p(d.getMonth() + 1) + p(d.getDate()) + "-" + p(d.getHours()) + p(d.getMinutes()) + p(d.getSeconds());
|
|
25
|
+
}
|
|
26
|
+
function contextOf(manifest) {
|
|
27
|
+
return {
|
|
28
|
+
shellVersion: manifest.shellVersion,
|
|
29
|
+
formatVersion: manifest.formatVersion,
|
|
30
|
+
exportedAt: manifest.exportedAt,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function validateManifest(value) {
|
|
34
|
+
if (!value || typeof value !== "object")
|
|
35
|
+
throw new BackupError("备份包缺少 manifest.json 或格式不正确");
|
|
36
|
+
if (value.kind !== "dsh-workbench-backup")
|
|
37
|
+
throw new BackupError("不是个人工作台备份文件");
|
|
38
|
+
if (!Number.isInteger(value.formatVersion))
|
|
39
|
+
throw new BackupError("备份包缺少格式版本");
|
|
40
|
+
if (value.formatVersion !== BACKUP_FORMAT_VERSION) {
|
|
41
|
+
throw new BackupError("备份格式版本 " + value.formatVersion + " 不兼容,当前工作台只支持版本 " + BACKUP_FORMAT_VERSION +
|
|
42
|
+
"。请升级工作台,或使用对应版本导出。", "incompatible");
|
|
43
|
+
}
|
|
44
|
+
if (typeof value.exportedAt !== "string" || Number.isNaN(Date.parse(value.exportedAt))) {
|
|
45
|
+
throw new BackupError("备份包缺少有效的导出时间");
|
|
46
|
+
}
|
|
47
|
+
if (typeof value.shellVersion !== "string" || !value.shellVersion)
|
|
48
|
+
throw new BackupError("备份包缺少壳版本");
|
|
49
|
+
if (!Array.isArray(value.modules))
|
|
50
|
+
throw new BackupError("备份包缺少模块清单");
|
|
51
|
+
if (value.modules.length > 100)
|
|
52
|
+
throw new BackupError("备份包模块数超过上限");
|
|
53
|
+
const seen = new Set();
|
|
54
|
+
const modules = value.modules.map((raw) => {
|
|
55
|
+
if (!raw || typeof raw !== "object")
|
|
56
|
+
throw new BackupError("模块清单格式不正确");
|
|
57
|
+
const id = String(raw.id || "");
|
|
58
|
+
const title = String(raw.title || id);
|
|
59
|
+
const version = String(raw.version || "");
|
|
60
|
+
const file = String(raw.file || "");
|
|
61
|
+
const size = Number(raw.size);
|
|
62
|
+
const digest = String(raw.sha256 || "");
|
|
63
|
+
if (!/^[a-z][a-z0-9_-]{2,63}$/.test(id))
|
|
64
|
+
throw new BackupError("模块 id 不合法:" + id);
|
|
65
|
+
if (seen.has(id))
|
|
66
|
+
throw new BackupError("模块 id 重复:" + id);
|
|
67
|
+
seen.add(id);
|
|
68
|
+
if (!file || file.startsWith("/") || file.includes("\\") || file.split("/").some((p) => p === ".." || p === ".")) {
|
|
69
|
+
throw new BackupError("模块文件路径不合法:" + file);
|
|
70
|
+
}
|
|
71
|
+
if (!Number.isInteger(size) || size < 0)
|
|
72
|
+
throw new BackupError("模块文件大小不合法:" + id);
|
|
73
|
+
if (!/^[a-f0-9]{64}$/.test(digest))
|
|
74
|
+
throw new BackupError("模块校验值不合法:" + id);
|
|
75
|
+
return { id, title, version, file, size, sha256: digest };
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
kind: "dsh-workbench-backup",
|
|
79
|
+
formatVersion: value.formatVersion,
|
|
80
|
+
shellVersion: value.shellVersion,
|
|
81
|
+
exportedAt: value.exportedAt,
|
|
82
|
+
modules,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 导出选中的业务模块;任一模块失败则整体失败,避免产出缺数据的备份。
|
|
87
|
+
*
|
|
88
|
+
* moduleIds 为空(含 `all`)=全量导出:跳过没有 exportData 的域,并在 manifest.skipped 注明;
|
|
89
|
+
* 显式列表=调用方指定:未知模块或不可导出模块都拒绝,不静默丢数据。
|
|
90
|
+
*/
|
|
91
|
+
export async function createBackup(opts) {
|
|
92
|
+
const asked = (opts.moduleIds ?? []).filter((id) => id && id !== "all");
|
|
93
|
+
const wanted = asked.length ? new Set(asked) : null;
|
|
94
|
+
const known = new Map(opts.domains.map((d) => [d.def.id, d]));
|
|
95
|
+
if (wanted) {
|
|
96
|
+
for (const id of wanted)
|
|
97
|
+
if (!known.has(id))
|
|
98
|
+
throw new BackupError("模块未安装:" + id);
|
|
99
|
+
for (const id of wanted) {
|
|
100
|
+
const rec = known.get(id);
|
|
101
|
+
if (!rec.def.exportData)
|
|
102
|
+
throw new BackupError("模块未实现数据导出:" + rec.def.title, "not_exportable");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const selected = opts.domains.filter((d) => d.enabled && (!wanted || wanted.has(d.def.id)));
|
|
106
|
+
if (!selected.length) {
|
|
107
|
+
throw new BackupError(wanted ? "选中的模块都未启用,无法导出:" + asked.join(", ") : "没有可导出的模块");
|
|
108
|
+
}
|
|
109
|
+
const skipped = wanted
|
|
110
|
+
? []
|
|
111
|
+
: opts.domains
|
|
112
|
+
.filter((d) => d.enabled && typeof d.def.exportData !== "function")
|
|
113
|
+
.map((d) => ({ id: d.def.id, title: d.def.title, reason: "no-export" }));
|
|
114
|
+
// 全量导出只遍历能导出的域;显式列表在上面的校验里已保证都能导出。
|
|
115
|
+
const exportable = wanted ? selected : selected.filter((d) => typeof d.def.exportData === "function");
|
|
116
|
+
if (!exportable.length)
|
|
117
|
+
throw new BackupError("没有可导出的模块");
|
|
118
|
+
const exportedAt = new Date().toISOString();
|
|
119
|
+
const shellVersion = opts.shellVersion;
|
|
120
|
+
const manifestBase = {
|
|
121
|
+
kind: "dsh-workbench-backup",
|
|
122
|
+
formatVersion: BACKUP_FORMAT_VERSION,
|
|
123
|
+
shellVersion,
|
|
124
|
+
exportedAt,
|
|
125
|
+
};
|
|
126
|
+
const modules = [];
|
|
127
|
+
const files = [];
|
|
128
|
+
for (const rec of exportable) {
|
|
129
|
+
const def = rec.def;
|
|
130
|
+
let data;
|
|
131
|
+
try {
|
|
132
|
+
data = await def.exportData({ shellVersion, formatVersion: BACKUP_FORMAT_VERSION, exportedAt });
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
throw new BackupError("模块「" + def.title + "」导出失败:" + String((e && e.message) || e), "export");
|
|
136
|
+
}
|
|
137
|
+
const text = typeof data === "string" ? data : JSON.stringify(data);
|
|
138
|
+
if (!text)
|
|
139
|
+
throw new BackupError("模块「" + def.title + "」没有可导出的数据", "export");
|
|
140
|
+
const buf = Buffer.from(text, "utf8");
|
|
141
|
+
const file = "modules/" + def.id + ".json";
|
|
142
|
+
files.push({ name: file, data: buf });
|
|
143
|
+
modules.push({
|
|
144
|
+
id: def.id,
|
|
145
|
+
title: def.title,
|
|
146
|
+
version: def.version,
|
|
147
|
+
file,
|
|
148
|
+
size: buf.length,
|
|
149
|
+
sha256: sha256(buf),
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
const manifest = skipped.length ? { ...manifestBase, modules, skipped } : { ...manifestBase, modules };
|
|
153
|
+
files.unshift({ name: "manifest.json", data: Buffer.from(JSON.stringify(manifest, null, 2), "utf8") });
|
|
154
|
+
const now = new Date();
|
|
155
|
+
return { filename: "dsh-workbench-backup-" + stamp(now) + ".zip", buffer: zipCreate(files), manifest };
|
|
156
|
+
}
|
|
157
|
+
/** 按已安装域的依赖稳定排序;每轮选择清单中最早的可执行模块。 */
|
|
158
|
+
function orderImports(modules, byId) {
|
|
159
|
+
const pending = [...modules];
|
|
160
|
+
const present = new Set(modules.map((item) => item.id));
|
|
161
|
+
const done = new Set();
|
|
162
|
+
const ordered = [];
|
|
163
|
+
while (pending.length) {
|
|
164
|
+
const index = pending.findIndex((item) => (byId.get(item.id)?.def.backupAfter ?? []).every((id) => !present.has(id) || done.has(id)));
|
|
165
|
+
if (index < 0)
|
|
166
|
+
throw new BackupError("备份模块导入依赖存在循环:" + pending.map((item) => item.id).join(", "));
|
|
167
|
+
const [item] = pending.splice(index, 1);
|
|
168
|
+
ordered.push(item);
|
|
169
|
+
done.add(item.id);
|
|
170
|
+
}
|
|
171
|
+
return ordered;
|
|
172
|
+
}
|
|
173
|
+
/** 解压并按域声明依赖分发导入;单个模块失败不会阻断其他模块。 */
|
|
174
|
+
export async function importBackup(opts) {
|
|
175
|
+
let files;
|
|
176
|
+
try {
|
|
177
|
+
files = zipRead(opts.buffer);
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
throw new BackupError("无法读取备份压缩包:" + String((e && e.message) || e));
|
|
181
|
+
}
|
|
182
|
+
const manifestBuf = files.get("manifest.json");
|
|
183
|
+
if (!manifestBuf)
|
|
184
|
+
throw new BackupError("备份包缺少 manifest.json");
|
|
185
|
+
let rawManifest;
|
|
186
|
+
try {
|
|
187
|
+
rawManifest = JSON.parse(manifestBuf.toString("utf8"));
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
throw new BackupError("备份包 manifest.json 不是有效 JSON");
|
|
191
|
+
}
|
|
192
|
+
const manifest = validateManifest(rawManifest);
|
|
193
|
+
const ctx = contextOf(manifest);
|
|
194
|
+
const byId = new Map();
|
|
195
|
+
for (const rec of opts.domains)
|
|
196
|
+
byId.set(rec.def.id, rec);
|
|
197
|
+
const results = [];
|
|
198
|
+
const seen = new Set();
|
|
199
|
+
for (const item of orderImports(manifest.modules, byId)) {
|
|
200
|
+
const policy = normalizeImportPolicy(opts.policies ? opts.policies[item.id] : undefined);
|
|
201
|
+
const base = {
|
|
202
|
+
id: item.id,
|
|
203
|
+
title: item.title,
|
|
204
|
+
moduleVersion: item.version,
|
|
205
|
+
status: "failed",
|
|
206
|
+
policy,
|
|
207
|
+
};
|
|
208
|
+
if (seen.has(item.id)) {
|
|
209
|
+
results.push({ ...base, status: "skipped", message: "模块清单重复,已跳过" });
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
seen.add(item.id);
|
|
213
|
+
const rec = byId.get(item.id);
|
|
214
|
+
if (!rec) {
|
|
215
|
+
results.push({ ...base, status: "skipped", message: "当前未安装该模块,已跳过" });
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (!rec.enabled || !rec.def.importData) {
|
|
219
|
+
results.push({ ...base, status: "failed", message: rec.error || "模块未启用或未实现数据导入" });
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const data = files.get(item.file);
|
|
223
|
+
if (!data) {
|
|
224
|
+
results.push({ ...base, status: "failed", message: "备份包缺少模块文件:" + item.file });
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (data.length !== item.size || sha256(data) !== item.sha256) {
|
|
228
|
+
results.push({ ...base, status: "failed", message: "模块文件校验失败:" + item.file });
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
let report;
|
|
232
|
+
try {
|
|
233
|
+
report = await rec.def.importData(data.toString("utf8"), ctx, { policy });
|
|
234
|
+
}
|
|
235
|
+
catch (e) {
|
|
236
|
+
results.push({ ...base, status: "failed", message: String((e && e.message) || e) });
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
const imported = Number(report && report.imported) || 0;
|
|
240
|
+
const failed = Number(report && report.failed) || 0;
|
|
241
|
+
const skipped = Number(report && report.skipped) || 0;
|
|
242
|
+
const status = failed > 0 ? (imported > 0 ? "partial" : "failed") : report && report.ok ? "ok" : imported > 0 ? "partial" : "failed";
|
|
243
|
+
results.push({
|
|
244
|
+
...base,
|
|
245
|
+
status,
|
|
246
|
+
imported,
|
|
247
|
+
skipped,
|
|
248
|
+
failed,
|
|
249
|
+
message: report && report.message ? String(report.message) : undefined,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
const summary = { total: results.length, ok: 0, partial: 0, failed: 0, skipped: 0 };
|
|
253
|
+
for (const r of results)
|
|
254
|
+
summary[r.status] += 1;
|
|
255
|
+
return {
|
|
256
|
+
ok: summary.failed === 0 && summary.partial === 0,
|
|
257
|
+
shellVersion: manifest.shellVersion,
|
|
258
|
+
formatVersion: manifest.formatVersion,
|
|
259
|
+
exportedAt: manifest.exportedAt,
|
|
260
|
+
summary,
|
|
261
|
+
results,
|
|
262
|
+
};
|
|
263
|
+
}
|