@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.5
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.
- package/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { lockForTransaction } from '#cloud/server/db/expressions'
|
|
3
|
+
import type { Transaction } from 'kysely'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How many invitations, to documents and workspaces alike, one person may send in a day, and how
|
|
7
|
+
* many one address may receive from everyone. Each sends an email naming the inviter and the file,
|
|
8
|
+
* both of which the sender chooses, so without a daily budget invitations are a way to send spam.
|
|
9
|
+
*/
|
|
10
|
+
export const INVITATION_BUDGET = {
|
|
11
|
+
windowMs: 24 * 60 * 60 * 1000,
|
|
12
|
+
perSender: 100,
|
|
13
|
+
perRecipient: 5
|
|
14
|
+
} as const
|
|
15
|
+
|
|
16
|
+
/** An invitation past the sender's or the recipient's daily budget. */
|
|
17
|
+
export class InvitationLimitError extends Error {
|
|
18
|
+
override readonly name = 'InvitationLimitError'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function sentSince(
|
|
22
|
+
transaction: Transaction<CloudDatabase>,
|
|
23
|
+
column: 'senderId' | 'emailNormalized',
|
|
24
|
+
value: string,
|
|
25
|
+
since: Date
|
|
26
|
+
): Promise<number> {
|
|
27
|
+
const row = await transaction
|
|
28
|
+
.selectFrom('invitationSend')
|
|
29
|
+
.select((expression) => expression.fn.countAll<string>().as('count'))
|
|
30
|
+
.where(column, '=', value)
|
|
31
|
+
.where('sentAt', '>', since)
|
|
32
|
+
.executeTakeFirstOrThrow()
|
|
33
|
+
return Number(row.count)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Counts an invitation from `senderId` to `email` against both daily budgets, in the transaction
|
|
38
|
+
* that creates it, or refuses it once either budget is used. Sends are logged apart from the
|
|
39
|
+
* invitations, so deleting a workspace or document gives nothing back, and the check holds locks
|
|
40
|
+
* on the sender and the recipient, so two invitations at once cannot both take the last one.
|
|
41
|
+
*/
|
|
42
|
+
export async function spendInvitationBudget(
|
|
43
|
+
transaction: Transaction<CloudDatabase>,
|
|
44
|
+
invitation: { senderId: string; email: string },
|
|
45
|
+
now = new Date()
|
|
46
|
+
): Promise<void> {
|
|
47
|
+
const email = invitation.email.trim().toLowerCase()
|
|
48
|
+
// Always sender first, then recipient, so two transactions never wait on each other in turn.
|
|
49
|
+
await lockForTransaction(transaction, `invitation-sender:${invitation.senderId}`)
|
|
50
|
+
await lockForTransaction(transaction, `invitation-recipient:${email}`)
|
|
51
|
+
const since = new Date(now.getTime() - INVITATION_BUDGET.windowMs)
|
|
52
|
+
const bySender = await sentSince(transaction, 'senderId', invitation.senderId, since)
|
|
53
|
+
const byRecipient = await sentSince(transaction, 'emailNormalized', email, since)
|
|
54
|
+
if (bySender >= INVITATION_BUDGET.perSender || byRecipient >= INVITATION_BUDGET.perRecipient) {
|
|
55
|
+
throw new InvitationLimitError()
|
|
56
|
+
}
|
|
57
|
+
await transaction
|
|
58
|
+
.insertInto('invitationSend')
|
|
59
|
+
.values({
|
|
60
|
+
id: crypto.randomUUID(),
|
|
61
|
+
senderId: invitation.senderId,
|
|
62
|
+
emailNormalized: email,
|
|
63
|
+
sentAt: now
|
|
64
|
+
})
|
|
65
|
+
.execute()
|
|
66
|
+
}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import type { DocumentPermission } from '#cloud/contract'
|
|
2
|
+
import type { EmailLocale } from '#cloud/email'
|
|
2
3
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
4
|
import type { Transaction } from 'kysely'
|
|
4
5
|
|
|
5
6
|
export type DocumentInvitationMessage = {
|
|
6
7
|
deliveryId: string
|
|
7
8
|
recipientEmail: string
|
|
8
|
-
|
|
9
|
+
/** The language the invitation is written in. */
|
|
10
|
+
locale: EmailLocale
|
|
11
|
+
/** Null when the inviter's account is gone, so the email says someone invited you. */
|
|
12
|
+
inviterName: string | null
|
|
9
13
|
documentName: string
|
|
10
14
|
permission: DocumentPermission
|
|
11
15
|
expiresAt: string
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { MiddlewareHandler } from 'hono'
|
|
2
|
+
|
|
3
|
+
export type CloudLogLevel = 'error' | 'warn' | 'info'
|
|
4
|
+
|
|
5
|
+
export type CloudLoggedError = {
|
|
6
|
+
name: string
|
|
7
|
+
message: string
|
|
8
|
+
stack?: string
|
|
9
|
+
cause?: CloudLoggedError
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** What a log line carries besides its message; never who made a request or what it carried. */
|
|
13
|
+
export type CloudLogFields = Record<
|
|
14
|
+
string,
|
|
15
|
+
string | number | boolean | undefined | CloudLoggedError
|
|
16
|
+
>
|
|
17
|
+
|
|
18
|
+
/** One structured log line. */
|
|
19
|
+
export type CloudLogLine = CloudLogFields & { level: CloudLogLevel; message: string }
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Writes a log line where the runtime's logs go, in the form they index best. Runtimes pass their
|
|
23
|
+
* own to `createLoggingErrorReporter`; `writeJSONLine` is the default.
|
|
24
|
+
*/
|
|
25
|
+
export type CloudLogWriter = (line: CloudLogLine) => void
|
|
26
|
+
|
|
27
|
+
/** Writes the line as one line of JSON, at the console method of its level. */
|
|
28
|
+
export const writeJSONLine: CloudLogWriter = (line) => {
|
|
29
|
+
console[line.level](JSON.stringify(line))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** How deep a chain of causes is written out. */
|
|
33
|
+
const MAXIMUM_CAUSES = 4
|
|
34
|
+
|
|
35
|
+
/** An error as a log line keeps it: its name, message, stack, and causes. */
|
|
36
|
+
export function loggedError(error: unknown, depth = 0): CloudLoggedError {
|
|
37
|
+
if (!(error instanceof Error)) return { name: 'NonError', message: String(error) }
|
|
38
|
+
const cause =
|
|
39
|
+
error.cause !== undefined && depth < MAXIMUM_CAUSES
|
|
40
|
+
? loggedError(error.cause, depth + 1)
|
|
41
|
+
: undefined
|
|
42
|
+
return { name: error.name, message: error.message, stack: error.stack, ...(cause && { cause }) }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Writes a warning or note through the default writer, from code no runtime hands a writer to.
|
|
47
|
+
* Errors go through `reportCloudError`, which logs with the runtime's.
|
|
48
|
+
*/
|
|
49
|
+
export function logCloud(level: CloudLogLevel, message: string, fields: CloudLogFields = {}): void {
|
|
50
|
+
writeJSONLine({ ...fields, level, message })
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Reads the ID that what stands in front of the server gave a request, if the runtime trusts it. */
|
|
54
|
+
export type ForwardedRequestId = (headers: Headers) => string | undefined
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Names each request as the `requestId` variable, from what the runtime trusts in front of it
|
|
58
|
+
* (such as Cloudflare's ray ID) or else a new UUID, and answers with it as `X-Request-Id` so a
|
|
59
|
+
* person reporting a failure can quote it. Nested servers each name the request; the innermost
|
|
60
|
+
* one that answered keeps its ID on the response.
|
|
61
|
+
*/
|
|
62
|
+
export function cloudRequestId(
|
|
63
|
+
forwarded?: ForwardedRequestId
|
|
64
|
+
): MiddlewareHandler<{ Variables: { requestId: string } }> {
|
|
65
|
+
return async (context, next) => {
|
|
66
|
+
const id = forwarded?.(context.req.raw.headers) ?? crypto.randomUUID()
|
|
67
|
+
context.set('requestId', id)
|
|
68
|
+
context.header('X-Request-Id', id)
|
|
69
|
+
await next()
|
|
70
|
+
// A response the handler built itself, such as a page an inner server answered, misses the
|
|
71
|
+
// prepared header; one an inner layer named keeps its ID. A socket upgrade is left alone.
|
|
72
|
+
if (context.res.status !== 101 && !context.res.headers.has('X-Request-Id')) {
|
|
73
|
+
context.header('X-Request-Id', id)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -90,4 +90,9 @@ export interface ObjectStore {
|
|
|
90
90
|
write(input: WriteObjectInput): Promise<void>
|
|
91
91
|
/** The bytes stored under `key`, or null when there are none. */
|
|
92
92
|
read(key: string): Promise<Uint8Array | null>
|
|
93
|
+
/**
|
|
94
|
+
* Bytes `start` up to `endExclusive` of the object under `key`, or null when there is none:
|
|
95
|
+
* enough of a large stored file to read one part of it, such as a document's thumbnail.
|
|
96
|
+
*/
|
|
97
|
+
readRange(key: string, start: number, endExclusive: number): Promise<Uint8Array | null>
|
|
93
98
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CLOUD_PICTURE_MAX_BYTES } from '#cloud/contract'
|
|
2
2
|
import type { CloudDatabase, UserPictureFormat } from '#cloud/server/db'
|
|
3
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
3
4
|
import { sha256 } from '@noble/hashes/sha2.js'
|
|
4
5
|
import { bytesToHex } from '@noble/hashes/utils.js'
|
|
5
6
|
import { imageSize } from 'image-size'
|
|
@@ -220,7 +221,7 @@ export function createUserPictures(database: Kysely<CloudDatabase>, options: Use
|
|
|
220
221
|
if (!bytes) throw new PictureInvalidError()
|
|
221
222
|
await save(userId, bytes, { from: 'provider', seen: current })
|
|
222
223
|
} catch (error) {
|
|
223
|
-
|
|
224
|
+
logCloud('warn', 'Keeping a linked account picture failed', { error: loggedError(error) })
|
|
224
225
|
await database
|
|
225
226
|
.updateTable('user')
|
|
226
227
|
.set({ image: null, updatedAt: new Date() })
|
|
@@ -8,16 +8,22 @@ import { createDefaultCloudPolicy } from './policy'
|
|
|
8
8
|
import { EntitlementOpenFeatureProvider } from './provider'
|
|
9
9
|
import { staticEntitlementValues } from './static'
|
|
10
10
|
|
|
11
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* The policy a deployment configures: its static entitlements, or the database's, over the
|
|
13
|
+
* defaults an account without stored entitlements takes.
|
|
14
|
+
*/
|
|
12
15
|
export function createConfiguredCloudPolicy(
|
|
13
|
-
config: Pick<CloudServerConfig, 'staticEntitlements'>,
|
|
16
|
+
config: Pick<CloudServerConfig, 'staticEntitlements' | 'defaultEntitlements'>,
|
|
14
17
|
database: Kysely<CloudDatabase>
|
|
15
18
|
) {
|
|
16
19
|
return createDefaultCloudPolicy(
|
|
17
20
|
new EntitlementOpenFeatureProvider(
|
|
18
21
|
config.staticEntitlements
|
|
19
22
|
? new StaticEntitlementSource(staticEntitlementValues(config.staticEntitlements))
|
|
20
|
-
: new DatabaseEntitlementSource(
|
|
23
|
+
: new DatabaseEntitlementSource(
|
|
24
|
+
database,
|
|
25
|
+
config.defaultEntitlements ? staticEntitlementValues(config.defaultEntitlements) : {}
|
|
26
|
+
)
|
|
21
27
|
)
|
|
22
28
|
)
|
|
23
29
|
}
|
|
@@ -6,17 +6,28 @@ import type { EntitlementSource, EntitlementSubject } from './entitlements'
|
|
|
6
6
|
import { staticEntitlementsSchema } from './schema'
|
|
7
7
|
import { staticEntitlementValues, type StaticEntitlementsInput } from './static'
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Each account's stored entitlements, value by value over `defaults`: what an account takes until
|
|
11
|
+
* billing or an operator stores its own, and whatever a stored plan leaves out.
|
|
12
|
+
*/
|
|
9
13
|
export class DatabaseEntitlementSource implements EntitlementSource {
|
|
10
|
-
constructor(
|
|
14
|
+
constructor(
|
|
15
|
+
private readonly database: Kysely<CloudDatabase>,
|
|
16
|
+
private readonly defaults: Record<string, boolean | number> = {}
|
|
17
|
+
) {}
|
|
11
18
|
|
|
12
19
|
private async values(subject: EntitlementSubject): Promise<Record<string, boolean | number>> {
|
|
13
|
-
if (subject.type !== '
|
|
20
|
+
if (subject.type !== 'user') return {}
|
|
14
21
|
const row = await this.database
|
|
15
|
-
.selectFrom('
|
|
22
|
+
.selectFrom('accountEntitlement')
|
|
16
23
|
.select('values')
|
|
17
|
-
.where('
|
|
24
|
+
.where('userId', '=', subject.id)
|
|
18
25
|
.executeTakeFirst()
|
|
19
|
-
|
|
26
|
+
if (!row) return this.defaults
|
|
27
|
+
return {
|
|
28
|
+
...this.defaults,
|
|
29
|
+
...staticEntitlementValues(v.parse(staticEntitlementsSchema, row.values))
|
|
30
|
+
}
|
|
20
31
|
}
|
|
21
32
|
|
|
22
33
|
async boolean(subject: EntitlementSubject, key: string): Promise<boolean | null> {
|
|
@@ -34,26 +45,43 @@ export class DatabaseEntitlementSource implements EntitlementSource {
|
|
|
34
45
|
}
|
|
35
46
|
}
|
|
36
47
|
|
|
37
|
-
|
|
48
|
+
/** Each account's stored entitlements, which every workspace it owns takes. */
|
|
49
|
+
const accountIdSchema = v.pipe(v.string(), v.uuid())
|
|
50
|
+
|
|
51
|
+
export function createAccountEntitlementRepository(database: Kysely<CloudDatabase>) {
|
|
38
52
|
return {
|
|
39
|
-
|
|
53
|
+
/**
|
|
54
|
+
* The account `userId` names, as the user table spells it, or null when there is none, so a
|
|
55
|
+
* plan is stored under the key policy reads, whatever case the caller wrote the ID in.
|
|
56
|
+
*/
|
|
57
|
+
async account(userId: string): Promise<string | null> {
|
|
58
|
+
if (!v.is(accountIdSchema, userId)) return null
|
|
59
|
+
const user = await database
|
|
60
|
+
.selectFrom('user')
|
|
61
|
+
.select('id')
|
|
62
|
+
.where('id', '=', userId)
|
|
63
|
+
.executeTakeFirst()
|
|
64
|
+
return user?.id ?? null
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
async get(userId: string) {
|
|
40
68
|
return database
|
|
41
|
-
.selectFrom('
|
|
69
|
+
.selectFrom('accountEntitlement')
|
|
42
70
|
.selectAll()
|
|
43
|
-
.where('
|
|
71
|
+
.where('userId', '=', userId)
|
|
44
72
|
.executeTakeFirst()
|
|
45
73
|
},
|
|
46
74
|
|
|
47
|
-
async set(
|
|
75
|
+
async set(userId: string, values: StaticEntitlementsInput, source: string) {
|
|
48
76
|
const validated = v.parse(staticEntitlementsSchema, values)
|
|
49
77
|
return database
|
|
50
|
-
.insertInto('
|
|
51
|
-
.values({
|
|
78
|
+
.insertInto('accountEntitlement')
|
|
79
|
+
.values({ userId, values: validated, source })
|
|
52
80
|
.onConflict((conflict) =>
|
|
53
|
-
conflict.column('
|
|
81
|
+
conflict.column('userId').doUpdateSet({
|
|
54
82
|
values: validated,
|
|
55
83
|
source,
|
|
56
|
-
revision: (expression) => expression('
|
|
84
|
+
revision: (expression) => expression('accountEntitlement.revision', '+', 1),
|
|
57
85
|
updatedAt: new Date()
|
|
58
86
|
})
|
|
59
87
|
)
|
|
@@ -67,42 +95,39 @@ export function createWorkspaceEntitlementRepository(database: Kysely<CloudDatab
|
|
|
67
95
|
* An unversioned row, such as one an operator set, is replaced.
|
|
68
96
|
*/
|
|
69
97
|
async setVersioned(
|
|
70
|
-
|
|
98
|
+
userId: string,
|
|
71
99
|
values: StaticEntitlementsInput,
|
|
72
100
|
source: string,
|
|
73
101
|
externalVersion: number
|
|
74
102
|
): Promise<{ applied: boolean }> {
|
|
75
103
|
const validated = v.parse(staticEntitlementsSchema, values)
|
|
76
104
|
const row = await database
|
|
77
|
-
.insertInto('
|
|
78
|
-
.values({
|
|
105
|
+
.insertInto('accountEntitlement')
|
|
106
|
+
.values({ userId, values: validated, source, externalVersion })
|
|
79
107
|
.onConflict((conflict) =>
|
|
80
108
|
conflict
|
|
81
|
-
.column('
|
|
109
|
+
.column('userId')
|
|
82
110
|
.doUpdateSet({
|
|
83
111
|
values: validated,
|
|
84
112
|
source,
|
|
85
113
|
externalVersion,
|
|
86
|
-
revision: (expression) => expression('
|
|
114
|
+
revision: (expression) => expression('accountEntitlement.revision', '+', 1),
|
|
87
115
|
updatedAt: new Date()
|
|
88
116
|
})
|
|
89
117
|
.where((expression) =>
|
|
90
118
|
expression.or([
|
|
91
|
-
expression('
|
|
92
|
-
expression('
|
|
119
|
+
expression('accountEntitlement.externalVersion', 'is', null),
|
|
120
|
+
expression('accountEntitlement.externalVersion', '<', externalVersion)
|
|
93
121
|
])
|
|
94
122
|
)
|
|
95
123
|
)
|
|
96
|
-
.returning('
|
|
124
|
+
.returning('userId')
|
|
97
125
|
.executeTakeFirst()
|
|
98
126
|
return { applied: row !== undefined }
|
|
99
127
|
},
|
|
100
128
|
|
|
101
|
-
async clear(
|
|
102
|
-
await database
|
|
103
|
-
.deleteFrom('workspaceEntitlement')
|
|
104
|
-
.where('workspaceId', '=', workspaceId)
|
|
105
|
-
.execute()
|
|
129
|
+
async clear(userId: string): Promise<void> {
|
|
130
|
+
await database.deleteFrom('accountEntitlement').where('userId', '=', userId).execute()
|
|
106
131
|
}
|
|
107
132
|
}
|
|
108
133
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { createAccountEntitlementRepository, DatabaseEntitlementSource } from './database'
|
|
2
|
+
export { workspacePolicyContext } from './workspace'
|
|
2
3
|
export {
|
|
3
4
|
StaticEntitlementSource,
|
|
4
5
|
type EntitlementSource,
|
|
@@ -9,12 +9,15 @@ export const CLOUD_FEATURE_KEYS = {
|
|
|
9
9
|
maximumFileBytes: 'cloud.documents.maximum-file-bytes',
|
|
10
10
|
maximumStorageBytes: 'cloud.storage.maximum-bytes',
|
|
11
11
|
maximumParticipants: 'cloud.collaboration.maximum-participants',
|
|
12
|
-
|
|
13
|
-
monthlyCreditAllowance: 'cloud.
|
|
12
|
+
cloudServices: 'cloud.services.enabled',
|
|
13
|
+
monthlyCreditAllowance: 'cloud.services.monthly-credit-allowance'
|
|
14
14
|
} as const
|
|
15
15
|
|
|
16
16
|
export type CloudPolicyContext = {
|
|
17
|
+
/** Whose entitlements apply: an account by default for a workspace, its owner's. */
|
|
17
18
|
targetingKey: string
|
|
19
|
+
/** Required, so an account ID never reads as a workspace's and falls back to defaults. */
|
|
20
|
+
subjectType: 'workspace' | 'user' | 'organization' | 'deployment'
|
|
18
21
|
actorId?: string
|
|
19
22
|
workspaceId?: string
|
|
20
23
|
organizationId?: string
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { CLOUD_PROTOCOL_MAX_UPLOAD_BYTES } from '#cloud/server/config'
|
|
2
2
|
import {
|
|
3
3
|
DefaultLogger,
|
|
4
|
+
ErrorCode,
|
|
5
|
+
OpenFeatureError,
|
|
4
6
|
TypedInMemoryProvider,
|
|
5
7
|
type EvaluationContext,
|
|
6
8
|
type Provider,
|
|
@@ -57,6 +59,7 @@ function evaluationContext(context: CloudPolicyContext): EvaluationContext {
|
|
|
57
59
|
targetingKey: context.targetingKey,
|
|
58
60
|
deploymentMode: context.deploymentMode
|
|
59
61
|
}
|
|
62
|
+
evaluation.subjectType = context.subjectType
|
|
60
63
|
if (context.actorId) evaluation.actorId = context.actorId
|
|
61
64
|
if (context.workspaceId) evaluation.workspaceId = context.workspaceId
|
|
62
65
|
if (context.organizationId) evaluation.organizationId = context.organizationId
|
|
@@ -68,11 +71,24 @@ const logger = new DefaultLogger()
|
|
|
68
71
|
|
|
69
72
|
/** A flag's value, or the default when the provider failed or reported an error, as a client does. */
|
|
70
73
|
async function resolved<T>(evaluation: () => Promise<ResolutionDetails<T>>, defaultValue: T) {
|
|
74
|
+
return (await resolution(evaluation, defaultValue)).value
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A flag's value, and whether the provider failed to answer. A flag it doesn't know is no failure:
|
|
79
|
+
* the default stands for it, as for a plan that sets no value.
|
|
80
|
+
*/
|
|
81
|
+
async function resolution<T>(
|
|
82
|
+
evaluation: () => Promise<ResolutionDetails<T>>,
|
|
83
|
+
defaultValue: T
|
|
84
|
+
): Promise<{ value: T; failed: boolean }> {
|
|
71
85
|
try {
|
|
72
86
|
const details = await evaluation()
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
87
|
+
if (!details.errorCode) return { value: details.value, failed: false }
|
|
88
|
+
return { value: defaultValue, failed: details.errorCode !== ErrorCode.FLAG_NOT_FOUND }
|
|
89
|
+
} catch (error) {
|
|
90
|
+
const unknown = error instanceof OpenFeatureError && error.code === ErrorCode.FLAG_NOT_FOUND
|
|
91
|
+
return { value: defaultValue, failed: !unknown }
|
|
76
92
|
}
|
|
77
93
|
}
|
|
78
94
|
|
|
@@ -99,15 +115,27 @@ export class CloudPolicy {
|
|
|
99
115
|
}
|
|
100
116
|
|
|
101
117
|
number(key: string, defaultValue: number, context: CloudPolicyContext): Promise<number> {
|
|
102
|
-
return resolved(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
118
|
+
return resolved(() => this.evaluateNumber(key, defaultValue, context), defaultValue)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Like `number`, and says whether the provider failed to answer, for a caller that must not
|
|
123
|
+
* take the default for an answer, such as one that undoes something when a limit is gone.
|
|
124
|
+
*/
|
|
125
|
+
numberResolution(
|
|
126
|
+
key: string,
|
|
127
|
+
defaultValue: number,
|
|
128
|
+
context: CloudPolicyContext
|
|
129
|
+
): Promise<{ value: number; failed: boolean }> {
|
|
130
|
+
return resolution(() => this.evaluateNumber(key, defaultValue, context), defaultValue)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private evaluateNumber(key: string, defaultValue: number, context: CloudPolicyContext) {
|
|
134
|
+
return this.provider.resolveNumberEvaluation(
|
|
135
|
+
key,
|
|
136
|
+
defaultValue,
|
|
137
|
+
evaluationContext(context),
|
|
138
|
+
logger
|
|
111
139
|
)
|
|
112
140
|
}
|
|
113
141
|
}
|
|
@@ -28,7 +28,7 @@ export const staticEntitlementsSchema = v.object({
|
|
|
28
28
|
}),
|
|
29
29
|
{}
|
|
30
30
|
),
|
|
31
|
-
|
|
31
|
+
services: v.optional(
|
|
32
32
|
v.object({
|
|
33
33
|
enabled: v.optional(v.boolean()),
|
|
34
34
|
monthlyCreditAllowance: v.optional(nonNegativeIntegerSchema)
|
|
@@ -60,7 +60,7 @@ export const staticEntitlementsTOMLSchema = v.strictObject({
|
|
|
60
60
|
maximum_participants: v.optional(positiveIntegerSchema)
|
|
61
61
|
})
|
|
62
62
|
),
|
|
63
|
-
|
|
63
|
+
services: v.optional(
|
|
64
64
|
v.strictObject({
|
|
65
65
|
enabled: v.optional(v.boolean()),
|
|
66
66
|
monthly_credit_allowance: v.optional(nonNegativeIntegerSchema)
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import type { WorkspaceEntitlements } from '#cloud/contract'
|
|
1
|
+
import type { AccountEntitlements, WorkspaceEntitlements } from '#cloud/contract'
|
|
2
2
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
3
|
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
4
4
|
import { workspaceRole } from '#cloud/server/documents/repository'
|
|
5
|
-
import { createStorageQuotaService } from '#cloud/server/quota'
|
|
5
|
+
import { createStorageQuotaService, type StorageQuotaSnapshot } from '#cloud/server/quota'
|
|
6
6
|
import type { Kysely } from 'kysely'
|
|
7
7
|
|
|
8
|
-
import { CLOUD_FEATURE_KEYS } from './keys'
|
|
8
|
+
import { CLOUD_FEATURE_KEYS, type CloudPolicyContext } from './keys'
|
|
9
9
|
import type { CloudPolicy } from './policy'
|
|
10
|
+
import { workspacePolicyContext } from './workspace'
|
|
10
11
|
|
|
11
12
|
export function createEntitlementService(
|
|
12
13
|
database: Kysely<CloudDatabase>,
|
|
@@ -16,70 +17,91 @@ export function createEntitlementService(
|
|
|
16
17
|
) {
|
|
17
18
|
const quota = createStorageQuotaService(database)
|
|
18
19
|
return {
|
|
20
|
+
/** A workspace's entitlements: its owner's plan and pooled storage, for any member to read. */
|
|
19
21
|
async workspace(userId: string, workspaceId: string): Promise<WorkspaceEntitlements> {
|
|
20
22
|
if (!(await workspaceRole(database, userId, workspaceId))) throw new DocumentNotFoundError()
|
|
21
|
-
const context =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const [
|
|
23
|
+
const context = await workspacePolicyContext(
|
|
24
|
+
database,
|
|
25
|
+
{ workspaceId },
|
|
26
|
+
{ actorId: userId, deploymentMode: 'self-hosted' }
|
|
27
|
+
)
|
|
28
|
+
if (!context) throw new DocumentNotFoundError()
|
|
29
|
+
const [entitlements, usage] = await Promise.all([
|
|
30
|
+
resolve(context),
|
|
31
|
+
quota.snapshot(workspaceId)
|
|
32
|
+
])
|
|
33
|
+
// The workspace may have gone since its role was read; it has no usage to report then.
|
|
34
|
+
if (!usage) throw new DocumentNotFoundError()
|
|
35
|
+
return { ...entitlements, usage: storageUsage(usage) }
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
/** The signed-in account's own plan, and the storage its workspaces hold together. */
|
|
39
|
+
async account(userId: string): Promise<AccountEntitlements> {
|
|
40
|
+
const [entitlements, usage] = await Promise.all([
|
|
41
|
+
resolve({
|
|
42
|
+
targetingKey: userId,
|
|
43
|
+
subjectType: 'user',
|
|
44
|
+
actorId: userId,
|
|
45
|
+
deploymentMode: 'self-hosted'
|
|
46
|
+
}),
|
|
47
|
+
quota.account(userId)
|
|
48
|
+
])
|
|
49
|
+
return { ...entitlements, usage: storageUsage(usage) }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function resolve(
|
|
54
|
+
context: CloudPolicyContext
|
|
55
|
+
): Promise<Omit<WorkspaceEntitlements, 'usage'>> {
|
|
56
|
+
const [
|
|
57
|
+
links,
|
|
58
|
+
guestView,
|
|
59
|
+
guestEdit,
|
|
60
|
+
guestPresence,
|
|
61
|
+
collaboration,
|
|
62
|
+
revisionHistory,
|
|
63
|
+
cloudServices,
|
|
64
|
+
maximumFileBytes,
|
|
65
|
+
maximumStorage,
|
|
66
|
+
maximumParticipants,
|
|
67
|
+
monthlyCreditAllowance
|
|
68
|
+
] = await Promise.all([
|
|
69
|
+
policy.boolean(CLOUD_FEATURE_KEYS.links, false, context),
|
|
70
|
+
policy.boolean(CLOUD_FEATURE_KEYS.guestView, false, context),
|
|
71
|
+
policy.boolean(CLOUD_FEATURE_KEYS.guestEdit, false, context),
|
|
72
|
+
policy.boolean(CLOUD_FEATURE_KEYS.guestPresence, false, context),
|
|
73
|
+
policy.boolean(CLOUD_FEATURE_KEYS.collaboration, false, context),
|
|
74
|
+
policy.boolean(CLOUD_FEATURE_KEYS.revisionHistory, false, context),
|
|
75
|
+
policy.boolean(CLOUD_FEATURE_KEYS.cloudServices, false, context),
|
|
76
|
+
policy.number(CLOUD_FEATURE_KEYS.maximumFileBytes, technicalMaximumFileBytes, context),
|
|
77
|
+
policy.number(CLOUD_FEATURE_KEYS.maximumStorageBytes, Number.MAX_SAFE_INTEGER, context),
|
|
78
|
+
policy.number(CLOUD_FEATURE_KEYS.maximumParticipants, technicalMaximumParticipants, context),
|
|
79
|
+
policy.number(CLOUD_FEATURE_KEYS.monthlyCreditAllowance, -1, context)
|
|
80
|
+
])
|
|
81
|
+
// Technical limits are ceilings: an entitlement can lower them but never raise them.
|
|
82
|
+
const participants = Math.min(technicalMaximumParticipants, maximumParticipants)
|
|
83
|
+
return {
|
|
84
|
+
features: {
|
|
28
85
|
links,
|
|
29
86
|
guestView,
|
|
30
87
|
guestEdit,
|
|
31
88
|
guestPresence,
|
|
32
89
|
collaboration,
|
|
33
90
|
revisionHistory,
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
policy.boolean(CLOUD_FEATURE_KEYS.links, false, context),
|
|
42
|
-
policy.boolean(CLOUD_FEATURE_KEYS.guestView, false, context),
|
|
43
|
-
policy.boolean(CLOUD_FEATURE_KEYS.guestEdit, false, context),
|
|
44
|
-
policy.boolean(CLOUD_FEATURE_KEYS.guestPresence, false, context),
|
|
45
|
-
policy.boolean(CLOUD_FEATURE_KEYS.collaboration, false, context),
|
|
46
|
-
policy.boolean(CLOUD_FEATURE_KEYS.revisionHistory, false, context),
|
|
47
|
-
policy.boolean(CLOUD_FEATURE_KEYS.cloudAI, false, context),
|
|
48
|
-
policy.number(CLOUD_FEATURE_KEYS.maximumFileBytes, technicalMaximumFileBytes, context),
|
|
49
|
-
policy.number(CLOUD_FEATURE_KEYS.maximumStorageBytes, Number.MAX_SAFE_INTEGER, context),
|
|
50
|
-
policy.number(
|
|
51
|
-
CLOUD_FEATURE_KEYS.maximumParticipants,
|
|
52
|
-
technicalMaximumParticipants,
|
|
53
|
-
context
|
|
54
|
-
),
|
|
55
|
-
policy.number(CLOUD_FEATURE_KEYS.monthlyCreditAllowance, -1, context),
|
|
56
|
-
quota.snapshot(workspaceId)
|
|
57
|
-
])
|
|
58
|
-
// Technical limits are ceilings: an entitlement can lower them but never raise them.
|
|
59
|
-
const participants = Math.min(technicalMaximumParticipants, maximumParticipants)
|
|
60
|
-
return {
|
|
61
|
-
features: {
|
|
62
|
-
links,
|
|
63
|
-
guestView,
|
|
64
|
-
guestEdit,
|
|
65
|
-
guestPresence,
|
|
66
|
-
collaboration,
|
|
67
|
-
revisionHistory,
|
|
68
|
-
cloudAI
|
|
69
|
-
},
|
|
70
|
-
limits: {
|
|
71
|
-
maximumFileBytes: Math.min(technicalMaximumFileBytes, maximumFileBytes),
|
|
72
|
-
maximumStorageBytes: maximumStorage === Number.MAX_SAFE_INTEGER ? null : maximumStorage,
|
|
73
|
-
maximumParticipants: participants === Number.MAX_SAFE_INTEGER ? null : participants,
|
|
74
|
-
monthlyCreditAllowance: monthlyCreditAllowance < 0 ? null : monthlyCreditAllowance
|
|
75
|
-
},
|
|
76
|
-
usage: {
|
|
77
|
-
committedStorageBytes: usage.committedBytes,
|
|
78
|
-
reservedStorageBytes: usage.reservedBytes
|
|
79
|
-
}
|
|
91
|
+
cloudServices
|
|
92
|
+
},
|
|
93
|
+
limits: {
|
|
94
|
+
maximumFileBytes: Math.min(technicalMaximumFileBytes, maximumFileBytes),
|
|
95
|
+
maximumStorageBytes: maximumStorage === Number.MAX_SAFE_INTEGER ? null : maximumStorage,
|
|
96
|
+
maximumParticipants: participants === Number.MAX_SAFE_INTEGER ? null : participants,
|
|
97
|
+
monthlyCreditAllowance: monthlyCreditAllowance < 0 ? null : monthlyCreditAllowance
|
|
80
98
|
}
|
|
81
99
|
}
|
|
82
100
|
}
|
|
83
101
|
}
|
|
84
102
|
|
|
103
|
+
function storageUsage(usage: StorageQuotaSnapshot): WorkspaceEntitlements['usage'] {
|
|
104
|
+
return { committedStorageBytes: usage.committedBytes, reservedStorageBytes: usage.reservedBytes }
|
|
105
|
+
}
|
|
106
|
+
|
|
85
107
|
export type EntitlementService = ReturnType<typeof createEntitlementService>
|