tinyzoo 0.0.0-stage → 0.2.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 (392) 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 +98 -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 +393 -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/cmsTiming.ts +14 -0
  312. package/server/utils/cmsTooMany.ts +9 -0
  313. package/server/utils/cmsTranslationWrite.ts +186 -0
  314. package/server/utils/cmsTree.ts +251 -0
  315. package/server/utils/cmsTreeAddress.ts +65 -0
  316. package/server/utils/cmsUpload.ts +228 -0
  317. package/server/utils/cmsWebhooks.ts +104 -0
  318. package/shared/json.ts +11 -0
  319. package/shared/richtext-editor.ts +56 -0
  320. package/shared/richtext-normalize.ts +33 -0
  321. package/shared/richtext-paste.ts +151 -0
  322. package/shared/storage-delivery.ts +53 -0
  323. package/src/blocks.ts +624 -0
  324. package/src/changes.ts +54 -0
  325. package/src/content-shape.ts +340 -0
  326. package/src/deprecate.ts +64 -0
  327. package/src/descriptor.ts +149 -0
  328. package/src/docs.ts +27 -0
  329. package/src/emitter.ts +94 -0
  330. package/src/entity.ts +624 -0
  331. package/src/errorReport.ts +41 -0
  332. package/src/fields.ts +465 -0
  333. package/src/i18n.ts +26 -0
  334. package/src/index.ts +54 -0
  335. package/src/linkref.ts +31 -0
  336. package/src/links.ts +179 -0
  337. package/src/locales.ts +39 -0
  338. package/src/media.ts +48 -0
  339. package/src/migrate.ts +236 -0
  340. package/src/openapi.ts +377 -0
  341. package/src/paths.ts +88 -0
  342. package/src/plugin.ts +505 -0
  343. package/src/presets.ts +230 -0
  344. package/src/richtext.ts +88 -0
  345. package/src/sdk.ts +181 -0
  346. package/src/search.ts +312 -0
  347. package/src/seed.ts +225 -0
  348. package/src/setupCode.ts +30 -0
  349. package/src/system.ts +219 -0
  350. package/src/tree.ts +421 -0
  351. package/src/upgrades.ts +137 -0
  352. package/starter/.env.example +10 -0
  353. package/starter/README.md +65 -0
  354. package/starter/app/app.config.ts +10 -0
  355. package/starter/app/app.vue +12 -0
  356. package/starter/app/assets/css/main.css +15 -0
  357. package/starter/app/components/global/BlockContact.vue +15 -0
  358. package/starter/app/components/global/BlockCta.vue +15 -0
  359. package/starter/app/components/global/BlockHero.vue +23 -0
  360. package/starter/app/components/global/BlockImage.vue +13 -0
  361. package/starter/app/components/global/BlockText.vue +12 -0
  362. package/starter/app/composables/useSite.ts +77 -0
  363. package/starter/app/error.vue +28 -0
  364. package/starter/app/layouts/default.vue +106 -0
  365. package/starter/app/lib/cms-content.ts +152 -0
  366. package/starter/app/pages/[...slug].vue +66 -0
  367. package/starter/app/pages/hledat.vue +59 -0
  368. package/starter/app/pages/index.vue +20 -0
  369. package/starter/cms.config.ts +137 -0
  370. package/starter/compose.yaml +15 -0
  371. package/starter/drizzle.config.ts +8 -0
  372. package/starter/gitignore.txt +8 -0
  373. package/starter/nuxt.config.ts +13 -0
  374. package/starter/package.json +38 -0
  375. package/starter/scripts/typecheck.mjs +38 -0
  376. package/starter/seed/index.ts +297 -0
  377. package/starter/seed/media/about.jpg +0 -0
  378. package/starter/seed/media/hero.jpg +0 -0
  379. package/starter/seed/media/logo.png +0 -0
  380. package/starter/seed/media/news-bread.jpg +0 -0
  381. package/starter/seed/media/news-morning.jpg +0 -0
  382. package/starter/server/db/migrations/0000_init.sql +269 -0
  383. package/starter/server/db/migrations/0001_page_tree.sql +5 -0
  384. package/starter/server/db/migrations/0002_opening_hours_per_language.sql +1 -0
  385. package/starter/server/db/migrations/meta/0000_snapshot.json +1890 -0
  386. package/starter/server/db/migrations/meta/0001_snapshot.json +1887 -0
  387. package/starter/server/db/migrations/meta/0002_snapshot.json +1893 -0
  388. package/starter/server/db/migrations/meta/_journal.json +27 -0
  389. package/starter/server/db/schema.ts +253 -0
  390. package/starter/server/plugins/sitemap.ts +11 -0
  391. package/starter/server/routes/robots.txt.get.ts +12 -0
  392. package/starter/tsconfig.json +9 -0
