zen-fs-config 0.7.4 → 0.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DESIGN.md +20 -0
- package/README.md +16 -0
- package/README.zh-CN.md +16 -0
- package/dist/{backend-registry-RBKWNJZI.mjs → backend-registry-47NQOGMU.mjs} +1 -1
- package/dist/{chunk-S2S3XBK2.mjs → chunk-H2F4TFQD.mjs} +8 -1
- package/dist/index.d.mts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +336 -140
- package/dist/index.mjs +328 -139
- package/dist/zen-fs-config.js +334 -139
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -117,7 +117,14 @@ async function wrapZenFSFileSystem(config) {
|
|
|
117
117
|
await isolatedFS.mkdir(dir, { uid: 0, gid: 0, mode: 493 });
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
|
-
|
|
120
|
+
let currentSize = 0;
|
|
121
|
+
if (await isolatedFS.exists(path)) {
|
|
122
|
+
currentSize = (await isolatedFS.stat(path)).size;
|
|
123
|
+
} else {
|
|
124
|
+
await isolatedFS.createFile(path, { uid: 0, gid: 0, mode: 420 });
|
|
125
|
+
}
|
|
126
|
+
if (currentSize > 0) {
|
|
127
|
+
await isolatedFS.unlink(path);
|
|
121
128
|
await isolatedFS.createFile(path, { uid: 0, gid: 0, mode: 420 });
|
|
122
129
|
}
|
|
123
130
|
await isolatedFS.write(path, bytes, 0);
|
|
@@ -519,7 +526,7 @@ __export(index_exports, {
|
|
|
519
526
|
ConflictStore: () => ConflictStore,
|
|
520
527
|
DATA_GROUP_DEFAULT_FEATURES: () => DATA_GROUP_DEFAULT_FEATURES,
|
|
521
528
|
FolderStore: () => FolderStore,
|
|
522
|
-
GROUP_DESCRIPTOR_FILE: () =>
|
|
529
|
+
GROUP_DESCRIPTOR_FILE: () => GROUP_DESCRIPTOR_FILE2,
|
|
523
530
|
LEGACY_CONFLICTS_DIR: () => LEGACY_CONFLICTS_DIR,
|
|
524
531
|
LEGACY_TOMBSTONES_DIR: () => LEGACY_TOMBSTONES_DIR,
|
|
525
532
|
LOCAL_IDB_BACKEND_ID: () => LOCAL_IDB_BACKEND_ID,
|
|
@@ -770,6 +777,92 @@ function createChrootFS(inner, root, options = {}) {
|
|
|
770
777
|
async function ensureParentDir(absolutePath) {
|
|
771
778
|
}
|
|
772
779
|
|
|
780
|
+
// src/group-type-guard.ts
|
|
781
|
+
var import_localstorage_logger = require("@richard432/localstorage-logger");
|
|
782
|
+
var log = (0, import_localstorage_logger.createLogger)("zen-fs-config:group-type-guard");
|
|
783
|
+
var GROUP_TYPE_FILES = /* @__PURE__ */ new Set(["/.config/group-type", "/.meta/group-type"]);
|
|
784
|
+
var GROUP_DESCRIPTOR_FILE = "/.meta/group.json";
|
|
785
|
+
var APP_DATA_GROUP_DESCRIPTOR_RE = /^\/\.config\/app-data-groups\/[^/]+\/[^/]+\.json$/;
|
|
786
|
+
function decode(data) {
|
|
787
|
+
if (typeof data === "string") return data;
|
|
788
|
+
if (data instanceof ArrayBuffer) return new TextDecoder().decode(new Uint8Array(data));
|
|
789
|
+
return new TextDecoder().decode(data);
|
|
790
|
+
}
|
|
791
|
+
function safeParse(text) {
|
|
792
|
+
try {
|
|
793
|
+
return JSON.parse(text);
|
|
794
|
+
} catch {
|
|
795
|
+
return null;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
function readGroupIdentity(path, data) {
|
|
799
|
+
const normalized = path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path;
|
|
800
|
+
const isGroupType = GROUP_TYPE_FILES.has(normalized);
|
|
801
|
+
const isGroupDescriptor = normalized === GROUP_DESCRIPTOR_FILE;
|
|
802
|
+
const isAppDataGroup = APP_DATA_GROUP_DESCRIPTOR_RE.test(normalized);
|
|
803
|
+
if (!isGroupType && !isGroupDescriptor && !isAppDataGroup) return null;
|
|
804
|
+
const text = decode(data);
|
|
805
|
+
if (isGroupType) {
|
|
806
|
+
const value = text.trim();
|
|
807
|
+
return value || null;
|
|
808
|
+
}
|
|
809
|
+
const parsed = safeParse(text);
|
|
810
|
+
if (isGroupDescriptor) {
|
|
811
|
+
return typeof parsed?.kind === "string" ? `kind:${parsed.kind}` : null;
|
|
812
|
+
}
|
|
813
|
+
return typeof parsed?.groupType === "string" ? `groupType:${parsed.groupType}` : null;
|
|
814
|
+
}
|
|
815
|
+
async function readExistingIdentity(fs, path) {
|
|
816
|
+
try {
|
|
817
|
+
const raw = await fs.readFile(path, "utf-8");
|
|
818
|
+
return readGroupIdentity(path, raw);
|
|
819
|
+
} catch {
|
|
820
|
+
return null;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
function wrapGroupTypeGuard(fs, side, onBlock) {
|
|
824
|
+
const reported = /* @__PURE__ */ new Set();
|
|
825
|
+
const guarded = Object.create(fs);
|
|
826
|
+
const lastBlock = { current: null };
|
|
827
|
+
guarded.getLastBlockedGroupType = () => lastBlock.current;
|
|
828
|
+
const allow = async (path, data) => {
|
|
829
|
+
const incoming = readGroupIdentity(path, data);
|
|
830
|
+
if (incoming === null) return true;
|
|
831
|
+
const existing = await readExistingIdentity(fs, path);
|
|
832
|
+
if (existing === null || existing === incoming) return true;
|
|
833
|
+
const signature = `${side}|${path}|${existing}->${incoming}`;
|
|
834
|
+
const block = { side, path, existing, incoming };
|
|
835
|
+
lastBlock.current = block;
|
|
836
|
+
if (!reported.has(signature)) {
|
|
837
|
+
reported.add(signature);
|
|
838
|
+
log.warn(
|
|
839
|
+
`[GroupType] blocked group-type change on ${side}: ${path} would change "${existing}" \u2192 "${incoming}" \u2014 identity is write-once, the incoming value is dropped (fix the group wiring instead of syncing it)`
|
|
840
|
+
);
|
|
841
|
+
}
|
|
842
|
+
try {
|
|
843
|
+
onBlock?.(block);
|
|
844
|
+
} catch {
|
|
845
|
+
}
|
|
846
|
+
return false;
|
|
847
|
+
};
|
|
848
|
+
guarded.writeFile = async (path, data) => {
|
|
849
|
+
if (!await allow(path, data)) return;
|
|
850
|
+
return fs.writeFile(path, data);
|
|
851
|
+
};
|
|
852
|
+
if (typeof fs.writeFileWithMtime === "function") {
|
|
853
|
+
guarded.writeFileWithMtime = async (path, data, mtime) => {
|
|
854
|
+
if (!await allow(path, data)) return;
|
|
855
|
+
return fs.writeFileWithMtime(path, data, mtime);
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
guarded.backendName = sideLabel(fs.backendName, side);
|
|
859
|
+
return guarded;
|
|
860
|
+
}
|
|
861
|
+
function sideLabel(name, side) {
|
|
862
|
+
if (!name) return side;
|
|
863
|
+
return name.includes(side) ? name : `${name}\xB7${side}`;
|
|
864
|
+
}
|
|
865
|
+
|
|
773
866
|
// src/adapters.ts
|
|
774
867
|
function toUint8Array(data) {
|
|
775
868
|
if (data instanceof Uint8Array) return data;
|
|
@@ -843,7 +936,7 @@ function backendToSyncableFS(backend, name) {
|
|
|
843
936
|
} else {
|
|
844
937
|
syncable.backendName = backend.constructor.name || "Backend";
|
|
845
938
|
}
|
|
846
|
-
return syncable;
|
|
939
|
+
return wrapGroupTypeGuard(syncable, syncable.backendName || "backend");
|
|
847
940
|
}
|
|
848
941
|
|
|
849
942
|
// src/config-repo.ts
|
|
@@ -854,15 +947,15 @@ init_backend_registry();
|
|
|
854
947
|
var import_core = require("@zenfs/core");
|
|
855
948
|
|
|
856
949
|
// src/logger.ts
|
|
857
|
-
var
|
|
950
|
+
var import_localstorage_logger2 = require("@richard432/localstorage-logger");
|
|
858
951
|
var MODULE_PREFIX = "zen-fs-config";
|
|
859
|
-
function
|
|
860
|
-
const logger = (0,
|
|
952
|
+
function createLogger2(prefix) {
|
|
953
|
+
const logger = (0, import_localstorage_logger2.createLogger)(`${MODULE_PREFIX}:${prefix}`);
|
|
861
954
|
return (...args) => logger.log(...args);
|
|
862
955
|
}
|
|
863
956
|
|
|
864
957
|
// src/folder-backend.ts
|
|
865
|
-
var
|
|
958
|
+
var log2 = createLogger2("folder-backend");
|
|
866
959
|
var FolderStore = class {
|
|
867
960
|
name = "folder";
|
|
868
961
|
label;
|
|
@@ -949,7 +1042,7 @@ function registerFolderBackend() {
|
|
|
949
1042
|
defaultOptions: { path: "" }
|
|
950
1043
|
}
|
|
951
1044
|
);
|
|
952
|
-
|
|
1045
|
+
log2("Folder backend registered");
|
|
953
1046
|
}
|
|
954
1047
|
registerFolderBackend();
|
|
955
1048
|
function isBrowserEnv() {
|
|
@@ -1102,7 +1195,7 @@ function deleteDataGroupLocalStore(appId, groupId, options = {}) {
|
|
|
1102
1195
|
}
|
|
1103
1196
|
|
|
1104
1197
|
// src/config-repo.ts
|
|
1105
|
-
var
|
|
1198
|
+
var import_localstorage_logger4 = require("@richard432/localstorage-logger");
|
|
1106
1199
|
|
|
1107
1200
|
// src/version.ts
|
|
1108
1201
|
function versionPathFor(configFilePath) {
|
|
@@ -1589,7 +1682,7 @@ async function migrateVersionSidecars(fs, options = {}) {
|
|
|
1589
1682
|
}
|
|
1590
1683
|
|
|
1591
1684
|
// src/group-meta.ts
|
|
1592
|
-
var
|
|
1685
|
+
var import_localstorage_logger3 = require("@richard432/localstorage-logger");
|
|
1593
1686
|
|
|
1594
1687
|
// src/canonical-json.ts
|
|
1595
1688
|
var FEATURE_ORDER = ["tombstones", "versions", "conflicts", "configHost", "appData"];
|
|
@@ -1664,11 +1757,25 @@ function mergeDescriptorCreatedAt(descriptor, existing) {
|
|
|
1664
1757
|
}
|
|
1665
1758
|
|
|
1666
1759
|
// src/group-meta.ts
|
|
1667
|
-
var
|
|
1760
|
+
var log3 = (0, import_localstorage_logger3.createLogger)("zen-fs-config:tombstones");
|
|
1668
1761
|
var META_DIR = "/.meta";
|
|
1669
|
-
var
|
|
1762
|
+
var GROUP_DESCRIPTOR_FILE2 = `${META_DIR}/group.json`;
|
|
1670
1763
|
var META_BACKENDS_DIR = `${META_DIR}/backends`;
|
|
1671
1764
|
var TOMBSTONES_DIR = `${META_DIR}/tombstones`;
|
|
1765
|
+
var RECREATE_AFTER_DELETE_SKEW_MS = 3e3;
|
|
1766
|
+
var TOMBSTONE_RETENTION_MS = 24 * 60 * 60 * 1e3;
|
|
1767
|
+
var TOMBSTONE_PLACEHOLDER_RETENTION_MS = 10 * 60 * 1e3;
|
|
1768
|
+
function tombstoneRetentionMs(tombstonePath) {
|
|
1769
|
+
if (tombstonePath.endsWith(".mtime") || tombstonePath === "/.keep" || tombstonePath.endsWith("/.keep")) {
|
|
1770
|
+
return TOMBSTONE_PLACEHOLDER_RETENTION_MS;
|
|
1771
|
+
}
|
|
1772
|
+
return TOMBSTONE_RETENTION_MS;
|
|
1773
|
+
}
|
|
1774
|
+
function isTombstoneRetentionElapsed(tombstone, now = Date.now()) {
|
|
1775
|
+
const deletedAt = Number(tombstone.deletedAt);
|
|
1776
|
+
if (!Number.isFinite(deletedAt)) return true;
|
|
1777
|
+
return now - deletedAt >= tombstoneRetentionMs(tombstone.path);
|
|
1778
|
+
}
|
|
1672
1779
|
var LEGACY_TOMBSTONES_DIR = `${META_DIR}/.deleted`;
|
|
1673
1780
|
var VERSIONS_DIR = `${META_DIR}/versions`;
|
|
1674
1781
|
var CONFLICTS_DIR = `${META_DIR}/conflicts`;
|
|
@@ -1872,16 +1979,16 @@ async function readTombstonesFromFs(fs, warn) {
|
|
|
1872
1979
|
return Array.from(byPath.values());
|
|
1873
1980
|
}
|
|
1874
1981
|
async function readGroupDescriptor(fs) {
|
|
1875
|
-
const parsed = await readJson(fs,
|
|
1982
|
+
const parsed = await readJson(fs, GROUP_DESCRIPTOR_FILE2);
|
|
1876
1983
|
if (!parsed) return null;
|
|
1877
1984
|
const { updatedAt: _ignored, ...rest } = parsed;
|
|
1878
1985
|
return rest;
|
|
1879
1986
|
}
|
|
1880
1987
|
async function writeGroupDescriptor(fs, descriptor) {
|
|
1881
1988
|
const bytes = serializeGroupDescriptor(descriptor);
|
|
1882
|
-
if (await readText(fs,
|
|
1883
|
-
await ensureParentDir2(fs,
|
|
1884
|
-
await fs.writeFile(
|
|
1989
|
+
if (await readText(fs, GROUP_DESCRIPTOR_FILE2) === bytes) return;
|
|
1990
|
+
await ensureParentDir2(fs, GROUP_DESCRIPTOR_FILE2);
|
|
1991
|
+
await fs.writeFile(GROUP_DESCRIPTOR_FILE2, new TextEncoder().encode(bytes));
|
|
1885
1992
|
}
|
|
1886
1993
|
function assertValidBackendId(id) {
|
|
1887
1994
|
if (typeof id !== "string" || id.trim() === "") {
|
|
@@ -1948,7 +2055,7 @@ var TombstoneStore = class {
|
|
|
1948
2055
|
}
|
|
1949
2056
|
/** All tombstones. */
|
|
1950
2057
|
async list() {
|
|
1951
|
-
return readTombstonesFromFs(this.fs, (message) =>
|
|
2058
|
+
return readTombstonesFromFs(this.fs, (message) => log3.warn(message));
|
|
1952
2059
|
}
|
|
1953
2060
|
/** Write a tombstone. */
|
|
1954
2061
|
async write(tombstone) {
|
|
@@ -1976,15 +2083,52 @@ var TombstoneStore = class {
|
|
|
1976
2083
|
/**
|
|
1977
2084
|
* Delete every tombstoned file (and its version record) on the local store
|
|
1978
2085
|
* and on each target backend, so bi-directional sync cannot resurrect it.
|
|
2086
|
+
*
|
|
2087
|
+
* 墓碑只对「删除之后没有再写入」的路径有效。同步类工具会不断重建某些文件
|
|
2088
|
+
* (最典型的是 `.sync.lock`:每轮删除 → 重建),若墓碑永久生效,路径会被每一
|
|
2089
|
+
* 轮 preSync 删掉,表现为「刚写完 1 秒就消失、锁永远拿不到」。因此这里先判定
|
|
2090
|
+
* 目标路径是否在 `deletedAt` 之后被重新写入:是则墓碑已被用户写操作撤销,保留
|
|
2091
|
+
* 文件并删除墓碑;否则按删除传播处理。
|
|
1979
2092
|
*/
|
|
1980
2093
|
async process(targets = []) {
|
|
1981
2094
|
const tombstones = await this.list();
|
|
2095
|
+
const expired = [];
|
|
1982
2096
|
for (const tombstone of tombstones) {
|
|
2097
|
+
if (await this.wasRecreatedAfterDeletion(tombstone)) {
|
|
2098
|
+
expired.push(tombstone);
|
|
2099
|
+
continue;
|
|
2100
|
+
}
|
|
1983
2101
|
await this.deleteEverywhere(this.fs, tombstone.path);
|
|
1984
2102
|
for (const target of targets) {
|
|
1985
2103
|
await this.deleteEverywhere(target.fs, tombstone.path);
|
|
1986
2104
|
}
|
|
1987
2105
|
}
|
|
2106
|
+
for (const tombstone of expired) {
|
|
2107
|
+
await this.remove(tombstone);
|
|
2108
|
+
log3.log(
|
|
2109
|
+
`[TombstoneStore] expired tombstone for ${tombstone.path} \u2014 recreated after deletion`
|
|
2110
|
+
);
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
/**
|
|
2114
|
+
* 本地存储上该路径的 mtime 是否晚于墓碑的删除时刻。
|
|
2115
|
+
*
|
|
2116
|
+
* 只看本地主存储:副本上的同路径文件可能只是「删除之前的旧副本」,它的 mtime
|
|
2117
|
+
* 本来就旧,用副本判定既不可靠又会掩盖真正的删除传播。
|
|
2118
|
+
*/
|
|
2119
|
+
async wasRecreatedAfterDeletion(tombstone) {
|
|
2120
|
+
const mtimeMs = await this.mtimeOf(this.fs, tombstone.path);
|
|
2121
|
+
if (mtimeMs === null) return false;
|
|
2122
|
+
return mtimeMs > tombstone.deletedAt + RECREATE_AFTER_DELETE_SKEW_MS;
|
|
2123
|
+
}
|
|
2124
|
+
async mtimeOf(fs, filePath) {
|
|
2125
|
+
try {
|
|
2126
|
+
const stat = await fs.stat?.(filePath);
|
|
2127
|
+
const mtimeMs = Number(stat?.mtimeMs);
|
|
2128
|
+
return Number.isFinite(mtimeMs) ? mtimeMs : null;
|
|
2129
|
+
} catch {
|
|
2130
|
+
return null;
|
|
2131
|
+
}
|
|
1988
2132
|
}
|
|
1989
2133
|
async deleteEverywhere(fs, filePath) {
|
|
1990
2134
|
if (!isMtimeSidecarPath(filePath) && !await existsSafe(fs, filePath)) return;
|
|
@@ -2018,6 +2162,7 @@ var TombstoneStore = class {
|
|
|
2018
2162
|
async gc(allBackendIds, targets = []) {
|
|
2019
2163
|
const tombstones = await this.list();
|
|
2020
2164
|
for (const tombstone of tombstones) {
|
|
2165
|
+
if (!isTombstoneRetentionElapsed(tombstone)) continue;
|
|
2021
2166
|
const allConfirmed = allBackendIds.every((id) => tombstone.confirmedBy.includes(id));
|
|
2022
2167
|
if (!allConfirmed) continue;
|
|
2023
2168
|
const p = this.pathFor(tombstone.path);
|
|
@@ -2169,8 +2314,8 @@ var ConflictStore = class {
|
|
|
2169
2314
|
};
|
|
2170
2315
|
|
|
2171
2316
|
// src/config-repo.ts
|
|
2172
|
-
var
|
|
2173
|
-
var
|
|
2317
|
+
var log4 = (0, import_localstorage_logger4.createLogger)("zen-fs-config:config-repo");
|
|
2318
|
+
var GROUP_DESCRIPTOR_FILE3 = `${META_DIR}/group.json`;
|
|
2174
2319
|
var GROUP_TYPE_FILE = `${CONFIG_DIR}/group-type`;
|
|
2175
2320
|
var LEGACY_GROUP_TYPE_FILE = `${META_DIR}/group-type`;
|
|
2176
2321
|
var BACKENDS_FILE = `${META_DIR}/backends.json`;
|
|
@@ -2304,6 +2449,7 @@ function normalizeGroupName(value) {
|
|
|
2304
2449
|
}
|
|
2305
2450
|
var CONFIG_ONLY_PREFIXES = [CONFIG_DIR, NODES_DIR, SHARED_DIR];
|
|
2306
2451
|
var LOCAL_IDB_BACKEND_ID = "local-idb";
|
|
2452
|
+
var TOMBSTONE_RECREATE_SKEW_MS = 3e3;
|
|
2307
2453
|
function tombstoneFileName(filePath) {
|
|
2308
2454
|
return `${encodeMetaPath(filePath)}.json`;
|
|
2309
2455
|
}
|
|
@@ -2483,7 +2629,7 @@ var ConfigRepo = class {
|
|
|
2483
2629
|
const bytes = this.serializer.serialize(data, fullPath);
|
|
2484
2630
|
this.configCache.set(fullPath, data);
|
|
2485
2631
|
this.persistConfig(fullPath, bytes).catch((err) => {
|
|
2486
|
-
|
|
2632
|
+
log4.error(`[zen-fs-config] Failed to persist ${fullPath}:`, err);
|
|
2487
2633
|
});
|
|
2488
2634
|
}
|
|
2489
2635
|
// -----------------------------------------------------------------------
|
|
@@ -2630,7 +2776,7 @@ var ConfigRepo = class {
|
|
|
2630
2776
|
}
|
|
2631
2777
|
const onEnumerateError = (label, error) => {
|
|
2632
2778
|
const reason = error?.message ?? String(error);
|
|
2633
|
-
|
|
2779
|
+
log4.warn(
|
|
2634
2780
|
`[ConfigRepo] deleteFile(directory): cannot enumerate "${label}" under ${normalizedPath}: ${reason}`
|
|
2635
2781
|
);
|
|
2636
2782
|
};
|
|
@@ -2645,7 +2791,7 @@ var ConfigRepo = class {
|
|
|
2645
2791
|
await removeDirectoryTree(this.cachedFS, normalizedPath);
|
|
2646
2792
|
this.invalidateTombstoneCache();
|
|
2647
2793
|
this.schedulePostDeleteSync();
|
|
2648
|
-
|
|
2794
|
+
log4.log(`[ConfigRepo] deleteFile(directory): ${normalizedPath} (${files.length} file(s))`);
|
|
2649
2795
|
return;
|
|
2650
2796
|
}
|
|
2651
2797
|
await this.writeTombstoneFor(normalizedPath);
|
|
@@ -2666,7 +2812,7 @@ var ConfigRepo = class {
|
|
|
2666
2812
|
} catch {
|
|
2667
2813
|
}
|
|
2668
2814
|
}
|
|
2669
|
-
|
|
2815
|
+
log4.log(`[ConfigRepo] deleteFile: ${normalizedPath} (tombstone at ${tombstonePathFor(normalizedPath)})`);
|
|
2670
2816
|
this.invalidateTombstoneCache();
|
|
2671
2817
|
this.schedulePostDeleteSync();
|
|
2672
2818
|
}
|
|
@@ -2684,7 +2830,7 @@ var ConfigRepo = class {
|
|
|
2684
2830
|
this.postDeleteSyncTimer = void 0;
|
|
2685
2831
|
if (this.disposed) return;
|
|
2686
2832
|
this.syncEngine.syncAll().catch((err) => {
|
|
2687
|
-
|
|
2833
|
+
log4.warn("[ConfigRepo] post-delete sync failed:", err);
|
|
2688
2834
|
});
|
|
2689
2835
|
}, 500);
|
|
2690
2836
|
}
|
|
@@ -2696,7 +2842,7 @@ var ConfigRepo = class {
|
|
|
2696
2842
|
if (this.tombstoneCache !== null) {
|
|
2697
2843
|
return this.tombstoneCache;
|
|
2698
2844
|
}
|
|
2699
|
-
this.tombstoneCache = await readTombstonesFromFs(this.cachedFS, (message) =>
|
|
2845
|
+
this.tombstoneCache = await readTombstonesFromFs(this.cachedFS, (message) => log4.warn(message));
|
|
2700
2846
|
this.refreshSyncExclusions(this.tombstoneCache);
|
|
2701
2847
|
return this.tombstoneCache;
|
|
2702
2848
|
}
|
|
@@ -2714,16 +2860,40 @@ var ConfigRepo = class {
|
|
|
2714
2860
|
for (const tombstone of tombstones) {
|
|
2715
2861
|
if (!tombstone.path) continue;
|
|
2716
2862
|
add(tombstone.path);
|
|
2717
|
-
if (tombstone.path === "/.keep" || tombstone.path.endsWith("/.keep") || tombstone.path.endsWith("/.keep.mtime")) {
|
|
2718
|
-
const dir = tombstone.path.slice(0, tombstone.path.lastIndexOf("/")) || "/";
|
|
2719
|
-
if (dir !== "/") add(dir);
|
|
2720
|
-
}
|
|
2721
2863
|
}
|
|
2722
2864
|
}
|
|
2723
2865
|
/** Invalidate the tombstone cache — call after tombstones are modified. */
|
|
2724
2866
|
invalidateTombstoneCache() {
|
|
2725
2867
|
this.tombstoneCache = null;
|
|
2726
2868
|
}
|
|
2869
|
+
/**
|
|
2870
|
+
* 判定墓碑目标路径是否在删除之后被重新创建。
|
|
2871
|
+
*
|
|
2872
|
+
* 墓碑表达的是「删除意图」,但删除之后同一路径完全可能被再次写入。同步类工具
|
|
2873
|
+
* 的锁文件(`.sync.lock`)就是每轮「删除 → 重建」;若把墓碑当作永久否决权,
|
|
2874
|
+
* 这个路径会被每一轮 sync 反复删除,表现为「刚写进去 1 秒后就消失」、锁永远拿
|
|
2875
|
+
* 不到、深层目录刚建就没了。
|
|
2876
|
+
*
|
|
2877
|
+
* 判定依据是文件 mtime 晚于 `deletedAt`:写入必然刷新 mtime,因此 mtime 比删除
|
|
2878
|
+
* 时刻新就意味着这条墓碑已经被写操作撤销。
|
|
2879
|
+
*/
|
|
2880
|
+
async isPathRecreatedAfterTombstone(tombstone) {
|
|
2881
|
+
const surfaces = [{ label: "local-cache", fs: this.cachedFS }];
|
|
2882
|
+
for (const [id, replica] of this.replicaBackends) {
|
|
2883
|
+
surfaces.push({ label: id, fs: replica.instance });
|
|
2884
|
+
}
|
|
2885
|
+
for (const surface of surfaces) {
|
|
2886
|
+
let mtimeMs;
|
|
2887
|
+
try {
|
|
2888
|
+
const inode = await surface.fs.stat(tombstone.path);
|
|
2889
|
+
mtimeMs = Number(inode?.mtimeMs ?? 0);
|
|
2890
|
+
} catch {
|
|
2891
|
+
continue;
|
|
2892
|
+
}
|
|
2893
|
+
if (mtimeMs > tombstone.deletedAt + TOMBSTONE_RECREATE_SKEW_MS) return true;
|
|
2894
|
+
}
|
|
2895
|
+
return false;
|
|
2896
|
+
}
|
|
2727
2897
|
/**
|
|
2728
2898
|
* Before sync: for each tombstone, delete the actual file on all replicas.
|
|
2729
2899
|
* This prevents bi-directional sync from copying the file back.
|
|
@@ -2741,10 +2911,15 @@ var ConfigRepo = class {
|
|
|
2741
2911
|
let processed = 0;
|
|
2742
2912
|
let alreadyDeleted = 0;
|
|
2743
2913
|
let unresolved = 0;
|
|
2914
|
+
const expired = [];
|
|
2744
2915
|
for (const tombstone of tombstones) {
|
|
2745
2916
|
const isKeepPlaceholder = tombstone.path === "/.keep" || tombstone.path.endsWith("/.keep");
|
|
2746
2917
|
const forceDelete = isMtimeSidecar(tombstone.path) || isKeepPlaceholder;
|
|
2747
2918
|
const tVersionPath = forceDelete ? null : versionPathFor(tombstone.path);
|
|
2919
|
+
if (!forceDelete && await this.isPathRecreatedAfterTombstone(tombstone)) {
|
|
2920
|
+
expired.push(tombstone);
|
|
2921
|
+
continue;
|
|
2922
|
+
}
|
|
2748
2923
|
const sidecarOwner = sidecarOwnerPath(tombstone.path);
|
|
2749
2924
|
const isKeepSidecar = sidecarOwner === "/.keep" || (sidecarOwner?.endsWith("/.keep") ?? false);
|
|
2750
2925
|
const liveSidecarCandidate = sidecarOwner !== null && !isKeepSidecar;
|
|
@@ -2798,14 +2973,27 @@ var ConfigRepo = class {
|
|
|
2798
2973
|
confirmed.add(replicaId);
|
|
2799
2974
|
} else {
|
|
2800
2975
|
unresolved++;
|
|
2801
|
-
|
|
2976
|
+
log4.warn(
|
|
2802
2977
|
`[ConfigRepo] tombstone kept: ${tombstone.path} still present on replica ${replicaId}`
|
|
2803
2978
|
);
|
|
2804
2979
|
}
|
|
2805
2980
|
}
|
|
2806
2981
|
}
|
|
2982
|
+
if (expired.length > 0) {
|
|
2983
|
+
for (const tombstone of expired) {
|
|
2984
|
+
try {
|
|
2985
|
+
await this.cachedFS.unlink(tombstonePathFor(tombstone.path, TOMBSTONES_DIR));
|
|
2986
|
+
} catch {
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
this.invalidateTombstoneCache();
|
|
2990
|
+
await this.readTombstones();
|
|
2991
|
+
log4.log(
|
|
2992
|
+
`[ConfigRepo] processTombstones: expired ${expired.length} tombstone(s) \u2014 path recreated after deletion`
|
|
2993
|
+
);
|
|
2994
|
+
}
|
|
2807
2995
|
if (processed > 0 || alreadyDeleted > 0 || unresolved > 0) {
|
|
2808
|
-
|
|
2996
|
+
log4.log(
|
|
2809
2997
|
`[ConfigRepo] processTombstones: ${tombstones.length} tombstone(s), ${processed} deleted, ${alreadyDeleted} already gone, ${unresolved} unresolved`
|
|
2810
2998
|
);
|
|
2811
2999
|
}
|
|
@@ -2906,21 +3094,21 @@ var ConfigRepo = class {
|
|
|
2906
3094
|
const innerBackendName = fs?.inner?.backendName;
|
|
2907
3095
|
const directBackendName = fs?.backendName;
|
|
2908
3096
|
const backendLabel = innerBackendName ?? directBackendName ?? innerBackend ?? ctorName;
|
|
2909
|
-
|
|
3097
|
+
log4.log(`[TOMB-TRACE] safeExists(${path}): fs type=${ctorName} backend=${backendLabel} hasExists=${hasExists} hasStat=${hasStat} inner=${innerBackend ?? "N/A"}`);
|
|
2910
3098
|
if (hasExists) {
|
|
2911
|
-
|
|
3099
|
+
log4.log(`[TOMB-TRACE] safeExists(${path}): \u2192 calling fs.exists() [backend=${backendLabel}]`);
|
|
2912
3100
|
const result = await fs.exists(path);
|
|
2913
|
-
|
|
3101
|
+
log4.log(`[TOMB-TRACE] safeExists(${path}): \u2190 fs.exists() [backend=${backendLabel}] \u2192 ${result}`);
|
|
2914
3102
|
return result;
|
|
2915
3103
|
}
|
|
2916
|
-
|
|
3104
|
+
log4.log(`[TOMB-TRACE] safeExists(${path}): no exists(), calling fs.stat() [backend=${backendLabel}]`);
|
|
2917
3105
|
await fs.stat(path);
|
|
2918
|
-
|
|
3106
|
+
log4.log(`[TOMB-TRACE] safeExists(${path}): \u2190 fs.stat() [backend=${backendLabel}] \u2192 OK (exists)`);
|
|
2919
3107
|
return true;
|
|
2920
3108
|
} catch (err) {
|
|
2921
3109
|
const errCode = err?.code ?? err?.status ?? "";
|
|
2922
3110
|
const errName = err?.constructor?.name ?? "";
|
|
2923
|
-
|
|
3111
|
+
log4.log(`[TOMB-TRACE] safeExists(${path}): \u2190 ERROR ${errName} ${errCode} ${err?.message ?? err}`);
|
|
2924
3112
|
return false;
|
|
2925
3113
|
}
|
|
2926
3114
|
}
|
|
@@ -2947,13 +3135,13 @@ var ConfigRepo = class {
|
|
|
2947
3135
|
this.assertNotDisposed();
|
|
2948
3136
|
const result = await purgeMtimeSidecars(this.cachedFS, options);
|
|
2949
3137
|
if (result.removed.length > 0) {
|
|
2950
|
-
|
|
3138
|
+
log4.log(
|
|
2951
3139
|
`[ConfigRepo] purged ${result.removed.length} .mtime sidecar(s) from local primary` + (options?.dryRun ? " (dry run)" : ""),
|
|
2952
3140
|
result.removed
|
|
2953
3141
|
);
|
|
2954
3142
|
}
|
|
2955
3143
|
if (result.failed.length > 0) {
|
|
2956
|
-
|
|
3144
|
+
log4.warn(`[ConfigRepo] failed to purge ${result.failed.length} .mtime sidecar(s):`, result.failed);
|
|
2957
3145
|
}
|
|
2958
3146
|
return result;
|
|
2959
3147
|
}
|
|
@@ -2973,13 +3161,13 @@ var ConfigRepo = class {
|
|
|
2973
3161
|
this.assertNotDisposed();
|
|
2974
3162
|
const result = await purgeKeepFiles(this.cachedFS, options);
|
|
2975
3163
|
if (result.removed.length > 0) {
|
|
2976
|
-
|
|
3164
|
+
log4.log(
|
|
2977
3165
|
`[ConfigRepo] purged ${result.removed.length} .keep placeholder(s) from local primary` + (options?.dryRun ? " (dry run)" : ""),
|
|
2978
3166
|
result.removed
|
|
2979
3167
|
);
|
|
2980
3168
|
}
|
|
2981
3169
|
if (result.failed.length > 0) {
|
|
2982
|
-
|
|
3170
|
+
log4.warn(`[ConfigRepo] failed to purge ${result.failed.length} .keep placeholder(s):`, result.failed);
|
|
2983
3171
|
}
|
|
2984
3172
|
return result;
|
|
2985
3173
|
}
|
|
@@ -2998,24 +3186,24 @@ var ConfigRepo = class {
|
|
|
2998
3186
|
}));
|
|
2999
3187
|
const result = await migrateVersionSidecars(this.cachedFS, { replicas });
|
|
3000
3188
|
if (result.renamed.length > 0) {
|
|
3001
|
-
|
|
3189
|
+
log4.log(
|
|
3002
3190
|
`[ConfigRepo] migrated ${result.renamed.length} legacy version sidecar(s) to new naming`,
|
|
3003
3191
|
result.renamed
|
|
3004
3192
|
);
|
|
3005
3193
|
}
|
|
3006
3194
|
if (result.deleted.length > 0) {
|
|
3007
|
-
|
|
3195
|
+
log4.log(
|
|
3008
3196
|
`[ConfigRepo] removed ${result.deleted.length} orphaned legacy version sidecar(s)`,
|
|
3009
3197
|
result.deleted
|
|
3010
3198
|
);
|
|
3011
3199
|
}
|
|
3012
3200
|
if (result.remoteDeleted.length > 0) {
|
|
3013
|
-
|
|
3201
|
+
log4.log(
|
|
3014
3202
|
`[ConfigRepo] removed ${result.remoteDeleted.length} remote residual legacy version sidecar(s)`
|
|
3015
3203
|
);
|
|
3016
3204
|
}
|
|
3017
3205
|
if (result.failed.length > 0) {
|
|
3018
|
-
|
|
3206
|
+
log4.warn(`[ConfigRepo] failed to migrate ${result.failed.length} legacy version sidecar(s):`, result.failed);
|
|
3019
3207
|
}
|
|
3020
3208
|
return result;
|
|
3021
3209
|
}
|
|
@@ -3038,9 +3226,9 @@ var ConfigRepo = class {
|
|
|
3038
3226
|
*/
|
|
3039
3227
|
startBackgroundSync() {
|
|
3040
3228
|
this.initialSyncPromise = this.initialSyncAndDedup().then(() => {
|
|
3041
|
-
|
|
3229
|
+
log4.log("[ConfigRepo] Background initial sync complete");
|
|
3042
3230
|
}).catch((err) => {
|
|
3043
|
-
|
|
3231
|
+
log4.error("[ConfigRepo] Background initial sync failed:", err);
|
|
3044
3232
|
}).finally(() => {
|
|
3045
3233
|
this.initialSyncPromise = null;
|
|
3046
3234
|
});
|
|
@@ -3079,7 +3267,7 @@ var ConfigRepo = class {
|
|
|
3079
3267
|
}
|
|
3080
3268
|
}
|
|
3081
3269
|
}
|
|
3082
|
-
|
|
3270
|
+
log4.log(`[ConfigRepo] updateTombstoneConfirmations: ${modified} tombstone(s) updated`);
|
|
3083
3271
|
if (modified > 0) this.invalidateTombstoneCache();
|
|
3084
3272
|
}
|
|
3085
3273
|
/**
|
|
@@ -3117,7 +3305,12 @@ var ConfigRepo = class {
|
|
|
3117
3305
|
const tombstones = await this.readTombstones();
|
|
3118
3306
|
if (tombstones.length === 0) return;
|
|
3119
3307
|
const allBackendIds = [...this.replicaBackends.keys()];
|
|
3308
|
+
let keptByRetention = 0;
|
|
3120
3309
|
for (const tombstone of tombstones) {
|
|
3310
|
+
if (!isTombstoneRetentionElapsed(tombstone)) {
|
|
3311
|
+
keptByRetention++;
|
|
3312
|
+
continue;
|
|
3313
|
+
}
|
|
3121
3314
|
const allConfirmed = await this.allReplicasConfirmAbsent(tombstone.path);
|
|
3122
3315
|
if (allConfirmed) {
|
|
3123
3316
|
const tombstonePath = tombstonePathFor(tombstone.path, TOMBSTONES_DIR);
|
|
@@ -3133,9 +3326,12 @@ var ConfigRepo = class {
|
|
|
3133
3326
|
}
|
|
3134
3327
|
}
|
|
3135
3328
|
}
|
|
3136
|
-
|
|
3329
|
+
log4.log(`[ConfigRepo] gcTombstones: removed ${tombstonePath} (all ${allBackendIds.length} backends confirmed)`);
|
|
3137
3330
|
}
|
|
3138
3331
|
}
|
|
3332
|
+
if (keptByRetention > 0) {
|
|
3333
|
+
log4.log(`[ConfigRepo] gcTombstones: kept ${keptByRetention} tombstone(s) within retention window`);
|
|
3334
|
+
}
|
|
3139
3335
|
}
|
|
3140
3336
|
/**
|
|
3141
3337
|
* Sync .meta/ files (backends.json) to all replica backends.
|
|
@@ -3149,7 +3345,7 @@ var ConfigRepo = class {
|
|
|
3149
3345
|
this.assertNotDisposed();
|
|
3150
3346
|
const results = await this.flush();
|
|
3151
3347
|
for (const result of results) {
|
|
3152
|
-
|
|
3348
|
+
log4.log(
|
|
3153
3349
|
`[ConfigRepo] syncMetaToReplicas: ${result.pairId} +${result.filesCreated}/~${result.filesUpdated}/-${result.filesDeleted} skip:${result.filesSkipped} ${result.durationMs}ms`
|
|
3154
3350
|
);
|
|
3155
3351
|
}
|
|
@@ -3322,21 +3518,21 @@ var ConfigRepo = class {
|
|
|
3322
3518
|
// Internal — Setup
|
|
3323
3519
|
// -----------------------------------------------------------------------
|
|
3324
3520
|
async setupSync(backends, primaryBackendId, pollIntervalMs) {
|
|
3325
|
-
|
|
3521
|
+
log4.log(`[ConfigRepo] setupSync: ${backends.length} backends, primary=${primaryBackendId} pollInterval=${pollIntervalMs ?? "default"}ms`);
|
|
3326
3522
|
for (const desc of backends) {
|
|
3327
3523
|
if (desc.id === primaryBackendId) continue;
|
|
3328
3524
|
if (desc.enabled === false) {
|
|
3329
|
-
|
|
3525
|
+
log4.log(`[ConfigRepo] Skipping disabled replica: ${desc.id}`);
|
|
3330
3526
|
continue;
|
|
3331
3527
|
}
|
|
3332
|
-
|
|
3528
|
+
log4.log(`[ConfigRepo] Creating replica backend: id=${desc.id}, type=${desc.type}`);
|
|
3333
3529
|
try {
|
|
3334
3530
|
const instance = await createBackend(desc);
|
|
3335
3531
|
let fsInstance = instance;
|
|
3336
3532
|
if (this.cacheOptions) {
|
|
3337
3533
|
const { wrapWithCache: wrapWithCache2 } = await Promise.resolve().then(() => (init_cache_wrapper(), cache_wrapper_exports));
|
|
3338
3534
|
fsInstance = wrapWithCache2(instance, desc.id, this.cacheOptions);
|
|
3339
|
-
|
|
3535
|
+
log4.log(`[ConfigRepo] Replica ${desc.id} wrapped with CachedFileSystem (store=${this.cacheOptions.storeType ?? "IdbCacheStore"})`);
|
|
3340
3536
|
}
|
|
3341
3537
|
const syncable = backendToSyncableFS(fsInstance, `${desc.type}(${desc.id})`);
|
|
3342
3538
|
const pair = this.syncEngine.addPair(
|
|
@@ -3354,7 +3550,7 @@ var ConfigRepo = class {
|
|
|
3354
3550
|
this.invalidateTombstoneCache();
|
|
3355
3551
|
await this.processTombstones();
|
|
3356
3552
|
} catch (err) {
|
|
3357
|
-
|
|
3553
|
+
log4.warn("[ConfigRepo] preSyncHook processTombstones failed:", err);
|
|
3358
3554
|
}
|
|
3359
3555
|
},
|
|
3360
3556
|
postSyncHook: async () => {
|
|
@@ -3363,7 +3559,7 @@ var ConfigRepo = class {
|
|
|
3363
3559
|
await this.processTombstones();
|
|
3364
3560
|
await this.updateTombstoneConfirmations();
|
|
3365
3561
|
} catch (err) {
|
|
3366
|
-
|
|
3562
|
+
log4.warn("[ConfigRepo] postSyncHook tombstone processing failed:", err);
|
|
3367
3563
|
}
|
|
3368
3564
|
}
|
|
3369
3565
|
},
|
|
@@ -3376,9 +3572,9 @@ var ConfigRepo = class {
|
|
|
3376
3572
|
};
|
|
3377
3573
|
this.syncEngine.on(pair.pairId, "conflict", conflictHandler);
|
|
3378
3574
|
this.trackSyncErrors(desc.id, pair.pairId);
|
|
3379
|
-
|
|
3575
|
+
log4.log(`[ConfigRepo] Replica ${desc.id} created, sync pair=${pair.pairId}`);
|
|
3380
3576
|
} catch (err) {
|
|
3381
|
-
|
|
3577
|
+
log4.error(`[ConfigRepo] Failed to create replica ${desc.id} (${desc.type}):`, err);
|
|
3382
3578
|
this.backendStates.set(desc.id, {
|
|
3383
3579
|
backendId: desc.id,
|
|
3384
3580
|
error: {
|
|
@@ -3388,8 +3584,8 @@ var ConfigRepo = class {
|
|
|
3388
3584
|
});
|
|
3389
3585
|
}
|
|
3390
3586
|
}
|
|
3391
|
-
|
|
3392
|
-
|
|
3587
|
+
log4.log(`[ConfigRepo] setupSync complete. Replicas:`, Array.from(this.replicaBackends.keys()));
|
|
3588
|
+
log4.log(`[ConfigRepo] Sync statuses:`, this.getSyncStatuses());
|
|
3393
3589
|
}
|
|
3394
3590
|
// -----------------------------------------------------------------------
|
|
3395
3591
|
// Internal — Persistence
|
|
@@ -3401,12 +3597,12 @@ var ConfigRepo = class {
|
|
|
3401
3597
|
try {
|
|
3402
3598
|
const existing = await readVersion(this.fullFS, vPath);
|
|
3403
3599
|
if (existing && existing.hash === version.hash) {
|
|
3404
|
-
|
|
3600
|
+
log4.log("[ConfigRepo] skip version sidecar (hash unchanged):", vPath);
|
|
3405
3601
|
return;
|
|
3406
3602
|
}
|
|
3407
3603
|
} catch {
|
|
3408
3604
|
}
|
|
3409
|
-
|
|
3605
|
+
log4.log("[ConfigRepo] write version sidecar:", vPath, "v" + version.version);
|
|
3410
3606
|
await this.ensureDir(vPath);
|
|
3411
3607
|
await writeVersion(this.fullFS, vPath, version);
|
|
3412
3608
|
}
|
|
@@ -3510,7 +3706,7 @@ var ConfigRepo = class {
|
|
|
3510
3706
|
await this.resolveConflict(`${conflictId}/meta.json`, customMerge);
|
|
3511
3707
|
}
|
|
3512
3708
|
} catch (err) {
|
|
3513
|
-
|
|
3709
|
+
log4.error("[zen-fs-config] Conflict handler error:", err);
|
|
3514
3710
|
}
|
|
3515
3711
|
}
|
|
3516
3712
|
}
|
|
@@ -3626,11 +3822,11 @@ var ConfigRepo = class {
|
|
|
3626
3822
|
}
|
|
3627
3823
|
return { kind: "ok", desc, mtime };
|
|
3628
3824
|
} else {
|
|
3629
|
-
|
|
3825
|
+
log4.warn(`[ConfigRepo] Backend descriptor ${entry} is missing id/type fields, marking for cleanup`);
|
|
3630
3826
|
return { kind: "corrupt", filePath };
|
|
3631
3827
|
}
|
|
3632
3828
|
} catch (parseErr) {
|
|
3633
|
-
|
|
3829
|
+
log4.warn(`[ConfigRepo] Backend descriptor ${entry} has corrupted JSON: ${parseErr}. Marking for cleanup.`);
|
|
3634
3830
|
return { kind: "corrupt", filePath };
|
|
3635
3831
|
}
|
|
3636
3832
|
})
|
|
@@ -3696,7 +3892,7 @@ var ConfigRepo = class {
|
|
|
3696
3892
|
}
|
|
3697
3893
|
}
|
|
3698
3894
|
if (duplicates.length > 0) {
|
|
3699
|
-
|
|
3895
|
+
log4.log(
|
|
3700
3896
|
`[ConfigRepo] readAllBackendDescriptors: removing ${duplicates.length} duplicate(s): ${duplicates.join(", ")}`
|
|
3701
3897
|
);
|
|
3702
3898
|
for (const dupId of duplicates) {
|
|
@@ -3828,7 +4024,7 @@ var ConfigRepo = class {
|
|
|
3828
4024
|
* Shared by addBackend() and replaceBackend().
|
|
3829
4025
|
*/
|
|
3830
4026
|
async setupReplica(id, type, options, description) {
|
|
3831
|
-
|
|
4027
|
+
log4.log(`[ConfigRepo] addBackend: creating ${id} (${type})...`);
|
|
3832
4028
|
const instance = await createBackend({ type, options });
|
|
3833
4029
|
const syncable = backendToSyncableFS(instance, `${type}(${id})`);
|
|
3834
4030
|
const desc = { id, type, options, description };
|
|
@@ -3846,7 +4042,7 @@ var ConfigRepo = class {
|
|
|
3846
4042
|
this.invalidateTombstoneCache();
|
|
3847
4043
|
await this.processTombstones();
|
|
3848
4044
|
} catch (err) {
|
|
3849
|
-
|
|
4045
|
+
log4.warn("[ConfigRepo] preSyncHook processTombstones failed:", err);
|
|
3850
4046
|
}
|
|
3851
4047
|
},
|
|
3852
4048
|
postSyncHook: async () => {
|
|
@@ -3855,7 +4051,7 @@ var ConfigRepo = class {
|
|
|
3855
4051
|
await this.processTombstones();
|
|
3856
4052
|
await this.updateTombstoneConfirmations();
|
|
3857
4053
|
} catch (err) {
|
|
3858
|
-
|
|
4054
|
+
log4.warn("[ConfigRepo] postSyncHook tombstone processing failed:", err);
|
|
3859
4055
|
}
|
|
3860
4056
|
}
|
|
3861
4057
|
},
|
|
@@ -3868,7 +4064,7 @@ var ConfigRepo = class {
|
|
|
3868
4064
|
};
|
|
3869
4065
|
this.syncEngine.on(pair.pairId, "conflict", conflictHandler);
|
|
3870
4066
|
this.trackSyncErrors(id, pair.pairId);
|
|
3871
|
-
|
|
4067
|
+
log4.log(`[ConfigRepo] addBackend: ${id} (${type}) added, sync pair=${pair.pairId}`);
|
|
3872
4068
|
await this.syncMetaToReplicas();
|
|
3873
4069
|
this.syncEngine.watch(pair.pairId);
|
|
3874
4070
|
return pair.pairId;
|
|
@@ -3894,13 +4090,13 @@ var ConfigRepo = class {
|
|
|
3894
4090
|
}
|
|
3895
4091
|
}
|
|
3896
4092
|
this.syncEngine.removePair(replica.pairId);
|
|
3897
|
-
|
|
4093
|
+
log4.log(`[ConfigRepo] removeBackend: sync pair ${replica.pairId} removed`);
|
|
3898
4094
|
this.replicaBackends.delete(id);
|
|
3899
4095
|
if (replica.instance?.dispose) {
|
|
3900
4096
|
await replica.instance.dispose();
|
|
3901
4097
|
}
|
|
3902
4098
|
} else {
|
|
3903
|
-
|
|
4099
|
+
log4.log(`[ConfigRepo] removeBackend: "${id}" not in replicaBackends, cleaning up descriptor only`);
|
|
3904
4100
|
for (const descPath of descPaths) {
|
|
3905
4101
|
try {
|
|
3906
4102
|
await this.deleteFile(descPath);
|
|
@@ -3911,7 +4107,7 @@ var ConfigRepo = class {
|
|
|
3911
4107
|
}
|
|
3912
4108
|
this.backendStates.delete(id);
|
|
3913
4109
|
this.schedulePostDeleteSync();
|
|
3914
|
-
|
|
4110
|
+
log4.log(`[ConfigRepo] removeBackend: ${id} removed (tombstone written, remote cleaned)`);
|
|
3915
4111
|
}
|
|
3916
4112
|
/**
|
|
3917
4113
|
* Replace a config-sync backend's parameters in place (same id).
|
|
@@ -3958,11 +4154,11 @@ var ConfigRepo = class {
|
|
|
3958
4154
|
await this.setupReplica(id, type, options, description ?? before.description);
|
|
3959
4155
|
} catch (err) {
|
|
3960
4156
|
const reason = err instanceof Error ? err.message : String(err);
|
|
3961
|
-
|
|
4157
|
+
log4.warn(`[ConfigRepo] replaceBackend: "${id}" failed, rolling back old options:`, err);
|
|
3962
4158
|
try {
|
|
3963
4159
|
await this.setupReplica(id, before.type, before.options, before.description);
|
|
3964
4160
|
} catch (rollbackErr) {
|
|
3965
|
-
|
|
4161
|
+
log4.error(`[ConfigRepo] replaceBackend: rollback for "${id}" ALSO failed:`, rollbackErr);
|
|
3966
4162
|
}
|
|
3967
4163
|
throw new Error(`\u66F4\u65B0\u540E\u7AEF\u53C2\u6570\u5931\u8D25\uFF1A${reason}\uFF08\u5DF2\u5C1D\u8BD5\u56DE\u6EDA\u65E7\u53C2\u6570\uFF09`);
|
|
3968
4164
|
}
|
|
@@ -3978,7 +4174,7 @@ var ConfigRepo = class {
|
|
|
3978
4174
|
const existing = await this.cachedFS.readFile(path, "utf-8");
|
|
3979
4175
|
const current = existing.trim();
|
|
3980
4176
|
if (current && current !== type) {
|
|
3981
|
-
|
|
4177
|
+
log4.warn(`[ConfigRepo] group-type already set to "${current}" at ${path}, ignoring request to set "${type}"`);
|
|
3982
4178
|
return;
|
|
3983
4179
|
}
|
|
3984
4180
|
} catch {
|
|
@@ -4009,7 +4205,7 @@ var ConfigRepo = class {
|
|
|
4009
4205
|
features: existingDescriptor.features ?? CONFIG_GROUP_FEATURES
|
|
4010
4206
|
});
|
|
4011
4207
|
}
|
|
4012
|
-
|
|
4208
|
+
log4.log(`[ConfigRepo] group-type set to "${type}"`);
|
|
4013
4209
|
}
|
|
4014
4210
|
/** Read the group-type marker. Returns null if not set (new path, then legacy). */
|
|
4015
4211
|
async getGroupType() {
|
|
@@ -4104,7 +4300,7 @@ var ConfigRepo = class {
|
|
|
4104
4300
|
if (this.appDataGroups.has(id)) {
|
|
4105
4301
|
throw new Error(`App data group "${id}" already exists. Use removeAppDataGroup() first.`);
|
|
4106
4302
|
}
|
|
4107
|
-
|
|
4303
|
+
log4.log(`[ConfigRepo] createAppDataGroup: creating "${id}" with ${backends.length} backend(s)`);
|
|
4108
4304
|
const resolvedBackends = [];
|
|
4109
4305
|
for (const desc of backends) {
|
|
4110
4306
|
assertValidBackendId(desc.id);
|
|
@@ -4131,7 +4327,7 @@ var ConfigRepo = class {
|
|
|
4131
4327
|
};
|
|
4132
4328
|
await this.writeAppDataGroupDescriptorFile(id, descriptor);
|
|
4133
4329
|
this.appDataGroups.set(id, group);
|
|
4134
|
-
|
|
4330
|
+
log4.log(`[ConfigRepo] createAppDataGroup: "${id}" created`);
|
|
4135
4331
|
return group;
|
|
4136
4332
|
}
|
|
4137
4333
|
/**
|
|
@@ -4174,7 +4370,7 @@ var ConfigRepo = class {
|
|
|
4174
4370
|
try {
|
|
4175
4371
|
await live.writeOwnGroupDescriptor();
|
|
4176
4372
|
} catch (err) {
|
|
4177
|
-
|
|
4373
|
+
log4.warn(`[ConfigRepo] updateAppDataGroupName: "${id}" group.json update failed:`, err);
|
|
4178
4374
|
}
|
|
4179
4375
|
}
|
|
4180
4376
|
const descriptor = {
|
|
@@ -4237,7 +4433,7 @@ var ConfigRepo = class {
|
|
|
4237
4433
|
return group;
|
|
4238
4434
|
} catch (err) {
|
|
4239
4435
|
const reason = err instanceof Error ? err.message : String(err);
|
|
4240
|
-
|
|
4436
|
+
log4.warn(`[ConfigRepo] replaceAppDataGroupBackend: "${backendId}" failed, rolling back:`, err);
|
|
4241
4437
|
try {
|
|
4242
4438
|
const restored = new AppDataGroupImpl(
|
|
4243
4439
|
groupId,
|
|
@@ -4251,7 +4447,7 @@ var ConfigRepo = class {
|
|
|
4251
4447
|
await this.updateAppDataGroupDescriptor(groupId, restored.listDeclaredBackends());
|
|
4252
4448
|
this.appDataGroups.set(groupId, restored);
|
|
4253
4449
|
} catch (rollbackErr) {
|
|
4254
|
-
|
|
4450
|
+
log4.error("[ConfigRepo] replaceAppDataGroupBackend: rollback ALSO failed:", rollbackErr);
|
|
4255
4451
|
}
|
|
4256
4452
|
throw new Error(`\u66F4\u65B0\u540E\u7AEF\u53C2\u6570\u5931\u8D25\uFF1A${reason}\uFF08\u5DF2\u5C1D\u8BD5\u56DE\u6EDA\u65E7\u53C2\u6570\uFF09`);
|
|
4257
4453
|
}
|
|
@@ -4369,7 +4565,7 @@ var ConfigRepo = class {
|
|
|
4369
4565
|
try {
|
|
4370
4566
|
await this.deleteFile(descPath);
|
|
4371
4567
|
} catch (err) {
|
|
4372
|
-
|
|
4568
|
+
log4.log(`[ConfigRepo] removeAppDataGroup: deleteFile failed for "${id}" at ${descPath}:`, err);
|
|
4373
4569
|
try {
|
|
4374
4570
|
await this.cachedFS.unlink(descPath);
|
|
4375
4571
|
} catch {
|
|
@@ -4377,7 +4573,7 @@ var ConfigRepo = class {
|
|
|
4377
4573
|
await this.unlinkVersionSidecar(this.cachedFS, descPath);
|
|
4378
4574
|
}
|
|
4379
4575
|
}
|
|
4380
|
-
|
|
4576
|
+
log4.log(`[ConfigRepo] removeAppDataGroup: "${id}" removed`);
|
|
4381
4577
|
}
|
|
4382
4578
|
async listAccountBackends() {
|
|
4383
4579
|
this.assertNotDisposed();
|
|
@@ -4566,24 +4762,24 @@ var AppDataGroupImpl = class {
|
|
|
4566
4762
|
try {
|
|
4567
4763
|
const purged = await purgeMtimeSidecars(this.localFS);
|
|
4568
4764
|
if (purged.removed.length > 0) {
|
|
4569
|
-
|
|
4765
|
+
log4.warn(
|
|
4570
4766
|
`[AppDataGroup:${this.groupId}] purged ${purged.removed.length} leaked .mtime sidecar(s):`,
|
|
4571
4767
|
purged.removed
|
|
4572
4768
|
);
|
|
4573
4769
|
}
|
|
4574
4770
|
} catch (err) {
|
|
4575
|
-
|
|
4771
|
+
log4.warn(`[AppDataGroup:${this.groupId}] .mtime purge failed:`, err?.message ?? err);
|
|
4576
4772
|
}
|
|
4577
4773
|
try {
|
|
4578
4774
|
const purgedKeep = await purgeKeepFiles(this.localFS);
|
|
4579
4775
|
if (purgedKeep.removed.length > 0) {
|
|
4580
|
-
|
|
4776
|
+
log4.warn(
|
|
4581
4777
|
`[AppDataGroup:${this.groupId}] purged ${purgedKeep.removed.length} leaked .keep placeholder(s):`,
|
|
4582
4778
|
purgedKeep.removed
|
|
4583
4779
|
);
|
|
4584
4780
|
}
|
|
4585
4781
|
} catch (err) {
|
|
4586
|
-
|
|
4782
|
+
log4.warn(`[AppDataGroup:${this.groupId}] .keep purge failed:`, err?.message ?? err);
|
|
4587
4783
|
}
|
|
4588
4784
|
const localSyncable = backendToSyncableFS(this.localFS, `local(${this.groupId})`);
|
|
4589
4785
|
this.tombstones = new TombstoneStore(this.localFS, this.localBackendId);
|
|
@@ -4593,13 +4789,13 @@ var AppDataGroupImpl = class {
|
|
|
4593
4789
|
try {
|
|
4594
4790
|
backendsChanged = await this.loadBackendDescriptors();
|
|
4595
4791
|
} catch (err) {
|
|
4596
|
-
|
|
4792
|
+
log4.warn(`[AppDataGroup:${this.groupId}] failed to load .meta/backends:`, err);
|
|
4597
4793
|
this._backends = this._backends ?? [];
|
|
4598
4794
|
}
|
|
4599
4795
|
try {
|
|
4600
4796
|
await this.writeOwnGroupDescriptor();
|
|
4601
4797
|
} catch (err) {
|
|
4602
|
-
|
|
4798
|
+
log4.warn(`[AppDataGroup:${this.groupId}] failed to write .meta/group.json:`, err);
|
|
4603
4799
|
}
|
|
4604
4800
|
this.fs = createChrootFS(this.localFS, "/", {
|
|
4605
4801
|
hiddenPrefixes: [META_DIR, CONFIG_DIR],
|
|
@@ -4609,7 +4805,7 @@ var AppDataGroupImpl = class {
|
|
|
4609
4805
|
});
|
|
4610
4806
|
const { kept, removed } = dedupeAppDataBackends(this._backends);
|
|
4611
4807
|
if (removed.length > 0) {
|
|
4612
|
-
|
|
4808
|
+
log4.warn(
|
|
4613
4809
|
`[AppDataGroup:${this.groupId}] dedupe: removing ${removed.length} duplicate backend(s) (${removed.map((b) => b.id).join(", ")}) \u2014 keeping ${kept.map((b) => b.id).join(", ")}`
|
|
4614
4810
|
);
|
|
4615
4811
|
this._backends = kept;
|
|
@@ -4623,7 +4819,7 @@ var AppDataGroupImpl = class {
|
|
|
4623
4819
|
features: this._declaredFeatures
|
|
4624
4820
|
});
|
|
4625
4821
|
} catch (err) {
|
|
4626
|
-
|
|
4822
|
+
log4.warn(`[AppDataGroup:${this.groupId}] failed to persist backend topology:`, err);
|
|
4627
4823
|
}
|
|
4628
4824
|
}
|
|
4629
4825
|
for (const desc of this._backends) {
|
|
@@ -4655,26 +4851,26 @@ var AppDataGroupImpl = class {
|
|
|
4655
4851
|
if (this.features.conflicts) {
|
|
4656
4852
|
const conflictHandler = (event) => {
|
|
4657
4853
|
this.archiveConflict(event).catch((err) => {
|
|
4658
|
-
|
|
4854
|
+
log4.warn(`[AppDataGroup:${this.groupId}] conflict archive failed:`, err);
|
|
4659
4855
|
});
|
|
4660
4856
|
};
|
|
4661
4857
|
this.syncEngine.on(pair.pairId, "conflict", conflictHandler);
|
|
4662
4858
|
}
|
|
4663
4859
|
this.trackSyncErrors(desc.id, pair.pairId);
|
|
4664
|
-
|
|
4860
|
+
log4.log(`[AppDataGroup:${this.groupId}] backend ${desc.id} (${desc.type}) connected, pair=${pair.pairId}`);
|
|
4665
4861
|
} catch (err) {
|
|
4666
4862
|
const message = err instanceof Error ? err.message : String(err);
|
|
4667
4863
|
this.backendStates.set(desc.id, {
|
|
4668
4864
|
backendId: desc.id,
|
|
4669
4865
|
error: { message, timestamp: Date.now() }
|
|
4670
4866
|
});
|
|
4671
|
-
|
|
4867
|
+
log4.error(`[AppDataGroup:${this.groupId}] Failed to create backend ${desc.id} (${desc.type}):`, err);
|
|
4672
4868
|
}
|
|
4673
4869
|
}
|
|
4674
4870
|
try {
|
|
4675
4871
|
await this.syncEngine.syncAll();
|
|
4676
4872
|
} catch (err) {
|
|
4677
|
-
|
|
4873
|
+
log4.warn(`[AppDataGroup:${this.groupId}] Initial sync failed:`, err);
|
|
4678
4874
|
}
|
|
4679
4875
|
this.syncEngine.watchAll();
|
|
4680
4876
|
}
|
|
@@ -4689,7 +4885,7 @@ var AppDataGroupImpl = class {
|
|
|
4689
4885
|
try {
|
|
4690
4886
|
await this.tombstones.gc(this.allBackendIds(), this.tombstoneTargets());
|
|
4691
4887
|
} catch (err) {
|
|
4692
|
-
|
|
4888
|
+
log4.warn(`[AppDataGroup:${this.groupId}] tombstone GC failed:`, err);
|
|
4693
4889
|
}
|
|
4694
4890
|
return Array.from(results.values());
|
|
4695
4891
|
}
|
|
@@ -4724,7 +4920,7 @@ var AppDataGroupImpl = class {
|
|
|
4724
4920
|
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : toUint8Array2(data);
|
|
4725
4921
|
await this.versions.record(path, bytes);
|
|
4726
4922
|
} catch (err) {
|
|
4727
|
-
|
|
4923
|
+
log4.warn(`[AppDataGroup:${this.groupId}] version record failed for ${path}:`, err);
|
|
4728
4924
|
}
|
|
4729
4925
|
}
|
|
4730
4926
|
/** Delete tombstoned files on local + every connected backend before/after sync. */
|
|
@@ -4732,14 +4928,14 @@ var AppDataGroupImpl = class {
|
|
|
4732
4928
|
try {
|
|
4733
4929
|
await this.tombstones.process(this.tombstoneTargets());
|
|
4734
4930
|
} catch (err) {
|
|
4735
|
-
|
|
4931
|
+
log4.warn(`[AppDataGroup:${this.groupId}] ${stage} tombstone pass failed:`, err);
|
|
4736
4932
|
}
|
|
4737
4933
|
}
|
|
4738
4934
|
async confirmTombstones() {
|
|
4739
4935
|
try {
|
|
4740
4936
|
await this.tombstones.confirm(this.allBackendIds());
|
|
4741
4937
|
} catch (err) {
|
|
4742
|
-
|
|
4938
|
+
log4.warn(`[AppDataGroup:${this.groupId}] tombstone confirmations failed:`, err);
|
|
4743
4939
|
}
|
|
4744
4940
|
}
|
|
4745
4941
|
/** Write a tombstone for `filePath`. Records metadata only — it never creates the file. */
|
|
@@ -4772,7 +4968,7 @@ var AppDataGroupImpl = class {
|
|
|
4772
4968
|
}
|
|
4773
4969
|
await removeDirectoryTree(this.localFS, normalized);
|
|
4774
4970
|
this.schedulePostDeleteSync();
|
|
4775
|
-
|
|
4971
|
+
log4.log(`[AppDataGroup:${this.groupId}] deleteFile(directory): ${normalized} (${files.length} file(s))`);
|
|
4776
4972
|
return;
|
|
4777
4973
|
}
|
|
4778
4974
|
await this.writeTombstoneFor(normalized);
|
|
@@ -4811,7 +5007,7 @@ var AppDataGroupImpl = class {
|
|
|
4811
5007
|
try {
|
|
4812
5008
|
await this.syncEngine.syncAll();
|
|
4813
5009
|
} catch (err) {
|
|
4814
|
-
|
|
5010
|
+
log4.warn(`[AppDataGroup:${this.groupId}] sync after conflict resolve failed:`, err);
|
|
4815
5011
|
}
|
|
4816
5012
|
}
|
|
4817
5013
|
readConflictBackup(conflictId, fileType) {
|
|
@@ -4846,7 +5042,7 @@ var AppDataGroupImpl = class {
|
|
|
4846
5042
|
if (this.dataBackends.has(id)) {
|
|
4847
5043
|
throw new Error(`Backend "${id}" already exists in data group "${this.groupId}"`);
|
|
4848
5044
|
}
|
|
4849
|
-
|
|
5045
|
+
log4.log(`[AppDataGroup:${this.groupId}] addBackend: creating ${id} (${type})...`);
|
|
4850
5046
|
const instance = await createBackend({ type, options });
|
|
4851
5047
|
const syncable = backendToSyncableFS(instance, `${type}(${id})`);
|
|
4852
5048
|
const localSyncable = backendToSyncableFS(this.localFS, `local(${this.groupId})`);
|
|
@@ -4874,13 +5070,13 @@ var AppDataGroupImpl = class {
|
|
|
4874
5070
|
if (this.features.conflicts) {
|
|
4875
5071
|
this.syncEngine.on(pair.pairId, "conflict", (event) => {
|
|
4876
5072
|
this.archiveConflict(event).catch((err) => {
|
|
4877
|
-
|
|
5073
|
+
log4.warn(`[AppDataGroup:${this.groupId}] conflict archive failed:`, err);
|
|
4878
5074
|
});
|
|
4879
5075
|
});
|
|
4880
5076
|
}
|
|
4881
5077
|
this.trackSyncErrors(id, pair.pairId);
|
|
4882
5078
|
this._backends = [...this._backends.filter((b) => b.id !== id), desc];
|
|
4883
|
-
|
|
5079
|
+
log4.log(`[AppDataGroup:${this.groupId}] addBackend: ${id} (${type}) connected, pair=${pair.pairId}`);
|
|
4884
5080
|
try {
|
|
4885
5081
|
const stale = (await this.tombstones.list()).find(
|
|
4886
5082
|
(t) => t.path === backendMetaFilePath(id)
|
|
@@ -4888,12 +5084,12 @@ var AppDataGroupImpl = class {
|
|
|
4888
5084
|
if (stale) await this.tombstones.remove(stale);
|
|
4889
5085
|
await writeBackendMetaDescriptor(this.localFS, desc);
|
|
4890
5086
|
} catch (err) {
|
|
4891
|
-
|
|
5087
|
+
log4.warn(`[AppDataGroup:${this.groupId}] addBackend: failed to write .meta/backends/${id}.json:`, err);
|
|
4892
5088
|
}
|
|
4893
5089
|
try {
|
|
4894
5090
|
await this.syncEngine.sync(pair.pairId);
|
|
4895
5091
|
} catch (err) {
|
|
4896
|
-
|
|
5092
|
+
log4.warn(`[AppDataGroup:${this.groupId}] addBackend: initial sync failed for ${id}:`, err);
|
|
4897
5093
|
}
|
|
4898
5094
|
this.syncEngine.watch(pair.pairId);
|
|
4899
5095
|
await this.parent.updateAppDataGroupDescriptor(this.groupId, this._backends, {
|
|
@@ -4924,7 +5120,7 @@ var AppDataGroupImpl = class {
|
|
|
4924
5120
|
if (backend?.instance?.dispose) {
|
|
4925
5121
|
await backend.instance.dispose();
|
|
4926
5122
|
}
|
|
4927
|
-
|
|
5123
|
+
log4.log(`[AppDataGroup:${this.groupId}] removeBackend: ${id} removed`);
|
|
4928
5124
|
}
|
|
4929
5125
|
listBackends() {
|
|
4930
5126
|
return Array.from(this.dataBackends.values()).map((b) => b.desc);
|
|
@@ -4966,44 +5162,44 @@ async function createConfigRepo(appId, options = {}) {
|
|
|
4966
5162
|
idbStoreName: options.idbStoreName,
|
|
4967
5163
|
folderPath: options.folderPath
|
|
4968
5164
|
});
|
|
4969
|
-
|
|
5165
|
+
log4.log(`[createConfigRepo] Creating local primary (type: ${localPrimary.type})...`);
|
|
4970
5166
|
const primaryInstance = await createBackend(localPrimary);
|
|
4971
5167
|
const cachedFS = primaryInstance;
|
|
4972
5168
|
if (options.purgeMtimeSidecars !== false) {
|
|
4973
5169
|
try {
|
|
4974
5170
|
const purged = await purgeMtimeSidecars(cachedFS);
|
|
4975
5171
|
if (purged.removed.length > 0) {
|
|
4976
|
-
|
|
5172
|
+
log4.warn(
|
|
4977
5173
|
`[createConfigRepo] purged ${purged.removed.length} leaked .mtime sidecar(s) from local primary:`,
|
|
4978
5174
|
purged.removed
|
|
4979
5175
|
);
|
|
4980
5176
|
}
|
|
4981
5177
|
} catch (err) {
|
|
4982
|
-
|
|
5178
|
+
log4.warn(`[createConfigRepo] .mtime purge failed:`, err?.message ?? err);
|
|
4983
5179
|
}
|
|
4984
5180
|
}
|
|
4985
5181
|
if (options.purgeKeepFiles !== false) {
|
|
4986
5182
|
try {
|
|
4987
5183
|
const purged = await purgeKeepFiles(cachedFS);
|
|
4988
5184
|
if (purged.removed.length > 0) {
|
|
4989
|
-
|
|
5185
|
+
log4.warn(
|
|
4990
5186
|
`[createConfigRepo] purged ${purged.removed.length} leaked .keep placeholder(s) from local primary:`,
|
|
4991
5187
|
purged.removed
|
|
4992
5188
|
);
|
|
4993
5189
|
}
|
|
4994
5190
|
} catch (err) {
|
|
4995
|
-
|
|
5191
|
+
log4.warn(`[createConfigRepo] .keep purge failed:`, err?.message ?? err);
|
|
4996
5192
|
}
|
|
4997
5193
|
}
|
|
4998
5194
|
const cacheOptions = options.cache === false ? void 0 : options.cache ?? {};
|
|
4999
5195
|
try {
|
|
5000
5196
|
await primaryInstance.mkdir(META_DIR);
|
|
5001
|
-
|
|
5197
|
+
log4.log(`[createConfigRepo] /.meta/ ready`);
|
|
5002
5198
|
} catch (err) {
|
|
5003
5199
|
const msg = err.message || "";
|
|
5004
5200
|
if (msg.includes("File exists") || msg.includes("EEXIST") || err.code === "EEXIST") {
|
|
5005
5201
|
} else {
|
|
5006
|
-
|
|
5202
|
+
log4.error(`[createConfigRepo] Failed to ensure /.meta/:`, err.message);
|
|
5007
5203
|
}
|
|
5008
5204
|
}
|
|
5009
5205
|
try {
|
|
@@ -5014,7 +5210,7 @@ async function createConfigRepo(appId, options = {}) {
|
|
|
5014
5210
|
} catch {
|
|
5015
5211
|
await ensureParentDir2(primaryInstance, path);
|
|
5016
5212
|
await primaryInstance.writeFile(path, groupTypeBytes);
|
|
5017
|
-
|
|
5213
|
+
log4.log(`[createConfigRepo] group-type set to "config-sync" at ${path}`);
|
|
5018
5214
|
}
|
|
5019
5215
|
}
|
|
5020
5216
|
if (!await readGroupDescriptor(primaryInstance)) {
|
|
@@ -5026,7 +5222,7 @@ async function createConfigRepo(appId, options = {}) {
|
|
|
5026
5222
|
});
|
|
5027
5223
|
}
|
|
5028
5224
|
} catch (err) {
|
|
5029
|
-
|
|
5225
|
+
log4.warn(`[createConfigRepo] Failed to write group metadata:`, err.message);
|
|
5030
5226
|
}
|
|
5031
5227
|
const tempRepo = new ConfigRepo(
|
|
5032
5228
|
appId,
|
|
@@ -5040,11 +5236,11 @@ async function createConfigRepo(appId, options = {}) {
|
|
|
5040
5236
|
);
|
|
5041
5237
|
const oldBackendsMeta = await tempRepo.readMetaFile(BACKENDS_FILE);
|
|
5042
5238
|
if (oldBackendsMeta && oldBackendsMeta.backends?.length > 0) {
|
|
5043
|
-
|
|
5239
|
+
log4.log(`[createConfigRepo] Migrating ${oldBackendsMeta.backends.length} backend(s) from backends.json to individual files...`);
|
|
5044
5240
|
await tempRepo.ensureDir(`${BACKENDS_DIR}/.keep`);
|
|
5045
5241
|
for (const desc of oldBackendsMeta.backends) {
|
|
5046
5242
|
if (desc.id === LOCAL_IDB_BACKEND_ID) {
|
|
5047
|
-
|
|
5243
|
+
log4.log(`[createConfigRepo] Skipping local backend ${desc.id} during migration`);
|
|
5048
5244
|
continue;
|
|
5049
5245
|
}
|
|
5050
5246
|
await tempRepo.writeBackendDescriptor(desc);
|
|
@@ -5062,7 +5258,7 @@ async function createConfigRepo(appId, options = {}) {
|
|
|
5062
5258
|
}
|
|
5063
5259
|
}
|
|
5064
5260
|
}
|
|
5065
|
-
|
|
5261
|
+
log4.log(`[createConfigRepo] Migration complete`);
|
|
5066
5262
|
}
|
|
5067
5263
|
let allBackends = await tempRepo.readAllBackendDescriptors();
|
|
5068
5264
|
if (options.backendInfo) {
|
|
@@ -5080,19 +5276,19 @@ async function createConfigRepo(appId, options = {}) {
|
|
|
5080
5276
|
type: options.backendInfo.type,
|
|
5081
5277
|
options: options.backendInfo.options
|
|
5082
5278
|
});
|
|
5083
|
-
|
|
5279
|
+
log4.log(`[createConfigRepo] Added replica backend: ${replicaId} (${options.backendInfo.type})`);
|
|
5084
5280
|
allBackends = [...allBackends, { id: replicaId, type: options.backendInfo.type, options: options.backendInfo.options }];
|
|
5085
5281
|
} else if (dupConfig) {
|
|
5086
|
-
|
|
5282
|
+
log4.log(`[createConfigRepo] Replica with same config already registered as "${dupConfig.id}", skipping`);
|
|
5087
5283
|
} else {
|
|
5088
|
-
|
|
5284
|
+
log4.log(`[createConfigRepo] Replica ${replicaId} already registered`);
|
|
5089
5285
|
}
|
|
5090
5286
|
}
|
|
5091
|
-
|
|
5287
|
+
log4.log(`[createConfigRepo] Replica backends: ${allBackends.map((b) => b.id).join(", ") || "(none)"}`);
|
|
5092
5288
|
let nodeId = options.nodeId;
|
|
5093
5289
|
if (!nodeId) {
|
|
5094
5290
|
nodeId = `node-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
|
5095
|
-
|
|
5291
|
+
log4.log(`[createConfigRepo] Generated nodeId: ${nodeId}`);
|
|
5096
5292
|
}
|
|
5097
5293
|
const serializer = createSerializerChain(options.serializer);
|
|
5098
5294
|
const repo = new ConfigRepo(
|
|
@@ -5115,19 +5311,19 @@ async function createConfigRepo(appId, options = {}) {
|
|
|
5115
5311
|
try {
|
|
5116
5312
|
const mig = await repo.migrateLegacyVersionSidecars();
|
|
5117
5313
|
if (mig.renamed.length === 0 && mig.deleted.length === 0 && mig.remoteDeleted.length === 0) {
|
|
5118
|
-
|
|
5314
|
+
log4.log("[createConfigRepo] no legacy version sidecars to migrate");
|
|
5119
5315
|
}
|
|
5120
5316
|
} catch (err) {
|
|
5121
|
-
|
|
5317
|
+
log4.warn(`[createConfigRepo] version sidecar migration failed:`, err?.message ?? err);
|
|
5122
5318
|
}
|
|
5123
5319
|
}
|
|
5124
5320
|
await repo.load();
|
|
5125
5321
|
if (repo.replicaCount > 0) {
|
|
5126
|
-
|
|
5322
|
+
log4.log("[createConfigRepo] Starting background initial sync + dedup...");
|
|
5127
5323
|
repo.startBackgroundSync();
|
|
5128
5324
|
}
|
|
5129
5325
|
repo.syncMetaToReplicas().catch((err) => {
|
|
5130
|
-
|
|
5326
|
+
log4.error("[createConfigRepo] background syncMetaToReplicas failed:", err);
|
|
5131
5327
|
});
|
|
5132
5328
|
return repo;
|
|
5133
5329
|
}
|
|
@@ -5141,11 +5337,11 @@ function sidecarOwnerPath(path) {
|
|
|
5141
5337
|
|
|
5142
5338
|
// src/connect.ts
|
|
5143
5339
|
init_backend_registry();
|
|
5144
|
-
var
|
|
5340
|
+
var log5 = createLogger2("connect");
|
|
5145
5341
|
var GROUP_TYPE_FILE2 = "/.config/group-type";
|
|
5146
5342
|
var LEGACY_GROUP_TYPE_FILE2 = "/.meta/group-type";
|
|
5147
5343
|
async function detectGroupType(type, options) {
|
|
5148
|
-
|
|
5344
|
+
log5(`detectGroupType: connecting to ${type}...`);
|
|
5149
5345
|
const tempBackend = await createBackend({ type, options });
|
|
5150
5346
|
try {
|
|
5151
5347
|
for (const path of [GROUP_TYPE_FILE2, LEGACY_GROUP_TYPE_FILE2]) {
|
|
@@ -5153,15 +5349,15 @@ async function detectGroupType(type, options) {
|
|
|
5153
5349
|
const raw = await tempBackend.readFile(path, "utf-8");
|
|
5154
5350
|
const groupType = raw.trim();
|
|
5155
5351
|
if (groupType === "config-sync" || groupType === "data-sync") {
|
|
5156
|
-
|
|
5352
|
+
log5(`detectGroupType: detected "${groupType}" at ${path}`);
|
|
5157
5353
|
return groupType;
|
|
5158
5354
|
}
|
|
5159
|
-
|
|
5355
|
+
log5(`detectGroupType: unknown group-type value "${groupType}" at ${path}, treating as null`);
|
|
5160
5356
|
return null;
|
|
5161
5357
|
} catch {
|
|
5162
5358
|
}
|
|
5163
5359
|
}
|
|
5164
|
-
|
|
5360
|
+
log5(`detectGroupType: no group-type file found (new backend)`);
|
|
5165
5361
|
return null;
|
|
5166
5362
|
} finally {
|
|
5167
5363
|
if (tempBackend?.dispose) {
|
|
@@ -5170,7 +5366,7 @@ async function detectGroupType(type, options) {
|
|
|
5170
5366
|
}
|
|
5171
5367
|
}
|
|
5172
5368
|
async function connect(appId, options = {}) {
|
|
5173
|
-
|
|
5369
|
+
log5(`connect: appId=${appId}`);
|
|
5174
5370
|
if (!options.backendInfo) {
|
|
5175
5371
|
const repo2 = await createConfigRepo(appId, {
|
|
5176
5372
|
idbStoreName: options.idbStoreName,
|
|
@@ -5188,12 +5384,12 @@ async function connect(appId, options = {}) {
|
|
|
5188
5384
|
try {
|
|
5189
5385
|
appDataGroups2.push(await repo2.getAppDataGroup(d.id));
|
|
5190
5386
|
} catch (err) {
|
|
5191
|
-
|
|
5387
|
+
log5(`connect: failed to load app data group "${d.id}":`, err);
|
|
5192
5388
|
}
|
|
5193
5389
|
}
|
|
5194
5390
|
const dataGroup2 = appDataGroups2[0];
|
|
5195
5391
|
if (!dataGroup2 && descriptors2.length > 0) {
|
|
5196
|
-
|
|
5392
|
+
log5(
|
|
5197
5393
|
`connect: ${descriptors2.length} data group descriptor(s) found but none could be loaded; leaving them untouched (no empty group is created)`
|
|
5198
5394
|
);
|
|
5199
5395
|
}
|
|
@@ -5218,7 +5414,7 @@ async function connect(appId, options = {}) {
|
|
|
5218
5414
|
groupType = detectedType;
|
|
5219
5415
|
} else {
|
|
5220
5416
|
groupType = options.groupType ?? "config-sync";
|
|
5221
|
-
|
|
5417
|
+
log5(`connect: new backend, using groupType="${groupType}"`);
|
|
5222
5418
|
}
|
|
5223
5419
|
const repo = await createConfigRepo(
|
|
5224
5420
|
appId,
|
|
@@ -5247,14 +5443,14 @@ async function connect(appId, options = {}) {
|
|
|
5247
5443
|
try {
|
|
5248
5444
|
appDataGroups.push(await repo.getAppDataGroup(d.id));
|
|
5249
5445
|
} catch (err) {
|
|
5250
|
-
|
|
5446
|
+
log5(`connect: failed to load app data group "${d.id}":`, err);
|
|
5251
5447
|
}
|
|
5252
5448
|
}
|
|
5253
5449
|
let dataGroup = appDataGroups[0];
|
|
5254
5450
|
if (groupType === "data-sync") {
|
|
5255
5451
|
if (!dataGroup) {
|
|
5256
5452
|
if (descriptors.length > 0) {
|
|
5257
|
-
|
|
5453
|
+
log5(
|
|
5258
5454
|
`connect: ${descriptors.length} data group descriptor(s) exist but could not be loaded; refusing to create/overwrite "default"`
|
|
5259
5455
|
);
|
|
5260
5456
|
} else {
|