@cossackframework/cms 0.0.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +145 -2
- package/agent-docs.json +259 -0
- package/app/README.md +78 -0
- package/app/cossack-recipe.json +43 -0
- package/app/src/auth-bot-check.ts +32 -0
- package/app/src/auth-bot-token.ts +11 -0
- package/app/src/auth-input.ts +35 -0
- package/app/src/auth-options.ts +18 -0
- package/app/src/auth-providers.ts +16 -0
- package/app/src/auth.ts +1082 -0
- package/app/src/brand.ts +68 -0
- package/app/src/cms/ai.ts +54 -0
- package/app/src/cms/analytics.ts +33 -0
- package/app/src/cms/block-errors.ts +55 -0
- package/app/src/cms/block-inserter.ts +111 -0
- package/app/src/cms/block-panel.ts +68 -0
- package/app/src/cms/block-rows.ts +44 -0
- package/app/src/cms/block-tree.ts +585 -0
- package/app/src/cms/blocks.css +708 -0
- package/app/src/cms/builder.ts +126 -0
- package/app/src/cms/cache.ts +44 -0
- package/app/src/cms/carousel.client.ts +52 -0
- package/app/src/cms/collab/presence.ts +69 -0
- package/app/src/cms/collab/rich-text-collab.client.ts +197 -0
- package/app/src/cms/collab/rich-text-notes.client.ts +138 -0
- package/app/src/cms/collab/room.ts +881 -0
- package/app/src/cms/compare.client.ts +47 -0
- package/app/src/cms/condition-control.ts +109 -0
- package/app/src/cms/content-api.ts +143 -0
- package/app/src/cms/content.ts +524 -0
- package/app/src/cms/countdown.client.ts +37 -0
- package/app/src/cms/editor-layout-editor.ts +260 -0
- package/app/src/cms/editor-layout-view.ts +37 -0
- package/app/src/cms/editor.ts +381 -0
- package/app/src/cms/entry.ts +26 -0
- package/app/src/cms/feeds.ts +145 -0
- package/app/src/cms/field-controls.ts +222 -0
- package/app/src/cms/field-group-editor.ts +105 -0
- package/app/src/cms/field-group-view.ts +211 -0
- package/app/src/cms/form-view.ts +203 -0
- package/app/src/cms/form.client.ts +203 -0
- package/app/src/cms/guards.ts +27 -0
- package/app/src/cms/handoff.ts +202 -0
- package/app/src/cms/headings.ts +51 -0
- package/app/src/cms/icons.ts +80 -0
- package/app/src/cms/import.client.ts +16 -0
- package/app/src/cms/index.ts +191 -0
- package/app/src/cms/lightbox.client.ts +109 -0
- package/app/src/cms/machine.ts +118 -0
- package/app/src/cms/maintenance.ts +97 -0
- package/app/src/cms/manage.ts +1236 -0
- package/app/src/cms/mcp-tools.ts +388 -0
- package/app/src/cms/mcp.ts +242 -0
- package/app/src/cms/media-chooser.ts +123 -0
- package/app/src/cms/media-usage.ts +57 -0
- package/app/src/cms/middleware.ts +199 -0
- package/app/src/cms/middlewares.ts +13 -0
- package/app/src/cms/network-ui.ts +36 -0
- package/app/src/cms/network-view.ts +38 -0
- package/app/src/cms/notes-announce.ts +65 -0
- package/app/src/cms/notes-rail.ts +207 -0
- package/app/src/cms/notification-rules.ts +66 -0
- package/app/src/cms/recovery.ts +82 -0
- package/app/src/cms/replace.ts +63 -0
- package/app/src/cms/reserved-paths.ts +19 -0
- package/app/src/cms/rich-text-field.ts +325 -0
- package/app/src/cms/rich-text-schema.ts +91 -0
- package/app/src/cms/rich-text.client.ts +255 -0
- package/app/src/cms/search.client.ts +116 -0
- package/app/src/cms/seo-head.ts +78 -0
- package/app/src/cms/seo.ts +241 -0
- package/app/src/cms/settings-draft.ts +218 -0
- package/app/src/cms/settings-file.ts +127 -0
- package/app/src/cms/settings-form.ts +180 -0
- package/app/src/cms/settings-languages.ts +125 -0
- package/app/src/cms/settings-nav.ts +102 -0
- package/app/src/cms/settings-network.ts +151 -0
- package/app/src/cms/settings-permalinks.ts +114 -0
- package/app/src/cms/settings-scroll.ts +10 -0
- package/app/src/cms/settings-search.ts +77 -0
- package/app/src/cms/settings-section.ts +38 -0
- package/app/src/cms/share.client.ts +27 -0
- package/app/src/cms/site-editing.ts +219 -0
- package/app/src/cms/site-editor.client.ts +1008 -0
- package/app/src/cms/site-editor.ts +99 -0
- package/app/src/cms/site-export.ts +60 -0
- package/app/src/cms/site-forms.ts +143 -0
- package/app/src/cms/site-import.ts +28 -0
- package/app/src/cms/site-session.ts +36 -0
- package/app/src/cms/site-taskbar.ts +77 -0
- package/app/src/cms/site-view.ts +1014 -0
- package/app/src/cms/site.ts +1322 -0
- package/app/src/cms/sitemaps.ts +131 -0
- package/app/src/cms/stat.client.ts +56 -0
- package/app/src/cms/style-controls.ts +252 -0
- package/app/src/cms/tabs.client.ts +36 -0
- package/app/src/cms/taskbar.ts +134 -0
- package/app/src/cms/term-icons.ts +10 -0
- package/app/src/cms/traffic.ts +26 -0
- package/app/src/cms/ui.ts +170 -0
- package/app/src/cms/updates.ts +124 -0
- package/app/src/cms/uploads.ts +26 -0
- package/app/src/cms/worker.ts +13 -0
- package/app/src/config/auth.ts +30 -0
- package/app/src/config/cms.ts +18 -0
- package/app/src/dashboard/access.ts +344 -0
- package/app/src/dashboard/agent-icons.ts +44 -0
- package/app/src/dashboard/app-passwords.ts +74 -0
- package/app/src/dashboard/avatar-field.ts +58 -0
- package/app/src/dashboard/bulk.ts +53 -0
- package/app/src/dashboard/head.ts +29 -0
- package/app/src/dashboard/icons.ts +15 -0
- package/app/src/dashboard/list-table.ts +120 -0
- package/app/src/dashboard/modules/roles.ts +15 -0
- package/app/src/dashboard/modules/sessions.ts +15 -0
- package/app/src/dashboard/modules/settings.ts +15 -0
- package/app/src/dashboard/modules/users.ts +18 -0
- package/app/src/dashboard/page.ts +67 -0
- package/app/src/dashboard/registry.ts +13 -0
- package/app/src/dashboard/session-list.ts +98 -0
- package/app/src/dashboard/tabs.ts +26 -0
- package/app/src/dashboard/time-ago.ts +16 -0
- package/app/src/dashboard/types.ts +40 -0
- package/app/src/dashboard/user-sections.ts +172 -0
- package/app/src/lib/permissions.ts +84 -0
- package/app/src/middlewares/auth.ts +117 -0
- package/app/src/pages/(site)/[...path]/index.ts +609 -0
- package/app/src/pages/(site)/feed/[...rest]/index.ts +13 -0
- package/app/src/pages/(site)/feed/index.ts +12 -0
- package/app/src/pages/(site)/index.ts +11 -0
- package/app/src/pages/(site)/indexnow/[file]/index.ts +18 -0
- package/app/src/pages/(site)/layout.ts +219 -0
- package/app/src/pages/(site)/llms.txt/index.ts +23 -0
- package/app/src/pages/(site)/manifest.webmanifest/index.ts +29 -0
- package/app/src/pages/(site)/media/[id]/[name]/index.ts +59 -0
- package/app/src/pages/(site)/preview/[collection]/[id]/index.ts +115 -0
- package/app/src/pages/(site)/robots.txt/index.ts +29 -0
- package/app/src/pages/(site)/search/index.ts +36 -0
- package/app/src/pages/(site)/sitemap.xml/index.ts +26 -0
- package/app/src/pages/(site)/sitemaps/[file]/index.ts +33 -0
- package/app/src/pages/api/auth/session.ts +48 -0
- package/app/src/pages/api/cms/appearance/areas/[name].ts +12 -0
- package/app/src/pages/api/cms/appearance/areas/index.ts +6 -0
- package/app/src/pages/api/cms/appearance/locations/[location].ts +6 -0
- package/app/src/pages/api/cms/appearance/locations/index.ts +6 -0
- package/app/src/pages/api/cms/appearance/menus/[id].ts +12 -0
- package/app/src/pages/api/cms/appearance/menus/index.ts +13 -0
- package/app/src/pages/api/cms/appearance/themes/[name]/activate.ts +6 -0
- package/app/src/pages/api/cms/appearance/themes/index.ts +6 -0
- package/app/src/pages/api/cms/cache/purge.ts +6 -0
- package/app/src/pages/api/cms/collections/index.ts +6 -0
- package/app/src/pages/api/cms/commands/[plugin]/[command].ts +26 -0
- package/app/src/pages/api/cms/comments/[id].ts +9 -0
- package/app/src/pages/api/cms/comments/index.ts +126 -0
- package/app/src/pages/api/cms/comments/queue.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/autosave.ts +27 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/blocks/[block].ts +16 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/notes.ts +18 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/publish.ts +11 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/[revision]/index.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/[revision]/restore.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/revisions/index.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry]/unpublish.ts +6 -0
- package/app/src/pages/api/cms/content/[collection]/[entry].ts +42 -0
- package/app/src/pages/api/cms/content/[collection]/index.ts +24 -0
- package/app/src/pages/api/cms/export/[collection].ts +37 -0
- package/app/src/pages/api/cms/forms/[slug]/index.ts +173 -0
- package/app/src/pages/api/cms/forms/[slug]/submissions/[submission]/files/[file].ts +25 -0
- package/app/src/pages/api/cms/forms/[slug]/submissions/export.ts +34 -0
- package/app/src/pages/api/cms/forms/[slug]/submissions/index.ts +24 -0
- package/app/src/pages/api/cms/import/[collection].ts +23 -0
- package/app/src/pages/api/cms/mcp.ts +29 -0
- package/app/src/pages/api/cms/me/index.ts +6 -0
- package/app/src/pages/api/cms/me/recent-blocks.ts +37 -0
- package/app/src/pages/api/cms/me/tokens/[id].ts +6 -0
- package/app/src/pages/api/cms/me/tokens/index.ts +13 -0
- package/app/src/pages/api/cms/media/[id].ts +6 -0
- package/app/src/pages/api/cms/media.ts +26 -0
- package/app/src/pages/api/cms/menus/[location].ts +14 -0
- package/app/src/pages/api/cms/network/sites/[id]/export.ts +15 -0
- package/app/src/pages/api/cms/network/sites/[id]/index.ts +9 -0
- package/app/src/pages/api/cms/network/sites/index.ts +14 -0
- package/app/src/pages/api/cms/notes/[id].ts +13 -0
- package/app/src/pages/api/cms/notes/index.ts +6 -0
- package/app/src/pages/api/cms/payments/webhook.ts +28 -0
- package/app/src/pages/api/cms/plugins/[name]/grant.ts +6 -0
- package/app/src/pages/api/cms/plugins/[name]/revoke.ts +6 -0
- package/app/src/pages/api/cms/plugins/index.ts +6 -0
- package/app/src/pages/api/cms/recovery.ts +29 -0
- package/app/src/pages/api/cms/redirects/index.ts +9 -0
- package/app/src/pages/api/cms/roles/index.ts +6 -0
- package/app/src/pages/api/cms/schema/[collection].ts +13 -0
- package/app/src/pages/api/cms/search.ts +16 -0
- package/app/src/pages/api/cms/settings/[group].ts +9 -0
- package/app/src/pages/api/cms/settings/index.ts +9 -0
- package/app/src/pages/api/cms/site-export.ts +34 -0
- package/app/src/pages/api/cms/taxonomies/index.ts +6 -0
- package/app/src/pages/api/cms/terms/[taxonomy]/[id].ts +12 -0
- package/app/src/pages/api/cms/terms/[taxonomy]/index.ts +9 -0
- package/app/src/pages/api/cms/uploads/[id]/complete.ts +15 -0
- package/app/src/pages/api/cms/uploads/[id]/index.ts +15 -0
- package/app/src/pages/api/cms/uploads/index.ts +26 -0
- package/app/src/pages/api/cms/users/[id]/index.ts +12 -0
- package/app/src/pages/api/cms/users/[id]/meta.ts +40 -0
- package/app/src/pages/api/cms/users/index.ts +12 -0
- package/app/src/pages/api/cms/webhooks/[id].ts +6 -0
- package/app/src/pages/api/cms/webhooks/index.ts +9 -0
- package/app/src/pages/auth/[provider]/callback.ts +11 -0
- package/app/src/pages/auth/[provider]/redirect.ts +9 -0
- package/app/src/pages/auth/forgot-password/index.ts +75 -0
- package/app/src/pages/auth/layout.ts +32 -0
- package/app/src/pages/auth/login/index.ts +198 -0
- package/app/src/pages/auth/magic/index.ts +68 -0
- package/app/src/pages/auth/register/index.ts +117 -0
- package/app/src/pages/auth/reset-password/index.ts +65 -0
- package/app/src/pages/auth/setup/index.ts +194 -0
- package/app/src/pages/auth/two-factor/index.ts +67 -0
- package/app/src/pages/auth/verify/index.ts +62 -0
- package/app/src/pages/dashboard/(cms)/appearance/customize/index.ts +263 -0
- package/app/src/pages/dashboard/(cms)/appearance/dashboard-menu/index.ts +241 -0
- package/app/src/pages/dashboard/(cms)/appearance/index.ts +89 -0
- package/app/src/pages/dashboard/(cms)/appearance/menus/[id]/index.ts +697 -0
- package/app/src/pages/dashboard/(cms)/appearance/menus/index.ts +145 -0
- package/app/src/pages/dashboard/(cms)/appearance/widgets/index.ts +279 -0
- package/app/src/pages/dashboard/(cms)/blocks/index.ts +84 -0
- package/app/src/pages/dashboard/(cms)/calendar/index.ts +92 -0
- package/app/src/pages/dashboard/(cms)/collections/[name]/index.ts +525 -0
- package/app/src/pages/dashboard/(cms)/collections/index.ts +72 -0
- package/app/src/pages/dashboard/(cms)/collections/new/index.ts +66 -0
- package/app/src/pages/dashboard/(cms)/comments/index.ts +237 -0
- package/app/src/pages/dashboard/(cms)/content/[collection]/[id]/index.ts +2382 -0
- package/app/src/pages/dashboard/(cms)/content/[collection]/[id]/revisions/index.ts +126 -0
- package/app/src/pages/dashboard/(cms)/content/[collection]/index.ts +693 -0
- package/app/src/pages/dashboard/(cms)/field-groups/[id]/index.ts +193 -0
- package/app/src/pages/dashboard/(cms)/field-groups/index.ts +65 -0
- package/app/src/pages/dashboard/(cms)/forms/[id]/submissions/[submission]/index.ts +279 -0
- package/app/src/pages/dashboard/(cms)/forms/[id]/submissions/index.ts +170 -0
- package/app/src/pages/dashboard/(cms)/forms/index.ts +171 -0
- package/app/src/pages/dashboard/(cms)/media/[id]/index.ts +317 -0
- package/app/src/pages/dashboard/(cms)/media/index.ts +156 -0
- package/app/src/pages/dashboard/(cms)/media/new/index.ts +9 -0
- package/app/src/pages/dashboard/(cms)/media/summary/index.ts +74 -0
- package/app/src/pages/dashboard/(cms)/my-sites/index.ts +63 -0
- package/app/src/pages/dashboard/(cms)/network/index.ts +53 -0
- package/app/src/pages/dashboard/(cms)/network/plugins/index.ts +110 -0
- package/app/src/pages/dashboard/(cms)/network/sites/[id]/index.ts +243 -0
- package/app/src/pages/dashboard/(cms)/network/sites/index.ts +186 -0
- package/app/src/pages/dashboard/(cms)/network/sites/new/index.ts +77 -0
- package/app/src/pages/dashboard/(cms)/network/themes/index.ts +82 -0
- package/app/src/pages/dashboard/(cms)/network/updates/index.ts +48 -0
- package/app/src/pages/dashboard/(cms)/network/users/[id]/index.ts +176 -0
- package/app/src/pages/dashboard/(cms)/network/users/index.ts +174 -0
- package/app/src/pages/dashboard/(cms)/network/users/new/index.ts +66 -0
- package/app/src/pages/dashboard/(cms)/plugins/index.ts +172 -0
- package/app/src/pages/dashboard/(cms)/profile/public/index.ts +105 -0
- package/app/src/pages/dashboard/(cms)/registrations/index.ts +70 -0
- package/app/src/pages/dashboard/(cms)/settings/index.ts +359 -0
- package/app/src/pages/dashboard/(cms)/settings/json/index.ts +202 -0
- package/app/src/pages/dashboard/(cms)/settings/layout.ts +67 -0
- package/app/src/pages/dashboard/(cms)/taxonomies/[name]/index.ts +85 -0
- package/app/src/pages/dashboard/(cms)/taxonomies/index.ts +98 -0
- package/app/src/pages/dashboard/(cms)/terms/[taxonomy]/[id]/index.ts +244 -0
- package/app/src/pages/dashboard/(cms)/terms/[taxonomy]/index.ts +148 -0
- package/app/src/pages/dashboard/(cms)/tools/activity/index.ts +94 -0
- package/app/src/pages/dashboard/(cms)/tools/automations/index.ts +131 -0
- package/app/src/pages/dashboard/(cms)/tools/cache/index.ts +92 -0
- package/app/src/pages/dashboard/(cms)/tools/export/index.ts +99 -0
- package/app/src/pages/dashboard/(cms)/tools/health/index.ts +189 -0
- package/app/src/pages/dashboard/(cms)/tools/jobs/index.ts +93 -0
- package/app/src/pages/dashboard/(cms)/tools/links/index.ts +115 -0
- package/app/src/pages/dashboard/(cms)/tools/redirects/index.ts +161 -0
- package/app/src/pages/dashboard/(cms)/tools/replace/index.ts +124 -0
- package/app/src/pages/dashboard/(cms)/tools/schema/index.ts +54 -0
- package/app/src/pages/dashboard/(cms)/tools/seo/index.ts +156 -0
- package/app/src/pages/dashboard/(cms)/tools/updates/index.ts +114 -0
- package/app/src/pages/dashboard/(cms)/tools/webhooks/index.ts +182 -0
- package/app/src/pages/dashboard/index.ts +214 -0
- package/app/src/pages/dashboard/layout.ts +320 -0
- package/app/src/pages/dashboard/profile/index.ts +211 -0
- package/app/src/pages/dashboard/profile/security/index.ts +255 -0
- package/app/src/pages/dashboard/roles/[id]/index.ts +116 -0
- package/app/src/pages/dashboard/roles/index.ts +134 -0
- package/app/src/pages/dashboard/roles/new/index.ts +95 -0
- package/app/src/pages/dashboard/sessions/index.ts +105 -0
- package/app/src/pages/dashboard/users/[id]/index.ts +472 -0
- package/app/src/pages/dashboard/users/index.ts +299 -0
- package/app/src/pages/dashboard/users/new/index.ts +123 -0
- package/app/src/passkeys.client.ts +18 -0
- package/app/src/plugins/default-theme/assets/cover.svg +31 -0
- package/app/src/plugins/default-theme/assets/logo.svg +8 -0
- package/app/src/plugins/default-theme/parts/comments.ts +66 -0
- package/app/src/plugins/default-theme/parts/list.ts +38 -0
- package/app/src/plugins/default-theme/parts/media.ts +15 -0
- package/app/src/plugins/default-theme/parts/options.ts +45 -0
- package/app/src/plugins/default-theme/plugin.json +275 -0
- package/app/src/plugins/default-theme/templates/archive.ts +14 -0
- package/app/src/plugins/default-theme/templates/entry-blank.ts +13 -0
- package/app/src/plugins/default-theme/templates/entry.ts +27 -0
- package/app/src/plugins/default-theme/templates/home.ts +8 -0
- package/app/src/plugins/default-theme/templates/layout.ts +40 -0
- package/app/src/plugins/default-theme/templates/not-found.ts +10 -0
- package/app/src/plugins/default-theme/templates/search.ts +32 -0
- package/app/src/services/rbac.ts +65 -0
- package/app/src/services/roles.ts +89 -0
- package/app/src/services/users.ts +173 -0
- package/package.json +114 -2
- package/src/automations.ts +95 -0
- package/src/bindings.ts +138 -0
- package/src/block-references.ts +239 -0
- package/src/block-shape.ts +155 -0
- package/src/blocks.ts +1157 -0
- package/src/cache.ts +54 -0
- package/src/checks.ts +198 -0
- package/src/cloudflare-workers.d.ts +13 -0
- package/src/cloudflare.ts +61 -0
- package/src/collab.ts +372 -0
- package/src/collections.ts +421 -0
- package/src/comments.ts +46 -0
- package/src/cron.ts +95 -0
- package/src/dashboard-menu.ts +105 -0
- package/src/diff.ts +113 -0
- package/src/editor-layout.ts +316 -0
- package/src/errors.ts +50 -0
- package/src/field-groups.ts +498 -0
- package/src/fields.ts +411 -0
- package/src/form-builder.ts +1053 -0
- package/src/forms.ts +109 -0
- package/src/highlight.ts +134 -0
- package/src/hooks.ts +510 -0
- package/src/icon-catalog.ts +55 -0
- package/src/icons.ts +128 -0
- package/src/images.ts +149 -0
- package/src/index.ts +100 -0
- package/src/isolate-core.ts +245 -0
- package/src/isolate.ts +19 -0
- package/src/isolation.ts +69 -0
- package/src/mail.ts +182 -0
- package/src/manifest.ts +476 -0
- package/src/media.ts +193 -0
- package/src/menus.ts +287 -0
- package/src/module.ts +32 -0
- package/src/modules/content.ts +64 -0
- package/src/modules/forms.ts +35 -0
- package/src/node.ts +66 -0
- package/src/notes.ts +237 -0
- package/src/permissions.ts +139 -0
- package/src/plugin-settings.ts +114 -0
- package/src/records.ts +56 -0
- package/src/redirects.ts +59 -0
- package/src/resource.ts +245 -0
- package/src/richtext.ts +402 -0
- package/src/schema-json.ts +112 -0
- package/src/search.ts +89 -0
- package/src/seo.ts +477 -0
- package/src/server/accounts.ts +418 -0
- package/src/server/ai.ts +33 -0
- package/src/server/api-tokens.ts +91 -0
- package/src/server/areas.ts +116 -0
- package/src/server/audit.ts +94 -0
- package/src/server/auth.ts +89 -0
- package/src/server/automations.ts +190 -0
- package/src/server/autosaves.ts +69 -0
- package/src/server/brand-icons.ts +50 -0
- package/src/server/brands.ts +86 -0
- package/src/server/budget.ts +97 -0
- package/src/server/cache.ts +104 -0
- package/src/server/capabilities.ts +333 -0
- package/src/server/cloudflare.ts +92 -0
- package/src/server/comments.ts +237 -0
- package/src/server/drafts.ts +176 -0
- package/src/server/field-groups.ts +88 -0
- package/src/server/files.ts +780 -0
- package/src/server/forms.ts +726 -0
- package/src/server/handoffs.ts +107 -0
- package/src/server/host-schema.ts +312 -0
- package/src/server/host.ts +2892 -0
- package/src/server/images.ts +34 -0
- package/src/server/index.ts +59 -0
- package/src/server/indexnow.ts +91 -0
- package/src/server/isolation.ts +183 -0
- package/src/server/locked.ts +45 -0
- package/src/server/mail.ts +93 -0
- package/src/server/markdown.ts +24 -0
- package/src/server/menus.ts +286 -0
- package/src/server/network-policy.ts +60 -0
- package/src/server/network.ts +665 -0
- package/src/server/notes.ts +275 -0
- package/src/server/outbox.ts +213 -0
- package/src/server/path-moves.ts +33 -0
- package/src/server/payments.ts +59 -0
- package/src/server/previews.ts +62 -0
- package/src/server/purge.ts +140 -0
- package/src/server/reconcile.ts +38 -0
- package/src/server/redirects.ts +183 -0
- package/src/server/repository.ts +1107 -0
- package/src/server/schedules.ts +105 -0
- package/src/server/schema.ts +562 -0
- package/src/server/security.ts +391 -0
- package/src/server/seed.ts +52 -0
- package/src/server/shared-fields.ts +34 -0
- package/src/server/site-export.ts +151 -0
- package/src/server/site-import.ts +482 -0
- package/src/server/site-search.ts +289 -0
- package/src/server/site-settings.ts +286 -0
- package/src/server/sites.ts +243 -0
- package/src/server/stores.ts +314 -0
- package/src/server/stripe.ts +173 -0
- package/src/server/taxonomies.ts +415 -0
- package/src/server/tokens.ts +25 -0
- package/src/server/traffic.ts +118 -0
- package/src/server/users.ts +653 -0
- package/src/server/webhooks.ts +245 -0
- package/src/settings-controls.ts +54 -0
- package/src/settings-document.ts +111 -0
- package/src/settings.ts +426 -0
- package/src/storage/cloudflare.ts +94 -0
- package/src/storage/index.ts +30 -0
- package/src/storage/node.ts +114 -0
- package/src/taxonomy.ts +51 -0
- package/src/theme.ts +1135 -0
- package/src/to-markdown.ts +179 -0
- package/src/updates.ts +126 -0
- package/src/version.ts +2 -0
- package/src/vite.d.ts +25 -0
- package/src/vite.js +199 -0
- package/src/webhooks.ts +107 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A site export (`siteExport`) read back into a new site, so a site can move: out of a network
|
|
3
|
+
* to its own install, or into another network. The file is read as a stream, a row at a time.
|
|
4
|
+
* The site's tables are replaced by the export's, then the imported collections get their
|
|
5
|
+
* tables and records. Pending work (the outbox, webhook deliveries, jobs) stays behind, so
|
|
6
|
+
* nothing is sent twice; webhooks arrive turned off with new secrets, since exports leave the
|
|
7
|
+
* secrets out. Media files are not in exports: copy the storage separately.
|
|
8
|
+
*/
|
|
9
|
+
import { decodeValue, MAX_STATEMENTS, RequestTooLargeError, type PortableValue, type Statement, type Value } from '@cossackframework/romedb';
|
|
10
|
+
import { CmsError } from '../errors';
|
|
11
|
+
import { NETWORK_ADMIN } from '../permissions';
|
|
12
|
+
import { UNRESTRICTED_GRANT } from '../manifest';
|
|
13
|
+
import { HOST_TABLES } from './host-schema';
|
|
14
|
+
import { EXPORT_SKIPPED_TABLES, newSecret, quote } from './site-export';
|
|
15
|
+
import type { CmsHost } from './host';
|
|
16
|
+
import type { CmsUser } from './users';
|
|
17
|
+
|
|
18
|
+
export interface SiteImportReport {
|
|
19
|
+
/** What the export says it is. */
|
|
20
|
+
readonly site: { readonly id: string; readonly name: string; readonly host: string | null };
|
|
21
|
+
readonly exportedAt: string;
|
|
22
|
+
readonly tables: number;
|
|
23
|
+
readonly rows: number;
|
|
24
|
+
/** Tables in the export that this install has no table for, such as a plugin's that is not installed. */
|
|
25
|
+
readonly skipped: readonly string[];
|
|
26
|
+
/**
|
|
27
|
+
* Members after the import: `network` rejoined network accounts with their ID, `site` got
|
|
28
|
+
* site accounts without passwords, and `pending` have no account yet: on the main site,
|
|
29
|
+
* setup's takeover gives them one.
|
|
30
|
+
*/
|
|
31
|
+
readonly members: { readonly network: number; readonly site: number; readonly pending: number };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SiteImportOptions {
|
|
35
|
+
readonly signal?: AbortSignal;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Tables the site keeps its own of: the schema's ledgers, and pending work that would run again. */
|
|
39
|
+
const KEPT_TABLES: ReadonlySet<string> = new Set(['cms_migrations', 'cms_schema_state', 'cms_schema_generations', 'cms_outbox', 'cms_webhook_deliveries', 'cossack_jobs']);
|
|
40
|
+
const HOST: ReadonlySet<string> = new Set(HOST_TABLES);
|
|
41
|
+
/** The `cms_network` key that marks an import in progress, so a retry may replace what it wrote. */
|
|
42
|
+
const IMPORT_KEY = 'import';
|
|
43
|
+
/** An import that wrote nothing for this long has stopped; another may take over. */
|
|
44
|
+
const IMPORT_LEASE_MS = 5 * 60_000;
|
|
45
|
+
const WRITE = { kind: 'cms.import', retryConflicts: 3 } as const;
|
|
46
|
+
/** Of the database's request size, the share rows take: the rest is the statements' encoding around them. */
|
|
47
|
+
const BATCH_SHARE = 0.75;
|
|
48
|
+
/** A statement's text around its row's values, at most. */
|
|
49
|
+
const STATEMENT_TEXT = 1024;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Import an export into this site. The site must have no entries and no accounts of its own,
|
|
53
|
+
* unless an earlier import of it stopped: then this one starts over. Members of the main site,
|
|
54
|
+
* such as the network admin who added the site, stay members when the export does not have them.
|
|
55
|
+
*/
|
|
56
|
+
export async function siteImport(host: CmsHost, source: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>, options: SiteImportOptions = {}): Promise<SiteImportReport> {
|
|
57
|
+
const migrated = await host.migrate(options.signal ? { signal: options.signal } : {});
|
|
58
|
+
if (migrated.failed) throw new CmsError('CMS_CONFLICT', `The schema could not be applied: ${migrated.failed.message}`);
|
|
59
|
+
await host.ready({ force: true });
|
|
60
|
+
const lease = await claim(host);
|
|
61
|
+
try { return await importInto(host, source, lease, options); }
|
|
62
|
+
catch (error) {
|
|
63
|
+
await lease.stop().catch(() => undefined);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function importInto(host: CmsHost, source: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>, lease: Lease, options: SiteImportOptions): Promise<SiteImportReport> {
|
|
69
|
+
const db = host.db;
|
|
70
|
+
// One write holds what the database takes in a request: its statement count, and its size with room for the encoding.
|
|
71
|
+
const batchText = Math.floor(await db.maxRequestBytes() * BATCH_SHARE);
|
|
72
|
+
const write = async (statements: Statement[]) => {
|
|
73
|
+
try { return await db.write(statements, { ...WRITE, ...(options.signal ? { signal: options.signal } : {}) }); }
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (error instanceof RequestTooLargeError) throw new CmsError('CMS_LIMIT', 'A row in this export is larger than the database takes in one write, so the export cannot be imported here.', { cause: error });
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const before = (await allUsers(host)).filter(user => user.account === 'network');
|
|
80
|
+
|
|
81
|
+
const reader = new ExportReader(source);
|
|
82
|
+
const header = await reader.header();
|
|
83
|
+
if (header.format !== 'cossack-site-export') throw new CmsError('CMS_VALIDATION', 'This is not a site export.');
|
|
84
|
+
if (header.version !== 1) throw new CmsError('CMS_VALIDATION', `This export has version ${String(header.version)}; this install reads version 1. Update it first.`);
|
|
85
|
+
|
|
86
|
+
let tables = await tablesHere(host);
|
|
87
|
+
let collections = false;
|
|
88
|
+
let count = 0;
|
|
89
|
+
let rows = 0;
|
|
90
|
+
const skipped: string[] = [];
|
|
91
|
+
for (let table = await reader.table(); table; table = await reader.table()) {
|
|
92
|
+
// The CMS's tables come first: once they are in, the imported collections get their tables.
|
|
93
|
+
if (!collections && !HOST.has(table.name)) {
|
|
94
|
+
collections = true;
|
|
95
|
+
tables = await openCollections(host, options);
|
|
96
|
+
}
|
|
97
|
+
const here = tables.get(table.name);
|
|
98
|
+
if (KEPT_TABLES.has(table.name) || EXPORT_SKIPPED_TABLES.has(table.name) || !here || here.model !== table.model) {
|
|
99
|
+
if (!KEPT_TABLES.has(table.name) && !EXPORT_SKIPPED_TABLES.has(table.name)) skipped.push(table.name);
|
|
100
|
+
while (await reader.row() !== undefined) { /* not imported */ }
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const columns = table.model === 'relational' ? await columnsOf(host, table.name, table.columns ?? []) : null;
|
|
104
|
+
let batch: Statement[] = [{ rql: `DELETE FROM ${quote(table.name)}` }];
|
|
105
|
+
let size = 0;
|
|
106
|
+
const flush = async () => {
|
|
107
|
+
await write(batch);
|
|
108
|
+
await lease.wrote();
|
|
109
|
+
batch = [];
|
|
110
|
+
size = 0;
|
|
111
|
+
};
|
|
112
|
+
for (let row = await reader.row(); row !== undefined; row = await reader.row()) {
|
|
113
|
+
const statement = columns ? insertRow(table.name, columns, row) : { rql: `INSERT INTO ${quote(table.name)} DOCUMENT $document`, params: { $document: decoded(row) } };
|
|
114
|
+
const text = reader.lastSize + STATEMENT_TEXT;
|
|
115
|
+
if (batch.length >= MAX_STATEMENTS || size + text > batchText) await flush();
|
|
116
|
+
batch.push(statement);
|
|
117
|
+
size += text;
|
|
118
|
+
rows++;
|
|
119
|
+
}
|
|
120
|
+
if (batch.length) await flush();
|
|
121
|
+
count++;
|
|
122
|
+
}
|
|
123
|
+
await reader.end();
|
|
124
|
+
// Unrestricted access is granted where it runs, and network admins are the network's: an export can make neither.
|
|
125
|
+
await write([
|
|
126
|
+
{ rql: 'DELETE FROM cms_grants WHERE permission = $permission', params: { $permission: UNRESTRICTED_GRANT } },
|
|
127
|
+
{ rql: 'DELETE FROM cms_user_roles WHERE role_id = $role', params: { $role: NETWORK_ADMIN } },
|
|
128
|
+
{ rql: 'DELETE FROM cms_roles WHERE id = $role', params: { $role: NETWORK_ADMIN } },
|
|
129
|
+
]);
|
|
130
|
+
if (!collections) await openCollections(host, options);
|
|
131
|
+
const members = await adoptMembers(host, before);
|
|
132
|
+
await lease.release();
|
|
133
|
+
forget(host);
|
|
134
|
+
await host.ready({ force: true });
|
|
135
|
+
await host.siteSearch.rebuild();
|
|
136
|
+
return { site: header.site, exportedAt: header.exportedAt, tables: count, rows, skipped, members };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Whether an export can be imported here: `new` (no entries, no accounts of its own),
|
|
141
|
+
* `stopped` (an import stopped partway; another starts over), `running`, or `used`.
|
|
142
|
+
*/
|
|
143
|
+
export async function siteImportState(host: CmsHost): Promise<'new' | 'stopped' | 'running' | 'used'> {
|
|
144
|
+
if (!(await host.installed())) return 'new';
|
|
145
|
+
const marker = (await host.db.read([{ rql: 'SELECT updated_at FROM cms_network WHERE key = $key', params: { $key: IMPORT_KEY } }])).results[0]!.rows[0];
|
|
146
|
+
if (marker) return Number(marker[0] as bigint) > host.now() - IMPORT_LEASE_MS ? 'running' : 'stopped';
|
|
147
|
+
return (await (host.siteAccounts ?? host.accounts).count()) > 0 || await hasEntries(host) ? 'used' : 'new';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The imported collection and taxonomy definitions are loaded and migrated, so their tables exist. */
|
|
151
|
+
async function openCollections(host: CmsHost, options: SiteImportOptions): Promise<Map<string, { model: string }>> {
|
|
152
|
+
forget(host);
|
|
153
|
+
await host.ready({ force: true });
|
|
154
|
+
const report = await host.migrate(options.signal ? { signal: options.signal } : {});
|
|
155
|
+
if (report.failed) throw new CmsError('CMS_CONFLICT', `The imported collections could not be created: ${report.failed.message}`);
|
|
156
|
+
return tablesHere(host);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Drop what the host cached from before the import: settings (and plugin states in them), menus, collections. */
|
|
160
|
+
function forget(host: CmsHost): void {
|
|
161
|
+
host.settings.refresh();
|
|
162
|
+
host.menus.refresh();
|
|
163
|
+
host.refreshActivation();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function tablesHere(host: CmsHost): Promise<Map<string, { model: string }>> {
|
|
167
|
+
const info = (await host.db.read([{ rql: 'INFO FOR DB' }])).results[0]!.rows[0]![0] as unknown as { tables?: { name: string; model: string }[] };
|
|
168
|
+
return new Map((info.tables ?? []).map(table => [table.name, { model: table.model }]));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The export's columns, which this install's table must all have; newer exports may have more. */
|
|
172
|
+
async function columnsOf(host: CmsHost, table: string, columns: readonly string[]): Promise<readonly string[]> {
|
|
173
|
+
const described = (await host.db.read([{ rql: `INFO FOR TABLE ${quote(table)}` }])).results[0]!.rows[0]![0] as unknown as { columns: { name: string }[] };
|
|
174
|
+
const here = new Set(described.columns.map(column => column.name));
|
|
175
|
+
const missing = columns.filter(column => !here.has(column));
|
|
176
|
+
if (missing.length) throw new CmsError('CMS_VALIDATION', `This export comes from a newer CMS: ${table} has ${missing.join(', ')}, which this install does not. Update it first.`);
|
|
177
|
+
return columns;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** A RomeDB value from its portable form, as `siteExport` wrote it. */
|
|
181
|
+
function decoded(value: unknown): Value {
|
|
182
|
+
try { return decodeValue(value as PortableValue); }
|
|
183
|
+
catch { throw new CmsError('CMS_VALIDATION', 'The export has a value that is not a RomeDB value. Export the site again.'); }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function insertRow(table: string, columns: readonly string[], row: unknown): Statement {
|
|
187
|
+
if (!Array.isArray(row) || row.length !== columns.length) throw new CmsError('CMS_VALIDATION', `The export has a row of ${table} that does not match its columns.`);
|
|
188
|
+
const values = columns.map((_, index) => decoded(row[index]));
|
|
189
|
+
if (table === 'cms_webhooks') {
|
|
190
|
+
// Exports leave webhook secrets out: a new one, and the webhook off until someone checks it.
|
|
191
|
+
values[columns.indexOf('secret')] = newSecret();
|
|
192
|
+
if (columns.includes('active')) values[columns.indexOf('active')] = 0n;
|
|
193
|
+
}
|
|
194
|
+
// Automations send email from the moment they are on: off until someone here checks them, as webhooks.
|
|
195
|
+
if (table === 'cms_automations' && columns.includes('active')) values[columns.indexOf('active')] = 0n;
|
|
196
|
+
return {
|
|
197
|
+
rql: `INSERT INTO ${quote(table)} (${columns.map(quote).join(', ')}) VALUES (${columns.map((_, index) => `$c${index}`).join(', ')})`,
|
|
198
|
+
params: Object.fromEntries(values.map((value, index) => [`$c${index}`, value as Value])),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
interface Lease {
|
|
203
|
+
/** Rows were written: renew the claim now and then. */
|
|
204
|
+
wrote(): Promise<void>;
|
|
205
|
+
release(): Promise<void>;
|
|
206
|
+
/** The import failed: free the site if it wrote nothing, or leave it marked as stopped so a retry may start over. */
|
|
207
|
+
stop(): Promise<void>;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Claim the site for this import: it must be new (no entries, no accounts of its own), or an
|
|
212
|
+
* earlier import of it stopped. The claim is renewed as rows are written.
|
|
213
|
+
*/
|
|
214
|
+
async function claim(host: CmsHost): Promise<Lease> {
|
|
215
|
+
const db = host.db;
|
|
216
|
+
const now = host.now();
|
|
217
|
+
const marker = (await db.read([{ rql: 'SELECT updated_at FROM cms_network WHERE key = $key', params: { $key: IMPORT_KEY } }])).results[0]!.rows[0];
|
|
218
|
+
if (marker) {
|
|
219
|
+
if (Number(marker[0] as bigint) > now - IMPORT_LEASE_MS) throw new CmsError('CMS_CONFLICT', 'An import into this site is running. Wait for it to finish.');
|
|
220
|
+
} else {
|
|
221
|
+
// An added site's own accounts, or on the main site any account: a fresh install has none.
|
|
222
|
+
if ((await (host.siteAccounts ?? host.accounts).count()) > 0 || await hasEntries(host)) {
|
|
223
|
+
throw new CmsError('CMS_CONFLICT', 'Import into a new site: this one has entries or accounts of its own.');
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// Taking over a stopped import is guarded by the time it last wrote, so only one import takes it.
|
|
227
|
+
const result = await db.write([marker
|
|
228
|
+
? { rql: 'UPDATE cms_network SET updated_at = $now WHERE key = $key AND updated_at = $was', params: { $key: IMPORT_KEY, $now: now, $was: marker[0] as bigint } }
|
|
229
|
+
: { rql: 'INSERT INTO cms_network (key, value, updated_at) VALUES ($key, $value, $now) ON CONFLICT (key) DO NOTHING', params: { $key: IMPORT_KEY, $value: JSON.stringify({ startedAt: now }), $now: now } }], WRITE);
|
|
230
|
+
if (result.results[0]!.affected !== 1n) throw new CmsError('CMS_CONFLICT', 'An import into this site is running. Wait for it to finish.');
|
|
231
|
+
let last = now;
|
|
232
|
+
let written = !!marker;
|
|
233
|
+
const release = async () => { await db.write([{ rql: 'DELETE FROM cms_network WHERE key = $key', params: { $key: IMPORT_KEY } }], WRITE); };
|
|
234
|
+
return {
|
|
235
|
+
async wrote() {
|
|
236
|
+
written = true;
|
|
237
|
+
const at = host.now();
|
|
238
|
+
if (at - last < 30_000) return;
|
|
239
|
+
await db.write([{ rql: 'UPDATE cms_network SET updated_at = $now WHERE key = $key', params: { $key: IMPORT_KEY, $now: at } }], WRITE);
|
|
240
|
+
last = at;
|
|
241
|
+
},
|
|
242
|
+
release,
|
|
243
|
+
async stop() {
|
|
244
|
+
if (!written) return release();
|
|
245
|
+
await db.write([{ rql: 'UPDATE cms_network SET updated_at = 0 WHERE key = $key', params: { $key: IMPORT_KEY } }], WRITE);
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function hasEntries(host: CmsHost): Promise<boolean> {
|
|
251
|
+
const tables = await tablesHere(host);
|
|
252
|
+
const storages = [...new Set(host.resources.map(resource => resource.storage))].filter(storage => tables.has(storage));
|
|
253
|
+
if (!storages.length) return false;
|
|
254
|
+
const result = await host.db.read(storages.map(storage => ({ rql: `SELECT 1 FROM ${quote(storage)} LIMIT 1` })));
|
|
255
|
+
return result.results.some(rows => rows.rows.length > 0);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Members whose ID is a network account rejoin it; on an added site the others get site
|
|
260
|
+
* accounts without passwords, set with Forgot password. Network members from before the
|
|
261
|
+
* import that the export does not have stay members, unless their email is now taken.
|
|
262
|
+
*/
|
|
263
|
+
async function adoptMembers(host: CmsHost, before: readonly { id: string; roles: readonly string[] }[]): Promise<SiteImportReport['members']> {
|
|
264
|
+
const counts = { network: 0, site: 0, pending: 0 };
|
|
265
|
+
const now = host.now();
|
|
266
|
+
for (let offset = 0; ; offset += 200) {
|
|
267
|
+
const ids = (await host.db.read([{ rql: 'SELECT id FROM cms_users ORDER BY id LIMIT 200 OFFSET $offset', params: { $offset: offset } }])).results[0]!.rows.map(row => row[0] as string);
|
|
268
|
+
if (!ids.length) break;
|
|
269
|
+
const network = await host.accounts.existing(ids);
|
|
270
|
+
const local = host.siteAccounts ? await host.siteAccounts.existing(ids) : new Set<string>();
|
|
271
|
+
for (const id of ids) if (network.has(id)) { await host.accounts.join(id, host.site); counts.network++; }
|
|
272
|
+
const orphans = ids.filter(id => !network.has(id) && !local.has(id));
|
|
273
|
+
if (host.siteAccounts && orphans.length) {
|
|
274
|
+
const params = Object.fromEntries(orphans.map((id, index) => [`$u${index}`, id]));
|
|
275
|
+
await host.db.write([{
|
|
276
|
+
rql: `INSERT INTO cms_accounts (id, email, name, password_hash, avatar, status, network_role, created_at, updated_at) SELECT id, email, name, NULL, avatar, status, NULL, created_at, $now FROM cms_users WHERE id IN (${orphans.map((_, index) => `$u${index}`).join(', ')}) ON CONFLICT (id) DO NOTHING`,
|
|
277
|
+
params: { ...params, $now: now },
|
|
278
|
+
}], WRITE);
|
|
279
|
+
counts.site += orphans.length;
|
|
280
|
+
} else counts.pending += orphans.length;
|
|
281
|
+
counts.site += [...local].length;
|
|
282
|
+
if (ids.length < 200) break;
|
|
283
|
+
}
|
|
284
|
+
for (const member of before) {
|
|
285
|
+
if (await host.users.get(member.id)) continue;
|
|
286
|
+
try { await host.users.addMember(member.id); }
|
|
287
|
+
catch {
|
|
288
|
+
await host.accounts.leave(member.id, host.site);
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const roles = [];
|
|
292
|
+
for (const role of member.roles) if (await host.users.role(role)) roles.push(role);
|
|
293
|
+
await host.users.setRoles(member.id, roles);
|
|
294
|
+
counts.network++;
|
|
295
|
+
}
|
|
296
|
+
return counts;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
interface ExportHeader {
|
|
300
|
+
readonly format: unknown;
|
|
301
|
+
readonly version: unknown;
|
|
302
|
+
readonly exportedAt: string;
|
|
303
|
+
readonly site: SiteImportReport['site'];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Reads an export as a stream: the header up to `tables`, then each table's name, model and
|
|
308
|
+
* columns, then its rows one at a time. Each row is parsed on its own, so memory holds one row.
|
|
309
|
+
*/
|
|
310
|
+
class ExportReader {
|
|
311
|
+
readonly #chunks: AsyncIterator<Uint8Array | string>;
|
|
312
|
+
readonly #decoder = new TextDecoder();
|
|
313
|
+
#text = '';
|
|
314
|
+
#at = 0;
|
|
315
|
+
#done = false;
|
|
316
|
+
/** In a table's rows, between rows. */
|
|
317
|
+
#inRows = false;
|
|
318
|
+
#firstRow = true;
|
|
319
|
+
#firstTable = true;
|
|
320
|
+
/** The length of the last row's text, for batching writes. */
|
|
321
|
+
lastSize = 0;
|
|
322
|
+
|
|
323
|
+
constructor(source: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>) {
|
|
324
|
+
const iterable: AsyncIterable<Uint8Array | string> = Symbol.asyncIterator in source ? source as AsyncIterable<Uint8Array | string> : readStream(source as ReadableStream<Uint8Array>);
|
|
325
|
+
this.#chunks = iterable[Symbol.asyncIterator]();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** The document's fields before `tables`; the writer puts `tables` last. */
|
|
329
|
+
async header(): Promise<ExportHeader> {
|
|
330
|
+
await this.#expect('{');
|
|
331
|
+
const fields: { [key: string]: unknown } = {};
|
|
332
|
+
for (let first = true; ; first = false) {
|
|
333
|
+
if (!first) await this.#expect(',');
|
|
334
|
+
const key = await this.#value();
|
|
335
|
+
if (typeof key !== 'string') throw truncated('a field name');
|
|
336
|
+
await this.#expect(':');
|
|
337
|
+
if (key === 'tables') break;
|
|
338
|
+
fields[key] = await this.#value();
|
|
339
|
+
}
|
|
340
|
+
await this.#expect('[');
|
|
341
|
+
const site = fields.site as SiteImportReport['site'] | undefined;
|
|
342
|
+
return { format: fields.format, version: fields.version, exportedAt: String(fields.exportedAt ?? ''), site: { id: String(site?.id ?? ''), name: String(site?.name ?? ''), host: site?.host ?? null } };
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** The next table's description, or null after the last one. Its rows must be read first. */
|
|
346
|
+
async table(): Promise<{ name: string; model: string; columns: string[] | null } | null> {
|
|
347
|
+
if (this.#inRows) throw new Error('Read the rows of the table before the next one.');
|
|
348
|
+
if (await this.#peek() === ']') { this.#at++; return null; }
|
|
349
|
+
if (!this.#firstTable) await this.#expect(',');
|
|
350
|
+
this.#firstTable = false;
|
|
351
|
+
await this.#expect('{');
|
|
352
|
+
const fields: { [key: string]: unknown } = {};
|
|
353
|
+
for (let first = true; ; first = false) {
|
|
354
|
+
if (!first) await this.#expect(',');
|
|
355
|
+
const key = await this.#value();
|
|
356
|
+
await this.#expect(':');
|
|
357
|
+
if (key === 'rows') break;
|
|
358
|
+
fields[key as string] = await this.#value();
|
|
359
|
+
}
|
|
360
|
+
await this.#expect('[');
|
|
361
|
+
this.#inRows = true;
|
|
362
|
+
this.#firstRow = true;
|
|
363
|
+
if (typeof fields.name !== 'string' || (fields.model !== 'relational' && fields.model !== 'document')) throw new CmsError('CMS_VALIDATION', 'The export has a table without a name or model.');
|
|
364
|
+
return { name: fields.name, model: fields.model, columns: Array.isArray(fields.columns) ? fields.columns.map(String) : null };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** The table's next row, or undefined after its last. */
|
|
368
|
+
async row(): Promise<unknown> {
|
|
369
|
+
if (!this.#inRows) return undefined;
|
|
370
|
+
if (await this.#peek() === ']') {
|
|
371
|
+
this.#at++;
|
|
372
|
+
await this.#expect('}');
|
|
373
|
+
this.#inRows = false;
|
|
374
|
+
return undefined;
|
|
375
|
+
}
|
|
376
|
+
if (!this.#firstRow) await this.#expect(',');
|
|
377
|
+
this.#firstRow = false;
|
|
378
|
+
return this.#value();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** The end of the document. */
|
|
382
|
+
async end(): Promise<void> {
|
|
383
|
+
await this.#expect('}');
|
|
384
|
+
if (await this.#peek() !== null) throw new CmsError('CMS_VALIDATION', 'The export has text after its end.');
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** The next character after whitespace, or null at the end of the stream. */
|
|
388
|
+
async #peek(): Promise<string | null> {
|
|
389
|
+
for (;;) {
|
|
390
|
+
while (this.#at < this.#text.length) {
|
|
391
|
+
const code = this.#text.charCodeAt(this.#at);
|
|
392
|
+
if (code === 0x20 || code === 0x0a || code === 0x0d || code === 0x09) this.#at++;
|
|
393
|
+
else return this.#text[this.#at]!;
|
|
394
|
+
}
|
|
395
|
+
if (!(await this.#more())) return null;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async #expect(character: string): Promise<void> {
|
|
400
|
+
const next = await this.#peek();
|
|
401
|
+
if (next !== character) throw next === null ? truncated(`"${character}"`) : new CmsError('CMS_VALIDATION', `The export is not valid: expected "${character}" but found "${next}".`);
|
|
402
|
+
this.#at++;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** One whole JSON value, parsed. */
|
|
406
|
+
async #value(): Promise<unknown> {
|
|
407
|
+
const first = await this.#peek();
|
|
408
|
+
if (first === null) throw truncated('a value');
|
|
409
|
+
let index = this.#at;
|
|
410
|
+
let depth = 0;
|
|
411
|
+
let inString = false;
|
|
412
|
+
let escaped = false;
|
|
413
|
+
const literal = first !== '{' && first !== '[' && first !== '"';
|
|
414
|
+
for (;;) {
|
|
415
|
+
if (index >= this.#text.length) {
|
|
416
|
+
// Reading more drops the text before this value, which starts at 0 after.
|
|
417
|
+
const start = this.#at;
|
|
418
|
+
const more = await this.#more();
|
|
419
|
+
index -= start;
|
|
420
|
+
if (more) continue;
|
|
421
|
+
if (literal && index > 0) break;
|
|
422
|
+
throw truncated('a value');
|
|
423
|
+
}
|
|
424
|
+
const character = this.#text[index]!;
|
|
425
|
+
if (literal) {
|
|
426
|
+
if (character === ',' || character === '}' || character === ']' || /\s/.test(character)) break;
|
|
427
|
+
} else if (inString) {
|
|
428
|
+
if (escaped) escaped = false;
|
|
429
|
+
else if (character === '\\') escaped = true;
|
|
430
|
+
else if (character === '"') { inString = false; if (depth === 0) { index++; break; } }
|
|
431
|
+
} else if (character === '"') inString = true;
|
|
432
|
+
else if (character === '{' || character === '[') depth++;
|
|
433
|
+
else if (character === '}' || character === ']') { if (--depth === 0) { index++; break; } }
|
|
434
|
+
index++;
|
|
435
|
+
}
|
|
436
|
+
const text = this.#text.slice(this.#at, index);
|
|
437
|
+
this.#at = index;
|
|
438
|
+
this.lastSize = text.length;
|
|
439
|
+
try { return JSON.parse(text); }
|
|
440
|
+
catch { throw new CmsError('CMS_VALIDATION', 'The export is not valid JSON.'); }
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** Read the next chunk, dropping text already read. False at the end of the stream. */
|
|
444
|
+
async #more(): Promise<boolean> {
|
|
445
|
+
this.#text = this.#text.slice(this.#at);
|
|
446
|
+
this.#at = 0;
|
|
447
|
+
if (this.#done) return false;
|
|
448
|
+
const next = await this.#chunks.next();
|
|
449
|
+
if (next.done) {
|
|
450
|
+
this.#done = true;
|
|
451
|
+
const rest = this.#decoder.decode();
|
|
452
|
+
this.#text += rest;
|
|
453
|
+
return rest.length > 0;
|
|
454
|
+
}
|
|
455
|
+
this.#text += typeof next.value === 'string' ? next.value : this.#decoder.decode(next.value, { stream: true });
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async function* readStream(stream: ReadableStream<Uint8Array>): AsyncGenerator<Uint8Array> {
|
|
461
|
+
const reader = stream.getReader();
|
|
462
|
+
try {
|
|
463
|
+
for (;;) {
|
|
464
|
+
const { done, value } = await reader.read();
|
|
465
|
+
if (done) return;
|
|
466
|
+
yield value;
|
|
467
|
+
}
|
|
468
|
+
} finally { reader.releaseLock(); }
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const truncated = (wanted: string) => new CmsError('CMS_VALIDATION', `The export ends early: expected ${wanted}. Download it again.`);
|
|
472
|
+
|
|
473
|
+
/** Every member of the site, a page at a time by ID, so no page limit or offset cap stops it. */
|
|
474
|
+
async function allUsers(host: CmsHost): Promise<CmsUser[]> {
|
|
475
|
+
const users: CmsUser[] = [];
|
|
476
|
+
for (let after: string | null = null; ;) {
|
|
477
|
+
const page = await host.users.after(after, 200);
|
|
478
|
+
users.push(...page.users);
|
|
479
|
+
if (page.next === null) return users;
|
|
480
|
+
after = page.next;
|
|
481
|
+
}
|
|
482
|
+
}
|