jazz-tools 0.15.15 → 0.16.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.
Files changed (217) hide show
  1. package/.svelte-kit/__package__/jazz.class.svelte.d.ts +2 -2
  2. package/.svelte-kit/__package__/jazz.class.svelte.d.ts.map +1 -1
  3. package/.svelte-kit/__package__/jazz.class.svelte.js +5 -5
  4. package/.svelte-kit/__package__/jazz.svelte.d.ts +2 -2
  5. package/.svelte-kit/__package__/jazz.svelte.d.ts.map +1 -1
  6. package/.turbo/turbo-build.log +46 -50
  7. package/CHANGELOG.md +35 -0
  8. package/dist/browser/index.d.ts +2 -2
  9. package/dist/browser/index.d.ts.map +1 -1
  10. package/dist/browser/index.js.map +1 -1
  11. package/dist/browser-media-images/index.d.ts +3 -1
  12. package/dist/browser-media-images/index.d.ts.map +1 -1
  13. package/dist/browser-media-images/index.js.map +1 -1
  14. package/dist/{chunk-4CFNXQE7.js → chunk-MLCNE3TL.js} +791 -698
  15. package/dist/chunk-MLCNE3TL.js.map +1 -0
  16. package/dist/index.js +363 -11
  17. package/dist/index.js.map +1 -1
  18. package/dist/react/hooks.d.ts +2 -2
  19. package/dist/react/hooks.d.ts.map +1 -1
  20. package/dist/react/index.js +2 -0
  21. package/dist/react/index.js.map +1 -1
  22. package/dist/react/testing.js +3 -1
  23. package/dist/react/testing.js.map +1 -1
  24. package/dist/react-core/hooks.d.ts +2 -2
  25. package/dist/react-core/hooks.d.ts.map +1 -1
  26. package/dist/react-core/index.js +3 -3
  27. package/dist/react-core/index.js.map +1 -1
  28. package/dist/react-native-core/hooks.d.ts +2 -2
  29. package/dist/react-native-core/hooks.d.ts.map +1 -1
  30. package/dist/react-native-core/index.js.map +1 -1
  31. package/dist/svelte/jazz.class.svelte.d.ts +2 -2
  32. package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
  33. package/dist/svelte/jazz.class.svelte.js +5 -5
  34. package/dist/svelte/jazz.svelte.d.ts +2 -2
  35. package/dist/svelte/jazz.svelte.d.ts.map +1 -1
  36. package/dist/testing.js +3 -3
  37. package/dist/testing.js.map +1 -1
  38. package/dist/tools/coValues/CoValueBase.d.ts +3 -13
  39. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  40. package/dist/tools/coValues/account.d.ts +2 -2
  41. package/dist/tools/coValues/account.d.ts.map +1 -1
  42. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  43. package/dist/tools/coValues/coList.d.ts.map +1 -1
  44. package/dist/tools/coValues/coMap.d.ts +5 -18
  45. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  46. package/dist/tools/coValues/deepLoading.d.ts +4 -1
  47. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  48. package/dist/tools/coValues/extensions/imageDef.d.ts +4 -7
  49. package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
  50. package/dist/tools/coValues/group.d.ts +1 -0
  51. package/dist/tools/coValues/group.d.ts.map +1 -1
  52. package/dist/tools/coValues/inbox.d.ts +2 -2
  53. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  54. package/dist/tools/coValues/interfaces.d.ts +58 -17
  55. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  56. package/dist/tools/coValues/request.d.ts +82 -0
  57. package/dist/tools/coValues/request.d.ts.map +1 -0
  58. package/dist/tools/coValues/schemaUnion.d.ts +5 -1
  59. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  60. package/dist/tools/exports.d.ts +4 -3
  61. package/dist/tools/exports.d.ts.map +1 -1
  62. package/dist/tools/implementation/createContext.d.ts +4 -4
  63. package/dist/tools/implementation/createContext.d.ts.map +1 -1
  64. package/dist/tools/implementation/invites.d.ts +2 -2
  65. package/dist/tools/implementation/invites.d.ts.map +1 -1
  66. package/dist/tools/implementation/schemaUtils.d.ts +8 -0
  67. package/dist/tools/implementation/schemaUtils.d.ts.map +1 -0
  68. package/dist/tools/implementation/zodSchema/coExport.d.ts +11 -1
  69. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  70. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts +22 -0
  71. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -0
  72. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +10 -0
  73. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -0
  74. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +11 -11
  75. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  76. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +34 -24
  77. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
  78. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +16 -14
  79. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  80. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +23 -17
  81. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  82. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +24 -16
  83. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  84. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +20 -9
  85. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
  86. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +18 -12
  87. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  88. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +18 -0
  89. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -0
  90. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +14 -9
  91. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  92. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +14 -9
  93. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  94. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +14 -9
  95. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  96. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +15 -13
  97. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  98. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
  99. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  100. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
  101. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  102. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
  103. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  104. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
  105. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  106. package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
  107. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  108. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
  109. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  110. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
  111. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  112. package/dist/tools/internal.d.ts +3 -2
  113. package/dist/tools/internal.d.ts.map +1 -1
  114. package/dist/tools/lib/id.d.ts +2 -0
  115. package/dist/tools/lib/id.d.ts.map +1 -0
  116. package/dist/tools/lib/utilityTypes.d.ts +10 -0
  117. package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
  118. package/dist/tools/subscribe/SubscriptionScope.d.ts +3 -2
  119. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  120. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  121. package/dist/tools/testing.d.ts +2 -2
  122. package/dist/tools/testing.d.ts.map +1 -1
  123. package/dist/tools/tests/exportImport.test.d.ts +2 -0
  124. package/dist/tools/tests/exportImport.test.d.ts.map +1 -0
  125. package/dist/tools/tests/request.test.d.ts +2 -0
  126. package/dist/tools/tests/request.test.d.ts.map +1 -0
  127. package/dist/worker/index.d.ts.map +1 -1
  128. package/dist/worker/index.js +2 -2
  129. package/dist/worker/index.js.map +1 -1
  130. package/package.json +7 -6
  131. package/src/browser/index.ts +2 -4
  132. package/src/browser-media-images/index.ts +1 -1
  133. package/src/react/hooks.tsx +2 -2
  134. package/src/react-core/hooks.ts +6 -6
  135. package/src/react-core/tests/useAccount.test.ts +2 -2
  136. package/src/react-core/tests/useCoState.test.ts +3 -2
  137. package/src/react-native-core/hooks.tsx +2 -2
  138. package/src/svelte/jazz.class.svelte.ts +10 -7
  139. package/src/svelte/jazz.svelte.ts +2 -2
  140. package/src/tools/coValues/CoValueBase.ts +8 -20
  141. package/src/tools/coValues/account.ts +18 -14
  142. package/src/tools/coValues/coFeed.ts +0 -4
  143. package/src/tools/coValues/coList.ts +7 -9
  144. package/src/tools/coValues/coMap.ts +1 -6
  145. package/src/tools/coValues/coPlainText.ts +4 -4
  146. package/src/tools/coValues/deepLoading.ts +4 -1
  147. package/src/tools/coValues/extensions/imageDef.ts +3 -3
  148. package/src/tools/coValues/group.ts +1 -0
  149. package/src/tools/coValues/inbox.ts +8 -7
  150. package/src/tools/coValues/interfaces.ts +177 -69
  151. package/src/tools/coValues/request.ts +633 -0
  152. package/src/tools/coValues/schemaUnion.ts +8 -4
  153. package/src/tools/exports.ts +11 -14
  154. package/src/tools/implementation/createContext.ts +9 -9
  155. package/src/tools/implementation/invites.ts +2 -2
  156. package/src/tools/implementation/schemaUtils.ts +18 -0
  157. package/src/tools/implementation/zodSchema/coExport.ts +14 -0
  158. package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
  159. package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +133 -0
  160. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
  161. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
  162. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
  163. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
  164. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +187 -137
  165. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
  166. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
  167. package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
  168. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
  169. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
  170. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
  171. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +28 -23
  172. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +28 -25
  173. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +86 -78
  174. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +91 -82
  175. package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
  176. package/src/tools/implementation/zodSchema/zodCo.ts +42 -75
  177. package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
  178. package/src/tools/implementation/zodSchema/zodSchema.ts +64 -102
  179. package/src/tools/internal.ts +3 -2
  180. package/src/tools/lib/id.ts +3 -0
  181. package/src/tools/lib/utilityTypes.ts +7 -0
  182. package/src/tools/subscribe/SubscriptionScope.ts +10 -1
  183. package/src/tools/subscribe/utils.ts +4 -2
  184. package/src/tools/testing.ts +4 -4
  185. package/src/tools/tests/ContextManager.test.ts +8 -9
  186. package/src/tools/tests/account.test.ts +65 -3
  187. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
  188. package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
  189. package/src/tools/tests/coFeed.test-d.ts +4 -3
  190. package/src/tools/tests/coList.test-d.ts +32 -3
  191. package/src/tools/tests/coList.test.ts +20 -2
  192. package/src/tools/tests/coMap.record.test-d.ts +31 -3
  193. package/src/tools/tests/coMap.record.test.ts +9 -9
  194. package/src/tools/tests/coMap.test-d.ts +8 -8
  195. package/src/tools/tests/coMap.test.ts +19 -5
  196. package/src/tools/tests/coOptional.test.ts +63 -1
  197. package/src/tools/tests/createContext.test.ts +7 -9
  198. package/src/tools/tests/deepLoading.test.ts +4 -10
  199. package/src/tools/tests/exportImport.test.ts +526 -0
  200. package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
  201. package/src/tools/tests/inbox.test.ts +3 -2
  202. package/src/tools/tests/load.test.ts +3 -29
  203. package/src/tools/tests/request.test.ts +951 -0
  204. package/src/tools/tests/schemaUnion.test.ts +2 -2
  205. package/src/tools/tests/subscribe.test.ts +22 -114
  206. package/src/tools/tests/testing.test.ts +6 -6
  207. package/src/tools/tests/zod.test-d.ts +27 -0
  208. package/src/tools/tests/zod.test.ts +50 -45
  209. package/src/worker/index.ts +0 -1
  210. package/tsup.config.ts +0 -2
  211. package/dist/chunk-4CFNXQE7.js.map +0 -1
  212. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
  213. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
  214. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
  215. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
  216. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
  217. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +0 -218
@@ -42,15 +42,27 @@ __export(zodReExport_exports, {
42
42
  z: () => z
43
43
  });
