zen-fs-config 0.6.4 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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`;
@@ -2130,6 +2120,45 @@ async function removeDirectoryTree(fs, dir) {
2130
2120
  } catch {
2131
2121
  }
2132
2122
  }
2123
+ async function collectFilesUnderAny(surfaces, dir, onError) {
2124
+ const merged = /* @__PURE__ */ new Set();
2125
+ for (const surface of surfaces) {
2126
+ try {
2127
+ for (const file of await collectFilesUnder(surface.fs, dir)) merged.add(file);
2128
+ } catch (error) {
2129
+ onError(surface.label, error);
2130
+ }
2131
+ }
2132
+ return [...merged];
2133
+ }
2134
+ async function collectDirectoriesUnderStrict(fs, dir, out = []) {
2135
+ out.push(dir);
2136
+ const entries = await fs.readdir(dir);
2137
+ for (const entry of entries) {
2138
+ const child = dir === "/" ? `/${entry}` : `${dir}/${entry}`;
2139
+ if (await isDirectoryPath(fs, child)) await collectDirectoriesUnderStrict(fs, child, out);
2140
+ }
2141
+ return out;
2142
+ }
2143
+ async function collectDirectoriesUnderAny(surfaces, dir, onError) {
2144
+ const merged = /* @__PURE__ */ new Set();
2145
+ for (const surface of surfaces) {
2146
+ const found = [];
2147
+ try {
2148
+ await collectDirectoriesUnderStrict(surface.fs, dir, found);
2149
+ } catch (error) {
2150
+ onError(surface.label, error);
2151
+ }
2152
+ for (const child of found) merged.add(child);
2153
+ }
2154
+ return [...merged];
2155
+ }
2156
+ async function isDirectoryAny(surfaces, path) {
2157
+ for (const surface of surfaces) {
2158
+ if (await isDirectoryPath(surface.fs, path)) return true;
2159
+ }
2160
+ return false;
2161
+ }
2133
2162
  function isNotFoundError(err) {
2134
2163
  const code = err?.code;
2135
2164
  if (code === "ENOENT" || code === "ENOTDIR") return true;
@@ -2447,9 +2476,9 @@ var ConfigRepo = class {
2447
2476
  // Tombstone (Deletion Tracking)
2448
2477
  // -----------------------------------------------------------------------
2449
2478
  /**
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.
2479
+ * Write the tombstone record for `filePath` under `.meta/tombstones/`.
2480
+ * Records only metadata — it never creates the deleted file locally, and the
2481
+ * legacy `/.meta/.deleted/` archive is neither read nor written.
2453
2482
  */
2454
2483
  async writeTombstoneFor(filePath) {
2455
2484
  const normalizedPath = filePath.startsWith("/") ? filePath : "/" + filePath;
@@ -2460,11 +2489,9 @@ var ConfigRepo = class {
2460
2489
  confirmedBy: [this.primaryBackendId]
2461
2490
  };
2462
2491
  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
- }
2492
+ const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
2493
+ await this.ensureDir(tombstonePath);
2494
+ await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
2468
2495
  }
2469
2496
  /**
2470
2497
  * Delete a file and write a tombstone so the deletion propagates
@@ -2473,10 +2500,20 @@ var ConfigRepo = class {
2473
2500
  async deleteFile(path) {
2474
2501
  this.assertNotDisposed();
2475
2502
  const normalizedPath = path.startsWith("/") ? path : "/" + path;
2476
- if (await isDirectoryPath(this.cachedFS, normalizedPath)) {
2477
- const files = await collectFilesUnder(this.cachedFS, normalizedPath);
2503
+ const surfaces = [{ label: "local-cache", fs: this.cachedFS }];
2504
+ for (const [id, replica] of this.replicaBackends) {
2505
+ surfaces.push({ label: id, fs: replica.instance });
2506
+ }
2507
+ const onEnumerateError = (label, error) => {
2508
+ const reason = error?.message ?? String(error);
2509
+ log3.warn(
2510
+ `[ConfigRepo] deleteFile(directory): cannot enumerate "${label}" under ${normalizedPath}: ${reason}`
2511
+ );
2512
+ };
2513
+ if (await isDirectoryAny(surfaces, normalizedPath)) {
2514
+ const files = await collectFilesUnderAny(surfaces, normalizedPath, onEnumerateError);
2478
2515
  for (const file of files) await this.deleteFile(file);
2479
- for (const dir of await collectDirectoriesUnder(this.cachedFS, normalizedPath)) {
2516
+ for (const dir of await collectDirectoriesUnderAny(surfaces, normalizedPath, onEnumerateError)) {
2480
2517
  const keepPath = dir === "/" ? "/.keep" : `${dir}/.keep`;
2481
2518
  await this.writeTombstoneFor(keepPath);
2482
2519
  await this.writeTombstoneFor(`${keepPath}.mtime`);
@@ -2909,11 +2946,9 @@ var ConfigRepo = class {
2909
2946
  if (changed) {
2910
2947
  try {
2911
2948
  const bytes = new TextEncoder().encode(JSON.stringify(tombstone, null, 2));
2912
- for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
2913
- const tombstonePath = tombstonePathFor(tombstone.path, dir);
2914
- if (await this.safeExists(this.cachedFS, tombstonePath)) {
2915
- await this.cachedFS.writeFile(tombstonePath, bytes);
2916
- }
2949
+ const tombstonePath = tombstonePathFor(tombstone.path, TOMBSTONES_DIR);
2950
+ if (await this.safeExists(this.cachedFS, tombstonePath)) {
2951
+ await this.cachedFS.writeFile(tombstonePath, bytes);
2917
2952
  }
2918
2953
  modified++;
2919
2954
  } catch {
@@ -2961,22 +2996,20 @@ var ConfigRepo = class {
2961
2996
  for (const tombstone of tombstones) {
2962
2997
  const allConfirmed = await this.allReplicasConfirmAbsent(tombstone.path);
2963
2998
  if (allConfirmed) {
2964
- for (const dir of [TOMBSTONES_DIR, LEGACY_TOMBSTONES_DIR]) {
2965
- const tombstonePath = tombstonePathFor(tombstone.path, dir);
2966
- try {
2967
- await this.cachedFS.unlink(tombstonePath);
2968
- } catch {
2969
- }
2970
- for (const [, replica] of this.replicaBackends) {
2971
- if (await this.safeExists(replica.instance, tombstonePath)) {
2972
- try {
2973
- await replica.instance.unlink(tombstonePath);
2974
- } catch {
2975
- }
2999
+ const tombstonePath = tombstonePathFor(tombstone.path, TOMBSTONES_DIR);
3000
+ try {
3001
+ await this.cachedFS.unlink(tombstonePath);
3002
+ } catch {
3003
+ }
3004
+ for (const [, replica] of this.replicaBackends) {
3005
+ if (await this.safeExists(replica.instance, tombstonePath)) {
3006
+ try {
3007
+ await replica.instance.unlink(tombstonePath);
3008
+ } catch {
2976
3009
  }
2977
3010
  }
2978
- log3.log(`[ConfigRepo] gcTombstones: removed ${tombstonePath} (all ${allBackendIds.length} backends confirmed)`);
2979
3011
  }
3012
+ log3.log(`[ConfigRepo] gcTombstones: removed ${tombstonePath} (all ${allBackendIds.length} backends confirmed)`);
2980
3013
  }
2981
3014
  }
2982
3015
  }
@@ -3040,7 +3073,7 @@ var ConfigRepo = class {
3040
3073
  this.assertNotDisposed();
3041
3074
  return [...this.backendDescriptorCache];
3042
3075
  }
3043
- /** 配置组的权威后端列表(`.config/backends/`),与 `listBackends()` 相同。 */
3076
+ /** 配置组的权威后端列表(`.meta/backends/`),与 `listBackends()` 相同。 */
3044
3077
  listDeclaredBackends() {
3045
3078
  return this.listBackends();
3046
3079
  }
@@ -3433,11 +3466,15 @@ var ConfigRepo = class {
3433
3466
  // -----------------------------------------------------------------------
3434
3467
  // Internal — Individual Backend Descriptor Files
3435
3468
  // -----------------------------------------------------------------------
3436
- /** Path for a single backend descriptor: .config/backends/{id}.json */
3469
+ /** Path for a single backend descriptor: `.meta/backends/{id}.json`. */
3437
3470
  backendFilePath(id) {
3438
3471
  return `${BACKENDS_DIR}/${id}.json`;
3439
3472
  }
3440
- /** Legacy path: .meta/backends/{id}.json (read + dual-write for one cycle). */
3473
+ /**
3474
+ * Legacy pre-0.7 path `/.config/backends/{id}.json`.
3475
+ *
3476
+ * 该目录已不再读取:此处只用于删除后端时尽力清理残留副本。
3477
+ */
3441
3478
  legacyBackendFilePath(id) {
3442
3479
  return `${LEGACY_BACKENDS_DIR}/${id}.json`;
3443
3480
  }
@@ -3486,8 +3523,11 @@ var ConfigRepo = class {
3486
3523
  return { items, corruptFiles };
3487
3524
  }
3488
3525
  /**
3489
- * Read all backend descriptors from `.config/backends/*.json` with legacy
3490
- * `.meta/backends/*.json` as a one-cycle fallback (new layout wins by id).
3526
+ * Read all backend descriptors from `.meta/backends/*.json` — the single
3527
+ * authoritative location for both config-sync and data-sync groups.
3528
+ *
3529
+ * `/.config/backends/` is intentionally **not** read: leftovers there are
3530
+ * plain content and never take part in backend enumeration.
3491
3531
  *
3492
3532
  * If duplicate backends are detected (same type + options but different id),
3493
3533
  * only the first one (sorted by id) is kept and the rest are removed
@@ -3495,16 +3535,8 @@ var ConfigRepo = class {
3495
3535
  */
