jazz-tools 0.10.4 → 0.10.6

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/dist/index.js CHANGED
@@ -35,7 +35,7 @@ import {
35
35
  parseInviteLink,
36
36
  randomSessionProvider,
37
37
  subscribeToCoValue
38
- } from "./chunk-XRX3CCYY.js";
38
+ } from "./chunk-DPW23T6J.js";
39
39
 
40
40
  // src/index.ts
41
41
  import { MAX_RECOMMENDED_TX_SIZE, cojsonInternals } from "cojson";
package/dist/testing.js CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  createAnonymousJazzContext,
6
6
  createJazzContext,
7
7
  randomSessionProvider
8
- } from "./chunk-XRX3CCYY.js";
8
+ } from "./chunk-DPW23T6J.js";
9
9
 
10
10
  // src/testing.ts
11
11
  import { LocalNode } from "cojson";
package/package.json CHANGED
@@ -17,10 +17,10 @@
17
17
  },
18
18
  "type": "module",
19
19
  "license": "MIT",
20
- "version": "0.10.4",
20
+ "version": "0.10.6",
21
21
  "dependencies": {
22
22
  "@scure/bip39": "^1.3.0",
23
- "cojson": "0.10.4"
23
+ "cojson": "0.10.6"
24
24
  },
25
25
  "devDependencies": {
26
26
  "tsup": "8.3.5",
@@ -28,16 +28,50 @@ export class AuthSecretStorage {
28
28
  if (!(await kvStore.get(STORAGE_KEY))) {
29
29
  const demoAuthSecret = await kvStore.get("demo-auth-logged-in-secret");
30
30
  if (demoAuthSecret) {
31
- await kvStore.set(STORAGE_KEY, demoAuthSecret);
31
+ const parsed = JSON.parse(demoAuthSecret);
32
+ await kvStore.set(
33
+ STORAGE_KEY,
34
+ JSON.stringify({
35
+ accountID: parsed.accountID,
36
+ accountSecret: parsed.accountSecret,
37
+ provider: "demo",
38
+ }),
39
+ );
32
40
  await kvStore.delete("demo-auth-logged-in-secret");
33
41
  }
34
42
 
35
43
  const clerkAuthSecret = await kvStore.get("jazz-clerk-auth");
36
44
  if (clerkAuthSecret) {
37
- await kvStore.set(STORAGE_KEY, clerkAuthSecret);
45
+ const parsed = JSON.parse(clerkAuthSecret);
46
+ await kvStore.set(
47
+ STORAGE_KEY,
48
+ JSON.stringify({
49
+ accountID: parsed.accountID,
50
+ accountSecret: parsed.secret,
51
+ provider: "clerk",
52
+ }),
53
+ );
38
54
  await kvStore.delete("jazz-clerk-auth");
39
55
  }
40
56
  }
57
+
58
+ const value = await kvStore.get(STORAGE_KEY);
59
+
60
+ if (value) {
61
+ const parsed = JSON.parse(value);
62
+
63
+ if ("secret" in parsed) {
64
+ await kvStore.set(
65
+ STORAGE_KEY,
66
+ JSON.stringify({
67
+ accountID: parsed.accountID,
68
+ secretSeed: parsed.secretSeed,
69
+ accountSecret: parsed.secret,
70
+ provider: parsed.provider,
71
+ }),
72
+ );
73
+ }
74
+ }
41
75
  }
42
76
 
43
77
  async get(): Promise<AuthCredentials | null> {
@@ -110,14 +110,19 @@ export async function createJazzContextFromExistingCredentials<
110
110
  sessionID: sessionID,
111
111
  peersToLoadFrom: peersToLoadFrom,
112
112
  crypto: crypto,
113
+ migration: async (rawAccount, _node, creationProps) => {
114
+ const account = new CurrentAccountSchema({
115
+ fromRaw: rawAccount,
116
+ }) as Acc;
117
+ activeAccountContext.set(account);
118
+
119
+ await account.applyMigration(creationProps);
120
+ },
113
121
  });
114
122
 
115
123
  const account = CurrentAccountSchema.fromNode(node);
116
124
  activeAccountContext.set(account);
117
125
 
