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
@@ -0,0 +1,188 @@
1
+ import { Account, Group, TypeSym, type GroupRole } from "../../internal.js";
2
+
3
+ /**
4
+ * Defines how a nested CoValue’s owner is obtained when creating CoValues from JSON.
5
+ *
6
+ * This configuration is not used when using an explicit .create() for nested CoValues.
7
+ * In that case, {@link SchemaPermissions.default} is used.
8
+ */
9
+ export type OnInlineCreateOptions =
10
+ /**
11
+ * Always create a new group for CoValues created inline
12
+ */
13
+ | "newGroup"
14
+ /**
15
+ * Use the same owner as the container CoValue
16
+ */
17
+ | "sameAsContainer"
18
+ /**
19
+ * Create a new group that includes the container CoValue's owner as a member (effectively inheriting
20
+ * all permissions from the container)
21
+ */
22
+ | "extendsContainer"
23
+ /**
24
+ * Similar to "extendsContainer", but allows overriding the role of the container CoValue's owner
25
+ */
26
+ | { extendsContainer: GroupRole }
27
+ /**
28
+ * Create a new group and configure it as needed
29
+ */
30
+ | InlineGroupConfigurationCallback;
31
+
32
+ export type InlineGroupConfigurationCallback = (
33
+ newGroup: Group,
34
+ context: { containerOwner: Group },
35
+ ) => void;
36
+
37
+ export type OnCreateCallback = (newGroup: Group) => void;
38
+
39
+ /**
40
+ * Internal callback type used by RefPermissions that includes init for discriminated union support.
41
+ * @internal
42
+ */
43
+ export type RefOnCreateCallback = (newGroup: Group, init?: unknown) => void;
44
+
45
+ /**
46
+ * Permissions to be used when creating or composing CoValues
47
+ * @param default - default owner to be used when creating a CoValue without providing an explicit owner.
48
+ * @param onInlineCreate - defines how a nested CoValue's owner is obtained when creating CoValues from JSON.
49
+ * @param onCreate - callback that runs every time a CoValue is created. Can be used to configure the CoValue's owner.
50
+ * Runs both when creating CoValues with `.create()` and when creating CoValues from JSON.
51
+ * @default { default: () => Group.create(), onInlineCreate: "extendsContainer" }
52
+ */
53
+ export type SchemaPermissions = {
54
+ /**
55
+ * default owner to be used when creating a CoValue without providing an explicit owner.
56
+ */
57
+ default?: () => Group;
58
+ /**
59
+ * Defines how a nested CoValue's owner is obtained when creating CoValues from JSON.
60
+ */
61
+ onInlineCreate?: OnInlineCreateOptions;
62
+ /**
63
+ * callback that runs every time a CoValue is created. Can be used to configure the CoValue's owner.
64
+ * Runs both when creating CoValues with `.create()` and when creating CoValues from JSON.
65
+ */
66
+ onCreate?: OnCreateCallback;
67
+ };
68
+
69
+ export let DEFAULT_SCHEMA_PERMISSIONS: SchemaPermissions = {
70
+ default: () => Group.create(),
71
+ onInlineCreate: "extendsContainer",
72
+ };
73
+
74
+ /**
75
+ * Update the default schema permissions for all new CoValue schemas.
76
+ * Schemas created before calling this function will not be affected.
77
+ */
78
+ export function setDefaultSchemaPermissions(permissions: SchemaPermissions) {
79
+ DEFAULT_SCHEMA_PERMISSIONS = {
80
+ ...DEFAULT_SCHEMA_PERMISSIONS,
81
+ ...permissions,
82
+ };
83
+ }
84
+
85
+ /**
86
+ * Parsed {@link SchemaPermissions}, used by CoValue classes to set up permissions for referenced CoValues.
87
+ */
88
+ export type RefPermissions = {
89
+ newInlineOwnerStrategy: NewInlineOwnerStrategy;
90
+ onCreate?: RefOnCreateCallback;
91
+ };
92
+
93
+ /**
94
+ * A function that creates a new owner for a new CoValue created inline.
95
+ * @param createNewGroup - A function that creates a new group.
96
+ * @param containerOwner - The owner of the container CoValue.
97
+ * @param init - The value used to create the new CoValue. Necessary to determine the concrete
98
+ * strategy to use in discriminated unions.
99
+ * @returns The new owner.
100
+ */
101
+ export type NewInlineOwnerStrategy = (
102
+ createNewGroup: () => Group,
103
+ containerOwner: Group,
104
+ init?: unknown,
105
+ ) => Group;
106
+
107
+ export const extendContainerOwnerFactory =
108
+ (roleOverride?: GroupRole): NewInlineOwnerStrategy =>
109
+ (createNewGroup: () => Group, containerOwner: Group): Group => {
110
+ const node = containerOwner.$jazz.localNode;
111
+ const rawGroup = node.createGroup();
112
+ const owner = new Group({ fromRaw: rawGroup });
113
+ owner.addMember(containerOwner, roleOverride);
114
+ return owner;
115
+ };
116
+
117
+ /**
118
+ * A function that creates a new owner for a new CoValue by extending the container CoValue's owner
119
+ * (without overriding its role)
120
+ */
121
+ export const extendContainerOwner = extendContainerOwnerFactory();
122
+
123
+ export function schemaToRefPermissions(
124
+ permissions: SchemaPermissions,
125
+ ): RefPermissions {
126
+ const newInlineOwnerStrategy = parseOnInlineCreate(
127
+ permissions.onInlineCreate,
128
+ );
129
+ const onCreate: RefOnCreateCallback | undefined = permissions.onCreate
130
+ ? (newGroup, _init) => permissions.onCreate?.(newGroup)
131
+ : undefined;
132
+ return {
133
+ newInlineOwnerStrategy,
134
+ onCreate,
135
+ };
136
+ }
137
+
138
+ function parseOnInlineCreate(
139
+ onInlineCreate?: OnInlineCreateOptions,
140
+ ): NewInlineOwnerStrategy {
141
+ if (!onInlineCreate || onInlineCreate === "extendsContainer") {
142
+ return extendContainerOwner;
143
+ }
144
+ if (
145
+ typeof onInlineCreate === "object" &&
146
+ "extendsContainer" in onInlineCreate
147
+ ) {
148
+ return extendContainerOwnerFactory(onInlineCreate.extendsContainer);
149
+ }
150
+ if (onInlineCreate === "newGroup") {
151
+ return (createNewGroup) => createNewGroup();
152
+ }
153
+ if (onInlineCreate === "sameAsContainer") {
154
+ return (_createNewGroup, containerOwner) => containerOwner;
155
+ }
156
+ return (createNewGroup, containerOwner) => {
157
+ const newGroup = createNewGroup();
158
+ onInlineCreate(newGroup, { containerOwner });
159
+ return newGroup;
160
+ };
161
+ }
162
+
163
+ export function getDefaultRefPermissions(): RefPermissions {
164
+ return schemaToRefPermissions(DEFAULT_SCHEMA_PERMISSIONS);
165
+ }
166
+
167
+ export function withSchemaPermissions<T extends { owner?: Account | Group }>(
168
+ options?: T | Account | Group,
169
+ schemaPermissions?: SchemaPermissions,
170
+ ): T & { onCreate?: OnCreateCallback } {
171
+ const onCreate = schemaPermissions?.onCreate;
172
+ if (!options) {
173
+ const owner = schemaPermissions?.default?.() ?? Group.create();
174
+ return { owner, onCreate } as T & { onCreate?: OnCreateCallback };
175
+ }
176
+ if (TypeSym in options) {
177
+ return { owner: options, onCreate } as T & {
178
+ onCreate?: OnCreateCallback;
179
+ };
180
+ }
181
+ const owner =
182
+ options.owner ?? schemaPermissions?.default?.() ?? Group.create();
183
+ return {
184
+ ...options,
185
+ owner,
186
+ onCreate,
187
+ } as T & { onCreate?: OnCreateCallback };
188
+ }
@@ -4,6 +4,7 @@ import {
4
4
  BranchDefinition,
5
5
  CoFeed,
6
6
  Group,
7
+ hydrateCoreCoValueSchema,
7
8
  Settled,
8
9
  RefsToResolve,
9
10
  RefsToResolveStrict,
@@ -12,6 +13,7 @@ import {
12
13
  coOptionalDefiner,
13
14
  parseSubscribeRestArgs,
14
15
  unstable_mergeBranchWithResolve,
16
+ withSchemaPermissions,
15
17
  } from "../../../internal.js";
16
18
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
17
19
  import { CoFeedSchemaInit } from "../typeConverters/CoFieldSchemaInit.js";
@@ -20,6 +22,10 @@ import { InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded } from "../typeConverter
20
22
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
21
23
  import { CoreCoValueSchema, CoreResolveQuery } from "./CoValueSchema.js";
22
24
  import { withSchemaResolveQuery } from "../../schemaUtils.js";
25
+ import {
26
+ DEFAULT_SCHEMA_PERMISSIONS,
27
+ SchemaPermissions,
28
+ } from "../schemaPermissions.js";
23
29
 
24
30
  export class CoFeedSchema<
25
31
  T extends AnyZodOrCoValueSchema,
@@ -36,6 +42,12 @@ export class CoFeedSchema<
36
42
  */
37
43
  resolveQuery: DefaultResolveQuery = true as DefaultResolveQuery;
38
44
 
45
+ /**
46
+ * Permissions to be used when creating or composing CoValues
47
+ * @internal
48
+ */
49
+ permissions: SchemaPermissions = DEFAULT_SCHEMA_PERMISSIONS;
50
+
39
51
  constructor(
40
52
  public element: T,
41
53
  private coValueClass: typeof CoFeed,
@@ -54,7 +66,14 @@ export class CoFeedSchema<
54
66
  init: CoFeedSchemaInit<T>,
55
67
  options?: { owner: Account | Group } | Account | Group,
56
68
  ): CoFeedInstance<T> {
57
- return this.coValueClass.create(init as any, options) as CoFeedInstance<T>;
69
+ const optionsWithPermissions = withSchemaPermissions(
70
+ options,
71
+ this.permissions,
72
+ );
73
+ return this.coValueClass.create(
74
+ init as any,
75
+ optionsWithPermissions,
76
+ ) as CoFeedInstance<T>;
58
77
  }
59
78
 
60
79
  load<
@@ -146,8 +165,32 @@ export class CoFeedSchema<
146
165
  >(
147
166
  resolveQuery: RefsToResolveStrict<CoFeedInstanceCoValuesMaybeLoaded<T>, R>,
148
167
  ): CoFeedSchema<T, R> {
149
- const copy = new CoFeedSchema<T, R>(this.element, this.coValueClass);
150
- copy.resolveQuery = resolveQuery as R;
168
+ return this.copy({ resolveQuery: resolveQuery as R });
169
+ }
170
+
171
+ /**
172
+ * Configure permissions to be used when creating or composing CoValues
173
+ */
174
+ withPermissions(
175
+ permissions: SchemaPermissions,
176
+ ): CoFeedSchema<T, DefaultResolveQuery> {
177
+ return this.copy({ permissions });
178
+ }
179
+
180
+ private copy<ResolveQuery extends CoreResolveQuery = DefaultResolveQuery>({
181
+ permissions,
182
+ resolveQuery,
183
+ }: {
184
+ permissions?: SchemaPermissions;
185
+ resolveQuery?: ResolveQuery;
186
+ }): CoFeedSchema<T, ResolveQuery> {
187
+ const coreSchema = createCoreCoFeedSchema(this.element);
188
+ // @ts-expect-error
189
+ const copy: CoFeedSchema<T, ResolveQuery> =
190
+ hydrateCoreCoValueSchema(coreSchema);
191
+ // @ts-expect-error TS cannot infer that the resolveQuery type is valid
192
+ copy.resolveQuery = resolveQuery ?? this.resolveQuery;
193
+ copy.permissions = permissions ?? this.permissions;
151
194
  return copy;
152
195
  }
153
196
  }
