zen-fs-config 0.7.24 → 0.8.0
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/README.md +36 -5
- package/README.zh-CN.md +36 -5
- package/dist/{backend-registry-KRGXHZRB.mjs → backend-registry-CUMSOE2S.mjs} +1 -1
- package/dist/{chunk-JS3F3RBC.mjs → chunk-J6QHNG4F.mjs} +4 -1
- package/dist/index.d.mts +174 -4
- package/dist/index.d.ts +174 -4
- package/dist/index.js +403 -80
- package/dist/index.mjs +398 -81
- package/dist/zen-fs-config.js +416 -98
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -155,6 +155,9 @@ const { repo } = await connect('my-app', {
|
|
|
155
155
|
options: { token: 'xxx', owner: 'xxx', repo: 'xxx', branch: 'main' },
|
|
156
156
|
},
|
|
157
157
|
idbStoreName: 'my-app-config', // custom IndexedDB store name
|
|
158
|
+
// A brand-new (empty) remote has no group type of its own: state it explicitly,
|
|
159
|
+
// otherwise connect() throws NeedsInitError instead of guessing.
|
|
160
|
+
initGroupType: 'config-sync',
|
|
158
161
|
});
|
|
159
162
|
|
|
160
163
|
// Next time you don't need to pass backend info again
|
|
@@ -190,16 +193,43 @@ if (result.groupType === 'data-sync') {
|
|
|
190
193
|
Detection & dispatch rules:
|
|
191
194
|
|
|
192
195
|
- Always creates (or reuses) a **config-sync `ConfigRepo`** as the anchor.
|
|
193
|
-
-
|
|
194
|
-
-
|
|
195
|
-
-
|
|
196
|
-
-
|
|
196
|
+
- Probes the backend before creating anything local: it reads `/.meta/group.json` strictly (only a genuine "not found" counts as empty — a 401/403 is surfaced, not swallowed) and then the legacy `/.meta/group-type` marker. The probe yields one of `unreachable | empty | marked | occupied`.
|
|
197
|
+
- `unreachable` → throws `RemoteUnreachableError` (`code: 'REMOTE_UNREACHABLE'`); no local group is created.
|
|
198
|
+
- `empty` (no descriptor and no marker) → throws `NeedsInitError` (`code: 'NEEDS_INIT'`, `requiresInit: true`) unless you pass `initGroupType` (or the older `groupType`). **The library never guesses a group type for a brand-new backend** — ask the user and retry.
|
|
199
|
+
- `marked` → `config-sync` connects the repo to that backend; `data-sync` attaches the backend to the default app data group inside the config-sync repo.
|
|
200
|
+
- If `options.groupType` conflicts with the detected type, `connect()` throws a `Group type mismatch` error. An `occupied` descriptor with an invalid `kind` is rejected outright.
|
|
201
|
+
- The returned `ConnectResult` always carries `groupType` and `repo` (the config-sync repo). It also carries `dataGroup` (the default app data group) when a data-sync backend was connected.
|
|
197
202
|
|
|
198
203
|
| Method | Description |
|
|
199
204
|
|--------|-------------|
|
|
200
205
|
| `connect(appId, options?)` | Unified entry: always returns a `ConfigRepo`, optionally with a default data group |
|
|
206
|
+
| `probeGroupTypeDetailed(type, options)` | Raw probe (no local writes): returns `{ state, groupType, remote, error?, descriptor? }` |
|
|
201
207
|
|
|
202
|
-
`connect()` accepts the same options as `createConfigRepo` (`backendInfo`, `primaryBackendId`, `idbStoreName`, `nodeId`, `folderPath`, `cache`, `serializer`, `onConflict`, `syncPollIntervalMs`) plus `groupType`. On Node.js, pass `folderPath` (or set `ZEN_FS_CONFIG_HOME`) to enable local disk persistence.
|
|
208
|
+
`connect()` accepts the same options as `createConfigRepo` (`backendInfo`, `primaryBackendId`, `idbStoreName`, `nodeId`, `folderPath`, `cache`, `serializer`, `onConflict`, `syncPollIntervalMs`) plus `groupType`, `initGroupType` (the type to use when the remote is empty) and `deferReplicaSetup`. On Node.js, pass `folderPath` (or set `ZEN_FS_CONFIG_HOME`) to enable local disk persistence.
|
|
209
|
+
|
|
210
|
+
### Deferred replica setup: two signals
|
|
211
|
+
|
|
212
|
+
`connect()` normally verifies every remote backend's group identity (a network round-trip per replica) before it returns. With `deferReplicaSetup: true` it stops doing that inline:
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
const result = await connect('my-app', {
|
|
216
|
+
backendInfo: { type: 'Gitee', options: { token, owner, repo, branch } },
|
|
217
|
+
deferReplicaSetup: true,
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// Signal ① — connect() has returned. Local config works right now.
|
|
221
|
+
result.repo.getConfig('/theme');
|
|
222
|
+
result.replicasPending; // true
|
|
223
|
+
|
|
224
|
+
// Signal ② — remote identity check + pair creation finished.
|
|
225
|
+
const summary = await result.whenReplicasReady?.();
|
|
226
|
+
// { ready: ['Gitee-replica'], skipped: [], failed: [] }
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
- Signal ① (`connect()` resolved): the local repo is fully usable — reads/writes, `getBackends()` (descriptors are on disk), `listAppDataGroups()`. But no sync pair exists yet, so `getSyncStatuses()` is empty. **Never render an empty pair list as "no remote backend"**: check `replicasPending` first.
|
|
230
|
+
- Signal ② (`whenReplicasReady()`): resolves with `{ ready, skipped, failed }`. `skipped` holds `{ backendId, reason }` for backends that belong to another group (identity conflict) — these are **not thrown** from `connect()` any more. `failed` holds `{ backendId, error }` for backend/network failures. The promise never rejects; it resolves immediately (with the current state) when defer is off, so consumers can `await` unconditionally.
|
|
231
|
+
- No pair is created before verification succeeds. That removes the "create then tear down on conflict" window (and any need to gate syncing behind a flag); the price is that consumers must honour the two signals.
|
|
232
|
+
- `dispose()` and `flush()` await the background setup, so a late pair can never attach to a disposed engine and `flush()` never runs against zero pairs.
|
|
203
233
|
|
|
204
234
|
## App data group path semantics
|
|
205
235
|
|
|
@@ -253,6 +283,7 @@ Each config file has a sidecar version file: `db.json` → `.db.json.version` (v
|
|
|
253
283
|
| `publishNodeConfig(nodeId)` | Explicit one-shot push of node config to all backends (auto-sync also covers it) |
|
|
254
284
|
| `peekNodeConfig<T>(nodeId, path)` | Read another node's config (from the synced-in local copy) |
|
|
255
285
|
| `flush()` | Manually trigger all pending syncs |
|
|
286
|
+
| `whenReplicasReady?()` | Optional; resolves with the replica setup summary (see deferred setup above) |
|
|
256
287
|
| `listConflicts()` | List all archived conflicts |
|
|
257
288
|
| `resolveConflict(id, merged)` | Resolve a conflict with merged content |
|
|
258
289
|
| `fs.promises.*` | Standard fs API, chrooted to `/{appId}/` |
|
package/README.zh-CN.md
CHANGED
|
@@ -153,6 +153,9 @@ const { repo } = await connect('my-app', {
|
|
|
153
153
|
options: { token: 'xxx', owner: 'xxx', repo: 'xxx', branch: 'main' },
|
|
154
154
|
},
|
|
155
155
|
idbStoreName: 'my-app-config', // 自定义 IndexedDB store 名称
|
|
156
|
+
// 全新(空)远端自己没有组类型:必须显式声明,
|
|
157
|
+
// 否则 connect() 抛 NeedsInitError,而不是替你猜。
|
|
158
|
+
initGroupType: 'config-sync',
|
|
156
159
|
});
|
|
157
160
|
|
|
158
161
|
// 之后重新打开时不需要再传后端参数
|
|
@@ -183,16 +186,43 @@ if (result.groupType === 'data-sync') {
|
|
|
183
186
|
探测与分发规则:
|
|
184
187
|
|
|
185
188
|
- 始终创建(或复用)一个**配置同步组 `ConfigRepo`** 作为锚点。
|
|
186
|
-
-
|
|
187
|
-
-
|
|
188
|
-
-
|
|
189
|
-
-
|
|
189
|
+
- 建任何本地东西之前先探测后端:严格读 `/.meta/group.json`(只有真正的「不存在」才算空,401/403 会照实报错而不是被吞成空库),没有描述符时再读旧标记 `/.meta/group-type`。探测结果是 `unreachable | empty | marked | occupied` 四态之一。
|
|
190
|
+
- `unreachable` → 抛 `RemoteUnreachableError`(`code: 'REMOTE_UNREACHABLE'`),不创建本地组。
|
|
191
|
+
- `empty`(无描述符也无标记)→ 抛 `NeedsInitError`(`code: 'NEEDS_INIT'`,`requiresInit: true`),除非你传了 `initGroupType`(或旧选项 `groupType`)。**库不会替全新后端猜组类型** —— 先问用户,再带类型重连。
|
|
192
|
+
- `marked` → `config-sync` 把该后端连到配置同步组;`data-sync` 把它挂到配置同步组下的默认数据组。
|
|
193
|
+
- 显式传的 `options.groupType` 与探测结果不符 → `Group type mismatch`;`occupied` 但描述符 `kind` 非法 → 直接报错。
|
|
194
|
+
- 返回的 `ConnectResult` 始终带 `groupType` 和 `repo`(配置同步组);接入 data-sync 后端时还带 `dataGroup`(默认数据组)。
|
|
190
195
|
|
|
191
196
|
| 方法 | 说明 |
|
|
192
197
|
|---|---|
|
|
193
198
|
| `connect(appId, options?)` | 统一入口:始终返回 `ConfigRepo`,可选附带默认数据组 |
|
|
199
|
+
| `probeGroupTypeDetailed(type, options)` | 只探测、不写本地:返回 `{ state, groupType, remote, error?, descriptor? }` |
|
|
194
200
|
|
|
195
|
-
`connect()` 接受与 `createConfigRepo` 相同的选项(`backendInfo`、`primaryBackendId`、`idbStoreName`、`nodeId`、`folderPath`、`cache`、`serializer`、`onConflict`、`syncPollIntervalMs`),另加 `groupType`。在 Node.js 上通过 `folderPath`(或设 `ZEN_FS_CONFIG_HOME`)开启本地磁盘持久化。
|
|
201
|
+
`connect()` 接受与 `createConfigRepo` 相同的选项(`backendInfo`、`primaryBackendId`、`idbStoreName`、`nodeId`、`folderPath`、`cache`、`serializer`、`onConflict`、`syncPollIntervalMs`),另加 `groupType`、`initGroupType`(远端为空时用户选定的组类型)与 `deferReplicaSetup`。在 Node.js 上通过 `folderPath`(或设 `ZEN_FS_CONFIG_HOME`)开启本地磁盘持久化。
|
|
202
|
+
|
|
203
|
+
### deferReplicaSetup:两个信号
|
|
204
|
+
|
|
205
|
+
默认情况下,`connect()` 要逐个校验远端后端的组身份(每个副本一次网络往返)才能返回。传 `deferReplicaSetup: true` 后这段挪到后台:
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
const result = await connect('my-app', {
|
|
209
|
+
backendInfo: { type: 'Gitee', options: { token, owner, repo, branch } },
|
|
210
|
+
deferReplicaSetup: true,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// 信号① —— connect() 已返回,本地配置立刻可用
|
|
214
|
+
result.repo.getConfig('/theme');
|
|
215
|
+
result.replicasPending; // true
|
|
216
|
+
|
|
217
|
+
// 信号② —— 远端校验 + 建对完成
|
|
218
|
+
const summary = await result.whenReplicasReady?.();
|
|
219
|
+
// { ready: ['Gitee-replica'], skipped: [], failed: [] }
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
- 信号①(`connect()` resolve):本地仓完整可用 —— 读写、`getBackends()`(描述符已落盘)、`listAppDataGroups()` 都正常。但此时还没有同步对,`getSyncStatuses()` 是空的。**不能把空的同步对列表当成「没有远端后端」**,先看 `replicasPending`。
|
|
223
|
+
- 信号②(`whenReplicasReady()`):resolve 出 `{ ready, skipped, failed }`。`skipped` 是 `{ backendId, reason }`,表示该后端归属别的同步组(身份冲突)—— 这类情况**不再从 `connect()` 抛出**;`failed` 是 `{ backendId, error }`,表示建实例 / 网络失败。该方法永不 reject;未开 defer 时也会立即 resolve(读当前状态),所以调用方可以无分支地 `await`。
|
|
224
|
+
- 校验通过前不建任何同步对,因此没有「建了又拆」的冲突窗口,也不需要给同步加闸门。代价是消费方必须认上面两个信号。
|
|
225
|
+
- `dispose()` 与 `flush()` 都会等待后台建对,晚到的 pair 不会挂到已销毁的引擎上,`flush()` 也不会跑在 0 个 pair 上。
|
|
196
226
|
|
|
197
227
|
## 数据组的路径语义
|
|
198
228
|
|
|
@@ -244,6 +274,7 @@ await dataGroup.fs.promises.writeFile('/struct/host.json', json);
|
|
|
244
274
|
| `publishNodeConfig(nodeId)` | 显式一次性推送节点配置到所有后端(自动同步已覆盖) |
|
|
245
275
|
| `peekNodeConfig<T>(nodeId, path)` | 只读查看其他节点的已发布配置 |
|
|
246
276
|
| `flush()` | 手动触发所有同步 |
|
|
277
|
+
| `whenReplicasReady?()` | 可选;resolve 出副本建对摘要(见上文 defer 模式) |
|
|
247
278
|
| `listConflicts()` | 列出所有冲突归档 |
|
|
248
279
|
| `resolveConflict(id, merged)` | 用合并内容解决冲突 |
|
|
249
280
|
| `fs.promises.*` | 标准 fs API,chroot 隔离到 `/{appId}/` |
|
|
@@ -232,7 +232,10 @@ async function wrapZenFSFileSystem(config) {
|
|
|
232
232
|
const size = st.size;
|
|
233
233
|
const buf = new Uint8Array(size);
|
|
234
234
|
await isolatedFS.read(path, buf, 0, size);
|
|
235
|
-
|
|
235
|
+
const enc = typeof args[0] === "string" ? args[0] : args[0] && typeof args[0] === "object" ? args[0].encoding : void 0;
|
|
236
|
+
if (enc === "utf-8" || enc === "utf8" || enc === "UTF-8") {
|
|
237
|
+
return new TextDecoder().decode(buf);
|
|
238
|
+
}
|
|
236
239
|
return buf;
|
|
237
240
|
},
|
|
238
241
|
async writeFile(path, data, _options) {
|
package/dist/index.d.mts
CHANGED
|
@@ -264,6 +264,22 @@ interface ConfigRepoOptions {
|
|
|
264
264
|
type: string;
|
|
265
265
|
options: Record<string, unknown>;
|
|
266
266
|
};
|
|
267
|
+
/**
|
|
268
|
+
* 连接前已探测到的远端描述符(内部优化):同一个 type+options 的副本在
|
|
269
|
+
* `setupSync()` 里直接复用,省掉一次重复的 `group.json` 远端读。
|
|
270
|
+
* `descriptor: null` 表示已确认远端没有描述符(空库 / 老仓库)。
|
|
271
|
+
*/
|
|
272
|
+
prefetchedRemote?: {
|
|
273
|
+
type: string;
|
|
274
|
+
options?: Record<string, unknown>;
|
|
275
|
+
descriptor: GroupDescriptor | null;
|
|
276
|
+
};
|
|
277
|
+
/**
|
|
278
|
+
* 把副本建对推迟到后台(默认 false)。开启后 `createConfigRepo` 先返回本地
|
|
279
|
+
* 可用的 repo,副本校验与建对由 `whenReplicasReady()` 通知;校验完成前不会
|
|
280
|
+
* 建任何 pair,也不会启动首同步。
|
|
281
|
+
*/
|
|
282
|
+
deferReplicaSetup?: boolean;
|
|
267
283
|
/** IndexedDB store name for the local primary backend. Default: `zen-fs-config-{appId}` */
|
|
268
284
|
idbStoreName?: string;
|
|
269
285
|
/**
|
|
@@ -385,6 +401,27 @@ interface BackendSyncState {
|
|
|
385
401
|
timestamp: number;
|
|
386
402
|
};
|
|
387
403
|
}
|
|
404
|
+
/**
|
|
405
|
+
* 同步对事件(`onSyncEvent`)。
|
|
406
|
+
*
|
|
407
|
+
* `sync:end` 每轮同步结束都会发(**包含零变更轮**),调用方可用 `result`
|
|
408
|
+
* 里的计数过滤自己关心的变更;`sync:error` 只在整轮抛错时发。
|
|
409
|
+
*/
|
|
410
|
+
interface SyncGroupSyncEvent {
|
|
411
|
+
type: 'sync:end' | 'sync:error';
|
|
412
|
+
/** 事件所属组的类型。 */
|
|
413
|
+
groupKind: SyncGroupKind;
|
|
414
|
+
/** 配置组固定为 `config`;数据组为其 groupId。 */
|
|
415
|
+
groupId: string;
|
|
416
|
+
/** 该同步对对应的后端 id(不含隐式本地主库)。 */
|
|
417
|
+
backendId: string;
|
|
418
|
+
pairId: string;
|
|
419
|
+
timestamp: number;
|
|
420
|
+
/** `sync:end` 时附带本轮结果。 */
|
|
421
|
+
result?: SyncResult;
|
|
422
|
+
/** `sync:error` 时的错误消息。 */
|
|
423
|
+
error?: string;
|
|
424
|
+
}
|
|
388
425
|
/** Structural discriminator for a sync group. */
|
|
389
426
|
type SyncGroupKind = 'config' | 'data';
|
|
390
427
|
/** Capability switches, derived from `kind` + the group descriptor. */
|
|
@@ -576,6 +613,11 @@ interface SyncGroupHandle {
|
|
|
576
613
|
readonly fs: typeof node_fs;
|
|
577
614
|
getSyncStatuses(): Map<string, SyncPairStatus>;
|
|
578
615
|
getBackendSyncStates(): Map<string, BackendSyncState>;
|
|
616
|
+
/**
|
|
617
|
+
* 订阅同步对事件(`sync:end` / `sync:error`)。返回取消订阅函数。
|
|
618
|
+
* 可选:老版本实现可能没有该方法,调用方按可选处理。
|
|
619
|
+
*/
|
|
620
|
+
onSyncEvent?(handler: (event: SyncGroupSyncEvent) => void): () => void;
|
|
579
621
|
flush(): Promise<SyncResult[]>;
|
|
580
622
|
dispose(): Promise<void>;
|
|
581
623
|
addBackend(id: string, type: string, options: Record<string, unknown>, description?: string): Promise<void>;
|
|
@@ -616,8 +658,17 @@ interface ConnectOptions {
|
|
|
616
658
|
type: string;
|
|
617
659
|
options: Record<string, unknown>;
|
|
618
660
|
};
|
|
619
|
-
/**
|
|
661
|
+
/**
|
|
662
|
+
* Force a specific group type. For an already-marked remote it must match the
|
|
663
|
+
* remote's type; for an empty remote it is treated as the user's explicit
|
|
664
|
+
* choice (legacy callers). New callers should use `initGroupType` instead.
|
|
665
|
+
*/
|
|
620
666
|
groupType?: SyncGroupType;
|
|
667
|
+
/**
|
|
668
|
+
* 空库初始化授权:仅当远端完全为空(无 group.json、无 group-type 标记)时使用。
|
|
669
|
+
* 未提供时 connect() 抛 `NeedsInitError`,由 UI 询问用户后带上本字段重试。
|
|
670
|
+
*/
|
|
671
|
+
initGroupType?: SyncGroupType;
|
|
621
672
|
/** IndexedDB store name. Default: `zen-fs-config-{appId}` */
|
|
622
673
|
idbStoreName?: string;
|
|
623
674
|
/**
|
|
@@ -646,6 +697,27 @@ interface ConnectOptions {
|
|
|
646
697
|
onConflict?: (conflict: ConflictInfo) => Promise<unknown | null>;
|
|
647
698
|
/** Sync polling interval in ms. Default: 1800000 (30 min). */
|
|
648
699
|
syncPollIntervalMs?: number;
|
|
700
|
+
/**
|
|
701
|
+
* 把「建副本同步对」整体推迟到后台:`connect()` 只做探测 + 本地仓初始化,
|
|
702
|
+
* 返回后由 `whenReplicasReady` 通知结果。默认 false(保持旧行为)。
|
|
703
|
+
* 校验完成前不建任何 pair,因此不存在「建了又拆」的冲突窗口。
|
|
704
|
+
*/
|
|
705
|
+
deferReplicaSetup?: boolean;
|
|
706
|
+
}
|
|
707
|
+
/** `whenReplicasReady()` 的结果:哪些副本已可用、哪些被跳过、哪些失败。 */
|
|
708
|
+
interface ReplicaSetupSummary {
|
|
709
|
+
/** 校验通过并已建同步对的副本 id。 */
|
|
710
|
+
ready: string[];
|
|
711
|
+
/** 因归属冲突/不可读被跳过的副本。 */
|
|
712
|
+
skipped: {
|
|
713
|
+
backendId: string;
|
|
714
|
+
reason: string;
|
|
715
|
+
}[];
|
|
716
|
+
/** 建实例或建对失败的副本。 */
|
|
717
|
+
failed: {
|
|
718
|
+
backendId: string;
|
|
719
|
+
error: string;
|
|
720
|
+
}[];
|
|
649
721
|
}
|
|
650
722
|
/** Result of connect(). */
|
|
651
723
|
interface ConnectResult {
|
|
@@ -667,6 +739,16 @@ interface ConnectResult {
|
|
|
667
739
|
dataGroup?: AppDataGroup;
|
|
668
740
|
/** All app data group handles loaded for this app (may be empty). */
|
|
669
741
|
appDataGroups?: AppDataGroup[];
|
|
742
|
+
/**
|
|
743
|
+
* 副本是否还在后台准备中。`true` 时 `repo` 已可用(本地数据可读),
|
|
744
|
+
* 但远端同步对尚未建好,调用方应等待 `whenReplicasReady` 再做远端相关操作。
|
|
745
|
+
*/
|
|
746
|
+
replicasPending?: boolean;
|
|
747
|
+
/**
|
|
748
|
+
* 副本准备结果。未开启 `deferReplicaSetup` 时是一个已 resolve 的 Promise。
|
|
749
|
+
* 永不 reject:失败信息放在 `failed` / `skipped` 里。
|
|
750
|
+
*/
|
|
751
|
+
whenReplicasReady?: Promise<ReplicaSetupSummary>;
|
|
670
752
|
}
|
|
671
753
|
/** The main configuration repository interface. */
|
|
672
754
|
interface IConfigRepo {
|
|
@@ -674,6 +756,11 @@ interface IConfigRepo {
|
|
|
674
756
|
readonly appId: string;
|
|
675
757
|
/** Node ID. */
|
|
676
758
|
readonly nodeId: string;
|
|
759
|
+
/**
|
|
760
|
+
* 副本后台准备的结果(`deferReplicaSetup` 时才有意义)。
|
|
761
|
+
* 未推迟时立即 resolve;永不 reject。
|
|
762
|
+
*/
|
|
763
|
+
whenReplicasReady?(): Promise<ReplicaSetupSummary>;
|
|
677
764
|
/** Fixed group id of the config-sync group (`config`). */
|
|
678
765
|
readonly groupId: string;
|
|
679
766
|
/** Structural discriminator — always `'config'`. */
|
|
@@ -706,6 +793,11 @@ interface IConfigRepo {
|
|
|
706
793
|
flush(): Promise<SyncResult[]>;
|
|
707
794
|
/** Get sync status for all registered sync pairs. */
|
|
708
795
|
getSyncStatuses(): Map<string, SyncPairStatus>;
|
|
796
|
+
/**
|
|
797
|
+
* Subscribe to pair-level sync events (`sync:end` / `sync:error`).
|
|
798
|
+
* Returns an unsubscribe function. Optional for older implementations.
|
|
799
|
+
*/
|
|
800
|
+
onSyncEvent?(handler: (event: SyncGroupSyncEvent) => void): () => void;
|
|
709
801
|
/**
|
|
710
802
|
* Per-backend sync state, including replica descriptors that failed to
|
|
711
803
|
* connect and therefore have no pair (they do not appear in
|
|
@@ -1108,6 +1200,11 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1108
1200
|
private onConflictCallback?;
|
|
1109
1201
|
private disposed;
|
|
1110
1202
|
private configCache;
|
|
1203
|
+
/** 后台建对(deferReplicaSetup)的状态,见 `whenReplicasReady()`。 */
|
|
1204
|
+
private replicaSetupPromise;
|
|
1205
|
+
private replicaSetupSummary;
|
|
1206
|
+
/** 同步对事件订阅者,见 `onSyncEvent()`。 */
|
|
1207
|
+
private syncEventHandlers;
|
|
1111
1208
|
private readonly primaryBackendId;
|
|
1112
1209
|
private readonly cacheOptions?;
|
|
1113
1210
|
private readonly pollIntervalMs?;
|
|
@@ -1121,6 +1218,16 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1121
1218
|
* 这些路径改由 processTombstones 主动删除,直到所有副本确认不存在、
|
|
1122
1219
|
* 墓碑被 GC 之后才恢复同步。
|
|
1123
1220
|
*/
|
|
1221
|
+
/**
|
|
1222
|
+
* 永不参与内容同步的元数据路径。
|
|
1223
|
+
*
|
|
1224
|
+
* `.meta/group.json` 是同步身份(kind/id/instanceId)的载体,只在身份流程里写:
|
|
1225
|
+
* 建同步对时读它、认领/对齐时写它。一旦让普通内容同步(按 mtime 双向)
|
|
1226
|
+
* 也去搬这个文件,身份就成了"谁最后写谁赢"——两台设备各持一个实例 ID 时会
|
|
1227
|
+
* 互相覆盖,远端根的 instanceId 便会反复变化,且全程绕开 claim / 冲突判定。
|
|
1228
|
+
* 所以这里固定排除,刷新墓碑排除表时也不得清掉。
|
|
1229
|
+
*/
|
|
1230
|
+
private static readonly PERMANENT_SYNC_EXCLUSIONS;
|
|
1124
1231
|
private readonly tombstoneExcludePrefixes;
|
|
1125
1232
|
private readonly syncFilter;
|
|
1126
1233
|
/**
|
|
@@ -1349,6 +1456,13 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1349
1456
|
*/
|
|
1350
1457
|
syncMetaToReplicas(): Promise<void>;
|
|
1351
1458
|
getSyncStatuses(): Map<string, SyncPairStatus>;
|
|
1459
|
+
/**
|
|
1460
|
+
* Subscribe to sync-pair events (`sync:end` / `sync:error`). Returns an
|
|
1461
|
+
* unsubscribe function. Handlers run after the existing state bookkeeping,
|
|
1462
|
+
* so `getSyncStatuses()` is already up to date when they fire.
|
|
1463
|
+
*/
|
|
1464
|
+
onSyncEvent(handler: (event: SyncGroupSyncEvent) => void): () => void;
|
|
1465
|
+
private emitSyncEvent;
|
|
1352
1466
|
/**
|
|
1353
1467
|
* Per-backend sync state. Includes replica descriptors that failed to connect
|
|
1354
1468
|
* (they have no sync pair, so they never show up in getSyncStatuses()).
|
|
@@ -1374,7 +1488,21 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1374
1488
|
listConflicts(): Promise<ConflictArchive[]>;
|
|
1375
1489
|
readConflictBackup(conflictId: string, fileType: 'source' | 'target' | 'resolved'): Promise<string>;
|
|
1376
1490
|
dispose(): Promise<void>;
|
|
1377
|
-
|
|
1491
|
+
/** 当前副本准备结果(不等待)。 */
|
|
1492
|
+
private collectReplicaSetupSummary;
|
|
1493
|
+
/** 副本后台准备的结果。未开启 defer 时立即 resolve。永不 reject。 */
|
|
1494
|
+
whenReplicasReady(): Promise<ReplicaSetupSummary>;
|
|
1495
|
+
/**
|
|
1496
|
+
* 后台建对:`connect()` 只做探测 + 本地初始化,副本的 createBackend /
|
|
1497
|
+
* 归属校验 / addPair 全部放在这里。
|
|
1498
|
+
*
|
|
1499
|
+
* 关键点:**校验完成前不建任何 pair**,因此不存在「pair 已存在但还没校验」
|
|
1500
|
+
* 的冲突窗口,也不需要给同步加闸门;首同步与 watch 在 pair 建好之后才启动。
|
|
1501
|
+
*/
|
|
1502
|
+
startReplicaSetupInBackground(backends: BackendDescriptor[], primaryBackendId: string, pollIntervalMs?: number, prefetchedRemote?: ConfigRepoOptions['prefetchedRemote'], opts?: {
|
|
1503
|
+
migrateVersionSidecars?: boolean;
|
|
1504
|
+
}): Promise<ReplicaSetupSummary>;
|
|
1505
|
+
setupSync(backends: BackendDescriptor[], primaryBackendId: string, pollIntervalMs?: number, prefetchedRemote?: ConfigRepoOptions['prefetchedRemote']): Promise<void>;
|
|
1378
1506
|
/** Write version sidecar for a config file (no-op for .version files). */
|
|
1379
1507
|
private writeVersionSidecar;
|
|
1380
1508
|
/** Delete version sidecar on a backend (no-op for .version files). */
|
|
@@ -1456,13 +1584,15 @@ declare class ConfigRepo implements IConfigRepo {
|
|
|
1456
1584
|
* tombstoned, so this never delete-then-recreates it.
|
|
1457
1585
|
*/
|
|
1458
1586
|
replaceBackend(id: string, type: string, options: Record<string, unknown>, description?: string): Promise<void>;
|
|
1587
|
+
private identityPromise;
|
|
1588
|
+
private ensureGroupIdentity;
|
|
1459
1589
|
/**
|
|
1460
1590
|
* 本组的同步身份(kind + id + 不可变 instanceId)。
|
|
1461
1591
|
*
|
|
1462
1592
|
* 首次调用时若 `.meta/group.json` 里还没有 `instanceId`(老部署),生成一个
|
|
1463
1593
|
* 并就地写回 —— 只写一次,之后字节相同不再落盘。
|
|
1464
1594
|
*/
|
|
1465
|
-
private
|
|
1595
|
+
private resolveGroupIdentity;
|
|
1466
1596
|
/**
|
|
1467
1597
|
* 只读收集配置组身份的候选:本地描述符 + 每个已注册后端根的 `group.json`。
|
|
1468
1598
|
* 只看 `kind` / `id` 与本组一致的根;读失败的根不参与裁决(不猜)。
|
|
@@ -1892,6 +2022,46 @@ declare function resolveRemoteIdentity(remoteFS: any, identity: GroupIdentity, m
|
|
|
1892
2022
|
* dispatches to the appropriate factory function.
|
|
1893
2023
|
*/
|
|
1894
2024
|
|
|
2025
|
+
/** 探测结果:不可达 / 空库 / 只有 group-type 标记 / 已有组描述符。 */
|
|
2026
|
+
type GroupProbeState = 'unreachable' | 'empty' | 'marked' | 'occupied';
|
|
2027
|
+
interface GroupProbeResult {
|
|
2028
|
+
state: GroupProbeState;
|
|
2029
|
+
/** 已知的组类型;`empty`/`unreachable` 下为 null。 */
|
|
2030
|
+
groupType: SyncGroupType | null;
|
|
2031
|
+
/** 远端已有的身份摘要(仅 occupied 时可能非空)。 */
|
|
2032
|
+
remote: RemoteIdentitySnapshot | null;
|
|
2033
|
+
/** 出错原因(unreachable 时是失败原因;occupied 但 kind 非法时是说明)。 */
|
|
2034
|
+
error?: string;
|
|
2035
|
+
/**
|
|
2036
|
+
* 探测时读到的完整描述符(`null` = 已确认远端没有描述符)。
|
|
2037
|
+
* 供 `createConfigRepo` 复用,避免 setupSync 再读一次同一个 `group.json`。
|
|
2038
|
+
*/
|
|
2039
|
+
descriptor?: GroupDescriptor | null;
|
|
2040
|
+
}
|
|
2041
|
+
/** 远端连不上(网络/鉴权/超时):不能当成空库处理,直接拒绝,避免误建本地组。 */
|
|
2042
|
+
declare class RemoteUnreachableError extends Error {
|
|
2043
|
+
readonly backendType: string;
|
|
2044
|
+
readonly reason?: string | undefined;
|
|
2045
|
+
readonly code = "REMOTE_UNREACHABLE";
|
|
2046
|
+
constructor(backendType: string, reason?: string | undefined);
|
|
2047
|
+
}
|
|
2048
|
+
/** 远端是空库:必须由用户显式选择组类型(initGroupType)才允许初始化。 */
|
|
2049
|
+
declare class NeedsInitError extends Error {
|
|
2050
|
+
readonly backendType: string;
|
|
2051
|
+
readonly rootHint?: string | undefined;
|
|
2052
|
+
readonly code = "NEEDS_INIT";
|
|
2053
|
+
readonly requiresInit = true;
|
|
2054
|
+
constructor(backendType: string, rootHint?: string | undefined);
|
|
2055
|
+
}
|
|
2056
|
+
/**
|
|
2057
|
+
* 三态探测:严格区分「远端不可达」与「真·空库」。
|
|
2058
|
+
*
|
|
2059
|
+
* - 严格读 `/.meta/group.json`:只有明确的“文件不存在”才继续;
|
|
2060
|
+
* 其它错误一律视为不可达,不再 `catch {}` 吞掉。
|
|
2061
|
+
* - 无描述符时读 `/.config/group-type`、`/.meta/group-type`:
|
|
2062
|
+
* 读到合法值 = `marked`;都不存在 = `empty`。
|
|
2063
|
+
*/
|
|
2064
|
+
declare function probeGroupTypeDetailed(type: string, options: Record<string, unknown>): Promise<GroupProbeResult>;
|
|
1895
2065
|
/**
|
|
1896
2066
|
* Unified entry point for connecting to a zen-fs-config sync group.
|
|
1897
2067
|
*
|
|
@@ -2140,4 +2310,4 @@ declare class ConflictStore {
|
|
|
2140
2310
|
resolve(conflictId: string, mergedContent: string | Uint8Array): Promise<void>;
|
|
2141
2311
|
}
|
|
2142
2312
|
|
|
2143
|
-
export { type AppDataBackendDescriptor, type AppDataGroup, type AppDataGroupDescriptor, type BackendConflictInfo, type BackendConflictReason, type BackendConflictResolution, type BackendConflictResolutionResult, type BackendDescriptor, type BackendFactory, type BackendInstance, type BackendMetadata, type BackendParamDef, type BackendSyncState, type BackendsMeta, CONFIG_DIR, CONFLICTS_DIR, type CacheOptions, ConfigRepo, type ConfigRepoOptions, type ConfigSerializer, type ConfigSyncGroupDescriptor, type ConfigSyncGroupHandle, type ConflictArchive, type ConflictInfo, ConflictStore, type ConnectOptions, type ConnectResult, DATA_GROUP_DEFAULT_FEATURES, FolderStore, GROUP_DESCRIPTOR_FILE, type GroupDescriptor, type GroupIdentity, type GroupInstanceIdRootResult, type IConfigRepo, type IdentityBindingResult, type IdentityCandidate, type IdentityCandidateSource, type IdentityConflictReason, type IdentityResolutionMode, type IdentityResolutionResult, 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, type PurgeKeepOptions, type PurgeLocalSyncConfigOptions, type PurgeLocalSyncConfigResult, type PurgeMtimeOptions, type PurgeableFS$1 as PurgeableFS, type RemoteIdentitySnapshot, type RemoteIdentityState, type SetGroupInstanceIdOptions, type SetGroupInstanceIdResult, type SyncGroupFeatures, type SyncGroupHandle, type SyncGroupKind, type SyncGroupType, TOMBSTONES_DIR, type TombstoneMeta, TombstoneStore, VERSIONS_DIR, type VersionMeta, type VersionMigrationOptions, type VersionMigrationResult, VersionStore, bindRemoteGroupIdentity, bindRemoteGroupIdentityDetailed, configKeyToFilePath, connect, createBackend, createConfigRepo, createSerializerChain, dataGroupLocalStoreName, deleteDataGroupLocalStore, deleteIndexedDbStore, encodeMetaPath, getAccountFields, getBackendMetadata, getExtension, hasBackend, incrementVersion, isBrowserEnv, isKeepFile, isLocalStorageUnavailableError, isMtimeSidecar, isValidGroupInstanceId, legacyVersionPathFor, listBackendMetadata, listBackends, localPrimaryType, localSyncStoreNames, mergeAccountFields, migrateVersionSidecars, newGroupInstanceId, purgeKeepFiles, purgeLocalSyncConfig, purgeMtimeSidecars, readGroupDescriptor, readVersion, registerBackend, registerFolderBackend, resolveLocalPrimary, resolveRemoteIdentity, sha256, shortInstanceId, summarizeIdentity, unregisterBackend, verifyOrRepairVersion, versionPathFor, wrapZenFSFileSystem, writeGroupDescriptor, writeVersion };
|
|
2313
|
+
export { type AppDataBackendDescriptor, type AppDataGroup, type AppDataGroupDescriptor, type BackendConflictInfo, type BackendConflictReason, type BackendConflictResolution, type BackendConflictResolutionResult, type BackendDescriptor, type BackendFactory, type BackendInstance, type BackendMetadata, type BackendParamDef, type BackendSyncState, type BackendsMeta, CONFIG_DIR, CONFLICTS_DIR, type CacheOptions, ConfigRepo, type ConfigRepoOptions, type ConfigSerializer, type ConfigSyncGroupDescriptor, type ConfigSyncGroupHandle, type ConflictArchive, type ConflictInfo, ConflictStore, type ConnectOptions, type ConnectResult, DATA_GROUP_DEFAULT_FEATURES, 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, 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, VERSIONS_DIR, type VersionMeta, type VersionMigrationOptions, type VersionMigrationResult, VersionStore, bindRemoteGroupIdentity, bindRemoteGroupIdentityDetailed, configKeyToFilePath, connect, createBackend, createConfigRepo, createSerializerChain, dataGroupLocalStoreName, deleteDataGroupLocalStore, deleteIndexedDbStore, encodeMetaPath, getAccountFields, getBackendMetadata, getExtension, 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 };
|