zen-fs-config 0.9.4 → 0.9.6

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
  *
@@ -2587,4 +2605,124 @@ declare class ConflictStore {
2587
2605
  resolve(conflictId: string, mergedContent: string | Uint8Array): Promise<void>;
2588
2606
  }
2589
2607
 
2590
- export { type AddBackendResult, type AppDataBackendDescriptor, type AppDataGroup, type AppDataGroupDescriptor, type AppRepo, BackendAlreadyExistsError, type BackendConflictInfo, type BackendConflictReason, type BackendConflictResolution, type BackendConflictResolutionResult, type BackendDescriptor, BackendEndpointConflictError, type BackendFactory, BackendIdentityError, type BackendInstance, type BackendMetadata, type BackendParamDef, type BackendSyncState, type BackendsMeta, CONFIG_DIR, CONFLICTS_DIR, type CacheOptions, type ConfigBackendInput, ConfigNotReadyError, ConfigRepo, type ConfigRepoOptions, type ConfigSerializer, type ConfigSyncGroupDescriptor, type ConfigSyncGroupHandle, type ConflictArchive, type ConflictInfo, ConflictStore, ConnectFailedError, type ConnectOptions, type ConnectResult, ConnectV2Error, type ConnectV2Options, DATA_GROUP_DEFAULT_FEATURES, type DataBackendInput, type DataGroup, DataGroupBackendError, DataGroupCreateError, type DataGroupFlushResult, type DataGroupInfo, DataGroupNotFoundError, DataGroupOperationError, type DataGroupSyncEvent, type DataGroupSyncProgress, type DataGroupSyncResult, FolderStore, GROUP_DESCRIPTOR_FILE, type GroupDescriptor, type GroupIdentity, type GroupInstanceIdRootResult, type GroupProbeResult, type GroupProbeState, type IConfigRepo, type IdentityBindingResult, type IdentityCandidate, type IdentityCandidateSource, type IdentityConflictReason, type IdentityResolutionMode, type IdentityResolutionResult, InvalidBackendError, type KeepPurgeResult, LEGACY_CONFLICTS_DIR, LEGACY_TOMBSTONES_DIR, LOCAL_IDB_BACKEND_ID, LOCAL_STORAGE_UNAVAILABLE, type LocalStorageUnavailableDetails, LocalStorageUnavailableError, type LocalStoreDeletionResult, META_DIR, type MigrateLocalFS, type MigrateReplicaFS, type MtimePurgeResult, NeedsInitError, type PurgeKeepOptions, type PurgeLocalSyncConfigOptions, type PurgeLocalSyncConfigResult, type PurgeMtimeOptions, type PurgeableFS$1 as PurgeableFS, type RemoteIdentitySnapshot, type RemoteIdentityState, RemoteUnreachableError, type ReplicaSetupSummary, type SetGroupInstanceIdOptions, type SetGroupInstanceIdResult, type SyncGroupFeatures, type SyncGroupHandle, type SyncGroupKind, type SyncGroupSyncEvent, type SyncGroupType, TOMBSTONES_DIR, type TombstoneMeta, TombstoneStore, type UiConnection, VERSIONS_DIR, type VersionMeta, type VersionMigrationOptions, type VersionMigrationResult, VersionStore, bindRemoteGroupIdentity, bindRemoteGroupIdentityDetailed, configKeyToFilePath, connect, connectV2, connectionToAppRepo, createBackend, createConfigRepo, createSerializerChain, dataGroupLocalStoreName, deleteDataGroupLocalStore, deleteIndexedDbStore, encodeMetaPath, getAccountFields, getBackendMetadata, getExtension, getUiConnection, hasBackend, incrementVersion, isBrowserEnv, isKeepFile, isLocalStorageUnavailableError, isMtimeSidecar, isValidGroupInstanceId, legacyVersionPathFor, listBackendMetadata, listBackends, localPrimaryType, localSyncStoreNames, mergeAccountFields, migrateVersionSidecars, newGroupInstanceId, probeGroupTypeDetailed, purgeKeepFiles, purgeLocalSyncConfig, purgeMtimeSidecars, readGroupDescriptor, readVersion, registerBackend, registerFolderBackend, resolveLocalPrimary, resolveRemoteIdentity, sha256, shortInstanceId, summarizeIdentity, unregisterBackend, verifyOrRepairVersion, versionPathFor, wrapZenFSFileSystem, writeGroupDescriptor, writeVersion };
2608
+ /**
2609
+ * 数据组选择(跨 app 通用)
2610
+ *
2611
+ * 一个 appId 下可能存在多个数据同步组:历史 `default` 空组、换过后端的旧组、
2612
+ * 新建组。旧写法「取 `appDataGroups[0]`」会挂错组,数据写进空组、看起来像
2613
+ * 「没同步」。这一层解决「该挂哪个组」:
2614
+ *
2615
+ * 1. 用户在界面上显式选过的组(本机偏好,localStorage);
2616
+ * 2. 目标目录下确实已有内容的组;
2617
+ * 3. 带远端后端的组(唯一一个时直接用);
2618
+ * 4. 只有一个候选时用它。
2619
+ *
2620
+ * 多个候选且无法判定时返回 `null`,由界面列出来让用户选 —— 绝不静默择一。
2621
+ *
2622
+ * 这些逻辑与具体应用无关,所以放在库里;应用只提供 `appId` 与自己的数据目录
2623
+ * (如 `/golf`、`/bookkeeping`)。
2624
+ *
2625
+ * 作用对象既可以是 `ConfigRepo`,也可以是配置面板 `onConnected` 交出的
2626
+ * `{ repo, dataGroup }` 句柄:函数内部统一归一化,避免各应用各写一份适配。
2627
+ */
2628
+
2629
+ /** 该后端类型是否只是本机存储(不参与跨设备同步)。 */
2630
+ declare function isLocalBackendType(type: string): boolean;
2631
+ /** 默认 localStorage 键前缀:`zen-fs-config:data-group:{appId}`。 */
2632
+ declare const DATA_GROUP_PREFERENCE_PREFIX = "zen-fs-config:data-group:";
2633
+ /** 最小 storage 形状(浏览器 localStorage / 测试替身)。 */
2634
+ interface PreferenceStorage {
2635
+ getItem(key: string): string | null;
2636
+ setItem(key: string, value: string): void;
2637
+ removeItem(key: string): void;
2638
+ }
2639
+ interface PreferenceOptions {
2640
+ storage?: PreferenceStorage;
2641
+ /** 写入用的键前缀,默认 {@link DATA_GROUP_PREFERENCE_PREFIX}。 */
2642
+ keyPrefix?: string;
2643
+ /**
2644
+ * 读取时依次尝试的键前缀(默认只用 `keyPrefix`)。
2645
+ * 迁移场景可把旧产品自己的键放进来,例如 `['zen-fs-config:data-group:', 'onenav:data-group:']`。
2646
+ */
2647
+ readPrefixes?: string[];
2648
+ }
2649
+ /** 本机为该 appId 记住的数据组 id(没有则 `null`)。 */
2650
+ declare function getPreferredDataGroupId(appId: string, opts?: PreferenceOptions): string | null;
2651
+ /** 记住(`id === null` 时清除)本机为该 appId 选定的数据组。 */
2652
+ declare function setPreferredDataGroupId(appId: string, id: string | null, opts?: PreferenceOptions): void;
2653
+ /** 一个候选数据组(配置仓描述符 + 已实例化的组)。 */
2654
+ interface DataGroupCandidate {
2655
+ id: string;
2656
+ name?: string;
2657
+ group: AppDataGroup;
2658
+ /** 组里是否有非本地后端(即真能跨设备同步) */
2659
+ hasRemote: boolean;
2660
+ /** 后端类型列表,便于界面展示 */
2661
+ backendTypes: string[];
2662
+ /** 目标目录下是否已有内容(用于判断「哪个组才是真正装了数据的」) */
2663
+ hasData: boolean;
2664
+ /**
2665
+ * 该组的隐式本地主存储名(浏览器里是 IndexedDB)。
2666
+ *
2667
+ * 每个数据组都内置一个 local primary,它是组的本地 source / 离线缓存,
2668
+ * 不参与远端同步、也不出现在组描述符里;不能把它当成一个独立可选的数据组。
2669
+ */
2670
+ localStore: string;
2671
+ }
2672
+ /** 数据组来源:`ConfigRepo` 本身,或配置面板 `onConnected` 交出的句柄。 */
2673
+ type DataGroupSource = IConfigRepo | {
2674
+ repo?: IConfigRepo | null;
2675
+ dataGroup?: AppDataGroup | null;
2676
+ };
2677
+ /** 组在 `path` 下是否至少有一个条目(读失败/未连接都算空)。 */
2678
+ declare function groupHasPath(group: AppDataGroup, path: string): Promise<boolean>;
2679
+ /**
2680
+ * 列出当前 appId 的所有数据组候选(带「是否有数据 / 是否有远端后端」)。
2681
+ *
2682
+ * @param source `ConfigRepo` 或 `{ repo, dataGroup }`
2683
+ * @param appId 应用 id(与 `connect()` 用的是同一个)
2684
+ * @param opts.path 判断「组里是否已有数据」的目录(通常是应用自己的数据目录)
2685
+ */
2686
+ declare function listDataGroupCandidates(source: DataGroupSource, appId: string, opts?: {
2687
+ path?: string;
2688
+ }): Promise<DataGroupCandidate[]>;
2689
+ /** 兼容旧命名(应用侧曾叫 collectDataGroupCandidates)。 */
2690
+ declare const collectDataGroupCandidates: typeof listDataGroupCandidates;
2691
+ interface ResolveDataGroupOptions {
2692
+ /** 显式指定的组 id;缺省读本机偏好。 */
2693
+ preferGroupId?: string | null;
2694
+ /** 应用自己的数据目录,用于「哪个组里真有数据」的判定。 */
2695
+ path?: string;
2696
+ preference?: PreferenceOptions;
2697
+ }
2698
+ /**
2699
+ * 按「偏好 → 有数据 → 唯一远端 → 单候选」的顺序解析出要挂载的组。
2700
+ * 多个候选且无法判定时返回 `null`,交给界面让用户选。
2701
+ */
2702
+ declare function resolveDataGroup(source: DataGroupSource, appId: string, opts?: ResolveDataGroupOptions): Promise<AppDataGroup | null>;
2703
+ interface EnsureDataGroupOptions extends ResolveDataGroupOptions {
2704
+ /** 是否允许在「一个描述符都没有」时补建本机默认组(默认允许)。 */
2705
+ allowCreateLocal?: boolean;
2706
+ }
2707
+ interface EnsureDataGroupResult {
2708
+ group: AppDataGroup | null;
2709
+ candidates: DataGroupCandidate[];
2710
+ /** 本次是否新建了本机默认组(界面据此提示「仅本地」) */
2711
+ created: boolean;
2712
+ /** 有描述符但一个都加载不出来(此时不建 default,避免与远端组冲突) */
2713
+ descriptorsUnavailable: boolean;
2714
+ }
2715
+ /**
2716
+ * 确保某个 appId **至少有一个可用的数据组实例**,没有就建一个本机默认组。
2717
+ *
2718
+ * 数据组不是「只有配了远端后端才有」:每个组都自带隐式本地主存储。应用要在
2719
+ * 数据组里读写就必须有一个组实例,否则页面只能停在「还没有数据组」。
2720
+ *
2721
+ * - 已有描述符:按 {@link resolveDataGroup} 的规则选;
2722
+ * - **一个描述符都没有**:`createAppDataGroup('default', [])`,得到一个只有
2723
+ * 隐式本地存储的组(不参与远端同步,之后可再加远端后端);
2724
+ * - **有描述符但都加载失败**:绝不建 default,返回 `descriptorsUnavailable`。
2725
+ */
2726
+ declare function ensureDataGroup(source: DataGroupSource, appId: string, opts?: EnsureDataGroupOptions): Promise<EnsureDataGroupResult>;
2727
+
2728
+ export { type AddBackendResult, type AppDataBackendDescriptor, type AppDataGroup, type AppDataGroupDescriptor, type AppRepo, BackendAlreadyExistsError, type BackendConflictInfo, type BackendConflictReason, type BackendConflictResolution, type BackendConflictResolutionResult, type BackendDescriptor, BackendEndpointConflictError, type BackendFactory, BackendIdentityError, type BackendInstance, type BackendMetadata, type BackendParamDef, type BackendSyncState, type BackendsMeta, CONFIG_DIR, CONFLICTS_DIR, type CacheOptions, type ConfigBackendInput, ConfigNotReadyError, ConfigRepo, type ConfigRepoOptions, type ConfigSerializer, type ConfigSyncGroupDescriptor, type ConfigSyncGroupHandle, type ConflictArchive, type ConflictInfo, ConflictStore, ConnectFailedError, type ConnectOptions, type ConnectResult, ConnectV2Error, type ConnectV2Options, DATA_GROUP_DEFAULT_FEATURES, DATA_GROUP_PREFERENCE_PREFIX, type DataBackendInput, type DataGroup, DataGroupBackendError, type DataGroupCandidate, DataGroupCreateError, type DataGroupFlushResult, type DataGroupInfo, DataGroupNotFoundError, DataGroupOperationError, type DataGroupSource, type DataGroupSyncEvent, type DataGroupSyncProgress, type DataGroupSyncResult, type EnsureDataGroupOptions, type EnsureDataGroupResult, FolderStore, GROUP_DESCRIPTOR_FILE, type GroupDescriptor, type GroupIdentity, type GroupInstanceIdRootResult, type GroupProbeResult, type GroupProbeState, type IConfigRepo, type IdentityBindingResult, type IdentityCandidate, type IdentityCandidateSource, type IdentityConflictReason, type IdentityResolutionMode, type IdentityResolutionResult, InvalidBackendError, type KeepPurgeResult, LEGACY_CONFLICTS_DIR, LEGACY_TOMBSTONES_DIR, LOCAL_IDB_BACKEND_ID, LOCAL_STORAGE_UNAVAILABLE, type LocalStorageUnavailableDetails, LocalStorageUnavailableError, type LocalStoreDeletionResult, META_DIR, type MigrateLocalFS, type MigrateReplicaFS, type MtimePurgeResult, NeedsInitError, type PreferenceOptions, type PreferenceStorage, type PurgeKeepOptions, type PurgeLocalSyncConfigOptions, type PurgeLocalSyncConfigResult, type PurgeMtimeOptions, type PurgeableFS$1 as PurgeableFS, type RemoteIdentitySnapshot, type RemoteIdentityState, RemoteUnreachableError, type ReplicaSetupSummary, type ResolveDataGroupOptions, type SetGroupInstanceIdOptions, type SetGroupInstanceIdResult, type SyncGroupFeatures, type SyncGroupHandle, type SyncGroupKind, type SyncGroupSyncEvent, type SyncGroupType, TOMBSTONES_DIR, type TombstoneMeta, TombstoneStore, type UiConnection, VERSIONS_DIR, type VersionMeta, type VersionMigrationOptions, type VersionMigrationResult, VersionStore, bindRemoteGroupIdentity, bindRemoteGroupIdentityDetailed, collectDataGroupCandidates, configKeyToFilePath, connect, connectV2, connectionToAppRepo, createBackend, createConfigRepo, createSerializerChain, dataGroupLocalStoreName, deleteDataGroupLocalStore, deleteIndexedDbStore, encodeMetaPath, ensureDataGroup, getAccountFields, getBackendMetadata, getExtension, getPreferredDataGroupId, getUiConnection, groupHasPath, hasBackend, incrementVersion, isBrowserEnv, isKeepFile, isLocalBackendType, isLocalStorageUnavailableError, isMtimeSidecar, isValidGroupInstanceId, legacyVersionPathFor, listBackendMetadata, listBackends, listDataGroupCandidates, localPrimaryType, localSyncStoreNames, mergeAccountFields, migrateVersionSidecars, newGroupInstanceId, probeGroupTypeDetailed, purgeKeepFiles, purgeLocalSyncConfig, purgeMtimeSidecars, readGroupDescriptor, readVersion, registerBackend, registerFolderBackend, resolveDataGroup, resolveLocalPrimary, resolveRemoteIdentity, setPreferredDataGroupId, sha256, shortInstanceId, summarizeIdentity, unregisterBackend, verifyOrRepairVersion, versionPathFor, wrapZenFSFileSystem, writeGroupDescriptor, writeVersion };
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
  *
