@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,216 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, rm, rmdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { FILE_TYPES, type FileType, type FormFile, safeFileName } from "./file-types.ts";
|
|
6
|
+
|
|
7
|
+
export * from "./file-types.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Where the bytes of visitors' files are kept: a folder of the server that nothing serves, or a private storage
|
|
11
|
+
* reached over HTTP. Paths are written by Brim only (`<message id>/<file id>`, two uuids).
|
|
12
|
+
*/
|
|
13
|
+
export interface PrivateFiles {
|
|
14
|
+
write(path: string, bytes: Uint8Array): Promise<void>;
|
|
15
|
+
/** `undefined` when the file is not there (it was deleted, or the site was moved without its files). */
|
|
16
|
+
read(path: string): Promise<Uint8Array | undefined>;
|
|
17
|
+
remove(path: string): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const PATH = /^[0-9a-f-]{36}\/[0-9a-f-]{36}$/;
|
|
21
|
+
|
|
22
|
+
function checked(path: string): string {
|
|
23
|
+
// Paths come from the database, where Brim wrote them; anything else never reaches a disk or a storage.
|
|
24
|
+
if (!PATH.test(path)) throw new Error("Not a form file.");
|
|
25
|
+
return path;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Files in a folder of the server outside its public folder: `.brim/form-files` beside the local database. */
|
|
29
|
+
export class DiskPrivateFiles implements PrivateFiles {
|
|
30
|
+
readonly #directory: string;
|
|
31
|
+
|
|
32
|
+
constructor(directory: string) {
|
|
33
|
+
this.#directory = directory;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async write(path: string, bytes: Uint8Array): Promise<void> {
|
|
37
|
+
const file = join(this.#directory, checked(path));
|
|
38
|
+
await mkdir(dirname(file), { recursive: true, mode: 0o700 });
|
|
39
|
+
await writeFile(file, bytes, { mode: 0o600 });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async read(path: string): Promise<Uint8Array | undefined> {
|
|
43
|
+
const file = join(this.#directory, checked(path));
|
|
44
|
+
try {
|
|
45
|
+
return new Uint8Array(await readFile(file));
|
|
46
|
+
} catch {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async remove(path: string): Promise<void> {
|
|
52
|
+
const file = join(this.#directory, checked(path));
|
|
53
|
+
await rm(file, { force: true });
|
|
54
|
+
// The message's folder goes with its last file.
|
|
55
|
+
await rmdir(dirname(file)).catch(() => undefined);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Files in a private storage: `PUT`, `GET` and `DELETE <url>/form-files/<message id>/<file id>` with the headers the
|
|
61
|
+
* site gives (its key). The storage must answer only to that key: nothing under this address may be public.
|
|
62
|
+
*/
|
|
63
|
+
export class HttpPrivateFiles implements PrivateFiles {
|
|
64
|
+
readonly #url: string;
|
|
65
|
+
readonly #headers: Record<string, string>;
|
|
66
|
+
readonly #fetch: typeof fetch;
|
|
67
|
+
|
|
68
|
+
constructor(url: string, headers: Record<string, string> = {}, fetcher: typeof fetch = fetch) {
|
|
69
|
+
this.#url = url.replace(/\/+$/, "");
|
|
70
|
+
this.#headers = headers;
|
|
71
|
+
this.#fetch = fetcher;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#address(path: string): string {
|
|
75
|
+
return `${this.#url}/form-files/${checked(path)}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async write(path: string, bytes: Uint8Array): Promise<void> {
|
|
79
|
+
const response = await this.#fetch(this.#address(path), {
|
|
80
|
+
method: "PUT",
|
|
81
|
+
headers: { ...this.#headers, "content-type": "application/octet-stream" },
|
|
82
|
+
body: new Uint8Array(bytes),
|
|
83
|
+
signal: AbortSignal.timeout(30_000),
|
|
84
|
+
});
|
|
85
|
+
if (!response.ok) throw new Error(`The file could not be stored (${response.status}).`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async read(path: string): Promise<Uint8Array | undefined> {
|
|
89
|
+
const response = await this.#fetch(this.#address(path), {
|
|
90
|
+
headers: this.#headers,
|
|
91
|
+
signal: AbortSignal.timeout(30_000),
|
|
92
|
+
});
|
|
93
|
+
if (response.status === 404) return undefined;
|
|
94
|
+
if (!response.ok) throw new Error(`The file could not be read (${response.status}).`);
|
|
95
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async remove(path: string): Promise<void> {
|
|
99
|
+
const response = await this.#fetch(this.#address(path), {
|
|
100
|
+
method: "DELETE",
|
|
101
|
+
headers: this.#headers,
|
|
102
|
+
signal: AbortSignal.timeout(30_000),
|
|
103
|
+
});
|
|
104
|
+
if (!response.ok && response.status !== 404) throw new Error(`The file could not be deleted (${response.status}).`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Where a site keeps visitors' files, or nothing when it has nowhere private:
|
|
110
|
+
* - a private storage, when the host or the config names one (`BRIM_PRIVATE_STORAGE_URL` and `_TOKEN`, or
|
|
111
|
+
* `forms.files.storage`);
|
|
112
|
+
* - else the server's own disk, unless the site keeps its uploads in a storage (`media.storage`): such a host's disk
|
|
113
|
+
* is not kept, and its media storage is public, so neither may take them. File fields are then left out of forms.
|
|
114
|
+
*/
|
|
115
|
+
export function resolveFormFiles(input: {
|
|
116
|
+
storage?: { url: string; headers?: Record<string, string> } | undefined;
|
|
117
|
+
/** True when uploads go to a storage (`media.storage`): the disk is then not a place to keep anything. */
|
|
118
|
+
mediaInStorage: boolean;
|
|
119
|
+
directory: string;
|
|
120
|
+
}): PrivateFiles | undefined {
|
|
121
|
+
const url = (process.env.BRIM_PRIVATE_STORAGE_URL ?? "").trim();
|
|
122
|
+
const token = (process.env.BRIM_PRIVATE_STORAGE_TOKEN ?? "").trim(); // betterleaks:allow
|
|
123
|
+
if (/^https?:\/\//.test(url)) return new HttpPrivateFiles(url, token ? { authorization: `Bearer ${token}` } : {});
|
|
124
|
+
if (input.storage?.url) return new HttpPrivateFiles(input.storage.url, input.storage.headers ?? {});
|
|
125
|
+
return input.mediaInStorage ? undefined : new DiskPrivateFiles(input.directory);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* The folder of visitors' files on the server's disk: `form-files` beside the database when that is a local file
|
|
130
|
+
* (`.brim/form-files`: ignored by git, served by nothing, and wherever the host put the database it keeps), else
|
|
131
|
+
* `.brim/form-files` in the site's folder.
|
|
132
|
+
*/
|
|
133
|
+
export function formFilesDirectory(databaseUrl: string, projectRoot: string): string {
|
|
134
|
+
const local = /^file:(?!:memory:)(.+)$/.exec(databaseUrl)?.[1]?.replace(/\?.*$/, "");
|
|
135
|
+
return local ? join(dirname(resolve(projectRoot, local)), "form-files") : join(projectRoot, ".brim", "form-files");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function sealingKey(secret: string): Buffer {
|
|
139
|
+
return createHash("sha256").update(`brim-form-files\n${secret}`).digest();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* A file as it is stored: sealed (AES-256-GCM) with a key made from the site's forms secret, which lives in the
|
|
144
|
+
* database. Whoever reaches the stored bytes without the database (a storage made public by mistake, a copied
|
|
145
|
+
* disk) holds nothing readable, and a file changed where it is stored is refused when opened.
|
|
146
|
+
*/
|
|
147
|
+
export function sealFile(secret: string, bytes: Uint8Array): Uint8Array {
|
|
148
|
+
const iv = randomBytes(12);
|
|
149
|
+
const cipher = createCipheriv("aes-256-gcm", sealingKey(secret), iv);
|
|
150
|
+
return new Uint8Array(Buffer.concat([iv, cipher.update(bytes), cipher.final(), cipher.getAuthTag()]));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function openFile(secret: string, sealed: Uint8Array): Uint8Array | undefined {
|
|
154
|
+
if (sealed.byteLength < 28) return undefined;
|
|
155
|
+
try {
|
|
156
|
+
const decipher = createDecipheriv("aes-256-gcm", sealingKey(secret), sealed.subarray(0, 12));
|
|
157
|
+
decipher.setAuthTag(sealed.subarray(sealed.byteLength - 16));
|
|
158
|
+
return new Uint8Array(
|
|
159
|
+
Buffer.concat([decipher.update(sealed.subarray(12, sealed.byteLength - 16)), decipher.final()]),
|
|
160
|
+
);
|
|
161
|
+
} catch {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The files of the site's form messages: sealed on their way in, opened on their way out. */
|
|
167
|
+
export class FormFiles {
|
|
168
|
+
readonly #files: PrivateFiles;
|
|
169
|
+
readonly #secret: () => Promise<string>;
|
|
170
|
+
|
|
171
|
+
constructor(files: PrivateFiles, secret: () => Promise<string>) {
|
|
172
|
+
this.#files = files;
|
|
173
|
+
this.#secret = secret;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async write(responseId: string, fileId: string, bytes: Uint8Array): Promise<void> {
|
|
177
|
+
await this.#files.write(`${responseId}/${fileId}`, sealFile(await this.#secret(), bytes));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async read(responseId: string, fileId: string): Promise<Uint8Array | undefined> {
|
|
181
|
+
const sealed = await this.#files.read(`${responseId}/${fileId}`);
|
|
182
|
+
return sealed && openFile(await this.#secret(), sealed);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Deletes the files of one message. A file that will not go is logged, and the others still go. */
|
|
186
|
+
async remove(responseId: string, files: Pick<FormFile, "id">[]): Promise<void> {
|
|
187
|
+
for (const file of files) {
|
|
188
|
+
await this.#files.remove(`${responseId}/${file.id}`).catch((error: unknown) => {
|
|
189
|
+
console.error("[brim] A form file was not deleted:", error instanceof Error ? error.message : error);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* A visitor's file as the editor's API hands it to someone who may read messages: always a download, never a page.
|
|
197
|
+
* The browser is told its real kind and forbidden to guess another, to run or load anything from it, or to keep it.
|
|
198
|
+
*/
|
|
199
|
+
export function fileDownload(file: FormFile, bytes: Uint8Array): Response {
|
|
200
|
+
const type = (Object.keys(FILE_TYPES) as FileType[]).find((key) => FILE_TYPES[key].mime === file.mime);
|
|
201
|
+
const name = safeFileName(file.name, type ?? "pdf");
|
|
202
|
+
// The plain name for old clients, the full one (RFC 5987) for the rest; both are free of quotes and line ends.
|
|
203
|
+
const plain = name.replace(/[^\x20-\x7e]/g, "_").replace(/["\\]/g, "_");
|
|
204
|
+
return new Response(new Uint8Array(bytes), {
|
|
205
|
+
headers: {
|
|
206
|
+
"content-type": type ? file.mime : "application/octet-stream",
|
|
207
|
+
"content-length": String(bytes.byteLength),
|
|
208
|
+
"content-disposition": `attachment; filename="${plain}"; filename*=UTF-8''${encodeURIComponent(name)}`,
|
|
209
|
+
"x-content-type-options": "nosniff",
|
|
210
|
+
"content-security-policy": "default-src 'none'; sandbox",
|
|
211
|
+
"cache-control": "no-store, private",
|
|
212
|
+
"cross-origin-resource-policy": "same-origin",
|
|
213
|
+
"x-robots-tag": "noindex",
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page a form was sent from, out of the form's id (`/produse/ceramica/marazzi-art#comanda-form`): the same form
|
|
3
|
+
* placed in a template (a product's order form) is sent from every item's page, and this says which one.
|
|
4
|
+
* `undefined` for a form declared by name, which has no page in its id.
|
|
5
|
+
*/
|
|
6
|
+
export function formPage(form: string): string | undefined {
|
|
7
|
+
const page = form.split("#")[0] ?? "";
|
|
8
|
+
return page.startsWith("/") && form.includes("#") ? page : undefined;
|
|
9
|
+
}
|