jazz-tools 0.17.14 → 0.18.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 (228) 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 +50 -50
  10. package/CHANGELOG.md +31 -0
  11. package/dist/browser/index.js +2 -2
  12. package/dist/browser/index.js.map +1 -1
  13. package/dist/{chunk-LZOF6WP5.js → chunk-HJ3GTGY7.js} +1320 -1018
  14. package/dist/chunk-HJ3GTGY7.js.map +1 -0
  15. package/dist/index.js +18 -18
  16. package/dist/index.js.map +1 -1
  17. package/dist/inspector/{custom-element-ZSNTCECD.js → custom-element-WCY6D3QJ.js} +3 -3
  18. package/dist/inspector/{custom-element-ZSNTCECD.js.map → custom-element-WCY6D3QJ.js.map} +1 -1
  19. package/dist/inspector/index.js +1 -1
  20. package/dist/inspector/index.js.map +1 -1
  21. package/dist/inspector/register-custom-element.js +1 -1
  22. package/dist/media/{chunk-E5J3WLQW.js → chunk-KR2V6X2N.js} +14 -9
  23. package/dist/media/chunk-KR2V6X2N.js.map +1 -0
  24. package/dist/media/create-image.d.ts +6 -6
  25. package/dist/media/index.browser.d.ts +6 -6
  26. package/dist/media/index.browser.js +1 -1
  27. package/dist/media/index.d.ts +1 -1
  28. package/dist/media/index.js +1 -1
  29. package/dist/media/index.native.d.ts +6 -6
  30. package/dist/media/index.native.js +1 -1
  31. package/dist/media/utils.d.ts.map +1 -1
  32. package/dist/prosemirror/index.js +2 -2
  33. package/dist/prosemirror/index.js.map +1 -1
  34. package/dist/react/index.js +7 -5
  35. package/dist/react/index.js.map +1 -1
  36. package/dist/react-core/hooks.d.ts.map +1 -1
  37. package/dist/react-core/index.js +4658 -23
  38. package/dist/react-core/index.js.map +1 -1
  39. package/dist/react-native-core/index.js +1 -1
  40. package/dist/react-native-core/index.js.map +1 -1
  41. package/dist/svelte/jazz.class.svelte.js +1 -1
  42. package/dist/svelte/media/image.svelte +3 -9
  43. package/dist/svelte/media/image.svelte.d.ts +1 -6
  44. package/dist/svelte/media/image.svelte.d.ts.map +1 -1
  45. package/dist/svelte/media/image.types.d.ts +7 -0
  46. package/dist/svelte/media/image.types.d.ts.map +1 -0
  47. package/dist/svelte/media/image.types.js +1 -0
  48. package/dist/svelte/tests/media/image.svelte.test.js +31 -31
  49. package/dist/testing.js +18 -14
  50. package/dist/testing.js.map +1 -1
  51. package/dist/tools/coValues/CoFieldInit.d.ts +13 -0
  52. package/dist/tools/coValues/CoFieldInit.d.ts.map +1 -0
  53. package/dist/tools/coValues/CoValueBase.d.ts +18 -15
  54. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  55. package/dist/tools/coValues/account.d.ts +100 -46
  56. package/dist/tools/coValues/account.d.ts.map +1 -1
  57. package/dist/tools/coValues/coFeed.d.ts +78 -62
  58. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  59. package/dist/tools/coValues/coList.d.ts +212 -99
  60. package/dist/tools/coValues/coList.d.ts.map +1 -1
  61. package/dist/tools/coValues/coMap.d.ts +200 -192
  62. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  63. package/dist/tools/coValues/coPlainText.d.ts +30 -22
  64. package/dist/tools/coValues/coPlainText.d.ts.map +1 -1
  65. package/dist/tools/coValues/deepLoading.d.ts +13 -13
  66. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  67. package/dist/tools/coValues/extensions/imageDef.d.ts +1 -1
  68. package/dist/tools/coValues/group.d.ts +32 -32
  69. package/dist/tools/coValues/group.d.ts.map +1 -1
  70. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  71. package/dist/tools/coValues/interfaces.d.ts +18 -17
  72. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  73. package/dist/tools/coValues/profile.d.ts +6 -5
  74. package/dist/tools/coValues/profile.d.ts.map +1 -1
  75. package/dist/tools/coValues/schemaUnion.d.ts +3 -3
  76. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  77. package/dist/tools/exports.d.ts +1 -1
  78. package/dist/tools/exports.d.ts.map +1 -1
  79. package/dist/tools/implementation/anonymousJazzAgent.d.ts +2 -1
  80. package/dist/tools/implementation/anonymousJazzAgent.d.ts.map +1 -1
  81. package/dist/tools/implementation/schema.d.ts +5 -5
  82. package/dist/tools/implementation/schema.d.ts.map +1 -1
  83. package/dist/tools/implementation/symbols.d.ts +2 -0
  84. package/dist/tools/implementation/symbols.d.ts.map +1 -1
  85. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -2
  86. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  87. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +6 -2
  88. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  89. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +8 -3
  90. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  91. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +12 -7
  92. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  93. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +13 -7
  94. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  95. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +11 -2
  96. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  97. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +4 -0
  98. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  99. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +4 -0
  100. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  101. package/dist/tools/implementation/zodSchema/typeConverters/{CoFieldInit.d.ts → CoFieldSchemaInit.d.ts} +7 -7
  102. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldSchemaInit.d.ts.map +1 -0
  103. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +4 -4
  104. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  105. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +4 -4
  106. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  107. package/dist/tools/implementation/zodSchema/zodCo.d.ts +2 -2
  108. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  109. package/dist/tools/internal.d.ts +2 -1
  110. package/dist/tools/internal.d.ts.map +1 -1
  111. package/dist/tools/lib/migration.d.ts +1 -1
  112. package/dist/tools/lib/migration.d.ts.map +1 -1
  113. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  114. package/dist/tools/subscribe/index.d.ts +1 -1
  115. package/dist/tools/subscribe/index.d.ts.map +1 -1
  116. package/dist/tools/subscribe/utils.d.ts +2 -2
  117. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  118. package/dist/tools/testing.d.ts.map +1 -1
  119. package/dist/tools/tests/utils.d.ts +2 -6
  120. package/dist/tools/tests/utils.d.ts.map +1 -1
  121. package/dist/worker/index.js +3 -3
  122. package/dist/worker/index.js.map +1 -1
  123. package/package.json +4 -4
  124. package/src/browser/auth/PasskeyAuth.ts +2 -2
  125. package/src/browser/createBrowserContext.ts +2 -2
  126. package/src/browser/tests/PasskeyAuth.test.ts +2 -2
  127. package/src/inspector/custom-element.tsx +2 -2
  128. package/src/inspector/viewer/new-app.tsx +1 -1
  129. package/src/media/create-image.test.ts +7 -7
  130. package/src/media/create-image.ts +5 -3
  131. package/src/media/index.ts +1 -1
  132. package/src/media/utils.test.ts +72 -66
  133. package/src/media/utils.ts +9 -6
  134. package/src/prosemirror/lib/plugin.ts +1 -1
  135. package/src/prosemirror/lib/sync.ts +1 -1
  136. package/src/prosemirror/tests/plugin.test.ts +4 -4
  137. package/src/react/media/image.tsx +2 -2
  138. package/src/react/tests/media/image.test.tsx +52 -32
  139. package/src/react-core/hooks.ts +11 -5
  140. package/src/react-core/tests/useAccount.test.ts +16 -22
  141. package/src/react-core/tests/useCoState.test.ts +19 -19
  142. package/src/react-core/tests/useInboxSender.test.ts +5 -2
  143. package/src/react-core/tests/usePassPhraseAuth.test.ts +6 -6
  144. package/src/react-native-core/media/image.tsx +1 -1
  145. package/src/svelte/jazz.class.svelte.ts +1 -1
  146. package/src/svelte/media/image.svelte +3 -9
  147. package/src/svelte/media/image.types.ts +7 -0
  148. package/src/svelte/tests/media/image.svelte.test.ts +34 -32
  149. package/src/tools/auth/DemoAuth.ts +2 -2
  150. package/src/tools/auth/PassphraseAuth.ts +2 -2
  151. package/src/tools/auth/clerk/index.ts +2 -2
  152. package/src/tools/auth/clerk/tests/JazzClerkAuth.test.ts +1 -1
  153. package/src/tools/coValues/CoFieldInit.ts +20 -0
  154. package/src/tools/coValues/CoValueBase.ts +40 -60
  155. package/src/tools/coValues/account.ts +306 -232
  156. package/src/tools/coValues/coFeed.ts +185 -153
  157. package/src/tools/coValues/coList.ts +507 -334
  158. package/src/tools/coValues/coMap.ts +420 -286
  159. package/src/tools/coValues/coPlainText.ts +94 -110
  160. package/src/tools/coValues/deepLoading.ts +13 -13
  161. package/src/tools/coValues/group.ts +100 -114
  162. package/src/tools/coValues/inbox.ts +16 -14
  163. package/src/tools/coValues/interfaces.ts +49 -31
  164. package/src/tools/coValues/profile.ts +8 -6
  165. package/src/tools/coValues/request.ts +9 -9
  166. package/src/tools/coValues/schemaUnion.ts +11 -5
  167. package/src/tools/exports.ts +1 -1
  168. package/src/tools/implementation/ContextManager.ts +4 -4
  169. package/src/tools/implementation/anonymousJazzAgent.ts +2 -1
  170. package/src/tools/implementation/createContext.ts +1 -1
  171. package/src/tools/implementation/devtoolsFormatters.ts +9 -9
  172. package/src/tools/implementation/invites.ts +2 -2
  173. package/src/tools/implementation/schema.ts +7 -7
  174. package/src/tools/implementation/symbols.ts +3 -0
  175. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +2 -2
  176. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +11 -2
  177. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +18 -7
  178. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +17 -7
  179. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +20 -11
  180. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +19 -2
  181. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +6 -0
  182. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +6 -0
  183. package/src/tools/implementation/zodSchema/typeConverters/{CoFieldInit.ts → CoFieldSchemaInit.ts} +11 -11
  184. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +4 -4
  185. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +4 -4
  186. package/src/tools/implementation/zodSchema/zodCo.ts +2 -2
  187. package/src/tools/internal.ts +2 -1
  188. package/src/tools/lib/migration.ts +5 -5
  189. package/src/tools/subscribe/SubscriptionScope.ts +32 -24
  190. package/src/tools/subscribe/index.ts +4 -4
  191. package/src/tools/subscribe/utils.ts +11 -11
  192. package/src/tools/testing.ts +17 -13
  193. package/src/tools/tests/ContextManager.test.ts +68 -57
  194. package/src/tools/tests/PassphraseAuth.test.ts +2 -2
  195. package/src/tools/tests/account.test.ts +139 -69
  196. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +12 -6
  197. package/src/tools/tests/coDiscriminatedUnion.test.ts +26 -17
  198. package/src/tools/tests/coFeed.test-d.ts +18 -17
  199. package/src/tools/tests/coFeed.test.ts +108 -97
  200. package/src/tools/tests/coList.test-d.ts +18 -23
  201. package/src/tools/tests/coList.test.ts +350 -165
  202. package/src/tools/tests/coMap.record.test-d.ts +9 -13
  203. package/src/tools/tests/coMap.record.test.ts +37 -23
  204. package/src/tools/tests/coMap.test-d.ts +43 -21
  205. package/src/tools/tests/coMap.test.ts +360 -182
  206. package/src/tools/tests/coOptional.test.ts +28 -13
  207. package/src/tools/tests/coPlainText.test.ts +15 -15
  208. package/src/tools/tests/createContext.test.ts +14 -14
  209. package/src/tools/tests/deepLoading.test.ts +95 -94
  210. package/src/tools/tests/exportImport.test.ts +61 -41
  211. package/src/tools/tests/groupsAndAccounts.test.ts +333 -116
  212. package/src/tools/tests/inbox.test.ts +22 -17
  213. package/src/tools/tests/interfaces.test.ts +12 -11
  214. package/src/tools/tests/invites.test.ts +6 -4
  215. package/src/tools/tests/load.test.ts +20 -18
  216. package/src/tools/tests/patterns/notifications.test.ts +6 -6
  217. package/src/tools/tests/patterns/quest.test.ts +3 -3
  218. package/src/tools/tests/patterns/requestToJoin.test.ts +22 -22
  219. package/src/tools/tests/request.test.ts +38 -39
  220. package/src/tools/tests/schemaUnion.test.ts +64 -10
  221. package/src/tools/tests/subscribe.test.ts +64 -64
  222. package/src/tools/tests/testing.test.ts +5 -9
  223. package/src/tools/tests/utils.ts +3 -3
  224. package/src/tools/tests/zod.test.ts +3 -3
  225. package/src/worker/index.ts +3 -3
  226. package/dist/chunk-LZOF6WP5.js.map +0 -1
  227. package/dist/media/chunk-E5J3WLQW.js.map +0 -1
  228. 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,156 @@ 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
