@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/README.md +2 -1
  2. package/dist/bin/server.mjs +1 -1
  3. package/dist/bin/server.mjs.map +1 -1
  4. package/dist/bin/services.mjs +1 -1
  5. package/dist/bin/services.mjs.map +1 -1
  6. package/dist/client.d.ts +296 -27
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/client.js +649 -295
  9. package/dist/client.js.map +1 -1
  10. package/dist/contract.d.ts +2 -2
  11. package/dist/contract.js +2 -2
  12. package/dist/contract2.js +331 -21
  13. package/dist/contract2.js.map +1 -1
  14. package/dist/email.d.ts +1773 -32
  15. package/dist/email.d.ts.map +1 -1
  16. package/dist/email.js +3478 -288
  17. package/dist/email.js.map +1 -1
  18. package/dist/errors.d.ts +2 -0
  19. package/dist/errors.js +48 -0
  20. package/dist/errors.js.map +1 -0
  21. package/dist/index.d.ts +466 -13
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index2.d.ts +35 -0
  24. package/dist/index2.d.ts.map +1 -0
  25. package/dist/listen.mjs +447 -290
  26. package/dist/listen.mjs.map +1 -1
  27. package/dist/runtime-cloudflare.d.ts +89 -0
  28. package/dist/runtime-cloudflare.d.ts.map +1 -0
  29. package/dist/runtime-cloudflare.js +15667 -0
  30. package/dist/runtime-cloudflare.js.map +1 -0
  31. package/dist/runtime-node.d.mts +2963 -294
  32. package/dist/runtime-node.d.mts.map +1 -1
  33. package/dist/server.d.ts +3473 -842
  34. package/dist/server.d.ts.map +1 -1
  35. package/dist/server.js +12187 -8244
  36. package/dist/server.js.map +1 -1
  37. package/dist/server.mjs +7739 -3548
  38. package/dist/server.mjs.map +1 -1
  39. package/dist/server2.mjs +5 -5
  40. package/dist/server2.mjs.map +1 -1
  41. package/package.json +22 -5
  42. package/src/admin/audit/service.ts +2 -2
  43. package/src/admin/documents/service.ts +76 -6
  44. package/src/admin/reports/service.ts +7 -1
  45. package/src/client/api.ts +23 -471
  46. package/src/client/auth/client.ts +22 -3
  47. package/src/client/billing.ts +80 -0
  48. package/src/client/community.ts +40 -0
  49. package/src/client/discovery.ts +35 -9
  50. package/src/client/documents.ts +160 -0
  51. package/src/client/index.ts +7 -0
  52. package/src/client/portal/account.ts +54 -2
  53. package/src/client/portal/request.ts +2 -0
  54. package/src/client/response.ts +21 -0
  55. package/src/client/schema.ts +8 -0
  56. package/src/client/services.ts +23 -18
  57. package/src/client/sharing.ts +262 -0
  58. package/src/client/versions.ts +104 -0
  59. package/src/contract/account.ts +50 -0
  60. package/src/contract/billing.ts +166 -0
  61. package/src/contract/collaboration.ts +5 -0
  62. package/src/contract/community.ts +60 -0
  63. package/src/contract/discovery.ts +63 -2
  64. package/src/contract/entitlements.ts +10 -0
  65. package/src/contract/index.ts +54 -5
  66. package/src/contract/links.ts +57 -4
  67. package/src/contract/locale.ts +27 -0
  68. package/src/contract/portal.ts +12 -1
  69. package/src/contract/routes.ts +10 -0
  70. package/src/contract/services.ts +18 -16
  71. package/src/email/i18n.ts +174 -0
  72. package/src/email/index.ts +13 -2
  73. package/src/email/locales/ar.json +141 -0
  74. package/src/email/locales/de.json +141 -0
  75. package/src/email/locales/es.json +141 -0
  76. package/src/email/locales/fr.json +141 -0
  77. package/src/email/locales/it.json +141 -0
  78. package/src/email/locales/ja.json +141 -0
  79. package/src/email/locales/pl.json +141 -0
  80. package/src/email/locales/pt-br.json +141 -0
  81. package/src/email/locales/ru.json +141 -0
  82. package/src/email/locales/zh-cn.json +141 -0
  83. package/src/email/messages.ts +200 -0
  84. package/src/email/render.ts +142 -276
  85. package/src/email/samples.ts +115 -0
  86. package/src/email/schema.ts +70 -6
  87. package/src/email/templates/BrandEmail.vue +54 -0
  88. package/src/email/templates/DocumentBlocked.vue +45 -0
  89. package/src/email/templates/DocumentInvitation.vue +24 -0
  90. package/src/email/templates/DocumentReport.vue +32 -0
  91. package/src/email/templates/DocumentReportDecided.vue +36 -0
  92. package/src/email/templates/DocumentReportReceived.vue +18 -0
  93. package/src/email/templates/DocumentRestored.vue +24 -0
  94. package/src/email/templates/EmailChangeRequested.vue +24 -0
  95. package/src/email/templates/EmailRoot.vue +19 -0
  96. package/src/email/templates/EmailText.vue +9 -0
  97. package/src/email/templates/EmailVerification.vue +38 -0
  98. package/src/email/templates/EnrollmentNotification.vue +24 -0
  99. package/src/email/templates/EnrollmentUpdate.vue +52 -0
  100. package/src/email/templates/NewSignIn.vue +37 -0
  101. package/src/email/templates/PasswordChanged.vue +22 -0
  102. package/src/email/templates/PasswordReset.vue +23 -0
  103. package/src/email/templates/StorageLimitReached.vue +28 -0
  104. package/src/email/templates/TwoStepChanged.vue +32 -0
  105. package/src/email/templates/WaitlistConfirmation.vue +29 -0
  106. package/src/email/templates/WorkspaceInvitation.vue +28 -0
  107. package/src/email/templates/brand.ts +9 -61
  108. package/src/email/templates/localization.ts +17 -0
  109. package/src/errors/index.ts +6 -0
  110. package/src/global.d.ts +7 -0
  111. package/src/runtime/cloudflare/relay/index.ts +7 -1
  112. package/src/runtime/cloudflare/tsconfig.json +2 -1
  113. package/src/runtime/cloudflare/worker.ts +8 -1
  114. package/src/runtime/node/cleanup.ts +5 -1
  115. package/src/runtime/node/email/transport.ts +13 -10
  116. package/src/runtime/node/main.ts +1 -1
  117. package/src/runtime/node/server.ts +12 -2
  118. package/src/runtime/node/services/main.ts +1 -1
  119. package/src/runtime/posthog/errors.ts +2 -1
  120. package/src/runtime/s3/objects.ts +17 -0
  121. package/src/runtime/site/community.ts +101 -0
  122. package/src/runtime/site/server.ts +17 -1
  123. package/src/server/account/deletion.ts +2 -0
  124. package/src/server/account/routes.ts +271 -240
  125. package/src/server/account/tokens.ts +57 -0
  126. package/src/server/api/router.ts +23 -0
  127. package/src/server/app/checks.ts +84 -0
  128. package/src/server/app/editors.ts +20 -0
  129. package/src/server/app/index.ts +128 -0
  130. package/src/server/app/indexing.ts +40 -0
  131. package/src/server/app/limiters.ts +53 -0
  132. package/src/server/app/routes.ts +114 -0
  133. package/src/server/app/services.ts +185 -0
  134. package/src/server/auth/access-tokens.ts +62 -0
  135. package/src/server/auth/adapter.ts +21 -1
  136. package/src/server/auth/email.ts +4 -1
  137. package/src/server/auth/enrollment.ts +116 -0
  138. package/src/server/auth/factory.ts +88 -131
  139. package/src/server/auth/logger.ts +23 -0
  140. package/src/server/auth/mfa-assurance.ts +51 -0
  141. package/src/server/auth/passwords.ts +43 -0
  142. package/src/server/auth/session.ts +2 -0
  143. package/src/server/billing/index.ts +6 -0
  144. package/src/server/billing/routes.ts +27 -0
  145. package/src/server/billing/tokens.ts +37 -0
  146. package/src/server/cleanup/configured.ts +16 -3
  147. package/src/server/cleanup/worker.ts +10 -1
  148. package/src/server/collaboration/relay/hub.ts +25 -142
  149. package/src/server/collaboration/relay/room.ts +154 -0
  150. package/src/server/collaboration/service.ts +21 -2
  151. package/src/server/community/index.ts +2 -0
  152. package/src/server/community/routes.ts +93 -0
  153. package/src/server/community/service.ts +134 -0
  154. package/src/server/config/deployment.ts +17 -5
  155. package/src/server/config/index.ts +1 -1
  156. package/src/server/config/public.ts +7 -1
  157. package/src/server/config/schema.ts +40 -292
  158. package/src/server/config/toml/schema.ts +307 -0
  159. package/src/server/db/index.ts +1 -0
  160. package/src/server/db/migrate.ts +13 -1
  161. package/src/server/db/migrations/009_access_tokens.ts +47 -0
  162. package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
  163. package/src/server/db/migrations/011_account_ids.ts +30 -0
  164. package/src/server/db/migrations/012_email_locales.ts +34 -0
  165. package/src/server/db/migrations/013_document_block_emails.ts +40 -0
  166. package/src/server/db/migrations/014_report_replies.ts +28 -0
  167. package/src/server/db/migrations/schema.ts +26 -1
  168. package/src/server/db/tables.ts +30 -1
  169. package/src/server/db/users.ts +22 -0
  170. package/src/server/documents/index.ts +1 -1
  171. package/src/server/documents/limits.ts +7 -1
  172. package/src/server/documents/service.ts +12 -402
  173. package/src/server/documents/uploads.ts +450 -0
  174. package/src/server/documents/versions/service.ts +81 -24
  175. package/src/server/email/invitations.ts +1 -0
  176. package/src/server/email/service.ts +31 -4
  177. package/src/server/email/types.ts +12 -0
  178. package/src/server/errors/index.ts +1 -9
  179. package/src/server/index.ts +12 -5
  180. package/src/server/invitations/types.ts +5 -1
  181. package/src/server/objects/types.ts +5 -0
  182. package/src/server/policy/configured.ts +9 -3
  183. package/src/server/policy/database.ts +13 -2
  184. package/src/server/policy/policy.ts +39 -12
  185. package/src/server/policy/service.ts +71 -52
  186. package/src/server/policy/storage.ts +33 -0
  187. package/src/server/quota/index.ts +6 -0
  188. package/src/server/quota/notices.ts +121 -0
  189. package/src/server/quota/storage.ts +5 -0
  190. package/src/server/rate-limit/middleware.ts +4 -0
  191. package/src/server/reports/index.ts +1 -0
  192. package/src/server/reports/replies.ts +55 -0
  193. package/src/server/reports/routes.ts +3 -1
  194. package/src/server/reports/service.ts +24 -2
  195. package/src/server/sharing/capabilities/service.ts +2 -3
  196. package/src/server/sharing/invitations/service.ts +14 -4
  197. package/src/server/validation.ts +3 -0
  198. package/src/server/workspaces/members.ts +7 -19
  199. package/src/services/app.ts +1 -3
  200. package/src/services/config.ts +1 -0
  201. package/src/services/usage.ts +6 -14
  202. package/src/email/templates/document-invitation.ts +0 -35
  203. package/src/email/templates/enrollment.ts +0 -27
  204. package/src/email/templates/layout.ts +0 -18
  205. package/src/server/app.ts +0 -453
  206. /package/src/{server/errors → errors}/posthog.ts +0 -0
