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.
@@ -26,7 +26,7 @@ import {
26
26
  loadCoValue,
27
27
  randomSessionProvider,
28
28
  subscribeToCoValue
29
- } from "./chunk-VQZOWIPU.js";
29
+ } from "./chunk-ICWP2U63.js";
30
30
 
31
31
  // src/index.native.ts
32
32
  import {
package/dist/index.web.js CHANGED
@@ -26,7 +26,7 @@ import {
26
26
  loadCoValue,
27
27
  randomSessionProvider,
28
28
  subscribeToCoValue
29
- } from "./chunk-VQZOWIPU.js";
29
+ } from "./chunk-ICWP2U63.js";
30
30
 
31
31
  // src/index.web.ts
32
32
  import { cojsonInternals, MAX_RECOMMENDED_TX_SIZE, WasmCrypto } from "cojson";
package/dist/testing.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  Account,
3
3
  activeAccountContext,
4
4
  createAnonymousJazzContext
5
- } from "./chunk-VQZOWIPU.js";
5
+ } from "./chunk-ICWP2U63.js";
6
6
 
7
7
  // src/testing.ts
8
8
  import { cojsonInternals } from "cojson";
package/package.json CHANGED
@@ -23,9 +23,9 @@
23
23
  },
24
24
  "type": "module",
25
25
  "license": "MIT",
26
- "version": "0.9.9",
26
+ "version": "0.9.10",
27
27
  "dependencies": {
28
- "cojson": "0.9.9"
28
+ "cojson": "0.9.10"
29
29
  },
30
30
  "devDependencies": {
31
31
  "tsup": "8.3.5",
@@ -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>(
@@ -372,6 +372,7 @@ export class CoRichText extends CoMap {
372
372
  end + 1,
373
373
  mark.endBefore,
374
374
  RangeClass,
375
+ // @ts-ignore Some Typescript versions flag this as an error
375
376
  {},
376
377
  {
377
378
  markOwner: mark.sourceMark._owner || this._owner,
@@ -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
- // eslint-disable-next-line no-constant-condition
133
- while (true) {
134
- if (!("auth" in options)) {
135
- return createAnonymousJazzContext({
136
- peersToLoadFrom: options.peersToLoadFrom,
137
- crypto: options.crypto,
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
- const { auth, sessionProvider, peersToLoadFrom, crypto } = options;
142
- const AccountSchema =
143
- options.AccountSchema ??
144
- (RegisteredSchemas["Account"] as unknown as AccountClass<Acc>);
145
- let authResult: AuthResult;
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
- if (authResult.type === "existing") {
154
- try {
155
- const { sessionID, sessionDone } = await sessionProvider(
156
- authResult.credentials.accountID,
157
- crypto,
158
- );
145
+ if (authResult.type === "existing") {
146
+ const { sessionID, sessionDone } = await sessionProvider(
147
+ authResult.credentials.accountID,
148
+ crypto,
149
+ );
159
150
 
160
- try {
161
- const node = await LocalNode.withLoadedAccount({
162
- accountID: authResult.credentials
163
- .accountID as unknown as CoID<RawAccount>,
164
- accountSecret: authResult.credentials.secret,
165
- sessionID: sessionID,
166
- peersToLoadFrom: peersToLoadFrom,
167
- crypto: crypto,
168
- migration: async (rawAccount, _node, creationProps) => {
169
- const account = new AccountSchema({
170
- fromRaw: rawAccount,
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
- activeAccountContext.set(account);
163
+ activeAccountContext.set(account);
174
164
 
175
- await account.applyMigration(creationProps);
176
- },
177
- });
165
+ await account.applyMigration(creationProps);
166
+ },
167
+ });
178
168
 
179
- const account = AccountSchema.fromNode(node);
180
- activeAccountContext.set(account);
169
+ const account = AccountSchema.fromNode(node);
170
+ activeAccountContext.set(account);
181
171
 
182
- if (authResult.saveCredentials) {
183
- await authResult.saveCredentials({
184
- accountID: node.account.id as unknown as ID<Account>,
185
- secret: node.account.agentSecret,
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
- authResult.onSuccess();
179
+ authResult.onSuccess();
190
180
 
191
- return {
192
- account,
193
- done: () => {
194
- node.gracefulShutdown();
195
- sessionDone();
196
- },
197
- logOut: () => {
198
- node.gracefulShutdown();
199
- sessionDone();
200
- authResult.logOut();
201
- },
202
- };
203
- } catch (e) {
204
- authResult.onError(new Error("Error loading account", { cause: e }));
205
- sessionDone();
206
- }
207
- } catch (e) {
208
- authResult.onError(
209
- new Error("Error acquiring sessionID", { cause: e }),
210
- );
211
- }
212
- } else if (authResult.type === "new") {
213
- try {
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
- await account.applyMigration(creationProps);
227
- },
228
- });
205
+ await account.applyMigration(creationProps);
206
+ },
207
+ });
229
208
 
230
- const account = AccountSchema.fromNode(node);
231
- activeAccountContext.set(account);
209
+ const account = AccountSchema.fromNode(node);
210
+ activeAccountContext.set(account);
232
211
 
233
- await authResult.saveCredentials({
234
- accountID: node.account.id as unknown as ID<Account>,
235
- secret: node.account.agentSecret,
236
- });
212
+ await authResult.saveCredentials({
213
+ accountID: node.account.id as unknown as ID<Account>,
214
+ secret: node.account.agentSecret,
215
+ });
237
216
 
238
- authResult.onSuccess();
239
- return {
240
- account,
241
- done: () => {
242
- node.gracefulShutdown();
243
- },
244
- logOut: () => {
245
- node.gracefulShutdown();
246
- authResult.logOut();
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 { RawGroup } from "cojson";
1
+ import { WasmCrypto } from "cojson";
2
2
  import { describe, expect, test } from "vitest";
3
- import { Account, CoMap, Group, WasmCrypto, co } from "../index.web.js";
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 CoMap {
9
+ class CustomProfile extends Profile {
10
10
  name = co.string;
11
11
  color = co.string;
12
12
  }
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "extends": "./tsconfig.json",
3
3
  "include": ["./src/**/*.ts"],
4
- "exclude": ["./node_modules", "./src/tests", "./src/index.web.ts"]
4
+ "exclude": ["./node_modules", "./src/tests", "./src/index.native.ts"]
5
5
  }
package/tsconfig.web.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "extends": "./tsconfig.json",
3
3
  "include": ["./src/**/*.ts"],
4
- "exclude": ["./node_modules", "./src/tests", "./src/index.native.ts"]
4
+ "exclude": ["./node_modules", "./src/tests", "./src/index.web.ts"]
5
5
  }