jazz-tools 0.8.15 → 0.8.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. package/CHANGELOG.md +276 -269
  2. package/dist/native/coValues/account.js +3 -6
  3. package/dist/native/coValues/account.js.map +1 -1
  4. package/dist/native/coValues/coList.js +3 -7
  5. package/dist/native/coValues/coList.js.map +1 -1
  6. package/dist/native/coValues/coMap.js +8 -14
  7. package/dist/native/coValues/coMap.js.map +1 -1
  8. package/dist/native/coValues/coStream.js +7 -12
  9. package/dist/native/coValues/coStream.js.map +1 -1
  10. package/dist/native/coValues/deepLoading.js +6 -3
  11. package/dist/native/coValues/deepLoading.js.map +1 -1
  12. package/dist/native/coValues/extensions/imageDef.js.map +1 -1
  13. package/dist/native/coValues/group.js +3 -6
  14. package/dist/native/coValues/group.js.map +1 -1
  15. package/dist/native/coValues/interfaces.js +4 -3
  16. package/dist/native/coValues/interfaces.js.map +1 -1
  17. package/dist/native/exports.js +3 -9
  18. package/dist/native/exports.js.map +1 -1
  19. package/dist/native/implementation/createContext.js +1 -2
  20. package/dist/native/implementation/createContext.js.map +1 -1
  21. package/dist/native/implementation/devtoolsFormatters.js +5 -25
  22. package/dist/native/implementation/devtoolsFormatters.js.map +1 -1
  23. package/dist/native/implementation/refs.js +1 -2
  24. package/dist/native/implementation/refs.js.map +1 -1
  25. package/dist/native/implementation/schema.js +1 -1
  26. package/dist/native/implementation/schema.js.map +1 -1
  27. package/dist/native/implementation/subscriptionScope.js +2 -4
  28. package/dist/native/implementation/subscriptionScope.js.map +1 -1
  29. package/dist/native/index.native.js +1 -1
  30. package/dist/native/index.native.js.map +1 -1
  31. package/dist/native/lib/cache.js.map +1 -1
  32. package/dist/native/lib/cache.test.js +1 -1
  33. package/dist/native/lib/cache.test.js.map +1 -1
  34. package/dist/web/coValues/account.js +3 -6
  35. package/dist/web/coValues/account.js.map +1 -1
  36. package/dist/web/coValues/coList.js +3 -7
  37. package/dist/web/coValues/coList.js.map +1 -1
  38. package/dist/web/coValues/coMap.js +8 -14
  39. package/dist/web/coValues/coMap.js.map +1 -1
  40. package/dist/web/coValues/coStream.js +7 -12
  41. package/dist/web/coValues/coStream.js.map +1 -1
  42. package/dist/web/coValues/deepLoading.js +6 -3
  43. package/dist/web/coValues/deepLoading.js.map +1 -1
  44. package/dist/web/coValues/extensions/imageDef.js.map +1 -1
  45. package/dist/web/coValues/group.js +3 -6
  46. package/dist/web/coValues/group.js.map +1 -1
  47. package/dist/web/coValues/interfaces.js +4 -3
  48. package/dist/web/coValues/interfaces.js.map +1 -1
  49. package/dist/web/exports.js +3 -9
  50. package/dist/web/exports.js.map +1 -1
  51. package/dist/web/implementation/createContext.js +1 -2
  52. package/dist/web/implementation/createContext.js.map +1 -1
  53. package/dist/web/implementation/devtoolsFormatters.js +5 -25
  54. package/dist/web/implementation/devtoolsFormatters.js.map +1 -1
  55. package/dist/web/implementation/refs.js +1 -2
  56. package/dist/web/implementation/refs.js.map +1 -1
  57. package/dist/web/implementation/schema.js +1 -1
  58. package/dist/web/implementation/schema.js.map +1 -1
  59. package/dist/web/implementation/subscriptionScope.js +2 -4
  60. package/dist/web/implementation/subscriptionScope.js.map +1 -1
  61. package/dist/web/lib/cache.js.map +1 -1
  62. package/dist/web/lib/cache.test.js +1 -1
  63. package/dist/web/lib/cache.test.js.map +1 -1
  64. package/package.json +5 -9
  65. package/src/coValues/account.ts +330 -339
  66. package/src/coValues/coList.ts +474 -495
  67. package/src/coValues/coMap.ts +584 -604
  68. package/src/coValues/coStream.ts +624 -650
  69. package/src/coValues/deepLoading.ts +184 -200
  70. package/src/coValues/extensions/imageDef.ts +44 -44
  71. package/src/coValues/group.ts +196 -210
  72. package/src/coValues/interfaces.ts +197 -199
  73. package/src/exports.ts +38 -26
  74. package/src/implementation/createContext.ts +206 -213
  75. package/src/implementation/devtoolsFormatters.ts +80 -100
  76. package/src/implementation/refs.ts +127 -139
  77. package/src/implementation/schema.ts +124 -128
  78. package/src/implementation/subscriptionScope.ts +111 -121
  79. package/src/index.native.ts +3 -3
  80. package/src/lib/cache.test.ts +48 -48
  81. package/src/lib/cache.ts +9 -9
  82. package/src/tests/coList.test.ts +264 -283
  83. package/src/tests/coMap.test.ts +741 -761
  84. package/src/tests/coStream.test.ts +405 -438
  85. package/src/tests/deepLoading.test.ts +251 -256
  86. package/src/tests/groupsAndAccounts.test.ts +70 -74
  87. package/src/tests/schema.test.ts +198 -198
  88. package/src/tests/subscribe.test.ts +312 -299
  89. package/tsconfig.json +2 -4
  90. package/tsconfig.native.json +4 -10
  91. package/tsconfig.web.json +4 -10
  92. package/.eslintrc.cjs +0 -24
  93. package/.prettierrc.js +0 -9
