tinyzoo 0.0.0-stage → 0.2.0

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 (391) hide show
  1. package/LICENSE +10 -0
  2. package/README.md +46 -2
  3. package/app/app.config.ts +35 -0
  4. package/app/assets/brand/characters/bright/bird.svg +1 -0
  5. package/app/assets/brand/characters/bright/bunny.svg +1 -0
  6. package/app/assets/brand/characters/bright/elephant.svg +1 -0
  7. package/app/assets/brand/characters/bright/fox.svg +1 -0
  8. package/app/assets/brand/characters/bright/frog.svg +1 -0
  9. package/app/assets/brand/characters/bright/octopus.svg +1 -0
  10. package/app/assets/brand/characters/muted/bee.svg +1 -0
  11. package/app/assets/brand/characters/muted/bird.svg +1 -0
  12. package/app/assets/brand/characters/muted/butterfly.svg +1 -0
  13. package/app/assets/brand/characters/muted/chick.svg +1 -0
  14. package/app/assets/brand/characters/muted/cow.svg +1 -0
  15. package/app/assets/brand/characters/muted/deer.svg +1 -0
  16. package/app/assets/brand/characters/muted/duck.svg +1 -0
  17. package/app/assets/brand/characters/muted/elephant.svg +1 -0
  18. package/app/assets/brand/characters/muted/fox.svg +1 -0
  19. package/app/assets/brand/characters/muted/hedgehog.svg +1 -0
  20. package/app/assets/brand/characters/muted/koala.svg +1 -0
  21. package/app/assets/brand/characters/muted/ladybug.svg +1 -0
  22. package/app/assets/brand/characters/muted/lion.svg +1 -0
  23. package/app/assets/brand/characters/muted/mouse.svg +1 -0
  24. package/app/assets/brand/characters/muted/octopus.svg +1 -0
  25. package/app/assets/brand/characters/muted/owl.svg +1 -0
  26. package/app/assets/brand/characters/muted/panda.svg +1 -0
  27. package/app/assets/brand/characters/muted/penguin.svg +1 -0
  28. package/app/assets/brand/characters/muted/pig.svg +1 -0
  29. package/app/assets/brand/characters/muted/poodle.svg +1 -0
  30. package/app/assets/brand/characters/muted/raccoon.svg +1 -0
  31. package/app/assets/brand/characters/muted/seal.svg +1 -0
  32. package/app/assets/brand/characters/muted/sheep.svg +1 -0
  33. package/app/assets/brand/characters/muted/snail.svg +1 -0
  34. package/app/assets/brand/characters/muted/turtle.svg +1 -0
  35. package/app/assets/brand/characters/muted/whale.svg +1 -0
  36. package/app/assets/brand/logo/favicon.svg +1 -0
  37. package/app/assets/brand/logo/logo-primary-reversed.svg +1 -0
  38. package/app/assets/brand/logo/logo-primary.svg +1 -0
  39. package/app/assets/brand/logo/mascot-reversed.svg +1 -0
  40. package/app/assets/brand/logo/mascot.svg +1 -0
  41. package/app/assets/brand/logo/wordmark-reversed.svg +1 -0
  42. package/app/assets/brand/logo/wordmark.svg +1 -0
  43. package/app/assets/css/cms-admin.css +607 -0
  44. package/app/components/CmsBlocks.vue +30 -0
  45. package/app/components/CmsCell.vue +16 -0
  46. package/app/components/CmsCellBadge.vue +18 -0
  47. package/app/components/CmsCellBlocks.vue +9 -0
  48. package/app/components/CmsCellBoolean.vue +12 -0
  49. package/app/components/CmsCellCode.vue +7 -0
  50. package/app/components/CmsCellColor.vue +13 -0
  51. package/app/components/CmsCellDate.vue +10 -0
  52. package/app/components/CmsCellExcerpt.vue +21 -0
  53. package/app/components/CmsCellGallery.vue +10 -0
  54. package/app/components/CmsCellGroup.vue +23 -0
  55. package/app/components/CmsCellNumber.vue +7 -0
  56. package/app/components/CmsCellRelation.vue +8 -0
  57. package/app/components/CmsCellText.vue +12 -0
  58. package/app/components/CmsCellThumb.vue +18 -0
  59. package/app/components/CmsConfirmDialog.vue +55 -0
  60. package/app/components/CmsContent.vue +103 -0
  61. package/app/components/CmsDayPicker.vue +44 -0
  62. package/app/components/CmsEmptyState.vue +35 -0
  63. package/app/components/CmsField.vue +111 -0
  64. package/app/components/CmsFieldBlocks.vue +353 -0
  65. package/app/components/CmsFieldBoolean.vue +8 -0
  66. package/app/components/CmsFieldColor.vue +42 -0
  67. package/app/components/CmsFieldDate.vue +131 -0
  68. package/app/components/CmsFieldGallery.vue +114 -0
  69. package/app/components/CmsFieldGroup.vue +53 -0
  70. package/app/components/CmsFieldMedia.vue +219 -0
  71. package/app/components/CmsFieldNumber.vue +11 -0
  72. package/app/components/CmsFieldRelationMany.vue +54 -0
  73. package/app/components/CmsFieldRelationOne.vue +47 -0
  74. package/app/components/CmsFieldRichText.vue +45 -0
  75. package/app/components/CmsFieldSelect.vue +41 -0
  76. package/app/components/CmsFieldSlug.vue +46 -0
  77. package/app/components/CmsFieldText.vue +75 -0
  78. package/app/components/CmsFormSections.vue +150 -0
  79. package/app/components/CmsHighlight.vue +22 -0
  80. package/app/components/CmsImage.vue +39 -0
  81. package/app/components/CmsLinkPicker.vue +122 -0
  82. package/app/components/CmsLinkTarget.vue +35 -0
  83. package/app/components/CmsListFilters.vue +107 -0
  84. package/app/components/CmsLiveNotice.vue +64 -0
  85. package/app/components/CmsLoadError.vue +24 -0
  86. package/app/components/CmsMascot.vue +10 -0
  87. package/app/components/CmsMediaLibrary.vue +114 -0
  88. package/app/components/CmsMediaPreview.vue +34 -0
  89. package/app/components/CmsMediaThumb.vue +22 -0
  90. package/app/components/CmsMoment.vue +42 -0
  91. package/app/components/CmsNav.vue +39 -0
  92. package/app/components/CmsPage.vue +86 -0
  93. package/app/components/CmsPasswordInput.vue +22 -0
  94. package/app/components/CmsPresence.vue +29 -0
  95. package/app/components/CmsPreviewPanel.vue +131 -0
  96. package/app/components/CmsPublishReview.vue +102 -0
  97. package/app/components/CmsRevisions.vue +195 -0
  98. package/app/components/CmsRichTextEditor.vue +290 -0
  99. package/app/components/CmsSearch.vue +99 -0
  100. package/app/components/CmsSection.vue +20 -0
  101. package/app/components/CmsSingletonForm.vue +283 -0
  102. package/app/components/CmsStatusBadge.vue +21 -0
  103. package/app/components/CmsTime.vue +16 -0
  104. package/app/components/CmsTranslationRef.vue +27 -0
  105. package/app/components/CmsTreeList.vue +453 -0
  106. package/app/components/CmsUnsavedText.vue +34 -0
  107. package/app/components/CmsWordmark.vue +10 -0
  108. package/app/composables/useCmsAdminHead.ts +18 -0
  109. package/app/composables/useCmsCollab.ts +165 -0
  110. package/app/composables/useCmsConfirm.ts +39 -0
  111. package/app/composables/useCmsEntryActions.ts +75 -0
  112. package/app/composables/useCmsFocus.ts +13 -0
  113. package/app/composables/useCmsForm.ts +273 -0
  114. package/app/composables/useCmsI18n.ts +185 -0
  115. package/app/composables/useCmsLinkTargets.ts +84 -0
  116. package/app/composables/useCmsLive.ts +199 -0
  117. package/app/composables/useCmsSearch.ts +62 -0
  118. package/app/composables/useCmsSeo.ts +99 -0
  119. package/app/composables/useCmsSignedIn.ts +24 -0
  120. package/app/composables/useCmsUiLocale.ts +24 -0
  121. package/app/i18n/cs.ts +781 -0
  122. package/app/i18n/de.ts +781 -0
  123. package/app/i18n/en.ts +782 -0
  124. package/app/i18n/types.ts +22 -0
  125. package/app/layouts/admin.vue +224 -0
  126. package/app/layouts/cms-auth.vue +37 -0
  127. package/app/middleware/cms-api-not-a-page.global.ts +10 -0
  128. package/app/middleware/cms-auth.global.ts +16 -0
  129. package/app/pages/admin/[entity]/[id].vue +1109 -0
  130. package/app/pages/admin/[entity]/index.vue +733 -0
  131. package/app/pages/admin/account.vue +117 -0
  132. package/app/pages/admin/forgot.vue +50 -0
  133. package/app/pages/admin/index.vue +184 -0
  134. package/app/pages/admin/invite/[token].vue +73 -0
  135. package/app/pages/admin/login.vue +84 -0
  136. package/app/pages/admin/media.vue +494 -0
  137. package/app/pages/admin/privacy.vue +193 -0
  138. package/app/pages/admin/reset/[token].vue +52 -0
  139. package/app/pages/admin/setup.vue +87 -0
  140. package/app/pages/admin/styleguide.vue +261 -0
  141. package/app/pages/admin/team.vue +258 -0
  142. package/app/plugins/cmsLivePreview.client.ts +20 -0
  143. package/app/plugins/cmsSignedOut.client.ts +31 -0
  144. package/app/utils/cmsBlockType.ts +11 -0
  145. package/app/utils/cmsBrand.ts +38 -0
  146. package/app/utils/cmsDiff.ts +83 -0
  147. package/app/utils/cmsFetch.ts +7 -0
  148. package/app/utils/cmsFocusInvalid.ts +22 -0
  149. package/app/utils/cmsIssues.ts +128 -0
  150. package/app/utils/cmsJson.ts +28 -0
  151. package/app/utils/cmsMedia.ts +63 -0
  152. package/app/utils/cmsSections.ts +154 -0
  153. package/app/utils/cmsTime.ts +41 -0
  154. package/cli/backup.ts +713 -0
  155. package/cli/breaking.ts +174 -0
  156. package/cli/content-migrate.ts +239 -0
  157. package/cli/db.ts +86 -0
  158. package/cli/export.ts +336 -0
  159. package/cli/generate.ts +146 -0
  160. package/cli/import.ts +643 -0
  161. package/cli/index.ts +140 -0
  162. package/cli/links-convert.ts +291 -0
  163. package/cli/seed-engine.ts +705 -0
  164. package/cli/seed.ts +339 -0
  165. package/cli/status.ts +49 -0
  166. package/cli/tar.ts +304 -0
  167. package/cli/transfer.ts +677 -0
  168. package/cli/up.ts +27 -0
  169. package/cli/upgrade.ts +60 -0
  170. package/cli/versions.ts +60 -0
  171. package/forms/components/CmsForm.vue +186 -0
  172. package/forms/index.ts +203 -0
  173. package/forms/messages.ts +104 -0
  174. package/forms/retention.ts +48 -0
  175. package/forms/server/form.get.ts +12 -0
  176. package/forms/server/form.post.ts +109 -0
  177. package/forms/server/formSettings.ts +12 -0
  178. package/forms/server/forms.ts +127 -0
  179. package/module.ts +373 -0
  180. package/nuxt.config.ts +43 -0
  181. package/package.json +99 -3
  182. package/server/api/cms/[entity]/[id]/revisions/[rev].get.ts +19 -0
  183. package/server/api/cms/[entity]/[id]/revisions/index.get.ts +14 -0
  184. package/server/api/cms/[entity]/[id].delete.ts +52 -0
  185. package/server/api/cms/[entity]/[id].get.ts +21 -0
  186. package/server/api/cms/[entity]/[id].patch.ts +116 -0
  187. package/server/api/cms/[entity]/_export.get.ts +69 -0
  188. package/server/api/cms/[entity]/_move.post.ts +81 -0
  189. package/server/api/cms/[entity]/_order.post.ts +49 -0
  190. package/server/api/cms/[entity]/_tree.get.ts +41 -0
  191. package/server/api/cms/[entity]/index.get.ts +62 -0
  192. package/server/api/cms/[entity]/index.post.ts +38 -0
  193. package/server/api/cms/[entity]/options.get.ts +55 -0
  194. package/server/api/cms/_auth/accept.post.ts +33 -0
  195. package/server/api/cms/_auth/forgot.post.ts +39 -0
  196. package/server/api/cms/_auth/invite/[token].get.ts +18 -0
  197. package/server/api/cms/_auth/login.post.ts +42 -0
  198. package/server/api/cms/_auth/logout.post.ts +7 -0
  199. package/server/api/cms/_auth/me.get.ts +5 -0
  200. package/server/api/cms/_auth/me.patch.ts +32 -0
  201. package/server/api/cms/_auth/password.post.ts +29 -0
  202. package/server/api/cms/_auth/reset.post.ts +34 -0
  203. package/server/api/cms/_auth/setup.post.ts +46 -0
  204. package/server/api/cms/_auth/status.get.ts +18 -0
  205. package/server/api/cms/_collab/session.get.ts +30 -0
  206. package/server/api/cms/_collab/socket.ts +51 -0
  207. package/server/api/cms/_collab/unsaved.delete.ts +16 -0
  208. package/server/api/cms/_graph.get.ts +20 -0
  209. package/server/api/cms/_links/resolve.post.ts +17 -0
  210. package/server/api/cms/_links.get.ts +81 -0
  211. package/server/api/cms/_live.ts +30 -0
  212. package/server/api/cms/_media/[id]/usage.get.ts +14 -0
  213. package/server/api/cms/_media/[id].delete.ts +29 -0
  214. package/server/api/cms/_media/[id].get.ts +13 -0
  215. package/server/api/cms/_media/[id].patch.ts +64 -0
  216. package/server/api/cms/_media/exists.post.ts +15 -0
  217. package/server/api/cms/_media/img/[id]/[file].get.ts +42 -0
  218. package/server/api/cms/_media/index.get.ts +44 -0
  219. package/server/api/cms/_media/index.post.ts +147 -0
  220. package/server/api/cms/_media/raw/[...key].get.ts +85 -0
  221. package/server/api/cms/_media/raw/[...key].head.ts +2 -0
  222. package/server/api/cms/_overview.get.ts +87 -0
  223. package/server/api/cms/_preview.post.ts +17 -0
  224. package/server/api/cms/_privacy/delete.post.ts +14 -0
  225. package/server/api/cms/_privacy/export.post.ts +25 -0
  226. package/server/api/cms/_privacy/lookup.post.ts +27 -0
  227. package/server/api/cms/_search.get.ts +17 -0
  228. package/server/api/cms/_singleton/[name].get.ts +19 -0
  229. package/server/api/cms/_singleton/[name].put.ts +76 -0
  230. package/server/api/cms/_users/[id]/resend.post.ts +25 -0
  231. package/server/api/cms/_users/[id].delete.ts +21 -0
  232. package/server/api/cms/_users/[id].patch.ts +45 -0
  233. package/server/api/cms/_users/index.get.ts +28 -0
  234. package/server/api/cms/_users/index.post.ts +39 -0
  235. package/server/api/content/[entity]/[slug].get.ts +74 -0
  236. package/server/api/content/[entity]/index.get.ts +100 -0
  237. package/server/api/content/_global/[name].get.ts +23 -0
  238. package/server/api/content/_search.get.ts +28 -0
  239. package/server/api/content/_sitemap.get.ts +8 -0
  240. package/server/api/content/_tree/[entity].get.ts +31 -0
  241. package/server/api/content-types.ts.get.ts +17 -0
  242. package/server/api/health/live.get.ts +10 -0
  243. package/server/api/health/ready.get.ts +21 -0
  244. package/server/api/openapi.get.ts +48 -0
  245. package/server/api/openapi.json.get.ts +32 -0
  246. package/server/db/driver.ts +8 -0
  247. package/server/mail/mailer.ts +43 -0
  248. package/server/middleware/cms-0-headers.ts +37 -0
  249. package/server/middleware/cms-0-migrate-gate.ts +14 -0
  250. package/server/middleware/cms-csrf.ts +28 -0
  251. package/server/middleware/cms-limits.ts +20 -0
  252. package/server/middleware/cms-redirects.ts +21 -0
  253. package/server/middleware/cms-session.ts +41 -0
  254. package/server/middleware/cms-z-plugin-access.ts +15 -0
  255. package/server/plugins/cms-0-config-check.ts +35 -0
  256. package/server/plugins/cms-changes.ts +53 -0
  257. package/server/plugins/cms-content-etag.ts +21 -0
  258. package/server/plugins/cms-errors.ts +45 -0
  259. package/server/plugins/cms-migrate.ts +5 -0
  260. package/server/plugins/cms-plugins.ts +17 -0
  261. package/server/plugins/cms-search.ts +12 -0
  262. package/server/plugins/cms-setup-code.ts +15 -0
  263. package/server/routes/sitemap.xml.get.ts +25 -0
  264. package/server/store/redis.ts +95 -0
  265. package/server/store/types.ts +21 -0
  266. package/server/tasks/cms/retention.ts +10 -0
  267. package/server/tasks/cms/scheduled.ts +10 -0
  268. package/server/tasks/cms/search-reindex.ts +12 -0
  269. package/server/utils/cmsAudit.ts +29 -0
  270. package/server/utils/cmsAuth.ts +143 -0
  271. package/server/utils/cmsBlocks.ts +47 -0
  272. package/server/utils/cmsChanges.ts +222 -0
  273. package/server/utils/cmsCollab.ts +286 -0
  274. package/server/utils/cmsCollabServer.ts +451 -0
  275. package/server/utils/cmsCollate.ts +27 -0
  276. package/server/utils/cmsContent.ts +376 -0
  277. package/server/utils/cmsCrud.ts +361 -0
  278. package/server/utils/cmsCsv.ts +24 -0
  279. package/server/utils/cmsDb.ts +23 -0
  280. package/server/utils/cmsDemo.ts +36 -0
  281. package/server/utils/cmsEmails.ts +125 -0
  282. package/server/utils/cmsErrors.ts +40 -0
  283. package/server/utils/cmsFold.ts +17 -0
  284. package/server/utils/cmsImageSizes.ts +51 -0
  285. package/server/utils/cmsJson.ts +6 -0
  286. package/server/utils/cmsLinkAddress.ts +50 -0
  287. package/server/utils/cmsLinks.ts +248 -0
  288. package/server/utils/cmsListQuery.ts +93 -0
  289. package/server/utils/cmsLive.ts +392 -0
  290. package/server/utils/cmsLocales.ts +96 -0
  291. package/server/utils/cmsMail.ts +28 -0
  292. package/server/utils/cmsMediaGc.ts +188 -0
  293. package/server/utils/cmsMediaKeys.ts +62 -0
  294. package/server/utils/cmsMigrate.ts +86 -0
  295. package/server/utils/cmsPreviewDraft.ts +72 -0
  296. package/server/utils/cmsPrivacy.ts +176 -0
  297. package/server/utils/cmsPublished.ts +57 -0
  298. package/server/utils/cmsRange.ts +38 -0
  299. package/server/utils/cmsRateLimit.ts +82 -0
  300. package/server/utils/cmsRedirects.ts +160 -0
  301. package/server/utils/cmsRetention.ts +61 -0
  302. package/server/utils/cmsRevisions.ts +103 -0
  303. package/server/utils/cmsScheduler.ts +45 -0
  304. package/server/utils/cmsScrub.ts +107 -0
  305. package/server/utils/cmsSearch.ts +354 -0
  306. package/server/utils/cmsSession.ts +102 -0
  307. package/server/utils/cmsSetupCode.ts +97 -0
  308. package/server/utils/cmsSitemap.ts +176 -0
  309. package/server/utils/cmsStorage.ts +580 -0
  310. package/server/utils/cmsStore.ts +109 -0
  311. package/server/utils/cmsTooMany.ts +9 -0
  312. package/server/utils/cmsTranslationWrite.ts +186 -0
  313. package/server/utils/cmsTree.ts +251 -0
  314. package/server/utils/cmsTreeAddress.ts +65 -0
  315. package/server/utils/cmsUpload.ts +197 -0
  316. package/server/utils/cmsWebhooks.ts +101 -0
  317. package/shared/json.ts +11 -0
  318. package/shared/richtext-editor.ts +56 -0
  319. package/shared/richtext-normalize.ts +33 -0
  320. package/shared/richtext-paste.ts +151 -0
  321. package/shared/storage-delivery.ts +53 -0
  322. package/src/blocks.ts +624 -0
  323. package/src/changes.ts +54 -0
  324. package/src/content-shape.ts +340 -0
  325. package/src/deprecate.ts +64 -0
  326. package/src/descriptor.ts +149 -0
  327. package/src/docs.ts +27 -0
  328. package/src/emitter.ts +94 -0
  329. package/src/entity.ts +624 -0
  330. package/src/errorReport.ts +41 -0
  331. package/src/fields.ts +465 -0
  332. package/src/i18n.ts +26 -0
  333. package/src/index.ts +54 -0
  334. package/src/linkref.ts +31 -0
  335. package/src/links.ts +179 -0
  336. package/src/locales.ts +39 -0
  337. package/src/media.ts +48 -0
  338. package/src/migrate.ts +236 -0
  339. package/src/openapi.ts +377 -0
  340. package/src/paths.ts +88 -0
  341. package/src/plugin.ts +505 -0
  342. package/src/presets.ts +230 -0
  343. package/src/richtext.ts +88 -0
  344. package/src/sdk.ts +181 -0
  345. package/src/search.ts +312 -0
  346. package/src/seed.ts +225 -0
  347. package/src/setupCode.ts +30 -0
  348. package/src/system.ts +219 -0
  349. package/src/tree.ts +421 -0
  350. package/src/upgrades.ts +137 -0
  351. package/starter/.env.example +10 -0
  352. package/starter/README.md +65 -0
  353. package/starter/app/app.config.ts +10 -0
  354. package/starter/app/app.vue +12 -0
  355. package/starter/app/assets/css/main.css +15 -0
  356. package/starter/app/components/global/BlockContact.vue +15 -0
  357. package/starter/app/components/global/BlockCta.vue +15 -0
  358. package/starter/app/components/global/BlockHero.vue +23 -0
  359. package/starter/app/components/global/BlockImage.vue +13 -0
  360. package/starter/app/components/global/BlockText.vue +12 -0
  361. package/starter/app/composables/useSite.ts +77 -0
  362. package/starter/app/error.vue +28 -0
  363. package/starter/app/layouts/default.vue +106 -0
  364. package/starter/app/lib/cms-content.ts +152 -0
  365. package/starter/app/pages/[...slug].vue +66 -0
  366. package/starter/app/pages/hledat.vue +59 -0
  367. package/starter/app/pages/index.vue +20 -0
  368. package/starter/cms.config.ts +137 -0
  369. package/starter/compose.yaml +15 -0
  370. package/starter/drizzle.config.ts +8 -0
  371. package/starter/gitignore.txt +8 -0
  372. package/starter/nuxt.config.ts +13 -0
  373. package/starter/package.json +38 -0
  374. package/starter/scripts/typecheck.mjs +38 -0
  375. package/starter/seed/index.ts +297 -0
  376. package/starter/seed/media/about.jpg +0 -0
  377. package/starter/seed/media/hero.jpg +0 -0
  378. package/starter/seed/media/logo.png +0 -0
  379. package/starter/seed/media/news-bread.jpg +0 -0
  380. package/starter/seed/media/news-morning.jpg +0 -0
  381. package/starter/server/db/migrations/0000_init.sql +269 -0
  382. package/starter/server/db/migrations/0001_page_tree.sql +5 -0
  383. package/starter/server/db/migrations/0002_opening_hours_per_language.sql +1 -0
  384. package/starter/server/db/migrations/meta/0000_snapshot.json +1890 -0
  385. package/starter/server/db/migrations/meta/0001_snapshot.json +1887 -0
  386. package/starter/server/db/migrations/meta/0002_snapshot.json +1893 -0
  387. package/starter/server/db/migrations/meta/_journal.json +27 -0
  388. package/starter/server/db/schema.ts +253 -0
  389. package/starter/server/plugins/sitemap.ts +11 -0
  390. package/starter/server/routes/robots.txt.get.ts +12 -0
  391. package/starter/tsconfig.json +9 -0