@@ -3,6 +3,7 @@ import {
3
3
  BranchDefinition,
4
4
  CoList,
5
5
  Group,
6
+ hydrateCoreCoValueSchema,
6
7
  ID,
7
8
  Settled,
8
9
  RefsToResolve,
@@ -11,6 +12,7 @@ import {
11
12
  SubscribeListenerOptions,
12
13
  coOptionalDefiner,
13
14
  unstable_mergeBranchWithResolve,
15
+ withSchemaPermissions,
14
16
  } from "../../../internal.js";
15
17
  import { CoValueUniqueness } from "cojson";
16
18
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
@@ -21,6 +23,10 @@ import { AnyZodOrCoValueSchema } from "../zodSchema.js";
21
23
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
22
24
  import { CoreCoValueSchema, CoreResolveQuery } from "./CoValueSchema.js";
23
25
  import { withSchemaResolveQuery } from "../../schemaUtils.js";
26
+ import {
27
+ DEFAULT_SCHEMA_PERMISSIONS,
28
+ SchemaPermissions,
29
+ } from "../schemaPermissions.js";
24
30
 
25
31
  export class CoListSchema<
26
32
  T extends AnyZodOrCoValueSchema,
@@ -37,6 +43,12 @@ export class CoListSchema<
37
43
  */
38
44
  resolveQuery: DefaultResolveQuery = true as DefaultResolveQuery;
39
45
 
46
+ /**
47
+ * Permissions to be used when creating or composing CoValues
48
+ * @internal
49
+ */
50
+ permissions: SchemaPermissions = DEFAULT_SCHEMA_PERMISSIONS;
51
+
40
52
  constructor(
41
53
  public element: T,
42
54
  private coValueClass: typeof CoList,
@@ -63,7 +75,14 @@ export class CoListSchema<
63
75
  | Account
64
76
  | Group,
65
77
  ): CoListInstance<T> {
66
- return this.coValueClass.create(items as any, options) as CoListInstance<T>;
78
+ const optionsWithPermissions = withSchemaPermissions(
79
+ options,
80
+ this.permissions,
81
+ );
82
+ return this.coValueClass.create(
83
+ items as any,
84
+ optionsWithPermissions,
85
+ ) as CoListInstance<T>;
67
86
  }
68
87
 
69
88
  load<
@@ -189,8 +208,32 @@ export class CoListSchema<
189
208
  >(
190
209
  resolveQuery: RefsToResolveStrict<CoListInstanceCoValuesMaybeLoaded<T>, R>,
191
210
  ): CoListSchema<T, R> {
192
- const copy = new CoListSchema<T, R>(this.element, this.coValueClass);
193
- copy.resolveQuery = resolveQuery as R;
211
+ return this.copy({ resolveQuery: resolveQuery as R });
212
+ }
213
+
214
+ /**
215
+ * Configure permissions to be used when creating or composing CoValues
216
+ */
217
+ withPermissions(
218
+ permissions: SchemaPermissions,
219
+ ): CoListSchema<T, DefaultResolveQuery> {
220
+ return this.copy({ permissions });
221
+ }
222
+
223
+ private copy<ResolveQuery extends CoreResolveQuery = DefaultResolveQuery>({
224
+ permissions,
225
+ resolveQuery,
226
+ }: {
227
+ permissions?: SchemaPermissions;
228
+ resolveQuery?: ResolveQuery;
229
+ }): CoListSchema<T, ResolveQuery> {
230
+ const coreSchema = createCoreCoListSchema(this.element);
231
+ // @ts-expect-error
232
+ const copy: CoListSchema<T, ResolveQuery> =
233
+ hydrateCoreCoValueSchema(coreSchema);
234
+ // @ts-expect-error TS cannot infer that the resolveQuery type is valid
235
+ copy.resolveQuery = resolveQuery ?? this.resolveQuery;
236
+ copy.permissions = permissions ?? this.permissions;
194
237
  return copy;
195
238
  }
196
239
  }
