@cossackframework/cms 0.0.0 → 0.9.1

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 (427) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +145 -2
  3. package/agent-docs.json +259 -0
  4. package/app/README.md +78 -0
  5. package/app/cossack-recipe.json +43 -0
  6. package/app/src/auth-bot-check.ts +32 -0
  7. package/app/src/auth-bot-token.ts +11 -0
  8. package/app/src/auth-input.ts +35 -0
  9. package/app/src/auth-options.ts +18 -0
  10. package/app/src/auth-providers.ts +16 -0
  11. package/app/src/auth.ts +1082 -0
  12. package/app/src/brand.ts +68 -0
  13. package/app/src/cms/ai.ts +54 -0
  14. package/app/src/cms/analytics.ts +33 -0
  15. package/app/src/cms/block-errors.ts +55 -0
  16. package/app/src/cms/block-inserter.ts +111 -0
  17. package/app/src/cms/block-panel.ts +68 -0
  18. package/app/src/cms/block-rows.ts +44 -0
  19. package/app/src/cms/block-tree.ts +585 -0
  20. package/app/src/cms/blocks.css +708 -0
  21. package/app/src/cms/builder.ts +126 -0
  22. package/app/src/cms/cache.ts +44 -0
  23. package/app/src/cms/carousel.client.ts +52 -0
  24. package/app/src/cms/collab/presence.ts +69 -0
  25. package/app/src/cms/collab/rich-text-collab.client.ts +197 -0
  26. package/app/src/cms/collab/rich-text-notes.client.ts +138 -0
  27. package/app/src/cms/collab/room.ts +881 -0
  28. package/app/src/cms/compare.client.ts +47 -0
  29. package/app/src/cms/condition-control.ts +109 -0
  30. package/app/src/cms/content-api.ts +143 -0
  31. package/app/src/cms/content.ts +524 -0
  32. package/app/src/cms/countdown.client.ts +37 -0
  33. package/app/src/cms/editor-layout-editor.ts +260 -0
  34. package/app/src/cms/editor-layout-view.ts +37 -0
  35. package/app/src/cms/editor.ts +381 -0
  36. package/app/src/cms/entry.ts +26 -0
  37. package/app/src/cms/feeds.ts +145 -0
  38. package/app/src/cms/field-controls.ts +222 -0
  39. package/app/src/cms/field-group-editor.ts +105 -0
  40. package/app/src/cms/field-group-view.ts +211 -0
  41. package/app/src/cms/form-view.ts +203 -0
  42. package/app/src/cms/form.client.ts +203 -0
  43. package/app/src/cms/guards.ts +27 -0
  44. package/app/src/cms/handoff.ts +202 -0
  45. package/app/src/cms/headings.ts +51 -0
  46. package/app/src/cms/icons.ts +80 -0
  47. package/app/src/cms/import.client.ts +16 -0
  48. package/app/src/cms/index.ts +191 -0
  49. package/app/src/cms/lightbox.client.ts +109 -0
  50. package/app/src/cms/machine.ts +118 -0
  51. package/app/src/cms/maintenance.ts +97 -0
  52. package/app/src/cms/manage.ts +1236 -0
  53. package/app/src/cms/mcp-tools.ts +388 -0
  54. package/app/src/cms/mcp.ts +242 -0
  55. package/app/src/cms/media-chooser.ts +123 -0
  56. package/app/src/cms/media-usage.ts +57 -0
  57. package/app/src/cms/middleware.ts +199 -0
  58. package/app/src/cms/middlewares.ts +13 -0
  59. package/app/src/cms/network-ui.ts +36 -0
  60. package/app/src/cms/network-view.ts +38 -0
  61. package/app/src/cms/notes-announce.ts +65 -0
  62. package/app/src/cms/notes-rail.ts +207 -0
  63. package/app/src/cms/notification-rules.ts +66 -0
  64. package/app/src/cms/recovery.ts +82 -0
  65. package/app/src/cms/replace.ts +63 -0
  66. package/app/src/cms/reserved-paths.ts +19 -0
  67. package/app/src/cms/rich-text-field.ts +325 -0
  68. package/app/src/cms/rich-text-schema.ts +91 -0
  69. package/app/src/cms/rich-text.client.ts +255 -0
  70. package/app/src/cms/search.client.ts +116 -0
  71. package/app/src/cms/seo-head.ts +78 -0
  72. package/app/src/cms/seo.ts +241 -0
  73. package/app/src/cms/settings-draft.ts +218 -0
  74. package/app/src/cms/settings-file.ts +127 -0
  75. package/app/src/cms/settings-form.ts +180 -0
  76. package/app/src/cms/settings-languages.ts +125 -0
  77. package/app/src/cms/settings-nav.ts +102 -0
  78. package/app/src/cms/settings-network.ts +151 -0
  79. package/app/src/cms/settings-permalinks.ts +114 -0
  80. package/app/src/cms/settings-scroll.ts +10 -0
  81. package/app/src/cms/settings-search.ts +77 -0
  82. package/app/src/cms/settings-section.ts +38 -0
  83. package/app/src/cms/share.client.ts +27 -0
  84. package/app/src/cms/site-editing.ts +219 -0
  85. package/app/src/cms/site-editor.client.ts +1008 -0
  86. package/app/src/cms/site-editor.ts +99 -0
  87. package/app/src/cms/site-export.ts +60 -0
  88. package/app/src/cms/site-forms.ts +143 -0
  89. package/app/src/cms/site-import.ts +28 -0
  90. package/app/src/cms/site-session.ts +36 -0
  91. package/app/src/cms/site-taskbar.ts +77 -0
  92. package/app/src/cms/site-view.ts +1014 -0
  93. package/app/src/cms/site.ts +1322 -0
  94. package/app/src/cms/sitemaps.ts +131 -0
  95. package/app/src/cms/stat.client.ts +56 -0
  96. package/app/src/cms/style-controls.ts +252 -0
  97. package/app/src/cms/tabs.client.ts +36 -0
  98. package/app/src/cms/taskbar.ts +134 -0
  99. package/app/src/cms/term-icons.ts +10 -0
  100. package/app/src/cms/traffic.ts +26 -0
  101. package/app/src/cms/ui.ts +170 -0
  102. package/app/src/cms/updates.ts +124 -0
  103. package/app/src/cms/uploads.ts +26 -0
  104. package/app/src/cms/worker.ts +13 -0
  105. package/app/src/config/auth.ts +30 -0
  106. package/app/src/config/cms.ts +18 -0
  107. package/app/src/dashboard/access.ts +344 -0
  108. package/app/src/dashboard/agent-icons.ts +44 -0
  109. package/app/src/dashboard/app-passwords.ts +74 -0
  110. package/app/src/dashboard/avatar-field.ts +58 -0
  111. package/app/src/dashboard/bulk.ts +53 -0
  112. package/app/src/dashboard/head.ts +29 -0
  113. package/app/src/dashboard/icons.ts +15 -0
  114. package/app/src/dashboard/list-table.ts +120 -0
  115. package/app/src/dashboard/modules/roles.ts +15 -0
  116. package/app/src/dashboard/modules/sessions.ts +15 -0
  117. package/app/src/dashboard/modules/settings.ts +15 -0
  118. package/app/src/dashboard/modules/users.ts +18 -0
  119. package/app/src/dashboard/page.ts +67 -0
  120. package/app/src/dashboard/registry.ts +13 -0
  121. package/app/src/dashboard/session-list.ts +98 -0
  122. package/app/src/dashboard/tabs.ts +26 -0
  123. package/app/src/dashboard/time-ago.ts +16 -0
  124. package/app/src/dashboard/types.ts +40 -0
  125. package/app/src/dashboard/user-sections.ts +172 -0
  126. package/app/src/lib/permissions.ts +84 -0
  127. package/app/src/middlewares/auth.ts +117 -0
  128. package/app/src/pages/(site)/[...path]/index.ts +609 -0
  129. package/app/src/pages/(site)/feed/[...rest]/index.ts +13 -0
  130. package/app/src/pages/(site)/feed/index.ts +12 -0
  131. package/app/src/pages/(site)/index.ts +11 -0
  132. package/app/src/pages/(site)/indexnow/[file]/index.ts +18 -0
  133. package/app/src/pages/(site)/layout.ts +219 -0
  134. package/app/src/pages/(site)/llms.txt/index.ts +23 -0
  135. package/app/src/pages/(site)/manifest.webmanifest/index.ts +29 -0
  136. package/app/src/pages/(site)/media/[id]/[name]/index.ts +59 -0
  137. package/app/src/pages/(site)/preview/[collection]/[id]/index.ts +115 -0
  138. package/app/src/pages/(site)/robots.txt/index.ts +29 -0
  139. package/app/src/pages/(site)/search/index.ts +36 -0
  140. package/app/src/pages/(site)/sitemap.xml/index.ts +26 -0
  141. package/app/src/pages/(site)/sitemaps/[file]/index.ts +33 -0
  142. package/app/src/pages/api/auth/session.ts +48 -0
  143. package/app/src/pages/api/cms/appearance/areas/[name].ts +12 -0
  144. package/app/src/pages/api/cms/appearance/areas/index.ts +6 -0
  145. package/app/src/pages/api/cms/appearance/locations/[location].ts +6 -0
  146. package/app/src/pages/api/cms/appearance/locations/index.ts +6 -0
  147. package/app/src/pages/api/cms/appearance/menus/[id].ts +12 -0
  148. package/app/src/pages/api/cms/appearance/menus/index.ts +13 -0
  149. package/app/src/pages/api/cms/appearance/themes/[name]/activate.ts +6 -0
  150. package/app/src/pages/api/cms/appearance/themes/index.ts +6 -0
  151. package/app/src/pages/api/cms/cache/purge.ts +6 -0
  152. package/app/src/pages/api/cms/collections/index.ts +6 -0
  153. package/app/src/pages/api/cms/commands/[plugin]/[command].ts +26 -0
  154. package/app/src/pages/api/cms/comments/[id].ts +9 -0
  155. package/app/src/pages/api/cms/comments/index.ts +126 -0
  156. package/app/src/pages/api/cms/comments/queue.ts +6 -0
  157. package/app/src/pages/api/cms/content/[collection]/[entry]/autosave.ts +27 -0
  158. package/app/src/pages/api/cms/content/[collection]/[entry]/blocks/[block].ts +16 -0
  159. package/app/src/pages/api/cms/content/[collection]/[entry]/notes.ts +18 -0
  160. package/app/src/pages/api/cms/content/[collection]/[entry]/publish.ts +11 -0
  161. package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/[revision]/index.ts +6 -0
  162. package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/[revision]/restore.ts +6 -0
  163. package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/index.ts +6 -0
  164. package/app/src/pages/api/cms/content/[collection]/[entry]/unpublish.ts +6 -0
  165. package/app/src/pages/api/cms/content/[collection]/[entry].ts +42 -0
  166. package/app/src/pages/api/cms/content/[collection]/index.ts +24 -0
  167. package/app/src/pages/api/cms/export/[collection].ts +37 -0
  168. package/app/src/pages/api/cms/forms/[slug]/index.ts +173 -0
  169. package/app/src/pages/api/cms/forms/[slug]/submissions/[submission]/files/[file].ts +25 -0
  170. package/app/src/pages/api/cms/forms/[slug]/submissions/export.ts +34 -0
  171. package/app/src/pages/api/cms/forms/[slug]/submissions/index.ts +24 -0
  172. package/app/src/pages/api/cms/import/[collection].ts +23 -0
  173. package/app/src/pages/api/cms/mcp.ts +29 -0
  174. package/app/src/pages/api/cms/me/index.ts +6 -0
  175. package/app/src/pages/api/cms/me/recent-blocks.ts +37 -0
  176. package/app/src/pages/api/cms/me/tokens/[id].ts +6 -0
  177. package/app/src/pages/api/cms/me/tokens/index.ts +13 -0
  178. package/app/src/pages/api/cms/media/[id].ts +6 -0
  179. package/app/src/pages/api/cms/media.ts +26 -0
  180. package/app/src/pages/api/cms/menus/[location].ts +14 -0
  181. package/app/src/pages/api/cms/network/sites/[id]/export.ts +15 -0
  182. package/app/src/pages/api/cms/network/sites/[id]/index.ts +9 -0
  183. package/app/src/pages/api/cms/network/sites/index.ts +14 -0
  184. package/app/src/pages/api/cms/notes/[id].ts +13 -0
  185. package/app/src/pages/api/cms/notes/index.ts +6 -0
  186. package/app/src/pages/api/cms/payments/webhook.ts +28 -0
  187. package/app/src/pages/api/cms/plugins/[name]/grant.ts +6 -0
  188. package/app/src/pages/api/cms/plugins/[name]/revoke.ts +6 -0
  189. package/app/src/pages/api/cms/plugins/index.ts +6 -0
  190. package/app/src/pages/api/cms/recovery.ts +29 -0
  191. package/app/src/pages/api/cms/redirects/index.ts +9 -0
  192. package/app/src/pages/api/cms/roles/index.ts +6 -0
  193. package/app/src/pages/api/cms/schema/[collection].ts +13 -0
  194. package/app/src/pages/api/cms/search.ts +16 -0
  195. package/app/src/pages/api/cms/settings/[group].ts +9 -0
  196. package/app/src/pages/api/cms/settings/index.ts +9 -0
  197. package/app/src/pages/api/cms/site-export.ts +34 -0
  198. package/app/src/pages/api/cms/taxonomies/index.ts +6 -0
  199. package/app/src/pages/api/cms/terms/[taxonomy]/[id].ts +12 -0
  200. package/app/src/pages/api/cms/terms/[taxonomy]/index.ts +9 -0
  201. package/app/src/pages/api/cms/uploads/[id]/complete.ts +15 -0
  202. package/app/src/pages/api/cms/uploads/[id]/index.ts +15 -0
  203. package/app/src/pages/api/cms/uploads/index.ts +26 -0
  204. package/app/src/pages/api/cms/users/[id]/index.ts +12 -0
  205. package/app/src/pages/api/cms/users/[id]/meta.ts +40 -0
  206. package/app/src/pages/api/cms/users/index.ts +12 -0
  207. package/app/src/pages/api/cms/webhooks/[id].ts +6 -0
  208. package/app/src/pages/api/cms/webhooks/index.ts +9 -0
  209. package/app/src/pages/auth/[provider]/callback.ts +11 -0
  210. package/app/src/pages/auth/[provider]/redirect.ts +9 -0
  211. package/app/src/pages/auth/forgot-password/index.ts +75 -0
  212. package/app/src/pages/auth/layout.ts +32 -0
  213. package/app/src/pages/auth/login/index.ts +198 -0
  214. package/app/src/pages/auth/magic/index.ts +68 -0
  215. package/app/src/pages/auth/register/index.ts +117 -0
  216. package/app/src/pages/auth/reset-password/index.ts +65 -0
  217. package/app/src/pages/auth/setup/index.ts +194 -0
  218. package/app/src/pages/auth/two-factor/index.ts +67 -0
  219. package/app/src/pages/auth/verify/index.ts +62 -0
  220. package/app/src/pages/dashboard/(cms)/appearance/customize/index.ts +263 -0
  221. package/app/src/pages/dashboard/(cms)/appearance/dashboard-menu/index.ts +241 -0
  222. package/app/src/pages/dashboard/(cms)/appearance/index.ts +89 -0
  223. package/app/src/pages/dashboard/(cms)/appearance/menus/[id]/index.ts +697 -0
  224. package/app/src/pages/dashboard/(cms)/appearance/menus/index.ts +145 -0
  225. package/app/src/pages/dashboard/(cms)/appearance/widgets/index.ts +279 -0
  226. package/app/src/pages/dashboard/(cms)/blocks/index.ts +84 -0
  227. package/app/src/pages/dashboard/(cms)/calendar/index.ts +92 -0
  228. package/app/src/pages/dashboard/(cms)/collections/[name]/index.ts +525 -0
  229. package/app/src/pages/dashboard/(cms)/collections/index.ts +72 -0
  230. package/app/src/pages/dashboard/(cms)/collections/new/index.ts +66 -0
  231. package/app/src/pages/dashboard/(cms)/comments/index.ts +237 -0
  232. package/app/src/pages/dashboard/(cms)/content/[collection]/[id]/index.ts +2382 -0
  233. package/app/src/pages/dashboard/(cms)/content/[collection]/[id]/revisions/index.ts +126 -0
  234. package/app/src/pages/dashboard/(cms)/content/[collection]/index.ts +693 -0
  235. package/app/src/pages/dashboard/(cms)/field-groups/[id]/index.ts +193 -0
  236. package/app/src/pages/dashboard/(cms)/field-groups/index.ts +65 -0
  237. package/app/src/pages/dashboard/(cms)/forms/[id]/submissions/[submission]/index.ts +279 -0
  238. package/app/src/pages/dashboard/(cms)/forms/[id]/submissions/index.ts +170 -0
  239. package/app/src/pages/dashboard/(cms)/forms/index.ts +171 -0
  240. package/app/src/pages/dashboard/(cms)/media/[id]/index.ts +317 -0
  241. package/app/src/pages/dashboard/(cms)/media/index.ts +156 -0
  242. package/app/src/pages/dashboard/(cms)/media/new/index.ts +9 -0
  243. package/app/src/pages/dashboard/(cms)/media/summary/index.ts +74 -0
  244. package/app/src/pages/dashboard/(cms)/my-sites/index.ts +63 -0
  245. package/app/src/pages/dashboard/(cms)/network/index.ts +53 -0
  246. package/app/src/pages/dashboard/(cms)/network/plugins/index.ts +110 -0
  247. package/app/src/pages/dashboard/(cms)/network/sites/[id]/index.ts +243 -0
  248. package/app/src/pages/dashboard/(cms)/network/sites/index.ts +186 -0
  249. package/app/src/pages/dashboard/(cms)/network/sites/new/index.ts +77 -0
  250. package/app/src/pages/dashboard/(cms)/network/themes/index.ts +82 -0
  251. package/app/src/pages/dashboard/(cms)/network/updates/index.ts +48 -0
  252. package/app/src/pages/dashboard/(cms)/network/users/[id]/index.ts +176 -0
  253. package/app/src/pages/dashboard/(cms)/network/users/index.ts +174 -0
  254. package/app/src/pages/dashboard/(cms)/network/users/new/index.ts +66 -0
  255. package/app/src/pages/dashboard/(cms)/plugins/index.ts +172 -0
  256. package/app/src/pages/dashboard/(cms)/profile/public/index.ts +105 -0
  257. package/app/src/pages/dashboard/(cms)/registrations/index.ts +70 -0
  258. package/app/src/pages/dashboard/(cms)/settings/index.ts +359 -0
  259. package/app/src/pages/dashboard/(cms)/settings/json/index.ts +202 -0
  260. package/app/src/pages/dashboard/(cms)/settings/layout.ts +67 -0
  261. package/app/src/pages/dashboard/(cms)/taxonomies/[name]/index.ts +85 -0
  262. package/app/src/pages/dashboard/(cms)/taxonomies/index.ts +98 -0
  263. package/app/src/pages/dashboard/(cms)/terms/[taxonomy]/[id]/index.ts +244 -0
  264. package/app/src/pages/dashboard/(cms)/terms/[taxonomy]/index.ts +148 -0
  265. package/app/src/pages/dashboard/(cms)/tools/activity/index.ts +94 -0
  266. package/app/src/pages/dashboard/(cms)/tools/automations/index.ts +131 -0
  267. package/app/src/pages/dashboard/(cms)/tools/cache/index.ts +92 -0
  268. package/app/src/pages/dashboard/(cms)/tools/export/index.ts +99 -0
  269. package/app/src/pages/dashboard/(cms)/tools/health/index.ts +189 -0
  270. package/app/src/pages/dashboard/(cms)/tools/jobs/index.ts +93 -0
  271. package/app/src/pages/dashboard/(cms)/tools/links/index.ts +115 -0
  272. package/app/src/pages/dashboard/(cms)/tools/redirects/index.ts +161 -0
  273. package/app/src/pages/dashboard/(cms)/tools/replace/index.ts +124 -0
  274. package/app/src/pages/dashboard/(cms)/tools/schema/index.ts +54 -0
  275. package/app/src/pages/dashboard/(cms)/tools/seo/index.ts +156 -0
  276. package/app/src/pages/dashboard/(cms)/tools/updates/index.ts +114 -0
  277. package/app/src/pages/dashboard/(cms)/tools/webhooks/index.ts +182 -0
  278. package/app/src/pages/dashboard/index.ts +214 -0
  279. package/app/src/pages/dashboard/layout.ts +320 -0
  280. package/app/src/pages/dashboard/profile/index.ts +211 -0
  281. package/app/src/pages/dashboard/profile/security/index.ts +255 -0
  282. package/app/src/pages/dashboard/roles/[id]/index.ts +116 -0
  283. package/app/src/pages/dashboard/roles/index.ts +134 -0
  284. package/app/src/pages/dashboard/roles/new/index.ts +95 -0
  285. package/app/src/pages/dashboard/sessions/index.ts +105 -0
  286. package/app/src/pages/dashboard/users/[id]/index.ts +472 -0
  287. package/app/src/pages/dashboard/users/index.ts +299 -0
  288. package/app/src/pages/dashboard/users/new/index.ts +123 -0
  289. package/app/src/passkeys.client.ts +18 -0
  290. package/app/src/plugins/default-theme/assets/cover.svg +31 -0
  291. package/app/src/plugins/default-theme/assets/logo.svg +8 -0
  292. package/app/src/plugins/default-theme/parts/comments.ts +66 -0
  293. package/app/src/plugins/default-theme/parts/list.ts +38 -0
  294. package/app/src/plugins/default-theme/parts/media.ts +15 -0
  295. package/app/src/plugins/default-theme/parts/options.ts +45 -0
  296. package/app/src/plugins/default-theme/plugin.json +275 -0
  297. package/app/src/plugins/default-theme/templates/archive.ts +14 -0
  298. package/app/src/plugins/default-theme/templates/entry-blank.ts +13 -0
  299. package/app/src/plugins/default-theme/templates/entry.ts +27 -0
  300. package/app/src/plugins/default-theme/templates/home.ts +8 -0
  301. package/app/src/plugins/default-theme/templates/layout.ts +40 -0
  302. package/app/src/plugins/default-theme/templates/not-found.ts +10 -0
  303. package/app/src/plugins/default-theme/templates/search.ts +32 -0
  304. package/app/src/services/rbac.ts +65 -0
  305. package/app/src/services/roles.ts +89 -0
  306. package/app/src/services/users.ts +173 -0
  307. package/package.json +114 -2
  308. package/src/automations.ts +95 -0
  309. package/src/bindings.ts +138 -0
  310. package/src/block-references.ts +239 -0
  311. package/src/block-shape.ts +155 -0
  312. package/src/blocks.ts +1157 -0
  313. package/src/cache.ts +54 -0
  314. package/src/checks.ts +198 -0
  315. package/src/cloudflare-workers.d.ts +13 -0
  316. package/src/cloudflare.ts +61 -0
  317. package/src/collab.ts +372 -0
  318. package/src/collections.ts +421 -0
  319. package/src/comments.ts +46 -0
  320. package/src/cron.ts +95 -0
  321. package/src/dashboard-menu.ts +105 -0
  322. package/src/diff.ts +113 -0
  323. package/src/editor-layout.ts +316 -0
  324. package/src/errors.ts +50 -0
  325. package/src/field-groups.ts +498 -0
  326. package/src/fields.ts +411 -0
  327. package/src/form-builder.ts +1053 -0
  328. package/src/forms.ts +109 -0
  329. package/src/highlight.ts +134 -0
  330. package/src/hooks.ts +510 -0
  331. package/src/icon-catalog.ts +55 -0
  332. package/src/icons.ts +128 -0
  333. package/src/images.ts +149 -0
  334. package/src/index.ts +100 -0
  335. package/src/isolate-core.ts +245 -0
  336. package/src/isolate.ts +19 -0
  337. package/src/isolation.ts +69 -0
  338. package/src/mail.ts +182 -0
  339. package/src/manifest.ts +476 -0
  340. package/src/media.ts +193 -0
  341. package/src/menus.ts +287 -0
  342. package/src/module.ts +32 -0
  343. package/src/modules/content.ts +64 -0
  344. package/src/modules/forms.ts +35 -0
  345. package/src/node.ts +66 -0
  346. package/src/notes.ts +237 -0
  347. package/src/permissions.ts +139 -0
  348. package/src/plugin-settings.ts +114 -0
  349. package/src/records.ts +56 -0
  350. package/src/redirects.ts +59 -0
  351. package/src/resource.ts +245 -0
  352. package/src/richtext.ts +402 -0
  353. package/src/schema-json.ts +112 -0
  354. package/src/search.ts +89 -0
  355. package/src/seo.ts +477 -0
  356. package/src/server/accounts.ts +418 -0
  357. package/src/server/ai.ts +33 -0
  358. package/src/server/api-tokens.ts +91 -0
  359. package/src/server/areas.ts +116 -0
  360. package/src/server/audit.ts +94 -0
  361. package/src/server/auth.ts +89 -0
  362. package/src/server/automations.ts +190 -0
  363. package/src/server/autosaves.ts +69 -0
  364. package/src/server/brand-icons.ts +50 -0
  365. package/src/server/brands.ts +86 -0
  366. package/src/server/budget.ts +97 -0
  367. package/src/server/cache.ts +104 -0
  368. package/src/server/capabilities.ts +333 -0
  369. package/src/server/cloudflare.ts +92 -0
  370. package/src/server/comments.ts +237 -0
  371. package/src/server/drafts.ts +176 -0
  372. package/src/server/field-groups.ts +88 -0
  373. package/src/server/files.ts +780 -0
  374. package/src/server/forms.ts +726 -0
  375. package/src/server/handoffs.ts +107 -0
  376. package/src/server/host-schema.ts +312 -0
  377. package/src/server/host.ts +2892 -0
  378. package/src/server/images.ts +34 -0
  379. package/src/server/index.ts +59 -0
  380. package/src/server/indexnow.ts +91 -0
  381. package/src/server/isolation.ts +183 -0
  382. package/src/server/locked.ts +45 -0
  383. package/src/server/mail.ts +93 -0
  384. package/src/server/markdown.ts +24 -0
  385. package/src/server/menus.ts +286 -0
  386. package/src/server/network-policy.ts +60 -0
  387. package/src/server/network.ts +665 -0
  388. package/src/server/notes.ts +275 -0
  389. package/src/server/outbox.ts +213 -0
  390. package/src/server/path-moves.ts +33 -0
  391. package/src/server/payments.ts +59 -0
  392. package/src/server/previews.ts +62 -0
  393. package/src/server/purge.ts +140 -0
  394. package/src/server/reconcile.ts +38 -0
  395. package/src/server/redirects.ts +183 -0
  396. package/src/server/repository.ts +1107 -0
  397. package/src/server/schedules.ts +105 -0
  398. package/src/server/schema.ts +562 -0
  399. package/src/server/security.ts +391 -0
  400. package/src/server/seed.ts +52 -0
  401. package/src/server/shared-fields.ts +34 -0
  402. package/src/server/site-export.ts +151 -0
  403. package/src/server/site-import.ts +482 -0
  404. package/src/server/site-search.ts +289 -0
  405. package/src/server/site-settings.ts +286 -0
  406. package/src/server/sites.ts +243 -0
  407. package/src/server/stores.ts +314 -0
  408. package/src/server/stripe.ts +173 -0
  409. package/src/server/taxonomies.ts +415 -0
  410. package/src/server/tokens.ts +25 -0
  411. package/src/server/traffic.ts +118 -0
  412. package/src/server/users.ts +653 -0
  413. package/src/server/webhooks.ts +245 -0
  414. package/src/settings-controls.ts +54 -0
  415. package/src/settings-document.ts +111 -0
  416. package/src/settings.ts +426 -0
  417. package/src/storage/cloudflare.ts +94 -0
  418. package/src/storage/index.ts +30 -0
  419. package/src/storage/node.ts +114 -0
  420. package/src/taxonomy.ts +51 -0
  421. package/src/theme.ts +1135 -0
  422. package/src/to-markdown.ts +179 -0
  423. package/src/updates.ts +126 -0
  424. package/src/version.ts +2 -0
  425. package/src/vite.d.ts +25 -0
  426. package/src/vite.js +199 -0
  427. package/src/webhooks.ts +107 -0
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Sign-in handoffs on the network database. Accounts sign in only on the network host; a site
3
+ * gets its own session from a one-use code bound to the site and the network session, valid for
4
+ * a minute. The network remembers which sites each session reached, to end their child
5
+ * sessions with it.
6
+ */
7
+ import type { RomeDB } from '@cossackframework/romedb';
8
+ import type { EndedSessions } from './stores';
9
+
10
+ const CODE = /^[a-f0-9]{64}$/;
11
+ const WRITE = { kind: 'cms.handoffs', retryConflicts: 5 } as const;
12
+
13
+ const sha256 = async (text: string) => [...new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)))]
14
+ .map(byte => byte.toString(16).padStart(2, '0')).join('');
15
+
16
+ export interface Handoff {
17
+ /** The network session's public reference: the child session's parent. */
18
+ readonly session: string;
19
+ readonly user: string;
20
+ /** The account's network role now, which the child session copies. */
21
+ readonly networkRole: string | null;
22
+ /** When the network session expires; the child session ends no later. */
23
+ readonly expiresAt: number;
24
+ /** Whether the network session's sign-in used a second factor; the child session copies it. */
25
+ readonly secondFactor: boolean;
26
+ }
27
+
28
+ export class CmsHandoffs {
29
+ readonly #db: RomeDB;
30
+ readonly #now: () => number;
31
+
32
+ constructor(db: RomeDB, options: { now?: () => number } = {}) {
33
+ this.#db = db;
34
+ this.#now = options.now ?? Date.now;
35
+ }
36
+
37
+ /** A code for the network session to sign in on the site. Only its SHA-256 is stored. */
38
+ async issue(input: { session: string; user: string; site: string; ttlMs?: number }): Promise<string> {
39
+ const code = [...crypto.getRandomValues(new Uint8Array(32))].map(byte => byte.toString(16).padStart(2, '0')).join('');
40
+ const now = this.#now();
41
+ await this.#db.write([{ rql: 'INSERT INTO cms_handoffs (id, session_id, user_id, site_id, created_at, expires_at) VALUES ($id, $session, $user, $site, $now, $expires)',
42
+ params: { $id: await sha256(code), $session: input.session, $user: input.user, $site: input.site, $now: now, $expires: now + (input.ttlMs ?? 60_000) } }], WRITE);
43
+ return code;
44
+ }
45
+
46
+ /**
47
+ * The handoff a code names for this site, used at most once. Null when the code is unknown,
48
+ * expired, for another site or used, or when its network session or active account is gone.
49
+ */
50
+ async consume(code: string, site: string): Promise<Handoff | null> {
51
+ if (typeof code !== 'string' || !CODE.test(code)) return null;
52
+ const id = await sha256(code);
53
+ const now = this.#now();
54
+ const found = await this.#db.read([{
55
+ rql: `SELECT h.session_id, h.user_id, a.network_role, s.expires_at, s.second_factor FROM cms_handoffs h JOIN cms_accounts a ON a.id = h.user_id
56
+ JOIN cms_auth_sessions s ON s.public_id = h.session_id AND s.user_id = h.user_id
57
+ WHERE h.id = $id AND h.site_id = $site AND h.expires_at > $now AND s.expires_at > $now AND a.status = 'active'`,
58
+ params: { $id: id, $site: site, $now: now },
59
+ }]);
60
+ const row = found.results[0]!.rows[0];
61
+ if (!row) {
62
+ await this.#db.write([{ rql: 'DELETE FROM cms_handoffs WHERE id = $id', params: { $id: id } }], WRITE);
63
+ return null;
64
+ }
65
+ const handoff = { session: row[0] as string, user: row[1] as string, networkRole: row[2] as string | null, expiresAt: Number(row[3]), secondFactor: row[4] !== null && Number(row[4]) === 1 };
66
+ // Only the request whose delete removed the code may use it. The site is recorded for the
67
+ // network session only while that session lives, so a sign-out finds every site it reached.
68
+ const taken = await this.#db.write([
69
+ { rql: 'DELETE FROM cms_handoffs WHERE id = $id', params: { $id: id } },
70
+ {
71
+ rql: `INSERT INTO cms_session_sites (session_id, site_id, user_id, created_at) SELECT $session, $site, $user, $now
72
+ WHERE EXISTS (SELECT 1 FROM cms_auth_sessions WHERE public_id = $session AND user_id = $user AND expires_at > $now) ON CONFLICT (session_id, site_id) DO NOTHING`,
73
+ params: { $session: handoff.session, $site: site, $user: handoff.user, $now: now },
74
+ },
75
+ ], WRITE);
76
+ if (taken.results[0]!.affected !== 1n) return null;
77
+ // Signed out since the read: no child session. A sign-out after this check lists the site.
78
+ const alive = await this.#db.read([{ rql: 'SELECT 1 FROM cms_auth_sessions WHERE public_id = $session AND user_id = $user AND expires_at > $now', params: { $session: handoff.session, $user: handoff.user, $now: now } }]);
79
+ if (!alive.results[0]!.rows.length) return null;
80
+ return Object.freeze(handoff);
81
+ }
82
+
83
+ /** The sites these network sessions reached; `forget` drops the record once their child sessions ended. */
84
+ async reached(ended: EndedSessions): Promise<string[]> {
85
+ const result = await this.#db.read(['session' in ended
86
+ ? { rql: 'SELECT DISTINCT site_id FROM cms_session_sites WHERE session_id = $session', params: { $session: ended.session } }
87
+ : { rql: `SELECT DISTINCT site_id FROM cms_session_sites WHERE user_id = $user${ended.except ? ' AND session_id != $except' : ''}`, params: { $user: ended.user, ...(ended.except ? { $except: ended.except } : {}) } }]);
88
+ return result.results[0]!.rows.map(row => row[0] as string);
89
+ }
90
+
91
+ async forget(ended: EndedSessions): Promise<void> {
92
+ await this.#db.write(['session' in ended
93
+ ? { rql: 'DELETE FROM cms_session_sites WHERE session_id = $session', params: { $session: ended.session } }
94
+ : { rql: `DELETE FROM cms_session_sites WHERE user_id = $user${ended.except ? ' AND session_id != $except' : ''}`, params: { $user: ended.user, ...(ended.except ? { $except: ended.except } : {}) } }], WRITE);
95
+ }
96
+
97
+ async purgeExpired(): Promise<void> {
98
+ const now = this.#now();
99
+ const expired = await this.#db.read([{ rql: 'SELECT 1 FROM cms_handoffs WHERE expires_at <= $now LIMIT 1', params: { $now: now } }]);
100
+ if (!expired.results[0]!.rows.length) return;
101
+ await this.#db.write([
102
+ { rql: 'DELETE FROM cms_handoffs WHERE id IN (SELECT id FROM cms_handoffs WHERE expires_at <= $now LIMIT 500)', params: { $now: now } },
103
+ // Records of network sessions that ended without telling the sites, after their expiry.
104
+ { rql: 'DELETE FROM cms_session_sites WHERE session_id IN (SELECT ss.session_id FROM cms_session_sites ss WHERE NOT EXISTS (SELECT 1 FROM cms_auth_sessions s WHERE s.public_id = ss.session_id) LIMIT 500)' },
105
+ ], WRITE);
106
+ }
107
+ }
@@ -0,0 +1,312 @@
1
+ /**
2
+ * Relational host tables. The host owns the `cms_` and `cossack_` namespaces; plugins
3
+ * never create tables. Each host step is versioned and applies once through the ledger,
4
+ * and fits one batch (32 statements, including the ledger row). The steps create today's
5
+ * tables; a later change is a new step after them. A database whose ledger names a step this
6
+ * build does not have was made by another build, and the host refuses it (`unknownHostSteps`).
7
+ */
8
+ import type { Statement } from '@cossackframework/romedb';
9
+ import { jobStateSchema } from '@cossackframework/core/jobs';
10
+ import { notificationStateSchema } from '@cossackframework/core/notifications';
11
+
12
+ export interface HostStep {
13
+ readonly version: number;
14
+ readonly description: string;
15
+ readonly statements: readonly Statement[];
16
+ }
17
+
18
+ const split = (script: string): Statement[] => script.split(';').map(rql => rql.trim()).filter(Boolean)
19
+ .map(rql => ({ rql: rql.replace(/\bIF NOT EXISTS\s+/g, '') }));
20
+
21
+ export const HOST_TABLES = [
22
+ 'cms_migrations', 'cms_schema_state', 'cms_users', 'cms_roles', 'cms_user_roles', 'cms_auth_sessions', 'cms_oauth_states',
23
+ 'cms_password_resets', 'cms_sessions', 'cms_session_values', 'cms_grants', 'cms_audit', 'cms_revisions', 'cms_outbox', 'cms_files',
24
+ 'cms_terms', 'cms_settings', 'cms_collections', 'cms_taxonomies', 'cms_field_groups', 'cms_menus', 'cms_menu_locations',
25
+ 'cms_preview_tokens', 'cms_autosaves', 'cms_file_variants', 'cms_redirects', 'cms_api_tokens', 'cms_webhooks', 'cms_webhook_deliveries',
26
+ 'cms_comments', 'cms_user_security', 'cms_passkeys', 'cms_auth_challenges', 'cms_areas', 'cms_form_sends', 'cms_form_mail', 'cms_form_results', 'cms_crawler_visits', 'cms_not_found', 'cms_cache',
27
+ 'cms_accounts', 'cms_sites', 'cms_site_domains', 'cms_handoffs', 'cms_session_sites', 'cms_memberships', 'cms_network', 'cossack_jobs', 'cossack_notifications',
28
+ 'cms_drafts', 'cms_notes', 'cms_note_mentions', 'cms_schedules', 'cms_automations', 'cms_registrations', 'cms_search', 'cms_attempts', 'cms_schema_generations',
29
+ ] as const;
30
+
31
+ export const HOST_STEPS: readonly HostStep[] = [{
32
+ version: 1,
33
+ description: 'Core tables: the ledger, members, roles, sessions, grants, the audit trail, revisions and the outbox',
34
+ statements: [
35
+ { rql: `CREATE TABLE cms_migrations (id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK(kind IN ('host','additive','explicit')), owner TEXT NOT NULL,
36
+ resource TEXT, version INTEGER NOT NULL, declaration_hash TEXT, rql TEXT NOT NULL, engine_version TEXT NOT NULL, applied_at INTEGER NOT NULL)` },
37
+ { rql: 'CREATE TABLE cms_schema_state (resource TEXT PRIMARY KEY, declaration_hash TEXT NOT NULL, updated_at INTEGER NOT NULL)' },
38
+ // A site's members. Accounts sign in (cms_accounts, the network database's); members keep the same ID. The slug names their author page.
39
+ { rql: `CREATE TABLE cms_users (id TEXT PRIMARY KEY, email TEXT NOT NULL UNIQUE, name TEXT NOT NULL, avatar TEXT,
40
+ status TEXT NOT NULL CHECK(status IN ('active','disabled')), created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL,
41
+ last_sign_in_at INTEGER, meta TEXT NOT NULL DEFAULT '{}', slug TEXT)` },
42
+ { rql: 'CREATE INDEX cms_users_created ON cms_users (created_at)' },
43
+ { rql: 'CREATE INDEX cms_users_slug ON cms_users (slug)' },
44
+ { rql: 'CREATE TABLE cms_roles (id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, permissions TEXT NOT NULL, created_at INTEGER NOT NULL)' },
45
+ { rql: 'CREATE TABLE cms_user_roles (user_id TEXT NOT NULL, role_id TEXT NOT NULL, PRIMARY KEY (user_id, role_id))' },
46
+ { rql: 'CREATE INDEX cms_user_roles_role ON cms_user_roles (role_id)' },
47
+ // Keyed by the session token, so rotation is one conditional update of the key. On a site other than the main one, a
48
+ // session is a child of a network session: its public reference, and the network role copied at the handoff.
49
+ { rql: `CREATE TABLE cms_auth_sessions (id TEXT PRIMARY KEY, public_id TEXT NOT NULL UNIQUE, user_id TEXT NOT NULL,
50
+ created_at INTEGER NOT NULL, expires_at INTEGER NOT NULL, ip TEXT, user_agent TEXT, parent_id TEXT, network_role TEXT)` },
51
+ { rql: 'CREATE INDEX cms_auth_sessions_user ON cms_auth_sessions (user_id, expires_at)' },
52
+ { rql: 'CREATE INDEX cms_auth_sessions_parent ON cms_auth_sessions (parent_id)' },
53
+ { rql: 'CREATE TABLE cms_oauth_states (key TEXT PRIMARY KEY, provider TEXT NOT NULL, created_at INTEGER NOT NULL, expires_at INTEGER NOT NULL)' },
54
+ // Keyed by the SHA-256 of the emailed token, so a database read cannot reset a password.
55
+ { rql: 'CREATE TABLE cms_password_resets (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, created_at INTEGER NOT NULL, expires_at INTEGER NOT NULL)' },
56
+ { rql: 'CREATE INDEX cms_password_resets_user ON cms_password_resets (user_id)' },
57
+ { rql: 'CREATE TABLE cms_sessions (id TEXT PRIMARY KEY, user_id TEXT, created_at INTEGER NOT NULL, expires_at INTEGER NOT NULL)' },
58
+ { rql: 'CREATE INDEX cms_sessions_expiry ON cms_sessions (expires_at)' },
59
+ // One row per key, so concurrent writes to different keys never overwrite each other.
60
+ { rql: 'CREATE TABLE cms_session_values (session_id TEXT NOT NULL, key TEXT NOT NULL, value TEXT NOT NULL, PRIMARY KEY (session_id, key))' },
61
+ { rql: `CREATE TABLE cms_grants (id TEXT PRIMARY KEY, subject TEXT NOT NULL, permission TEXT NOT NULL, revision INTEGER NOT NULL,
62
+ created_at INTEGER NOT NULL, UNIQUE (subject, permission))` },
63
+ { rql: `CREATE TABLE cms_audit (id TEXT PRIMARY KEY, actor TEXT, action TEXT NOT NULL, resource TEXT NOT NULL, record_id TEXT NOT NULL,
64
+ revision INTEGER, at INTEGER NOT NULL)` },
65
+ { rql: 'CREATE INDEX cms_audit_record ON cms_audit (resource, record_id, at)' },
66
+ { rql: `CREATE TABLE cms_revisions (id TEXT PRIMARY KEY, resource TEXT NOT NULL, record_id TEXT NOT NULL, revision INTEGER NOT NULL,
67
+ data TEXT NOT NULL, actor TEXT, at INTEGER NOT NULL, UNIQUE (resource, record_id, revision))` },
68
+ { rql: `CREATE TABLE cms_outbox (id TEXT PRIMARY KEY, topic TEXT NOT NULL, payload TEXT NOT NULL, created_at INTEGER NOT NULL,
69
+ available_at INTEGER NOT NULL, attempts INTEGER NOT NULL, lease_token TEXT, lease_until INTEGER NOT NULL,
70
+ delivered_at INTEGER, pending_plugins TEXT)` },
71
+ { rql: 'CREATE INDEX cms_outbox_ready ON cms_outbox (delivered_at, available_at)' },
72
+ ],
73
+ }, {
74
+ version: 2,
75
+ description: 'Content tables: files, terms, settings, definitions, field groups, menus, areas, previews, autosaves, redirects, the cache, jobs and notifications',
76
+ statements: [
77
+ // Crop and focal point of an image as JSON (`edits`), and a tiny WebP of it as a data: URL, shown while it loads (`placeholder`).
78
+ { rql: `CREATE TABLE cms_files (id TEXT PRIMARY KEY, key TEXT NOT NULL UNIQUE, owner_id TEXT, name TEXT NOT NULL, content_type TEXT NOT NULL,
79
+ size INTEGER NOT NULL, status TEXT NOT NULL CHECK(status IN ('pending','ready','deleting')), title TEXT NOT NULL DEFAULT '', alt TEXT NOT NULL DEFAULT '',
80
+ caption TEXT NOT NULL DEFAULT '', description TEXT NOT NULL DEFAULT '', width INTEGER, height INTEGER, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL,
81
+ edits TEXT, placeholder TEXT)` },
82
+ { rql: 'CREATE INDEX cms_files_status ON cms_files (status, updated_at)' },
83
+ { rql: 'CREATE INDEX cms_files_created ON cms_files (status, created_at)' },
84
+ // The resized variants made of an image.
85
+ { rql: 'CREATE TABLE cms_file_variants (key TEXT PRIMARY KEY, file_id TEXT NOT NULL, created_at INTEGER NOT NULL, size INTEGER)' },
86
+ { rql: 'CREATE INDEX cms_file_variants_file ON cms_file_variants (file_id)' },
87
+ // Materialized path of ancestor IDs, such as /a/b/: a subtree is one prefix match.
88
+ { rql: `CREATE TABLE cms_terms (id TEXT PRIMARY KEY, taxonomy TEXT NOT NULL, name TEXT NOT NULL, slug TEXT NOT NULL, description TEXT NOT NULL DEFAULT '',
89
+ parent_id TEXT, path TEXT NOT NULL, depth INTEGER NOT NULL, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL,
90
+ icon TEXT NOT NULL DEFAULT '', meta TEXT NOT NULL DEFAULT '{}', UNIQUE (taxonomy, slug))` },
91
+ { rql: 'CREATE INDEX cms_terms_path ON cms_terms (taxonomy, path)' },
92
+ { rql: 'CREATE INDEX cms_terms_parent ON cms_terms (parent_id)' },
93
+ { rql: 'CREATE TABLE cms_settings (key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_at INTEGER NOT NULL, updated_by TEXT)' },
94
+ // Collection and taxonomy definitions made in the dashboard; built-ins store only what was added.
95
+ { rql: 'CREATE TABLE cms_collections (name TEXT PRIMARY KEY, definition TEXT NOT NULL, updated_at INTEGER NOT NULL, updated_by TEXT)' },
96
+ { rql: 'CREATE TABLE cms_taxonomies (name TEXT PRIMARY KEY, definition TEXT NOT NULL, updated_at INTEGER NOT NULL, updated_by TEXT)' },
97
+ { rql: 'CREATE TABLE cms_field_groups (id TEXT PRIMARY KEY, definition TEXT NOT NULL, sort INTEGER NOT NULL DEFAULT 0, updated_at INTEGER NOT NULL, updated_by TEXT)' },
98
+ // A menu's items are one JSON tree, saved with a revision check.
99
+ { rql: `CREATE TABLE cms_menus (id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE, items TEXT NOT NULL, revision INTEGER NOT NULL,
100
+ created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, updated_by TEXT)` },
101
+ { rql: 'CREATE TABLE cms_menu_locations (location TEXT PRIMARY KEY, menu_id TEXT NOT NULL, updated_at INTEGER NOT NULL)' },
102
+ { rql: 'CREATE INDEX cms_menu_locations_menu ON cms_menu_locations (menu_id)' },
103
+ // A theme's widget area: its blocks as JSON, saved with a revision check.
104
+ { rql: 'CREATE TABLE cms_areas (name TEXT PRIMARY KEY, blocks TEXT NOT NULL, revision INTEGER NOT NULL, updated_at INTEGER NOT NULL, updated_by TEXT)' },
105
+ // Keyed by the SHA-256 of the shared token, so a database read cannot preview anything.
106
+ { rql: `CREATE TABLE cms_preview_tokens (id TEXT PRIMARY KEY, resource TEXT NOT NULL, record_id TEXT NOT NULL, created_by TEXT,
107
+ created_at INTEGER NOT NULL, expires_at INTEGER NOT NULL)` },
108
+ { rql: 'CREATE INDEX cms_preview_tokens_record ON cms_preview_tokens (resource, record_id)' },
109
+ // One autosave per user and entry; `new` stands for an entry not saved yet.
110
+ { rql: `CREATE TABLE cms_autosaves (id TEXT PRIMARY KEY, resource TEXT NOT NULL, record_id TEXT NOT NULL, user_id TEXT NOT NULL, data TEXT NOT NULL,
111
+ at INTEGER NOT NULL)` },
112
+ { rql: 'CREATE INDEX cms_autosaves_at ON cms_autosaves (at)' },
113
+ { rql: `CREATE TABLE cms_redirects (id TEXT PRIMARY KEY, source TEXT NOT NULL UNIQUE, target TEXT NOT NULL, status INTEGER NOT NULL CHECK(status IN (301,302,307,308,410)),
114
+ prefix INTEGER NOT NULL CHECK(prefix IN (0,1)), kind TEXT NOT NULL CHECK(kind IN ('auto','manual')), note TEXT NOT NULL DEFAULT '', hits INTEGER NOT NULL DEFAULT 0,
115
+ last_hit_at INTEGER, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, created_by TEXT)` },
116
+ { rql: 'CREATE INDEX cms_redirects_target ON cms_redirects (target)' },
117
+ { rql: 'CREATE INDEX cms_redirects_prefix ON cms_redirects (prefix, updated_at)' },
118
+ // JSON values by key; a NULL expiry keeps the value until it is deleted.
119
+ { rql: 'CREATE TABLE cms_cache (key TEXT PRIMARY KEY, value TEXT NOT NULL, expires_at INTEGER)' },
120
+ ...split(jobStateSchema),
121
+ ...split(notificationStateSchema),
122
+ ],
123
+ }, {
124
+ version: 3,
125
+ description: 'Sign-in, discussion, webhooks and forms',
126
+ statements: [
127
+ // Application passwords: the secret is stored as its SHA-256.
128
+ { rql: `CREATE TABLE cms_api_tokens (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, name TEXT NOT NULL, secret_hash TEXT NOT NULL,
129
+ scope TEXT NOT NULL CHECK(scope IN ('read','write')), created_at INTEGER NOT NULL, last_used_at INTEGER, expires_at INTEGER)` },
130
+ { rql: 'CREATE INDEX cms_api_tokens_user ON cms_api_tokens (user_id, created_at)' },
131
+ // Second factors: an authenticator app's secret, the last step it used, and recovery code hashes.
132
+ { rql: `CREATE TABLE cms_user_security (user_id TEXT PRIMARY KEY, totp_secret TEXT, totp_confirmed_at INTEGER, totp_last_step INTEGER, recovery TEXT,
133
+ updated_at INTEGER NOT NULL)` },
134
+ { rql: `CREATE TABLE cms_passkeys (id TEXT PRIMARY KEY, user_id TEXT NOT NULL, name TEXT NOT NULL, public_key TEXT NOT NULL, counter INTEGER NOT NULL,
135
+ transports TEXT, device_type TEXT, backed_up INTEGER NOT NULL, created_at INTEGER NOT NULL, last_used_at INTEGER)` },
136
+ { rql: 'CREATE INDEX cms_passkeys_user ON cms_passkeys (user_id)' },
137
+ // Pending sign-ins and WebAuthn challenges, keyed by a random ID kept in a cookie.
138
+ { rql: `CREATE TABLE cms_auth_challenges (id TEXT PRIMARY KEY, kind TEXT NOT NULL, user_id TEXT, challenge TEXT NOT NULL, attempts INTEGER NOT NULL,
139
+ expires_at INTEGER NOT NULL)` },
140
+ { rql: 'CREATE INDEX cms_auth_challenges_expiry ON cms_auth_challenges (expires_at)' },
141
+ { rql: `CREATE TABLE cms_comments (id TEXT PRIMARY KEY, resource TEXT NOT NULL, record_id TEXT NOT NULL, parent_id TEXT, user_id TEXT,
142
+ author_name TEXT NOT NULL, author_email TEXT NOT NULL, author_url TEXT NOT NULL DEFAULT '', content TEXT NOT NULL,
143
+ status TEXT NOT NULL CHECK(status IN ('pending','approved','spam','trash')), ip_hash TEXT, user_agent TEXT, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL)` },
144
+ { rql: 'CREATE INDEX cms_comments_entry ON cms_comments (resource, record_id, status, created_at)' },
145
+ { rql: 'CREATE INDEX cms_comments_status ON cms_comments (status, created_at)' },
146
+ { rql: 'CREATE INDEX cms_comments_ip ON cms_comments (ip_hash, created_at)' },
147
+ { rql: 'CREATE INDEX cms_comments_author ON cms_comments (author_email, status)' },
148
+ { rql: `CREATE TABLE cms_webhooks (id TEXT PRIMARY KEY, name TEXT NOT NULL, url TEXT NOT NULL, events TEXT NOT NULL, collections TEXT NOT NULL, secret TEXT NOT NULL,
149
+ active INTEGER NOT NULL CHECK(active IN (0,1)), created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, created_by TEXT)` },
150
+ // One delivery per webhook and event, leased like outbox rows.
151
+ { rql: `CREATE TABLE cms_webhook_deliveries (id TEXT PRIMARY KEY, webhook_id TEXT NOT NULL, event_id TEXT NOT NULL, event TEXT NOT NULL, payload TEXT NOT NULL,
152
+ status TEXT NOT NULL CHECK(status IN ('pending','delivered','failed')), attempts INTEGER NOT NULL, next_attempt_at INTEGER NOT NULL, lease_token TEXT,
153
+ lease_until INTEGER NOT NULL, response_status INTEGER, error TEXT, created_at INTEGER NOT NULL, delivered_at INTEGER, UNIQUE (webhook_id, event_id))` },
154
+ { rql: 'CREATE INDEX cms_webhook_deliveries_due ON cms_webhook_deliveries (status, next_attempt_at)' },
155
+ { rql: 'CREATE INDEX cms_webhook_deliveries_hook ON cms_webhook_deliveries (webhook_id, created_at)' },
156
+ // One row per form submission for a day: the write's receipt and the per-address throttle. Submissions live in each form's own table.
157
+ { rql: 'CREATE TABLE cms_form_sends (id TEXT PRIMARY KEY, form_id TEXT NOT NULL, ip_hash TEXT, at INTEGER NOT NULL)' },
158
+ { rql: 'CREATE INDEX cms_form_sends_ip ON cms_form_sends (form_id, ip_hash, at)' },
159
+ { rql: 'CREATE INDEX cms_form_sends_at ON cms_form_sends (at)' },
160
+ // Notification emails already sent, by event, rule and recipient, so outbox retries do not send them again.
161
+ { rql: 'CREATE TABLE cms_form_mail (id TEXT PRIMARY KEY, sent_at INTEGER NOT NULL)' },
162
+ { rql: 'CREATE INDEX cms_form_mail_sent ON cms_form_mail (sent_at)' },
163
+ // What a form post returns to the page after its redirect: the confirmation, or the errors and values. Read once.
164
+ { rql: 'CREATE TABLE cms_form_results (id TEXT PRIMARY KEY, form_id TEXT NOT NULL, data TEXT NOT NULL, expires_at INTEGER NOT NULL)' },
165
+ { rql: 'CREATE INDEX cms_form_results_expiry ON cms_form_results (expires_at)' },
166
+ ],
167
+ }, {
168
+ version: 4,
169
+ description: 'The network: accounts, sites, domains, handoffs, memberships and network settings',
170
+ statements: [
171
+ // Who can sign in. Only the network database (the main site's) has rows; sites keep members in cms_users under the same ID.
172
+ { rql: `CREATE TABLE cms_accounts (id TEXT PRIMARY KEY, email TEXT NOT NULL UNIQUE, name TEXT NOT NULL, password_hash TEXT, avatar TEXT,
173
+ status TEXT NOT NULL CHECK(status IN ('active','disabled')), network_role TEXT CHECK(network_role IN ('network_admin')),
174
+ created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, last_sign_in_at INTEGER)` },
175
+ { rql: 'CREATE INDEX cms_accounts_created ON cms_accounts (created_at)' },
176
+ // Sites added to the network, each with its own database. Only the network database has rows; the main site has none.
177
+ { rql: `CREATE TABLE cms_sites (id TEXT PRIMARY KEY, name TEXT NOT NULL, database TEXT NOT NULL UNIQUE,
178
+ status TEXT NOT NULL CHECK(status IN ('creating','active','archived','deleted')), step TEXT,
179
+ created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, deleted_at INTEGER)` },
180
+ { rql: 'CREATE INDEX cms_sites_created ON cms_sites (created_at)' },
181
+ // The host names a site answers on: one primary, the rest redirect to it. The main site's are under `main`.
182
+ { rql: `CREATE TABLE cms_site_domains (host TEXT PRIMARY KEY, site_id TEXT NOT NULL, is_primary INTEGER NOT NULL CHECK(is_primary IN (0,1)),
183
+ verified_at INTEGER, created_at INTEGER NOT NULL)` },
184
+ { rql: 'CREATE INDEX cms_site_domains_site ON cms_site_domains (site_id, is_primary)' },
185
+ // One-use codes that carry a network session to a site, keyed by the SHA-256 of the code.
186
+ { rql: `CREATE TABLE cms_handoffs (id TEXT PRIMARY KEY, session_id TEXT NOT NULL, user_id TEXT NOT NULL, site_id TEXT NOT NULL,
187
+ created_at INTEGER NOT NULL, expires_at INTEGER NOT NULL)` },
188
+ { rql: 'CREATE INDEX cms_handoffs_expiry ON cms_handoffs (expires_at)' },
189
+ // The sites each network session reached, so ending it ends their child sessions.
190
+ { rql: 'CREATE TABLE cms_session_sites (session_id TEXT NOT NULL, site_id TEXT NOT NULL, user_id TEXT NOT NULL, created_at INTEGER NOT NULL, PRIMARY KEY (session_id, site_id))' },
191
+ { rql: 'CREATE INDEX cms_session_sites_user ON cms_session_sites (user_id)' },
192
+ // Which sites each account is a member of, so the network lists them without opening every site's database.
193
+ { rql: 'CREATE TABLE cms_memberships (account_id TEXT NOT NULL, site_id TEXT NOT NULL, created_at INTEGER NOT NULL, PRIMARY KEY (account_id, site_id))' },
194
+ { rql: 'CREATE INDEX cms_memberships_site ON cms_memberships (site_id)' },
195
+ // Network settings and state by key, as JSON.
196
+ { rql: 'CREATE TABLE cms_network (key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_at INTEGER NOT NULL)' },
197
+ ],
198
+ }, {
199
+ version: 5,
200
+ description: 'Co-editing, notes, plugin schedules, automations, crawler visits and missing pages',
201
+ statements: [
202
+ // One row per entry: the room's values and rich text versions between saves, and who is editing.
203
+ { rql: `CREATE TABLE cms_drafts (id TEXT PRIMARY KEY, resource TEXT NOT NULL, record_id TEXT NOT NULL, base_rev INTEGER NOT NULL, data TEXT NOT NULL,
204
+ logs TEXT NOT NULL, editors TEXT NOT NULL, editors_at INTEGER NOT NULL, at INTEGER NOT NULL)` },
205
+ { rql: 'CREATE INDEX cms_drafts_editing ON cms_drafts (resource, editors_at)' },
206
+ { rql: 'CREATE INDEX cms_drafts_at ON cms_drafts (at)' },
207
+ // A thread is a root (parent_id NULL, carrying the status and the last activity) and its replies; the anchor is `target`, a passage adds its range and quote.
208
+ { rql: `CREATE TABLE cms_notes (id TEXT PRIMARY KEY, resource TEXT NOT NULL, record_id TEXT NOT NULL, parent_id TEXT, user_id TEXT NOT NULL, target TEXT NOT NULL,
209
+ text_range TEXT, quoted TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL CHECK(status IN ('open','resolved')), resolved_by TEXT, resolved_at INTEGER,
210
+ created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL)` },
211
+ { rql: 'CREATE INDEX cms_notes_entry ON cms_notes (resource, record_id, status)' },
212
+ { rql: 'CREATE INDEX cms_notes_user ON cms_notes (user_id, created_at)' },
213
+ // Who a note mentions (or replies to), and when they opened the entry: "notes for you" and its count.
214
+ { rql: 'CREATE TABLE cms_note_mentions (note_id TEXT NOT NULL, thread_id TEXT NOT NULL, user_id TEXT NOT NULL, created_at INTEGER NOT NULL, seen_at INTEGER, PRIMARY KEY (note_id, user_id))' },
215
+ { rql: 'CREATE INDEX cms_note_mentions_user ON cms_note_mentions (user_id, seen_at)' },
216
+ { rql: 'CREATE INDEX cms_note_mentions_thread ON cms_note_mentions (thread_id)' },
217
+ // One row per declared schedule of a hosted plugin: the next occurrence, claimed by a conditional update when due.
218
+ { rql: 'CREATE TABLE cms_schedules (id TEXT PRIMARY KEY, plugin TEXT NOT NULL, name TEXT NOT NULL, cron TEXT NOT NULL, next_at INTEGER NOT NULL, last_at INTEGER, updated_at INTEGER NOT NULL)' },
219
+ { rql: 'CREATE INDEX cms_schedules_due ON cms_schedules (next_at)' },
220
+ // When an event happens, send an email.
221
+ { rql: `CREATE TABLE cms_automations (id TEXT PRIMARY KEY, name TEXT NOT NULL, event TEXT NOT NULL, conditions TEXT NOT NULL, action TEXT NOT NULL, active INTEGER NOT NULL,
222
+ runs INTEGER NOT NULL, last_run_at INTEGER, last_error TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL)` },
223
+ // One row per day, crawler and page kind; only known crawlers are counted.
224
+ { rql: 'CREATE TABLE cms_crawler_visits (day TEXT NOT NULL, agent TEXT NOT NULL, grp TEXT NOT NULL, kind TEXT NOT NULL, count INTEGER NOT NULL, PRIMARY KEY (day, agent, kind))' },
225
+ // Paths that answered 404, for Tools → Health and redirects.
226
+ { rql: "CREATE TABLE cms_not_found (path TEXT PRIMARY KEY, hits INTEGER NOT NULL, first_at INTEGER NOT NULL, last_hit_at INTEGER NOT NULL, referrer TEXT NOT NULL DEFAULT '')" },
227
+ { rql: 'CREATE INDEX cms_not_found_hits ON cms_not_found (hits, last_hit_at)' },
228
+ ],
229
+ }, {
230
+ version: 6,
231
+ description: 'Registrations waiting for approval',
232
+ statements: [
233
+ // A registered member the site's administrators have not approved yet: their account is disabled until then. `verified` once their email link was opened (1 when the site asks for no confirmation).
234
+ { rql: 'CREATE TABLE cms_registrations (user_id TEXT PRIMARY KEY, verified INTEGER NOT NULL, created_at INTEGER NOT NULL)' },
235
+ { rql: 'CREATE INDEX cms_registrations_created ON cms_registrations (created_at)' },
236
+ ],
237
+ }, {
238
+ version: 7,
239
+ description: 'The site search index',
240
+ statements: [
241
+ // One document per entry visitors may read (site-search.ts): collection, entry, locale, title, text and publication time.
242
+ { rql: 'CREATE TABLE cms_search' },
243
+ { rql: 'CREATE SEARCH INDEX cms_search__text ON cms_search(title, text) USING FULLTEXT' },
244
+ // Fill it from the entries a site already has, from the outbox, a round at a time.
245
+ { rql: `INSERT INTO cms_outbox (id, topic, payload, created_at, available_at, attempts, lease_until) VALUES ('search:host-7:0', 'search.rebuild',
246
+ '{"eventId":"search:host-7:0","topic":"search.rebuild","at":0,"run":"host-7","only":null,"collection":null,"after":null,"step":0}', 0, 0, 0, 0)` },
247
+ ],
248
+ }, {
249
+ version: 8,
250
+ description: 'Title matches first in the site search',
251
+ statements: [
252
+ // Weights are part of an index's definition (RomeDB 0.2.21), so the index is made again. It is
253
+ // made on an empty table, which is instant, and refilled from the outbox like step 7, so no
254
+ // request builds an index over every row. A rebuild still waiting is replaced by this one.
255
+ { rql: 'DROP INDEX cms_search__text' },
256
+ { rql: 'DELETE FROM cms_search' },
257
+ { rql: `CREATE SEARCH INDEX cms_search__text ON cms_search(title, text) USING FULLTEXT WITH (weights='title=3')` },
258
+ { rql: `DELETE FROM cms_outbox WHERE topic = 'search.rebuild' AND delivered_at IS NULL` },
259
+ { rql: `INSERT INTO cms_outbox (id, topic, payload, created_at, available_at, attempts, lease_until) VALUES ('search:host-8:0', 'search.rebuild',
260
+ '{"eventId":"search:host-8:0","topic":"search.rebuild","at":0,"run":"host-8","only":null,"collection":null,"after":null,"step":0}', 0, 0, 0, 0)` },
261
+ ],
262
+ }, {
263
+ version: 9,
264
+ description: 'Security counters counted in the database',
265
+ statements: [
266
+ // Sign-in failures and request budgets (security.ts) by key: each attempt is one conditional increment, so concurrent attempts cannot overwrite each other's count.
267
+ { rql: 'CREATE TABLE cms_attempts (key TEXT PRIMARY KEY, count INTEGER NOT NULL, until INTEGER NOT NULL)' },
268
+ { rql: 'CREATE INDEX cms_attempts_until ON cms_attempts (until)' },
269
+ ],
270
+ }, {
271
+ version: 10,
272
+ description: 'Automations run as their author',
273
+ statements: [
274
+ // The user who saved or switched on an automation: it runs with their permissions, so it sends only what they may read.
275
+ { rql: 'ALTER TABLE cms_automations ADD COLUMN author TEXT' },
276
+ ],
277
+ }, {
278
+ version: 11,
279
+ description: 'Schema changes take the next generation',
280
+ statements: [
281
+ // Every migration batch inserts the next generation, so two that planned from the same schema collide here and one replans; writes to other rows do not hold migrations up.
282
+ { rql: 'CREATE TABLE cms_schema_generations (id INTEGER PRIMARY KEY, at INTEGER NOT NULL)' },
283
+ ],
284
+ }, {
285
+ version: 12,
286
+ description: 'Sessions record a second factor used at sign-in',
287
+ statements: [
288
+ // 1 when the sign-in that made the session used an authenticator code, a recovery code or a passkey: required two-step sign-in asks it of the session, not only of the account.
289
+ { rql: 'ALTER TABLE cms_auth_sessions ADD COLUMN second_factor INTEGER' },
290
+ ],
291
+ }, {
292
+ version: 13,
293
+ description: 'Accounts record a hold for approval',
294
+ statements: [
295
+ // 1 for an account created waiting for a site's approval: it is not an unconfirmed registration, so another registration of its email never replaces it.
296
+ { rql: 'ALTER TABLE cms_accounts ADD COLUMN held INTEGER' },
297
+ ],
298
+ }];
299
+
300
+ /**
301
+ * The ledger's host steps this build does not have: a database made by another build of the
302
+ * CMS, which this one cannot read safely. Empty for this build's own databases.
303
+ */
304
+ export function unknownHostSteps(applied: Iterable<number>): number[] {
305
+ const known = new Set(HOST_STEPS.map(step => step.version));
306
+ return [...applied].filter(version => !known.has(version)).sort((a, b) => a - b);
307
+ }
308
+
309
+ /** The error a database of another build gets, naming its steps and this build's. */
310
+ export function hostSchemaMismatch(unknown: readonly number[]): string {
311
+ return `This database was made by another build of the CMS: its schema has host steps ${unknown.join(', ')}, which this build does not have (it has 1 to ${HOST_STEPS.length}). Start a new database, or run the build that made it.`;
312
+ }