jazz-tools 0.10.13 → 0.10.15

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.
@@ -1,5 +1,5 @@
1
1
 
2
- > jazz-tools@0.10.13 build /home/runner/_work/jazz/jazz/packages/jazz-tools
2
+ > jazz-tools@0.10.15 build /home/runner/_work/jazz/jazz/packages/jazz-tools
3
3
  > tsup
4
4
 
5
5
  CLI Building entry: {"index":"src/index.ts","testing":"src/testing.ts"}
@@ -10,9 +10,9 @@
10
10
  CLI Cleaning output folder
11
11
  ESM Build start
12
12
  ESM dist/index.js 1.50 KB
13
- ESM dist/testing.js 6.28 KB
14
- ESM dist/chunk-GSIV52ZH.js 115.71 KB
13
+ ESM dist/testing.js 6.18 KB
14
+ ESM dist/chunk-5USJBXLW.js 116.50 KB
15
15
  ESM dist/index.js.map 259.00 B
16
- ESM dist/testing.js.map 12.41 KB
17
- ESM dist/chunk-GSIV52ZH.js.map 276.19 KB
18
- ESM ⚡️ Build success in 92ms
16
+ ESM dist/testing.js.map 12.24 KB
17
+ ESM dist/chunk-5USJBXLW.js.map 277.65 KB
18
+ ESM ⚡️ Build success in 59ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # jazz-tools
2
2
 
3
+ ## 0.10.15
4
+
5
+ ### Patch Changes
6
+
7
+ - 2f99de0: Avoid race conditions on context creation and run the anonymous migration only when the hook is provided
8
+ - Updated dependencies [f86e278]
9
+ - cojson@0.10.15
10
+
11
+ ## 0.10.14
12
+
13
+ ### Patch Changes
14
+
15
+ - 75211e3: Fixes invalid authentication state when logging out after signUp
16
+
3
17
  ## 0.10.13
4
18
 
5
19
  ### Patch Changes
@@ -545,7 +545,7 @@ async function createJazzContext(options) {
545
545
  AccountSchema: options.AccountSchema,
546
546
  sessionProvider: options.sessionProvider,
547
547
  onLogOut: () => {
548
- authSecretStorage.clear();
548
+ authSecretStorage.clearWithoutNotify();
549
549
  }
550
550
  });
551
551
  } else {
@@ -561,11 +561,11 @@ async function createJazzContext(options) {
561
561
  crypto,
562
562
  AccountSchema: options.AccountSchema,
563
563
  onLogOut: async () => {
564
- await authSecretStorage.clear();
564
+ await authSecretStorage.clearWithoutNotify();
565
565
  }
566
566
  });