@@ -17,6 +17,7 @@ import {
17
17
  hydrateCoreCoValueSchema,
18
18
  isAnyCoValueSchema,
19
19
  unstable_mergeBranchWithResolve,
20
+ withSchemaPermissions,
20
21
  } from "../../../internal.js";
21
22
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
22
23
  import { removeGetters, withSchemaResolveQuery } from "../../schemaUtils.js";
@@ -27,6 +28,10 @@ import { z } from "../zodReExport.js";
27
28
  import { AnyZodOrCoValueSchema, AnyZodSchema } from "../zodSchema.js";
28
29
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
29
30
  import { CoreCoValueSchema, CoreResolveQuery } from "./CoValueSchema.js";
31
+ import {
32
+ DEFAULT_SCHEMA_PERMISSIONS,
33
+ SchemaPermissions,
34
+ } from "../schemaPermissions.js";
30
35
 
31
36
  export class CoMapSchema<
32
37
  Shape extends z.core.$ZodLooseShape,
@@ -48,6 +53,12 @@ export class CoMapSchema<
48
53
  */
49
54
  resolveQuery: DefaultResolveQuery = true as DefaultResolveQuery;
50
55
 
56
+ /**
57
+ * Permissions to be used when creating or composing CoValues
58
+ * @internal
59
+ */
60
+ permissions: SchemaPermissions = DEFAULT_SCHEMA_PERMISSIONS;
61
+
51
62
  constructor(
52
63
  coreSchema: CoreCoMapSchema<Shape, CatchAll>,
53
64
  private coValueClass: typeof CoMap,
@@ -76,8 +87,12 @@ export class CoMapSchema<
76
87
  }
