@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
package/src/contract/links.ts
CHANGED
|
@@ -3,10 +3,18 @@ import { encodeParam, parseURL, withFragment, withQuery } from 'ufo'
|
|
|
3
3
|
/** The two kinds of Cloud link that carry a secret: invitations and documents shared by link. */
|
|
4
4
|
export type CloudLinkKind = 'invitations' | 'share'
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Every kind of Cloud link: those that carry a secret, a document's address for members, and a
|
|
8
|
+
* document of the server's community gallery, which anyone may open.
|
|
9
|
+
*/
|
|
10
|
+
export type CloudLinkPathKind = CloudLinkKind | 'documents' | 'community'
|
|
8
11
|
|
|
9
12
|
const CLOUD_LINK_PATH = /^\/(invitations|share|documents)\/([^/]+)$/
|
|
13
|
+
/** A community document opened in the editor; `/community/:id` alone is its page. */
|
|
14
|
+
const COMMUNITY_EDITOR_PATH = /^\/community\/([^/]+)\/open$/
|
|
15
|
+
|
|
16
|
+
/** The server's community gallery: every document in it, each with a page of its own. */
|
|
17
|
+
export const CLOUD_COMMUNITY_PATH = '/community'
|
|
10
18
|
|
|
11
19
|
/**
|
|
12
20
|
* A link on the server's own editor. The server is the link's own address, so the link needs
|
|
@@ -55,6 +63,29 @@ export function cloudDocumentPath(documentId: string): string {
|
|
|
55
63
|
return `/documents/${encodeParam(documentId)}`
|
|
56
64
|
}
|
|
57
65
|
|
|
66
|
+
/**
|
|
67
|
+
* A document of the server's community gallery opened in the editor, `/community/:id/open`, or
|
|
68
|
+
* one of its named versions with `?version=…`, which anyone opens read-only without signing in.
|
|
69
|
+
*/
|
|
70
|
+
export function cloudCommunityURL(
|
|
71
|
+
serverURL: string,
|
|
72
|
+
documentId: string,
|
|
73
|
+
versionId: string | null = null
|
|
74
|
+
): string {
|
|
75
|
+
return serverLink(serverURL, cloudCommunityPath(documentId), {
|
|
76
|
+
version: versionId ?? undefined
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function cloudCommunityPath(documentId: string): string {
|
|
81
|
+
return `${cloudCommunityPagePath(documentId)}/open`
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A community document's page on the server: its preview, versions, and ways to open it. */
|
|
85
|
+
export function cloudCommunityPagePath(documentId: string): string {
|
|
86
|
+
return `${CLOUD_COMMUNITY_PATH}/${encodeParam(documentId)}`
|
|
87
|
+
}
|
|
88
|
+
|
|
58
89
|
/**
|
|
59
90
|
* The kind and ID of a Cloud link's path, or null for any other path. The ID is decoded; a broken
|
|
60
91
|
* escape such as `%E0` is no link.
|
|
@@ -62,8 +93,16 @@ export function cloudDocumentPath(documentId: string): string {
|
|
|
62
93
|
export function parseCloudLinkPath(
|
|
63
94
|
pathname: string
|
|
64
95
|
): { kind: CloudLinkPathKind; id: string } | null {
|
|
65
|
-
const
|
|
66
|
-
|
|
96
|
+
const community = pathname.match(COMMUNITY_EDITOR_PATH)
|
|
97
|
+
const [, kind, id] = community
|
|
98
|
+
? [pathname, 'community' as const, community[1]]
|
|
99
|
+
: (pathname.match(CLOUD_LINK_PATH) ?? [])
|
|
100
|
+
if (
|
|
101
|
+
(kind !== 'invitations' && kind !== 'share' && kind !== 'documents' && kind !== 'community') ||
|
|
102
|
+
!id
|
|
103
|
+
) {
|
|
104
|
+
return null
|
|
105
|
+
}
|
|
67
106
|
try {
|
|
68
107
|
return { kind, id: decodeURIComponent(id) }
|
|
69
108
|
} catch {
|
|
@@ -85,3 +124,17 @@ export function cloudDesktopLinkURL(
|
|
|
85
124
|
const link = serverLink(serverURL, `/${kind}/${encodeParam(id)}`)
|
|
86
125
|
return withFragment(withQuery('openpencil://cloud', { url: link }), secret)
|
|
87
126
|
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The desktop app's form of a community document's link, `openpencil://cloud?url=<link>`: the
|
|
130
|
+
* editor's address for it, with the named version it asks for, if any. It carries no secret.
|
|
131
|
+
*/
|
|
132
|
+
export function cloudCommunityDesktopLinkURL(
|
|
133
|
+
documentId: string,
|
|
134
|
+
serverURL: string,
|
|
135
|
+
versionId: string | null = null
|
|
136
|
+
): string {
|
|
137
|
+
return withQuery('openpencil://cloud', {
|
|
138
|
+
url: cloudCommunityURL(serverURL, documentId, versionId)
|
|
139
|
+
})
|
|
140
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
/** Whether `tag` is a well-formed BCP 47 language tag: `de`, `pt-BR`, `zh-Hant-TW`. */
|
|
4
|
+
function isLanguageTag(tag: string): boolean {
|
|
5
|
+
try {
|
|
6
|
+
return Intl.getCanonicalLocales(tag).length === 1
|
|
7
|
+
} catch {
|
|
8
|
+
return false
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A language as the app names it, which the server writes email in when it has that language, or
|
|
14
|
+
* the nearest it has, and otherwise in English.
|
|
15
|
+
*/
|
|
16
|
+
export const cloudLanguageTagSchema = v.pipe(
|
|
17
|
+
v.string(),
|
|
18
|
+
v.maxLength(35),
|
|
19
|
+
v.check(isLanguageTag, 'Not a language tag')
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
/** The language an account's email is written in, as the app last said it uses. */
|
|
23
|
+
export const cloudLocaleUpdateSchema = v.object({ locale: cloudLanguageTagSchema })
|
|
24
|
+
|
|
25
|
+
export function parseCloudLocaleUpdate(input: unknown) {
|
|
26
|
+
return v.parse(cloudLocaleUpdateSchema, input)
|
|
27
|
+
}
|
package/src/contract/portal.ts
CHANGED
|
@@ -6,6 +6,17 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export const CLOUD_PORTAL_PAGE_ROOTS = ['/welcome', '/auth', '/account', '/admin'] as const
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* The community gallery's pages, which the portal serves too: the gallery at `/community` and a
|
|
11
|
+
* document's page at `/community/:id`. The editor opens a document at `/community/:id/open`.
|
|
12
|
+
*/
|
|
13
|
+
export const CLOUD_PORTAL_COMMUNITY_PAGES = ['/community', '/community/:documentId'] as const
|
|
14
|
+
|
|
15
|
+
const COMMUNITY_PAGE = /^\/community(?:\/[^/]+)?\/?$/
|
|
16
|
+
|
|
9
17
|
export function isCloudPortalPage(path: string): boolean {
|
|
10
|
-
return
|
|
18
|
+
return (
|
|
19
|
+
COMMUNITY_PAGE.test(path) ||
|
|
20
|
+
CLOUD_PORTAL_PAGE_ROOTS.some((root) => path === root || path.startsWith(`${root}/`))
|
|
21
|
+
)
|
|
11
22
|
}
|
package/src/contract/relay.ts
CHANGED
|
@@ -109,6 +109,12 @@ export const RELAY_CLOSE = {
|
|
|
109
109
|
restored: 4410
|
|
110
110
|
} as const
|
|
111
111
|
|
|
112
|
+
/**
|
|
113
|
+
* The text a peer sends to learn whether the relay still hears it, and the relay's answer. Relay
|
|
114
|
+
* frames are binary, so neither is taken for one, and a Durable Object answers without waking.
|
|
115
|
+
*/
|
|
116
|
+
export const RELAY_HEARTBEAT = { ping: 'ping', pong: 'pong' } as const
|
|
117
|
+
|
|
112
118
|
const CLIENT_AUTH = 0
|
|
113
119
|
const CLIENT_MESSAGE = 1
|
|
114
120
|
const CLIENT_FRAGMENT = 2
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
import { resolveDocumentShareSchema } from './sharing'
|
|
4
|
+
|
|
5
|
+
/** Why someone reports a document shared with them. */
|
|
6
|
+
export const documentReportReasonSchema = v.picklist([
|
|
7
|
+
'spam-phishing',
|
|
8
|
+
'illegal',
|
|
9
|
+
'copyright',
|
|
10
|
+
'harassment',
|
|
11
|
+
'other'
|
|
12
|
+
])
|
|
13
|
+
export type DocumentReportReason = v.InferOutput<typeof documentReportReasonSchema>
|
|
14
|
+
|
|
15
|
+
/** How long a report's description may be. */
|
|
16
|
+
export const DOCUMENT_REPORT_DESCRIPTION_MAX = 2000
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A report about a document someone opened from a share link: the link's secret proves they can
|
|
20
|
+
* see it, as resolving the link does, and a guest names themselves as they do there.
|
|
21
|
+
*/
|
|
22
|
+
export const fileDocumentReportSchema = v.object({
|
|
23
|
+
secret: resolveDocumentShareSchema.entries.secret,
|
|
24
|
+
guestId: resolveDocumentShareSchema.entries.guestId,
|
|
25
|
+
reason: documentReportReasonSchema,
|
|
26
|
+
description: v.pipe(
|
|
27
|
+
v.string(),
|
|
28
|
+
v.trim(),
|
|
29
|
+
v.minLength(1),
|
|
30
|
+
v.maxLength(DOCUMENT_REPORT_DESCRIPTION_MAX)
|
|
31
|
+
),
|
|
32
|
+
/** Where an administrator may answer; optional, and never shown to the document's people. */
|
|
33
|
+
replyEmail: v.optional(v.pipe(v.string(), v.trim(), v.toLowerCase(), v.email(), v.maxLength(254)))
|
|
34
|
+
})
|
|
35
|
+
export type FileDocumentReportInput = v.InferOutput<typeof fileDocumentReportSchema>
|
|
36
|
+
|
|
37
|
+
export function parseFileDocumentReport(input: unknown): FileDocumentReportInput {
|
|
38
|
+
return v.parse(fileDocumentReportSchema, input)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What filing a report answers: that it arrived, and nothing about the document. */
|
|
42
|
+
export const fileDocumentReportResponseSchema = v.object({ received: v.literal(true) })
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { TypedResponse } from 'hono'
|
|
2
|
+
import type { BlankInput, ToSchema } from 'hono/types'
|
|
3
|
+
|
|
4
|
+
/** A route's input, and the JSON it answers with a 200, as a contract's typed Hono schema names it. */
|
|
5
|
+
export type JSONRoute<
|
|
6
|
+
Method extends string,
|
|
7
|
+
Path extends string,
|
|
8
|
+
Input extends BlankInput,
|
|
9
|
+
Output
|
|
10
|
+
> = ToSchema<Method, Path, Input, TypedResponse<Output, 200, 'json'>>
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import type { Hono } from 'hono'
|
|
2
|
+
import type { BlankEnv, BlankInput } from 'hono/types'
|
|
3
|
+
import * as v from 'valibot'
|
|
4
|
+
|
|
5
|
+
import type { JSONRoute } from './routes'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The contract between editors, Cloud, and the services Cloud advertises: AI models today, and
|
|
9
|
+
* other paid capabilities such as image generation and search as they arrive. Cloud only
|
|
10
|
+
* advertises the service and signs short-lived tokens for it; the service, official or
|
|
11
|
+
* self-hosted, calls the providers and keeps the credit wallet. It is versioned apart from the
|
|
12
|
+
* Cloud protocol so a service can change without a Cloud release.
|
|
13
|
+
*/
|
|
14
|
+
export const CLOUD_SERVICES_CONTRACT_VERSION = '1' as const
|
|
15
|
+
/** The `aud` of every token Cloud signs for its services. */
|
|
16
|
+
export const CLOUD_SERVICES_TOKEN_AUDIENCE = 'openpencil-services' as const
|
|
17
|
+
/** Where Cloud publishes the public keys a service verifies its tokens with. */
|
|
18
|
+
export const CLOUD_SERVICES_JWKS_PATH = '/.well-known/jwks.json' as const
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Where a service answers, below the discovered `endpoint`. Model requests follow the OpenAI chat
|
|
22
|
+
* completions API, so an OpenAI-compatible client takes `${endpoint}${base}` as its base URL.
|
|
23
|
+
*/
|
|
24
|
+
export const CLOUD_SERVICES_API_PATHS = {
|
|
25
|
+
base: '/v1',
|
|
26
|
+
chatCompletions: '/v1/chat/completions',
|
|
27
|
+
catalog: '/v1/catalog',
|
|
28
|
+
wallet: '/v1/wallet',
|
|
29
|
+
usage: '/v1/usage',
|
|
30
|
+
search: '/v1/search'
|
|
31
|
+
} as const
|
|
32
|
+
|
|
33
|
+
const urlSchema = v.pipe(v.string(), v.url())
|
|
34
|
+
const creditsSchema = v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
35
|
+
|
|
36
|
+
export const cloudServicesDiscoverySchema = v.object({
|
|
37
|
+
endpoint: urlSchema,
|
|
38
|
+
contractVersion: v.literal(CLOUD_SERVICES_CONTRACT_VERSION)
|
|
39
|
+
})
|
|
40
|
+
export type CloudServicesDiscovery = v.InferOutput<typeof cloudServicesDiscoverySchema>
|
|
41
|
+
|
|
42
|
+
export const createServicesTokenSchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) })
|
|
43
|
+
export type CreateServicesTokenInput = v.InferOutput<typeof createServicesTokenSchema>
|
|
44
|
+
|
|
45
|
+
export function parseCreateServicesToken(input: unknown): CreateServicesTokenInput {
|
|
46
|
+
return v.parse(createServicesTokenSchema, input)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const servicesTokenSchema = v.object({
|
|
50
|
+
token: v.pipe(v.string(), v.minLength(1)),
|
|
51
|
+
expiresAt: v.pipe(v.string(), v.isoTimestamp())
|
|
52
|
+
})
|
|
53
|
+
export type ServicesToken = v.InferOutput<typeof servicesTokenSchema>
|
|
54
|
+
|
|
55
|
+
/** What a service may rely on once a token's signature, issuer, audience, and expiry check. */
|
|
56
|
+
export const servicesTokenClaimsSchema = v.object({
|
|
57
|
+
iss: urlSchema,
|
|
58
|
+
aud: v.literal(CLOUD_SERVICES_TOKEN_AUDIENCE),
|
|
59
|
+
sub: v.pipe(v.string(), v.minLength(1)),
|
|
60
|
+
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
61
|
+
/**
|
|
62
|
+
* The account whose credits the request spends: the workspace's owner, whose allowance every
|
|
63
|
+
* workspace they own shares.
|
|
64
|
+
*/
|
|
65
|
+
accountId: v.pipe(v.string(), v.minLength(1)),
|
|
66
|
+
/** Credits the owner's plan grants each month; `null` when Cloud sets no allowance. */
|
|
67
|
+
monthlyCreditAllowance: v.nullable(creditsSchema),
|
|
68
|
+
iat: v.pipe(v.number(), v.integer()),
|
|
69
|
+
exp: v.pipe(v.number(), v.integer())
|
|
70
|
+
})
|
|
71
|
+
export type ServicesTokenClaims = v.InferOutput<typeof servicesTokenClaimsSchema>
|
|
72
|
+
|
|
73
|
+
export const chatModelSchema = v.object({
|
|
74
|
+
id: v.pipe(v.string(), v.minLength(1)),
|
|
75
|
+
label: v.pipe(v.string(), v.minLength(1)),
|
|
76
|
+
/** Credits per million input and output tokens. */
|
|
77
|
+
creditsPerMillionInputTokens: creditsSchema,
|
|
78
|
+
creditsPerMillionOutputTokens: creditsSchema,
|
|
79
|
+
contextTokens: v.pipe(v.number(), v.integer(), v.minValue(1)),
|
|
80
|
+
capabilities: v.object({ reasoning: v.boolean(), vision: v.boolean(), tools: v.boolean() })
|
|
81
|
+
})
|
|
82
|
+
export type ChatModel = v.InferOutput<typeof chatModelSchema>
|
|
83
|
+
|
|
84
|
+
/** What one web search costs; a service without search leaves it out of its catalog. */
|
|
85
|
+
export const searchPricingSchema = v.object({ creditsPerRequest: creditsSchema })
|
|
86
|
+
export type SearchPricing = v.InferOutput<typeof searchPricingSchema>
|
|
87
|
+
|
|
88
|
+
export const serviceCatalogSchema = v.object({
|
|
89
|
+
models: v.array(chatModelSchema),
|
|
90
|
+
defaultModelID: v.pipe(v.string(), v.minLength(1)),
|
|
91
|
+
search: v.optional(searchPricingSchema)
|
|
92
|
+
})
|
|
93
|
+
export type ServiceCatalog = v.InferOutput<typeof serviceCatalogSchema>
|
|
94
|
+
|
|
95
|
+
export const creditBucketSchema = v.object({
|
|
96
|
+
kind: v.picklist(['allowance', 'purchased', 'grant']),
|
|
97
|
+
remaining: creditsSchema,
|
|
98
|
+
expiresAt: v.nullable(v.pipe(v.string(), v.isoTimestamp()))
|
|
99
|
+
})
|
|
100
|
+
export type CreditBucket = v.InferOutput<typeof creditBucketSchema>
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A service records a request's charge before the request's answer ends, so a wallet read after
|
|
104
|
+
* an answer includes what it cost.
|
|
105
|
+
*/
|
|
106
|
+
export const creditWalletSchema = v.object({
|
|
107
|
+
/** Credits left to spend; `null` when the service sets no limit. */
|
|
108
|
+
balance: v.nullable(creditsSchema),
|
|
109
|
+
/** Spent first to last. */
|
|
110
|
+
buckets: v.array(creditBucketSchema)
|
|
111
|
+
})
|
|
112
|
+
export type CreditWallet = v.InferOutput<typeof creditWalletSchema>
|
|
113
|
+
|
|
114
|
+
export const usageEntrySchema = v.object({
|
|
115
|
+
id: v.pipe(v.string(), v.minLength(1)),
|
|
116
|
+
createdAt: v.pipe(v.string(), v.isoTimestamp()),
|
|
117
|
+
kind: v.picklist(['model', 'image', 'search']),
|
|
118
|
+
modelID: v.nullable(v.string()),
|
|
119
|
+
userId: v.pipe(v.string(), v.minLength(1)),
|
|
120
|
+
credits: creditsSchema
|
|
121
|
+
})
|
|
122
|
+
export type UsageEntry = v.InferOutput<typeof usageEntrySchema>
|
|
123
|
+
|
|
124
|
+
/** The most results one search returns. */
|
|
125
|
+
export const MAX_SEARCH_RESULTS = 10
|
|
126
|
+
/** The longest query a search takes, in characters. */
|
|
127
|
+
export const MAX_SEARCH_QUERY_LENGTH = 1_000
|
|
128
|
+
|
|
129
|
+
export const searchRequestSchema = v.object({
|
|
130
|
+
query: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(MAX_SEARCH_QUERY_LENGTH)),
|
|
131
|
+
numResults: v.optional(
|
|
132
|
+
v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(MAX_SEARCH_RESULTS)),
|
|
133
|
+
5
|
|
134
|
+
)
|
|
135
|
+
})
|
|
136
|
+
export type SearchRequestInput = v.InferInput<typeof searchRequestSchema>
|
|
137
|
+
export type SearchRequest = v.InferOutput<typeof searchRequestSchema>
|
|
138
|
+
|
|
139
|
+
export function parseSearchRequest(input: unknown): SearchRequest {
|
|
140
|
+
return v.parse(searchRequestSchema, input)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** One page a search found, with the part of its text that answers the query. */
|
|
144
|
+
export const searchResultSchema = v.object({
|
|
145
|
+
// Only web pages: editors open it when clicked, so a `javascript:` URL must never pass.
|
|
146
|
+
url: v.pipe(urlSchema, v.regex(/^https?:\/\//i, 'A search result links to an HTTP(S) page')),
|
|
147
|
+
title: v.string(),
|
|
148
|
+
publishedAt: v.nullable(v.pipe(v.string(), v.isoTimestamp())),
|
|
149
|
+
author: v.nullable(v.string()),
|
|
150
|
+
text: v.string()
|
|
151
|
+
})
|
|
152
|
+
export type SearchResult = v.InferOutput<typeof searchResultSchema>
|
|
153
|
+
|
|
154
|
+
export const searchResponseSchema = v.object({ results: v.array(searchResultSchema) })
|
|
155
|
+
export type SearchResponse = v.InferOutput<typeof searchResponseSchema>
|
|
156
|
+
|
|
157
|
+
export const usageQuerySchema = v.object({
|
|
158
|
+
cursor: v.optional(v.pipe(v.string(), v.minLength(1)))
|
|
159
|
+
})
|
|
160
|
+
export type UsageQuery = v.InferOutput<typeof usageQuerySchema>
|
|
161
|
+
|
|
162
|
+
export function parseUsageQuery(input: unknown): UsageQuery {
|
|
163
|
+
return v.parse(usageQuerySchema, input)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const usagePageSchema = v.object({
|
|
167
|
+
entries: v.array(usageEntrySchema),
|
|
168
|
+
next: v.nullable(v.string())
|
|
169
|
+
})
|
|
170
|
+
export type UsagePage = v.InferOutput<typeof usagePageSchema>
|
|
171
|
+
|
|
172
|
+
/** Errors a service answers with, beside the upstream provider's own. */
|
|
173
|
+
export const serviceErrorCodeSchema = v.picklist([
|
|
174
|
+
'invalid_request',
|
|
175
|
+
'unauthorized',
|
|
176
|
+
'insufficient_credits',
|
|
177
|
+
'model_not_allowed',
|
|
178
|
+
'capability_unavailable',
|
|
179
|
+
'rate_limited'
|
|
180
|
+
])
|
|
181
|
+
export type ServiceErrorCode = v.InferOutput<typeof serviceErrorCodeSchema>
|
|
182
|
+
|
|
183
|
+
export const serviceErrorSchema = v.object({
|
|
184
|
+
error: v.object({
|
|
185
|
+
code: serviceErrorCodeSchema,
|
|
186
|
+
message: v.optional(v.string()),
|
|
187
|
+
/** With `insufficient_credits`: when the workspace's allowance renews, if the service knows. */
|
|
188
|
+
resetsAt: v.optional(v.pipe(v.string(), v.isoTimestamp()))
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
export type ServiceError = v.InferOutput<typeof serviceErrorSchema>
|
|
192
|
+
|
|
193
|
+
export const SERVICE_ERROR_STATUS = {
|
|
194
|
+
invalid_request: 400,
|
|
195
|
+
unauthorized: 401,
|
|
196
|
+
insufficient_credits: 402,
|
|
197
|
+
model_not_allowed: 403,
|
|
198
|
+
/** A capability, such as search, the service does not offer. */
|
|
199
|
+
capability_unavailable: 404,
|
|
200
|
+
rate_limited: 429
|
|
201
|
+
} as const satisfies Record<ServiceErrorCode, number>
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Every route a service answers besides chat completions, which OpenAI-compatible clients call
|
|
205
|
+
* themselves. Editors build a typed client from it with Hono's `hc`; a service's own app must
|
|
206
|
+
* match it, so the two cannot drift apart.
|
|
207
|
+
*/
|
|
208
|
+
export type CloudServicesSchema = JSONRoute<
|
|
209
|
+
'get',
|
|
210
|
+
typeof CLOUD_SERVICES_API_PATHS.catalog,
|
|
211
|
+
BlankInput,
|
|
212
|
+
ServiceCatalog
|
|
213
|
+
> &
|
|
214
|
+
JSONRoute<'get', typeof CLOUD_SERVICES_API_PATHS.wallet, BlankInput, CreditWallet> &
|
|
215
|
+
JSONRoute<'get', typeof CLOUD_SERVICES_API_PATHS.usage, { query: UsageQuery }, UsagePage> &
|
|
216
|
+
JSONRoute<
|
|
217
|
+
'post',
|
|
218
|
+
typeof CLOUD_SERVICES_API_PATHS.search,
|
|
219
|
+
{ json: SearchRequestInput },
|
|
220
|
+
SearchResponse
|
|
221
|
+
>
|
|
222
|
+
|
|
223
|
+
export type CloudServicesAPI = Hono<BlankEnv, CloudServicesSchema>
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Whether a bearer token may travel to `url`: over HTTPS, or in the clear only to a loopback
|
|
227
|
+
* address, for development. Only literal addresses count, since a name such as `localhost` is
|
|
228
|
+
* whatever the resolver answers. Services and billing tokens act as the account until they expire.
|
|
229
|
+
*/
|
|
230
|
+
export function sendsTokenSafely(url: string | URL): boolean {
|
|
231
|
+
const { protocol, hostname } = new URL(url)
|
|
232
|
+
if (protocol === 'https:') return true
|
|
233
|
+
if (protocol !== 'http:') return false
|
|
234
|
+
return hostname === '[::1]' || /^127(\.\d{1,3}){3}$/.test(hostname)
|
|
235
|
+
}
|
package/src/contract/sharing.ts
CHANGED
|
@@ -53,7 +53,10 @@ export function parseRequestSharedImageLinks(input: unknown): RequestSharedImage
|
|
|
53
53
|
|
|
54
54
|
export const resolvedDocumentShareSchema = v.object({
|
|
55
55
|
documentId: v.pipe(v.string(), v.uuid()),
|
|
56
|
+
/** What this person may do; a guest on an edit link may only view where guests may not edit. */
|
|
56
57
|
permission: documentPermissionSchema,
|
|
58
|
+
/** What the link lets someone signed in to the server do. */
|
|
59
|
+
linkPermission: documentPermissionSchema,
|
|
57
60
|
principal: collaborationPrincipalSchema,
|
|
58
61
|
roomEpoch: v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
59
62
|
})
|
|
@@ -89,11 +92,6 @@ export const acceptedInvitationSchema = v.variant('kind', [
|
|
|
89
92
|
])
|
|
90
93
|
export type AcceptedInvitation = v.InferOutput<typeof acceptedInvitationSchema>
|
|
91
94
|
|
|
92
|
-
export const lookupCloudUserSchema = v.object({
|
|
93
|
-
email: v.pipe(v.string(), v.trim(), v.toLowerCase(), v.email())
|
|
94
|
-
})
|
|
95
|
-
export type LookupCloudUserInput = v.InferOutput<typeof lookupCloudUserSchema>
|
|
96
|
-
|
|
97
95
|
export const putDocumentGrantSchema = v.object({ permission: documentPermissionSchema })
|
|
98
96
|
export type PutDocumentGrantInput = v.InferOutput<typeof putDocumentGrantSchema>
|
|
99
97
|
|
|
@@ -162,10 +160,6 @@ export function parseCreateInvitationContinuation(
|
|
|
162
160
|
return v.parse(createInvitationContinuationSchema, input)
|
|
163
161
|
}
|
|
164
162
|
|
|
165
|
-
export function parseLookupCloudUser(input: unknown): LookupCloudUserInput {
|
|
166
|
-
return v.parse(lookupCloudUserSchema, input)
|
|
167
|
-
}
|
|
168
|
-
|
|
169
163
|
export function parseCreateDocumentShare(input: unknown): CreateDocumentShareInput {
|
|
170
164
|
return v.parse(createDocumentShareSchema, input)
|
|
171
165
|
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { createI18n, type Components, type Messages } from '@nanostores/i18n'
|
|
2
|
+
import { mapValues } from 'es-toolkit'
|
|
3
|
+
import { parseAccept } from 'hono/utils/accept'
|
|
4
|
+
import { atom, type StoreValue } from 'nanostores'
|
|
5
|
+
|
|
6
|
+
import ar from './locales/ar.json'
|
|
7
|
+
import de from './locales/de.json'
|
|
8
|
+
import es from './locales/es.json'
|
|
9
|
+
import fr from './locales/fr.json'
|
|
10
|
+
import it from './locales/it.json'
|
|
11
|
+
import ja from './locales/ja.json'
|
|
12
|
+
import pl from './locales/pl.json'
|
|
13
|
+
import ptBR from './locales/pt-br.json'
|
|
14
|
+
import ru from './locales/ru.json'
|
|
15
|
+
import zhCN from './locales/zh-cn.json'
|
|
16
|
+
import { emailMessageDefaults } from './messages'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Each language's translations of `emailMessageDefaults`, bundled so a Worker reads no files. They
|
|
20
|
+
* are what Cloud writes in: a language is supported once its file is here.
|
|
21
|
+
*/
|
|
22
|
+
const TRANSLATIONS = {
|
|
23
|
+
ar,
|
|
24
|
+
de,
|
|
25
|
+
es,
|
|
26
|
+
fr,
|
|
27
|
+
it,
|
|
28
|
+
ja,
|
|
29
|
+
pl,
|
|
30
|
+
'pt-BR': ptBR,
|
|
31
|
+
ru,
|
|
32
|
+
'zh-CN': zhCN
|
|
33
|
+
} satisfies Record<string, Record<string, string>>
|
|
34
|
+
|
|
35
|
+
/** A language Cloud writes email in: English, or one it has translations for. */
|
|
36
|
+
export type EmailLocale = 'en' | keyof typeof TRANSLATIONS
|
|
37
|
+
|
|
38
|
+
/** Every language Cloud writes email in, English first. */
|
|
39
|
+
export const EMAIL_LOCALES: readonly EmailLocale[] = ['en', ...typedKeys(TRANSLATIONS)]
|
|
40
|
+
|
|
41
|
+
function typedKeys<Value extends object>(value: Value): Array<keyof Value & string> {
|
|
42
|
+
return Object.keys(value).filter((key): key is keyof Value & string => key in value)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function isEmailLocale(value: unknown): value is EmailLocale {
|
|
46
|
+
return EMAIL_LOCALES.some((code) => code === value)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A tag's language and script, as Unicode's likely subtags fill them in: `zh-TW` is `zh-Hant`. */
|
|
50
|
+
function languageScript(tag: string): string | null {
|
|
51
|
+
try {
|
|
52
|
+
const { language, script } = new Intl.Locale(tag).maximize()
|
|
53
|
+
return `${language}-${script ?? ''}`
|
|
54
|
+
} catch {
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The first of someone's languages Cloud writes in: the same tag, or the same language in the same
|
|
61
|
+
* script, so `de-AT` reads German and `zh-TW`, written in Traditional, doesn't read Simplified.
|
|
62
|
+
*/
|
|
63
|
+
export function emailLocale(languages: readonly string[]): EmailLocale | null {
|
|
64
|
+
const byTag = new Map(EMAIL_LOCALES.map((code) => [code.toLowerCase(), code]))
|
|
65
|
+
const byScript = new Map(EMAIL_LOCALES.map((code) => [languageScript(code), code]))
|
|
66
|
+
for (const language of languages) {
|
|
67
|
+
const match = byTag.get(language.toLowerCase()) ?? byScript.get(languageScript(language))
|
|
68
|
+
if (match) return match
|
|
69
|
+
}
|
|
70
|
+
return null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Locales written right to left. */
|
|
74
|
+
const RIGHT_TO_LEFT: ReadonlySet<EmailLocale> = new Set<EmailLocale>(['ar'])
|
|
75
|
+
|
|
76
|
+
const RIGHT_TO_LEFT_MARK = String.fromCodePoint(0x200f)
|
|
77
|
+
const FIRST_STRONG_ISOLATE = String.fromCodePoint(0x2068)
|
|
78
|
+
const POP_DIRECTIONAL_ISOLATE = String.fromCodePoint(0x2069)
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Right-to-left messages as the app shows them: each between right-to-left marks, so punctuation
|
|
82
|
+
* after a Latin name ends the sentence on its left, and each placeholder isolated, so a name or
|
|
83
|
+
* address put into it keeps its own order: `report.pdf`, not `pdf.report`.
|
|
84
|
+
*/
|
|
85
|
+
function directed(messages: Record<string, string>): Record<string, string> {
|
|
86
|
+
return mapValues(messages, (message) => {
|
|
87
|
+
const isolated = message.replaceAll(
|
|
88
|
+
/\{\w+\}/g,
|
|
89
|
+
(placeholder) => `${FIRST_STRONG_ISOLATE}${placeholder}${POP_DIRECTIONAL_ISOLATE}`
|
|
90
|
+
)
|
|
91
|
+
return `${RIGHT_TO_LEFT_MARK}${isolated}${RIGHT_TO_LEFT_MARK}`
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type EmailMessages = StoreValue<Messages<typeof emailMessageDefaults>>
|
|
96
|
+
|
|
97
|
+
/** The words, formats, and direction of email written in one locale. */
|
|
98
|
+
export type EmailLocalization = {
|
|
99
|
+
locale: EmailLocale
|
|
100
|
+
dir: 'ltr' | 'rtl'
|
|
101
|
+
t: EmailMessages
|
|
102
|
+
/** A moment in UTC, as Cloud doesn't know the reader's time zone: 13 November 2026, 10:00 UTC. */
|
|
103
|
+
dateTime: (iso: string) => string
|
|
104
|
+
/** Bytes in binary units, as the portal shows them: 300 MB, 10 GB. */
|
|
105
|
+
storage: (bytes: number) => string
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const STORAGE_UNITS = [
|
|
109
|
+
{ unit: 'gigabyte', bytes: 1024 ** 3 },
|
|
110
|
+
{ unit: 'megabyte', bytes: 1024 ** 2 },
|
|
111
|
+
{ unit: 'kilobyte', bytes: 1024 }
|
|
112
|
+
] as const
|
|
113
|
+
|
|
114
|
+
function messages(locale: EmailLocale): EmailMessages {
|
|
115
|
+
const translations: Record<string, Components> =
|
|
116
|
+
locale === 'en'
|
|
117
|
+
? {}
|
|
118
|
+
: {
|
|
119
|
+
[locale]: {
|
|
120
|
+
email: RIGHT_TO_LEFT.has(locale) ? directed(TRANSLATIONS[locale]) : TRANSLATIONS[locale]
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Every translation is in the cache, so nothing is ever fetched.
|
|
124
|
+
const i18n = createI18n(atom(locale), {
|
|
125
|
+
baseLocale: 'en',
|
|
126
|
+
cache: translations,
|
|
127
|
+
get: async () => ({})
|
|
128
|
+
})
|
|
129
|
+
return i18n('email', emailMessageDefaults).get()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const localizations = new Map<EmailLocale, EmailLocalization>()
|
|
133
|
+
|
|
134
|
+
export function emailLocalization(locale: EmailLocale): EmailLocalization {
|
|
135
|
+
const cached = localizations.get(locale)
|
|
136
|
+
if (cached) return cached
|
|
137
|
+
const dateTime = new Intl.DateTimeFormat(locale, {
|
|
138
|
+
year: 'numeric',
|
|
139
|
+
month: 'long',
|
|
140
|
+
day: 'numeric',
|
|
141
|
+
hour: 'numeric',
|
|
142
|
+
minute: '2-digit',
|
|
143
|
+
timeZone: 'UTC',
|
|
144
|
+
timeZoneName: 'short'
|
|
145
|
+
})
|
|
146
|
+
const localization: EmailLocalization = {
|
|
147
|
+
locale,
|
|
148
|
+
dir: RIGHT_TO_LEFT.has(locale) ? 'rtl' : 'ltr',
|
|
149
|
+
t: messages(locale),
|
|
150
|
+
dateTime: (iso) => dateTime.format(new Date(iso)),
|
|
151
|
+
storage: (bytes) => {
|
|
152
|
+
const { unit, bytes: size } = STORAGE_UNITS.find((step) => bytes >= step.bytes) ?? {
|
|
153
|
+
unit: 'byte',
|
|
154
|
+
bytes: 1
|
|
155
|
+
}
|
|
156
|
+
return new Intl.NumberFormat(locale, {
|
|
157
|
+
style: 'unit',
|
|
158
|
+
unit,
|
|
159
|
+
unitDisplay: 'short',
|
|
160
|
+
maximumFractionDigits: 1
|
|
161
|
+
}).format(bytes / size)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
localizations.set(locale, localization)
|
|
165
|
+
return localization
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The language an `Accept-Language` header prefers among Cloud's, or null when it names none. */
|
|
169
|
+
export function acceptedLocale(header: string | null | undefined): EmailLocale | null {
|
|
170
|
+
const languages = parseAccept(header ?? '')
|
|
171
|
+
.filter((language) => language.q > 0)
|
|
172
|
+
.map((language) => language.type)
|
|
173
|
+
return emailLocale(languages)
|
|
174
|
+
}
|
package/src/email/index.ts
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
|
-
export {
|
|
2
|
-
|
|
1
|
+
export {
|
|
2
|
+
acceptedLocale,
|
|
3
|
+
EMAIL_LOCALES,
|
|
4
|
+
emailLocale,
|
|
5
|
+
emailLocalization,
|
|
6
|
+
isEmailLocale,
|
|
7
|
+
type EmailLocale,
|
|
8
|
+
type EmailLocalization,
|
|
9
|
+
type EmailMessages
|
|
10
|
+
} from './i18n'
|
|
11
|
+
export { emailMessageDefaults } from './messages'
|
|
12
|
+
export { renderTransactionalEmail, renderWaitlistConfirmation } from './render'
|
|
13
|
+
export { EMAIL_SAMPLE_URL, EMAIL_SAMPLES } from './samples'
|
|
3
14
|
export { transactionalEmailContentSchema } from './schema'
|
|
4
15
|
export type {
|
|
5
16
|
AdminEnrollmentNotificationPayload,
|