@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,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a plugin may do beyond its data: library files it owns, email as the site, jobs queued
|
|
3
|
+
* for later (the clock's too, through the manifest's `schedules`), the site's members, a cache
|
|
4
|
+
* namespace and the bindings it names. Each member checks the plugin's grant on every call and
|
|
5
|
+
* throws `CMS_FORBIDDEN` when it is not held or `CMS_UNAVAILABLE` when the plugin is inactive
|
|
6
|
+
* or the site lacks the service. See `specs/cms-plugin-capabilities.md`.
|
|
7
|
+
*/
|
|
8
|
+
import { CmsError } from '../errors';
|
|
9
|
+
import type { PluginJobEvent } from '../hooks';
|
|
10
|
+
import { RESERVED_PLUGIN_NAMES, type CmsPluginManifest } from '../manifest';
|
|
11
|
+
import { mediaUrl, type MediaKind } from '../media';
|
|
12
|
+
import { systemPrincipal, type Principal } from '../permissions';
|
|
13
|
+
import type { CmsCache } from './cache';
|
|
14
|
+
import type { CmsFile, FileMetadata, FileService } from './files';
|
|
15
|
+
import type { CmsMailer } from './mail';
|
|
16
|
+
import type { Outbox } from './outbox';
|
|
17
|
+
import type { CmsUser, CmsUsers } from './users';
|
|
18
|
+
import type { AiProvider, AiRequest } from './ai';
|
|
19
|
+
import type { Checkout, CheckoutInput, PaymentsProvider } from './payments';
|
|
20
|
+
|
|
21
|
+
export interface PluginFiles {
|
|
22
|
+
/** Store a file the plugin owns in the media library; a stream needs its `size`. */
|
|
23
|
+
upload(file: { name: string; contentType: string; size?: number; body: ReadableStream<Uint8Array> | Uint8Array | string } & FileMetadata): Promise<CmsFile>;
|
|
24
|
+
/** Any library file. */
|
|
25
|
+
get(id: string): Promise<CmsFile | null>;
|
|
26
|
+
/** The plugin's own files. */
|
|
27
|
+
list(options?: { kind?: MediaKind; search?: string; limit?: number; offset?: number }): Promise<{ items: CmsFile[]; total: number }>;
|
|
28
|
+
/** Any library file's bytes. */
|
|
29
|
+
open(id: string): Promise<{ file: CmsFile; body: ReadableStream<Uint8Array> } | null>;
|
|
30
|
+
/** Title, alt, caption and description of one of the plugin's files. */
|
|
31
|
+
update(id: string, patch: FileMetadata): Promise<CmsFile>;
|
|
32
|
+
/** Delete one of the plugin's files. */
|
|
33
|
+
delete(id: string): Promise<void>;
|
|
34
|
+
/** The file's public address. */
|
|
35
|
+
url(file: { readonly id: string; readonly name: string }): string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface PluginMail {
|
|
39
|
+
/** Send one email from the site's sender: the text, and the HTML the site's layout makes from it (Settings → Email). */
|
|
40
|
+
send(message: { to: string; subject: string; text: string; replyTo?: string }): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface PluginJobContext {
|
|
44
|
+
/** The system principal `plugin:<name>`, for the plugin's repositories. */
|
|
45
|
+
readonly principal: Principal;
|
|
46
|
+
/** The occurrence a schedule queued, or null for a job the plugin queued. */
|
|
47
|
+
readonly scheduledAt: number | null;
|
|
48
|
+
readonly signal?: AbortSignal;
|
|
49
|
+
}
|
|
50
|
+
export type PluginJobHandler<P = unknown> = (payload: P, context: PluginJobContext) => unknown;
|
|
51
|
+
|
|
52
|
+
export interface PluginJobs {
|
|
53
|
+
/** Name a handler at `register()`; the manifest's `schedules` run the handler of their name. */
|
|
54
|
+
handle<P = unknown>(name: string, handler: PluginJobHandler<P>): void;
|
|
55
|
+
/**
|
|
56
|
+
* Queue a job for now, `at` a time or after `delayMs`. With a `key`, the same key queues once
|
|
57
|
+
* and later calls return false. The payload is JSON up to 64 KB.
|
|
58
|
+
*/
|
|
59
|
+
enqueue(name: string, payload?: unknown, options?: { at?: number; delayMs?: number; key?: string }): Promise<boolean>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** A member as a plugin sees it: no permissions, no account kind, only the plugin's own meta. */
|
|
63
|
+
export interface PluginUser {
|
|
64
|
+
readonly id: string;
|
|
65
|
+
readonly email: string;
|
|
66
|
+
readonly name: string;
|
|
67
|
+
readonly slug: string | null;
|
|
68
|
+
readonly avatar: string | null;
|
|
69
|
+
readonly status: 'active' | 'disabled';
|
|
70
|
+
readonly roles: readonly string[];
|
|
71
|
+
readonly createdAt: number;
|
|
72
|
+
/** What the plugin stored with `setMeta`, or null. */
|
|
73
|
+
readonly meta: unknown;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface PluginUsers {
|
|
77
|
+
get(id: string): Promise<PluginUser | null>;
|
|
78
|
+
findByEmail(email: string): Promise<PluginUser | null>;
|
|
79
|
+
list(options?: { search?: string; role?: string; limit?: number; offset?: number }): Promise<{ items: PluginUser[]; total: number }>;
|
|
80
|
+
/** Create a member. Roles must be assignable: every permission of theirs is the plugin's own (`<plugin>:…` or `<plugin>/…`), or they have none. */
|
|
81
|
+
create(input: { email: string; name: string; password?: string; roles?: readonly string[] }): Promise<PluginUser>;
|
|
82
|
+
/** Store a JSON value with the member under the plugin's own key; null removes it. */
|
|
83
|
+
setMeta(id: string, value: unknown): Promise<boolean>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface PluginCache {
|
|
87
|
+
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
88
|
+
/** Store a value for `ttlMs` milliseconds, or until deleted. */
|
|
89
|
+
set<T = unknown>(key: string, value: T, ttlMs?: number): Promise<void>;
|
|
90
|
+
delete(key: string): Promise<void>;
|
|
91
|
+
remember<T>(key: string, ttlMs: number, compute: () => Promise<T>): Promise<T>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface PluginAi {
|
|
95
|
+
/** The application's model answers the prompt; `CMS_UNAVAILABLE` when the application has no provider. */
|
|
96
|
+
complete(request: AiRequest): Promise<string>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface PluginPayments {
|
|
100
|
+
/** Start a checkout with the application's provider; the result arrives as a `cms.payment.event` action. */
|
|
101
|
+
checkout(input: CheckoutInput): Promise<Checkout>;
|
|
102
|
+
/** The provider's name, or null when the application has none. */
|
|
103
|
+
readonly provider: string | null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface PluginSecrets {
|
|
107
|
+
/** The binding's value, or undefined when the application has none. The name must be in `permissions.secrets`. */
|
|
108
|
+
get(name: string): Promise<string | undefined>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface PluginCapabilities {
|
|
112
|
+
readonly files: PluginFiles;
|
|
113
|
+
readonly mail: PluginMail;
|
|
114
|
+
readonly jobs: PluginJobs;
|
|
115
|
+
readonly users: PluginUsers;
|
|
116
|
+
readonly cache: PluginCache;
|
|
117
|
+
readonly secrets: PluginSecrets;
|
|
118
|
+
readonly ai: PluginAi;
|
|
119
|
+
readonly payments: PluginPayments;
|
|
120
|
+
/** Run the plugin's handler for one of its job events; throws so the outbox retries. */
|
|
121
|
+
runJob(event: PluginJobEvent, context: { signal?: AbortSignal }): Promise<void>;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** What the host lends the capabilities. */
|
|
125
|
+
export interface CapabilityHost {
|
|
126
|
+
readonly name: string;
|
|
127
|
+
readonly manifest: CmsPluginManifest;
|
|
128
|
+
/** Throws unless the plugin is active and holds the grant. */
|
|
129
|
+
allowed(grant: string, what: string): Promise<void>;
|
|
130
|
+
/** An isolated plugin's job handlers, in its Worker: any job name is passed on to it. */
|
|
131
|
+
readonly jobRunner?: (name: string, payload: unknown, context: PluginJobContext) => Promise<unknown>;
|
|
132
|
+
readonly files: FileService | null;
|
|
133
|
+
readonly mail: CmsMailer | null;
|
|
134
|
+
readonly mailFrom: string;
|
|
135
|
+
readonly users: CmsUsers;
|
|
136
|
+
readonly cache: CmsCache;
|
|
137
|
+
readonly outbox: Outbox;
|
|
138
|
+
readonly secrets: (name: string) => string | undefined;
|
|
139
|
+
readonly ai: AiProvider | null;
|
|
140
|
+
readonly payments: PaymentsProvider | null;
|
|
141
|
+
readonly now: () => number;
|
|
142
|
+
/** An outbox row was added: dispatch after the request. */
|
|
143
|
+
readonly queued: () => void;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
147
|
+
export const JOB_NAME = /^[a-z][a-z0-9-]{0,40}$/;
|
|
148
|
+
const JOB_KEY = /^[\x21-\x7e]{1,120}$/;
|
|
149
|
+
const CACHE_KEY = /^[\x21-\x7e]{1,150}$/;
|
|
150
|
+
const MAX_PAYLOAD = 64 * 1024;
|
|
151
|
+
const MAX_TEXT = 100 * 1024;
|
|
152
|
+
/** A job runs within a year. */
|
|
153
|
+
const MAX_DELAY = 366 * 24 * 60 * 60_000;
|
|
154
|
+
|
|
155
|
+
/** The user meta key a plugin's values live under: a system key (`_` and up to 40 characters). */
|
|
156
|
+
export function pluginMetaKey(plugin: string): string {
|
|
157
|
+
const safe = plugin.replace(/-/g, '_');
|
|
158
|
+
if (safe.length <= 37) return `_p_${safe}`;
|
|
159
|
+
let hash = 0x811c9dc5;
|
|
160
|
+
for (const char of plugin) { hash ^= char.charCodeAt(0); hash = Math.imul(hash, 0x01000193) >>> 0; }
|
|
161
|
+
return `_p_${safe.slice(0, 28)}_${hash.toString(16).padStart(8, '0')}`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function pluginCapabilities(host: CapabilityHost): PluginCapabilities {
|
|
165
|
+
const { name } = host;
|
|
166
|
+
const owner = `plugin:${name}`;
|
|
167
|
+
const principal = systemPrincipal(owner);
|
|
168
|
+
const metaKey = pluginMetaKey(name);
|
|
169
|
+
const handlers = new Map<string, PluginJobHandler>();
|
|
170
|
+
|
|
171
|
+
const storage = (): FileService => {
|
|
172
|
+
if (!host.files) throw new CmsError('CMS_UNAVAILABLE', 'The site has no file storage.');
|
|
173
|
+
return host.files;
|
|
174
|
+
};
|
|
175
|
+
const own = async (id: string): Promise<CmsFile> => {
|
|
176
|
+
const file = await storage().get(id);
|
|
177
|
+
if (!file) throw new CmsError('CMS_NOT_FOUND', 'File not found.');
|
|
178
|
+
if (file.owner !== owner) throw new CmsError('CMS_FORBIDDEN', `Plugin ${name} does not own this file.`);
|
|
179
|
+
return file;
|
|
180
|
+
};
|
|
181
|
+
const files: PluginFiles = {
|
|
182
|
+
upload: async file => {
|
|
183
|
+
await host.allowed('capability:files', 'files');
|
|
184
|
+
const service = storage();
|
|
185
|
+
const { body, size: declared, ...metadata } = file;
|
|
186
|
+
let stream: ReadableStream<Uint8Array>;
|
|
187
|
+
let size: number;
|
|
188
|
+
if (typeof body === 'string' || body instanceof Uint8Array) {
|
|
189
|
+
const bytes = typeof body === 'string' ? new TextEncoder().encode(body) : body.slice();
|
|
190
|
+
size = bytes.byteLength;
|
|
191
|
+
stream = new Response(bytes.buffer as ArrayBuffer).body!;
|
|
192
|
+
} else {
|
|
193
|
+
if (!Number.isSafeInteger(declared) || (declared as number) < 0) throw new CmsError('CMS_VALIDATION', 'A streamed upload needs its size.');
|
|
194
|
+
size = declared as number;
|
|
195
|
+
stream = body;
|
|
196
|
+
}
|
|
197
|
+
return service.upload({ ...metadata, size, body: stream }, { principal, owner });
|
|
198
|
+
},
|
|
199
|
+
get: async id => { await host.allowed('capability:files', 'files'); return storage().get(id); },
|
|
200
|
+
list: async (options = {}) => { await host.allowed('capability:files', 'files'); return storage().list({ ...options, owner }); },
|
|
201
|
+
open: async id => { await host.allowed('capability:files', 'files'); return storage().open(id); },
|
|
202
|
+
update: async (id, patch) => { await host.allowed('capability:files', 'files'); await own(id); return storage().update(id, patch, { principal }); },
|
|
203
|
+
delete: async id => { await host.allowed('capability:files', 'files'); await own(id); await storage().delete(id, { principal }); },
|
|
204
|
+
url: file => mediaUrl(file),
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const mail: PluginMail = {
|
|
208
|
+
send: async message => {
|
|
209
|
+
await host.allowed('capability:mail', 'mail');
|
|
210
|
+
if (!host.mail) throw new CmsError('CMS_UNAVAILABLE', 'The site cannot send email: no sender is configured.');
|
|
211
|
+
if (typeof message?.to !== 'string' || message.to.length > 320 || !EMAIL.test(message.to)) throw new CmsError('CMS_VALIDATION', 'to must be one email address.');
|
|
212
|
+
if (typeof message.subject !== 'string' || !message.subject.trim() || message.subject.length > 200) throw new CmsError('CMS_VALIDATION', 'subject is 1 to 200 characters.');
|
|
213
|
+
if (typeof message.text !== 'string' || !message.text.trim() || message.text.length > MAX_TEXT) throw new CmsError('CMS_VALIDATION', 'text is up to 100 KB.');
|
|
214
|
+
if (message.replyTo !== undefined && (typeof message.replyTo !== 'string' || !EMAIL.test(message.replyTo))) throw new CmsError('CMS_VALIDATION', 'replyTo must be an email address.');
|
|
215
|
+
await host.mail.send({ kind: 'plugin', plugin: host.name, to: message.to, from: host.mailFrom, subject: message.subject.trim(), text: message.text, ...(message.replyTo ? { replyTo: message.replyTo } : {}) });
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const jobs: PluginJobs = {
|
|
220
|
+
handle: (jobName, handler) => {
|
|
221
|
+
if (typeof jobName !== 'string' || !JOB_NAME.test(jobName)) throw new Error(`Plugin ${name} names job ${String(jobName)}; job names are lowercase letters, digits and hyphens.`);
|
|
222
|
+
if (typeof handler !== 'function') throw new Error(`Plugin ${name} handles job ${jobName} without a function.`);
|
|
223
|
+
if (handlers.has(jobName)) throw new Error(`Plugin ${name} handles job ${jobName} twice.`);
|
|
224
|
+
handlers.set(jobName, handler as PluginJobHandler);
|
|
225
|
+
},
|
|
226
|
+
enqueue: async (jobName, payload = null, options = {}) => {
|
|
227
|
+
await host.allowed('capability:jobs', 'jobs');
|
|
228
|
+
// An isolated plugin checked its own handlers before asking.
|
|
229
|
+
const known = typeof jobName === 'string' && (handlers.has(jobName) || (host.jobRunner !== undefined && JOB_NAME.test(jobName)));
|
|
230
|
+
if (!known) throw new CmsError('CMS_NOT_FOUND', `Plugin ${name} has no handler for job ${String(jobName)}.`);
|
|
231
|
+
const json = JSON.stringify(payload ?? null);
|
|
232
|
+
if (json === undefined || json.length > MAX_PAYLOAD) throw new CmsError('CMS_LIMIT', 'A job payload is JSON up to 64 KB.');
|
|
233
|
+
const now = host.now();
|
|
234
|
+
let at = now;
|
|
235
|
+
if (options.at !== undefined) {
|
|
236
|
+
if (!Number.isFinite(options.at)) throw new CmsError('CMS_VALIDATION', 'at must be a time in milliseconds.');
|
|
237
|
+
at = Math.max(now, Math.floor(options.at));
|
|
238
|
+
} else if (options.delayMs !== undefined) {
|
|
239
|
+
if (!Number.isFinite(options.delayMs) || options.delayMs < 0) throw new CmsError('CMS_VALIDATION', 'delayMs must be a non-negative number.');
|
|
240
|
+
at = now + Math.floor(options.delayMs);
|
|
241
|
+
}
|
|
242
|
+
if (at - now > MAX_DELAY) throw new CmsError('CMS_VALIDATION', 'A job runs within a year.');
|
|
243
|
+
if (options.key !== undefined && (typeof options.key !== 'string' || !JOB_KEY.test(options.key))) throw new CmsError('CMS_VALIDATION', 'A job key is 1-120 printable characters without spaces.');
|
|
244
|
+
const eventId = `job:${name}:${jobName}:${options.key ?? crypto.randomUUID()}`;
|
|
245
|
+
const event: PluginJobEvent = { eventId, topic: 'plugin.job', at: now, plugin: name, name: jobName, payload: JSON.parse(json) as unknown, scheduledAt: null, key: options.key ?? null };
|
|
246
|
+
const queued = await host.outbox.enqueue('plugin.job', event, { availableAt: at, ignoreDuplicate: options.key !== undefined });
|
|
247
|
+
if (queued) host.queued();
|
|
248
|
+
return queued;
|
|
249
|
+
},
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const view = (user: CmsUser): PluginUser => Object.freeze({
|
|
253
|
+
id: user.id, email: user.email, name: user.name, slug: user.slug, avatar: user.avatar, status: user.status, roles: Object.freeze([...user.roles]),
|
|
254
|
+
createdAt: user.createdAt, meta: Object.hasOwn(user.meta, metaKey) ? structuredClone(user.meta[metaKey]) : null,
|
|
255
|
+
});
|
|
256
|
+
const assignable = async (): Promise<Set<string>> => {
|
|
257
|
+
const roles = await host.users.roles({ limit: 100 });
|
|
258
|
+
// A reserved name never owns permissions: the manifest check refuses it, and this does not rely on that.
|
|
259
|
+
const own = (permission: string) => !RESERVED_PLUGIN_NAMES.has(name) && (permission.startsWith(`${name}:`) || permission.startsWith(`${name}/`));
|
|
260
|
+
return new Set(roles.filter(role => role.permissions.every(own)).map(role => role.id));
|
|
261
|
+
};
|
|
262
|
+
const users: PluginUsers = {
|
|
263
|
+
get: async id => { await host.allowed('users:read', 'reading members'); const user = await host.users.get(id); return user ? view(user) : null; },
|
|
264
|
+
findByEmail: async email => { await host.allowed('users:read', 'reading members'); const user = await host.users.findByEmail(email); return user ? view(user) : null; },
|
|
265
|
+
list: async (options = {}) => {
|
|
266
|
+
await host.allowed('users:read', 'reading members');
|
|
267
|
+
const page = await host.users.list(options);
|
|
268
|
+
return { items: page.items.map(view), total: page.total };
|
|
269
|
+
},
|
|
270
|
+
create: async input => {
|
|
271
|
+
await host.allowed('users:write', 'creating members');
|
|
272
|
+
const roles = [...new Set(input.roles ?? [])];
|
|
273
|
+
if (roles.length) {
|
|
274
|
+
const allowed = await assignable();
|
|
275
|
+
for (const role of roles) if (!allowed.has(role)) throw new CmsError('CMS_FORBIDDEN', `Plugin ${name} may not assign the role ${role}.`);
|
|
276
|
+
}
|
|
277
|
+
return view(await host.users.create({ email: input.email, name: input.name, roles, ...(input.password !== undefined ? { password: input.password } : {}) }));
|
|
278
|
+
},
|
|
279
|
+
setMeta: async (id, value) => {
|
|
280
|
+
await host.allowed('users:write', 'writing members');
|
|
281
|
+
return host.users.setSystemMeta(id, metaKey, value === undefined ? null : value);
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const prefix = `plugin:${name}:`;
|
|
286
|
+
const cacheKey = (key: string): string => {
|
|
287
|
+
if (typeof key !== 'string' || !CACHE_KEY.test(key)) throw new CmsError('CMS_VALIDATION', 'Cache keys are 1-150 printable characters without spaces.');
|
|
288
|
+
return prefix + key;
|
|
289
|
+
};
|
|
290
|
+
const cache: PluginCache = {
|
|
291
|
+
get: async key => { await host.allowed('capability:cache', 'the cache'); return host.cache.get(cacheKey(key)); },
|
|
292
|
+
set: async (key, value, ttlMs) => { await host.allowed('capability:cache', 'the cache'); await host.cache.set(cacheKey(key), value, ttlMs); },
|
|
293
|
+
delete: async key => { await host.allowed('capability:cache', 'the cache'); await host.cache.delete(cacheKey(key)); },
|
|
294
|
+
remember: async (key, ttlMs, compute) => { await host.allowed('capability:cache', 'the cache'); return (await host.cache.remember(cacheKey(key), ttlMs, compute)).value; },
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const declaredSecrets = new Set(host.manifest.permissions?.secrets ?? []);
|
|
298
|
+
const anySecret = host.manifest.permissions?.unrestricted === true;
|
|
299
|
+
const secrets: PluginSecrets = {
|
|
300
|
+
get: async secret => {
|
|
301
|
+
if (typeof secret !== 'string' || (!anySecret && !declaredSecrets.has(secret))) throw new CmsError('CMS_FORBIDDEN', `Plugin ${name} does not declare the secret ${String(secret)}.`);
|
|
302
|
+
await host.allowed(`secret:${secret}`, `the secret ${secret}`);
|
|
303
|
+
return host.secrets(secret);
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
const ai: PluginAi = {
|
|
308
|
+
complete: async request => {
|
|
309
|
+
await host.allowed('capability:ai', 'the AI provider');
|
|
310
|
+
if (!host.ai) throw new CmsError('CMS_UNAVAILABLE', 'The application has no AI provider.');
|
|
311
|
+
return host.ai.complete(request);
|
|
312
|
+
},
|
|
313
|
+
};
|
|
314
|
+
const payments: PluginPayments = {
|
|
315
|
+
get provider() { return host.payments?.name ?? null; },
|
|
316
|
+
checkout: async input => {
|
|
317
|
+
await host.allowed('capability:payments', 'payments');
|
|
318
|
+
if (!host.payments) throw new CmsError('CMS_UNAVAILABLE', 'The application has no payments provider.');
|
|
319
|
+
return host.payments.checkout(input);
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const runJob = async (event: PluginJobEvent, context: { signal?: AbortSignal }): Promise<void> => {
|
|
324
|
+
// Inactive or revoked: the outbox retries and gives up in its own time.
|
|
325
|
+
await host.allowed('capability:jobs', 'jobs');
|
|
326
|
+
const runner = host.jobRunner;
|
|
327
|
+
const handler = handlers.get(event.name) ?? (runner ? ((payload: unknown, context: PluginJobContext) => runner(event.name, payload, context)) : undefined);
|
|
328
|
+
if (!handler) throw new CmsError('CMS_NOT_FOUND', `Plugin ${name} has no handler for job ${event.name}.`);
|
|
329
|
+
await handler(event.payload, { principal, scheduledAt: event.scheduledAt, ...(context.signal ? { signal: context.signal } : {}) });
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
return Object.freeze({ files, mail, jobs, users, cache, secrets, ai, payments, runJob });
|
|
333
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Cloudflare purger: purges the zone's cache by tag and by URL through the API. The token
|
|
3
|
+
* needs Zone → Cache Purge, and Zone → Zone Read unless the zone ID is given: the zone is then
|
|
4
|
+
* found by the site's host and its parent domains, and remembered for a day in the site's
|
|
5
|
+
* cache. A 429 or 5xx retries through the outbox; another refusal is recorded and dropped.
|
|
6
|
+
*/
|
|
7
|
+
import { PurgeError, type CachePurger, type PurgeContext } from './purge';
|
|
8
|
+
import type { CachePurgeEvent } from '../hooks';
|
|
9
|
+
|
|
10
|
+
export const CLOUDFLARE_API = 'https://api.cloudflare.com/client/v4';
|
|
11
|
+
/** Tags or URLs one purge request takes. */
|
|
12
|
+
const PER_REQUEST = 30;
|
|
13
|
+
const ZONE_TTL = 24 * 60 * 60_000;
|
|
14
|
+
const MISSING_TTL = 10 * 60_000;
|
|
15
|
+
|
|
16
|
+
export interface CloudflarePurgerOptions {
|
|
17
|
+
/** An API token with Zone → Cache Purge, and Zone → Zone Read when `zoneId` is not given. */
|
|
18
|
+
readonly token: string;
|
|
19
|
+
/** The zone to purge; without it the zone is looked up by host. */
|
|
20
|
+
readonly zoneId?: string;
|
|
21
|
+
/** The hosts the site answers at, for the zone lookup. */
|
|
22
|
+
readonly hosts: () => readonly string[] | Promise<readonly string[]>;
|
|
23
|
+
readonly fetch?: typeof globalThis.fetch;
|
|
24
|
+
/** The API's base URL, for tests. */
|
|
25
|
+
readonly api?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The zones a host may belong to, from the host itself up to its registrable parent. */
|
|
29
|
+
export function zoneCandidates(host: string): string[] {
|
|
30
|
+
const name = host.trim().toLowerCase().replace(/:\d+$/, '').replace(/\.$/, '');
|
|
31
|
+
if (!name || name === 'localhost' || name.endsWith('.localhost') || /^[\d.]+$/.test(name) || name.includes(':')) return [];
|
|
32
|
+
const labels = name.split('.');
|
|
33
|
+
const candidates: string[] = [];
|
|
34
|
+
for (let index = 0; index <= labels.length - 2; index++) candidates.push(labels.slice(index).join('.'));
|
|
35
|
+
return candidates;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function cloudflarePurger(options: CloudflarePurgerOptions): CachePurger {
|
|
39
|
+
const api = (options.api ?? CLOUDFLARE_API).replace(/\/+$/, '');
|
|
40
|
+
const send = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
41
|
+
const headers = { authorization: `Bearer ${options.token}`, 'content-type': 'application/json' };
|
|
42
|
+
|
|
43
|
+
const call = async (method: string, path: string, body: unknown, signal: AbortSignal): Promise<{ result?: unknown; errors?: { message?: string }[] }> => {
|
|
44
|
+
let response: Response;
|
|
45
|
+
try {
|
|
46
|
+
response = await send(`${api}${path}`, { method, headers, signal, ...(body === undefined ? {} : { body: JSON.stringify(body) }) });
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new PurgeError(`Cloudflare could not be reached: ${(error as Error).message}`, { retry: true, cause: error });
|
|
49
|
+
}
|
|
50
|
+
const json = await response.json().catch(() => ({})) as { success?: boolean; result?: unknown; errors?: { message?: string }[] };
|
|
51
|
+
if (response.status === 429 || response.status >= 500) throw new PurgeError(`Cloudflare answered ${response.status}.`, { retry: true, status: response.status });
|
|
52
|
+
if (!response.ok || json.success === false) {
|
|
53
|
+
const message = json.errors?.map(error => error.message).filter(Boolean).join(' ') ?? '';
|
|
54
|
+
throw new PurgeError(`Cloudflare refused the request (${response.status})${message ? `: ${message}` : '.'}`, { retry: false, status: response.status });
|
|
55
|
+
}
|
|
56
|
+
return json;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** The zone of a host, from the cache or the API; '' when no zone of the account serves it. */
|
|
60
|
+
const zoneOf = async (host: string, context: PurgeContext): Promise<string> => {
|
|
61
|
+
const key = `cloudflare.zone:${host.toLowerCase()}`;
|
|
62
|
+
const known = await context.cache.get<string>(key);
|
|
63
|
+
if (typeof known === 'string') return known;
|
|
64
|
+
for (const candidate of zoneCandidates(host)) {
|
|
65
|
+
const json = await call('GET', `/zones?name=${encodeURIComponent(candidate)}&per_page=1`, undefined, context.signal);
|
|
66
|
+
const zone = Array.isArray(json.result) ? (json.result[0] as { id?: unknown } | undefined)?.id : undefined;
|
|
67
|
+
if (typeof zone === 'string' && zone) {
|
|
68
|
+
await context.cache.set(key, zone, ZONE_TTL);
|
|
69
|
+
return zone;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
await context.cache.set(key, '', MISSING_TTL);
|
|
73
|
+
return '';
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
name: 'cloudflare',
|
|
78
|
+
async purge(event: CachePurgeEvent, context: PurgeContext): Promise<void> {
|
|
79
|
+
let zones: string[];
|
|
80
|
+
if (options.zoneId) zones = [options.zoneId];
|
|
81
|
+
else {
|
|
82
|
+
const hosts = [...new Set(await options.hosts())];
|
|
83
|
+
zones = [...new Set((await Promise.all(hosts.map(host => zoneOf(host, context)))).filter(Boolean))];
|
|
84
|
+
if (!zones.length) throw new PurgeError(`No Cloudflare zone of this token serves ${hosts.join(', ') || 'the site'}.`, { retry: false });
|
|
85
|
+
}
|
|
86
|
+
for (const zone of zones) {
|
|
87
|
+
for (let index = 0; index < event.tags.length; index += PER_REQUEST) await call('POST', `/zones/${zone}/purge_cache`, { tags: event.tags.slice(index, index + PER_REQUEST) }, context.signal);
|
|
88
|
+
for (let index = 0; index < event.urls.length; index += PER_REQUEST) await call('POST', `/zones/${zone}/purge_cache`, { files: event.urls.slice(index, index + PER_REQUEST) }, context.signal);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|