jazz-tools 0.9.9 → 0.9.10
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.
- package/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +8 -0
- package/dist/{chunk-VQZOWIPU.js → chunk-ICWP2U63.js} +78 -115
- package/dist/chunk-ICWP2U63.js.map +1 -0
- package/dist/index.native.js +1 -1
- package/dist/index.web.js +1 -1
- package/dist/testing.js +1 -1
- package/package.json +2 -2
- package/src/coValues/coMap.ts +0 -2
- package/src/coValues/coPlainText.ts +0 -31
- package/src/coValues/coRichText.ts +1 -0
- package/src/implementation/createContext.ts +86 -109
- package/src/tests/groupsAndAccounts.test.ts +3 -3
- package/tsconfig.native.json +1 -1
- package/tsconfig.web.json +1 -1
- package/dist/chunk-VQZOWIPU.js.map +0 -1
package/dist/index.native.js
CHANGED
package/dist/index.web.js
CHANGED
package/dist/testing.js
CHANGED
package/package.json
CHANGED
package/src/coValues/coMap.ts
CHANGED
@@ -426,7 +426,6 @@ export class CoMap extends CoValueBase implements CoValue {
|
|
426
426
|
* ```ts
|
427
427
|
* const person = await Person.load(
|
428
428
|
* "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax",
|
429
|
-
* me,
|
430
429
|
* { pet: {} }
|
431
430
|
* );
|
432
431
|
* ```
|
@@ -473,7 +472,6 @@ export class CoMap extends CoValueBase implements CoValue {
|
|
473
472
|
* ```ts
|
474
473
|
* const unsub = Person.subscribe(
|
475
474
|
* "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax",
|
476
|
-
* me,
|
477
475
|
* { pet: {} },
|
478
476
|
* (person) => console.log(person)
|
479
477
|
* );
|
@@ -117,22 +117,6 @@ export class CoPlainText extends String implements CoValue {
|
|
117
117
|
/**
|
118
118
|
* Load a `CoPlainText` with a given ID, as a given account.
|
119
119
|
*
|
120
|
-
* `depth` specifies which (if any) fields that reference other CoValues to load as well before resolving.
|
121
|
-
* The `DeeplyLoaded` return type guarantees that corresponding referenced CoValues are loaded to the specified depth.
|
122
|
-
*
|
123
|
-
* You can pass `[]` or `{}` for shallowly loading only this CoPlainText, or `{ fieldA: depthA, fieldB: depthB }` for recursively loading referenced CoValues.
|
124
|
-
*
|
125
|
-
* Check out the `load` methods on `CoMap`/`CoList`/`CoStream`/`Group`/`Account` to see which depth structures are valid to nest.
|
126
|
-
*
|
127
|
-
* @example
|
128
|
-
* ```ts
|
129
|
-
* const person = await Person.load(
|
130
|
-
* "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax",
|
131
|
-
* me,
|
132
|
-
* { pet: {} }
|
133
|
-
* );
|
134
|
-
* ```
|
135
|
-
*
|
136
120
|
* @category Subscription & Loading
|
137
121
|
*/
|
138
122
|
static load<T extends CoPlainText>(
|
@@ -162,27 +146,12 @@ export class CoPlainText extends String implements CoValue {
|
|
162
146
|
*
|
163
147
|
* Automatically also subscribes to updates to all referenced/nested CoValues as soon as they are accessed in the listener.
|
164
148
|
*
|
165
|
-
* `depth` specifies which (if any) fields that reference other CoValues to load as well before calling `listener` for the first time.
|
166
|
-
* The `DeeplyLoaded` return type guarantees that corresponding referenced CoValues are loaded to the specified depth.
|
167
|
-
*
|
168
|
-
* You can pass `[]` or `{}` for shallowly loading only this CoMap, or `{ fieldA: depthA, fieldB: depthB }` for recursively loading referenced CoValues.
|
169
|
-
*
|
170
149
|
* Check out the `load` methods on `CoMap`/`CoList`/`CoStream`/`Group`/`Account` to see which depth structures are valid to nest.
|
171
150
|
*
|
172
151
|
* Returns an unsubscribe function that you should call when you no longer need updates.
|
173
152
|
*
|
174
153
|
* Also see the `useCoState` hook to reactively subscribe to a CoValue in a React component.
|
175
154
|
*
|
176
|
-
* @example
|
177
|
-
* ```ts
|
178
|
-
* const unsub = Person.subscribe(
|
179
|
-
* "co_zdsMhHtfG6VNKt7RqPUPvUtN2Ax",
|
180
|
-
* me,
|
181
|
-
* { pet: {} },
|
182
|
-
* (person) => console.log(person)
|
183
|
-
* );
|
184
|
-
* ```
|
185
|
-
*
|
186
155
|
* @category Subscription & Loading
|
187
156
|
*/
|
188
157
|
static subscribe<T extends CoPlainText>(
|
@@ -128,129 +128,106 @@ export async function createJazzContext<Acc extends Account>(
|
|
128
128
|
): Promise<JazzContext<Acc>>;
|
129
129
|
export async function createJazzContext<Acc extends Account>(
|
130
130
|
options: ContextParamsWithAuth<Acc> | BaseContextParams,
|
131
|
-
): Promise<JazzContext<Acc
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
});
|
139
|
-
}
|
131
|
+
): Promise<JazzContext<Acc> | JazzContextWithAgent> {
|
132
|
+
if (!("auth" in options)) {
|
133
|
+
return createAnonymousJazzContext({
|
134
|
+
peersToLoadFrom: options.peersToLoadFrom,
|
135
|
+
crypto: options.crypto,
|
136
|
+
});
|
137
|
+
}
|
140
138
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
try {
|
147
|
-
authResult = await auth.start(crypto);
|
148
|
-
} catch (e) {
|
149
|
-
console.error("error", e);
|
150
|
-
throw e;
|
151
|
-
}
|
139
|
+
const { auth, sessionProvider, peersToLoadFrom, crypto } = options;
|
140
|
+
const AccountSchema =
|
141
|
+
options.AccountSchema ??
|
142
|
+
(RegisteredSchemas["Account"] as unknown as AccountClass<Acc>);
|
143
|
+
const authResult = await auth.start(crypto);
|
152
144
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
);
|
145
|
+
if (authResult.type === "existing") {
|
146
|
+
const { sessionID, sessionDone } = await sessionProvider(
|
147
|
+
authResult.credentials.accountID,
|
148
|
+
crypto,
|
149
|
+
);
|
159
150
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
}) as Acc;
|
151
|
+
const node = await LocalNode.withLoadedAccount({
|
152
|
+
accountID: authResult.credentials
|
153
|
+
.accountID as unknown as CoID<RawAccount>,
|
154
|
+
accountSecret: authResult.credentials.secret,
|
155
|
+
sessionID: sessionID,
|
156
|
+
peersToLoadFrom: peersToLoadFrom,
|
157
|
+
crypto: crypto,
|
158
|
+
migration: async (rawAccount, _node, creationProps) => {
|
159
|
+
const account = new AccountSchema({
|
160
|
+
fromRaw: rawAccount,
|
161
|
+
}) as Acc;
|
172
162
|
|
173
|
-
|
163
|
+
activeAccountContext.set(account);
|
174
164
|
|
175
|
-
|
176
|
-
|
177
|
-
|
165
|
+
await account.applyMigration(creationProps);
|
166
|
+
},
|
167
|
+
});
|
178
168
|
|
179
|
-
|
180
|
-
|
169
|
+
const account = AccountSchema.fromNode(node);
|
170
|
+
activeAccountContext.set(account);
|
181
171
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
172
|
+
if (authResult.saveCredentials) {
|
173
|
+
await authResult.saveCredentials({
|
174
|
+
accountID: node.account.id as unknown as ID<Account>,
|
175
|
+
secret: node.account.agentSecret,
|
176
|
+
});
|
177
|
+
}
|
188
178
|
|
189
|
-
|
179
|
+
authResult.onSuccess();
|
190
180
|
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
// TODO: figure out a way to not "waste" the first SessionID
|
215
|
-
const { node } = await LocalNode.withNewlyCreatedAccount({
|
216
|
-
creationProps: authResult.creationProps,
|
217
|
-
peersToLoadFrom: peersToLoadFrom,
|
218
|
-
crypto: crypto,
|
219
|
-
initialAgentSecret: authResult.initialSecret,
|
220
|
-
migration: async (rawAccount, _node, creationProps) => {
|
221
|
-
const account = new AccountSchema({
|
222
|
-
fromRaw: rawAccount,
|
223
|
-
}) as Acc;
|
224
|
-
activeAccountContext.set(account);
|
181
|
+
return {
|
182
|
+
account,
|
183
|
+
done: () => {
|
184
|
+
node.gracefulShutdown();
|
185
|
+
sessionDone();
|
186
|
+
},
|
187
|
+
logOut: () => {
|
188
|
+
node.gracefulShutdown();
|
189
|
+
sessionDone();
|
190
|
+
authResult.logOut();
|
191
|
+
},
|
192
|
+
};
|
193
|
+
} else if (authResult.type === "new") {
|
194
|
+
const { node } = await LocalNode.withNewlyCreatedAccount({
|
195
|
+
creationProps: authResult.creationProps,
|
196
|
+
peersToLoadFrom: peersToLoadFrom,
|
197
|
+
crypto: crypto,
|
198
|
+
initialAgentSecret: authResult.initialSecret,
|
199
|
+
migration: async (rawAccount, _node, creationProps) => {
|
200
|
+
const account = new AccountSchema({
|
201
|
+
fromRaw: rawAccount,
|
202
|
+
}) as Acc;
|
203
|
+
activeAccountContext.set(account);
|
225
204
|
|
226
|
-
|
227
|
-
|
228
|
-
|
205
|
+
await account.applyMigration(creationProps);
|
206
|
+
},
|
207
|
+
});
|
229
208
|
|
230
|
-
|
231
|
-
|
209
|
+
const account = AccountSchema.fromNode(node);
|
210
|
+
activeAccountContext.set(account);
|
232
211
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
212
|
+
await authResult.saveCredentials({
|
213
|
+
accountID: node.account.id as unknown as ID<Account>,
|
214
|
+
secret: node.account.agentSecret,
|
215
|
+
});
|
237
216
|
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
} catch (e) {
|
250
|
-
authResult.onError(new Error("Error creating account", { cause: e }));
|
251
|
-
}
|
252
|
-
}
|
217
|
+
authResult.onSuccess();
|
218
|
+
return {
|
219
|
+
account,
|
220
|
+
done: () => {
|
221
|
+
node.gracefulShutdown();
|
222
|
+
},
|
223
|
+
logOut: () => {
|
224
|
+
node.gracefulShutdown();
|
225
|
+
authResult.logOut();
|
226
|
+
},
|
227
|
+
};
|
253
228
|
}
|
229
|
+
|
230
|
+
throw new Error("Invalid auth result");
|
254
231
|
}
|
255
232
|
|
256
233
|
export async function createAnonymousJazzContext({
|
@@ -1,12 +1,12 @@
|
|
1
|
-
import {
|
1
|
+
import { WasmCrypto } from "cojson";
|
2
2
|
import { describe, expect, test } from "vitest";
|
3
|
-
import { Account, CoMap, Group,
|
3
|
+
import { Account, CoMap, Group, Profile, co } from "../exports.js";
|
4
4
|
import { setupTwoNodes } from "./utils.js";
|
5
5
|
|
6
6
|
const Crypto = await WasmCrypto.create();
|
7
7
|
|
8
8
|
describe("Custom accounts and groups", async () => {
|
9
|
-
class CustomProfile extends
|
9
|
+
class CustomProfile extends Profile {
|
10
10
|
name = co.string;
|
11
11
|
color = co.string;
|
12
12
|
}
|
package/tsconfig.native.json
CHANGED
package/tsconfig.web.json
CHANGED