@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,89 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import type { FormField } from "./fields.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a form on a page asks for. The page signs it when it is rendered, so the server can check a
|
|
6
|
+
* submission against the form as its author made it, without trusting anything the visitor sends.
|
|
7
|
+
*/
|
|
8
|
+
export interface FormSpec {
|
|
9
|
+
/** Which form this is: the page it is on and its node id. */
|
|
10
|
+
form: string;
|
|
11
|
+
/** Name shown to the owner in the list of responses. */
|
|
12
|
+
title: string;
|
|
13
|
+
fields: FormField[];
|
|
14
|
+
consentRequired: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* More addresses the form's author wants told about each message (the form's Notification Email). In the page it
|
|
17
|
+
* is sealed (`sealAddress`): the spec is only signed, so anything else in it can be read in the page's source.
|
|
18
|
+
*/
|
|
19
|
+
notify?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The confirmation the visitor is emailed once their message is kept, when the owner turned it on: which email
|
|
22
|
+
* field of the form names the recipient, and the subject and message the owner wrote. Signed with the rest, so a
|
|
23
|
+
* visitor can choose neither the words nor a field that was not checked as an email address.
|
|
24
|
+
*/
|
|
25
|
+
confirm?: FormConfirmation;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface FormConfirmation {
|
|
29
|
+
/** The `name` of one of the form's `email` fields. */
|
|
30
|
+
field: string;
|
|
31
|
+
subject: string;
|
|
32
|
+
message: string;
|
|
33
|
+
/** The email also lists what the visitor answered, as text and shortened. Off unless the owner asks. */
|
|
34
|
+
answers?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function encodeSpec(spec: FormSpec): string {
|
|
38
|
+
return Buffer.from(JSON.stringify(spec)).toString("base64url");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function sign(secret: string, payload: string): string {
|
|
42
|
+
return createHmac("sha256", secret).update(payload).digest("base64url");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function isSigned(secret: string, payload: string, signature: string): boolean {
|
|
46
|
+
const expected = Buffer.from(sign(secret, payload));
|
|
47
|
+
const actual = Buffer.from(signature);
|
|
48
|
+
return expected.length === actual.length && timingSafeEqual(expected, actual);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Sealed addresses start with this; an older page's spec may still carry the address itself. */
|
|
52
|
+
const SEALED = "v1.";
|
|
53
|
+
|
|
54
|
+
/** The key that seals addresses: the forms secret, set apart from the one that signs. */
|
|
55
|
+
function sealingKey(secret: string): Buffer {
|
|
56
|
+
return createHash("sha256").update(`brim-form-notify\n${secret}`).digest();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Seals a form's notification addresses for the page, which anyone can read: only the server, with the site's
|
|
61
|
+
* secret, opens them again. Without this a page would hand its owner's address to every harvester of addresses.
|
|
62
|
+
*/
|
|
63
|
+
export function sealAddress(secret: string, address: string): string {
|
|
64
|
+
const iv = randomBytes(12);
|
|
65
|
+
const cipher = createCipheriv("aes-256-gcm", sealingKey(secret), iv);
|
|
66
|
+
const sealed = Buffer.concat([cipher.update(address, "utf8"), cipher.final()]);
|
|
67
|
+
return `${SEALED}${[iv, sealed, cipher.getAuthTag()].map((part) => part.toString("base64url")).join(".")}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The addresses a page's spec carries: opened when sealed, as they are when an older page wrote them plainly. */
|
|
71
|
+
export function openAddress(secret: string, value: string): string | undefined {
|
|
72
|
+
if (!value.startsWith(SEALED)) return value;
|
|
73
|
+
const [iv, sealed, tag] = value
|
|
74
|
+
.slice(SEALED.length)
|
|
75
|
+
.split(".")
|
|
76
|
+
.map((part) => Buffer.from(part, "base64url"));
|
|
77
|
+
if (!iv || !sealed || !tag) return undefined;
|
|
78
|
+
try {
|
|
79
|
+
const decipher = createDecipheriv("aes-256-gcm", sealingKey(secret), iv);
|
|
80
|
+
decipher.setAuthTag(tag);
|
|
81
|
+
return Buffer.concat([decipher.update(sealed), decipher.final()]).toString("utf8");
|
|
82
|
+
} catch {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function decodeSpec(encoded: string): FormSpec {
|
|
88
|
+
return JSON.parse(Buffer.from(encoded, "base64url").toString()) as FormSpec;
|
|
89
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
2
|
+
import type { Database, Row } from "../db/database.ts";
|
|
3
|
+
import type { FormFile } from "./files.ts";
|
|
4
|
+
|
|
5
|
+
export interface FormAnswer {
|
|
6
|
+
label: string;
|
|
7
|
+
value: string;
|
|
8
|
+
/**
|
|
9
|
+
* For a question with choices: every choice it offered when it was answered (with its icon or photo), so the
|
|
10
|
+
* answer can be shown as the form showed it, and which of them were picked.
|
|
11
|
+
*/
|
|
12
|
+
choices?: { label: string; icon?: string; image?: string }[];
|
|
13
|
+
picked?: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface FormResponse {
|
|
17
|
+
id: string;
|
|
18
|
+
/** Which form it came from: the page it is on and its node id. */
|
|
19
|
+
form: string;
|
|
20
|
+
title: string;
|
|
21
|
+
answers: FormAnswer[];
|
|
22
|
+
/** The files the visitor attached: what they are, never their bytes. Empty for most messages. */
|
|
23
|
+
files: FormFile[];
|
|
24
|
+
createdAt: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const SECRET_KEY = "form_secret"; // betterleaks:allow (the name of the setting that holds the secret, not the secret)
|
|
28
|
+
|
|
29
|
+
/** Responses visitors sent through the site's forms, and the secret the site signs its forms with. */
|
|
30
|
+
export class FormStore {
|
|
31
|
+
readonly #db: Database;
|
|
32
|
+
#secret: Promise<string> | undefined;
|
|
33
|
+
|
|
34
|
+
constructor(db: Database) {
|
|
35
|
+
this.#db = db;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Made on first use and kept in the database, so the build and the server sign with the same one. */
|
|
39
|
+
secret(): Promise<string> {
|
|
40
|
+
this.#secret ??= (async () => {
|
|
41
|
+
await this.#db.query("INSERT INTO brim_meta (key, value) VALUES (?, ?) ON CONFLICT (key) DO NOTHING", [
|
|
42
|
+
SECRET_KEY,
|
|
43
|
+
randomBytes(32).toString("base64url"),
|
|
44
|
+
]);
|
|
45
|
+
const rows = await this.#db.query("SELECT value FROM brim_meta WHERE key = ?", [SECRET_KEY]);
|
|
46
|
+
return String(rows[0]?.value);
|
|
47
|
+
})();
|
|
48
|
+
return this.#secret;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Keeps a response and answers its id, which the editor opens it by (`/admin/edit?response=<id>`). `id` is given
|
|
53
|
+
* when the message has files, which are stored under it before the message is.
|
|
54
|
+
*/
|
|
55
|
+
async save(
|
|
56
|
+
form: string,
|
|
57
|
+
title: string,
|
|
58
|
+
answers: FormAnswer[],
|
|
59
|
+
files: FormFile[] = [],
|
|
60
|
+
id: string = randomUUID(),
|
|
61
|
+
): Promise<string> {
|
|
62
|
+
await this.#db.query(
|
|
63
|
+
"INSERT INTO brim_form_responses (id, form, title, answers, files, created_at) VALUES (?, ?, ?, ?, ?, ?)",
|
|
64
|
+
[id, form, title, JSON.stringify(answers), JSON.stringify(files), new Date().toISOString()],
|
|
65
|
+
);
|
|
66
|
+
return id;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async list(): Promise<FormResponse[]> {
|
|
70
|
+
const rows = await this.#db.query("SELECT * FROM brim_form_responses ORDER BY created_at DESC LIMIT 1000");
|
|
71
|
+
return rows.map(toResponse);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async get(id: string): Promise<FormResponse | undefined> {
|
|
75
|
+
const rows = await this.#db.query("SELECT * FROM brim_form_responses WHERE id = ?", [id]);
|
|
76
|
+
return rows[0] && toResponse(rows[0]);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Deletes a message and gives back what it was, so its files can be deleted too. */
|
|
80
|
+
async remove(id: string): Promise<FormResponse | undefined> {
|
|
81
|
+
const response = await this.get(id);
|
|
82
|
+
await this.#db.query("DELETE FROM brim_form_responses WHERE id = ?", [id]);
|
|
83
|
+
return response;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function toResponse(row: Row): FormResponse {
|
|
88
|
+
return {
|
|
89
|
+
id: String(row.id),
|
|
90
|
+
form: String(row.form),
|
|
91
|
+
title: String(row.title),
|
|
92
|
+
answers: JSON.parse(String(row.answers)) as FormAnswer[],
|
|
93
|
+
files: JSON.parse(String(row.files ?? "[]")) as FormFile[],
|
|
94
|
+
createdAt: String(row.created_at),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A little work every form asks of the visitor's browser before it sends, on every site, with nothing to set up and
|
|
5
|
+
* no third party: find a number that, after the form's token, gives a SHA-256 starting with this many zero bits.
|
|
6
|
+
* A browser finds one in well under a second while the visitor types (it starts when they first touch the form); the
|
|
7
|
+
* server checks it with one hash. Each token is good once, so the work cannot be reused: a script sending thousands
|
|
8
|
+
* of messages pays for every one. A challenge from Turnstile or hCaptcha (`forms.captcha`) goes further.
|
|
9
|
+
*/
|
|
10
|
+
export const WORK_BITS = 16;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* More work when the site is being sent many messages: each step doubles it twice (about 0.4 s, 1.5 s, 6 s in a
|
|
14
|
+
* desktop browser, a few times that on an old phone). Real visitors wait a little while it lasts; a script sending
|
|
15
|
+
* thousands pays sixteen times as much for each. The levels are messages tried on the whole site in ten minutes,
|
|
16
|
+
* this window or the one before, so the work does not drop the moment a new window starts.
|
|
17
|
+
*/
|
|
18
|
+
export const WORK_LEVELS: readonly { attempts: number; bits: number }[] = [
|
|
19
|
+
{ attempts: 100, bits: 20 },
|
|
20
|
+
{ attempts: 30, bits: 18 },
|
|
21
|
+
];
|
|
22
|
+
export const WORK_WINDOW_SECONDS = 10 * 60;
|
|
23
|
+
|
|
24
|
+
/** The work asked for a new token, after `attempts` messages were tried on the site lately. */
|
|
25
|
+
export function workBits(attempts: number): number {
|
|
26
|
+
return WORK_LEVELS.find((level) => attempts >= level.attempts)?.bits ?? WORK_BITS;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Whether `nonce` is the work asked for `token`. */
|
|
30
|
+
export function workDone(token: string, nonce: unknown, bits = WORK_BITS): boolean {
|
|
31
|
+
if (typeof nonce !== "number" || !Number.isSafeInteger(nonce) || nonce < 0) return false;
|
|
32
|
+
return leadingZeroBits(createHash("sha256").update(`${token}.${nonce}`).digest()) >= bits;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function leadingZeroBits(bytes: Uint8Array): number {
|
|
36
|
+
let bits = 0;
|
|
37
|
+
for (const byte of bytes) {
|
|
38
|
+
if (byte === 0) {
|
|
39
|
+
bits += 8;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
return bits + Math.clz32(byte) - 24;
|
|
43
|
+
}
|
|
44
|
+
return bits;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Does the work for `token` (tests and scripts of the site's own; the page does it in the browser). */
|
|
48
|
+
export function solveWork(token: string, bits = WORK_BITS): number {
|
|
49
|
+
for (let nonce = 0; ; nonce++) if (workDone(token, nonce, bits)) return nonce;
|
|
50
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { ICONS } from "../blocks/data/icons.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One pack of icons, in the Iconify JSON format that the `@iconify-json/*` packages ship, so any of them can be
|
|
5
|
+
* given as it is. A hand-written pack only needs `prefix` and `icons`; an icon may be just its SVG content.
|
|
6
|
+
*/
|
|
7
|
+
export interface IconPack {
|
|
8
|
+
/** Short name of the pack, part of every icon's saved name: `prefix:icon`. Letters, digits and dashes. */
|
|
9
|
+
prefix: string;
|
|
10
|
+
/** Shown in the editor above the pack's icons. Defaults to `info.name`, then to the prefix. */
|
|
11
|
+
name?: string;
|
|
12
|
+
info?: { name?: string };
|
|
13
|
+
/** Size of the drawing area of the pack's icons. Defaults to 16, as the Iconify format says. */
|
|
14
|
+
width?: number;
|
|
15
|
+
height?: number;
|
|
16
|
+
/** What goes inside each icon's `<svg>`. Icons that follow the text colour use `currentColor`. */
|
|
17
|
+
icons: Record<string, string | { body: string; width?: number; height?: number }>;
|
|
18
|
+
/** Other names for an icon. Aliases that turn or flip their icon are left out. */
|
|
19
|
+
aliases?: Record<string, { parent: string; rotate?: number; hFlip?: boolean; vFlip?: boolean }>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Which icons the editor offers. Without it: Brim's own set. */
|
|
23
|
+
export interface IconsOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Brim's own icons: `true` (default) offers all of them, `false` none, a list only the ones named.
|
|
26
|
+
* An icon already placed on a page keeps showing either way.
|
|
27
|
+
*/
|
|
28
|
+
builtIn?: boolean | string[];
|
|
29
|
+
/**
|
|
30
|
+
* More packs. Each is an Iconify JSON package (`"@iconify-json/tabler"`), a JSON file of the site
|
|
31
|
+
* (`"./src/icons/brand.json"`), or the pack itself.
|
|
32
|
+
*/
|
|
33
|
+
packs?: (string | IconPack)[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** An icon ready to draw. */
|
|
37
|
+
export interface Icon {
|
|
38
|
+
/** What a block saves: the bare name for Brim's own icons, `prefix:name` for a pack's. */
|
|
39
|
+
value: string;
|
|
40
|
+
name: string;
|
|
41
|
+
body: string;
|
|
42
|
+
viewBox: string;
|
|
43
|
+
/** Brim's own icons are outlines drawn with the text colour; a pack's icons carry their own look. */
|
|
44
|
+
outline: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface IconSetInfo {
|
|
48
|
+
id: string;
|
|
49
|
+
name: string;
|
|
50
|
+
total: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface IconSearch {
|
|
54
|
+
icons: Icon[];
|
|
55
|
+
/** How many icons match, of which `icons` holds the first ones. */
|
|
56
|
+
total: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The id of Brim's own set. Its icons are saved without a prefix, as they always were. */
|
|
60
|
+
export const BUILT_IN_SET = "brim";
|
|
61
|
+
const BUILT_IN_VIEW_BOX = "0 0 24 24";
|
|
62
|
+
const PREFIX = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
63
|
+
/** Enough to show all of Brim's own set at once; a larger pack is narrowed by searching. */
|
|
64
|
+
const DEFAULT_LIMIT = 300;
|
|
65
|
+
const MAX_LIMIT = 500;
|
|
66
|
+
|
|
67
|
+
interface IconSet extends IconSetInfo {
|
|
68
|
+
icons: Map<string, Icon>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Every icon the site can draw, and the part of them the editor offers. */
|
|
72
|
+
export class IconLibrary {
|
|
73
|
+
readonly #sets: IconSet[] = [];
|
|
74
|
+
/** Brim's own icons the editor offers; all of them can still be drawn. */
|
|
75
|
+
readonly #offered: Set<string>;
|
|
76
|
+
|
|
77
|
+
constructor(options: IconsOptions = {}, packs: IconPack[] = []) {
|
|
78
|
+
const { builtIn = true } = options;
|
|
79
|
+
const known = Object.keys(ICONS);
|
|
80
|
+
this.#offered = new Set(builtIn === true ? known : builtIn === false ? [] : builtIn.filter((name) => ICONS[name]));
|
|
81
|
+
if (this.#offered.size > 0) {
|
|
82
|
+
this.#sets.push({
|
|
83
|
+
id: BUILT_IN_SET,
|
|
84
|
+
name: "Brim",
|
|
85
|
+
total: this.#offered.size,
|
|
86
|
+
icons: new Map(known.filter((name) => this.#offered.has(name)).map((name) => [name, builtInIcon(name)])),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
for (const pack of packs) this.#sets.push(readPack(pack, this.#sets));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The sets the editor offers, Brim's own first. */
|
|
93
|
+
sets(): IconSetInfo[] {
|
|
94
|
+
return this.#sets.map(({ id, name, total }) => ({ id, name, total }));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Icons of one set whose name contains `query`, in the set's own order. An unknown set has none. */
|
|
98
|
+
search(setId: string, query = "", limit = DEFAULT_LIMIT): IconSearch {
|
|
99
|
+
const set = this.#sets.find((candidate) => candidate.id === setId);
|
|
100
|
+
if (!set) return { icons: [], total: 0 };
|
|
101
|
+
const wanted = query.trim().toLowerCase();
|
|
102
|
+
const matches = [...set.icons.values()].filter((icon) => icon.name.includes(wanted));
|
|
103
|
+
const count = Math.min(Math.max(1, Math.trunc(limit) || DEFAULT_LIMIT), MAX_LIMIT);
|
|
104
|
+
return { icons: matches.slice(0, count), total: matches.length };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The icon a block saved. Brim's own icons are found even when the editor no longer offers them. */
|
|
108
|
+
find(value: unknown): Icon | undefined {
|
|
109
|
+
if (typeof value !== "string") return undefined;
|
|
110
|
+
const split = value.indexOf(":");
|
|
111
|
+
if (split === -1) return ICONS[value] ? builtInIcon(value) : undefined;
|
|
112
|
+
const set = this.#sets.find((candidate) => candidate.id === value.slice(0, split));
|
|
113
|
+
return set?.id === BUILT_IN_SET ? undefined : set?.icons.get(value.slice(split + 1));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function builtInIcon(name: string): Icon {
|
|
118
|
+
return { value: name, name, body: ICONS[name] ?? "", viewBox: BUILT_IN_VIEW_BOX, outline: true };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function readPack(pack: IconPack, before: IconSet[]): IconSet {
|
|
122
|
+
const prefix = typeof pack?.prefix === "string" ? pack.prefix : "";
|
|
123
|
+
if (!PREFIX.test(prefix) || prefix === BUILT_IN_SET) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
`An icon pack needs a "prefix" of lowercase letters, digits and dashes, other than "${BUILT_IN_SET}". Got: ${JSON.stringify(pack?.prefix)}.`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
if (before.some((set) => set.id === prefix)) throw new Error(`Two icon packs have the prefix "${prefix}".`);
|
|
129
|
+
if (!pack.icons || typeof pack.icons !== "object") throw new Error(`The icon pack "${prefix}" has no "icons".`);
|
|
130
|
+
|
|
131
|
+
const icons = new Map<string, Icon>();
|
|
132
|
+
const add = (name: string, source: string | { body: string; width?: number; height?: number }): void => {
|
|
133
|
+
const { body, width, height } = typeof source === "string" ? { body: source } : source;
|
|
134
|
+
if (typeof body !== "string" || body === "") return;
|
|
135
|
+
const viewBox = `0 0 ${width ?? pack.width ?? 16} ${height ?? pack.height ?? 16}`;
|
|
136
|
+
icons.set(name, { value: `${prefix}:${name}`, name, body, viewBox, outline: false });
|
|
137
|
+
};
|
|
138
|
+
for (const [name, source] of Object.entries(pack.icons)) add(name, source);
|
|
139
|
+
for (const [name, alias] of Object.entries(pack.aliases ?? {})) {
|
|
140
|
+
const parent = pack.icons[alias.parent];
|
|
141
|
+
if (parent && !alias.rotate && !alias.hFlip && !alias.vFlip && !icons.has(name)) add(name, parent);
|
|
142
|
+
}
|
|
143
|
+
return { id: prefix, name: pack.name ?? pack.info?.name ?? prefix, total: icons.size, icons };
|
|
144
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
4
|
+
import type { IconPack, IconsOptions } from "./library.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Where each pack of the options is: packs named by package or path become the JSON file to read, packs written
|
|
8
|
+
* in the config stay as they are. A pack that cannot be found stops the site from starting, with the reason.
|
|
9
|
+
*/
|
|
10
|
+
export function iconPackSources(options: IconsOptions | undefined, projectRoot: string): (string | IconPack)[] {
|
|
11
|
+
const fromSite = createRequire(join(projectRoot, "package.json"));
|
|
12
|
+
return (options?.packs ?? []).map((pack) => {
|
|
13
|
+
if (typeof pack !== "string") return pack;
|
|
14
|
+
if (pack.startsWith(".") || isAbsolute(pack)) return resolve(projectRoot, pack);
|
|
15
|
+
try {
|
|
16
|
+
return fromSite.resolve(pack.endsWith(".json") ? pack : `${pack}/icons.json`);
|
|
17
|
+
} catch {
|
|
18
|
+
throw new Error(`The icon pack "${pack}" was not found. Install it (npm install ${pack}) or fix its name.`);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The packs themselves, read from their files. */
|
|
24
|
+
export function loadIconPacks(sources: (string | IconPack)[]): IconPack[] {
|
|
25
|
+
return sources.map((source) => {
|
|
26
|
+
if (typeof source !== "string") return source;
|
|
27
|
+
try {
|
|
28
|
+
const pack = JSON.parse(readFileSync(source, "utf8")) as IconPack;
|
|
29
|
+
const name = packName(source);
|
|
30
|
+
return name && !pack.name ? { ...pack, name } : pack;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
throw new Error(`The icon pack at ${source} could not be read: ${(error as Error).message}`);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** An `@iconify-json/*` package keeps its pack's name in `info.json`, next to `icons.json`. */
|
|
38
|
+
function packName(file: string): string | undefined {
|
|
39
|
+
const info = join(dirname(file), "info.json");
|
|
40
|
+
if (basename(file) !== "icons.json" || !existsSync(info)) return undefined;
|
|
41
|
+
try {
|
|
42
|
+
const { name } = JSON.parse(readFileSync(info, "utf8")) as { name?: unknown };
|
|
43
|
+
return typeof name === "string" && name !== "" ? name : undefined;
|
|
44
|
+
} catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A module that hands the packs to the site's pages and server: files are imported, so a build carries them. */
|
|
50
|
+
export function iconsModule(options: IconsOptions | undefined, sources: (string | IconPack)[]): string {
|
|
51
|
+
const imports = sources.flatMap((source, index) =>
|
|
52
|
+
typeof source === "string" ? [`import pack${index} from ${JSON.stringify(source)};`] : [],
|
|
53
|
+
);
|
|
54
|
+
const packs = sources.map((source, index) => {
|
|
55
|
+
if (typeof source !== "string") return JSON.stringify(source);
|
|
56
|
+
const name = packName(source);
|
|
57
|
+
return name ? `{ name: ${JSON.stringify(name)}, ...pack${index} }` : `pack${index}`;
|
|
58
|
+
});
|
|
59
|
+
return [
|
|
60
|
+
...imports,
|
|
61
|
+
`export default { options: ${JSON.stringify({ builtIn: options?.builtIn ?? true })}, packs: [${packs.join(", ")}] };`,
|
|
62
|
+
].join("\n");
|
|
63
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export { default, type BrimOptions } from "./integration.ts";
|
|
2
|
+
export { ContentStore } from "./content/store.ts";
|
|
3
|
+
export { isNodeId, normalizePagePath, pageScope, siteScope, typeScope } from "./content/ids.ts";
|
|
4
|
+
export { BrimContext, type NodeScope } from "./runtime/context.ts";
|
|
5
|
+
export { connect, type Brim } from "./runtime/connect.ts";
|
|
6
|
+
export { Accounts, type Role, type User } from "./auth/accounts.ts";
|
|
7
|
+
export { ROLES, ROLE_PERMISSIONS, can, permissionsOf, type Permission } from "./auth/permissions.ts";
|
|
8
|
+
export { HistoryStore, type HistoryEntry } from "./content/history.ts";
|
|
9
|
+
export type {
|
|
10
|
+
FooterGroup,
|
|
11
|
+
FooterValue,
|
|
12
|
+
ImageValue,
|
|
13
|
+
LinkValue,
|
|
14
|
+
NavItem,
|
|
15
|
+
NavLink,
|
|
16
|
+
NavbarValue,
|
|
17
|
+
NodeKind,
|
|
18
|
+
NodeValues,
|
|
19
|
+
PageRecord,
|
|
20
|
+
SeoSettings,
|
|
21
|
+
RichTextBlock,
|
|
22
|
+
RichTextValue,
|
|
23
|
+
TextAlign,
|
|
24
|
+
StoredNode,
|
|
25
|
+
TextValue,
|
|
26
|
+
Visibility,
|
|
27
|
+
} from "./content/types.ts";
|
|
28
|
+
export {
|
|
29
|
+
ADMIN_PATH,
|
|
30
|
+
LIVE_CHANGE_HEADER,
|
|
31
|
+
createAdminHandler,
|
|
32
|
+
type AdminHandler,
|
|
33
|
+
type AdminOptions,
|
|
34
|
+
} from "./server/handler.ts";
|
|
35
|
+
export { type QueryRules, composeQueryRules } from "./server/query-params.ts";
|
|
36
|
+
export { toNodeMiddleware } from "./server/node.ts";
|
|
37
|
+
export type { LocaleConfig } from "./server/locales.ts";
|
|
38
|
+
export { blockDefinitions } from "./blocks/schemas/index.ts";
|
|
39
|
+
export { iconNames } from "./blocks/data/icons.ts";
|
|
40
|
+
export { IconLibrary, type Icon, type IconPack, type IconsOptions } from "./icons/library.ts";
|
|
41
|
+
export type { BlockDefinition, BlockSchema, Fieldset, SchemaProperty } from "./blocks/types.ts";
|
|
42
|
+
export {
|
|
43
|
+
BLOCK_TYPES,
|
|
44
|
+
type BlockEntry,
|
|
45
|
+
type BlockType,
|
|
46
|
+
type ContentField,
|
|
47
|
+
type ContentFieldInput,
|
|
48
|
+
type ContentTypeInput,
|
|
49
|
+
type KeyValueEntry,
|
|
50
|
+
type SelectOption,
|
|
51
|
+
type ContentType,
|
|
52
|
+
type FieldType,
|
|
53
|
+
defineContentType,
|
|
54
|
+
defineVocabulary,
|
|
55
|
+
optionLabel,
|
|
56
|
+
optionsOf,
|
|
57
|
+
} from "./content/content-types.ts";
|
|
58
|
+
export type { ContentItem, ItemOf, ItemPageProps } from "./runtime/content.ts";
|
|
59
|
+
export type { DataOf, FieldValue, FieldsData, KeyValueRow, SettingsData } from "./content/field-types.ts";
|
|
60
|
+
export type { BrowseState, Facet, FacetOption } from "./blocks/listing/query.ts";
|
|
61
|
+
export type { CarouselValue, GalleryValue, ShownImage, StartingImage } from "./blocks/images.ts";
|
|
62
|
+
export type { SocialLink, SocialMediaValue } from "./blocks/social/social-links.ts";
|
|
63
|
+
export {
|
|
64
|
+
type FormDefinition,
|
|
65
|
+
type FormFieldInput,
|
|
66
|
+
type FormInput,
|
|
67
|
+
type FormStepInput,
|
|
68
|
+
defineForm,
|
|
69
|
+
} from "./forms/define.ts";
|
|
70
|
+
export type { FormStep, FormValue, ShownField } from "./forms/fields.ts";
|
|
71
|
+
export type { SearchResult, SearchState } from "./blocks/search/state.ts";
|
|
72
|
+
export { type SearchHit, searchItems } from "./content/search.ts";
|
|
73
|
+
export {
|
|
74
|
+
type SiteSettingField,
|
|
75
|
+
type SiteSettingFieldInput,
|
|
76
|
+
type SiteSettings,
|
|
77
|
+
defineSiteSettings,
|
|
78
|
+
} from "./content/site-settings.ts";
|
|
79
|
+
export {
|
|
80
|
+
type PageComponent,
|
|
81
|
+
type PageTemplate,
|
|
82
|
+
type TemplatePageProps,
|
|
83
|
+
definePageTemplate,
|
|
84
|
+
} from "./content/page-templates.ts";
|
|
85
|
+
export { type Item, type ItemDraft, type ItemSort, parseItem } from "./content/items.ts";
|
|
86
|
+
export { renderBlocks, renderInline } from "./content/rich-text.ts";
|
|
87
|
+
export { blocksOf } from "./content/block-values.ts";
|
|
88
|
+
export {
|
|
89
|
+
type AccountEmail,
|
|
90
|
+
type AccountTemplate,
|
|
91
|
+
type ConfirmationEmail,
|
|
92
|
+
type EmailContent,
|
|
93
|
+
type FormEmail,
|
|
94
|
+
type Html,
|
|
95
|
+
type SiteEmails,
|
|
96
|
+
defineEmails,
|
|
97
|
+
html,
|
|
98
|
+
rawHtml,
|
|
99
|
+
} from "./mail/templates.ts";
|