@casual-simulation/aux-records 3.4.6-alpha.14601027727 → 3.5.0-alpha.15117651144

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 (211) hide show
  1. package/AIController.js +835 -890
  2. package/AIController.js.map +1 -1
  3. package/AIHumeInterface.js +43 -54
  4. package/AIHumeInterface.js.map +1 -1
  5. package/AIOpenAIRealtimeInterface.js +60 -71
  6. package/AIOpenAIRealtimeInterface.js.map +1 -1
  7. package/AnthropicAIChatInterface.js +96 -142
  8. package/AnthropicAIChatInterface.js.map +1 -1
  9. package/AuthController.d.ts +3 -2
  10. package/AuthController.js +1907 -1933
  11. package/AuthController.js.map +1 -1
  12. package/AuthStore.d.ts +1 -10
  13. package/BlockadeLabsGenerateSkyboxInterface.js +57 -72
  14. package/BlockadeLabsGenerateSkyboxInterface.js.map +1 -1
  15. package/CachingConfigStore.js +30 -45
  16. package/CachingConfigStore.js.map +1 -1
  17. package/CachingPolicyStore.d.ts +8 -2
  18. package/CachingPolicyStore.js +108 -135
  19. package/CachingPolicyStore.js.map +1 -1
  20. package/ComIdConfig.d.ts +18 -18
  21. package/ComIdConfig.js.map +1 -1
  22. package/ConsoleAuthMessenger.js +7 -20
  23. package/ConsoleAuthMessenger.js.map +1 -1
  24. package/DataRecordsController.d.ts +2 -2
  25. package/DataRecordsController.js +369 -377
  26. package/DataRecordsController.js.map +1 -1
  27. package/DataRecordsStore.d.ts +1 -1
  28. package/DataRecordsStore.js +1 -1
  29. package/DataRecordsStore.js.map +1 -1
  30. package/EventRecordsController.js +226 -240
  31. package/EventRecordsController.js.map +1 -1
  32. package/FileRecordsController.d.ts +13 -2
  33. package/FileRecordsController.js +458 -450
  34. package/FileRecordsController.js.map +1 -1
  35. package/GoogleAIChatInterface.js +133 -179
  36. package/GoogleAIChatInterface.js.map +1 -1
  37. package/LivekitController.js +43 -54
  38. package/LivekitController.js.map +1 -1
  39. package/LoomController.js +64 -75
  40. package/LoomController.js.map +1 -1
  41. package/MemoryAuthMessenger.js +10 -23
  42. package/MemoryAuthMessenger.js.map +1 -1
  43. package/MemoryCache.js +18 -35
  44. package/MemoryCache.js.map +1 -1
  45. package/MemoryFileRecordsLookup.js +105 -125
  46. package/MemoryFileRecordsLookup.js.map +1 -1
  47. package/MemoryModerationJobProvider.js +17 -30
  48. package/MemoryModerationJobProvider.js.map +1 -1
  49. package/MemoryRateLimiter.js +12 -27
  50. package/MemoryRateLimiter.js.map +1 -1
  51. package/MemoryStore.d.ts +18 -6
  52. package/MemoryStore.js +1879 -1997
  53. package/MemoryStore.js.map +1 -1
  54. package/MetricsStore.d.ts +2 -2
  55. package/ModerationController.js +186 -200
  56. package/ModerationController.js.map +1 -1
  57. package/OpenAIChatInterface.js +105 -135
  58. package/OpenAIChatInterface.js.map +1 -1
  59. package/OpenAIImageInterface.js +57 -51
  60. package/OpenAIImageInterface.js.map +1 -1
  61. package/PolicyController.d.ts +150 -10
  62. package/PolicyController.js +1546 -1299
  63. package/PolicyController.js.map +1 -1
  64. package/PolicyStore.d.ts +110 -2
  65. package/PolicyStore.js +36 -1
  66. package/PolicyStore.js.map +1 -1
  67. package/PrivoClient.js +398 -435
  68. package/PrivoClient.js.map +1 -1
  69. package/RateLimitController.js +25 -36
  70. package/RateLimitController.js.map +1 -1
  71. package/RecordsClient.js +51 -74
  72. package/RecordsClient.js.map +1 -1
  73. package/RecordsController.d.ts +2 -42
  74. package/RecordsController.js +1026 -1182
  75. package/RecordsController.js.map +1 -1
  76. package/RecordsServer.d.ts +196 -27
  77. package/RecordsServer.js +1701 -1343
  78. package/RecordsServer.js.map +1 -1
  79. package/RecordsStore.d.ts +1 -10
  80. package/RecordsStore.js.map +1 -1
  81. package/ServerConfig.d.ts +339 -195
  82. package/ServerConfig.js +13 -0
  83. package/ServerConfig.js.map +1 -1
  84. package/SloydInterface.js +62 -75
  85. package/SloydInterface.js.map +1 -1
  86. package/StabilityAIImageInterface.js +150 -167
  87. package/StabilityAIImageInterface.js.map +1 -1
  88. package/SubscriptionConfigBuilder.d.ts +6 -1
  89. package/SubscriptionConfigBuilder.js +22 -0
  90. package/SubscriptionConfigBuilder.js.map +1 -1
  91. package/SubscriptionConfiguration.d.ts +266 -169
  92. package/SubscriptionConfiguration.js +101 -79
  93. package/SubscriptionConfiguration.js.map +1 -1
  94. package/SubscriptionController.d.ts +2 -1
  95. package/SubscriptionController.js +643 -650
  96. package/SubscriptionController.js.map +1 -1
  97. package/SystemNotificationMessenger.d.ts +21 -4
  98. package/SystemNotificationMessenger.js +36 -30
  99. package/SystemNotificationMessenger.js.map +1 -1
  100. package/TestUtils.d.ts +9 -1
  101. package/TestUtils.js +105 -129
  102. package/TestUtils.js.map +1 -1
  103. package/Utils.d.ts +2 -16
  104. package/Utils.js +21 -22
  105. package/Utils.js.map +1 -1
  106. package/crud/CrudHelpers.js +17 -26
  107. package/crud/CrudHelpers.js.map +1 -1
  108. package/crud/CrudRecordsController.d.ts +1 -1
  109. package/crud/CrudRecordsController.js +259 -267
  110. package/crud/CrudRecordsController.js.map +1 -1
  111. package/crud/CrudRecordsControllerTests.js +174 -185
  112. package/crud/CrudRecordsControllerTests.js.map +1 -1
  113. package/crud/CrudRecordsStore.d.ts +7 -3
  114. package/crud/MemoryCrudRecordsStore.d.ts +4 -4
  115. package/crud/MemoryCrudRecordsStore.js +98 -118
  116. package/crud/MemoryCrudRecordsStore.js.map +1 -1
  117. package/crud/sub/MemorySubCrudRecordsStore.d.ts +24 -0
  118. package/crud/sub/MemorySubCrudRecordsStore.js +146 -0
  119. package/crud/sub/MemorySubCrudRecordsStore.js.map +1 -0
  120. package/crud/sub/SubCrudRecordsController.d.ts +182 -0
  121. package/crud/sub/SubCrudRecordsController.js +360 -0
  122. package/crud/sub/SubCrudRecordsController.js.map +1 -0
  123. package/crud/sub/SubCrudRecordsControllerTests.d.ts +39 -0
  124. package/crud/sub/SubCrudRecordsControllerTests.js +821 -0
  125. package/crud/sub/SubCrudRecordsControllerTests.js.map +1 -0
  126. package/crud/sub/SubCrudRecordsStore.d.ts +95 -0
  127. package/{forms/index.js → crud/sub/SubCrudRecordsStore.js} +2 -2
  128. package/crud/sub/SubCrudRecordsStore.js.map +1 -0
  129. package/crud/sub/index.d.ts +3 -0
  130. package/crud/sub/index.js +20 -0
  131. package/{forms → crud/sub}/index.js.map +1 -1
  132. package/index.d.ts +1 -1
  133. package/index.js +1 -1
  134. package/index.js.map +1 -1
  135. package/notifications/MemoryNotificationRecordsStore.js +189 -198
  136. package/notifications/MemoryNotificationRecordsStore.js.map +1 -1
  137. package/notifications/NotificationRecordsController.js +438 -460
  138. package/notifications/NotificationRecordsController.js.map +1 -1
  139. package/notifications/NotificationRecordsStore.d.ts +2 -1
  140. package/notifications/WebPushInterface.d.ts +0 -1
  141. package/notifications/WebPushInterface.js +0 -1
  142. package/notifications/WebPushInterface.js.map +1 -1
  143. package/package.json +6 -6
  144. package/packages/MemoryPackageRecordsStore.d.ts +10 -0
  145. package/packages/MemoryPackageRecordsStore.js +38 -0
  146. package/packages/MemoryPackageRecordsStore.js.map +1 -0
  147. package/packages/PackageRecordsController.d.ts +26 -0
  148. package/packages/PackageRecordsController.js +49 -0
  149. package/packages/PackageRecordsController.js.map +1 -0
  150. package/packages/PackageRecordsStore.d.ts +32 -0
  151. package/packages/PackageRecordsStore.js +19 -0
  152. package/packages/PackageRecordsStore.js.map +1 -0
  153. package/packages/index.d.ts +4 -0
  154. package/packages/index.js +21 -0
  155. package/packages/index.js.map +1 -0
  156. package/packages/version/MemoryPackageVersionRecordsStore.d.ts +21 -0
  157. package/packages/version/MemoryPackageVersionRecordsStore.js +177 -0
  158. package/packages/version/MemoryPackageVersionRecordsStore.js.map +1 -0
  159. package/packages/version/PackageVersionRecordsController.d.ts +144 -0
  160. package/packages/version/PackageVersionRecordsController.js +656 -0
  161. package/packages/version/PackageVersionRecordsController.js.map +1 -0
  162. package/packages/version/PackageVersionRecordsStore.d.ts +342 -0
  163. package/packages/version/PackageVersionRecordsStore.js +126 -0
  164. package/packages/version/PackageVersionRecordsStore.js.map +1 -0
  165. package/packages/version/index.d.ts +4 -0
  166. package/packages/version/index.js +21 -0
  167. package/packages/version/index.js.map +1 -0
  168. package/tracing/TracingDecorators.js +31 -40
  169. package/tracing/TracingDecorators.js.map +1 -1
  170. package/webhooks/MemoryWebhookRecordsStore.js +56 -72
  171. package/webhooks/MemoryWebhookRecordsStore.js.map +1 -1
  172. package/webhooks/WebhookEnvironment.d.ts +3 -3
  173. package/webhooks/WebhookRecordsController.d.ts +2 -1
  174. package/webhooks/WebhookRecordsController.js +389 -382
  175. package/webhooks/WebhookRecordsController.js.map +1 -1
  176. package/webhooks/WebhookRecordsStore.d.ts +2 -1
  177. package/websockets/InstRecordsStore.d.ts +50 -0
  178. package/websockets/InstRecordsStore.js +17 -0
  179. package/websockets/InstRecordsStore.js.map +1 -1
  180. package/websockets/MemoryTempInstRecordsStore.d.ts +5 -0
  181. package/websockets/MemoryTempInstRecordsStore.js +168 -179
  182. package/websockets/MemoryTempInstRecordsStore.js.map +1 -1
  183. package/websockets/MemoryWebsocketConnectionStore.js +98 -135
  184. package/websockets/MemoryWebsocketConnectionStore.js.map +1 -1
  185. package/websockets/MemoryWebsocketMessenger.js +29 -48
  186. package/websockets/MemoryWebsocketMessenger.js.map +1 -1
  187. package/websockets/SplitInstRecordsStore.d.ts +4 -1
  188. package/websockets/SplitInstRecordsStore.js +167 -185
  189. package/websockets/SplitInstRecordsStore.js.map +1 -1
  190. package/websockets/TemporaryInstRecordsStore.d.ts +19 -1
  191. package/websockets/TemporaryInstRecordsStore.js +17 -0
  192. package/websockets/TemporaryInstRecordsStore.js.map +1 -1
  193. package/websockets/WebsocketController.d.ts +147 -3
  194. package/websockets/WebsocketController.js +1735 -1391
  195. package/websockets/WebsocketController.js.map +1 -1
  196. package/websockets/index.d.ts +0 -1
  197. package/websockets/index.js +0 -1
  198. package/websockets/index.js.map +1 -1
  199. package/AAGUID.d.ts +0 -11
  200. package/AAGUID.js +0 -116
  201. package/AAGUID.js.map +0 -1
  202. package/AuthUtils.d.ts +0 -162
  203. package/AuthUtils.js +0 -327
  204. package/AuthUtils.js.map +0 -1
  205. package/forms/FormError.d.ts +0 -43
  206. package/forms/FormError.js +0 -56
  207. package/forms/FormError.js.map +0 -1
  208. package/forms/index.d.ts +0 -2
  209. package/websockets/Utils.d.ts +0 -33
  210. package/websockets/Utils.js +0 -82
  211. package/websockets/Utils.js.map +0 -1