@@ -1,6 +1,7 @@
1
1
  import * as v from 'valibot'
2
2
 
3
- import { cloudServicesDiscoverySchema, cloudBillingDiscoverySchema } from './services'
3
+ import { cloudBillingDiscoverySchema } from './billing'
4
+ import { cloudServicesDiscoverySchema } from './services'
4
5
 
5
6
  export const CLOUD_PROTOCOL_VERSION = '1' as const
6
7
  export const CLOUD_DISCOVERY_PATH = '/.well-known/openpencil' as const
@@ -46,6 +47,33 @@ export type CloudCapabilities = v.InferOutput<typeof cloudCapabilitiesSchema>
46
47
  /** A released editor version, `major.minor.patch`. */
47
48
  export const cloudEditorVersionSchema = v.pipe(v.string(), v.regex(/^\d+\.\d+\.\d+$/))
48
49
 
50
+ /**
51
+ * The header desktop editors send with each request, naming their version, so a server can refuse
52
+ * an editor older than its `minimumEditorVersion`. Browser editors leave it out: the server serves
53
+ * them, and an older server's CORS rules would refuse a header they don't list.
54
+ */
55
+ export const CLOUD_EDITOR_VERSION_HEADER = 'X-OpenPencil-Editor-Version' as const
56
+
57
+ function editorVersionParts(version: string): number[] | null {
58
+ const match = /^(\d+)\.(\d+)\.(\d+)/.exec(version)
59
+ return match ? match.slice(1).map(Number) : null
60
+ }
61
+
62
+ /**
63
+ * Whether `version` is `minimum` or later, comparing `major.minor.patch` numerically; a prerelease
64
+ * counts as its release. A version that isn't one, on either side, is not at least anything.
65
+ */
66
+ export function isEditorVersionAtLeast(version: string, minimum: string): boolean {
67
+ const have = editorVersionParts(version)
68
+ const need = editorVersionParts(minimum)
69
+ if (!have || !need) return false
70
+ for (let index = 0; index < need.length; index++) {
71
+ const difference = (have[index] ?? 0) - (need[index] ?? 0)
72
+ if (difference !== 0) return difference > 0
73
+ }
74
+ return true
75
+ }
76
+
49
77
  /**
50
78
  * Whether editors offer this server to everyone, and from which editor version on. Only the
51
79
  * official server's listing is read: it turns the Cloud entry points on without an editor release,
@@ -71,6 +99,31 @@ export const cloudLegalDiscoverySchema = v.object({
71
99
  })
72
100
  export type CloudLegalDiscovery = v.InferOutput<typeof cloudLegalDiscoverySchema>
73
101
 
102
+ /** A web address a page may link or load: `http` or `https` only, never a script or data URL. */
103
+ const webURLSchema = v.pipe(
104
+ v.string(),
105
+ v.url(),
106
+ v.check((value) => ['http:', 'https:'].includes(new URL(value).protocol))
107
+ )
108
+
109
+ /**
110
+ * How a self-hosted server names itself on its public pages: the operator's name and logo in the
111
+ * bar, and where its people get help. The official server is OpenPencil's own and names none.
112
+ */
113
+ export const cloudBrandingSchema = v.object({
114
+ name: v.optional(v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(80))),
115
+ logoURL: v.optional(webURLSchema),
116
+ helpURL: v.optional(webURLSchema)
117
+ })
118
+ export type CloudBranding = v.InferOutput<typeof cloudBrandingSchema>
119
+
120
+ /**
121
+ * The server's community gallery: the workspace whose documents anyone may open read-only.
122
+ * Saving to it publishes, so editors never choose it for anyone and say so when it is chosen.
123
+ */
124
+ export const cloudCommunityDiscoverySchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) })
125
+ export type CloudCommunityDiscovery = v.InferOutput<typeof cloudCommunityDiscoverySchema>
126
+
74
127
  export const cloudDiscoverySchema = v.object({
75
128
  protocolVersion: v.literal(CLOUD_PROTOCOL_VERSION),
76
129
  deployment: cloudDeploymentSchema,
@@ -81,11 +134,19 @@ export const cloudDiscoverySchema = v.object({
81
134
  authentication: cloudAuthenticationSchema,
82
135
  capabilities: cloudCapabilitiesSchema,
83
136
  listing: v.optional(cloudListingSchema),
137
+ /**
138
+ * The oldest editor version the server works with. Older editors ask to be updated instead of
139
+ * connecting, and the server refuses their requests.
140
+ */
141
+ minimumEditorVersion: v.optional(cloudEditorVersionSchema),
84
142
  /** The services editors reach with Cloud-signed tokens; absent when the deployment has none. */
85
143
  services: v.optional(cloudServicesDiscoverySchema),
86
144
  /** Where people manage their plan and buy credits; absent when the deployment does not bill. */
87
145
  billing: v.optional(cloudBillingDiscoverySchema),
88
- legal: v.optional(cloudLegalDiscoverySchema)
146
+ /** The community gallery; absent when the deployment has none. */
147
+ community: v.optional(cloudCommunityDiscoverySchema),
148
+ legal: v.optional(cloudLegalDiscoverySchema),
149
+ branding: v.optional(cloudBrandingSchema)
89
150
  })
90
151
  export type CloudDiscovery = v.InferOutput<typeof cloudDiscoverySchema>
91
152
 
@@ -26,3 +26,13 @@ export const workspaceEntitlementsSchema = v.object({
26
26
  })
27
27
  })
