zen-fs-config 0.10.1 → 0.10.3
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/dist/index.d.mts +48 -3
- package/dist/index.d.ts +48 -3
- package/dist/index.js +270 -22
- package/dist/index.mjs +270 -22
- package/dist/zen-fs-config.js +270 -22
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1014,8 +1014,13 @@ interface IConfigRepo {
|
|
|
1014
1014
|
* replaceBackend: old pair is rebuilt against the new target).
|
|
1015
1015
|
*/
|
|
1016
1016
|
replaceAppDataGroupBackend(groupId: string, backendId: string, type: string, options: Record<string, unknown>, description?: string): Promise<AppDataGroup>;
|
|
1017
|
-
/**
|
|
1018
|
-
|
|
1017
|
+
/**
|
|
1018
|
+
* Remove a data-sync group (stops sync, removes descriptor).
|
|
1019
|
+
*
|
|
1020
|
+
* @param appId 目标登记所属 appId;默认本实例的 appId。传其它 appId 时只删
|
|
1021
|
+
* 配置仓里该 appId 的登记与引导副本(写墓碑),不会 dispose 别的实例里的组。
|
|
1022
|
+
*/
|
|
1023
|
+
removeAppDataGroup(id: string, appId?: string): Promise<void>;
|
|
1019
1024
|
/**
|
|
1020
1025
|
* List config-sync backends that can be used as account sources
|
|
1021
1026
|
* for data-sync backends. Returns backends that have `accountFields`
|
|
@@ -1353,6 +1358,12 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1353
1358
|
* unlink() 是否成功来判定。
|
|
1354
1359
|
*/
|
|
1355
1360
|
private readonly tombstoneConfirmations;
|
|
1361
|
+
/**
|
|
1362
|
+
* 上次从副本预取墓碑的时间戳(毫秒)。
|
|
1363
|
+
* 节流用:preSyncHook 每轮都会调用 {@link prefetchReplicaTombstones},
|
|
1364
|
+
* 但远端墓碑不会秒级变化,一分钟一次足够。
|
|
1365
|
+
*/
|
|
1366
|
+
private lastTombstonePrefetchAt;
|
|
1356
1367
|
/** Per-backend sync state (successful pairs + connect failures). */
|
|
1357
1368
|
private backendStates;
|
|
1358
1369
|
/**
|
|
@@ -1442,6 +1453,21 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1442
1453
|
private refreshSyncExclusions;
|
|
1443
1454
|
/** Invalidate the tombstone cache — call after tombstones are modified. */
|
|
1444
1455
|
private invalidateTombstoneCache;
|
|
1456
|
+
/**
|
|
1457
|
+
* 把各副本上的墓碑合并进本地,必须早于本轮的快照比对。
|
|
1458
|
+
*
|
|
1459
|
+
* 为什么需要它:墓碑文件本身也是被同步的文件,一个节点在「拿到墓碑」之前
|
|
1460
|
+
* 就已经跑完了 preSyncHook —— 这时它本地没有该路径的墓碑,同步就会把自己
|
|
1461
|
+
* 那份陈旧副本当成「本地新增」推回远端(BiDirectional + source-wins),
|
|
1462
|
+
* 删除被复活。只靠墓碑文件被同步拉下来,永远慢半拍。
|
|
1463
|
+
*
|
|
1464
|
+
* 所以在每轮同步之前先只读地把副本上的 `.meta/tombstones/` 合并到本地:
|
|
1465
|
+
* 合并完成后排除表立刻生效,随后的 processTombstones() 会把本地陈旧副本
|
|
1466
|
+
* 一并清掉,同步再不会把它推上去。
|
|
1467
|
+
*
|
|
1468
|
+
* @param minIntervalMs 节流间隔(0 = 立即执行)。preSyncHook 传 60s。
|
|
1469
|
+
*/
|
|
1470
|
+
private prefetchReplicaTombstones;
|
|
1445
1471
|
/**
|
|
1446
1472
|
* 写入成功即撤销该路径(及其被删祖先目录)上的墓碑。
|
|
1447
1473
|
*
|
|
@@ -1463,6 +1489,16 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1463
1489
|
* 时刻新就意味着这条墓碑已经被写操作撤销。
|
|
1464
1490
|
*/
|
|
1465
1491
|
private isPathRecreatedAfterTombstone;
|
|
1492
|
+
/**
|
|
1493
|
+
* 目录墓碑专用:目录下是否存在「删除之后写入」的真实数据项。
|
|
1494
|
+
*
|
|
1495
|
+
* 只递归看普通文件,不看目录 inode 的 mtime;`.keep` / `.mtime` / `.version`
|
|
1496
|
+
* 这些后端元数据每轮同步都可能被重写,不能当作重建证据。目录里只剩占位
|
|
1497
|
+
* 文件时返回 false —— 那属于待清理残留,不是用户重建。
|
|
1498
|
+
*/
|
|
1499
|
+
private dirHasEntryNewerThan;
|
|
1500
|
+
/** 后端内部元数据:不作为「删除后重建」的证据。 */
|
|
1501
|
+
private isBackendMetadataPath;
|
|
1466
1502
|
/**
|
|
1467
1503
|
* Before sync: for each tombstone, delete the actual file on all replicas.
|
|
1468
1504
|
* This prevents bi-directional sync from copying the file back.
|
|
@@ -1934,7 +1970,15 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1934
1970
|
* 真实意图,允许推送空副本,不参与自愈。
|
|
1935
1971
|
*/
|
|
1936
1972
|
private healEmptyBootstrapDescriptors;
|
|
1937
|
-
|
|
1973
|
+
/**
|
|
1974
|
+
* 删除一个数据组的登记(`.config/app-data-groups/{appId}/{id}.json` + 该组
|
|
1975
|
+
* 在配置仓的后端描述符)。
|
|
1976
|
+
*
|
|
1977
|
+
* @param appId 目标登记所属的 appId,默认为本实例的 appId。传其它 appId 时
|
|
1978
|
+
* 只删配置仓里那份登记与引导副本,不会(也无法)去 dispose 另一个
|
|
1979
|
+
* appId 实例里的组句柄 —— 调用方应优先走实例注册表,让实例自己刷新。
|
|
1980
|
+
*/
|
|
1981
|
+
removeAppDataGroup(id: string, appId?: string): Promise<void>;
|
|
1938
1982
|
listAccountBackends(): Promise<BackendDescriptor[]>;
|
|
1939
1983
|
private tryParse;
|
|
1940
1984
|
private assertNotDisposed;
|
|
@@ -2708,6 +2752,7 @@ declare class TombstoneStore {
|
|
|
2708
2752
|
* 本来就旧,用副本判定既不可靠又会掩盖真正的删除传播。
|
|
2709
2753
|
*/
|
|
2710
2754
|
private wasRecreatedAfterDeletion;
|
|
2755
|
+
private dirHasEntryNewerThan;
|
|
2711
2756
|
private mtimeOf;
|
|
2712
2757
|
private deleteEverywhere;
|
|
2713
2758
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1014,8 +1014,13 @@ interface IConfigRepo {
|
|
|
1014
1014
|
* replaceBackend: old pair is rebuilt against the new target).
|
|
1015
1015
|
*/
|
|
1016
1016
|
replaceAppDataGroupBackend(groupId: string, backendId: string, type: string, options: Record<string, unknown>, description?: string): Promise<AppDataGroup>;
|
|
1017
|
-
/**
|
|
1018
|
-
|
|
1017
|
+
/**
|
|
1018
|
+
* Remove a data-sync group (stops sync, removes descriptor).
|
|
1019
|
+
*
|
|
1020
|
+
* @param appId 目标登记所属 appId;默认本实例的 appId。传其它 appId 时只删
|
|
1021
|
+
* 配置仓里该 appId 的登记与引导副本(写墓碑),不会 dispose 别的实例里的组。
|
|
1022
|
+
*/
|
|
1023
|
+
removeAppDataGroup(id: string, appId?: string): Promise<void>;
|
|
1019
1024
|
/**
|
|
1020
1025
|
* List config-sync backends that can be used as account sources
|
|
1021
1026
|
* for data-sync backends. Returns backends that have `accountFields`
|
|
@@ -1353,6 +1358,12 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1353
1358
|
* unlink() 是否成功来判定。
|
|
1354
1359
|
*/
|
|
1355
1360
|
private readonly tombstoneConfirmations;
|
|
1361
|
+
/**
|
|
1362
|
+
* 上次从副本预取墓碑的时间戳(毫秒)。
|
|
1363
|
+
* 节流用:preSyncHook 每轮都会调用 {@link prefetchReplicaTombstones},
|
|
1364
|
+
* 但远端墓碑不会秒级变化,一分钟一次足够。
|
|
1365
|
+
*/
|
|
1366
|
+
private lastTombstonePrefetchAt;
|
|
1356
1367
|
/** Per-backend sync state (successful pairs + connect failures). */
|
|
1357
1368
|
private backendStates;
|
|
1358
1369
|
/**
|
|
@@ -1442,6 +1453,21 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1442
1453
|
private refreshSyncExclusions;
|
|
1443
1454
|
/** Invalidate the tombstone cache — call after tombstones are modified. */
|
|
1444
1455
|
private invalidateTombstoneCache;
|
|
1456
|
+
/**
|
|
1457
|
+
* 把各副本上的墓碑合并进本地,必须早于本轮的快照比对。
|
|
1458
|
+
*
|
|
1459
|
+
* 为什么需要它:墓碑文件本身也是被同步的文件,一个节点在「拿到墓碑」之前
|
|
1460
|
+
* 就已经跑完了 preSyncHook —— 这时它本地没有该路径的墓碑,同步就会把自己
|
|
1461
|
+
* 那份陈旧副本当成「本地新增」推回远端(BiDirectional + source-wins),
|
|
1462
|
+
* 删除被复活。只靠墓碑文件被同步拉下来,永远慢半拍。
|
|
1463
|
+
*
|
|
1464
|
+
* 所以在每轮同步之前先只读地把副本上的 `.meta/tombstones/` 合并到本地:
|
|
1465
|
+
* 合并完成后排除表立刻生效,随后的 processTombstones() 会把本地陈旧副本
|
|
1466
|
+
* 一并清掉,同步再不会把它推上去。
|
|
1467
|
+
*
|
|
1468
|
+
* @param minIntervalMs 节流间隔(0 = 立即执行)。preSyncHook 传 60s。
|
|
1469
|
+
*/
|
|
1470
|
+
private prefetchReplicaTombstones;
|
|
1445
1471
|
/**
|
|
1446
1472
|
* 写入成功即撤销该路径(及其被删祖先目录)上的墓碑。
|
|
1447
1473
|
*
|
|
@@ -1463,6 +1489,16 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1463
1489
|
* 时刻新就意味着这条墓碑已经被写操作撤销。
|
|
1464
1490
|
*/
|
|
1465
1491
|
private isPathRecreatedAfterTombstone;
|
|
1492
|
+
/**
|
|
1493
|
+
* 目录墓碑专用:目录下是否存在「删除之后写入」的真实数据项。
|
|
1494
|
+
*
|
|
1495
|
+
* 只递归看普通文件,不看目录 inode 的 mtime;`.keep` / `.mtime` / `.version`
|
|
1496
|
+
* 这些后端元数据每轮同步都可能被重写,不能当作重建证据。目录里只剩占位
|
|
1497
|
+
* 文件时返回 false —— 那属于待清理残留,不是用户重建。
|
|
1498
|
+
*/
|
|
1499
|
+
private dirHasEntryNewerThan;
|
|
1500
|
+
/** 后端内部元数据:不作为「删除后重建」的证据。 */
|
|
1501
|
+
private isBackendMetadataPath;
|
|
1466
1502
|
/**
|
|
1467
1503
|
* Before sync: for each tombstone, delete the actual file on all replicas.
|
|
1468
1504
|
* This prevents bi-directional sync from copying the file back.
|
|
@@ -1934,7 +1970,15 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1934
1970
|
* 真实意图,允许推送空副本,不参与自愈。
|
|
1935
1971
|
*/
|
|
1936
1972
|
private healEmptyBootstrapDescriptors;
|
|
1937
|
-
|
|
1973
|
+
/**
|
|
1974
|
+
* 删除一个数据组的登记(`.config/app-data-groups/{appId}/{id}.json` + 该组
|
|
1975
|
+
* 在配置仓的后端描述符)。
|
|
1976
|
+
*
|
|
1977
|
+
* @param appId 目标登记所属的 appId,默认为本实例的 appId。传其它 appId 时
|
|
1978
|
+
* 只删配置仓里那份登记与引导副本,不会(也无法)去 dispose 另一个
|
|
1979
|
+
* appId 实例里的组句柄 —— 调用方应优先走实例注册表,让实例自己刷新。
|
|
1980
|
+
*/
|
|
1981
|
+
removeAppDataGroup(id: string, appId?: string): Promise<void>;
|
|
1938
1982
|
listAccountBackends(): Promise<BackendDescriptor[]>;
|
|
1939
1983
|
private tryParse;
|
|
1940
1984
|
private assertNotDisposed;
|
|
@@ -2708,6 +2752,7 @@ declare class TombstoneStore {
|
|
|
2708
2752
|
* 本来就旧,用副本判定既不可靠又会掩盖真正的删除传播。
|
|
2709
2753
|
*/
|
|
2710
2754
|
private wasRecreatedAfterDeletion;
|
|
2755
|
+
private dirHasEntryNewerThan;
|
|
2711
2756
|
private mtimeOf;
|
|
2712
2757
|
private deleteEverywhere;
|
|
2713
2758
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1789,7 +1789,7 @@ var META_BACKENDS_DIR = `${META_DIR}/backends`;
|
|
|
1789
1789
|
var TOMBSTONES_DIR = `${META_DIR}/tombstones`;
|
|
1790
1790
|
var ALIASES_DIR = `${META_DIR}/aliases`;
|
|
1791
1791
|
var RECREATE_AFTER_DELETE_SKEW_MS = 3e3;
|
|
1792
|
-
var TOMBSTONE_RETENTION_MS = 24 * 60 * 60 * 1e3;
|
|
1792
|
+
var TOMBSTONE_RETENTION_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
1793
1793
|
var TOMBSTONE_PLACEHOLDER_RETENTION_MS = 10 * 60 * 1e3;
|
|
1794
1794
|
function tombstoneRetentionMs(tombstonePath) {
|
|
1795
1795
|
if (tombstonePath.endsWith(".mtime") || tombstonePath === "/.keep" || tombstonePath.endsWith("/.keep")) {
|
|
@@ -2389,10 +2389,40 @@ var TombstoneStore = class {
|
|
|
2389
2389
|
* 本来就旧,用副本判定既不可靠又会掩盖真正的删除传播。
|
|
2390
2390
|
*/
|
|
2391
2391
|
async wasRecreatedAfterDeletion(tombstone) {
|
|
2392
|
+
if (isDirectoryTombstone(tombstone)) {
|
|
2393
|
+
return this.dirHasEntryNewerThan(this.fs, tombstone.path, tombstone.deletedAt);
|
|
2394
|
+
}
|
|
2392
2395
|
const mtimeMs = await this.mtimeOf(this.fs, tombstone.path);
|
|
2393
2396
|
if (mtimeMs === null) return false;
|
|
2394
2397
|
return mtimeMs > tombstone.deletedAt + RECREATE_AFTER_DELETE_SKEW_MS;
|
|
2395
2398
|
}
|
|
2399
|
+
async dirHasEntryNewerThan(fs, dirPath, deletedAt, depth = 0) {
|
|
2400
|
+
let names;
|
|
2401
|
+
try {
|
|
2402
|
+
names = await fs.readdir(dirPath);
|
|
2403
|
+
} catch {
|
|
2404
|
+
return false;
|
|
2405
|
+
}
|
|
2406
|
+
for (const name of names) {
|
|
2407
|
+
const childPath = `${dirPath}/${name}`;
|
|
2408
|
+
if (isHiddenPlaceholderPath(childPath) || childPath.endsWith(".version")) continue;
|
|
2409
|
+
let inode;
|
|
2410
|
+
try {
|
|
2411
|
+
inode = await fs.stat(childPath);
|
|
2412
|
+
} catch {
|
|
2413
|
+
continue;
|
|
2414
|
+
}
|
|
2415
|
+
const isChildDir = typeof inode?.isDirectory === "function" ? inode.isDirectory() : !!(inode && ((inode.mode ?? 0) & 61440) === 16384);
|
|
2416
|
+
if (isChildDir) {
|
|
2417
|
+
if (depth < 4 && await this.dirHasEntryNewerThan(fs, childPath, deletedAt, depth + 1)) {
|
|
2418
|
+
return true;
|
|
2419
|
+
}
|
|
2420
|
+
continue;
|
|
2421
|
+
}
|
|
2422
|
+
if (Number(inode?.mtimeMs ?? 0) > deletedAt + RECREATE_AFTER_DELETE_SKEW_MS) return true;
|
|
2423
|
+
}
|
|
2424
|
+
return false;
|
|
2425
|
+
}
|
|
2396
2426
|
async mtimeOf(fs, filePath) {
|
|
2397
2427
|
try {
|
|
2398
2428
|
const stat = await fs.stat?.(filePath);
|
|
@@ -3256,6 +3286,7 @@ function normalizeGroupName(value) {
|
|
|
3256
3286
|
var CONFIG_ONLY_PREFIXES = [CONFIG_DIR, NODES_DIR, SHARED_DIR];
|
|
3257
3287
|
var LOCAL_IDB_BACKEND_ID = "local-idb";
|
|
3258
3288
|
var TOMBSTONE_RECREATE_SKEW_MS = 3e3;
|
|
3289
|
+
var TOMBSTONE_PREFETCH_INTERVAL_MS = 60 * 1e3;
|
|
3259
3290
|
function tombstoneFileName(filePath) {
|
|
3260
3291
|
return `${encodeMetaPath(filePath)}.json`;
|
|
3261
3292
|
}
|
|
@@ -3401,6 +3432,12 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
3401
3432
|
* unlink() 是否成功来判定。
|
|
3402
3433
|
*/
|
|
3403
3434
|
tombstoneConfirmations = /* @__PURE__ */ new Map();
|
|
3435
|
+
/**
|
|
3436
|
+
* 上次从副本预取墓碑的时间戳(毫秒)。
|
|
3437
|
+
* 节流用:preSyncHook 每轮都会调用 {@link prefetchReplicaTombstones},
|
|
3438
|
+
* 但远端墓碑不会秒级变化,一分钟一次足够。
|
|
3439
|
+
*/
|
|
3440
|
+
lastTombstonePrefetchAt = 0;
|
|
3404
3441
|
/** Per-backend sync state (successful pairs + connect failures). */
|
|
3405
3442
|
backendStates = /* @__PURE__ */ new Map();
|
|
3406
3443
|
/**
|
|
@@ -3615,6 +3652,11 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
3615
3652
|
await this.initialSyncPromise;
|
|
3616
3653
|
this.initialSyncPromise = null;
|
|
3617
3654
|
}
|
|
3655
|
+
try {
|
|
3656
|
+
await this.prefetchReplicaTombstones();
|
|
3657
|
+
} catch (err) {
|
|
3658
|
+
log5.warn("[ConfigRepo] flush tombstone prefetch failed:", err);
|
|
3659
|
+
}
|
|
3618
3660
|
await this.processTombstones();
|
|
3619
3661
|
await this.healEmptyBootstrapDescriptors();
|
|
3620
3662
|
const resultsMap = await this.syncEngine.syncAll();
|
|
@@ -3741,6 +3783,89 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
3741
3783
|
invalidateTombstoneCache() {
|
|
3742
3784
|
this.tombstoneCache = null;
|
|
3743
3785
|
}
|
|
3786
|
+
/**
|
|
3787
|
+
* 把各副本上的墓碑合并进本地,必须早于本轮的快照比对。
|
|
3788
|
+
*
|
|
3789
|
+
* 为什么需要它:墓碑文件本身也是被同步的文件,一个节点在「拿到墓碑」之前
|
|
3790
|
+
* 就已经跑完了 preSyncHook —— 这时它本地没有该路径的墓碑,同步就会把自己
|
|
3791
|
+
* 那份陈旧副本当成「本地新增」推回远端(BiDirectional + source-wins),
|
|
3792
|
+
* 删除被复活。只靠墓碑文件被同步拉下来,永远慢半拍。
|
|
3793
|
+
*
|
|
3794
|
+
* 所以在每轮同步之前先只读地把副本上的 `.meta/tombstones/` 合并到本地:
|
|
3795
|
+
* 合并完成后排除表立刻生效,随后的 processTombstones() 会把本地陈旧副本
|
|
3796
|
+
* 一并清掉,同步再不会把它推上去。
|
|
3797
|
+
*
|
|
3798
|
+
* @param minIntervalMs 节流间隔(0 = 立即执行)。preSyncHook 传 60s。
|
|
3799
|
+
*/
|
|
3800
|
+
async prefetchReplicaTombstones(minIntervalMs = 0) {
|
|
3801
|
+
if (this.disposed || this.replicaBackends.size === 0) return;
|
|
3802
|
+
const now = Date.now();
|
|
3803
|
+
if (minIntervalMs > 0 && now - this.lastTombstonePrefetchAt < minIntervalMs) return;
|
|
3804
|
+
this.lastTombstonePrefetchAt = now;
|
|
3805
|
+
let local;
|
|
3806
|
+
try {
|
|
3807
|
+
local = await this.readTombstones();
|
|
3808
|
+
} catch {
|
|
3809
|
+
return;
|
|
3810
|
+
}
|
|
3811
|
+
const byPath = new Map(local.map((t) => [t.path, t]));
|
|
3812
|
+
let changed = false;
|
|
3813
|
+
for (const [replicaId, replica] of this.replicaBackends) {
|
|
3814
|
+
let remote;
|
|
3815
|
+
try {
|
|
3816
|
+
remote = await readTombstonesFromFs(replica.instance, (message) => log5.warn(`[ConfigRepo] prefetch tombstones (${replicaId}): ${message}`));
|
|
3817
|
+
} catch (err) {
|
|
3818
|
+
log5.warn(`[ConfigRepo] prefetch tombstones from ${replicaId} failed:`, err);
|
|
3819
|
+
continue;
|
|
3820
|
+
}
|
|
3821
|
+
for (const tombstone of remote) {
|
|
3822
|
+
if (!tombstone.path) continue;
|
|
3823
|
+
const tombstonePath = tombstonePathFor(tombstone.path, TOMBSTONES_DIR);
|
|
3824
|
+
const prev = byPath.get(tombstone.path);
|
|
3825
|
+
if (!prev) {
|
|
3826
|
+
byPath.set(tombstone.path, tombstone);
|
|
3827
|
+
try {
|
|
3828
|
+
await this.ensureDir(tombstonePath);
|
|
3829
|
+
await writeFileOverwrite(
|
|
3830
|
+
this.cachedFS,
|
|
3831
|
+
tombstonePath,
|
|
3832
|
+
new TextEncoder().encode(serializeTombstone(tombstone))
|
|
3833
|
+
);
|
|
3834
|
+
changed = true;
|
|
3835
|
+
} catch (err) {
|
|
3836
|
+
log5.warn(`[ConfigRepo] prefetch tombstone write failed for ${tombstone.path}:`, err);
|
|
3837
|
+
}
|
|
3838
|
+
continue;
|
|
3839
|
+
}
|
|
3840
|
+
const merged = {
|
|
3841
|
+
...prev,
|
|
3842
|
+
deletedAt: Math.min(prev.deletedAt, tombstone.deletedAt),
|
|
3843
|
+
confirmedBy: Array.from(/* @__PURE__ */ new Set([...prev.confirmedBy, ...tombstone.confirmedBy]))
|
|
3844
|
+
};
|
|
3845
|
+
if (isDirectoryTombstone(tombstone) && !isDirectoryTombstone(prev)) {
|
|
3846
|
+
merged.type = "directory";
|
|
3847
|
+
}
|
|
3848
|
+
const dirty = merged.deletedAt !== prev.deletedAt || merged.confirmedBy.length !== prev.confirmedBy.length || merged.type === "directory" && prev.type !== "directory";
|
|
3849
|
+
if (!dirty) continue;
|
|
3850
|
+
byPath.set(tombstone.path, merged);
|
|
3851
|
+
try {
|
|
3852
|
+
await writeFileOverwrite(
|
|
3853
|
+
this.cachedFS,
|
|
3854
|
+
tombstonePath,
|
|
3855
|
+
new TextEncoder().encode(serializeTombstone(merged))
|
|
3856
|
+
);
|
|
3857
|
+
changed = true;
|
|
3858
|
+
} catch (err) {
|
|
3859
|
+
log5.warn(`[ConfigRepo] prefetch tombstone merge failed for ${tombstone.path}:`, err);
|
|
3860
|
+
}
|
|
3861
|
+
}
|
|
3862
|
+
}
|
|
3863
|
+
if (changed) {
|
|
3864
|
+
this.invalidateTombstoneCache();
|
|
3865
|
+
await this.readTombstones();
|
|
3866
|
+
log5.log("[ConfigRepo] prefetchReplicaTombstones: merged remote tombstones");
|
|
3867
|
+
}
|
|
3868
|
+
}
|
|
3744
3869
|
/**
|
|
3745
3870
|
* 写入成功即撤销该路径(及其被删祖先目录)上的墓碑。
|
|
3746
3871
|
*
|
|
@@ -3791,7 +3916,12 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
3791
3916
|
for (const [id, replica] of this.replicaBackends) {
|
|
3792
3917
|
surfaces.push({ label: id, fs: replica.instance });
|
|
3793
3918
|
}
|
|
3919
|
+
const isDirTombstone = isDirectoryTombstone(tombstone);
|
|
3794
3920
|
for (const surface of surfaces) {
|
|
3921
|
+
if (isDirTombstone) {
|
|
3922
|
+
if (await this.dirHasEntryNewerThan(surface.fs, tombstone.path, tombstone.deletedAt)) return true;
|
|
3923
|
+
continue;
|
|
3924
|
+
}
|
|
3795
3925
|
let mtimeMs;
|
|
3796
3926
|
try {
|
|
3797
3927
|
const inode = await surface.fs.stat(tombstone.path);
|
|
@@ -3803,6 +3933,44 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
3803
3933
|
}
|
|
3804
3934
|
return false;
|
|
3805
3935
|
}
|
|
3936
|
+
/**
|
|
3937
|
+
* 目录墓碑专用:目录下是否存在「删除之后写入」的真实数据项。
|
|
3938
|
+
*
|
|
3939
|
+
* 只递归看普通文件,不看目录 inode 的 mtime;`.keep` / `.mtime` / `.version`
|
|
3940
|
+
* 这些后端元数据每轮同步都可能被重写,不能当作重建证据。目录里只剩占位
|
|
3941
|
+
* 文件时返回 false —— 那属于待清理残留,不是用户重建。
|
|
3942
|
+
*/
|
|
3943
|
+
async dirHasEntryNewerThan(fs, dirPath, deletedAt, depth = 0) {
|
|
3944
|
+
let names;
|
|
3945
|
+
try {
|
|
3946
|
+
names = await fs.readdir(dirPath);
|
|
3947
|
+
} catch {
|
|
3948
|
+
return false;
|
|
3949
|
+
}
|
|
3950
|
+
for (const name of names) {
|
|
3951
|
+
const childPath = `${dirPath}/${name}`;
|
|
3952
|
+
if (this.isBackendMetadataPath(childPath)) continue;
|
|
3953
|
+
let inode;
|
|
3954
|
+
try {
|
|
3955
|
+
inode = await fs.stat(childPath);
|
|
3956
|
+
} catch {
|
|
3957
|
+
continue;
|
|
3958
|
+
}
|
|
3959
|
+
const childIsDir = typeof inode?.isDirectory === "function" ? inode.isDirectory() : !!(inode && ((inode.mode ?? 0) & 61440) === 16384);
|
|
3960
|
+
if (childIsDir) {
|
|
3961
|
+
if (depth < 4 && await this.dirHasEntryNewerThan(fs, childPath, deletedAt, depth + 1)) {
|
|
3962
|
+
return true;
|
|
3963
|
+
}
|
|
3964
|
+
continue;
|
|
3965
|
+
}
|
|
3966
|
+
if (Number(inode?.mtimeMs ?? 0) > deletedAt + TOMBSTONE_RECREATE_SKEW_MS) return true;
|
|
3967
|
+
}
|
|
3968
|
+
return false;
|
|
3969
|
+
}
|
|
3970
|
+
/** 后端内部元数据:不作为「删除后重建」的证据。 */
|
|
3971
|
+
isBackendMetadataPath(filePath) {
|
|
3972
|
+
return filePath.endsWith("/.keep") || isMtimeSidecar(filePath) || filePath.endsWith(".version");
|
|
3973
|
+
}
|
|
3806
3974
|
/**
|
|
3807
3975
|
* Before sync: for each tombstone, delete the actual file on all replicas.
|
|
3808
3976
|
* This prevents bi-directional sync from copying the file back.
|
|
@@ -4125,6 +4293,12 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
4125
4293
|
async initialSyncAndDedup() {
|
|
4126
4294
|
this.syncEngine.unwatchAll();
|
|
4127
4295
|
await this.healEmptyBootstrapDescriptors();
|
|
4296
|
+
try {
|
|
4297
|
+
await this.prefetchReplicaTombstones();
|
|
4298
|
+
await this.processTombstones();
|
|
4299
|
+
} catch (err) {
|
|
4300
|
+
log5.warn("[ConfigRepo] initial tombstone prefetch failed:", err);
|
|
4301
|
+
}
|
|
4128
4302
|
await this.syncEngine.syncAll();
|
|
4129
4303
|
await this.readAllBackendDescriptors();
|
|
4130
4304
|
await this.processTombstones();
|
|
@@ -4618,6 +4792,8 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
4618
4792
|
filter: this.syncFilter,
|
|
4619
4793
|
preSyncHook: async () => {
|
|
4620
4794
|
try {
|
|
4795
|
+
this.invalidateTombstoneCache();
|
|
4796
|
+
await this.prefetchReplicaTombstones(TOMBSTONE_PREFETCH_INTERVAL_MS);
|
|
4621
4797
|
this.invalidateTombstoneCache();
|
|
4622
4798
|
await this.processTombstones();
|
|
4623
4799
|
} catch (err) {
|
|
@@ -5163,6 +5339,8 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
5163
5339
|
filter: this.syncFilter,
|
|
5164
5340
|
preSyncHook: async () => {
|
|
5165
5341
|
try {
|
|
5342
|
+
this.invalidateTombstoneCache();
|
|
5343
|
+
await this.prefetchReplicaTombstones(TOMBSTONE_PREFETCH_INTERVAL_MS);
|
|
5166
5344
|
this.invalidateTombstoneCache();
|
|
5167
5345
|
await this.processTombstones();
|
|
5168
5346
|
} catch (err) {
|
|
@@ -5738,12 +5916,12 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
5738
5916
|
// IConfigRepo — App Data Groups (data-sync groups)
|
|
5739
5917
|
// -----------------------------------------------------------------------
|
|
5740
5918
|
/** Path for a single app data group descriptor: .config/app-data-groups/{appId}/{id}.json */
|
|
5741
|
-
appDataGroupFilePath(id) {
|
|
5742
|
-
return `${APP_DATA_GROUPS_DIR}/${
|
|
5919
|
+
appDataGroupFilePath(id, appId = this.appId) {
|
|
5920
|
+
return `${APP_DATA_GROUPS_DIR}/${appId}/${id}.json`;
|
|
5743
5921
|
}
|
|
5744
5922
|
/** Legacy descriptor path: .meta/app-data-groups/{appId}/{id}.json */
|
|
5745
|
-
legacyAppDataGroupFilePath(id) {
|
|
5746
|
-
return `${LEGACY_APP_DATA_GROUPS_DIR}/${
|
|
5923
|
+
legacyAppDataGroupFilePath(id, appId = this.appId) {
|
|
5924
|
+
return `${LEGACY_APP_DATA_GROUPS_DIR}/${appId}/${id}.json`;
|
|
5747
5925
|
}
|
|
5748
5926
|
/**
|
|
5749
5927
|
* Write a data-group descriptor to `.config/app-data-groups/` (registration
|
|
@@ -5775,15 +5953,15 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
5775
5953
|
* 归属靠文件内的 `groupId` 字段区分:同一 app 下不同组即使后端 id 相同,
|
|
5776
5954
|
* 也各用各自的文件,不会在同步时互相覆盖(同一组不同端点同理)。
|
|
5777
5955
|
*/
|
|
5778
|
-
appDataBackendsDirPath() {
|
|
5779
|
-
return `${APP_DATA_GROUPS_DIR}/${
|
|
5956
|
+
appDataBackendsDirPath(appId = this.appId) {
|
|
5957
|
+
return `${APP_DATA_GROUPS_DIR}/${appId}/backends`;
|
|
5780
5958
|
}
|
|
5781
|
-
appDataBackendFilePath(backendId) {
|
|
5782
|
-
return `${this.appDataBackendsDirPath()}/${backendId}.json`;
|
|
5959
|
+
appDataBackendFilePath(backendId, appId = this.appId) {
|
|
5960
|
+
return `${this.appDataBackendsDirPath(appId)}/${backendId}.json`;
|
|
5783
5961
|
}
|
|
5784
5962
|
/** 读本 app 目录下所有后端文件(忽略损坏/非本格式的文件)。 */
|
|
5785
|
-
async readAppDataBackendFiles() {
|
|
5786
|
-
const dir = this.appDataBackendsDirPath();
|
|
5963
|
+
async readAppDataBackendFiles(appId = this.appId) {
|
|
5964
|
+
const dir = this.appDataBackendsDirPath(appId);
|
|
5787
5965
|
let entries;
|
|
5788
5966
|
try {
|
|
5789
5967
|
entries = await this.cachedFS.readdir(dir);
|
|
@@ -5805,9 +5983,9 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
5805
5983
|
return out;
|
|
5806
5984
|
}
|
|
5807
5985
|
/** 读某个数据组声明的全部后端文件(按 id 去重,先到先得)。 */
|
|
5808
|
-
async readAppDataBackendFilesForGroup(groupId) {
|
|
5986
|
+
async readAppDataBackendFilesForGroup(groupId, appId = this.appId) {
|
|
5809
5987
|
const byId = /* @__PURE__ */ new Map();
|
|
5810
|
-
for (const file of await this.readAppDataBackendFiles()) {
|
|
5988
|
+
for (const file of await this.readAppDataBackendFiles(appId)) {
|
|
5811
5989
|
if (file.groupId !== groupId) continue;
|
|
5812
5990
|
if (byId.has(file.id)) continue;
|
|
5813
5991
|
const { groupId: _groupId, ...desc } = file;
|
|
@@ -6426,15 +6604,25 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
6426
6604
|
}
|
|
6427
6605
|
}
|
|
6428
6606
|
}
|
|
6429
|
-
|
|
6607
|
+
/**
|
|
6608
|
+
* 删除一个数据组的登记(`.config/app-data-groups/{appId}/{id}.json` + 该组
|
|
6609
|
+
* 在配置仓的后端描述符)。
|
|
6610
|
+
*
|
|
6611
|
+
* @param appId 目标登记所属的 appId,默认为本实例的 appId。传其它 appId 时
|
|
6612
|
+
* 只删配置仓里那份登记与引导副本,不会(也无法)去 dispose 另一个
|
|
6613
|
+
* appId 实例里的组句柄 —— 调用方应优先走实例注册表,让实例自己刷新。
|
|
6614
|
+
*/
|
|
6615
|
+
async removeAppDataGroup(id, appId = this.appId) {
|
|
6430
6616
|
this.assertNotDisposed();
|
|
6431
|
-
|
|
6432
|
-
|
|
6433
|
-
|
|
6434
|
-
|
|
6617
|
+
if (appId === this.appId) {
|
|
6618
|
+
const group = this.appDataGroups.get(id);
|
|
6619
|
+
if (group) {
|
|
6620
|
+
await group.dispose();
|
|
6621
|
+
this.appDataGroups.delete(id);
|
|
6622
|
+
}
|
|
6435
6623
|
}
|
|
6436
|
-
for (const backend of await this.readAppDataBackendFilesForGroup(id)) {
|
|
6437
|
-
const backendPath = this.appDataBackendFilePath(backend.id);
|
|
6624
|
+
for (const backend of await this.readAppDataBackendFilesForGroup(id, appId)) {
|
|
6625
|
+
const backendPath = this.appDataBackendFilePath(backend.id, appId);
|
|
6438
6626
|
try {
|
|
6439
6627
|
await this.deleteFile(backendPath);
|
|
6440
6628
|
} catch (err) {
|
|
@@ -6446,7 +6634,7 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
6446
6634
|
await this.unlinkVersionSidecar(this.cachedFS, backendPath);
|
|
6447
6635
|
}
|
|
6448
6636
|
}
|
|
6449
|
-
for (const descPath of [this.appDataGroupFilePath(id), this.legacyAppDataGroupFilePath(id)]) {
|
|
6637
|
+
for (const descPath of [this.appDataGroupFilePath(id, appId), this.legacyAppDataGroupFilePath(id, appId)]) {
|
|
6450
6638
|
try {
|
|
6451
6639
|
await this.deleteFile(descPath);
|
|
6452
6640
|
} catch (err) {
|
|
@@ -6458,7 +6646,7 @@ var ConfigRepo = class _ConfigRepo {
|
|
|
6458
6646
|
await this.unlinkVersionSidecar(this.cachedFS, descPath);
|
|
6459
6647
|
}
|
|
6460
6648
|
}
|
|
6461
|
-
log5.log(`[ConfigRepo] removeAppDataGroup: "${id}" removed`);
|
|
6649
|
+
log5.log(`[ConfigRepo] removeAppDataGroup: "${appId}/${id}" removed`);
|
|
6462
6650
|
}
|
|
6463
6651
|
async listAccountBackends() {
|
|
6464
6652
|
this.assertNotDisposed();
|
|
@@ -6520,6 +6708,8 @@ var AppDataGroupImpl = class _AppDataGroupImpl {
|
|
|
6520
6708
|
static PERMANENT_SYNC_EXCLUSIONS = [...CONFIG_ONLY_PREFIXES, GROUP_DESCRIPTOR_FILE3];
|
|
6521
6709
|
tombstoneExcludePrefixes = [..._AppDataGroupImpl.PERMANENT_SYNC_EXCLUSIONS];
|
|
6522
6710
|
syncFilter = { excludePrefixes: this.tombstoneExcludePrefixes };
|
|
6711
|
+
/** 上次从数据后端预取墓碑的时间戳(节流用,见 prefetchRemoteTombstones)。 */
|
|
6712
|
+
lastTombstonePrefetchAt = 0;
|
|
6523
6713
|
constructor(groupId, appId, backends, pollIntervalMs, parent, descriptor = {}) {
|
|
6524
6714
|
this.groupId = groupId;
|
|
6525
6715
|
this.appId = appId;
|
|
@@ -7373,12 +7563,70 @@ var AppDataGroupImpl = class _AppDataGroupImpl {
|
|
|
7373
7563
|
/** Delete tombstoned files on local + every connected backend before/after sync. */
|
|
7374
7564
|
async runTombstonePass(stage) {
|
|
7375
7565
|
try {
|
|
7566
|
+
await this.prefetchRemoteTombstones(TOMBSTONE_PREFETCH_INTERVAL_MS);
|
|
7376
7567
|
await this.tombstones.process(this.tombstoneTargets());
|
|
7377
7568
|
this.refreshSyncExclusions(await this.tombstones.list());
|
|
7378
7569
|
} catch (err) {
|
|
7379
7570
|
log5.warn(`[AppDataGroup:${this.groupId}] ${stage} tombstone pass failed:`, err);
|
|
7380
7571
|
}
|
|
7381
7572
|
}
|
|
7573
|
+
/**
|
|
7574
|
+
* 把各数据后端上的墓碑合并进本地 `.meta/tombstones/`。
|
|
7575
|
+
*
|
|
7576
|
+
* 与配置组的 `ConfigRepo.prefetchReplicaTombstones` 同一目的、同一合并规则:
|
|
7577
|
+
* 远端删除意图优先于本地副本,先合并再让 `process()` 清掉陈旧数据,避免
|
|
7578
|
+
* 「本地有、别处已删」的文件被这一轮同步写回后端。
|
|
7579
|
+
*/
|
|
7580
|
+
async prefetchRemoteTombstones(minIntervalMs = 0) {
|
|
7581
|
+
if (this.disposed || this.dataBackends.size === 0) return;
|
|
7582
|
+
const now = Date.now();
|
|
7583
|
+
if (minIntervalMs > 0 && now - this.lastTombstonePrefetchAt < minIntervalMs) return;
|
|
7584
|
+
this.lastTombstonePrefetchAt = now;
|
|
7585
|
+
let local;
|
|
7586
|
+
try {
|
|
7587
|
+
local = await this.tombstones.list();
|
|
7588
|
+
} catch {
|
|
7589
|
+
return;
|
|
7590
|
+
}
|
|
7591
|
+
const byPath = new Map(local.map((t) => [t.path, t]));
|
|
7592
|
+
let changed = false;
|
|
7593
|
+
for (const [backendId, backend] of this.dataBackends) {
|
|
7594
|
+
let remote;
|
|
7595
|
+
try {
|
|
7596
|
+
remote = await readTombstonesFromFs(backend.instance, (message) => log5.warn(`[AppDataGroup:${this.groupId}] prefetch tombstones (${backendId}): ${message}`));
|
|
7597
|
+
} catch (err) {
|
|
7598
|
+
log5.warn(`[AppDataGroup:${this.groupId}] prefetch tombstones from ${backendId} failed:`, err);
|
|
7599
|
+
continue;
|
|
7600
|
+
}
|
|
7601
|
+
for (const tombstone of remote) {
|
|
7602
|
+
if (!tombstone.path) continue;
|
|
7603
|
+
const prev = byPath.get(tombstone.path);
|
|
7604
|
+
if (!prev) {
|
|
7605
|
+
byPath.set(tombstone.path, tombstone);
|
|
7606
|
+
await this.tombstones.write(tombstone);
|
|
7607
|
+
changed = true;
|
|
7608
|
+
continue;
|
|
7609
|
+
}
|
|
7610
|
+
const merged = {
|
|
7611
|
+
...prev,
|
|
7612
|
+
deletedAt: Math.min(prev.deletedAt, tombstone.deletedAt),
|
|
7613
|
+
confirmedBy: Array.from(/* @__PURE__ */ new Set([...prev.confirmedBy, ...tombstone.confirmedBy]))
|
|
7614
|
+
};
|
|
7615
|
+
if (isDirectoryTombstone(tombstone) && !isDirectoryTombstone(prev)) {
|
|
7616
|
+
merged.type = "directory";
|
|
7617
|
+
}
|
|
7618
|
+
const dirty = merged.deletedAt !== prev.deletedAt || merged.confirmedBy.length !== prev.confirmedBy.length || merged.type === "directory" && prev.type !== "directory";
|
|
7619
|
+
if (!dirty) continue;
|
|
7620
|
+
byPath.set(tombstone.path, merged);
|
|
7621
|
+
await this.tombstones.write(merged);
|
|
7622
|
+
changed = true;
|
|
7623
|
+
}
|
|
7624
|
+
}
|
|
7625
|
+
if (changed) {
|
|
7626
|
+
this.refreshSyncExclusions(await this.tombstones.list());
|
|
7627
|
+
log5.log(`[AppDataGroup:${this.groupId}] prefetchRemoteTombstones: merged remote tombstones`);
|
|
7628
|
+
}
|
|
7629
|
+
}
|
|
7382
7630
|
/**
|
|
7383
7631
|
* 把当前墓碑路径写进同步排除表(原地更新共享数组)。
|
|
7384
7632
|
*
|