jazz-tools 0.19.11 → 0.19.13

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.
Files changed (151) hide show
  1. package/.turbo/turbo-build.log +51 -51
  2. package/CHANGELOG.md +22 -0
  3. package/dist/browser/createBrowserContext.d.ts +1 -5
  4. package/dist/browser/createBrowserContext.d.ts.map +1 -1
  5. package/dist/browser/index.js +124 -47
  6. package/dist/browser/index.js.map +1 -1
  7. package/dist/browser/provideBrowserLockSession/BrowserSessionProvider.d.ts +12 -0
  8. package/dist/browser/provideBrowserLockSession/BrowserSessionProvider.d.ts.map +1 -0
  9. package/dist/browser/provideBrowserLockSession/BrowserSessionProvider.test.d.ts +2 -0
  10. package/dist/browser/provideBrowserLockSession/BrowserSessionProvider.test.d.ts.map +1 -0
  11. package/dist/browser/provideBrowserLockSession/SessionIDStorage.d.ts +6 -0
  12. package/dist/browser/provideBrowserLockSession/SessionIDStorage.d.ts.map +1 -0
  13. package/dist/browser/provideBrowserLockSession/index.d.ts +4 -0
  14. package/dist/browser/provideBrowserLockSession/index.d.ts.map +1 -0
  15. package/dist/{chunk-HX5S6W5E.js → chunk-GAPMDNJY.js} +492 -108
  16. package/dist/chunk-GAPMDNJY.js.map +1 -0
  17. package/dist/index.js +5 -3
  18. package/dist/index.js.map +1 -1
  19. package/dist/inspector/{chunk-C6BJPHBQ.js → chunk-YQNK5Y7B.js} +47 -35
  20. package/dist/inspector/chunk-YQNK5Y7B.js.map +1 -0
  21. package/dist/inspector/{custom-element-GJVBPZES.js → custom-element-KYV64IOC.js} +47 -35
  22. package/dist/inspector/{custom-element-GJVBPZES.js.map → custom-element-KYV64IOC.js.map} +1 -1
  23. package/dist/inspector/index.js +1 -1
  24. package/dist/inspector/register-custom-element.js +1 -1
  25. package/dist/inspector/standalone.js +1 -1
  26. package/dist/inspector/tests/utils/transactions-changes.test.d.ts +2 -0
  27. package/dist/inspector/tests/utils/transactions-changes.test.d.ts.map +1 -0
  28. package/dist/inspector/utils/transactions-changes.d.ts +13 -13
  29. package/dist/inspector/utils/transactions-changes.d.ts.map +1 -1
  30. package/dist/react/index.js +4 -1
  31. package/dist/react/index.js.map +1 -1
  32. package/dist/react/provider.d.ts.map +1 -1
  33. package/dist/react-core/index.js +2 -2
  34. package/dist/react-core/index.js.map +1 -1
  35. package/dist/react-native/index.js +45 -13
  36. package/dist/react-native/index.js.map +1 -1
  37. package/dist/react-native-core/ReactNativeSessionProvider.d.ts +11 -0
  38. package/dist/react-native-core/ReactNativeSessionProvider.d.ts.map +1 -0
  39. package/dist/react-native-core/index.js +45 -13
  40. package/dist/react-native-core/index.js.map +1 -1
  41. package/dist/react-native-core/platform.d.ts +2 -8
  42. package/dist/react-native-core/platform.d.ts.map +1 -1
  43. package/dist/react-native-core/provider.d.ts.map +1 -1
  44. package/dist/react-native-core/tests/ReactNativeSessionProvider.test.d.ts +2 -0
  45. package/dist/react-native-core/tests/ReactNativeSessionProvider.test.d.ts.map +1 -0
  46. package/dist/testing.js +4 -3
  47. package/dist/testing.js.map +1 -1
  48. package/dist/tools/coValues/account.d.ts.map +1 -1
  49. package/dist/tools/coValues/coFeed.d.ts +2 -2
  50. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  51. package/dist/tools/coValues/coList.d.ts +1 -2
  52. package/dist/tools/coValues/coList.d.ts.map +1 -1
  53. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  54. package/dist/tools/coValues/coVector.d.ts.map +1 -1
  55. package/dist/tools/coValues/group.d.ts +5 -1
  56. package/dist/tools/coValues/group.d.ts.map +1 -1
  57. package/dist/tools/coValues/interfaces.d.ts +2 -1
  58. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  59. package/dist/tools/exports.d.ts +2 -2
  60. package/dist/tools/exports.d.ts.map +1 -1
  61. package/dist/tools/implementation/ContextManager.d.ts.map +1 -1
  62. package/dist/tools/implementation/createContext.d.ts +21 -11
  63. package/dist/tools/implementation/createContext.d.ts.map +1 -1
  64. package/dist/tools/implementation/schema.d.ts +14 -6
  65. package/dist/tools/implementation/schema.d.ts.map +1 -1
  66. package/dist/tools/implementation/schemaUtils.d.ts +1 -1
  67. package/dist/tools/implementation/schemaUtils.d.ts.map +1 -1
  68. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -1
  69. package/dist/tools/implementation/zodSchema/schemaPermissions.d.ts +99 -0
  70. package/dist/tools/implementation/zodSchema/schemaPermissions.d.ts.map +1 -0
  71. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +11 -0
  72. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  73. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +11 -0
  74. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  75. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +15 -1
  76. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  77. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +10 -0
  78. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  79. package/dist/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.d.ts +9 -0
  80. package/dist/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.d.ts.map +1 -1
  81. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +13 -1
  82. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  83. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +10 -0
  84. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  85. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +6 -0
  86. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  87. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +12 -1
  88. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  89. package/dist/tools/internal.d.ts +1 -0
  90. package/dist/tools/internal.d.ts.map +1 -1
  91. package/dist/tools/subscribe/SubscriptionScope.d.ts +3 -6
  92. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  93. package/dist/tools/testing.d.ts.map +1 -1
  94. package/dist/tools/tests/schema.withPermissions.test.d.ts +2 -0
  95. package/dist/tools/tests/schema.withPermissions.test.d.ts.map +1 -0
  96. package/dist/worker/index.js +2 -2
  97. package/dist/worker/index.js.map +1 -1
  98. package/package.json +4 -4
  99. package/src/browser/createBrowserContext.ts +3 -62
  100. package/src/browser/provideBrowserLockSession/BrowserSessionProvider.test.ts +406 -0
  101. package/src/browser/provideBrowserLockSession/BrowserSessionProvider.ts +132 -0
  102. package/src/browser/provideBrowserLockSession/SessionIDStorage.ts +33 -0
  103. package/src/browser/provideBrowserLockSession/index.ts +11 -0
  104. package/src/inspector/tests/utils/transactions-changes.test.ts +102 -0
  105. package/src/inspector/ui/icons/add-icon.tsx +3 -3
  106. package/src/inspector/utils/history.ts +6 -6
  107. package/src/inspector/utils/transactions-changes.ts +37 -3
  108. package/src/inspector/viewer/history-view.tsx +13 -13
  109. package/src/react/provider.tsx +6 -1
  110. package/src/react-core/hooks.ts +2 -2
  111. package/src/react-core/tests/useSuspenseCoState.test.tsx +47 -0
  112. package/src/react-native-core/ReactNativeSessionProvider.ts +52 -0
  113. package/src/react-native-core/platform.ts +5 -30
  114. package/src/react-native-core/provider.tsx +6 -1
  115. package/src/react-native-core/tests/ReactNativeSessionProvider.test.ts +124 -0
  116. package/src/tools/coValues/account.ts +4 -0
  117. package/src/tools/coValues/coFeed.ts +8 -3
  118. package/src/tools/coValues/coList.ts +6 -3
  119. package/src/tools/coValues/coMap.ts +10 -0
  120. package/src/tools/coValues/coVector.ts +2 -1
  121. package/src/tools/coValues/group.ts +6 -4
  122. package/src/tools/coValues/interfaces.ts +19 -7
  123. package/src/tools/exports.ts +3 -1
  124. package/src/tools/implementation/ContextManager.ts +10 -0
  125. package/src/tools/implementation/createContext.ts +43 -15
  126. package/src/tools/implementation/schema.ts +23 -13
  127. package/src/tools/implementation/schemaUtils.ts +1 -1
  128. package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +105 -4
  129. package/src/tools/implementation/zodSchema/schemaPermissions.ts +188 -0
  130. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +46 -3
  131. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +46 -3
  132. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +50 -13
  133. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +14 -0
  134. package/src/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.ts +24 -1
  135. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +51 -4
  136. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +25 -1
  137. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +21 -1
  138. package/src/tools/implementation/zodSchema/unionUtils.ts +72 -20
  139. package/src/tools/internal.ts +1 -0
  140. package/src/tools/subscribe/SubscriptionScope.ts +61 -39
  141. package/src/tools/testing.ts +3 -1
  142. package/src/tools/tests/ContextManager.test.ts +2 -1
  143. package/src/tools/tests/coPlainText.test.ts +2 -2
  144. package/src/tools/tests/createContext.test.ts +79 -1
  145. package/src/tools/tests/deepLoading.test.ts +25 -2
  146. package/src/tools/tests/schema.resolved.test.ts +10 -0
  147. package/src/tools/tests/schema.withPermissions.test.ts +859 -0
  148. package/src/tools/tests/utils.ts +2 -2
  149. package/src/worker/index.ts +2 -2
  150. package/dist/chunk-HX5S6W5E.js.map +0 -1
  151. package/dist/inspector/chunk-C6BJPHBQ.js.map +0 -1
