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
package/cli/tar.ts ADDED
@@ -0,0 +1,304 @@
1
+ /**
2
+ * A minimal tar.gz writer and reader (ustar, regular files only) for the content
3
+ * archive — enough for our own archives and for what `tar` writes, without a
4
+ * dependency. Files are streamed from and to disk, so a large library of media never
5
+ * sits in memory.
6
+ */
7
+ import { createReadStream, createWriteStream, type WriteStream } from 'node:fs'
8
+ import { mkdir, rm, stat } from 'node:fs/promises'
9
+ import { dirname, join } from 'node:path'
10
+ import { once } from 'node:events'
11
+ import { Readable } from 'node:stream'
12
+ import { pipeline } from 'node:stream/promises'
13
+ import { createGunzip, createGzip } from 'node:zlib'
14
+
15
+ export interface TarEntry {
16
+ /** Path inside the archive, with `/` separators and no leading slash. */
17
+ name: string
18
+ /** A file on disk to read the content from… */
19
+ path?: string
20
+ /** …or the content itself. */
21
+ data?: Buffer
22
+ }
23
+
24
+ const BLOCK = 512
25
+
26
+ /** A name that stays inside the folder it is extracted to. */
27
+ export function safeArchivePath(name: string): string {
28
+ const parts = name.split('/')
29
+ if (!name || name.startsWith('/') || name.includes('\\') || name.includes('\0') || parts.some((p) => p === '..' || p === '')) {
30
+ throw new Error(`The archive contains an unsafe path: ${JSON.stringify(name)}`)
31
+ }
32
+ return name
33
+ }
34
+
35
+ function octal(value: number, length: number): string {
36
+ return value.toString(8).padStart(length - 1, '0') + '\0'
37
+ }
38
+
39
+ /** ustar splits a long path into prefix (≤155) and name (≤100) at a slash. */
40
+ function splitName(name: string): { prefix: string; base: string } {
41
+ if (Buffer.byteLength(name) <= 100) return { prefix: '', base: name }
42
+ for (let i = name.indexOf('/'); i !== -1; i = name.indexOf('/', i + 1)) {
43
+ const prefix = name.slice(0, i)
44
+ const base = name.slice(i + 1)
45
+ if (Buffer.byteLength(prefix) <= 155 && Buffer.byteLength(base) <= 100) return { prefix, base }
46
+ }
47
+ throw new Error(`Path too long for the archive: ${name}`)
48
+ }
49
+
50
+ function header(name: string, size: number, mtime: Date): Buffer {
51
+ if (size > 0o77777777777) throw new Error(`File too large for the archive: ${name}`)
52
+ const { prefix, base } = splitName(name)
53
+ const h = Buffer.alloc(BLOCK)
54
+ h.write(base, 0, 100, 'utf8')
55
+ h.write(octal(0o644, 8), 100, 8, 'ascii')
56
+ h.write(octal(0, 8), 108, 8, 'ascii')
57
+ h.write(octal(0, 8), 116, 8, 'ascii')
58
+ h.write(octal(size, 12), 124, 12, 'ascii')
59
+ h.write(octal(Math.floor(mtime.getTime() / 1000), 12), 136, 12, 'ascii')
60
+ h.fill(0x20, 148, 156) // the checksum is computed with this field as spaces
61
+ h.write('0', 156, 1, 'ascii')
62
+ h.write('ustar\0', 257, 6, 'ascii')
63
+ h.write('00', 263, 2, 'ascii')
64
+ if (prefix) h.write(prefix, 345, 155, 'utf8')
65
+ let sum = 0
66
+ for (const byte of h) sum += byte
67
+ h.write(sum.toString(8).padStart(6, '0') + '\0 ', 148, 8, 'ascii')
68
+ return h
69
+ }
70
+
71
+ async function* tarChunks(entries: Iterable<TarEntry>): AsyncGenerator<Buffer> {
72
+ const mtime = new Date()
73
+ for (const entry of entries) {
74
+ safeArchivePath(entry.name)
75
+ const size = entry.data ? entry.data.length : (await stat(entry.path!)).size
76
+ yield header(entry.name, size, mtime)
77
+ if (entry.data) yield entry.data
78
+ else {
79
+ let sent = 0
80
+ for await (const chunk of createReadStream(entry.path!)) {
81
+ sent += (chunk as Buffer).length
82
+ yield chunk as Buffer
83
+ }
84
+ if (sent !== size) throw new Error(`${entry.path} changed while it was being archived`)
85
+ }
86
+ const pad = (BLOCK - (size % BLOCK)) % BLOCK
87
+ if (pad) yield Buffer.alloc(pad)
88
+ }
89
+ yield Buffer.alloc(BLOCK * 2) // end of archive
90
+ }
91
+
92
+ /**
93
+ * Write `entries` as a gzip-compressed tar archive at `out`. The file is created
94
+ * readable by its owner only (`mode`, 0600 unless asked otherwise) from the first
95
+ * byte — an archive of a whole site is not something other users of the machine
96
+ * should be able to read while it is being written. An existing `out` is replaced.
97
+ */
98
+ export async function writeTarGz(out: string, entries: Iterable<TarEntry>, mode = 0o600): Promise<void> {
99
+ // Remove first: a leftover file keeps its old (maybe wider) permissions when reopened.
100
+ await rm(out, { force: true })
101
+ await pipeline(Readable.from(tarChunks(entries)), createGzip(), createWriteStream(out, { mode, flags: 'wx' }))
102
+ }
103
+
104
+ function field(h: Buffer, from: number, length: number): string {
105
+ const raw = h.subarray(from, from + length)
106
+ const end = raw.indexOf(0)
107
+ return raw.subarray(0, end === -1 ? raw.length : end).toString('utf8')
108
+ }
109
+
110
+ function parseSize(h: Buffer): number {
111
+ // GNU base-256 sizes (files over 8 GiB): big-endian after the marker byte.
112
+ if (h[124]! & 0x80) {
113
+ if (h[124] !== 0x80) throw new Error('The archive is damaged (bad file size)')
114
+ let size = 0
115
+ for (let i = 125; i < 136; i++) size = size * 256 + h[i]!
116
+ if (!Number.isSafeInteger(size)) throw new Error('The archive holds a file too large to read')
117
+ return size
118
+ }
119
+ const text = h.subarray(124, 136).toString('ascii').replace(/\0.*$/, '').trim()
120
+ const size = parseInt(text || '0', 8)
121
+ if (!Number.isFinite(size) || size < 0) throw new Error('The archive is damaged (bad file size)')
122
+ return size
123
+ }
124
+
125
+ function checksumOk(h: Buffer): boolean {
126
+ const stored = parseInt(h.subarray(148, 156).toString('ascii').replace(/\0.*$/, '').trim() || '-1', 8)
127
+ let sum = 0
128
+ for (let i = 0; i < BLOCK; i++) sum += i >= 148 && i < 156 ? 0x20 : h[i]!
129
+ return stored === sum
130
+ }
131
+
132
+ /** The `path` and `size` records of a pax extended header ("<length> <key>=<value>\n" each). */
133
+ function paxRecords(data: Buffer): { path: string | null; size: number | null } {
134
+ const out = { path: null as string | null, size: null as number | null }
135
+ let i = 0
136
+ while (i < data.length) {
137
+ const space = data.indexOf(0x20, i)
138
+ if (space === -1) break
139
+ const length = parseInt(data.subarray(i, space).toString('ascii'), 10)
140
+ if (!length) break
141
+ const record = data.subarray(space + 1, i + length - 1).toString('utf8')
142
+ if (record.startsWith('path=')) out.path = record.slice(5)
143
+ else if (record.startsWith('size=')) { const n = Number(record.slice(5)); if (Number.isSafeInteger(n) && n >= 0) out.size = n }
144
+ i += length
145
+ }
146
+ return out
147
+ }
148
+
149
+ /** The most a pax or GNU long-name header may hold: a path is at most a few KiB, never more than this. */
150
+ export const MAX_HEADER_BODY = 64 * 1024
151
+
152
+ /** The default cap on the bytes one archive may unpack to (20 GiB). */
153
+ export const DEFAULT_MAX_EXTRACT_BYTES = 20 * 1024 ** 3
154
+
155
+ /**
156
+ * The cap on unpacked bytes: `TINYZOO_MAX_ARCHIVE_BYTES` (a number of bytes), else 20 GiB.
157
+ * A gzip file of a few kilobytes can unpack to gigabytes; this stops it filling the disk.
158
+ */
159
+ export function archiveLimit(env: Record<string, string | undefined> = process.env): number {
160
+ const n = Number(env.TINYZOO_MAX_ARCHIVE_BYTES)
161
+ return Number.isFinite(n) && n > 0 ? Math.floor(n) : DEFAULT_MAX_EXTRACT_BYTES
162
+ }
163
+
164
+ export interface ExtractOptions {
165
+ /** Most bytes the archive may unpack to (default: `archiveLimit()`). */
166
+ maxBytes?: number
167
+ /**
168
+ * Refuse anything that is not a plain file or folder (symbolic and hard links,
169
+ * devices…) instead of skipping it. Links are never created either way.
170
+ */
171
+ strict?: boolean
172
+ }
173
+
174
+ const gib = (n: number) => (n >= 1024 ** 3 ? `${+(n / 1024 ** 3).toFixed(1)} GiB` : `${+(n / 1024 ** 2).toFixed(1)} MiB`)
175
+
176
+ /**
177
+ * Extract a .tar.gz into `dest` (created). Only regular files are written (links and
178
+ * devices are never created); paths that would leave `dest` are refused, as is an
179
+ * archive that would unpack to more than `maxBytes`. Returns the extracted paths,
180
+ * relative to `dest`.
181
+ */
182
+ export async function extractTarGz(file: string, dest: string, opts: ExtractOptions = {}): Promise<string[]> {
183
+ return extractFrom(file, dest, true, opts)
184
+ }
185
+
186
+ /** The same for an uncompressed .tar (what `tinyzoo backup` writes). */
187
+ export async function extractTar(file: string, dest: string, opts: ExtractOptions = {}): Promise<string[]> {
188
+ return extractFrom(file, dest, false, opts)
189
+ }
190
+
191
+ async function extractFrom(file: string, dest: string, gzip: boolean, opts: ExtractOptions): Promise<string[]> {
192
+ const maxBytes = opts.maxBytes ?? archiveLimit()
193
+ const source = createReadStream(file)
194
+ let input: AsyncIterable<Buffer> = source
195
+ if (gzip) {
196
+ const gunzip = createGunzip()
197
+ source.on('error', (err) => gunzip.destroy(err))
198
+ source.pipe(gunzip)
199
+ input = gunzip
200
+ }
201
+
202
+ const extracted: string[] = []
203
+ let head = Buffer.alloc(0)
204
+ type Body = { left: number; pad: number; kind: 'file' | 'skip' | 'pax' | 'gnuname'; name: string; out?: WriteStream; parts: Buffer[] }
205
+ let body = null as Body | null
206
+ let nextName: string | null = null
207
+ let nextSize: number | null = null // a pax `size` (members over 8 GiB, as bsdtar writes them)
208
+ let ended = false
209
+ let total = 0
210
+ let outError: Error | null = null
211
+
212
+ const finish = async () => {
213
+ const b = body!
214
+ body = null
215
+ if (b.kind === 'file') {
216
+ if (outError) throw outError
217
+ await new Promise<void>((resolve, reject) => { b.out!.once('error', reject); b.out!.end(resolve) })
218
+ extracted.push(b.name)
219
+ }
220
+ else if (b.kind === 'pax') { const r = paxRecords(Buffer.concat(b.parts)); nextName = r.path; nextSize = r.size }
221
+ else if (b.kind === 'gnuname') nextName = Buffer.concat(b.parts).toString('utf8').replace(/\0.*$/, '')
222
+ }
223
+
224
+ const start = async (h: Buffer) => {
225
+ if (!checksumOk(h)) throw new Error('The archive is damaged (bad header checksum)')
226
+ const type = String.fromCharCode(h[156] || 0x30)
227
+ let size = parseSize(h)
228
+ if (nextSize !== null && (type === '0' || type === '7')) { size = nextSize; nextSize = null }
229
+ const ustar = h.subarray(257, 262).toString('ascii') === 'ustar'
230
+ const prefix = ustar ? field(h, 345, 155) : ''
231
+ let name = prefix ? `${prefix}/${field(h, 0, 100)}` : field(h, 0, 100)
232
+ if (nextName && (type === '0' || type === '5' || type === '7')) { name = nextName; nextName = null }
233
+ const pad = (BLOCK - (size % BLOCK)) % BLOCK
234
+ if (type === '0' || type === '7') {
235
+ // The declared size counts before a byte is written: a bomb is refused at its header.
236
+ total += size
237
+ if (total > maxBytes) throw new Error(`The archive would unpack to more than ${gib(maxBytes)} (the limit). If it is genuine, raise the limit with TINYZOO_MAX_ARCHIVE_BYTES=<bytes>.`)
238
+ const clean = safeArchivePath(name.replace(/^\.\//, ''))
239
+ const target = join(dest, clean)
240
+ await mkdir(dirname(target), { recursive: true })
241
+ // `wx`: never open an existing path (a name listed twice; a link that was somehow there).
242
+ const out = createWriteStream(target, { flags: 'wx' })
243
+ out.on('error', (err: NodeJS.ErrnoException) => { outError = err.code === 'EEXIST' ? new Error(`The archive lists ${JSON.stringify(clean)} more than once`) : err })
244
+ body = { left: size, pad, kind: 'file', name: clean, out, parts: [] }
245
+ }
246
+ else if (type === 'x' || type === 'L') {
247
+ if (size > MAX_HEADER_BODY) throw new Error(`The archive is damaged or hostile (a ${type === 'x' ? 'pax' : 'long-name'} header of ${size} bytes; the limit is ${MAX_HEADER_BODY / 1024} KiB)`)
248
+ body = { left: size, pad, kind: type === 'x' ? 'pax' : 'gnuname', name, parts: [] }
249
+ }
250
+ else {
251
+ // Folders and the pax "global" header are harmless. A link or a device is never
252
+ // created; strict readers (backups) do not accept an archive that holds one.
253
+ if (opts.strict && type !== '5' && type !== 'g') {
254
+ const what = type === '2' ? 'a symbolic link' : type === '1' ? 'a hard link' : `an entry of type "${type}"`
255
+ throw new Error(`The archive contains ${what} (${JSON.stringify(name)}); only plain files are accepted.`)
256
+ }
257
+ body = { left: size, pad, kind: 'skip', name, parts: [] }
258
+ }
259
+ // An empty file has no body and no padding: it is complete right here.
260
+ if (body.left === 0) await finish()
261
+ }
262
+
263
+ await mkdir(dest, { recursive: true })
264
+ try {
265
+ for await (let chunk of input) {
266
+ while (chunk.length && !ended) {
267
+ if (outError) throw outError
268
+ if (body) {
269
+ if (body.left > 0) {
270
+ const part = chunk.subarray(0, body.left)
271
+ chunk = chunk.subarray(part.length)
272
+ body.left -= part.length
273
+ if (body.kind === 'file') { if (!body.out!.write(part)) await once(body.out!, 'drain') }
274
+ else if (body.kind !== 'skip') body.parts.push(Buffer.from(part))
275
+ if (body.left === 0 && body.pad === 0) await finish()
276
+ }
277
+ else {
278
+ const skip = Math.min(body.pad, chunk.length)
279
+ body.pad -= skip
280
+ chunk = chunk.subarray(skip)
281
+ if (body.pad === 0) await finish()
282
+ }
283
+ continue
284
+ }
285
+ const need = BLOCK - head.length
286
+ head = Buffer.concat([head, chunk.subarray(0, need)])
287
+ chunk = chunk.subarray(Math.min(need, chunk.length))
288
+ if (head.length < BLOCK) continue
289
+ const h = head
290
+ head = Buffer.alloc(0)
291
+ if (h.every((byte) => byte === 0)) { ended = true; continue }
292
+ await start(h)
293
+ }
294
+ }
295
+ if (body || head.length) throw new Error('The archive is incomplete (it ends in the middle of a file)')
296
+ if (outError) throw outError
297
+ return extracted
298
+ }
299
+ finally {
300
+ // A refused archive stops reading at once and leaves nothing open.
301
+ source.destroy()
302
+ ;(body as Body | null)?.out?.destroy()
303
+ }
304
+ }