zen-fs-config 0.7.19 → 0.7.21

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
@@ -551,6 +551,12 @@ interface SyncGroupHandle {
551
551
  * 不一致时界面必须让用户挑一个,不允许代码静默择一。
552
552
  */
553
553
  getIdentityCandidates(): IdentityCandidate[];
554
+ /**
555
+ * 同上,但会现场回读各根再返回(同步 getter 只给缓存)。
556
+ * 界面优先用它,才能在「本地已有身份、远端与本地不一致」时看到两个值。
557
+ * 可选:老版本实现可能没有这个方法。
558
+ */
559
+ getIdentityCandidatesAsync?(): Promise<IdentityCandidate[]>;
554
560
  /**
555
561
  * 候选之间是否互相矛盾(需要用户在界面上拍板)。
556
562
  * 可选:老版本实现可能没有这个方法,调用方按候选去重自行判断。
@@ -1466,8 +1472,16 @@ declare class ConfigRepo implements IConfigRepo {
1466
1472
  ensureGroupInstanceId(): Promise<string>;
1467
1473
  /** 组实例身份(同步读取;init 之前可能为 `undefined`)。 */
1468
1474
  getGroupInstanceId(): string | undefined;
1469
- /** 配置组的身份候选:本地描述符 + 已注册后端根上的值。 */
1475
+ /** 配置组的身份候选:本地描述符 + 已注册后端根上的值(只读缓存)。 */
1470
1476
  getIdentityCandidates(): IdentityCandidate[];
1477
+ /**
1478
+ * 现场回读各根,重算候选并填充缓存。
1479
+ *
1480
+ * 缓存只在「初始化时本地没有身份」才被填充;本地已有身份时缓存是空的,
1481
+ * 同步 getter 也就只剩一个本地值 —— 界面看不到「本地 6abe… vs 远端 5a52…」
1482
+ * 这种真正需要拍板的分歧。所以给界面一个能主动回读的入口。
1483
+ */
1484
+ getIdentityCandidatesAsync(): Promise<IdentityCandidate[]>;
1471
1485
  /** 候选是否互相矛盾(界面据此要求用户拍板)。 */
1472
1486
  isIdentityUnsettled(): boolean;
1473
1487
  /**
package/dist/index.d.ts CHANGED
@@ -551,6 +551,12 @@ interface SyncGroupHandle {
551
551
  * 不一致时界面必须让用户挑一个,不允许代码静默择一。
552
552
  */
553
553
  getIdentityCandidates(): IdentityCandidate[];
554
+ /**
555
+ * 同上,但会现场回读各根再返回(同步 getter 只给缓存)。
556
+ * 界面优先用它,才能在「本地已有身份、远端与本地不一致」时看到两个值。
557
+ * 可选:老版本实现可能没有这个方法。
558
+ */
559
+ getIdentityCandidatesAsync?(): Promise<IdentityCandidate[]>;
554
560
  /**
555
561
  * 候选之间是否互相矛盾(需要用户在界面上拍板)。
556
562
  * 可选:老版本实现可能没有这个方法,调用方按候选去重自行判断。
@@ -1466,8 +1472,16 @@ declare class ConfigRepo implements IConfigRepo {
1466
1472
  ensureGroupInstanceId(): Promise<string>;
1467
1473
  /** 组实例身份(同步读取;init 之前可能为 `undefined`)。 */
1468
1474
  getGroupInstanceId(): string | undefined;
1469
- /** 配置组的身份候选:本地描述符 + 已注册后端根上的值。 */
1475
+ /** 配置组的身份候选:本地描述符 + 已注册后端根上的值(只读缓存)。 */
1470
1476
  getIdentityCandidates(): IdentityCandidate[];
1477
+ /**
1478
+ * 现场回读各根,重算候选并填充缓存。
1479
+ *
1480
+ * 缓存只在「初始化时本地没有身份」才被填充;本地已有身份时缓存是空的,
1481
+ * 同步 getter 也就只剩一个本地值 —— 界面看不到「本地 6abe… vs 远端 5a52…」
1482
+ * 这种真正需要拍板的分歧。所以给界面一个能主动回读的入口。
1483
+ */
1484
+ getIdentityCandidatesAsync(): Promise<IdentityCandidate[]>;
1471
1485
  /** 候选是否互相矛盾(界面据此要求用户拍板)。 */
1472
1486
  isIdentityUnsettled(): boolean;
1473
1487
  /**
package/dist/index.js CHANGED
@@ -4853,12 +4853,40 @@ var ConfigRepo = class {
4853
4853
  getGroupInstanceId() {
4854
4854
  return this.instanceId;
4855
4855
  }
4856
- /** 配置组的身份候选:本地描述符 + 已注册后端根上的值。 */
4856
+ /** 配置组的身份候选:本地描述符 + 已注册后端根上的值(只读缓存)。 */
4857
4857
  getIdentityCandidates() {
4858
4858
  if (this.identityCandidates.length > 0) return this.identityCandidates.map((c) => ({ ...c }));
4859
4859
  if (!this.instanceId) return [];
4860
4860
  return [{ source: "local", instanceId: this.instanceId, rootCount: 1, kind: "config", id: CONFIG_GROUP_ID }];
4861
4861
  }
4862
+ /**
4863
+ * 现场回读各根,重算候选并填充缓存。
4864
+ *
4865
+ * 缓存只在「初始化时本地没有身份」才被填充;本地已有身份时缓存是空的,
4866
+ * 同步 getter 也就只剩一个本地值 —— 界面看不到「本地 6abe… vs 远端 5a52…」
4867
+ * 这种真正需要拍板的分歧。所以给界面一个能主动回读的入口。
4868
+ */
4869
+ async getIdentityCandidatesAsync() {
4870
+ if (!this.instanceId) return [];
4871
+ const collected = await this.collectConfigIdentityCandidates("config", CONFIG_GROUP_ID);
4872
+ if (!collected.candidates.some((c) => c.source === "local") && this.instanceId) {
4873
+ collected.candidates.unshift({
4874
+ source: "local",
4875
+ instanceId: this.instanceId,
4876
+ rootCount: 1,
4877
+ kind: "config",
4878
+ id: CONFIG_GROUP_ID
4879
+ });
4880
+ }
4881
+ this.identityCandidates = collected.candidates;
4882
+ this.configIdentityUnsettled = collected.distinct.length > 1;
4883
+ if (this.configIdentityUnsettled) {
4884
+ log5.warn(
4885
+ `[ConfigRepo] identity candidates disagree (${collected.distinct.join(" / ")}) \u2014 left to the user to decide in the UI`
4886
+ );
4887
+ }
4888
+ return this.identityCandidates.map((c) => ({ ...c }));
4889
+ }
4862
4890
  /** 候选是否互相矛盾(界面据此要求用户拍板)。 */
4863
4891
  isIdentityUnsettled() {
4864
4892
  return this.configIdentityUnsettled;
@@ -6546,9 +6574,19 @@ async function createConfigRepo(appId, options = {}) {
6546
6574
  }
6547
6575
  const declaredIdentity = await readGroupDescriptor(primaryInstance);
6548
6576
  if (declaredIdentity?.kind === "data") {
6549
- throw new Error(
6550
- `[createConfigRepo] \u8BE5\u8FDC\u7AEF\u6839\u7684 .meta/group.json \u58F0\u660E\u4E3A\u6570\u636E\u7EC4\uFF08kind=data \xB7 id=${declaredIdentity.id ?? "?"}\uFF09\u2014\u2014\u62D2\u7EDD\u5728\u8FD9\u91CC\u521D\u59CB\u5316\u914D\u7F6E\u7EC4\u3002\u8981\u7ED9\u6570\u636E\u7EC4\u6362\u4E13\u5C5E\u8FDC\u7AEF\u6839\uFF0C\u6216\u5728\u914D\u7F6E\u7EC4\u4FA7\u89E3\u9664\u5BF9\u8BE5\u6839\u7684\u7ED1\u5B9A\u3002`
6577
+ log5.warn(
6578
+ `[createConfigRepo] local primary carries a data-group descriptor (id=${declaredIdentity.id ?? "?"}) \u2014 rewriting it back to kind=config (its .meta/group.json was overwritten by sync)`
6551
6579
  );
6580
+ await writeGroupDescriptor(primaryInstance, {
6581
+ schemaVersion: 2,
6582
+ kind: "config",
6583
+ id: CONFIG_GROUP_ID,
6584
+ // 被污染的 instanceId 属于那个数据组,不能当成自己的身份继续用;
6585
+ // 重新分配一个,界面上的「实例 ID 不一致」会提示用户拍板统一。
6586
+ instanceId: newGroupInstanceId(),
6587
+ ...declaredIdentity.name ? { name: declaredIdentity.name } : {},
6588
+ features: CONFIG_GROUP_FEATURES
6589
+ });
6552
6590
  }
6553
6591
  try {
6554
6592
  const groupTypeBytes = new TextEncoder().encode("config-sync");
@@ -6825,12 +6863,20 @@ async function detectGroupTypeDetailed(type, options) {
6825
6863
  } finally {
6826
6864
  }
6827
6865
  try {
6866
+ if (remote?.kind === "data") {
6867
+ log6("detectGroupType: .meta/group.json declares kind=data \u2192 data-sync");
6868
+ return { groupType: "data-sync", remote };
6869
+ }
6870
+ if (remote?.kind === "config") {
6871
+ log6("detectGroupType: .meta/group.json declares kind=config \u2192 config-sync");
6872
+ return { groupType: "config-sync", remote };
6873
+ }
6828
6874
  for (const path of [GROUP_TYPE_FILE2, LEGACY_GROUP_TYPE_FILE2]) {
6829
6875
  try {
6830
6876
  const raw = await tempBackend.readFile(path, "utf-8");
6831
6877
  const groupType = raw.trim();
6832
6878
  if (groupType === "config-sync" || groupType === "data-sync") {
6833
- log6(`detectGroupType: detected "${groupType}" at ${path}`);
6879
+ log6(`detectGroupType: detected "${groupType}" at ${path} (no descriptor)`);
6834
6880
  return { groupType, remote };
6835
6881
  }
6836
6882
  log6(`detectGroupType: unknown group-type value "${groupType}" at ${path}, treating as null`);
@@ -6838,14 +6884,6 @@ async function detectGroupTypeDetailed(type, options) {
6838
6884
  } catch {
6839
6885
  }
6840
6886
  }
6841
- if (remote?.kind === "data") {
6842
- log6("detectGroupType: no marker, but .meta/group.json declares kind=data \u2192 data-sync");
6843
- return { groupType: "data-sync", remote };
6844
- }
6845
- if (remote?.kind === "config") {
6846
- log6("detectGroupType: no marker, but .meta/group.json declares kind=config \u2192 config-sync");
6847
- return { groupType: "config-sync", remote };
6848
- }
6849
6887
  log6(`detectGroupType: no group-type file and no descriptor kind (new backend)`);
6850
6888
  return { groupType: null, remote };
6851
6889
  } finally {
package/dist/index.mjs CHANGED
@@ -4094,12 +4094,40 @@ var ConfigRepo = class {
4094
4094
  getGroupInstanceId() {
4095
4095
  return this.instanceId;
4096
4096
  }
4097
- /** 配置组的身份候选:本地描述符 + 已注册后端根上的值。 */
4097
+ /** 配置组的身份候选:本地描述符 + 已注册后端根上的值(只读缓存)。 */
4098
4098
  getIdentityCandidates() {
4099
4099
  if (this.identityCandidates.length > 0) return this.identityCandidates.map((c) => ({ ...c }));
4100
4100
  if (!this.instanceId) return [];
4101
4101
  return [{ source: "local", instanceId: this.instanceId, rootCount: 1, kind: "config", id: CONFIG_GROUP_ID }];
4102
4102
  }
4103
+ /**
4104
+ * 现场回读各根,重算候选并填充缓存。
4105
+ *
4106
+ * 缓存只在「初始化时本地没有身份」才被填充;本地已有身份时缓存是空的,
4107
+ * 同步 getter 也就只剩一个本地值 —— 界面看不到「本地 6abe… vs 远端 5a52…」
4108
+ * 这种真正需要拍板的分歧。所以给界面一个能主动回读的入口。
4109
+ */
4110
+ async getIdentityCandidatesAsync() {
4111
+ if (!this.instanceId) return [];
4112
+ const collected = await this.collectConfigIdentityCandidates("config", CONFIG_GROUP_ID);
4113
+ if (!collected.candidates.some((c) => c.source === "local") && this.instanceId) {
4114
+ collected.candidates.unshift({
4115
+ source: "local",
4116
+ instanceId: this.instanceId,
4117
+ rootCount: 1,
4118
+ kind: "config",
4119
+ id: CONFIG_GROUP_ID
4120
+ });
4121
+ }
4122
+ this.identityCandidates = collected.candidates;
4123
+ this.configIdentityUnsettled = collected.distinct.length > 1;
4124
+ if (this.configIdentityUnsettled) {
4125
+ log5.warn(
4126
+ `[ConfigRepo] identity candidates disagree (${collected.distinct.join(" / ")}) \u2014 left to the user to decide in the UI`
4127
+ );
4128
+ }
4129
+ return this.identityCandidates.map((c) => ({ ...c }));
4130
+ }
4103
4131
  /** 候选是否互相矛盾(界面据此要求用户拍板)。 */
4104
4132
  isIdentityUnsettled() {
4105
4133
  return this.configIdentityUnsettled;
@@ -5787,9 +5815,19 @@ async function createConfigRepo(appId, options = {}) {
5787
5815
  }
5788
5816
  const declaredIdentity = await readGroupDescriptor(primaryInstance);
5789
5817
  if (declaredIdentity?.kind === "data") {
5790
- throw new Error(
5791
- `[createConfigRepo] \u8BE5\u8FDC\u7AEF\u6839\u7684 .meta/group.json \u58F0\u660E\u4E3A\u6570\u636E\u7EC4\uFF08kind=data \xB7 id=${declaredIdentity.id ?? "?"}\uFF09\u2014\u2014\u62D2\u7EDD\u5728\u8FD9\u91CC\u521D\u59CB\u5316\u914D\u7F6E\u7EC4\u3002\u8981\u7ED9\u6570\u636E\u7EC4\u6362\u4E13\u5C5E\u8FDC\u7AEF\u6839\uFF0C\u6216\u5728\u914D\u7F6E\u7EC4\u4FA7\u89E3\u9664\u5BF9\u8BE5\u6839\u7684\u7ED1\u5B9A\u3002`
5818
+ log5.warn(
5819
+ `[createConfigRepo] local primary carries a data-group descriptor (id=${declaredIdentity.id ?? "?"}) \u2014 rewriting it back to kind=config (its .meta/group.json was overwritten by sync)`
5792
5820
  );
5821
+ await writeGroupDescriptor(primaryInstance, {
5822
+ schemaVersion: 2,
5823
+ kind: "config",
5824
+ id: CONFIG_GROUP_ID,
5825
+ // 被污染的 instanceId 属于那个数据组,不能当成自己的身份继续用;
5826
+ // 重新分配一个,界面上的「实例 ID 不一致」会提示用户拍板统一。
5827
+ instanceId: newGroupInstanceId(),
5828
+ ...declaredIdentity.name ? { name: declaredIdentity.name } : {},
5829
+ features: CONFIG_GROUP_FEATURES
5830
+ });
5793
5831
  }
5794
5832
  try {
5795
5833
  const groupTypeBytes = new TextEncoder().encode("config-sync");
@@ -6065,12 +6103,20 @@ async function detectGroupTypeDetailed(type, options) {
6065
6103
  } finally {
6066
6104
  }
6067
6105
  try {
6106
+ if (remote?.kind === "data") {
6107
+ log6("detectGroupType: .meta/group.json declares kind=data \u2192 data-sync");
6108
+ return { groupType: "data-sync", remote };
6109
+ }
6110
+ if (remote?.kind === "config") {
6111
+ log6("detectGroupType: .meta/group.json declares kind=config \u2192 config-sync");
6112
+ return { groupType: "config-sync", remote };
6113
+ }
6068
6114
  for (const path of [GROUP_TYPE_FILE2, LEGACY_GROUP_TYPE_FILE2]) {
6069
6115
  try {
6070
6116
  const raw = await tempBackend.readFile(path, "utf-8");
6071
6117
  const groupType = raw.trim();
6072
6118
  if (groupType === "config-sync" || groupType === "data-sync") {
6073
- log6(`detectGroupType: detected "${groupType}" at ${path}`);
6119
+ log6(`detectGroupType: detected "${groupType}" at ${path} (no descriptor)`);
6074
6120
  return { groupType, remote };
6075
6121
  }
6076
6122
  log6(`detectGroupType: unknown group-type value "${groupType}" at ${path}, treating as null`);
@@ -6078,14 +6124,6 @@ async function detectGroupTypeDetailed(type, options) {
6078
6124
  } catch {
6079
6125
  }
6080
6126
  }
6081
- if (remote?.kind === "data") {
6082
- log6("detectGroupType: no marker, but .meta/group.json declares kind=data \u2192 data-sync");
6083
- return { groupType: "data-sync", remote };
6084
- }
6085
- if (remote?.kind === "config") {
6086
- log6("detectGroupType: no marker, but .meta/group.json declares kind=config \u2192 config-sync");
6087
- return { groupType: "config-sync", remote };
6088
- }
6089
6127
  log6(`detectGroupType: no group-type file and no descriptor kind (new backend)`);
6090
6128
  return { groupType: null, remote };
6091
6129
  } finally {
@@ -30810,12 +30810,40 @@ var ZenFSConfig = (() => {
30810
30810
  getGroupInstanceId() {
30811
30811
  return this.instanceId;
30812
30812
  }
30813
- /** 配置组的身份候选:本地描述符 + 已注册后端根上的值。 */
30813
+ /** 配置组的身份候选:本地描述符 + 已注册后端根上的值(只读缓存)。 */
30814
30814
  getIdentityCandidates() {
30815
30815
  if (this.identityCandidates.length > 0) return this.identityCandidates.map((c) => ({ ...c }));
30816
30816
  if (!this.instanceId) return [];
30817
30817
  return [{ source: "local", instanceId: this.instanceId, rootCount: 1, kind: "config", id: CONFIG_GROUP_ID }];
30818
30818
  }
30819
+ /**
30820
+ * 现场回读各根,重算候选并填充缓存。
30821
+ *
30822
+ * 缓存只在「初始化时本地没有身份」才被填充;本地已有身份时缓存是空的,
30823
+ * 同步 getter 也就只剩一个本地值 —— 界面看不到「本地 6abe… vs 远端 5a52…」
30824
+ * 这种真正需要拍板的分歧。所以给界面一个能主动回读的入口。
30825
+ */
30826
+ async getIdentityCandidatesAsync() {
30827
+ if (!this.instanceId) return [];
30828
+ const collected = await this.collectConfigIdentityCandidates("config", CONFIG_GROUP_ID);
30829
+ if (!collected.candidates.some((c) => c.source === "local") && this.instanceId) {
30830
+ collected.candidates.unshift({
30831
+ source: "local",
30832
+ instanceId: this.instanceId,
30833
+ rootCount: 1,
30834
+ kind: "config",
30835
+ id: CONFIG_GROUP_ID
30836
+ });
30837
+ }
30838
+ this.identityCandidates = collected.candidates;
30839
+ this.configIdentityUnsettled = collected.distinct.length > 1;
30840
+ if (this.configIdentityUnsettled) {
30841
+ log9.warn(
30842
+ `[ConfigRepo] identity candidates disagree (${collected.distinct.join(" / ")}) \u2014 left to the user to decide in the UI`
30843
+ );
30844
+ }
30845
+ return this.identityCandidates.map((c) => ({ ...c }));
30846
+ }
30819
30847
  /** 候选是否互相矛盾(界面据此要求用户拍板)。 */
30820
30848
  isIdentityUnsettled() {
30821
30849
  return this.configIdentityUnsettled;
@@ -32503,9 +32531,19 @@ var ZenFSConfig = (() => {
32503
32531
  }
32504
32532
  const declaredIdentity = await readGroupDescriptor(primaryInstance);
32505
32533
  if (declaredIdentity?.kind === "data") {
32506
- throw new Error(
32507
- `[createConfigRepo] \u8BE5\u8FDC\u7AEF\u6839\u7684 .meta/group.json \u58F0\u660E\u4E3A\u6570\u636E\u7EC4\uFF08kind=data \xB7 id=${declaredIdentity.id ?? "?"}\uFF09\u2014\u2014\u62D2\u7EDD\u5728\u8FD9\u91CC\u521D\u59CB\u5316\u914D\u7F6E\u7EC4\u3002\u8981\u7ED9\u6570\u636E\u7EC4\u6362\u4E13\u5C5E\u8FDC\u7AEF\u6839\uFF0C\u6216\u5728\u914D\u7F6E\u7EC4\u4FA7\u89E3\u9664\u5BF9\u8BE5\u6839\u7684\u7ED1\u5B9A\u3002`
32534
+ log9.warn(
32535
+ `[createConfigRepo] local primary carries a data-group descriptor (id=${declaredIdentity.id ?? "?"}) \u2014 rewriting it back to kind=config (its .meta/group.json was overwritten by sync)`
32508
32536
  );
32537
+ await writeGroupDescriptor(primaryInstance, {
32538
+ schemaVersion: 2,
32539
+ kind: "config",
32540
+ id: CONFIG_GROUP_ID,
32541
+ // 被污染的 instanceId 属于那个数据组,不能当成自己的身份继续用;
32542
+ // 重新分配一个,界面上的「实例 ID 不一致」会提示用户拍板统一。
32543
+ instanceId: newGroupInstanceId(),
32544
+ ...declaredIdentity.name ? { name: declaredIdentity.name } : {},
32545
+ features: CONFIG_GROUP_FEATURES
32546
+ });
32509
32547
  }
32510
32548
  try {
32511
32549
  const groupTypeBytes = new TextEncoder().encode("config-sync");
@@ -32782,12 +32820,20 @@ var ZenFSConfig = (() => {
32782
32820
  } finally {
32783
32821
  }
32784
32822
  try {
32823
+ if (remote?.kind === "data") {
32824
+ log10("detectGroupType: .meta/group.json declares kind=data \u2192 data-sync");
32825
+ return { groupType: "data-sync", remote };
32826
+ }
32827
+ if (remote?.kind === "config") {
32828
+ log10("detectGroupType: .meta/group.json declares kind=config \u2192 config-sync");
32829
+ return { groupType: "config-sync", remote };
32830
+ }
32785
32831
  for (const path of [GROUP_TYPE_FILE2, LEGACY_GROUP_TYPE_FILE2]) {
32786
32832
  try {
32787
32833
  const raw = await tempBackend.readFile(path, "utf-8");
32788
32834
  const groupType = raw.trim();
32789
32835
  if (groupType === "config-sync" || groupType === "data-sync") {
32790
- log10(`detectGroupType: detected "${groupType}" at ${path}`);
32836
+ log10(`detectGroupType: detected "${groupType}" at ${path} (no descriptor)`);
32791
32837
  return { groupType, remote };
32792
32838
  }
32793
32839
  log10(`detectGroupType: unknown group-type value "${groupType}" at ${path}, treating as null`);
@@ -32795,14 +32841,6 @@ var ZenFSConfig = (() => {
32795
32841
  } catch {
32796
32842
  }
32797
32843
  }
32798
- if (remote?.kind === "data") {
32799
- log10("detectGroupType: no marker, but .meta/group.json declares kind=data \u2192 data-sync");
32800
- return { groupType: "data-sync", remote };
32801
- }
32802
- if (remote?.kind === "config") {
32803
- log10("detectGroupType: no marker, but .meta/group.json declares kind=config \u2192 config-sync");
32804
- return { groupType: "config-sync", remote };
32805
- }
32806
32844
  log10(`detectGroupType: no group-type file and no descriptor kind (new backend)`);
32807
32845
  return { groupType: null, remote };
32808
32846
  } finally {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-fs-config",
3
- "version": "0.7.19",
3
+ "version": "0.7.21",
4
4
  "description": "Distributed config management library built on ZenFS, zen-fs-cache, and zen-fs-sync",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",