+ /**
422
+ * Get the descriptor for a given key
423
+ * @internal
424
+ */
425
+ getDescriptor(key: string) {
426
+ if (key === "profile") {
427
+ return this.schema.profile;
428
+ } else if (key === "root") {
429
+ return this.schema.root;
430
+ }
431
+
432
+ return undefined;
433
+ }
434
+
435
+ /**
436
+ * If property `prop` is a `coField.ref(...)`, you can use `account.$jazz.refs.prop` to access
437
+ * the `Ref` instead of the potentially loaded/null value.
438
+ *
439
+ * This allows you to always get the ID or load the value manually.
440
+ *
441
+ * @category Content
442
+ */
443
+ get refs(): {
444
+ profile: RefIfCoValue<Profile> | undefined;
445
+ root: RefIfCoValue<CoMap> | undefined;
446
+ } {
447
+ const profileID = this.raw.get("profile") as unknown as
448
+ | ID<NonNullable<(typeof this.account)["profile"]>>
449
+ | undefined;
450
+ const rootID = this.raw.get("root") as unknown as
451
+ | ID<NonNullable<(typeof this.account)["root"]>>
452
+ | undefined;
453
+
454
+ return {
455
+ profile: profileID
456
+ ? (new Ref(
457
+ profileID,
458
+ this.loadedAs,
459
+ this.schema.profile as RefEncoded<
460
+ NonNullable<(typeof this.account)["profile"]> & CoValue
461
+ >,
462
+ this.account,
463
+ ) as unknown as RefIfCoValue<(typeof this.account)["profile"]>)
464
+ : undefined,
465
+ root: rootID
466
+ ? (new Ref(
467
+ rootID,
468
+ this.loadedAs,
469
+ this.schema.root as RefEncoded<
470
+ NonNullable<(typeof this.account)["root"]> & CoValue
471
+ >,
472
+ this.account,
473
+ ) as unknown as RefIfCoValue<(typeof this.account)["root"]>)
474
+ : undefined,
475
+ };
476
+ }
422
477
 
