dsh-workbench-host 0.0.0-stage → 0.6.5
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 +1885 -0
- package/cordis.patch.yml +5 -0
- package/lib/actions/action-output.js +25 -0
- package/lib/actions/actions.js +475 -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 +338 -0
- package/lib/infra/audit.js +373 -0
- package/lib/infra/backup.js +244 -0
- package/lib/infra/network.js +393 -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,393 @@
|
|
|
1
|
+
import { actionErrorText } from "../actions/action-output.js";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { getSyncStateText, setSyncStateText } from "../core/index.js";
|
|
4
|
+
import { SyncEngine, SyncHttpError, listConflicts, resolveConflict } from "../sync/sync.js";
|
|
5
|
+
import { createBackup, importBackup, normalizeImportPolicy } from "./backup.js";
|
|
6
|
+
export function installNetwork({ ctx, state, saveCreds, clock, domains, emit, subscribers, listDomains, SHELL_VERSION, sessionModeOf, setSessionMode, audit, backupDomains, actionNames, invoke, log, schemaSnapshot }) {
|
|
7
|
+
/** 云端 fetch 封装(带 access token;401 时尝试 refresh 一次)。 */
|
|
8
|
+
async function cloudFetch(path, init, retry = true) {
|
|
9
|
+
const headers = {
|
|
10
|
+
"content-type": "application/json",
|
|
11
|
+
...init?.headers,
|
|
12
|
+
};
|
|
13
|
+
if (state.creds)
|
|
14
|
+
headers.authorization = "Bearer " + state.creds.accessToken;
|
|
15
|
+
const res = await fetch(state.cloudUrl + path, {
|
|
16
|
+
...init,
|
|
17
|
+
headers,
|
|
18
|
+
body: init?.json ? JSON.stringify(init.json) : init?.body,
|
|
19
|
+
});
|
|
20
|
+
if (res.status === 401 && retry && state.creds?.refreshToken) {
|
|
21
|
+
const r = await fetch(state.cloudUrl + "/v1/auth/refresh", {
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers: { "content-type": "application/json" },
|
|
24
|
+
body: JSON.stringify({ refresh_token: state.creds.refreshToken }),
|
|
25
|
+
}).then((x) => x.json());
|
|
26
|
+
if (r.ok) {
|
|
27
|
+
state.creds = {
|
|
28
|
+
accessToken: r.accessToken,
|
|
29
|
+
refreshToken: r.refreshToken,
|
|
30
|
+
userId: r.userId,
|
|
31
|
+
deviceId: r.deviceId,
|
|
32
|
+
};
|
|
33
|
+
saveCreds(state.creds);
|
|
34
|
+
return cloudFetch(path, init, false);
|
|
35
|
+
}
|
|
36
|
+
state.creds = null;
|
|
37
|
+
saveCreds(null);
|
|
38
|
+
}
|
|
39
|
+
return res.json();
|
|
40
|
+
}
|
|
41
|
+
/** 云端健康检查缓存:首页/账号页共用,避免同步事件风暴时反复探测。 */
|
|
42
|
+
let cloudHealthCache = null;
|
|
43
|
+
/** 轻量探测云端是否可达;结果缓存 10s,失败不抛异常。 */
|
|
44
|
+
async function cloudHealth() {
|
|
45
|
+
const now = Date.now();
|
|
46
|
+
if (cloudHealthCache && now - cloudHealthCache.at < 10_000)
|
|
47
|
+
return cloudHealthCache.value;
|
|
48
|
+
const started = now;
|
|
49
|
+
const controller = new AbortController();
|
|
50
|
+
const timer = setTimeout(() => controller.abort(), 3_000);
|
|
51
|
+
let value;
|
|
52
|
+
try {
|
|
53
|
+
const res = await fetch(state.cloudUrl + "/v1/health", { signal: controller.signal });
|
|
54
|
+
value = {
|
|
55
|
+
reachable: res.ok,
|
|
56
|
+
status: res.status,
|
|
57
|
+
checked_at: Date.now(),
|
|
58
|
+
latency_ms: Date.now() - started,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
value = {
|
|
63
|
+
reachable: false,
|
|
64
|
+
status: 0,
|
|
65
|
+
checked_at: Date.now(),
|
|
66
|
+
latency_ms: Date.now() - started,
|
|
67
|
+
error: String((e && e.message) || e),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
clearTimeout(timer);
|
|
72
|
+
}
|
|
73
|
+
cloudHealthCache = { at: now, value };
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
/** 把 /v1/sync/* 的错误响应转成可判别的 SyncHttpError。 */
|
|
77
|
+
async function syncCall(path, json) {
|
|
78
|
+
const r = await cloudFetch(path, { method: "POST", json });
|
|
79
|
+
if (r && r.ok === false) {
|
|
80
|
+
const code = String(r.code ?? "sync_error");
|
|
81
|
+
const status = code === "version_conflict" || code === "sync_locked" || code.startsWith("session_") ? 409 : Number(r.status) || 400;
|
|
82
|
+
throw new SyncHttpError(status, code, String(r.error ?? code), r);
|
|
83
|
+
}
|
|
84
|
+
return r;
|
|
85
|
+
}
|
|
86
|
+
const syncTransport = {
|
|
87
|
+
open: (protocol, deviceId) => syncCall("/v1/sync/open", { protocol, device_id: deviceId }),
|
|
88
|
+
push: (body) => syncCall("/v1/sync/push", body),
|
|
89
|
+
pull: (body) => syncCall("/v1/sync/pull", body),
|
|
90
|
+
snapshot: (body) => syncCall("/v1/sync/snapshot", body),
|
|
91
|
+
full: (body) => syncCall("/v1/sync/full", body),
|
|
92
|
+
close: (body) => syncCall("/v1/sync/close", body),
|
|
93
|
+
};
|
|
94
|
+
function syncDomains() {
|
|
95
|
+
return [...domains.values()].map((rec) => ({
|
|
96
|
+
id: rec.def.id,
|
|
97
|
+
tables: rec.def.tables,
|
|
98
|
+
sync: rec.def.sync,
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
function ensureSyncIdentity() {
|
|
102
|
+
if (!state.creds)
|
|
103
|
+
throw new Error("not logged in");
|
|
104
|
+
const bound = getSyncStateText(state.db, "account_user_id");
|
|
105
|
+
if (!bound)
|
|
106
|
+
setSyncStateText(state.db, "account_user_id", state.creds.userId);
|
|
107
|
+
let epoch = getSyncStateText(state.db, "sync_epoch");
|
|
108
|
+
if (!epoch) {
|
|
109
|
+
epoch = randomUUID();
|
|
110
|
+
setSyncStateText(state.db, "sync_epoch", epoch);
|
|
111
|
+
}
|
|
112
|
+
return { userId: state.creds.userId, deviceId: state.creds.deviceId, epoch };
|
|
113
|
+
}
|
|
114
|
+
let syncBusy = false;
|
|
115
|
+
async function runSync() {
|
|
116
|
+
if (syncBusy)
|
|
117
|
+
return { ok: false, error: "sync already running", skipped: true };
|
|
118
|
+
syncBusy = true;
|
|
119
|
+
try {
|
|
120
|
+
const identity = ensureSyncIdentity();
|
|
121
|
+
const engine = new SyncEngine({
|
|
122
|
+
db: state.db,
|
|
123
|
+
domains: syncDomains(),
|
|
124
|
+
transport: syncTransport,
|
|
125
|
+
clock,
|
|
126
|
+
userId: identity.userId,
|
|
127
|
+
deviceId: identity.deviceId,
|
|
128
|
+
epoch: identity.epoch,
|
|
129
|
+
onEvent: emit,
|
|
130
|
+
});
|
|
131
|
+
const report = await engine.run();
|
|
132
|
+
return report;
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
syncBusy = false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// ── HTTP:/workbench/api/*(同域 cookie 之外的连接门由 host connection.requestRejection 兜底)──
|
|
139
|
+
ctx.effect?.(() => ctx.webServer?.register({
|
|
140
|
+
kind: "prefix",
|
|
141
|
+
path: "/workbench/api",
|
|
142
|
+
handler: async (req, res) => {
|
|
143
|
+
const rejection = ctx.connection?.requestRejection?.(req);
|
|
144
|
+
if (rejection !== undefined) {
|
|
145
|
+
res.statusCode = rejection;
|
|
146
|
+
res.end(JSON.stringify({ ok: false, error: "connection rejected" }));
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const url = new URL(req.url ?? "", "http://local");
|
|
150
|
+
const p = url.pathname.replace(/^\/workbench\/api/, "");
|
|
151
|
+
const m = req.method ?? "GET";
|
|
152
|
+
const bodyOf = async () => {
|
|
153
|
+
const chunks = [];
|
|
154
|
+
for await (const c of req)
|
|
155
|
+
chunks.push(c);
|
|
156
|
+
try {
|
|
157
|
+
return JSON.parse(Buffer.concat(chunks).toString() || "{}");
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return {};
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
const rawBodyOf = async (maxBytes = 64 * 1024 * 1024) => {
|
|
164
|
+
const chunks = [];
|
|
165
|
+
let total = 0;
|
|
166
|
+
for await (const c of req) {
|
|
167
|
+
const buf = c;
|
|
168
|
+
total += buf.length;
|
|
169
|
+
if (total > maxBytes)
|
|
170
|
+
throw new Error("请求体超过上限(" + Math.round(maxBytes / 1024 / 1024) + "MB)");
|
|
171
|
+
chunks.push(buf);
|
|
172
|
+
}
|
|
173
|
+
return Buffer.concat(chunks);
|
|
174
|
+
};
|
|
175
|
+
const send = (code, obj) => {
|
|
176
|
+
res.writeHead(code, { "content-type": "application/json" });
|
|
177
|
+
res.end(JSON.stringify(obj));
|
|
178
|
+
};
|
|
179
|
+
try {
|
|
180
|
+
if (p === "/events" && m === "GET") {
|
|
181
|
+
res.writeHead(200, {
|
|
182
|
+
"content-type": "text/event-stream",
|
|
183
|
+
"cache-control": "no-cache",
|
|
184
|
+
connection: "keep-alive",
|
|
185
|
+
});
|
|
186
|
+
const sub = (event, data) => res.write("event: " + event + "\ndata: " + JSON.stringify(data) + "\n\n");
|
|
187
|
+
subscribers.add(sub);
|
|
188
|
+
req.on?.("close", () => subscribers.delete(sub));
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (p === "/domains" && m === "GET") {
|
|
192
|
+
return send(200, { ok: true, domains: listDomains(), shellVersion: SHELL_VERSION });
|
|
193
|
+
}
|
|
194
|
+
if (p === "/record" && m === "GET") {
|
|
195
|
+
const sid = String(url.searchParams.get("sessionId") ?? "").trim();
|
|
196
|
+
if (!sid)
|
|
197
|
+
return send(400, { ok: false, error: "缺少 sessionId" });
|
|
198
|
+
return send(200, { ok: true, sessionId: sid, enabled: sessionModeOf(state.db, sid) });
|
|
199
|
+
}
|
|
200
|
+
if (p === "/record" && m === "POST") {
|
|
201
|
+
const body = await bodyOf();
|
|
202
|
+
const sid = String(body?.sessionId ?? "").trim();
|
|
203
|
+
if (!sid || sid.length > 200)
|
|
204
|
+
return send(400, { ok: false, error: "sessionId 不合法" });
|
|
205
|
+
if (typeof body?.enabled !== "boolean")
|
|
206
|
+
return send(400, { ok: false, error: "enabled 必须是布尔值" });
|
|
207
|
+
const enabled = body.enabled === true;
|
|
208
|
+
setSessionMode(state.db, sid, enabled, clock.now());
|
|
209
|
+
emit("record-mode", { sessionId: sid, enabled });
|
|
210
|
+
return send(200, { ok: true, sessionId: sid, enabled });
|
|
211
|
+
}
|
|
212
|
+
if (p === "/audit" && m === "GET") {
|
|
213
|
+
// 审计页数据:列表 + 可选统计。参数与 audit.list 保持一致,便于同一套筛选复用到 UI。
|
|
214
|
+
try {
|
|
215
|
+
const q = url.searchParams;
|
|
216
|
+
const pick = (key) => (q.get(key) === null ? undefined : String(q.get(key)));
|
|
217
|
+
const result = audit.list({
|
|
218
|
+
action: pick("action"),
|
|
219
|
+
actionPrefix: pick("actionPrefix"),
|
|
220
|
+
actor: pick("actor"),
|
|
221
|
+
domain: pick("domain"),
|
|
222
|
+
ok: pick("ok"),
|
|
223
|
+
q: pick("q"),
|
|
224
|
+
target: pick("target"),
|
|
225
|
+
since: pick("since"),
|
|
226
|
+
until: pick("until"),
|
|
227
|
+
page: pick("page"),
|
|
228
|
+
pageSize: pick("pageSize"),
|
|
229
|
+
});
|
|
230
|
+
const stats = q.get("stats") === "1" ? audit.stats(pick("rangeDays") ?? 30) : null;
|
|
231
|
+
return send(200, { ok: true, shellVersion: SHELL_VERSION, ...result, stats });
|
|
232
|
+
}
|
|
233
|
+
catch (e) {
|
|
234
|
+
return send(500, { ok: false, error: String((e && e.message) || e) });
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (p === "/schema" && m === "GET") {
|
|
238
|
+
// 只读 schema 快照:Action 页用它渲染运行时注册,不经过 action 执行链路。
|
|
239
|
+
try {
|
|
240
|
+
const domain = String(url.searchParams.get("domain") ?? "").trim();
|
|
241
|
+
return send(200, { ok: true, readonly: true, ...schemaSnapshot(domain || undefined) });
|
|
242
|
+
}
|
|
243
|
+
catch (e) {
|
|
244
|
+
return send(500, { ok: false, error: String((e && e.message) || e) });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (p === "/backup/export" && m === "GET") {
|
|
248
|
+
try {
|
|
249
|
+
const raw = url.searchParams.get("modules");
|
|
250
|
+
const ids = raw && raw !== "all" ? raw.split(",").map((x) => x.trim()).filter(Boolean) : null;
|
|
251
|
+
const backup = await createBackup({ domains: backupDomains(), shellVersion: SHELL_VERSION, moduleIds: ids });
|
|
252
|
+
res.writeHead(200, {
|
|
253
|
+
"content-type": "application/zip",
|
|
254
|
+
"content-disposition": "attachment; filename=\"" + backup.filename + "\"",
|
|
255
|
+
"content-length": backup.buffer.length,
|
|
256
|
+
"cache-control": "no-store",
|
|
257
|
+
});
|
|
258
|
+
res.end(backup.buffer);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
catch (e) {
|
|
262
|
+
return send(500, { ok: false, error: String((e && e.message) || e) });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (p === "/backup/import" && m === "POST") {
|
|
266
|
+
try {
|
|
267
|
+
const buf = await rawBodyOf();
|
|
268
|
+
const policies = {};
|
|
269
|
+
const rawPolicy = req.headers?.["x-wb-backup-policy"];
|
|
270
|
+
if (typeof rawPolicy === "string" && rawPolicy) {
|
|
271
|
+
try {
|
|
272
|
+
const parsed = JSON.parse(rawPolicy);
|
|
273
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
274
|
+
for (const key of Object.keys(parsed))
|
|
275
|
+
policies[key] = normalizeImportPolicy(parsed[key]);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
// 策略头损坏时按默认 newest 处理,不阻断备份导入
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const result = await importBackup({ buffer: buf, domains: backupDomains(), policies });
|
|
283
|
+
emit("backup-imported", { summary: result.summary, exportedAt: result.exportedAt });
|
|
284
|
+
return send(200, result);
|
|
285
|
+
}
|
|
286
|
+
catch (e) {
|
|
287
|
+
const err = e;
|
|
288
|
+
const code = err && err.code ? err.code : "invalid";
|
|
289
|
+
return send(code === "incompatible" ? 422 : 400, {
|
|
290
|
+
ok: false,
|
|
291
|
+
code,
|
|
292
|
+
error: String((e && e.message) || e),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const actionM = /^\/domains\/([^/]+)\/actions\/([^/]+)$/.exec(p);
|
|
297
|
+
if (actionM && m === "POST") {
|
|
298
|
+
const id = decodeURIComponent(actionM[1]);
|
|
299
|
+
const action = decodeURIComponent(actionM[2]);
|
|
300
|
+
const owner = actionNames().find((a) => a.name === action);
|
|
301
|
+
if (!owner || owner.domainId !== id) {
|
|
302
|
+
return send(404, { ok: false, error: "业务插件未注册该 action:" + action });
|
|
303
|
+
}
|
|
304
|
+
const body = await bodyOf();
|
|
305
|
+
const r = await invoke(action, body, "ui");
|
|
306
|
+
return send(200, { ok: true, result: r.result, text: r.text });
|
|
307
|
+
}
|
|
308
|
+
if (p === "/sync/now" && m === "POST") {
|
|
309
|
+
if (!state.creds)
|
|
310
|
+
return send(400, { ok: false, error: "not logged in" });
|
|
311
|
+
const r = await runSync();
|
|
312
|
+
return send(200, { ok: true, ...r });
|
|
313
|
+
}
|
|
314
|
+
if (p === "/sync/status" && m === "GET") {
|
|
315
|
+
const identity = state.creds ? ensureSyncIdentity() : null;
|
|
316
|
+
const engine = new SyncEngine({
|
|
317
|
+
db: state.db,
|
|
318
|
+
domains: syncDomains(),
|
|
319
|
+
transport: syncTransport,
|
|
320
|
+
clock,
|
|
321
|
+
userId: identity?.userId ?? "",
|
|
322
|
+
deviceId: identity?.deviceId ?? "",
|
|
323
|
+
epoch: identity?.epoch ?? "",
|
|
324
|
+
onEvent: emit,
|
|
325
|
+
});
|
|
326
|
+
const cloud = state.creds
|
|
327
|
+
? await cloudHealth()
|
|
328
|
+
: { reachable: null, status: 0, checked_at: 0 };
|
|
329
|
+
return send(200, {
|
|
330
|
+
ok: true,
|
|
331
|
+
logged_in: Boolean(state.creds),
|
|
332
|
+
...engine.status(),
|
|
333
|
+
running: syncBusy,
|
|
334
|
+
cloud,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
if (p === "/sync/conflicts" && m === "GET") {
|
|
338
|
+
const limit = Number(url.searchParams.get("limit") ?? "100");
|
|
339
|
+
return send(200, { ok: true, conflicts: listConflicts(state.db, limit) });
|
|
340
|
+
}
|
|
341
|
+
const conflictM = /^\/sync\/conflicts\/(\d+)\/resolve$/.exec(p);
|
|
342
|
+
if (conflictM && m === "POST") {
|
|
343
|
+
const body = await bodyOf();
|
|
344
|
+
const action = String(body.action ?? "");
|
|
345
|
+
if (action !== "keep_local" && action !== "keep_server" && action !== "dismiss") {
|
|
346
|
+
return send(400, { ok: false, error: "action 必须是 keep_local/keep_server/dismiss" });
|
|
347
|
+
}
|
|
348
|
+
const ok = resolveConflict(state.db, Number(conflictM[1]), action, clock.now(), syncDomains());
|
|
349
|
+
return send(ok ? 200 : 404, { ok });
|
|
350
|
+
}
|
|
351
|
+
if (p === "/account" && m === "GET") {
|
|
352
|
+
if (!state.creds)
|
|
353
|
+
return send(200, { ok: true, logged_in: false });
|
|
354
|
+
const me = await cloudFetch("/v1/me");
|
|
355
|
+
return send(200, { ok: true, logged_in: true, user: me.user, devices: me.devices });
|
|
356
|
+
}
|
|
357
|
+
if (p === "/account/login" && m === "POST") {
|
|
358
|
+
const b = await bodyOf();
|
|
359
|
+
const deviceId = state.creds?.deviceId ?? "shell-" + Math.random().toString(36).slice(2, 10);
|
|
360
|
+
const r = await cloudFetch(b.signup ? "/v1/auth/signup" : "/v1/auth/login", {
|
|
361
|
+
method: "POST",
|
|
362
|
+
json: { email: b.email, password: b.password, device_id: deviceId, device_name: "DSH Web" },
|
|
363
|
+
});
|
|
364
|
+
if (r.ok) {
|
|
365
|
+
state.creds = {
|
|
366
|
+
accessToken: r.accessToken,
|
|
367
|
+
refreshToken: r.refreshToken,
|
|
368
|
+
userId: r.userId,
|
|
369
|
+
deviceId: r.deviceId,
|
|
370
|
+
};
|
|
371
|
+
saveCreds(state.creds);
|
|
372
|
+
cloudHealthCache = null;
|
|
373
|
+
emit("account-changed", { logged_in: true });
|
|
374
|
+
void runSync().catch((e) => log("登录后首轮同步失败", e));
|
|
375
|
+
}
|
|
376
|
+
return send(r.ok ? 200 : 401, r);
|
|
377
|
+
}
|
|
378
|
+
if (p === "/account/logout" && m === "POST") {
|
|
379
|
+
state.creds = null;
|
|
380
|
+
saveCreds(null);
|
|
381
|
+
cloudHealthCache = null;
|
|
382
|
+
emit("account-changed", { logged_in: false });
|
|
383
|
+
return send(200, { ok: true });
|
|
384
|
+
}
|
|
385
|
+
return send(404, { ok: false, error: "no route " + m + " " + p });
|
|
386
|
+
}
|
|
387
|
+
catch (e) {
|
|
388
|
+
return send(500, { ok: false, error: actionErrorText(e) });
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
}, "workbench-http"), "workbench-http");
|
|
392
|
+
return { runSync };
|
|
393
|
+
}
|
package/lib/infra/zip.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 最小 ZIP 读写实现(宿主备份用)。
|
|
3
|
+
*
|
|
4
|
+
* 不引入第三方依赖:写 ZIP 时用 deflateRaw,读时支持 store/deflate。
|
|
5
|
+
* 只接受本项目导出的备份包,因此刻意拒绝加密、ZIP64、data descriptor
|
|
6
|
+
* 和所有可疑路径,避免把压缩包当文件系统入口。
|
|
7
|
+
*/
|
|
8
|
+
import { deflateRawSync, inflateRawSync } from "node:zlib";
|
|
9
|
+
const DEFAULT_LIMITS = {
|
|
10
|
+
maxEntries: 200,
|
|
11
|
+
maxEntryBytes: 32 * 1024 * 1024,
|
|
12
|
+
maxTotalBytes: 64 * 1024 * 1024,
|
|
13
|
+
};
|
|
14
|
+
const CRC_TABLE = (() => {
|
|
15
|
+
const table = new Uint32Array(256);
|
|
16
|
+
for (let n = 0; n < 256; n++) {
|
|
17
|
+
let c = n;
|
|
18
|
+
for (let k = 0; k < 8; k++)
|
|
19
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
20
|
+
table[n] = c >>> 0;
|
|
21
|
+
}
|
|
22
|
+
return table;
|
|
23
|
+
})();
|
|
24
|
+
function crc32(buf) {
|
|
25
|
+
let c = 0xffffffff;
|
|
26
|
+
for (let i = 0; i < buf.length; i++)
|
|
27
|
+
c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
|
|
28
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
29
|
+
}
|
|
30
|
+
function assertSafeName(name) {
|
|
31
|
+
if (!name || name.length > 255)
|
|
32
|
+
throw new Error("ZIP 条目名长度非法");
|
|
33
|
+
if (name.includes("\\") || name.includes("\0"))
|
|
34
|
+
throw new Error("ZIP 条目名包含非法字符:" + name);
|
|
35
|
+
if (name.startsWith("/") || /^[a-zA-Z]:/.test(name))
|
|
36
|
+
throw new Error("ZIP 条目名不能是绝对路径:" + name);
|
|
37
|
+
const parts = name.split("/");
|
|
38
|
+
if (parts.some((p) => p === "" || p === "." || p === "..")) {
|
|
39
|
+
throw new Error("ZIP 条目名包含非法路径:" + name);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** 生成一个 ZIP。文件名在条目里使用 UTF-8。 */
|
|
43
|
+
export function zipCreate(entries) {
|
|
44
|
+
if (!Array.isArray(entries) || !entries.length)
|
|
45
|
+
throw new Error("ZIP 至少需要一个文件");
|
|
46
|
+
if (entries.length > DEFAULT_LIMITS.maxEntries)
|
|
47
|
+
throw new Error("ZIP 文件数超过上限");
|
|
48
|
+
const seen = new Set();
|
|
49
|
+
const locals = [];
|
|
50
|
+
const centrals = [];
|
|
51
|
+
let offset = 0;
|
|
52
|
+
let total = 0;
|
|
53
|
+
for (const entry of entries) {
|
|
54
|
+
assertSafeName(entry.name);
|
|
55
|
+
if (seen.has(entry.name))
|
|
56
|
+
throw new Error("ZIP 条目重名:" + entry.name);
|
|
57
|
+
seen.add(entry.name);
|
|
58
|
+
const data = Buffer.isBuffer(entry.data) ? entry.data : Buffer.from(entry.data);
|
|
59
|
+
if (data.length > DEFAULT_LIMITS.maxEntryBytes)
|
|
60
|
+
throw new Error("ZIP 单文件超过上限:" + entry.name);
|
|
61
|
+
total += data.length;
|
|
62
|
+
if (total > DEFAULT_LIMITS.maxTotalBytes)
|
|
63
|
+
throw new Error("ZIP 总大小超过上限");
|
|
64
|
+
const crc = crc32(data);
|
|
65
|
+
const compressed = deflateRawSync(data);
|
|
66
|
+
const method = compressed.length < data.length ? 8 : 0;
|
|
67
|
+
const body = method === 8 ? compressed : data;
|
|
68
|
+
const name = Buffer.from(entry.name, "utf8");
|
|
69
|
+
const flags = 0x0800; // UTF-8 文件名
|
|
70
|
+
const time = 0;
|
|
71
|
+
const date = 0x0021; // 1980-01-01
|
|
72
|
+
const local = Buffer.alloc(30 + name.length);
|
|
73
|
+
local.writeUInt32LE(0x04034b50, 0);
|
|
74
|
+
local.writeUInt16LE(20, 4);
|
|
75
|
+
local.writeUInt16LE(flags, 6);
|
|
76
|
+
local.writeUInt16LE(method, 8);
|
|
77
|
+
local.writeUInt16LE(time, 10);
|
|
78
|
+
local.writeUInt16LE(date, 12);
|
|
79
|
+
local.writeUInt32LE(crc, 14);
|
|
80
|
+
local.writeUInt32LE(body.length, 18);
|
|
81
|
+
local.writeUInt32LE(data.length, 22);
|
|
82
|
+
local.writeUInt16LE(name.length, 26);
|
|
83
|
+
local.writeUInt16LE(0, 28);
|
|
84
|
+
name.copy(local, 30);
|
|
85
|
+
locals.push(local, body);
|
|
86
|
+
const central = Buffer.alloc(46 + name.length);
|
|
87
|
+
central.writeUInt32LE(0x02014b50, 0);
|
|
88
|
+
central.writeUInt16LE(20, 4);
|
|
89
|
+
central.writeUInt16LE(20, 6);
|
|
90
|
+
central.writeUInt16LE(flags, 8);
|
|
91
|
+
central.writeUInt16LE(method, 10);
|
|
92
|
+
central.writeUInt16LE(time, 12);
|
|
93
|
+
central.writeUInt16LE(date, 14);
|
|
94
|
+
central.writeUInt32LE(crc, 16);
|
|
95
|
+
central.writeUInt32LE(body.length, 20);
|
|
96
|
+
central.writeUInt32LE(data.length, 24);
|
|
97
|
+
central.writeUInt16LE(name.length, 28);
|
|
98
|
+
central.writeUInt16LE(0, 30);
|
|
99
|
+
central.writeUInt16LE(0, 32);
|
|
100
|
+
central.writeUInt16LE(0, 34);
|
|
101
|
+
central.writeUInt16LE(0, 36);
|
|
102
|
+
central.writeUInt32LE(0, 38);
|
|
103
|
+
central.writeUInt32LE(offset, 42);
|
|
104
|
+
name.copy(central, 46);
|
|
105
|
+
centrals.push(central);
|
|
106
|
+
offset += local.length + body.length;
|
|
107
|
+
if (offset > 0xffffffff)
|
|
108
|
+
throw new Error("ZIP 超过 4GB 上限");
|
|
109
|
+
}
|
|
110
|
+
const centralBuf = Buffer.concat(centrals);
|
|
111
|
+
const eocd = Buffer.alloc(22);
|
|
112
|
+
eocd.writeUInt32LE(0x06054b50, 0);
|
|
113
|
+
eocd.writeUInt16LE(0, 4);
|
|
114
|
+
eocd.writeUInt16LE(0, 6);
|
|
115
|
+
eocd.writeUInt16LE(entries.length, 8);
|
|
116
|
+
eocd.writeUInt16LE(entries.length, 10);
|
|
117
|
+
eocd.writeUInt32LE(centralBuf.length, 12);
|
|
118
|
+
eocd.writeUInt32LE(offset, 16);
|
|
119
|
+
eocd.writeUInt16LE(0, 20);
|
|
120
|
+
return Buffer.concat([...locals, centralBuf, eocd]);
|
|
121
|
+
}
|
|
122
|
+
/** 读取 ZIP 为 Map<path, Buffer>;只接受本项目的备份包格式。 */
|
|
123
|
+
export function zipRead(input, limits = {}) {
|
|
124
|
+
const lim = { ...DEFAULT_LIMITS, ...limits };
|
|
125
|
+
const buf = Buffer.isBuffer(input) ? input : Buffer.from(input);
|
|
126
|
+
if (buf.length < 22)
|
|
127
|
+
throw new Error("不是有效的 ZIP:文件太小");
|
|
128
|
+
let eocd = -1;
|
|
129
|
+
const start = Math.max(0, buf.length - 22 - 65535);
|
|
130
|
+
for (let i = buf.length - 22; i >= start; i--) {
|
|
131
|
+
if (buf.readUInt32LE(i) === 0x06054b50) {
|
|
132
|
+
eocd = i;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (eocd < 0)
|
|
137
|
+
throw new Error("不是有效的 ZIP:找不到中央目录");
|
|
138
|
+
const disk = buf.readUInt16LE(eocd + 4);
|
|
139
|
+
const cdDisk = buf.readUInt16LE(eocd + 6);
|
|
140
|
+
const count = buf.readUInt16LE(eocd + 8);
|
|
141
|
+
const total = buf.readUInt16LE(eocd + 10);
|
|
142
|
+
const cdSize = buf.readUInt32LE(eocd + 12);
|
|
143
|
+
const cdOffset = buf.readUInt32LE(eocd + 16);
|
|
144
|
+
if (disk !== 0 || cdDisk !== 0)
|
|
145
|
+
throw new Error("不支持分卷 ZIP");
|
|
146
|
+
if (count === 0xffff || total === 0xffff || cdSize === 0xffffffff || cdOffset === 0xffffffff) {
|
|
147
|
+
throw new Error("不支持 ZIP64");
|
|
148
|
+
}
|
|
149
|
+
if (count !== total)
|
|
150
|
+
throw new Error("ZIP 中央目录计数不一致");
|
|
151
|
+
if (count > lim.maxEntries)
|
|
152
|
+
throw new Error("ZIP 文件数超过上限");
|
|
153
|
+
if (cdOffset + cdSize > buf.length)
|
|
154
|
+
throw new Error("ZIP 中央目录越界");
|
|
155
|
+
const out = new Map();
|
|
156
|
+
let cursor = cdOffset;
|
|
157
|
+
let totalBytes = 0;
|
|
158
|
+
for (let i = 0; i < count; i++) {
|
|
159
|
+
if (cursor + 46 > buf.length || buf.readUInt32LE(cursor) !== 0x02014b50) {
|
|
160
|
+
throw new Error("ZIP 中央目录损坏");
|
|
161
|
+
}
|
|
162
|
+
const flags = buf.readUInt16LE(cursor + 8);
|
|
163
|
+
const method = buf.readUInt16LE(cursor + 10);
|
|
164
|
+
const crc = buf.readUInt32LE(cursor + 16);
|
|
165
|
+
const compSize = buf.readUInt32LE(cursor + 20);
|
|
166
|
+
const size = buf.readUInt32LE(cursor + 24);
|
|
167
|
+
const nameLen = buf.readUInt16LE(cursor + 28);
|
|
168
|
+
const extraLen = buf.readUInt16LE(cursor + 30);
|
|
169
|
+
const commentLen = buf.readUInt16LE(cursor + 32);
|
|
170
|
+
const localOffset = buf.readUInt32LE(cursor + 42);
|
|
171
|
+
const nameStart = cursor + 46;
|
|
172
|
+
const nameEnd = nameStart + nameLen;
|
|
173
|
+
if (nameEnd > buf.length)
|
|
174
|
+
throw new Error("ZIP 条目名越界");
|
|
175
|
+
const name = buf.subarray(nameStart, nameEnd).toString("utf8");
|
|
176
|
+
assertSafeName(name);
|
|
177
|
+
if (out.has(name))
|
|
178
|
+
throw new Error("ZIP 条目重名:" + name);
|
|
179
|
+
if (flags & 0x0001)
|
|
180
|
+
throw new Error("不支持加密 ZIP");
|
|
181
|
+
if (flags & 0x0008)
|
|
182
|
+
throw new Error("不支持 data descriptor ZIP");
|
|
183
|
+
if (method !== 0 && method !== 8)
|
|
184
|
+
throw new Error("不支持的 ZIP 压缩方法:" + method);
|
|
185
|
+
if (size > lim.maxEntryBytes)
|
|
186
|
+
throw new Error("ZIP 单文件超过上限:" + name);
|
|
187
|
+
if (compSize > lim.maxEntryBytes + 1024)
|
|
188
|
+
throw new Error("ZIP 压缩条目异常:" + name);
|
|
189
|
+
if (localOffset + 30 > buf.length || buf.readUInt32LE(localOffset) !== 0x04034b50) {
|
|
190
|
+
throw new Error("ZIP 本地文件头损坏:" + name);
|
|
191
|
+
}
|
|
192
|
+
const localNameLen = buf.readUInt16LE(localOffset + 26);
|
|
193
|
+
const localExtraLen = buf.readUInt16LE(localOffset + 28);
|
|
194
|
+
const dataStart = localOffset + 30 + localNameLen + localExtraLen;
|
|
195
|
+
const dataEnd = dataStart + compSize;
|
|
196
|
+
if (dataEnd > buf.length)
|
|
197
|
+
throw new Error("ZIP 条目数据越界:" + name);
|
|
198
|
+
const body = buf.subarray(dataStart, dataEnd);
|
|
199
|
+
let data;
|
|
200
|
+
try {
|
|
201
|
+
data = method === 8 ? inflateRawSync(body) : Buffer.from(body);
|
|
202
|
+
}
|
|
203
|
+
catch (e) {
|
|
204
|
+
throw new Error("ZIP 解压失败:" + name + "(" + String((e && e.message) || e) + ")");
|
|
205
|
+
}
|
|
206
|
+
if (data.length !== size)
|
|
207
|
+
throw new Error("ZIP 条目大小不符:" + name);
|
|
208
|
+
if (crc32(data) !== crc)
|
|
209
|
+
throw new Error("ZIP 条目校验失败:" + name);
|
|
210
|
+
totalBytes += data.length;
|
|
211
|
+
if (totalBytes > lim.maxTotalBytes)
|
|
212
|
+
throw new Error("ZIP 解压后总大小超过上限");
|
|
213
|
+
out.set(name, data);
|
|
214
|
+
cursor = nameEnd + extraLen + commentLen;
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { applyIncoming, incomingTransaction } from "./sync-incoming.js";
|
|
2
|
+
import { parseObject, stableStringify } from "./sync-values.js";
|
|
3
|
+
export function listConflicts(db, limit = 100) {
|
|
4
|
+
return db.prepare("SELECT * FROM conflict_shadow ORDER BY id DESC LIMIT ?").all(Math.max(1, Math.min(500, Number(limit) || 100)));
|
|
5
|
+
}
|
|
6
|
+
/** Domain declarations are required for domain-specific validation during manual resolution. */
|
|
7
|
+
export function resolveConflict(db, id, action, now, domains = []) {
|
|
8
|
+
return incomingTransaction(db, () => {
|
|
9
|
+
const shadow = db.prepare("SELECT * FROM conflict_shadow WHERE id=?").get(id);
|
|
10
|
+
if (!shadow)
|
|
11
|
+
return false;
|
|
12
|
+
if (action === "dismiss") {
|
|
13
|
+
db.prepare("DELETE FROM conflict_shadow WHERE id=?").run(id);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
const name = String(shadow.table_name);
|
|
17
|
+
const table = domains.flatMap(d => d.sync?.tables ?? []).find(t => t.name === name) ?? { name, mode: "row" };
|
|
18
|
+
const row = parseObject(action === "keep_server" ? shadow.server_payload : shadow.local_payload);
|
|
19
|
+
if (!row)
|
|
20
|
+
return false;
|
|
21
|
+
if (row.id !== shadow.row_id)
|
|
22
|
+
throw new Error("SYNC_ROW_ID_MISMATCH");
|
|
23
|
+
if (action === "keep_local") {
|
|
24
|
+
row.updated_at = Math.max(Number(row.updated_at) || 0, Number(shadow.server_updated_at) || 0, now) + 1;
|
|
25
|
+
row.rev = (Number(row.rev) || 1) + 1;
|
|
26
|
+
}
|
|
27
|
+
if (!applyIncoming(db, table, row, Number(row.updated_at) || now, undefined, true))
|
|
28
|
+
return false;
|
|
29
|
+
if (action === "keep_local") {
|
|
30
|
+
const stored = db.prepare("SELECT * FROM " + name + " WHERE id=?").get(row.id);
|
|
31
|
+
db.prepare("INSERT INTO sync_change_log(table_name,row_id,op,payload,origin,updated_at) VALUES(?,?,?,?,?,?)")
|
|
32
|
+
.run(name, row.id, row.deleted_at == null ? "upsert" : "delete", stableStringify(stored), "conflict-resolve", row.updated_at);
|
|
33
|
+
}
|
|
34
|
+
db.prepare("DELETE FROM conflict_shadow WHERE id=?").run(id);
|
|
35
|
+
return true;
|
|
36
|
+
});
|
|
37
|
+
}
|