@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
package/dist/listen.mjs
CHANGED
|
@@ -19,53 +19,58 @@ var __exportAll = (all, no_symbols) => {
|
|
|
19
19
|
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
20
20
|
return target;
|
|
21
21
|
};
|
|
22
|
-
/** The `aud` of every token Cloud signs for
|
|
23
|
-
const
|
|
24
|
-
/** Where Cloud publishes the public keys
|
|
25
|
-
const
|
|
22
|
+
/** The `aud` of every token Cloud signs for its services. */
|
|
23
|
+
const CLOUD_SERVICES_TOKEN_AUDIENCE = "openpencil-services";
|
|
24
|
+
/** Where Cloud publishes the public keys a service verifies its tokens with. */
|
|
25
|
+
const CLOUD_SERVICES_JWKS_PATH = "/.well-known/jwks.json";
|
|
26
26
|
/**
|
|
27
27
|
* Where a service answers, below the discovered `endpoint`. Model requests follow the OpenAI chat
|
|
28
28
|
* completions API, so an OpenAI-compatible client takes `${endpoint}${base}` as its base URL.
|
|
29
29
|
*/
|
|
30
|
-
const
|
|
30
|
+
const CLOUD_SERVICES_API_PATHS = {
|
|
31
31
|
base: "/v1",
|
|
32
32
|
chatCompletions: "/v1/chat/completions",
|
|
33
33
|
catalog: "/v1/catalog",
|
|
34
34
|
wallet: "/v1/wallet",
|
|
35
|
-
usage: "/v1/usage"
|
|
35
|
+
usage: "/v1/usage",
|
|
36
|
+
search: "/v1/search"
|
|
36
37
|
};
|
|
37
38
|
const urlSchema$1 = v.pipe(v.string(), v.url());
|
|
38
|
-
const creditsSchema = v.pipe(v.number(), v.integer(), v.minValue(0));
|
|
39
|
-
const
|
|
39
|
+
const creditsSchema$1 = v.pipe(v.number(), v.integer(), v.minValue(0));
|
|
40
|
+
const cloudServicesDiscoverySchema = v.object({
|
|
40
41
|
endpoint: urlSchema$1,
|
|
41
42
|
contractVersion: v.literal("1")
|
|
42
43
|
});
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return v.parse(createAITokenSchema, input);
|
|
44
|
+
const createServicesTokenSchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) });
|
|
45
|
+
function parseCreateServicesToken(input) {
|
|
46
|
+
return v.parse(createServicesTokenSchema, input);
|
|
47
47
|
}
|
|
48
48
|
v.object({
|
|
49
49
|
token: v.pipe(v.string(), v.minLength(1)),
|
|
50
50
|
expiresAt: v.pipe(v.string(), v.isoTimestamp())
|
|
51
51
|
});
|
|
52
|
-
/** What
|
|
53
|
-
const
|
|
52
|
+
/** What a service may rely on once a token's signature, issuer, audience, and expiry check. */
|
|
53
|
+
const servicesTokenClaimsSchema = v.object({
|
|
54
54
|
iss: urlSchema$1,
|
|
55
|
-
aud: v.literal(
|
|
55
|
+
aud: v.literal(CLOUD_SERVICES_TOKEN_AUDIENCE),
|
|
56
56
|
sub: v.pipe(v.string(), v.minLength(1)),
|
|
57
57
|
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
58
|
-
/**
|
|
59
|
-
|
|
58
|
+
/**
|
|
59
|
+
* The account whose credits the request spends: the workspace's owner, whose allowance every
|
|
60
|
+
* workspace they own shares.
|
|
61
|
+
*/
|
|
62
|
+
accountId: v.pipe(v.string(), v.minLength(1)),
|
|
63
|
+
/** Credits the owner's plan grants each month; `null` when Cloud sets no allowance. */
|
|
64
|
+
monthlyCreditAllowance: v.nullable(creditsSchema$1),
|
|
60
65
|
iat: v.pipe(v.number(), v.integer()),
|
|
61
66
|
exp: v.pipe(v.number(), v.integer())
|
|
62
67
|
});
|
|
63
|
-
const
|
|
68
|
+
const chatModelSchema = v.object({
|
|
64
69
|
id: v.pipe(v.string(), v.minLength(1)),
|
|
65
70
|
label: v.pipe(v.string(), v.minLength(1)),
|
|
66
71
|
/** Credits per million input and output tokens. */
|
|
67
|
-
creditsPerMillionInputTokens: creditsSchema,
|
|
68
|
-
creditsPerMillionOutputTokens: creditsSchema,
|
|
72
|
+
creditsPerMillionInputTokens: creditsSchema$1,
|
|
73
|
+
creditsPerMillionOutputTokens: creditsSchema$1,
|
|
69
74
|
contextTokens: v.pipe(v.number(), v.integer(), v.minValue(1)),
|
|
70
75
|
capabilities: v.object({
|
|
71
76
|
reasoning: v.boolean(),
|
|
@@ -73,26 +78,29 @@ const aiModelSchema = v.object({
|
|
|
73
78
|
tools: v.boolean()
|
|
74
79
|
})
|
|
75
80
|
});
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
81
|
+
/** What one web search costs; a service without search leaves it out of its catalog. */
|
|
82
|
+
const searchPricingSchema = v.object({ creditsPerRequest: creditsSchema$1 });
|
|
83
|
+
const serviceCatalogSchema = v.object({
|
|
84
|
+
models: v.array(chatModelSchema),
|
|
85
|
+
defaultModelID: v.pipe(v.string(), v.minLength(1)),
|
|
86
|
+
search: v.optional(searchPricingSchema)
|
|
79
87
|
});
|
|
80
|
-
const
|
|
88
|
+
const creditBucketSchema = v.object({
|
|
81
89
|
kind: v.picklist([
|
|
82
90
|
"allowance",
|
|
83
91
|
"purchased",
|
|
84
92
|
"grant"
|
|
85
93
|
]),
|
|
86
|
-
remaining: creditsSchema,
|
|
94
|
+
remaining: creditsSchema$1,
|
|
87
95
|
expiresAt: v.nullable(v.pipe(v.string(), v.isoTimestamp()))
|
|
88
96
|
});
|
|
89
97
|
v.object({
|
|
90
98
|
/** Credits left to spend; `null` when the service sets no limit. */
|
|
91
|
-
balance: v.nullable(creditsSchema),
|
|
99
|
+
balance: v.nullable(creditsSchema$1),
|
|
92
100
|
/** Spent first to last. */
|
|
93
|
-
buckets: v.array(
|
|
101
|
+
buckets: v.array(creditBucketSchema)
|
|
94
102
|
});
|
|
95
|
-
const
|
|
103
|
+
const usageEntrySchema = v.object({
|
|
96
104
|
id: v.pipe(v.string(), v.minLength(1)),
|
|
97
105
|
createdAt: v.pipe(v.string(), v.isoTimestamp()),
|
|
98
106
|
kind: v.picklist([
|
|
@@ -102,41 +110,150 @@ const aiUsageEntrySchema = v.object({
|
|
|
102
110
|
]),
|
|
103
111
|
modelID: v.nullable(v.string()),
|
|
104
112
|
userId: v.pipe(v.string(), v.minLength(1)),
|
|
105
|
-
credits: creditsSchema
|
|
113
|
+
credits: creditsSchema$1
|
|
114
|
+
});
|
|
115
|
+
v.object({
|
|
116
|
+
query: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(1e3)),
|
|
117
|
+
numResults: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(10)), 5)
|
|
118
|
+
});
|
|
119
|
+
/** One page a search found, with the part of its text that answers the query. */
|
|
120
|
+
const searchResultSchema = v.object({
|
|
121
|
+
url: v.pipe(urlSchema$1, v.regex(/^https?:\/\//i, "A search result links to an HTTP(S) page")),
|
|
122
|
+
title: v.string(),
|
|
123
|
+
publishedAt: v.nullable(v.pipe(v.string(), v.isoTimestamp())),
|
|
124
|
+
author: v.nullable(v.string()),
|
|
125
|
+
text: v.string()
|
|
106
126
|
});
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
127
|
+
v.object({ results: v.array(searchResultSchema) });
|
|
128
|
+
const usageQuerySchema = v.object({ cursor: v.optional(v.pipe(v.string(), v.minLength(1))) });
|
|
129
|
+
function parseUsageQuery(input) {
|
|
130
|
+
return v.parse(usageQuerySchema, input);
|
|
110
131
|
}
|
|
111
132
|
v.object({
|
|
112
|
-
entries: v.array(
|
|
133
|
+
entries: v.array(usageEntrySchema),
|
|
113
134
|
next: v.nullable(v.string())
|
|
114
135
|
});
|
|
115
|
-
/** Errors
|
|
116
|
-
const
|
|
136
|
+
/** Errors a service answers with, beside the upstream provider's own. */
|
|
137
|
+
const serviceErrorCodeSchema = v.picklist([
|
|
138
|
+
"invalid_request",
|
|
117
139
|
"unauthorized",
|
|
118
140
|
"insufficient_credits",
|
|
119
141
|
"model_not_allowed",
|
|
142
|
+
"capability_unavailable",
|
|
120
143
|
"rate_limited"
|
|
121
144
|
]);
|
|
122
145
|
v.object({ error: v.object({
|
|
123
|
-
code:
|
|
146
|
+
code: serviceErrorCodeSchema,
|
|
124
147
|
message: v.optional(v.string()),
|
|
125
148
|
/** With `insufficient_credits`: when the workspace's allowance renews, if the service knows. */
|
|
126
149
|
resetsAt: v.optional(v.pipe(v.string(), v.isoTimestamp()))
|
|
127
150
|
}) });
|
|
128
|
-
const
|
|
151
|
+
const SERVICE_ERROR_STATUS = {
|
|
152
|
+
invalid_request: 400,
|
|
129
153
|
unauthorized: 401,
|
|
130
154
|
insufficient_credits: 402,
|
|
131
155
|
model_not_allowed: 403,
|
|
156
|
+
/** A capability, such as search, the service does not offer. */
|
|
157
|
+
capability_unavailable: 404,
|
|
132
158
|
rate_limited: 429
|
|
133
159
|
};
|
|
160
|
+
/**
|
|
161
|
+
* Whether a bearer token may travel to `url`: over HTTPS, or in the clear only to a loopback
|
|
162
|
+
* address, for development. Only literal addresses count, since a name such as `localhost` is
|
|
163
|
+
* whatever the resolver answers. Services and billing tokens act as the account until they expire.
|
|
164
|
+
*/
|
|
165
|
+
function sendsTokenSafely(url) {
|
|
166
|
+
const { protocol, hostname } = new URL(url);
|
|
167
|
+
if (protocol === "https:") return true;
|
|
168
|
+
if (protocol !== "http:") return false;
|
|
169
|
+
return hostname === "[::1]" || /^127(\.\d{1,3}){3}$/.test(hostname);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The `aud` of the tokens Cloud signs for its billing service, with the services' signing key,
|
|
173
|
+
* whose public keys it publishes at `CLOUD_SERVICES_JWKS_PATH`.
|
|
174
|
+
*/
|
|
175
|
+
const CLOUD_BILLING_TOKEN_AUDIENCE = "openpencil-billing";
|
|
176
|
+
/**
|
|
177
|
+
* The page of a Cloud server where the signed-in person sees their plan, buys a plan or credits,
|
|
178
|
+
* and manages what they pay for; editors link to it whenever the server advertises billing.
|
|
179
|
+
*/
|
|
180
|
+
const CLOUD_ACCOUNT_PLAN_PATH = "/account/plan";
|
|
181
|
+
const urlSchema = v.pipe(v.string(), v.url());
|
|
182
|
+
const idSchema = v.pipe(v.string(), v.regex(/^[a-z0-9][a-z0-9-]{0,62}$/));
|
|
183
|
+
const centsSchema = v.pipe(v.number(), v.integer(), v.minValue(0));
|
|
184
|
+
const bytesSchema = v.pipe(v.number(), v.integer(), v.minValue(0));
|
|
185
|
+
const creditsSchema = v.pipe(v.number(), v.integer(), v.minValue(0));
|
|
186
|
+
const cloudBillingDiscoverySchema = v.object({
|
|
187
|
+
endpoint: urlSchema,
|
|
188
|
+
contractVersion: v.literal("1")
|
|
189
|
+
});
|
|
190
|
+
v.object({
|
|
191
|
+
token: v.pipe(v.string(), v.minLength(1)),
|
|
192
|
+
expiresAt: v.pipe(v.string(), v.isoTimestamp())
|
|
193
|
+
});
|
|
194
|
+
v.object({
|
|
195
|
+
iss: urlSchema,
|
|
196
|
+
aud: v.literal(CLOUD_BILLING_TOKEN_AUDIENCE),
|
|
197
|
+
/** The account: the Cloud user whose plan and credits these are. */
|
|
198
|
+
sub: v.pipe(v.string(), v.minLength(1)),
|
|
199
|
+
/** Where the payment provider sends receipts and invoices. */
|
|
200
|
+
email: v.pipe(v.string(), v.email()),
|
|
201
|
+
iat: v.pipe(v.number(), v.integer()),
|
|
202
|
+
exp: v.pipe(v.number(), v.integer())
|
|
203
|
+
});
|
|
204
|
+
/** A plan an account subscribes to, billed every month in US dollars before tax. */
|
|
205
|
+
const billingPlanSchema = v.object({
|
|
206
|
+
id: idSchema,
|
|
207
|
+
name: v.pipe(v.string(), v.minLength(1)),
|
|
208
|
+
monthlyPriceCents: centsSchema,
|
|
209
|
+
storageBytes: bytesSchema,
|
|
210
|
+
monthlyCredits: creditsSchema
|
|
211
|
+
});
|
|
212
|
+
/** Credits an account on a plan buys once, in US dollars before tax. */
|
|
213
|
+
const creditPackSchema = v.object({
|
|
214
|
+
id: idSchema,
|
|
215
|
+
credits: creditsSchema,
|
|
216
|
+
priceCents: centsSchema,
|
|
217
|
+
/** How long the credits last from purchase. */
|
|
218
|
+
validDays: v.pipe(v.number(), v.integer(), v.minValue(1))
|
|
219
|
+
});
|
|
220
|
+
v.object({
|
|
221
|
+
plans: v.array(billingPlanSchema),
|
|
222
|
+
creditPacks: v.array(creditPackSchema)
|
|
223
|
+
});
|
|
224
|
+
v.object({ subscription: v.nullable(v.object({
|
|
225
|
+
planId: idSchema,
|
|
226
|
+
status: v.picklist(["active", "past_due"]),
|
|
227
|
+
/** When the current period ends: the next charge, or the end of a cancelled plan. */
|
|
228
|
+
currentPeriodEnd: v.pipe(v.string(), v.isoTimestamp()),
|
|
229
|
+
/** Whether the plan ends at `currentPeriodEnd` instead of renewing. */
|
|
230
|
+
cancelsAtPeriodEnd: v.boolean()
|
|
231
|
+
})) });
|
|
232
|
+
v.union([v.object({ planId: idSchema }), v.object({ creditPackId: idSchema })]);
|
|
233
|
+
v.object({ url: v.pipe(urlSchema, v.regex(/^https:\/\//i, "Billing sends people to an HTTPS page")) });
|
|
234
|
+
const billingErrorCodeSchema = v.picklist([
|
|
235
|
+
"invalid_request",
|
|
236
|
+
"unauthorized",
|
|
237
|
+
"not_allowed",
|
|
238
|
+
"provider_unavailable"
|
|
239
|
+
]);
|
|
240
|
+
v.object({ error: v.object({
|
|
241
|
+
code: billingErrorCodeSchema,
|
|
242
|
+
message: v.optional(v.string())
|
|
243
|
+
}) });
|
|
134
244
|
//#endregion
|
|
135
245
|
//#region src/contract/documents.ts
|
|
136
246
|
const CLOUD_PROTOCOL_MAX_UPLOAD_BYTES = 1073741824;
|
|
137
247
|
const checksumSchema = v.pipe(v.string(), v.regex(/^[A-Za-z0-9+/]{43}=$/));
|
|
138
248
|
/** The most images one revision, or one request for image uploads, may name. */
|
|
139
249
|
const CLOUD_MAX_DOCUMENT_IMAGES = 1e4;
|
|
250
|
+
/**
|
|
251
|
+
* The most a Cloud API request body may hold; a server refuses anything past it before reading
|
|
252
|
+
* it. Files go to the object store, never through the API, so only lists grow large: a save's
|
|
253
|
+
* images fit with room to spare, and a client commits image uploads, whose part lists can run
|
|
254
|
+
* long, in batches under half of it.
|
|
255
|
+
*/
|
|
256
|
+
const CLOUD_API_MAX_BODY_BYTES = 8388608;
|
|
140
257
|
const imageNameSchema = v.pipe(v.string(), v.regex(/^[A-Za-z0-9._-]{1,128}$/));
|
|
141
258
|
const objectDownloadSchema = v.object({
|
|
142
259
|
url: v.pipe(v.string(), v.url()),
|
|
@@ -189,6 +306,22 @@ v.object({
|
|
|
189
306
|
sharedBy: v.nullable(v.object({ name: v.string() })),
|
|
190
307
|
sharedAt: v.string()
|
|
191
308
|
});
|
|
309
|
+
/**
|
|
310
|
+
* A document in Trash the person may restore: who deleted it and when, and when it goes for good.
|
|
311
|
+
* `canPurge` says whether they may also delete it for good before then.
|
|
312
|
+
*/
|
|
313
|
+
const trashedDocumentSchema = v.object({
|
|
314
|
+
id: v.pipe(v.string(), v.uuid()),
|
|
315
|
+
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
316
|
+
workspaceName: v.string(),
|
|
317
|
+
name: v.string(),
|
|
318
|
+
kind: v.optional(documentKindSchema, "design"),
|
|
319
|
+
deletedAt: v.string(),
|
|
320
|
+
deletedBy: v.nullable(v.object({ name: v.string() })),
|
|
321
|
+
purgeAt: v.string(),
|
|
322
|
+
canPurge: v.boolean()
|
|
323
|
+
});
|
|
324
|
+
v.object({ documents: v.array(trashedDocumentSchema) });
|
|
192
325
|
const documentDownloadSchema = v.object({
|
|
193
326
|
document: documentSummarySchema,
|
|
194
327
|
revisionId: v.pipe(v.string(), v.uuid()),
|
|
@@ -387,6 +520,14 @@ const RELAY_CLOSE = {
|
|
|
387
520
|
/** The document was restored to a version, so its room started over under a new ID. */
|
|
388
521
|
restored: 4410
|
|
389
522
|
};
|
|
523
|
+
/**
|
|
524
|
+
* The text a peer sends to learn whether the relay still hears it, and the relay's answer. Relay
|
|
525
|
+
* frames are binary, so neither is taken for one, and a Durable Object answers without waking.
|
|
526
|
+
*/
|
|
527
|
+
const RELAY_HEARTBEAT = {
|
|
528
|
+
ping: "ping",
|
|
529
|
+
pong: "pong"
|
|
530
|
+
};
|
|
390
531
|
const CLIENT_AUTH = 0;
|
|
391
532
|
const CLIENT_MESSAGE = 1;
|
|
392
533
|
const CLIENT_FRAGMENT = 2;
|
|
@@ -594,11 +735,65 @@ const cloudCapabilitiesSchema = v.object({
|
|
|
594
735
|
/** A released editor version, `major.minor.patch`. */
|
|
595
736
|
const cloudEditorVersionSchema = v.pipe(v.string(), v.regex(/^\d+\.\d+\.\d+$/));
|
|
596
737
|
/**
|
|
738
|
+
* The header desktop editors send with each request, naming their version, so a server can refuse
|
|
739
|
+
* an editor older than its `minimumEditorVersion`. Browser editors leave it out: the server serves
|
|
740
|
+
* them, and an older server's CORS rules would refuse a header they don't list.
|
|
741
|
+
*/
|
|
742
|
+
const CLOUD_EDITOR_VERSION_HEADER = "X-OpenPencil-Editor-Version";
|
|
743
|
+
function editorVersionParts(version) {
|
|
744
|
+
const match = /^(\d+)\.(\d+)\.(\d+)/.exec(version);
|
|
745
|
+
return match ? match.slice(1).map(Number) : null;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Whether `version` is `minimum` or later, comparing `major.minor.patch` numerically; a prerelease
|
|
749
|
+
* counts as its release. A version that isn't one, on either side, is not at least anything.
|
|
750
|
+
*/
|
|
751
|
+
function isEditorVersionAtLeast(version, minimum) {
|
|
752
|
+
const have = editorVersionParts(version);
|
|
753
|
+
const need = editorVersionParts(minimum);
|
|
754
|
+
if (!have || !need) return false;
|
|
755
|
+
for (let index = 0; index < need.length; index++) {
|
|
756
|
+
const difference = (have[index] ?? 0) - (need[index] ?? 0);
|
|
757
|
+
if (difference !== 0) return difference > 0;
|
|
758
|
+
}
|
|
759
|
+
return true;
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
597
762
|
* Whether editors offer this server to everyone, and from which editor version on. Only the
|
|
598
763
|
* official server's listing is read: it turns the Cloud entry points on without an editor release,
|
|
599
764
|
* and an editor older than the minimum keeps them hidden.
|
|
600
765
|
*/
|
|
601
766
|
const cloudListingSchema = v.object({ minimumEditorVersion: cloudEditorVersionSchema });
|
|
767
|
+
const legalURLSchema = v.pipe(v.string(), v.url());
|
|
768
|
+
/**
|
|
769
|
+
* The operator's legal links, which clients show at sign-up and in account pages. A server that
|
|
770
|
+
* names none shows none; `requireAcceptance` means an account accepts `termsVersion` before it uses
|
|
771
|
+
* the server.
|
|
772
|
+
*/
|
|
773
|
+
const cloudLegalDiscoverySchema = v.object({
|
|
774
|
+
termsURL: v.optional(legalURLSchema),
|
|
775
|
+
privacyURL: v.optional(legalURLSchema),
|
|
776
|
+
termsVersion: v.optional(v.string()),
|
|
777
|
+
requireAcceptance: v.boolean(),
|
|
778
|
+
abuseEmail: v.optional(v.pipe(v.string(), v.email())),
|
|
779
|
+
minimumAge: v.optional(v.pipe(v.number(), v.integer()))
|
|
780
|
+
});
|
|
781
|
+
/** A web address a page may link or load: `http` or `https` only, never a script or data URL. */
|
|
782
|
+
const webURLSchema = v.pipe(v.string(), v.url(), v.check((value) => ["http:", "https:"].includes(new URL(value).protocol)));
|
|
783
|
+
/**
|
|
784
|
+
* How a self-hosted server names itself on its public pages: the operator's name and logo in the
|
|
785
|
+
* bar, and where its people get help. The official server is OpenPencil's own and names none.
|
|
786
|
+
*/
|
|
787
|
+
const cloudBrandingSchema = v.object({
|
|
788
|
+
name: v.optional(v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(80))),
|
|
789
|
+
logoURL: v.optional(webURLSchema),
|
|
790
|
+
helpURL: v.optional(webURLSchema)
|
|
791
|
+
});
|
|
792
|
+
/**
|
|
793
|
+
* The server's community gallery: the workspace whose documents anyone may open read-only.
|
|
794
|
+
* Saving to it publishes, so editors never choose it for anyone and say so when it is chosen.
|
|
795
|
+
*/
|
|
796
|
+
const cloudCommunityDiscoverySchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) });
|
|
602
797
|
const cloudDiscoverySchema = v.object({
|
|
603
798
|
protocolVersion: v.literal("1"),
|
|
604
799
|
deployment: cloudDeploymentSchema,
|
|
@@ -609,10 +804,19 @@ const cloudDiscoverySchema = v.object({
|
|
|
609
804
|
authentication: cloudAuthenticationSchema,
|
|
610
805
|
capabilities: cloudCapabilitiesSchema,
|
|
611
806
|
listing: v.optional(cloudListingSchema),
|
|
612
|
-
/**
|
|
613
|
-
|
|
807
|
+
/**
|
|
808
|
+
* The oldest editor version the server works with. Older editors ask to be updated instead of
|
|
809
|
+
* connecting, and the server refuses their requests.
|
|
810
|
+
*/
|
|
811
|
+
minimumEditorVersion: v.optional(cloudEditorVersionSchema),
|
|
812
|
+
/** The services editors reach with Cloud-signed tokens; absent when the deployment has none. */
|
|
813
|
+
services: v.optional(cloudServicesDiscoverySchema),
|
|
614
814
|
/** Where people manage their plan and buy credits; absent when the deployment does not bill. */
|
|
615
|
-
billing: v.optional(cloudBillingDiscoverySchema)
|
|
815
|
+
billing: v.optional(cloudBillingDiscoverySchema),
|
|
816
|
+
/** The community gallery; absent when the deployment has none. */
|
|
817
|
+
community: v.optional(cloudCommunityDiscoverySchema),
|
|
818
|
+
legal: v.optional(cloudLegalDiscoverySchema),
|
|
819
|
+
branding: v.optional(cloudBrandingSchema)
|
|
616
820
|
});
|
|
617
821
|
function parseCloudDiscovery(input) {
|
|
618
822
|
return v.parse(cloudDiscoverySchema, input);
|
|
@@ -679,7 +883,12 @@ const workspaceMemberParams = validatedParams(v.object({
|
|
|
679
883
|
workspaceId: uuidParamSchema,
|
|
680
884
|
userId: textParamSchema
|
|
681
885
|
}));
|
|
886
|
+
const accountParams = validatedParams(v.object({ userId: uuidParamSchema }));
|
|
682
887
|
const shareParams = validatedParams(v.object({ shareId: uuidParamSchema }));
|
|
888
|
+
const communityThumbnailParams = validatedParams(v.object({
|
|
889
|
+
documentId: uuidParamSchema,
|
|
890
|
+
revisionId: uuidParamSchema
|
|
891
|
+
}));
|
|
683
892
|
const invitationParams = validatedParams(v.object({ invitationId: uuidParamSchema }));
|
|
684
893
|
const invitationContinuationParams = validatedParams(v.object({ continuationId: textParamSchema }));
|
|
685
894
|
const uploadParams = validatedParams(v.object({ uploadId: uuidParamSchema }));
|
|
@@ -692,14 +901,14 @@ const pictureParams = validatedParams(v.object({
|
|
|
692
901
|
}));
|
|
693
902
|
//#endregion
|
|
694
903
|
//#region src/server/policy/schema.ts
|
|
695
|
-
const positiveIntegerSchema
|
|
904
|
+
const positiveIntegerSchema = v.pipe(v.number(), v.integer(), v.minValue(1));
|
|
696
905
|
const nonNegativeIntegerSchema = v.pipe(v.number(), v.integer(), v.minValue(0));
|
|
697
906
|
const staticEntitlementsSchema = v.object({
|
|
698
907
|
documents: v.optional(v.object({
|
|
699
|
-
maximumFileBytes: v.optional(positiveIntegerSchema
|
|
908
|
+
maximumFileBytes: v.optional(positiveIntegerSchema),
|
|
700
909
|
revisionHistory: v.optional(v.boolean())
|
|
701
910
|
}), {}),
|
|
702
|
-
storage: v.optional(v.object({ maximumBytes: v.optional(positiveIntegerSchema
|
|
911
|
+
storage: v.optional(v.object({ maximumBytes: v.optional(positiveIntegerSchema) }), {}),
|
|
703
912
|
sharing: v.optional(v.object({
|
|
704
913
|
links: v.optional(v.boolean()),
|
|
705
914
|
guestView: v.optional(v.boolean()),
|
|
@@ -708,19 +917,19 @@ const staticEntitlementsSchema = v.object({
|
|
|
708
917
|
}), {}),
|
|
709
918
|
collaboration: v.optional(v.object({
|
|
710
919
|
enabled: v.optional(v.boolean()),
|
|
711
|
-
maximumParticipants: v.optional(positiveIntegerSchema
|
|
920
|
+
maximumParticipants: v.optional(positiveIntegerSchema)
|
|
712
921
|
}), {}),
|
|
713
|
-
|
|
922
|
+
services: v.optional(v.object({
|
|
714
923
|
enabled: v.optional(v.boolean()),
|
|
715
924
|
monthlyCreditAllowance: v.optional(nonNegativeIntegerSchema)
|
|
716
925
|
}), {})
|
|
717
926
|
});
|
|
718
927
|
const staticEntitlementsTOMLSchema = v.strictObject({
|
|
719
928
|
documents: v.optional(v.strictObject({
|
|
720
|
-
maximum_file_bytes: v.optional(positiveIntegerSchema
|
|
929
|
+
maximum_file_bytes: v.optional(positiveIntegerSchema),
|
|
721
930
|
revision_history: v.optional(v.boolean())
|
|
722
931
|
})),
|
|
723
|
-
storage: v.optional(v.strictObject({ maximum_bytes: v.optional(positiveIntegerSchema
|
|
932
|
+
storage: v.optional(v.strictObject({ maximum_bytes: v.optional(positiveIntegerSchema) })),
|
|
724
933
|
sharing: v.optional(v.strictObject({
|
|
725
934
|
links: v.optional(v.boolean()),
|
|
726
935
|
guest_view: v.optional(v.boolean()),
|
|
@@ -729,9 +938,9 @@ const staticEntitlementsTOMLSchema = v.strictObject({
|
|
|
729
938
|
})),
|
|
730
939
|
collaboration: v.optional(v.strictObject({
|
|
731
940
|
enabled: v.optional(v.boolean()),
|
|
732
|
-
maximum_participants: v.optional(positiveIntegerSchema
|
|
941
|
+
maximum_participants: v.optional(positiveIntegerSchema)
|
|
733
942
|
})),
|
|
734
|
-
|
|
943
|
+
services: v.optional(v.strictObject({
|
|
735
944
|
enabled: v.optional(v.boolean()),
|
|
736
945
|
monthly_credit_allowance: v.optional(nonNegativeIntegerSchema)
|
|
737
946
|
}))
|
|
@@ -751,10 +960,16 @@ const websocketURLSchema = v.pipe(v.string(), v.url(), v.check((value) => {
|
|
|
751
960
|
return protocol === "ws:" || protocol === "wss:";
|
|
752
961
|
}, "URL must use WS or WSS"));
|
|
753
962
|
const optionalTextSchema = v.optional(v.pipe(v.string(), v.trim(), v.minLength(1)));
|
|
963
|
+
/**
|
|
964
|
+
* A prefix deleted files move under: one lowercase path segment and a slash, which no object key
|
|
965
|
+
* the server makes starts with (they start with `documents/`).
|
|
966
|
+
*/
|
|
967
|
+
const deletedPrefixSchema = v.pipe(v.string(), v.regex(/^[a-z0-9][a-z0-9-]*\/$/), v.check((prefix) => prefix !== "documents/", "deleted_prefix cannot be documents/"));
|
|
754
968
|
const rawCloudServerConfigSchema = v.object({
|
|
755
969
|
deployment: v.picklist(["official", "self-hosted"]),
|
|
756
970
|
publicURL: httpURLSchema,
|
|
757
971
|
listingMinimumEditorVersion: v.optional(cloudEditorVersionSchema),
|
|
972
|
+
minimumEditorVersion: v.optional(cloudEditorVersionSchema),
|
|
758
973
|
relayURL: v.optional(websocketURLSchema),
|
|
759
974
|
technicalLimits: v.optional(v.object({
|
|
760
975
|
maximumUploadBytes: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(CLOUD_PROTOCOL_MAX_UPLOAD_BYTES)), CLOUD_PROTOCOL_MAX_UPLOAD_BYTES),
|
|
@@ -766,6 +981,8 @@ const rawCloudServerConfigSchema = v.object({
|
|
|
766
981
|
maximumConnectionsPerRoom: CLOUD_DEFAULT_MAX_CONNECTIONS_PER_ROOM
|
|
767
982
|
}),
|
|
768
983
|
staticEntitlements: v.optional(staticEntitlementsSchema),
|
|
984
|
+
/** With database entitlements, what an account without stored ones takes, value by value. */
|
|
985
|
+
defaultEntitlements: v.optional(staticEntitlementsSchema),
|
|
769
986
|
databaseURL: v.pipe(v.string(), v.url()),
|
|
770
987
|
authSecret: v.pipe(v.string(), v.minLength(32)),
|
|
771
988
|
trustedOrigins: v.optional(v.array(httpURLSchema), []),
|
|
@@ -814,6 +1031,8 @@ const rawCloudServerConfigSchema = v.object({
|
|
|
814
1031
|
s3ChecksumVerification: v.optional(v.picklist(["native", "metadata"]), "native"),
|
|
815
1032
|
s3ServerSideEncryption: v.optional(v.picklist(["AES256", "aws:kms"])),
|
|
816
1033
|
s3KmsKeyId: optionalTextSchema,
|
|
1034
|
+
/** Where a deleted file is kept until the bucket's lifecycle rule expires it; see `deleted_prefix`. */
|
|
1035
|
+
s3DeletedPrefix: v.optional(deletedPrefixSchema),
|
|
817
1036
|
smtpHost: optionalTextSchema,
|
|
818
1037
|
smtpPort: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(65535))),
|
|
819
1038
|
smtpSecure: v.optional(v.boolean()),
|
|
@@ -834,14 +1053,44 @@ const rawCloudServerConfigSchema = v.object({
|
|
|
834
1053
|
cleanupIntervalMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1e3)), 6e4),
|
|
835
1054
|
cleanupLeaseDurationMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1e3)), 3e5),
|
|
836
1055
|
documentRetentionMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 2592e6),
|
|
837
|
-
/**
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
1056
|
+
/**
|
|
1057
|
+
* How long a document deleted forever keeps its files and rows, out of everyone's sight, so a
|
|
1058
|
+
* server administrator can still bring it back; 0 destroys it at once.
|
|
1059
|
+
*/
|
|
1060
|
+
documentPurgeGraceMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 6048e5),
|
|
1061
|
+
/** The OpenPencil services editors reach with tokens this server signs; see `src/contract/services.ts`. */
|
|
1062
|
+
servicesEndpoint: v.optional(httpURLSchema),
|
|
1063
|
+
/** The PKCS #8 PEM of the P-256 key that signs service tokens. */
|
|
1064
|
+
servicesSigningKey: optionalTextSchema,
|
|
1065
|
+
servicesTokenLifetimeSeconds: v.optional(v.pipe(v.number(), v.integer(), v.minValue(60), v.maxValue(3600)), 600),
|
|
1066
|
+
/** The billing service accounts buy plans and credits from, which discovery advertises. */
|
|
1067
|
+
billingEndpoint: v.optional(httpURLSchema),
|
|
843
1068
|
/** The bearer token services such as billing present to the internal API. */
|
|
844
1069
|
internalServiceToken: v.optional(v.pipe(v.string(), v.minLength(32))),
|
|
1070
|
+
/**
|
|
1071
|
+
* The operator's own terms, privacy policy, abuse contact, and minimum age; without it the
|
|
1072
|
+
* server shows none. With `requireAcceptance`, an account uses the server only once it accepted
|
|
1073
|
+
* `termsVersion`.
|
|
1074
|
+
*/
|
|
1075
|
+
legal: v.optional(v.object({
|
|
1076
|
+
termsURL: v.optional(httpURLSchema),
|
|
1077
|
+
privacyURL: v.optional(httpURLSchema),
|
|
1078
|
+
termsVersion: optionalTextSchema,
|
|
1079
|
+
requireAcceptance: v.boolean(),
|
|
1080
|
+
abuseEmail: v.optional(v.pipe(v.string(), v.email())),
|
|
1081
|
+
minimumAge: v.optional(v.pipe(v.number(), v.integer(), v.minValue(13), v.maxValue(21)))
|
|
1082
|
+
})),
|
|
1083
|
+
/**
|
|
1084
|
+
* The workspace whose documents anyone may open read-only, without signing in or a link: the
|
|
1085
|
+
* deployment's community gallery. Without it the server has none.
|
|
1086
|
+
*/
|
|
1087
|
+
community: v.optional(v.object({ workspaceId: v.pipe(v.string(), v.uuid()) })),
|
|
1088
|
+
/** How a self-hosted server names itself on its public pages. */
|
|
1089
|
+
branding: v.optional(v.object({
|
|
1090
|
+
name: v.optional(v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(80))),
|
|
1091
|
+
logoURL: v.optional(httpURLSchema),
|
|
1092
|
+
helpURL: v.optional(httpURLSchema)
|
|
1093
|
+
})),
|
|
845
1094
|
/** Where server errors are reported; without it the server reports nothing. */
|
|
846
1095
|
errorTracking: v.optional(v.object({
|
|
847
1096
|
provider: v.literal("posthog"),
|
|
@@ -863,6 +1112,7 @@ function validateAuthenticationConfig(config) {
|
|
|
863
1112
|
if (config.emailPasswordMinimumLength > config.emailPasswordMaximumLength) throw new CloudConfigError("Minimum password length cannot exceed maximum password length");
|
|
864
1113
|
if (config.emailPasswordEnabled && config.emailTransport === "none") throw new CloudConfigError("Email and password authentication requires transactional email delivery");
|
|
865
1114
|
if (config.deploymentAdminMFARequired && !config.totpEnabled && !config.passkeysEnabled) throw new CloudConfigError("Deployment administrator MFA requires TOTP or passkeys");
|
|
1115
|
+
if (config.deployment === "official" && !config.deploymentAdminMFARequired) throw new CloudConfigError("An official deployment requires MFA for deployment administrators");
|
|
866
1116
|
if (config.recoveryCodesEnabled && !config.totpEnabled) throw new CloudConfigError("MFA recovery codes require TOTP");
|
|
867
1117
|
requireTogether(config, "CAPTCHA", [
|
|
868
1118
|
"captchaProvider",
|
|
@@ -887,9 +1137,15 @@ function validateEmailConfig(config) {
|
|
|
887
1137
|
if (config.deployment === "official" && config.smtpHost && config.smtpSecure === false) throw new CloudConfigError("Official SMTP delivery must use a secure connection");
|
|
888
1138
|
}
|
|
889
1139
|
function validateServiceConfig(config) {
|
|
890
|
-
requireTogether(config, "
|
|
1140
|
+
requireTogether(config, "Services", ["servicesEndpoint", "servicesSigningKey"]);
|
|
1141
|
+
if (config.billingEndpoint && !config.servicesSigningKey) throw new CloudConfigError("Billing needs the services signing key to sign its tokens");
|
|
1142
|
+
if (config.billingEndpoint && !config.internalServiceToken) throw new CloudConfigError("Billing needs the internal API to set the plans it sells");
|
|
891
1143
|
if (config.internalServiceToken && config.staticEntitlements) throw new CloudConfigError("The internal API writes database entitlements, which static entitlements would hide");
|
|
892
|
-
|
|
1144
|
+
for (const [name, endpoint] of [["services", config.servicesEndpoint], ["billing", config.billingEndpoint]]) if (endpoint && !sendsTokenSafely(endpoint)) throw new CloudConfigError(`The ${name} endpoint must use HTTPS, or HTTP only to 127.0.0.1 or [::1] for development`);
|
|
1145
|
+
}
|
|
1146
|
+
function validateLegalConfig(config) {
|
|
1147
|
+
const legal = config.legal;
|
|
1148
|
+
if (legal?.requireAcceptance && !(legal.termsURL && legal.termsVersion)) throw new CloudConfigError("[legal] require_acceptance needs terms_url and terms_version");
|
|
893
1149
|
}
|
|
894
1150
|
function validateObjectStorageConfig(config) {
|
|
895
1151
|
if (config.s3KmsKeyId && config.s3ServerSideEncryption !== "aws:kms") throw new CloudConfigError("S3 KMS key ID requires aws:kms server-side encryption");
|
|
@@ -904,208 +1160,9 @@ function parseCloudServerConfig(input) {
|
|
|
904
1160
|
validateEmailConfig(config);
|
|
905
1161
|
validateServiceConfig(config);
|
|
906
1162
|
validateObjectStorageConfig(config);
|
|
1163
|
+
validateLegalConfig(config);
|
|
907
1164
|
return config;
|
|
908
1165
|
}
|
|
909
|
-
/** PostHog's US Cloud ingestion host. */
|
|
910
|
-
const POSTHOG_DEFAULT_HOST = "https://us.i.posthog.com";
|
|
911
|
-
const DEFAULT_SECRET_NAMES = {
|
|
912
|
-
databaseURL: "DATABASE_URL",
|
|
913
|
-
authSecret: "BETTER_AUTH_SECRET",
|
|
914
|
-
googleClientId: "GOOGLE_CLIENT_ID",
|
|
915
|
-
googleClientSecret: "GOOGLE_CLIENT_SECRET",
|
|
916
|
-
applePrivateKey: "APPLE_PRIVATE_KEY",
|
|
917
|
-
s3AccessKeyId: "S3_ACCESS_KEY_ID",
|
|
918
|
-
s3SecretAccessKey: "S3_SECRET_ACCESS_KEY",
|
|
919
|
-
s3SessionToken: "S3_SESSION_TOKEN",
|
|
920
|
-
smtpUser: "OPENPENCIL_CLOUD_SMTP_USER",
|
|
921
|
-
smtpPassword: "OPENPENCIL_CLOUD_SMTP_PASSWORD",
|
|
922
|
-
turnstileSecretKey: "TURNSTILE_SECRET_KEY",
|
|
923
|
-
aiSigningKey: "OPENPENCIL_CLOUD_AI_SIGNING_KEY",
|
|
924
|
-
internalServiceToken: "OPENPENCIL_CLOUD_SERVICE_TOKEN",
|
|
925
|
-
posthogProjectAPIKey: "POSTHOG_PROJECT_API_KEY"
|
|
926
|
-
};
|
|
927
|
-
const urlSchema = v.pipe(v.string(), v.url());
|
|
928
|
-
const textSchema = v.pipe(v.string(), v.trim(), v.minLength(1));
|
|
929
|
-
const positiveIntegerSchema = v.pipe(v.number(), v.integer(), v.minValue(1));
|
|
930
|
-
const secretReferenceSchema = v.strictObject({ from_env: textSchema });
|
|
931
|
-
const configurableTextSchema = v.union([textSchema, secretReferenceSchema]);
|
|
932
|
-
const defaultReference = (name) => v.optional(secretReferenceSchema, { from_env: name });
|
|
933
|
-
const providerSchema = v.strictObject({
|
|
934
|
-
google: v.optional(v.strictObject({
|
|
935
|
-
client_id: v.optional(configurableTextSchema, { from_env: DEFAULT_SECRET_NAMES.googleClientId }),
|
|
936
|
-
client_secret: defaultReference(DEFAULT_SECRET_NAMES.googleClientSecret)
|
|
937
|
-
})),
|
|
938
|
-
apple: v.optional(v.strictObject({
|
|
939
|
-
client_id: textSchema,
|
|
940
|
-
team_id: textSchema,
|
|
941
|
-
key_id: textSchema,
|
|
942
|
-
private_key: defaultReference(DEFAULT_SECRET_NAMES.applePrivateKey),
|
|
943
|
-
app_bundle_identifier: v.optional(textSchema)
|
|
944
|
-
}))
|
|
945
|
-
});
|
|
946
|
-
const emailSchema = v.optional(v.variant("transport", [
|
|
947
|
-
v.strictObject({ transport: v.literal("none") }),
|
|
948
|
-
v.strictObject({
|
|
949
|
-
transport: v.literal("smtp"),
|
|
950
|
-
from: textSchema,
|
|
951
|
-
smtp: v.strictObject({
|
|
952
|
-
host: textSchema,
|
|
953
|
-
port: positiveIntegerSchema,
|
|
954
|
-
secure: v.optional(v.boolean()),
|
|
955
|
-
user: v.optional(secretReferenceSchema, { from_env: DEFAULT_SECRET_NAMES.smtpUser }),
|
|
956
|
-
password: v.optional(secretReferenceSchema, { from_env: DEFAULT_SECRET_NAMES.smtpPassword })
|
|
957
|
-
})
|
|
958
|
-
}),
|
|
959
|
-
v.strictObject({
|
|
960
|
-
transport: v.literal("cloudflare"),
|
|
961
|
-
from: textSchema
|
|
962
|
-
})
|
|
963
|
-
]), { transport: "none" });
|
|
964
|
-
/**
|
|
965
|
-
* Every table is strict: a key the schema doesn't know, such as one renamed since, stops startup
|
|
966
|
-
* instead of leaving its setting at a default the operator never chose.
|
|
967
|
-
*/
|
|
968
|
-
const cloudDeploymentConfigSchema = v.strictObject({
|
|
969
|
-
schema_version: v.literal(2),
|
|
970
|
-
deployment: v.strictObject({
|
|
971
|
-
mode: v.picklist(["official", "self-hosted"]),
|
|
972
|
-
public_url: urlSchema,
|
|
973
|
-
indexing: v.optional(v.picklist(["allow", "deny"]), "deny"),
|
|
974
|
-
trusted_origins: v.optional(v.array(urlSchema), []),
|
|
975
|
-
local_editor: v.optional(v.boolean(), true),
|
|
976
|
-
listing: v.optional(v.strictObject({ minimum_editor_version: cloudEditorVersionSchema }))
|
|
977
|
-
}),
|
|
978
|
-
database: v.optional(v.strictObject({ url: defaultReference(DEFAULT_SECRET_NAMES.databaseURL) }), { url: { from_env: DEFAULT_SECRET_NAMES.databaseURL } }),
|
|
979
|
-
authentication: v.strictObject({
|
|
980
|
-
secret: defaultReference(DEFAULT_SECRET_NAMES.authSecret),
|
|
981
|
-
enrollment: v.optional(v.strictObject({
|
|
982
|
-
mode: v.optional(v.picklist([
|
|
983
|
-
"open",
|
|
984
|
-
"approval",
|
|
985
|
-
"closed"
|
|
986
|
-
]), "open"),
|
|
987
|
-
notification_emails: v.optional(v.array(v.pipe(v.string(), v.email())), [])
|
|
988
|
-
}), {
|
|
989
|
-
mode: "open",
|
|
990
|
-
notification_emails: []
|
|
991
|
-
}),
|
|
992
|
-
email_password: v.optional(v.strictObject({
|
|
993
|
-
enabled: v.optional(v.boolean(), false),
|
|
994
|
-
sign_up: v.optional(v.boolean(), true),
|
|
995
|
-
minimum_password_length: v.optional(v.pipe(v.number(), v.integer(), v.minValue(8), v.maxValue(128)), 8),
|
|
996
|
-
maximum_password_length: v.optional(v.pipe(v.number(), v.integer(), v.minValue(15), v.maxValue(128)), 128),
|
|
997
|
-
verification_link_lifetime_minutes: v.optional(positiveIntegerSchema, 60),
|
|
998
|
-
password_reset_link_lifetime_minutes: v.optional(positiveIntegerSchema, 60),
|
|
999
|
-
compromised_password_check: v.optional(v.boolean(), false)
|
|
1000
|
-
}), {
|
|
1001
|
-
enabled: false,
|
|
1002
|
-
sign_up: true,
|
|
1003
|
-
minimum_password_length: 8,
|
|
1004
|
-
maximum_password_length: 128,
|
|
1005
|
-
verification_link_lifetime_minutes: 60,
|
|
1006
|
-
password_reset_link_lifetime_minutes: 60,
|
|
1007
|
-
compromised_password_check: false
|
|
1008
|
-
}),
|
|
1009
|
-
mfa: v.optional(v.strictObject({
|
|
1010
|
-
required_for_admins: v.optional(v.boolean(), false),
|
|
1011
|
-
totp: v.optional(v.boolean(), false),
|
|
1012
|
-
recovery_codes: v.optional(v.boolean(), false),
|
|
1013
|
-
trusted_device_lifetime_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 14)
|
|
1014
|
-
}), {
|
|
1015
|
-
required_for_admins: false,
|
|
1016
|
-
totp: false,
|
|
1017
|
-
recovery_codes: false,
|
|
1018
|
-
trusted_device_lifetime_days: 14
|
|
1019
|
-
}),
|
|
1020
|
-
passkeys: v.optional(v.strictObject({
|
|
1021
|
-
enabled: v.optional(v.boolean(), false),
|
|
1022
|
-
rp_name: v.optional(textSchema, "OpenPencil Cloud")
|
|
1023
|
-
}), {
|
|
1024
|
-
enabled: false,
|
|
1025
|
-
rp_name: "OpenPencil Cloud"
|
|
1026
|
-
}),
|
|
1027
|
-
captcha: v.optional(v.strictObject({
|
|
1028
|
-
provider: v.literal("cloudflare-turnstile"),
|
|
1029
|
-
site_key: textSchema,
|
|
1030
|
-
secret_key: defaultReference(DEFAULT_SECRET_NAMES.turnstileSecretKey),
|
|
1031
|
-
allowed_hostnames: v.optional(v.array(textSchema), [])
|
|
1032
|
-
})),
|
|
1033
|
-
trusted_proxies: v.optional(v.strictObject({
|
|
1034
|
-
headers: v.optional(v.array(textSchema), []),
|
|
1035
|
-
addresses: v.optional(v.array(textSchema), [])
|
|
1036
|
-
}), {
|
|
1037
|
-
headers: [],
|
|
1038
|
-
addresses: []
|
|
1039
|
-
}),
|
|
1040
|
-
providers: v.optional(providerSchema, {})
|
|
1041
|
-
}),
|
|
1042
|
-
object_storage: v.strictObject({
|
|
1043
|
-
endpoint: urlSchema,
|
|
1044
|
-
region: textSchema,
|
|
1045
|
-
bucket: textSchema,
|
|
1046
|
-
credentials: v.optional(v.strictObject({
|
|
1047
|
-
access_key_id: defaultReference(DEFAULT_SECRET_NAMES.s3AccessKeyId),
|
|
1048
|
-
secret_access_key: defaultReference(DEFAULT_SECRET_NAMES.s3SecretAccessKey),
|
|
1049
|
-
session_token: v.optional(secretReferenceSchema, { from_env: DEFAULT_SECRET_NAMES.s3SessionToken })
|
|
1050
|
-
}), {
|
|
1051
|
-
access_key_id: { from_env: DEFAULT_SECRET_NAMES.s3AccessKeyId },
|
|
1052
|
-
secret_access_key: { from_env: DEFAULT_SECRET_NAMES.s3SecretAccessKey }
|
|
1053
|
-
}),
|
|
1054
|
-
force_path_style: v.optional(v.boolean(), true),
|
|
1055
|
-
checksum_verification: v.optional(v.picklist(["native", "metadata"]), "native"),
|
|
1056
|
-
server_side_encryption: v.optional(v.picklist(["AES256", "aws:kms"])),
|
|
1057
|
-
kms_key_id: v.optional(textSchema),
|
|
1058
|
-
maximum_upload_bytes: v.optional(positiveIntegerSchema)
|
|
1059
|
-
}),
|
|
1060
|
-
collaboration: v.optional(v.strictObject({
|
|
1061
|
-
relay_url: v.optional(urlSchema),
|
|
1062
|
-
maximum_message_bytes: v.optional(positiveIntegerSchema),
|
|
1063
|
-
maximum_connections_per_room: v.optional(positiveIntegerSchema)
|
|
1064
|
-
}), {}),
|
|
1065
|
-
documents: v.optional(v.strictObject({ retention_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 30) }), { retention_days: 30 }),
|
|
1066
|
-
email: emailSchema,
|
|
1067
|
-
workers: v.optional(v.strictObject({
|
|
1068
|
-
email: v.optional(v.strictObject({
|
|
1069
|
-
batch_size: v.optional(positiveIntegerSchema, 50),
|
|
1070
|
-
interval_ms: v.optional(positiveIntegerSchema, 3e4),
|
|
1071
|
-
lease_ms: v.optional(positiveIntegerSchema, 3e5),
|
|
1072
|
-
maximum_attempts: v.optional(positiveIntegerSchema, 5)
|
|
1073
|
-
}), {
|
|
1074
|
-
batch_size: 50,
|
|
1075
|
-
interval_ms: 3e4,
|
|
1076
|
-
lease_ms: 3e5,
|
|
1077
|
-
maximum_attempts: 5
|
|
1078
|
-
}),
|
|
1079
|
-
cleanup: v.optional(v.strictObject({
|
|
1080
|
-
enabled: v.optional(v.boolean(), true),
|
|
1081
|
-
batch_size: v.optional(positiveIntegerSchema, 100),
|
|
1082
|
-
interval_ms: v.optional(positiveIntegerSchema, 6e4),
|
|
1083
|
-
lease_ms: v.optional(positiveIntegerSchema, 3e5)
|
|
1084
|
-
}), {
|
|
1085
|
-
enabled: true,
|
|
1086
|
-
batch_size: 100,
|
|
1087
|
-
interval_ms: 6e4,
|
|
1088
|
-
lease_ms: 3e5
|
|
1089
|
-
})
|
|
1090
|
-
})),
|
|
1091
|
-
entitlements: v.optional(v.variant("source", [v.strictObject({
|
|
1092
|
-
source: v.literal("static"),
|
|
1093
|
-
...staticEntitlementsTOMLSchema.entries
|
|
1094
|
-
}), v.strictObject({ source: v.literal("database") })])),
|
|
1095
|
-
ai: v.optional(v.strictObject({
|
|
1096
|
-
endpoint: urlSchema,
|
|
1097
|
-
signing_key: defaultReference(DEFAULT_SECRET_NAMES.aiSigningKey),
|
|
1098
|
-
token_lifetime_minutes: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(60)), 10)
|
|
1099
|
-
})),
|
|
1100
|
-
billing: v.optional(v.strictObject({ portal_url: urlSchema })),
|
|
1101
|
-
internal_api: v.optional(v.strictObject({ service_token: defaultReference(DEFAULT_SECRET_NAMES.internalServiceToken) })),
|
|
1102
|
-
/** Optional: reports server errors to PostHog error tracking. Leave it out to send nothing. */
|
|
1103
|
-
error_tracking: v.optional(v.strictObject({
|
|
1104
|
-
provider: v.literal("posthog"),
|
|
1105
|
-
host: v.optional(urlSchema, POSTHOG_DEFAULT_HOST),
|
|
1106
|
-
project_api_key: v.optional(configurableTextSchema, { from_env: DEFAULT_SECRET_NAMES.posthogProjectAPIKey })
|
|
1107
|
-
}))
|
|
1108
|
-
});
|
|
1109
1166
|
//#endregion
|
|
1110
1167
|
//#region src/server/db/expressions.ts
|
|
1111
1168
|
const CURRENT_TIMESTAMP = sql`current_timestamp`;
|
|
@@ -1115,533 +1172,450 @@ function checkColumnIn(column, values) {
|
|
|
1115
1172
|
if (values.length === 0 || values.some((value) => !CHECK_VALUE_PATTERN.test(value))) throw new Error("Check constraint values must be non-empty lowercase identifiers");
|
|
1116
1173
|
return sql`${sql.ref(column)} in (${sql.join(values.map((value) => sql.lit(value)))})`;
|
|
1117
1174
|
}
|
|
1118
|
-
//#endregion
|
|
1119
|
-
//#region src/server/db/migrations/helpers.ts
|
|
1120
|
-
async function addCheckConstraint(database, table, constraint, column, values) {
|
|
1121
|
-
await database.schema.alterTable(table).addCheckConstraint(constraint, checkColumnIn(column, values)).execute();
|
|
1122
|
-
}
|
|
1123
|
-
async function replaceCheckConstraint(database, table, constraint, column, values) {
|
|
1124
|
-
await database.schema.alterTable(table).dropConstraint(constraint).execute();
|
|
1125
|
-
await addCheckConstraint(database, table, constraint, column, values);
|
|
1126
|
-
}
|
|
1127
|
-
async function addCurrentTimestampColumns(database, table, columns) {
|
|
1128
|
-
for (const column of columns) await database.schema.alterTable(table).addColumn(column, "timestamptz", (definition) => definition.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1129
|
-
}
|
|
1130
1175
|
/**
|
|
1131
|
-
*
|
|
1132
|
-
*
|
|
1176
|
+
* Holds a lock named `key` until the transaction ends, for a check and a write that must not
|
|
1177
|
+
* interleave with another transaction's, such as reserving storage from an account's allowance,
|
|
1178
|
+
* which spans rows no single one of which could be locked for it.
|
|
1133
1179
|
*/
|
|
1134
|
-
function
|
|
1135
|
-
|
|
1136
|
-
}
|
|
1137
|
-
//#endregion
|
|
1138
|
-
//#region src/server/db/migrations/001_foundation.ts
|
|
1139
|
-
var _001_foundation_exports = /* @__PURE__ */ __exportAll({
|
|
1140
|
-
down: () => down$27,
|
|
1141
|
-
up: () => up$27
|
|
1142
|
-
});
|
|
1143
|
-
const UPDATED_AT_TRIGGER = "openpencil_set_updated_at";
|
|
1144
|
-
async function up$27(database) {
|
|
1145
|
-
await database.schema.createTable("workspace").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("name", "text", (column) => column.notNull()).addColumn("slug", "text", (column) => column.notNull().unique()).addColumn("created_by", "text", (column) => column.notNull()).execute();
|
|
1146
|
-
await addCurrentTimestampColumns(database, "workspace", ["created_at", "updated_at"]);
|
|
1147
|
-
await database.schema.createTable("workspace_member").addColumn("workspace_id", "uuid", (column) => column.references("workspace.id").onDelete("cascade").notNull()).addColumn("user_id", "text", (column) => column.notNull()).addColumn("role", "text", (column) => column.notNull()).addPrimaryKeyConstraint("workspace_member_primary", ["workspace_id", "user_id"]).addCheckConstraint("workspace_member_role_check", checkColumnIn("role", [
|
|
1148
|
-
"admin",
|
|
1149
|
-
"editor",
|
|
1150
|
-
"viewer"
|
|
1151
|
-
])).execute();
|
|
1152
|
-
await addCurrentTimestampColumns(database, "workspace_member", ["created_at"]);
|
|
1153
|
-
await database.schema.createTable("document").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("workspace_id", "uuid", (column) => column.references("workspace.id").onDelete("cascade").notNull()).addColumn("name", "text", (column) => column.notNull()).addColumn("current_revision_id", "uuid").addColumn("version", "integer", (column) => column.notNull().defaultTo(0)).addColumn("created_by", "text", (column) => column.notNull()).addColumn("deleted_at", "timestamptz").execute();
|
|
1154
|
-
await addCurrentTimestampColumns(database, "document", ["created_at", "updated_at"]);
|
|
1155
|
-
await database.schema.createTable("storage_object").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("object_key", "text", (column) => column.notNull().unique()).addColumn("checksum", "text", (column) => column.notNull()).addColumn("byte_size", "bigint", (column) => column.notNull()).addColumn("content_type", "text", (column) => column.notNull()).addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1156
|
-
await database.schema.createTable("document_revision").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()).addColumn("parent_revision_id", "uuid", (column) => column.references("document_revision.id").onDelete("set null")).addColumn("storage_object_id", "uuid", (column) => column.references("storage_object.id").onDelete("restrict").notNull()).addColumn("created_by", "text", (column) => column.notNull()).addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1157
|
-
await database.schema.alterTable("document").addForeignKeyConstraint("document_current_revision_foreign", ["current_revision_id"], "document_revision", ["id"], (constraint) => constraint.onDelete("set null")).execute();
|
|
1158
|
-
await database.schema.createTable("upload").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()).addColumn("base_revision_id", "uuid", (column) => column.references("document_revision.id").onDelete("set null")).addColumn("object_key", "text", (column) => column.notNull().unique()).addColumn("status", "text", (column) => column.notNull()).addColumn("checksum", "text", (column) => column.notNull()).addColumn("created_by", "text", (column) => column.notNull()).addColumn("byte_size", "bigint", (column) => column.notNull()).addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("content_type", "text", (column) => column.notNull()).addColumn("multipart_upload_id", "text").addColumn("expires_at", "timestamptz", (column) => column.notNull()).addCheckConstraint("upload_status_check", checkColumnIn("status", [
|
|
1159
|
-
"pending",
|
|
1160
|
-
"committed",
|
|
1161
|
-
"abandoned"
|
|
1162
|
-
])).execute();
|
|
1163
|
-
await database.schema.createIndex("workspace_member_user_index").on("workspace_member").column("user_id").execute();
|
|
1164
|
-
await database.schema.createIndex("document_workspace_index").on("document").columns(["workspace_id", "updated_at"]).execute();
|
|
1165
|
-
await database.schema.createIndex("document_revision_document_index").on("document_revision").columns(["document_id", "created_at"]).execute();
|
|
1166
|
-
await database.schema.createIndex("upload_expiration_index").on("upload").columns(["status", "expires_at"]).execute();
|
|
1167
|
-
await sql`
|
|
1168
|
-
create function ${sql.id(UPDATED_AT_TRIGGER)}() returns trigger as $$
|
|
1169
|
-
begin
|
|
1170
|
-
new.updated_at = now();
|
|
1171
|
-
return new;
|
|
1172
|
-
end;
|
|
1173
|
-
$$ language plpgsql
|
|
1174
|
-
`.execute(database);
|
|
1175
|
-
for (const table of ["workspace", "document"]) await sql`
|
|
1176
|
-
create trigger ${sql.id(`${table}_updated_at`)}
|
|
1177
|
-
before update on ${sql.table(table)}
|
|
1178
|
-
for each row execute function ${sql.id(UPDATED_AT_TRIGGER)}()
|
|
1179
|
-
`.execute(database);
|
|
1180
|
-
}
|
|
1181
|
-
async function down$27(database) {
|
|
1182
|
-
await database.schema.dropTable("upload").ifExists().execute();
|
|
1183
|
-
await database.schema.alterTable("document").dropConstraint("document_current_revision_foreign").execute();
|
|
1184
|
-
await database.schema.dropTable("document_revision").ifExists().execute();
|
|
1185
|
-
await database.schema.dropTable("storage_object").ifExists().execute();
|
|
1186
|
-
await database.schema.dropTable("document").ifExists().execute();
|
|
1187
|
-
await database.schema.dropTable("workspace_member").ifExists().execute();
|
|
1188
|
-
await database.schema.dropTable("workspace").ifExists().execute();
|
|
1189
|
-
await sql`drop function if exists ${sql.id(UPDATED_AT_TRIGGER)}()`.execute(database);
|
|
1190
|
-
}
|
|
1191
|
-
//#endregion
|
|
1192
|
-
//#region src/server/db/migrations/002_upload_cleanup_claims.ts
|
|
1193
|
-
var _002_upload_cleanup_claims_exports = /* @__PURE__ */ __exportAll({
|
|
1194
|
-
down: () => down$26,
|
|
1195
|
-
up: () => up$26
|
|
1196
|
-
});
|
|
1197
|
-
async function up$26(database) {
|
|
1198
|
-
await database.schema.alterTable("upload").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").execute();
|
|
1199
|
-
await database.schema.alterTable("upload").dropConstraint("upload_status_check").execute();
|
|
1200
|
-
await database.schema.alterTable("upload").addCheckConstraint("upload_status_check", checkColumnIn("status", [
|
|
1201
|
-
"pending",
|
|
1202
|
-
"cleaning",
|
|
1203
|
-
"committed",
|
|
1204
|
-
"abandoned"
|
|
1205
|
-
])).execute();
|
|
1206
|
-
}
|
|
1207
|
-
async function down$26(database) {
|
|
1208
|
-
await database.schema.alterTable("upload").dropConstraint("upload_status_check").execute();
|
|
1209
|
-
await database.schema.alterTable("upload").addCheckConstraint("upload_status_check", checkColumnIn("status", [
|
|
1210
|
-
"pending",
|
|
1211
|
-
"committed",
|
|
1212
|
-
"abandoned"
|
|
1213
|
-
])).execute();
|
|
1214
|
-
await database.schema.alterTable("upload").dropColumn("cleanup_claimed_at").dropColumn("cleanup_claim_id").execute();
|
|
1215
|
-
}
|
|
1216
|
-
//#endregion
|
|
1217
|
-
//#region src/server/db/migrations/003_document_cleanup_claims.ts
|
|
1218
|
-
var _003_document_cleanup_claims_exports = /* @__PURE__ */ __exportAll({
|
|
1219
|
-
down: () => down$25,
|
|
1220
|
-
up: () => up$25
|
|
1221
|
-
});
|
|
1222
|
-
async function up$25(database) {
|
|
1223
|
-
await database.schema.alterTable("document").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").execute();
|
|
1224
|
-
await database.schema.createIndex("document_cleanup_index").on("document").columns(["deleted_at", "cleanup_claimed_at"]).execute();
|
|
1225
|
-
}
|
|
1226
|
-
async function down$25(database) {
|
|
1227
|
-
await database.schema.dropIndex("document_cleanup_index").ifExists().execute();
|
|
1228
|
-
await database.schema.alterTable("document").dropColumn("cleanup_claimed_at").dropColumn("cleanup_claim_id").execute();
|
|
1229
|
-
}
|
|
1230
|
-
//#endregion
|
|
1231
|
-
//#region src/server/db/migrations/004_document_sharing.ts
|
|
1232
|
-
var _004_document_sharing_exports = /* @__PURE__ */ __exportAll({
|
|
1233
|
-
down: () => down$24,
|
|
1234
|
-
up: () => up$24
|
|
1235
|
-
});
|
|
1236
|
-
async function up$24(database) {
|
|
1237
|
-
await database.schema.createTable("document_share").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()).addColumn("permission", "text", (column) => column.notNull()).addColumn("secret_hash", "text", (column) => column.notNull()).addColumn("room_epoch", "integer", (column) => column.notNull().defaultTo(0)).addColumn("created_by", "text", (column) => column.notNull()).addColumn("expires_at", "timestamptz").addColumn("revoked_at", "timestamptz").addColumn("last_used_at", "timestamptz").addCheckConstraint("document_share_permission_check", checkColumnIn("permission", ["view", "edit"])).execute();
|
|
1238
|
-
await addCurrentTimestampColumns(database, "document_share", ["created_at", "updated_at"]);
|
|
1239
|
-
await database.schema.createTable("document_grant").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()).addColumn("user_id", "text", (column) => column.notNull()).addColumn("permission", "text", (column) => column.notNull()).addColumn("created_by", "text", (column) => column.notNull()).addColumn("revoked_at", "timestamptz").addUniqueConstraint("document_grant_document_user_unique", ["document_id", "user_id"]).addCheckConstraint("document_grant_permission_check", checkColumnIn("permission", ["view", "edit"])).execute();
|
|
1240
|
-
await addCurrentTimestampColumns(database, "document_grant", ["created_at", "updated_at"]);
|
|
1241
|
-
await addInvitationColumns(database.schema.createTable("document_invitation").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()), "permission").addCheckConstraint("document_invitation_permission_check", checkColumnIn("permission", ["view", "edit"])).execute();
|
|
1242
|
-
await addCurrentTimestampColumns(database, "document_invitation", ["invited_at"]);
|
|
1243
|
-
await database.schema.createIndex("document_share_document_index").on("document_share").columns(["document_id", "revoked_at"]).execute();
|
|
1244
|
-
await database.schema.createIndex("document_grant_user_index").on("document_grant").columns(["user_id", "document_id"]).execute();
|
|
1245
|
-
await database.schema.createIndex("document_invitation_document_index").on("document_invitation").columns(["document_id", "revoked_at"]).execute();
|
|
1246
|
-
}
|
|
1247
|
-
async function down$24(database) {
|
|
1248
|
-
await database.schema.dropTable("document_invitation").ifExists().execute();
|
|
1249
|
-
await database.schema.dropTable("document_grant").ifExists().execute();
|
|
1250
|
-
await database.schema.dropTable("document_share").ifExists().execute();
|
|
1251
|
-
}
|
|
1252
|
-
//#endregion
|
|
1253
|
-
//#region src/server/db/migrations/005_document_collaboration_epoch.ts
|
|
1254
|
-
var _005_document_collaboration_epoch_exports = /* @__PURE__ */ __exportAll({
|
|
1255
|
-
down: () => down$23,
|
|
1256
|
-
up: () => up$23
|
|
1257
|
-
});
|
|
1258
|
-
async function up$23(database) {
|
|
1259
|
-
await database.schema.alterTable("document").addColumn("collaborationEpoch", "integer", (column) => column.notNull().defaultTo(0)).execute();
|
|
1260
|
-
}
|
|
1261
|
-
async function down$23(database) {
|
|
1262
|
-
await database.schema.alterTable("document").dropColumn("collaborationEpoch").execute();
|
|
1263
|
-
}
|
|
1264
|
-
//#endregion
|
|
1265
|
-
//#region src/server/db/migrations/006_invitation_continuation.ts
|
|
1266
|
-
var _006_invitation_continuation_exports = /* @__PURE__ */ __exportAll({
|
|
1267
|
-
down: () => down$22,
|
|
1268
|
-
up: () => up$22
|
|
1269
|
-
});
|
|
1270
|
-
async function up$22(database) {
|
|
1271
|
-
await database.schema.createTable("invitationContinuation").addColumn("id", "text", (column) => column.primaryKey()).addColumn("invitationId", "uuid", (column) => column.notNull().references("documentInvitation.id").onDelete("cascade")).addColumn("tokenEncrypted", "text", (column) => column.notNull()).addColumn("expiresAt", "timestamptz", (column) => column.notNull()).addColumn("consumedAt", "timestamptz").addColumn("createdAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).execute();
|
|
1272
|
-
await database.schema.createIndex("invitationContinuationExpiryIndex").on("invitationContinuation").column("expiresAt").execute();
|
|
1273
|
-
}
|
|
1274
|
-
async function down$22(database) {
|
|
1275
|
-
await database.schema.dropTable("invitationContinuation").execute();
|
|
1276
|
-
}
|
|
1277
|
-
//#endregion
|
|
1278
|
-
//#region src/server/db/migrations/007_collaboration_state.ts
|
|
1279
|
-
var _007_collaboration_state_exports = /* @__PURE__ */ __exportAll({
|
|
1280
|
-
down: () => down$21,
|
|
1281
|
-
up: () => up$21
|
|
1282
|
-
});
|
|
1283
|
-
async function up$21(database) {
|
|
1284
|
-
await database.schema.createTable("documentCollaborationState").addColumn("documentId", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("roomEpoch", "integer", (column) => column.notNull()).addColumn("state", "bytea", (column) => column.notNull()).addColumn("version", "integer", (column) => column.notNull().defaultTo(1)).addColumn("createdAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).addColumn("updatedAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).addPrimaryKeyConstraint("documentCollaborationStatePrimary", ["documentId", "roomEpoch"]).execute();
|
|
1285
|
-
}
|
|
1286
|
-
async function down$21(database) {
|
|
1287
|
-
await database.schema.dropTable("documentCollaborationState").execute();
|
|
1288
|
-
}
|
|
1289
|
-
//#endregion
|
|
1290
|
-
//#region src/server/db/migrations/008_storage_reservations.ts
|
|
1291
|
-
var _008_storage_reservations_exports = /* @__PURE__ */ __exportAll({
|
|
1292
|
-
down: () => down$20,
|
|
1293
|
-
up: () => up$20
|
|
1294
|
-
});
|
|
1295
|
-
async function up$20(database) {
|
|
1296
|
-
await database.schema.createTable("workspaceStorageUsage").addColumn("workspaceId", "uuid", (column) => column.primaryKey().references("workspace.id").onDelete("cascade")).addColumn("committedBytes", "bigint", (column) => column.notNull().defaultTo(0)).addColumn("updatedAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).execute();
|
|
1297
|
-
await database.schema.createTable("uploadStorageReservation").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("workspaceId", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("uploadId", "uuid", (column) => column.notNull().unique().references("upload.id").onDelete("cascade")).addColumn("bytes", "bigint", (column) => column.notNull()).addColumn("expiresAt", "timestamptz", (column) => column.notNull()).addColumn("committedAt", "timestamptz").addColumn("releasedAt", "timestamptz").addColumn("createdAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).execute();
|
|
1298
|
-
}
|
|
1299
|
-
async function down$20(database) {
|
|
1300
|
-
await database.schema.dropTable("uploadStorageReservation").execute();
|
|
1301
|
-
await database.schema.dropTable("workspaceStorageUsage").execute();
|
|
1302
|
-
}
|
|
1303
|
-
//#endregion
|
|
1304
|
-
//#region src/server/db/migrations/009_workspace_entitlements.ts
|
|
1305
|
-
var _009_workspace_entitlements_exports = /* @__PURE__ */ __exportAll({
|
|
1306
|
-
down: () => down$19,
|
|
1307
|
-
up: () => up$19
|
|
1308
|
-
});
|
|
1309
|
-
async function up$19(database) {
|
|
1310
|
-
await database.schema.createTable("workspaceEntitlement").addColumn("workspaceId", "uuid", (column) => column.primaryKey().references("workspace.id").onDelete("cascade")).addColumn("values", "jsonb", (column) => column.notNull()).addColumn("source", "text", (column) => column.notNull()).addColumn("revision", "integer", (column) => column.notNull().defaultTo(1)).addColumn("updatedAt", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1311
|
-
}
|
|
1312
|
-
async function down$19(database) {
|
|
1313
|
-
await database.schema.dropTable("workspaceEntitlement").execute();
|
|
1314
|
-
}
|
|
1315
|
-
//#endregion
|
|
1316
|
-
//#region src/server/db/migrations/010_upload_finalization.ts
|
|
1317
|
-
var _010_upload_finalization_exports = /* @__PURE__ */ __exportAll({
|
|
1318
|
-
down: () => down$18,
|
|
1319
|
-
up: () => up$18
|
|
1320
|
-
});
|
|
1321
|
-
async function up$18(database) {
|
|
1322
|
-
await replaceCheckConstraint(database, "upload", "upload_status_check", "status", [
|
|
1323
|
-
"pending",
|
|
1324
|
-
"finalizing",
|
|
1325
|
-
"cleaning",
|
|
1326
|
-
"committed",
|
|
1327
|
-
"abandoned"
|
|
1328
|
-
]);
|
|
1329
|
-
}
|
|
1330
|
-
async function down$18(database) {
|
|
1331
|
-
await replaceCheckConstraint(database, "upload", "upload_status_check", "status", [
|
|
1332
|
-
"pending",
|
|
1333
|
-
"cleaning",
|
|
1334
|
-
"committed",
|
|
1335
|
-
"abandoned"
|
|
1336
|
-
]);
|
|
1180
|
+
async function lockForTransaction(transaction, key) {
|
|
1181
|
+
await sql`select pg_advisory_xact_lock(hashtextextended(${key}, 0))`.execute(transaction);
|
|
1337
1182
|
}
|
|
1338
1183
|
//#endregion
|
|
1339
|
-
//#region src/server/db/migrations/
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1184
|
+
//#region src/server/db/migrations/schema.ts
|
|
1185
|
+
/**
|
|
1186
|
+
* What Cloud's migrations build tables with. Names follow PostgreSQL's own: `<table>_pkey`,
|
|
1187
|
+
* `<table>_<columns>_key` for unique, `<table>_<columns>_fkey`, and `<table>_<column>_check`;
|
|
1188
|
+
* indexes are `<table>_<columns>_idx`. Account IDs are text, since Better Auth's tables come after
|
|
1189
|
+
* Cloud's (see `migrateCloudDatabase`), and never foreign keys.
|
|
1190
|
+
*/
|
|
1191
|
+
const now = (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP);
|
|
1192
|
+
const required = (column) => column.notNull();
|
|
1193
|
+
const PERMISSIONS = ["view", "edit"];
|
|
1194
|
+
const ROLES = [
|
|
1195
|
+
"admin",
|
|
1196
|
+
"editor",
|
|
1197
|
+
"viewer"
|
|
1198
|
+
];
|
|
1199
|
+
/** What every emailed invitation keeps: whom it went to, its access, its secret, and its life. */
|
|
1200
|
+
function withInvitation(table, access) {
|
|
1201
|
+
return table.addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("email_normalized", "text", required).addColumn(access, "text", required).addColumn("token_hash", "text", required).addColumn("invited_by", "text", required).addColumn("invited_at", "timestamptz", now).addColumn("expires_at", "timestamptz", required).addColumn("accepted_at", "timestamptz").addColumn("revoked_at", "timestamptz");
|
|
1346
1202
|
}
|
|
1347
|
-
|
|
1348
|
-
|
|
1203
|
+
/** Text columns that take one of a few values, as `[table, column, values]`. */
|
|
1204
|
+
async function addChecks(database, checks) {
|
|
1205
|
+
for (const [table, column, values] of checks) await database.schema.alterTable(table).addCheckConstraint(`${table}_${column}_check`, checkColumnIn(column, values)).execute();
|
|
1349
1206
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
async function
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
"
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
}
|
|
1369
|
-
|
|
1370
|
-
//#region src/server/db/migrations/013_admin_enrollment.ts
|
|
1371
|
-
var _013_admin_enrollment_exports = /* @__PURE__ */ __exportAll({
|
|
1372
|
-
down: () => down$15,
|
|
1373
|
-
up: () => up$15
|
|
1374
|
-
});
|
|
1375
|
-
async function up$15(database) {
|
|
1376
|
-
await database.schema.createTable("cloud_enrollment").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("email_normalized", "text", (column) => column.notNull().unique()).addColumn("name", "text").addColumn("reason", "text").addColumn("status", "text", (column) => column.notNull().defaultTo("pending")).addColumn("requested_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("reviewed_at", "timestamptz").addColumn("reviewed_by", "text").addColumn("review_note", "text").addColumn("approved_user_id", "text").addCheckConstraint("cloud_enrollment_status_check", checkColumnIn("status", [
|
|
1377
|
-
"pending",
|
|
1378
|
-
"approved",
|
|
1379
|
-
"rejected",
|
|
1380
|
-
"revoked"
|
|
1381
|
-
])).execute();
|
|
1382
|
-
await database.schema.createIndex("cloud_enrollment_status_index").on("cloud_enrollment").columns(["status", "requested_at"]).execute();
|
|
1383
|
-
await database.schema.createTable("cloud_admin_audit_event").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("actor_user_id", "text", (column) => column.notNull()).addColumn("action", "text", (column) => column.notNull()).addColumn("subject_type", "text", (column) => column.notNull()).addColumn("subject_id", "text", (column) => column.notNull()).addColumn("metadata", "jsonb", (column) => column.notNull().defaultTo(EMPTY_JSON_OBJECT)).addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1384
|
-
await database.schema.createIndex("cloud_admin_audit_created_index").on("cloud_admin_audit_event").column("created_at").execute();
|
|
1385
|
-
}
|
|
1386
|
-
async function down$15(database) {
|
|
1387
|
-
await database.schema.dropTable("cloud_admin_audit_event").ifExists().execute();
|
|
1388
|
-
await database.schema.dropTable("cloud_enrollment").ifExists().execute();
|
|
1207
|
+
/**
|
|
1208
|
+
* Replaces the values a column may take, as `addChecks` named them. Widening them, as when a
|
|
1209
|
+
* release adds a kind the previous one never writes, adds the check `NOT VALID`: rows already
|
|
1210
|
+
* there passed the narrower one, so PostgreSQL skips scanning them while it holds the table, and
|
|
1211
|
+
* checks every row written from then on. Narrowing them, as a rollback does, checks every row.
|
|
1212
|
+
*/
|
|
1213
|
+
async function replaceChecks(database, change, checks) {
|
|
1214
|
+
for (const [table, column, values] of checks) {
|
|
1215
|
+
const name = `${table}_${column}_check`;
|
|
1216
|
+
await database.schema.alterTable(table).dropConstraint(name).execute();
|
|
1217
|
+
if (change === "narrow") {
|
|
1218
|
+
await addChecks(database, [[
|
|
1219
|
+
table,
|
|
1220
|
+
column,
|
|
1221
|
+
values
|
|
1222
|
+
]]);
|
|
1223
|
+
continue;
|
|
1224
|
+
}
|
|
1225
|
+
await sql`alter table ${sql.table(table)} add constraint ${sql.id(name)} check (${checkColumnIn(column, values)}) not valid`.execute(database);
|
|
1226
|
+
}
|
|
1389
1227
|
}
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
}
|
|
1396
|
-
async function up$14(database) {
|
|
1397
|
-
await replaceCheckConstraint(database, "transactional_email", "transactional_email_kind_check", "kind", [
|
|
1398
|
-
"document-invitation",
|
|
1399
|
-
"enrollment-requested",
|
|
1400
|
-
"admin-enrollment-notification",
|
|
1401
|
-
"enrollment-approved",
|
|
1402
|
-
"enrollment-rejected",
|
|
1403
|
-
"enrollment-revoked"
|
|
1404
|
-
]);
|
|
1228
|
+
/** Lookups beyond primary keys and unique columns, as `[table, columns]`; `desc` sorts newest first. */
|
|
1229
|
+
async function addIndexes(database, indexes) {
|
|
1230
|
+
for (const [table, columns] of indexes) {
|
|
1231
|
+
const name = columns.map((column) => column.split(" ")[0]).join("_");
|
|
1232
|
+
await database.schema.createIndex(`${table}_${name}_idx`).on(table).columns([...columns]).execute();
|
|
1233
|
+
}
|
|
1405
1234
|
}
|
|
1406
|
-
|
|
1407
|
-
|
|
1235
|
+
/** Drops a migration's tables, last created first. */
|
|
1236
|
+
async function dropTables(database, tables) {
|
|
1237
|
+
for (const table of tables.toReversed()) await database.schema.dropTable(table).ifExists().cascade().execute();
|
|
1408
1238
|
}
|
|
1409
1239
|
//#endregion
|
|
1410
|
-
//#region src/server/db/migrations/
|
|
1411
|
-
var
|
|
1240
|
+
//#region src/server/db/migrations/001_accounts.ts
|
|
1241
|
+
var _001_accounts_exports = /* @__PURE__ */ __exportAll({
|
|
1412
1242
|
down: () => down$13,
|
|
1413
1243
|
up: () => up$13
|
|
1414
1244
|
});
|
|
1245
|
+
/** What Cloud keeps about an account beside Better Auth's tables: its plan, sign-ins, and picture. */
|
|
1246
|
+
const TABLES$6 = [
|
|
1247
|
+
"auth_schema_version",
|
|
1248
|
+
"account_entitlement",
|
|
1249
|
+
"mfa_assurance",
|
|
1250
|
+
"sign_in",
|
|
1251
|
+
"user_picture",
|
|
1252
|
+
"api_rate_limit"
|
|
1253
|
+
];
|
|
1415
1254
|
async function up$13(database) {
|
|
1416
|
-
await database.schema.
|
|
1417
|
-
await database.schema.createTable("
|
|
1418
|
-
await
|
|
1255
|
+
await database.schema.createTable("auth_schema_version").addColumn("id", "text", (column) => column.primaryKey()).addColumn("version", "text", required).addColumn("updated_at", "timestamptz", now).execute();
|
|
1256
|
+
await database.schema.createTable("account_entitlement").addColumn("user_id", "text", (column) => column.primaryKey()).addColumn("values", "jsonb", required).addColumn("source", "text", required).addColumn("revision", "integer", (column) => column.notNull().defaultTo(1)).addColumn("external_version", "bigint").addColumn("updated_at", "timestamptz", now).execute();
|
|
1257
|
+
await database.schema.createTable("mfa_assurance").addColumn("session_id", "uuid", (column) => column.primaryKey()).addColumn("user_id", "uuid", required).addColumn("method", "text", required).addColumn("verified_at", "timestamptz", required).execute();
|
|
1258
|
+
await database.schema.createTable("sign_in").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("user_id", "uuid", required).addColumn("session_id", "uuid").addColumn("method", "text", required).addColumn("ip_address", "text").addColumn("user_agent", "text").addColumn("created_at", "timestamptz", now).execute();
|
|
1259
|
+
await database.schema.createTable("user_picture").addColumn("user_id", "uuid", (column) => column.primaryKey()).addColumn("bytes", "bytea", required).addColumn("format", "text", required).addColumn("checksum", "text", required).addColumn("source", "text", required).addColumn("created_at", "timestamptz", now).execute();
|
|
1260
|
+
await database.schema.createTable("api_rate_limit").addColumn("key_hash", "text", (column) => column.primaryKey()).addColumn("window_started_at", "timestamptz", required).addColumn("request_count", "integer", (column) => column.notNull().defaultTo(1)).addColumn("updated_at", "timestamptz", now).execute();
|
|
1261
|
+
await addChecks(database, [[
|
|
1262
|
+
"user_picture",
|
|
1263
|
+
"format",
|
|
1264
|
+
["png", "jpeg"]
|
|
1265
|
+
], [
|
|
1266
|
+
"user_picture",
|
|
1267
|
+
"source",
|
|
1268
|
+
["upload", "provider"]
|
|
1269
|
+
]]);
|
|
1270
|
+
await addIndexes(database, [["sign_in", ["user_id", "created_at"]], ["sign_in", ["session_id"]]]);
|
|
1419
1271
|
}
|
|
1420
1272
|
async function down$13(database) {
|
|
1421
|
-
await database
|
|
1422
|
-
await database.schema.alterTable("cloud_enrollment").dropColumn("request_revision").execute();
|
|
1273
|
+
await dropTables(database, TABLES$6);
|
|
1423
1274
|
}
|
|
1424
1275
|
//#endregion
|
|
1425
|
-
//#region src/server/db/migrations/
|
|
1426
|
-
var
|
|
1276
|
+
//#region src/server/db/migrations/002_workspaces.ts
|
|
1277
|
+
var _002_workspaces_exports = /* @__PURE__ */ __exportAll({
|
|
1427
1278
|
down: () => down$12,
|
|
1428
1279
|
up: () => up$12
|
|
1429
1280
|
});
|
|
1281
|
+
/** Workspaces, their members, and the people invited to join. */
|
|
1282
|
+
const TABLES$5 = [
|
|
1283
|
+
"workspace",
|
|
1284
|
+
"workspace_member",
|
|
1285
|
+
"workspace_invitation"
|
|
1286
|
+
];
|
|
1430
1287
|
async function up$12(database) {
|
|
1431
|
-
await database.schema.createTable("
|
|
1432
|
-
await
|
|
1288
|
+
await database.schema.createTable("workspace").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("name", "text", required).addColumn("slug", "text", (column) => column.notNull().unique()).addColumn("created_by", "text", required).addColumn("owner_id", "text", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).execute();
|
|
1289
|
+
await database.schema.createTable("workspace_member").addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("user_id", "text", required).addColumn("role", "text", required).addColumn("created_at", "timestamptz", now).addPrimaryKeyConstraint("workspace_member_pkey", ["workspace_id", "user_id"]).execute();
|
|
1290
|
+
await withInvitation(database.schema.createTable("workspace_invitation"), "role").addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).execute();
|
|
1291
|
+
await addChecks(database, [[
|
|
1292
|
+
"workspace_member",
|
|
1293
|
+
"role",
|
|
1294
|
+
ROLES
|
|
1295
|
+
], [
|
|
1296
|
+
"workspace_invitation",
|
|
1297
|
+
"role",
|
|
1298
|
+
ROLES
|
|
1299
|
+
]]);
|
|
1300
|
+
await addIndexes(database, [
|
|
1301
|
+
["workspace", ["owner_id"]],
|
|
1302
|
+
["workspace_member", ["user_id"]],
|
|
1303
|
+
["workspace_invitation", ["workspace_id", "revoked_at"]]
|
|
1304
|
+
]);
|
|
1433
1305
|
}
|
|
1434
1306
|
async function down$12(database) {
|
|
1435
|
-
await database
|
|
1307
|
+
await dropTables(database, TABLES$5);
|
|
1436
1308
|
}
|
|
1437
1309
|
//#endregion
|
|
1438
|
-
//#region src/server/db/migrations/
|
|
1439
|
-
var
|
|
1310
|
+
//#region src/server/db/migrations/003_storage.ts
|
|
1311
|
+
var _003_storage_exports = /* @__PURE__ */ __exportAll({
|
|
1440
1312
|
down: () => down$11,
|
|
1441
1313
|
up: () => up$11
|
|
1442
1314
|
});
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
"
|
|
1446
|
-
"
|
|
1447
|
-
"
|
|
1448
|
-
"enrollment-rejected",
|
|
1449
|
-
"enrollment-revoked"
|
|
1315
|
+
/** Files in the object store, a workspace's images among them, and how much each workspace uses. */
|
|
1316
|
+
const TABLES$4 = [
|
|
1317
|
+
"storage_object",
|
|
1318
|
+
"workspace_storage_usage",
|
|
1319
|
+
"workspace_image"
|
|
1450
1320
|
];
|
|
1451
1321
|
async function up$11(database) {
|
|
1452
|
-
await
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
"password-reset",
|
|
1456
|
-
"password-changed"
|
|
1457
|
-
]);
|
|
1322
|
+
await database.schema.createTable("storage_object").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("object_key", "text", (column) => column.notNull().unique()).addColumn("checksum", "text", required).addColumn("byte_size", "bigint", required).addColumn("content_type", "text", required).addColumn("created_at", "timestamptz", now).execute();
|
|
1323
|
+
await database.schema.createTable("workspace_storage_usage").addColumn("workspace_id", "uuid", (column) => column.primaryKey().references("workspace.id").onDelete("cascade")).addColumn("committed_bytes", "bigint", (column) => column.notNull().defaultTo(0)).addColumn("updated_at", "timestamptz", now).execute();
|
|
1324
|
+
await database.schema.createTable("workspace_image").addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("checksum", "text", required).addColumn("storage_object_id", "uuid", (column) => column.notNull().unique().references("storage_object.id")).addColumn("touched_at", "timestamptz", now).addPrimaryKeyConstraint("workspace_image_pkey", ["workspace_id", "checksum"]).execute();
|
|
1458
1325
|
}
|
|
1459
1326
|
async function down$11(database) {
|
|
1460
|
-
await
|
|
1327
|
+
await dropTables(database, TABLES$4);
|
|
1461
1328
|
}
|
|
1462
1329
|
//#endregion
|
|
1463
|
-
//#region src/server/db/migrations/
|
|
1464
|
-
var
|
|
1330
|
+
//#region src/server/db/migrations/004_documents.ts
|
|
1331
|
+
var _004_documents_exports = /* @__PURE__ */ __exportAll({
|
|
1465
1332
|
down: () => down$10,
|
|
1466
1333
|
up: () => up$10
|
|
1467
1334
|
});
|
|
1468
|
-
|
|
1469
|
-
|
|
1335
|
+
/** Documents and what they hold: revisions, versions, images, collaboration rooms, and uploads. */
|
|
1336
|
+
const TABLES$3 = [
|
|
1337
|
+
"document",
|
|
1338
|
+
"document_revision",
|
|
1339
|
+
"document_version",
|
|
1340
|
+
"revision_image",
|
|
1341
|
+
"document_room_snapshot",
|
|
1342
|
+
"document_room_update",
|
|
1343
|
+
"upload",
|
|
1344
|
+
"upload_storage_reservation"
|
|
1345
|
+
];
|
|
1346
|
+
async function createRevisions(database) {
|
|
1347
|
+
await database.schema.createTable("document").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("name", "text", required).addColumn("kind", "text", (column) => column.notNull().defaultTo("design")).addColumn("current_revision_id", "uuid").addColumn("version", "integer", (column) => column.notNull().defaultTo(0)).addColumn("collaboration_epoch", "integer", (column) => column.notNull().defaultTo(0)).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addColumn("deleted_at", "timestamptz").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").addColumn("disabled_at", "timestamptz").addColumn("disabled_reason", "text").execute();
|
|
1348
|
+
await database.schema.createTable("document_revision").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("parent_revision_id", "uuid", (column) => column.references("document_revision.id").onDelete("set null")).addColumn("storage_object_id", "uuid", (column) => column.notNull().references("storage_object.id").onDelete("restrict")).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).execute();
|
|
1349
|
+
await database.schema.alterTable("document").addForeignKeyConstraint("document_current_revision_id_fkey", ["current_revision_id"], "document_revision", ["id"], (constraint) => constraint.onDelete("set null")).execute();
|
|
1350
|
+
await database.schema.createTable("document_version").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("revision_id", "uuid", (column) => column.notNull().unique().references("document_revision.id").onDelete("cascade")).addColumn("kind", "text", required).addColumn("name", "text").addColumn("description", "text").addColumn("restored_from_version_id", "uuid", (column) => column.references("document_version.id").onDelete("set null")).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).execute();
|
|
1351
|
+
await database.schema.createTable("revision_image").addColumn("revision_id", "uuid", (column) => column.notNull().references("document_revision.id").onDelete("cascade")).addColumn("name", "text", required).addColumn("workspace_id", "uuid", required).addColumn("checksum", "text", required).addPrimaryKeyConstraint("revision_image_pkey", ["revision_id", "name"]).addForeignKeyConstraint("revision_image_workspace_id_checksum_fkey", ["workspace_id", "checksum"], "workspace_image", ["workspace_id", "checksum"]).execute();
|
|
1352
|
+
}
|
|
1353
|
+
async function createRooms(database) {
|
|
1354
|
+
await database.schema.createTable("document_room_snapshot").addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("room_epoch", "integer", required).addColumn("snapshot_object_id", "uuid", (column) => column.notNull().references("storage_object.id")).addColumn("through_sequence", "integer", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addPrimaryKeyConstraint("document_room_snapshot_pkey", ["document_id", "room_epoch"]).execute();
|
|
1355
|
+
await database.schema.createTable("document_room_update").addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("room_epoch", "integer", required).addColumn("sequence", "integer", required).addColumn("data", "bytea", required).addColumn("created_at", "timestamptz", now).addPrimaryKeyConstraint("document_room_update_pkey", [
|
|
1356
|
+
"document_id",
|
|
1357
|
+
"room_epoch",
|
|
1358
|
+
"sequence"
|
|
1359
|
+
]).execute();
|
|
1360
|
+
}
|
|
1361
|
+
async function createUploads(database) {
|
|
1362
|
+
await database.schema.createTable("upload").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("kind", "text", (column) => column.notNull().defaultTo("revision")).addColumn("base_revision_id", "uuid", (column) => column.references("document_revision.id").onDelete("set null")).addColumn("object_key", "text", (column) => column.notNull().unique()).addColumn("status", "text", required).addColumn("checksum", "text", required).addColumn("byte_size", "bigint", required).addColumn("content_type", "text", required).addColumn("multipart_upload_id", "text").addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).addColumn("expires_at", "timestamptz", required).addColumn("finalization_started_at", "timestamptz").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").execute();
|
|
1363
|
+
await database.schema.createTable("upload_storage_reservation").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("upload_id", "uuid", (column) => column.notNull().unique().references("upload.id").onDelete("cascade")).addColumn("bytes", "bigint", required).addColumn("created_at", "timestamptz", now).addColumn("expires_at", "timestamptz", required).addColumn("committed_at", "timestamptz").addColumn("released_at", "timestamptz").execute();
|
|
1470
1364
|
}
|
|
1471
1365
|
async function up$10(database) {
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
await database
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1366
|
+
await createRevisions(database);
|
|
1367
|
+
await createRooms(database);
|
|
1368
|
+
await createUploads(database);
|
|
1369
|
+
await addChecks(database, [
|
|
1370
|
+
[
|
|
1371
|
+
"document",
|
|
1372
|
+
"kind",
|
|
1373
|
+
["design", "board"]
|
|
1374
|
+
],
|
|
1375
|
+
[
|
|
1376
|
+
"document_version",
|
|
1377
|
+
"kind",
|
|
1378
|
+
["checkpoint", "named"]
|
|
1379
|
+
],
|
|
1380
|
+
[
|
|
1381
|
+
"upload",
|
|
1382
|
+
"kind",
|
|
1383
|
+
["revision", "image"]
|
|
1384
|
+
],
|
|
1385
|
+
[
|
|
1386
|
+
"upload",
|
|
1387
|
+
"status",
|
|
1388
|
+
[
|
|
1389
|
+
"pending",
|
|
1390
|
+
"finalizing",
|
|
1391
|
+
"cleaning",
|
|
1392
|
+
"committed",
|
|
1393
|
+
"abandoned"
|
|
1394
|
+
]
|
|
1395
|
+
]
|
|
1396
|
+
]);
|
|
1397
|
+
await addIndexes(database, [
|
|
1398
|
+
["document", ["workspace_id", "updated_at"]],
|
|
1399
|
+
["document", ["deleted_at", "cleanup_claimed_at"]],
|
|
1400
|
+
["document_revision", ["document_id", "created_at"]],
|
|
1401
|
+
["document_version", [
|
|
1402
|
+
"document_id",
|
|
1403
|
+
"created_at desc",
|
|
1404
|
+
"id desc"
|
|
1405
|
+
]],
|
|
1406
|
+
["revision_image", ["workspace_id", "checksum"]],
|
|
1407
|
+
["upload", ["status", "expires_at"]]
|
|
1408
|
+
]);
|
|
1485
1409
|
}
|
|
1486
1410
|
async function down$10(database) {
|
|
1487
|
-
await database
|
|
1488
|
-
await database.schema.dropTable("passkey").ifExists().execute();
|
|
1489
|
-
await database.schema.dropTable("two_factor").ifExists().execute();
|
|
1490
|
-
if ((await tableNames(database)).has("user")) await database.schema.alterTable("user").dropColumn("two_factor_enabled").execute();
|
|
1411
|
+
await dropTables(database, TABLES$3);
|
|
1491
1412
|
}
|
|
1492
1413
|
//#endregion
|
|
1493
|
-
//#region src/server/db/migrations/
|
|
1494
|
-
var
|
|
1414
|
+
//#region src/server/db/migrations/005_sharing.ts
|
|
1415
|
+
var _005_sharing_exports = /* @__PURE__ */ __exportAll({
|
|
1495
1416
|
down: () => down$9,
|
|
1496
1417
|
up: () => up$9
|
|
1497
1418
|
});
|
|
1498
|
-
/**
|
|
1499
|
-
const
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1419
|
+
/** Who a document is shared with: people, invitations, links, and the invitations budget. */
|
|
1420
|
+
const TABLES$2 = [
|
|
1421
|
+
"document_grant",
|
|
1422
|
+
"document_invitation",
|
|
1423
|
+
"invitation_continuation",
|
|
1424
|
+
"document_share",
|
|
1425
|
+
"invitation_send"
|
|
1426
|
+
];
|
|
1506
1427
|
async function up$9(database) {
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
await
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1428
|
+
await database.schema.createTable("document_grant").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("user_id", "text", required).addColumn("permission", "text", required).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addColumn("revoked_at", "timestamptz").addUniqueConstraint("document_grant_document_id_user_id_key", ["document_id", "user_id"]).execute();
|
|
1429
|
+
await withInvitation(database.schema.createTable("document_invitation"), "permission").addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).execute();
|
|
1430
|
+
await database.schema.createTable("invitation_continuation").addColumn("id", "text", (column) => column.primaryKey()).addColumn("invitation_id", "uuid", (column) => column.notNull().references("document_invitation.id").onDelete("cascade")).addColumn("token_encrypted", "text", required).addColumn("created_at", "timestamptz", now).addColumn("expires_at", "timestamptz", required).addColumn("consumed_at", "timestamptz").execute();
|
|
1431
|
+
await database.schema.createTable("document_share").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("permission", "text", required).addColumn("secret_hash", "text", required).addColumn("room_epoch", "integer", (column) => column.notNull().defaultTo(0)).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addColumn("expires_at", "timestamptz").addColumn("revoked_at", "timestamptz").addColumn("last_used_at", "timestamptz").execute();
|
|
1432
|
+
await database.schema.createTable("invitation_send").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("sender_id", "text", required).addColumn("email_normalized", "text", required).addColumn("sent_at", "timestamptz", now).execute();
|
|
1433
|
+
await addChecks(database, [
|
|
1434
|
+
[
|
|
1435
|
+
"document_grant",
|
|
1436
|
+
"permission",
|
|
1437
|
+
PERMISSIONS
|
|
1438
|
+
],
|
|
1439
|
+
[
|
|
1440
|
+
"document_invitation",
|
|
1441
|
+
"permission",
|
|
1442
|
+
PERMISSIONS
|
|
1443
|
+
],
|
|
1444
|
+
[
|
|
1445
|
+
"document_share",
|
|
1446
|
+
"permission",
|
|
1447
|
+
PERMISSIONS
|
|
1448
|
+
]
|
|
1449
|
+
]);
|
|
1450
|
+
await addIndexes(database, [
|
|
1451
|
+
["document_grant", ["user_id", "document_id"]],
|
|
1452
|
+
["document_invitation", ["document_id", "revoked_at"]],
|
|
1453
|
+
["document_share", ["document_id", "revoked_at"]],
|
|
1454
|
+
["invitation_continuation", ["expires_at"]],
|
|
1455
|
+
["invitation_send", ["sender_id", "sent_at"]],
|
|
1456
|
+
["invitation_send", ["email_normalized", "sent_at"]]
|
|
1457
|
+
]);
|
|
1516
1458
|
}
|
|
1517
1459
|
async function down$9(database) {
|
|
1518
|
-
await database
|
|
1519
|
-
version: "1.7.2+mfa.1",
|
|
1520
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1521
|
-
}).where("id", "=", "better-auth").where("version", "=", "1.7.7+mfa.1").execute();
|
|
1460
|
+
await dropTables(database, TABLES$2);
|
|
1522
1461
|
}
|
|
1523
1462
|
//#endregion
|
|
1524
|
-
//#region src/server/db/migrations/
|
|
1525
|
-
var
|
|
1463
|
+
//#region src/server/db/migrations/006_operations.ts
|
|
1464
|
+
var _006_operations_exports = /* @__PURE__ */ __exportAll({
|
|
1526
1465
|
down: () => down$8,
|
|
1527
1466
|
up: () => up$8
|
|
1528
1467
|
});
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
"
|
|
1532
|
-
"
|
|
1533
|
-
"
|
|
1534
|
-
"
|
|
1535
|
-
"enrollment-revoked",
|
|
1536
|
-
"email-verification",
|
|
1537
|
-
"password-reset",
|
|
1538
|
-
"password-changed"
|
|
1468
|
+
/** Running the deployment: the email outbox, enrollment, reports, and the administrators' log. */
|
|
1469
|
+
const TABLES$1 = [
|
|
1470
|
+
"transactional_email",
|
|
1471
|
+
"enrollment",
|
|
1472
|
+
"document_report",
|
|
1473
|
+
"admin_audit_event"
|
|
1539
1474
|
];
|
|
1540
1475
|
async function up$8(database) {
|
|
1541
|
-
await
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1476
|
+
await database.schema.createTable("transactional_email").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("idempotency_key", "text", (column) => column.notNull().unique()).addColumn("kind", "text", required).addColumn("recipient_email_normalized", "text", required).addColumn("payload_encrypted", "text").addColumn("status", "text", (column) => column.notNull().defaultTo("pending")).addColumn("attempt_count", "integer", (column) => column.notNull().defaultTo(0)).addColumn("next_attempt_at", "timestamptz", now).addColumn("claim_id", "uuid").addColumn("claimed_at", "timestamptz").addColumn("transport", "text").addColumn("transport_message_id", "text").addColumn("last_error_code", "text").addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addColumn("accepted_at", "timestamptz").execute();
|
|
1477
|
+
await database.schema.createTable("enrollment").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("email_normalized", "text", (column) => column.notNull().unique()).addColumn("name", "text").addColumn("reason", "text").addColumn("status", "text", (column) => column.notNull().defaultTo("pending")).addColumn("request_revision", "integer", (column) => column.notNull().defaultTo(1)).addColumn("requested_at", "timestamptz", now).addColumn("reviewed_at", "timestamptz").addColumn("reviewed_by", "text").addColumn("review_note", "text").addColumn("approved_user_id", "text").execute();
|
|
1478
|
+
await database.schema.createTable("document_report").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("share_id", "uuid").addColumn("reporter_user_id", "text").addColumn("reporter_guest_id", "text").addColumn("reply_email", "text").addColumn("reason", "text", required).addColumn("description", "text", required).addColumn("status", "text", (column) => column.notNull().defaultTo("open")).addColumn("created_at", "timestamptz", now).addColumn("resolved_at", "timestamptz").addColumn("resolved_by", "text").execute();
|
|
1479
|
+
await database.schema.createTable("admin_audit_event").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("actor_user_id", "text", required).addColumn("action", "text", required).addColumn("subject_type", "text", required).addColumn("subject_id", "text", required).addColumn("metadata", "jsonb", (column) => column.notNull().defaultTo(EMPTY_JSON_OBJECT)).addColumn("created_at", "timestamptz", now).execute();
|
|
1480
|
+
await addChecks(database, [
|
|
1481
|
+
[
|
|
1482
|
+
"transactional_email",
|
|
1483
|
+
"kind",
|
|
1484
|
+
[
|
|
1485
|
+
"document-invitation",
|
|
1486
|
+
"workspace-invitation",
|
|
1487
|
+
"enrollment-requested",
|
|
1488
|
+
"admin-enrollment-notification",
|
|
1489
|
+
"enrollment-approved",
|
|
1490
|
+
"enrollment-rejected",
|
|
1491
|
+
"enrollment-revoked",
|
|
1492
|
+
"email-verification",
|
|
1493
|
+
"password-reset",
|
|
1494
|
+
"password-changed",
|
|
1495
|
+
"email-change-requested",
|
|
1496
|
+
"new-sign-in",
|
|
1497
|
+
"two-step-changed",
|
|
1498
|
+
"document-report"
|
|
1499
|
+
]
|
|
1500
|
+
],
|
|
1501
|
+
[
|
|
1502
|
+
"transactional_email",
|
|
1503
|
+
"status",
|
|
1504
|
+
[
|
|
1505
|
+
"pending",
|
|
1506
|
+
"sending",
|
|
1507
|
+
"accepted",
|
|
1508
|
+
"failed",
|
|
1509
|
+
"suppressed"
|
|
1510
|
+
]
|
|
1511
|
+
],
|
|
1512
|
+
[
|
|
1513
|
+
"enrollment",
|
|
1514
|
+
"status",
|
|
1515
|
+
[
|
|
1516
|
+
"pending",
|
|
1517
|
+
"approved",
|
|
1518
|
+
"rejected",
|
|
1519
|
+
"revoked"
|
|
1520
|
+
]
|
|
1521
|
+
],
|
|
1522
|
+
[
|
|
1523
|
+
"document_report",
|
|
1524
|
+
"reason",
|
|
1525
|
+
[
|
|
1526
|
+
"spam-phishing",
|
|
1527
|
+
"illegal",
|
|
1528
|
+
"copyright",
|
|
1529
|
+
"harassment",
|
|
1530
|
+
"other"
|
|
1531
|
+
]
|
|
1532
|
+
],
|
|
1533
|
+
[
|
|
1534
|
+
"document_report",
|
|
1535
|
+
"status",
|
|
1536
|
+
[
|
|
1537
|
+
"open",
|
|
1538
|
+
"dismissed",
|
|
1539
|
+
"actioned"
|
|
1540
|
+
]
|
|
1541
|
+
]
|
|
1542
|
+
]);
|
|
1543
|
+
await addIndexes(database, [
|
|
1544
|
+
["transactional_email", ["status", "next_attempt_at"]],
|
|
1545
|
+
["enrollment", ["status", "requested_at"]],
|
|
1546
|
+
["document_report", ["document_id"]],
|
|
1547
|
+
["document_report", ["share_id", "created_at"]],
|
|
1548
|
+
["document_report", ["status", "created_at"]],
|
|
1549
|
+
["admin_audit_event", ["created_at"]]
|
|
1550
|
+
]);
|
|
1549
1551
|
}
|
|
1550
1552
|
async function down$8(database) {
|
|
1551
|
-
await
|
|
1552
|
-
await database.schema.dropTable("workspace_invitation").execute();
|
|
1553
|
+
await dropTables(database, TABLES$1);
|
|
1553
1554
|
}
|
|
1554
1555
|
//#endregion
|
|
1555
|
-
//#region src/server/db/migrations/
|
|
1556
|
-
var
|
|
1556
|
+
//#region src/server/db/migrations/007_trash.ts
|
|
1557
|
+
var _007_trash_exports = /* @__PURE__ */ __exportAll({
|
|
1557
1558
|
down: () => down$7,
|
|
1558
|
-
keepCurrentRevisions: () => keepCurrentRevisions,
|
|
1559
1559
|
up: () => up$7
|
|
1560
1560
|
});
|
|
1561
1561
|
/**
|
|
1562
|
-
*
|
|
1563
|
-
*
|
|
1562
|
+
* Trash: who deleted a document, and who deleted it forever, when, and the storage that took off
|
|
1563
|
+
* its workspace's usage while its files wait out the grace before cleanup destroys them.
|
|
1564
1564
|
*/
|
|
1565
1565
|
async function up$7(database) {
|
|
1566
|
-
await database.schema.
|
|
1567
|
-
await
|
|
1568
|
-
await database.schema.createIndex("document_version_document_index").on("document_version").columns([
|
|
1569
|
-
"document_id",
|
|
1570
|
-
"created_at desc",
|
|
1571
|
-
"id desc"
|
|
1572
|
-
]).execute();
|
|
1573
|
-
await keepCurrentRevisions(database);
|
|
1566
|
+
await database.schema.alterTable("document").addColumn("deleted_by", "text").addColumn("purged_at", "timestamptz").addColumn("purged_by", "text").addColumn("purged_bytes", "bigint", (column) => column.notNull().defaultTo(0)).execute();
|
|
1567
|
+
await addIndexes(database, [["document", ["workspace_id", "deleted_at desc"]], ["document", ["purged_at"]]]);
|
|
1574
1568
|
}
|
|
1575
1569
|
async function down$7(database) {
|
|
1576
|
-
await database.schema.
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
async function keepCurrentRevisions(database) {
|
|
1580
|
-
await database.insertInto("documentVersion").columns([
|
|
1581
|
-
"id",
|
|
1582
|
-
"documentId",
|
|
1583
|
-
"revisionId",
|
|
1584
|
-
"kind",
|
|
1585
|
-
"createdBy",
|
|
1586
|
-
"createdAt"
|
|
1587
|
-
]).expression((expression) => expression.selectFrom("document").innerJoin("documentRevision", "documentRevision.id", "document.currentRevisionId").select((select) => [
|
|
1588
|
-
select.fn("gen_random_uuid").as("id"),
|
|
1589
|
-
"document.id as documentId",
|
|
1590
|
-
"documentRevision.id as revisionId",
|
|
1591
|
-
select.val("checkpoint").as("kind"),
|
|
1592
|
-
"documentRevision.createdBy",
|
|
1593
|
-
"documentRevision.createdAt"
|
|
1594
|
-
])).execute();
|
|
1570
|
+
await database.schema.dropIndex("document_purged_at_idx").execute();
|
|
1571
|
+
await database.schema.dropIndex("document_workspace_id_deleted_at_idx").execute();
|
|
1572
|
+
await database.schema.alterTable("document").dropColumn("purged_bytes").dropColumn("purged_by").dropColumn("purged_at").dropColumn("deleted_by").execute();
|
|
1595
1573
|
}
|
|
1596
1574
|
//#endregion
|
|
1597
|
-
//#region src/server/db/migrations/
|
|
1598
|
-
var
|
|
1575
|
+
//#region src/server/db/migrations/008_terms.ts
|
|
1576
|
+
var _008_terms_exports = /* @__PURE__ */ __exportAll({
|
|
1599
1577
|
down: () => down$6,
|
|
1600
1578
|
up: () => up$6
|
|
1601
1579
|
});
|
|
1602
|
-
/**
|
|
1603
|
-
* The version an external writer, such as a billing service, attached to a workspace's
|
|
1604
|
-
* entitlements. Writers deliver out of order, so a write older than the stored one is refused.
|
|
1605
|
-
*/
|
|
1580
|
+
/** Which version of the deployment's terms each account accepted, and when. */
|
|
1606
1581
|
async function up$6(database) {
|
|
1607
|
-
await database.schema.
|
|
1582
|
+
await database.schema.createTable("terms_acceptance").addColumn("user_id", "text", required).addColumn("version", "text", required).addColumn("accepted_at", "timestamptz", now).addPrimaryKeyConstraint("terms_acceptance_pkey", ["user_id", "version"]).execute();
|
|
1608
1583
|
}
|
|
1609
1584
|
async function down$6(database) {
|
|
1610
|
-
await database.schema.
|
|
1585
|
+
await database.schema.dropTable("terms_acceptance").execute();
|
|
1611
1586
|
}
|
|
1612
1587
|
//#endregion
|
|
1613
|
-
//#region src/server/db/migrations/
|
|
1614
|
-
var
|
|
1588
|
+
//#region src/server/db/migrations/009_access_tokens.ts
|
|
1589
|
+
var _009_access_tokens_exports = /* @__PURE__ */ __exportAll({
|
|
1615
1590
|
down: () => down$5,
|
|
1616
1591
|
up: () => up$5
|
|
1617
1592
|
});
|
|
1618
1593
|
/**
|
|
1619
|
-
*
|
|
1620
|
-
*
|
|
1621
|
-
*
|
|
1622
|
-
*
|
|
1594
|
+
* Personal access tokens, kept by Better Auth's API-key plugin in the table it would build
|
|
1595
|
+
* itself. Cloud builds it here instead, since Better Auth's tables are built only once and a
|
|
1596
|
+
* deployment that has them gains this one as any other migration; Better Auth's migration then
|
|
1597
|
+
* leaves this plugin's table out (`createBetterAuthAdapter`). It holds each token's SHA-256
|
|
1598
|
+
* digest, never the token.
|
|
1623
1599
|
*/
|
|
1624
1600
|
async function up$5(database) {
|
|
1625
|
-
await database.schema.
|
|
1626
|
-
await
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1601
|
+
await database.schema.createTable("apikey").addColumn("id", "uuid", (column) => column.primaryKey().defaultTo(sql`gen_random_uuid()`)).addColumn("config_id", "text", required).addColumn("name", "text").addColumn("start", "text").addColumn("reference_id", "text", required).addColumn("prefix", "text").addColumn("key", "text", required).addColumn("refill_interval", "integer").addColumn("refill_amount", "integer").addColumn("last_refill_at", "timestamptz").addColumn("enabled", "boolean").addColumn("rate_limit_enabled", "boolean").addColumn("rate_limit_time_window", "integer").addColumn("rate_limit_max", "integer").addColumn("request_count", "integer").addColumn("remaining", "integer").addColumn("last_request", "timestamptz").addColumn("expires_at", "timestamptz").addColumn("created_at", "timestamptz", required).addColumn("updated_at", "timestamptz", required).addColumn("permissions", "text").addColumn("metadata", "text").execute();
|
|
1602
|
+
await addIndexes(database, [
|
|
1603
|
+
["apikey", ["config_id"]],
|
|
1604
|
+
["apikey", ["reference_id"]],
|
|
1605
|
+
["apikey", ["key"]]
|
|
1606
|
+
]);
|
|
1631
1607
|
}
|
|
1632
1608
|
async function down$5(database) {
|
|
1633
|
-
await database.schema.dropTable("
|
|
1634
|
-
await database.schema.dropTable("workspace_image").execute();
|
|
1635
|
-
await database.schema.alterTable("upload").dropConstraint("upload_kind_check").execute();
|
|
1636
|
-
await database.schema.alterTable("upload").dropColumn("kind").execute();
|
|
1609
|
+
await database.schema.dropTable("apikey").execute();
|
|
1637
1610
|
}
|
|
1638
1611
|
//#endregion
|
|
1639
|
-
//#region src/server/db/migrations/
|
|
1640
|
-
var
|
|
1612
|
+
//#region src/server/db/migrations/010_storage_limit_notices.ts
|
|
1613
|
+
var _010_storage_limit_notices_exports = /* @__PURE__ */ __exportAll({
|
|
1641
1614
|
down: () => down$4,
|
|
1642
1615
|
up: () => up$4
|
|
1643
1616
|
});
|
|
1644
|
-
|
|
1617
|
+
/** Every kind of transactional email, as of this migration. */
|
|
1618
|
+
const EMAIL_KINDS$1 = [
|
|
1645
1619
|
"document-invitation",
|
|
1646
1620
|
"workspace-invitation",
|
|
1647
1621
|
"enrollment-requested",
|
|
@@ -1651,302 +1625,203 @@ const emailKinds = [
|
|
|
1651
1625
|
"enrollment-revoked",
|
|
1652
1626
|
"email-verification",
|
|
1653
1627
|
"password-reset",
|
|
1654
|
-
"password-changed"
|
|
1628
|
+
"password-changed",
|
|
1629
|
+
"email-change-requested",
|
|
1630
|
+
"new-sign-in",
|
|
1631
|
+
"two-step-changed",
|
|
1632
|
+
"document-report"
|
|
1655
1633
|
];
|
|
1656
1634
|
/**
|
|
1657
|
-
*
|
|
1658
|
-
*
|
|
1659
|
-
*
|
|
1635
|
+
* The accounts told their storage is full, so each is told once each time it fills, not on every
|
|
1636
|
+
* cleanup pass; a row goes once the account holds less than its plan allows again. Email gains
|
|
1637
|
+
* the kind that tells them, which the previous release never writes.
|
|
1660
1638
|
*/
|
|
1661
1639
|
async function up$4(database) {
|
|
1662
|
-
await database.schema.createTable("
|
|
1663
|
-
await database.schema.
|
|
1664
|
-
await database
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
"
|
|
1668
|
-
|
|
1669
|
-
"two-step-changed"
|
|
1670
|
-
]);
|
|
1640
|
+
await database.schema.createTable("storage_limit_notice").addColumn("user_id", "text", (column) => column.primaryKey()).addColumn("notified_at", "timestamptz", now).execute();
|
|
1641
|
+
await database.schema.alterTable("transactional_email").dropConstraint("transactional_email_kind_check").execute();
|
|
1642
|
+
await addChecks(database, [[
|
|
1643
|
+
"transactional_email",
|
|
1644
|
+
"kind",
|
|
1645
|
+
[...EMAIL_KINDS$1, "storage-limit-reached"]
|
|
1646
|
+
]]);
|
|
1671
1647
|
}
|
|
1672
1648
|
async function down$4(database) {
|
|
1673
|
-
await
|
|
1674
|
-
await database
|
|
1649
|
+
await database.schema.alterTable("transactional_email").dropConstraint("transactional_email_kind_check").execute();
|
|
1650
|
+
await addChecks(database, [[
|
|
1651
|
+
"transactional_email",
|
|
1652
|
+
"kind",
|
|
1653
|
+
EMAIL_KINDS$1
|
|
1654
|
+
]]);
|
|
1655
|
+
await database.schema.dropTable("storage_limit_notice").execute();
|
|
1675
1656
|
}
|
|
1676
1657
|
//#endregion
|
|
1677
|
-
//#region src/server/db/migrations/
|
|
1678
|
-
var
|
|
1658
|
+
//#region src/server/db/migrations/011_account_ids.ts
|
|
1659
|
+
var _011_account_ids_exports = /* @__PURE__ */ __exportAll({
|
|
1660
|
+
contracts: () => contracts,
|
|
1679
1661
|
down: () => down$3,
|
|
1680
1662
|
up: () => up$3
|
|
1681
1663
|
});
|
|
1682
|
-
/**
|
|
1683
|
-
* OpenPencil's tables beside Better Auth's lose the `cloud_` prefix the rest never had; only the
|
|
1684
|
-
* rate limit keeps a qualifier, since Better Auth has a `rate_limit` of its own. Their keys,
|
|
1685
|
-
* checks, and indexes follow, so no name keeps the old prefix.
|
|
1686
|
-
*/
|
|
1664
|
+
/** The tables that named accounts by `uuid`, unlike every other table Cloud builds. */
|
|
1687
1665
|
const TABLES = [
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
["cloud_auth_schema", "auth_schema_version"],
|
|
1692
|
-
["cloud_rate_limit", "api_rate_limit"]
|
|
1693
|
-
];
|
|
1694
|
-
const CONSTRAINTS$1 = [
|
|
1695
|
-
[
|
|
1696
|
-
"enrollment",
|
|
1697
|
-
"cloud_enrollment_pkey",
|
|
1698
|
-
"enrollment_pkey"
|
|
1699
|
-
],
|
|
1700
|
-
[
|
|
1701
|
-
"enrollment",
|
|
1702
|
-
"cloud_enrollment_email_normalized_key",
|
|
1703
|
-
"enrollment_email_normalized_key"
|
|
1704
|
-
],
|
|
1705
|
-
[
|
|
1706
|
-
"enrollment",
|
|
1707
|
-
"cloud_enrollment_status_check",
|
|
1708
|
-
"enrollment_status_check"
|
|
1709
|
-
],
|
|
1710
|
-
[
|
|
1711
|
-
"mfa_assurance",
|
|
1712
|
-
"cloud_mfa_assurance_pkey",
|
|
1713
|
-
"mfa_assurance_pkey"
|
|
1714
|
-
],
|
|
1715
|
-
[
|
|
1716
|
-
"admin_audit_event",
|
|
1717
|
-
"cloud_admin_audit_event_pkey",
|
|
1718
|
-
"admin_audit_event_pkey"
|
|
1719
|
-
],
|
|
1720
|
-
[
|
|
1721
|
-
"auth_schema_version",
|
|
1722
|
-
"cloud_auth_schema_pkey",
|
|
1723
|
-
"auth_schema_version_pkey"
|
|
1724
|
-
],
|
|
1725
|
-
[
|
|
1726
|
-
"api_rate_limit",
|
|
1727
|
-
"cloud_rate_limit_pkey",
|
|
1728
|
-
"api_rate_limit_pkey"
|
|
1729
|
-
]
|
|
1666
|
+
"mfa_assurance",
|
|
1667
|
+
"sign_in",
|
|
1668
|
+
"user_picture"
|
|
1730
1669
|
];
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
const [source, target] = forward ? [from, to] : [to, from];
|
|
1738
|
-
await sql`alter index ${sql.id(source)} rename to ${sql.id(target)}`.execute(database);
|
|
1739
|
-
}
|
|
1740
|
-
if (!forward) for (const [from, to] of TABLES) await database.schema.alterTable(to).renameTo(from).execute();
|
|
1741
|
-
}
|
|
1670
|
+
/**
|
|
1671
|
+
* Names accounts by `text` in the last tables that used `uuid`, as `schema.ts` sets for every
|
|
1672
|
+
* table Cloud builds. The previous release reads and writes the same strings either way, so it
|
|
1673
|
+
* keeps working while this runs.
|
|
1674
|
+
*/
|
|
1675
|
+
const contracts = TABLES.map((table) => `${table}.user_id`);
|
|
1742
1676
|
async function up$3(database) {
|
|
1743
|
-
await
|
|
1677
|
+
for (const table of TABLES) await database.schema.alterTable(table).alterColumn("user_id", (column) => column.setDataType("text")).execute();
|
|
1744
1678
|
}
|
|
1745
1679
|
async function down$3(database) {
|
|
1746
|
-
await
|
|
1680
|
+
for (const table of TABLES) await database.schema.alterTable(table).alterColumn("user_id", (column) => column.setDataType(sql`uuid using user_id::uuid`)).execute();
|
|
1747
1681
|
}
|
|
1748
1682
|
//#endregion
|
|
1749
|
-
//#region src/server/db/migrations/
|
|
1750
|
-
var
|
|
1683
|
+
//#region src/server/db/migrations/012_email_locales.ts
|
|
1684
|
+
var _012_email_locales_exports = /* @__PURE__ */ __exportAll({
|
|
1751
1685
|
down: () => down$2,
|
|
1752
1686
|
up: () => up$2
|
|
1753
1687
|
});
|
|
1688
|
+
/** Whether Better Auth's `user` table exists yet, which it doesn't before a server first starts. */
|
|
1689
|
+
async function hasUserTable(database) {
|
|
1690
|
+
return (await database.introspection.getTables()).some((table) => table.name === "user");
|
|
1691
|
+
}
|
|
1754
1692
|
/**
|
|
1755
|
-
* The
|
|
1756
|
-
*
|
|
1693
|
+
* The language Cloud writes to each account in, and the one each queued email is written in,
|
|
1694
|
+
* settled when it's queued so a retry reads the same. Email from before this migration is
|
|
1695
|
+
* English, as it always was.
|
|
1696
|
+
*
|
|
1697
|
+
* `user.locale` is a Better Auth field, so a new database gets it from Better Auth's own schema,
|
|
1698
|
+
* built after Cloud's migrations. A database that already has that schema never builds it again,
|
|
1699
|
+
* so it gets the column here; accounts from before it have no language and read English.
|
|
1757
1700
|
*/
|
|
1758
1701
|
async function up$2(database) {
|
|
1759
|
-
await database.schema.
|
|
1702
|
+
await database.schema.alterTable("transactional_email").addColumn("locale", "text", (column) => column.notNull().defaultTo("en")).execute();
|
|
1703
|
+
if (await hasUserTable(database)) await database.schema.alterTable("user").addColumn("locale", "text", (column) => column.ifNotExists()).execute();
|
|
1760
1704
|
}
|
|
1761
1705
|
async function down$2(database) {
|
|
1762
|
-
await database.schema.
|
|
1706
|
+
await database.schema.alterTable("transactional_email").dropColumn("locale").execute();
|
|
1763
1707
|
}
|
|
1764
1708
|
//#endregion
|
|
1765
|
-
//#region src/server/db/migrations/
|
|
1766
|
-
var
|
|
1709
|
+
//#region src/server/db/migrations/013_document_block_emails.ts
|
|
1710
|
+
var _013_document_block_emails_exports = /* @__PURE__ */ __exportAll({
|
|
1711
|
+
DOCUMENT_BLOCK_EMAIL_KINDS: () => DOCUMENT_BLOCK_EMAIL_KINDS,
|
|
1767
1712
|
down: () => down$1,
|
|
1768
1713
|
up: () => up$1
|
|
1769
1714
|
});
|
|
1715
|
+
/** Every kind of transactional email before this migration. */
|
|
1716
|
+
const EMAIL_KINDS = [
|
|
1717
|
+
"document-invitation",
|
|
1718
|
+
"workspace-invitation",
|
|
1719
|
+
"enrollment-requested",
|
|
1720
|
+
"admin-enrollment-notification",
|
|
1721
|
+
"enrollment-approved",
|
|
1722
|
+
"enrollment-rejected",
|
|
1723
|
+
"enrollment-revoked",
|
|
1724
|
+
"email-verification",
|
|
1725
|
+
"password-reset",
|
|
1726
|
+
"password-changed",
|
|
1727
|
+
"email-change-requested",
|
|
1728
|
+
"new-sign-in",
|
|
1729
|
+
"two-step-changed",
|
|
1730
|
+
"document-report",
|
|
1731
|
+
"storage-limit-reached"
|
|
1732
|
+
];
|
|
1733
|
+
/** Every kind of transactional email once this migration has run. */
|
|
1734
|
+
const DOCUMENT_BLOCK_EMAIL_KINDS = [
|
|
1735
|
+
...EMAIL_KINDS,
|
|
1736
|
+
"document-blocked",
|
|
1737
|
+
"document-restored"
|
|
1738
|
+
];
|
|
1770
1739
|
/**
|
|
1771
|
-
*
|
|
1772
|
-
*
|
|
1740
|
+
* Email gains the kinds that tell a workspace's owner an administrator blocked one of its
|
|
1741
|
+
* documents, and that it is back, which the previous release never writes.
|
|
1773
1742
|
*/
|
|
1774
1743
|
async function up$1(database) {
|
|
1775
|
-
await database
|
|
1776
|
-
|
|
1744
|
+
await replaceChecks(database, "widen", [[
|
|
1745
|
+
"transactional_email",
|
|
1746
|
+
"kind",
|
|
1747
|
+
DOCUMENT_BLOCK_EMAIL_KINDS
|
|
1748
|
+
]]);
|
|
1777
1749
|
}
|
|
1778
1750
|
async function down$1(database) {
|
|
1779
|
-
await database
|
|
1751
|
+
await replaceChecks(database, "narrow", [[
|
|
1752
|
+
"transactional_email",
|
|
1753
|
+
"kind",
|
|
1754
|
+
EMAIL_KINDS
|
|
1755
|
+
]]);
|
|
1780
1756
|
}
|
|
1781
1757
|
//#endregion
|
|
1782
|
-
//#region src/server/db/migrations/
|
|
1783
|
-
var
|
|
1758
|
+
//#region src/server/db/migrations/014_report_replies.ts
|
|
1759
|
+
var _014_report_replies_exports = /* @__PURE__ */ __exportAll({
|
|
1784
1760
|
down: () => down,
|
|
1785
|
-
startRoomsOver: () => startRoomsOver,
|
|
1786
1761
|
up: () => up
|
|
1787
1762
|
});
|
|
1788
|
-
/** The renamed table's constraints, whose names spelled its old name. */
|
|
1789
|
-
const CONSTRAINTS = [["documentCollaborationStatePrimary", "documentRoomSnapshotPrimary"], ["documentCollaborationStateDocumentIdFkey", "documentRoomSnapshotDocumentIdFkey"]];
|
|
1790
|
-
async function renameConstraints(database, direction) {
|
|
1791
|
-
for (const [old, current] of CONSTRAINTS) {
|
|
1792
|
-
const [from, to] = direction === "up" ? [old, current] : [current, old];
|
|
1793
|
-
await database.schema.alterTable("documentRoomSnapshot").renameConstraint(from, to).execute();
|
|
1794
|
-
}
|
|
1795
|
-
}
|
|
1796
|
-
/** Moves every document with a saved room to a new epoch, and drops the saved rooms. */
|
|
1797
|
-
async function startRoomsOver(database) {
|
|
1798
|
-
await database.updateTable("document").set((expression) => ({ collaborationEpoch: expression("collaborationEpoch", "+", 1) })).where("id", "in", database.selectFrom("documentRoomSnapshot").select("documentId")).execute();
|
|
1799
|
-
await database.deleteFrom("documentRoomSnapshot").execute();
|
|
1800
|
-
}
|
|
1801
1763
|
/**
|
|
1802
|
-
*
|
|
1803
|
-
*
|
|
1804
|
-
*
|
|
1805
|
-
*
|
|
1806
|
-
*
|
|
1807
|
-
* Rooms saved before held their snapshots in the row itself. Those rows go, and each such
|
|
1808
|
-
* document moves to a new collaboration epoch, so its next room starts from its last saved
|
|
1809
|
-
* revision and no tab's local copy of the earlier room joins it.
|
|
1764
|
+
* Whoever reports a document and leaves an address hears that the report arrived and what the
|
|
1765
|
+
* administrators decided. A report keeps the language its reporter's browser asked for, since a
|
|
1766
|
+
* guest has no account to say it, and email gains the two kinds, which the previous release
|
|
1767
|
+
* never writes.
|
|
1810
1768
|
*/
|
|
1811
1769
|
async function up(database) {
|
|
1812
|
-
await database.schema.alterTable("
|
|
1813
|
-
await
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1770
|
+
await database.schema.alterTable("document_report").addColumn("reply_locale", "text").execute();
|
|
1771
|
+
await replaceChecks(database, "widen", [[
|
|
1772
|
+
"transactional_email",
|
|
1773
|
+
"kind",
|
|
1774
|
+
[
|
|
1775
|
+
...DOCUMENT_BLOCK_EMAIL_KINDS,
|
|
1776
|
+
"document-report-received",
|
|
1777
|
+
"document-report-decided"
|
|
1778
|
+
]
|
|
1779
|
+
]]);
|
|
1821
1780
|
}
|
|
1822
|
-
/** Back to snapshots in rows; the rooms saved meanwhile start over, as they did going up. */
|
|
1823
1781
|
async function down(database) {
|
|
1824
|
-
await database
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1782
|
+
await replaceChecks(database, "narrow", [[
|
|
1783
|
+
"transactional_email",
|
|
1784
|
+
"kind",
|
|
1785
|
+
DOCUMENT_BLOCK_EMAIL_KINDS
|
|
1786
|
+
]]);
|
|
1787
|
+
await database.schema.alterTable("document_report").dropColumn("reply_locale").execute();
|
|
1829
1788
|
}
|
|
1830
1789
|
//#endregion
|
|
1831
1790
|
//#region src/server/db/migrate.ts
|
|
1832
1791
|
const migrations = {
|
|
1833
|
-
"
|
|
1834
|
-
"
|
|
1835
|
-
"
|
|
1836
|
-
"
|
|
1837
|
-
"
|
|
1838
|
-
"
|
|
1839
|
-
"
|
|
1840
|
-
"
|
|
1841
|
-
"
|
|
1842
|
-
"
|
|
1843
|
-
"
|
|
1844
|
-
"
|
|
1845
|
-
"
|
|
1846
|
-
"
|
|
1847
|
-
"015_cloud_rate_limit": _015_cloud_rate_limit_exports,
|
|
1848
|
-
"016_auth_schema": _016_auth_schema_exports,
|
|
1849
|
-
"017_authentication_email_kinds": _017_authentication_email_kinds_exports,
|
|
1850
|
-
"018_authentication_mfa": _018_authentication_mfa_exports,
|
|
1851
|
-
"019_better_auth_account_issuer": _019_better_auth_account_issuer_exports,
|
|
1852
|
-
"020_workspace_invitations": _020_workspace_invitations_exports,
|
|
1853
|
-
"021_document_versions": _021_document_versions_exports,
|
|
1854
|
-
"022_entitlement_external_version": _022_entitlement_external_version_exports,
|
|
1855
|
-
"023_document_images": _023_document_images_exports,
|
|
1856
|
-
"024_account_activity": _024_account_activity_exports,
|
|
1857
|
-
"025_table_names": _025_table_names_exports,
|
|
1858
|
-
"026_user_picture": _026_user_picture_exports,
|
|
1859
|
-
"027_document_kind": _027_document_kind_exports,
|
|
1860
|
-
"028_collaboration_updates": _028_collaboration_updates_exports
|
|
1792
|
+
"001_accounts": _001_accounts_exports,
|
|
1793
|
+
"002_workspaces": _002_workspaces_exports,
|
|
1794
|
+
"003_storage": _003_storage_exports,
|
|
1795
|
+
"004_documents": _004_documents_exports,
|
|
1796
|
+
"005_sharing": _005_sharing_exports,
|
|
1797
|
+
"006_operations": _006_operations_exports,
|
|
1798
|
+
"007_trash": _007_trash_exports,
|
|
1799
|
+
"008_terms": _008_terms_exports,
|
|
1800
|
+
"009_access_tokens": _009_access_tokens_exports,
|
|
1801
|
+
"010_storage_limit_notices": _010_storage_limit_notices_exports,
|
|
1802
|
+
"011_account_ids": _011_account_ids_exports,
|
|
1803
|
+
"012_email_locales": _012_email_locales_exports,
|
|
1804
|
+
"013_document_block_emails": _013_document_block_emails_exports,
|
|
1805
|
+
"014_report_replies": _014_report_replies_exports
|
|
1861
1806
|
};
|
|
1807
|
+
/** Cloud's migrations, in order. */
|
|
1862
1808
|
var CloudMigrationProvider = class {
|
|
1863
1809
|
async getMigrations() {
|
|
1864
1810
|
return migrations;
|
|
1865
1811
|
}
|
|
1866
1812
|
};
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
"email_verified",
|
|
1873
|
-
"created_at",
|
|
1874
|
-
"updated_at",
|
|
1875
|
-
"role",
|
|
1876
|
-
"banned",
|
|
1877
|
-
"two_factor_enabled"
|
|
1878
|
-
],
|
|
1879
|
-
session: [
|
|
1880
|
-
"id",
|
|
1881
|
-
"expires_at",
|
|
1882
|
-
"token",
|
|
1883
|
-
"user_id",
|
|
1884
|
-
"impersonated_by"
|
|
1885
|
-
],
|
|
1886
|
-
account: [
|
|
1887
|
-
"id",
|
|
1888
|
-
"account_id",
|
|
1889
|
-
"provider_id",
|
|
1890
|
-
"user_id"
|
|
1891
|
-
],
|
|
1892
|
-
verification: [
|
|
1893
|
-
"id",
|
|
1894
|
-
"identifier",
|
|
1895
|
-
"value",
|
|
1896
|
-
"expires_at"
|
|
1897
|
-
],
|
|
1898
|
-
device_code: [
|
|
1899
|
-
"id",
|
|
1900
|
-
"device_code",
|
|
1901
|
-
"user_code",
|
|
1902
|
-
"status",
|
|
1903
|
-
"client_id"
|
|
1904
|
-
],
|
|
1905
|
-
rate_limit: [
|
|
1906
|
-
"id",
|
|
1907
|
-
"key",
|
|
1908
|
-
"count",
|
|
1909
|
-
"last_request"
|
|
1910
|
-
],
|
|
1911
|
-
two_factor: [
|
|
1912
|
-
"id",
|
|
1913
|
-
"secret",
|
|
1914
|
-
"backup_codes",
|
|
1915
|
-
"user_id",
|
|
1916
|
-
"verified",
|
|
1917
|
-
"failed_verification_count",
|
|
1918
|
-
"locked_until"
|
|
1919
|
-
],
|
|
1920
|
-
passkey: [
|
|
1921
|
-
"id",
|
|
1922
|
-
"public_key",
|
|
1923
|
-
"user_id",
|
|
1924
|
-
"credential_id",
|
|
1925
|
-
"counter",
|
|
1926
|
-
"device_type",
|
|
1927
|
-
"backed_up"
|
|
1928
|
-
]
|
|
1929
|
-
};
|
|
1930
|
-
async function existingAuthSchema(database) {
|
|
1931
|
-
const tables = await database.introspection.getTables();
|
|
1932
|
-
return new Map(tables.filter((table) => Object.hasOwn(REQUIRED_AUTH_SCHEMA, table.name)).map((table) => [table.name, new Set(table.columns.map((column) => column.name))]));
|
|
1933
|
-
}
|
|
1813
|
+
/**
|
|
1814
|
+
* Better Auth's tables come from its own migration API, once, after Cloud's: the version they
|
|
1815
|
+
* were built for is recorded, and a different one stops startup until someone migrates it
|
|
1816
|
+
* deliberately, since Better Auth cannot say what an upgrade between its versions changes.
|
|
1817
|
+
*/
|
|
1934
1818
|
async function migrateAuthSchema(database, migration) {
|
|
1935
1819
|
const recorded = await database.selectFrom("authSchemaVersion").select("version").where("id", "=", "better-auth").executeTakeFirst();
|
|
1936
1820
|
if (recorded) {
|
|
1937
1821
|
if (recorded.version !== migration.schemaVersion) throw new Error(`Better Auth schema ${recorded.version} requires an explicit migration to ${migration.schemaVersion}`);
|
|
1938
1822
|
return;
|
|
1939
1823
|
}
|
|
1940
|
-
|
|
1941
|
-
if (existing.size === 0) await migration.run();
|
|
1942
|
-
else {
|
|
1943
|
-
const missing = Object.entries(REQUIRED_AUTH_SCHEMA).flatMap(([table, requiredColumns]) => {
|
|
1944
|
-
const columns = existing.get(table);
|
|
1945
|
-
if (!columns) return [table];
|
|
1946
|
-
return requiredColumns.filter((column) => !columns.has(column)).map((column) => `${table}.${column}`);
|
|
1947
|
-
});
|
|
1948
|
-
if (missing.length > 0) throw new Error(`Existing Better Auth schema is incomplete: ${missing.join(", ")}`);
|
|
1949
|
-
}
|
|
1824
|
+
await migration.run();
|
|
1950
1825
|
await database.insertInto("authSchemaVersion").values({
|
|
1951
1826
|
id: "better-auth",
|
|
1952
1827
|
version: migration.schemaVersion,
|
|
@@ -1995,6 +1870,6 @@ async function listen(fetch, port, hostname) {
|
|
|
1995
1870
|
};
|
|
1996
1871
|
}
|
|
1997
1872
|
//#endregion
|
|
1998
|
-
export {
|
|
1873
|
+
export { CLOUD_ACCOUNT_PLAN_PATH, CLOUD_API_MAX_BODY_BYTES, CLOUD_BILLING_TOKEN_AUDIENCE, CLOUD_DISCOVERY_PATH, CLOUD_EDITOR_VERSION_HEADER, CLOUD_PROTOCOL_MAX_UPLOAD_BYTES, CLOUD_SERVICES_API_PATHS, CLOUD_SERVICES_JWKS_PATH, CLOUD_SERVICES_TOKEN_AUDIENCE, COLLAB_ACTIONS, CURRENT_TIMESTAMP, CloudConfigError, MAX_COLLAB_MESSAGE_BYTES, RELAY_CLOSE, RELAY_DOCUMENT_PARAM, RELAY_HEARTBEAT, RELAY_PATH, RELAY_SERVER_PEER, SERVICE_ERROR_STATUS, __exportAll, accountParams, cloudEditorVersionSchema, communityThumbnailParams, createRelayClientAssembler, decodeRelayClientFrame, decodeRoomUpdate, defaultRelayURL, deletedPrefixSchema, documentDownloadSchema, documentInvitationParams, documentKindSchema, documentParams, documentPermissionSchema, documentShareParams, documentSummarySchema, documentUserParams, documentVersionParams, encodeSyncReply, httpURLSchema, idParams, invitationContinuationParams, invitationParams, isEditorVersionAtLeast, listen, lockForTransaction, migrateCloudDatabase, migrateToLatest, parseCloudDiscovery, parseCloudServerConfig, parseCommitImageUploads, parseCommitUpload, parseCreateDocument, parseCreateServicesToken, parseCreateUpload, parseRequestImageLinks, parseRequestImageUploads, parseUsageQuery, pictureParams, presenceName, relayServerMessages, requestImageLinksSchema, serviceCatalogSchema, servicesTokenClaimsSchema, shareParams, staticEntitlementsSchema, staticEntitlementsTOMLSchema, uploadParams, validatedJSON, validatedQuery, workspaceInvitationParams, workspaceMemberParams, workspaceParams };
|
|
1999
1874
|
|
|
2000
1875
|
//# sourceMappingURL=listen.mjs.map
|