44
44
  import {
45
- array as zodArray,
46
- tuple as zodTuple,
47
- union as zodUnion
45
+ object as zodObject,
46
+ strictObject as zodStrictObject
48
47
  } from "zod/v4";
48
+
49
+ // src/tools/implementation/schemaUtils.ts
50
+ function removeGetters(obj) {
51
+ const result = {};
52
+ for (const key of Object.keys(obj)) {
53
+ const descriptor = Object.getOwnPropertyDescriptor(obj, key);
54
+ if (!descriptor?.get) {
55
+ result[key] = obj[key];
56
+ }
57
+ }
58
+ return result;
59
+ }
60
+
61
+ // src/tools/implementation/zodSchema/zodReExport.ts
49
62
  import {
50
63
  string,
51
64
  number,
52
65
  boolean,
53
- object,
54
66
  templateLiteral,
55
67
  json,
56
68
  date,
@@ -72,22 +84,45 @@ import {
72
84
  cidrv6,
73
85
  iso,
74
86
  int32,
75
- strictObject,
87
+ union,
76
88
  discriminatedUnion,
77
89
  int,
78
90
  optional,
91
+ array,
92
+ tuple,
79
93
  z
80
94
  } from "zod/v4";
81
- function union(options, params) {
82
- return zodUnion(options, params);
95
+ function object(shape, params) {
96
+ rejectCoValueSchemas(
97
+ shape,
98
+ "z.object() does not support collaborative types as values. Use co.map() instead"
99
+ );
100
+ return zodObject(shape, params);
101
+ }
102
+ function strictObject(shape, params) {
103
+ rejectCoValueSchemas(
104
+ shape,
105
+ "z.strictObject() does not support collaborative types as values. Use co.map() instead"
106
+ );
107
+ return zodStrictObject(shape, params);
108
+ }
109
+ function rejectCoValueSchemas(shape, errorMessage) {
110
+ if (containsCoValueSchema(shape)) {
111
+ throw Error(errorMessage);
112
+ }
83
113
  }
84
- function array(element, params) {
85
- return zodArray(element, params);
114
+ function containsCoValueSchema(shape) {
115
+ return Object.values(removeGetters(shape ?? {})).some(isAnyCoValueSchema);
86
116
  }
87
- function tuple(options, params) {
88
- return zodTuple(options, params);
117
+ function isAnyCoValueSchema(schema) {
118
+ return "collaborative" in schema && schema.collaborative === true;
89
119
  }
90
120
 
121
+ // src/tools/coValues/interfaces.ts
122
+ import {
123
+ emptyKnownState
124
+ } from "cojson";
125
+
91
126
  // src/tools/coValues/CoValueBase.ts
92
127
  import { ControlledAccount, RawAccount } from "cojson";
93
128
  var CoValueBase = class {
@@ -104,9 +139,9 @@ var CoValueBase = class {
104
139
  if (agent instanceof ControlledAccount) {
105
140
  return coValuesCache.get(
106
141
  agent.account,
107
- () => anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(
108
- agent.account
109
- )
142
+ () => coValueClassFromCoValueClassOrSchema(
143
+ RegisteredSchemas["Account"]
144
+ ).fromRaw(agent.account)
110
145
  );
111
146
  }
112
147
  return new AnonymousJazzAgent(this._raw.core.node);
@@ -718,9 +753,9 @@ var _CoList = class _CoList extends Array {
718
753
  }
719
754
  /** @category Collaboration */
720
755
  get _owner() {
721
- return this._raw.group instanceof RawAccount2 ? anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(
722
- this._raw.group
723
- ) : RegisteredSchemas["Group"].fromRaw(this._raw.group);
756
+ return this._raw.group instanceof RawAccount2 ? coValueClassFromCoValueClassOrSchema(
757
+ RegisteredSchemas["Account"]
758
+ ).fromRaw(this._raw.group) : RegisteredSchemas["Group"].fromRaw(this._raw.group);
724
759
  }
725
760
  /**
726
761
  * If a `CoList`'s items are a `coField.ref(...)`, you can use `coList._refs[i]` to access
@@ -756,9 +791,9 @@ var _CoList = class _CoList extends Array {
756
791
  if (agent instanceof ControlledAccount2) {
757
792
  return coValuesCache.get(
758
793
  agent.account,
759
- () => anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(
760
- agent.account
761
- )
794
+ () => coValueClassFromCoValueClassOrSchema(
795
+ RegisteredSchemas["Account"]
796
+ ).fromRaw(agent.account)
762
797
  );
763
798
  }
764
799
  return new AnonymousJazzAgent(this._raw.core.node);
@@ -1771,9 +1806,13 @@ var _Account = class _Account extends CoValueBase {
1771
1806
  valueID,
1772
1807
  inviteSecret
1773
1808
  );
1774
- return loadCoValue(anySchemaToCoSchema(coValueClass), valueID, {
1775
- loadAs: this
1776
- });
1809
+ return loadCoValue(
1810
+ coValueClassFromCoValueClassOrSchema(coValueClass),
1811
+ valueID,
1812
+ {
1813
+ loadAs: this
1814
+ }
1815
+ );
1777
1816
  }
1778
1817
  /** @private */
1779
1818
  static async create(options) {
@@ -1921,7 +1960,11 @@ var AccountAndGroupProxyHandler = {
1921
1960
  return true;
1922
1961
  } else if (key === "root") {
1923
1962
  if (value) {
1924
- target._raw.set("root", value.id);
1963
+ target._raw.set(
1964
+ "root",
1965
+ value.id,
1966
+ "trusting"
1967
+ );
1925
1968
  }
1926
1969
  return true;
1927
1970
  } else {
@@ -2179,6 +2222,13 @@ var Profile = class extends CoMap {
2179
2222
 
2180
2223
  // src/tools/coValues/inbox.ts
2181
2224
  import { RawAccount as RawAccount4 } from "cojson";
2225
+
2226
+ // src/tools/lib/id.ts
2227
+ function isCoValueId(id) {
2228
+ return typeof id === "string" && id.startsWith("co_z");
2229
+ }
2230
+
2231
+ // src/tools/coValues/inbox.ts
2182
2232
  function createInboxRoot(account) {
2183
2233
  if (!account.isLocalNodeOwner) {
2184
2234
  throw new Error("Account is not controlled");
@@ -2261,7 +2311,7 @@ var Inbox = class _Inbox {
2261
2311
  );
2262
2312
  }
2263
2313
  return loadCoValue(
2264
- anySchemaToCoSchema(Schema),
2314
+ coValueClassFromCoValueClassOrSchema(Schema),
2265
2315
  message.get("payload"),
2266
2316
  {
2267
2317
  loadAs: account
@@ -2403,7 +2453,7 @@ async function acceptInvite(invite, account) {
2403
2453
  account ||= activeAccountContext.get();
2404
2454
  const id = invite.slice(0, invite.indexOf("/"));
2405
2455
  const inviteSecret = invite.slice(invite.indexOf("/") + 1);
2406
- if (!id?.startsWith("co_z") || !inviteSecret.startsWith("inviteSecret_")) {
2456
+ if (!isCoValueId(id) || !inviteSecret.startsWith("inviteSecret_")) {
2407
2457
  throw new Error("Invalid inbox ticket");
2408
2458
  }
2409
2459
  if (!account.isLocalNodeOwner) {
@@ -2415,7 +2465,7 @@ async function acceptInvite(invite, account) {
2415
2465
  function getAccountIDfromSessionID(sessionID) {
2416
2466
  const until = sessionID.indexOf("_session");
2417
2467
  const accountID = sessionID.slice(0, until);
2418
- if (accountID.startsWith("co_z")) {
2468
+ if (isCoValueId(accountID)) {
2419
2469
  return accountID;
2420
2470
  }
2421
2471
  return;
@@ -2453,9 +2503,9 @@ var CoPlainText = class extends String {
2453
2503
  if (agent instanceof ControlledAccount4) {
2454
2504
  return coValuesCache.get(
2455
2505
  agent.account,
2456
- () => anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(
2457
- agent.account
2458
- )
2506
+ () => coValueClassFromCoValueClassOrSchema(
2507
+ RegisteredSchemas["Account"]
2508
+ ).fromRaw(agent.account)
2459
2509
  );
2460
2510
  }
2461
2511
  return new AnonymousJazzAgent(this._raw.core.node);
@@ -3007,7 +3057,9 @@ function getOwnerFromRawValue(raw) {
3007
3057
  const owner = raw.group;
3008
3058
  return coValuesCache.get(
3009
3059
  owner,
3010
- () => owner instanceof RawAccount6 ? anySchemaToCoSchema(RegisteredSchemas["Account"]).fromRaw(owner) : RegisteredSchemas["Group"].fromRaw(owner)
3060
+ () => owner instanceof RawAccount6 ? coValueClassFromCoValueClassOrSchema(
3061
+ RegisteredSchemas["Account"]
3062
+ ).fromRaw(owner) : RegisteredSchemas["Group"].fromRaw(owner)
3011
3063
  );
3012
3064
  }
3013
3065
  function createCoValue(ref2, raw, subscriptionScope) {
@@ -3027,11 +3079,12 @@ function createCoValue(ref2, raw, subscriptionScope) {
3027
3079
 
3028
3080
  // src/tools/subscribe/SubscriptionScope.ts
3029
3081
  var SubscriptionScope = class _SubscriptionScope {
3030
- constructor(node, resolve, id, schema, skipRetry) {
3082
+ constructor(node, resolve, id, schema, skipRetry = false, bestEffortResolution = false) {
3031
3083
  this.node = node;
3032
3084
  this.id = id;
3033
3085
  this.schema = schema;
3034
3086
  this.skipRetry = skipRetry;
3087
+ this.bestEffortResolution = bestEffortResolution;
3035
3088
  this.childNodes = /* @__PURE__ */ new Map();
3036
3089
  this.childValues = /* @__PURE__ */ new Map();
3037
3090
  this.childErrors = /* @__PURE__ */ new Map();
@@ -3162,6 +3215,9 @@ var SubscriptionScope = class _SubscriptionScope {
3162
3215
  if (this.childErrors.size === 0 && this.validationErrors.size === 0) {
3163
3216
  return void 0;
3164
3217
  }
3218
+ if (this.bestEffortResolution) {
3219
+ return void 0;
3220
+ }
3165
3221
  for (const [key, value] of this.childErrors.entries()) {
3166
3222
  if (this.autoloaded.has(key)) {
3167
3223
  continue;
@@ -3289,7 +3345,9 @@ var SubscriptionScope = class _SubscriptionScope {
3289
3345
  this.node,
3290
3346
  true,
3291
3347
  id,
3292
- descriptor
3348
+ descriptor,
3349
+ this.skipRetry,
3350
+ this.bestEffortResolution
3293
3351
  );
3294
3352
  this.childNodes.set(id, child);
3295
3353
  child.setListener((value) => this.handleChildUpdate(id, value));
@@ -3468,7 +3526,9 @@ var SubscriptionScope = class _SubscriptionScope {
3468
3526
  this.node,
3469
3527
  resolve,
3470
3528
  id,
3471
- descriptor
3529
+ descriptor,
3530
+ this.skipRetry,
3531
+ this.bestEffortResolution
3472
3532
  );
3473
3533
  this.childNodes.set(id, child);
3474
3534
  child.setListener((value) => this.handleChildUpdate(id, value, key));
@@ -3549,7 +3609,7 @@ async function createJazzContextFromExistingCredentials({
3549
3609
  crypto
3550
3610
  );
3551
3611
  const CurrentAccountSchema = PropsAccountSchema ?? RegisteredSchemas["Account"];
3552
- const AccountClass = anySchemaToCoSchema(CurrentAccountSchema);
3612
+ const AccountClass = coValueClassFromCoValueClassOrSchema(CurrentAccountSchema);
3553
3613
  const node = await LocalNode2.withLoadedAccount({
3554
3614
  accountID: credentials.accountID,
3555
3615
  accountSecret: credentials.secret,
@@ -3589,7 +3649,7 @@ async function createJazzContextForNewAccount({
3589
3649
  storage
3590
3650
  }) {
3591
3651
  const CurrentAccountSchema = PropsAccountSchema ?? RegisteredSchemas["Account"];
3592
- const AccountClass = anySchemaToCoSchema(CurrentAccountSchema);
3652
+ const AccountClass = coValueClassFromCoValueClassOrSchema(CurrentAccountSchema);
3593
3653
  const { node } = await LocalNode2.withNewlyCreatedAccount({
3594
3654
  creationProps,
3595
3655
  peersToLoadFrom,
@@ -3695,23 +3755,220 @@ function createAnonymousJazzContext({
3695
3755
  };
3696
3756
  }
3697
3757
 
3698
- // src/tools/implementation/zodSchema/zodSchema.ts
3699
- function isZodObject(schema) {
3700
- return schema.def?.type === "object";
3701
- }
3702
- function isZodArray(schema) {
3703
- return schema.def?.type === "array";
3704
- }
3705
- function isZodCustom(schema) {
3706
- return schema.def?.type === "custom";
3707
- }
3708
- function getDef(schema) {
3709
- return schema.def;
3758
+ // src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts
3759
+ var CoDiscriminatedUnionSchema = class {
3760
+ constructor(coreSchema, coValueClass) {
3761
+ this.coValueClass = coValueClass;
3762
+ this.collaborative = true;
3763
+ this.builtin = "CoDiscriminatedUnion";
3764
+ this.getDefinition = coreSchema.getDefinition;
3765
+ }
3766
+ load(id, options) {
3767
+ return this.coValueClass.load(id, options);
3768
+ }
3769
+ subscribe(id, options, listener) {
3770
+ return this.coValueClass.subscribe(id, options, listener);
3771
+ }
3772
+ getCoValueClass() {
3773
+ return this.coValueClass;
3774
+ }
3775
+ optional() {
3776
+ return coOptionalDefiner(this);
3777
+ }
3778
+ };
3779
+ function createCoreCoDiscriminatedUnionSchema(discriminator, schemas) {
3780
+ return {
3781
+ collaborative: true,
3782
+ builtin: "CoDiscriminatedUnion",
3783
+ getDefinition: () => ({
3784
+ discriminator,
3785
+ get discriminatorMap() {
3786
+ const propValues = {};
3787
+ for (const option of schemas) {
3788
+ const dm = option.getDefinition().discriminatorMap;
3789
+ if (!dm || Object.keys(dm).length === 0)
3790
+ throw new Error(
3791
+ `Invalid discriminated union option at index "${schemas.indexOf(option)}"`
3792
+ );
3793
+ for (const [k, v] of Object.entries(dm)) {
3794
+ propValues[k] ??= /* @__PURE__ */ new Set();
3795
+ for (const val of v) {
3796
+ propValues[k].add(val);
3797
+ }
3798
+ }
3799
+ }
3800
+ return propValues;
3801
+ },
3802
+ get options() {
3803
+ return schemas;
3804
+ }
3805
+ })
3806
+ };
3710
3807
  }
3711
3808
 
3809
+ // src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts
3810
+ var CoOptionalSchema = class {
3811
+ constructor(innerType) {
3812
+ this.innerType = innerType;
3813
+ this.collaborative = true;
3814
+ this.builtin = "CoOptional";
3815
+ this.getDefinition = () => ({
3816
+ innerType: this.innerType
3817
+ });
3818
+ }
3819
+ getCoValueClass() {
3820
+ return this.innerType.getCoValueClass();
3821
+ }
3822
+ };
3823
+
3712
3824
  // src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts
3713
- function enrichRichTextSchema(schema, coValueClass) {
3714
- return Object.assign(schema, {
3825
+ function createCoreCoRichTextSchema() {
3826
+ return {
3827
+ collaborative: true,
3828
+ builtin: "CoRichText"
3829
+ };
3830
+ }
3831
+ var RichTextSchema = class {
3832
+ constructor(coValueClass) {
3833
+ this.coValueClass = coValueClass;
3834
+ this.collaborative = true;
3835
+ this.builtin = "CoRichText";
3836
+ }
3837
+ create(text, options) {
3838
+ return this.coValueClass.create(text, options);
3839
+ }
3840
+ load(id, options) {
3841
+ return this.coValueClass.load(id, options);
3842
+ }
3843
+ subscribe(...args) {
3844
+ return this.coValueClass.subscribe(...args);
3845
+ }
3846
+ getCoValueClass() {
3847
+ return this.coValueClass;
3848
+ }
3849
+ optional() {
3850
+ return coOptionalDefiner(this);
3851
+ }
3852
+ };
3853
+
3854
+ // src/tools/implementation/zodSchema/zodCo.ts
3855
+ var coMapDefiner = (shape) => {
3856
+ const coreSchema = createCoreCoMapSchema(shape);
3857
+ return hydrateCoreCoValueSchema(coreSchema);
3858
+ };
3859
+ var coAccountDefiner = (shape = {
3860
+ profile: coMapDefiner({
3861
+ name: z.string(),
3862
+ inbox: z.optional(z.string()),
3863
+ inboxInvite: z.optional(z.string())
3864
+ }),
3865
+ root: coMapDefiner({})
3866
+ }) => {
3867
+ const coreSchema = createCoreAccountSchema(shape);
3868
+ return hydrateCoreCoValueSchema(coreSchema);
3869
+ };
3870
+ var coRecordDefiner = (_keyType, valueType) => {
3871
+ return coMapDefiner({}).catchall(valueType);
3872
+ };
3873
+ var coListDefiner = (element) => {
3874
+ const coreSchema = createCoreCoListSchema(element);
3875
+ return hydrateCoreCoValueSchema(coreSchema);
3876
+ };
3877
+ var coProfileDefiner = (shape = {}) => {
3878
+ const ehnancedShape = Object.assign(shape, {
3879
+ name: z.string(),
3880
+ inbox: z.optional(z.string()),
3881
+ inboxInvite: z.optional(z.string())
3882
+ });
3883
+ return coMapDefiner(ehnancedShape);
3884
+ };
3885
+ var coFeedDefiner = (element) => {
3886
+ const coreSchema = createCoreCoFeedSchema(element);
3887
+ return hydrateCoreCoValueSchema(coreSchema);
3888
+ };
3889
+ var coFileStreamDefiner = () => {
3890
+ const coreSchema = createCoreFileStreamSchema();
3891
+ return hydrateCoreCoValueSchema(coreSchema);
3892
+ };
3893
+ var coPlainTextDefiner = () => {
3894
+ const coreSchema = createCoreCoPlainTextSchema();
3895
+ return hydrateCoreCoValueSchema(coreSchema);
3896
+ };
3897
+ var coRichTextDefiner = () => {
3898
+ const coreSchema = createCoreCoRichTextSchema();
3899
+ return hydrateCoreCoValueSchema(coreSchema);
3900
+ };
3901
+ var coImageDefiner = () => {
3902
+ return ImageDefinition;
3903
+ };
3904
+ var coOptionalDefiner = (schema) => {
3905
+ return new CoOptionalSchema(schema);
3906
+ };
3907
+ var coDiscriminatedUnionDefiner = (discriminator, schemas) => {
3908
+ const coreSchema = createCoreCoDiscriminatedUnionSchema(
3909
+ discriminator,
3910
+ schemas
3911
+ );
3912
+ return hydrateCoreCoValueSchema(coreSchema);
3913
+ };
3914
+
3915
+ // src/tools/implementation/zodSchema/coExport.ts
3916
+ var coExport_exports = {};
3917
+ __export(coExport_exports, {
3918
+ DiscriminatedUnion: () => CoDiscriminatedUnionSchema,
3919
+ Feed: () => CoFeedSchema,
3920
+ FileStream: () => FileStreamSchema,
3921
+ List: () => CoListSchema,
3922
+ Optional: () => CoOptionalSchema,
3923
+ PlainText: () => PlainTextSchema,
3924
+ RichText: () => RichTextSchema,
3925
+ account: () => coAccountDefiner,
3926
+ discriminatedUnion: () => coDiscriminatedUnionDefiner,
3927
+ feed: () => coFeedDefiner,
3928
+ fileStream: () => coFileStreamDefiner,
3929
+ image: () => coImageDefiner,
3930
+ list: () => coListDefiner,
3931
+ map: () => coMapDefiner,
3932
+ optional: () => coOptionalDefiner,
3933
+ plainText: () => coPlainTextDefiner,
3934
+ profile: () => coProfileDefiner,
3935
+ record: () => coRecordDefiner,
3936
+ richText: () => coRichTextDefiner
3937
+ });
3938
+
3939
+ // src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts
3940
+ function createCoreCoMapSchema(shape, catchAll) {
3941
+ return {
3942
+ collaborative: true,
3943
+ builtin: "CoMap",
3944
+ shape,
3945
+ catchAll,
3946
+ getDefinition: () => ({
3947
+ get shape() {
3948
+ return shape;
3949
+ },
3950
+ get catchall() {
3951
+ return catchAll;
3952
+ },
3953
+ get discriminatorMap() {
3954
+ const propValues = {};
3955
+ for (const key in removeGetters(shape)) {
3956
+ if (isAnyCoValueSchema2(shape[key])) {
3957
+ continue;
3958
+ }
3959
+ const field = shape[key]._zod;
3960
+ if (field.values) {
3961
+ propValues[key] ??= /* @__PURE__ */ new Set();
3962
+ for (const v of field.values) propValues[key].add(v);
3963
+ }
3964
+ }
3965
+ return propValues;
3966
+ }
3967
+ })
3968
+ };
3969
+ }
3970
+ function enrichCoMapSchema(schema, coValueClass) {
3971
+ const coValueSchema = Object.assign(schema, {
3715
3972
  create: (...args) => {
3716
3973
  return coValueClass.create(...args);
3717
3974
  },
@@ -3721,591 +3978,176 @@ function enrichRichTextSchema(schema, coValueClass) {
3721
3978
  subscribe: (...args) => {
3722
3979
  return coValueClass.subscribe(...args);
3723
3980
  },
3981
+ findUnique: (...args) => {
3982
+ return coValueClass.findUnique(...args);
3983
+ },
3984
+ upsertUnique: (...args) => {
3985
+ return coValueClass.upsertUnique(...args);
3986
+ },
3987
+ loadUnique: (...args) => {
3988
+ return coValueClass.loadUnique(...args);
3989
+ },
3990
+ catchall: (catchAll) => {
3991
+ const schemaWithCatchAll = createCoreCoMapSchema(
3992
+ coValueSchema.getDefinition().shape,
3993
+ catchAll
3994
+ );
3995
+ return hydrateCoreCoValueSchema(schemaWithCatchAll);
3996
+ },
3997
+ withMigration: (migration) => {
3998
+ coValueClass.prototype.migrate = migration;
3999
+ return coValueSchema;
4000
+ },
3724
4001
  getCoValueClass: () => {
3725
4002
  return coValueClass;
4003
+ },
4004
+ optional: () => {
4005
+ return coOptionalDefiner(coValueSchema);
3726
4006
  }
3727
4007
  });
4008
+ return coValueSchema;
3728
4009
  }
3729
4010
 
3730
- // src/tools/implementation/zodSchema/unionUtils.ts
3731
- import { RawCoList } from "cojson";
3732
- function schemaUnionDiscriminatorFor(schema) {
3733
- if (isUnionOfCoMapsDeeply(schema)) {
3734
- if (!schema._zod.disc || schema._zod.disc.size == 0) {
3735
- throw new Error(
3736
- "z.union() of collaborative types is not supported, use co.discriminatedUnion() instead"
3737
- );
3738
- }
3739
- const discriminator = schema._zod.def.discriminator;
3740
- const field = schema._zod.disc.get(discriminator);
3741
- if (!field) {
3742
- throw new Error(
3743
- "co.discriminatedUnion() of collaborative types with non-existent discriminator key is not supported"
3744
- );
3745
- }
3746
- for (const value of field.values) {
3747
- if (typeof value !== "string" && typeof value !== "number") {
3748
- throw new Error(
3749
- "co.discriminatedUnion() of collaborative types with non-string or non-number discriminator value is not supported"
3750
- );
3751
- }
3752
- }
3753
- const availableOptions = [];
3754
- for (const option of schema._zod.def.options) {
3755
- if (option._zod.def.type === "object") {
3756
- availableOptions.push(option);
3757
- } else if (option._zod.def.type === "union") {
3758
- for (const subOption of option._zod.def.options) {
3759
- if (subOption._zod.def.type === "object") {
3760
- availableOptions.push(subOption);
3761
- }
3762
- }
3763
- } else {
3764
- throw new Error(
3765
- "Unsupported zod type in co.discriminatedUnion() of collaborative types"
3766
- );
3767
- }
3768
- }
3769
- const determineSchema = (_raw) => {
3770
- if (_raw instanceof RawCoList) {
3771
- throw new Error(
3772
- "co.discriminatedUnion() of collaborative types is not supported for CoLists"
3773
- );
3774
- }
3775
- for (const option of availableOptions) {
3776
- let match = true;
3777
- for (const key of schema._zod.disc.keys()) {
3778
- const discriminatorDef = option._zod.def.shape[key];
3779
- const discriminatorValue = _raw.get(key);
3780
- if (discriminatorValue && typeof discriminatorValue === "object") {
3781
- throw new Error("Discriminator must be a primitive value");
3782
- }
3783
- if (!discriminatorDef) {
3784
- if (key === discriminator) {
3785
- match = false;
3786
- break;
3787
- } else {
3788
- continue;
3789
- }
3790
- }
3791
- if (discriminatorDef._zod.def.type !== "literal") {
3792
- break;
3793
- }
3794
- const literalDef = discriminatorDef._zod.def;
3795
- if (!Array.from(literalDef.values).includes(discriminatorValue)) {
3796
- match = false;
3797
- break;
3798
- }
3799
- }
3800
- if (match) {
3801
- const coValueSchema = zodSchemaToCoSchema(option);
3802
- return coValueSchema.getCoValueClass();
3803
- }
3804
- }
3805
- throw new Error(
3806
- "co.discriminatedUnion() of collaborative types with no matching discriminator value found"
3807
- );
3808
- };
3809
- return determineSchema;
3810
- } else {
3811
- throw new Error(
3812
- "z.discriminatedUnion() of non-collaborative types is not supported"
3813
- );
4011
+ // src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts
4012
+ var CoListSchema = class {
4013
+ constructor(element, coValueClass) {
4014
+ this.element = element;
4015
+ this.coValueClass = coValueClass;
4016
+ this.collaborative = true;
4017
+ this.builtin = "CoList";
3814
4018
  }
3815
- }
3816
- function isUnionOfCoMapsDeeply(schema) {
3817
- if (schema instanceof z.core.$ZodUnion) {
3818
- return schema._zod.def.options.every(isCoMapOrUnionOfCoMapsDeeply);
3819
- } else {
3820
- return false;
4019
+ create(items, options) {
4020
+ return this.coValueClass.create(items, options);
3821
4021
  }
3822
- }
3823
- function isCoMapOrUnionOfCoMapsDeeply(schema) {
3824
- if (schema instanceof z.core.$ZodObject && isAnyCoValueSchema(schema)) {
3825
- return true;
3826
- } else if (schema instanceof z.core.$ZodUnion) {
3827
- return schema._zod.def.options.every(isCoMapOrUnionOfCoMapsDeeply);
3828
- } else {
3829
- return false;
4022
+ load(id, options) {
4023
+ return this.coValueClass.load(id, options);
3830
4024
  }
3831
- }
3832
- function isUnionOfPrimitivesDeeply(schema) {
3833
- if (schema instanceof z.core.$ZodUnion) {
3834
- return schema._zod.def.options.every(isUnionOfPrimitivesDeeply);
3835
- } else {
3836
- return !isAnyCoValueSchema(schema);
4025
+ subscribe(id, options, listener) {
4026
+ return this.coValueClass.subscribe(id, options, listener);
4027
+ }
4028
+ getCoValueClass() {
4029
+ return this.coValueClass;
4030
+ }
4031
+ optional() {
4032
+ return coOptionalDefiner(this);
3837
4033
  }
4034
+ };
4035
+ function createCoreCoListSchema(element) {
4036
+ return {
4037
+ collaborative: true,
4038
+ builtin: "CoList",
4039
+ element
4040
+ };
3838
4041
  }
3839
4042
 
3840
- // src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts
3841
- function schemaFieldToCoFieldDef(schema, isOptional = false) {
3842
- if (isCoValueClass(schema)) {
3843
- if (isOptional) {
3844
- return coField.ref(schema, { optional: true });
3845
- } else {
3846
- return coField.ref(schema);
3847
- }
3848
- } else if (isCoValueSchema(schema)) {
3849
- if (isAnyCoOptionalSchema(schema)) {
3850
- return coField.ref(schema.getCoValueClass(), {
3851
- optional: true
3852
- });
3853
- }
3854
- if (isOptional) {
3855
- return coField.ref(schema.getCoValueClass(), { optional: true });
3856
- } else {
3857
- return coField.ref(schema.getCoValueClass());
3858
- }
3859
- } else {
3860
- if ("_zod" in schema) {
3861
- if (schema._zod.def.type === "optional") {
3862
- const inner = zodSchemaToCoSchemaOrKeepPrimitive(
3863
- schema._zod.def.innerType
3864
- );
3865
- return schemaFieldToCoFieldDef(inner, true);
3866
- } else if (schema._zod.def.type === "string") {
3867
- return coField.string;
3868
- } else if (schema._zod.def.type === "number") {
3869
- return coField.number;
3870
- } else if (schema._zod.def.type === "boolean") {
3871
- return coField.boolean;
3872
- } else if (schema._zod.def.type === "null") {
3873
- return coField.null;
3874
- } else if (schema._zod.def.type === "enum") {
3875
- return coField.string;
3876
- } else if (schema._zod.def.type === "readonly") {
3877
- return schemaFieldToCoFieldDef(
3878
- schema.def.innerType,
3879
- isOptional
3880
- );
3881
- } else if (schema._zod.def.type === "date") {
3882
- return coField.optional.Date;
3883
- } else if (schema._zod.def.type === "template_literal") {
3884
- return coField.string;
3885
- } else if (schema._zod.def.type === "lazy") {
3886
- return schemaFieldToCoFieldDef(
3887
- schema.unwrap(),
3888
- isOptional
3889
- );
3890
- } else if (schema._zod.def.type === "default" || schema._zod.def.type === "catch") {
3891
- console.warn(
3892
- "z.default()/z.catch() are not supported in collaborative schemas. They will be ignored."
3893
- );
3894
- return schemaFieldToCoFieldDef(
3895
- schema.def.innerType,
3896
- isOptional
3897
- );
3898
- } else if (schema._zod.def.type === "literal") {
3899
- if (schema._zod.def.values.some(
3900
- (literal2) => typeof literal2 === "undefined"
3901
- )) {
3902
- throw new Error("z.literal() with undefined is not supported");
3903
- }
3904
- if (schema._zod.def.values.some((literal2) => literal2 === null)) {
3905
- throw new Error("z.literal() with null is not supported");
3906
- }
3907
- if (schema._zod.def.values.some((literal2) => typeof literal2 === "bigint")) {
3908
- throw new Error("z.literal() with bigint is not supported");
3909
- }
3910
- return coField.literal(
3911
- ...schema._zod.def.values
3912
- );
3913
- } else if (schema._zod.def.type === "object" || schema._zod.def.type === "array" || schema._zod.def.type === "tuple") {
3914
- return coField.json();
3915
- } else if (schema._zod.def.type === "custom") {
3916
- if ("builtin" in schema) {
3917
- return schemaFieldToCoFieldDef(schema.builtin, isOptional);
3918
- } else {
3919
- throw new Error(`Unsupported custom zod type`);
3920
- }
3921
- } else if (schema._zod.def.type === "union") {
3922
- if (isUnionOfPrimitivesDeeply(schema)) {
3923
- return coField.json();
3924
- } else if (isUnionOfCoMapsDeeply(schema)) {
3925
- const result = schemaUnionDiscriminatorFor(schema);
3926
- if (isOptional) {
3927
- return coField.ref(result, { optional: true });
3928
- } else {
3929
- return coField.ref(result);
3930
- }
3931
- } else {
3932
- throw new Error(
3933
- "z.union()/z.discriminatedUnion() of mixed collaborative and non-collaborative types is not supported"
3934
- );
3935
- }
3936
- } else {
3937
- throw new Error(
3938
- `Unsupported zod type: ${schema._zod?.def?.type || JSON.stringify(schema)}`
3939
- );
3940
- }
3941
- } else {
3942
- throw new Error(`Unsupported zod type: ${schema}`);
3943
- }
4043
+ // src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts
4044
+ var CoFeedSchema = class {
4045
+ constructor(element, coValueClass) {
4046
+ this.element = element;
4047
+ this.coValueClass = coValueClass;
4048
+ this.collaborative = true;
4049
+ this.builtin = "CoFeed";
3944
4050
  }
3945
- }
3946
-
3947
- // src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts
3948
- var coSchemasForZodSchemas = /* @__PURE__ */ new Map();
3949
- function isAnyCoValueSchema(schema) {
3950
- return "collaborative" in schema && schema.collaborative === true;
3951
- }
3952
- function isCoValueSchema(schema) {
3953
- return isAnyCoValueSchema(schema) && "getCoValueClass" in schema;
3954
- }
3955
- function tryZodSchemaToCoSchema(schema) {
3956
- if (isAnyCoValueSchema(schema)) {
3957
- if (coSchemasForZodSchemas.has(schema)) {
3958
- return coSchemasForZodSchemas.get(
3959
- schema
3960
- );
3961
- }
3962
- if (isAnyCoOptionalSchema(schema)) {
3963
- return null;
3964
- } else if (isZodObject(schema)) {
3965
- const def = getDef(schema);
3966
- const ClassToExtend = "builtin" in schema && schema.builtin === "Account" ? Account : CoMap;
3967
- const coValueClass = class ZCoMap extends ClassToExtend {
3968
- constructor(options) {
3969
- super(options);
3970
- for (const [field, fieldType] of Object.entries(
3971
- def.shape
3972
- )) {
3973
- this[field] = schemaFieldToCoFieldDef(
3974
- zodSchemaToCoSchemaOrKeepPrimitive(fieldType)
3975
- );
3976
- }
3977
- if (def.catchall) {
3978
- this[coField.items] = schemaFieldToCoFieldDef(
3979
- zodSchemaToCoSchemaOrKeepPrimitive(def.catchall)
3980
- );
3981
- }
3982
- }
3983
- };
3984
- const coValueSchema = ClassToExtend === Account ? enrichAccountSchema(schema, coValueClass) : enrichCoMapSchema(schema, coValueClass);
3985
- coSchemasForZodSchemas.set(schema, coValueSchema);
3986
- return coValueSchema;
3987
- } else if (isZodArray(schema)) {
3988
- const def = getDef(schema);
3989
- const coValueClass = class ZCoList extends CoList {
3990
- constructor(options) {
3991
- super(options);
3992
- this[coField.items] = schemaFieldToCoFieldDef(
3993
- zodSchemaToCoSchemaOrKeepPrimitive(def.element)
3994
- );
3995
- }
3996
- };
3997
- const coValueSchema = enrichCoListSchema(schema, coValueClass);
3998
- coSchemasForZodSchemas.set(schema, coValueSchema);
3999
- return coValueSchema;
4000
- } else if (isZodCustom(schema)) {
4001
- if ("builtin" in schema) {
4002
- if (schema.builtin === "CoFeed" && "element" in schema) {
4003
- const coValueClass = CoFeed.Of(
4004
- schemaFieldToCoFieldDef(
4005
- zodSchemaToCoSchemaOrKeepPrimitive(
4006
- schema.element
4007
- )
4008
- )
4009
- );
4010
- const coValueSchema = enrichCoFeedSchema(schema, coValueClass);
4011
- return coValueSchema;
4012
- } else if (schema.builtin === "FileStream") {
4013
- const coValueClass = FileStream;
4014
- const coValueSchema = enrichFileStreamSchema(schema, coValueClass);
4015
- return coValueSchema;
4016
- } else if (schema.builtin === "CoPlainText") {
4017
- const coValueClass = CoPlainText;
4018
- const coValueSchema = enrichPlainTextSchema(schema, coValueClass);
4019
- return coValueSchema;
4020
- } else if (schema.builtin === "CoRichText") {
4021
- const coValueClass = CoRichText;
4022
- const coValueSchema = enrichRichTextSchema(schema, coValueClass);
4023
- return coValueSchema;
4024
- } else {
4025
- throw new Error(`Unsupported builtin type: ${schema.builtin}`);
4026
- }
4027
- } else {
4028
- throw new Error(`Unsupported custom zod type`);
4029
- }
4030
- } else {
4031
- throw new Error(
4032
- `Unsupported zod CoValue type for top-level schema: ${schema._zod?.def?.type || JSON.stringify(schema, void 0, 2)}`
4033
- );
4034
- }
4035
- } else if (schema instanceof z.core.$ZodDiscriminatedUnion) {
4036
- if (isUnionOfCoMapsDeeply(schema)) {
4037
- const coValueClass = SchemaUnion.Of(schemaUnionDiscriminatorFor(schema));
4038
- const coValueSchema = enrichCoDiscriminatedUnionSchema(
4039
- schema,
4040
- coValueClass
4041
- );
4042
- return coValueSchema;
4043
- } else {
4044
- throw new Error(
4045
- "z.discriminatedUnion() of non-collaborative types is not supported as a top-level schema"
4046
- );
4047
- }
4048
- } else {
4049
- return null;
4051
+ create(init, options) {
4052
+ return this.coValueClass.create(init, options);
4050
4053
  }
4051
- }
4052
- function zodSchemaToCoSchema(schema) {
4053
- const coSchema = tryZodSchemaToCoSchema(schema);
4054
- if (!coSchema) {
4055
- throw new Error(
4056
- `Unsupported zod type: ${schema._zod?.def?.type || JSON.stringify(schema)}`
4057
- );
4054
+ load(id, options) {
4055
+ return this.coValueClass.load(id, options);
4058
4056
  }
4059
- return coSchema;
4060
- }
4061
- function anySchemaToCoSchema(schema) {
4062
- if (isCoValueClass(schema)) {
4063
- return schema;
4064
- } else if (isCoValueSchema(schema)) {
4065
- return schema.getCoValueClass();
4066
- } else if ("def" in schema) {
4067
- const coSchema = tryZodSchemaToCoSchema(
4068
- schema
4069
- );
4070
- if (!coSchema) {
4071
- throw new Error(
4072
- `Unsupported zod type: ${schema.def?.type || JSON.stringify(schema)}`
4073
- );
4074
- }
4075
- return coSchema.getCoValueClass();
4057
+ subscribe(...args) {
4058
+ return this.coValueClass.subscribe(...args);
4076
4059
  }
4077
- throw new Error(`Unsupported schema: ${JSON.stringify(schema)}`);
4078
- }
4079
- function zodSchemaToCoSchemaOrKeepPrimitive(schema) {
4080
- const coSchema = tryZodSchemaToCoSchema(schema);
4081
- if (!coSchema) {
4082
- return schema;
4060
+ getCoValueClass() {
4061
+ return this.coValueClass;
4062
+ }
4063
+ optional() {
4064
+ return coOptionalDefiner(this);
4083
4065
  }
4084
- return coSchema;
4085
- }
4086
-
4087
- // src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts
4088
- function createCoOptionalSchema(schema) {
4089
- return Object.assign(z.optional(schema), {
4090
- collaborative: true,
4091
- getCoValueClass: () => {
4092
- return schema.getCoValueClass();
4093
- }
4094
- });
4095
- }
4096
- function isAnyCoOptionalSchema(schema) {
4097
- return isAnyCoValueSchema(schema) && schema._zod.def.type === "optional";
4098
- }
4099
-
4100
- // src/tools/implementation/zodSchema/zodCo.ts
4101
- var coMapDefiner = (shape) => {
4102
- const objectSchema = z.object(shape).meta({
4103
- collaborative: true
4104
- });
4105
- const enrichedSchema = Object.assign(objectSchema, {
4106
- collaborative: true
4107
- });
4108
- return zodSchemaToCoSchema(enrichedSchema);
4109
- };
4110
- var coAccountDefiner = (shape = {
4111
- profile: coMapDefiner({
4112
- name: z.string(),
4113
- inbox: z.optional(z.string()),
4114
- inboxInvite: z.optional(z.string())
4115
- }),
4116
- root: coMapDefiner({})
4117
- }) => {
4118
- const schema = z.object(shape).meta({
4119
- collaborative: true
4120
- });
4121
- const enrichedSchema = Object.assign(schema, {
4122
- collaborative: true,
4123
- builtin: "Account"
4124
- });
4125
- return zodSchemaToCoSchema(enrichedSchema);
4126
- };
4127
- var coRecordDefiner = (_keyType, valueType) => {
4128
- return coMapDefiner({}).catchall(valueType);
4129
- };
4130
- var coListDefiner = (element) => {
4131
- const schema = z.array(element).meta({
4132
- collaborative: true
4133
- });
4134
- const enrichedSchema = Object.assign(schema, {
4135
- collaborative: true
4136
- });
4137
- return zodSchemaToCoSchema(enrichedSchema);
4138
- };
4139
- var coProfileDefiner = (shape = {}) => {
4140
- const ehnancedShape = Object.assign(shape, {
4141
- name: z.string(),
4142
- inbox: z.optional(z.string()),
4143
- inboxInvite: z.optional(z.string())
4144
- });
4145
- return coMapDefiner(ehnancedShape);
4146
4066
  };
4147
- var coFeedDefiner = (element) => {
4148
- const schema = z.instanceof(CoFeed);
4149
- const enrichedSchema = Object.assign(schema, {
4067
+ function createCoreCoFeedSchema(element) {
4068
+ return {
4150
4069
  collaborative: true,
4151
4070
  builtin: "CoFeed",
4152
4071
  element
4153
- });
4154
- return zodSchemaToCoSchema(enrichedSchema);
4155
- };
4156
- var coFileStreamDefiner = () => {
4157
- const schema = z.instanceof(FileStream);
4158
- const enrichedSchema = Object.assign(schema, {
4159
- collaborative: true,
4160
- builtin: "FileStream"
4161
- });
4162
- return zodSchemaToCoSchema(enrichedSchema);
4163
- };
4164
- var coPlainTextDefiner = () => {
4165
- const schema = z.instanceof(CoPlainText);
4166
- const enrichedSchema = Object.assign(schema, {
4167
- collaborative: true,
4168
- builtin: "CoPlainText"
4169
- });
4170
- return zodSchemaToCoSchema(enrichedSchema);
4171
- };
4172
- var coRichTextDefiner = () => {
4173
- const schema = z.instanceof(CoRichText);
4174
- const enrichedSchema = Object.assign(schema, {
4175
- collaborative: true,
4176
- builtin: "CoRichText"
4177
- });
4178
- return zodSchemaToCoSchema(enrichedSchema);
4179
- };
4180
- var coImageDefiner = () => {
4181
- return ImageDefinition;
4182
- };
4183
- var coOptionalDefiner = (schema) => {
4184
- return createCoOptionalSchema(schema);
4185
- };
4186
- var coDiscriminatedUnionDefiner = (discriminator, schemas) => {
4187
- const schema = z.discriminatedUnion(discriminator, schemas);
4188
- return zodSchemaToCoSchema(schema);
4189
- };
4190
-
4191
- // src/tools/implementation/zodSchema/coExport.ts
4192
- var coExport_exports = {};
4193
- __export(coExport_exports, {
4194
- account: () => coAccountDefiner,
4195
- discriminatedUnion: () => coDiscriminatedUnionDefiner,
4196
- feed: () => coFeedDefiner,
4197
- fileStream: () => coFileStreamDefiner,
4198
- image: () => coImageDefiner,
4199
- list: () => coListDefiner,
4200
- map: () => coMapDefiner,
4201
- optional: () => coOptionalDefiner,
4202
- plainText: () => coPlainTextDefiner,
4203
- profile: () => coProfileDefiner,
4204
- record: () => coRecordDefiner,
4205
- richText: () => coRichTextDefiner
4206
- });
4207
-
4208
- // src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts
4209
- function enrichCoMapSchema(schema, coValueClass) {
4210
- const baseCatchall = schema.catchall;
4211
- const coValueSchema = Object.assign(schema, {
4212
- create: (...args) => {
4213
- return coValueClass.create(...args);
4214
- },
4215
- load: (...args) => {
4216
- return coValueClass.load(...args);
4217
- },
4218
- subscribe: (...args) => {
4219
- return coValueClass.subscribe(...args);
4220
- },
4221
- findUnique: (...args) => {
4222
- return coValueClass.findUnique(...args);
4223
- },
4224
- upsertUnique: (...args) => {
4225
- return coValueClass.upsertUnique(...args);
4226
- },
4227
- loadUnique: (...args) => {
4228
- return coValueClass.loadUnique(...args);
4229
- },
4230
- catchall: (index) => {
4231
- const newSchema = baseCatchall(index);
4232
- const enrichedSchema = Object.assign(newSchema, {
4233
- collaborative: true
4234
- });
4235
- return zodSchemaToCoSchema(enrichedSchema);
4236
- },
4237
- withHelpers: (helpers) => {
4238
- return Object.assign(schema, helpers(schema));
4239
- },
4240
- withMigration: (migration) => {
4241
- coValueClass.prototype.migrate = migration;
4242
- return coValueSchema;
4243
- },
4244
- getCoValueClass: () => {
4245
- return coValueClass;
4246
- }
4247
- });
4248
- return coValueSchema;
4249
- }
4250
-
4251
- // src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts
4252
- function enrichCoDiscriminatedUnionSchema(schema, coValueClass) {
4253
- return Object.assign(schema, {
4254
- load: (...args) => {
4255
- return coValueClass.load(...args);
4256
- },
4257
- subscribe: (...args) => {
4258
- return coValueClass.subscribe(...args);
4259
- },
4260
- getCoValueClass: () => {
4261
- return coValueClass;
4262
- }
4263
- });
4072
+ };
4264
4073
  }
4265
4074
 
4266
- // src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts
4267
- function enrichCoListSchema(schema, coValueClass) {
4268
- return Object.assign(schema, {
4269
- create: (...args) => {
4270
- return coValueClass.create(...args);
4271
- },
4272
- load: (...args) => {
4273
- return coValueClass.load(...args);
4274
- },
4275
- subscribe: (...args) => {
4276
- return coValueClass.subscribe(...args);
4277
- },
4278
- withHelpers: (helpers) => {
4279
- return Object.assign(schema, helpers(schema));
4280
- },
4281
- getCoValueClass: () => {
4282
- return coValueClass;
4283
- }
4284
- });
4075
+ // src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts
4076
+ function createCoreCoPlainTextSchema() {
4077
+ return {
4078
+ collaborative: true,
4079
+ builtin: "CoPlainText"
4080
+ };
4285
4081
  }
4082
+ var PlainTextSchema = class {
4083
+ constructor(coValueClass) {
4084
+ this.coValueClass = coValueClass;
4085
+ this.collaborative = true;
4086
+ this.builtin = "CoPlainText";
4087
+ }
4088
+ create(text, options) {
4089
+ return this.coValueClass.create(text, options);
4090
+ }
4091
+ load(id, options) {
4092
+ return this.coValueClass.load(id, options);
4093
+ }
4094
+ subscribe(...args) {
4095
+ return this.coValueClass.subscribe(...args);
4096
+ }
4097
+ fromRaw(raw) {
4098
+ return this.coValueClass.fromRaw(raw);
4099
+ }
4100
+ getCoValueClass() {
4101
+ return this.coValueClass;
4102
+ }
4103
+ optional() {
4104
+ return coOptionalDefiner(this);
4105
+ }
4106
+ };
4286
4107
 
4287
- // src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts
4288
- function enrichCoFeedSchema(schema, coValueClass) {
4289
- return Object.assign(schema, {
4290
- create: (...args) => {
4291
- return coValueClass.create(...args);
4292
- },
4293
- load: (...args) => {
4294
- return coValueClass.load(...args);
4295
- },
4296
- subscribe: (...args) => {
4297
- return coValueClass.subscribe(...args);
4298
- },
4299
- withHelpers: (helpers) => {
4300
- return Object.assign(schema, helpers(schema));
4301
- },
4302
- getCoValueClass: () => {
4303
- return coValueClass;
4304
- }
4305
- });
4108
+ // src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts
4109
+ function createCoreFileStreamSchema() {
4110
+ return {
4111
+ collaborative: true,
4112
+ builtin: "FileStream"
4113
+ };
4306
4114
  }
4115
+ var FileStreamSchema = class {
4116
+ constructor(coValueClass) {
4117
+ this.coValueClass = coValueClass;
4118
+ this.collaborative = true;
4119
+ this.builtin = "FileStream";
4120
+ }
4121
+ create(options) {
4122
+ return this.coValueClass.create(options);
4123
+ }
4124
+ createFromBlob(blob, options) {
4125
+ return this.coValueClass.createFromBlob(blob, options);
4126
+ }
4127
+ loadAsBlob(id, options) {
4128
+ return this.coValueClass.loadAsBlob(id, options);
4129
+ }
4130
+ load(id, options) {
4131
+ return this.coValueClass.load(id, options);
4132
+ }
4133
+ subscribe(...args) {
4134
+ return this.coValueClass.subscribe(...args);
4135
+ }
4136
+ getCoValueClass() {
4137
+ return this.coValueClass;
4138
+ }
4139
+ optional() {
4140
+ return coOptionalDefiner(this);
4141
+ }
4142
+ };
4307
4143
 
4308
4144
  // src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts
4145
+ function createCoreAccountSchema(shape) {
4146
+ return {
4147
+ ...createCoreCoMapSchema(shape),
4148
+ builtin: "Account"
4149
+ };
4150
+ }
4309
4151
  function enrichAccountSchema(schema, coValueClass) {
4310
4152
  const enrichedSchema = Object.assign(schema, {
4311
4153
  create: (...args) => {
@@ -4323,9 +4165,6 @@ function enrichAccountSchema(schema, coValueClass) {
4323
4165
  subscribe: (...args) => {
4324
4166
  return coValueClass.subscribe(...args);
4325
4167
  },
4326
- withHelpers: (helpers) => {
4327
- return Object.assign(schema, helpers(schema));
4328
- },
4329
4168
  fromRaw: (...args) => {
4330
4169
  return coValueClass.fromRaw(...args);
4331
4170
  },
@@ -4342,49 +4181,268 @@ function enrichAccountSchema(schema, coValueClass) {
4342
4181
  return enrichedSchema;
4343
4182
  }
4344
4183
 
4345
- // src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts
4346
- function enrichFileStreamSchema(schema, coValueClass) {
4347
- return Object.assign(schema, {
4348
- create: (...args) => {
4349
- return coValueClass.create(...args);
4350
- },
4351
- createFromBlob: (...args) => {
4352
- return coValueClass.createFromBlob(...args);
4353
- },
4354
- load: (...args) => {
4355
- return coValueClass.load(...args);
4356
- },
4357
- loadAsBlob: (...args) => {
4358
- return coValueClass.loadAsBlob(...args);
4359
- },
4360
- subscribe: (...args) => {
4361
- return coValueClass.subscribe(...args);
4362
- },
4363
- getCoValueClass: () => {
4364
- return coValueClass;
4184
+ // src/tools/implementation/zodSchema/unionUtils.ts
4185
+ import { RawCoList } from "cojson";
4186
+ function schemaUnionDiscriminatorFor(schema) {
4187
+ if (isUnionOfCoMapsDeeply(schema)) {
4188
+ const definition = schema.getDefinition();
4189
+ const { discriminatorMap, discriminator, options } = definition;
4190
+ const field = discriminatorMap[discriminator];
4191
+ if (!field) {
4192
+ throw new Error(
4193
+ "co.discriminatedUnion() of collaborative types with non-existent discriminator key is not supported"
4194
+ );
4365
4195
  }
4366
- });
4196
+ for (const value of field) {
4197
+ if (typeof value !== "string" && typeof value !== "number") {
4198
+ throw new Error(
4199
+ "co.discriminatedUnion() of collaborative types with non-string or non-number discriminator value is not supported"
4200
+ );
4201
+ }
4202
+ }
4203
+ const availableOptions = [];
4204
+ for (const option of options) {
4205
+ if (option.builtin === "CoMap") {
4206
+ availableOptions.push(option);
4207
+ } else if (option.builtin === "CoDiscriminatedUnion") {
4208
+ for (const subOption of option.getDefinition().options) {
4209
+ if (!options.includes(subOption)) {
4210
+ options.push(subOption);
4211
+ }
4212
+ }
4213
+ } else {
4214
+ throw new Error(
4215
+ "Unsupported zod type in co.discriminatedUnion() of collaborative types"
4216
+ );
4217
+ }
4218
+ }
4219
+ const determineSchema = (_raw) => {
4220
+ if (_raw instanceof RawCoList) {
4221
+ throw new Error(
4222
+ "co.discriminatedUnion() of collaborative types is not supported for CoLists"
4223
+ );
4224
+ }
4225
+ for (const option of availableOptions) {
4226
+ let match = true;
4227
+ for (const key of Object.keys(discriminatorMap)) {
4228
+ const discriminatorDef = option.getDefinition().shape[key];
4229
+ const discriminatorValue = _raw.get(key);
4230
+ if (discriminatorValue && typeof discriminatorValue === "object") {
4231
+ throw new Error("Discriminator must be a primitive value");
4232
+ }
4233
+ if (!discriminatorDef) {
4234
+ if (key === discriminator) {
4235
+ match = false;
4236
+ break;
4237
+ } else {
4238
+ continue;
4239
+ }
4240
+ }
4241
+ if (discriminatorDef._zod.def.type !== "literal") {
4242
+ break;
4243
+ }
4244
+ const literalDef = discriminatorDef._zod.def;
4245
+ if (!Array.from(literalDef.values).includes(discriminatorValue)) {
4246
+ match = false;
4247
+ break;
4248
+ }
4249
+ }
4250
+ if (match) {
4251
+ const coValueSchema = hydrateCoreCoValueSchema(option);
4252
+ return coValueSchema.getCoValueClass();
4253
+ }
4254
+ }
4255
+ throw new Error(
4256
+ "co.discriminatedUnion() of collaborative types with no matching discriminator value found"
4257
+ );
4258
+ };
4259
+ return determineSchema;
4260
+ } else {
4261
+ throw new Error(
4262
+ "co.discriminatedUnion() of non-collaborative types is not supported"
4263
+ );
4264
+ }
4265
+ }
4266
+ function isUnionOfCoMapsDeeply(schema) {
4267
+ return schema.getDefinition().options.every(isCoMapOrUnionOfCoMapsDeeply);
4268
+ }
4269
+ function isCoMapOrUnionOfCoMapsDeeply(schema) {
4270
+ if (schema.builtin === "CoMap") {
4271
+ return true;
4272
+ } else if (schema.builtin === "CoDiscriminatedUnion") {
4273
+ return schema.getDefinition().options.every(isCoMapOrUnionOfCoMapsDeeply);
4274
+ } else {
4275
+ return false;
4276
+ }
4277
+ }
4278
+ function isUnionOfPrimitivesDeeply(schema) {
4279
+ if (schema instanceof z.core.$ZodUnion) {
4280
+ return schema._zod.def.options.every(isUnionOfPrimitivesDeeply);
4281
+ } else {
4282
+ return !isAnyCoValueSchema2(schema);
4283
+ }
4367
4284
  }
4368
4285
 
4369
- // src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts
4370
- function enrichPlainTextSchema(schema, coValueClass) {
4371
- return Object.assign(schema, {
4372
- create: (...args) => {
4373
- return coValueClass.create(...args);
4374
- },
4375
- load: (...args) => {
4376
- return coValueClass.load(...args);
4377
- },
4378
- subscribe: (...args) => {
4379
- return coValueClass.subscribe(...args);
4380
- },
4381
- fromRaw: (...args) => {
4382
- return coValueClass.fromRaw(...args);
4383
- },
4384
- getCoValueClass: () => {
4385
- return coValueClass;
4286
+ // src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts
4287
+ function schemaFieldToCoFieldDef(schema) {
4288
+ if (isCoValueClass(schema)) {
4289
+ return coField.ref(schema);
4290
+ } else if (isCoValueSchema(schema)) {
4291
+ if (schema.builtin === "CoOptional") {
4292
+ return coField.ref(schema.getCoValueClass(), {
4293
+ optional: true
4294
+ });
4386
4295
  }
4387
- });
4296
+ return coField.ref(schema.getCoValueClass());
4297
+ } else {
4298
+ if ("_zod" in schema) {
4299
+ const zodSchemaDef = schema._zod.def;
4300
+ if (zodSchemaDef.type === "optional") {
4301
+ const inner = zodSchemaDef.innerType;
4302
+ return schemaFieldToCoFieldDef(inner);
4303
+ } else if (zodSchemaDef.type === "string") {
4304
+ return coField.string;
4305
+ } else if (zodSchemaDef.type === "number") {
4306
+ return coField.number;
4307
+ } else if (zodSchemaDef.type === "boolean") {
4308
+ return coField.boolean;
4309
+ } else if (zodSchemaDef.type === "null") {
4310
+ return coField.null;
4311
+ } else if (zodSchemaDef.type === "enum") {
4312
+ return coField.string;
4313
+ } else if (zodSchemaDef.type === "readonly") {
4314
+ return schemaFieldToCoFieldDef(
4315
+ schema.def.innerType
4316
+ );
4317
+ } else if (zodSchemaDef.type === "date") {
4318
+ return coField.optional.Date;
4319
+ } else if (zodSchemaDef.type === "template_literal") {
4320
+ return coField.string;
4321
+ } else if (zodSchemaDef.type === "lazy") {
4322
+ return schemaFieldToCoFieldDef(
4323
+ schema.unwrap()
4324
+ );
4325
+ } else if (zodSchemaDef.type === "default" || zodSchemaDef.type === "catch") {
4326
+ console.warn(
4327
+ "z.default()/z.catch() are not supported in collaborative schemas. They will be ignored."
4328
+ );
4329
+ return schemaFieldToCoFieldDef(
4330
+ schema.def.innerType
4331
+ );
4332
+ } else if (zodSchemaDef.type === "literal") {
4333
+ if (zodSchemaDef.values.some((literal2) => typeof literal2 === "undefined")) {
4334
+ throw new Error("z.literal() with undefined is not supported");
4335
+ }
4336
+ if (zodSchemaDef.values.some((literal2) => literal2 === null)) {
4337
+ throw new Error("z.literal() with null is not supported");
4338
+ }
4339
+ if (zodSchemaDef.values.some((literal2) => typeof literal2 === "bigint")) {
4340
+ throw new Error("z.literal() with bigint is not supported");
4341
+ }
4342
+ return coField.literal(
4343
+ ...zodSchemaDef.values
4344
+ );
4345
+ } else if (zodSchemaDef.type === "object" || zodSchemaDef.type === "array" || zodSchemaDef.type === "tuple") {
4346
+ return coField.json();
4347
+ } else if (zodSchemaDef.type === "union") {
4348
+ if (isUnionOfPrimitivesDeeply(schema)) {
4349
+ return coField.json();
4350
+ } else {
4351
+ throw new Error(
4352
+ "z.union()/z.discriminatedUnion() of collaborative types is not supported. Use co.discriminatedUnion() instead."
4353
+ );
4354
+ }
4355
+ } else {
4356
+ throw new Error(
4357
+ `Unsupported zod type: ${schema._zod?.def?.type || JSON.stringify(schema)}`
4358
+ );
4359
+ }
4360
+ } else {
4361
+ throw new Error(`Unsupported zod type: ${schema}`);
4362
+ }
4363
+ }
4364
+ }
4365
+
4366
+ // src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts
4367
+ function isAnyCoValueSchema2(schema) {
4368
+ return "collaborative" in schema && schema.collaborative === true;
4369
+ }
4370
+ function isCoValueSchema(schema) {
4371
+ return isAnyCoValueSchema2(schema) && "getCoValueClass" in schema;
4372
+ }
4373
+ function hydrateCoreCoValueSchema(schema) {
4374
+ if (isCoValueSchema(schema)) {
4375
+ return schema;
4376
+ }
4377
+ if (schema.builtin === "CoOptional") {
4378
+ throw new Error(
4379
+ `co.optional() of collaborative types is not supported as top-level schema: ${JSON.stringify(schema)}`
4380
+ );
4381
+ } else if (schema.builtin === "CoMap" || schema.builtin === "Account") {
4382
+ const def = schema.getDefinition();
4383
+ const ClassToExtend = schema.builtin === "Account" ? Account : CoMap;
4384
+ const coValueClass = class ZCoMap extends ClassToExtend {
4385
+ constructor(options) {
4386
+ super(options);
4387
+ for (const [fieldName, fieldType] of Object.entries(def.shape)) {
4388
+ this[fieldName] = schemaFieldToCoFieldDef(
4389
+ fieldType
4390
+ );
4391
+ }
4392
+ if (def.catchall) {
4393
+ this[coField.items] = schemaFieldToCoFieldDef(
4394
+ def.catchall
4395
+ );
4396
+ }
4397
+ }
4398
+ };
4399
+ const coValueSchema = ClassToExtend === Account ? enrichAccountSchema(schema, coValueClass) : enrichCoMapSchema(schema, coValueClass);
4400
+ return coValueSchema;
4401
+ } else if (schema.builtin === "CoList") {
4402
+ const element = schema.element;
4403
+ const coValueClass = class ZCoList extends CoList {
4404
+ constructor(options) {
4405
+ super(options);
4406
+ this[coField.items] = schemaFieldToCoFieldDef(
4407
+ element
4408
+ );
4409
+ }
4410
+ };
4411
+ const coValueSchema = new CoListSchema(element, coValueClass);
4412
+ return coValueSchema;
4413
+ } else if (schema.builtin === "CoFeed") {
4414
+ const coValueClass = CoFeed.Of(
4415
+ schemaFieldToCoFieldDef(schema.element)
4416
+ );
4417
+ const coValueSchema = new CoFeedSchema(schema.element, coValueClass);
4418
+ return coValueSchema;
4419
+ } else if (schema.builtin === "FileStream") {
4420
+ const coValueClass = FileStream;
4421
+ return new FileStreamSchema(coValueClass);
4422
+ } else if (schema.builtin === "CoPlainText") {
4423
+ const coValueClass = CoPlainText;
4424
+ return new PlainTextSchema(coValueClass);
4425
+ } else if (schema.builtin === "CoRichText") {
4426
+ const coValueClass = CoRichText;
4427
+ return new RichTextSchema(coValueClass);
4428
+ } else if (schema.builtin === "CoDiscriminatedUnion") {
4429
+ const coValueClass = SchemaUnion.Of(schemaUnionDiscriminatorFor(schema));
4430
+ const coValueSchema = new CoDiscriminatedUnionSchema(schema, coValueClass);
4431
+ return coValueSchema;
4432
+ } else {
4433
+ const notReachable = schema;
4434
+ throw new Error(
4435
+ `Unsupported zod CoValue type for top-level schema: ${JSON.stringify(notReachable, void 0, 2)}`
4436
+ );
4437
+ }
4438
+ }
4439
+ function coValueClassFromCoValueClassOrSchema(schema) {
4440
+ if (isCoValueClass(schema)) {
4441
+ return schema;
4442
+ } else if (isCoValueSchema(schema)) {
4443
+ return schema.getCoValueClass();
4444
+ }
4445
+ throw new Error(`Unsupported schema: ${JSON.stringify(schema)}`);
4388
4446
  }
4389
4447
 
4390
4448
  // src/tools/coValues/extensions/imageDef.ts
@@ -4392,7 +4450,7 @@ var ImageDefinitionBase = coMapDefiner({
4392
4450
  originalSize: z.tuple([z.number(), z.number()]),
4393
4451
  placeholderDataURL: z.string().optional()
4394
4452
  }).catchall(coFileStreamDefiner());
4395
- var ImageDefinition = ImageDefinitionBase.withHelpers((Self) => ({
4453
+ var ImageDefinition = Object.assign({}, ImageDefinitionBase, {
4396
4454
  highestResAvailable(imageDef, options) {
4397
4455
  const resolutions = Object.keys(imageDef).filter(
4398
4456
  (key) => key.match(/^\d+x\d+$/)
@@ -4422,7 +4480,7 @@ var ImageDefinition = ImageDefinitionBase.withHelpers((Self) => ({
4422
4480
  stream: imageDef[highestAvailableResolution]
4423
4481
  };
4424
4482
  }
4425
- }));
4483
+ });
4426
4484
 
4427
4485
  // src/tools/implementation/ContextManager.ts
4428
4486
  import { cojsonInternals as cojsonInternals4 } from "cojson";
@@ -4993,46 +5051,6 @@ function subscribeToCoValue(cls, id, options, listener) {
4993
5051
  }
4994
5052
  return unsubscribe;
4995
5053
  }
4996
- function createCoValueObservable(initialValue = void 0) {
4997
- let currentValue = initialValue;
4998
- let subscriberCount = 0;
4999
- function subscribe(cls, id, options, listener) {
5000
- subscriberCount++;
5001
- const unsubscribe = subscribeToCoValue(
5002
- anySchemaToCoSchema(cls),
5003
- id,
5004
- {
5005
- loadAs: options.loadAs,
5006
- resolve: options.resolve,
5007
- onUnavailable: () => {
5008
- currentValue = null;
5009
- options.onUnavailable?.();
5010
- },
5011
- onUnauthorized: () => {
5012
- currentValue = null;
5013
- options.onUnauthorized?.();
5014
- },
5015
- syncResolution: options.syncResolution
5016
- },
5017
- (value) => {
5018
- currentValue = value;
5019
- listener();
5020
- }
5021
- );
5022
- return () => {
5023
- unsubscribe();
5024
- subscriberCount--;
5025
- if (subscriberCount === 0) {
5026
- currentValue = void 0;
5027
- }
5028
- };
5029
- }
5030
- const observable = {
5031
- getCurrentValue: () => currentValue,
5032
- subscribe
5033
- };
5034
- return observable;
5035
- }
5036
5054
  function subscribeToExistingCoValue(existing, options, listener) {
5037
5055
  return subscribeToCoValue(
5038
5056
  existing.constructor,
@@ -5053,9 +5071,9 @@ function isAccountInstance(instance) {
5053
5071
  return "_type" in instance && instance._type === "Account";
5054
5072
  }
5055
5073
  function parseCoValueCreateOptions(options) {
5056
- const Group4 = RegisteredSchemas["Group"];
5074
+ const Group9 = RegisteredSchemas["Group"];
5057
5075
  if (!options) {
5058
- return { owner: Group4.create(), uniqueness: void 0 };
5076
+ return { owner: Group9.create(), uniqueness: void 0 };
5059
5077
  }
5060
5078
  if ("_type" in options) {
5061
5079
  if (options._type === "Account" || options._type === "Group") {
@@ -5064,7 +5082,7 @@ function parseCoValueCreateOptions(options) {
5064
5082
  }
5065
5083
  const uniqueness = options.unique ? { uniqueness: options.unique } : void 0;
5066
5084
  return {
5067
- owner: options.owner ?? Group4.create(),
5085
+ owner: options.owner ?? Group9.create(),
5068
5086
  uniqueness
5069
5087
  };
5070
5088
  }
@@ -5074,12 +5092,86 @@ function parseGroupCreateOptions(options) {
5074
5092
  }
5075
5093
  return "_type" in options && isAccountInstance(options) ? { owner: options } : { owner: options.owner ?? activeAccountContext.get() };
5076
5094
  }
5095
+ async function exportCoValue(cls, id, options) {
5096
+ const loadAs = options.loadAs ?? activeAccountContext.get();
5097
+ const node = "node" in loadAs ? loadAs.node : loadAs._raw.core.node;
5098
+ const resolve = options.resolve ?? true;
5099
+ const rootNode = new SubscriptionScope(
5100
+ node,
5101
+ resolve,
5102
+ id,
5103
+ {
5104
+ ref: coValueClassFromCoValueClassOrSchema(cls),
5105
+ optional: false
5106
+ },
5107
+ options.skipRetry,
5108
+ options.bestEffortResolution
5109
+ );
5110
+ const value = await new Promise((resolve2) => {
5111
+ rootNode.setListener((value2) => {
5112
+ if (value2.type === "unavailable") {
5113
+ resolve2(null);
5114
+ console.error(value2.toString());
5115
+ } else if (value2.type === "unauthorized") {
5116
+ resolve2(null);
5117
+ console.error(value2.toString());
5118
+ } else if (value2.type === "loaded") {
5119
+ resolve2(value2.value);
5120
+ }
5121
+ rootNode.destroy();
5122
+ });
5123
+ });
5124
+ if (!value) {
5125
+ return null;
5126
+ }
5127
+ const valuesExported = /* @__PURE__ */ new Set();
5128
+ const contentPieces = [];
5129
+ loadContentPiecesFromSubscription(rootNode, valuesExported, contentPieces);
5130
+ return contentPieces;
5131
+ }
5132
+ function loadContentPiecesFromSubscription(subscription, valuesExported, contentPieces) {
5133
+ if (valuesExported.has(subscription.id)) {
5134
+ return;
5135
+ }
5136
+ valuesExported.add(subscription.id);
5137
+ const core2 = subscription.getCurrentValue()?._raw.core;
5138
+ if (core2) {
5139
+ loadContentPiecesFromCoValue(core2, valuesExported, contentPieces);
5140
+ }
5141
+ for (const child of subscription.childNodes.values()) {
5142
+ loadContentPiecesFromSubscription(child, valuesExported, contentPieces);
5143
+ }
5144
+ }
5145
+ function loadContentPiecesFromCoValue(core2, valuesExported, contentPieces) {
5146
+ for (const dependency of core2.getDependedOnCoValues()) {
5147
+ if (valuesExported.has(dependency)) {
5148
+ continue;
5149
+ }
5150
+ const depCoValue = core2.node.getCoValue(dependency);
5151
+ if (depCoValue.isAvailable()) {
5152
+ valuesExported.add(dependency);
5153
+ loadContentPiecesFromCoValue(depCoValue, valuesExported, contentPieces);
5154
+ }
5155
+ }
5156
+ const pieces = core2.verified.newContentSince(emptyKnownState(core2.id));
5157
+ for (const piece of pieces ?? []) {
5158
+ contentPieces.push(piece);
5159
+ }
5160
+ }
5161
+ function importContentPieces(contentPieces, loadAs) {
5162
+ const account = loadAs ?? Account.getMe();
5163
+ const node = "node" in account ? account.node : account._raw.core.node;
5164
+ for (const piece of contentPieces) {
5165
+ node.syncManager.handleNewContent(piece, "import");
5166
+ }
5167
+ }
5077
5168
 
5078
5169
  export {
5079
5170
  zodReExport_exports,
5080
5171
  loadCoValue,
5081
5172
  subscribeToCoValue,
5082
- createCoValueObservable,
5173
+ exportCoValue,
5174
+ importContentPieces,
5083
5175
  CoValueBase,
5084
5176
  CoMap,
5085
5177
  CoList,
@@ -5089,6 +5181,7 @@ export {
5089
5181
  isControlledAccount,
5090
5182
  Group,
5091
5183
  Profile,
5184
+ isCoValueId,
5092
5185
  Inbox,
5093
5186
  InboxSender,
5094
5187
  CoPlainText,
@@ -5104,9 +5197,9 @@ export {
5104
5197
  createJazzContextForNewAccount,
5105
5198
  createJazzContext,
5106
5199
  createAnonymousJazzContext,
5107
- zodSchemaToCoSchema,
5108
- anySchemaToCoSchema,
5200
+ coMapDefiner,
5109
5201
  coExport_exports,
5202
+ coValueClassFromCoValueClassOrSchema,
5110
5203
  ImageDefinition,
5111
5204
  KvStoreContext,
5112
5205
  AuthSecretStorage,
@@ -5114,4 +5207,4 @@ export {
5114
5207
  JazzContextManager
5115
5208
  };
5116
5209
  /* istanbul ignore file -- @preserve */
5117
- //# sourceMappingURL=chunk-4CFNXQE7.js.map
5210
+ //# sourceMappingURL=chunk-MLCNE3TL.js.map