567
567
  if (!options.newAccountProps) {
568
- await authSecretStorage.set({
568
+ await authSecretStorage.setWithoutNotify({
569
569
  accountID: context.account.id,
570
570
  secretSeed,
571
571
  accountSecret: context.node.account.agentSecret,
@@ -3457,7 +3457,6 @@ var KvStoreContext_default = KvStoreContext;
3457
3457
  var STORAGE_KEY = "jazz-logged-in-secret";
3458
3458
  var AuthSecretStorage = class {
3459
3459
  constructor() {
3460
- this.notify = false;
3461
3460
  this.listeners = /* @__PURE__ */ new Set();
3462
3461
  this.isAuthenticated = false;
3463
3462
  }
@@ -3522,7 +3521,7 @@ var AuthSecretStorage = class {
3522
3521
  provider: parsed.provider
3523
3522
  };
3524
3523
  }
3525
- async set(payload) {
3524
+ async setWithoutNotify(payload) {
3526
3525
  const kvStore = KvStoreContext_default.getInstance().getStorage();
3527
3526
  await kvStore.set(
3528
3527
  STORAGE_KEY,
@@ -3533,9 +3532,10 @@ var AuthSecretStorage = class {
3533
3532
  provider: payload.provider
3534
3533
  })
3535
3534
  );
3536
- if (this.notify) {
3537
- this.emitUpdate(payload);
3538
- }
3535
+ }
3536
+ async set(payload) {
3537
+ this.setWithoutNotify(payload);
3538
+ this.emitUpdate(payload);
3539
3539
  }
3540
3540
  getIsAuthenticated(data) {
3541
3541
  if (!data) return false;
@@ -3555,12 +3555,13 @@ var AuthSecretStorage = class {
3555
3555
  listener(this.isAuthenticated);
3556
3556
  }
3557
3557
  }
3558
- async clear() {
3558
+ async clearWithoutNotify() {
3559
3559
  const kvStore = KvStoreContext_default.getInstance().getStorage();
3560
3560
  await kvStore.delete(STORAGE_KEY);
3561
- if (this.notify) {
3562
- this.emitUpdate(null);
3563
- }
3561
+ }
3562
+ async clear() {
3563
+ await this.clearWithoutNotify();
3564
+ this.emitUpdate(null);
3564
3565
  }
3565
3566
  };
3566
3567
 
@@ -3590,11 +3591,7 @@ import { cojsonInternals as cojsonInternals4 } from "cojson";
3590
3591
  var JazzContextManager = class {
3591
3592
  constructor() {
3592
3593
  this.authSecretStorage = new AuthSecretStorage();
3593
- this.authSecretStorageWithNotify = Object.assign(
3594
- Object.create(this.authSecretStorage),
3595
- { notify: true }
3596
- );
3597
- this.authenticating = false;
3594
+ this.keepContextOpen = false;
3598
3595
  this.logOut = async () => {
3599
3596
  if (!this.context || !this.props) {
3600
3597
  return;
@@ -3609,6 +3606,14 @@ var JazzContextManager = class {
3609
3606
  }
3610
3607
  this.context.done();
3611
3608
  };
3609
+ this.shouldMigrateAnonymousAccount = async () => {
3610
+ if (!this.props?.onAnonymousAccountDiscarded) {
3611
+ return false;
3612
+ }
3613
+ const prevCredentials = await this.authSecretStorage.get();
3614
+ const wasAnonymous = this.authSecretStorage.getIsAuthenticated(prevCredentials) === false;
3615
+ return wasAnonymous;
3616
+ };
3612
3617
  /**
3613
3618
  * Authenticates the user with the given credentials
3614
3619
  */
@@ -3617,13 +3622,12 @@ var JazzContextManager = class {
3617
3622
  throw new Error("Props required");
3618
3623
  }
3619
3624
  const prevContext = this.context;
3620
- const prevCredentials = await this.authSecretStorage.get();
3621
- const wasAnonymous = this.authSecretStorage.getIsAuthenticated(prevCredentials) === false;
3622
- this.authenticating = true;
3625
+ const migratingAnonymousAccount = await this.shouldMigrateAnonymousAccount();
3626
+ this.keepContextOpen = migratingAnonymousAccount;
3623
3627
  await this.createContext(this.props, { credentials }).finally(() => {
3624
- this.authenticating = false;
3628
+ this.keepContextOpen = false;
3625
3629
  });
3626
- if (wasAnonymous) {
3630
+ if (migratingAnonymousAccount) {
3627
3631
  await this.handleAnonymousAccountMigration(prevContext);
3628
3632
  }
3629
3633
  };
@@ -3632,18 +3636,17 @@ var JazzContextManager = class {
3632
3636
  throw new Error("Props required");
3633
3637
  }
3634
3638
  const prevContext = this.context;
3635
- const prevCredentials = await this.authSecretStorage.get();
3636
- const wasAnonymous = this.authSecretStorage.getIsAuthenticated(prevCredentials) === false;
3637
- this.authenticating = true;
3639
+ const migratingAnonymousAccount = await this.shouldMigrateAnonymousAccount();
3640
+ this.keepContextOpen = migratingAnonymousAccount;
3638
3641
  await this.createContext(this.props, {
3639
3642
  newAccountProps: {
3640
3643
  secret: accountSecret,
3641
3644
  creationProps
3642
3645
  }
3643
3646
  }).finally(() => {
3644
- this.authenticating = false;
3647
+ this.keepContextOpen = false;
3645
3648
  });
3646
- if (wasAnonymous) {
3649
+ if (migratingAnonymousAccount) {
3647
3650
  await this.handleAnonymousAccountMigration(prevContext);
3648
3651
  }
3649
3652
  if (this.context && "me" in this.context) {
@@ -3664,12 +3667,27 @@ var JazzContextManager = class {
3664
3667
  return new InMemoryKVStore();
3665
3668
  }
3666
3669
  async createContext(props, authProps) {
3670
+ this.props = props;
3671
+ const { promise, resolve } = createResolvablePromise();
3672
+ const prevPromise = this.contextPromise;
3673
+ this.contextPromise = promise;
3674
+ await prevPromise;
3675
+ try {
3676
+ const result = await this.getNewContext(props, authProps);
3677
+ await this.updateContext(props, result, authProps);
3678
+ resolve();
3679
+ } catch (error) {
3680
+ resolve();
3681
+ throw error;
3682
+ }
3683
+ }
3684
+ async getNewContext(props, authProps) {
3667
3685
  props;
3668
3686
  authProps;
3669
3687
  throw new Error("Not implemented");
3670
3688
  }
3671
3689
  async updateContext(props, context, authProps) {
3672
- if (!this.authenticating) {
3690
+ if (!this.keepContextOpen) {
3673
3691
  this.context?.done();
3674
3692
  }
3675
3693
  this.context = context;
@@ -3696,7 +3714,7 @@ var JazzContextManager = class {
3696
3714
  return this.value;
3697
3715
  }
3698
3716
  getAuthSecretStorage() {
3699
- return this.authSecretStorageWithNotify;
3717
+ return this.authSecretStorage;
3700
3718
  }
3701
3719
  async handleAnonymousAccountMigration(prevContext) {
3702
3720
  if (!this.props) {
@@ -3731,6 +3749,13 @@ var JazzContextManager = class {
3731
3749
  }
3732
3750
  }
3733
3751
  };
3752
+ function createResolvablePromise() {
3753
+ let resolve;
3754
+ const promise = new Promise((res) => {
3755
+ resolve = res;
3756
+ });
3757
+ return { promise, resolve };
3758
+ }
3734
3759
 
3735
3760
  // src/auth/DemoAuth.ts
3736
3761
  var DemoAuth = class {
@@ -4048,4 +4073,4 @@ export {
4048
4073
  consumeInviteLink
4049
4074
  };
4050
4075
  /* istanbul ignore file -- @preserve */
4051
- //# sourceMappingURL=chunk-GSIV52ZH.js.map
4076
+ //# sourceMappingURL=chunk-5USJBXLW.js.map