@open-pencil/cloud 0.17.0-beta.1 → 0.17.0-beta.4
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 +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +236 -46
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -400
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +680 -443
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +9 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +99 -58
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +468 -64
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +575 -738
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +6564 -2934
- 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 +8931 -4198
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5260 -3859
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5686 -3736
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +9 -6
- package/src/admin/api/routes.ts +103 -33
- package/src/admin/audit/service.ts +25 -6
- package/src/admin/documents/service.ts +361 -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 +147 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +36 -21
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/index.ts +7 -7
- package/src/client/portal/account.ts +25 -0
- package/src/client/portal/administration.ts +56 -0
- package/src/client/{ai.ts → services.ts} +44 -27
- package/src/contract/account.ts +58 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/collaboration.ts +6 -1
- package/src/contract/discovery.ts +22 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +2 -2
- package/src/contract/index.ts +63 -37
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/services.ts +233 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/render.ts +26 -0
- package/src/email/schema.ts +11 -1
- package/src/email/templates/brand.ts +1 -1
- package/src/runtime/cloudflare/environment.ts +31 -6
- 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 +176 -0
- package/src/runtime/cloudflare/relay/sockets.ts +112 -0
- package/src/runtime/cloudflare/worker.ts +89 -26
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +20 -4
- package/src/runtime/node/cleanup.ts +16 -3
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.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 +71 -15
- package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
- 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 +32 -0
- package/src/runtime/s3/objects.ts +50 -1
- package/src/runtime/site/server.ts +33 -4
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +302 -0
- package/src/server/account/routes.ts +47 -3
- package/src/server/account/terms.ts +42 -0
- package/src/server/api/router.ts +12 -7
- package/src/server/api/types.ts +9 -0
- package/src/server/app.ts +93 -15
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/adapter.ts +11 -0
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/factory.ts +30 -3
- package/src/server/auth/index.ts +10 -1
- package/src/server/cleanup/configured.ts +12 -5
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/objects.ts +10 -2
- package/src/server/cleanup/revisions.ts +24 -7
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- package/src/server/collaboration/index.ts +3 -0
- package/src/server/collaboration/relay/connection.ts +196 -0
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +279 -195
- package/src/server/collaboration/relay/index.ts +3 -1
- package/src/server/collaboration/relay/log.ts +79 -0
- package/src/server/collaboration/relay/persistence.ts +147 -22
- package/src/server/collaboration/relay/resume.ts +28 -0
- package/src/server/collaboration/routes.ts +27 -16
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +35 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +104 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -114
- 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/schema.ts +71 -0
- package/src/server/db/tables.ts +70 -7
- 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/limits.ts +9 -9
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +132 -80
- package/src/server/documents/service.ts +18 -4
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/routes.ts +36 -25
- package/src/server/documents/versions/service.ts +22 -4
- package/src/server/errors/index.ts +99 -0
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +56 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -15
- 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/logging.ts +76 -0
- package/src/server/objects/types.ts +12 -0
- package/src/server/pictures/routes.ts +4 -2
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/database.ts +39 -25
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +1 -0
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +12 -9
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +69 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/index.ts +4 -1
- package/src/server/rate-limit/middleware.ts +100 -12
- package/src/server/reports/index.ts +8 -0
- package/src/server/reports/routes.ts +37 -0
- package/src/server/reports/service.ts +123 -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 -22
- package/src/server/sharing/document-routes.ts +30 -22
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +21 -11
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +108 -85
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +59 -2
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +89 -38
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +67 -45
- package/src/{ai-relay → services}/app.ts +48 -44
- package/src/{ai-relay → services}/config.ts +25 -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 +43 -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/contract/ai.ts +0 -150
- package/src/runtime/cloudflare/relay.ts +0 -136
- 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/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/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
package/dist/listen.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import * as v from "valibot";
|
|
2
|
-
import { ValiError } from "valibot";
|
|
3
2
|
import { Inflate, deflateSync } from "fflate";
|
|
4
3
|
import * as decoding from "lib0/decoding";
|
|
5
4
|
import * as encoding from "lib0/encoding";
|
|
@@ -20,48 +19,55 @@ var __exportAll = (all, no_symbols) => {
|
|
|
20
19
|
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
21
20
|
return target;
|
|
22
21
|
};
|
|
23
|
-
/** The `aud` of every token Cloud signs for
|
|
24
|
-
const
|
|
25
|
-
/** Where Cloud publishes the public keys
|
|
26
|
-
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";
|
|
27
26
|
/**
|
|
28
27
|
* Where a service answers, below the discovered `endpoint`. Model requests follow the OpenAI chat
|
|
29
28
|
* completions API, so an OpenAI-compatible client takes `${endpoint}${base}` as its base URL.
|
|
30
29
|
*/
|
|
31
|
-
const
|
|
30
|
+
const CLOUD_SERVICES_API_PATHS = {
|
|
32
31
|
base: "/v1",
|
|
33
32
|
chatCompletions: "/v1/chat/completions",
|
|
34
33
|
catalog: "/v1/catalog",
|
|
35
34
|
wallet: "/v1/wallet",
|
|
36
|
-
usage: "/v1/usage"
|
|
35
|
+
usage: "/v1/usage",
|
|
36
|
+
search: "/v1/search"
|
|
37
37
|
};
|
|
38
38
|
const urlSchema$1 = v.pipe(v.string(), v.url());
|
|
39
39
|
const creditsSchema = v.pipe(v.number(), v.integer(), v.minValue(0));
|
|
40
|
-
const
|
|
40
|
+
const cloudServicesDiscoverySchema = v.object({
|
|
41
41
|
endpoint: urlSchema$1,
|
|
42
42
|
contractVersion: v.literal("1")
|
|
43
43
|
});
|
|
44
44
|
const cloudBillingDiscoverySchema = v.object({ portalURL: urlSchema$1 });
|
|
45
|
-
const
|
|
46
|
-
function
|
|
47
|
-
return v.parse(
|
|
45
|
+
const createServicesTokenSchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) });
|
|
46
|
+
function parseCreateServicesToken(input) {
|
|
47
|
+
return v.parse(createServicesTokenSchema, input);
|
|
48
48
|
}
|
|
49
49
|
v.object({
|
|
50
50
|
token: v.pipe(v.string(), v.minLength(1)),
|
|
51
51
|
expiresAt: v.pipe(v.string(), v.isoTimestamp())
|
|
52
52
|
});
|
|
53
|
-
/** What
|
|
54
|
-
const
|
|
53
|
+
/** What a service may rely on once a token's signature, issuer, audience, and expiry check. */
|
|
54
|
+
const servicesTokenClaimsSchema = v.object({
|
|
55
55
|
iss: urlSchema$1,
|
|
56
|
-
aud: v.literal(
|
|
56
|
+
aud: v.literal(CLOUD_SERVICES_TOKEN_AUDIENCE),
|
|
57
57
|
sub: v.pipe(v.string(), v.minLength(1)),
|
|
58
58
|
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
59
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* The account whose credits the request spends: the workspace's owner, whose allowance every
|
|
61
|
+
* workspace they own shares. Absent from tokens a Cloud from before account plans issued, which
|
|
62
|
+
* a relay takes as the workspace's own allowance, as such a Cloud meant.
|
|
63
|
+
*/
|
|
64
|
+
accountId: v.optional(v.pipe(v.string(), v.minLength(1))),
|
|
65
|
+
/** Credits the owner's plan grants each month; `null` when Cloud sets no allowance. */
|
|
60
66
|
monthlyCreditAllowance: v.nullable(creditsSchema),
|
|
61
67
|
iat: v.pipe(v.number(), v.integer()),
|
|
62
68
|
exp: v.pipe(v.number(), v.integer())
|
|
63
69
|
});
|
|
64
|
-
const
|
|
70
|
+
const chatModelSchema = v.object({
|
|
65
71
|
id: v.pipe(v.string(), v.minLength(1)),
|
|
66
72
|
label: v.pipe(v.string(), v.minLength(1)),
|
|
67
73
|
/** Credits per million input and output tokens. */
|
|
@@ -74,11 +80,14 @@ const aiModelSchema = v.object({
|
|
|
74
80
|
tools: v.boolean()
|
|
75
81
|
})
|
|
76
82
|
});
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
83
|
+
/** What one web search costs; a service without search leaves it out of its catalog. */
|
|
84
|
+
const searchPricingSchema = v.object({ creditsPerRequest: creditsSchema });
|
|
85
|
+
const serviceCatalogSchema = v.object({
|
|
86
|
+
models: v.array(chatModelSchema),
|
|
87
|
+
defaultModelID: v.pipe(v.string(), v.minLength(1)),
|
|
88
|
+
search: v.optional(searchPricingSchema)
|
|
80
89
|
});
|
|
81
|
-
const
|
|
90
|
+
const creditBucketSchema = v.object({
|
|
82
91
|
kind: v.picklist([
|
|
83
92
|
"allowance",
|
|
84
93
|
"purchased",
|
|
@@ -91,9 +100,9 @@ v.object({
|
|
|
91
100
|
/** Credits left to spend; `null` when the service sets no limit. */
|
|
92
101
|
balance: v.nullable(creditsSchema),
|
|
93
102
|
/** Spent first to last. */
|
|
94
|
-
buckets: v.array(
|
|
103
|
+
buckets: v.array(creditBucketSchema)
|
|
95
104
|
});
|
|
96
|
-
const
|
|
105
|
+
const usageEntrySchema = v.object({
|
|
97
106
|
id: v.pipe(v.string(), v.minLength(1)),
|
|
98
107
|
createdAt: v.pipe(v.string(), v.isoTimestamp()),
|
|
99
108
|
kind: v.picklist([
|
|
@@ -105,31 +114,49 @@ const aiUsageEntrySchema = v.object({
|
|
|
105
114
|
userId: v.pipe(v.string(), v.minLength(1)),
|
|
106
115
|
credits: creditsSchema
|
|
107
116
|
});
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
117
|
+
v.object({
|
|
118
|
+
query: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(1e3)),
|
|
119
|
+
numResults: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(10)), 5)
|
|
120
|
+
});
|
|
121
|
+
/** One page a search found, with the part of its text that answers the query. */
|
|
122
|
+
const searchResultSchema = v.object({
|
|
123
|
+
url: v.pipe(urlSchema$1, v.regex(/^https?:\/\//i, "A search result links to an HTTP(S) page")),
|
|
124
|
+
title: v.string(),
|
|
125
|
+
publishedAt: v.nullable(v.pipe(v.string(), v.isoTimestamp())),
|
|
126
|
+
author: v.nullable(v.string()),
|
|
127
|
+
text: v.string()
|
|
128
|
+
});
|
|
129
|
+
v.object({ results: v.array(searchResultSchema) });
|
|
130
|
+
const usageQuerySchema = v.object({ cursor: v.optional(v.pipe(v.string(), v.minLength(1))) });
|
|
131
|
+
function parseUsageQuery(input) {
|
|
132
|
+
return v.parse(usageQuerySchema, input);
|
|
111
133
|
}
|
|
112
134
|
v.object({
|
|
113
|
-
entries: v.array(
|
|
135
|
+
entries: v.array(usageEntrySchema),
|
|
114
136
|
next: v.nullable(v.string())
|
|
115
137
|
});
|
|
116
|
-
/** Errors
|
|
117
|
-
const
|
|
138
|
+
/** Errors a service answers with, beside the upstream provider's own. */
|
|
139
|
+
const serviceErrorCodeSchema = v.picklist([
|
|
140
|
+
"invalid_request",
|
|
118
141
|
"unauthorized",
|
|
119
142
|
"insufficient_credits",
|
|
120
143
|
"model_not_allowed",
|
|
144
|
+
"capability_unavailable",
|
|
121
145
|
"rate_limited"
|
|
122
146
|
]);
|
|
123
147
|
v.object({ error: v.object({
|
|
124
|
-
code:
|
|
148
|
+
code: serviceErrorCodeSchema,
|
|
125
149
|
message: v.optional(v.string()),
|
|
126
150
|
/** With `insufficient_credits`: when the workspace's allowance renews, if the service knows. */
|
|
127
151
|
resetsAt: v.optional(v.pipe(v.string(), v.isoTimestamp()))
|
|
128
152
|
}) });
|
|
129
|
-
const
|
|
153
|
+
const SERVICE_ERROR_STATUS = {
|
|
154
|
+
invalid_request: 400,
|
|
130
155
|
unauthorized: 401,
|
|
131
156
|
insufficient_credits: 402,
|
|
132
157
|
model_not_allowed: 403,
|
|
158
|
+
/** A capability, such as search, the service does not offer. */
|
|
159
|
+
capability_unavailable: 404,
|
|
133
160
|
rate_limited: 429
|
|
134
161
|
};
|
|
135
162
|
//#endregion
|
|
@@ -138,6 +165,13 @@ const CLOUD_PROTOCOL_MAX_UPLOAD_BYTES = 1073741824;
|
|
|
138
165
|
const checksumSchema = v.pipe(v.string(), v.regex(/^[A-Za-z0-9+/]{43}=$/));
|
|
139
166
|
/** The most images one revision, or one request for image uploads, may name. */
|
|
140
167
|
const CLOUD_MAX_DOCUMENT_IMAGES = 1e4;
|
|
168
|
+
/**
|
|
169
|
+
* The most a Cloud API request body may hold; a server refuses anything past it before reading
|
|
170
|
+
* it. Files go to the object store, never through the API, so only lists grow large: a save's
|
|
171
|
+
* images fit with room to spare, and a client commits image uploads, whose part lists can run
|
|
172
|
+
* long, in batches under half of it.
|
|
173
|
+
*/
|
|
174
|
+
const CLOUD_API_MAX_BODY_BYTES = 8388608;
|
|
141
175
|
const imageNameSchema = v.pipe(v.string(), v.regex(/^[A-Za-z0-9._-]{1,128}$/));
|
|
142
176
|
const objectDownloadSchema = v.object({
|
|
143
177
|
url: v.pipe(v.string(), v.url()),
|
|
@@ -190,6 +224,22 @@ v.object({
|
|
|
190
224
|
sharedBy: v.nullable(v.object({ name: v.string() })),
|
|
191
225
|
sharedAt: v.string()
|
|
192
226
|
});
|
|
227
|
+
/**
|
|
228
|
+
* A document in Trash the person may restore: who deleted it and when, and when it goes for good.
|
|
229
|
+
* `canPurge` says whether they may also delete it for good before then.
|
|
230
|
+
*/
|
|
231
|
+
const trashedDocumentSchema = v.object({
|
|
232
|
+
id: v.pipe(v.string(), v.uuid()),
|
|
233
|
+
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
234
|
+
workspaceName: v.string(),
|
|
235
|
+
name: v.string(),
|
|
236
|
+
kind: v.optional(documentKindSchema, "design"),
|
|
237
|
+
deletedAt: v.string(),
|
|
238
|
+
deletedBy: v.nullable(v.object({ name: v.string() })),
|
|
239
|
+
purgeAt: v.string(),
|
|
240
|
+
canPurge: v.boolean()
|
|
241
|
+
});
|
|
242
|
+
v.object({ documents: v.array(trashedDocumentSchema) });
|
|
193
243
|
const documentDownloadSchema = v.object({
|
|
194
244
|
document: documentSummarySchema,
|
|
195
245
|
revisionId: v.pipe(v.string(), v.uuid()),
|
|
@@ -388,6 +438,14 @@ const RELAY_CLOSE = {
|
|
|
388
438
|
/** The document was restored to a version, so its room started over under a new ID. */
|
|
389
439
|
restored: 4410
|
|
390
440
|
};
|
|
441
|
+
/**
|
|
442
|
+
* The text a peer sends to learn whether the relay still hears it, and the relay's answer. Relay
|
|
443
|
+
* frames are binary, so neither is taken for one, and a Durable Object answers without waking.
|
|
444
|
+
*/
|
|
445
|
+
const RELAY_HEARTBEAT = {
|
|
446
|
+
ping: "ping",
|
|
447
|
+
pong: "pong"
|
|
448
|
+
};
|
|
391
449
|
const CLIENT_AUTH = 0;
|
|
392
450
|
const CLIENT_MESSAGE = 1;
|
|
393
451
|
const CLIENT_FRAGMENT = 2;
|
|
@@ -600,6 +658,20 @@ const cloudEditorVersionSchema = v.pipe(v.string(), v.regex(/^\d+\.\d+\.\d+$/));
|
|
|
600
658
|
* and an editor older than the minimum keeps them hidden.
|
|
601
659
|
*/
|
|
602
660
|
const cloudListingSchema = v.object({ minimumEditorVersion: cloudEditorVersionSchema });
|
|
661
|
+
const legalURLSchema = v.pipe(v.string(), v.url());
|
|
662
|
+
/**
|
|
663
|
+
* The operator's legal links, which clients show at sign-up and in account pages. A server that
|
|
664
|
+
* names none shows none; `requireAcceptance` means an account accepts `termsVersion` before it uses
|
|
665
|
+
* the server.
|
|
666
|
+
*/
|
|
667
|
+
const cloudLegalDiscoverySchema = v.object({
|
|
668
|
+
termsURL: v.optional(legalURLSchema),
|
|
669
|
+
privacyURL: v.optional(legalURLSchema),
|
|
670
|
+
termsVersion: v.optional(v.string()),
|
|
671
|
+
requireAcceptance: v.boolean(),
|
|
672
|
+
abuseEmail: v.optional(v.pipe(v.string(), v.email())),
|
|
673
|
+
minimumAge: v.optional(v.pipe(v.number(), v.integer()))
|
|
674
|
+
});
|
|
603
675
|
const cloudDiscoverySchema = v.object({
|
|
604
676
|
protocolVersion: v.literal("1"),
|
|
605
677
|
deployment: cloudDeploymentSchema,
|
|
@@ -610,10 +682,11 @@ const cloudDiscoverySchema = v.object({
|
|
|
610
682
|
authentication: cloudAuthenticationSchema,
|
|
611
683
|
capabilities: cloudCapabilitiesSchema,
|
|
612
684
|
listing: v.optional(cloudListingSchema),
|
|
613
|
-
/** The
|
|
614
|
-
|
|
685
|
+
/** The services editors reach with Cloud-signed tokens; absent when the deployment has none. */
|
|
686
|
+
services: v.optional(cloudServicesDiscoverySchema),
|
|
615
687
|
/** Where people manage their plan and buy credits; absent when the deployment does not bill. */
|
|
616
|
-
billing: v.optional(cloudBillingDiscoverySchema)
|
|
688
|
+
billing: v.optional(cloudBillingDiscoverySchema),
|
|
689
|
+
legal: v.optional(cloudLegalDiscoverySchema)
|
|
617
690
|
});
|
|
618
691
|
function parseCloudDiscovery(input) {
|
|
619
692
|
return v.parse(cloudDiscoverySchema, input);
|
|
@@ -625,7 +698,7 @@ function parseOrReject(parse, value, context) {
|
|
|
625
698
|
try {
|
|
626
699
|
return parse(value);
|
|
627
700
|
} catch (error) {
|
|
628
|
-
if (error instanceof ValiError) return context.json({ error: { code: "invalid_request" } }, 400);
|
|
701
|
+
if (error instanceof v.ValiError) return context.json({ error: { code: "invalid_request" } }, 400);
|
|
629
702
|
throw error;
|
|
630
703
|
}
|
|
631
704
|
}
|
|
@@ -636,6 +709,62 @@ function validatedJSON(parse) {
|
|
|
636
709
|
function validatedQuery(parse) {
|
|
637
710
|
return validator("query", (value, context) => parseOrReject(parse, value, context));
|
|
638
711
|
}
|
|
712
|
+
/**
|
|
713
|
+
* A route parameter that names a row by UUID, in lowercase: the database matches any case, but an
|
|
714
|
+
* ID that also names something else, such as a document's relay, must have one spelling.
|
|
715
|
+
*/
|
|
716
|
+
const uuidParamSchema = v.pipe(v.string(), v.uuid(), v.toLowerCase());
|
|
717
|
+
/**
|
|
718
|
+
* Route parameters through `schema`; handlers read them from `context.req.valid('param')`. A
|
|
719
|
+
* parameter that fails it names nothing, so the route answers 404 as for a missing row, and the
|
|
720
|
+
* value never reaches the database.
|
|
721
|
+
*/
|
|
722
|
+
function validatedParams(schema) {
|
|
723
|
+
return validator("param", (value, context) => {
|
|
724
|
+
const parsed = v.safeParse(schema, value);
|
|
725
|
+
return parsed.success ? parsed.output : context.json({ error: { code: "not_found" } }, 404);
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
/** A route parameter the server takes as text, such as a user ID, which is not a UUID column. */
|
|
729
|
+
const textParamSchema = v.pipe(v.string(), v.minLength(1));
|
|
730
|
+
const documentParams = validatedParams(v.object({ documentId: uuidParamSchema }));
|
|
731
|
+
const documentVersionParams = validatedParams(v.object({
|
|
732
|
+
documentId: uuidParamSchema,
|
|
733
|
+
versionId: uuidParamSchema
|
|
734
|
+
}));
|
|
735
|
+
const documentShareParams = validatedParams(v.object({
|
|
736
|
+
documentId: uuidParamSchema,
|
|
737
|
+
shareId: uuidParamSchema
|
|
738
|
+
}));
|
|
739
|
+
const documentInvitationParams = validatedParams(v.object({
|
|
740
|
+
documentId: uuidParamSchema,
|
|
741
|
+
invitationId: uuidParamSchema
|
|
742
|
+
}));
|
|
743
|
+
const documentUserParams = validatedParams(v.object({
|
|
744
|
+
documentId: uuidParamSchema,
|
|
745
|
+
userId: textParamSchema
|
|
746
|
+
}));
|
|
747
|
+
const workspaceParams = validatedParams(v.object({ workspaceId: uuidParamSchema }));
|
|
748
|
+
const workspaceInvitationParams = validatedParams(v.object({
|
|
749
|
+
workspaceId: uuidParamSchema,
|
|
750
|
+
invitationId: uuidParamSchema
|
|
751
|
+
}));
|
|
752
|
+
const workspaceMemberParams = validatedParams(v.object({
|
|
753
|
+
workspaceId: uuidParamSchema,
|
|
754
|
+
userId: textParamSchema
|
|
755
|
+
}));
|
|
756
|
+
const accountParams = validatedParams(v.object({ userId: uuidParamSchema }));
|
|
757
|
+
const shareParams = validatedParams(v.object({ shareId: uuidParamSchema }));
|
|
758
|
+
const invitationParams = validatedParams(v.object({ invitationId: uuidParamSchema }));
|
|
759
|
+
const invitationContinuationParams = validatedParams(v.object({ continuationId: textParamSchema }));
|
|
760
|
+
const uploadParams = validatedParams(v.object({ uploadId: uuidParamSchema }));
|
|
761
|
+
/** Admin records (enrollments, accounts, and messages), all named by UUID. */
|
|
762
|
+
const idParams = validatedParams(v.object({ id: uuidParamSchema }));
|
|
763
|
+
/** A picture's address names its owner and a prefix of its checksum, which the service checks. */
|
|
764
|
+
const pictureParams = validatedParams(v.object({
|
|
765
|
+
userId: textParamSchema,
|
|
766
|
+
version: textParamSchema
|
|
767
|
+
}));
|
|
639
768
|
//#endregion
|
|
640
769
|
//#region src/server/policy/schema.ts
|
|
641
770
|
const positiveIntegerSchema$1 = v.pipe(v.number(), v.integer(), v.minValue(1));
|
|
@@ -656,7 +785,7 @@ const staticEntitlementsSchema = v.object({
|
|
|
656
785
|
enabled: v.optional(v.boolean()),
|
|
657
786
|
maximumParticipants: v.optional(positiveIntegerSchema$1)
|
|
658
787
|
}), {}),
|
|
659
|
-
|
|
788
|
+
services: v.optional(v.object({
|
|
660
789
|
enabled: v.optional(v.boolean()),
|
|
661
790
|
monthlyCreditAllowance: v.optional(nonNegativeIntegerSchema)
|
|
662
791
|
}), {})
|
|
@@ -677,7 +806,7 @@ const staticEntitlementsTOMLSchema = v.strictObject({
|
|
|
677
806
|
enabled: v.optional(v.boolean()),
|
|
678
807
|
maximum_participants: v.optional(positiveIntegerSchema$1)
|
|
679
808
|
})),
|
|
680
|
-
|
|
809
|
+
services: v.optional(v.strictObject({
|
|
681
810
|
enabled: v.optional(v.boolean()),
|
|
682
811
|
monthly_credit_allowance: v.optional(nonNegativeIntegerSchema)
|
|
683
812
|
}))
|
|
@@ -697,6 +826,11 @@ const websocketURLSchema = v.pipe(v.string(), v.url(), v.check((value) => {
|
|
|
697
826
|
return protocol === "ws:" || protocol === "wss:";
|
|
698
827
|
}, "URL must use WS or WSS"));
|
|
699
828
|
const optionalTextSchema = v.optional(v.pipe(v.string(), v.trim(), v.minLength(1)));
|
|
829
|
+
/**
|
|
830
|
+
* A prefix deleted files move under: one lowercase path segment and a slash, which no object key
|
|
831
|
+
* the server makes starts with (they start with `documents/`).
|
|
832
|
+
*/
|
|
833
|
+
const deletedPrefixSchema = v.pipe(v.string(), v.regex(/^[a-z0-9][a-z0-9-]*\/$/), v.check((prefix) => prefix !== "documents/", "deleted_prefix cannot be documents/"));
|
|
700
834
|
const rawCloudServerConfigSchema = v.object({
|
|
701
835
|
deployment: v.picklist(["official", "self-hosted"]),
|
|
702
836
|
publicURL: httpURLSchema,
|
|
@@ -760,6 +894,8 @@ const rawCloudServerConfigSchema = v.object({
|
|
|
760
894
|
s3ChecksumVerification: v.optional(v.picklist(["native", "metadata"]), "native"),
|
|
761
895
|
s3ServerSideEncryption: v.optional(v.picklist(["AES256", "aws:kms"])),
|
|
762
896
|
s3KmsKeyId: optionalTextSchema,
|
|
897
|
+
/** Where a deleted file is kept until the bucket's lifecycle rule expires it; see `deleted_prefix`. */
|
|
898
|
+
s3DeletedPrefix: v.optional(deletedPrefixSchema),
|
|
763
899
|
smtpHost: optionalTextSchema,
|
|
764
900
|
smtpPort: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(65535))),
|
|
765
901
|
smtpSecure: v.optional(v.boolean()),
|
|
@@ -780,14 +916,38 @@ const rawCloudServerConfigSchema = v.object({
|
|
|
780
916
|
cleanupIntervalMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1e3)), 6e4),
|
|
781
917
|
cleanupLeaseDurationMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1e3)), 3e5),
|
|
782
918
|
documentRetentionMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 2592e6),
|
|
783
|
-
/**
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
919
|
+
/**
|
|
920
|
+
* How long a document deleted forever keeps its files and rows, out of everyone's sight, so a
|
|
921
|
+
* server administrator can still bring it back; 0 destroys it at once.
|
|
922
|
+
*/
|
|
923
|
+
documentPurgeGraceMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 6048e5),
|
|
924
|
+
/** The OpenPencil services editors reach with tokens this server signs; see `src/contract/services.ts`. */
|
|
925
|
+
servicesEndpoint: v.optional(httpURLSchema),
|
|
926
|
+
/** The PKCS #8 PEM of the P-256 key that signs service tokens. */
|
|
927
|
+
servicesSigningKey: optionalTextSchema,
|
|
928
|
+
servicesTokenLifetimeSeconds: v.optional(v.pipe(v.number(), v.integer(), v.minValue(60), v.maxValue(3600)), 600),
|
|
788
929
|
billingPortalURL: v.optional(httpURLSchema),
|
|
789
930
|
/** The bearer token services such as billing present to the internal API. */
|
|
790
|
-
internalServiceToken: v.optional(v.pipe(v.string(), v.minLength(32)))
|
|
931
|
+
internalServiceToken: v.optional(v.pipe(v.string(), v.minLength(32))),
|
|
932
|
+
/**
|
|
933
|
+
* The operator's own terms, privacy policy, abuse contact, and minimum age; without it the
|
|
934
|
+
* server shows none. With `requireAcceptance`, an account uses the server only once it accepted
|
|
935
|
+
* `termsVersion`.
|
|
936
|
+
*/
|
|
937
|
+
legal: v.optional(v.object({
|
|
938
|
+
termsURL: v.optional(httpURLSchema),
|
|
939
|
+
privacyURL: v.optional(httpURLSchema),
|
|
940
|
+
termsVersion: optionalTextSchema,
|
|
941
|
+
requireAcceptance: v.boolean(),
|
|
942
|
+
abuseEmail: v.optional(v.pipe(v.string(), v.email())),
|
|
943
|
+
minimumAge: v.optional(v.pipe(v.number(), v.integer(), v.minValue(13), v.maxValue(21)))
|
|
944
|
+
})),
|
|
945
|
+
/** Where server errors are reported; without it the server reports nothing. */
|
|
946
|
+
errorTracking: v.optional(v.object({
|
|
947
|
+
provider: v.literal("posthog"),
|
|
948
|
+
host: httpURLSchema,
|
|
949
|
+
projectAPIKey: v.pipe(v.string(), v.trim(), v.minLength(1))
|
|
950
|
+
}))
|
|
791
951
|
});
|
|
792
952
|
var CloudConfigError = class extends Error {
|
|
793
953
|
constructor(message) {
|
|
@@ -803,6 +963,7 @@ function validateAuthenticationConfig(config) {
|
|
|
803
963
|
if (config.emailPasswordMinimumLength > config.emailPasswordMaximumLength) throw new CloudConfigError("Minimum password length cannot exceed maximum password length");
|
|
804
964
|
if (config.emailPasswordEnabled && config.emailTransport === "none") throw new CloudConfigError("Email and password authentication requires transactional email delivery");
|
|
805
965
|
if (config.deploymentAdminMFARequired && !config.totpEnabled && !config.passkeysEnabled) throw new CloudConfigError("Deployment administrator MFA requires TOTP or passkeys");
|
|
966
|
+
if (config.deployment === "official" && !config.deploymentAdminMFARequired) throw new CloudConfigError("An official deployment requires MFA for deployment administrators");
|
|
806
967
|
if (config.recoveryCodesEnabled && !config.totpEnabled) throw new CloudConfigError("MFA recovery codes require TOTP");
|
|
807
968
|
requireTogether(config, "CAPTCHA", [
|
|
808
969
|
"captchaProvider",
|
|
@@ -827,9 +988,13 @@ function validateEmailConfig(config) {
|
|
|
827
988
|
if (config.deployment === "official" && config.smtpHost && config.smtpSecure === false) throw new CloudConfigError("Official SMTP delivery must use a secure connection");
|
|
828
989
|
}
|
|
829
990
|
function validateServiceConfig(config) {
|
|
830
|
-
requireTogether(config, "
|
|
991
|
+
requireTogether(config, "Services", ["servicesEndpoint", "servicesSigningKey"]);
|
|
831
992
|
if (config.internalServiceToken && config.staticEntitlements) throw new CloudConfigError("The internal API writes database entitlements, which static entitlements would hide");
|
|
832
|
-
if (config.deployment === "official" && config.
|
|
993
|
+
if (config.deployment === "official" && config.servicesEndpoint && new URL(config.servicesEndpoint).protocol !== "https:") throw new CloudConfigError("Official services endpoint must use HTTPS");
|
|
994
|
+
}
|
|
995
|
+
function validateLegalConfig(config) {
|
|
996
|
+
const legal = config.legal;
|
|
997
|
+
if (legal?.requireAcceptance && !(legal.termsURL && legal.termsVersion)) throw new CloudConfigError("[legal] require_acceptance needs terms_url and terms_version");
|
|
833
998
|
}
|
|
834
999
|
function validateObjectStorageConfig(config) {
|
|
835
1000
|
if (config.s3KmsKeyId && config.s3ServerSideEncryption !== "aws:kms") throw new CloudConfigError("S3 KMS key ID requires aws:kms server-side encryption");
|
|
@@ -844,8 +1009,11 @@ function parseCloudServerConfig(input) {
|
|
|
844
1009
|
validateEmailConfig(config);
|
|
845
1010
|
validateServiceConfig(config);
|
|
846
1011
|
validateObjectStorageConfig(config);
|
|
1012
|
+
validateLegalConfig(config);
|
|
847
1013
|
return config;
|
|
848
1014
|
}
|
|
1015
|
+
/** PostHog's EU Cloud ingestion host, where OpenPencil's own project is. */
|
|
1016
|
+
const POSTHOG_DEFAULT_HOST = "https://eu.i.posthog.com";
|
|
849
1017
|
const DEFAULT_SECRET_NAMES = {
|
|
850
1018
|
databaseURL: "DATABASE_URL",
|
|
851
1019
|
authSecret: "BETTER_AUTH_SECRET",
|
|
@@ -858,8 +1026,9 @@ const DEFAULT_SECRET_NAMES = {
|
|
|
858
1026
|
smtpUser: "OPENPENCIL_CLOUD_SMTP_USER",
|
|
859
1027
|
smtpPassword: "OPENPENCIL_CLOUD_SMTP_PASSWORD",
|
|
860
1028
|
turnstileSecretKey: "TURNSTILE_SECRET_KEY",
|
|
861
|
-
|
|
862
|
-
internalServiceToken: "OPENPENCIL_CLOUD_SERVICE_TOKEN"
|
|
1029
|
+
servicesSigningKey: "OPENPENCIL_CLOUD_SERVICES_SIGNING_KEY",
|
|
1030
|
+
internalServiceToken: "OPENPENCIL_CLOUD_SERVICE_TOKEN",
|
|
1031
|
+
posthogProjectAPIKey: "POSTHOG_PROJECT_API_KEY"
|
|
863
1032
|
};
|
|
864
1033
|
const urlSchema = v.pipe(v.string(), v.url());
|
|
865
1034
|
const textSchema = v.pipe(v.string(), v.trim(), v.minLength(1));
|
|
@@ -992,6 +1161,11 @@ const cloudDeploymentConfigSchema = v.strictObject({
|
|
|
992
1161
|
checksum_verification: v.optional(v.picklist(["native", "metadata"]), "native"),
|
|
993
1162
|
server_side_encryption: v.optional(v.picklist(["AES256", "aws:kms"])),
|
|
994
1163
|
kms_key_id: v.optional(textSchema),
|
|
1164
|
+
/**
|
|
1165
|
+
* Keeps each file cleanup deletes under this prefix, such as `deleted/`, so a database restored
|
|
1166
|
+
* to an earlier time still finds it; a lifecycle rule on the prefix expires them.
|
|
1167
|
+
*/
|
|
1168
|
+
deleted_prefix: v.optional(deletedPrefixSchema),
|
|
995
1169
|
maximum_upload_bytes: v.optional(positiveIntegerSchema)
|
|
996
1170
|
}),
|
|
997
1171
|
collaboration: v.optional(v.strictObject({
|
|
@@ -999,7 +1173,13 @@ const cloudDeploymentConfigSchema = v.strictObject({
|
|
|
999
1173
|
maximum_message_bytes: v.optional(positiveIntegerSchema),
|
|
1000
1174
|
maximum_connections_per_room: v.optional(positiveIntegerSchema)
|
|
1001
1175
|
}), {}),
|
|
1002
|
-
documents: v.optional(v.strictObject({
|
|
1176
|
+
documents: v.optional(v.strictObject({
|
|
1177
|
+
retention_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 30),
|
|
1178
|
+
purge_grace_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 7)
|
|
1179
|
+
}), {
|
|
1180
|
+
retention_days: 30,
|
|
1181
|
+
purge_grace_days: 7
|
|
1182
|
+
}),
|
|
1003
1183
|
email: emailSchema,
|
|
1004
1184
|
workers: v.optional(v.strictObject({
|
|
1005
1185
|
email: v.optional(v.strictObject({
|
|
@@ -1029,13 +1209,31 @@ const cloudDeploymentConfigSchema = v.strictObject({
|
|
|
1029
1209
|
source: v.literal("static"),
|
|
1030
1210
|
...staticEntitlementsTOMLSchema.entries
|
|
1031
1211
|
}), v.strictObject({ source: v.literal("database") })])),
|
|
1032
|
-
|
|
1212
|
+
services: v.optional(v.strictObject({
|
|
1033
1213
|
endpoint: urlSchema,
|
|
1034
|
-
signing_key: defaultReference(DEFAULT_SECRET_NAMES.
|
|
1214
|
+
signing_key: defaultReference(DEFAULT_SECRET_NAMES.servicesSigningKey),
|
|
1035
1215
|
token_lifetime_minutes: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(60)), 10)
|
|
1036
1216
|
})),
|
|
1037
1217
|
billing: v.optional(v.strictObject({ portal_url: urlSchema })),
|
|
1038
|
-
internal_api: v.optional(v.strictObject({ service_token: defaultReference(DEFAULT_SECRET_NAMES.internalServiceToken) }))
|
|
1218
|
+
internal_api: v.optional(v.strictObject({ service_token: defaultReference(DEFAULT_SECRET_NAMES.internalServiceToken) })),
|
|
1219
|
+
/**
|
|
1220
|
+
* Optional: the operator's terms and privacy policy, linked at sign-up and in the portal, and
|
|
1221
|
+
* with `require_acceptance`, accepted once per `terms_version` before an account uses the server.
|
|
1222
|
+
*/
|
|
1223
|
+
legal: v.optional(v.strictObject({
|
|
1224
|
+
terms_url: v.optional(urlSchema),
|
|
1225
|
+
privacy_url: v.optional(urlSchema),
|
|
1226
|
+
terms_version: v.optional(textSchema),
|
|
1227
|
+
require_acceptance: v.optional(v.boolean(), false),
|
|
1228
|
+
abuse_email: v.optional(v.pipe(v.string(), v.email())),
|
|
1229
|
+
minimum_age: v.optional(v.pipe(v.number(), v.integer(), v.minValue(13), v.maxValue(21)))
|
|
1230
|
+
})),
|
|
1231
|
+
/** Optional: reports server errors to PostHog error tracking. Leave it out to send nothing. */
|
|
1232
|
+
error_tracking: v.optional(v.strictObject({
|
|
1233
|
+
provider: v.literal("posthog"),
|
|
1234
|
+
host: v.optional(urlSchema, POSTHOG_DEFAULT_HOST),
|
|
1235
|
+
project_api_key: v.optional(configurableTextSchema, { from_env: DEFAULT_SECRET_NAMES.posthogProjectAPIKey })
|
|
1236
|
+
}))
|
|
1039
1237
|
});
|
|
1040
1238
|
//#endregion
|
|
1041
1239
|
//#region src/server/db/expressions.ts
|
|
@@ -1046,788 +1244,427 @@ function checkColumnIn(column, values) {
|
|
|
1046
1244
|
if (values.length === 0 || values.some((value) => !CHECK_VALUE_PATTERN.test(value))) throw new Error("Check constraint values must be non-empty lowercase identifiers");
|
|
1047
1245
|
return sql`${sql.ref(column)} in (${sql.join(values.map((value) => sql.lit(value)))})`;
|
|
1048
1246
|
}
|
|
1049
|
-
//#endregion
|
|
1050
|
-
//#region src/server/db/migrations/helpers.ts
|
|
1051
|
-
async function addCheckConstraint(database, table, constraint, column, values) {
|
|
1052
|
-
await database.schema.alterTable(table).addCheckConstraint(constraint, checkColumnIn(column, values)).execute();
|
|
1053
|
-
}
|
|
1054
|
-
async function replaceCheckConstraint(database, table, constraint, column, values) {
|
|
1055
|
-
await database.schema.alterTable(table).dropConstraint(constraint).execute();
|
|
1056
|
-
await addCheckConstraint(database, table, constraint, column, values);
|
|
1057
|
-
}
|
|
1058
|
-
async function addCurrentTimestampColumns(database, table, columns) {
|
|
1059
|
-
for (const column of columns) await database.schema.alterTable(table).addColumn(column, "timestamptz", (definition) => definition.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1060
|
-
}
|
|
1061
1247
|
/**
|
|
1062
|
-
*
|
|
1063
|
-
*
|
|
1248
|
+
* Holds a lock named `key` until the transaction ends, for a check and a write that must not
|
|
1249
|
+
* interleave with another transaction's, such as reserving storage from an account's allowance,
|
|
1250
|
+
* which spans rows no single one of which could be locked for it.
|
|
1064
1251
|
*/
|
|
1065
|
-
function
|
|
1066
|
-
|
|
1067
|
-
}
|
|
1068
|
-
//#endregion
|
|
1069
|
-
//#region src/server/db/migrations/001_foundation.ts
|
|
1070
|
-
var _001_foundation_exports = /* @__PURE__ */ __exportAll({
|
|
1071
|
-
down: () => down$26,
|
|
1072
|
-
up: () => up$26
|
|
1073
|
-
});
|
|
1074
|
-
const UPDATED_AT_TRIGGER = "openpencil_set_updated_at";
|
|
1075
|
-
async function up$26(database) {
|
|
1076
|
-
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();
|
|
1077
|
-
await addCurrentTimestampColumns(database, "workspace", ["created_at", "updated_at"]);
|
|
1078
|
-
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", [
|
|
1079
|
-
"admin",
|
|
1080
|
-
"editor",
|
|
1081
|
-
"viewer"
|
|
1082
|
-
])).execute();
|
|
1083
|
-
await addCurrentTimestampColumns(database, "workspace_member", ["created_at"]);
|
|
1084
|
-
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();
|
|
1085
|
-
await addCurrentTimestampColumns(database, "document", ["created_at", "updated_at"]);
|
|
1086
|
-
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();
|
|
1087
|
-
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();
|
|
1088
|
-
await database.schema.alterTable("document").addForeignKeyConstraint("document_current_revision_foreign", ["current_revision_id"], "document_revision", ["id"], (constraint) => constraint.onDelete("set null")).execute();
|
|
1089
|
-
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", [
|
|
1090
|
-
"pending",
|
|
1091
|
-
"committed",
|
|
1092
|
-
"abandoned"
|
|
1093
|
-
])).execute();
|
|
1094
|
-
await database.schema.createIndex("workspace_member_user_index").on("workspace_member").column("user_id").execute();
|
|
1095
|
-
await database.schema.createIndex("document_workspace_index").on("document").columns(["workspace_id", "updated_at"]).execute();
|
|
1096
|
-
await database.schema.createIndex("document_revision_document_index").on("document_revision").columns(["document_id", "created_at"]).execute();
|
|
1097
|
-
await database.schema.createIndex("upload_expiration_index").on("upload").columns(["status", "expires_at"]).execute();
|
|
1098
|
-
await sql`
|
|
1099
|
-
create function ${sql.id(UPDATED_AT_TRIGGER)}() returns trigger as $$
|
|
1100
|
-
begin
|
|
1101
|
-
new.updated_at = now();
|
|
1102
|
-
return new;
|
|
1103
|
-
end;
|
|
1104
|
-
$$ language plpgsql
|
|
1105
|
-
`.execute(database);
|
|
1106
|
-
for (const table of ["workspace", "document"]) await sql`
|
|
1107
|
-
create trigger ${sql.id(`${table}_updated_at`)}
|
|
1108
|
-
before update on ${sql.table(table)}
|
|
1109
|
-
for each row execute function ${sql.id(UPDATED_AT_TRIGGER)}()
|
|
1110
|
-
`.execute(database);
|
|
1111
|
-
}
|
|
1112
|
-
async function down$26(database) {
|
|
1113
|
-
await database.schema.dropTable("upload").ifExists().execute();
|
|
1114
|
-
await database.schema.alterTable("document").dropConstraint("document_current_revision_foreign").execute();
|
|
1115
|
-
await database.schema.dropTable("document_revision").ifExists().execute();
|
|
1116
|
-
await database.schema.dropTable("storage_object").ifExists().execute();
|
|
1117
|
-
await database.schema.dropTable("document").ifExists().execute();
|
|
1118
|
-
await database.schema.dropTable("workspace_member").ifExists().execute();
|
|
1119
|
-
await database.schema.dropTable("workspace").ifExists().execute();
|
|
1120
|
-
await sql`drop function if exists ${sql.id(UPDATED_AT_TRIGGER)}()`.execute(database);
|
|
1121
|
-
}
|
|
1122
|
-
//#endregion
|
|
1123
|
-
//#region src/server/db/migrations/002_upload_cleanup_claims.ts
|
|
1124
|
-
var _002_upload_cleanup_claims_exports = /* @__PURE__ */ __exportAll({
|
|
1125
|
-
down: () => down$25,
|
|
1126
|
-
up: () => up$25
|
|
1127
|
-
});
|
|
1128
|
-
async function up$25(database) {
|
|
1129
|
-
await database.schema.alterTable("upload").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").execute();
|
|
1130
|
-
await database.schema.alterTable("upload").dropConstraint("upload_status_check").execute();
|
|
1131
|
-
await database.schema.alterTable("upload").addCheckConstraint("upload_status_check", checkColumnIn("status", [
|
|
1132
|
-
"pending",
|
|
1133
|
-
"cleaning",
|
|
1134
|
-
"committed",
|
|
1135
|
-
"abandoned"
|
|
1136
|
-
])).execute();
|
|
1137
|
-
}
|
|
1138
|
-
async function down$25(database) {
|
|
1139
|
-
await database.schema.alterTable("upload").dropConstraint("upload_status_check").execute();
|
|
1140
|
-
await database.schema.alterTable("upload").addCheckConstraint("upload_status_check", checkColumnIn("status", [
|
|
1141
|
-
"pending",
|
|
1142
|
-
"committed",
|
|
1143
|
-
"abandoned"
|
|
1144
|
-
])).execute();
|
|
1145
|
-
await database.schema.alterTable("upload").dropColumn("cleanup_claimed_at").dropColumn("cleanup_claim_id").execute();
|
|
1146
|
-
}
|
|
1147
|
-
//#endregion
|
|
1148
|
-
//#region src/server/db/migrations/003_document_cleanup_claims.ts
|
|
1149
|
-
var _003_document_cleanup_claims_exports = /* @__PURE__ */ __exportAll({
|
|
1150
|
-
down: () => down$24,
|
|
1151
|
-
up: () => up$24
|
|
1152
|
-
});
|
|
1153
|
-
async function up$24(database) {
|
|
1154
|
-
await database.schema.alterTable("document").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").execute();
|
|
1155
|
-
await database.schema.createIndex("document_cleanup_index").on("document").columns(["deleted_at", "cleanup_claimed_at"]).execute();
|
|
1156
|
-
}
|
|
1157
|
-
async function down$24(database) {
|
|
1158
|
-
await database.schema.dropIndex("document_cleanup_index").ifExists().execute();
|
|
1159
|
-
await database.schema.alterTable("document").dropColumn("cleanup_claimed_at").dropColumn("cleanup_claim_id").execute();
|
|
1160
|
-
}
|
|
1161
|
-
//#endregion
|
|
1162
|
-
//#region src/server/db/migrations/004_document_sharing.ts
|
|
1163
|
-
var _004_document_sharing_exports = /* @__PURE__ */ __exportAll({
|
|
1164
|
-
down: () => down$23,
|
|
1165
|
-
up: () => up$23
|
|
1166
|
-
});
|
|
1167
|
-
async function up$23(database) {
|
|
1168
|
-
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();
|
|
1169
|
-
await addCurrentTimestampColumns(database, "document_share", ["created_at", "updated_at"]);
|
|
1170
|
-
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();
|
|
1171
|
-
await addCurrentTimestampColumns(database, "document_grant", ["created_at", "updated_at"]);
|
|
1172
|
-
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();
|
|
1173
|
-
await addCurrentTimestampColumns(database, "document_invitation", ["invited_at"]);
|
|
1174
|
-
await database.schema.createIndex("document_share_document_index").on("document_share").columns(["document_id", "revoked_at"]).execute();
|
|
1175
|
-
await database.schema.createIndex("document_grant_user_index").on("document_grant").columns(["user_id", "document_id"]).execute();
|
|
1176
|
-
await database.schema.createIndex("document_invitation_document_index").on("document_invitation").columns(["document_id", "revoked_at"]).execute();
|
|
1177
|
-
}
|
|
1178
|
-
async function down$23(database) {
|
|
1179
|
-
await database.schema.dropTable("document_invitation").ifExists().execute();
|
|
1180
|
-
await database.schema.dropTable("document_grant").ifExists().execute();
|
|
1181
|
-
await database.schema.dropTable("document_share").ifExists().execute();
|
|
1182
|
-
}
|
|
1183
|
-
//#endregion
|
|
1184
|
-
//#region src/server/db/migrations/005_document_collaboration_epoch.ts
|
|
1185
|
-
var _005_document_collaboration_epoch_exports = /* @__PURE__ */ __exportAll({
|
|
1186
|
-
down: () => down$22,
|
|
1187
|
-
up: () => up$22
|
|
1188
|
-
});
|
|
1189
|
-
async function up$22(database) {
|
|
1190
|
-
await database.schema.alterTable("document").addColumn("collaborationEpoch", "integer", (column) => column.notNull().defaultTo(0)).execute();
|
|
1191
|
-
}
|
|
1192
|
-
async function down$22(database) {
|
|
1193
|
-
await database.schema.alterTable("document").dropColumn("collaborationEpoch").execute();
|
|
1194
|
-
}
|
|
1195
|
-
//#endregion
|
|
1196
|
-
//#region src/server/db/migrations/006_invitation_continuation.ts
|
|
1197
|
-
var _006_invitation_continuation_exports = /* @__PURE__ */ __exportAll({
|
|
1198
|
-
down: () => down$21,
|
|
1199
|
-
up: () => up$21
|
|
1200
|
-
});
|
|
1201
|
-
async function up$21(database) {
|
|
1202
|
-
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();
|
|
1203
|
-
await database.schema.createIndex("invitationContinuationExpiryIndex").on("invitationContinuation").column("expiresAt").execute();
|
|
1204
|
-
}
|
|
1205
|
-
async function down$21(database) {
|
|
1206
|
-
await database.schema.dropTable("invitationContinuation").execute();
|
|
1207
|
-
}
|
|
1208
|
-
//#endregion
|
|
1209
|
-
//#region src/server/db/migrations/007_collaboration_state.ts
|
|
1210
|
-
var _007_collaboration_state_exports = /* @__PURE__ */ __exportAll({
|
|
1211
|
-
down: () => down$20,
|
|
1212
|
-
up: () => up$20
|
|
1213
|
-
});
|
|
1214
|
-
async function up$20(database) {
|
|
1215
|
-
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();
|
|
1216
|
-
}
|
|
1217
|
-
async function down$20(database) {
|
|
1218
|
-
await database.schema.dropTable("documentCollaborationState").execute();
|
|
1219
|
-
}
|
|
1220
|
-
//#endregion
|
|
1221
|
-
//#region src/server/db/migrations/008_storage_reservations.ts
|
|
1222
|
-
var _008_storage_reservations_exports = /* @__PURE__ */ __exportAll({
|
|
1223
|
-
down: () => down$19,
|
|
1224
|
-
up: () => up$19
|
|
1225
|
-
});
|
|
1226
|
-
async function up$19(database) {
|
|
1227
|
-
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();
|
|
1228
|
-
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();
|
|
1229
|
-
}
|
|
1230
|
-
async function down$19(database) {
|
|
1231
|
-
await database.schema.dropTable("uploadStorageReservation").execute();
|
|
1232
|
-
await database.schema.dropTable("workspaceStorageUsage").execute();
|
|
1233
|
-
}
|
|
1234
|
-
//#endregion
|
|
1235
|
-
//#region src/server/db/migrations/009_workspace_entitlements.ts
|
|
1236
|
-
var _009_workspace_entitlements_exports = /* @__PURE__ */ __exportAll({
|
|
1237
|
-
down: () => down$18,
|
|
1238
|
-
up: () => up$18
|
|
1239
|
-
});
|
|
1240
|
-
async function up$18(database) {
|
|
1241
|
-
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();
|
|
1242
|
-
}
|
|
1243
|
-
async function down$18(database) {
|
|
1244
|
-
await database.schema.dropTable("workspaceEntitlement").execute();
|
|
1245
|
-
}
|
|
1246
|
-
//#endregion
|
|
1247
|
-
//#region src/server/db/migrations/010_upload_finalization.ts
|
|
1248
|
-
var _010_upload_finalization_exports = /* @__PURE__ */ __exportAll({
|
|
1249
|
-
down: () => down$17,
|
|
1250
|
-
up: () => up$17
|
|
1251
|
-
});
|
|
1252
|
-
async function up$17(database) {
|
|
1253
|
-
await replaceCheckConstraint(database, "upload", "upload_status_check", "status", [
|
|
1254
|
-
"pending",
|
|
1255
|
-
"finalizing",
|
|
1256
|
-
"cleaning",
|
|
1257
|
-
"committed",
|
|
1258
|
-
"abandoned"
|
|
1259
|
-
]);
|
|
1260
|
-
}
|
|
1261
|
-
async function down$17(database) {
|
|
1262
|
-
await replaceCheckConstraint(database, "upload", "upload_status_check", "status", [
|
|
1263
|
-
"pending",
|
|
1264
|
-
"cleaning",
|
|
1265
|
-
"committed",
|
|
1266
|
-
"abandoned"
|
|
1267
|
-
]);
|
|
1268
|
-
}
|
|
1269
|
-
//#endregion
|
|
1270
|
-
//#region src/server/db/migrations/011_upload_finalization_lease.ts
|
|
1271
|
-
var _011_upload_finalization_lease_exports = /* @__PURE__ */ __exportAll({
|
|
1272
|
-
down: () => down$16,
|
|
1273
|
-
up: () => up$16
|
|
1274
|
-
});
|
|
1275
|
-
async function up$16(database) {
|
|
1276
|
-
await database.schema.alterTable("upload").addColumn("finalization_started_at", "timestamptz").execute();
|
|
1277
|
-
}
|
|
1278
|
-
async function down$16(database) {
|
|
1279
|
-
await database.schema.alterTable("upload").dropColumn("finalization_started_at").execute();
|
|
1280
|
-
}
|
|
1281
|
-
//#endregion
|
|
1282
|
-
//#region src/server/db/migrations/012_transactional_email.ts
|
|
1283
|
-
var _012_transactional_email_exports = /* @__PURE__ */ __exportAll({
|
|
1284
|
-
down: () => down$15,
|
|
1285
|
-
up: () => up$15
|
|
1286
|
-
});
|
|
1287
|
-
async function up$15(database) {
|
|
1288
|
-
await database.schema.createTable("transactional_email").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("idempotency_key", "text", (column) => column.notNull().unique()).addColumn("kind", "text", (column) => column.notNull()).addColumn("recipient_email_normalized", "text", (column) => column.notNull()).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", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).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", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("updated_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("accepted_at", "timestamptz").addCheckConstraint("transactional_email_kind_check", checkColumnIn("kind", ["document-invitation"])).addCheckConstraint("transactional_email_status_check", checkColumnIn("status", [
|
|
1289
|
-
"pending",
|
|
1290
|
-
"sending",
|
|
1291
|
-
"accepted",
|
|
1292
|
-
"failed",
|
|
1293
|
-
"suppressed"
|
|
1294
|
-
])).execute();
|
|
1295
|
-
await database.schema.createIndex("transactional_email_delivery_index").on("transactional_email").columns(["status", "next_attempt_at"]).execute();
|
|
1296
|
-
}
|
|
1297
|
-
async function down$15(database) {
|
|
1298
|
-
await database.schema.dropTable("transactional_email").ifExists().execute();
|
|
1299
|
-
}
|
|
1300
|
-
//#endregion
|
|
1301
|
-
//#region src/server/db/migrations/013_admin_enrollment.ts
|
|
1302
|
-
var _013_admin_enrollment_exports = /* @__PURE__ */ __exportAll({
|
|
1303
|
-
down: () => down$14,
|
|
1304
|
-
up: () => up$14
|
|
1305
|
-
});
|
|
1306
|
-
async function up$14(database) {
|
|
1307
|
-
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", [
|
|
1308
|
-
"pending",
|
|
1309
|
-
"approved",
|
|
1310
|
-
"rejected",
|
|
1311
|
-
"revoked"
|
|
1312
|
-
])).execute();
|
|
1313
|
-
await database.schema.createIndex("cloud_enrollment_status_index").on("cloud_enrollment").columns(["status", "requested_at"]).execute();
|
|
1314
|
-
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();
|
|
1315
|
-
await database.schema.createIndex("cloud_admin_audit_created_index").on("cloud_admin_audit_event").column("created_at").execute();
|
|
1316
|
-
}
|
|
1317
|
-
async function down$14(database) {
|
|
1318
|
-
await database.schema.dropTable("cloud_admin_audit_event").ifExists().execute();
|
|
1319
|
-
await database.schema.dropTable("cloud_enrollment").ifExists().execute();
|
|
1252
|
+
async function lockForTransaction(transaction, key) {
|
|
1253
|
+
await sql`select pg_advisory_xact_lock(hashtextextended(${key}, 0))`.execute(transaction);
|
|
1320
1254
|
}
|
|
1321
1255
|
//#endregion
|
|
1322
|
-
//#region src/server/db/migrations/
|
|
1323
|
-
var _014_enrollment_email_kinds_exports = /* @__PURE__ */ __exportAll({
|
|
1324
|
-
down: () => down$13,
|
|
1325
|
-
up: () => up$13
|
|
1326
|
-
});
|
|
1327
|
-
async function up$13(database) {
|
|
1328
|
-
await replaceCheckConstraint(database, "transactional_email", "transactional_email_kind_check", "kind", [
|
|
1329
|
-
"document-invitation",
|
|
1330
|
-
"enrollment-requested",
|
|
1331
|
-
"admin-enrollment-notification",
|
|
1332
|
-
"enrollment-approved",
|
|
1333
|
-
"enrollment-rejected",
|
|
1334
|
-
"enrollment-revoked"
|
|
1335
|
-
]);
|
|
1336
|
-
}
|
|
1337
|
-
async function down$13(database) {
|
|
1338
|
-
await replaceCheckConstraint(database, "transactional_email", "transactional_email_kind_check", "kind", ["document-invitation"]);
|
|
1339
|
-
}
|
|
1340
|
-
//#endregion
|
|
1341
|
-
//#region src/server/db/migrations/015_cloud_rate_limit.ts
|
|
1342
|
-
var _015_cloud_rate_limit_exports = /* @__PURE__ */ __exportAll({
|
|
1343
|
-
down: () => down$12,
|
|
1344
|
-
up: () => up$12
|
|
1345
|
-
});
|
|
1346
|
-
async function up$12(database) {
|
|
1347
|
-
await database.schema.alterTable("cloud_enrollment").addColumn("request_revision", "integer", (column) => column.notNull().defaultTo(1)).execute();
|
|
1348
|
-
await database.schema.createTable("cloud_rate_limit").addColumn("key_hash", "text", (column) => column.primaryKey()).addColumn("window_started_at", "timestamptz", (column) => column.notNull()).addColumn("request_count", "integer", (column) => column.notNull().defaultTo(1)).execute();
|
|
1349
|
-
await addCurrentTimestampColumns(database, "cloud_rate_limit", ["updated_at"]);
|
|
1350
|
-
}
|
|
1351
|
-
async function down$12(database) {
|
|
1352
|
-
await database.schema.dropTable("cloud_rate_limit").ifExists().execute();
|
|
1353
|
-
await database.schema.alterTable("cloud_enrollment").dropColumn("request_revision").execute();
|
|
1354
|
-
}
|
|
1355
|
-
//#endregion
|
|
1356
|
-
//#region src/server/db/migrations/016_auth_schema.ts
|
|
1357
|
-
var _016_auth_schema_exports = /* @__PURE__ */ __exportAll({
|
|
1358
|
-
down: () => down$11,
|
|
1359
|
-
up: () => up$11
|
|
1360
|
-
});
|
|
1361
|
-
async function up$11(database) {
|
|
1362
|
-
await database.schema.createTable("cloud_auth_schema").addColumn("id", "text", (column) => column.primaryKey()).addColumn("version", "text", (column) => column.notNull()).execute();
|
|
1363
|
-
await addCurrentTimestampColumns(database, "cloud_auth_schema", ["updated_at"]);
|
|
1364
|
-
}
|
|
1365
|
-
async function down$11(database) {
|
|
1366
|
-
await database.schema.dropTable("cloud_auth_schema").ifExists().execute();
|
|
1367
|
-
}
|
|
1368
|
-
//#endregion
|
|
1369
|
-
//#region src/server/db/migrations/017_authentication_email_kinds.ts
|
|
1370
|
-
var _017_authentication_email_kinds_exports = /* @__PURE__ */ __exportAll({
|
|
1371
|
-
down: () => down$10,
|
|
1372
|
-
up: () => up$10
|
|
1373
|
-
});
|
|
1374
|
-
const kinds = [
|
|
1375
|
-
"document-invitation",
|
|
1376
|
-
"enrollment-requested",
|
|
1377
|
-
"admin-enrollment-notification",
|
|
1378
|
-
"enrollment-approved",
|
|
1379
|
-
"enrollment-rejected",
|
|
1380
|
-
"enrollment-revoked"
|
|
1381
|
-
];
|
|
1382
|
-
async function up$10(database) {
|
|
1383
|
-
await replaceCheckConstraint(database, "transactional_email", "transactional_email_kind_check", "kind", [
|
|
1384
|
-
...kinds,
|
|
1385
|
-
"email-verification",
|
|
1386
|
-
"password-reset",
|
|
1387
|
-
"password-changed"
|
|
1388
|
-
]);
|
|
1389
|
-
}
|
|
1390
|
-
async function down$10(database) {
|
|
1391
|
-
await replaceCheckConstraint(database, "transactional_email", "transactional_email_kind_check", "kind", kinds);
|
|
1392
|
-
}
|
|
1393
|
-
//#endregion
|
|
1394
|
-
//#region src/server/db/migrations/018_authentication_mfa.ts
|
|
1395
|
-
var _018_authentication_mfa_exports = /* @__PURE__ */ __exportAll({
|
|
1396
|
-
down: () => down$9,
|
|
1397
|
-
up: () => up$9
|
|
1398
|
-
});
|
|
1399
|
-
async function tableNames(database) {
|
|
1400
|
-
return new Set((await database.introspection.getTables()).map((table) => table.name));
|
|
1401
|
-
}
|
|
1402
|
-
async function up$9(database) {
|
|
1403
|
-
const cloud = database;
|
|
1404
|
-
const tables = await tableNames(database);
|
|
1405
|
-
await database.schema.createTable("cloud_mfa_assurance").addColumn("session_id", "uuid", (column) => column.primaryKey()).addColumn("user_id", "uuid", (column) => column.notNull()).addColumn("method", "text", (column) => column.notNull()).addColumn("verified_at", "timestamptz", (column) => column.notNull()).execute();
|
|
1406
|
-
if (!tables.has("user")) return;
|
|
1407
|
-
await database.schema.alterTable("user").addColumn("two_factor_enabled", "boolean", (column) => column.notNull().defaultTo(false)).execute();
|
|
1408
|
-
await database.schema.createTable("two_factor").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("secret", "text", (column) => column.notNull().unique()).addColumn("backup_codes", "text", (column) => column.notNull()).addColumn("user_id", "uuid", (column) => column.notNull().references("user.id").onDelete("cascade")).addColumn("verified", "boolean", (column) => column.notNull().defaultTo(true)).addColumn("failed_verification_count", "integer", (column) => column.notNull().defaultTo(0)).addColumn("locked_until", "timestamptz").execute();
|
|
1409
|
-
await database.schema.createIndex("two_factor_user_index").on("two_factor").column("user_id").execute();
|
|
1410
|
-
await database.schema.createTable("passkey").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("name", "text").addColumn("public_key", "text", (column) => column.notNull()).addColumn("user_id", "uuid", (column) => column.notNull().references("user.id").onDelete("cascade")).addColumn("credential_id", "text", (column) => column.notNull().unique()).addColumn("counter", "integer", (column) => column.notNull()).addColumn("device_type", "text", (column) => column.notNull()).addColumn("backed_up", "boolean", (column) => column.notNull()).addColumn("transports", "text").addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("aaguid", "text").execute();
|
|
1411
|
-
await database.schema.createIndex("passkey_user_index").on("passkey").column("user_id").execute();
|
|
1412
|
-
await cloud.updateTable("cloudAuthSchema").set({
|
|
1413
|
-
version: "1.7.2+mfa.1",
|
|
1414
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1415
|
-
}).where("id", "=", "better-auth").where("version", "=", "1.7.2").execute();
|
|
1416
|
-
}
|
|
1417
|
-
async function down$9(database) {
|
|
1418
|
-
await database.schema.dropTable("cloud_mfa_assurance").ifExists().execute();
|
|
1419
|
-
await database.schema.dropTable("passkey").ifExists().execute();
|
|
1420
|
-
await database.schema.dropTable("two_factor").ifExists().execute();
|
|
1421
|
-
if ((await tableNames(database)).has("user")) await database.schema.alterTable("user").dropColumn("two_factor_enabled").execute();
|
|
1422
|
-
}
|
|
1423
|
-
//#endregion
|
|
1424
|
-
//#region src/server/db/migrations/019_better_auth_account_issuer.ts
|
|
1425
|
-
var _019_better_auth_account_issuer_exports = /* @__PURE__ */ __exportAll({
|
|
1426
|
-
down: () => down$8,
|
|
1427
|
-
up: () => up$8
|
|
1428
|
-
});
|
|
1429
|
-
/** The unique index Better Auth 1.7.0 through 1.7.2 created on `account (issuer, accountId)`. */
|
|
1430
|
-
const ISSUER_INDEX = "account_issuer_accountId_uidx";
|
|
1256
|
+
//#region src/server/db/migrations/schema.ts
|
|
1431
1257
|
/**
|
|
1432
|
-
*
|
|
1433
|
-
*
|
|
1434
|
-
*
|
|
1435
|
-
*
|
|
1258
|
+
* What Cloud's migrations build tables with. Names follow PostgreSQL's own: `<table>_pkey`,
|
|
1259
|
+
* `<table>_<columns>_key` for unique, `<table>_<columns>_fkey`, and `<table>_<column>_check`;
|
|
1260
|
+
* indexes are `<table>_<columns>_idx`. Account IDs are text, since Better Auth's tables come after
|
|
1261
|
+
* Cloud's (see `migrateCloudDatabase`), and never foreign keys.
|
|
1436
1262
|
*/
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1263
|
+
const now = (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP);
|
|
1264
|
+
const required = (column) => column.notNull();
|
|
1265
|
+
const PERMISSIONS = ["view", "edit"];
|
|
1266
|
+
const ROLES = [
|
|
1267
|
+
"admin",
|
|
1268
|
+
"editor",
|
|
1269
|
+
"viewer"
|
|
1270
|
+
];
|
|
1271
|
+
/** What every emailed invitation keeps: whom it went to, its access, its secret, and its life. */
|
|
1272
|
+
function withInvitation(table, access) {
|
|
1273
|
+
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");
|
|
1274
|
+
}
|
|
1275
|
+
/** Text columns that take one of a few values, as `[table, column, values]`. */
|
|
1276
|
+
async function addChecks(database, checks) {
|
|
1277
|
+
for (const [table, column, values] of checks) await database.schema.alterTable(table).addCheckConstraint(`${table}_${column}_check`, checkColumnIn(column, values)).execute();
|
|
1278
|
+
}
|
|
1279
|
+
/** Lookups beyond primary keys and unique columns, as `[table, columns]`; `desc` sorts newest first. */
|
|
1280
|
+
async function addIndexes(database, indexes) {
|
|
1281
|
+
for (const [table, columns] of indexes) {
|
|
1282
|
+
const name = columns.map((column) => column.split(" ")[0]).join("_");
|
|
1283
|
+
await database.schema.createIndex(`${table}_${name}_idx`).on(table).columns([...columns]).execute();
|
|
1442
1284
|
}
|
|
1443
|
-
await cloud.updateTable("cloudAuthSchema").set({
|
|
1444
|
-
version: "1.7.7+mfa.1",
|
|
1445
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1446
|
-
}).where("id", "=", "better-auth").where("version", "=", "1.7.2+mfa.1").execute();
|
|
1447
1285
|
}
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1452
|
-
}).where("id", "=", "better-auth").where("version", "=", "1.7.7+mfa.1").execute();
|
|
1286
|
+
/** Drops a migration's tables, last created first. */
|
|
1287
|
+
async function dropTables(database, tables) {
|
|
1288
|
+
for (const table of tables.toReversed()) await database.schema.dropTable(table).ifExists().cascade().execute();
|
|
1453
1289
|
}
|
|
1454
1290
|
//#endregion
|
|
1455
|
-
//#region src/server/db/migrations/
|
|
1456
|
-
var
|
|
1291
|
+
//#region src/server/db/migrations/001_accounts.ts
|
|
1292
|
+
var _001_accounts_exports = /* @__PURE__ */ __exportAll({
|
|
1457
1293
|
down: () => down$7,
|
|
1458
1294
|
up: () => up$7
|
|
1459
1295
|
});
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
"
|
|
1463
|
-
"
|
|
1464
|
-
"
|
|
1465
|
-
"
|
|
1466
|
-
"
|
|
1467
|
-
"
|
|
1468
|
-
"password-reset",
|
|
1469
|
-
"password-changed"
|
|
1296
|
+
/** What Cloud keeps about an account beside Better Auth's tables: its plan, sign-ins, and picture. */
|
|
1297
|
+
const TABLES$5 = [
|
|
1298
|
+
"auth_schema_version",
|
|
1299
|
+
"account_entitlement",
|
|
1300
|
+
"mfa_assurance",
|
|
1301
|
+
"sign_in",
|
|
1302
|
+
"user_picture",
|
|
1303
|
+
"api_rate_limit"
|
|
1470
1304
|
];
|
|
1471
1305
|
async function up$7(database) {
|
|
1472
|
-
await
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
await
|
|
1478
|
-
await database
|
|
1479
|
-
|
|
1306
|
+
await database.schema.createTable("auth_schema_version").addColumn("id", "text", (column) => column.primaryKey()).addColumn("version", "text", required).addColumn("updated_at", "timestamptz", now).execute();
|
|
1307
|
+
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();
|
|
1308
|
+
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();
|
|
1309
|
+
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();
|
|
1310
|
+
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();
|
|
1311
|
+
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();
|
|
1312
|
+
await addChecks(database, [[
|
|
1313
|
+
"user_picture",
|
|
1314
|
+
"format",
|
|
1315
|
+
["png", "jpeg"]
|
|
1316
|
+
], [
|
|
1317
|
+
"user_picture",
|
|
1318
|
+
"source",
|
|
1319
|
+
["upload", "provider"]
|
|
1320
|
+
]]);
|
|
1321
|
+
await addIndexes(database, [["sign_in", ["user_id", "created_at"]], ["sign_in", ["session_id"]]]);
|
|
1480
1322
|
}
|
|
1481
1323
|
async function down$7(database) {
|
|
1482
|
-
await
|
|
1483
|
-
await database.schema.dropTable("workspace_invitation").execute();
|
|
1324
|
+
await dropTables(database, TABLES$5);
|
|
1484
1325
|
}
|
|
1485
1326
|
//#endregion
|
|
1486
|
-
//#region src/server/db/migrations/
|
|
1487
|
-
var
|
|
1327
|
+
//#region src/server/db/migrations/002_workspaces.ts
|
|
1328
|
+
var _002_workspaces_exports = /* @__PURE__ */ __exportAll({
|
|
1488
1329
|
down: () => down$6,
|
|
1489
|
-
keepCurrentRevisions: () => keepCurrentRevisions,
|
|
1490
1330
|
up: () => up$6
|
|
1491
1331
|
});
|
|
1492
|
-
/**
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1332
|
+
/** Workspaces, their members, and the people invited to join. */
|
|
1333
|
+
const TABLES$4 = [
|
|
1334
|
+
"workspace",
|
|
1335
|
+
"workspace_member",
|
|
1336
|
+
"workspace_invitation"
|
|
1337
|
+
];
|
|
1496
1338
|
async function up$6(database) {
|
|
1497
|
-
await database.schema.createTable("
|
|
1498
|
-
await
|
|
1499
|
-
await database.schema.
|
|
1500
|
-
|
|
1501
|
-
"
|
|
1502
|
-
"
|
|
1503
|
-
|
|
1504
|
-
|
|
1339
|
+
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();
|
|
1340
|
+
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();
|
|
1341
|
+
await withInvitation(database.schema.createTable("workspace_invitation"), "role").addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).execute();
|
|
1342
|
+
await addChecks(database, [[
|
|
1343
|
+
"workspace_member",
|
|
1344
|
+
"role",
|
|
1345
|
+
ROLES
|
|
1346
|
+
], [
|
|
1347
|
+
"workspace_invitation",
|
|
1348
|
+
"role",
|
|
1349
|
+
ROLES
|
|
1350
|
+
]]);
|
|
1351
|
+
await addIndexes(database, [
|
|
1352
|
+
["workspace", ["owner_id"]],
|
|
1353
|
+
["workspace_member", ["user_id"]],
|
|
1354
|
+
["workspace_invitation", ["workspace_id", "revoked_at"]]
|
|
1355
|
+
]);
|
|
1505
1356
|
}
|
|
1506
1357
|
async function down$6(database) {
|
|
1507
|
-
await database
|
|
1508
|
-
}
|
|
1509
|
-
/** Keeps each document's current revision as the first checkpoint of its history. */
|
|
1510
|
-
async function keepCurrentRevisions(database) {
|
|
1511
|
-
await database.insertInto("documentVersion").columns([
|
|
1512
|
-
"id",
|
|
1513
|
-
"documentId",
|
|
1514
|
-
"revisionId",
|
|
1515
|
-
"kind",
|
|
1516
|
-
"createdBy",
|
|
1517
|
-
"createdAt"
|
|
1518
|
-
]).expression((expression) => expression.selectFrom("document").innerJoin("documentRevision", "documentRevision.id", "document.currentRevisionId").select((select) => [
|
|
1519
|
-
select.fn("gen_random_uuid").as("id"),
|
|
1520
|
-
"document.id as documentId",
|
|
1521
|
-
"documentRevision.id as revisionId",
|
|
1522
|
-
select.val("checkpoint").as("kind"),
|
|
1523
|
-
"documentRevision.createdBy",
|
|
1524
|
-
"documentRevision.createdAt"
|
|
1525
|
-
])).execute();
|
|
1358
|
+
await dropTables(database, TABLES$4);
|
|
1526
1359
|
}
|
|
1527
1360
|
//#endregion
|
|
1528
|
-
//#region src/server/db/migrations/
|
|
1529
|
-
var
|
|
1361
|
+
//#region src/server/db/migrations/003_storage.ts
|
|
1362
|
+
var _003_storage_exports = /* @__PURE__ */ __exportAll({
|
|
1530
1363
|
down: () => down$5,
|
|
1531
1364
|
up: () => up$5
|
|
1532
1365
|
});
|
|
1533
|
-
/**
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1366
|
+
/** Files in the object store, a workspace's images among them, and how much each workspace uses. */
|
|
1367
|
+
const TABLES$3 = [
|
|
1368
|
+
"storage_object",
|
|
1369
|
+
"workspace_storage_usage",
|
|
1370
|
+
"workspace_image"
|
|
1371
|
+
];
|
|
1537
1372
|
async function up$5(database) {
|
|
1538
|
-
await database.schema.
|
|
1373
|
+
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();
|
|
1374
|
+
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();
|
|
1375
|
+
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();
|
|
1539
1376
|
}
|
|
1540
1377
|
async function down$5(database) {
|
|
1541
|
-
await database
|
|
1378
|
+
await dropTables(database, TABLES$3);
|
|
1542
1379
|
}
|
|
1543
1380
|
//#endregion
|
|
1544
|
-
//#region src/server/db/migrations/
|
|
1545
|
-
var
|
|
1381
|
+
//#region src/server/db/migrations/004_documents.ts
|
|
1382
|
+
var _004_documents_exports = /* @__PURE__ */ __exportAll({
|
|
1546
1383
|
down: () => down$4,
|
|
1547
1384
|
up: () => up$4
|
|
1548
1385
|
});
|
|
1549
|
-
/**
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1386
|
+
/** Documents and what they hold: revisions, versions, images, collaboration rooms, and uploads. */
|
|
1387
|
+
const TABLES$2 = [
|
|
1388
|
+
"document",
|
|
1389
|
+
"document_revision",
|
|
1390
|
+
"document_version",
|
|
1391
|
+
"revision_image",
|
|
1392
|
+
"document_room_snapshot",
|
|
1393
|
+
"document_room_update",
|
|
1394
|
+
"upload",
|
|
1395
|
+
"upload_storage_reservation"
|
|
1396
|
+
];
|
|
1397
|
+
async function createRevisions(database) {
|
|
1398
|
+
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();
|
|
1399
|
+
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();
|
|
1400
|
+
await database.schema.alterTable("document").addForeignKeyConstraint("document_current_revision_id_fkey", ["current_revision_id"], "document_revision", ["id"], (constraint) => constraint.onDelete("set null")).execute();
|
|
1401
|
+
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();
|
|
1402
|
+
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();
|
|
1403
|
+
}
|
|
1404
|
+
async function createRooms(database) {
|
|
1405
|
+
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();
|
|
1406
|
+
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", [
|
|
1407
|
+
"document_id",
|
|
1408
|
+
"room_epoch",
|
|
1409
|
+
"sequence"
|
|
1410
|
+
]).execute();
|
|
1411
|
+
}
|
|
1412
|
+
async function createUploads(database) {
|
|
1413
|
+
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();
|
|
1414
|
+
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();
|
|
1415
|
+
}
|
|
1555
1416
|
async function up$4(database) {
|
|
1556
|
-
await database
|
|
1557
|
-
await
|
|
1558
|
-
await database
|
|
1559
|
-
await
|
|
1560
|
-
|
|
1561
|
-
|
|
1417
|
+
await createRevisions(database);
|
|
1418
|
+
await createRooms(database);
|
|
1419
|
+
await createUploads(database);
|
|
1420
|
+
await addChecks(database, [
|
|
1421
|
+
[
|
|
1422
|
+
"document",
|
|
1423
|
+
"kind",
|
|
1424
|
+
["design", "board"]
|
|
1425
|
+
],
|
|
1426
|
+
[
|
|
1427
|
+
"document_version",
|
|
1428
|
+
"kind",
|
|
1429
|
+
["checkpoint", "named"]
|
|
1430
|
+
],
|
|
1431
|
+
[
|
|
1432
|
+
"upload",
|
|
1433
|
+
"kind",
|
|
1434
|
+
["revision", "image"]
|
|
1435
|
+
],
|
|
1436
|
+
[
|
|
1437
|
+
"upload",
|
|
1438
|
+
"status",
|
|
1439
|
+
[
|
|
1440
|
+
"pending",
|
|
1441
|
+
"finalizing",
|
|
1442
|
+
"cleaning",
|
|
1443
|
+
"committed",
|
|
1444
|
+
"abandoned"
|
|
1445
|
+
]
|
|
1446
|
+
]
|
|
1447
|
+
]);
|
|
1448
|
+
await addIndexes(database, [
|
|
1449
|
+
["document", ["workspace_id", "updated_at"]],
|
|
1450
|
+
["document", ["deleted_at", "cleanup_claimed_at"]],
|
|
1451
|
+
["document_revision", ["document_id", "created_at"]],
|
|
1452
|
+
["document_version", [
|
|
1453
|
+
"document_id",
|
|
1454
|
+
"created_at desc",
|
|
1455
|
+
"id desc"
|
|
1456
|
+
]],
|
|
1457
|
+
["revision_image", ["workspace_id", "checksum"]],
|
|
1458
|
+
["upload", ["status", "expires_at"]]
|
|
1459
|
+
]);
|
|
1562
1460
|
}
|
|
1563
1461
|
async function down$4(database) {
|
|
1564
|
-
await database
|
|
1565
|
-
await database.schema.dropTable("workspace_image").execute();
|
|
1566
|
-
await database.schema.alterTable("upload").dropConstraint("upload_kind_check").execute();
|
|
1567
|
-
await database.schema.alterTable("upload").dropColumn("kind").execute();
|
|
1462
|
+
await dropTables(database, TABLES$2);
|
|
1568
1463
|
}
|
|
1569
1464
|
//#endregion
|
|
1570
|
-
//#region src/server/db/migrations/
|
|
1571
|
-
var
|
|
1465
|
+
//#region src/server/db/migrations/005_sharing.ts
|
|
1466
|
+
var _005_sharing_exports = /* @__PURE__ */ __exportAll({
|
|
1572
1467
|
down: () => down$3,
|
|
1573
1468
|
up: () => up$3
|
|
1574
1469
|
});
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
"
|
|
1578
|
-
"
|
|
1579
|
-
"
|
|
1580
|
-
"
|
|
1581
|
-
"
|
|
1582
|
-
"enrollment-revoked",
|
|
1583
|
-
"email-verification",
|
|
1584
|
-
"password-reset",
|
|
1585
|
-
"password-changed"
|
|
1470
|
+
/** Who a document is shared with: people, invitations, links, and the invitations budget. */
|
|
1471
|
+
const TABLES$1 = [
|
|
1472
|
+
"document_grant",
|
|
1473
|
+
"document_invitation",
|
|
1474
|
+
"invitation_continuation",
|
|
1475
|
+
"document_share",
|
|
1476
|
+
"invitation_send"
|
|
1586
1477
|
];
|
|
1587
|
-
/**
|
|
1588
|
-
* Every sign-in an account makes, so its owner can read where it was used and hear about one from
|
|
1589
|
-
* an address it never signed in from; a session keeps the row of the sign-in that made it, which
|
|
1590
|
-
* names how it was made. The security notices that follow get their own email kinds.
|
|
1591
|
-
*/
|
|
1592
1478
|
async function up$3(database) {
|
|
1593
|
-
await database.schema.createTable("
|
|
1594
|
-
await database.schema.
|
|
1595
|
-
await database.schema.
|
|
1596
|
-
await
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1479
|
+
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();
|
|
1480
|
+
await withInvitation(database.schema.createTable("document_invitation"), "permission").addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).execute();
|
|
1481
|
+
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();
|
|
1482
|
+
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();
|
|
1483
|
+
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();
|
|
1484
|
+
await addChecks(database, [
|
|
1485
|
+
[
|
|
1486
|
+
"document_grant",
|
|
1487
|
+
"permission",
|
|
1488
|
+
PERMISSIONS
|
|
1489
|
+
],
|
|
1490
|
+
[
|
|
1491
|
+
"document_invitation",
|
|
1492
|
+
"permission",
|
|
1493
|
+
PERMISSIONS
|
|
1494
|
+
],
|
|
1495
|
+
[
|
|
1496
|
+
"document_share",
|
|
1497
|
+
"permission",
|
|
1498
|
+
PERMISSIONS
|
|
1499
|
+
]
|
|
1500
|
+
]);
|
|
1501
|
+
await addIndexes(database, [
|
|
1502
|
+
["document_grant", ["user_id", "document_id"]],
|
|
1503
|
+
["document_invitation", ["document_id", "revoked_at"]],
|
|
1504
|
+
["document_share", ["document_id", "revoked_at"]],
|
|
1505
|
+
["invitation_continuation", ["expires_at"]],
|
|
1506
|
+
["invitation_send", ["sender_id", "sent_at"]],
|
|
1507
|
+
["invitation_send", ["email_normalized", "sent_at"]]
|
|
1601
1508
|
]);
|
|
1602
1509
|
}
|
|
1603
1510
|
async function down$3(database) {
|
|
1604
|
-
await
|
|
1605
|
-
await database.schema.dropTable("sign_in").execute();
|
|
1511
|
+
await dropTables(database, TABLES$1);
|
|
1606
1512
|
}
|
|
1607
1513
|
//#endregion
|
|
1608
|
-
//#region src/server/db/migrations/
|
|
1609
|
-
var
|
|
1514
|
+
//#region src/server/db/migrations/006_operations.ts
|
|
1515
|
+
var _006_operations_exports = /* @__PURE__ */ __exportAll({
|
|
1610
1516
|
down: () => down$2,
|
|
1611
1517
|
up: () => up$2
|
|
1612
1518
|
});
|
|
1613
|
-
/**
|
|
1614
|
-
* OpenPencil's tables beside Better Auth's lose the `cloud_` prefix the rest never had; only the
|
|
1615
|
-
* rate limit keeps a qualifier, since Better Auth has a `rate_limit` of its own. Their keys,
|
|
1616
|
-
* checks, and indexes follow, so no name keeps the old prefix.
|
|
1617
|
-
*/
|
|
1519
|
+
/** Running the deployment: the email outbox, enrollment, reports, and the administrators' log. */
|
|
1618
1520
|
const TABLES = [
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
["cloud_rate_limit", "api_rate_limit"]
|
|
1521
|
+
"transactional_email",
|
|
1522
|
+
"enrollment",
|
|
1523
|
+
"document_report",
|
|
1524
|
+
"admin_audit_event"
|
|
1624
1525
|
];
|
|
1625
|
-
const CONSTRAINTS = [
|
|
1626
|
-
[
|
|
1627
|
-
"enrollment",
|
|
1628
|
-
"cloud_enrollment_pkey",
|
|
1629
|
-
"enrollment_pkey"
|
|
1630
|
-
],
|
|
1631
|
-
[
|
|
1632
|
-
"enrollment",
|
|
1633
|
-
"cloud_enrollment_email_normalized_key",
|
|
1634
|
-
"enrollment_email_normalized_key"
|
|
1635
|
-
],
|
|
1636
|
-
[
|
|
1637
|
-
"enrollment",
|
|
1638
|
-
"cloud_enrollment_status_check",
|
|
1639
|
-
"enrollment_status_check"
|
|
1640
|
-
],
|
|
1641
|
-
[
|
|
1642
|
-
"mfa_assurance",
|
|
1643
|
-
"cloud_mfa_assurance_pkey",
|
|
1644
|
-
"mfa_assurance_pkey"
|
|
1645
|
-
],
|
|
1646
|
-
[
|
|
1647
|
-
"admin_audit_event",
|
|
1648
|
-
"cloud_admin_audit_event_pkey",
|
|
1649
|
-
"admin_audit_event_pkey"
|
|
1650
|
-
],
|
|
1651
|
-
[
|
|
1652
|
-
"auth_schema_version",
|
|
1653
|
-
"cloud_auth_schema_pkey",
|
|
1654
|
-
"auth_schema_version_pkey"
|
|
1655
|
-
],
|
|
1656
|
-
[
|
|
1657
|
-
"api_rate_limit",
|
|
1658
|
-
"cloud_rate_limit_pkey",
|
|
1659
|
-
"api_rate_limit_pkey"
|
|
1660
|
-
]
|
|
1661
|
-
];
|
|
1662
|
-
const INDEXES = [["cloud_enrollment_status_index", "enrollment_status_index"], ["cloud_admin_audit_created_index", "admin_audit_event_created_index"]];
|
|
1663
|
-
async function rename(database, direction) {
|
|
1664
|
-
const forward = direction === "up";
|
|
1665
|
-
if (forward) for (const [from, to] of TABLES) await database.schema.alterTable(from).renameTo(to).execute();
|
|
1666
|
-
for (const [table, from, to] of CONSTRAINTS) await database.schema.alterTable(table).renameConstraint(forward ? from : to, forward ? to : from).execute();
|
|
1667
|
-
for (const [from, to] of INDEXES) {
|
|
1668
|
-
const [source, target] = forward ? [from, to] : [to, from];
|
|
1669
|
-
await sql`alter index ${sql.id(source)} rename to ${sql.id(target)}`.execute(database);
|
|
1670
|
-
}
|
|
1671
|
-
if (!forward) for (const [from, to] of TABLES) await database.schema.alterTable(to).renameTo(from).execute();
|
|
1672
|
-
}
|
|
1673
1526
|
async function up$2(database) {
|
|
1674
|
-
await
|
|
1527
|
+
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();
|
|
1528
|
+
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();
|
|
1529
|
+
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();
|
|
1530
|
+
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();
|
|
1531
|
+
await addChecks(database, [
|
|
1532
|
+
[
|
|
1533
|
+
"transactional_email",
|
|
1534
|
+
"kind",
|
|
1535
|
+
[
|
|
1536
|
+
"document-invitation",
|
|
1537
|
+
"workspace-invitation",
|
|
1538
|
+
"enrollment-requested",
|
|
1539
|
+
"admin-enrollment-notification",
|
|
1540
|
+
"enrollment-approved",
|
|
1541
|
+
"enrollment-rejected",
|
|
1542
|
+
"enrollment-revoked",
|
|
1543
|
+
"email-verification",
|
|
1544
|
+
"password-reset",
|
|
1545
|
+
"password-changed",
|
|
1546
|
+
"email-change-requested",
|
|
1547
|
+
"new-sign-in",
|
|
1548
|
+
"two-step-changed",
|
|
1549
|
+
"document-report"
|
|
1550
|
+
]
|
|
1551
|
+
],
|
|
1552
|
+
[
|
|
1553
|
+
"transactional_email",
|
|
1554
|
+
"status",
|
|
1555
|
+
[
|
|
1556
|
+
"pending",
|
|
1557
|
+
"sending",
|
|
1558
|
+
"accepted",
|
|
1559
|
+
"failed",
|
|
1560
|
+
"suppressed"
|
|
1561
|
+
]
|
|
1562
|
+
],
|
|
1563
|
+
[
|
|
1564
|
+
"enrollment",
|
|
1565
|
+
"status",
|
|
1566
|
+
[
|
|
1567
|
+
"pending",
|
|
1568
|
+
"approved",
|
|
1569
|
+
"rejected",
|
|
1570
|
+
"revoked"
|
|
1571
|
+
]
|
|
1572
|
+
],
|
|
1573
|
+
[
|
|
1574
|
+
"document_report",
|
|
1575
|
+
"reason",
|
|
1576
|
+
[
|
|
1577
|
+
"spam-phishing",
|
|
1578
|
+
"illegal",
|
|
1579
|
+
"copyright",
|
|
1580
|
+
"harassment",
|
|
1581
|
+
"other"
|
|
1582
|
+
]
|
|
1583
|
+
],
|
|
1584
|
+
[
|
|
1585
|
+
"document_report",
|
|
1586
|
+
"status",
|
|
1587
|
+
[
|
|
1588
|
+
"open",
|
|
1589
|
+
"dismissed",
|
|
1590
|
+
"actioned"
|
|
1591
|
+
]
|
|
1592
|
+
]
|
|
1593
|
+
]);
|
|
1594
|
+
await addIndexes(database, [
|
|
1595
|
+
["transactional_email", ["status", "next_attempt_at"]],
|
|
1596
|
+
["enrollment", ["status", "requested_at"]],
|
|
1597
|
+
["document_report", ["document_id"]],
|
|
1598
|
+
["document_report", ["share_id", "created_at"]],
|
|
1599
|
+
["document_report", ["status", "created_at"]],
|
|
1600
|
+
["admin_audit_event", ["created_at"]]
|
|
1601
|
+
]);
|
|
1675
1602
|
}
|
|
1676
1603
|
async function down$2(database) {
|
|
1677
|
-
await
|
|
1604
|
+
await dropTables(database, TABLES);
|
|
1678
1605
|
}
|
|
1679
1606
|
//#endregion
|
|
1680
|
-
//#region src/server/db/migrations/
|
|
1681
|
-
var
|
|
1607
|
+
//#region src/server/db/migrations/007_trash.ts
|
|
1608
|
+
var _007_trash_exports = /* @__PURE__ */ __exportAll({
|
|
1682
1609
|
down: () => down$1,
|
|
1683
1610
|
up: () => up$1
|
|
1684
1611
|
});
|
|
1685
1612
|
/**
|
|
1686
|
-
*
|
|
1687
|
-
*
|
|
1613
|
+
* Trash: who deleted a document, and who deleted it forever, when, and the storage that took off
|
|
1614
|
+
* its workspace's usage while its files wait out the grace before cleanup destroys them.
|
|
1688
1615
|
*/
|
|
1689
1616
|
async function up$1(database) {
|
|
1690
|
-
await database.schema.
|
|
1617
|
+
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();
|
|
1618
|
+
await addIndexes(database, [["document", ["workspace_id", "deleted_at desc"]], ["document", ["purged_at"]]]);
|
|
1691
1619
|
}
|
|
1692
1620
|
async function down$1(database) {
|
|
1693
|
-
await database.schema.
|
|
1621
|
+
await database.schema.dropIndex("document_purged_at_idx").execute();
|
|
1622
|
+
await database.schema.dropIndex("document_workspace_id_deleted_at_idx").execute();
|
|
1623
|
+
await database.schema.alterTable("document").dropColumn("purged_bytes").dropColumn("purged_by").dropColumn("purged_at").dropColumn("deleted_by").execute();
|
|
1694
1624
|
}
|
|
1695
1625
|
//#endregion
|
|
1696
|
-
//#region src/server/db/migrations/
|
|
1697
|
-
var
|
|
1626
|
+
//#region src/server/db/migrations/008_terms.ts
|
|
1627
|
+
var _008_terms_exports = /* @__PURE__ */ __exportAll({
|
|
1698
1628
|
down: () => down,
|
|
1699
1629
|
up: () => up
|
|
1700
1630
|
});
|
|
1701
|
-
/**
|
|
1702
|
-
* Whether each document is a Figma design or a FigJam board, so lists can tell them apart
|
|
1703
|
-
* without opening the file. Documents stored before this are designs; a board's next save says so.
|
|
1704
|
-
*/
|
|
1631
|
+
/** Which version of the deployment's terms each account accepted, and when. */
|
|
1705
1632
|
async function up(database) {
|
|
1706
|
-
await database.schema.
|
|
1707
|
-
await addCheckConstraint(database, "document", "document_kind_check", "kind", ["design", "board"]);
|
|
1633
|
+
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();
|
|
1708
1634
|
}
|
|
1709
1635
|
async function down(database) {
|
|
1710
|
-
await database.schema.
|
|
1636
|
+
await database.schema.dropTable("terms_acceptance").execute();
|
|
1711
1637
|
}
|
|
1712
1638
|
//#endregion
|
|
1713
1639
|
//#region src/server/db/migrate.ts
|
|
1714
1640
|
const migrations = {
|
|
1715
|
-
"
|
|
1716
|
-
"
|
|
1717
|
-
"
|
|
1718
|
-
"
|
|
1719
|
-
"
|
|
1720
|
-
"
|
|
1721
|
-
"
|
|
1722
|
-
"
|
|
1723
|
-
"009_workspace_entitlements": _009_workspace_entitlements_exports,
|
|
1724
|
-
"010_upload_finalization": _010_upload_finalization_exports,
|
|
1725
|
-
"011_upload_finalization_lease": _011_upload_finalization_lease_exports,
|
|
1726
|
-
"012_transactional_email": _012_transactional_email_exports,
|
|
1727
|
-
"013_admin_enrollment": _013_admin_enrollment_exports,
|
|
1728
|
-
"014_enrollment_email_kinds": _014_enrollment_email_kinds_exports,
|
|
1729
|
-
"015_cloud_rate_limit": _015_cloud_rate_limit_exports,
|
|
1730
|
-
"016_auth_schema": _016_auth_schema_exports,
|
|
1731
|
-
"017_authentication_email_kinds": _017_authentication_email_kinds_exports,
|
|
1732
|
-
"018_authentication_mfa": _018_authentication_mfa_exports,
|
|
1733
|
-
"019_better_auth_account_issuer": _019_better_auth_account_issuer_exports,
|
|
1734
|
-
"020_workspace_invitations": _020_workspace_invitations_exports,
|
|
1735
|
-
"021_document_versions": _021_document_versions_exports,
|
|
1736
|
-
"022_entitlement_external_version": _022_entitlement_external_version_exports,
|
|
1737
|
-
"023_document_images": _023_document_images_exports,
|
|
1738
|
-
"024_account_activity": _024_account_activity_exports,
|
|
1739
|
-
"025_table_names": _025_table_names_exports,
|
|
1740
|
-
"026_user_picture": _026_user_picture_exports,
|
|
1741
|
-
"027_document_kind": _027_document_kind_exports
|
|
1641
|
+
"001_accounts": _001_accounts_exports,
|
|
1642
|
+
"002_workspaces": _002_workspaces_exports,
|
|
1643
|
+
"003_storage": _003_storage_exports,
|
|
1644
|
+
"004_documents": _004_documents_exports,
|
|
1645
|
+
"005_sharing": _005_sharing_exports,
|
|
1646
|
+
"006_operations": _006_operations_exports,
|
|
1647
|
+
"007_trash": _007_trash_exports,
|
|
1648
|
+
"008_terms": _008_terms_exports
|
|
1742
1649
|
};
|
|
1650
|
+
/** Cloud's migrations, in order. */
|
|
1743
1651
|
var CloudMigrationProvider = class {
|
|
1744
1652
|
async getMigrations() {
|
|
1745
1653
|
return migrations;
|
|
1746
1654
|
}
|
|
1747
1655
|
};
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
"email_verified",
|
|
1754
|
-
"created_at",
|
|
1755
|
-
"updated_at",
|
|
1756
|
-
"role",
|
|
1757
|
-
"banned",
|
|
1758
|
-
"two_factor_enabled"
|
|
1759
|
-
],
|
|
1760
|
-
session: [
|
|
1761
|
-
"id",
|
|
1762
|
-
"expires_at",
|
|
1763
|
-
"token",
|
|
1764
|
-
"user_id",
|
|
1765
|
-
"impersonated_by"
|
|
1766
|
-
],
|
|
1767
|
-
account: [
|
|
1768
|
-
"id",
|
|
1769
|
-
"account_id",
|
|
1770
|
-
"provider_id",
|
|
1771
|
-
"user_id"
|
|
1772
|
-
],
|
|
1773
|
-
verification: [
|
|
1774
|
-
"id",
|
|
1775
|
-
"identifier",
|
|
1776
|
-
"value",
|
|
1777
|
-
"expires_at"
|
|
1778
|
-
],
|
|
1779
|
-
device_code: [
|
|
1780
|
-
"id",
|
|
1781
|
-
"device_code",
|
|
1782
|
-
"user_code",
|
|
1783
|
-
"status",
|
|
1784
|
-
"client_id"
|
|
1785
|
-
],
|
|
1786
|
-
rate_limit: [
|
|
1787
|
-
"id",
|
|
1788
|
-
"key",
|
|
1789
|
-
"count",
|
|
1790
|
-
"last_request"
|
|
1791
|
-
],
|
|
1792
|
-
two_factor: [
|
|
1793
|
-
"id",
|
|
1794
|
-
"secret",
|
|
1795
|
-
"backup_codes",
|
|
1796
|
-
"user_id",
|
|
1797
|
-
"verified",
|
|
1798
|
-
"failed_verification_count",
|
|
1799
|
-
"locked_until"
|
|
1800
|
-
],
|
|
1801
|
-
passkey: [
|
|
1802
|
-
"id",
|
|
1803
|
-
"public_key",
|
|
1804
|
-
"user_id",
|
|
1805
|
-
"credential_id",
|
|
1806
|
-
"counter",
|
|
1807
|
-
"device_type",
|
|
1808
|
-
"backed_up"
|
|
1809
|
-
]
|
|
1810
|
-
};
|
|
1811
|
-
async function existingAuthSchema(database) {
|
|
1812
|
-
const tables = await database.introspection.getTables();
|
|
1813
|
-
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))]));
|
|
1814
|
-
}
|
|
1656
|
+
/**
|
|
1657
|
+
* Better Auth's tables come from its own migration API, once, after Cloud's: the version they
|
|
1658
|
+
* were built for is recorded, and a different one stops startup until someone migrates it
|
|
1659
|
+
* deliberately, since Better Auth cannot say what an upgrade between its versions changes.
|
|
1660
|
+
*/
|
|
1815
1661
|
async function migrateAuthSchema(database, migration) {
|
|
1816
1662
|
const recorded = await database.selectFrom("authSchemaVersion").select("version").where("id", "=", "better-auth").executeTakeFirst();
|
|
1817
1663
|
if (recorded) {
|
|
1818
1664
|
if (recorded.version !== migration.schemaVersion) throw new Error(`Better Auth schema ${recorded.version} requires an explicit migration to ${migration.schemaVersion}`);
|
|
1819
1665
|
return;
|
|
1820
1666
|
}
|
|
1821
|
-
|
|
1822
|
-
if (existing.size === 0) await migration.run();
|
|
1823
|
-
else {
|
|
1824
|
-
const missing = Object.entries(REQUIRED_AUTH_SCHEMA).flatMap(([table, requiredColumns]) => {
|
|
1825
|
-
const columns = existing.get(table);
|
|
1826
|
-
if (!columns) return [table];
|
|
1827
|
-
return requiredColumns.filter((column) => !columns.has(column)).map((column) => `${table}.${column}`);
|
|
1828
|
-
});
|
|
1829
|
-
if (missing.length > 0) throw new Error(`Existing Better Auth schema is incomplete: ${missing.join(", ")}`);
|
|
1830
|
-
}
|
|
1667
|
+
await migration.run();
|
|
1831
1668
|
await database.insertInto("authSchemaVersion").values({
|
|
1832
1669
|
id: "better-auth",
|
|
1833
1670
|
version: migration.schemaVersion,
|
|
@@ -1876,6 +1713,6 @@ async function listen(fetch, port, hostname) {
|
|
|
1876
1713
|
};
|
|
1877
1714
|
}
|
|
1878
1715
|
//#endregion
|
|
1879
|
-
export {
|
|
1716
|
+
export { CLOUD_API_MAX_BODY_BYTES, CLOUD_DISCOVERY_PATH, 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, cloudDeploymentConfigSchema, createRelayClientAssembler, decodeRelayClientFrame, decodeRoomUpdate, defaultRelayURL, documentDownloadSchema, documentInvitationParams, documentParams, documentPermissionSchema, documentShareParams, documentSummarySchema, documentUserParams, documentVersionParams, encodeSyncReply, httpURLSchema, idParams, invitationContinuationParams, invitationParams, listen, lockForTransaction, migrateCloudDatabase, migrateToLatest, parseCloudDiscovery, parseCloudServerConfig, parseCommitImageUploads, parseCommitUpload, parseCreateDocument, parseCreateServicesToken, parseCreateUpload, parseRequestImageLinks, parseRequestImageUploads, parseUsageQuery, pictureParams, presenceName, relayServerMessages, requestImageLinksSchema, serviceCatalogSchema, servicesTokenClaimsSchema, shareParams, staticEntitlementsSchema, uploadParams, validatedJSON, validatedQuery, workspaceInvitationParams, workspaceMemberParams, workspaceParams };
|
|
1880
1717
|
|
|
1881
1718
|
//# sourceMappingURL=listen.mjs.map
|