zen-fs-config 0.6.3 → 0.7.0

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.js CHANGED
@@ -1717,12 +1717,13 @@ function mergeTombstone(byPath, tombstone) {
1717
1717
  }
1718
1718
  async function readTombstonesFromFs(fs, warn) {
1719
1719
  const byPath = /* @__PURE__ */ new Map();
1720
- for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
1720
+ {
1721
+ const dir = TOMBSTONES_DIR;
1721
1722
  let entries;
1722
1723
  try {
1723
1724
  entries = await fs.readdir(dir);
1724
1725
  } catch {
1725
- continue;
1726
+ return [];
1726
1727
  }
1727
1728
  for (const entry of entries) {
1728
1729
  if (!entry.endsWith(".json")) continue;
@@ -1818,43 +1819,34 @@ var TombstoneStore = class {
1818
1819
  }
1819
1820
  fs;
1820
1821
  author;
1821
- pathFor(filePath, dir = TOMBSTONES_DIR) {
1822
- return `${dir}/${encodeMetaPath(filePath)}.json`;
1822
+ pathFor(filePath) {
1823
+ return `${TOMBSTONES_DIR}/${encodeMetaPath(filePath)}.json`;
1823
1824
  }
1824
- /** All tombstones, new path first, legacy only for paths not already seen. */
1825
+ /** All tombstones. */
1825
1826
  async list() {
1826
1827
  return readTombstonesFromFs(this.fs, (message) => log2.warn(message));
1827
1828
  }
1828
- /** Write a tombstone (new + legacy mirror). */
1829
+ /** Write a tombstone. */
1829
1830
  async write(tombstone) {
1830
1831
  const bytes = new TextEncoder().encode(JSON.stringify(tombstone, null, 2));
1831
- const paths = [
1832
- this.pathFor(tombstone.path),
1833
- this.pathFor(tombstone.path, LEGACY_TOMBSTONES_DIR)
1834
- ];
1835
- for (const p of paths) {
1836
- await ensureParentDir2(this.fs, p);
1837
- await this.fs.writeFile(p, bytes);
1838
- }
1832
+ const p = this.pathFor(tombstone.path);
1833
+ await ensureParentDir2(this.fs, p);
1834
+ await this.fs.writeFile(p, bytes);
1839
1835
  }
1840
- /** Persist an updated tombstone (confirmations) to whichever paths exist. */
1836
+ /** Persist an updated tombstone (confirmations) when it still exists. */
1841
1837
  async save(tombstone) {
1842
1838
  const bytes = new TextEncoder().encode(JSON.stringify(tombstone, null, 2));
1843
- for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
1844
- const p = this.pathFor(tombstone.path, dir);
1845
- if (!await existsSafe(this.fs, p)) continue;
1846
- await this.fs.writeFile(p, bytes);
1847
- }
1839
+ const p = this.pathFor(tombstone.path);
1840
+ if (!await existsSafe(this.fs, p)) return;
1841
+ await this.fs.writeFile(p, bytes);
1848
1842
  }
1849
- /** Delete a tombstone from both layouts. */
1843
+ /** Delete a tombstone. */
1850
1844
  async remove(tombstone) {
1851
- for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
1852
- const p = this.pathFor(tombstone.path, dir);
1853
- if (!await existsSafe(this.fs, p)) continue;
1854
- try {
1855
- await this.fs.unlink(p);
1856
- } catch {
1857
- }
1845
+ const p = this.pathFor(tombstone.path);
1846
+ if (!await existsSafe(this.fs, p)) return;
1847
+ try {
1848
+ await this.fs.unlink(p);
1849
+ } catch {
1858
1850
  }
1859
1851
  }
1860
1852
  /**
@@ -1898,28 +1890,26 @@ var TombstoneStore = class {
1898
1890
  }
1899
1891
  return modified;
1900
1892
  }
1901
- /** Remove tombstones confirmed by every connected backend (and their mirrors). */
1893
+ /** Remove tombstones confirmed by every connected backend. */
1902
1894
  async gc(allBackendIds, targets = []) {
1903
1895
  const tombstones = await this.list();
1904
1896
  for (const tombstone of tombstones) {
1905
1897
  const allConfirmed = allBackendIds.every((id) => tombstone.confirmedBy.includes(id));
1906
1898
  if (!allConfirmed) continue;
1907
- for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
1908
- const p = this.pathFor(tombstone.path, dir);
1909
- if (await existsSafe(this.fs, p)) {
1899
+ const p = this.pathFor(tombstone.path);
1900
+ if (await existsSafe(this.fs, p)) {
1901
+ try {
1902
+ await this.fs.unlink(p);
1903
+ } catch {
1904
+ }
1905
+ }
1906
+ for (const target of targets) {
1907
+ if (await existsSafe(target.fs, p)) {
1910
1908
  try {
1911
- await this.fs.unlink(p);
1909
+ await target.fs.unlink(p);
1912
1910
  } catch {
1913
1911
  }
1914
1912
  }
1915
- for (const target of targets) {
1916
- if (await existsSafe(target.fs, p)) {
1917
- try {
1918
- await target.fs.unlink(p);
1919
- } catch {
1920
- }
1921
- }
1922
- }
1923
1913
  }
1924
1914
  }
1925
1915
  }
@@ -2060,8 +2050,8 @@ var GROUP_DESCRIPTOR_FILE2 = `${META_DIR}/group.json`;
2060
2050
  var GROUP_TYPE_FILE = `${CONFIG_DIR}/group-type`;
2061
2051
  var LEGACY_GROUP_TYPE_FILE = `${META_DIR}/group-type`;
2062
2052
  var BACKENDS_FILE = `${META_DIR}/backends.json`;
2063
- var BACKENDS_DIR = `${CONFIG_DIR}/backends`;
2064
- var LEGACY_BACKENDS_DIR = META_BACKENDS_DIR;
2053
+ var BACKENDS_DIR = META_BACKENDS_DIR;
2054
+ var LEGACY_BACKENDS_DIR = `${CONFIG_DIR}/backends`;
2065
2055
  var APP_DATA_GROUPS_DIR = `${CONFIG_DIR}/app-data-groups`;
2066
2056
  var LEGACY_APP_DATA_GROUPS_DIR = `${META_DIR}/app-data-groups`;
2067
2057
  var CONFIG_SYNC_GROUP_FILE = `${CONFIG_DIR}/config-sync-group.json`;
@@ -2447,9 +2437,9 @@ var ConfigRepo = class {
2447
2437
  // Tombstone (Deletion Tracking)
2448
2438
  // -----------------------------------------------------------------------
2449
2439
  /**
2450
- * Write the tombstone record for `filePath` (new `.meta/tombstones` + legacy
2451
- * mirror for one release cycle). Records only metadata — it never creates
2452
- * the deleted file locally.
2440
+ * Write the tombstone record for `filePath` under `.meta/tombstones/`.
2441
+ * Records only metadata — it never creates the deleted file locally, and the
2442
+ * legacy `/.meta/.deleted/` archive is neither read nor written.
2453
2443
  */
2454
2444
  async writeTombstoneFor(filePath) {
2455
2445
  const normalizedPath = filePath.startsWith("/") ? filePath : "/" + filePath;
@@ -2460,11 +2450,9 @@ var ConfigRepo = class {
2460
2450
  confirmedBy: [this.primaryBackendId]
2461
2451
  };
2462
2452
  const tombstoneBytes = new TextEncoder().encode(JSON.stringify(tombstone, null, 2));
2463
- for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
2464
- const tombstonePath = tombstonePathFor(normalizedPath, dir);
2465
- await this.ensureDir(tombstonePath);
2466
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
2467
- }
2453
+ const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
2454
+ await this.ensureDir(tombstonePath);
2455
+ await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
2468
2456
  }
2469
2457
  /**
2470
2458
  * Delete a file and write a tombstone so the deletion propagates
@@ -2584,11 +2572,16 @@ var ConfigRepo = class {
2584
2572
  const isKeepPlaceholder = tombstone.path === "/.keep" || tombstone.path.endsWith("/.keep");
2585
2573
  const forceDelete = isMtimeSidecar(tombstone.path) || isKeepPlaceholder;
2586
2574
  const tVersionPath = forceDelete ? null : versionPathFor(tombstone.path);
2575
+ const sidecarOwner = sidecarOwnerPath(tombstone.path);
2576
+ const isKeepSidecar = sidecarOwner === "/.keep" || (sidecarOwner?.endsWith("/.keep") ?? false);
2577
+ const liveSidecarCandidate = sidecarOwner !== null && !isKeepSidecar;
2587
2578
  if (forceDelete) {
2588
- try {
2589
- await this.cachedFS.unlink(tombstone.path);
2590
- processed++;
2591
- } catch {
2579
+ if (!(liveSidecarCandidate && await this.pathExistsOnBackend(this.cachedFS, sidecarOwner))) {
2580
+ try {
2581
+ await this.cachedFS.unlink(tombstone.path);
2582
+ processed++;
2583
+ } catch {
2584
+ }
2592
2585
  }
2593
2586
  } else {
2594
2587
  const existedOnPrimary = await this.pathExistsOnBackend(this.cachedFS, tombstone.path);
@@ -2602,13 +2595,18 @@ var ConfigRepo = class {
2602
2595
  for (const [replicaId, replica] of this.replicaBackends) {
2603
2596
  let gone;
2604
2597
  if (forceDelete) {
2605
- try {
2606
- await replica.instance.unlink(tombstone.path);
2598
+ if (liveSidecarCandidate && await this.pathExistsOnBackend(replica.instance, sidecarOwner)) {
2607
2599
  gone = true;
2608
- } catch (err) {
2609
- gone = isNotFoundError(err);
2600
+ alreadyDeleted++;
2601
+ } else {
2602
+ try {
2603
+ await replica.instance.unlink(tombstone.path);
2604
+ gone = true;
2605
+ } catch (err) {
2606
+ gone = isNotFoundError(err);
2607
+ }
2608
+ if (gone) processed++;
2610
2609
  }
2611
- if (gone) processed++;
2612
2610
  } else {
2613
2611
  const existed = await this.pathExistsOnBackend(replica.instance, tombstone.path);
2614
2612
  gone = await this.purgePathOnBackend(replica.instance, tombstone.path);
@@ -2899,11 +2897,9 @@ var ConfigRepo = class {
2899
2897
  if (changed) {
2900
2898
  try {
2901
2899
  const bytes = new TextEncoder().encode(JSON.stringify(tombstone, null, 2));
2902
- for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
2903
- const tombstonePath = tombstonePathFor(tombstone.path, dir);
2904
- if (await this.safeExists(this.cachedFS, tombstonePath)) {
2905
- await this.cachedFS.writeFile(tombstonePath, bytes);
2906
- }
2900
+ const tombstonePath = tombstonePathFor(tombstone.path, TOMBSTONES_DIR);
2901
+ if (await this.safeExists(this.cachedFS, tombstonePath)) {
2902
+ await this.cachedFS.writeFile(tombstonePath, bytes);
2907
2903
  }
2908
2904
  modified++;
2909
2905
  } catch {
@@ -2951,22 +2947,20 @@ var ConfigRepo = class {
2951
2947
  for (const tombstone of tombstones) {
2952
2948
  const allConfirmed = await this.allReplicasConfirmAbsent(tombstone.path);
2953
2949
  if (allConfirmed) {
2954
- for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
2955
- const tombstonePath = tombstonePathFor(tombstone.path, dir);
2956
- try {
2957
- await this.cachedFS.unlink(tombstonePath);
2958
- } catch {
2959
- }
2960
- for (const [, replica] of this.replicaBackends) {
2961
- if (await this.safeExists(replica.instance, tombstonePath)) {
2962
- try {
2963
- await replica.instance.unlink(tombstonePath);
2964
- } catch {
2965
- }
2950
+ const tombstonePath = tombstonePathFor(tombstone.path, TOMBSTONES_DIR);
2951
+ try {
2952
+ await this.cachedFS.unlink(tombstonePath);
2953
+ } catch {
2954
+ }
2955
+ for (const [, replica] of this.replicaBackends) {
2956
+ if (await this.safeExists(replica.instance, tombstonePath)) {
2957
+ try {
2958
+ await replica.instance.unlink(tombstonePath);
2959
+ } catch {
2966
2960
  }
2967
2961
  }
2968
- log3.log(`[ConfigRepo] gcTombstones: removed ${tombstonePath} (all ${allBackendIds.length} backends confirmed)`);
2969
2962
  }
2963
+ log3.log(`[ConfigRepo] gcTombstones: removed ${tombstonePath} (all ${allBackendIds.length} backends confirmed)`);
2970
2964
  }
2971
2965
  }
2972
2966
  }
@@ -3030,7 +3024,7 @@ var ConfigRepo = class {
3030
3024
  this.assertNotDisposed();
3031
3025
  return [...this.backendDescriptorCache];
3032
3026
  }
3033
- /** 配置组的权威后端列表(`.config/backends/`),与 `listBackends()` 相同。 */
3027
+ /** 配置组的权威后端列表(`.meta/backends/`),与 `listBackends()` 相同。 */
3034
3028
  listDeclaredBackends() {
3035
3029
  return this.listBackends();
3036
3030
  }
@@ -3423,11 +3417,15 @@ var ConfigRepo = class {
3423
3417
  // -----------------------------------------------------------------------
3424
3418
  // Internal — Individual Backend Descriptor Files
3425
3419
  // -----------------------------------------------------------------------
3426
- /** Path for a single backend descriptor: .config/backends/{id}.json */
3420
+ /** Path for a single backend descriptor: `.meta/backends/{id}.json`. */
3427
3421
  backendFilePath(id) {
3428
3422
  return `${BACKENDS_DIR}/${id}.json`;
3429
3423
  }
3430
- /** Legacy path: .meta/backends/{id}.json (read + dual-write for one cycle). */
3424
+ /**
3425
+ * Legacy pre-0.7 path `/.config/backends/{id}.json`.
3426
+ *
3427
+ * 该目录已不再读取:此处只用于删除后端时尽力清理残留副本。
3428
+ */
3431
3429
  legacyBackendFilePath(id) {
3432
3430
  return `${LEGACY_BACKENDS_DIR}/${id}.json`;
3433
3431
  }
@@ -3476,8 +3474,11 @@ var ConfigRepo = class {
3476
3474
  return { items, corruptFiles };
3477
3475
  }
3478
3476
  /**
3479
- * Read all backend descriptors from `.config/backends/*.json` with legacy
3480
- * `.meta/backends/*.json` as a one-cycle fallback (new layout wins by id).
3477
+ * Read all backend descriptors from `.meta/backends/*.json` — the single
3478
+ * authoritative location for both config-sync and data-sync groups.
3479
+ *
3480
+ * `/.config/backends/` is intentionally **not** read: leftovers there are
3481
+ * plain content and never take part in backend enumeration.
3481
3482
  *
3482
3483
  * If duplicate backends are detected (same type + options but different id),
3483
3484
  * only the first one (sorted by id) is kept and the rest are removed
@@ -3485,16 +3486,8 @@ var ConfigRepo = class {
3485
3486
  */
3486
3487
  async readAllBackendDescriptors() {
3487
3488
  const fresh = await this.readBackendDescriptorsFrom(BACKENDS_DIR);
3488
- const legacy = await this.readBackendDescriptorsFrom(LEGACY_BACKENDS_DIR);
3489
3489
  const items = [...fresh.items];
3490
- const seenIds = new Set(items.map((i) => i.desc.id));
3491
- for (const item of legacy.items) {
3492
- if (!seenIds.has(item.desc.id)) {
3493
- items.push(item);
3494
- seenIds.add(item.desc.id);
3495
- }
3496
- }
3497
- const corruptFiles = [...fresh.corruptFiles, ...legacy.corruptFiles];
3490
+ const corruptFiles = [...fresh.corruptFiles];
3498
3491
  for (const corruptPath of corruptFiles) {
3499
3492
  for (const [, replica] of this.replicaBackends) {
3500
3493
  try {
@@ -3559,20 +3552,18 @@ var ConfigRepo = class {
3559
3552
  return result;
3560
3553
  }
3561
3554
  /**
3562
- * Write a single backend descriptor to `.config/backends/{id}.json`,
3563
- * mirroring to the legacy `.meta/backends/{id}.json` for one release cycle
3564
- * so older clients still see the backend.
3555
+ * Write a single backend descriptor to `.meta/backends/{id}.json`.
3565
3556
  *
3566
- * Both files use the canonical serializer and are only written when their
3567
- * bytes actually differ (no needless sync churn).
3557
+ * Uses the canonical serializer and is only written when the bytes actually
3558
+ * differ (no needless sync churn). `/.config/backends/` is never written.
3568
3559
  */
3569
3560
  async writeBackendDescriptor(desc) {
3570
3561
  assertValidBackendId(desc.id);
3571
3562
  const author = `${this.appId}/${this.nodeId}`;
3572
3563
  const text = serializeBackendDescriptor(desc);
3573
3564
  const bytes = new TextEncoder().encode(text);
3574
- for (const path of [this.backendFilePath(desc.id), this.legacyBackendFilePath(desc.id)]) {
3575
- if (await this.readTextIfExists(path) === text) continue;
3565
+ const path = this.backendFilePath(desc.id);
3566
+ if (await this.readTextIfExists(path) !== text) {
3576
3567
  await this.ensureDir(path);
3577
3568
  await this.cachedFS.writeFile(path, bytes);
3578
3569
  const version = await incrementVersion(this.fullFS, path, bytes, author);
@@ -3582,7 +3573,10 @@ var ConfigRepo = class {
3582
3573
  if (existing >= 0) this.backendDescriptorCache[existing] = { ...desc };
3583
3574
  else this.backendDescriptorCache.push({ ...desc });
3584
3575
  }
3585
- /** Remove a single backend descriptor file + its version sidecar (both layouts) */
3576
+ /**
3577
+ * Remove a single backend descriptor: always `.meta/backends/{id}.json`, plus
3578
+ * a best-effort cleanup of the legacy `/.config/backends/{id}.json` residue.
3579
+ */
3586
3580
  async removeBackendDescriptor(id) {
3587
3581
  for (const path of [this.backendFilePath(id), this.legacyBackendFilePath(id)]) {
3588
3582
  try {
@@ -3869,8 +3863,9 @@ var ConfigRepo = class {
3869
3863
  return `${LEGACY_APP_DATA_GROUPS_DIR}/${this.appId}/${id}.json`;
3870
3864
  }
3871
3865
  /**
3872
- * Write a data-group descriptor to `.config/` (bootstrap copy), mirroring to
3873
- * the legacy `.meta/` path.
3866
+ * Write a data-group descriptor to `.config/app-data-groups/` (registration
3867
+ * + bootstrap copy). The legacy `.meta/app-data-groups/` mirror is no longer
3868
+ * written; it is still read so old layouts keep working.
3874
3869
  *
3875
3870
  * 这份描述符是**引导副本**:它让只有配置组凭据的新设备知道有哪些数据组、
3876
3871
  * 远端在哪。权威定义在数据组自己的 `.meta/backends/`。
@@ -3883,13 +3878,12 @@ var ConfigRepo = class {
3883
3878
  const text = serializeAppDataGroupDescriptor(mergeDescriptorCreatedAt(descriptor, existing));
3884
3879
  const bytes = new TextEncoder().encode(text);
3885
3880
  const author = `${this.appId}/${this.nodeId}`;
3886
- for (const descPath of [this.appDataGroupFilePath(id), this.legacyAppDataGroupFilePath(id)]) {
3887
- if (await this.readTextIfExists(descPath) === text) continue;
3888
- await this.ensureDir(descPath);
3889
- await this.cachedFS.writeFile(descPath, bytes);
3890
- const version = await incrementVersion(this.fullFS, descPath, bytes, author);
3891
- await this.writeVersionSidecar(descPath, version);
3892
- }
3881
+ const descPath = this.appDataGroupFilePath(id);
3882
+ if (await this.readTextIfExists(descPath) === text) return;
3883
+ await this.ensureDir(descPath);
3884
+ await this.cachedFS.writeFile(descPath, bytes);
3885
+ const version = await incrementVersion(this.fullFS, descPath, bytes, author);
3886
+ await this.writeVersionSidecar(descPath, version);
3893
3887
  }
3894
3888
  /**
3895
3889
  * Resolve a backend descriptor's options by merging account fields
@@ -4950,6 +4944,13 @@ async function createConfigRepo(appId, options = {}) {
4950
4944
  });
4951
4945
  return repo;
4952
4946
  }
4947
+ function sidecarOwnerPath(path) {
4948
+ let owner = null;
4949
+ if (path.endsWith(".mtime")) owner = path.slice(0, -".mtime".length);
4950
+ else if (path.endsWith(".version")) owner = path.slice(0, -".version".length);
4951
+ if (owner && owner.endsWith(".version")) owner = owner.slice(0, -".version".length);
4952
+ return owner === "" ? null : owner;
4953
+ }
4953
4954
 
4954
4955
  // src/connect.ts
4955
4956
  init_backend_registry();