zen-fs-config 0.5.46 → 0.5.47

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
@@ -577,6 +577,21 @@ interface IConfigRepo {
577
577
  updateConfigSyncGroupDescriptor(patch: {
578
578
  name: string;
579
579
  }): Promise<ConfigSyncGroupDescriptor>;
580
+ /**
581
+ * Replace a backend's parameters while keeping its id.
582
+ *
583
+ * Changing parameters means switching to a different remote target:
584
+ * the old sync pair is torn down, the new backend is instantiated, paired
585
+ * and initially synced. Old remote data is NOT migrated. If the new
586
+ * parameters cannot be instantiated, the old backend/pair is left intact
587
+ * and the error is rethrown.
588
+ */
589
+ replaceBackend(id: string, type: string, options: Record<string, unknown>, description?: string): Promise<void>;
590
+ /**
591
+ * Replace a data-sync backend's parameters (same semantics as
592
+ * replaceBackend: old pair is rebuilt against the new target).
593
+ */
594
+ replaceAppDataGroupBackend(groupId: string, backendId: string, type: string, options: Record<string, unknown>, description?: string): Promise<AppDataGroup>;
580
595
  /** Remove a data-sync group (stops sync, removes descriptor). */
581
596
  removeAppDataGroup(id: string): Promise<void>;
582
597
  /**
@@ -1008,7 +1023,24 @@ declare class ConfigRepo implements IConfigRepo {
1008
1023
  getBackends(): Promise<BackendsMeta | null>;
1009
1024
  updateBackends(meta: BackendsMeta): Promise<void>;
1010
1025
  addBackend(id: string, type: string, options: Record<string, unknown>, description?: string): Promise<void>;
1026
+ /**
1027
+ * Instantiate a config-sync replica, persist its descriptor, create its
1028
+ * sync pair, register lifecycle hooks and run the initial sync/watch.
1029
+ * Shared by addBackend() and replaceBackend().
1030
+ */
1031
+ private setupReplica;
1011
1032
  removeBackend(id: string): Promise<void>;
1033
+ /**
1034
+ * Replace a config-sync backend's parameters in place (same id).
1035
+ *
1036
+ * Probe the new parameters first: if the backend cannot even be created
1037
+ * (e.g. missing token), nothing is touched and the error is rethrown.
1038
+ * Otherwise the old pair is removed, the descriptor rewritten, and a new
1039
+ * pair created against the new target with an initial sync. If that fails,
1040
+ * the old descriptor and pair are restored. The descriptor file is never
1041
+ * tombstoned, so this never delete-then-recreates it.
1042
+ */
1043
+ replaceBackend(id: string, type: string, options: Record<string, unknown>, description?: string): Promise<void>;
1012
1044
  /** Write the group-type marker file if it doesn't exist. */
1013
1045
  ensureGroupType(type: SyncGroupType): Promise<void>;
1014
1046
  /** Read the group-type marker. Returns null if not set. */
@@ -1032,6 +1064,16 @@ declare class ConfigRepo implements IConfigRepo {
1032
1064
  * and remote data are untouched, so existing sync pairs keep working.
1033
1065
  */
1034
1066
  updateAppDataGroupName(id: string, name: string): Promise<void>;
1067
+ /**
1068
+ * Replace a data-sync backend's parameters in place (same backend id).
1069
+ *
1070
+ * Changing parameters means switching to a different remote target: the live
1071
+ * group is disposed (old pairs torn down), the descriptor is rewritten, and a
1072
+ * fresh group instance creates pairs + performs the initial sync. Old remote
1073
+ * data is NOT migrated. If the new options cannot be instantiated, nothing is
1074
+ * touched; if re-initialisation fails, the old descriptor is restored.
1075
+ */
1076
+ replaceAppDataGroupBackend(groupId: string, backendId: string, type: string, options: Record<string, unknown>, description?: string): Promise<AppDataGroup>;
1035
1077
  getConfigSyncGroupDescriptor(): Promise<ConfigSyncGroupDescriptor | null>;
1036
1078
  /**
1037
1079
  * Change the config-sync group's display name only. Backends, the local
package/dist/index.d.ts CHANGED
@@ -577,6 +577,21 @@ interface IConfigRepo {
577
577
  updateConfigSyncGroupDescriptor(patch: {
578
578
  name: string;
579
579
  }): Promise<ConfigSyncGroupDescriptor>;
580
+ /**
581
+ * Replace a backend's parameters while keeping its id.
582
+ *
583
+ * Changing parameters means switching to a different remote target:
584
+ * the old sync pair is torn down, the new backend is instantiated, paired
585
+ * and initially synced. Old remote data is NOT migrated. If the new
586
+ * parameters cannot be instantiated, the old backend/pair is left intact
587
+ * and the error is rethrown.
588
+ */
589
+ replaceBackend(id: string, type: string, options: Record<string, unknown>, description?: string): Promise<void>;
590
+ /**
591
+ * Replace a data-sync backend's parameters (same semantics as
592
+ * replaceBackend: old pair is rebuilt against the new target).
593
+ */
594
+ replaceAppDataGroupBackend(groupId: string, backendId: string, type: string, options: Record<string, unknown>, description?: string): Promise<AppDataGroup>;
580
595
  /** Remove a data-sync group (stops sync, removes descriptor). */
581
596
  removeAppDataGroup(id: string): Promise<void>;
582
597
  /**
@@ -1008,7 +1023,24 @@ declare class ConfigRepo implements IConfigRepo {
1008
1023
  getBackends(): Promise<BackendsMeta | null>;
1009
1024
  updateBackends(meta: BackendsMeta): Promise<void>;
1010
1025
  addBackend(id: string, type: string, options: Record<string, unknown>, description?: string): Promise<void>;
1026
+ /**
1027
+ * Instantiate a config-sync replica, persist its descriptor, create its
1028
+ * sync pair, register lifecycle hooks and run the initial sync/watch.
1029
+ * Shared by addBackend() and replaceBackend().
1030
+ */
1031
+ private setupReplica;
1011
1032
  removeBackend(id: string): Promise<void>;
1033
+ /**
1034
+ * Replace a config-sync backend's parameters in place (same id).
1035
+ *
1036
+ * Probe the new parameters first: if the backend cannot even be created
1037
+ * (e.g. missing token), nothing is touched and the error is rethrown.
1038
+ * Otherwise the old pair is removed, the descriptor rewritten, and a new
1039
+ * pair created against the new target with an initial sync. If that fails,
1040
+ * the old descriptor and pair are restored. The descriptor file is never
1041
+ * tombstoned, so this never delete-then-recreates it.
1042
+ */
1043
+ replaceBackend(id: string, type: string, options: Record<string, unknown>, description?: string): Promise<void>;
1012
1044
  /** Write the group-type marker file if it doesn't exist. */
1013
1045
  ensureGroupType(type: SyncGroupType): Promise<void>;
1014
1046
  /** Read the group-type marker. Returns null if not set. */
@@ -1032,6 +1064,16 @@ declare class ConfigRepo implements IConfigRepo {
1032
1064
  * and remote data are untouched, so existing sync pairs keep working.
1033
1065
  */
1034
1066
  updateAppDataGroupName(id: string, name: string): Promise<void>;
1067
+ /**
1068
+ * Replace a data-sync backend's parameters in place (same backend id).
1069
+ *
1070
+ * Changing parameters means switching to a different remote target: the live
1071
+ * group is disposed (old pairs torn down), the descriptor is rewritten, and a
1072
+ * fresh group instance creates pairs + performs the initial sync. Old remote
1073
+ * data is NOT migrated. If the new options cannot be instantiated, nothing is
1074
+ * touched; if re-initialisation fails, the old descriptor is restored.
1075
+ */
1076
+ replaceAppDataGroupBackend(groupId: string, backendId: string, type: string, options: Record<string, unknown>, description?: string): Promise<AppDataGroup>;
1035
1077
  getConfigSyncGroupDescriptor(): Promise<ConfigSyncGroupDescriptor | null>;
1036
1078
  /**
1037
1079
  * Change the config-sync group's display name only. Backends, the local
package/dist/index.js CHANGED
@@ -2628,6 +2628,14 @@ var ConfigRepo = class {
2628
2628
  `Backend "${id}" has the same configuration as existing backend "${dup.id}" (type=${type}). Use removeBackend("${dup.id}") first, or connect with the existing backend's ID.`
2629
2629
  );
2630
2630
  }
2631
+ await this.setupReplica(id, type, options, description);
2632
+ }
2633
+ /**
2634
+ * Instantiate a config-sync replica, persist its descriptor, create its
2635
+ * sync pair, register lifecycle hooks and run the initial sync/watch.
2636
+ * Shared by addBackend() and replaceBackend().
2637
+ */
2638
+ async setupReplica(id, type, options, description) {
2631
2639
  log2.log(`[ConfigRepo] addBackend: creating ${id} (${type})...`);
2632
2640
  const instance = await createBackend({ type, options });
2633
2641
  const syncable = backendToSyncableFS(instance, `${type}(${id})`);
@@ -2667,6 +2675,7 @@ var ConfigRepo = class {
2667
2675
  log2.log(`[ConfigRepo] addBackend: ${id} (${type}) added, sync pair=${pair.pairId}`);
2668
2676
  await this.syncMetaToReplicas();
2669
2677
  this.syncEngine.watch(pair.pairId);
2678
+ return pair.pairId;
2670
2679
  }
2671
2680
  async removeBackend(id) {
2672
2681
  this.assertNotDisposed();
@@ -2703,6 +2712,60 @@ var ConfigRepo = class {
2703
2712
  this.schedulePostDeleteSync();
2704
2713
  log2.log(`[ConfigRepo] removeBackend: ${id} removed (tombstone written, remote cleaned)`);
2705
2714
  }
2715
+ /**
2716
+ * Replace a config-sync backend's parameters in place (same id).
2717
+ *
2718
+ * Probe the new parameters first: if the backend cannot even be created
2719
+ * (e.g. missing token), nothing is touched and the error is rethrown.
2720
+ * Otherwise the old pair is removed, the descriptor rewritten, and a new
2721
+ * pair created against the new target with an initial sync. If that fails,
2722
+ * the old descriptor and pair are restored. The descriptor file is never
2723
+ * tombstoned, so this never delete-then-recreates it.
2724
+ */
2725
+ async replaceBackend(id, type, options, description) {
2726
+ this.assertNotDisposed();
2727
+ if (id === LOCAL_IDB_BACKEND_ID) {
2728
+ throw new Error("Cannot edit the local IndexedDB primary backend");
2729
+ }
2730
+ const all = await this.readAllBackendDescriptors();
2731
+ const before = all.find((b) => b.id === id);
2732
+ if (!before) {
2733
+ throw new Error(`Backend "${id}" not found`);
2734
+ }
2735
+ const probe = await createBackend({ type, options });
2736
+ try {
2737
+ await probe.dispose?.();
2738
+ } catch {
2739
+ }
2740
+ const newKey = backendDedupKey({ id, type, options });
2741
+ const dup = all.find((b) => b.id !== id && backendDedupKey(b) === newKey);
2742
+ if (dup) {
2743
+ throw new Error(
2744
+ `\u65B0\u53C2\u6570\u4E0E\u5DF2\u6709\u540E\u7AEF\u300C${dup.id}\u300D\u6307\u5411\u540C\u4E00\u8FDC\u7AEF\uFF08\u7C7B\u578B\u4E0E\u53C2\u6570\u5B8C\u5168\u76F8\u540C\uFF09\uFF0C\u8BF7\u5148\u5220\u9664\u91CD\u590D\u540E\u7AEF\u300C${dup.id}\u300D\u6216\u6539\u7528\u4E0D\u540C\u53C2\u6570`
2745
+ );
2746
+ }
2747
+ const oldReplica = this.replicaBackends.get(id);
2748
+ if (oldReplica) {
2749
+ this.syncEngine.removePair(oldReplica.pairId);
2750
+ this.replicaBackends.delete(id);
2751
+ try {
2752
+ await oldReplica.instance?.dispose?.();
2753
+ } catch {
2754
+ }
2755
+ }
2756
+ try {
2757
+ await this.setupReplica(id, type, options, description ?? before.description);
2758
+ } catch (err) {
2759
+ const reason = err instanceof Error ? err.message : String(err);
2760
+ log2.warn(`[ConfigRepo] replaceBackend: "${id}" failed, rolling back old options:`, err);
2761
+ try {
2762
+ await this.setupReplica(id, before.type, before.options, before.description);
2763
+ } catch (rollbackErr) {
2764
+ log2.error(`[ConfigRepo] replaceBackend: rollback for "${id}" ALSO failed:`, rollbackErr);
2765
+ }
2766
+ throw new Error(`\u66F4\u65B0\u540E\u7AEF\u53C2\u6570\u5931\u8D25\uFF1A${reason}\uFF08\u5DF2\u5C1D\u8BD5\u56DE\u6EDA\u65E7\u53C2\u6570\uFF09`);
2767
+ }
2768
+ }
2706
2769
  // -----------------------------------------------------------------------
2707
2770
  // IConfigRepo — Group Type
2708
2771
  // -----------------------------------------------------------------------
@@ -2838,6 +2901,73 @@ var ConfigRepo = class {
2838
2901
  const version = await incrementVersion(this.fullFS, descPath, bytes, author);
2839
2902
  await this.writeVersionSidecar(descPath, version);
2840
2903
  }
2904
+ /**
2905
+ * Replace a data-sync backend's parameters in place (same backend id).
2906
+ *
2907
+ * Changing parameters means switching to a different remote target: the live
2908
+ * group is disposed (old pairs torn down), the descriptor is rewritten, and a
2909
+ * fresh group instance creates pairs + performs the initial sync. Old remote
2910
+ * data is NOT migrated. If the new options cannot be instantiated, nothing is
2911
+ * touched; if re-initialisation fails, the old descriptor is restored.
2912
+ */
2913
+ async replaceAppDataGroupBackend(groupId, backendId, type, options, description) {
2914
+ this.assertNotDisposed();
2915
+ const descriptor = await this.readAppDataGroupDescriptor(groupId);
2916
+ if (!descriptor) {
2917
+ throw new Error(`App data group "${groupId}" not found`);
2918
+ }
2919
+ const index = descriptor.backends.findIndex((b) => b.id === backendId);
2920
+ if (index < 0) {
2921
+ throw new Error(`Backend "${backendId}" not found in data group "${groupId}"`);
2922
+ }
2923
+ const candidate = { ...descriptor.backends[index], type, options, description: description ?? descriptor.backends[index].description };
2924
+ const mergedOptions = await this.resolveAppDataBackendOptions(candidate);
2925
+ const probe = await createBackend({ type, options: mergedOptions });
2926
+ try {
2927
+ await probe.dispose?.();
2928
+ } catch {
2929
+ }
2930
+ const oldBackends = descriptor.backends.map((b) => ({ ...b }));
2931
+ const nextBackends = descriptor.backends.map(
2932
+ (b, i) => i === index ? { ...candidate, options: mergedOptions } : b
2933
+ );
2934
+ const live = this.appDataGroups.get(groupId);
2935
+ if (live) {
2936
+ await live.dispose();
2937
+ this.appDataGroups.delete(groupId);
2938
+ }
2939
+ await this.updateAppDataGroupDescriptor(groupId, nextBackends);
2940
+ try {
2941
+ const group = new AppDataGroupImpl(
2942
+ groupId,
2943
+ this.appId,
2944
+ nextBackends,
2945
+ this.pollIntervalMs,
2946
+ this
2947
+ );
2948
+ await group.init();
2949
+ this.appDataGroups.set(groupId, group);
2950
+ return group;
2951
+ } catch (err) {
2952
+ const reason = err instanceof Error ? err.message : String(err);
2953
+ log2.warn(`[ConfigRepo] replaceAppDataGroupBackend: "${backendId}" failed, rolling back:`, err);
2954
+ try {
2955
+ await this.updateAppDataGroupDescriptor(groupId, oldBackends);
2956
+ const restored = new AppDataGroupImpl(
2957
+ groupId,
2958
+ this.appId,
2959
+ oldBackends,
2960
+ this.pollIntervalMs,
2961
+ this
2962
+ );
2963
+ await restored.init();
2964
+ this.appDataGroups.set(groupId, restored);
2965
+ } catch (rollbackErr) {
2966
+ log2.error("[ConfigRepo] replaceAppDataGroupBackend: rollback ALSO failed:", rollbackErr);
2967
+ }
2968
+ throw new Error(`\u66F4\u65B0\u540E\u7AEF\u53C2\u6570\u5931\u8D25\uFF1A${reason}\uFF08\u5DF2\u5C1D\u8BD5\u56DE\u6EDA\u65E7\u53C2\u6570\uFF09`);
2969
+ }
2970
+ }
2841
2971
  async getConfigSyncGroupDescriptor() {
2842
2972
  this.assertNotDisposed();
2843
2973
  try {
package/dist/index.mjs CHANGED
@@ -2088,6 +2088,14 @@ var ConfigRepo = class {
2088
2088
  `Backend "${id}" has the same configuration as existing backend "${dup.id}" (type=${type}). Use removeBackend("${dup.id}") first, or connect with the existing backend's ID.`
2089
2089
  );
2090
2090
  }
2091
+ await this.setupReplica(id, type, options, description);
2092
+ }
2093
+ /**
2094
+ * Instantiate a config-sync replica, persist its descriptor, create its
2095
+ * sync pair, register lifecycle hooks and run the initial sync/watch.
2096
+ * Shared by addBackend() and replaceBackend().
2097
+ */
2098
+ async setupReplica(id, type, options, description) {
2091
2099
  log2.log(`[ConfigRepo] addBackend: creating ${id} (${type})...`);
2092
2100
  const instance = await createBackend({ type, options });
2093
2101
  const syncable = backendToSyncableFS(instance, `${type}(${id})`);
@@ -2127,6 +2135,7 @@ var ConfigRepo = class {
2127
2135
  log2.log(`[ConfigRepo] addBackend: ${id} (${type}) added, sync pair=${pair.pairId}`);
2128
2136
  await this.syncMetaToReplicas();
2129
2137
  this.syncEngine.watch(pair.pairId);
2138
+ return pair.pairId;
2130
2139
  }
2131
2140
  async removeBackend(id) {
2132
2141
  this.assertNotDisposed();
@@ -2163,6 +2172,60 @@ var ConfigRepo = class {
2163
2172
  this.schedulePostDeleteSync();
2164
2173
  log2.log(`[ConfigRepo] removeBackend: ${id} removed (tombstone written, remote cleaned)`);
2165
2174
  }
2175
+ /**
2176
+ * Replace a config-sync backend's parameters in place (same id).
2177
+ *
2178
+ * Probe the new parameters first: if the backend cannot even be created
2179
+ * (e.g. missing token), nothing is touched and the error is rethrown.
2180
+ * Otherwise the old pair is removed, the descriptor rewritten, and a new
2181
+ * pair created against the new target with an initial sync. If that fails,
2182
+ * the old descriptor and pair are restored. The descriptor file is never
2183
+ * tombstoned, so this never delete-then-recreates it.
2184
+ */
2185
+ async replaceBackend(id, type, options, description) {
2186
+ this.assertNotDisposed();
2187
+ if (id === LOCAL_IDB_BACKEND_ID) {
2188
+ throw new Error("Cannot edit the local IndexedDB primary backend");
2189
+ }
2190
+ const all = await this.readAllBackendDescriptors();
2191
+ const before = all.find((b) => b.id === id);
2192
+ if (!before) {
2193
+ throw new Error(`Backend "${id}" not found`);
2194
+ }
2195
+ const probe = await createBackend({ type, options });
2196
+ try {
2197
+ await probe.dispose?.();
2198
+ } catch {
2199
+ }
2200
+ const newKey = backendDedupKey({ id, type, options });
2201
+ const dup = all.find((b) => b.id !== id && backendDedupKey(b) === newKey);
2202
+ if (dup) {
2203
+ throw new Error(
2204
+ `\u65B0\u53C2\u6570\u4E0E\u5DF2\u6709\u540E\u7AEF\u300C${dup.id}\u300D\u6307\u5411\u540C\u4E00\u8FDC\u7AEF\uFF08\u7C7B\u578B\u4E0E\u53C2\u6570\u5B8C\u5168\u76F8\u540C\uFF09\uFF0C\u8BF7\u5148\u5220\u9664\u91CD\u590D\u540E\u7AEF\u300C${dup.id}\u300D\u6216\u6539\u7528\u4E0D\u540C\u53C2\u6570`
2205
+ );
2206
+ }
2207
+ const oldReplica = this.replicaBackends.get(id);
2208
+ if (oldReplica) {
2209
+ this.syncEngine.removePair(oldReplica.pairId);
2210
+ this.replicaBackends.delete(id);
2211
+ try {
2212
+ await oldReplica.instance?.dispose?.();
2213
+ } catch {
2214
+ }
2215
+ }
2216
+ try {
2217
+ await this.setupReplica(id, type, options, description ?? before.description);
2218
+ } catch (err) {
2219
+ const reason = err instanceof Error ? err.message : String(err);
2220
+ log2.warn(`[ConfigRepo] replaceBackend: "${id}" failed, rolling back old options:`, err);
2221
+ try {
2222
+ await this.setupReplica(id, before.type, before.options, before.description);
2223
+ } catch (rollbackErr) {
2224
+ log2.error(`[ConfigRepo] replaceBackend: rollback for "${id}" ALSO failed:`, rollbackErr);
2225
+ }
2226
+ throw new Error(`\u66F4\u65B0\u540E\u7AEF\u53C2\u6570\u5931\u8D25\uFF1A${reason}\uFF08\u5DF2\u5C1D\u8BD5\u56DE\u6EDA\u65E7\u53C2\u6570\uFF09`);
2227
+ }
2228
+ }
2166
2229
  // -----------------------------------------------------------------------
2167
2230
  // IConfigRepo — Group Type
2168
2231
  // -----------------------------------------------------------------------
@@ -2298,6 +2361,73 @@ var ConfigRepo = class {
2298
2361
  const version = await incrementVersion(this.fullFS, descPath, bytes, author);
2299
2362
  await this.writeVersionSidecar(descPath, version);
2300
2363
  }
2364
+ /**
2365
+ * Replace a data-sync backend's parameters in place (same backend id).
2366
+ *
2367
+ * Changing parameters means switching to a different remote target: the live
2368
+ * group is disposed (old pairs torn down), the descriptor is rewritten, and a
2369
+ * fresh group instance creates pairs + performs the initial sync. Old remote
2370
+ * data is NOT migrated. If the new options cannot be instantiated, nothing is
2371
+ * touched; if re-initialisation fails, the old descriptor is restored.
2372
+ */
2373
+ async replaceAppDataGroupBackend(groupId, backendId, type, options, description) {
2374
+ this.assertNotDisposed();
2375
+ const descriptor = await this.readAppDataGroupDescriptor(groupId);
2376
+ if (!descriptor) {
2377
+ throw new Error(`App data group "${groupId}" not found`);
2378
+ }
2379
+ const index = descriptor.backends.findIndex((b) => b.id === backendId);
2380
+ if (index < 0) {
2381
+ throw new Error(`Backend "${backendId}" not found in data group "${groupId}"`);
2382
+ }
2383
+ const candidate = { ...descriptor.backends[index], type, options, description: description ?? descriptor.backends[index].description };
2384
+ const mergedOptions = await this.resolveAppDataBackendOptions(candidate);
2385
+ const probe = await createBackend({ type, options: mergedOptions });
2386
+ try {
2387
+ await probe.dispose?.();
2388
+ } catch {
2389
+ }
2390
+ const oldBackends = descriptor.backends.map((b) => ({ ...b }));
2391
+ const nextBackends = descriptor.backends.map(
2392
+ (b, i) => i === index ? { ...candidate, options: mergedOptions } : b
2393
+ );
2394
+ const live = this.appDataGroups.get(groupId);
2395
+ if (live) {
2396
+ await live.dispose();
2397
+ this.appDataGroups.delete(groupId);
2398
+ }
2399
+ await this.updateAppDataGroupDescriptor(groupId, nextBackends);
2400
+ try {
2401
+ const group = new AppDataGroupImpl(
2402
+ groupId,
2403
+ this.appId,
2404
+ nextBackends,
2405
+ this.pollIntervalMs,
2406
+ this
2407
+ );
2408
+ await group.init();
2409
+ this.appDataGroups.set(groupId, group);
2410
+ return group;
2411
+ } catch (err) {
2412
+ const reason = err instanceof Error ? err.message : String(err);
2413
+ log2.warn(`[ConfigRepo] replaceAppDataGroupBackend: "${backendId}" failed, rolling back:`, err);
2414
+ try {
2415
+ await this.updateAppDataGroupDescriptor(groupId, oldBackends);
2416
+ const restored = new AppDataGroupImpl(
2417
+ groupId,
2418
+ this.appId,
2419
+ oldBackends,
2420
+ this.pollIntervalMs,
2421
+ this
2422
+ );
2423
+ await restored.init();
2424
+ this.appDataGroups.set(groupId, restored);
2425
+ } catch (rollbackErr) {
2426
+ log2.error("[ConfigRepo] replaceAppDataGroupBackend: rollback ALSO failed:", rollbackErr);
2427
+ }
2428
+ throw new Error(`\u66F4\u65B0\u540E\u7AEF\u53C2\u6570\u5931\u8D25\uFF1A${reason}\uFF08\u5DF2\u5C1D\u8BD5\u56DE\u6EDA\u65E7\u53C2\u6570\uFF09`);
2429
+ }
2430
+ }
2301
2431
  async getConfigSyncGroupDescriptor() {
2302
2432
  this.assertNotDisposed();
2303
2433
  try {
@@ -28653,6 +28653,14 @@ var ZenFSConfig = (() => {
28653
28653
  `Backend "${id}" has the same configuration as existing backend "${dup.id}" (type=${type}). Use removeBackend("${dup.id}") first, or connect with the existing backend's ID.`
28654
28654
  );
28655
28655
  }
28656
+ await this.setupReplica(id, type, options, description);
28657
+ }
28658
+ /**
28659
+ * Instantiate a config-sync replica, persist its descriptor, create its
28660
+ * sync pair, register lifecycle hooks and run the initial sync/watch.
28661
+ * Shared by addBackend() and replaceBackend().
28662
+ */
28663
+ async setupReplica(id, type, options, description) {
28656
28664
  log6.log(`[ConfigRepo] addBackend: creating ${id} (${type})...`);
28657
28665
  const instance = await createBackend({ type, options });
28658
28666
  const syncable = backendToSyncableFS(instance, `${type}(${id})`);
@@ -28692,6 +28700,7 @@ var ZenFSConfig = (() => {
28692
28700
  log6.log(`[ConfigRepo] addBackend: ${id} (${type}) added, sync pair=${pair.pairId}`);
28693
28701
  await this.syncMetaToReplicas();
28694
28702
  this.syncEngine.watch(pair.pairId);
28703
+ return pair.pairId;
28695
28704
  }
28696
28705
  async removeBackend(id) {
28697
28706
  this.assertNotDisposed();
@@ -28728,6 +28737,60 @@ var ZenFSConfig = (() => {
28728
28737
  this.schedulePostDeleteSync();
28729
28738
  log6.log(`[ConfigRepo] removeBackend: ${id} removed (tombstone written, remote cleaned)`);
28730
28739
  }
28740
+ /**
28741
+ * Replace a config-sync backend's parameters in place (same id).
28742
+ *
28743
+ * Probe the new parameters first: if the backend cannot even be created
28744
+ * (e.g. missing token), nothing is touched and the error is rethrown.
28745
+ * Otherwise the old pair is removed, the descriptor rewritten, and a new
28746
+ * pair created against the new target with an initial sync. If that fails,
28747
+ * the old descriptor and pair are restored. The descriptor file is never
28748
+ * tombstoned, so this never delete-then-recreates it.
28749
+ */
28750
+ async replaceBackend(id, type, options, description) {
28751
+ this.assertNotDisposed();
28752
+ if (id === LOCAL_IDB_BACKEND_ID) {
28753
+ throw new Error("Cannot edit the local IndexedDB primary backend");
28754
+ }
28755
+ const all = await this.readAllBackendDescriptors();
28756
+ const before = all.find((b) => b.id === id);
28757
+ if (!before) {
28758
+ throw new Error(`Backend "${id}" not found`);
28759
+ }
28760
+ const probe = await createBackend({ type, options });
28761
+ try {
28762
+ await probe.dispose?.();
28763
+ } catch {
28764
+ }
28765
+ const newKey = backendDedupKey({ id, type, options });
28766
+ const dup = all.find((b) => b.id !== id && backendDedupKey(b) === newKey);
28767
+ if (dup) {
28768
+ throw new Error(
28769
+ `\u65B0\u53C2\u6570\u4E0E\u5DF2\u6709\u540E\u7AEF\u300C${dup.id}\u300D\u6307\u5411\u540C\u4E00\u8FDC\u7AEF\uFF08\u7C7B\u578B\u4E0E\u53C2\u6570\u5B8C\u5168\u76F8\u540C\uFF09\uFF0C\u8BF7\u5148\u5220\u9664\u91CD\u590D\u540E\u7AEF\u300C${dup.id}\u300D\u6216\u6539\u7528\u4E0D\u540C\u53C2\u6570`
28770
+ );
28771
+ }
28772
+ const oldReplica = this.replicaBackends.get(id);
28773
+ if (oldReplica) {
28774
+ this.syncEngine.removePair(oldReplica.pairId);
28775
+ this.replicaBackends.delete(id);
28776
+ try {
28777
+ await oldReplica.instance?.dispose?.();
28778
+ } catch {
28779
+ }
28780
+ }
28781
+ try {
28782
+ await this.setupReplica(id, type, options, description ?? before.description);
28783
+ } catch (err2) {
28784
+ const reason = err2 instanceof Error ? err2.message : String(err2);
28785
+ log6.warn(`[ConfigRepo] replaceBackend: "${id}" failed, rolling back old options:`, err2);
28786
+ try {
28787
+ await this.setupReplica(id, before.type, before.options, before.description);
28788
+ } catch (rollbackErr) {
28789
+ log6.error(`[ConfigRepo] replaceBackend: rollback for "${id}" ALSO failed:`, rollbackErr);
28790
+ }
28791
+ throw new Error(`\u66F4\u65B0\u540E\u7AEF\u53C2\u6570\u5931\u8D25\uFF1A${reason}\uFF08\u5DF2\u5C1D\u8BD5\u56DE\u6EDA\u65E7\u53C2\u6570\uFF09`);
28792
+ }
28793
+ }
28731
28794
  // -----------------------------------------------------------------------
28732
28795
  // IConfigRepo — Group Type
28733
28796
  // -----------------------------------------------------------------------
@@ -28863,6 +28926,73 @@ var ZenFSConfig = (() => {
28863
28926
  const version2 = await incrementVersion(this.fullFS, descPath, bytes, author);
28864
28927
  await this.writeVersionSidecar(descPath, version2);
28865
28928
  }
28929
+ /**
28930
+ * Replace a data-sync backend's parameters in place (same backend id).
28931
+ *
28932
+ * Changing parameters means switching to a different remote target: the live
28933
+ * group is disposed (old pairs torn down), the descriptor is rewritten, and a
28934
+ * fresh group instance creates pairs + performs the initial sync. Old remote
28935
+ * data is NOT migrated. If the new options cannot be instantiated, nothing is
28936
+ * touched; if re-initialisation fails, the old descriptor is restored.
28937
+ */
28938
+ async replaceAppDataGroupBackend(groupId, backendId, type, options, description) {
28939
+ this.assertNotDisposed();
28940
+ const descriptor = await this.readAppDataGroupDescriptor(groupId);
28941
+ if (!descriptor) {
28942
+ throw new Error(`App data group "${groupId}" not found`);
28943
+ }
28944
+ const index = descriptor.backends.findIndex((b) => b.id === backendId);
28945
+ if (index < 0) {
28946
+ throw new Error(`Backend "${backendId}" not found in data group "${groupId}"`);
28947
+ }
28948
+ const candidate = { ...descriptor.backends[index], type, options, description: description ?? descriptor.backends[index].description };
28949
+ const mergedOptions = await this.resolveAppDataBackendOptions(candidate);
28950
+ const probe = await createBackend({ type, options: mergedOptions });
28951
+ try {
28952
+ await probe.dispose?.();
28953
+ } catch {
28954
+ }
28955
+ const oldBackends = descriptor.backends.map((b) => ({ ...b }));
28956
+ const nextBackends = descriptor.backends.map(
28957
+ (b, i) => i === index ? { ...candidate, options: mergedOptions } : b
28958
+ );
28959
+ const live = this.appDataGroups.get(groupId);
28960
+ if (live) {
28961
+ await live.dispose();
28962
+ this.appDataGroups.delete(groupId);
28963
+ }
28964
+ await this.updateAppDataGroupDescriptor(groupId, nextBackends);
28965
+ try {
28966
+ const group = new AppDataGroupImpl(
28967
+ groupId,
28968
+ this.appId,
28969
+ nextBackends,
28970
+ this.pollIntervalMs,
28971
+ this
28972
+ );
28973
+ await group.init();
28974
+ this.appDataGroups.set(groupId, group);
28975
+ return group;
28976
+ } catch (err2) {
28977
+ const reason = err2 instanceof Error ? err2.message : String(err2);
28978
+ log6.warn(`[ConfigRepo] replaceAppDataGroupBackend: "${backendId}" failed, rolling back:`, err2);
28979
+ try {
28980
+ await this.updateAppDataGroupDescriptor(groupId, oldBackends);
28981
+ const restored = new AppDataGroupImpl(
28982
+ groupId,
28983
+ this.appId,
28984
+ oldBackends,
28985
+ this.pollIntervalMs,
28986
+ this
28987
+ );
28988
+ await restored.init();
28989
+ this.appDataGroups.set(groupId, restored);
28990
+ } catch (rollbackErr) {
28991
+ log6.error("[ConfigRepo] replaceAppDataGroupBackend: rollback ALSO failed:", rollbackErr);
28992
+ }
28993
+ throw new Error(`\u66F4\u65B0\u540E\u7AEF\u53C2\u6570\u5931\u8D25\uFF1A${reason}\uFF08\u5DF2\u5C1D\u8BD5\u56DE\u6EDA\u65E7\u53C2\u6570\uFF09`);
28994
+ }
28995
+ }
28866
28996
  async getConfigSyncGroupDescriptor() {
28867
28997
  this.assertNotDisposed();
28868
28998
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-fs-config",
3
- "version": "0.5.46",
3
+ "version": "0.5.47",
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",