@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -1,22 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
Body,
|
|
3
|
-
Button,
|
|
4
|
-
Container,
|
|
5
|
-
Head,
|
|
6
|
-
Heading,
|
|
7
|
-
// eslint-disable-next-line open-pencil/no-mixed-case-acronym-identifiers -- Upstream export spelling.
|
|
8
|
-
Html as HTML,
|
|
9
|
-
Img,
|
|
10
|
-
Link,
|
|
11
|
-
Preview,
|
|
12
|
-
Section,
|
|
13
|
-
Text
|
|
14
|
-
} from '@vue-email/components'
|
|
15
|
-
import { h, type VNode } from 'vue'
|
|
1
|
+
/** The brand every OpenPencil email shares, from the waitlist to Cloud: its colors and type. */
|
|
16
2
|
|
|
17
3
|
/** The ivory app-icon tile, served as PNG because mail clients do not draw SVG. */
|
|
18
|
-
const LOGO_URL = 'https://openpencil.dev/brand/app-icon-1024.png'
|
|
19
|
-
const SITE_URL = 'https://openpencil.dev'
|
|
4
|
+
export const LOGO_URL = 'https://openpencil.dev/brand/app-icon-1024.png'
|
|
5
|
+
export const SITE_URL = 'https://openpencil.dev'
|
|
20
6
|
|
|
21
7
|
const FONT = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif'
|
|
22
8
|
const INK = '#171717'
|
|
@@ -24,7 +10,8 @@ const MUTED = '#6B6B6B'
|
|
|
24
10
|
/** Deep teal from the mark; white text on it passes WCAG AA. */
|
|
25
11
|
const TEAL = '#0A6C65'
|
|
26
12
|
|
|
27
|
-
|
|
13
|
+
/** Inline styles, since mail clients drop most stylesheets. */
|
|
14
|
+
export const styles = {
|
|
28
15
|
body: { backgroundColor: '#F5F5EF', fontFamily: FONT, margin: '0', padding: '40px 16px' },
|
|
29
16
|
card: {
|
|
30
17
|
maxWidth: '480px',
|
|
@@ -42,7 +29,8 @@ const styles = {
|
|
|
42
29
|
lineHeight: '1.3',
|
|
43
30
|
margin: '0 0 12px'
|
|
44
31
|
},
|
|
45
|
-
text: { color: '#404040', fontSize: '15px', lineHeight: '1.6', margin: '0 0
|
|
32
|
+
text: { color: '#404040', fontSize: '15px', lineHeight: '1.6', margin: '0 0 16px' },
|
|
33
|
+
action: { paddingTop: '8px' },
|
|
46
34
|
button: {
|
|
47
35
|
backgroundColor: TEAL,
|
|
48
36
|
borderRadius: '10px',
|
|
@@ -60,46 +48,6 @@ const styles = {
|
|
|
60
48
|
lineHeight: '1.6',
|
|
61
49
|
textAlign: 'center' as const,
|
|
62
50
|
margin: '24px 0 0'
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export type EmailLayoutProps = {
|
|
67
|
-
/** The line mail clients show beside the subject. */
|
|
68
|
-
preview: string
|
|
69
|
-
heading: string
|
|
70
|
-
message: string
|
|
71
|
-
actionLabel: string
|
|
72
|
-
actionURL: string
|
|
73
|
-
/** Small print under the button, with an optional link at its end. */
|
|
74
|
-
note?: { text: string; link?: { label: string; url: string } }
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function noteContent(note: NonNullable<EmailLayoutProps['note']>) {
|
|
78
|
-
if (!note.link) return note.text
|
|
79
|
-
const { label, url } = note.link
|
|
80
|
-
return [`${note.text} `, h(Link, { href: url, style: styles.link }, () => label), '.']
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** The branded email every OpenPencil service sends: the waitlist and Cloud alike. */
|
|
84
|
-
export function emailLayout(props: EmailLayoutProps): VNode {
|
|
85
|
-
const { note } = props
|
|
86
|
-
return h(HTML, { lang: 'en' }, () => [
|
|
87
|
-
h(Head),
|
|
88
|
-
h(Preview, () => props.preview),
|
|
89
|
-
h(Body, { style: styles.body }, () => [
|
|
90
|
-
h(Container, { style: styles.card }, () => [
|
|
91
|
-
h(Img, { src: LOGO_URL, width: 44, height: 44, alt: 'OpenPencil', style: styles.logo }),
|
|
92
|
-
h(Heading, { as: 'h1', style: styles.heading }, () => props.heading),
|
|
93
|
-
h(Text, { style: styles.text }, () => props.message),
|
|
94
|
-
h(Section, () =>
|
|
95
|
-
h(Button, { href: props.actionURL, style: styles.button }, () => props.actionLabel)
|
|
96
|
-
),
|
|
97
|
-
note ? h(Text, { style: styles.note }, () => noteContent(note)) : null
|
|
98
|
-
]),
|
|
99
|
-
h(Text, { style: styles.footer }, () => [
|
|
100
|
-
'OpenPencil, the open-source design editor · ',
|
|
101
|
-
h(Link, { href: SITE_URL, style: { color: MUTED } }, () => 'openpencil.dev')
|
|
102
|
-
])
|
|
103
|
-
])
|
|
104
|
-
])
|
|
51
|
+
},
|
|
52
|
+
footerLink: { color: MUTED }
|
|
105
53
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { inject, type InjectionKey } from 'vue'
|
|
2
|
+
|
|
3
|
+
import type { EmailLocalization } from '../i18n'
|
|
4
|
+
|
|
5
|
+
/** The reader's language, which every email and its layout write in. */
|
|
6
|
+
export const EMAIL_LOCALIZATION: InjectionKey<EmailLocalization> = Symbol('email-localization')
|
|
7
|
+
|
|
8
|
+
export function useEmailLocalization(): EmailLocalization {
|
|
9
|
+
const localization = inject(EMAIL_LOCALIZATION)
|
|
10
|
+
if (!localization) throw new Error('An email renders inside EmailRoot, which gives its language')
|
|
11
|
+
return localization
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** "Hi Ada," or, when the account has no name, "Hi,". */
|
|
15
|
+
export function greeting({ t }: EmailLocalization, name: string | null): string {
|
|
16
|
+
return name ? t.greeting({ name }) : t.greetingWithoutName
|
|
17
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { PostHog, type PostHogOptions } from 'posthog-node'
|
|
2
|
+
|
|
3
|
+
/** Where a server caught an error, and for a request, its method, matched route, and ID. */
|
|
4
|
+
export type ErrorReportContext<Source extends string = string> = {
|
|
5
|
+
source: Source
|
|
6
|
+
method?: string
|
|
7
|
+
route?: string
|
|
8
|
+
/** The request's ID, as `cloudRequestId` named it; logs carry it too. */
|
|
9
|
+
requestId?: string
|
|
10
|
+
/** What failed, as the server's log says it; not sent to PostHog, whose report has the error. */
|
|
11
|
+
message?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Sends a caught error where it is seen; it never rejects. */
|
|
15
|
+
export type ErrorReporter<Source extends string = string> = (
|
|
16
|
+
error: unknown,
|
|
17
|
+
context: ErrorReportContext<Source>
|
|
18
|
+
) => Promise<void>
|
|
19
|
+
|
|
20
|
+
export type PostHogErrorReporterOptions = {
|
|
21
|
+
/** The project API key, or how to read it; a key that cannot be read yet is read again later. */
|
|
22
|
+
key: string | (() => Promise<string>)
|
|
23
|
+
host: string
|
|
24
|
+
/** Sent with every report: which service and deployment report, never who made the request. */
|
|
25
|
+
properties: Record<string, string>
|
|
26
|
+
/** The property that names the context's `source`, such as `cloud_source`. */
|
|
27
|
+
sourceProperty: string
|
|
28
|
+
/** Names the service in its own log, such as `Cloud`. */
|
|
29
|
+
logName: string
|
|
30
|
+
fetch?: PostHogOptions['fetch']
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Reports errors to PostHog error tracking, each as it is caught, as a `$exception` with no person
|
|
35
|
+
* profile and no GeoIP. A report carries `properties`, the source, and a request's method and
|
|
36
|
+
* matched route and ID; never who made the request or what it carried.
|
|
37
|
+
*/
|
|
38
|
+
export function createPostHogErrorReporter<Source extends string>(
|
|
39
|
+
options: PostHogErrorReporterOptions
|
|
40
|
+
): ErrorReporter<Source> {
|
|
41
|
+
const { key, logName } = options
|
|
42
|
+
const connect = async () => {
|
|
43
|
+
const client = new PostHog(typeof key === 'string' ? key : await key(), {
|
|
44
|
+
host: options.host,
|
|
45
|
+
disableGeoip: true,
|
|
46
|
+
// The SDK's own fetch unless a caller passes one.
|
|
47
|
+
...(options.fetch && { fetch: options.fetch })
|
|
48
|
+
})
|
|
49
|
+
// The SDK resolves a capture that PostHog refused and emits the refusal instead.
|
|
50
|
+
client.on('error', (failure: unknown) => {
|
|
51
|
+
console.error(`[${logName}] PostHog refused an error report:`, failure)
|
|
52
|
+
})
|
|
53
|
+
return client
|
|
54
|
+
}
|
|
55
|
+
let connecting: Promise<PostHog> | undefined
|
|
56
|
+
const posthog = async () => {
|
|
57
|
+
connecting ??= connect()
|
|
58
|
+
try {
|
|
59
|
+
return await connecting
|
|
60
|
+
} catch (failure) {
|
|
61
|
+
connecting = undefined
|
|
62
|
+
throw failure
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return async (error, context) => {
|
|
66
|
+
try {
|
|
67
|
+
const client = await posthog()
|
|
68
|
+
// Undefined request fields are left out of the event's JSON.
|
|
69
|
+
await client.captureExceptionImmediate(error, undefined, {
|
|
70
|
+
...options.properties,
|
|
71
|
+
[options.sourceProperty]: context.source,
|
|
72
|
+
http_method: context.method,
|
|
73
|
+
http_route: context.route,
|
|
74
|
+
request_id: context.requestId
|
|
75
|
+
})
|
|
76
|
+
} catch (failure) {
|
|
77
|
+
console.error(`[${logName}] Reporting an error failed:`, failure)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
package/src/global.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Single-file components as plain TypeScript sees them; vue-tsc reads the components themselves
|
|
2
|
+
// (`bun run typecheck`).
|
|
3
|
+
declare module '*.vue' {
|
|
4
|
+
import type { DefineComponent } from 'vue'
|
|
5
|
+
const component: DefineComponent<object, object, unknown>
|
|
6
|
+
export default component
|
|
7
|
+
}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createCloudErrorReporter } from '#cloud/runtime/posthog/errors'
|
|
2
2
|
import {
|
|
3
|
-
|
|
3
|
+
createLoggingErrorReporter,
|
|
4
|
+
loadServicesSigningKey,
|
|
4
5
|
parseCloudDeploymentTOML,
|
|
5
|
-
type
|
|
6
|
+
type ServicesSigningKey,
|
|
6
7
|
type CloudEnvironment,
|
|
7
8
|
type CloudErrorReporter,
|
|
8
9
|
type CloudServerConfig
|
|
9
10
|
} from '#cloud/server'
|
|
10
11
|
|
|
12
|
+
import { writeWorkersLogLine } from './logging'
|
|
11
13
|
import type { CollaborationRelayRPC } from './relay'
|
|
12
14
|
|
|
13
15
|
/** The bindings `deploy/cloudflare/wrangler.jsonc` gives the Worker and its relay objects. */
|
|
@@ -27,9 +29,9 @@ export type CloudflareCloudEnvironment = CloudflareCloudBindings & Record<string
|
|
|
27
29
|
|
|
28
30
|
export type PreparedCloudflareCloud = {
|
|
29
31
|
config: CloudServerConfig
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
reportError: CloudErrorReporter
|
|
32
|
+
servicesSigningKey: ServicesSigningKey | undefined
|
|
33
|
+
/** Logs errors, and sends them to error tracking when the deployment configures it. */
|
|
34
|
+
reportError: CloudErrorReporter
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
/** The Worker's string variables and secrets, with `DATABASE_URL` from Hyperdrive. */
|
|
@@ -43,9 +45,12 @@ function configEnvironment(env: CloudflareCloudEnvironment): CloudEnvironment {
|
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
/**
|
|
46
|
-
* Reads the deployment TOML once per isolate: its config and
|
|
48
|
+
* Reads the deployment TOML once per isolate: its config and services signing key stay the same for the
|
|
47
49
|
* Worker's lifetime. A failure is not kept, so the next request tries again.
|
|
48
50
|
*/
|
|
51
|
+
/** Logs errors from before a config parsed, which has no error tracking to send them to. */
|
|
52
|
+
const logOnly = createLoggingErrorReporter(writeWorkersLogLine)
|
|
53
|
+
|
|
49
54
|
export function cloudflareCloudSetup(source: string) {
|
|
50
55
|
let prepared: Promise<PreparedCloudflareCloud> | null = null
|
|
51
56
|
/** Set once an attempt's config parses, so its later setup steps that fail can be reported. */
|
|
@@ -55,8 +60,11 @@ export function cloudflareCloudSetup(source: string) {
|
|
|
55
60
|
// An attempt whose config fails to parse has no project to report to, not an earlier one's.
|
|
56
61
|
reportError = undefined
|
|
57
62
|
const config = parseCloudDeploymentTOML(source, configEnvironment(env))
|
|
58
|
-
reportError =
|
|
59
|
-
|
|
63
|
+
reportError = createLoggingErrorReporter(
|
|
64
|
+
writeWorkersLogLine,
|
|
65
|
+
createCloudErrorReporter(config, 'cloudflare')
|
|
66
|
+
)
|
|
67
|
+
return { config, servicesSigningKey: await loadServicesSigningKey(config), reportError }
|
|
60
68
|
})().catch((error: unknown) => {
|
|
61
69
|
prepared = null
|
|
62
70
|
throw error
|
|
@@ -65,7 +73,10 @@ export function cloudflareCloudSetup(source: string) {
|
|
|
65
73
|
}
|
|
66
74
|
return {
|
|
67
75
|
prepare,
|
|
68
|
-
/**
|
|
69
|
-
|
|
76
|
+
/**
|
|
77
|
+
* The reporter of the latest attempt once its config parsed, which also sends to error tracking
|
|
78
|
+
* when the config has it; before that, one that only logs.
|
|
79
|
+
*/
|
|
80
|
+
reporter: () => reportError ?? logOnly
|
|
70
81
|
}
|
|
71
82
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { createCloudflareEmailTransport, type CloudflareEmailBinding } from './email'
|
|
2
2
|
export type { CloudflareCloudBindings, CloudflareCloudEnvironment } from './environment'
|
|
3
|
-
export { createCloudflareCloud } from './worker'
|
|
3
|
+
export { createCloudflareCloud, type CloudflareCloudOptions } from './worker'
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CloudLogWriter, ForwardedRequestId } from '#cloud/server'
|
|
2
|
+
|
|
3
|
+
/** Workers Logs indexes the keys of an object passed to `console`, so the line goes as one. */
|
|
4
|
+
export const writeWorkersLogLine: CloudLogWriter = (line) => {
|
|
5
|
+
console[line.level](line)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Cloudflare's ray ID, such as `8f1e2d3c4b5a6978-FRA`. */
|
|
9
|
+
const RAY_ID = /^[0-9a-f]{16}(-[A-Z]{3})?$/
|
|
10
|
+
|
|
11
|
+
/** Names a request by the ray ID Cloudflare's edge set on it, which a client cannot choose. */
|
|
12
|
+
export const cloudflareRayId: ForwardedRequestId = (headers) => {
|
|
13
|
+
const ray = headers.get('cf-ray')
|
|
14
|
+
return ray && RAY_ID.test(ray) ? ray : undefined
|
|
15
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { cloudflareRayId } from './logging'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The Worker variable that takes a deployment off the air, as the Deploy Cloud workflow's
|
|
5
|
+
* `maintenance` input sets it: every request answers 503 and scheduled cleanup does nothing, while
|
|
6
|
+
* the deploy itself restarts the rooms' objects and closes their sockets. A database restore runs
|
|
7
|
+
* under it (deploy/openpencil/README.md).
|
|
8
|
+
*/
|
|
9
|
+
export const MAINTENANCE_VARIABLE = 'OPENPENCIL_CLOUD_MAINTENANCE'
|
|
10
|
+
|
|
11
|
+
export function inMaintenance(env: Record<string, unknown>): boolean {
|
|
12
|
+
return env[MAINTENANCE_VARIABLE] === 'on'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** How a relay socket open when maintenance began is closed: the service restarts. */
|
|
16
|
+
export const MAINTENANCE_CLOSE_CODE = 1012
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What every request gets meanwhile; clients retry, and nothing reaches the database. It names the
|
|
20
|
+
* request as other responses do, by its ray ID.
|
|
21
|
+
*/
|
|
22
|
+
export function maintenanceResponse(request: Request): Response {
|
|
23
|
+
return new Response('OpenPencil Cloud is down for maintenance.', {
|
|
24
|
+
status: 503,
|
|
25
|
+
headers: {
|
|
26
|
+
'Content-Type': 'text/plain; charset=utf-8',
|
|
27
|
+
'Retry-After': '300',
|
|
28
|
+
'X-Request-Id': cloudflareRayId(request.headers) ?? crypto.randomUUID()
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RELAY_DOCUMENT_PARAM } from '#cloud/contract'
|
|
1
|
+
import { RELAY_DOCUMENT_PARAM, RELAY_HEARTBEAT } from '#cloud/contract'
|
|
2
2
|
import { createPostgresCloudDatabase } from '#cloud/runtime/postgres/database'
|
|
3
3
|
import { createS3ObjectStore } from '#cloud/runtime/s3/objects'
|
|
4
4
|
import {
|
|
@@ -14,36 +14,61 @@ import type { Context } from 'hono'
|
|
|
14
14
|
import * as v from 'valibot'
|
|
15
15
|
|
|
16
16
|
import type { CloudflareCloudEnvironment, PreparedCloudflareCloud } from '../environment'
|
|
17
|
+
import { inMaintenance, MAINTENANCE_CLOSE_CODE, maintenanceResponse } from '../maintenance'
|
|
17
18
|
import { createRelaySockets } from './sockets'
|
|
18
19
|
|
|
19
20
|
/** What the Worker calls on a document's relay object. */
|
|
20
21
|
export interface CollaborationRelayRPC extends DurableObject<CloudflareCloudEnvironment> {
|
|
21
22
|
/** Ends the document's live rooms after a restore, without saving them. */
|
|
22
23
|
startOver(documentId: string): Promise<void>
|
|
24
|
+
/** Ends a taken-down document's live rooms, saving them. */
|
|
25
|
+
closeDocument(documentId: string): Promise<void>
|
|
23
26
|
/** Tells the document's live rooms its version history changed. */
|
|
24
27
|
announceVersions(documentId: string): Promise<void>
|
|
25
28
|
}
|
|
26
29
|
|
|
30
|
+
/** The Durable Object class a Worker exports for collaboration rooms. */
|
|
31
|
+
export type CollaborationRelayObjectClass = new (
|
|
32
|
+
ctx: DurableObjectState,
|
|
33
|
+
env: CloudflareCloudEnvironment
|
|
34
|
+
) => CollaborationRelayRPC
|
|
35
|
+
|
|
27
36
|
const documentIdSchema = v.pipe(v.string(), v.uuid())
|
|
28
37
|
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
38
|
+
/**
|
|
39
|
+
* The most a room's document may hold on Cloudflare. A Durable Object has 128 MB of memory for
|
|
40
|
+
* the document, the update joining it, and the snapshot written from it, so a room past this is
|
|
41
|
+
* turned away and its editors save without one.
|
|
42
|
+
*/
|
|
43
|
+
export const CLOUDFLARE_MAXIMUM_ROOM_BYTES = 32 * 1024 * 1024
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The document's relay object. With a jurisdiction, objects are created and keep their storage
|
|
47
|
+
* only inside it; an object named outside one is a different object, so a deployment never
|
|
48
|
+
* changes its jurisdiction while rooms are live.
|
|
49
|
+
*/
|
|
50
|
+
export function relayFor(
|
|
51
|
+
env: CloudflareCloudEnvironment,
|
|
52
|
+
documentId: string,
|
|
53
|
+
jurisdiction?: DurableObjectJurisdiction
|
|
54
|
+
) {
|
|
55
|
+
const namespace = jurisdiction ? env.RELAY.jurisdiction(jurisdiction) : env.RELAY
|
|
56
|
+
return namespace.get(namespace.idFromName(documentId))
|
|
32
57
|
}
|
|
33
58
|
|
|
34
59
|
/**
|
|
35
60
|
* Sends a relay socket to its document's relay object. The ticket's URL names the document; the
|
|
36
61
|
* object refuses tickets for any other.
|
|
37
62
|
*/
|
|
38
|
-
export
|
|
39
|
-
c: Context<{ Bindings: CloudflareCloudEnvironment }>
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
|
|
63
|
+
export function relayRoute(jurisdiction?: DurableObjectJurisdiction) {
|
|
64
|
+
return async (c: Context<{ Bindings: CloudflareCloudEnvironment }>): Promise<Response> => {
|
|
65
|
+
if (c.req.header('Upgrade')?.toLowerCase() !== 'websocket') {
|
|
66
|
+
return c.text('Expected a WebSocket upgrade', 426)
|
|
67
|
+
}
|
|
68
|
+
const documentId = c.req.query(RELAY_DOCUMENT_PARAM)
|
|
69
|
+
if (!v.is(documentIdSchema, documentId)) return c.text('Missing document', 400)
|
|
70
|
+
return relayFor(c.env, documentId, jurisdiction).fetch(c.req.raw)
|
|
43
71
|
}
|
|
44
|
-
const documentId = c.req.query(RELAY_DOCUMENT_PARAM)
|
|
45
|
-
if (!v.is(documentIdSchema, documentId)) return c.text('Missing document', 400)
|
|
46
|
-
return relayFor(c.env, documentId).fetch(c.req.raw)
|
|
47
72
|
}
|
|
48
73
|
|
|
49
74
|
/** The relay over the deployment's PostgreSQL and object store. */
|
|
@@ -52,12 +77,17 @@ function configuredRelay(
|
|
|
52
77
|
documentId: string,
|
|
53
78
|
onError: (message: string, error: unknown) => void
|
|
54
79
|
): CollaborationRelay {
|
|
55
|
-
|
|
80
|
+
// Hyperdrive keeps no session settings and ends queries past its own limit.
|
|
81
|
+
const database = createPostgresCloudDatabase({
|
|
82
|
+
connectionString: config.databaseURL,
|
|
83
|
+
statementTimeoutMs: null
|
|
84
|
+
})
|
|
56
85
|
return createCollaborationRelay({
|
|
57
86
|
authSecret: config.authSecret,
|
|
58
87
|
documentId,
|
|
59
88
|
store: createCollaborationStateStore(database, createS3ObjectStore(config)),
|
|
60
89
|
maximumMessageBytes: config.technicalLimits.maximumCollaborationMessageBytes,
|
|
90
|
+
maximumRoomBytes: CLOUDFLARE_MAXIMUM_ROOM_BYTES,
|
|
61
91
|
maximumConnectionsPerRoom: config.technicalLimits.maximumConnectionsPerRoom,
|
|
62
92
|
onError,
|
|
63
93
|
...relayDocumentLookups(
|
|
@@ -75,11 +105,19 @@ function configuredRelay(
|
|
|
75
105
|
*/
|
|
76
106
|
export function createCloudflareRelayObject(
|
|
77
107
|
prepare: (env: CloudflareCloudEnvironment) => Promise<PreparedCloudflareCloud>
|
|
78
|
-
) {
|
|
108
|
+
): CollaborationRelayObjectClass {
|
|
79
109
|
return class CollaborationRelayObject
|
|
80
110
|
extends DurableObject<CloudflareCloudEnvironment>
|
|
81
111
|
implements CollaborationRelayRPC
|
|
82
112
|
{
|
|
113
|
+
constructor(ctx: DurableObjectState, env: CloudflareCloudEnvironment) {
|
|
114
|
+
super(ctx, env)
|
|
115
|
+
// Peers check the relay still hears them; the runtime answers without waking the object.
|
|
116
|
+
ctx.setWebSocketAutoResponse(
|
|
117
|
+
new WebSocketRequestResponsePair(RELAY_HEARTBEAT.ping, RELAY_HEARTBEAT.pong)
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
|
|
83
121
|
readonly #sockets = createRelaySockets({
|
|
84
122
|
sockets: () => this.ctx.getWebSockets(),
|
|
85
123
|
createRelay: async (documentId) => {
|
|
@@ -92,8 +130,9 @@ export function createCloudflareRelayObject(
|
|
|
92
130
|
}
|
|
93
131
|
})
|
|
94
132
|
|
|
95
|
-
/** Accepts a socket that `
|
|
133
|
+
/** Accepts a socket that `relayRoute` sent here. */
|
|
96
134
|
override async fetch(request: Request): Promise<Response> {
|
|
135
|
+
if (inMaintenance(this.env)) return maintenanceResponse(request)
|
|
97
136
|
const documentId = new URL(request.url).searchParams.get(RELAY_DOCUMENT_PARAM)
|
|
98
137
|
if (!v.is(documentIdSchema, documentId)) return new Response(null, { status: 400 })
|
|
99
138
|
const relay = await this.#sockets.open(documentId)
|
|
@@ -104,14 +143,24 @@ export function createCloudflareRelayObject(
|
|
|
104
143
|
}
|
|
105
144
|
|
|
106
145
|
override async webSocketMessage(socket: WebSocket, message: string | ArrayBuffer) {
|
|
146
|
+
// A socket from before maintenance began reaches the room no more: its editor reconnects
|
|
147
|
+
// once maintenance ends, and nothing it sends meanwhile is written.
|
|
148
|
+
if (inMaintenance(this.env)) {
|
|
149
|
+
socket.close(MAINTENANCE_CLOSE_CODE, 'Down for maintenance')
|
|
150
|
+
return
|
|
151
|
+
}
|
|
107
152
|
await this.#sockets.message(socket, message)
|
|
108
153
|
}
|
|
109
154
|
|
|
155
|
+
// Under maintenance a closing socket leaves its room as it is: closing the room would save
|
|
156
|
+
// its state into a database that may be mid-restore.
|
|
110
157
|
override async webSocketClose(socket: WebSocket) {
|
|
158
|
+
if (inMaintenance(this.env)) return
|
|
111
159
|
await this.#sockets.closed(socket)
|
|
112
160
|
}
|
|
113
161
|
|
|
114
162
|
override async webSocketError(socket: WebSocket) {
|
|
163
|
+
if (inMaintenance(this.env)) return
|
|
115
164
|
await this.#sockets.closed(socket)
|
|
116
165
|
}
|
|
117
166
|
|
|
@@ -120,6 +169,11 @@ export function createCloudflareRelayObject(
|
|
|
120
169
|
;(await this.#sockets.open(documentId)).startOver(documentId)
|
|
121
170
|
}
|
|
122
171
|
|
|
172
|
+
async closeDocument(documentId: string) {
|
|
173
|
+
if (!this.#sockets.active) return
|
|
174
|
+
;(await this.#sockets.open(documentId)).closeDocument(documentId)
|
|
175
|
+
}
|
|
176
|
+
|
|
123
177
|
async announceVersions(documentId: string) {
|
|
124
178
|
if (!this.#sockets.active) return
|
|
125
179
|
;(await this.#sockets.open(documentId)).announceVersions(documentId)
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"skipLibCheck": true,
|
|
14
14
|
"types": ["@cloudflare/workers-types"],
|
|
15
15
|
"paths": {
|
|
16
|
-
"#cloud/*": ["../../*"]
|
|
16
|
+
"#cloud/*": ["../../*"],
|
|
17
|
+
"@open-pencil/cloud/runtime/cloudflare": ["./index.ts"]
|
|
17
18
|
}
|
|
18
19
|
},
|
|
19
20
|
"include": ["./**/*.ts", "../../../deploy/**/*.ts"],
|
|
@@ -5,6 +5,7 @@ import { createCloudServer } from '#cloud/runtime/site/server'
|
|
|
5
5
|
import {
|
|
6
6
|
cleanupPassOptions,
|
|
7
7
|
cloudErrorHandler,
|
|
8
|
+
cloudRequestId,
|
|
8
9
|
createCleanupServices,
|
|
9
10
|
createCloudApp,
|
|
10
11
|
createCloudAuthenticationRuntime,
|
|
@@ -25,7 +26,9 @@ import {
|
|
|
25
26
|
type CloudflareCloudEnvironment,
|
|
26
27
|
type PreparedCloudflareCloud
|
|
27
28
|
} from './environment'
|
|
28
|
-
import {
|
|
29
|
+
import { cloudflareRayId } from './logging'
|
|
30
|
+
import { inMaintenance, maintenanceResponse } from './maintenance'
|
|
31
|
+
import { createCloudflareRelayObject, relayFor, relayRoute } from './relay'
|
|
29
32
|
import { assetSiteFiles } from './site'
|
|
30
33
|
|
|
31
34
|
function emailTransport({ config }: PreparedCloudflareCloud, env: CloudflareCloudEnvironment) {
|
|
@@ -60,7 +63,11 @@ function cloudServices(
|
|
|
60
63
|
const reporter = keptAlive(prepared.reportError, context)
|
|
61
64
|
const reportError = (message: string, error: unknown, source: CloudErrorSource) =>
|
|
62
65
|
reportCloudError(reporter, message, error, { source })
|
|
63
|
-
|
|
66
|
+
// Hyperdrive keeps no session settings and ends queries past its own limit.
|
|
67
|
+
const database = createPostgresCloudDatabase({
|
|
68
|
+
connectionString: config.databaseURL,
|
|
69
|
+
statementTimeoutMs: null
|
|
70
|
+
})
|
|
64
71
|
const objects = createS3ObjectStore(config)
|
|
65
72
|
const transport = emailTransport(prepared, env)
|
|
66
73
|
const queued = createTransactionalEmailService(database, {
|
|
@@ -81,7 +88,7 @@ function cloudServices(
|
|
|
81
88
|
const background: Promise<unknown>[] = []
|
|
82
89
|
const runInBackground = (task: Promise<unknown>) => {
|
|
83
90
|
background.push(
|
|
84
|
-
task.catch((error: unknown) => reportError('Background work failed
|
|
91
|
+
task.catch((error: unknown) => reportError('Background work failed', error, 'background'))
|
|
85
92
|
)
|
|
86
93
|
}
|
|
87
94
|
const deliver = () =>
|
|
@@ -104,7 +111,7 @@ function cloudServices(
|
|
|
104
111
|
context.waitUntil(
|
|
105
112
|
Promise.allSettled(background)
|
|
106
113
|
.then(() => (enqueued && transport ? deliver() : undefined))
|
|
107
|
-
.catch((error: unknown) => reportError('Sending email failed
|
|
114
|
+
.catch((error: unknown) => reportError('Sending email failed', error, 'email'))
|
|
108
115
|
.then(() => database.destroy())
|
|
109
116
|
)
|
|
110
117
|
}
|
|
@@ -123,7 +130,14 @@ async function runScheduled(
|
|
|
123
130
|
if (config.cleanupEnabled) {
|
|
124
131
|
const policy = createConfiguredCloudPolicy(config, database)
|
|
125
132
|
await runCleanup(
|
|
126
|
-
createCleanupServices(
|
|
133
|
+
createCleanupServices(
|
|
134
|
+
config,
|
|
135
|
+
database,
|
|
136
|
+
objects,
|
|
137
|
+
policy,
|
|
138
|
+
services.reporter,
|
|
139
|
+
services.transport ? services.email : undefined
|
|
140
|
+
),
|
|
127
141
|
cleanupPassOptions(config)
|
|
128
142
|
)
|
|
129
143
|
}
|
|
@@ -138,13 +152,27 @@ async function runScheduled(
|
|
|
138
152
|
* API, the editor, and the portal per request, a Durable Object per document runs its collaboration rooms, and a
|
|
139
153
|
* Cron Trigger runs cleanup and retries email.
|
|
140
154
|
*/
|
|
141
|
-
export
|
|
155
|
+
export type CloudflareCloudOptions = {
|
|
156
|
+
/**
|
|
157
|
+
* Where collaboration rooms' Durable Objects run and keep their storage, such as `eu` for a
|
|
158
|
+
* deployment whose data stays in the EU; by default, near whoever opens the room first.
|
|
159
|
+
*/
|
|
160
|
+
relayJurisdiction?: DurableObjectJurisdiction
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function createCloudflareCloud(
|
|
164
|
+
configSource: string,
|
|
165
|
+
{ relayJurisdiction }: CloudflareCloudOptions = {}
|
|
166
|
+
) {
|
|
142
167
|
const { prepare, reporter } = cloudflareCloudSetup(configSource)
|
|
168
|
+
const relay = (env: CloudflareCloudEnvironment, documentId: string) =>
|
|
169
|
+
relayFor(env, documentId, relayJurisdiction)
|
|
143
170
|
|
|
144
171
|
const router = new Hono<{ Bindings: CloudflareCloudEnvironment }>()
|
|
145
172
|
// Setup, relay routing, and the runtime around the app; the app reports its own errors.
|
|
146
173
|
.onError((error, c) => cloudErrorHandler(keptAlive(reporter(), c.executionCtx))(error, c))
|
|
147
|
-
.
|
|
174
|
+
.use('*', cloudRequestId(cloudflareRayId))
|
|
175
|
+
.get(RELAY_PATH, relayRoute(relayJurisdiction))
|
|
148
176
|
.all('*', async (c) => {
|
|
149
177
|
const prepared = await prepare(c.env)
|
|
150
178
|
const services = cloudServices(prepared, c.env, c.executionCtx)
|
|
@@ -162,13 +190,16 @@ export function createCloudflareCloud(configSource: string) {
|
|
|
162
190
|
invitationOutbox: transport ? createInvitationOutbox(email) : undefined,
|
|
163
191
|
transactionalEmail: email,
|
|
164
192
|
enrollment,
|
|
165
|
-
|
|
193
|
+
servicesSigningKey: prepared.servicesSigningKey,
|
|
166
194
|
relayURL: config.relayURL ?? defaultRelayURL(config.publicURL),
|
|
167
195
|
onDocumentRestored: (documentId) =>
|
|
168
|
-
runInBackground(
|
|
196
|
+
runInBackground(relay(c.env, documentId).startOver(documentId)),
|
|
197
|
+
onDocumentTakenDown: (documentId) =>
|
|
198
|
+
runInBackground(relay(c.env, documentId).closeDocument(documentId)),
|
|
169
199
|
onDocumentVersionsChanged: (documentId) =>
|
|
170
|
-
runInBackground(
|
|
171
|
-
reportError: services.reporter
|
|
200
|
+
runInBackground(relay(c.env, documentId).announceVersions(documentId)),
|
|
201
|
+
reportError: services.reporter,
|
|
202
|
+
forwardedRequestId: cloudflareRayId
|
|
172
203
|
})
|
|
173
204
|
return await createCloudServer(app, {
|
|
174
205
|
site: assetSiteFiles(c.env.ASSETS),
|
|
@@ -181,13 +212,15 @@ export function createCloudflareCloud(configSource: string) {
|
|
|
181
212
|
})
|
|
182
213
|
|
|
183
214
|
const worker: ExportedHandler<CloudflareCloudEnvironment> = {
|
|
184
|
-
fetch: (request, env, context) =>
|
|
215
|
+
fetch: (request, env, context) =>
|
|
216
|
+
inMaintenance(env) ? maintenanceResponse(request) : router.fetch(request, env, context),
|
|
185
217
|
|
|
186
218
|
async scheduled(_controller, env, context) {
|
|
219
|
+
if (inMaintenance(env)) return
|
|
187
220
|
try {
|
|
188
221
|
await runScheduled(await prepare(env), env, context)
|
|
189
222
|
} catch (error) {
|
|
190
|
-
await reportCloudError(reporter(), 'Scheduled run failed
|
|
223
|
+
await reportCloudError(reporter(), 'Scheduled run failed', error, { source: 'scheduled' })
|
|
191
224
|
throw error
|
|
192
225
|
}
|
|
193
226
|
}
|