zen-fs-config 0.9.4 → 0.9.5
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 +121 -1
- package/dist/index.d.ts +121 -1
- package/dist/index.js +212 -0
- package/dist/index.mjs +203 -0
- package/dist/zen-fs-config.js +202 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -2587,4 +2587,124 @@ declare class ConflictStore {
|
|
|
2587
2587
|
resolve(conflictId: string, mergedContent: string | Uint8Array): Promise<void>;
|
|
2588
2588
|
}
|
|
2589
2589
|
|
|
2590
|
-
|
|
2590
|
+
/**
|
|
2591
|
+
* 数据组选择(跨 app 通用)
|
|
2592
|
+
*
|
|
2593
|
+
* 一个 appId 下可能存在多个数据同步组:历史 `default` 空组、换过后端的旧组、
|
|
2594
|
+
* 新建组。旧写法「取 `appDataGroups[0]`」会挂错组,数据写进空组、看起来像
|
|
2595
|
+
* 「没同步」。这一层解决「该挂哪个组」:
|
|
2596
|
+
*
|
|
2597
|
+
* 1. 用户在界面上显式选过的组(本机偏好,localStorage);
|
|
2598
|
+
* 2. 目标目录下确实已有内容的组;
|
|
2599
|
+
* 3. 带远端后端的组(唯一一个时直接用);
|
|
2600
|
+
* 4. 只有一个候选时用它。
|
|
2601
|
+
*
|
|
2602
|
+
* 多个候选且无法判定时返回 `null`,由界面列出来让用户选 —— 绝不静默择一。
|
|
2603
|
+
*
|
|
2604
|
+
* 这些逻辑与具体应用无关,所以放在库里;应用只提供 `appId` 与自己的数据目录
|
|
2605
|
+
* (如 `/golf`、`/bookkeeping`)。
|
|
2606
|
+
*
|
|
2607
|
+
* 作用对象既可以是 `ConfigRepo`,也可以是配置面板 `onConnected` 交出的
|
|
2608
|
+
* `{ repo, dataGroup }` 句柄:函数内部统一归一化,避免各应用各写一份适配。
|
|
2609
|
+
*/
|
|
2610
|
+
|
|
2611
|
+
/** 该后端类型是否只是本机存储(不参与跨设备同步)。 */
|
|
2612
|
+
declare function isLocalBackendType(type: string): boolean;
|
|
2613
|
+
/** 默认 localStorage 键前缀:`zen-fs-config:data-group:{appId}`。 */
|
|
2614
|
+
declare const DATA_GROUP_PREFERENCE_PREFIX = "zen-fs-config:data-group:";
|
|
2615
|
+
/** 最小 storage 形状(浏览器 localStorage / 测试替身)。 */
|
|
2616
|
+
interface PreferenceStorage {
|
|
2617
|
+
getItem(key: string): string | null;
|
|
2618
|
+
setItem(key: string, value: string): void;
|
|
2619
|
+
removeItem(key: string): void;
|
|
2620
|
+
}
|
|
2621
|
+
interface PreferenceOptions {
|
|
2622
|
+
storage?: PreferenceStorage;
|
|
2623
|
+
/** 写入用的键前缀,默认 {@link DATA_GROUP_PREFERENCE_PREFIX}。 */
|
|
2624
|
+
keyPrefix?: string;
|
|
2625
|
+
/**
|
|
2626
|
+
* 读取时依次尝试的键前缀(默认只用 `keyPrefix`)。
|
|
2627
|
+
* 迁移场景可把旧产品自己的键放进来,例如 `['zen-fs-config:data-group:', 'onenav:data-group:']`。
|
|
2628
|
+
*/
|
|
2629
|
+
readPrefixes?: string[];
|
|
2630
|
+
}
|
|
2631
|
+
/** 本机为该 appId 记住的数据组 id(没有则 `null`)。 */
|
|
2632
|
+
declare function getPreferredDataGroupId(appId: string, opts?: PreferenceOptions): string | null;
|
|
2633
|
+
/** 记住(`id === null` 时清除)本机为该 appId 选定的数据组。 */
|
|
2634
|
+
declare function setPreferredDataGroupId(appId: string, id: string | null, opts?: PreferenceOptions): void;
|
|
2635
|
+
/** 一个候选数据组(配置仓描述符 + 已实例化的组)。 */
|
|
2636
|
+
interface DataGroupCandidate {
|
|
2637
|
+
id: string;
|
|
2638
|
+
name?: string;
|
|
2639
|
+
group: AppDataGroup;
|
|
2640
|
+
/** 组里是否有非本地后端(即真能跨设备同步) */
|
|
2641
|
+
hasRemote: boolean;
|
|
2642
|
+
/** 后端类型列表,便于界面展示 */
|
|
2643
|
+
backendTypes: string[];
|
|
2644
|
+
/** 目标目录下是否已有内容(用于判断「哪个组才是真正装了数据的」) */
|
|
2645
|
+
hasData: boolean;
|
|
2646
|
+
/**
|
|
2647
|
+
* 该组的隐式本地主存储名(浏览器里是 IndexedDB)。
|
|
2648
|
+
*
|
|
2649
|
+
* 每个数据组都内置一个 local primary,它是组的本地 source / 离线缓存,
|
|
2650
|
+
* 不参与远端同步、也不出现在组描述符里;不能把它当成一个独立可选的数据组。
|
|
2651
|
+
*/
|
|
2652
|
+
localStore: string;
|
|
2653
|
+
}
|
|
2654
|
+
/** 数据组来源:`ConfigRepo` 本身,或配置面板 `onConnected` 交出的句柄。 */
|
|
2655
|
+
type DataGroupSource = IConfigRepo | {
|
|
2656
|
+
repo?: IConfigRepo | null;
|
|
2657
|
+
dataGroup?: AppDataGroup | null;
|
|
2658
|
+
};
|
|
2659
|
+
/** 组在 `path` 下是否至少有一个条目(读失败/未连接都算空)。 */
|
|
2660
|
+
declare function groupHasPath(group: AppDataGroup, path: string): Promise<boolean>;
|
|
2661
|
+
/**
|
|
2662
|
+
* 列出当前 appId 的所有数据组候选(带「是否有数据 / 是否有远端后端」)。
|
|
2663
|
+
*
|
|
2664
|
+
* @param source `ConfigRepo` 或 `{ repo, dataGroup }`
|
|
2665
|
+
* @param appId 应用 id(与 `connect()` 用的是同一个)
|
|
2666
|
+
* @param opts.path 判断「组里是否已有数据」的目录(通常是应用自己的数据目录)
|
|
2667
|
+
*/
|
|
2668
|
+
declare function listDataGroupCandidates(source: DataGroupSource, appId: string, opts?: {
|
|
2669
|
+
path?: string;
|
|
2670
|
+
}): Promise<DataGroupCandidate[]>;
|
|
2671
|
+
/** 兼容旧命名(应用侧曾叫 collectDataGroupCandidates)。 */
|
|
2672
|
+
declare const collectDataGroupCandidates: typeof listDataGroupCandidates;
|
|
2673
|
+
interface ResolveDataGroupOptions {
|
|
2674
|
+
/** 显式指定的组 id;缺省读本机偏好。 */
|
|
2675
|
+
preferGroupId?: string | null;
|
|
2676
|
+
/** 应用自己的数据目录,用于「哪个组里真有数据」的判定。 */
|
|
2677
|
+
path?: string;
|
|
2678
|
+
preference?: PreferenceOptions;
|
|
2679
|
+
}
|
|
2680
|
+
/**
|
|
2681
|
+
* 按「偏好 → 有数据 → 唯一远端 → 单候选」的顺序解析出要挂载的组。
|
|
2682
|
+
* 多个候选且无法判定时返回 `null`,交给界面让用户选。
|
|
2683
|
+
*/
|
|
2684
|
+
declare function resolveDataGroup(source: DataGroupSource, appId: string, opts?: ResolveDataGroupOptions): Promise<AppDataGroup | null>;
|
|
2685
|
+
interface EnsureDataGroupOptions extends ResolveDataGroupOptions {
|
|
2686
|
+
/** 是否允许在「一个描述符都没有」时补建本机默认组(默认允许)。 */
|
|
2687
|
+
allowCreateLocal?: boolean;
|
|
2688
|
+
}
|
|
2689
|
+
interface EnsureDataGroupResult {
|
|
2690
|
+
group: AppDataGroup | null;
|
|
2691
|
+
candidates: DataGroupCandidate[];
|
|
2692
|
+
/** 本次是否新建了本机默认组(界面据此提示「仅本地」) */
|
|
2693
|
+
created: boolean;
|
|
2694
|
+
/** 有描述符但一个都加载不出来(此时不建 default,避免与远端组冲突) */
|
|
2695
|
+
descriptorsUnavailable: boolean;
|
|
2696
|
+
}
|
|
2697
|
+
/**
|
|
2698
|
+
* 确保某个 appId **至少有一个可用的数据组实例**,没有就建一个本机默认组。
|
|
2699
|
+
*
|
|
2700
|
+
* 数据组不是「只有配了远端后端才有」:每个组都自带隐式本地主存储。应用要在
|
|
2701
|
+
* 数据组里读写就必须有一个组实例,否则页面只能停在「还没有数据组」。
|
|
2702
|
+
*
|
|
2703
|
+
* - 已有描述符:按 {@link resolveDataGroup} 的规则选;
|
|
2704
|
+
* - **一个描述符都没有**:`createAppDataGroup('default', [])`,得到一个只有
|
|
2705
|
+
* 隐式本地存储的组(不参与远端同步,之后可再加远端后端);
|
|
2706
|
+
* - **有描述符但都加载失败**:绝不建 default,返回 `descriptorsUnavailable`。
|
|
2707
|
+
*/
|
|
2708
|
+
declare function ensureDataGroup(source: DataGroupSource, appId: string, opts?: EnsureDataGroupOptions): Promise<EnsureDataGroupResult>;
|
|
2709
|
+
|
|
2710
|
+
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
|
@@ -2587,4 +2587,124 @@ declare class ConflictStore {
|
|
|
2587
2587
|
resolve(conflictId: string, mergedContent: string | Uint8Array): Promise<void>;
|
|
2588
2588
|
}
|
|
2589
2589
|
|
|
2590
|
-
|
|
2590
|
+
/**
|
|
2591
|
+
* 数据组选择(跨 app 通用)
|
|
2592
|
+
*
|
|
2593
|
+
* 一个 appId 下可能存在多个数据同步组:历史 `default` 空组、换过后端的旧组、
|
|
2594
|
+
* 新建组。旧写法「取 `appDataGroups[0]`」会挂错组,数据写进空组、看起来像
|
|
2595
|
+
* 「没同步」。这一层解决「该挂哪个组」:
|
|
2596
|
+
*
|
|
2597
|
+
* 1. 用户在界面上显式选过的组(本机偏好,localStorage);
|
|
2598
|
+
* 2. 目标目录下确实已有内容的组;
|
|
2599
|
+
* 3. 带远端后端的组(唯一一个时直接用);
|
|
2600
|
+
* 4. 只有一个候选时用它。
|
|
2601
|
+
*
|
|
2602
|
+
* 多个候选且无法判定时返回 `null`,由界面列出来让用户选 —— 绝不静默择一。
|
|
2603
|
+
*
|
|
2604
|
+
* 这些逻辑与具体应用无关,所以放在库里;应用只提供 `appId` 与自己的数据目录
|
|
2605
|
+
* (如 `/golf`、`/bookkeeping`)。
|
|
2606
|
+
*
|
|
2607
|
+
* 作用对象既可以是 `ConfigRepo`,也可以是配置面板 `onConnected` 交出的
|
|
2608
|
+
* `{ repo, dataGroup }` 句柄:函数内部统一归一化,避免各应用各写一份适配。
|
|
2609
|
+
*/
|
|
2610
|
+
|
|
2611
|
+
/** 该后端类型是否只是本机存储(不参与跨设备同步)。 */
|
|
2612
|
+
declare function isLocalBackendType(type: string): boolean;
|
|
2613
|
+
/** 默认 localStorage 键前缀:`zen-fs-config:data-group:{appId}`。 */
|
|
2614
|
+
declare const DATA_GROUP_PREFERENCE_PREFIX = "zen-fs-config:data-group:";
|
|
2615
|
+
/** 最小 storage 形状(浏览器 localStorage / 测试替身)。 */
|
|
2616
|
+
interface PreferenceStorage {
|
|
2617
|
+
getItem(key: string): string | null;
|
|
2618
|
+
setItem(key: string, value: string): void;
|
|
2619
|
+
removeItem(key: string): void;
|
|
2620
|
+
}
|
|
2621
|
+
interface PreferenceOptions {
|
|
2622
|
+
storage?: PreferenceStorage;
|
|
2623
|
+
/** 写入用的键前缀,默认 {@link DATA_GROUP_PREFERENCE_PREFIX}。 */
|
|
2624
|
+
keyPrefix?: string;
|
|
2625
|
+
/**
|
|
2626
|
+
* 读取时依次尝试的键前缀(默认只用 `keyPrefix`)。
|
|
2627
|
+
* 迁移场景可把旧产品自己的键放进来,例如 `['zen-fs-config:data-group:', 'onenav:data-group:']`。
|
|
2628
|
+
*/
|
|
2629
|
+
readPrefixes?: string[];
|
|
2630
|
+
}
|
|
2631
|
+
/** 本机为该 appId 记住的数据组 id(没有则 `null`)。 */
|
|
2632
|
+
declare function getPreferredDataGroupId(appId: string, opts?: PreferenceOptions): string | null;
|
|
2633
|
+
/** 记住(`id === null` 时清除)本机为该 appId 选定的数据组。 */
|
|
2634
|
+
declare function setPreferredDataGroupId(appId: string, id: string | null, opts?: PreferenceOptions): void;
|
|
2635
|
+
/** 一个候选数据组(配置仓描述符 + 已实例化的组)。 */
|
|
2636
|
+
interface DataGroupCandidate {
|
|
2637
|
+
id: string;
|
|
2638
|
+
name?: string;
|
|
2639
|
+
group: AppDataGroup;
|
|
2640
|
+
/** 组里是否有非本地后端(即真能跨设备同步) */
|
|
2641
|
+
hasRemote: boolean;
|
|
2642
|
+
/** 后端类型列表,便于界面展示 */
|
|
2643
|
+
backendTypes: string[];
|
|
2644
|
+
/** 目标目录下是否已有内容(用于判断「哪个组才是真正装了数据的」) */
|
|
2645
|
+
hasData: boolean;
|
|
2646
|
+
/**
|
|
2647
|
+
* 该组的隐式本地主存储名(浏览器里是 IndexedDB)。
|
|
2648
|
+
*
|
|
2649
|
+
* 每个数据组都内置一个 local primary,它是组的本地 source / 离线缓存,
|
|
2650
|
+
* 不参与远端同步、也不出现在组描述符里;不能把它当成一个独立可选的数据组。
|
|
2651
|
+
*/
|
|
2652
|
+
localStore: string;
|
|
2653
|
+
}
|
|
2654
|
+
/** 数据组来源:`ConfigRepo` 本身,或配置面板 `onConnected` 交出的句柄。 */
|
|
2655
|
+
type DataGroupSource = IConfigRepo | {
|
|
2656
|
+
repo?: IConfigRepo | null;
|
|
2657
|
+
dataGroup?: AppDataGroup | null;
|
|
2658
|
+
};
|
|
2659
|
+
/** 组在 `path` 下是否至少有一个条目(读失败/未连接都算空)。 */
|
|
2660
|
+
declare function groupHasPath(group: AppDataGroup, path: string): Promise<boolean>;
|
|
2661
|
+
/**
|
|
2662
|
+
* 列出当前 appId 的所有数据组候选(带「是否有数据 / 是否有远端后端」)。
|
|
2663
|
+
*
|
|
2664
|
+
* @param source `ConfigRepo` 或 `{ repo, dataGroup }`
|
|
2665
|
+
* @param appId 应用 id(与 `connect()` 用的是同一个)
|
|
2666
|
+
* @param opts.path 判断「组里是否已有数据」的目录(通常是应用自己的数据目录)
|
|
2667
|
+
*/
|
|
2668
|
+
declare function listDataGroupCandidates(source: DataGroupSource, appId: string, opts?: {
|
|
2669
|
+
path?: string;
|
|
2670
|
+
}): Promise<DataGroupCandidate[]>;
|
|
2671
|
+
/** 兼容旧命名(应用侧曾叫 collectDataGroupCandidates)。 */
|
|
2672
|
+
declare const collectDataGroupCandidates: typeof listDataGroupCandidates;
|
|
2673
|
+
interface ResolveDataGroupOptions {
|
|
2674
|
+
/** 显式指定的组 id;缺省读本机偏好。 */
|
|
2675
|
+
preferGroupId?: string | null;
|
|
2676
|
+
/** 应用自己的数据目录,用于「哪个组里真有数据」的判定。 */
|
|
2677
|
+
path?: string;
|
|
2678
|
+
preference?: PreferenceOptions;
|
|
2679
|
+
}
|
|
2680
|
+
/**
|
|
2681
|
+
* 按「偏好 → 有数据 → 唯一远端 → 单候选」的顺序解析出要挂载的组。
|
|
2682
|
+
* 多个候选且无法判定时返回 `null`,交给界面让用户选。
|
|
2683
|
+
*/
|
|
2684
|
+
declare function resolveDataGroup(source: DataGroupSource, appId: string, opts?: ResolveDataGroupOptions): Promise<AppDataGroup | null>;
|
|
2685
|
+
interface EnsureDataGroupOptions extends ResolveDataGroupOptions {
|
|
2686
|
+
/** 是否允许在「一个描述符都没有」时补建本机默认组(默认允许)。 */
|
|
2687
|
+
allowCreateLocal?: boolean;
|
|
2688
|
+
}
|
|
2689
|
+
interface EnsureDataGroupResult {
|
|
2690
|
+
group: AppDataGroup | null;
|
|
2691
|
+
candidates: DataGroupCandidate[];
|
|
2692
|
+
/** 本次是否新建了本机默认组(界面据此提示「仅本地」) */
|
|
2693
|
+
created: boolean;
|
|
2694
|
+
/** 有描述符但一个都加载不出来(此时不建 default,避免与远端组冲突) */
|
|
2695
|
+
descriptorsUnavailable: boolean;
|
|
2696
|
+
}
|
|
2697
|
+
/**
|
|
2698
|
+
* 确保某个 appId **至少有一个可用的数据组实例**,没有就建一个本机默认组。
|
|
2699
|
+
*
|
|
2700
|
+
* 数据组不是「只有配了远端后端才有」:每个组都自带隐式本地主存储。应用要在
|
|
2701
|
+
* 数据组里读写就必须有一个组实例,否则页面只能停在「还没有数据组」。
|
|
2702
|
+
*
|
|
2703
|
+
* - 已有描述符:按 {@link resolveDataGroup} 的规则选;
|
|
2704
|
+
* - **一个描述符都没有**:`createAppDataGroup('default', [])`,得到一个只有
|
|
2705
|
+
* 隐式本地存储的组(不参与远端同步,之后可再加远端后端);
|
|
2706
|
+
* - **有描述符但都加载失败**:绝不建 default,返回 `descriptorsUnavailable`。
|
|
2707
|
+
*/
|
|
2708
|
+
declare function ensureDataGroup(source: DataGroupSource, appId: string, opts?: EnsureDataGroupOptions): Promise<EnsureDataGroupResult>;
|
|
2709
|
+
|
|
2710
|
+
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.js
CHANGED
|
@@ -716,6 +716,7 @@ __export(index_exports, {
|
|
|
716
716
|
ConnectFailedError: () => ConnectFailedError,
|
|
717
717
|
ConnectV2Error: () => ConnectV2Error,
|
|
718
718
|
DATA_GROUP_DEFAULT_FEATURES: () => DATA_GROUP_DEFAULT_FEATURES,
|
|
719
|
+
DATA_GROUP_PREFERENCE_PREFIX: () => DATA_GROUP_PREFERENCE_PREFIX,
|
|
719
720
|
DataGroupBackendError: () => DataGroupBackendError,
|
|
720
721
|
DataGroupCreateError: () => DataGroupCreateError,
|
|
721
722
|
DataGroupNotFoundError: () => DataGroupNotFoundError,
|
|
@@ -737,6 +738,7 @@ __export(index_exports, {
|
|
|
737
738
|
VersionStore: () => VersionStore,
|
|
738
739
|
bindRemoteGroupIdentity: () => bindRemoteGroupIdentity,
|
|
739
740
|
bindRemoteGroupIdentityDetailed: () => bindRemoteGroupIdentityDetailed,
|
|
741
|
+
collectDataGroupCandidates: () => collectDataGroupCandidates,
|
|
740
742
|
configKeyToFilePath: () => configKeyToFilePath,
|
|
741
743
|
connect: () => connect,
|
|
742
744
|
connectV2: () => connectV2,
|
|
@@ -748,20 +750,25 @@ __export(index_exports, {
|
|
|
748
750
|
deleteDataGroupLocalStore: () => deleteDataGroupLocalStore,
|
|
749
751
|
deleteIndexedDbStore: () => deleteIndexedDbStore,
|
|
750
752
|
encodeMetaPath: () => encodeMetaPath,
|
|
753
|
+
ensureDataGroup: () => ensureDataGroup,
|
|
751
754
|
getAccountFields: () => getAccountFields,
|
|
752
755
|
getBackendMetadata: () => getBackendMetadata,
|
|
753
756
|
getExtension: () => getExtension,
|
|
757
|
+
getPreferredDataGroupId: () => getPreferredDataGroupId,
|
|
754
758
|
getUiConnection: () => getUiConnection,
|
|
759
|
+
groupHasPath: () => groupHasPath,
|
|
755
760
|
hasBackend: () => hasBackend,
|
|
756
761
|
incrementVersion: () => incrementVersion,
|
|
757
762
|
isBrowserEnv: () => isBrowserEnv,
|
|
758
763
|
isKeepFile: () => isKeepFile,
|
|
764
|
+
isLocalBackendType: () => isLocalBackendType,
|
|
759
765
|
isLocalStorageUnavailableError: () => isLocalStorageUnavailableError,
|
|
760
766
|
isMtimeSidecar: () => isMtimeSidecar,
|
|
761
767
|
isValidGroupInstanceId: () => isValidGroupInstanceId,
|
|
762
768
|
legacyVersionPathFor: () => legacyVersionPathFor,
|
|
763
769
|
listBackendMetadata: () => listBackendMetadata,
|
|
764
770
|
listBackends: () => listBackends,
|
|
771
|
+
listDataGroupCandidates: () => listDataGroupCandidates,
|
|
765
772
|
localPrimaryType: () => localPrimaryType,
|
|
766
773
|
localSyncStoreNames: () => localSyncStoreNames,
|
|
767
774
|
mergeAccountFields: () => mergeAccountFields,
|
|
@@ -775,8 +782,10 @@ __export(index_exports, {
|
|
|
775
782
|
readVersion: () => readVersion,
|
|
776
783
|
registerBackend: () => registerBackend,
|
|
777
784
|
registerFolderBackend: () => registerFolderBackend,
|
|
785
|
+
resolveDataGroup: () => resolveDataGroup,
|
|
778
786
|
resolveLocalPrimary: () => resolveLocalPrimary,
|
|
779
787
|
resolveRemoteIdentity: () => resolveRemoteIdentity,
|
|
788
|
+
setPreferredDataGroupId: () => setPreferredDataGroupId,
|
|
780
789
|
sha256: () => sha256,
|
|
781
790
|
shortInstanceId: () => shortInstanceId,
|
|
782
791
|
summarizeIdentity: () => summarizeIdentity,
|
|
@@ -8181,6 +8190,200 @@ function normalizeOptions(options) {
|
|
|
8181
8190
|
|
|
8182
8191
|
// src/index.ts
|
|
8183
8192
|
init_backend_registry();
|
|
8193
|
+
|
|
8194
|
+
// src/data-group-selection.ts
|
|
8195
|
+
var import_localstorage_logger6 = require("@richard432/localstorage-logger");
|
|
8196
|
+
var log7 = (0, import_localstorage_logger6.createLogger)("zen-fs-config:data-group-selection");
|
|
8197
|
+
var LOCAL_BACKEND_TYPES = /* @__PURE__ */ new Set(["IndexedDB", "InMemory"]);
|
|
8198
|
+
function isLocalBackendType(type) {
|
|
8199
|
+
return LOCAL_BACKEND_TYPES.has(type);
|
|
8200
|
+
}
|
|
8201
|
+
var DATA_GROUP_PREFERENCE_PREFIX = "zen-fs-config:data-group:";
|
|
8202
|
+
function defaultStorage() {
|
|
8203
|
+
try {
|
|
8204
|
+
const ls = globalThis?.localStorage;
|
|
8205
|
+
if (ls && typeof ls.getItem === "function") return ls;
|
|
8206
|
+
} catch {
|
|
8207
|
+
}
|
|
8208
|
+
return null;
|
|
8209
|
+
}
|
|
8210
|
+
function getPreferredDataGroupId(appId, opts = {}) {
|
|
8211
|
+
const storage = opts.storage ?? defaultStorage();
|
|
8212
|
+
if (!storage) return null;
|
|
8213
|
+
const prefixes = opts.readPrefixes ?? [opts.keyPrefix ?? DATA_GROUP_PREFERENCE_PREFIX];
|
|
8214
|
+
for (const prefix of prefixes) {
|
|
8215
|
+
try {
|
|
8216
|
+
const value = storage.getItem(`${prefix}${appId}`);
|
|
8217
|
+
if (value && value.trim()) return value.trim();
|
|
8218
|
+
} catch {
|
|
8219
|
+
}
|
|
8220
|
+
}
|
|
8221
|
+
return null;
|
|
8222
|
+
}
|
|
8223
|
+
function setPreferredDataGroupId(appId, id, opts = {}) {
|
|
8224
|
+
const storage = opts.storage ?? defaultStorage();
|
|
8225
|
+
if (!storage) return;
|
|
8226
|
+
const key = `${opts.keyPrefix ?? DATA_GROUP_PREFERENCE_PREFIX}${appId}`;
|
|
8227
|
+
try {
|
|
8228
|
+
if (id) storage.setItem(key, id);
|
|
8229
|
+
else storage.removeItem(key);
|
|
8230
|
+
} catch {
|
|
8231
|
+
}
|
|
8232
|
+
}
|
|
8233
|
+
function asRepo(source) {
|
|
8234
|
+
const any = source;
|
|
8235
|
+
if (typeof any?.listAppDataGroups === "function") return any;
|
|
8236
|
+
const repo = any?.repo ?? any?.dataGroup?.parent;
|
|
8237
|
+
return typeof repo?.listAppDataGroups === "function" ? repo : null;
|
|
8238
|
+
}
|
|
8239
|
+
function directGroup(source) {
|
|
8240
|
+
const any = source;
|
|
8241
|
+
if (any?.dataGroup?.fs?.promises) return any.dataGroup;
|
|
8242
|
+
if (typeof any?.listAppDataGroups !== "function" && any?.fs?.promises) {
|
|
8243
|
+
return any;
|
|
8244
|
+
}
|
|
8245
|
+
return null;
|
|
8246
|
+
}
|
|
8247
|
+
function listGroupBackends(group, descriptor) {
|
|
8248
|
+
const anyGroup = group;
|
|
8249
|
+
const connected = typeof anyGroup?.listBackends === "function" ? anyGroup.listBackends() : [];
|
|
8250
|
+
const declared = typeof anyGroup?.listDeclaredBackends === "function" ? anyGroup.listDeclaredBackends() : [];
|
|
8251
|
+
const fromDescriptor = descriptor?.backends ?? [];
|
|
8252
|
+
return [...connected, ...declared, ...fromDescriptor];
|
|
8253
|
+
}
|
|
8254
|
+
function hasRemoteBackend(group, descriptor) {
|
|
8255
|
+
return listGroupBackends(group, descriptor).some((b) => b && !isLocalBackendType(b.type));
|
|
8256
|
+
}
|
|
8257
|
+
async function groupHasPath(group, path) {
|
|
8258
|
+
try {
|
|
8259
|
+
const p = group?.fs?.promises;
|
|
8260
|
+
if (!p?.readdir) return false;
|
|
8261
|
+
const names = await p.readdir(path);
|
|
8262
|
+
return Array.isArray(names) ? names.length > 0 : false;
|
|
8263
|
+
} catch {
|
|
8264
|
+
return false;
|
|
8265
|
+
}
|
|
8266
|
+
}
|
|
8267
|
+
async function listDataGroupCandidates(source, appId, opts = {}) {
|
|
8268
|
+
const repo = asRepo(source);
|
|
8269
|
+
const direct = directGroup(source);
|
|
8270
|
+
if (!repo && direct) {
|
|
8271
|
+
const id = String(direct.groupId ?? direct.id ?? "");
|
|
8272
|
+
return [{
|
|
8273
|
+
id,
|
|
8274
|
+
name: direct.name,
|
|
8275
|
+
group: direct,
|
|
8276
|
+
hasRemote: hasRemoteBackend(direct),
|
|
8277
|
+
backendTypes: Array.from(new Set(listGroupBackends(direct).map((b) => String(b?.type ?? "?")))),
|
|
8278
|
+
hasData: opts.path ? await groupHasPath(direct, opts.path) : false,
|
|
8279
|
+
localStore: dataGroupLocalStoreName(appId, id || "default")
|
|
8280
|
+
}];
|
|
8281
|
+
}
|
|
8282
|
+
const descriptors = [];
|
|
8283
|
+
if (repo) {
|
|
8284
|
+
try {
|
|
8285
|
+
descriptors.push(...await repo.listAppDataGroups());
|
|
8286
|
+
} catch (err) {
|
|
8287
|
+
log7.warn(`[${appId}] \u5217\u51FA\u6570\u636E\u7EC4\u5931\u8D25:`, err);
|
|
8288
|
+
}
|
|
8289
|
+
}
|
|
8290
|
+
const groups = [];
|
|
8291
|
+
if (repo?.getAppDataGroup) {
|
|
8292
|
+
for (const d of descriptors) {
|
|
8293
|
+
try {
|
|
8294
|
+
const group = await repo.getAppDataGroup(d.id);
|
|
8295
|
+
if (group?.fs?.promises) groups.push({ group, descriptor: d });
|
|
8296
|
+
} catch (err) {
|
|
8297
|
+
log7.warn(`[${appId}] \u52A0\u8F7D\u6570\u636E\u7EC4 ${d.id} \u5931\u8D25:`, err);
|
|
8298
|
+
}
|
|
8299
|
+
}
|
|
8300
|
+
} else if (direct) {
|
|
8301
|
+
groups.push({ group: direct });
|
|
8302
|
+
}
|
|
8303
|
+
const out = [];
|
|
8304
|
+
for (const { group, descriptor } of groups) {
|
|
8305
|
+
const id = String(group.groupId ?? group.id ?? descriptor?.id ?? "");
|
|
8306
|
+
out.push({
|
|
8307
|
+
id,
|
|
8308
|
+
name: group.name ?? descriptor?.name,
|
|
8309
|
+
group,
|
|
8310
|
+
hasRemote: hasRemoteBackend(group, descriptor),
|
|
8311
|
+
backendTypes: Array.from(new Set(listGroupBackends(group, descriptor).map((b) => String(b?.type ?? "?")))),
|
|
8312
|
+
hasData: opts.path ? await groupHasPath(group, opts.path) : false,
|
|
8313
|
+
localStore: dataGroupLocalStoreName(appId, id || "default")
|
|
8314
|
+
});
|
|
8315
|
+
}
|
|
8316
|
+
out.sort((a, b) => Number(b.hasData) - Number(a.hasData) || Number(b.hasRemote) - Number(a.hasRemote));
|
|
8317
|
+
return out;
|
|
8318
|
+
}
|
|
8319
|
+
var collectDataGroupCandidates = listDataGroupCandidates;
|
|
8320
|
+
async function resolveDataGroup(source, appId, opts = {}) {
|
|
8321
|
+
const direct = directGroup(source);
|
|
8322
|
+
const candidates = await listDataGroupCandidates(source, appId, { path: opts.path });
|
|
8323
|
+
if (candidates.length === 0) return direct ?? null;
|
|
8324
|
+
const want = opts.preferGroupId ?? getPreferredDataGroupId(appId, opts.preference);
|
|
8325
|
+
if (want) {
|
|
8326
|
+
const named = candidates.find((c) => c.id === want);
|
|
8327
|
+
if (named) return named.group;
|
|
8328
|
+
log7.warn(`[${appId}] \u504F\u597D\u7684\u6570\u636E\u7EC4 ${want} \u4E0D\u5728\u672C\u673A\u914D\u7F6E\u4ED3\u4E2D\uFF0C\u6539\u7528\u5176\u5B83\u5019\u9009`);
|
|
8329
|
+
}
|
|
8330
|
+
if (opts.path) {
|
|
8331
|
+
const withData = candidates.find((c) => c.hasData);
|
|
8332
|
+
if (withData) return withData.group;
|
|
8333
|
+
}
|
|
8334
|
+
const remote = candidates.filter((c) => c.hasRemote);
|
|
8335
|
+
if (remote.length === 1) return remote[0].group;
|
|
8336
|
+
if (candidates.length === 1) return candidates[0].group;
|
|
8337
|
+
if (remote.length > 1) {
|
|
8338
|
+
log7.warn(
|
|
8339
|
+
`[${appId}] \u5B58\u5728\u591A\u4E2A\u53EF\u7528\u6570\u636E\u7EC4\uFF0C\u9700\u7528\u6237\u9009\u62E9\uFF1A${candidates.map((c) => c.id).join(", ")}`
|
|
8340
|
+
);
|
|
8341
|
+
return null;
|
|
8342
|
+
}
|
|
8343
|
+
return null;
|
|
8344
|
+
}
|
|
8345
|
+
async function ensureDataGroup(source, appId, opts = {}) {
|
|
8346
|
+
const allowCreate = opts.allowCreateLocal !== false;
|
|
8347
|
+
const candidates = await listDataGroupCandidates(source, appId, { path: opts.path });
|
|
8348
|
+
if (candidates.length > 0) {
|
|
8349
|
+
const picked = await resolveDataGroup(source, appId, opts);
|
|
8350
|
+
return { group: picked, candidates, created: false, descriptorsUnavailable: false };
|
|
8351
|
+
}
|
|
8352
|
+
const repo = asRepo(source);
|
|
8353
|
+
const canCreate = allowCreate && typeof repo?.createAppDataGroup === "function" && typeof repo?.getAppDataGroup === "function" && typeof repo?.listAppDataGroups === "function";
|
|
8354
|
+
if (!repo || !canCreate) {
|
|
8355
|
+
return { group: null, candidates, created: false, descriptorsUnavailable: false };
|
|
8356
|
+
}
|
|
8357
|
+
let descriptors = null;
|
|
8358
|
+
try {
|
|
8359
|
+
descriptors = await repo.listAppDataGroups();
|
|
8360
|
+
} catch (err) {
|
|
8361
|
+
log7.warn(`[${appId}] \u65E0\u6CD5\u786E\u8BA4\u6570\u636E\u7EC4\u63CF\u8FF0\u7B26\uFF0C\u8DF3\u8FC7\u672C\u673A\u9ED8\u8BA4\u7EC4\u521B\u5EFA:`, err);
|
|
8362
|
+
descriptors = null;
|
|
8363
|
+
}
|
|
8364
|
+
if (descriptors === null) {
|
|
8365
|
+
return { group: null, candidates, created: false, descriptorsUnavailable: true };
|
|
8366
|
+
}
|
|
8367
|
+
if (descriptors.length > 0) {
|
|
8368
|
+
return { group: null, candidates, created: false, descriptorsUnavailable: true };
|
|
8369
|
+
}
|
|
8370
|
+
let created = false;
|
|
8371
|
+
try {
|
|
8372
|
+
await repo.createAppDataGroup("default", []);
|
|
8373
|
+
created = true;
|
|
8374
|
+
} catch (err) {
|
|
8375
|
+
log7.warn(`[${appId}] \u521B\u5EFA\u672C\u673A\u9ED8\u8BA4\u6570\u636E\u7EC4\u5931\u8D25\uFF08\u5C06\u5C1D\u8BD5\u8BFB\u53D6\u73B0\u6709 default\uFF09:`, err);
|
|
8376
|
+
}
|
|
8377
|
+
let group = null;
|
|
8378
|
+
try {
|
|
8379
|
+
group = await repo.getAppDataGroup("default") ?? null;
|
|
8380
|
+
} catch (err) {
|
|
8381
|
+
log7.warn(`[${appId}] \u8BFB\u53D6\u672C\u673A\u9ED8\u8BA4\u6570\u636E\u7EC4\u5931\u8D25:`, err);
|
|
8382
|
+
group = null;
|
|
8383
|
+
}
|
|
8384
|
+
const next = await listDataGroupCandidates(source, appId, { path: opts.path });
|
|
8385
|
+
return { group, candidates: next, created: created && !!group, descriptorsUnavailable: false };
|
|
8386
|
+
}
|
|
8184
8387
|
// Annotate the CommonJS export names for ESM import in node:
|
|
8185
8388
|
0 && (module.exports = {
|
|
8186
8389
|
BackendAlreadyExistsError,
|
|
@@ -8194,6 +8397,7 @@ init_backend_registry();
|
|
|
8194
8397
|
ConnectFailedError,
|
|
8195
8398
|
ConnectV2Error,
|
|
8196
8399
|
DATA_GROUP_DEFAULT_FEATURES,
|
|
8400
|
+
DATA_GROUP_PREFERENCE_PREFIX,
|
|
8197
8401
|
DataGroupBackendError,
|
|
8198
8402
|
DataGroupCreateError,
|
|
8199
8403
|
DataGroupNotFoundError,
|
|
@@ -8215,6 +8419,7 @@ init_backend_registry();
|
|
|
8215
8419
|
VersionStore,
|
|
8216
8420
|
bindRemoteGroupIdentity,
|
|
8217
8421
|
bindRemoteGroupIdentityDetailed,
|
|
8422
|
+
collectDataGroupCandidates,
|
|
8218
8423
|
configKeyToFilePath,
|
|
8219
8424
|
connect,
|
|
8220
8425
|
connectV2,
|
|
@@ -8226,20 +8431,25 @@ init_backend_registry();
|
|
|
8226
8431
|
deleteDataGroupLocalStore,
|
|
8227
8432
|
deleteIndexedDbStore,
|
|
8228
8433
|
encodeMetaPath,
|
|
8434
|
+
ensureDataGroup,
|
|
8229
8435
|
getAccountFields,
|
|
8230
8436
|
getBackendMetadata,
|
|
8231
8437
|
getExtension,
|
|
8438
|
+
getPreferredDataGroupId,
|
|
8232
8439
|
getUiConnection,
|
|
8440
|
+
groupHasPath,
|
|
8233
8441
|
hasBackend,
|
|
8234
8442
|
incrementVersion,
|
|
8235
8443
|
isBrowserEnv,
|
|
8236
8444
|
isKeepFile,
|
|
8445
|
+
isLocalBackendType,
|
|
8237
8446
|
isLocalStorageUnavailableError,
|
|
8238
8447
|
isMtimeSidecar,
|
|
8239
8448
|
isValidGroupInstanceId,
|
|
8240
8449
|
legacyVersionPathFor,
|
|
8241
8450
|
listBackendMetadata,
|
|
8242
8451
|
listBackends,
|
|
8452
|
+
listDataGroupCandidates,
|
|
8243
8453
|
localPrimaryType,
|
|
8244
8454
|
localSyncStoreNames,
|
|
8245
8455
|
mergeAccountFields,
|
|
@@ -8253,8 +8463,10 @@ init_backend_registry();
|
|
|
8253
8463
|
readVersion,
|
|
8254
8464
|
registerBackend,
|
|
8255
8465
|
registerFolderBackend,
|
|
8466
|
+
resolveDataGroup,
|
|
8256
8467
|
resolveLocalPrimary,
|
|
8257
8468
|
resolveRemoteIdentity,
|
|
8469
|
+
setPreferredDataGroupId,
|
|
8258
8470
|
sha256,
|
|
8259
8471
|
shortInstanceId,
|
|
8260
8472
|
summarizeIdentity,
|
package/dist/index.mjs
CHANGED
|
@@ -7397,6 +7397,200 @@ function normalizeOptions(options) {
|
|
|
7397
7397
|
configBackends: extra
|
|
7398
7398
|
};
|
|
7399
7399
|
}
|
|
7400
|
+
|
|
7401
|
+
// src/data-group-selection.ts
|
|
7402
|
+
import { createLogger as createLogger6 } from "@richard432/localstorage-logger";
|
|
7403
|
+
var log7 = createLogger6("zen-fs-config:data-group-selection");
|
|
7404
|
+
var LOCAL_BACKEND_TYPES = /* @__PURE__ */ new Set(["IndexedDB", "InMemory"]);
|
|
7405
|
+
function isLocalBackendType(type) {
|
|
7406
|
+
return LOCAL_BACKEND_TYPES.has(type);
|
|
7407
|
+
}
|
|
7408
|
+
var DATA_GROUP_PREFERENCE_PREFIX = "zen-fs-config:data-group:";
|
|
7409
|
+
function defaultStorage() {
|
|
7410
|
+
try {
|
|
7411
|
+
const ls = globalThis?.localStorage;
|
|
7412
|
+
if (ls && typeof ls.getItem === "function") return ls;
|
|
7413
|
+
} catch {
|
|
7414
|
+
}
|
|
7415
|
+
return null;
|
|
7416
|
+
}
|
|
7417
|
+
function getPreferredDataGroupId(appId, opts = {}) {
|
|
7418
|
+
const storage = opts.storage ?? defaultStorage();
|
|
7419
|
+
if (!storage) return null;
|
|
7420
|
+
const prefixes = opts.readPrefixes ?? [opts.keyPrefix ?? DATA_GROUP_PREFERENCE_PREFIX];
|
|
7421
|
+
for (const prefix of prefixes) {
|
|
7422
|
+
try {
|
|
7423
|
+
const value = storage.getItem(`${prefix}${appId}`);
|
|
7424
|
+
if (value && value.trim()) return value.trim();
|
|
7425
|
+
} catch {
|
|
7426
|
+
}
|
|
7427
|
+
}
|
|
7428
|
+
return null;
|
|
7429
|
+
}
|
|
7430
|
+
function setPreferredDataGroupId(appId, id, opts = {}) {
|
|
7431
|
+
const storage = opts.storage ?? defaultStorage();
|
|
7432
|
+
if (!storage) return;
|
|
7433
|
+
const key = `${opts.keyPrefix ?? DATA_GROUP_PREFERENCE_PREFIX}${appId}`;
|
|
7434
|
+
try {
|
|
7435
|
+
if (id) storage.setItem(key, id);
|
|
7436
|
+
else storage.removeItem(key);
|
|
7437
|
+
} catch {
|
|
7438
|
+
}
|
|
7439
|
+
}
|
|
7440
|
+
function asRepo(source) {
|
|
7441
|
+
const any = source;
|
|
7442
|
+
if (typeof any?.listAppDataGroups === "function") return any;
|
|
7443
|
+
const repo = any?.repo ?? any?.dataGroup?.parent;
|
|
7444
|
+
return typeof repo?.listAppDataGroups === "function" ? repo : null;
|
|
7445
|
+
}
|
|
7446
|
+
function directGroup(source) {
|
|
7447
|
+
const any = source;
|
|
7448
|
+
if (any?.dataGroup?.fs?.promises) return any.dataGroup;
|
|
7449
|
+
if (typeof any?.listAppDataGroups !== "function" && any?.fs?.promises) {
|
|
7450
|
+
return any;
|
|
7451
|
+
}
|
|
7452
|
+
return null;
|
|
7453
|
+
}
|
|
7454
|
+
function listGroupBackends(group, descriptor) {
|
|
7455
|
+
const anyGroup = group;
|
|
7456
|
+
const connected = typeof anyGroup?.listBackends === "function" ? anyGroup.listBackends() : [];
|
|
7457
|
+
const declared = typeof anyGroup?.listDeclaredBackends === "function" ? anyGroup.listDeclaredBackends() : [];
|
|
7458
|
+
const fromDescriptor = descriptor?.backends ?? [];
|
|
7459
|
+
return [...connected, ...declared, ...fromDescriptor];
|
|
7460
|
+
}
|
|
7461
|
+
function hasRemoteBackend(group, descriptor) {
|
|
7462
|
+
return listGroupBackends(group, descriptor).some((b) => b && !isLocalBackendType(b.type));
|
|
7463
|
+
}
|
|
7464
|
+
async function groupHasPath(group, path) {
|
|
7465
|
+
try {
|
|
7466
|
+
const p = group?.fs?.promises;
|
|
7467
|
+
if (!p?.readdir) return false;
|
|
7468
|
+
const names = await p.readdir(path);
|
|
7469
|
+
return Array.isArray(names) ? names.length > 0 : false;
|
|
7470
|
+
} catch {
|
|
7471
|
+
return false;
|
|
7472
|
+
}
|
|
7473
|
+
}
|
|
7474
|
+
async function listDataGroupCandidates(source, appId, opts = {}) {
|
|
7475
|
+
const repo = asRepo(source);
|
|
7476
|
+
const direct = directGroup(source);
|
|
7477
|
+
if (!repo && direct) {
|
|
7478
|
+
const id = String(direct.groupId ?? direct.id ?? "");
|
|
7479
|
+
return [{
|
|
7480
|
+
id,
|
|
7481
|
+
name: direct.name,
|
|
7482
|
+
group: direct,
|
|
7483
|
+
hasRemote: hasRemoteBackend(direct),
|
|
7484
|
+
backendTypes: Array.from(new Set(listGroupBackends(direct).map((b) => String(b?.type ?? "?")))),
|
|
7485
|
+
hasData: opts.path ? await groupHasPath(direct, opts.path) : false,
|
|
7486
|
+
localStore: dataGroupLocalStoreName(appId, id || "default")
|
|
7487
|
+
}];
|
|
7488
|
+
}
|
|
7489
|
+
const descriptors = [];
|
|
7490
|
+
if (repo) {
|
|
7491
|
+
try {
|
|
7492
|
+
descriptors.push(...await repo.listAppDataGroups());
|
|
7493
|
+
} catch (err) {
|
|
7494
|
+
log7.warn(`[${appId}] \u5217\u51FA\u6570\u636E\u7EC4\u5931\u8D25:`, err);
|
|
7495
|
+
}
|
|
7496
|
+
}
|
|
7497
|
+
const groups = [];
|
|
7498
|
+
if (repo?.getAppDataGroup) {
|
|
7499
|
+
for (const d of descriptors) {
|
|
7500
|
+
try {
|
|
7501
|
+
const group = await repo.getAppDataGroup(d.id);
|
|
7502
|
+
if (group?.fs?.promises) groups.push({ group, descriptor: d });
|
|
7503
|
+
} catch (err) {
|
|
7504
|
+
log7.warn(`[${appId}] \u52A0\u8F7D\u6570\u636E\u7EC4 ${d.id} \u5931\u8D25:`, err);
|
|
7505
|
+
}
|
|
7506
|
+
}
|
|
7507
|
+
} else if (direct) {
|
|
7508
|
+
groups.push({ group: direct });
|
|
7509
|
+
}
|
|
7510
|
+
const out = [];
|
|
7511
|
+
for (const { group, descriptor } of groups) {
|
|
7512
|
+
const id = String(group.groupId ?? group.id ?? descriptor?.id ?? "");
|
|
7513
|
+
out.push({
|
|
7514
|
+
id,
|
|
7515
|
+
name: group.name ?? descriptor?.name,
|
|
7516
|
+
group,
|
|
7517
|
+
hasRemote: hasRemoteBackend(group, descriptor),
|
|
7518
|
+
backendTypes: Array.from(new Set(listGroupBackends(group, descriptor).map((b) => String(b?.type ?? "?")))),
|
|
7519
|
+
hasData: opts.path ? await groupHasPath(group, opts.path) : false,
|
|
7520
|
+
localStore: dataGroupLocalStoreName(appId, id || "default")
|
|
7521
|
+
});
|
|
7522
|
+
}
|
|
7523
|
+
out.sort((a, b) => Number(b.hasData) - Number(a.hasData) || Number(b.hasRemote) - Number(a.hasRemote));
|
|
7524
|
+
return out;
|
|
7525
|
+
}
|
|
7526
|
+
var collectDataGroupCandidates = listDataGroupCandidates;
|
|
7527
|
+
async function resolveDataGroup(source, appId, opts = {}) {
|
|
7528
|
+
const direct = directGroup(source);
|
|
7529
|
+
const candidates = await listDataGroupCandidates(source, appId, { path: opts.path });
|
|
7530
|
+
if (candidates.length === 0) return direct ?? null;
|
|
7531
|
+
const want = opts.preferGroupId ?? getPreferredDataGroupId(appId, opts.preference);
|
|
7532
|
+
if (want) {
|
|
7533
|
+
const named = candidates.find((c) => c.id === want);
|
|
7534
|
+
if (named) return named.group;
|
|
7535
|
+
log7.warn(`[${appId}] \u504F\u597D\u7684\u6570\u636E\u7EC4 ${want} \u4E0D\u5728\u672C\u673A\u914D\u7F6E\u4ED3\u4E2D\uFF0C\u6539\u7528\u5176\u5B83\u5019\u9009`);
|
|
7536
|
+
}
|
|
7537
|
+
if (opts.path) {
|
|
7538
|
+
const withData = candidates.find((c) => c.hasData);
|
|
7539
|
+
if (withData) return withData.group;
|
|
7540
|
+
}
|
|
7541
|
+
const remote = candidates.filter((c) => c.hasRemote);
|
|
7542
|
+
if (remote.length === 1) return remote[0].group;
|
|
7543
|
+
if (candidates.length === 1) return candidates[0].group;
|
|
7544
|
+
if (remote.length > 1) {
|
|
7545
|
+
log7.warn(
|
|
7546
|
+
`[${appId}] \u5B58\u5728\u591A\u4E2A\u53EF\u7528\u6570\u636E\u7EC4\uFF0C\u9700\u7528\u6237\u9009\u62E9\uFF1A${candidates.map((c) => c.id).join(", ")}`
|
|
7547
|
+
);
|
|
7548
|
+
return null;
|
|
7549
|
+
}
|
|
7550
|
+
return null;
|
|
7551
|
+
}
|
|
7552
|
+
async function ensureDataGroup(source, appId, opts = {}) {
|
|
7553
|
+
const allowCreate = opts.allowCreateLocal !== false;
|
|
7554
|
+
const candidates = await listDataGroupCandidates(source, appId, { path: opts.path });
|
|
7555
|
+
if (candidates.length > 0) {
|
|
7556
|
+
const picked = await resolveDataGroup(source, appId, opts);
|
|
7557
|
+
return { group: picked, candidates, created: false, descriptorsUnavailable: false };
|
|
7558
|
+
}
|
|
7559
|
+
const repo = asRepo(source);
|
|
7560
|
+
const canCreate = allowCreate && typeof repo?.createAppDataGroup === "function" && typeof repo?.getAppDataGroup === "function" && typeof repo?.listAppDataGroups === "function";
|
|
7561
|
+
if (!repo || !canCreate) {
|
|
7562
|
+
return { group: null, candidates, created: false, descriptorsUnavailable: false };
|
|
7563
|
+
}
|
|
7564
|
+
let descriptors = null;
|
|
7565
|
+
try {
|
|
7566
|
+
descriptors = await repo.listAppDataGroups();
|
|
7567
|
+
} catch (err) {
|
|
7568
|
+
log7.warn(`[${appId}] \u65E0\u6CD5\u786E\u8BA4\u6570\u636E\u7EC4\u63CF\u8FF0\u7B26\uFF0C\u8DF3\u8FC7\u672C\u673A\u9ED8\u8BA4\u7EC4\u521B\u5EFA:`, err);
|
|
7569
|
+
descriptors = null;
|
|
7570
|
+
}
|
|
7571
|
+
if (descriptors === null) {
|
|
7572
|
+
return { group: null, candidates, created: false, descriptorsUnavailable: true };
|
|
7573
|
+
}
|
|
7574
|
+
if (descriptors.length > 0) {
|
|
7575
|
+
return { group: null, candidates, created: false, descriptorsUnavailable: true };
|
|
7576
|
+
}
|
|
7577
|
+
let created = false;
|
|
7578
|
+
try {
|
|
7579
|
+
await repo.createAppDataGroup("default", []);
|
|
7580
|
+
created = true;
|
|
7581
|
+
} catch (err) {
|
|
7582
|
+
log7.warn(`[${appId}] \u521B\u5EFA\u672C\u673A\u9ED8\u8BA4\u6570\u636E\u7EC4\u5931\u8D25\uFF08\u5C06\u5C1D\u8BD5\u8BFB\u53D6\u73B0\u6709 default\uFF09:`, err);
|
|
7583
|
+
}
|
|
7584
|
+
let group = null;
|
|
7585
|
+
try {
|
|
7586
|
+
group = await repo.getAppDataGroup("default") ?? null;
|
|
7587
|
+
} catch (err) {
|
|
7588
|
+
log7.warn(`[${appId}] \u8BFB\u53D6\u672C\u673A\u9ED8\u8BA4\u6570\u636E\u7EC4\u5931\u8D25:`, err);
|
|
7589
|
+
group = null;
|
|
7590
|
+
}
|
|
7591
|
+
const next = await listDataGroupCandidates(source, appId, { path: opts.path });
|
|
7592
|
+
return { group, candidates: next, created: created && !!group, descriptorsUnavailable: false };
|
|
7593
|
+
}
|
|
7400
7594
|
export {
|
|
7401
7595
|
BackendAlreadyExistsError,
|
|
7402
7596
|
BackendEndpointConflictError,
|
|
@@ -7409,6 +7603,7 @@ export {
|
|
|
7409
7603
|
ConnectFailedError,
|
|
7410
7604
|
ConnectV2Error,
|
|
7411
7605
|
DATA_GROUP_DEFAULT_FEATURES,
|
|
7606
|
+
DATA_GROUP_PREFERENCE_PREFIX,
|
|
7412
7607
|
DataGroupBackendError,
|
|
7413
7608
|
DataGroupCreateError,
|
|
7414
7609
|
DataGroupNotFoundError,
|
|
@@ -7430,6 +7625,7 @@ export {
|
|
|
7430
7625
|
VersionStore,
|
|
7431
7626
|
bindRemoteGroupIdentity,
|
|
7432
7627
|
bindRemoteGroupIdentityDetailed,
|
|
7628
|
+
collectDataGroupCandidates,
|
|
7433
7629
|
configKeyToFilePath,
|
|
7434
7630
|
connect,
|
|
7435
7631
|
connectV2,
|
|
@@ -7441,20 +7637,25 @@ export {
|
|
|
7441
7637
|
deleteDataGroupLocalStore,
|
|
7442
7638
|
deleteIndexedDbStore,
|
|
7443
7639
|
encodeMetaPath,
|
|
7640
|
+
ensureDataGroup,
|
|
7444
7641
|
getAccountFields,
|
|
7445
7642
|
getBackendMetadata,
|
|
7446
7643
|
getExtension,
|
|
7644
|
+
getPreferredDataGroupId,
|
|
7447
7645
|
getUiConnection,
|
|
7646
|
+
groupHasPath,
|
|
7448
7647
|
hasBackend,
|
|
7449
7648
|
incrementVersion,
|
|
7450
7649
|
isBrowserEnv,
|
|
7451
7650
|
isKeepFile,
|
|
7651
|
+
isLocalBackendType,
|
|
7452
7652
|
isLocalStorageUnavailableError,
|
|
7453
7653
|
isMtimeSidecar,
|
|
7454
7654
|
isValidGroupInstanceId,
|
|
7455
7655
|
legacyVersionPathFor,
|
|
7456
7656
|
listBackendMetadata,
|
|
7457
7657
|
listBackends,
|
|
7658
|
+
listDataGroupCandidates,
|
|
7458
7659
|
localPrimaryType,
|
|
7459
7660
|
localSyncStoreNames,
|
|
7460
7661
|
mergeAccountFields,
|
|
@@ -7468,8 +7669,10 @@ export {
|
|
|
7468
7669
|
readVersion,
|
|
7469
7670
|
registerBackend,
|
|
7470
7671
|
registerFolderBackend,
|
|
7672
|
+
resolveDataGroup,
|
|
7471
7673
|
resolveLocalPrimary,
|
|
7472
7674
|
resolveRemoteIdentity,
|
|
7675
|
+
setPreferredDataGroupId,
|
|
7473
7676
|
sha256,
|
|
7474
7677
|
shortInstanceId,
|
|
7475
7678
|
summarizeIdentity,
|
package/dist/zen-fs-config.js
CHANGED
|
@@ -25465,6 +25465,7 @@ var ZenFSConfig = (() => {
|
|
|
25465
25465
|
ConnectFailedError: () => ConnectFailedError,
|
|
25466
25466
|
ConnectV2Error: () => ConnectV2Error,
|
|
25467
25467
|
DATA_GROUP_DEFAULT_FEATURES: () => DATA_GROUP_DEFAULT_FEATURES,
|
|
25468
|
+
DATA_GROUP_PREFERENCE_PREFIX: () => DATA_GROUP_PREFERENCE_PREFIX,
|
|
25468
25469
|
DataGroupBackendError: () => DataGroupBackendError,
|
|
25469
25470
|
DataGroupCreateError: () => DataGroupCreateError,
|
|
25470
25471
|
DataGroupNotFoundError: () => DataGroupNotFoundError,
|
|
@@ -25486,6 +25487,7 @@ var ZenFSConfig = (() => {
|
|
|
25486
25487
|
VersionStore: () => VersionStore,
|
|
25487
25488
|
bindRemoteGroupIdentity: () => bindRemoteGroupIdentity,
|
|
25488
25489
|
bindRemoteGroupIdentityDetailed: () => bindRemoteGroupIdentityDetailed,
|
|
25490
|
+
collectDataGroupCandidates: () => collectDataGroupCandidates,
|
|
25489
25491
|
configKeyToFilePath: () => configKeyToFilePath,
|
|
25490
25492
|
connect: () => connect,
|
|
25491
25493
|
connectV2: () => connectV2,
|
|
@@ -25497,20 +25499,25 @@ var ZenFSConfig = (() => {
|
|
|
25497
25499
|
deleteDataGroupLocalStore: () => deleteDataGroupLocalStore,
|
|
25498
25500
|
deleteIndexedDbStore: () => deleteIndexedDbStore,
|
|
25499
25501
|
encodeMetaPath: () => encodeMetaPath,
|
|
25502
|
+
ensureDataGroup: () => ensureDataGroup,
|
|
25500
25503
|
getAccountFields: () => getAccountFields,
|
|
25501
25504
|
getBackendMetadata: () => getBackendMetadata,
|
|
25502
25505
|
getExtension: () => getExtension,
|
|
25506
|
+
getPreferredDataGroupId: () => getPreferredDataGroupId,
|
|
25503
25507
|
getUiConnection: () => getUiConnection,
|
|
25508
|
+
groupHasPath: () => groupHasPath,
|
|
25504
25509
|
hasBackend: () => hasBackend,
|
|
25505
25510
|
incrementVersion: () => incrementVersion,
|
|
25506
25511
|
isBrowserEnv: () => isBrowserEnv,
|
|
25507
25512
|
isKeepFile: () => isKeepFile,
|
|
25513
|
+
isLocalBackendType: () => isLocalBackendType,
|
|
25508
25514
|
isLocalStorageUnavailableError: () => isLocalStorageUnavailableError,
|
|
25509
25515
|
isMtimeSidecar: () => isMtimeSidecar,
|
|
25510
25516
|
isValidGroupInstanceId: () => isValidGroupInstanceId,
|
|
25511
25517
|
legacyVersionPathFor: () => legacyVersionPathFor,
|
|
25512
25518
|
listBackendMetadata: () => listBackendMetadata,
|
|
25513
25519
|
listBackends: () => listBackends,
|
|
25520
|
+
listDataGroupCandidates: () => listDataGroupCandidates,
|
|
25514
25521
|
localPrimaryType: () => localPrimaryType,
|
|
25515
25522
|
localSyncStoreNames: () => localSyncStoreNames,
|
|
25516
25523
|
mergeAccountFields: () => mergeAccountFields,
|
|
@@ -25524,8 +25531,10 @@ var ZenFSConfig = (() => {
|
|
|
25524
25531
|
readVersion: () => readVersion,
|
|
25525
25532
|
registerBackend: () => registerBackend,
|
|
25526
25533
|
registerFolderBackend: () => registerFolderBackend,
|
|
25534
|
+
resolveDataGroup: () => resolveDataGroup,
|
|
25527
25535
|
resolveLocalPrimary: () => resolveLocalPrimary,
|
|
25528
25536
|
resolveRemoteIdentity: () => resolveRemoteIdentity,
|
|
25537
|
+
setPreferredDataGroupId: () => setPreferredDataGroupId,
|
|
25529
25538
|
sha256: () => sha256,
|
|
25530
25539
|
shortInstanceId: () => shortInstanceId,
|
|
25531
25540
|
summarizeIdentity: () => summarizeIdentity,
|
|
@@ -34135,6 +34144,199 @@ var ZenFSConfig = (() => {
|
|
|
34135
34144
|
|
|
34136
34145
|
// src/index.ts
|
|
34137
34146
|
init_backend_registry();
|
|
34147
|
+
|
|
34148
|
+
// src/data-group-selection.ts
|
|
34149
|
+
var log11 = createLogger("zen-fs-config:data-group-selection");
|
|
34150
|
+
var LOCAL_BACKEND_TYPES = /* @__PURE__ */ new Set(["IndexedDB", "InMemory"]);
|
|
34151
|
+
function isLocalBackendType(type) {
|
|
34152
|
+
return LOCAL_BACKEND_TYPES.has(type);
|
|
34153
|
+
}
|
|
34154
|
+
var DATA_GROUP_PREFERENCE_PREFIX = "zen-fs-config:data-group:";
|
|
34155
|
+
function defaultStorage() {
|
|
34156
|
+
try {
|
|
34157
|
+
const ls = globalThis?.localStorage;
|
|
34158
|
+
if (ls && typeof ls.getItem === "function") return ls;
|
|
34159
|
+
} catch {
|
|
34160
|
+
}
|
|
34161
|
+
return null;
|
|
34162
|
+
}
|
|
34163
|
+
function getPreferredDataGroupId(appId, opts = {}) {
|
|
34164
|
+
const storage = opts.storage ?? defaultStorage();
|
|
34165
|
+
if (!storage) return null;
|
|
34166
|
+
const prefixes = opts.readPrefixes ?? [opts.keyPrefix ?? DATA_GROUP_PREFERENCE_PREFIX];
|
|
34167
|
+
for (const prefix of prefixes) {
|
|
34168
|
+
try {
|
|
34169
|
+
const value = storage.getItem(`${prefix}${appId}`);
|
|
34170
|
+
if (value && value.trim()) return value.trim();
|
|
34171
|
+
} catch {
|
|
34172
|
+
}
|
|
34173
|
+
}
|
|
34174
|
+
return null;
|
|
34175
|
+
}
|
|
34176
|
+
function setPreferredDataGroupId(appId, id, opts = {}) {
|
|
34177
|
+
const storage = opts.storage ?? defaultStorage();
|
|
34178
|
+
if (!storage) return;
|
|
34179
|
+
const key = `${opts.keyPrefix ?? DATA_GROUP_PREFERENCE_PREFIX}${appId}`;
|
|
34180
|
+
try {
|
|
34181
|
+
if (id) storage.setItem(key, id);
|
|
34182
|
+
else storage.removeItem(key);
|
|
34183
|
+
} catch {
|
|
34184
|
+
}
|
|
34185
|
+
}
|
|
34186
|
+
function asRepo(source) {
|
|
34187
|
+
const any = source;
|
|
34188
|
+
if (typeof any?.listAppDataGroups === "function") return any;
|
|
34189
|
+
const repo = any?.repo ?? any?.dataGroup?.parent;
|
|
34190
|
+
return typeof repo?.listAppDataGroups === "function" ? repo : null;
|
|
34191
|
+
}
|
|
34192
|
+
function directGroup(source) {
|
|
34193
|
+
const any = source;
|
|
34194
|
+
if (any?.dataGroup?.fs?.promises) return any.dataGroup;
|
|
34195
|
+
if (typeof any?.listAppDataGroups !== "function" && any?.fs?.promises) {
|
|
34196
|
+
return any;
|
|
34197
|
+
}
|
|
34198
|
+
return null;
|
|
34199
|
+
}
|
|
34200
|
+
function listGroupBackends(group, descriptor) {
|
|
34201
|
+
const anyGroup = group;
|
|
34202
|
+
const connected = typeof anyGroup?.listBackends === "function" ? anyGroup.listBackends() : [];
|
|
34203
|
+
const declared = typeof anyGroup?.listDeclaredBackends === "function" ? anyGroup.listDeclaredBackends() : [];
|
|
34204
|
+
const fromDescriptor = descriptor?.backends ?? [];
|
|
34205
|
+
return [...connected, ...declared, ...fromDescriptor];
|
|
34206
|
+
}
|
|
34207
|
+
function hasRemoteBackend(group, descriptor) {
|
|
34208
|
+
return listGroupBackends(group, descriptor).some((b) => b && !isLocalBackendType(b.type));
|
|
34209
|
+
}
|
|
34210
|
+
async function groupHasPath(group, path) {
|
|
34211
|
+
try {
|
|
34212
|
+
const p = group?.fs?.promises;
|
|
34213
|
+
if (!p?.readdir) return false;
|
|
34214
|
+
const names = await p.readdir(path);
|
|
34215
|
+
return Array.isArray(names) ? names.length > 0 : false;
|
|
34216
|
+
} catch {
|
|
34217
|
+
return false;
|
|
34218
|
+
}
|
|
34219
|
+
}
|
|
34220
|
+
async function listDataGroupCandidates(source, appId, opts = {}) {
|
|
34221
|
+
const repo = asRepo(source);
|
|
34222
|
+
const direct = directGroup(source);
|
|
34223
|
+
if (!repo && direct) {
|
|
34224
|
+
const id = String(direct.groupId ?? direct.id ?? "");
|
|
34225
|
+
return [{
|
|
34226
|
+
id,
|
|
34227
|
+
name: direct.name,
|
|
34228
|
+
group: direct,
|
|
34229
|
+
hasRemote: hasRemoteBackend(direct),
|
|
34230
|
+
backendTypes: Array.from(new Set(listGroupBackends(direct).map((b) => String(b?.type ?? "?")))),
|
|
34231
|
+
hasData: opts.path ? await groupHasPath(direct, opts.path) : false,
|
|
34232
|
+
localStore: dataGroupLocalStoreName(appId, id || "default")
|
|
34233
|
+
}];
|
|
34234
|
+
}
|
|
34235
|
+
const descriptors = [];
|
|
34236
|
+
if (repo) {
|
|
34237
|
+
try {
|
|
34238
|
+
descriptors.push(...await repo.listAppDataGroups());
|
|
34239
|
+
} catch (err2) {
|
|
34240
|
+
log11.warn(`[${appId}] \u5217\u51FA\u6570\u636E\u7EC4\u5931\u8D25:`, err2);
|
|
34241
|
+
}
|
|
34242
|
+
}
|
|
34243
|
+
const groups = [];
|
|
34244
|
+
if (repo?.getAppDataGroup) {
|
|
34245
|
+
for (const d of descriptors) {
|
|
34246
|
+
try {
|
|
34247
|
+
const group = await repo.getAppDataGroup(d.id);
|
|
34248
|
+
if (group?.fs?.promises) groups.push({ group, descriptor: d });
|
|
34249
|
+
} catch (err2) {
|
|
34250
|
+
log11.warn(`[${appId}] \u52A0\u8F7D\u6570\u636E\u7EC4 ${d.id} \u5931\u8D25:`, err2);
|
|
34251
|
+
}
|
|
34252
|
+
}
|
|
34253
|
+
} else if (direct) {
|
|
34254
|
+
groups.push({ group: direct });
|
|
34255
|
+
}
|
|
34256
|
+
const out = [];
|
|
34257
|
+
for (const { group, descriptor } of groups) {
|
|
34258
|
+
const id = String(group.groupId ?? group.id ?? descriptor?.id ?? "");
|
|
34259
|
+
out.push({
|
|
34260
|
+
id,
|
|
34261
|
+
name: group.name ?? descriptor?.name,
|
|
34262
|
+
group,
|
|
34263
|
+
hasRemote: hasRemoteBackend(group, descriptor),
|
|
34264
|
+
backendTypes: Array.from(new Set(listGroupBackends(group, descriptor).map((b) => String(b?.type ?? "?")))),
|
|
34265
|
+
hasData: opts.path ? await groupHasPath(group, opts.path) : false,
|
|
34266
|
+
localStore: dataGroupLocalStoreName(appId, id || "default")
|
|
34267
|
+
});
|
|
34268
|
+
}
|
|
34269
|
+
out.sort((a, b) => Number(b.hasData) - Number(a.hasData) || Number(b.hasRemote) - Number(a.hasRemote));
|
|
34270
|
+
return out;
|
|
34271
|
+
}
|
|
34272
|
+
var collectDataGroupCandidates = listDataGroupCandidates;
|
|
34273
|
+
async function resolveDataGroup(source, appId, opts = {}) {
|
|
34274
|
+
const direct = directGroup(source);
|
|
34275
|
+
const candidates = await listDataGroupCandidates(source, appId, { path: opts.path });
|
|
34276
|
+
if (candidates.length === 0) return direct ?? null;
|
|
34277
|
+
const want = opts.preferGroupId ?? getPreferredDataGroupId(appId, opts.preference);
|
|
34278
|
+
if (want) {
|
|
34279
|
+
const named = candidates.find((c) => c.id === want);
|
|
34280
|
+
if (named) return named.group;
|
|
34281
|
+
log11.warn(`[${appId}] \u504F\u597D\u7684\u6570\u636E\u7EC4 ${want} \u4E0D\u5728\u672C\u673A\u914D\u7F6E\u4ED3\u4E2D\uFF0C\u6539\u7528\u5176\u5B83\u5019\u9009`);
|
|
34282
|
+
}
|
|
34283
|
+
if (opts.path) {
|
|
34284
|
+
const withData = candidates.find((c) => c.hasData);
|
|
34285
|
+
if (withData) return withData.group;
|
|
34286
|
+
}
|
|
34287
|
+
const remote = candidates.filter((c) => c.hasRemote);
|
|
34288
|
+
if (remote.length === 1) return remote[0].group;
|
|
34289
|
+
if (candidates.length === 1) return candidates[0].group;
|
|
34290
|
+
if (remote.length > 1) {
|
|
34291
|
+
log11.warn(
|
|
34292
|
+
`[${appId}] \u5B58\u5728\u591A\u4E2A\u53EF\u7528\u6570\u636E\u7EC4\uFF0C\u9700\u7528\u6237\u9009\u62E9\uFF1A${candidates.map((c) => c.id).join(", ")}`
|
|
34293
|
+
);
|
|
34294
|
+
return null;
|
|
34295
|
+
}
|
|
34296
|
+
return null;
|
|
34297
|
+
}
|
|
34298
|
+
async function ensureDataGroup(source, appId, opts = {}) {
|
|
34299
|
+
const allowCreate = opts.allowCreateLocal !== false;
|
|
34300
|
+
const candidates = await listDataGroupCandidates(source, appId, { path: opts.path });
|
|
34301
|
+
if (candidates.length > 0) {
|
|
34302
|
+
const picked = await resolveDataGroup(source, appId, opts);
|
|
34303
|
+
return { group: picked, candidates, created: false, descriptorsUnavailable: false };
|
|
34304
|
+
}
|
|
34305
|
+
const repo = asRepo(source);
|
|
34306
|
+
const canCreate = allowCreate && typeof repo?.createAppDataGroup === "function" && typeof repo?.getAppDataGroup === "function" && typeof repo?.listAppDataGroups === "function";
|
|
34307
|
+
if (!repo || !canCreate) {
|
|
34308
|
+
return { group: null, candidates, created: false, descriptorsUnavailable: false };
|
|
34309
|
+
}
|
|
34310
|
+
let descriptors = null;
|
|
34311
|
+
try {
|
|
34312
|
+
descriptors = await repo.listAppDataGroups();
|
|
34313
|
+
} catch (err2) {
|
|
34314
|
+
log11.warn(`[${appId}] \u65E0\u6CD5\u786E\u8BA4\u6570\u636E\u7EC4\u63CF\u8FF0\u7B26\uFF0C\u8DF3\u8FC7\u672C\u673A\u9ED8\u8BA4\u7EC4\u521B\u5EFA:`, err2);
|
|
34315
|
+
descriptors = null;
|
|
34316
|
+
}
|
|
34317
|
+
if (descriptors === null) {
|
|
34318
|
+
return { group: null, candidates, created: false, descriptorsUnavailable: true };
|
|
34319
|
+
}
|
|
34320
|
+
if (descriptors.length > 0) {
|
|
34321
|
+
return { group: null, candidates, created: false, descriptorsUnavailable: true };
|
|
34322
|
+
}
|
|
34323
|
+
let created = false;
|
|
34324
|
+
try {
|
|
34325
|
+
await repo.createAppDataGroup("default", []);
|
|
34326
|
+
created = true;
|
|
34327
|
+
} catch (err2) {
|
|
34328
|
+
log11.warn(`[${appId}] \u521B\u5EFA\u672C\u673A\u9ED8\u8BA4\u6570\u636E\u7EC4\u5931\u8D25\uFF08\u5C06\u5C1D\u8BD5\u8BFB\u53D6\u73B0\u6709 default\uFF09:`, err2);
|
|
34329
|
+
}
|
|
34330
|
+
let group = null;
|
|
34331
|
+
try {
|
|
34332
|
+
group = await repo.getAppDataGroup("default") ?? null;
|
|
34333
|
+
} catch (err2) {
|
|
34334
|
+
log11.warn(`[${appId}] \u8BFB\u53D6\u672C\u673A\u9ED8\u8BA4\u6570\u636E\u7EC4\u5931\u8D25:`, err2);
|
|
34335
|
+
group = null;
|
|
34336
|
+
}
|
|
34337
|
+
const next = await listDataGroupCandidates(source, appId, { path: opts.path });
|
|
34338
|
+
return { group, candidates: next, created: created && !!group, descriptorsUnavailable: false };
|
|
34339
|
+
}
|
|
34138
34340
|
return __toCommonJS(src_exports);
|
|
34139
34341
|
})();
|
|
34140
34342
|
/*! Bundled license information:
|