@@ -0,0 +1,143 @@
1
+ import { and, eq, gt, isNull, ne, sql } from 'tinyzoo'
2
+ import type { CmsRegistry } from 'tinyzoo'
3
+ import { z } from 'zod'
4
+
5
+ /**
6
+ * Built-in accounts: sessions (sealed cookie via nuxt-auth-utils), one-time links
7
+ * for invites and password resets, and the helpers the auth and team endpoints share.
8
+ *
9
+ * Password hashing is nuxt-auth-utils' scrypt.
10
+ */
11
+
12
+ export type CmsAuthMode = 'builtin' | 'external'
13
+
14
+ /** 'builtin' = the CMS owns users and sign-in; 'external' = the host sets event.context.cmsUser. */
15
+ export function cmsAuthMode(): CmsAuthMode {
16
+ return ((useRuntimeConfig().cms as any)?.auth?.mode === 'external') ? 'external' : 'builtin'
17
+ }
18
+
19
+ export function assertBuiltinAuth(): void {
20
+ if (cmsAuthMode() !== 'builtin') throw createError({ statusCode: 404, statusMessage: 'Not found' })
21
+ }
22
+
23
+ export const normalizeEmail = (email: string) => email.trim().toLowerCase()
24
+
25
+ /** Password rules, shown to people up front: at least 10 characters. */
26
+ export const passwordSchema = z.string().min(10, 'At least 10 characters').max(200)
27
+ export const emailSchema = z.string().trim().toLowerCase().pipe(z.email())
28
+
29
+ // ── one-time links ─────────────────────────────────────────────────────────────
30
+
31
+ const toBase64Url = (bytes: Uint8Array) =>
32
+ btoa(String.fromCharCode(...bytes)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
33
+
34
+ export async function sha256Hex(text: string): Promise<string> {
35
+ const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text))
36
+ return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, '0')).join('')
37
+ }
38
+
39
+ export const INVITE_TTL_MS = 7 * 24 * 60 * 60 * 1000
40
+ export const RESET_TTL_MS = 60 * 60 * 1000
41
+
42
+ /**
43
+ * Create a one-time link secret for a user. Earlier unused links of the same kind
44
+ * stop working (a resent invite replaces the old one). Only the hash is stored.
45
+ */
46
+ export async function issueToken(db: any, reg: CmsRegistry, opts: { userId: string; kind: 'invite' | 'reset'; ttlMs: number; createdBy?: string | null }): Promise<string> {
47
+ const secret = toBase64Url(crypto.getRandomValues(new Uint8Array(32)))
48
+ const t = reg.cmsTokens
49
+ await db.transaction(async (tx: any) => {
50
+ await tx.update(t).set({ usedAt: new Date() })
51
+ .where(and(eq(t.userId, opts.userId), eq(t.kind, opts.kind), isNull(t.usedAt)))
52
+ await tx.insert(t).values({
53
+ userId: opts.userId,
54
+ kind: opts.kind,
55
+ tokenHash: await sha256Hex(secret),
56
+ expiresAt: new Date(Date.now() + opts.ttlMs),
57
+ createdBy: opts.createdBy ?? null,
58
+ })
59
+ })
60
+ return secret
61
+ }
62
+
63
+ /** The valid (unused, unexpired) token row for a secret, with its user — or null. */
64
+ export async function findToken(db: any, reg: CmsRegistry, secret: string, kind: 'invite' | 'reset') {
65
+ if (!secret || secret.length > 200) return null
66
+ const t = reg.cmsTokens
67
+ const [row] = await db.select().from(t).where(and(
68
+ eq(t.tokenHash, await sha256Hex(secret)), eq(t.kind, kind), isNull(t.usedAt), gt(t.expiresAt, new Date()),
69
+ ))
70
+ if (!row) return null
71
+ const [user] = await db.select().from(reg.cmsUsers).where(eq(reg.cmsUsers.id, row.userId))
72
+ return user ? { token: row, user } : null
73
+ }
74
+
75
+ // ── sessions ───────────────────────────────────────────────────────────────────
76
+
77
+ /** What the browser's session carries (and `useUserSession()` exposes). */
78
+ export function sessionUser(u: any) {
79
+ return { id: u.id as string, email: u.email as string, name: (u.name as string) || u.email, role: u.role as string, locale: (u.locale as string | null) ?? null }
80
+ }
81
+
82
+ /** Sign the person in on this browser (replaces any previous session). */
83
+ export async function startCmsSession(event: any, u: any): Promise<void> {
84
+ await replaceUserSession(event, { user: sessionUser(u), cms: { uid: u.id as string, sv: u.sessionVersion as number } } as any)
85
+ }
86
+
87
+ // ── links in emails ────────────────────────────────────────────────────────────
88
+
89
+ /**
90
+ * Absolute URL of an admin page for links in emails. From `cms.publicUrl`
91
+ * (NUXT_CMS_PUBLIC_URL) — never from the request's Host header, which a client
92
+ * controls (a forged Host would put an attacker's domain in a reset link). Outside
93
+ * production the request origin is accepted so local development needs no setup.
94
+ */
95
+ export function adminLink(event: any, path: string): string {
96
+ const configured = String((useRuntimeConfig().cms as any)?.publicUrl || '').replace(/\/+$/, '')
97
+ if (configured) return configured + path
98
+ if (import.meta.dev || process.env.NODE_ENV !== 'production' || process.env.CMS_ALLOW_REQUEST_ORIGIN === '1') {
99
+ return getRequestURL(event).origin + path
100
+ }
101
+ throw createError({ statusCode: 500, statusMessage: 'cms.publicUrl (NUXT_CMS_PUBLIC_URL) must be set to send links by email' })
102
+ }
103
+
104
+ /**
105
+ * The client IP for rate limiting. `cms.auth.ipHeader` (e.g. 'cf-connecting-ip' on
106
+ * Cloudflare) wins when set; otherwise X-Forwarded-For is read through exactly
107
+ * `cms.auth.trustedProxies` hops (default 1: one ingress / load balancer, as on
108
+ * Pushbase). Set 0 when the app is reached directly.
109
+ */
110
+ export function clientIp(event: any): string {
111
+ const auth = ((useRuntimeConfig().cms as any)?.auth ?? {}) as { ipHeader?: string; trustedProxies?: number }
112
+ if (auth.ipHeader) {
113
+ const v = getRequestHeader(event, auth.ipHeader)
114
+ if (v) return v.trim().slice(0, 64)
115
+ }
116
+ const socketIp = getRequestIP(event) || undefined
117
+ return pickClientIp(getRequestHeader(event, 'x-forwarded-for'), socketIp, Number(auth.trustedProxies ?? 1))
118
+ }
119
+
120
+ /** Equal-time sign-in failures: verify against a fixed hash when the account does not exist. */
121
+ let dummyHash: Promise<string> | undefined
122
+ export function dummyPasswordHash(): Promise<string> {
123
+ dummyHash ??= hashPassword('not-a-real-password-just-for-timing')
124
+ return dummyHash
125
+ }
126
+
127
+ /** Advisory lock serialising changes that could remove the last active administrator. */
128
+ export const ADMIN_GUARD_LOCK = 72_011_002
129
+
130
+ /** 409 unless some other active administrator exists (never lock everyone out). */
131
+ export async function assertAnotherActiveAdmin(db: any, reg: CmsRegistry, exceptId: string): Promise<void> {
132
+ const u = reg.cmsUsers
133
+ const [row] = await db.select({ n: sql<number>`count(*)::int` }).from(u)
134
+ .where(and(eq(u.role, 'admin'), eq(u.status, 'active'), ne(u.id, exceptId)))
135
+ if ((row?.n ?? 0) === 0) {
136
+ throw createError({ statusCode: 409, statusMessage: 'There must always be at least one active administrator' })
137
+ }
138
+ }
139
+
140
+ /** The setup code first-run setup requires, or '' when none is configured. */
141
+ export function setupToken(): string {
142
+ return String((useRuntimeConfig().cms as any)?.auth?.setupToken || '')
143
+ }
@@ -0,0 +1,47 @@
1
+ import { logUpgradeFailure, upgradeStoredRow } from 'tinyzoo'
2
+ import type { CmsEntity } from 'tinyzoo'
3
+ import type { ZodTypeAny } from 'zod'
4
+
5
+ /**
6
+ * Stored JSON (blocks, lists, rich text) is brought to the current shape wherever it
7
+ * is READ, so a deploy never has to rewrite it (`tinyzoo content:migrate` does that
8
+ * when wanted). One place for every read site: the content API, the admin's form,
9
+ * the search index, revisions. Mutates `row` (a fresh database row, or a copy) and
10
+ * returns it.
11
+ *
12
+ * A block whose `upgrade` throws is logged once (entity, record, block id) and left
13
+ * as stored. With `admin`, it is also flagged (`__upgradeFailed`) so the editor can
14
+ * show it as needing attention instead of as a normal block, and every block and list
15
+ * item carries its `v` (version 1 included), so the editor sends it back and a save
16
+ * from an editor that is behind a deploy is recognised.
17
+ */
18
+ export function upgradeStoredJson<R extends Record<string, any>>(e: CmsEntity, row: R, opts: { admin?: boolean; recordId?: string } = {}): R {
19
+ const where = `${e.name} ${opts.recordId ?? row.id ?? ''}`.trim()
20
+ return upgradeStoredRow(e.fields, row, {
21
+ markFailed: !!opts.admin,
22
+ stamp: !!opts.admin,
23
+ where,
24
+ onError: (issue) => logUpgradeFailure(issue, where),
25
+ })
26
+ }
27
+
28
+ /** The same on a copy: a translation or revision row that must stay as loaded. */
29
+ export function upgradedCopy<R extends Record<string, any>>(e: CmsEntity, row: R, opts: { admin?: boolean; recordId?: string } = {}): R {
30
+ return upgradeStoredJson(e, { ...row }, opts)
31
+ }
32
+
33
+ /**
34
+ * The schema a write is checked with: `e.insertSchema` / `e.updateSchema`, with the
35
+ * blocks and list fields told what the row holds now (`stored`: the raw row, or the
36
+ * translation row for a translation; nothing for a new row). Blocks that are behind,
37
+ * whose upgrade fails, or whose type is gone are only kept when they are stored
38
+ * already — see `blocksSchema`.
39
+ */
40
+ export function writeSchema(e: CmsEntity, mode: 'create' | 'update', stored?: Record<string, any> | null): ZodTypeAny {
41
+ const base: any = mode === 'create' ? e.insertSchema : e.updateSchema
42
+ const own: Record<string, ZodTypeAny> = {}
43
+ for (const [key, fld] of Object.entries(e.fields)) {
44
+ if (fld.type === 'blocks' || fld.type === 'group') own[key] = fld.zod(mode, { stored: stored?.[key] })
45
+ }
46
+ return Object.keys(own).length ? base.extend(own) : base
47
+ }
@@ -0,0 +1,222 @@
1
+ import { and, eq, fillPathTemplate, indexTree, inArray, pathTemplateKeys, sitePath, sitePrefix, type CmsChange, type CmsChangeType, type CmsEntity, type CmsRegistry } from 'tinyzoo'
2
+
3
+ /**
4
+ * One place every content write reports to. A change:
5
+ *
6
+ * - clears this instance's caches built from content (redirects, sitemap) at once;
7
+ * - is published on the shared store's `changes` channel, so the other replicas clear
8
+ * theirs too and the realtime hub can tell people looking at the same content;
9
+ * - runs the `cms:change` Nitro hook, for host apps that react in-process;
10
+ * - when it can change the public website, is sent to the configured webhooks
11
+ * (signed; see cmsWebhooks.ts) so the site can purge its cache.
12
+ *
13
+ * Reporting never fails the write that triggered it.
14
+ */
15
+ export const CHANGES_CHANNEL = 'changes'
16
+
17
+ /**
18
+ * Identifies this process on the shared channel, so it can skip its own messages.
19
+ * Made on first use.
20
+ */
21
+ let _instanceId: string | undefined
22
+ export function instanceId(): string {
23
+ return (_instanceId ??= crypto.randomUUID())
24
+ }
25
+
26
+ /** The address prefix of a language ('/en'), per `defineLocales({ prefix })`. */
27
+ export function localePrefix(reg: CmsRegistry, locale: string | null): string {
28
+ return sitePrefix(reg.locales, locale)
29
+ }
30
+
31
+ /** A path template filled in from a row ('/blog/:slug' → '/blog/hello'), or null when a part is empty. */
32
+ export const fillPath = fillPathTemplate
33
+
34
+ /** Placeholder keys of `content.path` (every language's, for a per-language map). */
35
+ export const pathKeys = pathTemplateKeys
36
+
37
+ /**
38
+ * What visitors can see of one entry right now: whether it is live (public
39
+ * collection, and its `content.published` rule holds — the same rule the content
40
+ * API applies, scheduling included) and its addresses in every language.
41
+ */
42
+ export interface ChangeSnapshot {
43
+ live: boolean
44
+ paths: { locale: string | null; path: string }[]
45
+ /** Page tree: which of the entry and everything below it are live (the entry's own pages' addresses are in `paths`). */
46
+ liveIds?: string[]
47
+ }
48
+
49
+ /**
50
+ * In a page tree an entry's reach is its whole subtree: unpublishing, moving or renaming
51
+ * it changes what visitors see of every page below it, so `paths` lists theirs too.
52
+ */
53
+ async function treeSnapshot(db: any, e: CmsEntity, id: string): Promise<ChangeSnapshot> {
54
+ const { reg } = useCms()
55
+ const nodes = await loadTreeNodes(db, reg, e)
56
+ const ids = subtreeIds(nodes, id)
57
+ const live = await liveAmong(db, e, ids)
58
+ const index = indexTree(nodes)
59
+ const paths = ids.filter((i) => live.has(i)).flatMap((i) => treeVersions(reg, e, index, i).map((v) => ({ locale: v.locale, path: v.loc })))
60
+ return { live: live.has(id), paths, liveIds: [...live] }
61
+ }
62
+
63
+ export async function changeSnapshot(e: CmsEntity, id: string, tx?: any): Promise<ChangeSnapshot> {
64
+ const none: ChangeSnapshot = { live: false, paths: [] }
65
+ if (!e.content?.public) return none
66
+ const { db: appDb, reg } = useCms()
67
+ const db = tx ?? appDb
68
+ if (e.tree) return treeSnapshot(db, e, id)
69
+ const keys = !e.singleton ? pathKeys(e.content.path).filter((k) => e.table[k]) : []
70
+ const cols: Record<string, any> = { id: e.table.id }
71
+ for (const k of keys) cols[k] = e.table[k]
72
+ const [row] = await db.select(cols).from(e.table).where(both(eq(e.table.id, id), publishedCond(e)))
73
+ if (!row) return none
74
+ const out: ChangeSnapshot = { live: true, paths: [] }
75
+ if (!keys.length) return out
76
+ const defaultLocale = reg.locales?.default ?? null
77
+ const base = sitePath(reg.locales, e.content.path, defaultLocale, row)
78
+ if (base) out.paths.push({ locale: defaultLocale, path: base })
79
+ const t = e.localesTable
80
+ const others = reg.locales?.locales.filter((l) => l !== defaultLocale) ?? []
81
+ if (t && others.length) {
82
+ const tcols: Record<string, any> = { locale: t.locale }
83
+ for (const k of keys) if (e.localized.includes(k)) tcols[k] = t[k]
84
+ const rows: any[] = await db.select(tcols).from(t).where(and(eq(t.parentId, id), inArray(t.locale, others)))
85
+ for (const tr of rows) {
86
+ // Untranslated parts of the address fall back to the default language.
87
+ const merged = { ...row }
88
+ for (const k of keys) if (tr[k] !== null && tr[k] !== undefined && tr[k] !== '') merged[k] = tr[k]
89
+ const p = sitePath(reg.locales, e.content.path, tr.locale, merged)
90
+ if (p) out.paths.push({ locale: tr.locale, path: p })
91
+ }
92
+ }
93
+ return out
94
+ }
95
+
96
+ /** Did what visitors could reach change: live or not, or the address in some language? */
97
+ export function addressesDiffer(before: ChangeSnapshot, after: ChangeSnapshot): boolean {
98
+ const key = (s: ChangeSnapshot) => JSON.stringify(s.live ? [...s.paths].map((p) => `${p.locale ?? ''}=${p.path}`).sort() : null)
99
+ return key(before) !== key(after)
100
+ }
101
+
102
+ export interface ReportChange {
103
+ type: CmsChangeType
104
+ entity: CmsEntity
105
+ id: string
106
+ /** What visitors saw before the write (changeSnapshot); omit for a new entry. */
107
+ before?: ChangeSnapshot | null
108
+ /** The row after the write, for its version. */
109
+ after?: Record<string, any> | null
110
+ /** The language a translation change is about. */
111
+ locale?: string | null
112
+ /** Page tree: addresses that moved with this write (see `CmsChange.moved`). */
113
+ moved?: { id: string; locale: string | null; from: string; to: string }[]
114
+ /** Nothing was saved (a scheduled entry went live): no revision. */
115
+ noRevision?: boolean
116
+ }
117
+
118
+ export async function reportChange(event: any, r: ReportChange): Promise<void> {
119
+ try {
120
+ const e = r.entity
121
+ const locale = r.locale ?? null
122
+ const deleted = r.type === 'entry.deleted'
123
+ const before = r.before ?? { live: false, paths: [] }
124
+ const after = deleted ? { live: false, paths: [] } : await changeSnapshot(e, r.id)
125
+ // Only addresses a visitor could have reached, in the language the change is about.
126
+ const paths = new Set<string>()
127
+ for (const snap of [before, after]) {
128
+ if (!snap.live) continue
129
+ for (const p of snap.paths) if (!locale || p.locale === locale) paths.add(p.path)
130
+ }
131
+ // Links follow their target: when this entry's address (or whether it can be reached at
132
+ // all) changed, everything that links to it reads differently now. Those pages go in
133
+ // `paths`, and the public globals (menus, settings) that link to it are named in `globals`:
134
+ // whatever shows them — usually every page — is stale too.
135
+ const globals = new Set<string>()
136
+ const { db, reg } = useCms()
137
+ const addressChanged = !deleted && !!e.content?.path && !e.singleton && addressesDiffer(before, after)
138
+ // An entry that went live at an address a redirect leaves from would be hidden by it: the redirect goes.
139
+ // (Writes that can do it in their own transaction have done so already; this catches every other path.)
140
+ if (addressChanged && after.live) {
141
+ await dropRedirectsFrom(db, reg, after.paths.map((p) => p.path)).catch((err) => console.error('[cms] could not remove redirects from the new addresses', scrubError(err)))
142
+ }
143
+ if (addressChanged && hasLinkFields(reg)) {
144
+ // In a page tree the whole subtree changed address (or went away) with the entry: what links to any of those pages reads differently too.
145
+ const ids = new Set([r.id, ...(r.moved ?? []).map((m) => m.id), ...(before.liveIds ?? []), ...(after.liveIds ?? [])])
146
+ for (const dep of await linksTo(db, reg, e, [...ids], 50).catch(() => [])) {
147
+ const target = reg.byName.get(dep.entity)
148
+ if (!target?.content?.public || (dep.locale && locale && dep.locale !== locale)) continue
149
+ if (target.singleton) globals.add(target.name)
150
+ else for (const p of (await changeSnapshot(target, dep.id)).paths) if (!locale || p.locale === locale) paths.add(p.path)
151
+ }
152
+ }
153
+ const change: CmsChange = {
154
+ type: r.type,
155
+ entity: e.name,
156
+ id: r.id,
157
+ locale,
158
+ public: before.live || after.live,
159
+ paths: [...paths],
160
+ ...(globals.size ? { globals: [...globals] } : {}),
161
+ ...(r.moved?.length ? { moved: r.moved } : {}),
162
+ by: event?.context?.cmsUser?.id ?? null,
163
+ version: r.after?.updatedAt ? new Date(r.after.updatedAt).toISOString() : null,
164
+ at: new Date().toISOString(),
165
+ }
166
+ // What was saved, for the history (not for deletions).
167
+ if (!r.noRevision && r.type !== 'entry.deleted' && r.type !== 'translation.deleted') {
168
+ await recordRevision(event, e, r.id, r.type === 'translation.updated' ? locale : null, r.type)
169
+ }
170
+ // The search index follows every write (its own failure never stops the rest).
171
+ if (r.type !== 'global.updated') {
172
+ await syncSearchIndex(e, r.id, r.type).catch((err) => console.error('[cms] could not update the search index', scrubError(err)))
173
+ }
174
+ applyChangeLocally(change)
175
+ notifyChangeListeners(change)
176
+ // Neither the other replicas nor the host's hooks hold up the response.
177
+ const background: Promise<unknown>[] = [
178
+ useCmsStore().then((store) => store.publish(CHANGES_CHANNEL, JSON.stringify({ origin: instanceId(), change })))
179
+ .catch((err) => console.error('[cms] could not publish change', scrubError(err))),
180
+ Promise.resolve(useNitroApp().hooks.callHook('cms:change', change))
181
+ .catch((err: unknown) => console.error('[cms] cms:change hook failed', scrubError(err))),
182
+ ]
183
+ if (change.public) background.push(sendWebhooks(change))
184
+ const all = Promise.all(background)
185
+ if (typeof event?.waitUntil === 'function') event.waitUntil(all)
186
+ }
187
+ catch (err) {
188
+ console.error('[cms] reporting a change failed', scrubError(err))
189
+ }
190
+ }
191
+
192
+ /** Clear what this instance derived from content the change touched. */
193
+ export function applyChangeLocally(change: Pick<CmsChange, 'entity'>): void {
194
+ const { reg } = useCms()
195
+ const e = reg.byName.get(change.entity)
196
+ if (!e) return
197
+ if ((e as any).preset === 'redirects') invalidateRedirects()
198
+ // A page's address moved or it went away: redirects to it (stored as a reference) point somewhere else now.
199
+ if (e.content?.path) {
200
+ invalidateSitemap()
201
+ invalidateRedirects()
202
+ }
203
+ }
204
+
205
+ const listeners = new Set<(change: CmsChange) => void>()
206
+
207
+ /** Run `fn` for every change — saved here or on another replica. Returns an unsubscribe function. */
208
+ export function onCmsChange(fn: (change: CmsChange) => void): () => void {
209
+ listeners.add(fn)
210
+ return () => listeners.delete(fn)
211
+ }
212
+
213
+ export function notifyChangeListeners(change: CmsChange): void {
214
+ for (const fn of listeners) {
215
+ try {
216
+ fn(change)
217
+ }
218
+ catch (err) {
219
+ console.error('[cms] change listener failed', scrubError(err))
220
+ }
221
+ }
222
+ }