@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/services.mjs +1 -1
- package/dist/bin/services.mjs.map +1 -1
- package/dist/client.d.ts +296 -27
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -295
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +331 -21
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1773 -32
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3478 -288
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +466 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +447 -290
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +2963 -294
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +3473 -842
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12187 -8244
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +7739 -3548
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +5 -5
- package/dist/server2.mjs.map +1 -1
- package/package.json +22 -5
- package/src/admin/audit/service.ts +2 -2
- package/src/admin/documents/service.ts +76 -6
- package/src/admin/reports/service.ts +7 -1
- package/src/client/api.ts +23 -471
- package/src/client/auth/client.ts +22 -3
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +7 -0
- package/src/client/portal/account.ts +54 -2
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +23 -18
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +50 -0
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +5 -0
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +63 -2
- package/src/contract/entitlements.ts +10 -0
- package/src/contract/index.ts +54 -5
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +18 -16
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +142 -276
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +70 -6
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/relay/index.ts +7 -1
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +8 -1
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/server.ts +12 -2
- package/src/runtime/node/services/main.ts +1 -1
- package/src/runtime/posthog/errors.ts +2 -1
- package/src/runtime/s3/objects.ts +17 -0
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +17 -1
- package/src/server/account/deletion.ts +2 -0
- package/src/server/account/routes.ts +271 -240
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +23 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +21 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +88 -131
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +16 -3
- package/src/server/cleanup/worker.ts +10 -1
- package/src/server/collaboration/relay/hub.ts +25 -142
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +21 -2
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +17 -5
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +7 -1
- package/src/server/config/schema.ts +40 -292
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +13 -1
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +26 -1
- package/src/server/db/tables.ts +30 -1
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +7 -1
- package/src/server/documents/service.ts +12 -402
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/service.ts +81 -24
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +1 -9
- package/src/server/index.ts +12 -5
- package/src/server/invitations/types.ts +5 -1
- package/src/server/objects/types.ts +5 -0
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +13 -2
- package/src/server/policy/policy.ts +39 -12
- package/src/server/policy/service.ts +71 -52
- package/src/server/policy/storage.ts +33 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/storage.ts +5 -0
- package/src/server/rate-limit/middleware.ts +4 -0
- package/src/server/reports/index.ts +1 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +3 -1
- package/src/server/reports/service.ts +24 -2
- package/src/server/sharing/capabilities/service.ts +2 -3
- package/src/server/sharing/invitations/service.ts +14 -4
- package/src/server/validation.ts +3 -0
- package/src/server/workspaces/members.ts +7 -19
- package/src/services/app.ts +1 -3
- package/src/services/config.ts +1 -0
- package/src/services/usage.ts +6 -14
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/app.ts +0 -453
- /package/src/{server/errors → errors}/posthog.ts +0 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { cloudEditorVersionSchema } from '#cloud/contract'
|
|
2
|
+
import { deletedPrefixSchema } from '#cloud/server/config/schema'
|
|
3
|
+
import { staticEntitlementsTOMLSchema } from '#cloud/server/policy/schema'
|
|
4
|
+
import * as v from 'valibot'
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* The deployment file, `openpencil-cloud.toml`: what an operator writes, with secrets named by the
|
|
8
|
+
* environment variables that hold them. `../deployment.ts` reads it into the server's configuration.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** PostHog's EU Cloud ingestion host, where OpenPencil's own project is. */
|
|
12
|
+
const POSTHOG_DEFAULT_HOST = 'https://eu.i.posthog.com'
|
|
13
|
+
|
|
14
|
+
const DEFAULT_SECRET_NAMES = {
|
|
15
|
+
databaseURL: 'DATABASE_URL',
|
|
16
|
+
authSecret: 'BETTER_AUTH_SECRET',
|
|
17
|
+
googleClientId: 'GOOGLE_CLIENT_ID',
|
|
18
|
+
googleClientSecret: 'GOOGLE_CLIENT_SECRET',
|
|
19
|
+
applePrivateKey: 'APPLE_PRIVATE_KEY',
|
|
20
|
+
s3AccessKeyId: 'S3_ACCESS_KEY_ID',
|
|
21
|
+
s3SecretAccessKey: 'S3_SECRET_ACCESS_KEY',
|
|
22
|
+
s3SessionToken: 'S3_SESSION_TOKEN',
|
|
23
|
+
smtpUser: 'OPENPENCIL_CLOUD_SMTP_USER',
|
|
24
|
+
smtpPassword: 'OPENPENCIL_CLOUD_SMTP_PASSWORD',
|
|
25
|
+
turnstileSecretKey: 'TURNSTILE_SECRET_KEY',
|
|
26
|
+
servicesSigningKey: 'OPENPENCIL_CLOUD_SERVICES_SIGNING_KEY',
|
|
27
|
+
internalServiceToken: 'OPENPENCIL_CLOUD_SERVICE_TOKEN',
|
|
28
|
+
posthogProjectAPIKey: 'POSTHOG_PROJECT_API_KEY'
|
|
29
|
+
} as const
|
|
30
|
+
|
|
31
|
+
const urlSchema = v.pipe(v.string(), v.url())
|
|
32
|
+
const textSchema = v.pipe(v.string(), v.trim(), v.minLength(1))
|
|
33
|
+
const positiveIntegerSchema = v.pipe(v.number(), v.integer(), v.minValue(1))
|
|
34
|
+
const secretReferenceSchema = v.strictObject({ from_env: textSchema })
|
|
35
|
+
const configurableTextSchema = v.union([textSchema, secretReferenceSchema])
|
|
36
|
+
const defaultReference = (name: string) => v.optional(secretReferenceSchema, { from_env: name })
|
|
37
|
+
|
|
38
|
+
const providerSchema = v.strictObject({
|
|
39
|
+
google: v.optional(
|
|
40
|
+
v.strictObject({
|
|
41
|
+
client_id: v.optional(configurableTextSchema, {
|
|
42
|
+
from_env: DEFAULT_SECRET_NAMES.googleClientId
|
|
43
|
+
}),
|
|
44
|
+
client_secret: defaultReference(DEFAULT_SECRET_NAMES.googleClientSecret)
|
|
45
|
+
})
|
|
46
|
+
),
|
|
47
|
+
apple: v.optional(
|
|
48
|
+
v.strictObject({
|
|
49
|
+
client_id: textSchema,
|
|
50
|
+
team_id: textSchema,
|
|
51
|
+
key_id: textSchema,
|
|
52
|
+
private_key: defaultReference(DEFAULT_SECRET_NAMES.applePrivateKey),
|
|
53
|
+
app_bundle_identifier: v.optional(textSchema)
|
|
54
|
+
})
|
|
55
|
+
)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
const emailSchema = v.optional(
|
|
59
|
+
v.variant('transport', [
|
|
60
|
+
v.strictObject({ transport: v.literal('none') }),
|
|
61
|
+
v.strictObject({
|
|
62
|
+
transport: v.literal('smtp'),
|
|
63
|
+
from: textSchema,
|
|
64
|
+
smtp: v.strictObject({
|
|
65
|
+
host: textSchema,
|
|
66
|
+
port: positiveIntegerSchema,
|
|
67
|
+
secure: v.optional(v.boolean()),
|
|
68
|
+
user: v.optional(secretReferenceSchema, {
|
|
69
|
+
from_env: DEFAULT_SECRET_NAMES.smtpUser
|
|
70
|
+
}),
|
|
71
|
+
password: v.optional(secretReferenceSchema, {
|
|
72
|
+
from_env: DEFAULT_SECRET_NAMES.smtpPassword
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
}),
|
|
76
|
+
v.strictObject({ transport: v.literal('cloudflare'), from: textSchema })
|
|
77
|
+
]),
|
|
78
|
+
{ transport: 'none' }
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Every table is strict: a key the schema doesn't know, such as one renamed since, stops startup
|
|
83
|
+
* instead of leaving its setting at a default the operator never chose.
|
|
84
|
+
*/
|
|
85
|
+
export const cloudDeploymentConfigSchema = v.strictObject({
|
|
86
|
+
schema_version: v.literal(2),
|
|
87
|
+
deployment: v.strictObject({
|
|
88
|
+
mode: v.picklist(['official', 'self-hosted']),
|
|
89
|
+
public_url: urlSchema,
|
|
90
|
+
indexing: v.optional(v.picklist(['allow', 'deny']), 'deny'),
|
|
91
|
+
trusted_origins: v.optional(v.array(urlSchema), []),
|
|
92
|
+
// The server's editor works with files on this device without signing in to the server.
|
|
93
|
+
local_editor: v.optional(v.boolean(), true),
|
|
94
|
+
listing: v.optional(v.strictObject({ minimum_editor_version: cloudEditorVersionSchema })),
|
|
95
|
+
// How a self-hosted server names itself on its public pages.
|
|
96
|
+
branding: v.optional(
|
|
97
|
+
v.strictObject({
|
|
98
|
+
name: v.optional(v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(80))),
|
|
99
|
+
logo_url: v.optional(urlSchema),
|
|
100
|
+
help_url: v.optional(urlSchema)
|
|
101
|
+
})
|
|
102
|
+
),
|
|
103
|
+
// Older editors ask to be updated, and the server refuses their requests.
|
|
104
|
+
minimum_editor_version: v.optional(cloudEditorVersionSchema)
|
|
105
|
+
}),
|
|
106
|
+
database: v.optional(
|
|
107
|
+
v.strictObject({ url: defaultReference(DEFAULT_SECRET_NAMES.databaseURL) }),
|
|
108
|
+
{
|
|
109
|
+
url: { from_env: DEFAULT_SECRET_NAMES.databaseURL }
|
|
110
|
+
}
|
|
111
|
+
),
|
|
112
|
+
authentication: v.strictObject({
|
|
113
|
+
secret: defaultReference(DEFAULT_SECRET_NAMES.authSecret),
|
|
114
|
+
enrollment: v.optional(
|
|
115
|
+
v.strictObject({
|
|
116
|
+
mode: v.optional(v.picklist(['open', 'approval', 'closed']), 'open'),
|
|
117
|
+
notification_emails: v.optional(v.array(v.pipe(v.string(), v.email())), [])
|
|
118
|
+
}),
|
|
119
|
+
{ mode: 'open', notification_emails: [] }
|
|
120
|
+
),
|
|
121
|
+
email_password: v.optional(
|
|
122
|
+
v.strictObject({
|
|
123
|
+
enabled: v.optional(v.boolean(), false),
|
|
124
|
+
sign_up: v.optional(v.boolean(), true),
|
|
125
|
+
minimum_password_length: v.optional(
|
|
126
|
+
v.pipe(v.number(), v.integer(), v.minValue(8), v.maxValue(128)),
|
|
127
|
+
8
|
|
128
|
+
),
|
|
129
|
+
maximum_password_length: v.optional(
|
|
130
|
+
v.pipe(v.number(), v.integer(), v.minValue(15), v.maxValue(128)),
|
|
131
|
+
128
|
|
132
|
+
),
|
|
133
|
+
verification_link_lifetime_minutes: v.optional(positiveIntegerSchema, 60),
|
|
134
|
+
password_reset_link_lifetime_minutes: v.optional(positiveIntegerSchema, 60),
|
|
135
|
+
compromised_password_check: v.optional(v.boolean(), false)
|
|
136
|
+
}),
|
|
137
|
+
{
|
|
138
|
+
enabled: false,
|
|
139
|
+
sign_up: true,
|
|
140
|
+
minimum_password_length: 8,
|
|
141
|
+
maximum_password_length: 128,
|
|
142
|
+
verification_link_lifetime_minutes: 60,
|
|
143
|
+
password_reset_link_lifetime_minutes: 60,
|
|
144
|
+
compromised_password_check: false
|
|
145
|
+
}
|
|
146
|
+
),
|
|
147
|
+
mfa: v.optional(
|
|
148
|
+
v.strictObject({
|
|
149
|
+
required_for_admins: v.optional(v.boolean(), false),
|
|
150
|
+
totp: v.optional(v.boolean(), false),
|
|
151
|
+
recovery_codes: v.optional(v.boolean(), false),
|
|
152
|
+
trusted_device_lifetime_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 14)
|
|
153
|
+
}),
|
|
154
|
+
{
|
|
155
|
+
required_for_admins: false,
|
|
156
|
+
totp: false,
|
|
157
|
+
recovery_codes: false,
|
|
158
|
+
trusted_device_lifetime_days: 14
|
|
159
|
+
}
|
|
160
|
+
),
|
|
161
|
+
// Passkeys belong to public_url's host and origin.
|
|
162
|
+
passkeys: v.optional(
|
|
163
|
+
v.strictObject({
|
|
164
|
+
enabled: v.optional(v.boolean(), false),
|
|
165
|
+
rp_name: v.optional(textSchema, 'OpenPencil Cloud')
|
|
166
|
+
}),
|
|
167
|
+
{ enabled: false, rp_name: 'OpenPencil Cloud' }
|
|
168
|
+
),
|
|
169
|
+
captcha: v.optional(
|
|
170
|
+
v.strictObject({
|
|
171
|
+
provider: v.literal('cloudflare-turnstile'),
|
|
172
|
+
site_key: textSchema,
|
|
173
|
+
secret_key: defaultReference(DEFAULT_SECRET_NAMES.turnstileSecretKey),
|
|
174
|
+
allowed_hostnames: v.optional(v.array(textSchema), [])
|
|
175
|
+
})
|
|
176
|
+
),
|
|
177
|
+
trusted_proxies: v.optional(
|
|
178
|
+
v.strictObject({
|
|
179
|
+
headers: v.optional(v.array(textSchema), []),
|
|
180
|
+
addresses: v.optional(v.array(textSchema), [])
|
|
181
|
+
}),
|
|
182
|
+
{ headers: [], addresses: [] }
|
|
183
|
+
),
|
|
184
|
+
providers: v.optional(providerSchema, {})
|
|
185
|
+
}),
|
|
186
|
+
object_storage: v.strictObject({
|
|
187
|
+
endpoint: urlSchema,
|
|
188
|
+
region: textSchema,
|
|
189
|
+
bucket: textSchema,
|
|
190
|
+
credentials: v.optional(
|
|
191
|
+
v.strictObject({
|
|
192
|
+
access_key_id: defaultReference(DEFAULT_SECRET_NAMES.s3AccessKeyId),
|
|
193
|
+
secret_access_key: defaultReference(DEFAULT_SECRET_NAMES.s3SecretAccessKey),
|
|
194
|
+
session_token: v.optional(secretReferenceSchema, {
|
|
195
|
+
from_env: DEFAULT_SECRET_NAMES.s3SessionToken
|
|
196
|
+
})
|
|
197
|
+
}),
|
|
198
|
+
{
|
|
199
|
+
access_key_id: { from_env: DEFAULT_SECRET_NAMES.s3AccessKeyId },
|
|
200
|
+
secret_access_key: { from_env: DEFAULT_SECRET_NAMES.s3SecretAccessKey }
|
|
201
|
+
}
|
|
202
|
+
),
|
|
203
|
+
force_path_style: v.optional(v.boolean(), true),
|
|
204
|
+
checksum_verification: v.optional(v.picklist(['native', 'metadata']), 'native'),
|
|
205
|
+
server_side_encryption: v.optional(v.picklist(['AES256', 'aws:kms'])),
|
|
206
|
+
kms_key_id: v.optional(textSchema),
|
|
207
|
+
/**
|
|
208
|
+
* Keeps each file cleanup deletes under this prefix, such as `deleted/`, so a database restored
|
|
209
|
+
* to an earlier time still finds it; a lifecycle rule on the prefix expires them.
|
|
210
|
+
*/
|
|
211
|
+
deleted_prefix: v.optional(deletedPrefixSchema),
|
|
212
|
+
maximum_upload_bytes: v.optional(positiveIntegerSchema)
|
|
213
|
+
}),
|
|
214
|
+
collaboration: v.optional(
|
|
215
|
+
v.strictObject({
|
|
216
|
+
relay_url: v.optional(urlSchema),
|
|
217
|
+
maximum_message_bytes: v.optional(positiveIntegerSchema),
|
|
218
|
+
maximum_connections_per_room: v.optional(positiveIntegerSchema)
|
|
219
|
+
}),
|
|
220
|
+
{}
|
|
221
|
+
),
|
|
222
|
+
documents: v.optional(
|
|
223
|
+
v.strictObject({
|
|
224
|
+
retention_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 30),
|
|
225
|
+
purge_grace_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 7)
|
|
226
|
+
}),
|
|
227
|
+
{ retention_days: 30, purge_grace_days: 7 }
|
|
228
|
+
),
|
|
229
|
+
email: emailSchema,
|
|
230
|
+
workers: v.optional(
|
|
231
|
+
v.strictObject({
|
|
232
|
+
email: v.optional(
|
|
233
|
+
v.strictObject({
|
|
234
|
+
batch_size: v.optional(positiveIntegerSchema, 50),
|
|
235
|
+
interval_ms: v.optional(positiveIntegerSchema, 30_000),
|
|
236
|
+
lease_ms: v.optional(positiveIntegerSchema, 300_000),
|
|
237
|
+
maximum_attempts: v.optional(positiveIntegerSchema, 5)
|
|
238
|
+
}),
|
|
239
|
+
{ batch_size: 50, interval_ms: 30_000, lease_ms: 300_000, maximum_attempts: 5 }
|
|
240
|
+
),
|
|
241
|
+
cleanup: v.optional(
|
|
242
|
+
v.strictObject({
|
|
243
|
+
enabled: v.optional(v.boolean(), true),
|
|
244
|
+
batch_size: v.optional(positiveIntegerSchema, 100),
|
|
245
|
+
interval_ms: v.optional(positiveIntegerSchema, 60_000),
|
|
246
|
+
lease_ms: v.optional(positiveIntegerSchema, 300_000)
|
|
247
|
+
}),
|
|
248
|
+
{ enabled: true, batch_size: 100, interval_ms: 60_000, lease_ms: 300_000 }
|
|
249
|
+
)
|
|
250
|
+
})
|
|
251
|
+
),
|
|
252
|
+
entitlements: v.optional(
|
|
253
|
+
v.variant('source', [
|
|
254
|
+
v.strictObject({ source: v.literal('static'), ...staticEntitlementsTOMLSchema.entries }),
|
|
255
|
+
// The values every account takes until billing or an operator stores its own: the free plan.
|
|
256
|
+
v.strictObject({ source: v.literal('database'), ...staticEntitlementsTOMLSchema.entries })
|
|
257
|
+
])
|
|
258
|
+
),
|
|
259
|
+
services: v.optional(
|
|
260
|
+
v.strictObject({
|
|
261
|
+
endpoint: urlSchema,
|
|
262
|
+
signing_key: defaultReference(DEFAULT_SECRET_NAMES.servicesSigningKey),
|
|
263
|
+
token_lifetime_minutes: v.optional(
|
|
264
|
+
v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(60)),
|
|
265
|
+
10
|
|
266
|
+
)
|
|
267
|
+
})
|
|
268
|
+
),
|
|
269
|
+
billing: v.optional(v.strictObject({ endpoint: urlSchema })),
|
|
270
|
+
internal_api: v.optional(
|
|
271
|
+
v.strictObject({ service_token: defaultReference(DEFAULT_SECRET_NAMES.internalServiceToken) })
|
|
272
|
+
),
|
|
273
|
+
/**
|
|
274
|
+
* Optional: the operator's terms and privacy policy, linked at sign-up and in the portal, and
|
|
275
|
+
* with `require_acceptance`, accepted once per `terms_version` before an account uses the server.
|
|
276
|
+
*/
|
|
277
|
+
legal: v.optional(
|
|
278
|
+
v.strictObject({
|
|
279
|
+
terms_url: v.optional(urlSchema),
|
|
280
|
+
privacy_url: v.optional(urlSchema),
|
|
281
|
+
terms_version: v.optional(textSchema),
|
|
282
|
+
require_acceptance: v.optional(v.boolean(), false),
|
|
283
|
+
abuse_email: v.optional(v.pipe(v.string(), v.email())),
|
|
284
|
+
minimum_age: v.optional(v.pipe(v.number(), v.integer(), v.minValue(13), v.maxValue(21)))
|
|
285
|
+
})
|
|
286
|
+
),
|
|
287
|
+
/**
|
|
288
|
+
* Optional: a workspace whose documents anyone may open read-only, without signing in or a
|
|
289
|
+
* link, as the deployment's community gallery. Everything saved there is public.
|
|
290
|
+
*/
|
|
291
|
+
community: v.optional(v.strictObject({ workspace: v.pipe(v.string(), v.uuid()) })),
|
|
292
|
+
/** Optional: reports server errors to PostHog error tracking. Leave it out to send nothing. */
|
|
293
|
+
error_tracking: v.optional(
|
|
294
|
+
v.strictObject({
|
|
295
|
+
provider: v.literal('posthog'),
|
|
296
|
+
host: v.optional(urlSchema, POSTHOG_DEFAULT_HOST),
|
|
297
|
+
project_api_key: v.optional(configurableTextSchema, {
|
|
298
|
+
from_env: DEFAULT_SECRET_NAMES.posthogProjectAPIKey
|
|
299
|
+
})
|
|
300
|
+
})
|
|
301
|
+
)
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
export type CloudDeploymentConfig = v.InferOutput<typeof cloudDeploymentConfigSchema>
|
|
305
|
+
/** Deployment configuration as an operator writes it, before defaults are applied. */
|
|
306
|
+
export type CloudDeploymentInput = v.InferInput<typeof cloudDeploymentConfigSchema>
|
|
307
|
+
export type CloudSecretReference = v.InferOutput<typeof secretReferenceSchema>
|
package/src/server/db/index.ts
CHANGED
package/src/server/db/migrate.ts
CHANGED
|
@@ -9,6 +9,12 @@ import * as sharing from './migrations/005_sharing'
|
|
|
9
9
|
import * as operations from './migrations/006_operations'
|
|
10
10
|
import * as trash from './migrations/007_trash'
|
|
11
11
|
import * as terms from './migrations/008_terms'
|
|
12
|
+
import * as accessTokens from './migrations/009_access_tokens'
|
|
13
|
+
import * as storageLimitNotices from './migrations/010_storage_limit_notices'
|
|
14
|
+
import * as accountIds from './migrations/011_account_ids'
|
|
15
|
+
import * as emailLocales from './migrations/012_email_locales'
|
|
16
|
+
import * as documentBlockEmails from './migrations/013_document_block_emails'
|
|
17
|
+
import * as reportReplies from './migrations/014_report_replies'
|
|
12
18
|
import type { CloudDatabase } from './tables'
|
|
13
19
|
|
|
14
20
|
/**
|
|
@@ -27,7 +33,13 @@ const migrations: Record<string, CloudMigration> = {
|
|
|
27
33
|
'005_sharing': sharing,
|
|
28
34
|
'006_operations': operations,
|
|
29
35
|
'007_trash': trash,
|
|
30
|
-
'008_terms': terms
|
|
36
|
+
'008_terms': terms,
|
|
37
|
+
'009_access_tokens': accessTokens,
|
|
38
|
+
'010_storage_limit_notices': storageLimitNotices,
|
|
39
|
+
'011_account_ids': accountIds,
|
|
40
|
+
'012_email_locales': emailLocales,
|
|
41
|
+
'013_document_block_emails': documentBlockEmails,
|
|
42
|
+
'014_report_replies': reportReplies
|
|
31
43
|
}
|
|
32
44
|
|
|
33
45
|
/** Cloud's migrations, in order. */
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { sql, type Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { addIndexes, required } from './schema'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Personal access tokens, kept by Better Auth's API-key plugin in the table it would build
|
|
7
|
+
* itself. Cloud builds it here instead, since Better Auth's tables are built only once and a
|
|
8
|
+
* deployment that has them gains this one as any other migration; Better Auth's migration then
|
|
9
|
+
* leaves this plugin's table out (`createBetterAuthAdapter`). It holds each token's SHA-256
|
|
10
|
+
* digest, never the token.
|
|
11
|
+
*/
|
|
12
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
13
|
+
await database.schema
|
|
14
|
+
.createTable('apikey')
|
|
15
|
+
.addColumn('id', 'uuid', (column) => column.primaryKey().defaultTo(sql`gen_random_uuid()`))
|
|
16
|
+
.addColumn('config_id', 'text', required)
|
|
17
|
+
.addColumn('name', 'text')
|
|
18
|
+
.addColumn('start', 'text')
|
|
19
|
+
.addColumn('reference_id', 'text', required)
|
|
20
|
+
.addColumn('prefix', 'text')
|
|
21
|
+
.addColumn('key', 'text', required)
|
|
22
|
+
.addColumn('refill_interval', 'integer')
|
|
23
|
+
.addColumn('refill_amount', 'integer')
|
|
24
|
+
.addColumn('last_refill_at', 'timestamptz')
|
|
25
|
+
.addColumn('enabled', 'boolean')
|
|
26
|
+
.addColumn('rate_limit_enabled', 'boolean')
|
|
27
|
+
.addColumn('rate_limit_time_window', 'integer')
|
|
28
|
+
.addColumn('rate_limit_max', 'integer')
|
|
29
|
+
.addColumn('request_count', 'integer')
|
|
30
|
+
.addColumn('remaining', 'integer')
|
|
31
|
+
.addColumn('last_request', 'timestamptz')
|
|
32
|
+
.addColumn('expires_at', 'timestamptz')
|
|
33
|
+
.addColumn('created_at', 'timestamptz', required)
|
|
34
|
+
.addColumn('updated_at', 'timestamptz', required)
|
|
35
|
+
.addColumn('permissions', 'text')
|
|
36
|
+
.addColumn('metadata', 'text')
|
|
37
|
+
.execute()
|
|
38
|
+
await addIndexes(database, [
|
|
39
|
+
['apikey', ['config_id']],
|
|
40
|
+
['apikey', ['reference_id']],
|
|
41
|
+
['apikey', ['key']]
|
|
42
|
+
])
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
46
|
+
await database.schema.dropTable('apikey').execute()
|
|
47
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { addChecks, now } from './schema'
|
|
4
|
+
|
|
5
|
+
/** Every kind of transactional email, as of this migration. */
|
|
6
|
+
const EMAIL_KINDS = [
|
|
7
|
+
'document-invitation',
|
|
8
|
+
'workspace-invitation',
|
|
9
|
+
'enrollment-requested',
|
|
10
|
+
'admin-enrollment-notification',
|
|
11
|
+
'enrollment-approved',
|
|
12
|
+
'enrollment-rejected',
|
|
13
|
+
'enrollment-revoked',
|
|
14
|
+
'email-verification',
|
|
15
|
+
'password-reset',
|
|
16
|
+
'password-changed',
|
|
17
|
+
'email-change-requested',
|
|
18
|
+
'new-sign-in',
|
|
19
|
+
'two-step-changed',
|
|
20
|
+
'document-report'
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The accounts told their storage is full, so each is told once each time it fills, not on every
|
|
25
|
+
* cleanup pass; a row goes once the account holds less than its plan allows again. Email gains
|
|
26
|
+
* the kind that tells them, which the previous release never writes.
|
|
27
|
+
*/
|
|
28
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
29
|
+
await database.schema
|
|
30
|
+
.createTable('storage_limit_notice')
|
|
31
|
+
.addColumn('user_id', 'text', (column) => column.primaryKey())
|
|
32
|
+
.addColumn('notified_at', 'timestamptz', now)
|
|
33
|
+
.execute()
|
|
34
|
+
await database.schema
|
|
35
|
+
.alterTable('transactional_email')
|
|
36
|
+
.dropConstraint('transactional_email_kind_check')
|
|
37
|
+
.execute()
|
|
38
|
+
await addChecks(database, [
|
|
39
|
+
['transactional_email', 'kind', [...EMAIL_KINDS, 'storage-limit-reached']]
|
|
40
|
+
])
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
44
|
+
await database.schema
|
|
45
|
+
.alterTable('transactional_email')
|
|
46
|
+
.dropConstraint('transactional_email_kind_check')
|
|
47
|
+
.execute()
|
|
48
|
+
await addChecks(database, [['transactional_email', 'kind', EMAIL_KINDS]])
|
|
49
|
+
await database.schema.dropTable('storage_limit_notice').execute()
|
|
50
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { sql, type Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
/** The tables that named accounts by `uuid`, unlike every other table Cloud builds. */
|
|
4
|
+
const TABLES = ['mfa_assurance', 'sign_in', 'user_picture']
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Names accounts by `text` in the last tables that used `uuid`, as `schema.ts` sets for every
|
|
8
|
+
* table Cloud builds. The previous release reads and writes the same strings either way, so it
|
|
9
|
+
* keeps working while this runs.
|
|
10
|
+
*/
|
|
11
|
+
export const contracts = TABLES.map((table) => `${table}.user_id`)
|
|
12
|
+
|
|
13
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
14
|
+
for (const table of TABLES) {
|
|
15
|
+
await database.schema
|
|
16
|
+
.alterTable(table)
|
|
17
|
+
.alterColumn('user_id', (column) => column.setDataType('text'))
|
|
18
|
+
.execute()
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
23
|
+
for (const table of TABLES) {
|
|
24
|
+
await database.schema
|
|
25
|
+
.alterTable(table)
|
|
26
|
+
// Text turns into a UUID only when told to, unlike a UUID into text.
|
|
27
|
+
.alterColumn('user_id', (column) => column.setDataType(sql`uuid using user_id::uuid`))
|
|
28
|
+
.execute()
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
/** Whether Better Auth's `user` table exists yet, which it doesn't before a server first starts. */
|
|
4
|
+
async function hasUserTable(database: Kysely<unknown>): Promise<boolean> {
|
|
5
|
+
const tables = await database.introspection.getTables()
|
|
6
|
+
return tables.some((table) => table.name === 'user')
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The language Cloud writes to each account in, and the one each queued email is written in,
|
|
11
|
+
* settled when it's queued so a retry reads the same. Email from before this migration is
|
|
12
|
+
* English, as it always was.
|
|
13
|
+
*
|
|
14
|
+
* `user.locale` is a Better Auth field, so a new database gets it from Better Auth's own schema,
|
|
15
|
+
* built after Cloud's migrations. A database that already has that schema never builds it again,
|
|
16
|
+
* so it gets the column here; accounts from before it have no language and read English.
|
|
17
|
+
*/
|
|
18
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
19
|
+
await database.schema
|
|
20
|
+
.alterTable('transactional_email')
|
|
21
|
+
.addColumn('locale', 'text', (column) => column.notNull().defaultTo('en'))
|
|
22
|
+
.execute()
|
|
23
|
+
if (await hasUserTable(database)) {
|
|
24
|
+
await database.schema
|
|
25
|
+
.alterTable('user')
|
|
26
|
+
.addColumn('locale', 'text', (column) => column.ifNotExists())
|
|
27
|
+
.execute()
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// `user.locale` stays: an earlier release ignores it, and the languages accounts chose aren't lost.
|
|
32
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
33
|
+
await database.schema.alterTable('transactional_email').dropColumn('locale').execute()
|
|
34
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { replaceChecks } from './schema'
|
|
4
|
+
|
|
5
|
+
/** Every kind of transactional email before this migration. */
|
|
6
|
+
const EMAIL_KINDS = [
|
|
7
|
+
'document-invitation',
|
|
8
|
+
'workspace-invitation',
|
|
9
|
+
'enrollment-requested',
|
|
10
|
+
'admin-enrollment-notification',
|
|
11
|
+
'enrollment-approved',
|
|
12
|
+
'enrollment-rejected',
|
|
13
|
+
'enrollment-revoked',
|
|
14
|
+
'email-verification',
|
|
15
|
+
'password-reset',
|
|
16
|
+
'password-changed',
|
|
17
|
+
'email-change-requested',
|
|
18
|
+
'new-sign-in',
|
|
19
|
+
'two-step-changed',
|
|
20
|
+
'document-report',
|
|
21
|
+
'storage-limit-reached'
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
/** Every kind of transactional email once this migration has run. */
|
|
25
|
+
export const DOCUMENT_BLOCK_EMAIL_KINDS = [...EMAIL_KINDS, 'document-blocked', 'document-restored']
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Email gains the kinds that tell a workspace's owner an administrator blocked one of its
|
|
29
|
+
* documents, and that it is back, which the previous release never writes.
|
|
30
|
+
*/
|
|
31
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
32
|
+
await replaceChecks(database, 'widen', [
|
|
33
|
+
['transactional_email', 'kind', DOCUMENT_BLOCK_EMAIL_KINDS]
|
|
34
|
+
])
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Narrowing back checks every row, so a rollback refuses while any email of the new kinds is kept.
|
|
38
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
39
|
+
await replaceChecks(database, 'narrow', [['transactional_email', 'kind', EMAIL_KINDS]])
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import { DOCUMENT_BLOCK_EMAIL_KINDS } from './013_document_block_emails'
|
|
4
|
+
import { replaceChecks } from './schema'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Whoever reports a document and leaves an address hears that the report arrived and what the
|
|
8
|
+
* administrators decided. A report keeps the language its reporter's browser asked for, since a
|
|
9
|
+
* guest has no account to say it, and email gains the two kinds, which the previous release
|
|
10
|
+
* never writes.
|
|
11
|
+
*/
|
|
12
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
13
|
+
await database.schema.alterTable('document_report').addColumn('reply_locale', 'text').execute()
|
|
14
|
+
await replaceChecks(database, 'widen', [
|
|
15
|
+
[
|
|
16
|
+
'transactional_email',
|
|
17
|
+
'kind',
|
|
18
|
+
[...DOCUMENT_BLOCK_EMAIL_KINDS, 'document-report-received', 'document-report-decided']
|
|
19
|
+
]
|
|
20
|
+
])
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
24
|
+
await replaceChecks(database, 'narrow', [
|
|
25
|
+
['transactional_email', 'kind', DOCUMENT_BLOCK_EMAIL_KINDS]
|
|
26
|
+
])
|
|
27
|
+
await database.schema.alterTable('document_report').dropColumn('reply_locale').execute()
|
|
28
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { checkColumnIn, CURRENT_TIMESTAMP } from '#cloud/server/db/expressions'
|
|
2
|
-
import type
|
|
2
|
+
import { sql, type ColumnDefinitionBuilder, type CreateTableBuilder, type Kysely } from 'kysely'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* What Cloud's migrations build tables with. Names follow PostgreSQL's own: `<table>_pkey`,
|
|
@@ -45,6 +45,31 @@ export async function addChecks(
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Replaces the values a column may take, as `addChecks` named them. Widening them, as when a
|
|
50
|
+
* release adds a kind the previous one never writes, adds the check `NOT VALID`: rows already
|
|
51
|
+
* there passed the narrower one, so PostgreSQL skips scanning them while it holds the table, and
|
|
52
|
+
* checks every row written from then on. Narrowing them, as a rollback does, checks every row.
|
|
53
|
+
*/
|
|
54
|
+
export async function replaceChecks(
|
|
55
|
+
database: Kysely<unknown>,
|
|
56
|
+
change: 'widen' | 'narrow',
|
|
57
|
+
checks: readonly (readonly [table: string, column: string, values: readonly string[]])[]
|
|
58
|
+
): Promise<void> {
|
|
59
|
+
for (const [table, column, values] of checks) {
|
|
60
|
+
const name = `${table}_${column}_check`
|
|
61
|
+
await database.schema.alterTable(table).dropConstraint(name).execute()
|
|
62
|
+
if (change === 'narrow') {
|
|
63
|
+
await addChecks(database, [[table, column, values]])
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
// Kysely builds no `NOT VALID`, so this one statement is written out.
|
|
67
|
+
await sql`alter table ${sql.table(table)} add constraint ${sql.id(name)} check (${checkColumnIn(column, values)}) not valid`.execute(
|
|
68
|
+
database
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
48
73
|
/** Lookups beyond primary keys and unique columns, as `[table, columns]`; `desc` sorts newest first. */
|
|
49
74
|
export async function addIndexes(
|
|
50
75
|
database: Kysely<unknown>,
|