@@ -1,283 +1,276 @@
1
1
  import {
2
- AgentSecret,
3
- CoID,
4
- ControlledAgent,
5
- CryptoProvider,
6
- LocalNode,
7
- Peer,
8
- RawAccount,
9
- RawAccountID,
10
- SessionID,
2
+ AgentSecret,
3
+ CoID,
4
+ ControlledAgent,
5
+ CryptoProvider,
6
+ LocalNode,
7
+ Peer,
8
+ RawAccount,
9
+ RawAccountID,
10
+ SessionID,
11
11
  } from "cojson";
12
12
  import { Account, AccountClass, ID } from "../internal.js";
13
13
 
14
14
  export type Credentials = {
15
- accountID: ID<Account>;
16
- secret: AgentSecret;
15
+ accountID: ID<Account>;
16
+ secret: AgentSecret;
17
17
  };
18
18
 
19
19
  export type AuthResult =
20
- | {
21
- type: "existing";
22
- credentials: Credentials;
23
- saveCredentials?: (credentials: Credentials) => Promise<void>;
24
- onSuccess: () => void;
25
- onError: (error: string | Error) => void;
26
- logOut: () => void;
27
- }
28
- | {
29
- type: "new";
30
- creationProps: { name: string };
31
- initialSecret?: AgentSecret;
32
- saveCredentials: (credentials: Credentials) => Promise<void>;
33
- onSuccess: () => void;
34
- onError: (error: string | Error) => void;
35
- logOut: () => void;
36
- };
20
+ | {
21
+ type: "existing";
22
+ credentials: Credentials;
23
+ saveCredentials?: (credentials: Credentials) => Promise<void>;
24
+ onSuccess: () => void;
25
+ onError: (error: string | Error) => void;
26
+ logOut: () => void;
27
+ }
28
+ | {
29
+ type: "new";
30
+ creationProps: { name: string };
31
+ initialSecret?: AgentSecret;
32
+ saveCredentials: (credentials: Credentials) => Promise<void>;
33
+ onSuccess: () => void;
34
+ onError: (error: string | Error) => void;
35
+ logOut: () => void;
36
+ };
37
37
 
38
38
  export interface AuthMethod {
39
- start(crypto: CryptoProvider): Promise<AuthResult>;
39
+ start(crypto: CryptoProvider): Promise<AuthResult>;
40
40
  }
41
41
 
42
42
  export const fixedCredentialsAuth = (credentials: {
43
- accountID: ID<Account>;
44
- secret: AgentSecret;
43
+ accountID: ID<Account>;
44
+ secret: AgentSecret;
45
45
  }): AuthMethod => {
46
- return {
47
- start: async () => ({
48
- type: "existing",
49
- credentials,
50
- saveCredentials: async () => {},
51
- onSuccess: () => {},
52
- onError: () => {},
53
- logOut: () => {},
54
- }),
55
- };
46
+ return {
47
+ start: async () => ({
48
+ type: "existing",
49
+ credentials,
50
+ saveCredentials: async () => {},
51
+ onSuccess: () => {},
52
+ onError: () => {},
53
+ logOut: () => {},
54
+ }),
55
+ };
56
56
  };
57
57
 