@@ -377,10 +377,14 @@ var _CoMap = class _CoMap extends CoValueBase {
377
377
  if (initValue != null) {
378
378
  let refId = initValue.$jazz?.id;
379
379
  if (!refId) {
380
+ const newOwnerStrategy = descriptor.permissions?.newInlineOwnerStrategy;
381
+ const onCreate = descriptor.permissions?.onCreate;
380
382
  const coValue = instantiateRefEncodedWithInit(
381
383
  descriptor,
382
384
  initValue,
383
- owner
385
+ owner,
386
+ newOwnerStrategy,
387
+ onCreate
384
388
  );
385
389
  refId = coValue.$jazz.id;
386
390
  }
@@ -586,10 +590,14 @@ var CoMapJazzApi = class extends CoValueJazzApi {
586
590
  this.raw.set(key, null);
587
591
  } else {
588
592
  if (!refId) {
593
+ const newOwnerStrategy = descriptor.permissions?.newInlineOwnerStrategy;
594
+ const onCreate = descriptor.permissions?.onCreate;
589
595
  const coValue = instantiateRefEncodedWithInit(
590
596
  descriptor,
591
597
  value,
592
- this.owner
598
+ this.owner,
599
+ newOwnerStrategy,
600
+ onCreate
593
601
  );
594
602
  refId = coValue.$jazz.id;
595
603
  }
@@ -939,8 +947,8 @@ var _CoList = class _CoList extends Array {
939
947
  * @deprecated Use `co.list(...).create` instead.
940
948
  **/
941
949
  static create(items, options) {
942
- const { owner, uniqueness } = parseCoValueCreateOptions(options);
943
950
  const instance = new this();
951
+ const { owner, uniqueness } = parseCoValueCreateOptions(options);
944
952
  Object.defineProperties(instance, {
945
953
  $jazz: {
946
954
  value: new CoListJazzApi(instance, () => raw),
@@ -1431,10 +1439,14 @@ function toRawItems(items, itemDescriptor, owner) {
1431
1439
  }
1432
1440
  let refId = value.$jazz?.id;
1433
1441
  if (!refId) {
1442
+ const newOwnerStrategy = itemDescriptor.permissions?.newInlineOwnerStrategy;
1443
+ const onCreate = itemDescriptor.permissions?.onCreate;
1434
1444
  const coValue = instantiateRefEncodedWithInit(
1435
1445
  itemDescriptor,
1436
1446
  value,
1437
- owner
1447
+ owner,
1448
+ newOwnerStrategy,
1449
+ onCreate
1438
1450
  );
1439
1451
  refId = coValue.$jazz.id;
1440
1452
  }
@@ -1727,10 +1739,14 @@ var CoFeedJazzApi = class extends CoValueJazzApi {
1727
1739
  } else if (isRefEncoded(itemDescriptor)) {
1728
1740
  let refId = item.$jazz?.id;
1729
1741
  if (!refId) {
1742
+ const newOwnerStrategy = itemDescriptor.permissions?.newInlineOwnerStrategy;
1743
+ const onCreate = itemDescriptor.permissions?.onCreate;
1730
1744
  const coValue = instantiateRefEncodedWithInit(
1731
1745
  itemDescriptor,
1732
1746
  item,
1733
- this.owner
1747
+ this.owner,
1748
+ newOwnerStrategy,
1749
+ onCreate
1734
1750
  );
1735
1751
  refId = coValue.$jazz.id;
1736
1752
  }
@@ -1940,6 +1956,7 @@ var FileStream = class extends CoValueBase {
1940
1956
  *
1941
1957
  * @param options - Configuration options for the new FileStream
1942
1958
  * @param options.owner - The Account or Group that will own this FileStream and control access rights
1959
+ * @param schemaConfiguration - Internal schema configuration
1943
1960
  *
1944
1961
  * @example
1945
1962
  * ```typescript
@@ -1960,7 +1977,8 @@ var FileStream = class extends CoValueBase {
1960
1977
  * @deprecated Use `co.fileStream(...).create` instead.
1961
1978
  */
1962
1979
  static create(options) {
1963
- return new this(parseCoValueCreateOptions(options));
1980
+ const { owner } = parseCoValueCreateOptions(options);
1981
+ return new this({ owner });
1964
1982
  }
1965
1983
  getMetadata() {
1966
1984
  return this.$jazz.raw.getBinaryStreamInfo();
@@ -2416,10 +2434,14 @@ var AccountJazzApi = class extends CoValueJazzApi {
2416
2434
  let refId = value.$jazz?.id;
2417
2435
  if (!refId) {
2418
2436
  const descriptor = this.schema[key];
2437
+ const newOwnerStrategy = descriptor.permissions?.newInlineOwnerStrategy;
2438
+ const onCreate = descriptor.permissions?.onCreate;
2419
2439
  const coValue = instantiateRefEncodedWithInit(
2420
2440
  descriptor,
2421
2441
  value,
2422
- accountOrGroupToGroup(this.account)
2442
+ accountOrGroupToGroup(this.account),
2443
+ newOwnerStrategy,
2444
+ onCreate
2423
2445
  );
2424
2446
  refId = coValue.$jazz.id;
2425
2447
  }
@@ -3438,7 +3460,8 @@ var _CoVector = class _CoVector extends Float32Array {
3438
3460
  `Vector dimension mismatch! Expected ${this.requiredDimensionsCount} dimensions, got ${givenVectorDimensions}`
3439
3461
  );
3440
3462
  }
3441
- const coVector = new this(parseCoValueCreateOptions(options));
3463
+ const { owner } = parseCoValueCreateOptions(options);
3464
+ const coVector = new this({ owner });
3442
3465
  coVector.setVectorData(vectorAsFloat32Array);
3443
3466
  const byteArray = _CoVector.toByteArray(vectorAsFloat32Array);
3444
3467
  coVector.$jazz.raw.startBinaryStream({
@@ -3886,14 +3909,15 @@ var coField = {
3886
3909
  items: ItemsSym,
3887
3910
  optional: optional2
3888
3911
  };
3889
- function optionalRef(arg) {
3890
- return ref(arg, { optional: true });
3912
+ function optionalRef(arg, options) {
3913
+ return ref(arg, { optional: true, permissions: options.permissions });
3891
3914
  }
3892
3915
  function ref(arg, options) {
3893
3916
  return {
3894
3917
  [SchemaInit]: {
3895
3918
  ref: arg,
3896
- optional: options?.optional || false
3919
+ optional: options.optional || false,
3920
+ permissions: options.permissions
3897
3921
  }
3898
3922
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
3899
3923
  };
@@ -3906,16 +3930,14 @@ function instantiateRefEncodedFromRaw(schema, raw) {
3906
3930
  raw
3907
3931
  ).fromRaw(raw);
3908
3932
  }
3909
- function instantiateRefEncodedWithInit(schema, init, parentOwner) {
3933
+ function instantiateRefEncodedWithInit(schema, init, containerOwner, newOwnerStrategy = extendContainerOwner, onCreate) {
3910
3934
  if (!isCoValueClass(schema.ref)) {
3911
3935
  throw Error(
3912
3936
  `Cannot automatically create CoValue from value: ${JSON.stringify(init)}. Use the CoValue schema's create() method instead.`
3913
3937
  );
3914
3938
  }
3915
- const node = parentOwner.$jazz.localNode;
3916
- const rawGroup = node.createGroup();
3917
- const owner = new Group2({ fromRaw: rawGroup });
3918
- owner.addMember(parentOwner);
3939
+ const owner = newOwnerStrategy(() => Group2.create(), containerOwner, init);
3940
+ onCreate?.(owner, init);
3919
3941
  return schema.ref.create(init, owner);
3920
3942
  }
3921
3943
 
@@ -4421,14 +4443,6 @@ var SubscriptionScope = class _SubscriptionScope {
4421
4443
  if (this.value.type !== CoValueLoadingState.LOADED) return true;
4422
4444
  return this.pendingLoadedChildren.size === 0;
4423
4445
  }
4424
- cachePromise(value, callback) {
4425
- if (this.lastPromise?.value === value) {
4426
- return this.lastPromise.promise;
4427
- }
4428
- const promise = callback();
4429
- this.lastPromise = { value, promise };
4430
- return promise;
4431
- }
4432
4446
  getPromise() {
4433
4447
  const currentValue = this.getCurrentValue();
4434
4448
  if (currentValue.$isLoaded) {
@@ -4442,26 +4456,57 @@ var SubscriptionScope = class _SubscriptionScope {
4442
4456
  })
4443
4457
  );
4444
4458
  }
4445
- return this.cachePromise(currentValue, () => {
4446
- return new Promise((resolve, reject) => {
4447
- const unsubscribe = this.subscribe(() => {
4448
- const currentValue2 = this.getCurrentValue();
4449
- if (currentValue2.$jazz.loadingState === CoValueLoadingState.LOADING) {
4450
- return;
4451
- }
4452
- if (currentValue2.$isLoaded) {
4453
- resolve(currentValue2);
4454
- } else {
4455
- reject(
4456
- new Error(this.getError()?.toString() ?? "Unknown error", {
4457
- cause: this.callerStack
4458
- })
4459
- );
4460
- }
4461
- unsubscribe();
4462
- });
4459
+ const promise = new Promise((resolve, reject) => {
4460
+ const unsubscribe = this.subscribe(() => {
4461
+ const currentValue2 = this.getCurrentValue();
4462
+ if (currentValue2.$jazz.loadingState === CoValueLoadingState.LOADING) {
4463
+ return;
4464
+ }
4465
+ if (currentValue2.$isLoaded) {
4466
+ promise.status = "fulfilled";
4467
+ promise.value = currentValue2;
4468
+ resolve(currentValue2);
4469
+ } else {
4470
+ promise.status = "rejected";
4471
+ promise.reason = new Error(
4472
+ this.getError()?.toString() ?? "Unknown error",
4473
+ {
4474
+ cause: this.callerStack
4475
+ }
4476
+ );
4477
+ reject(
4478
+ new Error(this.getError()?.toString() ?? "Unknown error", {
4479
+ cause: this.callerStack
4480
+ })
4481
+ );
4482
+ }
4483
+ unsubscribe();
4463
4484
  });
4464
4485
  });
4486
+ promise.status = "pending";
4487
+ return promise;
4488
+ }
4489
+ getCachedPromise() {
4490
+ if (this.lastPromise) {
4491
+ const value = this.getCurrentValue();
4492
+ if (value.$isLoaded) {
4493
+ this.lastPromise.status = "fulfilled";
4494
+ this.lastPromise.value = value;
4495
+ } else if (value.$jazz.loadingState !== CoValueLoadingState.LOADING) {
4496
+ this.lastPromise.status = "rejected";
4497
+ this.lastPromise.reason = new Error(
4498
+ this.getError()?.toString() ?? "Unknown error",
4499
+ {
4500
+ cause: this.callerStack
4501
+ }
4502
+ );
4503
+ } else if (this.lastPromise.status !== "pending") {
4504
+ this.lastPromise = this.getPromise();
4505
+ }
4506
+ } else {
4507
+ this.lastPromise = this.getPromise();
4508
+ }
4509
+ return this.lastPromise;
4465
4510
  }
4466
4511
  getUnloadedValue(reason) {
4467
4512
  if (this.unloadedValue?.$jazz.loadingState === reason) {
@@ -4926,13 +4971,23 @@ function accessChildById(parent, childId, schema) {
4926
4971
  import {
4927
4972
  LocalNode as LocalNode5
4928
4973
  } from "cojson";
4929
- async function randomSessionProvider(accountID, crypto) {
4930
- return {
4931
- sessionID: crypto.newRandomSessionID(accountID),
4932
- sessionDone: () => {
4933
- }
4934
- };
4935
- }
4974
+ var MockSessionProvider = class {
4975
+ async acquireSession(accountID, crypto) {
4976
+ return {
4977
+ sessionID: crypto.newRandomSessionID(
4978
+ accountID
4979
+ ),
4980
+ sessionDone: () => {
4981
+ }
4982
+ };
4983
+ }
4984
+ async persistSession(_accountID, _sessionID) {
4985
+ return {
4986
+ sessionDone: () => {
4987
+ }
4988
+ };
4989
+ }
4990
+ };
4936
4991
  async function createJazzContextFromExistingCredentials({
4937
4992
  credentials,
4938
4993
  peers,
@@ -4943,7 +4998,7 @@ async function createJazzContextFromExistingCredentials({
4943
4998
  onLogOut,
4944
4999
  asActiveAccount
4945
5000
  }) {
4946
- const { sessionID, sessionDone } = await sessionProvider(
5001
+ const { sessionID, sessionDone } = await sessionProvider.acquireSession(
4947
5002
  credentials.accountID,
4948
5003
  crypto
4949
5004
  );
@@ -4989,7 +5044,8 @@ async function createJazzContextForNewAccount({
4989
5044
  crypto,
4990
5045
  AccountSchema: PropsAccountSchema,
4991
5046
  onLogOut,
4992
- storage
5047
+ storage,
5048
+ sessionProvider
4993
5049
  }) {
4994
5050
  const CurrentAccountSchema = PropsAccountSchema ?? RegisteredSchemas["Account"];
4995
5051
  const AccountClass = coValueClassFromCoValueClassOrSchema(CurrentAccountSchema);
@@ -5007,11 +5063,16 @@ async function createJazzContextForNewAccount({
5007
5063
  });
5008
5064
  const account = AccountClass.fromNode(node);
5009
5065
  activeAccountContext3.set(account);
5066
+ const { sessionDone } = await sessionProvider.persistSession(
5067
+ account.$jazz.id,
5068
+ node.currentSessionID
5069
+ );
5010
5070
  return {
5011
5071
  node,
5012
5072
  account,
5013
5073
  done: () => {
5014
5074
  node.gracefulShutdown();
5075
+ sessionDone();
5015
5076
  },
5016
5077
  logOut: async () => {
5017
5078
  node.gracefulShutdown();
@@ -5053,6 +5114,7 @@ async function createJazzContext(options) {
5053
5114
  peers: options.peers,
5054
5115
  crypto,
5055
5116
  AccountSchema: options.AccountSchema,
5117
+ sessionProvider: options.sessionProvider,
5056
5118
  onLogOut: async () => {
5057
5119
  await authSecretStorage.clearWithoutNotify();
5058
5120
  },
@@ -5198,6 +5260,74 @@ var CoOptionalSchema = class {
5198
5260
  }
5199
5261
  };
5200
5262
 
5263
+ // src/tools/implementation/zodSchema/schemaPermissions.ts
5264
+ var DEFAULT_SCHEMA_PERMISSIONS = {
5265
+ default: () => Group2.create(),
5266
+ onInlineCreate: "extendsContainer"
5267
+ };
5268
+ function setDefaultSchemaPermissions(permissions) {
5269
+ DEFAULT_SCHEMA_PERMISSIONS = {
5270
+ ...DEFAULT_SCHEMA_PERMISSIONS,
5271
+ ...permissions
5272
+ };
5273
+ }
5274
+ var extendContainerOwnerFactory = (roleOverride) => (createNewGroup, containerOwner) => {
5275
+ const node = containerOwner.$jazz.localNode;
5276
+ const rawGroup = node.createGroup();
5277
+ const owner = new Group2({ fromRaw: rawGroup });
5278
+ owner.addMember(containerOwner, roleOverride);
5279
+ return owner;
5280
+ };
5281
+ var extendContainerOwner = extendContainerOwnerFactory();
5282
+ function schemaToRefPermissions(permissions) {
5283
+ const newInlineOwnerStrategy = parseOnInlineCreate(
5284
+ permissions.onInlineCreate
5285
+ );
5286
+ const onCreate = permissions.onCreate ? (newGroup, _init) => permissions.onCreate?.(newGroup) : void 0;
5287
+ return {
5288
+ newInlineOwnerStrategy,
5289
+ onCreate
5290
+ };
5291
+ }
5292
+ function parseOnInlineCreate(onInlineCreate) {
5293
+ if (!onInlineCreate || onInlineCreate === "extendsContainer") {
5294
+ return extendContainerOwner;
5295
+ }
5296
+ if (typeof onInlineCreate === "object" && "extendsContainer" in onInlineCreate) {
5297
+ return extendContainerOwnerFactory(onInlineCreate.extendsContainer);
5298
+ }
5299
+ if (onInlineCreate === "newGroup") {
5300
+ return (createNewGroup) => createNewGroup();
5301
+ }
5302
+ if (onInlineCreate === "sameAsContainer") {
5303
+ return (_createNewGroup, containerOwner) => containerOwner;
5304
+ }
5305
+ return (createNewGroup, containerOwner) => {
5306
+ const newGroup = createNewGroup();
5307
+ onInlineCreate(newGroup, { containerOwner });
5308
+ return newGroup;
5309
+ };
5310
+ }
5311
+ function getDefaultRefPermissions() {
5312
+ return schemaToRefPermissions(DEFAULT_SCHEMA_PERMISSIONS);
5313
+ }
5314
+ function withSchemaPermissions(options, schemaPermissions) {
5315
+ const onCreate = schemaPermissions?.onCreate;
5316
+ if (!options) {
5317
+ const owner2 = schemaPermissions?.default?.() ?? Group2.create();
5318
+ return { owner: owner2, onCreate };
5319
+ }
5320
+ if (TypeSym in options) {
5321
+ return { owner: options, onCreate };
5322
+ }
5323
+ const owner = options.owner ?? schemaPermissions?.default?.() ?? Group2.create();
5324
+ return {
5325
+ ...options,
5326
+ owner,
5327
+ onCreate
5328
+ };
5329
+ }
5330
+
5201
5331
  // src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts
5202
5332
  function createCoreCoRichTextSchema() {
5203
5333
  return {
@@ -5206,15 +5336,20 @@ function createCoreCoRichTextSchema() {
5206
5336
  resolveQuery: true
5207
5337
  };
5208
5338
  }
5209
- var RichTextSchema = class {
5339
+ var RichTextSchema = class _RichTextSchema {
5210
5340
  constructor(coValueClass) {
5211
5341
  this.coValueClass = coValueClass;
5212
5342
  this.collaborative = true;
5213
5343
  this.builtin = "CoRichText";
5214
5344
  this.resolveQuery = true;
5345
+ this.permissions = DEFAULT_SCHEMA_PERMISSIONS;
5215
5346
  }
5216
5347
  create(text, options) {
5217
- return this.coValueClass.create(text, options);
5348
+ const optionsWithPermissions = withSchemaPermissions(
5349
+ options,
5350
+ this.permissions
5351
+ );
5352
+ return this.coValueClass.create(text, optionsWithPermissions);
5218
5353
  }
5219
5354
  load(id, options) {
5220
5355
  return this.coValueClass.load(id, options);
@@ -5231,6 +5366,14 @@ var RichTextSchema = class {
5231
5366
  optional() {
5232
5367
  return coOptionalDefiner(this);
5233
5368
  }
5369
+ /**
5370
+ * Configure permissions to be used when creating or composing CoValues
5371
+ */
5372
+ withPermissions(permissions) {
5373
+ const copy = new _RichTextSchema(this.coValueClass);
5374
+ copy.permissions = permissions;
5375
+ return copy;
5376
+ }
5234
5377
  };
5235
5378
 
5236
5379
  // src/tools/implementation/zodSchema/schemaTypes/GroupSchema.ts
@@ -5405,7 +5548,7 @@ __export(coExport_exports, {
5405
5548
  });
5406
5549
 
5407
5550
  // src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts
5408
- var CoMapSchema = class _CoMapSchema {
5551
+ var CoMapSchema = class {
5409
5552
  constructor(coreSchema, coValueClass) {
5410
5553
  this.coValueClass = coValueClass;
5411
5554
  this.collaborative = true;
@@ -5416,12 +5559,21 @@ var CoMapSchema = class _CoMapSchema {
5416
5559
  * @default true
5417
5560
  */
5418
5561
  this.resolveQuery = true;
5562
+ /**
5563
+ * Permissions to be used when creating or composing CoValues
5564
+ * @internal
5565
+ */
5566
+ this.permissions = DEFAULT_SCHEMA_PERMISSIONS;
5419
5567
  this.shape = coreSchema.shape;
5420
5568
  this.catchAll = coreSchema.catchAll;
5421
5569
  this.getDefinition = coreSchema.getDefinition;
5422
5570
  }
5423
- create(...args) {
5424
- return this.coValueClass.create(...args);
5571
+ create(init, options) {
5572
+ const optionsWithPermissions = withSchemaPermissions(
5573
+ options,
5574
+ this.permissions
5575
+ );
5576
+ return this.coValueClass.create(init, optionsWithPermissions);
5425
5577
  }
5426
5578
  load(id, options) {
5427
5579
  return this.coValueClass.load(
@@ -5543,15 +5695,26 @@ var CoMapSchema = class _CoMapSchema {
5543
5695
  * This resolve query will be used when no resolve query is provided to the load method.
5544
5696
  */
5545
5697
  resolved(resolveQuery) {
5546
- const coreSchema = createCoreCoMapSchema(
5547
- this.shape,
5548
- this.catchAll
5549
- );
5550
- const copy = new _CoMapSchema(
5551
- coreSchema,
5552
- this.coValueClass
5553
- );
5554
- copy.resolveQuery = resolveQuery;
5698
+ return this.copy({ resolveQuery });
5699
+ }
5700
+ /**
5701
+ * Configure permissions to be used when creating or composing CoValues
5702
+ */
5703
+ withPermissions(permissions) {
5704
+ return this.copy({ permissions });
5705
+ }
5706
+ /**
5707
+ * Creates a copy of this schema, preserving all previous configuration
5708
+ */
5709
+ copy({
5710
+ permissions,
5711
+ resolveQuery
5712
+ }) {
5713
+ const coreSchema = createCoreCoMapSchema(this.shape, this.catchAll);
5714
+ const copy = hydrateCoreCoValueSchema(coreSchema);
5715
+ copy.coValueClass.prototype.migrate = this.coValueClass.prototype.migrate;
5716
+ copy.resolveQuery = resolveQuery ?? this.resolveQuery;
5717
+ copy.permissions = permissions ?? this.permissions;
5555
5718
  return copy;
5556
5719
  }
5557
5720
  };
@@ -5588,7 +5751,7 @@ function createCoreCoMapSchema(shape, catchAll) {
5588
5751
  }
5589
5752
 
5590
5753
  // src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts
5591
- var CoListSchema = class _CoListSchema {
5754
+ var CoListSchema = class {
5592
5755
  constructor(element, coValueClass) {
5593
5756
  this.element = element;
5594
5757
  this.coValueClass = coValueClass;
@@ -5600,9 +5763,21 @@ var CoListSchema = class _CoListSchema {
5600
5763
  * @default true
5601
5764
  */
5602
5765
  this.resolveQuery = true;
5766
+ /**
5767
+ * Permissions to be used when creating or composing CoValues
5768
+ * @internal
5769
+ */
5770
+ this.permissions = DEFAULT_SCHEMA_PERMISSIONS;
5603
5771
  }
5604
5772
  create(items, options) {
5605
- return this.coValueClass.create(items, options);
5773
+ const optionsWithPermissions = withSchemaPermissions(
5774
+ options,
5775
+ this.permissions
5776
+ );
5777
+ return this.coValueClass.create(
5778
+ items,
5779
+ optionsWithPermissions
5780
+ );
5606
5781
  }
5607
5782
  load(id, options) {
5608
5783
  return this.coValueClass.load(
@@ -5655,8 +5830,22 @@ var CoListSchema = class _CoListSchema {
5655
5830
  * This resolve query will be used when no resolve query is provided to the load method.
5656
5831
  */
5657
5832
  resolved(resolveQuery) {
5658
- const copy = new _CoListSchema(this.element, this.coValueClass);
5659
- copy.resolveQuery = resolveQuery;
5833
+ return this.copy({ resolveQuery });
5834
+ }
5835
+ /**
5836
+ * Configure permissions to be used when creating or composing CoValues
5837
+ */
5838
+ withPermissions(permissions) {
5839
+ return this.copy({ permissions });
5840
+ }
5841
+ copy({
5842
+ permissions,
5843
+ resolveQuery
5844
+ }) {
5845
+ const coreSchema = createCoreCoListSchema(this.element);
5846
+ const copy = hydrateCoreCoValueSchema(coreSchema);
5847
+ copy.resolveQuery = resolveQuery ?? this.resolveQuery;
5848
+ copy.permissions = permissions ?? this.permissions;
5660
5849
  return copy;
5661
5850
  }
5662
5851
  };
@@ -5670,7 +5859,7 @@ function createCoreCoListSchema(element) {
5670
5859
  }
5671
5860
 
5672
5861
  // src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts
5673
- var CoFeedSchema = class _CoFeedSchema {
5862
+ var CoFeedSchema = class {
5674
5863
  constructor(element, coValueClass) {
5675
5864
  this.element = element;
5676
5865
  this.coValueClass = coValueClass;
@@ -5682,9 +5871,21 @@ var CoFeedSchema = class _CoFeedSchema {
5682
5871
  * @default true
5683
5872
  */
5684
5873
  this.resolveQuery = true;
5874
+ /**
5875
+ * Permissions to be used when creating or composing CoValues
5876
+ * @internal
5877
+ */
5878
+ this.permissions = DEFAULT_SCHEMA_PERMISSIONS;
5685
5879
  }
5686
5880
  create(init, options) {
5687
- return this.coValueClass.create(init, options);
5881
+ const optionsWithPermissions = withSchemaPermissions(
5882
+ options,
5883
+ this.permissions
5884
+ );
5885
+ return this.coValueClass.create(
5886
+ init,
5887
+ optionsWithPermissions
5888
+ );
5688
5889
  }
5689
5890
  load(id, options) {
5690
5891
  return this.coValueClass.load(
@@ -5721,8 +5922,22 @@ var CoFeedSchema = class _CoFeedSchema {
5721
5922
  * This resolve query will be used when no resolve query is provided to the load method.
5722
5923
  */
5723
5924
  resolved(resolveQuery) {
5724
- const copy = new _CoFeedSchema(this.element, this.coValueClass);
5725
- copy.resolveQuery = resolveQuery;
5925
+ return this.copy({ resolveQuery });
5926
+ }
5927
+ /**
5928
+ * Configure permissions to be used when creating or composing CoValues
5929
+ */
5930
+ withPermissions(permissions) {
5931
+ return this.copy({ permissions });
5932
+ }
5933
+ copy({
5934
+ permissions,
5935
+ resolveQuery
5936
+ }) {
5937
+ const coreSchema = createCoreCoFeedSchema(this.element);
5938
+ const copy = hydrateCoreCoValueSchema(coreSchema);
5939
+ copy.resolveQuery = resolveQuery ?? this.resolveQuery;
5940
+ copy.permissions = permissions ?? this.permissions;
5726
5941
  return copy;
5727
5942
  }
5728
5943
  };
@@ -5743,15 +5958,24 @@ function createCoreCoPlainTextSchema() {
5743
5958
  resolveQuery: true
5744
5959
  };
5745
5960
  }
5746
- var PlainTextSchema = class {
5961
+ var PlainTextSchema = class _PlainTextSchema {
5747
5962
  constructor(coValueClass) {
5748
5963
  this.coValueClass = coValueClass;
5749
5964
  this.collaborative = true;
5750
5965
  this.builtin = "CoPlainText";
5751
5966
  this.resolveQuery = true;
5967
+ /**
5968
+ * Permissions to be used when creating or composing CoValues
5969
+ * @internal
5970
+ */
5971
+ this.permissions = DEFAULT_SCHEMA_PERMISSIONS;
5752
5972
  }
5753
5973
  create(text, options) {
5754
- return this.coValueClass.create(text, options);
5974
+ const optionsWithPermissions = withSchemaPermissions(
5975
+ options,
5976
+ this.permissions
5977
+ );
5978
+ return this.coValueClass.create(text, optionsWithPermissions);
5755
5979
  }
5756
5980
  load(id, options) {
5757
5981
  return this.coValueClass.load(id, options);
@@ -5771,6 +5995,14 @@ var PlainTextSchema = class {
5771
5995
  optional() {
5772
5996
  return coOptionalDefiner(this);
5773
5997
  }
5998
+ /**
5999
+ * Configure permissions to be used when creating or composing CoValues
6000
+ */
6001
+ withPermissions(permissions) {
6002
+ const copy = new _PlainTextSchema(this.coValueClass);
6003
+ copy.permissions = permissions;
6004
+ return copy;
6005
+ }
5774
6006
  };
5775
6007
 
5776
6008
  // src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts
@@ -5781,21 +6013,42 @@ function createCoreFileStreamSchema() {
5781
6013
  resolveQuery: true
5782
6014
  };
5783
6015
  }
5784
- var FileStreamSchema = class {
6016
+ var FileStreamSchema = class _FileStreamSchema {
5785
6017
  constructor(coValueClass) {
5786
6018
  this.coValueClass = coValueClass;
5787
6019
  this.collaborative = true;
5788
6020
  this.builtin = "FileStream";
5789
6021
  this.resolveQuery = true;
6022
+ /**
6023
+ * Permissions to be used when creating or composing CoValues
6024
+ */
6025
+ this.permissions = DEFAULT_SCHEMA_PERMISSIONS;
5790
6026
  }
5791
6027
  create(options) {
5792
- return this.coValueClass.create(options);
6028
+ const optionsWithPermissions = withSchemaPermissions(
6029
+ options,
6030
+ this.permissions
6031
+ );
6032
+ return this.coValueClass.create(optionsWithPermissions);
5793
6033
  }
5794
6034
  createFromBlob(blob, options) {
5795
- return this.coValueClass.createFromBlob(blob, options);
6035
+ const optionsWithPermissions = withSchemaPermissions(
6036
+ options,
6037
+ this.permissions
6038
+ );
6039
+ return this.coValueClass.createFromBlob(blob, optionsWithPermissions);
5796
6040
  }
5797
- createFromArrayBuffer(...args) {
5798
- return this.coValueClass.createFromArrayBuffer(...args);
6041
+ createFromArrayBuffer(arrayBuffer, mimeType, fileName, options) {
6042
+ const optionsWithPermissions = withSchemaPermissions(
6043
+ options,
6044
+ this.permissions
6045
+ );
6046
+ return this.coValueClass.createFromArrayBuffer(
6047
+ arrayBuffer,
6048
+ mimeType,
6049
+ fileName,
6050
+ optionsWithPermissions
6051
+ );
5799
6052
  }
5800
6053
  loadAsBlob(id, options) {
5801
6054
  return this.coValueClass.loadAsBlob(id, options);
@@ -5815,6 +6068,14 @@ var FileStreamSchema = class {
5815
6068
  optional() {
5816
6069
  return coOptionalDefiner(this);
5817
6070
  }
6071
+ /**
6072
+ * Configure permissions to be used when creating or composing CoValues
6073
+ */
6074
+ withPermissions(permissions) {
6075
+ const copy = new _FileStreamSchema(this.coValueClass);
6076
+ copy.permissions = permissions;
6077
+ return copy;
6078
+ }
5818
6079
  };
5819
6080
 
5820
6081
  // src/tools/implementation/zodSchema/schemaTypes/CoVectorSchema.ts
@@ -5826,16 +6087,24 @@ function createCoreCoVectorSchema(dimensions) {
5826
6087
  resolveQuery: true
5827
6088
  };
5828
6089
  }
5829
- var CoVectorSchema = class {
6090
+ var CoVectorSchema = class _CoVectorSchema {
5830
6091
  constructor(dimensions, coValueClass) {
5831
6092
  this.dimensions = dimensions;
5832
6093
  this.coValueClass = coValueClass;
5833
6094
  this.collaborative = true;
5834
6095
  this.builtin = "CoVector";
5835
6096
  this.resolveQuery = true;
6097
+ /**
6098
+ * Permissions to be used when creating or composing CoValues
6099
+ */
6100
+ this.permissions = DEFAULT_SCHEMA_PERMISSIONS;
5836
6101
  }
5837
6102
  create(vector, options) {
5838
- return this.coValueClass.create(vector, options);
6103
+ const optionsWithPermissions = withSchemaPermissions(
6104
+ options,
6105
+ this.permissions
6106
+ );
6107
+ return this.coValueClass.create(vector, optionsWithPermissions);
5839
6108
  }
5840
6109
  /**
5841
6110
  * Load a `CoVector` with a given ID.
@@ -5852,6 +6121,14 @@ var CoVectorSchema = class {
5852
6121
  optional() {
5853
6122
  return coOptionalDefiner(this);
5854
6123
  }
6124
+ /**
6125
+ * Configure permissions to be used when creating or composing CoValues
6126
+ */
6127
+ withPermissions(permissions) {
6128
+ const copy = new _CoVectorSchema(this.dimensions, this.coValueClass);
6129
+ copy.permissions = permissions;
6130
+ return copy;
6131
+ }
5855
6132
  };
5856
6133
 
5857
6134
  // src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts
@@ -5935,6 +6212,7 @@ function createCoreAccountSchema(shape) {
5935
6212
  }
5936
6213
 
5937
6214
  // src/tools/implementation/zodSchema/unionUtils.ts
6215
+ import { RawCoMap as RawCoMap3 } from "cojson";
5938
6216
  function schemaUnionDiscriminatorFor(schema) {
5939
6217
  if (isUnionOfCoMapsDeeply(schema)) {
5940
6218
  const definition = schema.getDefinition();
@@ -5952,22 +6230,7 @@ function schemaUnionDiscriminatorFor(schema) {
5952
6230
  );
5953
6231
  }
5954
6232
  }
5955
- const availableOptions = [];
5956
- for (const option of options) {
5957
- if (option.builtin === "CoMap") {
5958
- availableOptions.push(option);
5959
- } else if (option.builtin === "CoDiscriminatedUnion") {
5960
- for (const subOption of option.getDefinition().options) {
5961
- if (!options.includes(subOption)) {
5962
- options.push(subOption);
5963
- }
5964
- }
5965
- } else {
5966
- throw new Error(
5967
- "Unsupported zod type in co.discriminatedUnion() of collaborative types"
5968
- );
5969
- }
5970
- }
6233
+ const availableOptions = getFlattenedUnionOptions(schema);
5971
6234
  const determineSchema = (discriminable) => {
5972
6235
  const allNestedRefKeys = /* @__PURE__ */ new Set();
5973
6236
  for (const option of availableOptions) {
@@ -5983,7 +6246,10 @@ function schemaUnionDiscriminatorFor(schema) {
5983
6246
  const optionDef = option.getDefinition();
5984
6247
  for (const key of Object.keys(discriminatorMap)) {
5985
6248
  const discriminatorDef = optionDef.shape[key];
5986
- const discriminatorValue = discriminable.get(key);
6249
+ const discriminatorValue = resolveDiscriminantValue(
6250
+ discriminable,
6251
+ key
6252
+ );
5987
6253
  if (discriminatorValue && typeof discriminatorValue === "object") {
5988
6254
  throw new Error("Discriminator must be a primitive value");
5989
6255
  }
@@ -6053,6 +6319,49 @@ function isUnionOfPrimitivesDeeply(schema) {
6053
6319
  return !isAnyCoValueSchema2(schema);
6054
6320
  }
6055
6321
  }
6322
+ function getFlattenedUnionOptions(schema) {
6323
+ const definition = schema.getDefinition();
6324
+ const options = definition.options;
6325
+ const availableOptions = [];
6326
+ for (const option of options) {
6327
+ if (option.builtin === "CoMap") {
6328
+ availableOptions.push(option);
6329
+ } else if (option.builtin === "CoDiscriminatedUnion") {
6330
+ const nestedOptions = getFlattenedUnionOptions(
6331
+ option
6332
+ );
6333
+ for (const subOption of nestedOptions) {
6334
+ if (!availableOptions.includes(subOption)) {
6335
+ availableOptions.push(subOption);
6336
+ }
6337
+ }
6338
+ } else {
6339
+ throw new Error(
6340
+ "Unsupported zod type in co.discriminatedUnion() of collaborative types"
6341
+ );
6342
+ }
6343
+ }
6344
+ return availableOptions;
6345
+ }
6346
+ function getDiscriminatorValuesForOption(option, discriminatorKey) {
6347
+ const optionDefinition = option.getDefinition();
6348
+ return optionDefinition.discriminatorMap?.[discriminatorKey];
6349
+ }
6350
+ function resolveDiscriminantValue(init, discriminatorKey) {
6351
+ if (init == null) {
6352
+ return void 0;
6353
+ }
6354
+ if (init instanceof Map || init instanceof RawCoMap3) {
6355
+ return init.get(discriminatorKey);
6356
+ }
6357
+ if (typeof init === "object") {
6358
+ const record2 = init;
6359
+ if (discriminatorKey in record2) {
6360
+ return record2[discriminatorKey];
6361
+ }
6362
+ }
6363
+ return void 0;
6364
+ }
6056
6365
 
6057
6366
  // src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts
6058
6367
  function makeCodecCoField(codec2) {
@@ -6083,17 +6392,28 @@ function schemaFieldToCoFieldDef(schema) {
6083
6392
  return cachedCoFieldDef;
6084
6393
  }
6085
6394
  if (isCoValueClass(schema)) {
6086
- return cacheSchemaField(schema, coField.ref(schema));
6395
+ return cacheSchemaField(
6396
+ schema,
6397
+ coField.ref(schema, {
6398
+ permissions: getDefaultRefPermissions()
6399
+ })
6400
+ );
6087
6401
  } else if (isCoValueSchema(schema)) {
6088
6402
  if (schema.builtin === "CoOptional") {
6089
6403
  return cacheSchemaField(
6090
6404
  schema,
6091
6405
  coField.ref(schema.getCoValueClass(), {
6092
- optional: true
6406
+ optional: true,
6407
+ permissions: schemaFieldPermissions(schema)
6093
6408
  })
6094
6409
  );
6095
6410
  }
6096
- return cacheSchemaField(schema, coField.ref(schema.getCoValueClass()));
6411
+ return cacheSchemaField(
6412
+ schema,
6413
+ coField.ref(schema.getCoValueClass(), {
6414
+ permissions: schemaFieldPermissions(schema)
6415
+ })
6416
+ );
6097
6417
  } else {
6098
6418
  if ("_zod" in schema) {
6099
6419
  const zodSchemaDef = schema._zod.def;
@@ -6199,6 +6519,55 @@ function schemaFieldToCoFieldDef(schema) {
6199
6519
  }
6200
6520
  }
6201
6521
  }
6522
+ function schemaFieldPermissions(schema) {
6523
+ if (schema.builtin === "CoOptional") {
6524
+ return schemaFieldPermissions(schema.innerType);
6525
+ }
6526
+ if (schema.builtin === "CoDiscriminatedUnion") {
6527
+ return discriminatedUnionFieldPermissions(
6528
+ schema
6529
+ );
6530
+ }
6531
+ return "permissions" in schema ? schemaToRefPermissions(schema.permissions) : getDefaultRefPermissions();
6532
+ }
6533
+ function discriminatedUnionFieldPermissions(schema) {
6534
+ const discriminatorKey = schema.getDefinition().discriminator;
6535
+ const allOptions = getFlattenedUnionOptions(schema);
6536
+ const valueToStrategy = /* @__PURE__ */ new Map();
6537
+ for (const option of allOptions) {
6538
+ const optionPermissions = schemaFieldPermissions(option);
6539
+ const discriminatorValues = getDiscriminatorValuesForOption(
6540
+ option,
6541
+ discriminatorKey
6542
+ );
6543
+ if (!discriminatorValues) {
6544
+ continue;
6545
+ }
6546
+ for (const value of discriminatorValues) {
6547
+ if (!valueToStrategy.has(value)) {
6548
+ valueToStrategy.set(value, optionPermissions);
6549
+ }
6550
+ }
6551
+ }
6552
+ const fallbackStrategy = getDefaultRefPermissions();
6553
+ const newInlineOwnerStrategy = (createNewGroup, containerOwner, init) => {
6554
+ const discriminantValue = resolveDiscriminantValue(init, discriminatorKey);
6555
+ const strategy = discriminantValue !== void 0 ? valueToStrategy.get(discriminantValue) : void 0;
6556
+ const effectiveStrategy = strategy ?? fallbackStrategy;
6557
+ return effectiveStrategy.newInlineOwnerStrategy(
6558
+ createNewGroup,
6559
+ containerOwner,
6560
+ init
6561
+ );
6562
+ };
6563
+ const onCreate = (newGroup, init) => {
6564
+ const discriminantValue = resolveDiscriminantValue(init, discriminatorKey);
6565
+ const strategy = discriminantValue !== void 0 ? valueToStrategy.get(discriminantValue) : void 0;
6566
+ const effectiveStrategy = strategy ?? fallbackStrategy;
6567
+ effectiveStrategy.onCreate?.(newGroup, init);
6568
+ };
6569
+ return { newInlineOwnerStrategy, onCreate };
6570
+ }
6202
6571
 
6203
6572
  // src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts
6204
6573
  function isAnyCoValueSchema2(schema) {
@@ -6860,6 +7229,12 @@ var JazzContextManager = class {
6860
7229
  }
6861
7230
  );
6862
7231
  prevContext.node.removeStorage();
7232
+ for (const peer of Object.values(prevContext.node.syncManager.peers)) {
7233
+ if (!peer.closed) {
7234
+ peer.gracefulShutdown();
7235
+ }
7236
+ }
7237
+ prevContext.node.syncManager.peers = {};
6863
7238
  currentContext.node.syncManager.addPeer(prevAccountAsPeer);
6864
7239
  prevContext.node.syncManager.addPeer(currentAccountAsPeer);
6865
7240
  try {
@@ -7133,20 +7508,28 @@ function isAccountInstance(instance) {
7133
7508
  return TypeSym in instance && instance[TypeSym] === "Account";
7134
7509
  }
7135
7510
  function parseCoValueCreateOptions(options) {
7136
- const Group17 = RegisteredSchemas["Group"];
7511
+ const onCreate = options && "onCreate" in options ? options.onCreate : void 0;
7512
+ const Group18 = RegisteredSchemas["Group"];
7137
7513
  if (!options) {
7138
- return { owner: Group17.create(), uniqueness: void 0 };
7514
+ const owner2 = Group18.create();
7515
+ onCreate?.(owner2);
7516
+ return { owner: owner2, uniqueness: void 0 };
7139
7517
  }
7140
7518
  if (TypeSym in options) {
7141
7519
  if (options[TypeSym] === "Account") {
7142
- return { owner: accountOrGroupToGroup(options), uniqueness: void 0 };
7520
+ const owner2 = accountOrGroupToGroup(options);
7521
+ onCreate?.(owner2);
7522
+ return { owner: owner2, uniqueness: void 0 };
7143
7523
  } else if (options[TypeSym] === "Group") {
7524
+ onCreate?.(options);
7144
7525
  return { owner: options, uniqueness: void 0 };
7145
7526
  }
7146
7527
  }
7147
7528
  const uniqueness = options.unique ? { uniqueness: options.unique } : void 0;
7529
+ const owner = options.owner ? accountOrGroupToGroup(options.owner) : Group18.create();
7530
+ onCreate?.(owner);
7148
7531
  const opts = {
7149
- owner: options.owner ? accountOrGroupToGroup(options.owner) : Group17.create(),
7532
+ owner,
7150
7533
  uniqueness
7151
7534
  };
7152
7535
  return opts;
@@ -7383,11 +7766,12 @@ export {
7383
7766
  setCustomErrorReporter,
7384
7767
  captureStack,
7385
7768
  SubscriptionScope,
7386
- randomSessionProvider,
7769
+ MockSessionProvider,
7387
7770
  createJazzContextFromExistingCredentials,
7388
7771
  createJazzContextForNewAccount,
7389
7772
  createJazzContext,
7390
7773
  createAnonymousJazzContext,
7774
+ setDefaultSchemaPermissions,
7391
7775
  coMapDefiner,
7392
7776
  coExport_exports,
7393
7777
  coValueClassFromCoValueClassOrSchema,
@@ -7398,4 +7782,4 @@ export {
7398
7782
  JazzContextManager
7399
7783
  };
7400
7784
  /* istanbul ignore file -- @preserve */
7401
- //# sourceMappingURL=chunk-HX5S6W5E.js.map
7785
+ //# sourceMappingURL=chunk-GAPMDNJY.js.map