@@ -4,16 +4,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
4
4
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
5
  return c > 3 && r && Object.defineProperty(target, key, r), r;
6
6
  };
7
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
9
- return new (P || (P = Promise))(function (resolve, reject) {
10
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
12
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
13
- step((generator = generator.apply(thisArg, _arguments || [])).next());
14
- });
15
- };
16
- import { doesSubjectMatchPolicy, isValidUserPolicy, } from './DataRecordsStore';
7
+ import { doesSubjectMatchPolicy, isValidUserPolicy } from './DataRecordsStore';
17
8
  import { getMarkerResourcesForCreation, getMarkerResourcesForUpdate, } from './PolicyController';
18
9
  import { ACCOUNT_MARKER, PUBLIC_READ_MARKER, hasValue, } from '@casual-simulation/aux-common';
19
10
  import { getSubscriptionFeatures } from './SubscriptionConfiguration';
@@ -49,185 +40,186 @@ export class DataRecordsController {
49
40
  * @param markers The list of markers that should be applied to the new record. If null, then the publicRead marker will be applied.
50
41
  * @param instances The list of instances that are currently loaded.
51
42
  */
52
- recordData(recordKeyOrRecordName, address, data, subjectId, updatePolicy, deletePolicy, markers = null, instances = null) {
43
+ async recordData(recordKeyOrRecordName, address, data, subjectId, updatePolicy, deletePolicy, markers = null, instances = null) {
53
44
  var _a, _b, _c, _d;
54
- return __awaiter(this, void 0, void 0, function* () {
55
- try {
56
- const contextResult = yield this._policies.constructAuthorizationContext({
57
- recordKeyOrRecordName,
58
- userId: subjectId,
59
- });
60
- if (contextResult.success === false) {
61
- return contextResult;
62
- }
63
- const policy = contextResult.context.subjectPolicy;
64
- if (!subjectId && policy !== 'subjectless') {
45
+ try {
46
+ const contextResult = await this._policies.constructAuthorizationContext({
47
+ recordKeyOrRecordName,
48
+ userId: subjectId,
49
+ });
50
+ if (contextResult.success === false) {
51
+ return contextResult;
52
+ }
53
+ const policy = contextResult.context.subjectPolicy;
54
+ if (!subjectId && policy !== 'subjectless') {
55
+ return {
56
+ success: false,
57
+ errorCode: 'not_logged_in',
58
+ errorMessage: 'The user must be logged in in order to record data.',
59
+ };
60
+ }
61
+ if (policy === 'subjectless') {
62
+ subjectId = null;
63
+ }
64
+ if (!updatePolicy) {
65
+ updatePolicy = true;
66
+ }
67
+ if (!deletePolicy) {
68
+ deletePolicy = true;
69
+ }
70
+ if (!isValidUserPolicy(updatePolicy)) {
71
+ return {
72
+ success: false,
73
+ errorCode: 'invalid_update_policy',
74
+ errorMessage: 'The given updatePolicy is invalid or not supported.',
75
+ };
76
+ }
77
+ if (!isValidUserPolicy(deletePolicy)) {
78
+ return {
79
+ success: false,
80
+ errorCode: 'invalid_delete_policy',
81
+ errorMessage: 'The given deletePolicy is invalid or not supported.',
82
+ };
83
+ }
84
+ if (policy === 'subjectless') {
85
+ if (updatePolicy !== true) {
65
86
  return {
66
87
  success: false,
67
- errorCode: 'not_logged_in',
68
- errorMessage: 'The user must be logged in in order to record data.',
88
+ errorCode: 'invalid_record_key',
89
+ errorMessage: 'It is not possible to set update policies using a subjectless key.',
69
90
  };
70
91
  }
71
- if (policy === 'subjectless') {
72
- subjectId = null;
73
- }
74
- if (!updatePolicy) {
75
- updatePolicy = true;
76
- }
77
- if (!deletePolicy) {
78
- deletePolicy = true;
79
- }
80
- if (!isValidUserPolicy(updatePolicy)) {
92
+ if (deletePolicy !== true) {
81
93
  return {
82
94
  success: false,
83
- errorCode: 'invalid_update_policy',
84
- errorMessage: 'The given updatePolicy is invalid or not supported.',
95
+ errorCode: 'invalid_record_key',
96
+ errorMessage: 'It is not possible to set delete policies using a subjectless key.',
85
97
  };
86
98
  }
87
- if (!isValidUserPolicy(deletePolicy)) {
99
+ }
100
+ const recordName = contextResult.context.recordName;
101
+ const existingRecord = await this._store.getData(recordName, address);
102
+ if (existingRecord.success) {
103
+ const existingUpdatePolicy = (_a = existingRecord.updatePolicy) !== null && _a !== void 0 ? _a : true;
104
+ if (!doesSubjectMatchPolicy(existingUpdatePolicy, subjectId)) {
88
105
  return {
89
106
  success: false,
90
- errorCode: 'invalid_delete_policy',
91
- errorMessage: 'The given deletePolicy is invalid or not supported.',
107
+ errorCode: 'not_authorized',
108
+ errorMessage: 'The updatePolicy does not permit this user to update the data record.',
92
109
  };
93
110
  }
94
- if (policy === 'subjectless') {
95
- if (updatePolicy !== true) {
96
- return {
97
- success: false,
98
- errorCode: 'invalid_record_key',
99
- errorMessage: 'It is not possible to set update policies using a subjectless key.',
100
- };
101
- }
102
- if (deletePolicy !== true) {
103
- return {
104
- success: false,
105
- errorCode: 'invalid_record_key',
106
- errorMessage: 'It is not possible to set delete policies using a subjectless key.',
107
- };
108
- }
109
- }
110
- const recordName = contextResult.context.recordName;
111
- const existingRecord = yield this._store.getData(recordName, address);
112
- if (existingRecord.success) {
113
- const existingUpdatePolicy = (_a = existingRecord.updatePolicy) !== null && _a !== void 0 ? _a : true;
114
- if (!doesSubjectMatchPolicy(existingUpdatePolicy, subjectId)) {
115
- return {
116
- success: false,
117
- errorCode: 'not_authorized',
118
- errorMessage: 'The updatePolicy does not permit this user to update the data record.',
119
- };
120
- }
121
- }
122
- let resourceMarkers;
123
- if (existingRecord.success) {
124
- const existingMarkers = (_b = existingRecord.markers) !== null && _b !== void 0 ? _b : [
125
- PUBLIC_READ_MARKER,
126
- ];
127
- resourceMarkers = markers !== null && markers !== void 0 ? markers : existingMarkers;
128
- const authorization = yield this._policies.authorizeUserAndInstancesForResources(contextResult.context, {
129
- userId: subjectId,
130
- instances,
131
- resources: [
132
- {
133
- resourceKind: 'data',
134
- resourceId: address,
135
- action: 'update',
136
- markers: resourceMarkers,
137
- },
138
- ...getMarkerResourcesForUpdate(existingMarkers, markers),
139
- ],
140
- });
141
- if (authorization.success === false) {
142
- return authorization;
143
- }
111
+ }
112
+ let resourceMarkers;
113
+ if (existingRecord.success) {
114
+ const existingMarkers = (_b = existingRecord.markers) !== null && _b !== void 0 ? _b : [
115
+ PUBLIC_READ_MARKER,
116
+ ];
117
+ resourceMarkers = markers !== null && markers !== void 0 ? markers : existingMarkers;
118
+ const authorization = await this._policies.authorizeUserAndInstancesForResources(contextResult.context, {
119
+ userId: subjectId,
120
+ instances,
121
+ resources: [
122
+ {
123
+ resourceKind: 'data',
124
+ resourceId: address,
125
+ action: 'update',
126
+ markers: resourceMarkers,
127
+ },
128
+ ...getMarkerResourcesForUpdate(existingMarkers, markers),
129
+ ],
130
+ });
131
+ if (authorization.success === false) {
132
+ return authorization;
144
133
  }
145
- else {
146
- resourceMarkers = markers !== null && markers !== void 0 ? markers : [PUBLIC_READ_MARKER];
147
- const authorization = yield this._policies.authorizeUserAndInstancesForResources(contextResult.context, {
148
- userId: subjectId,
149
- instances,
150
- resources: [
151
- {
152
- resourceKind: 'data',
153
- resourceId: address,
154
- action: 'create',
155
- markers: resourceMarkers,
156
- },
157
- ...getMarkerResourcesForCreation(resourceMarkers),
158
- ],
159
- });
160
- if (authorization.success === false) {
161
- return authorization;
162
- }
134
+ }
135
+ else {
136
+ resourceMarkers = markers !== null && markers !== void 0 ? markers : [PUBLIC_READ_MARKER];
137
+ const authorization = await this._policies.authorizeUserAndInstancesForResources(contextResult.context, {
138
+ userId: subjectId,
139
+ instances,
140
+ resources: [
141
+ {
142
+ resourceKind: 'data',
143
+ resourceId: address,
144
+ action: 'create',
145
+ markers: resourceMarkers,
146
+ },
147
+ ...getMarkerResourcesForCreation(resourceMarkers),
148
+ ],
149
+ });
150
+ if (authorization.success === false) {
151
+ return authorization;
163
152
  }
164
- const metricsResult = yield this._metrics.getSubscriptionDataMetricsByRecordName(recordName);
165
- const config = yield this._config.getSubscriptionConfiguration();
166
- const features = getSubscriptionFeatures(config, metricsResult.subscriptionStatus, metricsResult.subscriptionId, metricsResult.ownerId ? 'user' : 'studio');
167
- if (!features.data.allowed) {
153
+ }
154
+ const metricsResult = await this._metrics.getSubscriptionDataMetricsByRecordName(recordName);
155
+ const config = await this._config.getSubscriptionConfiguration();
156
+ const features = getSubscriptionFeatures(config, metricsResult.subscriptionStatus, metricsResult.subscriptionId, metricsResult.ownerId ? 'user' : 'studio');
157
+ if (!features.data.allowed) {
158
+ return {
159
+ success: false,
160
+ errorCode: 'not_authorized',
161
+ errorMessage: 'The subscription does not permit the recording of data.',
162
+ errorReason: 'data_not_allowed',
163
+ };
164
+ }
165
+ if (hasValue(features.data.maxItemSizeInBytes)) {
166
+ const dataString = typeof data === 'string' ? data : stringify(data);
167
+ const size = byteLengthOfString(dataString);
168
+ const schema = z.number().max(features.data.maxItemSizeInBytes);
169
+ const result = schema.safeParse(size, {
170
+ path: ['data', 'sizeInBytes'],
171
+ });
172
+ if (result.success === false) {
168
173
  return {
169
174
  success: false,
170
- errorCode: 'not_authorized',
171
- errorMessage: 'The subscription does not permit the recording of data.',
172
- errorReason: 'data_not_allowed',
175
+ errorCode: 'subscription_limit_reached',
176
+ errorMessage: 'The size of the item is larger than the subscription allows.',
177
+ errorReason: 'data_too_large',
178
+ issues: result.error.issues,
173
179
  };
174
180
  }
175
- if (hasValue(features.data.maxItemSizeInBytes)) {
176
- const dataString = typeof data === 'string' ? data : stringify(data);
177
- const size = byteLengthOfString(dataString);
178
- const schema = z.number().max(features.data.maxItemSizeInBytes);
179
- const result = schema.safeParse(size, {
180
- path: ['data', 'sizeInBytes'],
181
- });
182
- if (result.success === false) {
181
+ }
182
+ if (!existingRecord.success) {
183
+ // Check metrics
184
+ if (hasValue(features.data.maxItems) &&
185
+ features.data.maxItems > 0) {
186
+ if (metricsResult.totalItems >= features.data.maxItems) {
183
187
  return {
184
188
  success: false,
185
189
  errorCode: 'subscription_limit_reached',
186
- errorMessage: 'The size of the item is larger than the subscription allows.',
187
- errorReason: 'data_too_large',
188
- issues: result.error.issues,
190
+ errorMessage: 'The maximum number of items has been reached for your subscription.',
191
+ errorReason: 'too_many_items',
189
192
  };
190
193
  }
191
194
  }
192
- if (!existingRecord.success) {
193
- // Check metrics
194
- if (features.data.maxItems > 0) {
195
- if (metricsResult.totalItems >= features.data.maxItems) {
196
- return {
197
- success: false,
198
- errorCode: 'subscription_limit_reached',
199
- errorMessage: 'The maximum number of items has been reached for your subscription.',
200
- errorReason: 'too_many_items',
201
- };
202
- }
203
- }
204
- }
205
- const result2 = yield this._store.setData(recordName, address, data, (_d = (_c = contextResult.context.recordKeyCreatorId) !== null && _c !== void 0 ? _c : subjectId) !== null && _d !== void 0 ? _d : contextResult.context.recordOwnerId, subjectId, updatePolicy, deletePolicy, resourceMarkers);
206
- if (result2.success === false) {
207
- return {
208
- success: false,
209
- errorCode: result2.errorCode,
210
- errorMessage: result2.errorMessage,
211
- };
212
- }
213
- return {
214
- success: true,
215
- recordName: recordName,
216
- address: address,
217
- };
218
195
  }
219
- catch (err) {
220
- const span = trace.getActiveSpan();
221
- span === null || span === void 0 ? void 0 : span.recordException(err);
222
- span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
223
- console.error(`[DataRecordsController] A server error occurred while recording data:`, err);
196
+ const result2 = await this._store.setData(recordName, address, data, (_d = (_c = contextResult.context.recordKeyCreatorId) !== null && _c !== void 0 ? _c : subjectId) !== null && _d !== void 0 ? _d : contextResult.context.recordOwnerId, subjectId, updatePolicy, deletePolicy, resourceMarkers);
197
+ if (result2.success === false) {
224
198
  return {
225
199
  success: false,
226
- errorCode: 'server_error',
227
- errorMessage: 'A server error occurred.',
200
+ errorCode: result2.errorCode,
201
+ errorMessage: result2.errorMessage,
228
202
  };
229
203
  }
230
- });
204
+ return {
205
+ success: true,
206
+ recordName: recordName,
207
+ address: address,
208
+ };
209
+ }
210
+ catch (err) {
211
+ const span = trace.getActiveSpan();
212
+ if (err instanceof Error) {
213
+ span === null || span === void 0 ? void 0 : span.recordException(err);
214
+ }
215
+ span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
216
+ console.error(`[DataRecordsController] A server error occurred while recording data:`, err);
217
+ return {
218
+ success: false,
219
+ errorCode: 'server_error',
220
+ errorMessage: 'A server error occurred.',
221
+ };
222
+ }
231
223
  }
232
224
  /**
233
225
  * Gets the data that is stored in the given record at the given address.
@@ -236,62 +228,62 @@ export class DataRecordsController {
236
228
  * @param userId The ID of the user who is retrieving the data. If null, then it is assumed that the user is not logged in.
237
229
  * @param instances The list of instances that are loaded.
238
230
  */
239
- getData(recordName, address, userId, instances) {
231
+ async getData(recordName, address, userId, instances) {
240
232
  var _a, _b, _c;
241
- return __awaiter(this, void 0, void 0, function* () {
242
- try {
243
- const baseRequest = {
244
- recordKeyOrRecordName: recordName,
245
- userId,
246
- instances,
247
- };
248
- const context = yield this._policies.constructAuthorizationContext(baseRequest);
249
- if (context.success === false) {
250
- return context;
251
- }
252
- const result = yield this._store.getData(context.context.recordName, address);
253
- if (result.success === false) {
254
- return {
255
- success: false,
256
- errorCode: result.errorCode,
257
- errorMessage: result.errorMessage,
258
- };
259
- }
260
- const markers = (_a = result.markers) !== null && _a !== void 0 ? _a : [PUBLIC_READ_MARKER];
261
- const authorization = yield this._policies.authorizeUserAndInstances(context.context, {
262
- userId: userId,
263
- instances,
264
- resourceKind: 'data',
265
- resourceId: address,
266
- action: 'read',
267
- markers: markers,
268
- });
269
- if (authorization.success === false) {
270
- return authorization;
271
- }
272
- return {
273
- success: true,
274
- data: result.data,
275
- publisherId: result.publisherId,
276
- subjectId: result.subjectId,
277
- recordName,
278
- updatePolicy: (_b = result.updatePolicy) !== null && _b !== void 0 ? _b : true,
279
- deletePolicy: (_c = result.deletePolicy) !== null && _c !== void 0 ? _c : true,
280
- markers: markers,
281
- };
233
+ try {
234
+ const baseRequest = {
235
+ recordKeyOrRecordName: recordName,
236
+ userId,
237
+ instances,
238
+ };
239
+ const context = await this._policies.constructAuthorizationContext(baseRequest);
240
+ if (context.success === false) {
241
+ return context;
282
242
  }
283
- catch (err) {
284
- const span = trace.getActiveSpan();
285
- span === null || span === void 0 ? void 0 : span.recordException(err);
286
- span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
287
- console.error('[DataRecordsController] An error occurred while getting data:', err);
243
+ const result = await this._store.getData(context.context.recordName, address);
244
+ if (result.success === false) {
288
245
  return {
289
246
  success: false,
290
- errorCode: 'server_error',
291
- errorMessage: 'A server error occurred.',
247
+ errorCode: result.errorCode,
248
+ errorMessage: result.errorMessage,
292
249
  };
293
250
  }
294
- });
251
+ const markers = (_a = result.markers) !== null && _a !== void 0 ? _a : [PUBLIC_READ_MARKER];
252
+ const authorization = await this._policies.authorizeUserAndInstances(context.context, {
253
+ userId: userId,
254
+ instances,
255
+ resourceKind: 'data',
256
+ resourceId: address,
257
+ action: 'read',
258
+ markers: markers,
259
+ });
260
+ if (authorization.success === false) {
261
+ return authorization;
262
+ }
263
+ return {
264
+ success: true,
265
+ data: result.data,
266
+ publisherId: result.publisherId,
267
+ subjectId: result.subjectId,
268
+ recordName,
269
+ updatePolicy: (_b = result.updatePolicy) !== null && _b !== void 0 ? _b : true,
270
+ deletePolicy: (_c = result.deletePolicy) !== null && _c !== void 0 ? _c : true,
271
+ markers: markers,
272
+ };
273
+ }
274
+ catch (err) {
275
+ const span = trace.getActiveSpan();
276
+ if (err instanceof Error) {
277
+ span === null || span === void 0 ? void 0 : span.recordException(err);
278
+ }
279
+ span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
280
+ console.error('[DataRecordsController] An error occurred while getting data:', err);
281
+ return {
282
+ success: false,
283
+ errorCode: 'server_error',
284
+ errorMessage: 'A server error occurred.',
285
+ };
286
+ }
295
287
  }
296
288
  /**
297
289
  * Lists some data from the given record, starting after the given address.
@@ -300,114 +292,114 @@ export class DataRecordsController {
300
292
  * @param userId The ID of the user who is retrieving the data. If null, then it is assumed that the user is not logged in.
301
293
  * @param instances The instances that are loaded.
302
294
  */
303
- listData(recordName, address, userId, instances) {
304
- return __awaiter(this, void 0, void 0, function* () {
305
- try {
306
- const baseRequest = {
307
- recordKeyOrRecordName: recordName,
308
- userId,
309
- instances,
310
- };
311
- const context = yield this._policies.constructAuthorizationContext(baseRequest);
312
- if (context.success === false) {
313
- return context;
314
- }
315
- const authorization = yield this._policies.authorizeUserAndInstances(context.context, {
316
- userId,
317
- instances,
318
- resourceKind: 'data',
319
- action: 'list',
320
- markers: [ACCOUNT_MARKER],
321
- });
322
- const result2 = yield this._store.listData(context.context.recordName, address);
323
- if (result2.success === false) {
324
- return {
325
- success: false,
326
- errorCode: result2.errorCode,
327
- errorMessage: result2.errorMessage,
328
- };
329
- }
330
- if (authorization.success === false) {
331
- return authorization;
332
- }
333
- return {
334
- success: true,
335
- recordName: context.context.recordName,
336
- items: result2.items,
337
- totalCount: result2.totalCount,
338
- };
295
+ async listData(recordName, address, userId, instances) {
296
+ try {
297
+ const baseRequest = {
298
+ recordKeyOrRecordName: recordName,
299
+ userId,
300
+ instances,
301
+ };
302
+ const context = await this._policies.constructAuthorizationContext(baseRequest);
303
+ if (context.success === false) {
304
+ return context;
339
305
  }
340
- catch (err) {
341
- const span = trace.getActiveSpan();
342
- span === null || span === void 0 ? void 0 : span.recordException(err);
343
- span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
344
- console.error('[DataRecordsController] An error occurred while listing data:', err);
306
+ const authorization = await this._policies.authorizeUserAndInstances(context.context, {
307
+ userId,
308
+ instances,
309
+ resourceKind: 'data',
310
+ action: 'list',
311
+ markers: [ACCOUNT_MARKER],
312
+ });
313
+ const result2 = await this._store.listData(context.context.recordName, address);
314
+ if (result2.success === false) {
345
315
  return {
346
316
  success: false,
347
- errorCode: 'server_error',
348
- errorMessage: 'A server error occurred.',
317
+ errorCode: result2.errorCode,
318
+ errorMessage: result2.errorMessage,
349
319
  };
350
320
  }
351
- });
321
+ if (authorization.success === false) {
322
+ return authorization;
323
+ }
324
+ return {
325
+ success: true,
326
+ recordName: context.context.recordName,
327
+ items: result2.items,
328
+ totalCount: result2.totalCount,
329
+ };
330
+ }
331
+ catch (err) {
332
+ const span = trace.getActiveSpan();
333
+ if (err instanceof Error) {
334
+ span === null || span === void 0 ? void 0 : span.recordException(err);
335
+ }
336
+ span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
337
+ console.error('[DataRecordsController] An error occurred while listing data:', err);
338
+ return {
339
+ success: false,
340
+ errorCode: 'server_error',
341
+ errorMessage: 'A server error occurred.',
342
+ };
343
+ }
352
344
  }
353
345
  /**
354
346
  * Lists some data from the given record, filtered by the given marker and starting at the given address.
355
347
  * @param request The request that should be used to list the data.
356
348
  */
357
- listDataByMarker(request) {
358
- return __awaiter(this, void 0, void 0, function* () {
359
- try {
360
- const baseRequest = {
361
- recordKeyOrRecordName: request.recordKeyOrName,
362
- userId: request.userId,
363
- };
364
- const context = yield this._policies.constructAuthorizationContext(baseRequest);
365
- if (context.success === false) {
366
- return context;
367
- }
368
- const authorization = yield this._policies.authorizeUserAndInstances(context.context, {
369
- userId: context.context.userId,
370
- instances: request.instances,
371
- resourceKind: 'data',
372
- action: 'list',
373
- markers: [request.marker],
374
- });
375
- const result2 = yield this._store.listDataByMarker({
376
- recordName: context.context.recordName,
377
- marker: request.marker,
378
- startingAddress: request.startingAddress,
379
- sort: request.sort,
380
- });
381
- if (result2.success === false) {
382
- return {
383
- success: false,
384
- errorCode: result2.errorCode,
385
- errorMessage: result2.errorMessage,
386
- };
387
- }
388
- if (authorization.success === false) {
389
- return authorization;
390
- }
391
- return {
392
- success: true,
393
- recordName: context.context.recordName,
394
- items: result2.items,
395
- totalCount: result2.totalCount,
396
- marker: result2.marker,
397
- };
349
+ async listDataByMarker(request) {
350
+ try {
351
+ const baseRequest = {
352
+ recordKeyOrRecordName: request.recordKeyOrName,
353
+ userId: request.userId,
354
+ };
355
+ const context = await this._policies.constructAuthorizationContext(baseRequest);
356
+ if (context.success === false) {
357
+ return context;
398
358
  }
399
- catch (err) {
400
- const span = trace.getActiveSpan();
401
- span === null || span === void 0 ? void 0 : span.recordException(err);
402
- span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
403
- console.error('[DataRecordsController] An error occurred while listing data by marker:', err);
359
+ const authorization = await this._policies.authorizeUserAndInstances(context.context, {
360
+ userId: context.context.userId,
361
+ instances: request.instances,
362
+ resourceKind: 'data',
363
+ action: 'list',
364
+ markers: [request.marker],
365
+ });
366
+ const result2 = await this._store.listDataByMarker({
367
+ recordName: context.context.recordName,
368
+ marker: request.marker,
369
+ startingAddress: request.startingAddress,
370
+ sort: request.sort,
371
+ });
372
+ if (result2.success === false) {
404
373
  return {
405
374
  success: false,
406
- errorCode: 'server_error',
407
- errorMessage: 'A server error occurred.',
375
+ errorCode: result2.errorCode,
376
+ errorMessage: result2.errorMessage,
408
377
  };
409
378
  }
410
- });
379
+ if (authorization.success === false) {
380
+ return authorization;
381
+ }
382
+ return {
383
+ success: true,
384
+ recordName: context.context.recordName,
385
+ items: result2.items,
386
+ totalCount: result2.totalCount,
387
+ marker: result2.marker,
388
+ };
389
+ }
390
+ catch (err) {
391
+ const span = trace.getActiveSpan();
392
+ if (err instanceof Error) {
393
+ span === null || span === void 0 ? void 0 : span.recordException(err);
394
+ }
395
+ span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
396
+ console.error('[DataRecordsController] An error occurred while listing data by marker:', err);
397
+ return {
398
+ success: false,
399
+ errorCode: 'server_error',
400
+ errorMessage: 'A server error occurred.',
401
+ };
402
+ }
411
403
  }
412
404
  /**
413
405
  * Erases the data in the given record and address.
@@ -417,91 +409,91 @@ export class DataRecordsController {
417
409
  * @param subjectId The ID of the user that this request came from.
418
410
  * @param instances The instances that are loaded.
419
411
  */
420
- eraseData(recordKeyOrName, address, subjectId, instances) {
412
+ async eraseData(recordKeyOrName, address, subjectId, instances) {
421
413
  var _a, _b;
422
- return __awaiter(this, void 0, void 0, function* () {
423
- try {
424
- const context = yield this._policies.constructAuthorizationContext({
425
- recordKeyOrRecordName: recordKeyOrName,
426
- userId: subjectId,
427
- });
428
- if (context.success === false) {
429
- return context;
430
- }
431
- const policy = context.context.subjectPolicy;
432
- if (!subjectId && policy !== 'subjectless') {
433
- return {
434
- success: false,
435
- errorCode: 'not_logged_in',
436
- errorMessage: 'The user must be logged in in order to erase data using the provided record key.',
437
- };
438
- }
439
- if (policy === 'subjectless') {
440
- subjectId = null;
441
- }
442
- const recordName = context.context.recordName;
443
- const existingRecord = yield this._store.getData(recordName, address);
444
- const markers = (_a = (existingRecord.success
445
- ? existingRecord.markers
446
- : null)) !== null && _a !== void 0 ? _a : [PUBLIC_READ_MARKER];
447
- const authorization = yield this._policies.authorizeUserAndInstances(context.context, {
448
- userId: subjectId,
449
- instances,
450
- resourceKind: 'data',
451
- resourceId: address,
452
- action: 'delete',
453
- markers: markers,
454
- });
455
- // const authorization =
456
- // await this._policies.authorizeRequestUsingContext(
457
- // context.context,
458
- // {
459
- // action: 'data.delete',
460
- // ...baseRequest,
461
- // address,
462
- // resourceMarkers: markers,
463
- // }
464
- // );
465
- if (authorization.success === false) {
466
- return authorization;
467
- }
468
- if (existingRecord.success) {
469
- const existingDeletePolicy = (_b = existingRecord.deletePolicy) !== null && _b !== void 0 ? _b : true;
470
- if (subjectId !== context.context.recordOwnerId &&
471
- !doesSubjectMatchPolicy(existingDeletePolicy, subjectId)) {
472
- return {
473
- success: false,
474
- errorCode: 'not_authorized',
475
- errorMessage: 'The deletePolicy does not permit this user to erase the data record.',
476
- };
477
- }
478
- }
479
- const result2 = yield this._store.eraseData(recordName, address);
480
- if (result2.success === false) {
414
+ try {
415
+ const context = await this._policies.constructAuthorizationContext({
416
+ recordKeyOrRecordName: recordKeyOrName,
417
+ userId: subjectId,
418
+ });
419
+ if (context.success === false) {
420
+ return context;
421
+ }
422
+ const policy = context.context.subjectPolicy;
423
+ if (!subjectId && policy !== 'subjectless') {
424
+ return {
425
+ success: false,
426
+ errorCode: 'not_logged_in',
427
+ errorMessage: 'The user must be logged in in order to erase data using the provided record key.',
428
+ };
429
+ }
430
+ if (policy === 'subjectless') {
431
+ subjectId = null;
432
+ }
433
+ const recordName = context.context.recordName;
434
+ const existingRecord = await this._store.getData(recordName, address);
435
+ const markers = (_a = (existingRecord.success
436
+ ? existingRecord.markers
437
+ : null)) !== null && _a !== void 0 ? _a : [PUBLIC_READ_MARKER];
438
+ const authorization = await this._policies.authorizeUserAndInstances(context.context, {
439
+ userId: subjectId,
440
+ instances,
441
+ resourceKind: 'data',
442
+ resourceId: address,
443
+ action: 'delete',
444
+ markers: markers,
445
+ });
446
+ // const authorization =
447
+ // await this._policies.authorizeRequestUsingContext(
448
+ // context.context,
449
+ // {
450
+ // action: 'data.delete',
451
+ // ...baseRequest,
452
+ // address,
453
+ // resourceMarkers: markers,
454
+ // }
455
+ // );
456
+ if (authorization.success === false) {
457
+ return authorization;
458
+ }
459
+ if (existingRecord.success) {
460
+ const existingDeletePolicy = (_b = existingRecord.deletePolicy) !== null && _b !== void 0 ? _b : true;
461
+ if (subjectId !== context.context.recordOwnerId &&
462
+ !doesSubjectMatchPolicy(existingDeletePolicy, subjectId)) {
481
463
  return {
482
464
  success: false,
483
- errorCode: result2.errorCode,
484
- errorMessage: result2.errorMessage,
465
+ errorCode: 'not_authorized',
466
+ errorMessage: 'The deletePolicy does not permit this user to erase the data record.',
485
467
  };
486
468
  }
487
- return {
488
- success: true,
489
- recordName,
490
- address,
491
- };
492
469
  }
493
- catch (err) {
494
- const span = trace.getActiveSpan();
495
- span === null || span === void 0 ? void 0 : span.recordException(err);
496
- span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
497
- console.error(`[DataRecordsController] A server error occurred while erasing data:`, err);
470
+ const result2 = await this._store.eraseData(recordName, address);
471
+ if (result2.success === false) {
498
472
  return {
499
473
  success: false,
500
- errorCode: 'server_error',
501
- errorMessage: 'A server error occurred.',
474
+ errorCode: result2.errorCode,
475
+ errorMessage: result2.errorMessage,
502
476
  };
503
477
  }
504
- });
478
+ return {
479
+ success: true,
480
+ recordName,
481
+ address,
482
+ };
483
+ }
484
+ catch (err) {
485
+ const span = trace.getActiveSpan();
486
+ if (err instanceof Error) {
487
+ span === null || span === void 0 ? void 0 : span.recordException(err);
488
+ }
489
+ span === null || span === void 0 ? void 0 : span.setStatus({ code: SpanStatusCode.ERROR });
490
+ console.error(`[DataRecordsController] A server error occurred while erasing data:`, err);
491
+ return {
492
+ success: false,
493
+ errorCode: 'server_error',
494
+ errorMessage: 'A server error occurred.',
495
+ };
496
+ }
505
497
  }
506
498
  }
507
499
  __decorate([