58
58
  export const ephemeralCredentialsAuth = (): AuthMethod => {
59
- return {
60
- start: async () => ({
61
- type: "new",
62
- creationProps: { name: "Ephemeral" },
63
- saveCredentials: async () => {},
64
- onSuccess: () => {},
65
- onError: () => {},
66
- logOut: () => {},
67
- }),
68
- };
59
+ return {
60
+ start: async () => ({
61
+ type: "new",
62
+ creationProps: { name: "Ephemeral" },
63
+ saveCredentials: async () => {},
64
+ onSuccess: () => {},
65
+ onError: () => {},
66
+ logOut: () => {},
67
+ }),
68
+ };
69
69
  };
70
70
 
71
71
  export async function randomSessionProvider(
72
- accountID: ID<Account>,
73
- crypto: CryptoProvider,
72
+ accountID: ID<Account>,
73
+ crypto: CryptoProvider,
74
74
  ) {
75
- return {
76
- sessionID: crypto.newRandomSessionID(
77
- accountID as unknown as RawAccountID,
78
- ),
79
- sessionDone: () => {},
80
- };
75
+ return {
76
+ sessionID: crypto.newRandomSessionID(accountID as unknown as RawAccountID),
77
+ sessionDone: () => {},
78
+ };
81
79
  }
82
80
 
83
81
  type ContextParamsWithAuth<Acc extends Account> = {
84
- AccountSchema?: AccountClass<Acc>;
85
- auth: AuthMethod;
86
- sessionProvider: (
87
- accountID: ID<Account>,
88
- crypto: CryptoProvider,
89
- ) => Promise<{ sessionID: SessionID; sessionDone: () => void }>;
82
+ AccountSchema?: AccountClass<Acc>;
83
+ auth: AuthMethod;
84
+ sessionProvider: (
85
+ accountID: ID<Account>,
86
+ crypto: CryptoProvider,
87
+ ) => Promise<{ sessionID: SessionID; sessionDone: () => void }>;
90
88
  } & BaseContextParams;
91
89
 
92
90
  type BaseContextParams = {
93
- peersToLoadFrom: Peer[];
94
- crypto: CryptoProvider;
91
+ peersToLoadFrom: Peer[];
92
+ crypto: CryptoProvider;
95
93
  };
96
94
 
97
95
  export type JazzContextWithAccount<Acc extends Account> = {
98
- account: Acc;
99
- done: () => void;
100
- logOut: () => void;
96
+ account: Acc;
97
+ done: () => void;
98
+ logOut: () => void;
101
99
  };
102
100
 
103
101
  export type JazzContextWithAgent = {
104
- agent: AnonymousJazzAgent;
105
- done: () => void;
106
- logOut: () => void;
102
+ agent: AnonymousJazzAgent;
103
+ done: () => void;
104
+ logOut: () => void;
107
105
  };
108
106
 
109
107
  export type JazzContext<Acc extends Account> =
110
- | JazzContextWithAccount<Acc>
111
- | JazzContextWithAgent;
108
+ | JazzContextWithAccount<Acc>
109
+ | JazzContextWithAgent;
112
110
 
113
111
  export async function createJazzContext<Acc extends Account>({
114
- AccountSchema,
115
- auth,
116
- sessionProvider,
117
- peersToLoadFrom,
118
- crypto,
112
+ AccountSchema,
113
+ auth,
114
+ sessionProvider,
115
+ peersToLoadFrom,
116
+ crypto,
119
117
  }: ContextParamsWithAuth<Acc>): Promise<JazzContextWithAccount<Acc>>;
120
118
  export async function createJazzContext({
121
- peersToLoadFrom,
122
- crypto,
119
+ peersToLoadFrom,
120
+ crypto,
123
121
  }: BaseContextParams): Promise<JazzContextWithAgent>;
124
122
  export async function createJazzContext<Acc extends Account>(
125
- options: ContextParamsWithAuth<Acc> | BaseContextParams,
123
+ options: ContextParamsWithAuth<Acc> | BaseContextParams,
126
124
  ): Promise<JazzContext<Acc>>;