118
- // Running the migration outside of withLoadedAccount for better error management
119
- await account.applyMigration();
120
-
121
126
  return {
122
127
  node,
123
128
  account,
@@ -28,20 +28,52 @@ describe("AuthSecretStorage", () => {
28
28
 
29
29
  await authSecretStorage.migrate();
30
30
 
31
- expect(await kvStore.get("jazz-logged-in-secret")).toBe(demoSecret);
31
+ expect(await kvStore.get("jazz-logged-in-secret")).toBe(
32
+ JSON.stringify({
33
+ accountID: "demo123",
34
+ accountSecret: "secret123",
35
+ provider: "demo",
36
+ }),
37
+ );
32
38
  expect(await kvStore.get("demo-auth-logged-in-secret")).toBeNull();
33
39
  });
34
40
 
35
41
  it("should migrate clerk auth secret", async () => {
36
42
  const clerkSecret = JSON.stringify({
37
43
  accountID: "clerk123",
38
- accountSecret: "secret123",
44
+ secret: "secret123",
39
45
  });
40
46
  await kvStore.set("jazz-clerk-auth", clerkSecret);
41
47
 
42
48
  await authSecretStorage.migrate();
43
49
 
44
- expect(await kvStore.get("jazz-logged-in-secret")).toBe(clerkSecret);
50
+ expect(await kvStore.get("jazz-logged-in-secret")).toBe(
51
+ JSON.stringify({
52
+ accountID: "clerk123",
53
+ accountSecret: "secret123",
54
+ provider: "clerk",
55
+ }),
56
+ );
57
+ expect(await kvStore.get("jazz-clerk-auth")).toBeNull();
58
+ });
59
+
60
+ it("should migrate auth wrong secret key to accountSecret", async () => {
61
+ const clerkSecret = JSON.stringify({
62
+ accountID: "clerk123",
63
+ secret: "secret123",
64
+ provider: "clerk",
65
+ });
66
+ await kvStore.set("jazz-logged-in-secret", clerkSecret);
67
+
68
+ await authSecretStorage.migrate();
69
+
70
+ expect(await kvStore.get("jazz-logged-in-secret")).toBe(
71
+ JSON.stringify({
72
+ accountID: "clerk123",
73
+ accountSecret: "secret123",
74
+ provider: "clerk",
75
+ }),
76
+ );
45
77
  expect(await kvStore.get("jazz-clerk-auth")).toBeNull();
46
78
  });
47
79
  });
@@ -196,6 +196,87 @@ describe("ContextManager", () => {
196
196
  expect(onAnonymousAccountDiscarded).not.toHaveBeenCalled();
197
197
  });
198
198
 
199
+ test("the migration should be applied correctly on existing accounts ", async () => {
200
+ class AccountRoot extends CoMap {
201
+ value = co.string;
202
+ }
203
+
204
+ let lastRootId: string | undefined;
205
+
206
+ class CustomAccount extends Account {
207
+ root = co.ref(AccountRoot);
208
+
209
+ migrate() {
210
+ this.root = AccountRoot.create({
211
+ value: "Hello",
212
+ });
213
+ lastRootId = this.root.id;
214
+ }
215
+ }
216
+ const customManager = new TestJazzContextManager<CustomAccount>();
217
+
218
+ // Create initial anonymous context
219
+ await customManager.createContext({
220
+ AccountSchema: CustomAccount,
221
+ });
222
+
223
+ const account = (
224
+ customManager.getCurrentValue() as JazzAuthContext<CustomAccount>
225
+ ).me;
226
+
227
+ await customManager.authenticate({
228
+ accountID: account.id,
229
+ accountSecret: account._raw.core.node.account.agentSecret,
230
+ provider: "test",
231
+ });
232
+
233
+ const me = await CustomAccount.getMe().ensureLoaded({ root: {} });
234
+
235
+ expect(me.root.id).toBe(lastRootId);
236
+ });
237
+
238
+ test("the migration should be applied correctly on existing accounts (2)", async () => {
239
+ class AccountRoot extends CoMap {
240
+ value = co.number;
241
+ }
242
+
243
+ class CustomAccount extends Account {
244
+ root = co.ref(AccountRoot);
245
+
246
+ async migrate(this: CustomAccount) {
247
+ if (this.root === undefined) {
248
+ this.root = AccountRoot.create({
249
+ value: 1,
250
+ });
251
+ } else {
252
+ const { root } = await this.ensureLoaded({ root: {} });
253
+
254
+ root.value = 2;
255
+ }
256
+ }
257
+ }
258
+ const customManager = new TestJazzContextManager<CustomAccount>();
259
+
260
+ // Create initial anonymous context
261
+ await customManager.createContext({
262
+ AccountSchema: CustomAccount,
263
+ });
264
+
265
+ const account = (
266
+ customManager.getCurrentValue() as JazzAuthContext<CustomAccount>
267
+ ).me;
268
+
269
+ await customManager.authenticate({
270
+ accountID: account.id,
271
+ accountSecret: account._raw.core.node.account.agentSecret,
272
+ provider: "test",
273
+ });
274
+
275
+ const me = await CustomAccount.getMe().ensureLoaded({ root: {} });
276
+
277
+ expect(me.root.value).toBe(2);
278
+ });
279
+
199
280
  test("onAnonymousAccountDiscarded should work on transfering data between accounts", async () => {
200
281
  class AccountRoot extends CoMap {
201
282
  value = co.string;