package/cli/backup.ts ADDED
@@ -0,0 +1,713 @@
1
+ /**
2
+ * `tinyzoo backup [file]` and `tinyzoo restore <file>`.
3
+ *
4
+ * One archive holds everything a site needs to come back:
5
+ *
6
+ * tinyzoo-backup-<database>-<time>.tar
7
+ * manifest.json what is inside: tinyzoo version, time, the media list
8
+ * database.dump `pg_dump --format=custom` of DATABASE_URL
9
+ * media/<key> every file `cms_media` points at (the local upload folder or the S3 bucket)
10
+ *
11
+ * A plain tar (the system's `tar`), so it can be listed and unpacked without tinyzoo.
12
+ * It holds password hashes, form messages and every upload: keep it as private as the
13
+ * database (the file is created readable by its owner only, before the first byte).
14
+ *
15
+ * Restore puts the archive into a CLEAN database, all or nothing: `pg_restore -f -` turns
16
+ * the dump into a SQL stream, a filter leaves out the session settings the target server
17
+ * does not know (a dump made by pg_dump 17+ starts with `SET transaction_timeout`, which a
18
+ * 16 server rejects), and `psql` runs it inside ONE transaction that first drops and
19
+ * recreates the `public` and `drizzle` schemas. A restore that fails for any reason
20
+ * rolls back and leaves the database as it was; it is never left empty or half old, half
21
+ * new. Restoring over a database that has tables needs `--force`.
22
+ *
23
+ * Tool versions: `pg_dump` must be the server's major version or newer; `pg_restore` must
24
+ * be at least as new as the pg_dump that made the dump (checked before anything is
25
+ * touched); `psql` can be any current version. Set TINYZOO_PG_DUMP / TINYZOO_PG_RESTORE /
26
+ * TINYZOO_PSQL to use a particular binary or a wrapper script (e.g. one that runs them
27
+ * inside a Docker container).
28
+ *
29
+ * Settings are the ones the running app uses: NUXT_CMS_DATABASE_URL / DATABASE_URL,
30
+ * NUXT_CMS_STORAGE_* (from the environment, `.env` in the app folder, then the
31
+ * `cms:` options of nuxt.config), so run it from the site's folder.
32
+ */
33
+ import { docsUrl } from '../src/docs'
34
+ import { spawn, spawnSync } from 'node:child_process'
35
+ import { chmodSync, closeSync, copyFileSync, createWriteStream, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
36
+ import { tmpdir } from 'node:os'
37
+ import { dirname, join, resolve, sep } from 'node:path'
38
+ import { createInterface } from 'node:readline/promises'
39
+ import { Readable, Transform } from 'node:stream'
40
+ import { pipeline } from 'node:stream/promises'
41
+ import { fileURLToPath } from 'node:url'
42
+ import pg from 'pg'
43
+ import { extractTar } from './tar'
44
+
45
+ // ── settings ───────────────────────────────────────────────────────────────────
46
+
47
+ export interface S3Settings { endpoint: string; region: string; bucket: string; accessKeyId: string; secretAccessKey: string; publicBaseUrl?: string; delivery?: string }
48
+ export interface StorageSettings {
49
+ driver: 'local' | 's3' | 'r2'
50
+ localDir: string
51
+ /** Where the local folder is served from (`/api/cms/_media/raw`). */
52
+ localPublicBase?: string
53
+ s3: S3Settings
54
+ }
55
+ export interface BackupSettings { databaseUrl: string; storage: StorageSettings }
56
+
57
+ /** The database and media settings the app itself would use, read from the site's folder. */
58
+ export async function loadSettings(cwd: string, env: NodeJS.ProcessEnv = process.env): Promise<BackupSettings> {
59
+ try { (process as any).loadEnvFile?.(join(cwd, '.env')) }
60
+ catch { /* no .env */ }
61
+ let cms: any = {}
62
+ try {
63
+ const { loadNuxtConfig } = await import('@nuxt/kit')
64
+ cms = ((await loadNuxtConfig({ cwd })) as any)?.cms ?? {}
65
+ }
66
+ catch { /* no nuxt config readable: environment only */ }
67
+ return settingsFrom(cwd, env, cms)
68
+ }
69
+
70
+ /** Environment first, then the module options, then the defaults (same precedence as the server). */
71
+ export function settingsFrom(cwd: string, env: NodeJS.ProcessEnv, cms: any): BackupSettings {
72
+ const s = cms?.storage ?? {}
73
+ const driver = (env.NUXT_CMS_STORAGE_DRIVER || s.driver || 'local') as StorageSettings['driver']
74
+ const s3 = s.s3 ?? {}
75
+ return {
76
+ databaseUrl: env.NUXT_CMS_DATABASE_URL || env.DATABASE_URL || cms?.databaseUrl || '',
77
+ storage: {
78
+ driver,
79
+ localDir: resolve(cwd, env.NUXT_CMS_STORAGE_LOCAL_DIR || s.local?.dir || '.cms-uploads'),
80
+ localPublicBase: env.NUXT_CMS_STORAGE_LOCAL_PUBLIC_BASE || s.local?.publicBase || '/api/cms/_media/raw',
81
+ s3: {
82
+ endpoint: env.NUXT_CMS_STORAGE_S3_ENDPOINT || s3.endpoint || '',
83
+ region: env.NUXT_CMS_STORAGE_S3_REGION || s3.region || (driver === 'r2' ? 'auto' : 'us-east-1'),
84
+ bucket: env.NUXT_CMS_STORAGE_S3_BUCKET || s3.bucket || '',
85
+ accessKeyId: env.NUXT_CMS_STORAGE_S3_ACCESS_KEY_ID || s3.accessKeyId || '',
86
+ secretAccessKey: env.NUXT_CMS_STORAGE_S3_SECRET_ACCESS_KEY || s3.secretAccessKey || '',
87
+ publicBaseUrl: env.NUXT_CMS_STORAGE_S3_PUBLIC_BASE_URL || s3.publicBaseUrl || '',
88
+ delivery: env.NUXT_CMS_STORAGE_S3_DELIVERY || s3.delivery || '',
89
+ },
90
+ },
91
+ }
92
+ }
93
+
94
+ class CliError extends Error {}
95
+ const fail = (message: string): never => { throw new CliError(message) }
96
+
97
+ // ── Postgres tools ─────────────────────────────────────────────────────────────
98
+
99
+ /**
100
+ * How a connection string's TLS settings become libpq's — the same protection the app
101
+ * gets from node-postgres (pg-connection-string 2.x, which `new pg.Client` uses):
102
+ *
103
+ * sslmode=disable no TLS
104
+ * sslmode=prefer|require|verify-ca|verify-full,
105
+ * ssl=true|1, or any of sslrootcert / sslcert / sslkey
106
+ * TLS, certificate AND host name verified
107
+ * → libpq `verify-full` (libpq's own `require`
108
+ * would accept any certificate)
109
+ * sslmode=no-verify TLS, certificate not checked → `require`
110
+ * uselibpqcompat=true libpq's own meanings, passed through
111
+ *
112
+ * `sslrootcert`, `sslcert` and `sslkey` are passed on as PGSSLROOTCERT / PGSSLCERT /
113
+ * PGSSLKEY, and PGSSL* variables of the shell stay (node-postgres reads PGSSLMODE too).
114
+ * Nothing in the URL means whatever the environment says, as for the app.
115
+ */
116
+ export function sslEnv(u: URL): Record<string, string> {
117
+ const q = (k: string) => u.searchParams.get(k)
118
+ const out: Record<string, string> = {}
119
+ const mode = q('sslmode')
120
+ const flag = q('ssl')
121
+ const files = { sslrootcert: 'PGSSLROOTCERT', sslcert: 'PGSSLCERT', sslkey: 'PGSSLKEY' } as const
122
+ for (const [param, name] of Object.entries(files)) if (q(param)) out[name] = q(param)!
123
+ const hasFiles = Object.keys(out).length > 0
124
+ if (q('uselibpqcompat') === 'true') {
125
+ if (mode) out.PGSSLMODE = mode === 'no-verify' ? 'require' : mode
126
+ else if (flag === 'true' || flag === '1' || hasFiles) out.PGSSLMODE = 'verify-full'
127
+ return out
128
+ }
129
+ if (mode === 'disable' || (!mode && flag === '0')) out.PGSSLMODE = 'disable'
130
+ else if (mode === 'no-verify') out.PGSSLMODE = 'require'
131
+ else if (mode || flag === 'true' || flag === '1' || hasFiles) out.PGSSLMODE = 'verify-full'
132
+ return out
133
+ }
134
+
135
+ /** libpq environment for a connection string: the password never appears in the process list. */
136
+ export function pgEnv(databaseUrl: string, inherited: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
137
+ let u: URL
138
+ try { u = new URL(databaseUrl) }
139
+ catch { return fail('DATABASE_URL is not a valid connection string (postgres://user:password@host:5432/database).') }
140
+ const env: NodeJS.ProcessEnv = { ...inherited }
141
+ // A stray PG* variable of the shell must not send the dump to another database
142
+ // (but its TLS settings, PGSSL*, keep protecting the connection).
143
+ for (const k of Object.keys(env)) if (k.startsWith('PG') && !k.startsWith('PGSSL')) delete env[k]
144
+ env.PGHOST = decodeURIComponent(u.hostname) || 'localhost'
145
+ if (u.port) env.PGPORT = u.port
146
+ if (u.username) env.PGUSER = decodeURIComponent(u.username)
147
+ if (u.password) env.PGPASSWORD = decodeURIComponent(u.password)
148
+ env.PGDATABASE = decodeURIComponent(u.pathname.replace(/^\//, ''))
149
+ Object.assign(env, sslEnv(u))
150
+ return env
151
+ }
152
+
153
+ const databaseName = (databaseUrl: string) => decodeURIComponent(new URL(databaseUrl).pathname.replace(/^\//, ''))
154
+ const targetLabel = (databaseUrl: string) => { const u = new URL(databaseUrl); return `${databaseName(databaseUrl)} on ${u.hostname}${u.port ? ':' + u.port : ''}` }
155
+
156
+ type PgTool = 'pg_dump' | 'pg_restore' | 'psql'
157
+ const TOOL_ENV: Record<PgTool, string> = { pg_dump: 'TINYZOO_PG_DUMP', pg_restore: 'TINYZOO_PG_RESTORE', psql: 'TINYZOO_PSQL' }
158
+ const TOOL_VERSION_RULE: Record<PgTool, string> = {
159
+ pg_dump: 'the same major version as the database server, or newer',
160
+ pg_restore: 'at least as new as the pg_dump that made the backup',
161
+ psql: 'any current version',
162
+ }
163
+
164
+ function toolCommand(tool: PgTool): string {
165
+ return process.env[TOOL_ENV[tool]] || tool
166
+ }
167
+
168
+ const missingToolHelp = (tool: PgTool, databaseUrl: string) => `${tool} was not found.
169
+ Install the PostgreSQL client tools (${tool}: ${TOOL_VERSION_RULE[tool]}):
170
+ macOS: brew install libpq && brew link --force libpq
171
+ Debian: sudo apt install postgresql-client
172
+ or point ${TOOL_ENV[tool]} at a ${tool} binary or wrapper script.
173
+ No client tools at all? ${tool === 'pg_dump'
174
+ ? `Take the database dump inside the database's own container:
175
+ docker exec <container> pg_dump --format=custom -U <user> ${databaseName(databaseUrl) || '<database>'} > database.dump
176
+ and keep it with a copy of the media`
177
+ : `Restore the dump inside the database's own container (empty the schemas first):
178
+ docker exec -i <container> pg_restore --single-transaction --exit-on-error --no-owner --no-acl -U <user> -d ${databaseName(databaseUrl) || '<database>'} < database.dump
179
+ and copy the media back`} (${docsUrl('backups')}).`
180
+
181
+ const MAX_CAPTURE = 64 * 1024
182
+ /** Collect a stream as text, keeping the first part of a very long one. */
183
+ const collect = (stream: NodeJS.ReadableStream | null): (() => string) => {
184
+ let text = ''
185
+ stream?.on('data', (d) => { if (text.length < MAX_CAPTURE) text += String(d) })
186
+ return () => text
187
+ }
188
+
189
+ /** How a tool that is missing or cannot start is reported. */
190
+ const spawnFailure = (tool: PgTool, err: NodeJS.ErrnoException, databaseUrl: string) =>
191
+ new CliError(err.code === 'ENOENT' ? missingToolHelp(tool, databaseUrl) : `${tool} could not start: ${err.message}`)
192
+
193
+ /**
194
+ * Run a Postgres tool, with stdin/stdout wired to files (or stdout captured as text).
195
+ * Resolves with what it printed; rejects when it is missing or exits non-zero.
196
+ */
197
+ function runTool(tool: PgTool, args: string[], env: NodeJS.ProcessEnv, io: { stdin?: string; stdout?: string; capture?: boolean }, databaseUrl: string): Promise<{ stdout: string; stderr: string }> {
198
+ const cmd = toolCommand(tool)
199
+ const inFd = io.stdin ? openSync(io.stdin, 'r') : null
200
+ const outFd = io.stdout ? openSync(io.stdout, 'w', 0o600) : null
201
+ return new Promise((resolvePromise, reject) => {
202
+ const child = spawn(cmd, args, { env, stdio: [inFd ?? 'ignore', outFd ?? (io.capture ? 'pipe' : 'ignore'), 'pipe'] })
203
+ const stdout = collect(child.stdout)
204
+ const stderr = collect(child.stderr)
205
+ let closed = false // an 'error' (tool missing) is followed by 'close': release the files once
206
+ const close = () => {
207
+ if (closed) return
208
+ closed = true
209
+ if (inFd !== null) closeSync(inFd)
210
+ if (outFd !== null) closeSync(outFd)
211
+ }
212
+ child.on('error', (err: NodeJS.ErrnoException) => {
213
+ close()
214
+ reject(spawnFailure(tool, err, databaseUrl))
215
+ })
216
+ child.on('close', (code) => {
217
+ close()
218
+ if (code === 0) return resolvePromise({ stdout: stdout(), stderr: stderr() })
219
+ const mismatch = /version mismatch|server version/i.test(stderr())
220
+ ? `\n The ${tool} on this machine is older than the database server: install a newer PostgreSQL client.` : ''
221
+ reject(new CliError(`${tool} failed (exit ${code}):\n${stderr().trim()}${mismatch}`))
222
+ })
223
+ })
224
+ }
225
+
226
+ // ── versions and the dump's preamble ───────────────────────────────────────────
227
+
228
+ /** `pg_restore (PostgreSQL) 17.9 (Debian 17.9-1)` → 17 (also `18beta1`, `19devel`). */
229
+ export function toolMajor(versionOutput: string): number | null {
230
+ const m = /\(PostgreSQL\)\s+(\d+)/.exec(versionOutput)
231
+ return m ? Number(m[1]) : null
232
+ }
233
+
234
+ /** The major version of the pg_dump that made a dump, from `pg_restore --list` (`; Dumped by pg_dump version: 17.9`). */
235
+ export function dumpedByMajor(listing: string): number | null {
236
+ const m = /^;\s*Dumped by pg_dump version:\s*(\d+)/m.exec(listing)
237
+ return m ? Number(m[1]) : null
238
+ }
239
+
240
+ /**
241
+ * Leaves out of the dump's opening lines the `SET name = …;` of a session setting the target
242
+ * server does not have. pg_dump 17 began its script with `SET transaction_timeout = 0;`,
243
+ * which a 16 server answers with "unrecognized configuration parameter": the settings are
244
+ * housekeeping (timeouts off, encoding, search path), so a server that lacks one loses
245
+ * nothing. `known` says whether the server has a setting (from its `pg_settings`).
246
+ *
247
+ * Only the preamble is touched: from the first line that is not blank, a comment, a psql
248
+ * `\restrict`, a `SET` or the `set_config` call, everything is passed on byte for byte, so
249
+ * the table data after it can never be altered, and nothing is buffered beyond one line.
250
+ * Settings with a dot (`app.setting`) are the extension's and always kept.
251
+ */
252
+ export function preambleFilter(known: (setting: string) => boolean): Transform & { dropped: string[] } {
253
+ let preamble = true
254
+ let pending: Buffer = Buffer.alloc(0)
255
+ const dropped: string[] = []
256
+ const isPreamble = (line: string) => /^(\s*|--.*|\\restrict\s.*|SET\s.*|SELECT pg_catalog\.set_config\(.*)$/i.test(line)
257
+ const settingOf = (line: string) => /^SET\s+(?:SESSION\s+|LOCAL\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*(?:=|TO)\s/i.exec(line)?.[1]?.toLowerCase()
258
+ const filter = new Transform({
259
+ transform(chunk: Buffer, _enc, done) {
260
+ if (!preamble) return done(null, chunk)
261
+ pending = Buffer.concat([pending, chunk])
262
+ let nl: number
263
+ while (preamble && (nl = pending.indexOf(0x0a)) >= 0) {
264
+ const line = pending.subarray(0, nl + 1)
265
+ pending = pending.subarray(nl + 1)
266
+ const text = line.toString('latin1').replace(/\r?\n$/, '')
267
+ if (!isPreamble(text)) { preamble = false; this.push(line); break }
268
+ const name = settingOf(text)
269
+ if (name && !known(name)) dropped.push(name)
270
+ else this.push(line)
271
+ }
272
+ if (!preamble) { this.push(pending); pending = Buffer.alloc(0) }
273
+ done()
274
+ },
275
+ flush(done) { if (pending.length) this.push(pending); done() },
276
+ })
277
+ return Object.assign(filter, { dropped })
278
+ }
279
+
280
+ /** What the target server is: its major version and the settings it knows. */
281
+ async function serverInfo(databaseUrl: string): Promise<{ major: number; settings: Set<string> }> {
282
+ return withClient(databaseUrl, async (c) => {
283
+ const num = Number((await c.query('show server_version_num')).rows[0]?.server_version_num)
284
+ const settings = new Set((await c.query('select name from pg_settings')).rows.map((r) => String(r.name).toLowerCase()))
285
+ return { major: Math.floor(num / 10000), settings }
286
+ })
287
+ }
288
+
289
+ /**
290
+ * Empty the database of tinyzoo's schemas, then the restore, in ONE transaction: `drizzle`
291
+ * (the migration journal) and `public` go, `public` is created again (pg_dump never writes
292
+ * the CREATE of `public`, so the restore needs it to be there). Extensions that live in
293
+ * `public` (pg_trgm) go with it and come back with the dump, which carries `CREATE
294
+ * EXTENSION`. Dropping everything — and not just what the dump contains — is what makes a
295
+ * restore over a newer database come out exactly as the backup was, instead of leaving the
296
+ * newer tables and journal beside the old ones. The dump's own `SET lock_timeout = 0`
297
+ * follows the drops, so the 15 s limit applies to waiting for the lock only.
298
+ */
299
+ const RESET_SCHEMAS_SQL = `BEGIN;
300
+ SET LOCAL lock_timeout = '15s';
301
+ SET LOCAL client_min_messages = warning;
302
+ DROP SCHEMA IF EXISTS drizzle CASCADE;
303
+ DROP SCHEMA IF EXISTS public CASCADE;
304
+ CREATE SCHEMA public;
305
+ GRANT USAGE ON SCHEMA public TO PUBLIC;
306
+ `
307
+
308
+ /**
309
+ * `pg_restore -f -` → preamble filter → `psql`, with the reset and the dump in one
310
+ * transaction. Streamed: a large dump is never held in memory. The transaction is opened
311
+ * and committed by us (not `psql --single-transaction`): psql commits at the end of its
312
+ * input, so a pg_restore that died half way would have a truncated script COMMITTED. Here
313
+ * `COMMIT` is only written once pg_restore has exited 0; any other end of the input, or an
314
+ * error in psql (ON_ERROR_STOP), leaves the transaction to roll back.
315
+ */
316
+ function streamRestore(dump: string, databaseUrl: string, known: Set<string>): Promise<{ dropped: string[]; notes: string }> {
317
+ const env = pgEnv(databaseUrl)
318
+ const filter = preambleFilter((name) => known.has(name))
319
+ const inFd = openSync(dump, 'r')
320
+ type Exit = { code: number | null; error?: NodeJS.ErrnoException }
321
+ return new Promise((resolvePromise, reject) => {
322
+ const rst = spawn(toolCommand('pg_restore'), ['-f', '-', '--no-owner', '--no-acl', '--no-comments'], { env, stdio: [inFd, 'pipe', 'pipe'] })
323
+ const sql = spawn(toolCommand('psql'), ['-X', '-q', '-v', 'ON_ERROR_STOP=1', '-f', '-'], { env, stdio: ['pipe', 'ignore', 'pipe'] })
324
+ closeSync(inFd) // the child has its own copy
325
+ const rstErr = collect(rst.stderr)
326
+ const sqlErr = collect(sql.stderr)
327
+ let rstRunning = true
328
+ let killedRst = false // stopped by us because psql went first: its failure is the one to report
329
+ const exit = (child: typeof rst) => new Promise<Exit>((r) => {
330
+ child.on('error', (error: NodeJS.ErrnoException) => r({ code: null, error }))
331
+ child.on('close', (code) => r({ code }))
332
+ })
333
+ const rstExit = exit(rst).then((x) => { rstRunning = false; return x })
334
+ const sqlExit = exit(sql)
335
+ // psql stopped early (an error): pg_restore must not wait for a reader that is gone.
336
+ void sqlExit.then((x) => { if (x.code !== 0 && rstRunning) { killedRst = true; rst.kill() } })
337
+ void rstExit.then((x) => { if (x.error) sql.kill() })
338
+
339
+ sql.stdin!.on('error', () => { /* EPIPE: psql is gone, its exit says why */ })
340
+ sql.stdin!.write(RESET_SCHEMAS_SQL)
341
+ rst.stdout!.pipe(filter).pipe(sql.stdin!, { end: false })
342
+ const inputDone = new Promise<void>((r) => filter.on('end', () => r()))
343
+ void Promise.all([inputDone, rstExit]).then(([, x]) => {
344
+ try { if (x.code === 0 && !x.error) sql.stdin!.end('COMMIT;\n'); else sql.stdin!.end() }
345
+ catch { /* psql already ended */ }
346
+ })
347
+
348
+ void Promise.all([rstExit, sqlExit]).then(([r, s]) => {
349
+ if (r.error) return reject(spawnFailure('pg_restore', r.error, databaseUrl))
350
+ if (s.error) return reject(spawnFailure('psql', s.error, databaseUrl))
351
+ if (r.code !== 0 && !killedRst) return reject(new CliError(`pg_restore failed (exit ${r.code}):\n${rstErr().trim()}`))
352
+ if (s.code !== 0) return reject(new CliError(`psql failed (exit ${s.code}):\n${sqlErr().trim()}`))
353
+ resolvePromise({ dropped: filter.dropped, notes: [rstErr(), sqlErr()].map((t) => t.trim()).filter(Boolean).join('\n') })
354
+ })
355
+ })
356
+ }
357
+
358
+ // ── media stores ───────────────────────────────────────────────────────────────
359
+
360
+ export interface MediaStore {
361
+ read(key: string): Promise<{ body: Buffer; contentType: string } | null>
362
+ /** Copy a file out of the store to `dest` as a stream (a video is never held in memory). Null when it is not there. */
363
+ readToFile(key: string, dest: string): Promise<{ size: number } | null>
364
+ write(key: string, body: Buffer, contentType: string): Promise<void>
365
+ }
366
+
367
+ /** A storage key is `2026/11/<uuid>.png`: never a path that leaves the folder. */
368
+ export function safeKey(key: string): boolean {
369
+ return !!key && key.length <= 500 && !key.startsWith('/') && !key.includes('\\') && !key.includes('\0')
370
+ && key.split('/').every((part) => part !== '' && part !== '.' && part !== '..')
371
+ }
372
+
373
+ function insideDir(dir: string, key: string): string {
374
+ const abs = resolve(dir, key)
375
+ if (!safeKey(key) || (abs !== dir && !abs.startsWith(dir + sep))) fail(`Refusing the unsafe storage key "${key}".`)
376
+ return abs
377
+ }
378
+
379
+ export function localStore(dir: string): MediaStore {
380
+ return {
381
+ async read(key) {
382
+ const abs = insideDir(dir, key)
383
+ return existsSync(abs) && statSync(abs).isFile() ? { body: readFileSync(abs), contentType: '' } : null
384
+ },
385
+ async readToFile(key, dest) {
386
+ const abs = insideDir(dir, key)
387
+ if (!existsSync(abs) || !statSync(abs).isFile()) return null
388
+ mkdirSync(dirname(dest), { recursive: true })
389
+ copyFileSync(abs, dest)
390
+ return { size: statSync(dest).size }
391
+ },
392
+ async write(key, body) {
393
+ const abs = insideDir(dir, key)
394
+ mkdirSync(dirname(abs), { recursive: true })
395
+ writeFileSync(abs, body)
396
+ },
397
+ }
398
+ }
399
+
400
+ export function s3Store(cfg: S3Settings): MediaStore {
401
+ const missing = (['endpoint', 'bucket', 'accessKeyId', 'secretAccessKey'] as const).filter((k) => !cfg[k])
402
+ if (missing.length) fail(`The S3 storage is missing ${missing.join(', ')} (NUXT_CMS_STORAGE_S3_*).`)
403
+ let client: Promise<any> | undefined
404
+ const aws = () => client ??= import('aws4fetch').then(({ AwsClient }) => new AwsClient({ accessKeyId: cfg.accessKeyId, secretAccessKey: cfg.secretAccessKey, region: cfg.region, service: 's3' }))
405
+ // The same path-style address the app's own S3 driver uses.
406
+ const urlOf = (key: string) => `${cfg.endpoint.replace(/\/$/, '')}/${cfg.bucket}/${key.split('/').map(encodeURIComponent).join('/')}`
407
+ return {
408
+ async read(key) {
409
+ if (!safeKey(key)) fail(`Refusing the unsafe storage key "${key}".`)
410
+ const res = await (await aws()).fetch(urlOf(key))
411
+ if (res.status === 404) return null
412
+ if (!res.ok) fail(`S3 download of ${key} failed (${res.status} ${res.statusText}).`)
413
+ return { body: Buffer.from(await res.arrayBuffer()), contentType: res.headers.get('content-type') || '' }
414
+ },
415
+ async readToFile(key, dest) {
416
+ if (!safeKey(key)) fail(`Refusing the unsafe storage key "${key}".`)
417
+ const res = await (await aws()).fetch(urlOf(key))
418
+ if (res.status === 404) return null
419
+ if (!res.ok || !res.body) fail(`S3 download of ${key} failed (${res.status} ${res.statusText}).`)
420
+ mkdirSync(dirname(dest), { recursive: true })
421
+ let size = 0
422
+ const count = new Transform({ transform(chunk, _enc, cb) { size += chunk.length; cb(null, chunk) } })
423
+ try { await pipeline(Readable.fromWeb(res.body as any), count, createWriteStream(dest)) }
424
+ catch (err) { rmSync(dest, { force: true }); throw err }
425
+ return { size }
426
+ },
427
+ async write(key, body, contentType) {
428
+ if (!safeKey(key)) fail(`Refusing the unsafe storage key "${key}".`)
429
+ const res = await (await aws()).fetch(urlOf(key), { method: 'PUT', body: new Uint8Array(body), headers: { 'content-type': contentType || 'application/octet-stream' } })
430
+ if (!res.ok) fail(`S3 upload of ${key} failed (${res.status} ${res.statusText}).`)
431
+ },
432
+ }
433
+ }
434
+
435
+ export function mediaStore(storage: StorageSettings): MediaStore {
436
+ if (storage.driver === 'local') return localStore(storage.localDir)
437
+ if (storage.driver === 's3' || storage.driver === 'r2') return s3Store(storage.s3)
438
+ return fail(`Storage driver "${storage.driver}" is not supported (local, s3, r2).`)
439
+ }
440
+
441
+ /** Run `fn` over `items` with a few at a time. */
442
+ async function pool<T>(items: T[], size: number, fn: (item: T) => Promise<void>): Promise<void> {
443
+ let next = 0
444
+ await Promise.all(Array.from({ length: Math.min(size, items.length) }, async () => {
445
+ while (next < items.length) await fn(items[next++]!)
446
+ }))
447
+ }
448
+
449
+ // ── the archive ────────────────────────────────────────────────────────────────
450
+
451
+ export interface BackupManifest {
452
+ format: 'tinyzoo-backup'
453
+ version: 1
454
+ tinyzooVersion: string
455
+ createdAt: string
456
+ database: string
457
+ storageDriver: string
458
+ media: { key: string; mime: string; size: number }[]
459
+ /** Referenced by the database but not found in storage when the backup was taken. */
460
+ missingMedia: string[]
461
+ }
462
+
463
+ const packageVersion = (): string => {
464
+ try { return JSON.parse(readFileSync(join(dirname(fileURLToPath(import.meta.url)), '..', 'package.json'), 'utf8')).version }
465
+ catch { return 'unknown' }
466
+ }
467
+
468
+ function tar(args: string[]): string {
469
+ const res = spawnSync('tar', args, { encoding: 'utf8' })
470
+ if (res.error) return fail('tar was not found: backups are tar archives, install tar (it ships with macOS, Linux and Windows 10+).')
471
+ if (res.status !== 0) return fail(`tar failed: ${res.stderr.trim()}`)
472
+ return res.stdout
473
+ }
474
+
475
+ async function withClient<T>(databaseUrl: string, fn: (c: pg.Client) => Promise<T>): Promise<T> {
476
+ const client = new pg.Client({ connectionString: databaseUrl })
477
+ try { await client.connect() }
478
+ catch (err) { return fail(`Cannot connect to the database (${targetLabel(databaseUrl)}): ${(err as Error).message}`) }
479
+ try { return await fn(client) }
480
+ finally { await client.end().catch(() => {}) }
481
+ }
482
+
483
+ /**
484
+ * Copy the files the database refers to into `dir` (a few at a time). Returns what was
485
+ * copied and the keys that were not in the store.
486
+ */
487
+ export async function copyMedia(store: MediaStore, rows: { key: string; mime: string }[], dir: string): Promise<{ media: BackupManifest['media']; missing: string[] }> {
488
+ const media: BackupManifest['media'] = []
489
+ const missing: string[] = []
490
+ await pool(rows, 4, async (row) => {
491
+ if (!safeKey(row.key)) { missing.push(row.key); return }
492
+ const copied = await store.readToFile(row.key, insideDir(dir, row.key))
493
+ if (!copied) { missing.push(row.key); return }
494
+ media.push({ key: row.key, mime: row.mime, size: copied.size })
495
+ })
496
+ return { media, missing }
497
+ }
498
+
499
+ export interface RunIo { log: (line: string) => void }
500
+
501
+ /** Make the archive. Returns the manifest. */
502
+ export async function runBackup(settings: BackupSettings, outFile: string, io: RunIo = { log: console.log }): Promise<BackupManifest> {
503
+ if (!settings.databaseUrl) fail('No database: set DATABASE_URL (or NUXT_CMS_DATABASE_URL), here or in .env.')
504
+ if (existsSync(outFile)) fail(`${outFile} already exists; choose another name.`)
505
+ const env = pgEnv(settings.databaseUrl)
506
+ const store = mediaStore(settings.storage)
507
+ const staging = mkdtempSync(join(tmpdir(), 'tinyzoo-backup-'))
508
+ // Created readable by its owner only BEFORE anything is written to it (tar keeps the
509
+ // mode of a file it overwrites); `wx` also refuses to follow a link someone left there.
510
+ mkdirSync(dirname(resolve(outFile)), { recursive: true })
511
+ try { closeSync(openSync(outFile, 'wx', 0o600)) }
512
+ catch (err) { rmSync(staging, { recursive: true, force: true }); return fail((err as NodeJS.ErrnoException).code === 'EEXIST' ? `${outFile} already exists; choose another name.` : `Cannot create ${outFile}: ${(err as Error).message}`) }
513
+ try {
514
+ io.log(`Database ${targetLabel(settings.databaseUrl)}: dumping…`)
515
+ await runTool('pg_dump', ['--format=custom'], env, { stdout: join(staging, 'database.dump') }, settings.databaseUrl)
516
+
517
+ const rows = await withClient(settings.databaseUrl, async (c) => {
518
+ const exists = (await c.query(`select to_regclass('public.cms_media') as t`)).rows[0]?.t
519
+ return exists ? (await c.query('select key, mime, size from cms_media order by created_at')).rows as { key: string; mime: string; size: number }[] : []
520
+ })
521
+ const manifest: BackupManifest = {
522
+ format: 'tinyzoo-backup', version: 1, tinyzooVersion: packageVersion(), createdAt: new Date().toISOString(),
523
+ database: databaseName(settings.databaseUrl), storageDriver: settings.storage.driver, media: [], missingMedia: [],
524
+ }
525
+ io.log(`Media (${settings.storage.driver}): ${rows.length} file(s)…`)
526
+ const copied = await copyMedia(store, rows, join(staging, 'media'))
527
+ manifest.media.push(...copied.media)
528
+ manifest.missingMedia.push(...copied.missing)
529
+ manifest.media.sort((a, b) => a.key.localeCompare(b.key))
530
+ manifest.missingMedia.sort()
531
+ writeFileSync(join(staging, 'manifest.json'), JSON.stringify(manifest, null, 2) + '\n')
532
+
533
+ tar(['-cf', resolve(outFile), '-C', staging, 'manifest.json', 'database.dump', ...(manifest.media.length ? ['media'] : [])])
534
+ chmodSync(outFile, 0o600)
535
+ const mb = (statSync(outFile).size / 1024 / 1024).toFixed(1)
536
+ io.log(`✓ ${outFile} (${mb} MB): database and ${manifest.media.length} media file(s).`)
537
+ if (manifest.missingMedia.length) io.log(`! ${manifest.missingMedia.length} file(s) the database refers to are missing from storage: ${manifest.missingMedia.slice(0, 5).join(', ')}${manifest.missingMedia.length > 5 ? ', …' : ''}`)
538
+ return manifest
539
+ }
540
+ catch (err) {
541
+ rmSync(outFile, { force: true }) // never leave a half-written archive that looks like a backup
542
+ throw err
543
+ }
544
+ finally {
545
+ rmSync(staging, { recursive: true, force: true })
546
+ }
547
+ }
548
+
549
+ export interface RestoreOptions {
550
+ /** Skip the question (scripts). */
551
+ yes?: boolean
552
+ /** Restore over a database that already has tables (everything in it is replaced). */
553
+ force?: boolean
554
+ /** Ask a yes/no question; the default reads the terminal. */
555
+ confirm?: (question: string, expected: string) => Promise<boolean>
556
+ }
557
+
558
+ async function askInTerminal(question: string, expected: string): Promise<boolean> {
559
+ if (!process.stdin.isTTY) return fail('This asks for confirmation and there is no terminal: add --yes to go ahead without asking.')
560
+ const rl = createInterface({ input: process.stdin, output: process.stdout })
561
+ try { return (await rl.question(`${question}\nType ${expected} to continue: `)).trim() === expected }
562
+ finally { rl.close() }
563
+ }
564
+
565
+ /** What the target database already holds: tables, applied migrations, accounts. */
566
+ export interface TargetState { tables: number; migrations: number; accounts: number }
567
+
568
+ export async function inspectTarget(databaseUrl: string): Promise<TargetState> {
569
+ return withClient(databaseUrl, async (c) => {
570
+ const one = async (text: string) => Number((await c.query(text)).rows[0]?.n ?? 0)
571
+ // Any table, view or materialized view in `public` (the extension's own functions do not count).
572
+ const tables = await one(`select count(*)::int as n from pg_class c join pg_namespace n on n.oid = c.relnamespace where n.nspname = 'public' and c.relkind in ('r', 'p', 'v', 'm', 'f')`)
573
+ const migrations = (await c.query(`select to_regclass('drizzle.__drizzle_migrations') as t`)).rows[0]?.t
574
+ ? await one('select count(*)::int as n from drizzle.__drizzle_migrations') : 0
575
+ const accounts = (await c.query(`select to_regclass('public.cms_users') as t`)).rows[0]?.t
576
+ ? await one('select count(*)::int as n from public.cms_users') : 0
577
+ return { tables, migrations, accounts }
578
+ })
579
+ }
580
+
581
+ /**
582
+ * Everything about the tools that can be known without touching the database: psql is
583
+ * there, pg_restore is there and can read the dump, and is not older than the pg_dump that
584
+ * made it. Returns the major version of that pg_dump (null if the dump does not say).
585
+ */
586
+ async function checkRestoreTools(dump: string, databaseUrl: string): Promise<number | null> {
587
+ const env = pgEnv(databaseUrl)
588
+ await runTool('psql', ['--version'], env, { capture: true }, databaseUrl)
589
+ const restoreMajor = toolMajor((await runTool('pg_restore', ['--version'], env, { capture: true }, databaseUrl)).stdout)
590
+ let listing: string
591
+ try { listing = (await runTool('pg_restore', ['--list'], env, { stdin: dump, capture: true }, databaseUrl)).stdout }
592
+ catch (err) {
593
+ if (!(err instanceof CliError) || /was not found|could not start/.test(err.message)) throw err
594
+ // An older pg_restore does not understand a newer archive format at all.
595
+ const newer = /unsupported version \(([\d.]+)\) in file header/.exec(err.message)
596
+ if (newer) return fail(`database.dump in the archive was made by a newer pg_dump (archive format ${newer[1]}) than this pg_restore${restoreMajor ? ` (${restoreMajor})` : ''}, which cannot read it; nothing was changed.\n Use a pg_restore of the same version as the pg_dump that made the backup, or newer: ${newerToolsHelp}`)
597
+ return fail(`database.dump in the archive is not a readable pg_dump archive; nothing was changed.\n${err.message}`)
598
+ }
599
+ const dumpMajor = dumpedByMajor(listing)
600
+ if (dumpMajor && restoreMajor && dumpMajor > restoreMajor) {
601
+ fail(`database.dump in the archive was made by pg_dump ${dumpMajor}, newer than this pg_restore (${restoreMajor}), which cannot restore it; nothing was changed.\n Use a pg_restore of version ${dumpMajor} or newer: ${newerToolsHelp}`)
602
+ }
603
+ return dumpMajor
604
+ }
605
+
606
+ const newerToolsHelp = `install newer PostgreSQL client tools, or point ${TOOL_ENV.pg_restore} (and ${TOOL_ENV.psql}) at them.`
607
+
608
+ /** A failed restore: what went wrong, and that the database is exactly as it was. */
609
+ function restoreFailure(err: Error, dumpMajor: number | null, serverMajor: number, target: string): CliError {
610
+ const hints: string[] = []
611
+ if (/lock timeout/i.test(err.message)) hints.push('Something is still using the database: stop the site first.')
612
+ if (/invalid command \\(un)?restrict/i.test(err.message)) hints.push(`This psql is older than the pg_restore that wrote the script (it does not know \\restrict, added in the August 2025 security releases): use the psql that comes with the pg_restore, or set ${TOOL_ENV.psql}.`)
613
+ if (dumpMajor && dumpMajor > serverMajor && err.message.startsWith('psql failed')) hints.push(`The dump was made by pg_dump ${dumpMajor} but the server is PostgreSQL ${serverMajor}; if the cause above is syntax this server lacks, take the backup with a pg_dump of version ${serverMajor}.`)
614
+ return new CliError(`${err.message}${hints.map((h) => `\n ${h}`).join('')}\n Nothing was changed: the old schemas are dropped and the backup loaded in ONE transaction, which was rolled back, so ${target} is exactly as it was. Fix the cause above and run the restore again; the archive is unchanged.`)
615
+ }
616
+
617
+ /** Put an archive back: the database (replaced as a whole), then the media. */
618
+ export async function runRestore(settings: BackupSettings, file: string, opts: RestoreOptions = {}, io: RunIo = { log: console.log }): Promise<BackupManifest> {
619
+ if (!settings.databaseUrl) fail('No database: set DATABASE_URL (or NUXT_CMS_DATABASE_URL), here or in .env.')
620
+ if (!existsSync(file)) fail(`${file} does not exist.`)
621
+ const staging = mkdtempSync(join(tmpdir(), 'tinyzoo-restore-'))
622
+ try {
623
+ // tinyzoo's own reader: plain files only. A link in the archive is refused (a link
624
+ // to ~/.ssh/id_rsa would otherwise be copied into the public media folder), sizes
625
+ // are checked against a cap before anything is written.
626
+ try { await extractTar(resolve(file), staging, { strict: true }) }
627
+ catch (err) { return fail(`${file} cannot be read as a tinyzoo backup: ${(err as Error).message}`) }
628
+ if (!existsSync(join(staging, 'manifest.json')) || !existsSync(join(staging, 'database.dump'))) fail(`${file} is not a tinyzoo backup (no manifest.json and database.dump).`)
629
+ let manifest: BackupManifest
630
+ try { manifest = JSON.parse(readFileSync(join(staging, 'manifest.json'), 'utf8')) }
631
+ catch { return fail('manifest.json in the archive is unreadable.') }
632
+ if (manifest.format !== 'tinyzoo-backup' || manifest.version !== 1) fail('This archive was made by a different tinyzoo backup format.')
633
+ // Everything the archive claims to hold is checked before anything is changed.
634
+ const mediaDir = join(staging, 'media')
635
+ for (const m of manifest.media ?? []) {
636
+ if (!safeKey(m.key)) fail(`The archive lists the unsafe storage key "${m.key}"; nothing was changed.`)
637
+ const abs = insideDir(mediaDir, m.key)
638
+ if (!existsSync(abs)) fail(`The archive lists ${m.key} but does not contain it; nothing was changed.`)
639
+ if (!lstatSync(abs).isFile()) fail(`${m.key} in the archive is not a plain file; nothing was changed.`)
640
+ }
641
+ // The tools and the dump are checked before the database is touched: a restore that
642
+ // cannot work must be found out now, not after the old data is gone.
643
+ const dumpMajor = await checkRestoreTools(join(staging, 'database.dump'), settings.databaseUrl)
644
+
645
+ const server = await serverInfo(settings.databaseUrl)
646
+ const state = await inspectTarget(settings.databaseUrl)
647
+ if ((state.tables > 0 || state.migrations > 0) && !opts.force) {
648
+ const parts = [`${state.tables} table(s)`, state.migrations ? `${state.migrations} applied migration(s)` : '', state.accounts ? `${state.accounts} account(s)` : ''].filter(Boolean)
649
+ fail(`${targetLabel(settings.databaseUrl)} is not empty (${parts.join(', ')}). A restore REPLACES everything in it: restore into an empty database, or add --force if that is what you want.`)
650
+ }
651
+ io.log(`Backup of ${manifest.database} from ${manifest.createdAt} (tinyzoo ${manifest.tinyzooVersion}): database and ${manifest.media.length} media file(s).`)
652
+ if (!opts.yes) {
653
+ const ok = await (opts.confirm ?? askInTerminal)(`This REPLACES the data in ${targetLabel(settings.databaseUrl)}: its public and drizzle schemas are dropped and recreated from the backup (and the media are written into ${settings.storage.driver === 'local' ? settings.storage.localDir : 'the bucket ' + settings.storage.s3.bucket}).`, databaseName(settings.databaseUrl))
654
+ if (!ok) fail('Not confirmed; nothing was changed.')
655
+ }
656
+
657
+ // Open the media first (a bad bucket config must not come after the database is replaced).
658
+ const store = mediaStore(settings.storage)
659
+ if (dumpMajor && dumpMajor > server.major) io.log(`The dump was made by pg_dump ${dumpMajor}, the server is PostgreSQL ${server.major}: settings the server does not know are left out of it.`)
660
+ io.log('Restoring the database…')
661
+ try {
662
+ const { dropped, notes } = await streamRestore(join(staging, 'database.dump'), settings.databaseUrl, server.settings)
663
+ if (dropped.length) io.log(`Left out for this server: ${[...new Set(dropped)].map((n) => `SET ${n}`).join(', ')}.`)
664
+ if (notes) io.log(notes)
665
+ }
666
+ catch (err) {
667
+ throw restoreFailure(err as Error, dumpMajor, server.major, targetLabel(settings.databaseUrl))
668
+ }
669
+
670
+ io.log(`Restoring ${manifest.media.length} media file(s)…`)
671
+ await pool(manifest.media, 4, async (m) => {
672
+ await store.write(m.key, readFileSync(insideDir(mediaDir, m.key)), m.mime)
673
+ })
674
+ io.log(`✓ restored. Start the site; it applies any newer migrations itself, or run \`tinyzoo up\`.`)
675
+ return manifest
676
+ }
677
+ finally {
678
+ rmSync(staging, { recursive: true, force: true })
679
+ }
680
+ }
681
+
682
+ // ── command line ───────────────────────────────────────────────────────────────
683
+
684
+ const stamp = () => new Date().toISOString().replace(/\.\d+Z$/, '').replace(/[-:]/g, '').replace('T', '-')
685
+
686
+ function flags(args: string[]): { positional: string[]; yes: boolean; force: boolean } {
687
+ return { positional: args.filter((a) => !a.startsWith('-')), yes: args.includes('--yes') || args.includes('-y'), force: args.includes('--force') }
688
+ }
689
+
690
+ function report(err: unknown): never {
691
+ console.error(err instanceof CliError ? `✗ ${err.message}` : `✗ ${(err as Error)?.stack ?? err}`)
692
+ process.exit(1)
693
+ }
694
+
695
+ export async function backup(args: string[]): Promise<void> {
696
+ try {
697
+ const settings = await loadSettings(process.cwd())
698
+ const { positional } = flags(args)
699
+ const out = positional[0] ?? `tinyzoo-backup-${settings.databaseUrl ? databaseName(settings.databaseUrl) : 'site'}-${stamp()}.tar`
700
+ await runBackup(settings, resolve(process.cwd(), out))
701
+ }
702
+ catch (err) { report(err) }
703
+ }
704
+
705
+ export async function restore(args: string[]): Promise<void> {
706
+ try {
707
+ const { positional, yes, force } = flags(args)
708
+ if (!positional[0]) fail('usage: tinyzoo restore <file> [--yes] [--force]')
709
+ const settings = await loadSettings(process.cwd())
710
+ await runRestore(settings, resolve(process.cwd(), positional[0]!), { yes, force })
711
+ }
712
+ catch (err) { report(err) }
713
+ }