zen-fs-config 0.9.5 → 0.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1247,6 +1247,11 @@ declare class ConfigRepo implements IConfigRepo {
1247
1247
  * 就是这么来的)。这里在同一轮变更后合并触发一次 flush,把描述符立刻推上去。
1248
1248
  */
1249
1249
  private descriptorPushTimer?;
1250
+ /**
1251
+ * 已做过「凭据回填」的端点去重键。同一进程内每个端点只回填一次,
1252
+ * 避免每次建对都重写一遍别人的描述符。
1253
+ */
1254
+ private credentialBackfillDone;
1250
1255
  /** 后台建对(deferReplicaSetup)的状态,见 `whenReplicasReady()`。 */
1251
1256
  private replicaSetupPromise;
1252
1257
  private replicaSetupSummary;
@@ -1606,6 +1611,19 @@ declare class ConfigRepo implements IConfigRepo {
1606
1611
  * (including their version sidecar files).
1607
1612
  */
1608
1613
  readAllBackendDescriptors(): Promise<BackendDescriptor[]>;
1614
+ /**
1615
+ * 把「连得上的凭据」回填到同一端点的重复描述符里。
1616
+ *
1617
+ * 背景:RemoteStorage 的 `token` 不进 `identityFields`(见 admin 的后端注册),
1618
+ * 同一 `href`+`basePath` 只会被当成一个端点;但历史上按 token 判重时,每次
1619
+ * 重新授权都会新增一个 `remotestorage-<时间戳>` 描述符,旧描述符仍留在
1620
+ * `.meta/backends/` 里。读时去重按 mtime 保留最早的一份,如果那份的 token
1621
+ * 已经失效,这个端点就会永远连不上。这里在某个描述符成功建对之后,把它的
1622
+ * token 写回同键的其它描述符,保证「留下的那份」凭据是可用的。
1623
+ *
1624
+ * 只改 token 一个字段,不新增/删除描述符,也不影响 id 与 mtime 之外的内容。
1625
+ */
1626
+ private backfillDuplicateBackendCredentials;
1609
1627
  /**
1610
1628
  * Write a single backend descriptor to `.meta/backends/{id}.json`.
1611
1629
  *
package/dist/index.d.ts CHANGED
@@ -1247,6 +1247,11 @@ declare class ConfigRepo implements IConfigRepo {
1247
1247
  * 就是这么来的)。这里在同一轮变更后合并触发一次 flush,把描述符立刻推上去。
1248
1248
  */
1249
1249
  private descriptorPushTimer?;
1250
+ /**
1251
+ * 已做过「凭据回填」的端点去重键。同一进程内每个端点只回填一次,
1252
+ * 避免每次建对都重写一遍别人的描述符。
1253
+ */
1254
+ private credentialBackfillDone;
1250
1255
  /** 后台建对(deferReplicaSetup)的状态,见 `whenReplicasReady()`。 */
1251
1256
  private replicaSetupPromise;
1252
1257
  private replicaSetupSummary;
@@ -1606,6 +1611,19 @@ declare class ConfigRepo implements IConfigRepo {
1606
1611
  * (including their version sidecar files).
1607
1612
  */
1608
1613
  readAllBackendDescriptors(): Promise<BackendDescriptor[]>;
1614
+ /**
1615
+ * 把「连得上的凭据」回填到同一端点的重复描述符里。
1616
+ *
1617
+ * 背景:RemoteStorage 的 `token` 不进 `identityFields`(见 admin 的后端注册),
1618
+ * 同一 `href`+`basePath` 只会被当成一个端点;但历史上按 token 判重时,每次
1619
+ * 重新授权都会新增一个 `remotestorage-<时间戳>` 描述符,旧描述符仍留在
1620
+ * `.meta/backends/` 里。读时去重按 mtime 保留最早的一份,如果那份的 token
1621
+ * 已经失效,这个端点就会永远连不上。这里在某个描述符成功建对之后,把它的
1622
+ * token 写回同键的其它描述符,保证「留下的那份」凭据是可用的。
1623
+ *
1624
+ * 只改 token 一个字段,不新增/删除描述符,也不影响 id 与 mtime 之外的内容。
1625
+ */
1626
+ private backfillDuplicateBackendCredentials;
1609
1627
  /**
1610
1628
  * Write a single backend descriptor to `.meta/backends/{id}.json`.
1611
1629
  *
package/dist/index.js CHANGED
@@ -1879,6 +1879,26 @@ function toBytes(raw) {
1879
1879
  if (typeof raw === "string") return new TextEncoder().encode(raw);
1880
1880
  return new Uint8Array(raw);
1881
1881
  }
1882
+ async function writeFileOverwrite(fs, path, data) {
1883
+ const bytes = toBytes(data);
1884
+ try {
1885
+ await fs.writeFile(path, bytes);
1886
+ return;
1887
+ } catch (err) {
1888
+ if (!isAlreadyExistsError(err)) throw err;
1889
+ }
1890
+ try {
1891
+ await fs.unlink(path);
1892
+ } catch {
1893
+ }
1894
+ await fs.writeFile(path, bytes);
1895
+ }
1896
+ function isAlreadyExistsError(err) {
1897
+ const e = err;
1898
+ if (!e) return false;
1899
+ if (e.code === "EEXIST") return true;
1900
+ return /file already exists|already exists|文件已存在/i.test(String(e.message ?? ""));
1901
+ }
1882
1902
  async function ensureParentDir2(fs, filePath) {
1883
1903
  const parts = filePath.split("/").filter(Boolean);
1884
1904
  parts.pop();
@@ -1909,7 +1929,7 @@ async function readText(fs, path) {
1909
1929
  }
1910
1930
  async function writeJson(fs, path, value) {
1911
1931
  await ensureParentDir2(fs, path);
1912
- await fs.writeFile(path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1932
+ await writeFileOverwrite(fs, path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1913
1933
  }
1914
1934
  async function existsSafe(fs, path) {
1915
1935
  try {
@@ -2187,14 +2207,14 @@ var TombstoneStore = class {
2187
2207
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
2188
2208
  const p = this.pathFor(tombstone.path);
2189
2209
  await ensureParentDir2(this.fs, p);
2190
- await this.fs.writeFile(p, bytes);
2210
+ await writeFileOverwrite(this.fs, p, bytes);
2191
2211
  }
2192
2212
  /** Persist an updated tombstone (confirmations) when it still exists. */
2193
2213
  async save(tombstone) {
2194
2214
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
2195
2215
  const p = this.pathFor(tombstone.path);
2196
2216
  if (!await existsSafe(this.fs, p)) return;
2197
- await this.fs.writeFile(p, bytes);
2217
+ await writeFileOverwrite(this.fs, p, bytes);
2198
2218
  }
2199
2219
  /** Delete a tombstone. */
2200
2220
  async remove(tombstone) {
@@ -3113,6 +3133,11 @@ var ConfigRepo = class _ConfigRepo {
3113
3133
  * 就是这么来的)。这里在同一轮变更后合并触发一次 flush,把描述符立刻推上去。
3114
3134
  */
3115
3135
  descriptorPushTimer;
3136
+ /**
3137
+ * 已做过「凭据回填」的端点去重键。同一进程内每个端点只回填一次,
3138
+ * 避免每次建对都重写一遍别人的描述符。
3139
+ */
3140
+ credentialBackfillDone = /* @__PURE__ */ new Set();
3116
3141
  /** 后台建对(deferReplicaSetup)的状态,见 `whenReplicasReady()`。 */
3117
3142
  replicaSetupPromise = null;
3118
3143
  replicaSetupSummary = null;
@@ -3387,7 +3412,7 @@ var ConfigRepo = class _ConfigRepo {
3387
3412
  const tombstoneBytes = new TextEncoder().encode(serializeTombstone(tombstone));
3388
3413
  const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
3389
3414
  await this.ensureDir(tombstonePath);
3390
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
3415
+ await writeFileOverwrite(this.cachedFS, tombstonePath, tombstoneBytes);
3391
3416
  }
3392
3417
  /**
3393
3418
  * Delete a file and write a tombstone so the deletion propagates
@@ -4377,6 +4402,9 @@ var ConfigRepo = class _ConfigRepo {
4377
4402
  );
4378
4403
  this.replicaBackends.set(desc.id, { instance: fsInstance, syncable, pairId: pair.pairId });
4379
4404
  this.backendStates.set(desc.id, { backendId: desc.id, pairId: pair.pairId });
4405
+ void this.backfillDuplicateBackendCredentials(desc).catch((err) => {
4406
+ log5.warn("[ConfigRepo] backfill duplicate backend credentials failed:", err);
4407
+ });
4380
4408
  const conflictHandler = (event) => {
4381
4409
  this.handleConflict(event);
4382
4410
  };
@@ -4721,6 +4749,50 @@ var ConfigRepo = class _ConfigRepo {
4721
4749
  this.backendDescriptorCache = [...result];
4722
4750
  return result;
4723
4751
  }
4752
+ /**
4753
+ * 把「连得上的凭据」回填到同一端点的重复描述符里。
4754
+ *
4755
+ * 背景:RemoteStorage 的 `token` 不进 `identityFields`(见 admin 的后端注册),
4756
+ * 同一 `href`+`basePath` 只会被当成一个端点;但历史上按 token 判重时,每次
4757
+ * 重新授权都会新增一个 `remotestorage-<时间戳>` 描述符,旧描述符仍留在
4758
+ * `.meta/backends/` 里。读时去重按 mtime 保留最早的一份,如果那份的 token
4759
+ * 已经失效,这个端点就会永远连不上。这里在某个描述符成功建对之后,把它的
4760
+ * token 写回同键的其它描述符,保证「留下的那份」凭据是可用的。
4761
+ *
4762
+ * 只改 token 一个字段,不新增/删除描述符,也不影响 id 与 mtime 之外的内容。
4763
+ */
4764
+ async backfillDuplicateBackendCredentials(desc) {
4765
+ const options = desc.options;
4766
+ const mine = options?.token;
4767
+ if (typeof mine !== "string" || mine.length === 0) return;
4768
+ const key = backendDedupKey(desc);
4769
+ if (this.credentialBackfillDone.has(key)) return;
4770
+ this.credentialBackfillDone.add(key);
4771
+ let items;
4772
+ try {
4773
+ items = (await this.readBackendDescriptorsFrom(BACKENDS_DIR)).items;
4774
+ } catch (err) {
4775
+ log5.warn("[ConfigRepo] backfill credentials: read descriptors failed:", err);
4776
+ return;
4777
+ }
4778
+ for (const item of items) {
4779
+ if (item.desc.id === desc.id) continue;
4780
+ if (backendDedupKey(item.desc) !== key) continue;
4781
+ const theirs = item.desc.options?.token;
4782
+ if (typeof theirs !== "string" || theirs === mine) continue;
4783
+ try {
4784
+ await this.writeBackendDescriptor({
4785
+ ...item.desc,
4786
+ options: { ...item.desc.options, token: mine }
4787
+ });
4788
+ log5.log(
4789
+ `[ConfigRepo] backfilled working credential into duplicate descriptor ${item.desc.id} (endpoint ${key})`
4790
+ );
4791
+ } catch (err) {
4792
+ log5.warn(`[ConfigRepo] backfill credentials: write ${item.desc.id} failed:`, err);
4793
+ }
4794
+ }
4795
+ }
4724
4796
  /**
4725
4797
  * Write a single backend descriptor to `.meta/backends/{id}.json`.
4726
4798
  *
package/dist/index.mjs CHANGED
@@ -1091,6 +1091,26 @@ function toBytes(raw) {
1091
1091
  if (typeof raw === "string") return new TextEncoder().encode(raw);
1092
1092
  return new Uint8Array(raw);
1093
1093
  }
1094
+ async function writeFileOverwrite(fs, path, data) {
1095
+ const bytes = toBytes(data);
1096
+ try {
1097
+ await fs.writeFile(path, bytes);
1098
+ return;
1099
+ } catch (err) {
1100
+ if (!isAlreadyExistsError(err)) throw err;
1101
+ }
1102
+ try {
1103
+ await fs.unlink(path);
1104
+ } catch {
1105
+ }
1106
+ await fs.writeFile(path, bytes);
1107
+ }
1108
+ function isAlreadyExistsError(err) {
1109
+ const e = err;
1110
+ if (!e) return false;
1111
+ if (e.code === "EEXIST") return true;
1112
+ return /file already exists|already exists|文件已存在/i.test(String(e.message ?? ""));
1113
+ }
1094
1114
  async function ensureParentDir2(fs, filePath) {
1095
1115
  const parts = filePath.split("/").filter(Boolean);
1096
1116
  parts.pop();
@@ -1121,7 +1141,7 @@ async function readText(fs, path) {
1121
1141
  }
1122
1142
  async function writeJson(fs, path, value) {
1123
1143
  await ensureParentDir2(fs, path);
1124
- await fs.writeFile(path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1144
+ await writeFileOverwrite(fs, path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
1125
1145
  }
1126
1146
  async function existsSafe(fs, path) {
1127
1147
  try {
@@ -1399,14 +1419,14 @@ var TombstoneStore = class {
1399
1419
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
1400
1420
  const p = this.pathFor(tombstone.path);
1401
1421
  await ensureParentDir2(this.fs, p);
1402
- await this.fs.writeFile(p, bytes);
1422
+ await writeFileOverwrite(this.fs, p, bytes);
1403
1423
  }
1404
1424
  /** Persist an updated tombstone (confirmations) when it still exists. */
1405
1425
  async save(tombstone) {
1406
1426
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
1407
1427
  const p = this.pathFor(tombstone.path);
1408
1428
  if (!await existsSafe(this.fs, p)) return;
1409
- await this.fs.writeFile(p, bytes);
1429
+ await writeFileOverwrite(this.fs, p, bytes);
1410
1430
  }
1411
1431
  /** Delete a tombstone. */
1412
1432
  async remove(tombstone) {
@@ -2325,6 +2345,11 @@ var ConfigRepo = class _ConfigRepo {
2325
2345
  * 就是这么来的)。这里在同一轮变更后合并触发一次 flush,把描述符立刻推上去。
2326
2346
  */
2327
2347
  descriptorPushTimer;
2348
+ /**
2349
+ * 已做过「凭据回填」的端点去重键。同一进程内每个端点只回填一次,
2350
+ * 避免每次建对都重写一遍别人的描述符。
2351
+ */
2352
+ credentialBackfillDone = /* @__PURE__ */ new Set();
2328
2353
  /** 后台建对(deferReplicaSetup)的状态,见 `whenReplicasReady()`。 */
2329
2354
  replicaSetupPromise = null;
2330
2355
  replicaSetupSummary = null;
@@ -2599,7 +2624,7 @@ var ConfigRepo = class _ConfigRepo {
2599
2624
  const tombstoneBytes = new TextEncoder().encode(serializeTombstone(tombstone));
2600
2625
  const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
2601
2626
  await this.ensureDir(tombstonePath);
2602
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
2627
+ await writeFileOverwrite(this.cachedFS, tombstonePath, tombstoneBytes);
2603
2628
  }
2604
2629
  /**
2605
2630
  * Delete a file and write a tombstone so the deletion propagates
@@ -3589,6 +3614,9 @@ var ConfigRepo = class _ConfigRepo {
3589
3614
  );
3590
3615
  this.replicaBackends.set(desc.id, { instance: fsInstance, syncable, pairId: pair.pairId });
3591
3616
  this.backendStates.set(desc.id, { backendId: desc.id, pairId: pair.pairId });
3617
+ void this.backfillDuplicateBackendCredentials(desc).catch((err) => {
3618
+ log5.warn("[ConfigRepo] backfill duplicate backend credentials failed:", err);
3619
+ });
3592
3620
  const conflictHandler = (event) => {
3593
3621
  this.handleConflict(event);
3594
3622
  };
@@ -3933,6 +3961,50 @@ var ConfigRepo = class _ConfigRepo {
3933
3961
  this.backendDescriptorCache = [...result];
3934
3962
  return result;
3935
3963
  }
3964
+ /**
3965
+ * 把「连得上的凭据」回填到同一端点的重复描述符里。
3966
+ *
3967
+ * 背景:RemoteStorage 的 `token` 不进 `identityFields`(见 admin 的后端注册),
3968
+ * 同一 `href`+`basePath` 只会被当成一个端点;但历史上按 token 判重时,每次
3969
+ * 重新授权都会新增一个 `remotestorage-<时间戳>` 描述符,旧描述符仍留在
3970
+ * `.meta/backends/` 里。读时去重按 mtime 保留最早的一份,如果那份的 token
3971
+ * 已经失效,这个端点就会永远连不上。这里在某个描述符成功建对之后,把它的
3972
+ * token 写回同键的其它描述符,保证「留下的那份」凭据是可用的。
3973
+ *
3974
+ * 只改 token 一个字段,不新增/删除描述符,也不影响 id 与 mtime 之外的内容。
3975
+ */
3976
+ async backfillDuplicateBackendCredentials(desc) {
3977
+ const options = desc.options;
3978
+ const mine = options?.token;
3979
+ if (typeof mine !== "string" || mine.length === 0) return;
3980
+ const key = backendDedupKey(desc);
3981
+ if (this.credentialBackfillDone.has(key)) return;
3982
+ this.credentialBackfillDone.add(key);
3983
+ let items;
3984
+ try {
3985
+ items = (await this.readBackendDescriptorsFrom(BACKENDS_DIR)).items;
3986
+ } catch (err) {
3987
+ log5.warn("[ConfigRepo] backfill credentials: read descriptors failed:", err);
3988
+ return;
3989
+ }
3990
+ for (const item of items) {
3991
+ if (item.desc.id === desc.id) continue;
3992
+ if (backendDedupKey(item.desc) !== key) continue;
3993
+ const theirs = item.desc.options?.token;
3994
+ if (typeof theirs !== "string" || theirs === mine) continue;
3995
+ try {
3996
+ await this.writeBackendDescriptor({
3997
+ ...item.desc,
3998
+ options: { ...item.desc.options, token: mine }
3999
+ });
4000
+ log5.log(
4001
+ `[ConfigRepo] backfilled working credential into duplicate descriptor ${item.desc.id} (endpoint ${key})`
4002
+ );
4003
+ } catch (err) {
4004
+ log5.warn(`[ConfigRepo] backfill credentials: write ${item.desc.id} failed:`, err);
4005
+ }
4006
+ }
4007
+ }
3936
4008
  /**
3937
4009
  * Write a single backend descriptor to `.meta/backends/{id}.json`.
3938
4010
  *
@@ -27845,6 +27845,26 @@ var ZenFSConfig = (() => {
27845
27845
  if (typeof raw === "string") return new TextEncoder().encode(raw);
27846
27846
  return new Uint8Array(raw);
27847
27847
  }
27848
+ async function writeFileOverwrite(fs, path, data) {
27849
+ const bytes = toBytes(data);
27850
+ try {
27851
+ await fs.writeFile(path, bytes);
27852
+ return;
27853
+ } catch (err2) {
27854
+ if (!isAlreadyExistsError(err2)) throw err2;
27855
+ }
27856
+ try {
27857
+ await fs.unlink(path);
27858
+ } catch {
27859
+ }
27860
+ await fs.writeFile(path, bytes);
27861
+ }
27862
+ function isAlreadyExistsError(err2) {
27863
+ const e = err2;
27864
+ if (!e) return false;
27865
+ if (e.code === "EEXIST") return true;
27866
+ return /file already exists|already exists|文件已存在/i.test(String(e.message ?? ""));
27867
+ }
27848
27868
  async function ensureParentDir2(fs, filePath) {
27849
27869
  const parts = filePath.split("/").filter(Boolean);
27850
27870
  parts.pop();
@@ -27875,7 +27895,7 @@ var ZenFSConfig = (() => {
27875
27895
  }
27876
27896
  async function writeJson(fs, path, value) {
27877
27897
  await ensureParentDir2(fs, path);
27878
- await fs.writeFile(path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
27898
+ await writeFileOverwrite(fs, path, new TextEncoder().encode(JSON.stringify(value, null, 2)));
27879
27899
  }
27880
27900
  async function existsSafe(fs, path) {
27881
27901
  try {
@@ -28151,14 +28171,14 @@ var ZenFSConfig = (() => {
28151
28171
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
28152
28172
  const p = this.pathFor(tombstone.path);
28153
28173
  await ensureParentDir2(this.fs, p);
28154
- await this.fs.writeFile(p, bytes);
28174
+ await writeFileOverwrite(this.fs, p, bytes);
28155
28175
  }
28156
28176
  /** Persist an updated tombstone (confirmations) when it still exists. */
28157
28177
  async save(tombstone) {
28158
28178
  const bytes = new TextEncoder().encode(serializeTombstone(tombstone));
28159
28179
  const p = this.pathFor(tombstone.path);
28160
28180
  if (!await existsSafe(this.fs, p)) return;
28161
- await this.fs.writeFile(p, bytes);
28181
+ await writeFileOverwrite(this.fs, p, bytes);
28162
28182
  }
28163
28183
  /** Delete a tombstone. */
28164
28184
  async remove(tombstone) {
@@ -29071,6 +29091,11 @@ var ZenFSConfig = (() => {
29071
29091
  * 就是这么来的)。这里在同一轮变更后合并触发一次 flush,把描述符立刻推上去。
29072
29092
  */
29073
29093
  __publicField(this, "descriptorPushTimer");
29094
+ /**
29095
+ * 已做过「凭据回填」的端点去重键。同一进程内每个端点只回填一次,
29096
+ * 避免每次建对都重写一遍别人的描述符。
29097
+ */
29098
+ __publicField(this, "credentialBackfillDone", /* @__PURE__ */ new Set());
29074
29099
  /** 后台建对(deferReplicaSetup)的状态,见 `whenReplicasReady()`。 */
29075
29100
  __publicField(this, "replicaSetupPromise", null);
29076
29101
  __publicField(this, "replicaSetupSummary", null);
@@ -29336,7 +29361,7 @@ var ZenFSConfig = (() => {
29336
29361
  const tombstoneBytes = new TextEncoder().encode(serializeTombstone(tombstone));
29337
29362
  const tombstonePath = tombstonePathFor(normalizedPath, TOMBSTONES_DIR);
29338
29363
  await this.ensureDir(tombstonePath);
29339
- await this.cachedFS.writeFile(tombstonePath, tombstoneBytes);
29364
+ await writeFileOverwrite(this.cachedFS, tombstonePath, tombstoneBytes);
29340
29365
  }
29341
29366
  /**
29342
29367
  * Delete a file and write a tombstone so the deletion propagates
@@ -30320,6 +30345,9 @@ var ZenFSConfig = (() => {
30320
30345
  );
30321
30346
  this.replicaBackends.set(desc.id, { instance: fsInstance, syncable, pairId: pair.pairId });
30322
30347
  this.backendStates.set(desc.id, { backendId: desc.id, pairId: pair.pairId });
30348
+ void this.backfillDuplicateBackendCredentials(desc).catch((err2) => {
30349
+ log9.warn("[ConfigRepo] backfill duplicate backend credentials failed:", err2);
30350
+ });
30323
30351
  const conflictHandler = (event) => {
30324
30352
  this.handleConflict(event);
30325
30353
  };
@@ -30664,6 +30692,50 @@ var ZenFSConfig = (() => {
30664
30692
  this.backendDescriptorCache = [...result];
30665
30693
  return result;
30666
30694
  }
30695
+ /**
30696
+ * 把「连得上的凭据」回填到同一端点的重复描述符里。
30697
+ *
30698
+ * 背景:RemoteStorage 的 `token` 不进 `identityFields`(见 admin 的后端注册),
30699
+ * 同一 `href`+`basePath` 只会被当成一个端点;但历史上按 token 判重时,每次
30700
+ * 重新授权都会新增一个 `remotestorage-<时间戳>` 描述符,旧描述符仍留在
30701
+ * `.meta/backends/` 里。读时去重按 mtime 保留最早的一份,如果那份的 token
30702
+ * 已经失效,这个端点就会永远连不上。这里在某个描述符成功建对之后,把它的
30703
+ * token 写回同键的其它描述符,保证「留下的那份」凭据是可用的。
30704
+ *
30705
+ * 只改 token 一个字段,不新增/删除描述符,也不影响 id 与 mtime 之外的内容。
30706
+ */
30707
+ async backfillDuplicateBackendCredentials(desc) {
30708
+ const options = desc.options;
30709
+ const mine = options?.token;
30710
+ if (typeof mine !== "string" || mine.length === 0) return;
30711
+ const key = backendDedupKey(desc);
30712
+ if (this.credentialBackfillDone.has(key)) return;
30713
+ this.credentialBackfillDone.add(key);
30714
+ let items;
30715
+ try {
30716
+ items = (await this.readBackendDescriptorsFrom(BACKENDS_DIR)).items;
30717
+ } catch (err2) {
30718
+ log9.warn("[ConfigRepo] backfill credentials: read descriptors failed:", err2);
30719
+ return;
30720
+ }
30721
+ for (const item of items) {
30722
+ if (item.desc.id === desc.id) continue;
30723
+ if (backendDedupKey(item.desc) !== key) continue;
30724
+ const theirs = item.desc.options?.token;
30725
+ if (typeof theirs !== "string" || theirs === mine) continue;
30726
+ try {
30727
+ await this.writeBackendDescriptor({
30728
+ ...item.desc,
30729
+ options: { ...item.desc.options, token: mine }
30730
+ });
30731
+ log9.log(
30732
+ `[ConfigRepo] backfilled working credential into duplicate descriptor ${item.desc.id} (endpoint ${key})`
30733
+ );
30734
+ } catch (err2) {
30735
+ log9.warn(`[ConfigRepo] backfill credentials: write ${item.desc.id} failed:`, err2);
30736
+ }
30737
+ }
30738
+ }
30667
30739
  /**
30668
30740
  * Write a single backend descriptor to `.meta/backends/{id}.json`.
30669
30741
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-fs-config",
3
- "version": "0.9.5",
3
+ "version": "0.9.7",
4
4
  "description": "Distributed config management library built on ZenFS, zen-fs-cache, and zen-fs-sync",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",