jazz-tools 0.17.14 → 0.18.1

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 (250) hide show
  1. package/.svelte-kit/__package__/jazz.class.svelte.js +1 -1
  2. package/.svelte-kit/__package__/media/image.svelte +3 -9
  3. package/.svelte-kit/__package__/media/image.svelte.d.ts +1 -6
  4. package/.svelte-kit/__package__/media/image.svelte.d.ts.map +1 -1
  5. package/.svelte-kit/__package__/media/image.types.d.ts +7 -0
  6. package/.svelte-kit/__package__/media/image.types.d.ts.map +1 -0
  7. package/.svelte-kit/__package__/media/image.types.js +1 -0
  8. package/.svelte-kit/__package__/tests/media/image.svelte.test.js +31 -31
  9. package/.turbo/turbo-build.log +60 -46
  10. package/CHANGELOG.md +41 -0
  11. package/dist/better-auth/auth/client.d.ts +29 -0
  12. package/dist/better-auth/auth/client.d.ts.map +1 -0
  13. package/dist/better-auth/auth/client.js +127 -0
  14. package/dist/better-auth/auth/client.js.map +1 -0
  15. package/dist/better-auth/auth/react.d.ts +2170 -0
  16. package/dist/better-auth/auth/react.d.ts.map +1 -0
  17. package/dist/better-auth/auth/react.js +40 -0
  18. package/dist/better-auth/auth/react.js.map +1 -0
  19. package/dist/better-auth/auth/server.d.ts +14 -0
  20. package/dist/better-auth/auth/server.d.ts.map +1 -0
  21. package/dist/better-auth/auth/server.js +198 -0
  22. package/dist/better-auth/auth/server.js.map +1 -0
  23. package/dist/better-auth/auth/tests/client.test.d.ts +2 -0
  24. package/dist/better-auth/auth/tests/client.test.d.ts.map +1 -0
  25. package/dist/better-auth/auth/tests/server.test.d.ts +2 -0
  26. package/dist/better-auth/auth/tests/server.test.d.ts.map +1 -0
  27. package/dist/browser/index.js +2 -2
  28. package/dist/browser/index.js.map +1 -1
  29. package/dist/{chunk-LZOF6WP5.js → chunk-IERUTUXB.js} +1336 -1017
  30. package/dist/chunk-IERUTUXB.js.map +1 -0
  31. package/dist/index.js +18 -18
  32. package/dist/index.js.map +1 -1
  33. package/dist/inspector/{custom-element-ZSNTCECD.js → custom-element-WCY6D3QJ.js} +3 -3
  34. package/dist/inspector/{custom-element-ZSNTCECD.js.map → custom-element-WCY6D3QJ.js.map} +1 -1
  35. package/dist/inspector/index.js +1 -1
  36. package/dist/inspector/index.js.map +1 -1
  37. package/dist/inspector/register-custom-element.js +1 -1
  38. package/dist/media/{chunk-E5J3WLQW.js → chunk-KR2V6X2N.js} +14 -9
  39. package/dist/media/chunk-KR2V6X2N.js.map +1 -0
  40. package/dist/media/create-image.d.ts +6 -6
  41. package/dist/media/index.browser.d.ts +6 -6
  42. package/dist/media/index.browser.js +1 -1
  43. package/dist/media/index.d.ts +1 -1
  44. package/dist/media/index.js +1 -1
  45. package/dist/media/index.native.d.ts +6 -6
  46. package/dist/media/index.native.js +1 -1
  47. package/dist/media/utils.d.ts.map +1 -1
  48. package/dist/prosemirror/index.js +2 -2
  49. package/dist/prosemirror/index.js.map +1 -1
  50. package/dist/react/index.js +7 -5
  51. package/dist/react/index.js.map +1 -1
  52. package/dist/react-core/hooks.d.ts.map +1 -1
  53. package/dist/react-core/index.js +4675 -23
  54. package/dist/react-core/index.js.map +1 -1
  55. package/dist/react-native-core/index.js +1 -1
  56. package/dist/react-native-core/index.js.map +1 -1
  57. package/dist/svelte/jazz.class.svelte.js +1 -1
  58. package/dist/svelte/media/image.svelte +3 -9
  59. package/dist/svelte/media/image.svelte.d.ts +1 -6
  60. package/dist/svelte/media/image.svelte.d.ts.map +1 -1
  61. package/dist/svelte/media/image.types.d.ts +7 -0
  62. package/dist/svelte/media/image.types.d.ts.map +1 -0
  63. package/dist/svelte/media/image.types.js +1 -0
  64. package/dist/svelte/tests/media/image.svelte.test.js +31 -31
  65. package/dist/testing.js +18 -14
  66. package/dist/testing.js.map +1 -1
  67. package/dist/tools/coValues/CoFieldInit.d.ts +13 -0
  68. package/dist/tools/coValues/CoFieldInit.d.ts.map +1 -0
  69. package/dist/tools/coValues/CoValueBase.d.ts +18 -15
  70. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  71. package/dist/tools/coValues/account.d.ts +101 -46
  72. package/dist/tools/coValues/account.d.ts.map +1 -1
  73. package/dist/tools/coValues/coFeed.d.ts +78 -62
  74. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  75. package/dist/tools/coValues/coList.d.ts +212 -99
  76. package/dist/tools/coValues/coList.d.ts.map +1 -1
  77. package/dist/tools/coValues/coMap.d.ts +210 -192
  78. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  79. package/dist/tools/coValues/coPlainText.d.ts +30 -22
  80. package/dist/tools/coValues/coPlainText.d.ts.map +1 -1
  81. package/dist/tools/coValues/deepLoading.d.ts +13 -13
  82. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  83. package/dist/tools/coValues/extensions/imageDef.d.ts +1 -1
  84. package/dist/tools/coValues/group.d.ts +32 -32
  85. package/dist/tools/coValues/group.d.ts.map +1 -1
  86. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  87. package/dist/tools/coValues/interfaces.d.ts +18 -17
  88. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  89. package/dist/tools/coValues/profile.d.ts +6 -5
  90. package/dist/tools/coValues/profile.d.ts.map +1 -1
  91. package/dist/tools/coValues/schemaUnion.d.ts +3 -3
  92. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  93. package/dist/tools/exports.d.ts +1 -1
  94. package/dist/tools/exports.d.ts.map +1 -1
  95. package/dist/tools/implementation/anonymousJazzAgent.d.ts +2 -1
  96. package/dist/tools/implementation/anonymousJazzAgent.d.ts.map +1 -1
  97. package/dist/tools/implementation/schema.d.ts +5 -5
  98. package/dist/tools/implementation/schema.d.ts.map +1 -1
  99. package/dist/tools/implementation/symbols.d.ts +2 -0
  100. package/dist/tools/implementation/symbols.d.ts.map +1 -1
  101. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -2
  102. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  103. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +6 -2
  104. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  105. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +8 -3
  106. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  107. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +12 -7
  108. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  109. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +13 -7
  110. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  111. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +11 -2
  112. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  113. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +4 -0
  114. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  115. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +4 -0
  116. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  117. package/dist/tools/implementation/zodSchema/typeConverters/{CoFieldInit.d.ts → CoFieldSchemaInit.d.ts} +7 -7
  118. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldSchemaInit.d.ts.map +1 -0
  119. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +4 -4
  120. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  121. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +4 -4
  122. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  123. package/dist/tools/implementation/zodSchema/zodCo.d.ts +3 -3
  124. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  125. package/dist/tools/internal.d.ts +2 -1
  126. package/dist/tools/internal.d.ts.map +1 -1
  127. package/dist/tools/lib/migration.d.ts +1 -1
  128. package/dist/tools/lib/migration.d.ts.map +1 -1
  129. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  130. package/dist/tools/subscribe/index.d.ts +1 -1
  131. package/dist/tools/subscribe/index.d.ts.map +1 -1
  132. package/dist/tools/subscribe/utils.d.ts +2 -2
  133. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  134. package/dist/tools/testing.d.ts.map +1 -1
  135. package/dist/tools/tests/utils.d.ts +2 -6
  136. package/dist/tools/tests/utils.d.ts.map +1 -1
  137. package/dist/worker/index.js +3 -3
  138. package/dist/worker/index.js.map +1 -1
  139. package/package.json +23 -4
  140. package/src/better-auth/auth/client.ts +169 -0
  141. package/src/better-auth/auth/react.tsx +105 -0
  142. package/src/better-auth/auth/server.ts +250 -0
  143. package/src/better-auth/auth/tests/client.test.ts +249 -0
  144. package/src/better-auth/auth/tests/server.test.ts +226 -0
  145. package/src/browser/auth/PasskeyAuth.ts +2 -2
  146. package/src/browser/createBrowserContext.ts +2 -2
  147. package/src/browser/tests/PasskeyAuth.test.ts +2 -2
  148. package/src/inspector/custom-element.tsx +2 -2
  149. package/src/inspector/viewer/new-app.tsx +1 -1
  150. package/src/media/create-image.test.ts +7 -7
  151. package/src/media/create-image.ts +5 -3
  152. package/src/media/index.ts +1 -1
  153. package/src/media/utils.test.ts +72 -66
  154. package/src/media/utils.ts +9 -6
  155. package/src/prosemirror/lib/plugin.ts +1 -1
  156. package/src/prosemirror/lib/sync.ts +1 -1
  157. package/src/prosemirror/tests/plugin.test.ts +4 -4
  158. package/src/react/media/image.tsx +2 -2
  159. package/src/react/tests/media/image.test.tsx +52 -32
  160. package/src/react-core/hooks.ts +11 -5
  161. package/src/react-core/tests/useAccount.test.ts +16 -22
  162. package/src/react-core/tests/useCoState.test.ts +19 -19
  163. package/src/react-core/tests/useInboxSender.test.ts +5 -2
  164. package/src/react-core/tests/usePassPhraseAuth.test.ts +6 -6
  165. package/src/react-native-core/media/image.tsx +1 -1
  166. package/src/svelte/jazz.class.svelte.ts +1 -1
  167. package/src/svelte/media/image.svelte +3 -9
  168. package/src/svelte/media/image.types.ts +7 -0
  169. package/src/svelte/tests/media/image.svelte.test.ts +34 -32
  170. package/src/tools/auth/DemoAuth.ts +2 -2
  171. package/src/tools/auth/PassphraseAuth.ts +2 -2
  172. package/src/tools/auth/clerk/index.ts +2 -2
  173. package/src/tools/auth/clerk/tests/JazzClerkAuth.test.ts +1 -1
  174. package/src/tools/coValues/CoFieldInit.ts +20 -0
  175. package/src/tools/coValues/CoValueBase.ts +40 -60
  176. package/src/tools/coValues/account.ts +311 -232
  177. package/src/tools/coValues/coFeed.ts +185 -153
  178. package/src/tools/coValues/coList.ts +507 -334
  179. package/src/tools/coValues/coMap.ts +434 -286
  180. package/src/tools/coValues/coPlainText.ts +94 -110
  181. package/src/tools/coValues/deepLoading.ts +13 -13
  182. package/src/tools/coValues/group.ts +100 -114
  183. package/src/tools/coValues/inbox.ts +16 -14
  184. package/src/tools/coValues/interfaces.ts +49 -31
  185. package/src/tools/coValues/profile.ts +8 -6
  186. package/src/tools/coValues/request.ts +9 -9
  187. package/src/tools/coValues/schemaUnion.ts +11 -5
  188. package/src/tools/exports.ts +1 -1
  189. package/src/tools/implementation/ContextManager.ts +4 -4
  190. package/src/tools/implementation/anonymousJazzAgent.ts +2 -1
  191. package/src/tools/implementation/createContext.ts +1 -1
  192. package/src/tools/implementation/devtoolsFormatters.ts +9 -9
  193. package/src/tools/implementation/invites.ts +2 -2
  194. package/src/tools/implementation/schema.ts +7 -7
  195. package/src/tools/implementation/symbols.ts +3 -0
  196. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +2 -2
  197. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +11 -2
  198. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +18 -7
  199. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +17 -7
  200. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +20 -11
  201. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +19 -2
  202. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +6 -0
  203. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +6 -0
  204. package/src/tools/implementation/zodSchema/typeConverters/{CoFieldInit.ts → CoFieldSchemaInit.ts} +11 -11
  205. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +4 -4
  206. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +4 -4
  207. package/src/tools/implementation/zodSchema/zodCo.ts +3 -3
  208. package/src/tools/internal.ts +2 -1
  209. package/src/tools/lib/migration.ts +5 -5
  210. package/src/tools/subscribe/SubscriptionScope.ts +32 -24
  211. package/src/tools/subscribe/index.ts +4 -4
  212. package/src/tools/subscribe/utils.ts +11 -11
  213. package/src/tools/testing.ts +17 -13
  214. package/src/tools/tests/ContextManager.test.ts +70 -59
  215. package/src/tools/tests/PassphraseAuth.test.ts +2 -2
  216. package/src/tools/tests/account.test.ts +188 -67
  217. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +12 -6
  218. package/src/tools/tests/coDiscriminatedUnion.test.ts +26 -17
  219. package/src/tools/tests/coFeed.test-d.ts +18 -17
  220. package/src/tools/tests/coFeed.test.ts +108 -97
  221. package/src/tools/tests/coList.test-d.ts +18 -23
  222. package/src/tools/tests/coList.test.ts +350 -165
  223. package/src/tools/tests/coMap.record.test-d.ts +9 -13
  224. package/src/tools/tests/coMap.record.test.ts +37 -23
  225. package/src/tools/tests/coMap.test-d.ts +43 -21
  226. package/src/tools/tests/coMap.test.ts +459 -182
  227. package/src/tools/tests/coOptional.test.ts +28 -13
  228. package/src/tools/tests/coPlainText.test.ts +15 -15
  229. package/src/tools/tests/createContext.test.ts +14 -14
  230. package/src/tools/tests/deepLoading.test.ts +95 -94
  231. package/src/tools/tests/exportImport.test.ts +61 -41
  232. package/src/tools/tests/groupsAndAccounts.test.ts +333 -116
  233. package/src/tools/tests/inbox.test.ts +22 -17
  234. package/src/tools/tests/interfaces.test.ts +12 -11
  235. package/src/tools/tests/invites.test.ts +6 -4
  236. package/src/tools/tests/load.test.ts +20 -18
  237. package/src/tools/tests/patterns/notifications.test.ts +7 -7
  238. package/src/tools/tests/patterns/quest.test.ts +3 -3
  239. package/src/tools/tests/patterns/requestToJoin.test.ts +22 -22
  240. package/src/tools/tests/request.test.ts +38 -39
  241. package/src/tools/tests/schemaUnion.test.ts +64 -10
  242. package/src/tools/tests/subscribe.test.ts +64 -64
  243. package/src/tools/tests/testing.test.ts +7 -11
  244. package/src/tools/tests/utils.ts +3 -3
  245. package/src/tools/tests/zod.test.ts +3 -3
  246. package/src/worker/index.ts +3 -3
  247. package/tsup.config.ts +9 -0
  248. package/dist/chunk-LZOF6WP5.js.map +0 -1
  249. package/dist/media/chunk-E5J3WLQW.js.map +0 -1
  250. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts.map +0 -1
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  AgentSecret,
3
3
  CoID,
