luckytri 1.0.0 → 1.0.1
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/.github/SECURITY.md +4 -4
- package/README.en.md +252 -250
- package/README.md +252 -250
- package/bin/luckytri.js +5 -1
- package/docs/en/CHANGELOG.md +8 -0
- package/docs/en/connect.md +18 -18
- package/docs/en/guide.md +192 -216
- package/docs/en/npm-release.md +25 -25
- package/docs/zh/CHANGELOG.md +8 -0
- package/docs/zh/connect.md +17 -17
- package/docs/zh/guide.md +192 -216
- package/docs/zh/npm-release.md +33 -33
- package/package.json +109 -108
- package/plugins/sdk/index.d.ts +155 -155
- package/plugins/sdk/testing.js +58 -58
- package/plugins/weather/index.js +82 -82
- package/plugins/weather/luckytri-plugin.json +29 -29
- package/public/app/assets/ChatsPage-B9Odvzfz.js +3 -0
- package/public/app/assets/Empty-SkA8PyMo.js +2 -0
- package/public/app/assets/Field-BjLhi8pL.js +1 -0
- package/public/app/assets/HeartPage-CscKqMsr.js +1 -0
- package/public/app/assets/LifePage-BHCmBHlH.js +1 -0
- package/public/app/assets/MemoryPage-Bhs2xa1k.js +1 -0
- package/public/app/assets/NaturePage-CaPIar_5.js +3 -0
- package/public/app/assets/PeoplePage-CuwSSVeX.js +1 -0
- package/public/app/assets/PluginsPage-BhVm1SUi.js +2 -0
- package/public/app/assets/PluginsPage-CVVQwEBI.css +1 -0
- package/public/app/assets/{SystemPage-CAMPoYqM.css → SystemPage-5yYqyUgR.css} +1 -1
- package/public/app/assets/SystemPage-Chm5fiGP.js +1 -0
- package/public/app/assets/TimePage-BUm4eUzB.js +3 -0
- package/public/app/assets/index-BIAdfObg.js +4 -0
- package/public/app/assets/{index-YDz1ree1.css → index-DY4e15Kh.css} +1 -1
- package/public/app/assets/{knowledge-DG0e0uKw.js → knowledge-DNDLyN9m.js} +1 -1
- package/public/app/assets/media-CKVAvML2.js +1 -0
- package/public/app/index.html +3 -3
- package/public/guide.en.html +32 -99
- package/public/guide.html +21 -86
- package/public/tutorial.en.md +15 -39
- package/public/tutorial.md +15 -39
- package/scripts/backup.js +311 -311
- package/scripts/browser.js +41 -41
- package/scripts/launch.js +101 -94
- package/scripts/plugin.js +150 -150
- package/scripts/reset-password.js +22 -0
- package/scripts/runtime.js +25 -25
- package/scripts/service.js +56 -47
- package/scripts/setup.js +50 -50
- package/scripts/start-server.js +41 -41
- package/scripts/stop.js +36 -36
- package/server/app.js +110 -96
- package/server/auth.js +217 -0
- package/server/channels/contract.js +57 -57
- package/server/channels/hub.js +165 -165
- package/server/channels/index.js +28 -25
- package/server/channels/onebot/index.js +335 -336
- package/server/channels/onebot/token.js +22 -3
- package/server/core/capabilities.js +208 -208
- package/server/core/events.js +64 -60
- package/server/core/image-preprocessor.js +208 -208
- package/server/core/model-manager.js +1213 -1213
- package/server/core/net-guard.js +49 -49
- package/server/core/orchestrator.js +569 -569
- package/server/core/perceivers.js +91 -91
- package/server/core/persona-manager.js +243 -243
- package/server/core/repository.js +293 -293
- package/server/core/response-validator.js +508 -508
- package/server/core/turn-processor.js +630 -630
- package/server/core/turn.js +236 -236
- package/server/core/vision-manager.js +633 -633
- package/server/i18n/errors.js +512 -497
- package/server/i18n/labels.js +649 -653
- package/server/index.js +107 -102
- package/server/knowledge/manager.js +386 -386
- package/server/local-access.js +32 -0
- package/server/mind/anticipations.js +507 -507
- package/server/mind/api.js +703 -703
- package/server/mind/budget.js +152 -152
- package/server/mind/context-selection.js +61 -61
- package/server/mind/day-planner.js +324 -324
- package/server/mind/evidence.js +148 -148
- package/server/mind/index.js +445 -445
- package/server/mind/life/diary.js +786 -786
- package/server/mind/life/index.js +1049 -1049
- package/server/mind/observations.js +84 -84
- package/server/mind/own-day.js +324 -324
- package/server/mind/own-voice.js +318 -318
- package/server/mind/senses.js +53 -53
- package/server/mind/time/activity-clock.js +196 -196
- package/server/mind/time/availability.js +69 -69
- package/server/mind/time/commitment-review.js +198 -198
- package/server/mind/time/executor.js +512 -512
- package/server/mind/time/experiences.js +128 -128
- package/server/mind/time/games.js +562 -562
- package/server/mind/time/index.js +739 -739
- package/server/mind/time/intent.js +166 -166
- package/server/mind/time/kinds.js +171 -171
- package/server/mind/time/plugin-activity.js +207 -207
- package/server/mind/time/tasks.js +587 -587
- package/server/mind/util.js +110 -110
- package/server/mind/view.js +273 -273
- package/server/paths.js +102 -102
- package/server/plugins/api.js +204 -204
- package/server/plugins/host.js +967 -967
- package/server/plugins/install.js +295 -295
- package/server/plugins/manifest.js +201 -201
- package/server/plugins/permissions.js +42 -42
- package/server/plugins/runner/guard.mjs +36 -36
- package/server/plugins/runner/worker.mjs +250 -250
- package/server/plugins/schema.js +91 -91
- package/server/plugins/zip.js +112 -112
- package/server/startup-lock.js +118 -118
- package/server/storage/archive.js +241 -241
- package/server/storage/backup-cleanup-worker.js +8 -8
- package/server/storage/backup-cleanup.js +414 -414
- package/server/storage/backup-lock.js +76 -76
- package/server/storage/backup-scheduler.js +131 -131
- package/server/storage/store.js +199 -199
- package/server/studio/readiness.js +181 -190
- package/server/studio/settings.js +236 -236
- package/public/app/assets/ChatsPage-C-FcRapR.js +0 -3
- package/public/app/assets/Empty-CpP5kQHH.js +0 -2
- package/public/app/assets/Field-D4gY-INU.js +0 -1
- package/public/app/assets/HeartPage-CPy_thQd.js +0 -1
- package/public/app/assets/LifePage-CxWvwmSf.js +0 -1
- package/public/app/assets/MemoryPage-DC4rhQ68.js +0 -1
- package/public/app/assets/NaturePage-IvYw04Zq.js +0 -3
- package/public/app/assets/PeoplePage-Cqefm9-q.js +0 -1
- package/public/app/assets/PluginsPage-DKSaKvXb.js +0 -2
- package/public/app/assets/PluginsPage-HyBe5D8w.css +0 -1
- package/public/app/assets/SystemPage-CJmtJkrK.js +0 -1
- package/public/app/assets/TimePage-Cw44inM4.js +0 -3
- package/public/app/assets/index-BheRW-1t.js +0 -4
- package/public/app/assets/media-BzPDBm7e.js +0 -1
|
@@ -1,414 +1,414 @@
|
|
|
1
|
-
import { Worker } from "node:worker_threads";
|
|
2
|
-
import { runtimePaths } from "../paths.js";
|
|
3
|
-
import { backupDirectory } from "../../scripts/backup.js";
|
|
4
|
-
import { dirname, join, resolve } from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
existsSync,
|
|
7
|
-
lstatSync,
|
|
8
|
-
readdirSync,
|
|
9
|
-
readFileSync,
|
|
10
|
-
realpathSync,
|
|
11
|
-
rmSync,
|
|
12
|
-
statSync,
|
|
13
|
-
} from "node:fs";
|
|
14
|
-
import { archiveIdentity, verifyArchive } from "./archive.js";
|
|
15
|
-
import { withBackupLock } from "./backup-lock.js";
|
|
16
|
-
|
|
17
|
-
const DAY = 86400000;
|
|
18
|
-
const DEFAULTS = Object.freeze({
|
|
19
|
-
enabled: true,
|
|
20
|
-
time: "04:30",
|
|
21
|
-
retainDays: 14,
|
|
22
|
-
keepFull: 2,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
function kind(scope, name) {
|
|
26
|
-
if (scope === "root")
|
|
27
|
-
return /^friend\.pre-[\w-]+\.db$/.test(name) ? "snapshot" : null;
|
|
28
|
-
if (/^luckytri-auto-\d{4}-\d\d-\d\dT[\d-]+Z\.db$/.test(name)) return "auto";
|
|
29
|
-
if (/^luckytri-migration-v\d+-to-v\d+-\d+-[\da-f]{8}\.db$/.test(name))
|
|
30
|
-
return "migration";
|
|
31
|
-
if (/^luckytri-\d{4}-\d\d-\d\dT[\d-]+Z-[\da-f]{8}\.db$/.test(name))
|
|
32
|
-
return "full";
|
|
33
|
-
if (/^(?:luckybot|unlucky|xiaoman)-[\w-]+\.db$/.test(name)) return "legacy";
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function stamped(name, modified) {
|
|
38
|
-
const match = /(\d{4}-\d\d-\d\d)T(\d\d)-(\d\d)-(\d\d)-(\d{3})Z/.exec(name);
|
|
39
|
-
const value = match
|
|
40
|
-
? Date.parse(`${match[1]}T${match[2]}:${match[3]}:${match[4]}.${match[5]}Z`)
|
|
41
|
-
: NaN;
|
|
42
|
-
return Number.isFinite(value) ? value : modified;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function cleanupPolicy(value = {}) {
|
|
46
|
-
if (
|
|
47
|
-
typeof value !== "object" ||
|
|
48
|
-
!value ||
|
|
49
|
-
Array.isArray(value) ||
|
|
50
|
-
typeof value.enabled !== "boolean" ||
|
|
51
|
-
!/^([01]\d|2[0-3]):[0-5]\d$/.test(value.time) ||
|
|
52
|
-
!Number.isInteger(value.retainDays) ||
|
|
53
|
-
value.retainDays < 1 ||
|
|
54
|
-
value.retainDays > 3650 ||
|
|
55
|
-
!Number.isInteger(value.keepFull) ||
|
|
56
|
-
value.keepFull < 0 ||
|
|
57
|
-
value.keepFull > 100
|
|
58
|
-
)
|
|
59
|
-
throw Error("清理规则无效:保留天数为 1–3650,完整备份数量为 0–100");
|
|
60
|
-
return Object.fromEntries(
|
|
61
|
-
Object.keys(DEFAULTS).map((key) => [key, value[key]]),
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function backupInventory({ directory, source }) {
|
|
66
|
-
const root = dirname(resolve(source));
|
|
67
|
-
const sourcePath = existsSync(source)
|
|
68
|
-
? realpathSync(source)
|
|
69
|
-
: resolve(source);
|
|
70
|
-
const sourceStat = existsSync(source) ? statSync(source) : null;
|
|
71
|
-
const sourceId = sourceStat ? archiveIdentity(source) : null;
|
|
72
|
-
const normalize = (path) =>
|
|
73
|
-
process.platform === "win32" ? path.toLowerCase() : path;
|
|
74
|
-
const folders = [
|
|
75
|
-
["backup", resolve(directory)],
|
|
76
|
-
["root", root],
|
|
77
|
-
];
|
|
78
|
-
const entries = [];
|
|
79
|
-
for (const [scope, folder] of folders) {
|
|
80
|
-
if (!existsSync(folder)) continue;
|
|
81
|
-
for (const entry of readdirSync(folder, { withFileTypes: true })) {
|
|
82
|
-
const category = kind(scope, entry.name);
|
|
83
|
-
if (!category || !entry.isFile()) continue;
|
|
84
|
-
const path = join(folder, entry.name);
|
|
85
|
-
const stat = lstatSync(path);
|
|
86
|
-
if (!stat.isFile()) continue;
|
|
87
|
-
if (
|
|
88
|
-
normalize(realpathSync(path)) === normalize(sourcePath) ||
|
|
89
|
-
(sourceStat?.ino &&
|
|
90
|
-
stat.ino === sourceStat.ino &&
|
|
91
|
-
stat.dev === sourceStat.dev)
|
|
92
|
-
)
|
|
93
|
-
continue;
|
|
94
|
-
let databaseId = null;
|
|
95
|
-
const manifestPath = `${path}.json`;
|
|
96
|
-
if (existsSync(manifestPath)) {
|
|
97
|
-
try {
|
|
98
|
-
if (lstatSync(manifestPath).isFile())
|
|
99
|
-
databaseId =
|
|
100
|
-
JSON.parse(readFileSync(manifestPath, "utf8")).databaseId || null;
|
|
101
|
-
} catch {
|
|
102
|
-
// A damaged manifest is not a trusted recovery point.
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
const inUse = existsSync(`${path}-wal`) || existsSync(`${path}-shm`);
|
|
106
|
-
entries.push({
|
|
107
|
-
scope,
|
|
108
|
-
name: entry.name,
|
|
109
|
-
kind: category,
|
|
110
|
-
bytes: stat.size,
|
|
111
|
-
modified: stat.mtimeMs,
|
|
112
|
-
at: stamped(entry.name, stat.mtimeMs),
|
|
113
|
-
manifest: existsSync(`${path}.json`),
|
|
114
|
-
databaseId,
|
|
115
|
-
protected: inUse,
|
|
116
|
-
protectionReason: inUse ? "存在运行伴随文件" : "",
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
entries.sort((a, b) => b.at - a.at || b.name.localeCompare(a.name));
|
|
121
|
-
for (const entry of entries
|
|
122
|
-
.filter(
|
|
123
|
-
(item) =>
|
|
124
|
-
item.kind === "auto" && sourceId && item.databaseId === sourceId,
|
|
125
|
-
)
|
|
126
|
-
.slice(0, 2)) {
|
|
127
|
-
entry.protected = true;
|
|
128
|
-
entry.protectionReason = "近期自动恢复点";
|
|
129
|
-
}
|
|
130
|
-
return entries;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export function cleanupPlan(entries, policy, now = Date.now()) {
|
|
134
|
-
const full = entries.filter((item) => item.kind === "full");
|
|
135
|
-
const items = full.filter(
|
|
136
|
-
(item, index) =>
|
|
137
|
-
!item.protected &&
|
|
138
|
-
(index >= policy.keepFull || item.at < now - policy.retainDays * DAY),
|
|
139
|
-
);
|
|
140
|
-
return { items, bytes: items.reduce((sum, item) => sum + item.bytes, 0) };
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function safeRecoveryPoint(entries, directory, source, now, chosen) {
|
|
144
|
-
const identity = archiveIdentity(source);
|
|
145
|
-
const selected = new Set(chosen.map((item) => `${item.scope}:${item.name}`));
|
|
146
|
-
for (const candidate of entries.filter(
|
|
147
|
-
(item) =>
|
|
148
|
-
item.kind === "auto" &&
|
|
149
|
-
identity &&
|
|
150
|
-
item.databaseId === identity &&
|
|
151
|
-
item.manifest &&
|
|
152
|
-
now - item.at <= 72 * 3600000 &&
|
|
153
|
-
item.at <= now + 5 * 60000 &&
|
|
154
|
-
!selected.has(`${item.scope}:${item.name}`),
|
|
155
|
-
)) {
|
|
156
|
-
try {
|
|
157
|
-
const result = verifyArchive(join(resolve(directory), candidate.name));
|
|
158
|
-
if (result.databaseId === identity)
|
|
159
|
-
return {
|
|
160
|
-
name: candidate.name,
|
|
161
|
-
at: candidate.at,
|
|
162
|
-
bytes: candidate.bytes,
|
|
163
|
-
};
|
|
164
|
-
} catch {
|
|
165
|
-
// A newer damaged copy must not hide an older valid recovery point.
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
throw Error("缺少同一实例近 72 小时内可校验的自动备份,已停止清理");
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export function executeCleanup({
|
|
172
|
-
directory,
|
|
173
|
-
source,
|
|
174
|
-
policy,
|
|
175
|
-
mode,
|
|
176
|
-
selection = [],
|
|
177
|
-
now = Date.now(),
|
|
178
|
-
remove = rmSync,
|
|
179
|
-
}) {
|
|
180
|
-
return withBackupLock(directory, () =>
|
|
181
|
-
cleanupBatch({ directory, source, policy, mode, selection, now, remove }),
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function cleanupBatch({
|
|
186
|
-
directory,
|
|
187
|
-
source,
|
|
188
|
-
policy,
|
|
189
|
-
mode,
|
|
190
|
-
selection,
|
|
191
|
-
now,
|
|
192
|
-
remove,
|
|
193
|
-
}) {
|
|
194
|
-
const entries = backupInventory({ directory, source });
|
|
195
|
-
const chosen =
|
|
196
|
-
mode === "scheduled"
|
|
197
|
-
? cleanupPlan(entries, cleanupPolicy(policy), now).items
|
|
198
|
-
: selection.map((request) => {
|
|
199
|
-
if (
|
|
200
|
-
!request ||
|
|
201
|
-
typeof request.name !== "string" ||
|
|
202
|
-
typeof request.scope !== "string" ||
|
|
203
|
-
!Number.isSafeInteger(request.bytes) ||
|
|
204
|
-
!Number.isFinite(request.modified)
|
|
205
|
-
)
|
|
206
|
-
throw Error("选择的备份信息无效");
|
|
207
|
-
const found = entries.find(
|
|
208
|
-
(item) =>
|
|
209
|
-
item.name === request.name && item.scope === request.scope,
|
|
210
|
-
);
|
|
211
|
-
if (
|
|
212
|
-
!found ||
|
|
213
|
-
found.protected ||
|
|
214
|
-
found.bytes !== request.bytes ||
|
|
215
|
-
found.modified !== request.modified
|
|
216
|
-
)
|
|
217
|
-
throw Error("备份已变化或属于必须保留的恢复点,请刷新列表");
|
|
218
|
-
return found;
|
|
219
|
-
});
|
|
220
|
-
if (!["scheduled", "manual"].includes(mode)) throw Error("清理方式无效");
|
|
221
|
-
if (
|
|
222
|
-
new Set(chosen.map((item) => `${item.scope}:${item.name}`)).size !==
|
|
223
|
-
chosen.length
|
|
224
|
-
)
|
|
225
|
-
throw Error("不能重复选择同一份备份");
|
|
226
|
-
if (!chosen.length) return { removed: [], bytes: 0, failures: [] };
|
|
227
|
-
const recovery = safeRecoveryPoint(entries, directory, source, now, chosen);
|
|
228
|
-
const removed = [];
|
|
229
|
-
const failures = [];
|
|
230
|
-
for (const item of chosen) {
|
|
231
|
-
const folder =
|
|
232
|
-
item.scope === "root" ? dirname(resolve(source)) : resolve(directory);
|
|
233
|
-
const path = join(folder, item.name);
|
|
234
|
-
try {
|
|
235
|
-
const stat = lstatSync(path);
|
|
236
|
-
if (
|
|
237
|
-
!stat.isFile() ||
|
|
238
|
-
stat.size !== item.bytes ||
|
|
239
|
-
stat.mtimeMs !== item.modified ||
|
|
240
|
-
existsSync(`${path}-wal`) ||
|
|
241
|
-
existsSync(`${path}-shm`)
|
|
242
|
-
)
|
|
243
|
-
throw Error("备份在清理期间发生变化,已停止后续清理");
|
|
244
|
-
remove(path);
|
|
245
|
-
removed.push({ scope: item.scope, name: item.name, bytes: item.bytes });
|
|
246
|
-
remove(`${path}.json`, { force: true });
|
|
247
|
-
} catch (error) {
|
|
248
|
-
failures.push({
|
|
249
|
-
scope: item.scope,
|
|
250
|
-
name: item.name,
|
|
251
|
-
error: error.code
|
|
252
|
-
? `删除失败(${error.code})`
|
|
253
|
-
: "备份文件已变化或不可删除",
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return {
|
|
258
|
-
removed,
|
|
259
|
-
bytes: removed.reduce((sum, item) => sum + item.bytes, 0),
|
|
260
|
-
failures,
|
|
261
|
-
recovery,
|
|
262
|
-
};
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function localDay(now) {
|
|
266
|
-
const d = new Date(now);
|
|
267
|
-
const day = [
|
|
268
|
-
d.getFullYear(),
|
|
269
|
-
String(d.getMonth() + 1).padStart(2, "0"),
|
|
270
|
-
String(d.getDate()).padStart(2, "0"),
|
|
271
|
-
].join("-");
|
|
272
|
-
const time = `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
|
|
273
|
-
return { day, time };
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
function workerRun(payload) {
|
|
277
|
-
return new Promise((resolve, reject) => {
|
|
278
|
-
const worker = new Worker(
|
|
279
|
-
new URL("./backup-cleanup-worker.js", import.meta.url),
|
|
280
|
-
{
|
|
281
|
-
workerData: payload,
|
|
282
|
-
},
|
|
283
|
-
);
|
|
284
|
-
worker.once("message", ({ result, error }) =>
|
|
285
|
-
error ? reject(Error(error)) : resolve(result),
|
|
286
|
-
);
|
|
287
|
-
worker.once("error", reject);
|
|
288
|
-
worker.once("exit", (code) => {
|
|
289
|
-
if (code) reject(Error(`清理工作异常结束:${code}`));
|
|
290
|
-
});
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export function createBackupCleanup({
|
|
295
|
-
store,
|
|
296
|
-
backup,
|
|
297
|
-
source = runtimePaths().database,
|
|
298
|
-
directory = backupDirectory(source),
|
|
299
|
-
now = () => Date.now(),
|
|
300
|
-
run = workerRun,
|
|
301
|
-
log = console,
|
|
302
|
-
} = {}) {
|
|
303
|
-
let running = false;
|
|
304
|
-
const policy = () => ({ ...DEFAULTS, ...store.settings().backupCleanup });
|
|
305
|
-
const last = () => store.settings().backupCleanupLast || null;
|
|
306
|
-
const info = () => {
|
|
307
|
-
const entries = backupInventory({ directory, source });
|
|
308
|
-
return {
|
|
309
|
-
policy: policy(),
|
|
310
|
-
last: last(),
|
|
311
|
-
running,
|
|
312
|
-
serverTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
313
|
-
totalBytes: entries.reduce((sum, item) => sum + item.bytes, 0),
|
|
314
|
-
plan: cleanupPlan(entries, policy(), now()),
|
|
315
|
-
entries,
|
|
316
|
-
};
|
|
317
|
-
};
|
|
318
|
-
const launch = async (mode, selection = []) => {
|
|
319
|
-
if (running || backup?.running)
|
|
320
|
-
throw Error("备份或清理正在进行,请稍后再试");
|
|
321
|
-
running = true;
|
|
322
|
-
try {
|
|
323
|
-
return await run({
|
|
324
|
-
directory,
|
|
325
|
-
source,
|
|
326
|
-
policy: policy(),
|
|
327
|
-
mode,
|
|
328
|
-
selection,
|
|
329
|
-
now: now(),
|
|
330
|
-
});
|
|
331
|
-
} finally {
|
|
332
|
-
running = false;
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
return {
|
|
336
|
-
info,
|
|
337
|
-
get running() {
|
|
338
|
-
return running;
|
|
339
|
-
},
|
|
340
|
-
save(value) {
|
|
341
|
-
const next = cleanupPolicy(value);
|
|
342
|
-
store.save({ backupCleanup: next });
|
|
343
|
-
return info();
|
|
344
|
-
},
|
|
345
|
-
async manual(selection) {
|
|
346
|
-
if (
|
|
347
|
-
!Array.isArray(selection) ||
|
|
348
|
-
!selection.length ||
|
|
349
|
-
selection.length > 100
|
|
350
|
-
)
|
|
351
|
-
throw Error("请先选择要清理的备份,单次最多 100 份");
|
|
352
|
-
return launch("manual", selection);
|
|
353
|
-
},
|
|
354
|
-
tick() {
|
|
355
|
-
const current = policy();
|
|
356
|
-
const clock = localDay(now());
|
|
357
|
-
if (
|
|
358
|
-
!current.enabled ||
|
|
359
|
-
running ||
|
|
360
|
-
backup?.running ||
|
|
361
|
-
last()?.day === clock.day ||
|
|
362
|
-
clock.time < current.time
|
|
363
|
-
)
|
|
364
|
-
return false;
|
|
365
|
-
const preview = cleanupPlan(
|
|
366
|
-
backupInventory({ directory, source }),
|
|
367
|
-
current,
|
|
368
|
-
now(),
|
|
369
|
-
);
|
|
370
|
-
if (!preview.items.length) {
|
|
371
|
-
store.save({
|
|
372
|
-
backupCleanupLast: {
|
|
373
|
-
day: clock.day,
|
|
374
|
-
at: now(),
|
|
375
|
-
removed: 0,
|
|
376
|
-
bytes: 0,
|
|
377
|
-
},
|
|
378
|
-
});
|
|
379
|
-
return false;
|
|
380
|
-
}
|
|
381
|
-
launch("scheduled")
|
|
382
|
-
.then((result) => {
|
|
383
|
-
store.save({
|
|
384
|
-
backupCleanupLast: {
|
|
385
|
-
day: clock.day,
|
|
386
|
-
at: now(),
|
|
387
|
-
removed: result.removed.length,
|
|
388
|
-
bytes: result.bytes,
|
|
389
|
-
...(result.failures?.length
|
|
390
|
-
? {
|
|
391
|
-
error: "部分备份未清理,请刷新列表后重试",
|
|
392
|
-
failures: result.failures,
|
|
393
|
-
}
|
|
394
|
-
: {}),
|
|
395
|
-
},
|
|
396
|
-
});
|
|
397
|
-
log.log(`定时备份整理完成:${result.removed.length} 份`);
|
|
398
|
-
})
|
|
399
|
-
.catch((error) => {
|
|
400
|
-
store.save({
|
|
401
|
-
backupCleanupLast: {
|
|
402
|
-
day: clock.day,
|
|
403
|
-
at: now(),
|
|
404
|
-
removed: 0,
|
|
405
|
-
bytes: 0,
|
|
406
|
-
error: error.message,
|
|
407
|
-
},
|
|
408
|
-
});
|
|
409
|
-
log.error(`定时备份整理失败:${error.message}`);
|
|
410
|
-
});
|
|
411
|
-
return true;
|
|
412
|
-
},
|
|
413
|
-
};
|
|
414
|
-
}
|
|
1
|
+
import { Worker } from "node:worker_threads";
|
|
2
|
+
import { runtimePaths } from "../paths.js";
|
|
3
|
+
import { backupDirectory } from "../../scripts/backup.js";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
existsSync,
|
|
7
|
+
lstatSync,
|
|
8
|
+
readdirSync,
|
|
9
|
+
readFileSync,
|
|
10
|
+
realpathSync,
|
|
11
|
+
rmSync,
|
|
12
|
+
statSync,
|
|
13
|
+
} from "node:fs";
|
|
14
|
+
import { archiveIdentity, verifyArchive } from "./archive.js";
|
|
15
|
+
import { withBackupLock } from "./backup-lock.js";
|
|
16
|
+
|
|
17
|
+
const DAY = 86400000;
|
|
18
|
+
const DEFAULTS = Object.freeze({
|
|
19
|
+
enabled: true,
|
|
20
|
+
time: "04:30",
|
|
21
|
+
retainDays: 14,
|
|
22
|
+
keepFull: 2,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
function kind(scope, name) {
|
|
26
|
+
if (scope === "root")
|
|
27
|
+
return /^friend\.pre-[\w-]+\.db$/.test(name) ? "snapshot" : null;
|
|
28
|
+
if (/^luckytri-auto-\d{4}-\d\d-\d\dT[\d-]+Z\.db$/.test(name)) return "auto";
|
|
29
|
+
if (/^luckytri-migration-v\d+-to-v\d+-\d+-[\da-f]{8}\.db$/.test(name))
|
|
30
|
+
return "migration";
|
|
31
|
+
if (/^luckytri-\d{4}-\d\d-\d\dT[\d-]+Z-[\da-f]{8}\.db$/.test(name))
|
|
32
|
+
return "full";
|
|
33
|
+
if (/^(?:luckybot|unlucky|xiaoman)-[\w-]+\.db$/.test(name)) return "legacy";
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function stamped(name, modified) {
|
|
38
|
+
const match = /(\d{4}-\d\d-\d\d)T(\d\d)-(\d\d)-(\d\d)-(\d{3})Z/.exec(name);
|
|
39
|
+
const value = match
|
|
40
|
+
? Date.parse(`${match[1]}T${match[2]}:${match[3]}:${match[4]}.${match[5]}Z`)
|
|
41
|
+
: NaN;
|
|
42
|
+
return Number.isFinite(value) ? value : modified;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function cleanupPolicy(value = {}) {
|
|
46
|
+
if (
|
|
47
|
+
typeof value !== "object" ||
|
|
48
|
+
!value ||
|
|
49
|
+
Array.isArray(value) ||
|
|
50
|
+
typeof value.enabled !== "boolean" ||
|
|
51
|
+
!/^([01]\d|2[0-3]):[0-5]\d$/.test(value.time) ||
|
|
52
|
+
!Number.isInteger(value.retainDays) ||
|
|
53
|
+
value.retainDays < 1 ||
|
|
54
|
+
value.retainDays > 3650 ||
|
|
55
|
+
!Number.isInteger(value.keepFull) ||
|
|
56
|
+
value.keepFull < 0 ||
|
|
57
|
+
value.keepFull > 100
|
|
58
|
+
)
|
|
59
|
+
throw Error("清理规则无效:保留天数为 1–3650,完整备份数量为 0–100");
|
|
60
|
+
return Object.fromEntries(
|
|
61
|
+
Object.keys(DEFAULTS).map((key) => [key, value[key]]),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function backupInventory({ directory, source }) {
|
|
66
|
+
const root = dirname(resolve(source));
|
|
67
|
+
const sourcePath = existsSync(source)
|
|
68
|
+
? realpathSync(source)
|
|
69
|
+
: resolve(source);
|
|
70
|
+
const sourceStat = existsSync(source) ? statSync(source) : null;
|
|
71
|
+
const sourceId = sourceStat ? archiveIdentity(source) : null;
|
|
72
|
+
const normalize = (path) =>
|
|
73
|
+
process.platform === "win32" ? path.toLowerCase() : path;
|
|
74
|
+
const folders = [
|
|
75
|
+
["backup", resolve(directory)],
|
|
76
|
+
["root", root],
|
|
77
|
+
];
|
|
78
|
+
const entries = [];
|
|
79
|
+
for (const [scope, folder] of folders) {
|
|
80
|
+
if (!existsSync(folder)) continue;
|
|
81
|
+
for (const entry of readdirSync(folder, { withFileTypes: true })) {
|
|
82
|
+
const category = kind(scope, entry.name);
|
|
83
|
+
if (!category || !entry.isFile()) continue;
|
|
84
|
+
const path = join(folder, entry.name);
|
|
85
|
+
const stat = lstatSync(path);
|
|
86
|
+
if (!stat.isFile()) continue;
|
|
87
|
+
if (
|
|
88
|
+
normalize(realpathSync(path)) === normalize(sourcePath) ||
|
|
89
|
+
(sourceStat?.ino &&
|
|
90
|
+
stat.ino === sourceStat.ino &&
|
|
91
|
+
stat.dev === sourceStat.dev)
|
|
92
|
+
)
|
|
93
|
+
continue;
|
|
94
|
+
let databaseId = null;
|
|
95
|
+
const manifestPath = `${path}.json`;
|
|
96
|
+
if (existsSync(manifestPath)) {
|
|
97
|
+
try {
|
|
98
|
+
if (lstatSync(manifestPath).isFile())
|
|
99
|
+
databaseId =
|
|
100
|
+
JSON.parse(readFileSync(manifestPath, "utf8")).databaseId || null;
|
|
101
|
+
} catch {
|
|
102
|
+
// A damaged manifest is not a trusted recovery point.
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const inUse = existsSync(`${path}-wal`) || existsSync(`${path}-shm`);
|
|
106
|
+
entries.push({
|
|
107
|
+
scope,
|
|
108
|
+
name: entry.name,
|
|
109
|
+
kind: category,
|
|
110
|
+
bytes: stat.size,
|
|
111
|
+
modified: stat.mtimeMs,
|
|
112
|
+
at: stamped(entry.name, stat.mtimeMs),
|
|
113
|
+
manifest: existsSync(`${path}.json`),
|
|
114
|
+
databaseId,
|
|
115
|
+
protected: inUse,
|
|
116
|
+
protectionReason: inUse ? "存在运行伴随文件" : "",
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
entries.sort((a, b) => b.at - a.at || b.name.localeCompare(a.name));
|
|
121
|
+
for (const entry of entries
|
|
122
|
+
.filter(
|
|
123
|
+
(item) =>
|
|
124
|
+
item.kind === "auto" && sourceId && item.databaseId === sourceId,
|
|
125
|
+
)
|
|
126
|
+
.slice(0, 2)) {
|
|
127
|
+
entry.protected = true;
|
|
128
|
+
entry.protectionReason = "近期自动恢复点";
|
|
129
|
+
}
|
|
130
|
+
return entries;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function cleanupPlan(entries, policy, now = Date.now()) {
|
|
134
|
+
const full = entries.filter((item) => item.kind === "full");
|
|
135
|
+
const items = full.filter(
|
|
136
|
+
(item, index) =>
|
|
137
|
+
!item.protected &&
|
|
138
|
+
(index >= policy.keepFull || item.at < now - policy.retainDays * DAY),
|
|
139
|
+
);
|
|
140
|
+
return { items, bytes: items.reduce((sum, item) => sum + item.bytes, 0) };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function safeRecoveryPoint(entries, directory, source, now, chosen) {
|
|
144
|
+
const identity = archiveIdentity(source);
|
|
145
|
+
const selected = new Set(chosen.map((item) => `${item.scope}:${item.name}`));
|
|
146
|
+
for (const candidate of entries.filter(
|
|
147
|
+
(item) =>
|
|
148
|
+
item.kind === "auto" &&
|
|
149
|
+
identity &&
|
|
150
|
+
item.databaseId === identity &&
|
|
151
|
+
item.manifest &&
|
|
152
|
+
now - item.at <= 72 * 3600000 &&
|
|
153
|
+
item.at <= now + 5 * 60000 &&
|
|
154
|
+
!selected.has(`${item.scope}:${item.name}`),
|
|
155
|
+
)) {
|
|
156
|
+
try {
|
|
157
|
+
const result = verifyArchive(join(resolve(directory), candidate.name));
|
|
158
|
+
if (result.databaseId === identity)
|
|
159
|
+
return {
|
|
160
|
+
name: candidate.name,
|
|
161
|
+
at: candidate.at,
|
|
162
|
+
bytes: candidate.bytes,
|
|
163
|
+
};
|
|
164
|
+
} catch {
|
|
165
|
+
// A newer damaged copy must not hide an older valid recovery point.
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
throw Error("缺少同一实例近 72 小时内可校验的自动备份,已停止清理");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function executeCleanup({
|
|
172
|
+
directory,
|
|
173
|
+
source,
|
|
174
|
+
policy,
|
|
175
|
+
mode,
|
|
176
|
+
selection = [],
|
|
177
|
+
now = Date.now(),
|
|
178
|
+
remove = rmSync,
|
|
179
|
+
}) {
|
|
180
|
+
return withBackupLock(directory, () =>
|
|
181
|
+
cleanupBatch({ directory, source, policy, mode, selection, now, remove }),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function cleanupBatch({
|
|
186
|
+
directory,
|
|
187
|
+
source,
|
|
188
|
+
policy,
|
|
189
|
+
mode,
|
|
190
|
+
selection,
|
|
191
|
+
now,
|
|
192
|
+
remove,
|
|
193
|
+
}) {
|
|
194
|
+
const entries = backupInventory({ directory, source });
|
|
195
|
+
const chosen =
|
|
196
|
+
mode === "scheduled"
|
|
197
|
+
? cleanupPlan(entries, cleanupPolicy(policy), now).items
|
|
198
|
+
: selection.map((request) => {
|
|
199
|
+
if (
|
|
200
|
+
!request ||
|
|
201
|
+
typeof request.name !== "string" ||
|
|
202
|
+
typeof request.scope !== "string" ||
|
|
203
|
+
!Number.isSafeInteger(request.bytes) ||
|
|
204
|
+
!Number.isFinite(request.modified)
|
|
205
|
+
)
|
|
206
|
+
throw Error("选择的备份信息无效");
|
|
207
|
+
const found = entries.find(
|
|
208
|
+
(item) =>
|
|
209
|
+
item.name === request.name && item.scope === request.scope,
|
|
210
|
+
);
|
|
211
|
+
if (
|
|
212
|
+
!found ||
|
|
213
|
+
found.protected ||
|
|
214
|
+
found.bytes !== request.bytes ||
|
|
215
|
+
found.modified !== request.modified
|
|
216
|
+
)
|
|
217
|
+
throw Error("备份已变化或属于必须保留的恢复点,请刷新列表");
|
|
218
|
+
return found;
|
|
219
|
+
});
|
|
220
|
+
if (!["scheduled", "manual"].includes(mode)) throw Error("清理方式无效");
|
|
221
|
+
if (
|
|
222
|
+
new Set(chosen.map((item) => `${item.scope}:${item.name}`)).size !==
|
|
223
|
+
chosen.length
|
|
224
|
+
)
|
|
225
|
+
throw Error("不能重复选择同一份备份");
|
|
226
|
+
if (!chosen.length) return { removed: [], bytes: 0, failures: [] };
|
|
227
|
+
const recovery = safeRecoveryPoint(entries, directory, source, now, chosen);
|
|
228
|
+
const removed = [];
|
|
229
|
+
const failures = [];
|
|
230
|
+
for (const item of chosen) {
|
|
231
|
+
const folder =
|
|
232
|
+
item.scope === "root" ? dirname(resolve(source)) : resolve(directory);
|
|
233
|
+
const path = join(folder, item.name);
|
|
234
|
+
try {
|
|
235
|
+
const stat = lstatSync(path);
|
|
236
|
+
if (
|
|
237
|
+
!stat.isFile() ||
|
|
238
|
+
stat.size !== item.bytes ||
|
|
239
|
+
stat.mtimeMs !== item.modified ||
|
|
240
|
+
existsSync(`${path}-wal`) ||
|
|
241
|
+
existsSync(`${path}-shm`)
|
|
242
|
+
)
|
|
243
|
+
throw Error("备份在清理期间发生变化,已停止后续清理");
|
|
244
|
+
remove(path);
|
|
245
|
+
removed.push({ scope: item.scope, name: item.name, bytes: item.bytes });
|
|
246
|
+
remove(`${path}.json`, { force: true });
|
|
247
|
+
} catch (error) {
|
|
248
|
+
failures.push({
|
|
249
|
+
scope: item.scope,
|
|
250
|
+
name: item.name,
|
|
251
|
+
error: error.code
|
|
252
|
+
? `删除失败(${error.code})`
|
|
253
|
+
: "备份文件已变化或不可删除",
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
removed,
|
|
259
|
+
bytes: removed.reduce((sum, item) => sum + item.bytes, 0),
|
|
260
|
+
failures,
|
|
261
|
+
recovery,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function localDay(now) {
|
|
266
|
+
const d = new Date(now);
|
|
267
|
+
const day = [
|
|
268
|
+
d.getFullYear(),
|
|
269
|
+
String(d.getMonth() + 1).padStart(2, "0"),
|
|
270
|
+
String(d.getDate()).padStart(2, "0"),
|
|
271
|
+
].join("-");
|
|
272
|
+
const time = `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
|
|
273
|
+
return { day, time };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function workerRun(payload) {
|
|
277
|
+
return new Promise((resolve, reject) => {
|
|
278
|
+
const worker = new Worker(
|
|
279
|
+
new URL("./backup-cleanup-worker.js", import.meta.url),
|
|
280
|
+
{
|
|
281
|
+
workerData: payload,
|
|
282
|
+
},
|
|
283
|
+
);
|
|
284
|
+
worker.once("message", ({ result, error }) =>
|
|
285
|
+
error ? reject(Error(error)) : resolve(result),
|
|
286
|
+
);
|
|
287
|
+
worker.once("error", reject);
|
|
288
|
+
worker.once("exit", (code) => {
|
|
289
|
+
if (code) reject(Error(`清理工作异常结束:${code}`));
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function createBackupCleanup({
|
|
295
|
+
store,
|
|
296
|
+
backup,
|
|
297
|
+
source = runtimePaths().database,
|
|
298
|
+
directory = backupDirectory(source),
|
|
299
|
+
now = () => Date.now(),
|
|
300
|
+
run = workerRun,
|
|
301
|
+
log = console,
|
|
302
|
+
} = {}) {
|
|
303
|
+
let running = false;
|
|
304
|
+
const policy = () => ({ ...DEFAULTS, ...store.settings().backupCleanup });
|
|
305
|
+
const last = () => store.settings().backupCleanupLast || null;
|
|
306
|
+
const info = () => {
|
|
307
|
+
const entries = backupInventory({ directory, source });
|
|
308
|
+
return {
|
|
309
|
+
policy: policy(),
|
|
310
|
+
last: last(),
|
|
311
|
+
running,
|
|
312
|
+
serverTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
313
|
+
totalBytes: entries.reduce((sum, item) => sum + item.bytes, 0),
|
|
314
|
+
plan: cleanupPlan(entries, policy(), now()),
|
|
315
|
+
entries,
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
const launch = async (mode, selection = []) => {
|
|
319
|
+
if (running || backup?.running)
|
|
320
|
+
throw Error("备份或清理正在进行,请稍后再试");
|
|
321
|
+
running = true;
|
|
322
|
+
try {
|
|
323
|
+
return await run({
|
|
324
|
+
directory,
|
|
325
|
+
source,
|
|
326
|
+
policy: policy(),
|
|
327
|
+
mode,
|
|
328
|
+
selection,
|
|
329
|
+
now: now(),
|
|
330
|
+
});
|
|
331
|
+
} finally {
|
|
332
|
+
running = false;
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
return {
|
|
336
|
+
info,
|
|
337
|
+
get running() {
|
|
338
|
+
return running;
|
|
339
|
+
},
|
|
340
|
+
save(value) {
|
|
341
|
+
const next = cleanupPolicy(value);
|
|
342
|
+
store.save({ backupCleanup: next });
|
|
343
|
+
return info();
|
|
344
|
+
},
|
|
345
|
+
async manual(selection) {
|
|
346
|
+
if (
|
|
347
|
+
!Array.isArray(selection) ||
|
|
348
|
+
!selection.length ||
|
|
349
|
+
selection.length > 100
|
|
350
|
+
)
|
|
351
|
+
throw Error("请先选择要清理的备份,单次最多 100 份");
|
|
352
|
+
return launch("manual", selection);
|
|
353
|
+
},
|
|
354
|
+
tick() {
|
|
355
|
+
const current = policy();
|
|
356
|
+
const clock = localDay(now());
|
|
357
|
+
if (
|
|
358
|
+
!current.enabled ||
|
|
359
|
+
running ||
|
|
360
|
+
backup?.running ||
|
|
361
|
+
last()?.day === clock.day ||
|
|
362
|
+
clock.time < current.time
|
|
363
|
+
)
|
|
364
|
+
return false;
|
|
365
|
+
const preview = cleanupPlan(
|
|
366
|
+
backupInventory({ directory, source }),
|
|
367
|
+
current,
|
|
368
|
+
now(),
|
|
369
|
+
);
|
|
370
|
+
if (!preview.items.length) {
|
|
371
|
+
store.save({
|
|
372
|
+
backupCleanupLast: {
|
|
373
|
+
day: clock.day,
|
|
374
|
+
at: now(),
|
|
375
|
+
removed: 0,
|
|
376
|
+
bytes: 0,
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
launch("scheduled")
|
|
382
|
+
.then((result) => {
|
|
383
|
+
store.save({
|
|
384
|
+
backupCleanupLast: {
|
|
385
|
+
day: clock.day,
|
|
386
|
+
at: now(),
|
|
387
|
+
removed: result.removed.length,
|
|
388
|
+
bytes: result.bytes,
|
|
389
|
+
...(result.failures?.length
|
|
390
|
+
? {
|
|
391
|
+
error: "部分备份未清理,请刷新列表后重试",
|
|
392
|
+
failures: result.failures,
|
|
393
|
+
}
|
|
394
|
+
: {}),
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
log.log(`定时备份整理完成:${result.removed.length} 份`);
|
|
398
|
+
})
|
|
399
|
+
.catch((error) => {
|
|
400
|
+
store.save({
|
|
401
|
+
backupCleanupLast: {
|
|
402
|
+
day: clock.day,
|
|
403
|
+
at: now(),
|
|
404
|
+
removed: 0,
|
|
405
|
+
bytes: 0,
|
|
406
|
+
error: error.message,
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
log.error(`定时备份整理失败:${error.message}`);
|
|
410
|
+
});
|
|
411
|
+
return true;
|
|
412
|
+
},
|
|
413
|
+
};
|
|
414
|
+
}
|