3496
3536
  async readAllBackendDescriptors() {
3497
3537
  const fresh = await this.readBackendDescriptorsFrom(BACKENDS_DIR);
3498
- const legacy = await this.readBackendDescriptorsFrom(LEGACY_BACKENDS_DIR);
3499
3538
  const items = [...fresh.items];
3500
- const seenIds = new Set(items.map((i) => i.desc.id));
3501
- for (const item of legacy.items) {
3502
- if (!seenIds.has(item.desc.id)) {
3503
- items.push(item);
3504
- seenIds.add(item.desc.id);
3505
- }
3506
- }
3507
- const corruptFiles = [...fresh.corruptFiles, ...legacy.corruptFiles];
3539
+ const corruptFiles = [...fresh.corruptFiles];
3508
3540
  for (const corruptPath of corruptFiles) {
3509
3541
  for (const [, replica] of this.replicaBackends) {
3510
3542
  try {
@@ -3569,20 +3601,18 @@ var ConfigRepo = class {
3569
3601
  return result;
3570
3602
  }
3571
3603
  /**
3572
- * Write a single backend descriptor to `.config/backends/{id}.json`,
3573
- * mirroring to the legacy `.meta/backends/{id}.json` for one release cycle
3574
- * so older clients still see the backend.
3604
+ * Write a single backend descriptor to `.meta/backends/{id}.json`.
3575
3605
  *
3576
- * Both files use the canonical serializer and are only written when their
3577
- * bytes actually differ (no needless sync churn).
3606
+ * Uses the canonical serializer and is only written when the bytes actually
3607
+ * differ (no needless sync churn). `/.config/backends/` is never written.
3578
3608
  */
3579
3609
  async writeBackendDescriptor(desc) {
3580
3610
  assertValidBackendId(desc.id);
3581
3611
  const author = `${this.appId}/${this.nodeId}`;
3582
3612
  const text = serializeBackendDescriptor(desc);
3583
3613
  const bytes = new TextEncoder().encode(text);
3584
- for (const path of [this.backendFilePath(desc.id), this.legacyBackendFilePath(desc.id)]) {
3585
- if (await this.readTextIfExists(path) === text) continue;
3614
+ const path = this.backendFilePath(desc.id);
3615
+ if (await this.readTextIfExists(path) !== text) {
3586
3616
  await this.ensureDir(path);
3587
3617
  await this.cachedFS.writeFile(path, bytes);
3588
3618
  const version = await incrementVersion(this.fullFS, path, bytes, author);
@@ -3592,7 +3622,10 @@ var ConfigRepo = class {
3592
3622
  if (existing >= 0) this.backendDescriptorCache[existing] = { ...desc };
3593
3623
  else this.backendDescriptorCache.push({ ...desc });
3594
3624
  }
3595
- /** Remove a single backend descriptor file + its version sidecar (both layouts) */
3625
+ /**
3626
+ * Remove a single backend descriptor: always `.meta/backends/{id}.json`, plus
3627
+ * a best-effort cleanup of the legacy `/.config/backends/{id}.json` residue.
3628
+ */
3596
3629
  async removeBackendDescriptor(id) {
3597
3630
  for (const path of [this.backendFilePath(id), this.legacyBackendFilePath(id)]) {
3598
3631
  try {
@@ -3879,8 +3912,9 @@ var ConfigRepo = class {
3879
3912
  return `${LEGACY_APP_DATA_GROUPS_DIR}/${this.appId}/${id}.json`;
3880
3913
  }
3881
3914
  /**
3882
- * Write a data-group descriptor to `.config/` (bootstrap copy), mirroring to
3883
- * the legacy `.meta/` path.
3915
+ * Write a data-group descriptor to `.config/app-data-groups/` (registration
3916
+ * + bootstrap copy). The legacy `.meta/app-data-groups/` mirror is no longer
3917
+ * written; it is still read so old layouts keep working.
3884
3918
  *
3885
3919
  * 这份描述符是**引导副本**:它让只有配置组凭据的新设备知道有哪些数据组、
3886
3920
  * 远端在哪。权威定义在数据组自己的 `.meta/backends/`。
@@ -3893,13 +3927,12 @@ var ConfigRepo = class {
3893
3927
  const text = serializeAppDataGroupDescriptor(mergeDescriptorCreatedAt(descriptor, existing));
3894
3928
  const bytes = new TextEncoder().encode(text);
3895
3929
  const author = `${this.appId}/${this.nodeId}`;
3896
- for (const descPath of [this.appDataGroupFilePath(id), this.legacyAppDataGroupFilePath(id)]) {
3897
- if (await this.readTextIfExists(descPath) === text) continue;
3898
- await this.ensureDir(descPath);
3899
- await this.cachedFS.writeFile(descPath, bytes);
3900
- const version = await incrementVersion(this.fullFS, descPath, bytes, author);
3901
- await this.writeVersionSidecar(descPath, version);
3902
- }
3930
+ const descPath = this.appDataGroupFilePath(id);
3931
+ if (await this.readTextIfExists(descPath) === text) return;
3932
+ await this.ensureDir(descPath);
3933
+ await this.cachedFS.writeFile(descPath, bytes);
3934
+ const version = await incrementVersion(this.fullFS, descPath, bytes, author);
3935
+ await this.writeVersionSidecar(descPath, version);
3903
3936
  }
3904
3937
  /**
3905
3938
  * Resolve a backend descriptor's options by merging account fields