28
28
  export type WorkspaceEntitlements = v.InferOutput<typeof workspaceEntitlementsSchema>
29
+
30
+ /**
31
+ * An account's own plan and the storage its workspaces hold together. A workspace reports the
32
+ * same for its owner, so the two read alike.
33
+ */
34
+ export const accountEntitlementsSchema = workspaceEntitlementsSchema
35
+ export type AccountEntitlements = WorkspaceEntitlements
36
+ export const accountEntitlementsResponseSchema = v.object({
37
+ entitlements: accountEntitlementsSchema
38
+ })
@@ -1,4 +1,5 @@
1
1
  export * from './account'
2
+ export * from './locale'
2
3
  export {
3
4
  SERVICE_ERROR_STATUS,
4
5
  serviceCatalogSchema,
@@ -23,7 +24,6 @@ export {
23
24
  CLOUD_SERVICES_JWKS_PATH,
24
25
  CLOUD_SERVICES_TOKEN_AUDIENCE,
25
26
  cloudServicesDiscoverySchema,
26
- cloudBillingDiscoverySchema,
27
27
  createServicesTokenSchema,
28
28
  parseUsageQuery,
29
29
  parseSearchRequest,
@@ -45,11 +45,42 @@ export {
45
45
  type SearchResult,
46
46
  type SearchResponse,
47
47
  type CloudServicesDiscovery,
48
- type CloudBillingDiscovery,
49
48
  type CreateServicesTokenInput,
50
49
  type CloudServicesAPI,
51
- type CloudServicesSchema
50
+ type CloudServicesSchema,
51
+ sendsTokenSafely
52
52
  } from './services'
53
+ export {
54
+ BILLING_ERROR_STATUS,
55
+ CLOUD_ACCOUNT_PLAN_PATH,
56
+ CLOUD_BILLING_API_PATHS,
57
+ CLOUD_BILLING_CONTRACT_VERSION,
58
+ CLOUD_BILLING_TOKEN_AUDIENCE,
59
+ billingAccountSchema,
60
+ billingCatalogSchema,
61
+ billingErrorCodeSchema,
62
+ billingErrorSchema,
63
+ billingPlanSchema,
64
+ billingRedirectSchema,
65
+ billingTokenClaimsSchema,
66
+ billingTokenSchema,
67
+ checkoutRequestSchema,
68
+ cloudBillingDiscoverySchema,
69
+ creditPackSchema,
70
+ type BillingAccount,
71
+ type BillingCatalog,
72
+ type BillingError,
73
+ type BillingErrorCode,
74
+ type BillingPlan,
75
+ type BillingRedirect,
76
+ type BillingToken,
77
+ type BillingTokenClaims,
78
+ type CheckoutRequest,
79
+ type CloudBillingAPI,
80
+ type CloudBillingDiscovery,
81
+ type CloudBillingSchema,
82
+ type CreditPack
83
+ } from './billing'
53
84
  export * from './admin'
54
85
  export {
55
86
  cloudPresenceSchema,
@@ -60,22 +91,34 @@ export {
60
91
  type CollaborationTicket
61
92
  } from './collaboration'
62
93
  export * from './relay'
63
- export { workspaceEntitlementsSchema, type WorkspaceEntitlements } from './entitlements'
94
+ export {
95
+ accountEntitlementsResponseSchema,
96
+ accountEntitlementsSchema,
97
+ workspaceEntitlementsSchema,
98
+ type AccountEntitlements,
99
+ type WorkspaceEntitlements
100
+ } from './entitlements'
64
101
  export {
65
102
  CLOUD_DISCOVERY_PATH,
103
+ CLOUD_EDITOR_VERSION_HEADER,
66
104
  CLOUD_PROTOCOL_VERSION,
67
105
  cloudAuthenticationSchema,
106
+ cloudBrandingSchema,
68
107
  cloudCapabilitiesSchema,
69
108
  cloudDeploymentSchema,
109
+ cloudCommunityDiscoverySchema,
70
110
  cloudDiscoverySchema,
71
111
  cloudEditorVersionSchema,
72
112
  cloudLegalDiscoverySchema,
73
113
  cloudListingSchema,
74
114
  cloudServerOrigin,
115
+ isEditorVersionAtLeast,
75
116
  parseCloudDiscovery,
76
117
  type CloudAuthentication,
118
+ type CloudBranding,
77
119
  type CloudCapabilities,
78
120
  type CloudDeployment,
121
+ type CloudCommunityDiscovery,
79
122
  type CloudDiscovery,
80
123
  type CloudLegalDiscovery,
81
124
  type CloudListing
@@ -208,17 +251,23 @@ export {
208
251
  type WorkspaceSummary
209
252
  } from './workspaces'
210
253
  export {
254
+ CLOUD_COMMUNITY_PATH,
255
+ cloudCommunityDesktopLinkURL,
256
+ cloudCommunityPagePath,
211
257
  cloudDesktopLinkURL,
212
258
  cloudDocumentPath,
213
259
  cloudDocumentURL,
214
260
  cloudInvitationURL,
215
261
  cloudShareURL,
216
262
  parseCloudLinkPath,
263
+ cloudCommunityPath,
264
+ cloudCommunityURL,
217
265
  type CloudLinkKind,
218
266
  type CloudLinkPathKind
219
267
  } from './links'
220
- export { CLOUD_PORTAL_PAGE_ROOTS, isCloudPortalPage } from './portal'
268
+ export { CLOUD_PORTAL_COMMUNITY_PAGES, CLOUD_PORTAL_PAGE_ROOTS, isCloudPortalPage } from './portal'
221
269
  export * from './versions'
270
+ export * from './community'
222
271
  export {
223
272
  DOCUMENT_REPORT_DESCRIPTION_MAX,
224
273
  documentReportReasonSchema,
@@ -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
- /** Every kind of Cloud link: those that carry a secret, and a document's address for members. */
7
- export type CloudLinkPathKind = CloudLinkKind | 'documents'
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 [, kind, id] = pathname.match(CLOUD_LINK_PATH) ?? []
66
- if ((kind !== 'invitations' && kind !== 'share' && kind !== 'documents') || !id) return null
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
+ }
@@ -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 CLOUD_PORTAL_PAGE_ROOTS.some((root) => path === root || path.startsWith(`${root}/`))
18
+ return (
19
+ COMMUNITY_PAGE.test(path) ||
20
+ CLOUD_PORTAL_PAGE_ROOTS.some((root) => path === root || path.startsWith(`${root}/`))
21
+ )
11
22
  }
@@ -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'>>
@@ -1,7 +1,9 @@
1
- import type { Hono, TypedResponse } from 'hono'
2
- import type { BlankEnv, BlankInput, ToSchema } from 'hono/types'
1
+ import type { Hono } from 'hono'
2
+ import type { BlankEnv, BlankInput } from 'hono/types'
3
3
  import * as v from 'valibot'
4
4
 
5
+ import type { JSONRoute } from './routes'
6
+
5
7
  /**
6
8
  * The contract between editors, Cloud, and the services Cloud advertises: AI models today, and
7
9
  * other paid capabilities such as image generation and search as they arrive. Cloud only
@@ -37,9 +39,6 @@ export const cloudServicesDiscoverySchema = v.object({
37
39
  })
38
40
  export type CloudServicesDiscovery = v.InferOutput<typeof cloudServicesDiscoverySchema>
39
41
 
40
- export const cloudBillingDiscoverySchema = v.object({ portalURL: urlSchema })
41
- export type CloudBillingDiscovery = v.InferOutput<typeof cloudBillingDiscoverySchema>
42
-
43
42
  export const createServicesTokenSchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) })
44
43
  export type CreateServicesTokenInput = v.InferOutput<typeof createServicesTokenSchema>
45
44
 
@@ -61,10 +60,9 @@ export const servicesTokenClaimsSchema = v.object({
61
60
  workspaceId: v.pipe(v.string(), v.uuid()),
62
61
  /**
63
62
  * The account whose credits the request spends: the workspace's owner, whose allowance every
64
- * workspace they own shares. Absent from tokens a Cloud from before account plans issued, which
65
- * a relay takes as the workspace's own allowance, as such a Cloud meant.
63
+ * workspace they own shares.
66
64
  */
67
- accountId: v.optional(v.pipe(v.string(), v.minLength(1))),
65
+ accountId: v.pipe(v.string(), v.minLength(1)),
68
66
  /** Credits the owner's plan grants each month; `null` when Cloud sets no allowance. */
69
67
  monthlyCreditAllowance: v.nullable(creditsSchema),
70
68
  iat: v.pipe(v.number(), v.integer()),
@@ -202,14 +200,6 @@ export const SERVICE_ERROR_STATUS = {
202
200
  rate_limited: 429
203
201
  } as const satisfies Record<ServiceErrorCode, number>
204
202
 
205
- /** A route's input, and the JSON it answers with a 200. */
206
- type JSONRoute<
207
- Method extends string,
208
- Path extends string,
209
- Input extends BlankInput,
210
- Output
211
- > = ToSchema<Method, Path, Input, TypedResponse<Output, 200, 'json'>>
212
-
213
203
  /**
214
204
  * Every route a service answers besides chat completions, which OpenAI-compatible clients call
215
205
  * themselves. Editors build a typed client from it with Hono's `hc`; a service's own app must
@@ -231,3 +221,15 @@ export type CloudServicesSchema = JSONRoute<
231
221
  >
232
222
 
233
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
+ }
@@ -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
+ }
@@ -1,5 +1,16 @@
1
- export { renderTransactionalEmail } from './render'
2
- export { emailLayout, type EmailLayoutProps } from './templates/brand'
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,