77
88
  | Owner,
78
89
  ): CoMapInstanceShape<Shape, CatchAll> & CoMap;
79
- create(...args: [any, ...any[]]) {
80
- return this.coValueClass.create(...args);
90
+ create(init: any, options?: any) {
91
+ const optionsWithPermissions = withSchemaPermissions(
92
+ options,
93
+ this.permissions,
94
+ );
95
+ return this.coValueClass.create(init, optionsWithPermissions);
81
96
  }
82
97
 
83
98
  load<
@@ -254,8 +269,8 @@ export class CoMapSchema<
254
269
  true
255
270
  >,
256
271
  ) => undefined,
257
- ): CoMapSchema<Shape, CatchAll, Owner, DefaultResolveQuery> {
258
- // @ts-expect-error
272
+ ): this {
273
+ // @ts-expect-error avoid exposing 'migrate' at the type level
259
274
  this.coValueClass.prototype.migrate = migration;
260
275
  return this;
261
276
  }
@@ -340,15 +355,37 @@ export class CoMapSchema<
340
355
  R
341
356
  >,
342
357
  ): CoMapSchema<Shape, CatchAll, Owner, R> {
343
- const coreSchema: CoreCoMapSchema<Shape, CatchAll> = createCoreCoMapSchema(
344
- this.shape,
345
- this.catchAll,
346
- );
347
- const copy = new CoMapSchema<Shape, CatchAll, Owner, R>(
348
- coreSchema,
349
- this.coValueClass,
350
- );
351
- copy.resolveQuery = resolveQuery as R;
358
+ return this.copy({ resolveQuery: resolveQuery as R });
359
+ }
360
+
361
+ /**
362
+ * Configure permissions to be used when creating or composing CoValues
363
+ */
364
+ withPermissions(
365
+ permissions: SchemaPermissions,
366
+ ): CoMapSchema<Shape, CatchAll, Owner, DefaultResolveQuery> {
367
+ return this.copy({ permissions });
368
+ }
369
+
370
+ /**
371
+ * Creates a copy of this schema, preserving all previous configuration
372
+ */
373
+ private copy<ResolveQuery extends CoreResolveQuery = DefaultResolveQuery>({
374
+ permissions,
375
+ resolveQuery,
376
+ }: {
377
+ permissions?: SchemaPermissions;
378
+ resolveQuery?: ResolveQuery;
379
+ }): CoMapSchema<Shape, CatchAll, Owner, ResolveQuery> {
380
+ const coreSchema = createCoreCoMapSchema(this.shape, this.catchAll);
381
+ // @ts-expect-error
382
+ const copy: CoMapSchema<Shape, CatchAll, Owner, ResolveQuery> =
383
+ hydrateCoreCoValueSchema(coreSchema);
384
+ // @ts-expect-error avoid exposing 'migrate' at the type level
385
+ copy.coValueClass.prototype.migrate = this.coValueClass.prototype.migrate;
386
+ // @ts-expect-error TS cannot infer that the resolveQuery type is valid
387
+ copy.resolveQuery = resolveQuery ?? this.resolveQuery;
388
+ copy.permissions = permissions ?? this.permissions;
352
389
  return copy;
353
390
  }
