jazz-tools 0.7.35-new-auth.0 → 0.7.35

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. package/.turbo/turbo-build.log +2 -2
  2. package/.turbo/turbo-test.log +144 -0
  3. package/CHANGELOG.md +8 -2
  4. package/dist/coValues/account.js +35 -20
  5. package/dist/coValues/account.js.map +1 -1
  6. package/dist/coValues/coList.js +19 -14
  7. package/dist/coValues/coList.js.map +1 -1
  8. package/dist/coValues/coMap.js +30 -14
  9. package/dist/coValues/coMap.js.map +1 -1
  10. package/dist/coValues/coStream.js +37 -15
  11. package/dist/coValues/coStream.js.map +1 -1
  12. package/dist/coValues/extensions/imageDef.js +8 -3
  13. package/dist/coValues/extensions/imageDef.js.map +1 -1
  14. package/dist/coValues/group.js +23 -20
  15. package/dist/coValues/group.js.map +1 -1
  16. package/dist/implementation/devtoolsFormatters.js +1 -0
  17. package/dist/implementation/devtoolsFormatters.js.map +1 -1
  18. package/dist/implementation/refs.js +0 -3
  19. package/dist/implementation/refs.js.map +1 -1
  20. package/dist/implementation/schema.js.map +1 -1
  21. package/dist/implementation/subscriptionScope.js +5 -8
  22. package/dist/implementation/subscriptionScope.js.map +1 -1
  23. package/dist/index.js +0 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/internal.js +0 -1
  26. package/dist/internal.js.map +1 -1
  27. package/dist/tests/coList.test.js +15 -23
  28. package/dist/tests/coList.test.js.map +1 -1
  29. package/dist/tests/coMap.test.js +213 -53
  30. package/dist/tests/coMap.test.js.map +1 -1
  31. package/dist/tests/coStream.test.js +59 -26
  32. package/dist/tests/coStream.test.js.map +1 -1
  33. package/dist/tests/deepLoading.test.js +41 -28
  34. package/dist/tests/deepLoading.test.js.map +1 -1
  35. package/dist/tests/groupsAndAccounts.test.js +22 -8
  36. package/dist/tests/groupsAndAccounts.test.js.map +1 -1
  37. package/package.json +3 -3
  38. package/src/coValues/account.ts +28 -5
  39. package/src/coValues/coList.ts +4 -4
  40. package/src/coValues/coMap.ts +54 -8
  41. package/src/coValues/coStream.ts +30 -5
  42. package/src/coValues/group.ts +2 -2
  43. package/src/coValues/interfaces.ts +4 -4
  44. package/src/implementation/devtoolsFormatters.ts +1 -0
  45. package/src/implementation/schema.ts +1 -0
  46. package/src/index.ts +1 -8
  47. package/src/internal.ts +0 -2
  48. package/src/tests/coList.test.ts +14 -30
  49. package/src/tests/coMap.test.ts +150 -16
  50. package/src/tests/coStream.test.ts +74 -27
  51. package/src/tests/deepLoading.test.ts +18 -22
  52. package/tsconfig.json +1 -1
  53. package/dist/implementation/createContext.js +0 -94
  54. package/dist/implementation/createContext.js.map +0 -1
  55. package/src/implementation/createContext.ts +0 -162
package/src/internal.ts CHANGED
@@ -16,6 +16,4 @@ export * from "./coValues/deepLoading.js";
16
16
 
17
17
  export * from "./coValues/extensions/imageDef.js";
18
18
 
19
- export * from "./implementation/createContext.js";
20
-
21
19
  import "./implementation/devtoolsFormatters.js";
@@ -1,16 +1,14 @@
1
1
  import { expect, describe, test } from "vitest";
2
2
  import { connectedPeers } from "cojson/src/streamUtils.js";
3
+ import { newRandomSessionID } from "cojson/src/coValueCore.js";
3
4
  import {
4
5
  Account,
5
6
  CoList,
6
7
  WasmCrypto,
7
8
  co,
8
9
  cojsonInternals,
9
- createJazzContext,
10
10
  isControlledAccount,
11
- fixedCredentialsAuth,
12
11
  } from "../index.js";
13
- import { randomSessionProvider } from "../internal.js";
14
12
 
