@brimsites/brimjs 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +110 -0
- package/README.md +22 -2
- package/bin/brim.js +5 -0
- package/dist/cli.js +3802 -0
- package/editor-dist/editor.css +1 -0
- package/editor-dist/editor.js +91 -0
- package/package.json +71 -3
- package/skills/brim-convert/SKILL.md +256 -0
- package/skills/brim-hosting/SKILL.md +221 -0
- package/src/astro-shim.d.ts +5 -0
- package/src/auth/accounts.ts +341 -0
- package/src/auth/password.ts +34 -0
- package/src/auth/permissions.ts +44 -0
- package/src/blocks/accordion/Accordion.astro +48 -0
- package/src/blocks/accordion/accordion.css +63 -0
- package/src/blocks/analytics/Analytics.astro +45 -0
- package/src/blocks/analytics/ConsentBanner.astro +41 -0
- package/src/blocks/analytics/analytics.client.ts +125 -0
- package/src/blocks/analytics/analytics.css +51 -0
- package/src/blocks/analytics/consent.ts +51 -0
- package/src/blocks/button/Button.astro +64 -0
- package/src/blocks/button/button.css +100 -0
- package/src/blocks/carousel/Carousel.astro +140 -0
- package/src/blocks/carousel/carousel.client.ts +153 -0
- package/src/blocks/carousel/carousel.css +181 -0
- package/src/blocks/data/icons.ts +285 -0
- package/src/blocks/data/image-shapes.ts +91 -0
- package/src/blocks/data/social-icons.ts +41 -0
- package/src/blocks/data/social-networks.ts +256 -0
- package/src/blocks/divider/Divider.astro +27 -0
- package/src/blocks/divider/divider.css +17 -0
- package/src/blocks/form/Captcha.astro +27 -0
- package/src/blocks/form/Form.astro +250 -0
- package/src/blocks/form/FormField.astro +138 -0
- package/src/blocks/form/Newsletter.astro +80 -0
- package/src/blocks/form/form.client.ts +339 -0
- package/src/blocks/form/form.css +271 -0
- package/src/blocks/gallery/ImageGallery.astro +126 -0
- package/src/blocks/gallery/gallery.css +43 -0
- package/src/blocks/gallery/lightbox.client.ts +178 -0
- package/src/blocks/gallery/lightbox.css +134 -0
- package/src/blocks/icon/Icon.astro +41 -0
- package/src/blocks/icon/icon.css +16 -0
- package/src/blocks/image/Image.astro +158 -0
- package/src/blocks/image/image.css +42 -0
- package/src/blocks/images.ts +64 -0
- package/src/blocks/in-editor.client.ts +8 -0
- package/src/blocks/listing/Listing.astro +288 -0
- package/src/blocks/listing/ListingCards.astro +48 -0
- package/src/blocks/listing/listing.css +136 -0
- package/src/blocks/listing/query.ts +363 -0
- package/src/blocks/map/Map.astro +75 -0
- package/src/blocks/map/map.css +32 -0
- package/src/blocks/schemas/accordion.ts +118 -0
- package/src/blocks/schemas/button.ts +78 -0
- package/src/blocks/schemas/carousel.ts +133 -0
- package/src/blocks/schemas/divider.ts +29 -0
- package/src/blocks/schemas/form-field-types.ts +29 -0
- package/src/blocks/schemas/form.ts +350 -0
- package/src/blocks/schemas/icon.ts +24 -0
- package/src/blocks/schemas/image-gallery.ts +54 -0
- package/src/blocks/schemas/image.ts +87 -0
- package/src/blocks/schemas/index.ts +33 -0
- package/src/blocks/schemas/listing.ts +14 -0
- package/src/blocks/schemas/map.ts +81 -0
- package/src/blocks/schemas/newsletter.ts +324 -0
- package/src/blocks/schemas/social-media.ts +348 -0
- package/src/blocks/schemas/text.ts +151 -0
- package/src/blocks/schemas/video.ts +57 -0
- package/src/blocks/search/Search.astro +147 -0
- package/src/blocks/search/search.css +69 -0
- package/src/blocks/search/state.ts +28 -0
- package/src/blocks/settings.ts +94 -0
- package/src/blocks/social/SocialMedia.astro +102 -0
- package/src/blocks/social/social-links.ts +16 -0
- package/src/blocks/social/social.css +31 -0
- package/src/blocks/theme.ts +35 -0
- package/src/blocks/types.ts +52 -0
- package/src/blocks/video/Video.astro +90 -0
- package/src/blocks/video/video-source.ts +77 -0
- package/src/blocks/video/video.client.ts +43 -0
- package/src/blocks/video/video.css +29 -0
- package/src/check.ts +1119 -0
- package/src/cli.ts +493 -0
- package/src/components/Blocks.astro +68 -0
- package/src/components/Field.astro +61 -0
- package/src/components/Footer.astro +36 -0
- package/src/components/Link.astro +18 -0
- package/src/components/MobileMenu.astro +77 -0
- package/src/components/Navbar.astro +42 -0
- package/src/components/RichText.astro +32 -0
- package/src/components/Seo.astro +202 -0
- package/src/components/Setting.astro +46 -0
- package/src/components/Shared.astro +17 -0
- package/src/components/Text.astro +91 -0
- package/src/components/blocks.css +86 -0
- package/src/components/index.ts +27 -0
- package/src/components/mobile-menu.client.ts +21 -0
- package/src/components/mobile-menu.css +52 -0
- package/src/components/text.css +48 -0
- package/src/config.ts +413 -0
- package/src/content/block-layout.ts +63 -0
- package/src/content/block-values.ts +48 -0
- package/src/content/content-check.ts +174 -0
- package/src/content/content-types.ts +408 -0
- package/src/content/drafts.ts +115 -0
- package/src/content/feed.ts +237 -0
- package/src/content/field-types.ts +107 -0
- package/src/content/history.ts +140 -0
- package/src/content/ids.ts +50 -0
- package/src/content/image-size.ts +212 -0
- package/src/content/image-sizes.ts +126 -0
- package/src/content/item-fields.ts +110 -0
- package/src/content/item-head.ts +126 -0
- package/src/content/item-image.ts +26 -0
- package/src/content/item-path.ts +122 -0
- package/src/content/items.ts +580 -0
- package/src/content/language-scopes.ts +40 -0
- package/src/content/media.ts +271 -0
- package/src/content/menus.ts +51 -0
- package/src/content/page-templates.ts +52 -0
- package/src/content/redirects.ts +112 -0
- package/src/content/references.ts +165 -0
- package/src/content/rich-text.ts +63 -0
- package/src/content/search.ts +167 -0
- package/src/content/seed.ts +229 -0
- package/src/content/site-settings.ts +111 -0
- package/src/content/sitemap.ts +143 -0
- package/src/content/store.ts +225 -0
- package/src/content/tags.ts +67 -0
- package/src/content/text-default.ts +139 -0
- package/src/content/types.ts +137 -0
- package/src/content/validate.ts +253 -0
- package/src/content/validation-error.ts +2 -0
- package/src/db/cached.ts +140 -0
- package/src/db/database.ts +15 -0
- package/src/db/libsql.ts +29 -0
- package/src/db/schema.ts +189 -0
- package/src/db/url.ts +44 -0
- package/src/editor/client/BlockSettings.tsx +1063 -0
- package/src/editor/client/Chrome.tsx +284 -0
- package/src/editor/client/ContentBrowser.tsx +1299 -0
- package/src/editor/client/Editor.tsx +1756 -0
- package/src/editor/client/HistoryPanel.tsx +114 -0
- package/src/editor/client/ImagesWidget.tsx +113 -0
- package/src/editor/client/Inspector.tsx +127 -0
- package/src/editor/client/ItemForm.tsx +535 -0
- package/src/editor/client/LinkWidget.tsx +440 -0
- package/src/editor/client/ListingPanel.tsx +99 -0
- package/src/editor/client/Login.tsx +338 -0
- package/src/editor/client/Menu.tsx +64 -0
- package/src/editor/client/PageSettings.tsx +270 -0
- package/src/editor/client/RedirectsSettings.tsx +130 -0
- package/src/editor/client/Resize.tsx +204 -0
- package/src/editor/client/Sidebar.tsx +51 -0
- package/src/editor/client/SiteMap.tsx +568 -0
- package/src/editor/client/SiteSettings.tsx +409 -0
- package/src/editor/client/TextTools.tsx +391 -0
- package/src/editor/client/Toolbar.tsx +449 -0
- package/src/editor/client/VideoUpload.tsx +251 -0
- package/src/editor/client/api.ts +417 -0
- package/src/editor/client/brand.ts +20 -0
- package/src/editor/client/connection.ts +98 -0
- package/src/editor/client/context.ts +19 -0
- package/src/editor/client/css.d.ts +2 -0
- package/src/editor/client/editor.css +4656 -0
- package/src/editor/client/fields.tsx +812 -0
- package/src/editor/client/icons.tsx +174 -0
- package/src/editor/client/keys.ts +15 -0
- package/src/editor/client/kinds.ts +289 -0
- package/src/editor/client/languages.ts +32 -0
- package/src/editor/client/main.tsx +100 -0
- package/src/editor/client/responses.ts +26 -0
- package/src/editor/client/rich-text.ts +326 -0
- package/src/editor/client/site-tree.ts +239 -0
- package/src/editor/client/theme.ts +55 -0
- package/src/editor/client/tsconfig.json +18 -0
- package/src/editor/client/tus.ts +137 -0
- package/src/editor/client/use-rect.ts +50 -0
- package/src/editor/client/widgets.tsx +1218 -0
- package/src/forms/captcha.ts +83 -0
- package/src/forms/define.ts +166 -0
- package/src/forms/fields.ts +197 -0
- package/src/forms/file-types.ts +108 -0
- package/src/forms/files.ts +216 -0
- package/src/forms/form-page.ts +9 -0
- package/src/forms/handler.ts +622 -0
- package/src/forms/spec.ts +89 -0
- package/src/forms/store.ts +96 -0
- package/src/forms/work.ts +50 -0
- package/src/icons/library.ts +144 -0
- package/src/icons/load.ts +63 -0
- package/src/index.ts +99 -0
- package/src/init.ts +257 -0
- package/src/integration.ts +469 -0
- package/src/mail/mailer.ts +197 -0
- package/src/mail/notify.ts +272 -0
- package/src/mail/settings.ts +173 -0
- package/src/mail/templates.ts +191 -0
- package/src/mcp/page-nodes.ts +209 -0
- package/src/mcp/server.ts +111 -0
- package/src/mcp/tools.ts +637 -0
- package/src/runtime/TemplatePage.astro +39 -0
- package/src/runtime/TemplatePageStatic.astro +13 -0
- package/src/runtime/admin-route.ts +80 -0
- package/src/runtime/connect.ts +53 -0
- package/src/runtime/content.ts +188 -0
- package/src/runtime/context.ts +131 -0
- package/src/runtime/form-files.ts +18 -0
- package/src/runtime/forms-route.ts +33 -0
- package/src/runtime/forms.ts +28 -0
- package/src/runtime/icons.ts +5 -0
- package/src/runtime/media.ts +175 -0
- package/src/runtime/middleware.ts +186 -0
- package/src/runtime/nav.ts +13 -0
- package/src/runtime/query-reads.ts +49 -0
- package/src/runtime/query-store.ts +16 -0
- package/src/runtime/request-state.ts +69 -0
- package/src/runtime/robots.ts +29 -0
- package/src/runtime/rss-static.ts +16 -0
- package/src/runtime/rss.ts +29 -0
- package/src/runtime/server.ts +29 -0
- package/src/runtime/sitemap.ts +34 -0
- package/src/runtime/template-paths.ts +34 -0
- package/src/runtime/uploads-route.ts +13 -0
- package/src/seed-cli.ts +253 -0
- package/src/server/auth.ts +31 -0
- package/src/server/fetch-image.ts +219 -0
- package/src/server/handler.ts +2106 -0
- package/src/server/http.ts +162 -0
- package/src/server/locales.ts +73 -0
- package/src/server/node.ts +57 -0
- package/src/server/query-params.ts +249 -0
- package/src/server/rate-limit.ts +72 -0
- package/src/server/video-host.ts +162 -0
- package/src/starter.ts +70 -0
- package/src/version.ts +4 -0
- package/src/virtual.d.ts +65 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
export type TextTag =
|
|
2
|
+
"p" | "span" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "blockquote" | "label" | "strong" | "em" | "small";
|
|
3
|
+
|
|
4
|
+
export type InlineMark = "strong" | "em" | "u" | "s" | "sub" | "sup";
|
|
5
|
+
export type TextAlign = "left" | "center" | "right" | "justify";
|
|
6
|
+
|
|
7
|
+
/** Inline formatting the owner can apply to text. Stored as a tree, never as HTML. */
|
|
8
|
+
export type InlineNode =
|
|
9
|
+
| string
|
|
10
|
+
| { tag: InlineMark | "br"; children: InlineNode[] }
|
|
11
|
+
| { tag: "a"; href: string; children: InlineNode[] }
|
|
12
|
+
| { tag: "span"; color?: string; background?: string; children: InlineNode[] };
|
|
13
|
+
|
|
14
|
+
/** One element of text: a heading, a paragraph, a label. */
|
|
15
|
+
export interface TextValue {
|
|
16
|
+
/** The plain text, always present. */
|
|
17
|
+
text: string;
|
|
18
|
+
/** Present when the owner formatted the text; `text` is then its plain reading. */
|
|
19
|
+
rich?: InlineNode[];
|
|
20
|
+
/** Present when the owner changed the element, e.g. from a paragraph to a heading. */
|
|
21
|
+
as?: TextTag;
|
|
22
|
+
align?: TextAlign;
|
|
23
|
+
/** Presentation choices of the text block: drop cap, quote style and the like. */
|
|
24
|
+
options?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type RichTextBlock =
|
|
28
|
+
| { tag: "p" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "blockquote"; align?: TextAlign; children: InlineNode[] }
|
|
29
|
+
| { tag: "ul" | "ol"; items: InlineNode[][] };
|
|
30
|
+
|
|
31
|
+
/** Flowing content: several paragraphs, headings and lists the owner writes freely. */
|
|
32
|
+
export interface RichTextValue {
|
|
33
|
+
blocks: RichTextBlock[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ImageValue {
|
|
37
|
+
src: string;
|
|
38
|
+
alt: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface LinkValue {
|
|
42
|
+
href: string;
|
|
43
|
+
/** When absent, the label written in source is kept. */
|
|
44
|
+
label?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A plain link of a list, as the footer keeps them. */
|
|
48
|
+
export interface NavLink {
|
|
49
|
+
label: string;
|
|
50
|
+
href: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A link of the navbar. `children` makes it a group, e.g. "Products" with a dropdown of categories, and `image`
|
|
55
|
+
* gives any link a picture, so the site's markup can draw a megamenu. Both are optional: a value saved before they
|
|
56
|
+
* existed is a flat list of links and stays valid. Children nest two levels at most (a column of a megamenu and
|
|
57
|
+
* its links).
|
|
58
|
+
*/
|
|
59
|
+
export interface NavItem extends NavLink {
|
|
60
|
+
image?: ImageValue;
|
|
61
|
+
children?: NavItem[];
|
|
62
|
+
/**
|
|
63
|
+
* A content type's key: the link's sub-links are that type's items (title, page, first picture), in the owner's
|
|
64
|
+
* order for an `ordered` type, so a category added in the editor shows in the menu. `children` is then ignored.
|
|
65
|
+
*/
|
|
66
|
+
from?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface NavbarValue {
|
|
70
|
+
logo?: ImageValue;
|
|
71
|
+
logoText?: string;
|
|
72
|
+
items: NavItem[];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface FooterGroup {
|
|
76
|
+
title?: string;
|
|
77
|
+
items: NavLink[];
|
|
78
|
+
/** A content type's key: the group lists that type's items, as `from` does for a navbar link. */
|
|
79
|
+
from?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface FooterValue {
|
|
83
|
+
logo?: ImageValue;
|
|
84
|
+
logoText?: string;
|
|
85
|
+
groups: FooterGroup[];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Every primitive and the shape of the value the owner can edit. Blocks keep a settings record instead. */
|
|
89
|
+
export interface NodeValues {
|
|
90
|
+
text: TextValue;
|
|
91
|
+
richtext: RichTextValue;
|
|
92
|
+
link: LinkValue;
|
|
93
|
+
navbar: NavbarValue;
|
|
94
|
+
footer: FooterValue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type NodeKind = keyof NodeValues;
|
|
98
|
+
|
|
99
|
+
/** A node as saved. `kind` is a primitive (`text`, `image`…) or a block type (`button`, `carousel`…). */
|
|
100
|
+
export interface StoredNode {
|
|
101
|
+
scope: string;
|
|
102
|
+
nodeId: string;
|
|
103
|
+
kind: string;
|
|
104
|
+
value: unknown;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Pages and content items are either visible on the public site or not. Media has no visibility. */
|
|
108
|
+
export type Visibility = "private" | "published";
|
|
109
|
+
|
|
110
|
+
/** What search engines and link previews show for a page. Anything left out falls back to the page's code. */
|
|
111
|
+
export interface SeoSettings {
|
|
112
|
+
title?: string;
|
|
113
|
+
description?: string;
|
|
114
|
+
/** Image shown when the page is shared. */
|
|
115
|
+
image?: string;
|
|
116
|
+
/** Title and description of the page's shared links (Open Graph, X); empty uses `title` and `description`. */
|
|
117
|
+
shareTitle?: string;
|
|
118
|
+
shareDescription?: string;
|
|
119
|
+
/**
|
|
120
|
+
* The page's preferred address, when the same content is at several: a path of this site (`/pricing`) or a full
|
|
121
|
+
* address. Empty is the page's own address. A page pointing elsewhere is left out of the sitemap.
|
|
122
|
+
*/
|
|
123
|
+
canonical?: string;
|
|
124
|
+
/** Asks search engines not to list the page. */
|
|
125
|
+
noindex?: boolean;
|
|
126
|
+
/** Asks search engines not to follow the page's links. */
|
|
127
|
+
nofollow?: boolean;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface PageRecord {
|
|
131
|
+
path: string;
|
|
132
|
+
visibility: Visibility;
|
|
133
|
+
seo: SeoSettings;
|
|
134
|
+
/** Set on pages the owner added from a template; pages written in code have none. */
|
|
135
|
+
template?: string;
|
|
136
|
+
title?: string;
|
|
137
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { text as textBlock } from "../blocks/schemas/text.ts";
|
|
2
|
+
import { parseSettings } from "../blocks/settings.ts";
|
|
3
|
+
import { ValidationError } from "./validation-error.ts";
|
|
4
|
+
import { TEXT_TAGS } from "./rich-text.ts";
|
|
5
|
+
import type {
|
|
6
|
+
FooterGroup,
|
|
7
|
+
FooterValue,
|
|
8
|
+
ImageValue,
|
|
9
|
+
InlineNode,
|
|
10
|
+
LinkValue,
|
|
11
|
+
NavItem,
|
|
12
|
+
NavLink,
|
|
13
|
+
NavbarValue,
|
|
14
|
+
NodeKind,
|
|
15
|
+
NodeValues,
|
|
16
|
+
RichTextBlock,
|
|
17
|
+
RichTextValue,
|
|
18
|
+
SeoSettings,
|
|
19
|
+
TextAlign,
|
|
20
|
+
TextValue,
|
|
21
|
+
} from "./types.ts";
|
|
22
|
+
|
|
23
|
+
export { ValidationError };
|
|
24
|
+
|
|
25
|
+
const MAX_TEXT = 20_000;
|
|
26
|
+
const MAX_LABEL = 200;
|
|
27
|
+
const MAX_URL = 2_000;
|
|
28
|
+
const MAX_NAV_ITEMS = 50;
|
|
29
|
+
/** How many levels of sub-links a navbar link can have: a megamenu's columns, and the links of each column. */
|
|
30
|
+
const MAX_NAV_DEPTH = 2;
|
|
31
|
+
const MAX_NAV_LINKS = 300;
|
|
32
|
+
const MAX_FOOTER_GROUPS = 12;
|
|
33
|
+
const MAX_INLINE_NODES = 2_000;
|
|
34
|
+
const MAX_INLINE_DEPTH = 8;
|
|
35
|
+
const MAX_BLOCKS = 500;
|
|
36
|
+
|
|
37
|
+
const INLINE_MARKS = ["strong", "em", "u", "s", "sub", "sup", "br"] as const;
|
|
38
|
+
const BLOCK_TAGS = ["p", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote"] as const;
|
|
39
|
+
const ALIGNMENTS: readonly TextAlign[] = ["left", "center", "right", "justify"];
|
|
40
|
+
const HEX_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
41
|
+
|
|
42
|
+
const LINK_SCHEMES = ["http:", "https:", "mailto:", "tel:"];
|
|
43
|
+
const IMAGE_SCHEMES = ["http:", "https:"];
|
|
44
|
+
const WEB_SCHEMES = ["http:", "https:"];
|
|
45
|
+
const MAX_SEO_DESCRIPTION = 500;
|
|
46
|
+
|
|
47
|
+
/** Turns untrusted input into a node value, keeping only known fields. Throws `ValidationError`. */
|
|
48
|
+
export function parseNodeValue<K extends NodeKind>(kind: K, input: unknown): NodeValues[K] {
|
|
49
|
+
const parsers: { [Kind in NodeKind]: (value: Record<string, unknown>) => NodeValues[Kind] } = {
|
|
50
|
+
text: parseText,
|
|
51
|
+
richtext: parseRichText,
|
|
52
|
+
link: parseLink,
|
|
53
|
+
navbar: parseNavbar,
|
|
54
|
+
footer: parseFooter,
|
|
55
|
+
};
|
|
56
|
+
if (!Object.hasOwn(parsers, kind)) throw new ValidationError(`Unknown node kind "${kind}".`);
|
|
57
|
+
return parsers[kind](record(input, "value"));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Empty fields are dropped, so the page falls back to what its code says. */
|
|
61
|
+
export function parseSeo(input: unknown): SeoSettings {
|
|
62
|
+
const value = record(input, "seo");
|
|
63
|
+
const text = (name: string, max: number): string =>
|
|
64
|
+
value[name] === undefined ? "" : string(value[name], name, max).trim();
|
|
65
|
+
const flag = (name: string): boolean => {
|
|
66
|
+
if (value[name] !== undefined && typeof value[name] !== "boolean") {
|
|
67
|
+
throw new ValidationError(`"${name}" must be true or false.`);
|
|
68
|
+
}
|
|
69
|
+
return value[name] === true;
|
|
70
|
+
};
|
|
71
|
+
const title = text("title", MAX_LABEL);
|
|
72
|
+
const description = text("description", MAX_SEO_DESCRIPTION);
|
|
73
|
+
const image = text("image", MAX_URL);
|
|
74
|
+
const shareTitle = text("shareTitle", MAX_LABEL);
|
|
75
|
+
const shareDescription = text("shareDescription", MAX_SEO_DESCRIPTION);
|
|
76
|
+
const canonical = text("canonical", MAX_URL);
|
|
77
|
+
if (canonical.startsWith("#")) throw new ValidationError('"canonical" must be a path of the site or a full address.');
|
|
78
|
+
return {
|
|
79
|
+
...(title && { title }),
|
|
80
|
+
...(description && { description }),
|
|
81
|
+
...(image && { image: url(image, "image", IMAGE_SCHEMES) }),
|
|
82
|
+
...(shareTitle && { shareTitle }),
|
|
83
|
+
...(shareDescription && { shareDescription }),
|
|
84
|
+
...(canonical && { canonical: url(canonical, "canonical", WEB_SCHEMES) }),
|
|
85
|
+
...(flag("noindex") && { noindex: true }),
|
|
86
|
+
...(flag("nofollow") && { nofollow: true }),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function parseText(value: Record<string, unknown>): TextValue {
|
|
91
|
+
const budget = { nodes: MAX_INLINE_NODES };
|
|
92
|
+
return {
|
|
93
|
+
text: string(value.text, "text", MAX_TEXT),
|
|
94
|
+
...(value.rich !== undefined && { rich: inlineNodes(value.rich, 0, budget) }),
|
|
95
|
+
...(value.as !== undefined && { as: oneOf(value.as, TEXT_TAGS, "as") }),
|
|
96
|
+
...(value.align !== undefined && { align: oneOf(value.align, ALIGNMENTS, "align") }),
|
|
97
|
+
...(value.options !== undefined && { options: parseSettings(textBlock, value.options) }),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function parseRichText(value: Record<string, unknown>): RichTextValue {
|
|
102
|
+
const budget = { nodes: MAX_INLINE_NODES };
|
|
103
|
+
const blocks = list(value.blocks, "blocks", MAX_BLOCKS).map((entry): RichTextBlock => {
|
|
104
|
+
const block = record(entry, "blocks");
|
|
105
|
+
if (block.tag === "ul" || block.tag === "ol") {
|
|
106
|
+
const items = list(block.items, "items", MAX_BLOCKS).map((item) => inlineNodes(item, 0, budget));
|
|
107
|
+
return { tag: block.tag, items };
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
tag: oneOf(block.tag, BLOCK_TAGS, "tag"),
|
|
111
|
+
...(block.align !== undefined && { align: oneOf(block.align, ALIGNMENTS, "align") }),
|
|
112
|
+
children: inlineNodes(block.children ?? [], 0, budget),
|
|
113
|
+
};
|
|
114
|
+
});
|
|
115
|
+
return { blocks };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function inlineNodes(input: unknown, depth: number, budget: { nodes: number }): InlineNode[] {
|
|
119
|
+
if (depth > MAX_INLINE_DEPTH) throw new ValidationError("The text formatting is nested too deeply.");
|
|
120
|
+
return list(input, "rich", MAX_INLINE_NODES).map((entry): InlineNode => {
|
|
121
|
+
if (--budget.nodes < 0) throw new ValidationError("The text has too much formatting.");
|
|
122
|
+
if (typeof entry === "string") return string(entry, "rich", MAX_TEXT);
|
|
123
|
+
|
|
124
|
+
const node = record(entry, "rich");
|
|
125
|
+
const children = inlineNodes(node.children ?? [], depth + 1, budget);
|
|
126
|
+
if (node.tag === "a") return { tag: "a", href: url(node.href, "href", LINK_SCHEMES), children };
|
|
127
|
+
if (node.tag === "span") {
|
|
128
|
+
return {
|
|
129
|
+
tag: "span",
|
|
130
|
+
...(node.color !== undefined && { color: color(node.color, "color") }),
|
|
131
|
+
...(node.background !== undefined && { background: color(node.background, "background") }),
|
|
132
|
+
children,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return { tag: oneOf(node.tag, INLINE_MARKS, "tag"), children };
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function oneOf<T extends string>(input: unknown, allowed: readonly T[], name: string): T {
|
|
140
|
+
if (typeof input !== "string" || !allowed.includes(input as T)) {
|
|
141
|
+
throw new ValidationError(`"${name}" is not an allowed value.`);
|
|
142
|
+
}
|
|
143
|
+
return input as T;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Colors are stored as hex, so a saved value can never smuggle other CSS in. */
|
|
147
|
+
function color(input: unknown, name: string): string {
|
|
148
|
+
if (typeof input !== "string" || !HEX_COLOR.test(input)) throw new ValidationError(`"${name}" must be a hex color.`);
|
|
149
|
+
return input.toLowerCase();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function parseImage(value: Record<string, unknown>): ImageValue {
|
|
153
|
+
return { src: url(value.src, "src", IMAGE_SCHEMES), alt: string(value.alt, "alt", MAX_LABEL * 2) };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function parseLink(value: Record<string, unknown>): LinkValue {
|
|
157
|
+
const href = url(value.href, "href", LINK_SCHEMES);
|
|
158
|
+
return value.label === undefined ? { href } : { href, label: string(value.label, "label", MAX_LABEL) };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function parseNavbar(value: Record<string, unknown>): NavbarValue {
|
|
162
|
+
const budget = { links: MAX_NAV_LINKS };
|
|
163
|
+
return { ...brand(value), items: navItems(value.items, "items", 0, budget) };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function parseFooter(value: Record<string, unknown>): FooterValue {
|
|
167
|
+
const groups = list(value.groups, "groups", MAX_FOOTER_GROUPS).map((entry, index): FooterGroup => {
|
|
168
|
+
const group = record(entry, `groups[${index}]`);
|
|
169
|
+
const items = navLinks(group.items ?? [], `groups[${index}].items`);
|
|
170
|
+
return {
|
|
171
|
+
...(group.title !== undefined && { title: string(group.title, "title", MAX_LABEL) }),
|
|
172
|
+
items,
|
|
173
|
+
...(group.from !== undefined && { from: typeKey(group.from, `groups[${index}].from`) }),
|
|
174
|
+
};
|
|
175
|
+
});
|
|
176
|
+
return { ...brand(value), groups };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function brand(value: Record<string, unknown>): { logo?: ImageValue; logoText?: string } {
|
|
180
|
+
return {
|
|
181
|
+
...(value.logo !== undefined && { logo: parseImage(record(value.logo, "logo")) }),
|
|
182
|
+
...(value.logoText !== undefined && { logoText: string(value.logoText, "logoText", MAX_LABEL) }),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function navLinks(input: unknown, name: string): NavLink[] {
|
|
187
|
+
return list(input, name, MAX_NAV_ITEMS).map((entry, index) => navLink(record(entry, `${name}[${index}]`)));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function navLink(item: Record<string, unknown>): NavLink {
|
|
191
|
+
return { label: string(item.label, "label", MAX_LABEL), href: url(item.href, "href", LINK_SCHEMES) };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The navbar's links with their sub-links and pictures. `budget` counts every link of the navbar, at any depth, so
|
|
196
|
+
* nesting cannot multiply the per-list limit into a value too large to draw. An empty `children` list is dropped,
|
|
197
|
+
* so a link without sub-links is stored as it was before groups existed.
|
|
198
|
+
*/
|
|
199
|
+
function navItems(input: unknown, name: string, depth: number, budget: { links: number }): NavItem[] {
|
|
200
|
+
const entries = list(input, name, MAX_NAV_ITEMS);
|
|
201
|
+
if (depth > MAX_NAV_DEPTH && entries.length > 0) {
|
|
202
|
+
throw new ValidationError(`"${name}" is nested too deeply: links can have sub-links ${MAX_NAV_DEPTH} levels deep.`);
|
|
203
|
+
}
|
|
204
|
+
return entries.map((entry, index): NavItem => {
|
|
205
|
+
if (--budget.links < 0) throw new ValidationError(`The navigation can hold at most ${MAX_NAV_LINKS} links.`);
|
|
206
|
+
const path = `${name}[${index}]`;
|
|
207
|
+
const item = record(entry, path);
|
|
208
|
+
const children = item.children === undefined ? [] : navItems(item.children, `${path}.children`, depth + 1, budget);
|
|
209
|
+
return {
|
|
210
|
+
...navLink(item),
|
|
211
|
+
...(item.image !== undefined && { image: parseImage(record(item.image, `${path}.image`)) }),
|
|
212
|
+
...(children.length > 0 && { children }),
|
|
213
|
+
...(item.from !== undefined && depth < MAX_NAV_DEPTH && { from: typeKey(item.from, `${path}.from`) }),
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** A content type's key, as `from` names the type a menu lists. Whether the site declares it is checked when drawn. */
|
|
219
|
+
function typeKey(input: unknown, name: string): string {
|
|
220
|
+
if (typeof input !== "string" || !/^[a-z][a-z0-9_-]{0,63}$/.test(input)) {
|
|
221
|
+
throw new ValidationError(`"${name}" must be the key of a content type.`);
|
|
222
|
+
}
|
|
223
|
+
return input;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function record(input: unknown, name: string): Record<string, unknown> {
|
|
227
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
228
|
+
throw new ValidationError(`"${name}" must be an object.`);
|
|
229
|
+
}
|
|
230
|
+
return input as Record<string, unknown>;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function list(input: unknown, name: string, max: number): unknown[] {
|
|
234
|
+
if (!Array.isArray(input)) throw new ValidationError(`"${name}" must be a list.`);
|
|
235
|
+
if (input.length > max) throw new ValidationError(`"${name}" can hold at most ${max} entries.`);
|
|
236
|
+
return input;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function string(input: unknown, name: string, max: number): string {
|
|
240
|
+
if (typeof input !== "string") throw new ValidationError(`"${name}" must be text.`);
|
|
241
|
+
if (input.length > max) throw new ValidationError(`"${name}" is longer than ${max} characters.`);
|
|
242
|
+
return input;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Accepts site-relative paths, in-page anchors and the listed schemes. Rejects `javascript:` and the like. */
|
|
246
|
+
function url(input: unknown, name: string, schemes: string[]): string {
|
|
247
|
+
const value = string(input, name, MAX_URL).trim();
|
|
248
|
+
const relative = (value.startsWith("/") && !value.startsWith("//")) || value.startsWith("#");
|
|
249
|
+
if (relative) return value;
|
|
250
|
+
const scheme = URL.canParse(value) ? new URL(value).protocol : "";
|
|
251
|
+
if (!schemes.includes(scheme)) throw new ValidationError(`"${name}" is not an allowed address.`);
|
|
252
|
+
return value;
|
|
253
|
+
}
|
package/src/db/cached.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import type { Database, Row, SqlValue, Statement } from "./database.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The tables of the owner's content: what pages are drawn from. Every write to one of them, by this server or any
|
|
6
|
+
* other process (the CLI, a restore, a second server), moves `brim_changes.version` forward through the triggers
|
|
7
|
+
* `ensureChangeTracking` adds. Accounts, sessions, rate limits, form responses, tokens and history are not content:
|
|
8
|
+
* reading them is never kept, writing them never forgets what is kept.
|
|
9
|
+
*/
|
|
10
|
+
const CONTENT_TABLES = new Set(["brim_nodes", "brim_pages", "brim_items", "brim_meta", "brim_media", "brim_drafts"]);
|
|
11
|
+
|
|
12
|
+
const TABLE = /\bbrim_\w+/g;
|
|
13
|
+
const READ = /^\s*SELECT\b/i;
|
|
14
|
+
const WRITE = /^\s*(INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|ALTER|WITH)\b/i;
|
|
15
|
+
/** Beyond this many different reads, what is kept is dropped and read again: a bound on memory, not a cache policy. */
|
|
16
|
+
const MAX_KEPT = 5000;
|
|
17
|
+
|
|
18
|
+
const VERSION_SQL = "SELECT version FROM brim_changes WHERE id = 1";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Adds `brim_changes` and the triggers that move its version on every write to a content table. Idempotent and in
|
|
22
|
+
* one round trip, so it runs at every start instead of as a numbered migration.
|
|
23
|
+
*/
|
|
24
|
+
export async function ensureChangeTracking(db: Database): Promise<void> {
|
|
25
|
+
const triggers = [...CONTENT_TABLES].flatMap((table) =>
|
|
26
|
+
(["INSERT", "UPDATE", "DELETE"] as const).map((event) => ({
|
|
27
|
+
sql: `CREATE TRIGGER IF NOT EXISTS ${table}_changed_${event.toLowerCase()} AFTER ${event} ON ${table}
|
|
28
|
+
BEGIN UPDATE brim_changes SET version = version + 1 WHERE id = 1; END`,
|
|
29
|
+
})),
|
|
30
|
+
);
|
|
31
|
+
await db.batch([
|
|
32
|
+
{
|
|
33
|
+
sql: "CREATE TABLE IF NOT EXISTS brim_changes (id INTEGER PRIMARY KEY CHECK (id = 1), version INTEGER NOT NULL)",
|
|
34
|
+
},
|
|
35
|
+
{ sql: "INSERT OR IGNORE INTO brim_changes (id, version) VALUES (1, 0)" },
|
|
36
|
+
...triggers,
|
|
37
|
+
]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** One visit: the content's version, asked once, and whether something was written since. */
|
|
41
|
+
interface ReadScope {
|
|
42
|
+
version?: Promise<number | undefined>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const scopes = new AsyncLocalStorage<ReadScope>();
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Runs `run` (one visit's page) with the content kept between visits: the first read of content asks the database
|
|
49
|
+
* for its version, one small query, and while it is the one last seen every other read of content is answered from
|
|
50
|
+
* memory. Outside it (the editor's API, the CLI, a build) every read goes to the database.
|
|
51
|
+
*/
|
|
52
|
+
export function withKeptReads<T>(run: () => T, visit?: object): T {
|
|
53
|
+
// The 404 page Astro draws after a page answered 404 is the same visit: it asks for the version only once.
|
|
54
|
+
let scope = visit ? visits.get(visit) : undefined;
|
|
55
|
+
if (!scope) {
|
|
56
|
+
scope = {};
|
|
57
|
+
if (visit) visits.set(visit, scope);
|
|
58
|
+
}
|
|
59
|
+
return scopes.run(scope, run);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const visits = new WeakMap<object, ReadScope>();
|
|
63
|
+
|
|
64
|
+
function tablesOf(sql: string): string[] {
|
|
65
|
+
return sql.match(TABLE) ?? [];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function readsOnlyContent(sql: string): boolean {
|
|
69
|
+
if (!READ.test(sql)) return false;
|
|
70
|
+
const tables = tablesOf(sql);
|
|
71
|
+
return tables.length > 0 && tables.every((table) => CONTENT_TABLES.has(table));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function writesContent(sql: string): boolean {
|
|
75
|
+
return WRITE.test(sql) && tablesOf(sql).some((table) => CONTENT_TABLES.has(table));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const copy = (rows: Row[]): Row[] => rows.map((row) => ({ ...row }));
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* `db`, keeping what pages read of the content between visits (see `withKeptReads`). On a remote database each
|
|
82
|
+
* query is a round trip of 100 ms or more, and a page asks for its nodes, its record, the site's redirects and
|
|
83
|
+
* settings and the lists it shows: kept, a visit costs one round trip however many parts the page has.
|
|
84
|
+
*/
|
|
85
|
+
export function keepingReads(db: Database): Database {
|
|
86
|
+
let keptVersion: number | undefined;
|
|
87
|
+
const kept = new Map<string, Promise<Row[]>>();
|
|
88
|
+
|
|
89
|
+
const forget = (): void => {
|
|
90
|
+
kept.clear();
|
|
91
|
+
keptVersion = undefined;
|
|
92
|
+
const scope = scopes.getStore();
|
|
93
|
+
if (scope) delete scope.version;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const version = (scope: ReadScope): Promise<number | undefined> => {
|
|
97
|
+
scope.version ??= db.query(VERSION_SQL).then(
|
|
98
|
+
(rows) => (rows[0] ? Number(rows[0].version) : undefined),
|
|
99
|
+
// A database without the table (made before it, and not writable to add it): nothing is kept.
|
|
100
|
+
() => undefined,
|
|
101
|
+
);
|
|
102
|
+
return scope.version;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
async query(sql: string, args: SqlValue[] = []): Promise<Row[]> {
|
|
107
|
+
const scope = scopes.getStore();
|
|
108
|
+
if (!scope || !readsOnlyContent(sql)) {
|
|
109
|
+
const rows = await db.query(sql, args);
|
|
110
|
+
if (writesContent(sql)) forget();
|
|
111
|
+
return rows;
|
|
112
|
+
}
|
|
113
|
+
const current = await version(scope);
|
|
114
|
+
if (current === undefined) return db.query(sql, args);
|
|
115
|
+
if (current !== keptVersion) {
|
|
116
|
+
kept.clear();
|
|
117
|
+
keptVersion = current;
|
|
118
|
+
}
|
|
119
|
+
if (kept.size >= MAX_KEPT) kept.clear();
|
|
120
|
+
const key = `${sql}\u0000${JSON.stringify(args)}`;
|
|
121
|
+
let reading = kept.get(key);
|
|
122
|
+
if (!reading) {
|
|
123
|
+
reading = db.query(sql, args);
|
|
124
|
+
kept.set(key, reading);
|
|
125
|
+
reading.catch(() => {
|
|
126
|
+
if (kept.get(key) === reading) kept.delete(key);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return copy(await reading);
|
|
130
|
+
},
|
|
131
|
+
async batch(statements: Statement[]): Promise<void> {
|
|
132
|
+
await db.batch(statements);
|
|
133
|
+
if (statements.some((statement) => writesContent(statement.sql))) forget();
|
|
134
|
+
},
|
|
135
|
+
close(): void {
|
|
136
|
+
kept.clear();
|
|
137
|
+
db.close();
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type SqlValue = string | number | null;
|
|
2
|
+
export type Row = Record<string, SqlValue>;
|
|
3
|
+
|
|
4
|
+
export interface Statement {
|
|
5
|
+
sql: string;
|
|
6
|
+
args?: SqlValue[];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** The only thing Brim needs from a database. SQLite locally, any SQLite-compatible host in production. */
|
|
10
|
+
export interface Database {
|
|
11
|
+
query(sql: string, args?: SqlValue[]): Promise<Row[]>;
|
|
12
|
+
/** Runs all statements in one transaction. */
|
|
13
|
+
batch(statements: Statement[]): Promise<void>;
|
|
14
|
+
close(): void;
|
|
15
|
+
}
|
package/src/db/libsql.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createClient } from "@libsql/client";
|
|
2
|
+
import type { Database, Row, SqlValue, Statement } from "./database.ts";
|
|
3
|
+
import { assertSecureDatabaseUrl } from "./url.ts";
|
|
4
|
+
|
|
5
|
+
/** Opens a SQLite file (`file:./data.db`), an in-memory database (`:memory:`) or a remote libSQL URL. */
|
|
6
|
+
export function openDatabase(url: string, authToken?: string): Database {
|
|
7
|
+
assertSecureDatabaseUrl(url);
|
|
8
|
+
const client = createClient(authToken ? { url, authToken } : { url });
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
async query(sql: string, args: SqlValue[] = []): Promise<Row[]> {
|
|
12
|
+
const result = await client.execute({ sql, args });
|
|
13
|
+
return result.rows.map((row) => {
|
|
14
|
+
const record: Row = {};
|
|
15
|
+
for (const column of result.columns) record[column] = row[column] as SqlValue;
|
|
16
|
+
return record;
|
|
17
|
+
});
|
|
18
|
+
},
|
|
19
|
+
async batch(statements: Statement[]): Promise<void> {
|
|
20
|
+
await client.batch(
|
|
21
|
+
statements.map(({ sql, args = [] }) => ({ sql, args })),
|
|
22
|
+
"write",
|
|
23
|
+
);
|
|
24
|
+
},
|
|
25
|
+
close(): void {
|
|
26
|
+
client.close();
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|