@open-pencil/cloud 0.0.0-stage → 0.16.0

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 (279) hide show
  1. package/LICENSE +202 -0
  2. package/LICENSE-MIT +21 -0
  3. package/NOTICE +7 -0
  4. package/README.md +37 -2
  5. package/dist/bin/ai-relay.d.mts +1 -0
  6. package/dist/bin/ai-relay.mjs +31 -0
  7. package/dist/bin/ai-relay.mjs.map +1 -0
  8. package/dist/bin/server.d.mts +1 -0
  9. package/dist/bin/server.mjs +29 -0
  10. package/dist/bin/server.mjs.map +1 -0
  11. package/dist/client.d.ts +1520 -0
  12. package/dist/client.d.ts.map +1 -0
  13. package/dist/client.js +1917 -0
  14. package/dist/client.js.map +1 -0
  15. package/dist/contract.d.ts +2 -0
  16. package/dist/contract.js +2 -0
  17. package/dist/contract2.js +1453 -0
  18. package/dist/contract2.js.map +1 -0
  19. package/dist/email.d.ts +169 -0
  20. package/dist/email.d.ts.map +1 -0
  21. package/dist/email.js +670 -0
  22. package/dist/email.js.map +1 -0
  23. package/dist/index.d.ts +1755 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/listen.mjs +1881 -0
  26. package/dist/listen.mjs.map +1 -0
  27. package/dist/runtime-node.d.mts +10040 -0
  28. package/dist/runtime-node.d.mts.map +1 -0
  29. package/dist/runtime-node.mjs +32 -0
  30. package/dist/runtime-node.mjs.map +1 -0
  31. package/dist/server.d.ts +12792 -0
  32. package/dist/server.d.ts.map +1 -0
  33. package/dist/server.js +8475 -0
  34. package/dist/server.js.map +1 -0
  35. package/dist/server.mjs +8324 -0
  36. package/dist/server.mjs.map +1 -0
  37. package/dist/server2.mjs +505 -0
  38. package/dist/server2.mjs.map +1 -0
  39. package/package.json +124 -4
  40. package/src/admin/api/routes.ts +223 -0
  41. package/src/admin/api/security.ts +12 -0
  42. package/src/admin/audit/service.ts +138 -0
  43. package/src/admin/email/service.ts +140 -0
  44. package/src/admin/enrollment/email.ts +77 -0
  45. package/src/admin/enrollment/service.ts +308 -0
  46. package/src/admin/errors.ts +20 -0
  47. package/src/admin/index.ts +19 -0
  48. package/src/admin/list.ts +61 -0
  49. package/src/admin/operations/service.ts +70 -0
  50. package/src/admin/schema.ts +28 -0
  51. package/src/admin/users/service.ts +369 -0
  52. package/src/ai-relay/app.ts +251 -0
  53. package/src/ai-relay/config.ts +120 -0
  54. package/src/ai-relay/db/index.ts +10 -0
  55. package/src/ai-relay/db/migrate.ts +32 -0
  56. package/src/ai-relay/db/migrations/001_usage.ts +26 -0
  57. package/src/ai-relay/db/schema.ts +20 -0
  58. package/src/ai-relay/index.ts +18 -0
  59. package/src/ai-relay/metering.ts +68 -0
  60. package/src/ai-relay/usage.ts +115 -0
  61. package/src/client/ai.ts +96 -0
  62. package/src/client/api.ts +528 -0
  63. package/src/client/auth/client.ts +188 -0
  64. package/src/client/auth/failure.ts +54 -0
  65. package/src/client/auth/navigation.ts +45 -0
  66. package/src/client/device-authorization.ts +134 -0
  67. package/src/client/discovery.ts +105 -0
  68. package/src/client/images.ts +81 -0
  69. package/src/client/index.ts +53 -0
  70. package/src/client/portal/account.ts +183 -0
  71. package/src/client/portal/administration.ts +130 -0
  72. package/src/client/portal/client.ts +18 -0
  73. package/src/client/portal/request.ts +115 -0
  74. package/src/client/response.ts +51 -0
  75. package/src/client/workspaces.ts +142 -0
  76. package/src/contract/account.ts +204 -0
  77. package/src/contract/admin/index.ts +187 -0
  78. package/src/contract/ai.ts +150 -0
  79. package/src/contract/collaboration.ts +45 -0
  80. package/src/contract/discovery.ts +81 -0
  81. package/src/contract/documents.ts +265 -0
  82. package/src/contract/entitlements.ts +28 -0
  83. package/src/contract/index.ts +204 -0
  84. package/src/contract/links.ts +87 -0
  85. package/src/contract/portal.ts +11 -0
  86. package/src/contract/relay.ts +368 -0
  87. package/src/contract/sharing.ts +191 -0
  88. package/src/contract/versions.ts +87 -0
  89. package/src/contract/workspaces.ts +100 -0
  90. package/src/email/index.ts +14 -0
  91. package/src/email/render.ts +276 -0
  92. package/src/email/schema.ts +144 -0
  93. package/src/email/templates/brand.ts +105 -0
  94. package/src/email/templates/document-invitation.ts +35 -0
  95. package/src/email/templates/enrollment.ts +27 -0
  96. package/src/email/templates/layout.ts +18 -0
  97. package/src/runtime/cloudflare/email.ts +70 -0
  98. package/src/runtime/cloudflare/environment.ts +57 -0
  99. package/src/runtime/cloudflare/index.ts +3 -0
  100. package/src/runtime/cloudflare/relay.ts +136 -0
  101. package/src/runtime/cloudflare/site.ts +13 -0
  102. package/src/runtime/cloudflare/tsconfig.json +21 -0
  103. package/src/runtime/cloudflare/worker.ts +160 -0
  104. package/src/runtime/node/admin.ts +59 -0
  105. package/src/runtime/node/ai-relay/main.ts +35 -0
  106. package/src/runtime/node/ai-relay/server.ts +63 -0
  107. package/src/runtime/node/application.ts +50 -0
  108. package/src/runtime/node/bootstrap.ts +22 -0
  109. package/src/runtime/node/cleanup.ts +24 -0
  110. package/src/runtime/node/command.ts +17 -0
  111. package/src/runtime/node/config.ts +16 -0
  112. package/src/runtime/node/email/runtime.ts +43 -0
  113. package/src/runtime/node/email/transport.ts +112 -0
  114. package/src/runtime/node/entitlements/main.ts +41 -0
  115. package/src/runtime/node/entitlements/refusal.ts +15 -0
  116. package/src/runtime/node/index.ts +25 -0
  117. package/src/runtime/node/listen.ts +35 -0
  118. package/src/runtime/node/main.ts +29 -0
  119. package/src/runtime/node/migrate.ts +13 -0
  120. package/src/runtime/node/quota-reconcile.ts +9 -0
  121. package/src/runtime/node/relay.ts +54 -0
  122. package/src/runtime/node/server.ts +121 -0
  123. package/src/runtime/node/site.ts +64 -0
  124. package/src/runtime/postgres/database.ts +19 -0
  125. package/src/runtime/s3/objects.ts +244 -0
  126. package/src/runtime/site/server.ts +82 -0
  127. package/src/server/account/actions.ts +66 -0
  128. package/src/server/account/routes.ts +300 -0
  129. package/src/server/ai/errors.ts +7 -0
  130. package/src/server/ai/index.ts +4 -0
  131. package/src/server/ai/keys.ts +43 -0
  132. package/src/server/ai/routes.ts +43 -0
  133. package/src/server/ai/service.ts +70 -0
  134. package/src/server/api/index.ts +8 -0
  135. package/src/server/api/router.ts +117 -0
  136. package/src/server/api/types.ts +7 -0
  137. package/src/server/app.ts +375 -0
  138. package/src/server/auth/account.ts +73 -0
  139. package/src/server/auth/activity.ts +213 -0
  140. package/src/server/auth/adapter.ts +79 -0
  141. package/src/server/auth/email.ts +125 -0
  142. package/src/server/auth/endpoints.ts +57 -0
  143. package/src/server/auth/factory.ts +583 -0
  144. package/src/server/auth/index.ts +17 -0
  145. package/src/server/auth/mfa-assurance.ts +55 -0
  146. package/src/server/auth/runtime.ts +26 -0
  147. package/src/server/auth/session.ts +19 -0
  148. package/src/server/auth/social-providers.ts +55 -0
  149. package/src/server/auth/user-agent.ts +16 -0
  150. package/src/server/auth/workspace-access.ts +23 -0
  151. package/src/server/cleanup/configured.ts +38 -0
  152. package/src/server/cleanup/documents.ts +231 -0
  153. package/src/server/cleanup/index.ts +29 -0
  154. package/src/server/cleanup/objects.ts +27 -0
  155. package/src/server/cleanup/revisions.ts +232 -0
  156. package/src/server/cleanup/uploads.ts +129 -0
  157. package/src/server/cleanup/worker.ts +79 -0
  158. package/src/server/collaboration/index.ts +28 -0
  159. package/src/server/collaboration/relay/authorize.ts +53 -0
  160. package/src/server/collaboration/relay/awareness.ts +104 -0
  161. package/src/server/collaboration/relay/documents.ts +39 -0
  162. package/src/server/collaboration/relay/hub.ts +524 -0
  163. package/src/server/collaboration/relay/index.ts +15 -0
  164. package/src/server/collaboration/relay/persistence.ts +48 -0
  165. package/src/server/collaboration/routes.ts +77 -0
  166. package/src/server/collaboration/service.ts +188 -0
  167. package/src/server/config/deployment.ts +178 -0
  168. package/src/server/config/index.ts +20 -0
  169. package/src/server/config/limits.ts +8 -0
  170. package/src/server/config/public.ts +66 -0
  171. package/src/server/config/schema.ts +512 -0
  172. package/src/server/db/database.ts +15 -0
  173. package/src/server/db/expressions.ts +13 -0
  174. package/src/server/db/index.ts +21 -0
  175. package/src/server/db/migrate.ts +177 -0
  176. package/src/server/db/migrations/001_foundation.ts +164 -0
  177. package/src/server/db/migrations/002_upload_cleanup_claims.ts +34 -0
  178. package/src/server/db/migrations/003_document_cleanup_claims.ts +23 -0
  179. package/src/server/db/migrations/004_document_sharing.ts +82 -0
  180. package/src/server/db/migrations/005_document_collaboration_epoch.ts +13 -0
  181. package/src/server/db/migrations/006_invitation_continuation.ts +27 -0
  182. package/src/server/db/migrations/007_collaboration_state.ts +25 -0
  183. package/src/server/db/migrations/008_storage_reservations.ts +37 -0
  184. package/src/server/db/migrations/009_workspace_entitlements.ts +22 -0
  185. package/src/server/db/migrations/010_upload_finalization.ts +22 -0
  186. package/src/server/db/migrations/011_upload_finalization_lease.ts +12 -0
  187. package/src/server/db/migrations/012_transactional_email.ts +48 -0
  188. package/src/server/db/migrations/013_admin_enrollment.ts +54 -0
  189. package/src/server/db/migrations/014_enrollment_email_kinds.ts +30 -0
  190. package/src/server/db/migrations/015_cloud_rate_limit.ts +22 -0
  191. package/src/server/db/migrations/016_auth_schema.ts +16 -0
  192. package/src/server/db/migrations/017_authentication_email_kinds.ts +32 -0
  193. package/src/server/db/migrations/018_authentication_mfa.ts +78 -0
  194. package/src/server/db/migrations/019_better_auth_account_issuer.ts +42 -0
  195. package/src/server/db/migrations/020_workspace_invitations.ts +57 -0
  196. package/src/server/db/migrations/021_document_versions.ts +66 -0
  197. package/src/server/db/migrations/022_entitlement_external_version.ts +17 -0
  198. package/src/server/db/migrations/023_document_images.ts +61 -0
  199. package/src/server/db/migrations/024_account_activity.ts +66 -0
  200. package/src/server/db/migrations/025_table_names.ts +58 -0
  201. package/src/server/db/migrations/026_user_picture.ts +30 -0
  202. package/src/server/db/migrations/027_document_kind.ts +19 -0
  203. package/src/server/db/migrations/helpers.ts +78 -0
  204. package/src/server/db/tables.ts +394 -0
  205. package/src/server/documents/access.ts +103 -0
  206. package/src/server/documents/errors.ts +16 -0
  207. package/src/server/documents/images.ts +407 -0
  208. package/src/server/documents/index.ts +14 -0
  209. package/src/server/documents/limits.ts +57 -0
  210. package/src/server/documents/repository.ts +141 -0
  211. package/src/server/documents/routes.ts +239 -0
  212. package/src/server/documents/service.ts +597 -0
  213. package/src/server/documents/summary.ts +37 -0
  214. package/src/server/documents/types.ts +14 -0
  215. package/src/server/documents/versions/checkpoint.ts +98 -0
  216. package/src/server/documents/versions/history.ts +25 -0
  217. package/src/server/documents/versions/index.ts +4 -0
  218. package/src/server/documents/versions/routes.ts +112 -0
  219. package/src/server/documents/versions/service.ts +397 -0
  220. package/src/server/email/crypto.ts +57 -0
  221. package/src/server/email/index.ts +18 -0
  222. package/src/server/email/invitations.ts +30 -0
  223. package/src/server/email/service.ts +245 -0
  224. package/src/server/email/types.ts +56 -0
  225. package/src/server/email/worker.ts +33 -0
  226. package/src/server/index.ts +279 -0
  227. package/src/server/internal/index.ts +8 -0
  228. package/src/server/internal/routes.ts +41 -0
  229. package/src/server/internal/schema.ts +10 -0
  230. package/src/server/internal/service.ts +44 -0
  231. package/src/server/invitations/index.ts +6 -0
  232. package/src/server/invitations/types.ts +30 -0
  233. package/src/server/objects/index.ts +16 -0
  234. package/src/server/objects/types.ts +81 -0
  235. package/src/server/pictures/index.ts +9 -0
  236. package/src/server/pictures/provider.ts +61 -0
  237. package/src/server/pictures/routes.ts +21 -0
  238. package/src/server/pictures/service.ts +235 -0
  239. package/src/server/policy/configured.ts +23 -0
  240. package/src/server/policy/database.ts +108 -0
  241. package/src/server/policy/entitlements.ts +29 -0
  242. package/src/server/policy/index.ts +17 -0
  243. package/src/server/policy/keys.ts +23 -0
  244. package/src/server/policy/policy.ts +117 -0
  245. package/src/server/policy/provider.ts +67 -0
  246. package/src/server/policy/schema.ts +69 -0
  247. package/src/server/policy/service.ts +85 -0
  248. package/src/server/policy/static.ts +62 -0
  249. package/src/server/quota/index.ts +7 -0
  250. package/src/server/quota/reconcile.ts +61 -0
  251. package/src/server/quota/storage.ts +165 -0
  252. package/src/server/rate-limit/cleanup.ts +16 -0
  253. package/src/server/rate-limit/index.ts +12 -0
  254. package/src/server/rate-limit/keys.ts +38 -0
  255. package/src/server/rate-limit/middleware.ts +137 -0
  256. package/src/server/rate-limit/postgres.ts +79 -0
  257. package/src/server/sharing/access.ts +29 -0
  258. package/src/server/sharing/actor.ts +12 -0
  259. package/src/server/sharing/capabilities/service.ts +262 -0
  260. package/src/server/sharing/continuation.ts +85 -0
  261. package/src/server/sharing/contracts.ts +80 -0
  262. package/src/server/sharing/crypto.ts +48 -0
  263. package/src/server/sharing/document-routes.ts +41 -0
  264. package/src/server/sharing/errors.ts +12 -0
  265. package/src/server/sharing/grants/service.ts +79 -0
  266. package/src/server/sharing/http.ts +41 -0
  267. package/src/server/sharing/index.ts +39 -0
  268. package/src/server/sharing/invitations/service.ts +315 -0
  269. package/src/server/sharing/policy.ts +44 -0
  270. package/src/server/sharing/routes.ts +251 -0
  271. package/src/server/sharing/service.ts +117 -0
  272. package/src/server/validation.ts +24 -0
  273. package/src/server/worker/interval.ts +23 -0
  274. package/src/server/worker/periodic.ts +42 -0
  275. package/src/server/workspaces/index.ts +16 -0
  276. package/src/server/workspaces/members.ts +502 -0
  277. package/src/server/workspaces/repository.ts +91 -0
  278. package/src/server/workspaces/routes.ts +198 -0
  279. package/src/server/workspaces/service.ts +84 -0