423
478
  /** @category Subscription & Loading */
424
479
  ensureLoaded<A extends Account, const R extends RefsToResolve<A>>(
425
- this: A,
426
- options: { resolve: RefsToResolveStrict<A, R> },
480
+ this: AccountJazzApi<A>,
481
+ options: {
482
+ resolve: RefsToResolveStrict<A, R>;
483
+ },
427
484
  ): Promise<Resolved<A, R>> {
428
- return ensureCoValueLoaded(this, options);
485
+ return ensureCoValueLoaded(this.account as unknown as A, options);
429
486
  }
430
487
 
431
488
  /** @category Subscription & Loading */
432
489
  subscribe<A extends Account, const R extends RefsToResolve<A>>(
433
- this: A,
490
+ this: AccountJazzApi<A>,
434
491
  listener: (value: Resolved<A, R>, unsubscribe: () => void) => void,
435
492
  ): () => void;
436
493
  subscribe<A extends Account, const R extends RefsToResolve<A>>(
437
- this: A,
494
+ this: AccountJazzApi<A>,
438
495
  options: { resolve?: RefsToResolveStrict<A, R> },
439
496
  listener: (value: Resolved<A, R>, unsubscribe: () => void) => void,
440
497
  ): () => void;
441
498
  subscribe<A extends Account, const R extends RefsToResolve<A>>(
442
- this: A,
499
+ this: AccountJazzApi<A>,
443
500
  ...args: SubscribeRestArgs<A, R>
444
501
  ): () => void {
445
502
  const { options, listener } = parseSubscribeRestArgs(args);
446
- return subscribeToExistingCoValue(this, options, listener);
503
+ return subscribeToExistingCoValue(this.account, options, listener);
447
504
  }
