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