@@ -0,0 +1,407 @@
1
+ import type {
2
+ CommitUploadInput,
3
+ DocumentImageDownload,
4
+ DocumentImageLink,
5
+ ImageUploadOffer,
6
+ RequestImageUploadsInput
7
+ } from '#cloud/contract'
8
+ import type { CloudDatabase } from '#cloud/server/db'
9
+ import type { ObjectStore } from '#cloud/server/objects'
10
+ import type { StorageQuotaService } from '#cloud/server/quota'
11
+ import { uniq, uniqBy } from 'es-toolkit'
12
+ import type { Kysely, Transaction } from 'kysely'
13
+
14
+ /** Images are stored as the bytes their file holds, whatever format those are. */
15
+ export const IMAGE_CONTENT_TYPE = 'application/octet-stream'
16
+
17
+ type Executor = Kysely<CloudDatabase> | Transaction<CloudDatabase>
18
+
19
+ /** An image no revision has used for this long may be released; a save uploads first. */
20
+ export const IMAGE_RELEASE_GRACE_MS = 24 * 60 * 60_000
21
+
22
+ /**
23
+ * The images a workspace's documents use, each stored once under the SHA-256 the object store
24
+ * verified, and which of them each stored revision uses under the names its file gives them.
25
+ */
26
+ export function createDocumentImages(
27
+ database: Kysely<CloudDatabase>,
28
+ objects: ObjectStore,
29
+ quota: StorageQuotaService
30
+ ) {
31
+ /**
32
+ * Gives up uploads a request reserved when it could not offer them all, so a retry is not
33
+ * refused for space they hold. A multipart upload the store began is aborted first; one that
34
+ * cannot be stays pending for cleanup, which aborts it once it expires.
35
+ */
36
+ async function abandonUploads(
37
+ uploads: readonly { uploadId: string; objectKey: string }[],
38
+ offers: readonly ImageUploadOffer[]
39
+ ): Promise<void> {
40
+ const begun = new Map(offers.map((offer) => [offer.uploadId, offer.upload]))
41
+ for (const { uploadId, objectKey } of uploads) {
42
+ try {
43
+ const upload = begun.get(uploadId)
44
+ if (upload?.kind === 'multipart') {
45
+ await objects.abortUpload({ key: objectKey, uploadId: upload.uploadId })
46
+ }
47
+ await quota.release(uploadId)
48
+ await database
49
+ .updateTable('upload')
50
+ .set({ status: 'abandoned' })
51
+ .where('id', '=', uploadId)
52
+ .where('status', '=', 'pending')
53
+ .execute()
54
+ } catch (error) {
55
+ // Cleanup releases it once it expires.
56
+ console.warn('Giving up an image upload failed', error)
57
+ }
58
+ }
59
+ }
60
+
61
+ return {
62
+ /**
63
+ * Uploads for the images of `input` the workspace lacks, each with its own object key and
64
+ * reservation, so an abandoned one is cleaned up like any other upload. Images it holds
65
+ * already need nothing.
66
+ */
67
+ async requestUploads(input: {
68
+ userId: string
69
+ documentId: string
70
+ workspaceId: string
71
+ images: RequestImageUploadsInput['images']
72
+ expiresAt: Date
73
+ maximumBytes: number | null
74
+ }): Promise<ImageUploadOffer[]> {
75
+ const requested = uniqBy(input.images, (image) => image.checksum)
76
+ // Held images are renewed, so none is released before the save that asked commits.
77
+ const held = new Set(
78
+ (
79
+ await database
80
+ .updateTable('workspaceImage')
81
+ .set({ touchedAt: new Date() })
82
+ .where('workspaceId', '=', input.workspaceId)
83
+ .where(
84
+ 'checksum',
85
+ 'in',
86
+ requested.map((image) => image.checksum)
87
+ )
88
+ .returning('checksum')
89
+ .execute()
90
+ ).map((row) => row.checksum)
91
+ )
92
+ const missing = requested
93
+ .filter((image) => !held.has(image.checksum))
94
+ .map((image) => {
95
+ const uploadId = crypto.randomUUID()
96
+ return {
97
+ ...image,
98
+ uploadId,
99
+ objectKey: `documents/${input.workspaceId}/images/${uploadId}`
100
+ }
101
+ })
102
+ if (missing.length === 0) return []
103
+ await database.transaction().execute(async (transaction) => {
104
+ for (const image of missing) {
105
+ await transaction
106
+ .insertInto('upload')
107
+ .values({
108
+ id: image.uploadId,
109
+ kind: 'image',
110
+ documentId: input.documentId,
111
+ baseRevisionId: null,
112
+ objectKey: image.objectKey,
113
+ checksum: image.checksum,
114
+ byteSize: image.byteSize,
115
+ contentType: IMAGE_CONTENT_TYPE,
116
+ multipartUploadId: null,
117
+ status: 'pending',
118
+ finalizationStartedAt: null,
119
+ createdBy: input.userId,
120
+ expiresAt: input.expiresAt
121
+ })
122
+ .execute()
123
+ await quota.reserveInTransaction(transaction, {
124
+ workspaceId: input.workspaceId,
125
+ uploadId: image.uploadId,
126
+ bytes: image.byteSize,
127
+ expiresAt: input.expiresAt,
128
+ maximumBytes: input.maximumBytes
129
+ })
130
+ }
131
+ })
132
+ const offers: ImageUploadOffer[] = []
133
+ try {
134
+ for (const image of missing) {
135
+ const upload = await objects.createUpload({
136
+ key: image.objectKey,
137
+ byteSize: image.byteSize,
138
+ checksum: image.checksum,
139
+ contentType: IMAGE_CONTENT_TYPE,
140
+ expiresAt: input.expiresAt
141
+ })
142
+ offers.push({ checksum: image.checksum, uploadId: image.uploadId, upload })
143
+ if (upload.kind === 'multipart') {
144
+ await database
145
+ .updateTable('upload')
146
+ .set({ multipartUploadId: upload.uploadId })
147
+ .where('id', '=', image.uploadId)
148
+ .execute()
149
+ }
150
+ }
151
+ } catch (error) {
152
+ await abandonUploads(missing, offers)
153
+ throw error
154
+ }
155
+ return offers
156
+ },
157
+
158
+ /**
159
+ * Stores a verified image upload as the workspace's copy of that image. When another upload of
160
+ * the same bytes got there first, this one's file is left unreferenced for cleanup to delete
161
+ * and its reservation is released rather than counted twice.
162
+ */
163
+ async landInTransaction(
164
+ transaction: Transaction<CloudDatabase>,
165
+ input: {
166
+ uploadId: string
167
+ workspaceId: string
168
+ objectKey: string
169
+ stored: { checksum: string; byteSize: number; contentType: string }
170
+ }
171
+ ): Promise<void> {
172
+ const storageObjectId = crypto.randomUUID()
173
+ await transaction
174
+ .insertInto('storageObject')
175
+ .values({
176
+ id: storageObjectId,
177
+ objectKey: input.objectKey,
178
+ checksum: input.stored.checksum,
179
+ byteSize: input.stored.byteSize,
180
+ contentType: input.stored.contentType
181
+ })
182
+ .execute()
183
+ const landed = await transaction
184
+ .insertInto('workspaceImage')
185
+ .values({
186
+ workspaceId: input.workspaceId,
187
+ checksum: input.stored.checksum,
188
+ storageObjectId
189
+ })
190
+ .onConflict((conflict) => conflict.columns(['workspaceId', 'checksum']).doNothing())
191
+ .returning('checksum')
192
+ .executeTakeFirst()
193
+ await transaction
194
+ .updateTable('upload')
195
+ .set({ status: 'committed', finalizationStartedAt: null })
196
+ .where('id', '=', input.uploadId)
197
+ .execute()
198
+ if (landed) await quota.commitInTransaction(transaction, input.uploadId)
199
+ else await quota.releaseInTransaction(transaction, input.uploadId)
200
+ },
201
+
202
+ /**
203
+ * Records the images a new revision uses. Each must be the workspace's already; holding them
204
+ * until the revision refers to them keeps a release of an unused image from racing the save.
205
+ * Whether every image was there.
206
+ */
207
+ async attachInTransaction(
208
+ transaction: Transaction<CloudDatabase>,
209
+ input: { workspaceId: string; revisionId: string; images: CommitUploadInput['images'] }
210
+ ): Promise<boolean> {
211
+ const images = uniqBy(input.images, (image) => image.name)
212
+ if (images.length === 0) return true
213
+ const checksums = uniq(images.map((image) => image.checksum))
214
+ const held = await transaction
215
+ .selectFrom('workspaceImage')
216
+ .select('checksum')
217
+ .where('workspaceId', '=', input.workspaceId)
218
+ .where('checksum', 'in', checksums)
219
+ .forShare()
220
+ .execute()
221
+ if (held.length !== checksums.length) return false
222
+ await transaction
223
+ .insertInto('revisionImage')
224
+ .values(
225
+ images.map((image) => ({
226
+ revisionId: input.revisionId,
227
+ name: image.name,
228
+ workspaceId: input.workspaceId,
229
+ checksum: image.checksum
230
+ }))
231
+ )
232
+ .execute()
233
+ return true
234
+ }
235
+ }
236
+ }
237
+
238
+ export type DocumentImages = ReturnType<typeof createDocumentImages>
239
+
240
+ /** Gives a revision made from another, by a restore or a duplicate, the same images. */
241
+ export async function copyRevisionImages(
242
+ transaction: Transaction<CloudDatabase>,
243
+ fromRevisionId: string,
244
+ toRevisionId: string
245
+ ): Promise<void> {
246
+ await transaction
247
+ .insertInto('revisionImage')
248
+ .columns(['revisionId', 'name', 'workspaceId', 'checksum'])
249
+ .expression((expression) =>
250
+ expression
251
+ .selectFrom('revisionImage')
252
+ .select((select) => [
253
+ select.val(toRevisionId).as('revisionId'),
254
+ 'name',
255
+ 'workspaceId',
256
+ 'checksum'
257
+ ])
258
+ .where('revisionId', '=', fromRevisionId)
259
+ )
260
+ .execute()
261
+ }
262
+
263
+ /** Where to download each image a revision uses, under its name in the file. */
264
+ export async function revisionImageDownloads(
265
+ executor: Executor,
266
+ objects: ObjectStore,
267
+ revisionId: string,
268
+ expiresAt: Date
269
+ ): Promise<DocumentImageDownload[]> {
270
+ const rows = await executor
271
+ .selectFrom('revisionImage')
272
+ .innerJoin('workspaceImage', (join) =>
273
+ join
274
+ .onRef('workspaceImage.workspaceId', '=', 'revisionImage.workspaceId')
275
+ .onRef('workspaceImage.checksum', '=', 'revisionImage.checksum')
276
+ )
277
+ .innerJoin('storageObject', 'storageObject.id', 'workspaceImage.storageObjectId')
278
+ .select([
279
+ 'revisionImage.name',
280
+ 'revisionImage.checksum',
281
+ 'storageObject.objectKey',
282
+ 'storageObject.byteSize'
283
+ ])
284
+ .where('revisionImage.revisionId', '=', revisionId)
285
+ .orderBy('revisionImage.name')
286
+ .execute()
287
+ return Promise.all(
288
+ rows.map(async (row) => ({
289
+ name: row.name,
290
+ checksum: row.checksum,
291
+ byteSize: Number(row.byteSize),
292
+ download: await objects.createDownload({ key: row.objectKey, expiresAt })
293
+ }))
294
+ )
295
+ }
296
+
297
+ /**
298
+ * Releases up to `batchSize` images no revision uses that no save has asked for since `before`:
299
+ * their bytes go back to the workspace's usage, and their files, no longer anyone's, are left for
300
+ * the object sweep. A save holds the images it refers to, so it either lands first and keeps them
301
+ * or finds them gone and uploads them again. How many were released and their bytes.
302
+ */
303
+ export async function releaseUnusedImages(
304
+ database: Kysely<CloudDatabase>,
305
+ before: Date,
306
+ batchSize: number
307
+ ): Promise<{ released: number; bytes: number }> {
308
+ return database.transaction().execute(async (transaction) => {
309
+ const unused = await transaction
310
+ .selectFrom('workspaceImage')
311
+ .innerJoin('storageObject', 'storageObject.id', 'workspaceImage.storageObjectId')
312
+ .select(['workspaceImage.workspaceId', 'workspaceImage.checksum', 'storageObject.byteSize'])
313
+ .where('workspaceImage.touchedAt', '<', before)
314
+ .where((expression) =>
315
+ expression.not(
316
+ expression.exists(
317
+ expression
318
+ .selectFrom('revisionImage')
319
+ .select('revisionImage.name')
320
+ .whereRef('revisionImage.workspaceId', '=', 'workspaceImage.workspaceId')
321
+ .whereRef('revisionImage.checksum', '=', 'workspaceImage.checksum')
322
+ )
323
+ )
324
+ )
325
+ .limit(batchSize)
326
+ .forUpdate('workspaceImage')
327
+ .skipLocked()
328
+ .execute()
329
+ let bytes = 0
330
+ for (const image of unused) {
331
+ await transaction
332
+ .deleteFrom('workspaceImage')
333
+ .where('workspaceId', '=', image.workspaceId)
334
+ .where('checksum', '=', image.checksum)
335
+ .execute()
336
+ await transaction
337
+ .updateTable('workspaceStorageUsage')
338
+ .set({
339
+ committedBytes: (expression) => expression('committedBytes', '-', Number(image.byteSize)),
340
+ updatedAt: new Date()
341
+ })
342
+ .where('workspaceId', '=', image.workspaceId)
343
+ .execute()
344
+ bytes += Number(image.byteSize)
345
+ }
346
+ return { released: unused.length, bytes }
347
+ })
348
+ }
349
+
350
+ /**
351
+ * Where to download the images of `checksums` the document's workspace holds, for someone who may
352
+ * view the document. A live room names images by checksum before any save does, and an editor
353
+ * whose image the workspace already holds through another document uploads nothing for this one,
354
+ * so the workspace, not the document, is what an image belongs to. Asking needs the image's
355
+ * SHA-256, which only someone with its bytes has.
356
+ */
357
+ export async function documentImageLinks(
358
+ executor: Executor,
359
+ objects: ObjectStore,
360
+ input: { documentId: string; checksums: string[]; expiresAt: Date }
361
+ ): Promise<DocumentImageLink[]> {
362
+ const rows = await executor
363
+ .selectFrom('workspaceImage')
364
+ .innerJoin('document', 'document.workspaceId', 'workspaceImage.workspaceId')
365
+ .innerJoin('storageObject', 'storageObject.id', 'workspaceImage.storageObjectId')
366
+ .select(['workspaceImage.checksum', 'storageObject.objectKey', 'storageObject.byteSize'])
367
+ .where('document.id', '=', input.documentId)
368
+ .where('workspaceImage.checksum', 'in', uniq(input.checksums))
369
+ .execute()
370
+ return Promise.all(
371
+ rows.map(async (row) => ({
372
+ checksum: row.checksum,
373
+ byteSize: Number(row.byteSize),
374
+ download: await objects.createDownload({ key: row.objectKey, expiresAt: input.expiresAt })
375
+ }))
376
+ )
377
+ }
378
+
379
+ /** The images the workspace's current revisions use, each counted once however many use it. */
380
+ export async function currentImageUsage(
381
+ executor: Executor,
382
+ workspaceId: string
383
+ ): Promise<{ objectCount: number; bytesUsed: number }> {
384
+ const usage = await executor
385
+ .selectFrom('workspaceImage')
386
+ .innerJoin('storageObject', 'storageObject.id', 'workspaceImage.storageObjectId')
387
+ .select((expression) => [
388
+ expression.fn.count('storageObject.id').as('objectCount'),
389
+ expression.fn
390
+ .coalesce(expression.fn.sum('storageObject.byteSize'), expression.val(0))
391
+ .as('bytesUsed')
392
+ ])
393
+ .where('workspaceImage.workspaceId', '=', workspaceId)
394
+ .where((expression) =>
395
+ expression.exists(
396
+ expression
397
+ .selectFrom('revisionImage')
398
+ .innerJoin('document', 'document.currentRevisionId', 'revisionImage.revisionId')
399
+ .select('revisionImage.name')
400
+ .whereRef('revisionImage.workspaceId', '=', 'workspaceImage.workspaceId')
401
+ .whereRef('revisionImage.checksum', '=', 'workspaceImage.checksum')
402
+ .where('document.deletedAt', 'is', null)
403
+ )
404
+ )
405
+ .executeTakeFirstOrThrow()
406
+ return { objectCount: Number(usage.objectCount), bytesUsed: Number(usage.bytesUsed) }
407
+ }
@@ -0,0 +1,14 @@
1
+ export {
2
+ canDeleteDocument,
3
+ canEditDocument,
4
+ resolveDocumentAccess,
5
+ type AccessDatabase
6
+ } from './access'
7
+ export { createDocumentRoutes, type DocumentRouteEnvironment } from './routes'
8
+ export {
9
+ DocumentConflictError,
10
+ DocumentForbiddenError,
11
+ DocumentNotFoundError,
12
+ UploadInvalidError
13
+ } from './errors'
14
+ export { createDocumentService, type DocumentService } from './service'
@@ -0,0 +1,57 @@
1
+ import type { CloudDatabase } from '#cloud/server/db'
2
+ import { canEditDocument, resolveDocumentAccess } from '#cloud/server/documents/access'
3
+ import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents/errors'
4
+ import type { DocumentServiceOptions } from '#cloud/server/documents/service'
5
+ import { documentSummary, getDocumentSummaryRow } from '#cloud/server/documents/summary'
6
+ import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
7
+ import type { Kysely } from 'kysely'
8
+
9
+ /**
10
+ * The document `userId` may write to, and how large one file and the workspace's storage may
11
+ * be for this save: technical limits, tightened by policy.
12
+ */
13
+ export async function writableDocument(
14
+ database: Kysely<CloudDatabase>,
15
+ options: Pick<DocumentServiceOptions, 'policy' | 'technicalMaximumUploadBytes'>,
16
+ userId: string,
17
+ documentId: string
18
+ ) {
19
+ const access = await resolveDocumentAccess(database, userId, documentId)
20
+ if (!access) throw new DocumentNotFoundError()
21
+ if (!canEditDocument(access)) throw new DocumentForbiddenError()
22
+ // Access was resolved just above, so the document is read without resolving it again.
23
+ const row = await getDocumentSummaryRow(database, documentId)
24
+ if (!row) throw new DocumentNotFoundError()
25
+ const document = documentSummary(row)
26
+ const policyContext = {
27
+ targetingKey: document.workspaceId,
28
+ actorId: userId,
29
+ workspaceId: document.workspaceId,
30
+ documentId,
31
+ deploymentMode: 'self-hosted' as const
32
+ }
33
+ const technicalMaximum = options.technicalMaximumUploadBytes ?? Number.MAX_SAFE_INTEGER
34
+ const maximumFileBytes = Math.min(
35
+ technicalMaximum,
36
+ options.policy
37
+ ? await options.policy.number(
38
+ CLOUD_FEATURE_KEYS.maximumFileBytes,
39
+ technicalMaximum,
40
+ policyContext
41
+ )
42
+ : technicalMaximum
43
+ )
44
+ const maximumStorageBytes = options.policy
45
+ ? await options.policy.number(
46
+ CLOUD_FEATURE_KEYS.maximumStorageBytes,
47
+ Number.MAX_SAFE_INTEGER,
48
+ policyContext
49
+ )
50
+ : Number.MAX_SAFE_INTEGER
51
+ return {
52
+ document,
53
+ maximumFileBytes,
54
+ maximumStorageBytes:
55
+ maximumStorageBytes === Number.MAX_SAFE_INTEGER ? null : maximumStorageBytes
56
+ }
57
+ }
@@ -0,0 +1,141 @@
1
+ import type { DocumentSummary, SharedDocument, WorkspaceRole } from '#cloud/contract'
2
+ import type { CloudDatabase } from '#cloud/server/db'
3
+ import type { CreateDocumentRecord } from '#cloud/server/documents/types'
4
+ import { uniq } from 'es-toolkit'
5
+ import type { Kysely, Transaction } from 'kysely'
6
+ import * as v from 'valibot'
7
+
8
+ import { resolveDocumentAccess } from './access'
9
+ import { documentSummary, getDocumentSummaryRow } from './summary'
10
+
11
+ const uuidSchema = v.pipe(v.string(), v.uuid())
12
+
13
+ export type DocumentDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
14
+
15
+ export type DocumentSummaryRow = Omit<DocumentSummary, 'createdAt' | 'updatedAt'> & {
16
+ createdAt: Date | string
17
+ updatedAt: Date | string
18
+ }
19
+
20
+ export async function workspaceRole(
21
+ database: DocumentDatabase,
22
+ userId: string,
23
+ workspaceId: string
24
+ ): Promise<WorkspaceRole | undefined> {
25
+ const member = await database
26
+ .selectFrom('workspaceMember')
27
+ .select('role')
28
+ .where('workspaceId', '=', workspaceId)
29
+ .where('userId', '=', userId)
30
+ .executeTakeFirst()
31
+ return member?.role
32
+ }
33
+
34
+ export async function listDocuments(
35
+ database: DocumentDatabase,
36
+ userId: string,
37
+ workspaceId: string
38
+ ): Promise<DocumentSummary[] | undefined> {
39
+ if (!(await workspaceRole(database, userId, workspaceId))) return undefined
40
+ const rows = await database
41
+ .selectFrom('document')
42
+ .select([
43
+ 'id',
44
+ 'workspaceId',
45
+ 'name',
46
+ 'kind',
47
+ 'currentRevisionId',
48
+ 'version',
49
+ 'createdAt',
50
+ 'updatedAt'
51
+ ])
52
+ .where('workspaceId', '=', workspaceId)
53
+ .where('deletedAt', 'is', null)
54
+ .orderBy('updatedAt', 'desc')
55
+ .execute()
56
+ return rows.map(documentSummary)
57
+ }
58
+
59
+ /** Documents granted to the person directly that live in workspaces they are not a member of. */
60
+ export async function listSharedDocuments(
61
+ database: DocumentDatabase,
62
+ userId: string
63
+ ): Promise<SharedDocument[]> {
64
+ const rows = await database
65
+ .selectFrom('documentGrant')
66
+ .innerJoin('document', 'document.id', 'documentGrant.documentId')
67
+ .innerJoin('workspace', 'workspace.id', 'document.workspaceId')
68
+ .select([
69
+ 'document.id',
70
+ 'document.workspaceId',
71
+ 'document.name',
72
+ 'document.kind',
73
+ 'document.currentRevisionId',
74
+ 'document.version',
75
+ 'document.createdAt',
76
+ 'document.updatedAt',
77
+ 'workspace.name as workspaceName',
78
+ 'documentGrant.permission',
79
+ 'documentGrant.createdAt as sharedAt',
80
+ 'documentGrant.createdBy'
81
+ ])
82
+ .where('documentGrant.userId', '=', userId)
83
+ .where('documentGrant.revokedAt', 'is', null)
84
+ .where('document.deletedAt', 'is', null)
85
+ .where((expression) =>
86
+ expression.not(
87
+ expression.exists(
88
+ expression
89
+ .selectFrom('workspaceMember')
90
+ .select('workspaceMember.userId')
91
+ .whereRef('workspaceMember.workspaceId', '=', 'document.workspaceId')
92
+ .where('workspaceMember.userId', '=', userId)
93
+ )
94
+ )
95
+ )
96
+ .orderBy('document.updatedAt', 'desc')
97
+ .execute()
98
+ // Grants can be made by operator tools whose IDs are not accounts, so only UUIDs are looked up.
99
+ const granters = uniq(rows.map((row) => row.createdBy)).filter((id) => v.is(uuidSchema, id))
100
+ const names = new Map(
101
+ granters.length === 0
102
+ ? []
103
+ : (
104
+ await database
105
+ .selectFrom('user')
106
+ .select(['id', 'name'])
107
+ .where('id', 'in', granters)
108
+ .execute()
109
+ ).map((user) => [user.id, user.name] as const)
110
+ )
111
+ return rows.map(({ workspaceName, permission, sharedAt, createdBy, ...row }) => {
112
+ const sharedByName = names.get(createdBy)
113
+ return {
114
+ ...documentSummary(row),
115
+ workspaceName,
116
+ permission,
117
+ sharedBy: sharedByName === undefined ? null : { name: sharedByName },
118
+ sharedAt: sharedAt instanceof Date ? sharedAt.toISOString() : sharedAt
119
+ }
120
+ })
121
+ }
122
+
123
+ export async function findDocument(
124
+ database: DocumentDatabase,
125
+ userId: string,
126
+ documentId: string
127
+ ): Promise<(DocumentSummary & { role: WorkspaceRole }) | undefined> {
128
+ const access = await resolveDocumentAccess(database, userId, documentId)
129
+ if (!access) return undefined
130
+ const row = await getDocumentSummaryRow(database, documentId)
131
+ if (!row) return undefined
132
+ const role: WorkspaceRole = access.permission === 'edit' ? 'editor' : 'viewer'
133
+ return { ...documentSummary(row), role }
134
+ }
135
+
136
+ export async function insertDocument(
137
+ database: DocumentDatabase,
138
+ input: CreateDocumentRecord
139
+ ): Promise<void> {
140
+ await database.insertInto('document').values(input).execute()
141
+ }