448
505
 
449
506
  /**
@@ -452,7 +509,7 @@ export class Account extends CoValueBase implements CoValue {
452
509
  * @category Subscription & Loading
453
510
  */
454
511
  waitForSync(options?: { timeout?: number }) {
455
- return this._raw.core.waitForSync(options);
512
+ return this.raw.core.waitForSync(options);
456
513
  }
457
514
 
458
515
  /**
@@ -461,16 +518,36 @@ export class Account extends CoValueBase implements CoValue {
461
518
  * @category Subscription & Loading
462
519
  */
463
520
  waitForAllCoValuesSync(options?: { timeout?: number }) {
464
- return this._raw.core.node.syncManager.waitForAllCoValuesSync(
465
- options?.timeout,
466
- );
521
+ return this.localNode.syncManager.waitForAllCoValuesSync(options?.timeout);
522
+ }
523
+
524
+ /** @internal */
525
+ get schema(): {
526
+ profile: RefEncoded<Profile>;
527
+ root: RefEncoded<CoMap>;
528
+ } {
529
+ return (this.account.constructor as typeof Account)._schema;
530
+ }
531
+
532
+ get loadedAs(): Account | AnonymousJazzAgent {
533
+ if (this.isLocalNodeOwner) return this.account;
534
+
535
+ const agent = this.localNode.getCurrentAgent();
536
+
537
+ if (agent instanceof RawControlledAccount) {
538
+ return coValuesCache.get(agent.account, () =>
539
+ Account.fromRaw(agent.account),
540
+ );
541
+ }
542
+
543
+ return new AnonymousJazzAgent(this.localNode);
467
544
  }
