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