4
- ControlledAccount,
4
+ ControlledAccount as RawControlledAccount,
5
5
  CryptoProvider,
6
6
  Everyone,
7
7
  InviteSecret,
@@ -10,21 +10,20 @@ import {
10
10
  RawAccount,
11
11
  RawCoMap,
12
12
  RawCoValue,
13
- Role,
14
13
  SessionID,
15
14
  cojsonInternals,
16
15
  } from "cojson";
17
16
  import {
18
17
  AnonymousJazzAgent,
18
+ CoFieldInit,
19
19
  type CoMap,
20
20
  type CoValue,
21
21
  CoValueBase,
22
22
  CoValueClass,
23
23
  CoValueClassOrSchema,
24
- CoreAccountSchema,
25
- type Group,
24
+ CoValueJazzApi,
25
+ Group,
26
26
  ID,
27
- InstanceOfSchema,
28
27
  InstanceOrPrimitiveOfSchema,
29
28
  Profile,
30
29
  Ref,
@@ -34,17 +33,19 @@ import {
34
33
  RefsToResolveStrict,
35
34
  RegisteredSchemas,
36
35
  Resolved,
37
- type Schema,
38
36
  SchemaInit,
39
37
  SubscribeListenerOptions,
40
38
  SubscribeRestArgs,
39
+ TypeSym,
41
40
  accessChildByKey,
41
+ accountOrGroupToGroup,
42
42
  activeAccountContext,
43
43
  coValueClassFromCoValueClassOrSchema,
44
44
  coValuesCache,
45
45
  createInboxRoot,
46
46
  ensureCoValueLoaded,
47
47
  inspect,
48
+ instantiateRefEncodedWithInit,
48
49
  loadCoValue,
49
50
  loadCoValueWithoutMe,
50
51
  parseSubscribeRestArgs,
@@ -57,184 +58,112 @@ export type AccountCreationProps = {
57
58
  onboarding?: boolean;
58
59
  };
59
60
 
60
- type AccountMembers<A extends Account> = [
61
- {
62
- id: string | "everyone";
63
- role: Role;
64
- ref: Ref<A>;
65
- account: A;
66
- },
67
- ];
68
-
69
61
  /** @category Identity & Permissions */
70
62
  export class Account extends CoValueBase implements CoValue {
71
- declare id: ID<this>;
72
- declare _type: "Account";
73
- declare _raw: RawAccount;
74
-
75
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
76
- static _schema: any;
77
- get _schema(): {
78
- profile: Schema;
79
- root: Schema;
80
- } {
81
- return (this.constructor as typeof Account)._schema;
82
- }
83
- static {
84
- this._schema = {
85
- profile: {
86
- ref: () => Profile,
87
- optional: false,
88
- } satisfies RefEncoded<Profile>,
89
- root: {
90
- ref: () => RegisteredSchemas["CoMap"],
91
- optional: true,
92
- } satisfies RefEncoded<CoMap>,
93
- };
94
- }
95
-
96
- get _owner(): Account {
97
- return this as Account;
98
- }
99
- get _loadedAs(): Account | AnonymousJazzAgent {
100
- if (this.isLocalNodeOwner) return this;
101
-
102
- const agent = this._raw.core.node.getCurrentAgent();
103
-
104
- if (agent instanceof ControlledAccount) {
105
- return coValuesCache.get(agent.account, () =>
106
- Account.fromRaw(agent.account),
107
- );
108
- }
109
-
110
- return new AnonymousJazzAgent(this._raw.core.node);
111
- }
112
-
113
- declare profile: Profile | null;
114
- declare root: CoMap | null;
115
-
116
- getDescriptor(key: string) {
117
- if (key === "profile") {
118
- return this._schema.profile;
119
- } else if (key === "root") {
120
- return this._schema.root;
121
- }
122
-
123
- return undefined;
124
- }
125
-
126
- get _refs(): {
127
- profile: RefIfCoValue<Profile> | undefined;
128
- root: RefIfCoValue<CoMap> | undefined;
129
- } {
130
- const profileID = this._raw.get("profile") as unknown as
131
- | ID<NonNullable<this["profile"]>>
132
- | undefined;
133
- const rootID = this._raw.get("root") as unknown as
134
- | ID<NonNullable<this["root"]>>
135
- | undefined;
136
-
137
- return {
138
- profile: profileID
139
- ? (new Ref(
140
- profileID,
141
- this._loadedAs,
142
- this._schema.profile as RefEncoded<
143
- NonNullable<this["profile"]> & CoValue
144
- >,
145
- this,
146
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
147
- ) as any as RefIfCoValue<this["profile"]>)
148
- : undefined,
149
- root: rootID
150
- ? (new Ref(
151
- rootID,
152
- this._loadedAs,
153
- this._schema.root as RefEncoded<
154
- NonNullable<this["root"]> & CoValue
155
- >,
156
- this,
157
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
158
- ) as any as RefIfCoValue<this["root"]>)
159
- : undefined,
160
- };
161
- }
63
+ declare [TypeSym]: "Account";
162
64
 
163
65
  /**
164
- * Whether this account is the currently active account.
66
+ * Jazz methods for Accounts are inside this property.
67
+ *
68
+ * This allows Accounts to be used as plain objects while still having
69
+ * access to Jazz methods.
165
70
  */
166
- get isMe() {
167
- return activeAccountContext.get().id === this.id;
168
- }
71
+ declare $jazz: AccountJazzApi<this>;
169
72
 
170
- /**
171
- * Whether this account is the owner of the local node.
172
- */
173
- isLocalNodeOwner: boolean;
174
- sessionID: SessionID | undefined;
73
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
+ static _schema: any = {
75
+ profile: {
76
+ ref: () => Profile,
77
+ optional: false,
78
+ } satisfies RefEncoded<Profile>,
79
+ root: {
80
+ ref: () => RegisteredSchemas["CoMap"],
81
+ optional: true,
82
+ } satisfies RefEncoded<CoMap>,
83
+ };
84
+
85
+ declare readonly profile: Profile | null;
86
+ declare readonly root: CoMap | null;
175
87
 
176
88
  constructor(options: { fromRaw: RawAccount }) {
177
89
  super();
178
90
  if (!("fromRaw" in options)) {
179
91
  throw new Error("Can only construct account from raw or with .create()");
180
92
  }
181
- this.isLocalNodeOwner =
182
- options.fromRaw.id == options.fromRaw.core.node.getCurrentAgent().id;
93
+
94
+ const proxy = new Proxy(
95
+ this,
96
+ AccountAndGroupProxyHandler as ProxyHandler<this>,
97
+ );
183
98
 
184
99
  Object.defineProperties(this, {
185
- id: {
186
- value: options.fromRaw.id,
100
+ [TypeSym]: { value: "Account", enumerable: false },
101
+ $jazz: {
102
+ value: new AccountJazzApi(proxy, options.fromRaw),
187
103
  enumerable: false,
188
104
  },
189
- _raw: { value: options.fromRaw, enumerable: false },
190
- _type: { value: "Account", enumerable: false },
191
105
  });
192
106
 
193
- if (this.isLocalNodeOwner) {
194
- this.sessionID = options.fromRaw.core.node.currentSessionID;
195
- }
107
+ return proxy;
108
+ }
196
109
 
197
- return new Proxy(this, AccountAndGroupProxyHandler as ProxyHandler<this>);
110
+ /**
111
+ * Whether this account is the currently active account.
112
+ */
113
+ get isMe(): boolean {
114
+ return activeAccountContext.get().$jazz.id === this.$jazz.id;
198
115
  }
199
116
 
200
- myRole(): "admin" | undefined {
201
- if (this.isLocalNodeOwner) {
202
- return "admin";
117
+ /**
118
+ * Accept an invite to a `CoValue` or `Group`.
119
+ *
120
+ * @param valueID The ID of the `CoValue` or `Group` to accept the invite to.
121
+ * @param inviteSecret The secret of the invite to accept.
122
+ * @param coValueClass The class of the `CoValue` or `Group` to accept the invite to.
123
+ * @returns The loaded `CoValue` or `Group`.
124
+ */
125
+ async acceptInvite<S extends CoValueClassOrSchema>(
126
+ valueID: string,
127
+ inviteSecret: InviteSecret,
128
+ coValueClass: S,
129
+ ): Promise<Resolved<InstanceOrPrimitiveOfSchema<S>, true> | null> {
130
+ if (!this.$jazz.isLocalNodeOwner) {
131
+ throw new Error("Only a controlled account can accept invites");
203
132
  }
133
+
134
+ await this.$jazz.localNode.acceptInvite(
135
+ valueID as unknown as CoID<RawCoValue>,
136
+ inviteSecret,
137
+ );
138
+
139
+ return loadCoValue(
140
+ coValueClassFromCoValueClassOrSchema(coValueClass),
141
+ valueID,
142
+ {
143
+ loadAs: this,
144
+ },
145
+ ) as Resolved<InstanceOrPrimitiveOfSchema<S>, true> | null;
204
146
  }
205
147
 
206
- getRoleOf(member: Everyone | ID<Account> | "me") {
148
+ getRoleOf(member: Everyone | ID<Account> | "me"): "admin" | undefined {
207
149
  if (member === "me") {
208
150
  return this.isMe ? "admin" : undefined;
209
151
  }
210
152
 
211
- if (member === this.id) {
153
+ if (member === this.$jazz.id) {
212
154
  return "admin";
213
155
  }
214
156
 
215
157
  return undefined;
216
158
  }
217
159
 
218
- getParentGroups(): Array<Group> {
219
- return [];
220
- }
221
-
222
- get members(): AccountMembers<this> {
223
- const ref = new Ref<typeof this>(
224
- this.id,
225
- this._loadedAs,
226
- {
227
- ref: () => this.constructor as AccountClass<typeof this>,
228
- optional: false,
229
- },
230
- this,
231
- );
232
-
233
- return [{ id: this.id, role: "admin", ref, account: this }];
234
- }
235
-
236
- canRead(value: CoValue) {
237
- const role = value._owner.getRoleOf(this.id);
160
+ canRead(value: CoValue): boolean {
161
+ const valueOwner = value.$jazz.owner;
162
+ if (!valueOwner) {
163
+ // Groups and Accounts are public
164
+ return true;
165
+ }
166
+ const role = valueOwner.getRoleOf(this.$jazz.id);
238
167
 
239
168
  return (
240
169
  role === "admin" ||
@@ -244,37 +173,36 @@ export class Account extends CoValueBase implements CoValue {
244
173
  );
245
174
  }
246
175
 
247
- canWrite(value: CoValue) {
248
- const role = value._owner.getRoleOf(this.id);
176
+ canWrite(value: CoValue): boolean {
177
+ const valueOwner = value.$jazz.owner;
178
+ if (!valueOwner) {
179
+ if (value[TypeSym] === "Group") {
180
+ const roleInGroup = (value as Group).getRoleOf(this.$jazz.id);
181
+ return roleInGroup === "admin" || roleInGroup === "writer";
182
+ }
183
+ if (value[TypeSym] === "Account") {
184
+ return value.$jazz.id === this.$jazz.id;
185
+ }
186
+ return false;
187
+ }
188
+ const role = valueOwner.getRoleOf(this.$jazz.id);
249
189
 
250
190
  return role === "admin" || role === "writer" || role === "writeOnly";
251
191
  }
252
192
 
253
- canAdmin(value: CoValue) {
254
- return value._owner.getRoleOf(this.id) === "admin";
255
- }
256
-
257
- async acceptInvite<S extends CoValueClassOrSchema>(
258
- valueID: string,
259
- inviteSecret: InviteSecret,
260
- coValueClass: S,
261
- ): Promise<Resolved<InstanceOrPrimitiveOfSchema<S>, true> | null> {
262
- if (!this.isLocalNodeOwner) {
263
- throw new Error("Only a controlled account can accept invites");
193
+ canAdmin(value: CoValue): boolean {
194
+ const valueOwner = value.$jazz.owner;
195
+ if (!valueOwner) {
196
+ if (value[TypeSym] === "Group") {
197
+ const roleInGroup = (value as Group).getRoleOf(this.$jazz.id);
198
+ return roleInGroup === "admin";
199
+ }
200
+ if (value[TypeSym] === "Account") {
201
+ return value.$jazz.id === this.$jazz.id;
202
+ }
203
+ return false;
264
204
  }
265
-
266
- await this._raw.core.node.acceptInvite(
267
- valueID as unknown as CoID<RawCoValue>,
268
- inviteSecret,
269
- );
270
-
271
- return loadCoValue(
272
- coValueClassFromCoValueClassOrSchema(coValueClass),
273
- valueID,
274
- {
275
- loadAs: this,
276
- },
277
- ) as Resolved<InstanceOrPrimitiveOfSchema<S>, true> | null;
205
+ return valueOwner.getRoleOf(this.$jazz.id) === "admin";
278
206
  }
279
207
 
280
208
  /** @private */
@@ -305,6 +233,9 @@ export class Account extends CoValueBase implements CoValue {
305
233
  return activeAccountContext.get() as A;
306
234
  }
307
235
 
236
+ /**
237
+ * @deprecated Use `co.account(...).createAs` instead.
238
+ */
308
239
  static async createAs<A extends Account>(
309
240
  this: CoValueClass<A> & typeof Account,
310
241
  as: Account,
@@ -319,15 +250,15 @@ export class Account extends CoValueBase implements CoValue {
319
250
  { peer1role: "server", peer2role: "client" },
320
251
  );
321
252
 
322
- as._raw.core.node.syncManager.addPeer(connectedPeers[1]);
253
+ as.$jazz.localNode.syncManager.addPeer(connectedPeers[1]);
323
254
 
324
255
  const account = await this.create<A>({
325
256
  creationProps: options.creationProps,
326
- crypto: as._raw.core.node.crypto,
257
+ crypto: as.$jazz.localNode.crypto,
327
258
  peersToLoadFrom: [connectedPeers[0]],
328
259
  });
329
260
 
330
- await account.waitForAllCoValuesSync();
261
+ await account.$jazz.waitForAllCoValuesSync();
331
262
 
332
263
  return account;
333
264
  }
@@ -343,10 +274,7 @@ export class Account extends CoValueBase implements CoValue {
343
274
 
344
275
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
345
276
  toJSON(): object | any[] {
346
- return {
347
- id: this.id,
348
- _type: this._type,
349
- };
277
+ return {};
350
278
  }
351
279
 
352
280
  [inspect]() {
@@ -360,19 +288,15 @@ export class Account extends CoValueBase implements CoValue {
360
288
  if (this.profile === undefined && creationProps) {
361
289
  const profileGroup = RegisteredSchemas["Group"].create({ owner: this });
362
290
 
363
- this.profile = Profile.create({ name: creationProps.name }, profileGroup);
291
+ this.$jazz.set(
292
+ "profile",
293
+ Profile.create({ name: creationProps.name }, profileGroup) as any,
294
+ );
364
295
  profileGroup.addMember("everyone", "reader");
365
- } else if (this.profile && creationProps) {
366
- if (this.profile._owner._type !== "Group") {
367
- throw new Error("Profile must be owned by a Group", {
368
- cause: `The profile of the account "${this.id}" was created with an Account as owner, which is not allowed.`,
369
- });
370
- }
371
296
  }
372
297
 
373
- const node = this._raw.core.node;
374
- const profile = node
375
- .expectCoValueLoaded(this._raw.get("profile")!)
298
+ const profile = this.$jazz.localNode
299
+ .expectCoValueLoaded(this.$jazz.raw.get("profile")!)
376
300
  .getCurrentContent() as RawCoMap;
377
301
 
378
302
  if (!profile.get("inbox")) {
@@ -387,7 +311,11 @@ export class Account extends CoValueBase implements CoValue {
387
311
  creationProps; // To avoid unused parameter warning
388
312
  }
389
313
 
390
- /** @category Subscription & Loading */
314
+ /**
315
+ * Load an `Account`
316
+ * @category Subscription & Loading
317
+ * @deprecated Use `co.account(...).load` instead.
318
+ */
391
319
  static load<A extends Account, const R extends RefsToResolve<A> = true>(
392
320
  this: CoValueClass<A>,
393
321
  id: ID<A>,
@@ -399,7 +327,11 @@ export class Account extends CoValueBase implements CoValue {
399
327
  return loadCoValueWithoutMe(this, id, options);
400
328
  }
401
329
 
402
- /** @category Subscription & Loading */
330
+ /**
331
+ * Subscribe to an `Account`, when you have an ID but don't have an `Account` instance yet
332
+ * @category Subscription & Loading
333
+ * @deprecated Use `co.account(...).subscribe` instead.
334
+ */
403
335
  static subscribe<A extends Account, const R extends RefsToResolve<A> = true>(
404
336
  this: CoValueClass<A>,
405
337
  id: ID<A>,
@@ -419,31 +351,161 @@ export class Account extends CoValueBase implements CoValue {
419
351
  const { options, listener } = parseSubscribeRestArgs(args);
420
352
  return subscribeToCoValueWithoutMe<A, R>(this, id, options, listener);
421
353
  }
354
+ }
355
+
356
+ class AccountJazzApi<A extends Account> extends CoValueJazzApi<A> {
357
+ /**
358
+ * Whether this account is the owner of the local node.
359
+ *
360
+ * @internal
361
+ */
362
+ isLocalNodeOwner: boolean;
363
+ /** @internal */
364
+ sessionID: SessionID | undefined;
365
+
366
+ constructor(
367
+ private account: A,
368
+ public raw: RawAccount,
369
+ ) {
370
+ super(account);
371
+ this.isLocalNodeOwner = this.raw.id === this.localNode.getCurrentAgent().id;
372
+ if (this.isLocalNodeOwner) {
373
+ this.sessionID = this.localNode.currentSessionID;
374
+ }
375
+ }
376
+
377
+ /**
378
+ * The ID of this `Account`
379
+ * @category Content
380
+ */
381
+ get id(): ID<A> {
382
+ return this.raw.id;
383
+ }
384
+
385
+ /**
386
+ * Accounts have no owner. They can be accessed by everyone.
387
+ */
388
+ get owner(): undefined {
389
+ return undefined;
390
+ }
391
+
392
+ /**
393
+ * Set the value of a key in the account.
394
+ *
395
+ * @param key The key to set.
396
+ * @param value The value to set.
397
+ *
398
+ * @category Content
399
+ */
400
+ set<K extends "root" | "profile">(
401
+ key: K,
402
+ value: CoFieldInit<NonNullable<A[K]>>,
403
+ ) {
404
+ if (value) {
405
+ let refId = (value as unknown as CoValue).$jazz?.id as
406
+ | CoID<RawCoMap>
407
+ | undefined;
408
+ if (!refId) {
409
+ const descriptor = this.schema[key];
410
+ const coValue = instantiateRefEncodedWithInit(
411
+ descriptor,
412
+ value,
413
+ accountOrGroupToGroup(this.account),
414
+ );
415
+ refId = coValue.$jazz.id as CoID<RawCoMap>;
416
+ }
417
+ this.raw.set(key, refId, "trusting");
418
+ }
419
+ }
420
+
421
+ has(key: "root" | "profile"): boolean {
422
+ const entry = this.raw.getRaw(key);
423
+ return entry?.change !== undefined && entry.change.op !== "del";
424
+ }
425
+
426
+ /**
427
+ * Get the descriptor for a given key
428
+ * @internal
429
+ */
430
+ getDescriptor(key: string) {
431
+ if (key === "profile") {
432
+ return this.schema.profile;
433
+ } else if (key === "root") {
434
+ return this.schema.root;
435
+ }
436
+
437
+ return undefined;
438
+ }
439
+
440
+ /**
441
+ * If property `prop` is a `coField.ref(...)`, you can use `account.$jazz.refs.prop` to access
442
+ * the `Ref` instead of the potentially loaded/null value.
443
+ *
444
+ * This allows you to always get the ID or load the value manually.
445
+ *
446
+ * @category Content
447
+ */
448
+ get refs(): {
449
+ profile: RefIfCoValue<Profile> | undefined;
450
+ root: RefIfCoValue<CoMap> | undefined;
451
+ } {
452
+ const profileID = this.raw.get("profile") as unknown as
453
+ | ID<NonNullable<(typeof this.account)["profile"]>>
454
+ | undefined;
455
+ const rootID = this.raw.get("root") as unknown as
456
+ | ID<NonNullable<(typeof this.account)["root"]>>
457
+ | undefined;
458
+
459
+ return {
460
+ profile: profileID
461
+ ? (new Ref(
462
+ profileID,
463
+ this.loadedAs,
464
+ this.schema.profile as RefEncoded<
465
+ NonNullable<(typeof this.account)["profile"]> & CoValue
466
+ >,
467
+ this.account,
468
+ ) as unknown as RefIfCoValue<(typeof this.account)["profile"]>)
469
+ : undefined,
470
+ root: rootID
471
+ ? (new Ref(
472
+ rootID,
473
+ this.loadedAs,
474
+ this.schema.root as RefEncoded<
475
+ NonNullable<(typeof this.account)["root"]> & CoValue
476
+ >,
477
+ this.account,
478
+ ) as unknown as RefIfCoValue<(typeof this.account)["root"]>)
479
+ : undefined,
480
+ };
481
+ }
422
482
 
423
483
  /** @category Subscription & Loading */
424
484
  ensureLoaded<A extends Account, const R extends RefsToResolve<A>>(
425
- this: A,
426
- options: { resolve: RefsToResolveStrict<A, R> },
485
+ this: AccountJazzApi<A>,
486
+ options: {
487
+ resolve: RefsToResolveStrict<A, R>;
488
+ },
427
489
  ): Promise<Resolved<A, R>> {
428
- return ensureCoValueLoaded(this, options);
490
+ return ensureCoValueLoaded(this.account as unknown as A, options);
429
491
  }
430
492
 
431
493
  /** @category Subscription & Loading */
432
494
  subscribe<A extends Account, const R extends RefsToResolve<A>>(
433
- this: A,
495
+ this: AccountJazzApi<A>,
434
496
  listener: (value: Resolved<A, R>, unsubscribe: () => void) => void,
435
497
  ): () => void;
436
498
  subscribe<A extends Account, const R extends RefsToResolve<A>>(
437
- this: A,
499
+ this: AccountJazzApi<A>,
438
500
  options: { resolve?: RefsToResolveStrict<A, R> },
439
501
  listener: (value: Resolved<A, R>, unsubscribe: () => void) => void,
440
502
  ): () => void;
441
503
  subscribe<A extends Account, const R extends RefsToResolve<A>>(
442
- this: A,
504
+ this: AccountJazzApi<A>,
443
505
  ...args: SubscribeRestArgs<A, R>
444
506
  ): () => void {
445
507
  const { options, listener } = parseSubscribeRestArgs(args);
446
- return subscribeToExistingCoValue(this, options, listener);
508
+ return subscribeToExistingCoValue(this.account, options, listener);
447
509
  }
448
510
 
449
511
  /**
@@ -452,7 +514,7 @@ export class Account extends CoValueBase implements CoValue {
452
514
  * @category Subscription & Loading
453
515
  */
454
516
  waitForSync(options?: { timeout?: number }) {
455
- return this._raw.core.waitForSync(options);
517
+ return this.raw.core.waitForSync(options);
456
518
  }
457
519
 
458
520
  /**
@@ -461,16 +523,36 @@ export class Account extends CoValueBase implements CoValue {
461
523
  * @category Subscription & Loading
462
524
  */
463
525
  waitForAllCoValuesSync(options?: { timeout?: number }) {
464
- return this._raw.core.node.syncManager.waitForAllCoValuesSync(
465
- options?.timeout,
466
- );
526
+ return this.localNode.syncManager.waitForAllCoValuesSync(options?.timeout);
527
+ }
528
+
529
+ /** @internal */
530
+ get schema(): {
531
+ profile: RefEncoded<Profile>;
532
+ root: RefEncoded<CoMap>;
533
+ } {
534
+ return (this.account.constructor as typeof Account)._schema;
535
+ }
536
+
537
+ get loadedAs(): Account | AnonymousJazzAgent {
538
+ if (this.isLocalNodeOwner) return this.account;
539
+
540
+ const agent = this.localNode.getCurrentAgent();
541
+
542
+ if (agent instanceof RawControlledAccount) {
543
+ return coValuesCache.get(agent.account, () =>
544
+ Account.fromRaw(agent.account),
545
+ );
546
+ }
547
+
548
+ return new AnonymousJazzAgent(this.localNode);
467
549
  }
468
550
  }
469
551
 
470
552
  export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
471
553
  get(target, key, receiver) {
472
554
  if (key === "profile" || key === "root") {
473
- const id = target._raw.get(key);
555
+ const id = target.$jazz.raw.get(key);
474
556
 
475
557
  if (id) {
476
558
  return accessChildByKey(target, id, key);
@@ -483,31 +565,22 @@ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
483
565
  },
484
566
  set(target, key, value, receiver) {
485
567
  if (
568
+ target instanceof Account &&
486
569
  (key === "profile" || key === "root") &&
487
570
  typeof value === "object" &&
488
571
  SchemaInit in value
489
572
  ) {
490
- (target.constructor as typeof CoMap)._schema ||= {};
491
- (target.constructor as typeof CoMap)._schema[key] = value[SchemaInit];
573
+ (target.constructor as typeof Account)._schema ||= {};
574
+ (target.constructor as typeof Account)._schema[key] = value[SchemaInit];
492
575
  return true;
493
- } else if (key === "profile") {
576
+ } else if (
577
+ target instanceof Account &&
578
+ (key === "profile" || key === "root")
579
+ ) {
494
580
  if (value) {
495
- target._raw.set(
496
- "profile",
497
- value.id as unknown as CoID<RawCoMap>,
498
- "trusting",
499
- );
581
+ target.$jazz.set(key, value);
500
582
  }
501
583
 
502
- return true;
503
- } else if (key === "root") {
504
- if (value) {
505
- target._raw.set(
506
- "root",
507
- value.id as unknown as CoID<RawCoMap>,
508
- "trusting",
509
- );
510
- }
511
584
  return true;
512
585
  } else {
513
586
  return Reflect.set(target, key, value, receiver);
@@ -519,8 +592,8 @@ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
519
592
  typeof descriptor.value === "object" &&
520
593
  SchemaInit in descriptor.value
521
594
  ) {
522
- (target.constructor as typeof CoMap)._schema ||= {};
523
- (target.constructor as typeof CoMap)._schema[key] =
595
+ (target.constructor as typeof Account)._schema ||= {};
596
+ (target.constructor as typeof Account)._schema[key] =
524
597
  descriptor.value[SchemaInit];
525
598
  return true;
526
599
  } else {
@@ -529,13 +602,19 @@ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
529
602
  },
530
603
  };
531
604
 
605
+ export type ControlledAccount = Account & {
606
+ $jazz: {
607
+ raw: RawAccount;
608
+ isLocalNodeOwner: true;
609
+ sessionID: SessionID;
610
+ };
611
+ };
612
+
532
613
  /** @category Identity & Permissions */
533
- export function isControlledAccount(account: Account): account is Account & {
534
- isLocalNodeOwner: true;
535
- sessionID: SessionID;
536
- _raw: RawAccount;
537
- } {
538
- return account.isLocalNodeOwner;
614
+ export function isControlledAccount(
615
+ account: Account,
616
+ ): account is ControlledAccount {
617
+ return account.$jazz.isLocalNodeOwner;
539
618
  }
540
619
 
541
620
  export type AccountClass<Acc extends Account> = CoValueClass<Acc> & {