354
391
  }
@@ -21,6 +21,7 @@ import { z } from "../zodReExport.js";
21
21
  import { AnyZodOrCoValueSchema } from "../zodSchema.js";
22
22
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
23
23
  import { CoreCoValueSchema, CoreResolveQuery } from "./CoValueSchema.js";
24
+ import { SchemaPermissions } from "../schemaPermissions.js";
24
25
 
25
26
  type CoRecordInit<
26
27
  K extends z.core.$ZodString<string>,
@@ -161,6 +162,19 @@ export interface CoRecordSchema<
161
162
  R
162
163
  >,
163
164
  ): CoRecordSchema<K, V, R>;
165
+
166
+ /**
167
+ * Permissions to be used when creating or composing CoValues
168
+ * @internal
169
+ */
170
+ permissions: SchemaPermissions;
171
+
172
+ /**
173
+ * Configure permissions to be used when creating or composing CoValues
174
+ */
175
+ withPermissions(
176
+ permissions: SchemaPermissions,
177
+ ): CoRecordSchema<K, V, DefaultResolveQuery>;
164
178
  }
165
179
 
166
180
  type CoRecordSchemaDefinition<
@@ -6,9 +6,14 @@ import {
6
6
  InstanceOrPrimitiveOfSchema,
7
7
  InstanceOrPrimitiveOfSchemaCoValuesMaybeLoaded,
8
8
  coOptionalDefiner,
9
+ withSchemaPermissions,
9
10
  } from "../../../internal.js";