@@ -2587,4 +2605,124 @@ declare class ConflictStore {
2587
2605
  resolve(conflictId: string, mergedContent: string | Uint8Array): Promise<void>;
2588
2606
  }
2589
2607
 
2590
- export { type AddBackendResult, type AppDataBackendDescriptor, type AppDataGroup, type AppDataGroupDescriptor, type AppRepo, BackendAlreadyExistsError, type BackendConflictInfo, type BackendConflictReason, type BackendConflictResolution, type BackendConflictResolutionResult, type BackendDescriptor, BackendEndpointConflictError, type BackendFactory, BackendIdentityError, type BackendInstance, type BackendMetadata, type BackendParamDef, type BackendSyncState, type BackendsMeta, CONFIG_DIR, CONFLICTS_DIR, type CacheOptions, type ConfigBackendInput, ConfigNotReadyError, ConfigRepo, type ConfigRepoOptions, type ConfigSerializer, type ConfigSyncGroupDescriptor, type ConfigSyncGroupHandle, type ConflictArchive, type ConflictInfo, ConflictStore, ConnectFailedError, type ConnectOptions, type ConnectResult, ConnectV2Error, type ConnectV2Options, DATA_GROUP_DEFAULT_FEATURES, type DataBackendInput, type DataGroup, DataGroupBackendError, DataGroupCreateError, type DataGroupFlushResult, type DataGroupInfo, DataGroupNotFoundError, DataGroupOperationError, type DataGroupSyncEvent, type DataGroupSyncProgress, type DataGroupSyncResult, FolderStore, GROUP_DESCRIPTOR_FILE, type GroupDescriptor, type GroupIdentity, type GroupInstanceIdRootResult, type GroupProbeResult, type GroupProbeState, type IConfigRepo, type IdentityBindingResult, type IdentityCandidate, type IdentityCandidateSource, type IdentityConflictReason, type IdentityResolutionMode, type IdentityResolutionResult, InvalidBackendError, type KeepPurgeResult, LEGACY_CONFLICTS_DIR, LEGACY_TOMBSTONES_DIR, LOCAL_IDB_BACKEND_ID, LOCAL_STORAGE_UNAVAILABLE, type LocalStorageUnavailableDetails, LocalStorageUnavailableError, type LocalStoreDeletionResult, META_DIR, type MigrateLocalFS, type MigrateReplicaFS, type MtimePurgeResult, NeedsInitError, type PurgeKeepOptions, type PurgeLocalSyncConfigOptions, type PurgeLocalSyncConfigResult, type PurgeMtimeOptions, type PurgeableFS$1 as PurgeableFS, type RemoteIdentitySnapshot, type RemoteIdentityState, RemoteUnreachableError, type ReplicaSetupSummary, type SetGroupInstanceIdOptions, type SetGroupInstanceIdResult, type SyncGroupFeatures, type SyncGroupHandle, type SyncGroupKind, type SyncGroupSyncEvent, type SyncGroupType, TOMBSTONES_DIR, type TombstoneMeta, TombstoneStore, type UiConnection, VERSIONS_DIR, type VersionMeta, type VersionMigrationOptions, type VersionMigrationResult, VersionStore, bindRemoteGroupIdentity, bindRemoteGroupIdentityDetailed, configKeyToFilePath, connect, connectV2, connectionToAppRepo, createBackend, createConfigRepo, createSerializerChain, dataGroupLocalStoreName, deleteDataGroupLocalStore, deleteIndexedDbStore, encodeMetaPath, getAccountFields, getBackendMetadata, getExtension, getUiConnection, hasBackend, incrementVersion, isBrowserEnv, isKeepFile, isLocalStorageUnavailableError, isMtimeSidecar, isValidGroupInstanceId, legacyVersionPathFor, listBackendMetadata, listBackends, localPrimaryType, localSyncStoreNames, mergeAccountFields, migrateVersionSidecars, newGroupInstanceId, probeGroupTypeDetailed, purgeKeepFiles, purgeLocalSyncConfig, purgeMtimeSidecars, readGroupDescriptor, readVersion, registerBackend, registerFolderBackend, resolveLocalPrimary, resolveRemoteIdentity, sha256, shortInstanceId, summarizeIdentity, unregisterBackend, verifyOrRepairVersion, versionPathFor, wrapZenFSFileSystem, writeGroupDescriptor, writeVersion };
2608
+ /**
2609
+ * 数据组选择(跨 app 通用)
2610
+ *
2611
+ * 一个 appId 下可能存在多个数据同步组:历史 `default` 空组、换过后端的旧组、
2612
+ * 新建组。旧写法「取 `appDataGroups[0]`」会挂错组,数据写进空组、看起来像
2613
+ * 「没同步」。这一层解决「该挂哪个组」:
2614
+ *
2615
+ * 1. 用户在界面上显式选过的组(本机偏好,localStorage);
2616
+ * 2. 目标目录下确实已有内容的组;
2617
+ * 3. 带远端后端的组(唯一一个时直接用);
2618
+ * 4. 只有一个候选时用它。
2619
+ *
2620
+ * 多个候选且无法判定时返回 `null`,由界面列出来让用户选 —— 绝不静默择一。
2621
+ *
2622
+ * 这些逻辑与具体应用无关,所以放在库里;应用只提供 `appId` 与自己的数据目录
2623
+ * (如 `/golf`、`/bookkeeping`)。
2624
+ *
2625
+ * 作用对象既可以是 `ConfigRepo`,也可以是配置面板 `onConnected` 交出的
2626
+ * `{ repo, dataGroup }` 句柄:函数内部统一归一化,避免各应用各写一份适配。
2627
+ */
2628
+
2629
+ /** 该后端类型是否只是本机存储(不参与跨设备同步)。 */
2630
+ declare function isLocalBackendType(type: string): boolean;
2631
+ /** 默认 localStorage 键前缀:`zen-fs-config:data-group:{appId}`。 */
2632
+ declare const DATA_GROUP_PREFERENCE_PREFIX = "zen-fs-config:data-group:";
2633
+ /** 最小 storage 形状(浏览器 localStorage / 测试替身)。 */
2634
+ interface PreferenceStorage {
2635
+ getItem(key: string): string | null;
2636
+ setItem(key: string, value: string): void;
2637
+ removeItem(key: string): void;
2638
+ }
2639
+ interface PreferenceOptions {
2640
+ storage?: PreferenceStorage;
2641
+ /** 写入用的键前缀,默认 {@link DATA_GROUP_PREFERENCE_PREFIX}。 */
2642
+ keyPrefix?: string;
2643
+ /**
2644
+ * 读取时依次尝试的键前缀(默认只用 `keyPrefix`)。
2645
+ * 迁移场景可把旧产品自己的键放进来,例如 `['zen-fs-config:data-group:', 'onenav:data-group:']`。
2646
+ */
2647
+ readPrefixes?: string[];
2648
+ }
2649
+ /** 本机为该 appId 记住的数据组 id(没有则 `null`)。 */
2650
+ declare function getPreferredDataGroupId(appId: string, opts?: PreferenceOptions): string | null;
2651
+ /** 记住(`id === null` 时清除)本机为该 appId 选定的数据组。 */
2652
+ declare function setPreferredDataGroupId(appId: string, id: string | null, opts?: PreferenceOptions): void;
2653
+ /** 一个候选数据组(配置仓描述符 + 已实例化的组)。 */
2654
+ interface DataGroupCandidate {
2655
+ id: string;
2656
+ name?: string;
2657
+ group: AppDataGroup;
2658
+ /** 组里是否有非本地后端(即真能跨设备同步) */
2659
+ hasRemote: boolean;
2660
+ /** 后端类型列表,便于界面展示 */
2661
+ backendTypes: string[];
2662
+ /** 目标目录下是否已有内容(用于判断「哪个组才是真正装了数据的」) */
2663
+ hasData: boolean;
2664
+ /**
2665
+ * 该组的隐式本地主存储名(浏览器里是 IndexedDB)。
2666
+ *
2667
+ * 每个数据组都内置一个 local primary,它是组的本地 source / 离线缓存,
2668
+ * 不参与远端同步、也不出现在组描述符里;不能把它当成一个独立可选的数据组。
2669
+ */
2670
+ localStore: string;
2671
+ }
2672
+ /** 数据组来源:`ConfigRepo` 本身,或配置面板 `onConnected` 交出的句柄。 */
2673
+ type DataGroupSource = IConfigRepo | {
2674
+ repo?: IConfigRepo | null;
2675
+ dataGroup?: AppDataGroup | null;
2676
+ };
2677
+ /** 组在 `path` 下是否至少有一个条目(读失败/未连接都算空)。 */
2678
+ declare function groupHasPath(group: AppDataGroup, path: string): Promise<boolean>;
2679
+ /**
2680
+ * 列出当前 appId 的所有数据组候选(带「是否有数据 / 是否有远端后端」)。
2681
+ *
2682
+ * @param source `ConfigRepo` 或 `{ repo, dataGroup }`
2683
+ * @param appId 应用 id(与 `connect()` 用的是同一个)
2684
+ * @param opts.path 判断「组里是否已有数据」的目录(通常是应用自己的数据目录)
2685
+ */
2686
+ declare function listDataGroupCandidates(source: DataGroupSource, appId: string, opts?: {
2687
+ path?: string;
2688
+ }): Promise<DataGroupCandidate[]>;
2689
+ /** 兼容旧命名(应用侧曾叫 collectDataGroupCandidates)。 */
2690
+ declare const collectDataGroupCandidates: typeof listDataGroupCandidates;
2691
+ interface ResolveDataGroupOptions {
2692
+ /** 显式指定的组 id;缺省读本机偏好。 */
2693
+ preferGroupId?: string | null;
2694
+ /** 应用自己的数据目录,用于「哪个组里真有数据」的判定。 */
2695
+ path?: string;
2696
+ preference?: PreferenceOptions;
2697
+ }
2698
+ /**
2699
+ * 按「偏好 → 有数据 → 唯一远端 → 单候选」的顺序解析出要挂载的组。
2700
+ * 多个候选且无法判定时返回 `null`,交给界面让用户选。
2701
+ */
2702
+ declare function resolveDataGroup(source: DataGroupSource, appId: string, opts?: ResolveDataGroupOptions): Promise<AppDataGroup | null>;
2703
+ interface EnsureDataGroupOptions extends ResolveDataGroupOptions {
2704
+ /** 是否允许在「一个描述符都没有」时补建本机默认组(默认允许)。 */
2705
+ allowCreateLocal?: boolean;
2706
+ }
2707
+ interface EnsureDataGroupResult {
2708
+ group: AppDataGroup | null;
2709
+ candidates: DataGroupCandidate[];
2710
+ /** 本次是否新建了本机默认组(界面据此提示「仅本地」) */
2711
+ created: boolean;
2712
+ /** 有描述符但一个都加载不出来(此时不建 default,避免与远端组冲突) */
2713
+ descriptorsUnavailable: boolean;
2714
+ }
2715
+ /**
2716
+ * 确保某个 appId **至少有一个可用的数据组实例**,没有就建一个本机默认组。
2717
+ *
2718
+ * 数据组不是「只有配了远端后端才有」:每个组都自带隐式本地主存储。应用要在
2719
+ * 数据组里读写就必须有一个组实例,否则页面只能停在「还没有数据组」。
2720
+ *
2721
+ * - 已有描述符:按 {@link resolveDataGroup} 的规则选;
2722
+ * - **一个描述符都没有**:`createAppDataGroup('default', [])`,得到一个只有
2723
+ * 隐式本地存储的组(不参与远端同步,之后可再加远端后端);
2724
+ * - **有描述符但都加载失败**:绝不建 default,返回 `descriptorsUnavailable`。
2725
+ */
2726
+ declare function ensureDataGroup(source: DataGroupSource, appId: string, opts?: EnsureDataGroupOptions): Promise<EnsureDataGroupResult>;
2727
+
2728
+ export { type AddBackendResult, type AppDataBackendDescriptor, type AppDataGroup, type AppDataGroupDescriptor, type AppRepo, BackendAlreadyExistsError, type BackendConflictInfo, type BackendConflictReason, type BackendConflictResolution, type BackendConflictResolutionResult, type BackendDescriptor, BackendEndpointConflictError, type BackendFactory, BackendIdentityError, type BackendInstance, type BackendMetadata, type BackendParamDef, type BackendSyncState, type BackendsMeta, CONFIG_DIR, CONFLICTS_DIR, type CacheOptions, type ConfigBackendInput, ConfigNotReadyError, ConfigRepo, type ConfigRepoOptions, type ConfigSerializer, type ConfigSyncGroupDescriptor, type ConfigSyncGroupHandle, type ConflictArchive, type ConflictInfo, ConflictStore, ConnectFailedError, type ConnectOptions, type ConnectResult, ConnectV2Error, type ConnectV2Options, DATA_GROUP_DEFAULT_FEATURES, DATA_GROUP_PREFERENCE_PREFIX, type DataBackendInput, type DataGroup, DataGroupBackendError, type DataGroupCandidate, DataGroupCreateError, type DataGroupFlushResult, type DataGroupInfo, DataGroupNotFoundError, DataGroupOperationError, type DataGroupSource, type DataGroupSyncEvent, type DataGroupSyncProgress, type DataGroupSyncResult, type EnsureDataGroupOptions, type EnsureDataGroupResult, FolderStore, GROUP_DESCRIPTOR_FILE, type GroupDescriptor, type GroupIdentity, type GroupInstanceIdRootResult, type GroupProbeResult, type GroupProbeState, type IConfigRepo, type IdentityBindingResult, type IdentityCandidate, type IdentityCandidateSource, type IdentityConflictReason, type IdentityResolutionMode, type IdentityResolutionResult, InvalidBackendError, type KeepPurgeResult, LEGACY_CONFLICTS_DIR, LEGACY_TOMBSTONES_DIR, LOCAL_IDB_BACKEND_ID, LOCAL_STORAGE_UNAVAILABLE, type LocalStorageUnavailableDetails, LocalStorageUnavailableError, type LocalStoreDeletionResult, META_DIR, type MigrateLocalFS, type MigrateReplicaFS, type MtimePurgeResult, NeedsInitError, type PreferenceOptions, type PreferenceStorage, type PurgeKeepOptions, type PurgeLocalSyncConfigOptions, type PurgeLocalSyncConfigResult, type PurgeMtimeOptions, type PurgeableFS$1 as PurgeableFS, type RemoteIdentitySnapshot, type RemoteIdentityState, RemoteUnreachableError, type ReplicaSetupSummary, type ResolveDataGroupOptions, type SetGroupInstanceIdOptions, type SetGroupInstanceIdResult, type SyncGroupFeatures, type SyncGroupHandle, type SyncGroupKind, type SyncGroupSyncEvent, type SyncGroupType, TOMBSTONES_DIR, type TombstoneMeta, TombstoneStore, type UiConnection, VERSIONS_DIR, type VersionMeta, type VersionMigrationOptions, type VersionMigrationResult, VersionStore, bindRemoteGroupIdentity, bindRemoteGroupIdentityDetailed, collectDataGroupCandidates, configKeyToFilePath, connect, connectV2, connectionToAppRepo, createBackend, createConfigRepo, createSerializerChain, dataGroupLocalStoreName, deleteDataGroupLocalStore, deleteIndexedDbStore, encodeMetaPath, ensureDataGroup, getAccountFields, getBackendMetadata, getExtension, getPreferredDataGroupId, getUiConnection, groupHasPath, hasBackend, incrementVersion, isBrowserEnv, isKeepFile, isLocalBackendType, isLocalStorageUnavailableError, isMtimeSidecar, isValidGroupInstanceId, legacyVersionPathFor, listBackendMetadata, listBackends, listDataGroupCandidates, localPrimaryType, localSyncStoreNames, mergeAccountFields, migrateVersionSidecars, newGroupInstanceId, probeGroupTypeDetailed, purgeKeepFiles, purgeLocalSyncConfig, purgeMtimeSidecars, readGroupDescriptor, readVersion, registerBackend, registerFolderBackend, resolveDataGroup, resolveLocalPrimary, resolveRemoteIdentity, setPreferredDataGroupId, sha256, shortInstanceId, summarizeIdentity, unregisterBackend, verifyOrRepairVersion, versionPathFor, wrapZenFSFileSystem, writeGroupDescriptor, writeVersion };