468
545
  }
469
546
 
470
547
  export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
471
548
  get(target, key, receiver) {
472
549
  if (key === "profile" || key === "root") {
473
- const id = target._raw.get(key);
550
+ const id = target.$jazz.raw.get(key);
474
551
 
475
552
  if (id) {
476
553
  return accessChildByKey(target, id, key);
@@ -483,31 +560,22 @@ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
483
560
  },
484
561
  set(target, key, value, receiver) {
485
562
  if (
563
+ target instanceof Account &&
486
564
  (key === "profile" || key === "root") &&
487
565
  typeof value === "object" &&
488
566
  SchemaInit in value
489
567
  ) {
490
- (target.constructor as typeof CoMap)._schema ||= {};
491
- (target.constructor as typeof CoMap)._schema[key] = value[SchemaInit];
568
+ (target.constructor as typeof Account)._schema ||= {};
569
+ (target.constructor as typeof Account)._schema[key] = value[SchemaInit];
492
570
  return true;
493
- } else if (key === "profile") {
571
+ } else if (
572
+ target instanceof Account &&
573
+ (key === "profile" || key === "root")
574
+ ) {
494
575
  if (value) {
495
- target._raw.set(
496
- "profile",
497
- value.id as unknown as CoID<RawCoMap>,
498
- "trusting",
499
- );
576
+ target.$jazz.set(key, value);
500
577
  }
501
578
 
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
579
  return true;
512
580
  } else {
513
581
  return Reflect.set(target, key, value, receiver);
@@ -519,8 +587,8 @@ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
519
587
  typeof descriptor.value === "object" &&
520
588
  SchemaInit in descriptor.value
521
589
  ) {
522
- (target.constructor as typeof CoMap)._schema ||= {};
523
- (target.constructor as typeof CoMap)._schema[key] =
590
+ (target.constructor as typeof Account)._schema ||= {};
591
+ (target.constructor as typeof Account)._schema[key] =
524
592
  descriptor.value[SchemaInit];
525
593
  return true;
526
594
  } else {
@@ -529,13 +597,19 @@ export const AccountAndGroupProxyHandler: ProxyHandler<Account | Group> = {
529
597
  },
530
598
  };
531
599
 
600
+ export type ControlledAccount = Account & {
601
+ $jazz: {
602
+ raw: RawAccount;
603
+ isLocalNodeOwner: true;
604
+ sessionID: SessionID;
605
+ };
606
+ };
607
+
532
608
  /** @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;
609
+ export function isControlledAccount(
610
+ account: Account,
611
+ ): account is ControlledAccount {
612
+ return account.$jazz.isLocalNodeOwner;
539
613
  }
540
614
 
541
615
  export type AccountClass<Acc extends Account> = CoValueClass<Acc> & {