@cossackframework/cms 0.0.0-stage → 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 +147 -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 +123 -3
- 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,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser's part of a before-and-after block: the range input over each pair moves the
|
|
3
|
+
* handle (pointer, touch and arrow keys), a mouse moving over the pair moves it too, and the
|
|
4
|
+
* thumbnails pick the pair shown. Without this every pair shows, split where it starts.
|
|
5
|
+
*/
|
|
6
|
+
const enhanced = new WeakSet<HTMLElement>();
|
|
7
|
+
|
|
8
|
+
export function enhanceCompare(root: HTMLElement): void {
|
|
9
|
+
if (enhanced.has(root)) return;
|
|
10
|
+
enhanced.add(root);
|
|
11
|
+
root.setAttribute('data-enhanced', '');
|
|
12
|
+
const pairs = [...root.querySelectorAll<HTMLElement>(':scope > .cms-compare-pair')];
|
|
13
|
+
const thumbs = [...root.querySelectorAll<HTMLButtonElement>(':scope > .cms-compare-thumbs > [role="tab"]')];
|
|
14
|
+
for (const stage of root.querySelectorAll<HTMLElement>('.cms-compare-stage')) {
|
|
15
|
+
const range = stage.querySelector<HTMLInputElement>('.cms-compare-range');
|
|
16
|
+
if (!range) continue;
|
|
17
|
+
const set = (percent: number) => {
|
|
18
|
+
const value = Math.min(Math.max(percent, 0), 100);
|
|
19
|
+
stage.style.setProperty('--cms-compare', `${value}%`);
|
|
20
|
+
range.value = String(Math.round(value));
|
|
21
|
+
};
|
|
22
|
+
range.addEventListener('input', () => set(Number(range.value)));
|
|
23
|
+
// A mouse reveals as it moves, with no press; touch and pen drag the range.
|
|
24
|
+
stage.addEventListener('pointermove', event => {
|
|
25
|
+
if (event.pointerType !== 'mouse') return;
|
|
26
|
+
const box = stage.getBoundingClientRect();
|
|
27
|
+
if (box.width) set(((event.clientX - box.left) / box.width) * 100);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (thumbs.length !== pairs.length || pairs.length < 2) return;
|
|
31
|
+
const select = (index: number) => {
|
|
32
|
+
pairs.forEach((pair, at) => { if (at === index) pair.setAttribute('data-active', ''); else pair.removeAttribute('data-active'); });
|
|
33
|
+
thumbs.forEach((thumb, at) => { thumb.setAttribute('aria-selected', at === index ? 'true' : 'false'); thumb.tabIndex = at === index ? 0 : -1; });
|
|
34
|
+
};
|
|
35
|
+
thumbs.forEach((thumb, index) => {
|
|
36
|
+
thumb.addEventListener('click', () => select(index));
|
|
37
|
+
thumb.addEventListener('keydown', event => {
|
|
38
|
+
const to = event.key === 'ArrowRight' ? index + 1 : event.key === 'ArrowLeft' ? index - 1 : -1;
|
|
39
|
+
if (to < 0 || to >= thumbs.length) return;
|
|
40
|
+
event.preventDefault();
|
|
41
|
+
select(to);
|
|
42
|
+
thumbs[to]!.focus();
|
|
43
|
+
thumbs[to]!.scrollIntoView({ block: 'nearest', inline: 'nearest' });
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
select(0);
|
|
47
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The conditional logic control: show or hide a form's block, or send a notification, only
|
|
3
|
+
* when the form's values match rules, as Gravity Forms' conditional logic. Client-safe.
|
|
4
|
+
*/
|
|
5
|
+
import { component, html, type TemplateResult } from '@cossackframework/renderer';
|
|
6
|
+
import { Button, Checkbox, Input, NativeSelect } from '@cossackframework/ui';
|
|
7
|
+
import { CONDITION_LIMIT, inputName, type BlockCondition, type ConditionOperator, type ConditionRule } from '@cossackframework/cms';
|
|
8
|
+
import type { BlockOption, EditorBlock } from './editor';
|
|
9
|
+
|
|
10
|
+
/** An input a rule can test. */
|
|
11
|
+
export interface ConditionField { name: string; label: string }
|
|
12
|
+
|
|
13
|
+
const OPERATORS: readonly { value: ConditionOperator; label: string }[] = [
|
|
14
|
+
{ value: 'is', label: 'is' }, { value: 'isNot', label: 'is not' }, { value: 'contains', label: 'contains' },
|
|
15
|
+
{ value: 'empty', label: 'is empty' }, { value: 'notEmpty', label: 'is not empty' },
|
|
16
|
+
{ value: 'gt', label: 'is greater than' }, { value: 'lt', label: 'is less than' }, { value: 'in', label: 'is one of' },
|
|
17
|
+
];
|
|
18
|
+
const NO_VALUE = new Set<ConditionOperator>(['empty', 'notEmpty']);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The inputs a condition on block `except` can test, in order, named as the server will name
|
|
22
|
+
* them: in a form, the inputs of the form's top level, and for a block inside a repeater, the
|
|
23
|
+
* repeater's rows' inputs first. In a field group, every field.
|
|
24
|
+
*/
|
|
25
|
+
export function conditionFields(blocks: readonly EditorBlock[], options: readonly BlockOption[], except?: string): readonly ConditionField[] {
|
|
26
|
+
// Editors render this on every keystroke; blocks are replaced, never changed in place, so the same list gives the same fields.
|
|
27
|
+
const known = conditionCache.get(blocks);
|
|
28
|
+
if (known?.options === options && known.fields.has(except)) return known.fields.get(except)!;
|
|
29
|
+
const fields = collectConditionFields(blocks, options, except);
|
|
30
|
+
const entry = known?.options === options ? known : { options, fields: new Map<string | undefined, readonly ConditionField[]>() };
|
|
31
|
+
entry.fields.set(except, fields);
|
|
32
|
+
conditionCache.set(blocks, entry);
|
|
33
|
+
return fields;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const conditionCache = new WeakMap<readonly EditorBlock[], { options: readonly BlockOption[]; fields: Map<string | undefined, readonly ConditionField[]> }>();
|
|
37
|
+
|
|
38
|
+
function collectConditionFields(blocks: readonly EditorBlock[], options: readonly BlockOption[], except?: string): ConditionField[] {
|
|
39
|
+
const types = new Map(options.map(option => [option.name, option]));
|
|
40
|
+
const input = (block: EditorBlock) => { const option = types.get(block.type); return !!option && (option.input || !!option.field); };
|
|
41
|
+
const repeater = (block: EditorBlock) => { const option = types.get(block.type); return !!option?.input && !!option.children; };
|
|
42
|
+
const named = (block: EditorBlock): ConditionField => {
|
|
43
|
+
const label = typeof block.data.label === 'string' ? block.data.label : '';
|
|
44
|
+
const name = typeof block.data.name === 'string' && block.data.name.trim() ? block.data.name.trim() : inputName(label);
|
|
45
|
+
return { name, label: label || name };
|
|
46
|
+
};
|
|
47
|
+
const top: ConditionField[] = [];
|
|
48
|
+
const inside = new Map<string, ConditionField[]>();
|
|
49
|
+
let scope: string | null = null;
|
|
50
|
+
const walk = (list: readonly EditorBlock[], within: string | null) => {
|
|
51
|
+
for (const block of list) {
|
|
52
|
+
if (block._id === except) scope = within;
|
|
53
|
+
if (input(block) && block._id !== except) {
|
|
54
|
+
const into = within ? inside.get(within)! : top;
|
|
55
|
+
const field = named(block);
|
|
56
|
+
if (!into.some(entry => entry.name === field.name)) into.push(field);
|
|
57
|
+
}
|
|
58
|
+
if (repeater(block) && !within) { inside.set(block._id, []); walk(block.children ?? [], block._id); }
|
|
59
|
+
else if (block.children) walk(block.children, within);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
walk(blocks, null);
|
|
63
|
+
const own = scope ? inside.get(scope) ?? [] : [];
|
|
64
|
+
return [...own, ...top.filter(field => !own.some(entry => entry.name === field.name))];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Edit a condition. `subject` names what it applies to, such as "this block"; `change`
|
|
69
|
+
* receives undefined when conditional logic is turned off.
|
|
70
|
+
*/
|
|
71
|
+
export function conditionControl(when: BlockCondition | undefined, fields: readonly ConditionField[], idPrefix: string, change: (when: BlockCondition | undefined) => void,
|
|
72
|
+
labels: { readonly toggle: string; readonly subject: string; readonly show?: string; readonly hide?: string } = { toggle: 'Conditional logic', subject: 'this block' }): TemplateResult {
|
|
73
|
+
const id = `${idPrefix}-when`;
|
|
74
|
+
const blank = (): ConditionRule => ({ field: fields[0]?.name ?? '', op: 'is', value: '' });
|
|
75
|
+
const toggle = html`<label for=${id} class="flex items-center gap-2 text-xs font-medium text-foreground">
|
|
76
|
+
${component(Checkbox, { id, checked: Boolean(when), disabled: !fields.length && !when, '@change': (event: Event) => change((event.target as HTMLInputElement).checked ? { rules: [blank()] } : undefined) })}
|
|
77
|
+
${labels.toggle}</label>`;
|
|
78
|
+
if (!when) return html`<div class="space-y-1">${toggle}${fields.length ? null : html`<p class="text-xs text-muted-foreground">${__('Add a field to the form to show or hide this on its value.')}</p>`}</div>`;
|
|
79
|
+
const set = (next: Partial<BlockCondition>) => change({ ...when, ...next });
|
|
80
|
+
const setRule = (index: number, next: Partial<ConditionRule>) => set({ rules: when.rules.map((rule, at) => at === index ? { ...rule, ...next } : rule) });
|
|
81
|
+
const select = (name: string, label: string, value: string, choices: readonly { value: string; label: string }[], pick: (value: string) => void) =>
|
|
82
|
+
component(NativeSelect, { id: name, size: 'sm', 'aria-label': label, '@change': (event: Event) => pick((event.target as HTMLSelectElement).value) },
|
|
83
|
+
html`${choices.map(choice => html`<option value=${choice.value} ?selected=${choice.value === value}>${choice.label}</option>`)}`);
|
|
84
|
+
const known = fields.map(field => ({ value: field.name, label: field.label }));
|
|
85
|
+
return html`<div class="space-y-2 rounded-md border border-border p-3">
|
|
86
|
+
${toggle}
|
|
87
|
+
<div class="flex flex-wrap items-center gap-2 text-xs text-foreground">
|
|
88
|
+
${select(`${id}-action`, __('Show or hide'), when.action ?? 'show', [{ value: 'show', label: labels.show ?? __('Show') }, { value: 'hide', label: labels.hide ?? __('Hide') }], value => set({ action: value === 'hide' ? 'hide' : 'show' }))}
|
|
89
|
+
<span>${labels.subject} ${__('if')}</span>
|
|
90
|
+
${select(`${id}-match`, __('Which rules'), when.match ?? 'all', [{ value: 'all', label: __('all') }, { value: 'any', label: __('any') }], value => set({ match: value === 'any' ? 'any' : 'all' }))}
|
|
91
|
+
<span>${__('of these match:')}</span>
|
|
92
|
+
</div>
|
|
93
|
+
<ul class="space-y-2">${when.rules.map((rule, index) => html`<li class="flex flex-wrap items-center gap-2" data-rule=${index}>
|
|
94
|
+
${select(`${id}-${index}-field`, __('Field'), rule.field, known.some(choice => choice.value === rule.field) ? known : [...known, { value: rule.field, label: rule.field || __('Choose a field') }], value => setRule(index, { field: value }))}
|
|
95
|
+
${select(`${id}-${index}-op`, __('Comparison'), rule.op, OPERATORS.map(entry => ({ value: entry.value, label: __(entry.label) })), value => setRule(index, { op: value as ConditionOperator }))}
|
|
96
|
+
${NO_VALUE.has(rule.op) ? null : component(Input, { id: `${id}-${index}-value`, size: 'sm', class: 'w-40', 'aria-label': __('Value'), placeholder: rule.op === 'in' ? __('a, b, c') : '', '.value': rule.value ?? '',
|
|
97
|
+
'@input': (event: Event) => setRule(index, { value: (event.target as HTMLInputElement).value }) })}
|
|
98
|
+
${when.rules.length > 1 ? component(Button, { type: 'button', size: 'sm', variant: 'ghost', 'aria-label': `${__('Remove rule')} ${index + 1}`, '@click': () => set({ rules: when.rules.filter((_, at) => at !== index) }) }, '×') : null}
|
|
99
|
+
</li>`)}</ul>
|
|
100
|
+
${when.rules.length < CONDITION_LIMIT ? component(Button, { type: 'button', size: 'sm', variant: 'outline', '@click': () => set({ rules: [...when.rules, blank()] }) }, __('Add rule')) : null}
|
|
101
|
+
</div>`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** A condition as saved: rules without a field are dropped, values trimmed; undefined when none is left. */
|
|
105
|
+
export function conditionInput(when: BlockCondition | undefined): BlockCondition | undefined {
|
|
106
|
+
const rules = (when?.rules ?? []).filter(rule => rule.field).map(rule => ({ field: rule.field, op: rule.op, ...(NO_VALUE.has(rule.op) ? {} : { value: (rule.value ?? '').trim() }) }));
|
|
107
|
+
if (!when || !rules.length) return undefined;
|
|
108
|
+
return { ...(when.action === 'hide' ? { action: 'hide' as const } : {}), ...(when.match === 'any' ? { match: 'any' as const } : {}), rules };
|
|
109
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The content API: published entries as JSON, with rich text as its JSON document so apps
|
|
3
|
+
* render it their own way, and writes for signed-in users and application passwords. Requests
|
|
4
|
+
* act as the signed-in user or the password's owner, otherwise as a visitor.
|
|
5
|
+
*/
|
|
6
|
+
import type { Context } from 'hono';
|
|
7
|
+
import { ClientVisibleError } from '@cossackframework/core';
|
|
8
|
+
import type { Principal } from '@cossackframework/cms';
|
|
9
|
+
import { flattenBlocks, isCmsError, isSameOriginPost, richTextBlocks, richTextHtml, systemPrincipal, type Block, type CollectionDefinition, type RecordShape, type RichTextDoc } from '@cossackframework/cms';
|
|
10
|
+
import { withheldFieldNames, type CmsHost } from '@cossackframework/cms/server';
|
|
11
|
+
import { blocksHtml } from './content';
|
|
12
|
+
import { entryLock } from './site';
|
|
13
|
+
|
|
14
|
+
export interface ApiEntry { [field: string]: unknown; id: string; url: string | null }
|
|
15
|
+
|
|
16
|
+
/** Entries with their public URLs and, with `?format=html`, their rich text and blocks rendered. */
|
|
17
|
+
/** Fields a locked entry's readers do not get (`withheldFieldNames`): all but the core fields, taxonomies and lock flags. */
|
|
18
|
+
export function withheldFields(cms: CmsHost, collection: CollectionDefinition): Set<string> {
|
|
19
|
+
return withheldFieldNames(cms.resourceById(`cms/${collection.name}`).fields);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function apiEntries(cms: CmsHost, collection: CollectionDefinition, records: readonly RecordShape[], html: boolean, principal?: Principal): Promise<ApiEntry[]> {
|
|
23
|
+
const repository = cms.resource(`cms/${collection.name}`);
|
|
24
|
+
const fields = Object.entries(cms.resourceById(`cms/${collection.name}`).fields);
|
|
25
|
+
// The password itself never leaves. A protected entry's fields stay with those who may edit
|
|
26
|
+
// it, and a members-only entry's with signed-in users: others get its core fields alone.
|
|
27
|
+
const withheld = withheldFields(cms, collection);
|
|
28
|
+
records = records.map(record => {
|
|
29
|
+
const { password, ...rest } = record;
|
|
30
|
+
const editor = principal !== undefined && repository.canUpdate(record, principal);
|
|
31
|
+
const lock = entryLock(record, { editor, signedIn: principal?.kind === 'user', roles: principal?.kind === 'user' ? principal.roles : [] });
|
|
32
|
+
if (!lock) return (typeof password === 'string' && password ? (editor ? record : rest) : rest) as RecordShape;
|
|
33
|
+
const bare = Object.fromEntries(Object.entries(rest).filter(([name]) => !withheld.has(name))) as unknown as RecordShape;
|
|
34
|
+
return { ...bare, ...(lock === 'password' ? { protected: true } : { membersOnly: true }), locked: lock };
|
|
35
|
+
});
|
|
36
|
+
const urls = await cms.entryUrls(collection, records);
|
|
37
|
+
const rich = fields.filter(([, field]) => field.kind === 'richtext').map(([name]) => name);
|
|
38
|
+
const blocks = fields.filter(([, field]) => field.kind === 'blocks' && !field.form).map(([name]) => name);
|
|
39
|
+
const files = html && blocks.length && cms.files ? await cms.files.getMany([...new Set(records.flatMap(record => blocks.flatMap(name => Array.isArray(record[name]) ? flattenBlocks(record[name] as Block[]).flatMap(block => Object.values(block.data).filter((value): value is string => typeof value === 'string')) : [])))].slice(0, 200)).catch(() => new Map()) : new Map();
|
|
40
|
+
return records.map(record => ({
|
|
41
|
+
...record, url: urls.get(record.id) ?? null,
|
|
42
|
+
...(html && (rich.length || blocks.length) ? { html: Object.fromEntries([
|
|
43
|
+
...rich.map(name => [name, record[name] ? richTextHtml(record[name] as RichTextDoc, { block: () => '' }) : '']),
|
|
44
|
+
...blocks.map(name => [name, Array.isArray(record[name]) ? blocksHtml(cms, record[name] as Block[], files) : '']),
|
|
45
|
+
]) } : {}),
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Synced patterns the entries' content and rich text use, by ID: their title and blocks. */
|
|
50
|
+
export async function apiPatterns(cms: CmsHost, collection: CollectionDefinition, records: readonly { readonly [field: string]: unknown }[]): Promise<Record<string, { title: string; content: readonly Block[] }>> {
|
|
51
|
+
const fields = Object.entries(cms.resourceById(`cms/${collection.name}`).fields);
|
|
52
|
+
const rich = fields.filter(([, field]) => field.kind === 'richtext').map(([name]) => name);
|
|
53
|
+
const blocks = fields.filter(([, field]) => field.kind === 'blocks').map(([name]) => name);
|
|
54
|
+
const ids = [...new Set(records.flatMap(record => [
|
|
55
|
+
...rich.flatMap(name => record[name] ? richTextBlocks(record[name] as RichTextDoc) : []),
|
|
56
|
+
...blocks.flatMap(name => Array.isArray(record[name]) ? flattenBlocks(record[name] as Block[]) : []),
|
|
57
|
+
]).filter(block => block.type === 'core/pattern').map(block => String(block.data.pattern ?? '')))].filter(Boolean);
|
|
58
|
+
if (!ids.length) return {};
|
|
59
|
+
const found = await cms.resource('cms/patterns').getMany(ids, { principal: systemPrincipal('api') }).catch(() => new Map<string, RecordShape>());
|
|
60
|
+
return Object.fromEntries([...found].map(([id, record]) => [id, { title: String(record.title ?? ''), content: Array.isArray(record.content) ? record.content as Block[] : [] }]));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** JSON for a CMS error (its status) or another error meant for people (400); anything else is rethrown. */
|
|
64
|
+
export function apiError(c: Context, error: unknown): Response {
|
|
65
|
+
if (isCmsError(error)) return c.json({ error: error.message, code: error.code, ...(error.fields ? { fields: error.fields } : {}) }, error.status as 400 | 401 | 403 | 404 | 409 | 422 | 503);
|
|
66
|
+
if (error instanceof ClientVisibleError) return c.json({ error: error.message, code: 'CMS_VALIDATION' }, 400);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Visitors' answers can be cached briefly; signed-in answers are private. */
|
|
71
|
+
export function apiHeaders(c: Context): void {
|
|
72
|
+
c.header('cache-control', c.get('principal')?.kind === 'user' ? 'private, no-store' : 'public, max-age=60');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Whether a write may proceed: application passwords carry no cookies, so only session
|
|
77
|
+
* requests need the same-origin check that stops cross-site forgery.
|
|
78
|
+
*/
|
|
79
|
+
export function writeAllowed(c: Context): Response | null {
|
|
80
|
+
if (!c.get('cms.token') && !isSameOriginPost(c.req.raw)) return c.json({ error: 'Cross-site writes are not allowed.', code: 'CMS_FORBIDDEN' }, 403);
|
|
81
|
+
if (c.get('principal')?.kind !== 'user') return c.json({ error: 'Sign in, or send an application password.', code: 'CMS_UNAUTHENTICATED' }, 401, { 'www-authenticate': 'Bearer' });
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The request's body as a Request of its own, read up to `maxBytes`: null past it, whether or not
|
|
87
|
+
* the request declared its length, so a chunked post cannot make the server buffer without limit.
|
|
88
|
+
*/
|
|
89
|
+
export async function cappedRequest(c: Context, maxBytes: number): Promise<Request | null> {
|
|
90
|
+
const raw = c.req.raw;
|
|
91
|
+
const declared = Number(raw.headers.get('content-length'));
|
|
92
|
+
if (Number.isFinite(declared) && declared > maxBytes) return null;
|
|
93
|
+
if (!raw.body) return raw;
|
|
94
|
+
const reader = raw.body.getReader();
|
|
95
|
+
const chunks: Uint8Array[] = [];
|
|
96
|
+
let size = 0;
|
|
97
|
+
for (;;) {
|
|
98
|
+
const { done, value } = await reader.read();
|
|
99
|
+
if (done) break;
|
|
100
|
+
size += value.byteLength;
|
|
101
|
+
if (size > maxBytes) { await reader.cancel().catch(() => undefined); return null; }
|
|
102
|
+
chunks.push(value);
|
|
103
|
+
}
|
|
104
|
+
const body = new Uint8Array(size);
|
|
105
|
+
let at = 0;
|
|
106
|
+
for (const chunk of chunks) { body.set(chunk, at); at += chunk.byteLength; }
|
|
107
|
+
const headers = new Headers(raw.headers);
|
|
108
|
+
headers.delete('content-length');
|
|
109
|
+
headers.delete('transfer-encoding');
|
|
110
|
+
return new Request(raw.url, { method: raw.method, headers, body });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The JSON object a write sends, or a 400 answer. */
|
|
114
|
+
export async function writeBody(c: Context): Promise<Record<string, unknown> | Response> {
|
|
115
|
+
try {
|
|
116
|
+
const body: unknown = await c.req.json();
|
|
117
|
+
if (body && typeof body === 'object' && !Array.isArray(body)) return body as Record<string, unknown>;
|
|
118
|
+
} catch { /* below */ }
|
|
119
|
+
return c.json({ error: 'Send the fields as a JSON object.', code: 'CMS_VALIDATION' }, 400);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Answer a management operation: its result as JSON (204 when it returns nothing), or its error.
|
|
124
|
+
* Reads are private to the caller; writes need a signed-in user, the same-origin check for
|
|
125
|
+
* sessions, and a JSON object body, handed to `work`.
|
|
126
|
+
*/
|
|
127
|
+
export async function answer(c: Context, work: (body: Record<string, unknown>) => Promise<unknown>, options: { write?: boolean; status?: 200 | 201 } = {}): Promise<Response> {
|
|
128
|
+
let body: Record<string, unknown> = {};
|
|
129
|
+
if (options.write) {
|
|
130
|
+
const refused = writeAllowed(c);
|
|
131
|
+
if (refused) return refused;
|
|
132
|
+
if (!['DELETE', 'GET', 'HEAD'].includes(c.req.method) && c.req.header('content-length') !== '0' && c.req.header('content-type')) {
|
|
133
|
+
const parsed = await writeBody(c);
|
|
134
|
+
if (parsed instanceof Response) return parsed;
|
|
135
|
+
body = parsed;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
c.header('cache-control', options.write ? 'no-store' : 'private, no-store');
|
|
139
|
+
try {
|
|
140
|
+
const data = await work(body);
|
|
141
|
+
return data === undefined ? c.body(null, 204) : c.json(data, options.status ?? 200);
|
|
142
|
+
} catch (error) { return apiError(c, error); }
|
|
143
|
+
}
|