10
11
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
11
12
  import { CoreCoValueSchema } from "./CoValueSchema.js";
13
+ import {
14
+ DEFAULT_SCHEMA_PERMISSIONS,
15
+ SchemaPermissions,
16
+ } from "../schemaPermissions.js";
12
17
 
13
18
  export interface CoreCoVectorSchema extends CoreCoValueSchema {
14
19
  builtin: "CoVector";
@@ -31,6 +36,11 @@ export class CoVectorSchema implements CoreCoVectorSchema {
31
36
  readonly builtin = "CoVector" as const;
32
37
  readonly resolveQuery = true as const;
33
38
 
39
+ /**
40
+ * Permissions to be used when creating or composing CoValues
41
+ */
42
+ permissions: SchemaPermissions = DEFAULT_SCHEMA_PERMISSIONS;
43
+
34
44
  constructor(
35
45
  public dimensions: number,
36
46
  private coValueClass: typeof CoVector,
@@ -56,7 +66,11 @@ export class CoVectorSchema implements CoreCoVectorSchema {
56
66
  vector: number[] | Float32Array,
57
67
  options?: { owner: Account | Group } | Account | Group,
58
68
  ): CoVectorInstance {
59
- return this.coValueClass.create(vector, options);
69
+ const optionsWithPermissions = withSchemaPermissions(
70
+ options,
71
+ this.permissions,
72
+ );
73
+ return this.coValueClass.create(vector, optionsWithPermissions);
60
74
  }
61
75
 
62
76
  /**
@@ -99,6 +113,15 @@ export class CoVectorSchema implements CoreCoVectorSchema {
99
113
  optional(): CoOptionalSchema<this> {
100
114
  return coOptionalDefiner(this);
101
115
  }
116
+
117
+ /**
118
+ * Configure permissions to be used when creating or composing CoValues
119
+ */
120
+ withPermissions(permissions: SchemaPermissions): CoVectorSchema {
121
+ const copy = new CoVectorSchema(this.dimensions, this.coValueClass);
122
+ copy.permissions = permissions;
123
+ return copy;
124
+ }
102
125
  }
103
126
 
104
127
  export type CoVectorInstance = InstanceOrPrimitiveOfSchema<CoVectorSchema>;
@@ -6,9 +6,14 @@ import {
6
6
  Settled,
7
7
  coOptionalDefiner,
8
8
  unstable_mergeBranchWithResolve,
9
+ withSchemaPermissions,
9
10
  } from "../../../internal.js";
10
11
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
11
12
  import { CoreCoValueSchema } from "./CoValueSchema.js";
13
+ import {
14
+ DEFAULT_SCHEMA_PERMISSIONS,
15
+ SchemaPermissions,
16
+ } from "../schemaPermissions.js";
12
17
 
13
18
  export interface CoreFileStreamSchema extends CoreCoValueSchema {
14
19
  builtin: "FileStream";
@@ -27,13 +32,22 @@ export class FileStreamSchema implements CoreFileStreamSchema {
27
32
  readonly builtin = "FileStream" as const;
28
33
  readonly resolveQuery = true as const;
29
34
 
35
+ /**
36
+ * Permissions to be used when creating or composing CoValues
37
+ */
38
+ permissions: SchemaPermissions = DEFAULT_SCHEMA_PERMISSIONS;
39
+
30
40
  constructor(private coValueClass: typeof FileStream) {}
31
41
 
32
42
  create(options?: { owner: Group } | Group): FileStream;
33
43
  /** @deprecated Creating CoValues with an Account as owner is deprecated. Use a Group instead. */
34
44
  create(options?: { owner: Account | Group } | Account | Group): FileStream;
35
45
  create(options?: { owner: Account | Group } | Account | Group): FileStream {
36
- return this.coValueClass.create(options);
46
+ const optionsWithPermissions = withSchemaPermissions(
47
+ options,
48
+ this.permissions,
49
+ );
50
+ return this.coValueClass.create(optionsWithPermissions);
37
51
  }
38
52
 
39
53
  createFromBlob(
@@ -60,13 +74,35 @@ export class FileStreamSchema implements CoreFileStreamSchema {
60
74
  | Account
61
75
  | Group,
62
76
  ): Promise<FileStream> {
63
- return this.coValueClass.createFromBlob(blob, options);
77
+ const optionsWithPermissions = withSchemaPermissions(
78
+ options,
79
+ this.permissions,
80
+ );
81
+ return this.coValueClass.createFromBlob(blob, optionsWithPermissions);
64
82
  }
65
83
 
66
84
  createFromArrayBuffer(
67
- ...args: Parameters<typeof FileStream.createFromArrayBuffer>
85
+ arrayBuffer: ArrayBuffer,
86
+ mimeType: string,
87
+ fileName: string | undefined,
88
+ options?:
89
+ | {
90
+ owner?: Account | Group;
91
+ onProgress?: (progress: number) => void;
92
+ }
93
+ | Account
94
+ | Group,
68
95
  ) {
69
- return this.coValueClass.createFromArrayBuffer(...args);
96
+ const optionsWithPermissions = withSchemaPermissions(
97
+ options,
98
+ this.permissions,
99
+ );
100
+ return this.coValueClass.createFromArrayBuffer(
101
+ arrayBuffer,
102
+ mimeType,
103
+ fileName,
104
+ optionsWithPermissions,
105
+ );
70
106
  }
71
107
 
72
108
  loadAsBlob(
@@ -115,4 +151,15 @@ export class FileStreamSchema implements CoreFileStreamSchema {
115
151
  optional(): CoOptionalSchema<this> {
116
152
  return coOptionalDefiner(this);
117
153
  }
154
+
155
+ /**
156
+ * Configure permissions to be used when creating or composing CoValues
157
+ */
158
+ withPermissions(
159
+ permissions: Omit<SchemaPermissions, "onInlineCreate">,
160
+ ): FileStreamSchema {
161
+ const copy = new FileStreamSchema(this.coValueClass);
162
+ copy.permissions = permissions;
163
+ return copy;
164
+ }
118
165
  }