127
125
  export async function createJazzContext<Acc extends Account>(
128
- options: ContextParamsWithAuth<Acc> | BaseContextParams,
126
+ options: ContextParamsWithAuth<Acc> | BaseContextParams,
129
127
  ): Promise<JazzContext<Acc>> {
130
- // eslint-disable-next-line no-constant-condition
131
- while (true) {
132
- if (!("auth" in options)) {
133
- return createAnonymousJazzContext({
134
- peersToLoadFrom: options.peersToLoadFrom,
135
- crypto: options.crypto,
136
- });
137
- }
128
+ // eslint-disable-next-line no-constant-condition
129
+ while (true) {
130
+ if (!("auth" in options)) {
131
+ return createAnonymousJazzContext({
132
+ peersToLoadFrom: options.peersToLoadFrom,
133
+ crypto: options.crypto,
134
+ });
135
+ }
138
136
 
139
- const { auth, sessionProvider, peersToLoadFrom, crypto } = options;
140
- const AccountSchema =
141
- options.AccountSchema ?? (Account as unknown as AccountClass<Acc>);
142
- let authResult: AuthResult;
143
- try {
144
- authResult = await auth.start(crypto);
145
- } catch (e) {
146
- console.error("error", e);
147
- throw e;
148
- }
137
+ const { auth, sessionProvider, peersToLoadFrom, crypto } = options;
138
+ const AccountSchema =
139
+ options.AccountSchema ?? (Account as unknown as AccountClass<Acc>);
140
+ let authResult: AuthResult;
141
+ try {
142
+ authResult = await auth.start(crypto);
143
+ } catch (e) {
144
+ console.error("error", e);
145
+ throw e;
146
+ }
149
147
 
150
- if (authResult.type === "existing") {
151
- try {
152
- const { sessionID, sessionDone } = await sessionProvider(
153
- authResult.credentials.accountID,
154
- crypto,
155
- );
148
+ if (authResult.type === "existing") {
149
+ try {
150
+ const { sessionID, sessionDone } = await sessionProvider(
151
+ authResult.credentials.accountID,
152
+ crypto,
153
+ );
156
154
 
157
- try {
158
- const node = await LocalNode.withLoadedAccount({
159
- accountID: authResult.credentials
160
- .accountID as unknown as CoID<RawAccount>,
161
- accountSecret: authResult.credentials.secret,
162
- sessionID: sessionID,
163
- peersToLoadFrom: peersToLoadFrom,
164
- crypto: crypto,
165
- migration: async (rawAccount, _node, creationProps) => {
166
- const account = new AccountSchema({
167
- fromRaw: rawAccount,
168
- }) as Acc;
155
+ try {
156
+ const node = await LocalNode.withLoadedAccount({
157
+ accountID: authResult.credentials
158
+ .accountID as unknown as CoID<RawAccount>,
159
+ accountSecret: authResult.credentials.secret,
160
+ sessionID: sessionID,
161
+ peersToLoadFrom: peersToLoadFrom,
162
+ crypto: crypto,
163
+ migration: async (rawAccount, _node, creationProps) => {
164
+ const account = new AccountSchema({
165
+ fromRaw: rawAccount,
166
+ }) as Acc;
169
167
 
170
- await account.migrate?.(creationProps);
171
- },
172
- });
168
+ await account.migrate?.(creationProps);
169
+ },
170
+ });
173
171
 
174
- const account = AccountSchema.fromNode(node);
172
+ const account = AccountSchema.fromNode(node);
175
173
 
176
- if (authResult.saveCredentials) {
177
- await authResult.saveCredentials({
178
- accountID: node.account
179
- .id as unknown as ID<Account>,
180
- secret: node.account.agentSecret,
181
- });
182
- }
174
+ if (authResult.saveCredentials) {
175
+ await authResult.saveCredentials({
176
+ accountID: node.account.id as unknown as ID<Account>,
177
+ secret: node.account.agentSecret,
178
+ });
179
+ }
183
180
 
184
- authResult.onSuccess();
181
+ authResult.onSuccess();
185
182
 
186
- return {
187
- account,
188
- done: () => {
189
- node.gracefulShutdown();
190
- sessionDone();
191
- },
192
- logOut: () => {
193
- node.gracefulShutdown();
194
- sessionDone();
195
- authResult.logOut();
196
- },
197
- };
198
- } catch (e) {
199
- authResult.onError(
200
- new Error("Error loading account", { cause: e }),
201
- );
202
- sessionDone();
203
- }
204
- } catch (e) {
205
- authResult.onError(
206
- new Error("Error acquiring sessionID", { cause: e }),
207
- );
208
- }
209
- } else if (authResult.type === "new") {
210
- try {
211
- // TODO: figure out a way to not "waste" the first SessionID
212
- const { node } = await LocalNode.withNewlyCreatedAccount({
213
- creationProps: authResult.creationProps,
214
- peersToLoadFrom: peersToLoadFrom,
215
- crypto: crypto,
216
- initialAgentSecret: authResult.initialSecret,
217
- migration: async (rawAccount, _node, creationProps) => {
218
- const account = new AccountSchema({
219
- fromRaw: rawAccount,
220
- }) as Acc;
183
+ return {
184
+ account,
185
+ done: () => {
186
+ node.gracefulShutdown();
187
+ sessionDone();
188
+ },
189
+ logOut: () => {
190
+ node.gracefulShutdown();
191
+ sessionDone();
192
+ authResult.logOut();
193
+ },
194
+ };
195
+ } catch (e) {
196
+ authResult.onError(new Error("Error loading account", { cause: e }));
197
+ sessionDone();
198
+ }
199
+ } catch (e) {
200
+ authResult.onError(
201
+ new Error("Error acquiring sessionID", { cause: e }),
202
+ );
203
+ }
204
+ } else if (authResult.type === "new") {
205
+ try {
206
+ // TODO: figure out a way to not "waste" the first SessionID
207
+ const { node } = await LocalNode.withNewlyCreatedAccount({
208
+ creationProps: authResult.creationProps,
209
+ peersToLoadFrom: peersToLoadFrom,
210
+ crypto: crypto,
211
+ initialAgentSecret: authResult.initialSecret,
212
+ migration: async (rawAccount, _node, creationProps) => {
213
+ const account = new AccountSchema({
214
+ fromRaw: rawAccount,
215
+ }) as Acc;
221
216
 
222
- await account.migrate?.(creationProps);
223
- },
224
- });
217
+ await account.migrate?.(creationProps);
218
+ },
219
+ });
225
220
 
226
- const account = AccountSchema.fromNode(node);
221
+ const account = AccountSchema.fromNode(node);
227
222
 
228
- await authResult.saveCredentials({
229
- accountID: node.account.id as unknown as ID<Account>,
230
- secret: node.account.agentSecret,
231
- });
223
+ await authResult.saveCredentials({
224
+ accountID: node.account.id as unknown as ID<Account>,
225
+ secret: node.account.agentSecret,
226
+ });
232
227
 
233
- authResult.onSuccess();
234
- return {
235
- account,
236
- done: () => {
237
- node.gracefulShutdown();
238
- },
239
- logOut: () => {
240
- node.gracefulShutdown();
241
- authResult.logOut();
242
- },
243
- };
244
- } catch (e) {
245
- authResult.onError(
246
- new Error("Error creating account", { cause: e }),
247
- );
248
- }
249
- }
228
+ authResult.onSuccess();
229
+ return {
230
+ account,
231
+ done: () => {
232
+ node.gracefulShutdown();
233
+ },
234
+ logOut: () => {
235
+ node.gracefulShutdown();
236
+ authResult.logOut();
237
+ },
238
+ };
239
+ } catch (e) {
240
+ authResult.onError(new Error("Error creating account", { cause: e }));
241
+ }
250
242
  }
243
+ }
251
244
  }
252
245
 
253
246
  export class AnonymousJazzAgent {
254
- _type = "Anonymous" as const;
255
- constructor(public node: LocalNode) {}
247
+ _type = "Anonymous" as const;
248
+ constructor(public node: LocalNode) {}
256
249
  }
257
250
 
258
251
  export async function createAnonymousJazzContext({
259
- peersToLoadFrom,
260
- crypto,
252
+ peersToLoadFrom,
253
+ crypto,
261
254
  }: {
262
- peersToLoadFrom: Peer[];
263
- crypto: CryptoProvider;
255
+ peersToLoadFrom: Peer[];
256
+ crypto: CryptoProvider;
264
257
  }): Promise<JazzContextWithAgent> {
265
- const agentSecret = crypto.newRandomAgentSecret();
266
- const rawAgent = new ControlledAgent(agentSecret, crypto);
258
+ const agentSecret = crypto.newRandomAgentSecret();
259
+ const rawAgent = new ControlledAgent(agentSecret, crypto);
267
260
 
268
- const node = new LocalNode(
269
- rawAgent,
270
- crypto.newRandomSessionID(rawAgent.id),
271
- crypto,
272
- );
261
+ const node = new LocalNode(
262
+ rawAgent,
263
+ crypto.newRandomSessionID(rawAgent.id),
264
+ crypto,
265
+ );
273
266
 
274
- for (const peer of peersToLoadFrom) {
275
- node.syncManager.addPeer(peer);
276
- }
267
+ for (const peer of peersToLoadFrom) {
268
+ node.syncManager.addPeer(peer);
269
+ }
277
270
 
278
- return {
279
- agent: new AnonymousJazzAgent(node),
280
- done: () => {},
281
- logOut: () => {},
282
- };
271
+ return {
272
+ agent: new AnonymousJazzAgent(node),
273
+ done: () => {},
274
+ logOut: () => {},
275
+ };
283
276
  }