15
13
  const Crypto = await WasmCrypto.create();
16
14
 
@@ -171,13 +169,12 @@ describe("CoList resolution", async () => {
171
169
  throw "me is not a controlled account";
172
170
  }
173
171
  me._raw.core.node.syncManager.addPeer(secondPeer);
174
- const { account: meOnSecondPeer } = await createJazzContext({
175
- auth: fixedCredentialsAuth({
176
- accountID: me.id,
177
- secret: me._raw.agentSecret,
178
- }),
179
- sessionProvider: randomSessionProvider,
172
+ const meOnSecondPeer = await Account.become({
173
+ accountID: me.id,
174
+ accountSecret: me._raw.agentSecret,
180
175
  peersToLoadFrom: [initialAsPeer],
176
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
177
+ sessionID: newRandomSessionID(me.id as any),
181
178
  crypto: Crypto,
182
179
  });
183
180
 
@@ -195,11 +192,7 @@ describe("CoList resolution", async () => {
195
192
  expect(loadedList?.[0]).toBeDefined();
196
193
  expect(loadedList?.[0]?.[0]).toBe(null);
197
194
  expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]![0]!.id);
198
- // TODO: this should be ref equal
199
- // expect(loadedList?._refs[0]?.value).toEqual(loadedNestedList);
200
- expect(loadedList?._refs[0]?.value?.toJSON()).toEqual(
201
- loadedNestedList?.toJSON(),
202
- );
195
+ expect(loadedList?._refs[0]?.value).toEqual(loadedNestedList);
203
196
 
204
197
  const loadedTwiceNestedList = await TwiceNestedList.load(
205
198
  list[0]![0]!.id,
@@ -211,11 +204,7 @@ describe("CoList resolution", async () => {
211
204
  expect(loadedList?.[0]?.[0]?.[0]).toBe("a");
212
205
  expect(loadedList?.[0]?.[0]?.joined()).toBe("a,b");
213
206
  expect(loadedList?.[0]?._refs[0]?.id).toEqual(list[0]?.[0]?.id);
214
- // TODO: this should be ref equal
215
- // expect(loadedList?.[0]?._refs[0]?.value).toEqual(loadedTwiceNestedList);
216
- expect(loadedList?.[0]?._refs[0]?.value?.toJSON()).toEqual(
217
- loadedTwiceNestedList?.toJSON(),
218
- );
207
+ expect(loadedList?.[0]?._refs[0]?.value).toEqual(loadedTwiceNestedList);
219
208
 
220
209
  const otherNestedList = NestedList.create(
221
210
  [TwiceNestedList.create(["e", "f"], { owner: meOnSecondPeer })],
@@ -223,11 +212,7 @@ describe("CoList resolution", async () => {
223
212
  );
224
213
 
225
214
  loadedList![0] = otherNestedList;
226
- // TODO: this should be ref equal
227
- // expect(loadedList?.[0]).toEqual(otherNestedList);
228
- expect(loadedList?._refs[0]?.value?.toJSON()).toEqual(
229
- otherNestedList.toJSON(),
230
- );
215
+ expect(loadedList?.[0]).toEqual(otherNestedList);
231
216
  expect(loadedList?._refs[0]?.id).toEqual(otherNestedList.id);
232
217
  });
233
218
 
@@ -246,13 +231,12 @@ describe("CoList resolution", async () => {
246
231
  throw "me is not a controlled account";
247
232
  }
248
233
  me._raw.core.node.syncManager.addPeer(secondPeer);
249
- const { account: meOnSecondPeer } = await createJazzContext({
250
- auth: fixedCredentialsAuth({
251
- accountID: me.id,
252
- secret: me._raw.agentSecret,
253
- }),
254
- sessionProvider: randomSessionProvider,
234
+ const meOnSecondPeer = await Account.become({
235
+ accountID: me.id,
236
+ accountSecret: me._raw.agentSecret,
255
237
  peersToLoadFrom: [initialAsPeer],
238
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
239
+ sessionID: newRandomSessionID(me.id as any),
256
240
  crypto: Crypto,
257
241
  });
258
242
 
@@ -1,5 +1,6 @@
1
- import { expect, describe, test } from "vitest";
1
+ import { expect, describe, test, expectTypeOf } from "vitest";
2
2
  import { connectedPeers } from "cojson/src/streamUtils.js";
3
+ import { newRandomSessionID } from "cojson/src/coValueCore.js";
3
4
  import {
4
5
  Account,
5
6
  Encoders,
@@ -8,10 +9,7 @@ import {
8
9
  WasmCrypto,
9
10
  isControlledAccount,
10
11
  cojsonInternals,
11
- createJazzContext,
12
- fixedCredentialsAuth,
13
12
  } from "../index.js";
14
- import { randomSessionProvider } from "../internal.js";
15
13
 
16
14
  const Crypto = await WasmCrypto.create();
17
15
 
@@ -299,13 +297,12 @@ describe("CoMap resolution", async () => {
299
297
  throw "me is not a controlled account";
300
298
  }
301
299
  me._raw.core.node.syncManager.addPeer(secondPeer);
302
- const { account: meOnSecondPeer } = await createJazzContext({
303
- auth: fixedCredentialsAuth({
304
- accountID: me.id,
305
- secret: me._raw.agentSecret,
306
- }),
307
- sessionProvider: randomSessionProvider,
300
+ const meOnSecondPeer = await Account.become({
301
+ accountID: me.id,
302
+ accountSecret: me._raw.agentSecret,
308
303
  peersToLoadFrom: [initialAsPeer],
304
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
305
+ sessionID: newRandomSessionID(me.id as any),
309
306
  crypto: Crypto,
310
307
  });
311
308
 
@@ -374,13 +371,12 @@ describe("CoMap resolution", async () => {
374
371
  throw "me is not a controlled account";
375
372
  }
376
373
  me._raw.core.node.syncManager.addPeer(secondAsPeer);
377
- const { account: meOnSecondPeer } = await createJazzContext({
378
- auth: fixedCredentialsAuth({
379
- accountID: me.id,
380
- secret: me._raw.agentSecret,
381
- }),
382
- sessionProvider: randomSessionProvider,
374
+ const meOnSecondPeer = await Account.become({
375
+ accountID: me.id,
376
+ accountSecret: me._raw.agentSecret,
383
377
  peersToLoadFrom: [initialAsPeer],
378
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
379
+ sessionID: newRandomSessionID(me.id as any),
384
380
  crypto: Crypto,
385
381
  });
386
382
 
@@ -592,6 +588,7 @@ describe("CoMap applyDiff", async () => {
592
588
  birthday = co.encoded(Encoders.Date);
593
589
  nested = co.ref(NestedMap);
594
590
  optionalField = co.optional.string;
591
+ optionalNested = co.optional.ref(NestedMap);
595
592
  }
596
593
 
597
594
  class NestedMap extends CoMap {
@@ -737,4 +734,141 @@ describe("CoMap applyDiff", async () => {
737
734
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
738
735
  expect((map as any).invalidField).toBeUndefined();
739
736
  });
737
+
738
+ test("applyDiff with optional reference set to null", () => {
739
+ const map = TestMap.create(
740
+ {
741
+ name: "Jack",
742
+ age: 50,
743
+ isActive: true,
744
+ birthday: new Date("1970-01-01"),
745
+ nested: NestedMap.create({ value: "original" }, { owner: me }),
746
+ optionalNested: NestedMap.create(
747
+ { value: "optional" },
748
+ { owner: me },
749
+ ),
750
+ },
751
+ { owner: me },
752
+ );
753
+
754
+ const newValues = {
755
+ optionalNested: null,
756
+ };
757
+
758
+ map.applyDiff(newValues);
759
+
760
+ expect(map.optionalNested).toBeNull();
761
+ });
762
+
763
+ test("applyDiff with required reference set to null should throw", () => {
764
+ const map = TestMap.create(
765
+ {
766
+ name: "Kate",
767
+ age: 55,
768
+ isActive: true,
769
+ birthday: new Date("1965-01-01"),
770
+ nested: NestedMap.create({ value: "original" }, { owner: me }),
771
+ },
772
+ { owner: me },
773
+ );
774
+
775
+ const newValues = {
776
+ nested: null,
777
+ };
778
+
779
+ // @ts-expect-error testing invalid usage
780
+ expect(() => map.applyDiff(newValues)).toThrowError(
781
+ "Cannot set required reference nested to null",
782
+ );
783
+ });
784
+ });
785
+
786
+ describe("CoMap Typescript validation", async () => {
787
+ const me = await Account.create({
788
+ creationProps: { name: "Hermes Puggington" },
789
+ crypto: Crypto,
790
+ });
791
+
792
+ test("Is not ok to pass null into a required ref", () => {
793
+ class TestMap extends CoMap {
794
+ required = co.ref(NestedMap);
795
+ optional = co.optional.ref(NestedMap);
796
+ }
797
+
798
+ class NestedMap extends CoMap {
799
+ value = co.string;
800
+ }
801
+
802
+ expectTypeOf<typeof TestMap.create<TestMap>>().toBeCallableWith(
803
+ {
804
+ optional: NestedMap.create({ value: "" }, { owner: me }),
805
+ // @ts-expect-error null can't be passed to a non-optional field
806
+ required: null,
807
+ },
808
+ { owner: me },
809
+ );
810
+ });
811
+
812
+ test("Is not ok if a required ref is omitted", () => {
813
+ class TestMap extends CoMap {
814
+ required = co.ref(NestedMap);
815
+ optional = co.ref(NestedMap, { optional: true });
816
+ }
817
+
818
+ class NestedMap extends CoMap {
819
+ value = co.string;
820
+ }
821
+
822
+ expectTypeOf<typeof TestMap.create<TestMap>>().toBeCallableWith(
823
+ // @ts-expect-error non-optional fields can't be omitted
824
+ {},
825
+ { owner: me },
826
+ );
827
+ });
828
+
829
+ test("Is ok to omit optional fields", () => {
830
+ class TestMap extends CoMap {
831
+ required = co.ref(NestedMap);
832
+ optional = co.ref(NestedMap, { optional: true });
833
+ }
834
+
835
+ class NestedMap extends CoMap {
836
+ value = co.string;
837
+ }
838
+
839
+ expectTypeOf<typeof TestMap.create<TestMap>>().toBeCallableWith(
840
+ {
841
+ required: NestedMap.create({ value: "" }, { owner: me }),
842
+ },
843
+ { owner: me },
844
+ );
845
+
846
+ expectTypeOf<typeof TestMap.create<TestMap>>().toBeCallableWith(
847
+ {
848
+ required: NestedMap.create({ value: "" }, { owner: me }),
849
+ optional: null,
850
+ },
851
+ { owner: me },
852
+ );
853
+ });
854
+
855
+ test("the required refs should be nullable", () => {
856
+ class TestMap extends CoMap {
857
+ required = co.ref(NestedMap);
858
+ optional = co.ref(NestedMap, { optional: true });
859
+ }
860
+
861
+ class NestedMap extends CoMap {
862
+ value = co.string;
863
+ }
864
+
865
+ const map = TestMap.create(
866
+ {
867
+ required: NestedMap.create({ value: "" }, { owner: me }),
868
+ },
869
+ { owner: me },
870
+ );
871
+
872
+ expectTypeOf(map.required).toBeNullable();
873
+ });
740
874
  });
@@ -1,5 +1,6 @@
1
1
  import { expect, describe, test } from "vitest";
2
2
  import { connectedPeers } from "cojson/src/streamUtils.js";
3
+ import { newRandomSessionID } from "cojson/src/coValueCore.js";
3
4
  import {
4
5
  BinaryCoStream,
5
6
  ID,
@@ -9,10 +10,7 @@ import {
9
10
  WasmCrypto,
10
11
  isControlledAccount,
11
12
  cojsonInternals,
12
- createJazzContext,
13
- fixedCredentialsAuth,
14
13
  } from "../index.js";
15
- import { randomSessionProvider } from "../internal.js";
16
14
 
17
15
  const Crypto = await WasmCrypto.create();
18
16
 
@@ -97,13 +95,12 @@ describe("CoStream resolution", async () => {
97
95
  throw "me is not a controlled account";
98
96
  }
99
97
  me._raw.core.node.syncManager.addPeer(secondPeer);
100
- const { account: meOnSecondPeer } = await createJazzContext({
101
- auth: fixedCredentialsAuth({
102
- accountID: me.id,
103
- secret: me._raw.agentSecret,
104
- }),
105
- sessionProvider: randomSessionProvider,
98
+ const meOnSecondPeer = await Account.become({
99
+ accountID: me.id,
100
+ accountSecret: me._raw.agentSecret,
106
101
  peersToLoadFrom: [initialAsPeer],
102
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
+ sessionID: newRandomSessionID(me.id as any),
107
104
  crypto: Crypto,
108
105
  });
109
106
 
@@ -194,13 +191,12 @@ describe("CoStream resolution", async () => {
194
191
  if (!isControlledAccount(me)) {
195
192
  throw "me is not a controlled account";
196
193
  }
197
- const { account: meOnSecondPeer } = await createJazzContext({
198
- auth: fixedCredentialsAuth({
199
- accountID: me.id,
200
- secret: me._raw.agentSecret,
201
- }),
202
- sessionProvider: randomSessionProvider,
194
+ const meOnSecondPeer = await Account.become({
195
+ accountID: me.id,
196
+ accountSecret: me._raw.agentSecret,
203
197
  peersToLoadFrom: [initialAsPeer],
198
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
199
+ sessionID: newRandomSessionID(me.id as any),
204
200
  crypto: Crypto,
205
201
  });
206
202
 
@@ -338,13 +334,12 @@ describe("BinaryCoStream loading & Subscription", async () => {
338
334
  throw "me is not a controlled account";
339
335
  }
340
336
  me._raw.core.node.syncManager.addPeer(secondAsPeer);
341
- const { account: meOnSecondPeer } = await createJazzContext({
342
- auth: fixedCredentialsAuth({
343
- accountID: me.id,
344
- secret: me._raw.agentSecret,
345
- }),
346
- sessionProvider: randomSessionProvider,
337
+ const meOnSecondPeer = await Account.become({
338
+ accountID: me.id,
339
+ accountSecret: me._raw.agentSecret,
347
340
  peersToLoadFrom: [initialAsPeer],
341
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
342
+ sessionID: newRandomSessionID(me.id as any),
348
343
  crypto: Crypto,
349
344
  });
350
345
 
@@ -374,13 +369,12 @@ describe("BinaryCoStream loading & Subscription", async () => {
374
369
  if (!isControlledAccount(me)) {
375
370
  throw "me is not a controlled account";
376
371
  }
377
- const { account: meOnSecondPeer } = await createJazzContext({
378
- auth: fixedCredentialsAuth({
379
- accountID: me.id,
380
- secret: me._raw.agentSecret,
381
- }),
382
- sessionProvider: randomSessionProvider,
372
+ const meOnSecondPeer = await Account.become({
373
+ accountID: me.id,
374
+ accountSecret: me._raw.agentSecret,
383
375
  peersToLoadFrom: [initialAsPeer],
376
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
377
+ sessionID: newRandomSessionID(me.id as any),
384
378
  crypto: Crypto,
385
379
  });
386
380
 
@@ -443,3 +437,56 @@ describe("BinaryCoStream loading & Subscription", async () => {
443
437
  });
444
438
  });
445
439
  });
440
+
441
+ describe("BinaryCoStream.loadAsBlob", async () => {
442
+ async function setup() {
443
+ const me = await Account.create({
444
+ creationProps: { name: "Hermes Puggington" },
445
+ crypto: Crypto,
446
+ });
447
+
448
+ const stream = BinaryCoStream.create({ owner: me });
449
+
450
+ stream.start({ mimeType: "text/plain" });
451
+
452
+ return { stream, me };
453
+ }
454
+
455
+ test("resolves only when the stream is ended", async () => {
456
+ const { stream, me } = await setup();
457
+ stream.push(new Uint8Array([1]));
458
+
459
+ const promise = BinaryCoStream.loadAsBlob(stream.id, me);
460
+
461
+ await stream.ensureLoaded([]);
462
+
463
+ stream.push(new Uint8Array([2]));
464
+ stream.end();
465
+
466
+ const blob = await promise;
467
+
468
+ // The promise resolves only when the stream is ended
469
+ // so we get a blob with all the chunks
470
+ expect(blob?.size).toBe(2);
471
+ });
472
+
473
+ test("resolves with an unfinshed blob if allowUnfinished: true", async () => {
474
+ const { stream, me } = await setup();
475
+ stream.push(new Uint8Array([1]));
476
+
477
+ const promise = BinaryCoStream.loadAsBlob(stream.id, me, {
478
+ allowUnfinished: true,
479
+ });
480
+
481
+ await stream.ensureLoaded([]);
482
+
483
+ stream.push(new Uint8Array([2]));
484
+ stream.end();
485
+
486
+ const blob = await promise;
487
+
488
+ // The promise resolves before the stream is ended
489
+ // so we get a blob only with the first chunk
490
+ expect(blob?.size).toBe(1);
491
+ });
492
+ });
@@ -12,10 +12,8 @@ import {
12
12
  Profile,
13
13
  isControlledAccount,
14
14
  ID,
15
- createJazzContext,
16
- fixedCredentialsAuth,
17
15
  } from "../index.js";
18
- import { randomSessionProvider } from "../internal.js";
16
+ import { newRandomSessionID } from "cojson/src/coValueCore.js";
19
17
 
20
18
  class TestMap extends CoMap {
21
19
  list = co.ref(TestList);
@@ -49,17 +47,17 @@ describe("Deep loading with depth arg", async () => {
49
47
  throw "me is not a controlled account";
50
48
  }
51
49
  me._raw.core.node.syncManager.addPeer(secondPeer);
52
- const { account: meOnSecondPeer } = await createJazzContext({
53
- auth: fixedCredentialsAuth({
54
- accountID: me.id,
55
- secret: me._raw.agentSecret,
56
- }),
57
- sessionProvider: randomSessionProvider,
50
+ const meOnSecondPeer = await Account.become({
51
+ accountID: me.id,
52
+ accountSecret: me._raw.agentSecret,
58
53
  peersToLoadFrom: [initialAsPeer],
54
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
+ sessionID: newRandomSessionID(me.id as any),
59
56
  crypto: Crypto,
60
57
  });
61
58
 
62
59
  test("loading a deeply nested object will wait until all required refs are loaded", async () => {
60
+ const ownership = { owner: me };
63
61
  const map = TestMap.create(
64
62
  {
65
63
  list: TestList.create(
@@ -70,19 +68,19 @@ describe("Deep loading with depth arg", async () => {
70
68
  [
71
69
  InnermostMap.create(
72
70
  { value: "hello" },
73
- { owner: me },
71
+ ownership,
74
72
  ),
75
73
  ],
76
- { owner: me },
74
+ ownership,
77
75
  ),
78
76
  },
79
- { owner: me },
77
+ ownership,
80
78
  ),
81
79
  ],
82
- { owner: me },
80
+ ownership,
83
81
  ),
84
82
  },
85
- { owner: me },
83
+ ownership,
86
84
  );
87
85
 
88
86
  const map1 = await TestMap.load(map.id, meOnSecondPeer, {});
@@ -141,8 +139,7 @@ describe("Deep loading with depth arg", async () => {
141
139
  throw new Error("map4 is undefined");
142
140
  }
143
141
  expect(map4.list[0]?.stream).not.toBe(null);
144
- // TODO: why is this actually defined?
145
- // expect(map4.list[0]?.stream?.[me.id]).toBe(undefined)
142
+ expect(map4.list[0]?.stream?.[me.id]).not.toBe(null);
146
143
  expect(map4.list[0]?.stream?.byMe?.value).toBe(null);
147
144
 
148
145
  const map5 = await TestMap.load(map.id, meOnSecondPeer, {
@@ -266,13 +263,12 @@ test("Deep loading a record-like coMap", async () => {
266
263
  }
267
264
 
268
265
  me._raw.core.node.syncManager.addPeer(secondPeer);
269
- const { account: meOnSecondPeer } = await createJazzContext({
270
- auth: fixedCredentialsAuth({
271
- accountID: me.id,
272
- secret: me._raw.agentSecret,
273
- }),
274
- sessionProvider: randomSessionProvider,
266
+ const meOnSecondPeer = await Account.become({
267
+ accountID: me.id,
268
+ accountSecret: me._raw.agentSecret,
275
269
  peersToLoadFrom: [initialAsPeer],
270
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
271
+ sessionID: newRandomSessionID(me.id as any),
276
272
  crypto: Crypto,
277
273
  });
278
274
 
package/tsconfig.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "compilerOptions": {
3
3
  "lib": ["ESNext"],
4
4
  "module": "esnext",
5
- "target": "es2022",
5
+ "target": "ES2020",
6
6
  "moduleResolution": "bundler",
7
7
  "moduleDetection": "force",
8
8
  "strict": true,
@@ -1,94 +0,0 @@
1
- import { cojsonInternals, LocalNode, } from "cojson";
2
- import { Account } from "../internal.js";
3
- export const fixedCredentialsAuth = (credentials) => {
4
- return {
5
- start: async () => ({
6
- type: "existing",
7
- credentials,
8
- onSuccess: () => { },
9
- onError: () => { },
10
- }),
11
- };
12
- };
13
- export async function randomSessionProvider(accountID) {
14
- return {
15
- sessionID: cojsonInternals.newRandomSessionID(accountID),
16
- sessionDone: () => { },
17
- };
18
- }
19
- export async function createJazzContext({ AccountSchema = Account, auth, sessionProvider, peersToLoadFrom, crypto, }) {
20
- // eslint-disable-next-line no-constant-condition
21
- while (true) {
22
- const authResult = await auth.start(crypto);
23
- if (authResult.type === "existing") {
24
- try {
25
- const { sessionID, sessionDone } = await sessionProvider(authResult.credentials.accountID);
26
- try {
27
- const node = await LocalNode.withLoadedAccount({
28
- accountID: authResult.credentials
29
- .accountID,
30
- accountSecret: authResult.credentials.secret,
31
- sessionID: sessionID,
32
- peersToLoadFrom: peersToLoadFrom,
33
- crypto: crypto,
34
- migration: async (rawAccount, _node, creationProps) => {
35
- const account = new AccountSchema({
36
- fromRaw: rawAccount,
37
- });
38
- await account.migrate?.(creationProps);
39
- },
40
- });
41
- const account = AccountSchema.fromNode(node);
42
- authResult.onSuccess();
43
- return {
44
- account,
45
- done: () => {
46
- node.gracefulShutdown();
47
- sessionDone();
48
- },
49
- };
50
- }
51
- catch (e) {
52
- authResult.onError(new Error("Error loading account", { cause: e }));
53
- sessionDone();
54
- }
55
- }
56
- catch (e) {
57
- authResult.onError(new Error("Error acquiring sessionID", { cause: e }));
58
- }
59
- }
60
- else if (authResult.type === "new") {
61
- try {
62
- // TODO: figure out a way to not "waste" the first SessionID
63
- const { node } = await LocalNode.withNewlyCreatedAccount({
64
- creationProps: authResult.creationProps,
65
- peersToLoadFrom: peersToLoadFrom,
66
- crypto: crypto,
67
- initialAgentSecret: authResult.initialSecret,
68
- migration: async (rawAccount, _node, creationProps) => {
69
- const account = new AccountSchema({
70
- fromRaw: rawAccount,
71
- });
72
- await account.migrate?.(creationProps);
73
- },
74
- });
75
- const account = AccountSchema.fromNode(node);
76
- await authResult.saveCredentials({
77
- accountID: node.account.id,
78
- secret: node.account.agentSecret,
79
- });
80
- authResult.onSuccess();
81
- return {
82
- account,
83
- done: () => {
84
- node.gracefulShutdown();
85
- },
86
- };
87
- }
88
- catch (e) {
89
- authResult.onError(new Error("Error creating account", { cause: e }));
90
- }
91
- }
92
- }
93
- }
94
- //# sourceMappingURL=createContext.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createContext.js","sourceRoot":"","sources":["../../src/implementation/createContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAGH,eAAe,EAEf,SAAS,GAKZ,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,OAAO,EAAoB,MAAM,gBAAgB,CAAC;AAyB3D,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,WAGpC,EAAc,EAAE;IACb,OAAO;QACH,KAAK,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC;YAChB,IAAI,EAAE,UAAU;YAChB,WAAW;YACX,SAAS,EAAE,GAAG,EAAE,GAAE,CAAC;YACnB,OA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