inlaycms 0.4.1 → 0.6.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/README.md +18 -709
- package/assets/admin/admin.css +1 -1
- package/assets/admin/admin.js +1 -1
- package/assets/admin/editor.js +63 -46
- package/assets/site/consent.js +1 -0
- package/dist/adapters/admin/context.d.ts +1 -1
- package/dist/adapters/admin/context.js +1 -0
- package/dist/adapters/admin/publish.js +2 -2
- package/dist/adapters/admin/routes.d.ts +2 -0
- package/dist/adapters/admin/routes.js +2 -0
- package/dist/adapters/admin/screens/backups.js +2 -2
- package/dist/adapters/admin/screens/editor.js +5 -2
- package/dist/adapters/admin/screens/integrations.d.ts +4 -0
- package/dist/adapters/admin/screens/integrations.js +140 -0
- package/dist/adapters/admin/screens/lists.js +2 -1
- package/dist/adapters/admin/screens/releases.js +9 -4
- package/dist/adapters/admin/views/data-table.js +1 -1
- package/dist/adapters/admin/views/layout.d.ts +1 -1
- package/dist/adapters/admin/views/layout.js +15 -2
- package/dist/adapters/cli/bin.js +8 -3
- package/dist/adapters/cli/main.js +12 -2
- package/dist/adapters/mcp/agent-refs.d.ts +2 -1
- package/dist/adapters/mcp/agent-refs.js +4 -4
- package/dist/adapters/mcp/tools/publish.js +1 -1
- package/dist/adapters/mcp/tools/read.js +19 -4
- package/dist/adapters/mcp/tools/write.js +5 -1
- package/dist/adapters/site/cache.d.ts +8 -2
- package/dist/adapters/site/cache.js +13 -4
- package/dist/adapters/site/code-files.d.ts +44 -0
- package/dist/adapters/site/code-files.js +212 -0
- package/dist/adapters/site/consent.d.ts +9 -0
- package/dist/adapters/site/consent.js +26 -0
- package/dist/adapters/site/context.d.ts +9 -1
- package/dist/adapters/site/context.js +106 -27
- package/dist/adapters/site/csp.d.ts +11 -0
- package/dist/adapters/site/csp.js +36 -0
- package/dist/adapters/site/head.d.ts +2 -0
- package/dist/adapters/site/head.js +7 -20
- package/dist/adapters/site/headers.d.ts +5 -2
- package/dist/adapters/site/headers.js +13 -6
- package/dist/adapters/site/integrations.d.ts +48 -0
- package/dist/adapters/site/integrations.js +118 -0
- package/dist/adapters/site/media-routes.d.ts +2 -0
- package/dist/adapters/site/media-routes.js +2 -1
- package/dist/adapters/site/plugin.d.ts +2 -0
- package/dist/adapters/site/plugin.js +78 -40
- package/dist/adapters/site/preview.d.ts +2 -0
- package/dist/adapters/site/preview.js +10 -5
- package/dist/adapters/site/render.d.ts +31 -14
- package/dist/adapters/site/render.js +33 -7
- package/dist/adapters/site/routes.d.ts +3 -0
- package/dist/adapters/site/routes.js +11 -6
- package/dist/adapters/site/scripts.d.ts +6 -0
- package/dist/adapters/site/scripts.js +27 -0
- package/dist/adapters/site/sitemap.d.ts +2 -1
- package/dist/adapters/site/sitemap.js +3 -3
- package/dist/core/auth/roles.d.ts +1 -1
- package/dist/core/auth/roles.js +1 -0
- package/dist/core/boot.d.ts +2 -0
- package/dist/core/boot.js +3 -0
- package/dist/core/content/shipped-input.js +1 -2
- package/dist/core/content/validate.d.ts +7 -0
- package/dist/core/content/validate.js +24 -1
- package/dist/core/core.d.ts +29 -0
- package/dist/core/core.js +10 -2
- package/dist/core/db/migrate.js +2 -0
- package/dist/core/db/migrations/0008_integrations.d.ts +3 -0
- package/dist/core/db/migrations/0008_integrations.js +11 -0
- package/dist/core/db/schema.d.ts +11 -0
- package/dist/core/export/export.js +4 -0
- package/dist/core/export/format.d.ts +11 -0
- package/dist/core/export/format.js +4 -0
- package/dist/core/export/import.js +2 -1
- package/dist/core/export/replace.d.ts +6 -1
- package/dist/core/export/replace.js +4 -1
- package/dist/core/export/report.d.ts +6 -0
- package/dist/core/export/report.js +7 -0
- package/dist/core/fields/builtins.d.ts +4 -0
- package/dist/core/fields/builtins.js +11 -1
- package/dist/core/fields/widgets.js +5 -1
- package/dist/core/integrations/integrations.d.ts +32 -0
- package/dist/core/integrations/integrations.js +80 -0
- package/dist/core/integrations/warnings.d.ts +4 -0
- package/dist/core/integrations/warnings.js +29 -0
- package/dist/core/media/store.d.ts +2 -0
- package/dist/core/media/store.js +17 -1
- package/dist/core/media/variants.d.ts +14 -0
- package/dist/core/media/variants.js +54 -0
- package/dist/core/messages/en.d.ts +83 -2
- package/dist/core/messages/en.js +83 -2
- package/dist/core/registry/code-image.d.ts +16 -0
- package/dist/core/registry/code-image.js +13 -0
- package/dist/core/registry/define.d.ts +12 -0
- package/dist/core/registry/define.js +3 -0
- package/dist/core/registry/designed.d.ts +6 -0
- package/dist/core/registry/designed.js +8 -0
- package/dist/core/registry/integration.d.ts +48 -0
- package/dist/core/registry/integration.js +29 -0
- package/dist/core/registry/js.d.ts +9 -0
- package/dist/core/registry/js.js +39 -0
- package/dist/core/registry/registry.js +46 -0
- package/dist/core/registry/render.d.ts +45 -5
- package/dist/core/release/bundle.d.ts +3 -1
- package/dist/core/release/bundle.js +169 -11
- package/dist/core/release/manifest.d.ts +29 -6
- package/dist/core/release/manifest.js +26 -25
- package/dist/core/release/pack.js +13 -2
- package/dist/core/release/pointer.js +14 -3
- package/dist/core/release/read.d.ts +4 -2
- package/dist/core/release/read.js +12 -12
- package/dist/core/release/window.d.ts +11 -0
- package/dist/core/release/window.js +34 -0
- package/dist/core/report.d.ts +3 -0
- package/dist/core/richtext/headings.d.ts +10 -0
- package/dist/core/richtext/headings.js +37 -0
- package/dist/core/richtext/html.d.ts +10 -1
- package/dist/core/richtext/html.js +17 -6
- package/dist/core/richtext/markdown-text.d.ts +24 -0
- package/dist/core/richtext/markdown-text.js +38 -0
- package/dist/core/richtext/markdown.js +44 -3
- package/dist/core/richtext/schema.d.ts +12 -3
- package/dist/core/richtext/schema.js +34 -0
- package/dist/core/site/live.d.ts +6 -0
- package/dist/core/site/live.js +25 -0
- package/dist/core/site/paths.d.ts +5 -0
- package/dist/core/site/paths.js +32 -0
- package/dist/core/site/resolve.js +17 -6
- package/dist/core/version.d.ts +4 -0
- package/dist/core/version.js +4 -0
- package/dist/host/activate.js +2 -1
- package/dist/host/app.d.ts +2 -0
- package/dist/host/app.js +1 -0
- package/dist/host/diff.d.ts +11 -0
- package/dist/host/diff.js +40 -0
- package/dist/host/ipc.d.ts +2 -0
- package/dist/host/ipc.js +5 -1
- package/dist/host/preview-worker.js +2 -2
- package/dist/host/primary.js +15 -4
- package/dist/host/push.d.ts +5 -1
- package/dist/host/push.js +18 -6
- package/dist/host/serve.js +1 -1
- package/dist/host/store.d.ts +1 -2
- package/dist/host/store.js +32 -6
- package/dist/host/worker.js +9 -3
- package/dist/image-modules.d.ts +29 -0
- package/dist/image-modules.js +1 -0
- package/dist/index.d.ts +7 -1
- package/dist/index.js +4 -0
- package/dist/site.d.ts +9 -2
- package/dist/site.js +5 -0
- package/package.json +5 -1
|
@@ -1,37 +1,46 @@
|
|
|
1
1
|
import { documentRefs } from '../../core/content/schema.js';
|
|
2
2
|
import { findRefs } from '../../core/fields/walk.js';
|
|
3
3
|
import { validateContent } from '../../core/content/validate.js';
|
|
4
|
+
import { InlayError } from '../../core/errors.js';
|
|
4
5
|
import { isRasterImage } from '../../core/media/detect.js';
|
|
5
6
|
import { IMAGE_WIDTHS } from '../../core/media/variants.js';
|
|
7
|
+
import { isCodeImage } from '../../core/registry/code-image.js';
|
|
6
8
|
import { richTextToHtml } from '../../core/richtext/html.js';
|
|
7
9
|
import { inlineTextToHtml } from '../../core/richtext/inline.js';
|
|
8
|
-
import {
|
|
10
|
+
import { markdownImages } from '../../core/richtext/markdown.js';
|
|
11
|
+
import { isMediaTarget, KEY_HREF, NO_DOCUMENT, parseMarkdownText } from '../../core/richtext/markdown-text.js';
|
|
12
|
+
import { richTextRefs } from '../../core/richtext/schema.js';
|
|
13
|
+
import { encodePath, presentHref } from '../../core/site/paths.js';
|
|
9
14
|
import { publicAssets } from './assets.js';
|
|
10
15
|
import { createHeadCollector } from './head.js';
|
|
16
|
+
import { codeFilesFor } from './code-files.js';
|
|
17
|
+
import { createScriptCollector } from './scripts.js';
|
|
11
18
|
import { escapeHtml, raw } from '../../jsx/html.js';
|
|
19
|
+
/** The encoded URL of the decoded site path `path`, in the form the site presents; `''` for `''`. */
|
|
20
|
+
export const pageUrl = (path, mode) => presentHref(encodePath(path), mode);
|
|
12
21
|
/** Rich text images get this width as `src` when the original is at least as wide. */
|
|
13
22
|
const RICH_TEXT_SRC_WIDTH = 1280;
|
|
14
23
|
const nonEmpty = (s) => typeof s === 'string' && s !== '';
|
|
15
24
|
const mediaUrl = (id) => `/media/${id}`;
|
|
16
25
|
const missing = (v) => v === undefined || v === null;
|
|
17
|
-
function resolveImage(
|
|
18
|
-
const url =
|
|
19
|
-
const raster = isRasterImage(
|
|
20
|
-
const variant = (width, format) => `${
|
|
26
|
+
function resolveImage(image, alt) {
|
|
27
|
+
const { url } = image;
|
|
28
|
+
const raster = isRasterImage(image.mime);
|
|
29
|
+
const variant = (width, format) => `${image.variants}/${width}.${format}`;
|
|
21
30
|
const widths = () => {
|
|
22
31
|
const all = [...IMAGE_WIDTHS];
|
|
23
|
-
if (
|
|
32
|
+
if (image.width === null)
|
|
24
33
|
return all;
|
|
25
|
-
const fitting = all.filter((w) => w <=
|
|
34
|
+
const fitting = all.filter((w) => w <= image.width);
|
|
26
35
|
return fitting.length > 0 ? fitting : all.slice(0, 1);
|
|
27
36
|
};
|
|
28
37
|
return {
|
|
29
|
-
id:
|
|
38
|
+
id: image.id,
|
|
30
39
|
url,
|
|
31
|
-
alt
|
|
32
|
-
width:
|
|
33
|
-
height:
|
|
34
|
-
mime:
|
|
40
|
+
alt,
|
|
41
|
+
width: image.width,
|
|
42
|
+
height: image.height,
|
|
43
|
+
mime: image.mime,
|
|
35
44
|
src(width, format = 'webp') {
|
|
36
45
|
if (!raster)
|
|
37
46
|
return url;
|
|
@@ -47,13 +56,34 @@ function resolveImage(media, alt) {
|
|
|
47
56
|
},
|
|
48
57
|
};
|
|
49
58
|
}
|
|
50
|
-
function
|
|
59
|
+
function mediaSource(media) {
|
|
60
|
+
const url = mediaUrl(media.id);
|
|
61
|
+
return { id: media.id, url, variants: url, mime: media.mime, width: media.width, height: media.height };
|
|
62
|
+
}
|
|
63
|
+
/** Alt is the value's, then the media's default, then `''`. */
|
|
64
|
+
const mediaAlt = (media, alt) => nonEmpty(alt) ? alt : nonEmpty(media.defaultAlt) ? media.defaultAlt : '';
|
|
65
|
+
function codeSource(file) {
|
|
66
|
+
return {
|
|
67
|
+
id: file.sha,
|
|
68
|
+
url: `/media/files/${file.sha}${file.ext.toLowerCase()}`,
|
|
69
|
+
variants: `/media/files/${file.sha}`,
|
|
70
|
+
mime: file.mime,
|
|
71
|
+
width: file.width,
|
|
72
|
+
height: file.height,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** A rich text image: `src` at `RICH_TEXT_SRC_WIDTH`, or the original's width when narrower. */
|
|
76
|
+
function richTextImage(img) {
|
|
77
|
+
const width = Math.min(img.width ?? RICH_TEXT_SRC_WIDTH, RICH_TEXT_SRC_WIDTH);
|
|
78
|
+
return { src: img.src(width), srcset: img.srcset(), alt: img.alt, width: img.width, height: img.height };
|
|
79
|
+
}
|
|
80
|
+
function toEntry(doc, mode) {
|
|
51
81
|
return {
|
|
52
82
|
id: doc.groupId,
|
|
53
83
|
type: doc.type,
|
|
54
84
|
locale: doc.contentLocale,
|
|
55
85
|
fallback: doc.fallback,
|
|
56
|
-
url: doc.path === null ? null : encodePath(doc.path),
|
|
86
|
+
url: doc.path === null ? null : presentHref(encodePath(doc.path), mode),
|
|
57
87
|
title: doc.title,
|
|
58
88
|
slug: doc.slug,
|
|
59
89
|
updatedAt: doc.updatedAt,
|
|
@@ -107,11 +137,13 @@ export function createRenderContext(core, opts) {
|
|
|
107
137
|
const { locale, state } = opts;
|
|
108
138
|
const preview = state === 'preview';
|
|
109
139
|
const assets = opts.assets ?? publicAssets(null);
|
|
140
|
+
const codeFiles = opts.codeFiles ?? codeFilesFor(core);
|
|
110
141
|
const media = new Map();
|
|
111
142
|
const docs = new Map();
|
|
112
143
|
const collections = new Map();
|
|
113
144
|
const globals = new Map();
|
|
114
145
|
const head = createHeadCollector();
|
|
146
|
+
const scripts = createScriptCollector();
|
|
115
147
|
/**
|
|
116
148
|
* Whether `doc` can be shown as is. In preview, a draft that isn't complete can't: an entry is
|
|
117
149
|
* then treated as not live, and a singleton or global falls back to its published version.
|
|
@@ -167,9 +199,18 @@ export function createRenderContext(core, opts) {
|
|
|
167
199
|
found.push(...findRefs(slot.block.schema, data.slots[key]?.data));
|
|
168
200
|
await loadRefs(found.map((r) => r.ref));
|
|
169
201
|
}
|
|
170
|
-
function image(v) {
|
|
202
|
+
function image(v, opts) {
|
|
203
|
+
if (isCodeImage(v)) {
|
|
204
|
+
codeFiles.register(v);
|
|
205
|
+
return resolveImage(codeSource(v), opts?.alt ?? '');
|
|
206
|
+
}
|
|
207
|
+
if (typeof v === 'string' || (typeof v === 'object' && v !== null && v.kind === 'codeImage')) {
|
|
208
|
+
throw new InlayError('definition', 'error.imageNotImported');
|
|
209
|
+
}
|
|
171
210
|
const info = v ? media.get(v.media) : undefined;
|
|
172
|
-
|
|
211
|
+
if (!info)
|
|
212
|
+
return null;
|
|
213
|
+
return resolveImage(mediaSource(info), opts?.alt ?? mediaAlt(info, v.alt));
|
|
173
214
|
}
|
|
174
215
|
function file(v) {
|
|
175
216
|
const info = v ? media.get(v.media) : undefined;
|
|
@@ -181,7 +222,7 @@ export function createRenderContext(core, opts) {
|
|
|
181
222
|
const doc = docs.get(groupId);
|
|
182
223
|
if (!doc || doc.path === null)
|
|
183
224
|
return null;
|
|
184
|
-
return { href: encodePath(doc.path), title: doc.title };
|
|
225
|
+
return { href: presentHref(encodePath(doc.path), registry.site.trailingSlash), title: doc.title };
|
|
185
226
|
}
|
|
186
227
|
function link(v) {
|
|
187
228
|
if (!v)
|
|
@@ -198,15 +239,50 @@ export function createRenderContext(core, opts) {
|
|
|
198
239
|
link: (groupId) => documentHref(groupId)?.href ?? null,
|
|
199
240
|
image: (id, alt) => {
|
|
200
241
|
const info = media.get(id);
|
|
201
|
-
|
|
202
|
-
return null;
|
|
203
|
-
const img = resolveImage(info, alt);
|
|
204
|
-
const width = Math.min(img.width ?? RICH_TEXT_SRC_WIDTH, RICH_TEXT_SRC_WIDTH);
|
|
205
|
-
return { src: img.src(width), srcset: img.srcset(), alt: img.alt, width: img.width, height: img.height };
|
|
242
|
+
return info ? richTextImage(resolveImage(mediaSource(info), mediaAlt(info, alt))) : null;
|
|
206
243
|
},
|
|
207
244
|
};
|
|
208
|
-
function richText(doc) {
|
|
209
|
-
return raw(doc ? richTextToHtml(doc, resolver) : '');
|
|
245
|
+
function richText(doc, opts) {
|
|
246
|
+
return raw(doc ? richTextToHtml(doc, resolver, opts) : '');
|
|
247
|
+
}
|
|
248
|
+
async function markdown(text, renderOpts) {
|
|
249
|
+
const keyRefs = [...text.matchAll(KEY_HREF)].map((m) => ({ type: m[1], key: m[2] ?? null }));
|
|
250
|
+
const groups = await core.groupIdsByKey(keyRefs);
|
|
251
|
+
await loadDocs(groups.values());
|
|
252
|
+
text = text.replace(KEY_HREF, (_href, type, key) => {
|
|
253
|
+
return `](inlay:document/${groups.get(`${type}/${key ?? type}`) ?? NO_DOCUMENT}`;
|
|
254
|
+
});
|
|
255
|
+
const found = new Map();
|
|
256
|
+
const unusable = [];
|
|
257
|
+
await Promise.all(markdownImages(text).map(async ({ src, line }) => {
|
|
258
|
+
if (isMediaTarget(src))
|
|
259
|
+
return;
|
|
260
|
+
const file = /^file:/i.test(src) ? await codeFiles.lookup(src) : null;
|
|
261
|
+
if (file)
|
|
262
|
+
found.set(src, file);
|
|
263
|
+
else
|
|
264
|
+
unusable.push({ src, line });
|
|
265
|
+
}));
|
|
266
|
+
const parsed = parseMarkdownText(text, new Map([...found].map(([src, file]) => [src, file.sha])));
|
|
267
|
+
if (!parsed.ok) {
|
|
268
|
+
const first = parsed.problems[0];
|
|
269
|
+
throw new InlayError('definition', 'error.markdownRender', { line: first?.line ?? 1, problem: first?.message ?? '' });
|
|
270
|
+
}
|
|
271
|
+
const refused = unusable.sort((a, b) => a.line - b.line)[0];
|
|
272
|
+
if (refused)
|
|
273
|
+
throw new InlayError('definition', 'error.markdownRenderImage', refused);
|
|
274
|
+
const files = new Map([...found.values()].map((file) => [file.sha, file]));
|
|
275
|
+
await loadRefs(richTextRefs(parsed.doc).filter((ref) => ref.kind !== 'media' || !files.has(ref.id)));
|
|
276
|
+
const markdownResolver = {
|
|
277
|
+
link: resolver.link,
|
|
278
|
+
image: (id, alt) => {
|
|
279
|
+
const file = files.get(id);
|
|
280
|
+
if (!file)
|
|
281
|
+
return resolver.image(id, alt);
|
|
282
|
+
return richTextImage(resolveImage(codeSource(file), alt ?? ''));
|
|
283
|
+
},
|
|
284
|
+
};
|
|
285
|
+
return raw(richTextToHtml(parsed.doc, markdownResolver, renderOpts));
|
|
210
286
|
}
|
|
211
287
|
function inline(value) {
|
|
212
288
|
if (value === null || value === undefined)
|
|
@@ -221,7 +297,7 @@ export function createRenderContext(core, opts) {
|
|
|
221
297
|
return doc ? [doc] : [];
|
|
222
298
|
});
|
|
223
299
|
await preload(found);
|
|
224
|
-
return found.map((doc) => toEntry(doc));
|
|
300
|
+
return found.map((doc) => toEntry(doc, registry.site.trailingSlash));
|
|
225
301
|
}
|
|
226
302
|
function liveCollection(name) {
|
|
227
303
|
let pending = collections.get(name);
|
|
@@ -240,7 +316,7 @@ export function createRenderContext(core, opts) {
|
|
|
240
316
|
async function query(collection, queryOpts = {}) {
|
|
241
317
|
const name = typeof collection === 'string' ? collection : collection.name;
|
|
242
318
|
const live = await liveCollection(name);
|
|
243
|
-
const all = live.map((doc) => toEntry(doc));
|
|
319
|
+
const all = live.map((doc) => toEntry(doc, registry.site.trailingSlash));
|
|
244
320
|
const filtered = queryOpts.filter ? all.filter(queryOpts.filter) : all;
|
|
245
321
|
const sorted = sortEntries(filtered, queryOpts.sort);
|
|
246
322
|
const total = sorted.length;
|
|
@@ -282,11 +358,13 @@ export function createRenderContext(core, opts) {
|
|
|
282
358
|
baseUrl: opts.baseUrl,
|
|
283
359
|
asset: (path) => assets.url(path),
|
|
284
360
|
head: head.api,
|
|
285
|
-
|
|
361
|
+
script: scripts.api,
|
|
362
|
+
alternates: opts.alternates.map((a) => ({ ...a, url: pageUrl(a.path, registry.site.trailingSlash) })),
|
|
286
363
|
image,
|
|
287
364
|
file,
|
|
288
365
|
link,
|
|
289
366
|
richText,
|
|
367
|
+
markdown,
|
|
290
368
|
inline,
|
|
291
369
|
entries,
|
|
292
370
|
query,
|
|
@@ -294,6 +372,7 @@ export function createRenderContext(core, opts) {
|
|
|
294
372
|
preload,
|
|
295
373
|
preloadData,
|
|
296
374
|
headTags: head.collected,
|
|
375
|
+
scriptHashes: scripts.hashes,
|
|
297
376
|
};
|
|
298
377
|
}
|
|
299
378
|
/** Published content at `locale`, the way templates query it, for one request. */
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type CspSources } from '../../core/registry/integration.js';
|
|
2
|
+
/**
|
|
3
|
+
* The Content-Security-Policy of a public response: Inlay's defaults, then in `script-src` the page's inline-script
|
|
4
|
+
* `hashes`, then the sources the site and then each integration declare, each source once. Hashes are left out when
|
|
5
|
+
* `'unsafe-inline'` is declared for `script-src`, since browsers ignore it beside a hash.
|
|
6
|
+
*/
|
|
7
|
+
export declare function buildCsp(input: {
|
|
8
|
+
site: CspSources;
|
|
9
|
+
integrations: readonly CspSources[];
|
|
10
|
+
hashes?: readonly string[];
|
|
11
|
+
}): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { CSP_DIRECTIVES } from '../../core/registry/integration.js';
|
|
2
|
+
/** Inlay's own sources per directive, in the order the policy lists them; `worker-src` only when something declares it. */
|
|
3
|
+
const BASE = [
|
|
4
|
+
['default-src', ["'self'"]],
|
|
5
|
+
['script-src', ["'self'"]],
|
|
6
|
+
['style-src', ["'self'", "'unsafe-inline'"]],
|
|
7
|
+
['img-src', ["'self'", 'data:']],
|
|
8
|
+
['font-src', ["'self'"]],
|
|
9
|
+
['connect-src', ["'self'"]],
|
|
10
|
+
['frame-src', ["'self'"]],
|
|
11
|
+
['media-src', ["'self'"]],
|
|
12
|
+
['worker-src', ["'self'"]],
|
|
13
|
+
['object-src', ["'none'"]],
|
|
14
|
+
['base-uri', ["'self'"]],
|
|
15
|
+
['form-action', ["'self'"]],
|
|
16
|
+
['frame-ancestors', ["'self'"]],
|
|
17
|
+
];
|
|
18
|
+
const UNSAFE_INLINE = "'unsafe-inline'";
|
|
19
|
+
const declarable = (directive) => CSP_DIRECTIVES.includes(directive);
|
|
20
|
+
/**
|
|
21
|
+
* The Content-Security-Policy of a public response: Inlay's defaults, then in `script-src` the page's inline-script
|
|
22
|
+
* `hashes`, then the sources the site and then each integration declare, each source once. Hashes are left out when
|
|
23
|
+
* `'unsafe-inline'` is declared for `script-src`, since browsers ignore it beside a hash.
|
|
24
|
+
*/
|
|
25
|
+
export function buildCsp(input) {
|
|
26
|
+
const declared = (directive) => declarable(directive) ? [input.site, ...input.integrations].flatMap((sources) => sources[directive] ?? []) : [];
|
|
27
|
+
const parts = [];
|
|
28
|
+
for (const [directive, base] of BASE) {
|
|
29
|
+
const own = declared(directive);
|
|
30
|
+
if (directive === 'worker-src' && own.length === 0)
|
|
31
|
+
continue;
|
|
32
|
+
const extra = directive === 'script-src' && !own.includes(UNSAFE_INLINE) ? (input.hashes ?? []) : [];
|
|
33
|
+
parts.push([directive, ...new Set([...base, ...extra, ...own])].join(' '));
|
|
34
|
+
}
|
|
35
|
+
return parts.join('; ');
|
|
36
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { OgType } from '../../core/registry/define.js';
|
|
2
2
|
import type { HeadApi, HeadLink } from '../../core/registry/render.js';
|
|
3
|
+
import { type TrailingSlash } from '../../core/site/paths.js';
|
|
3
4
|
import { type SafeHtml } from '../../jsx/html.js';
|
|
4
5
|
export interface HeadInput {
|
|
5
6
|
/** The final `<title>` text. */
|
|
@@ -21,6 +22,7 @@ export interface HeadInput {
|
|
|
21
22
|
}>;
|
|
22
23
|
defaultLocale: string;
|
|
23
24
|
baseUrl: string;
|
|
25
|
+
trailingSlash: TrailingSlash;
|
|
24
26
|
preview: boolean;
|
|
25
27
|
/** What the render added through `ctx.head`. */
|
|
26
28
|
collected?: CollectedHead;
|
|
@@ -1,22 +1,9 @@
|
|
|
1
1
|
import { InlayError } from '../../core/errors.js';
|
|
2
2
|
import { isSafeUrl } from '../../core/fields/define.js';
|
|
3
|
-
import {
|
|
3
|
+
import { scriptJson } from '../../core/registry/js.js';
|
|
4
|
+
import { encodePath, presentHref } from '../../core/site/paths.js';
|
|
4
5
|
import { escapeHtml, raw } from '../../jsx/html.js';
|
|
5
6
|
export const NO_HEAD = { meta: [], links: [], jsonLd: [] };
|
|
6
|
-
const SCRIPT_UNSAFE = {
|
|
7
|
-
'<': '\\u003c',
|
|
8
|
-
'>': '\\u003e',
|
|
9
|
-
'&': '\\u0026',
|
|
10
|
-
'\u2028': '\\u2028',
|
|
11
|
-
'\u2029': '\\u2029',
|
|
12
|
-
};
|
|
13
|
-
/** `value` as JSON that can't end its `<script>` or open an HTML comment in it. */
|
|
14
|
-
function scriptJson(value) {
|
|
15
|
-
const json = JSON.stringify(value);
|
|
16
|
-
if (json === undefined)
|
|
17
|
-
throw new InlayError('config', 'config.headJsonLd');
|
|
18
|
-
return json.replace(/[<>&\u2028\u2029]/g, (c) => SCRIPT_UNSAFE[c]);
|
|
19
|
-
}
|
|
20
7
|
const LINK_ATTRS = ['rel', 'hreflang', 'type', 'sizes', 'media', 'as', 'crossorigin', 'href'];
|
|
21
8
|
/** The `ctx.head` of one render, and what it collected. */
|
|
22
9
|
export function createHeadCollector() {
|
|
@@ -46,14 +33,14 @@ export function createHeadCollector() {
|
|
|
46
33
|
};
|
|
47
34
|
return { api, collected: () => ({ meta: [...head.meta], links: [...head.links], jsonLd: [...head.jsonLd] }) };
|
|
48
35
|
}
|
|
49
|
-
function absolute(baseUrl, path) {
|
|
36
|
+
function absolute(baseUrl, path, mode) {
|
|
50
37
|
const [p = '', query] = path.split('?');
|
|
51
|
-
return `${baseUrl}${p === '/' ? '/' : encodePath(p)}${query === undefined ? '' : `?${query}`}`;
|
|
38
|
+
return `${baseUrl}${p === '/' ? '/' : presentHref(encodePath(p), mode)}${query === undefined ? '' : `?${query}`}`;
|
|
52
39
|
}
|
|
53
40
|
const meta = (attr, key, content) => ({ tag: 'meta', attr, key, content });
|
|
54
41
|
/** Inlay's own tags for the page, before any Twitter tags. */
|
|
55
42
|
function inlayTags(input) {
|
|
56
|
-
const url = input.canonical === null ? null : absolute(input.baseUrl, input.canonical);
|
|
43
|
+
const url = input.canonical === null ? null : absolute(input.baseUrl, input.canonical, input.trailingSlash);
|
|
57
44
|
const tags = [{ tag: 'title', text: input.title }];
|
|
58
45
|
if (input.description)
|
|
59
46
|
tags.push(meta('name', 'description', input.description));
|
|
@@ -61,11 +48,11 @@ function inlayTags(input) {
|
|
|
61
48
|
tags.push({ tag: 'link', attrs: { rel: 'canonical', href: url } });
|
|
62
49
|
if (input.alternates.length >= 2) {
|
|
63
50
|
for (const a of input.alternates) {
|
|
64
|
-
tags.push({ tag: 'link', attrs: { rel: 'alternate', hreflang: a.locale, href: absolute(input.baseUrl, a.path) } });
|
|
51
|
+
tags.push({ tag: 'link', attrs: { rel: 'alternate', hreflang: a.locale, href: absolute(input.baseUrl, a.path, input.trailingSlash) } });
|
|
65
52
|
}
|
|
66
53
|
const def = input.alternates.find((a) => a.locale === input.defaultLocale);
|
|
67
54
|
if (def)
|
|
68
|
-
tags.push({ tag: 'link', attrs: { rel: 'alternate', hreflang: 'x-default', href: absolute(input.baseUrl, def.path) } });
|
|
55
|
+
tags.push({ tag: 'link', attrs: { rel: 'alternate', hreflang: 'x-default', href: absolute(input.baseUrl, def.path, input.trailingSlash) } });
|
|
69
56
|
}
|
|
70
57
|
tags.push(meta('property', 'og:type', input.ogType));
|
|
71
58
|
tags.push(meta('property', 'og:title', input.title));
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { FastifyInstance } from 'fastify';
|
|
2
2
|
/** The headers for a site at `baseUrl`: the defaults (HSTS only over https) with the site's own laid over them. */
|
|
3
3
|
export declare function siteHeaders(baseUrl: string, own: Readonly<Record<string, string | false>>): Record<string, string>;
|
|
4
|
-
/**
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Adds `headers` to every response that doesn't set them itself, so the admin's and a route's own headers win. Then,
|
|
6
|
+
* when the response still has no content-security-policy, sets the one `csp` gives, unless that is null.
|
|
7
|
+
*/
|
|
8
|
+
export declare function registerSiteHeaders(app: FastifyInstance, headers: Readonly<Record<string, string>>, csp?: () => Promise<string | null>): void;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* What every response gets unless it sets the header itself: no MIME sniffing
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* What every response gets unless it sets the header itself: no MIME sniffing and no full URLs leaked to other sites.
|
|
3
|
+
* The Content-Security-Policy is not among them: it depends on the integrations switched on, so it comes from
|
|
4
|
+
* `registerSiteHeaders`'s `csp`.
|
|
5
5
|
*/
|
|
6
6
|
const DEFAULTS = {
|
|
7
7
|
'x-content-type-options': 'nosniff',
|
|
8
8
|
'referrer-policy': 'strict-origin-when-cross-origin',
|
|
9
|
-
'content-security-policy': "frame-ancestors 'self'",
|
|
10
9
|
};
|
|
11
10
|
/** One year, without `includeSubDomains`, so other hosts under the same domain are left alone. */
|
|
12
11
|
const HSTS = 'max-age=31536000';
|
|
@@ -19,13 +18,21 @@ export function siteHeaders(baseUrl, own) {
|
|
|
19
18
|
headers[name.toLowerCase()] = value;
|
|
20
19
|
return Object.fromEntries(Object.entries(headers).filter((entry) => entry[1] !== false));
|
|
21
20
|
}
|
|
22
|
-
/**
|
|
23
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Adds `headers` to every response that doesn't set them itself, so the admin's and a route's own headers win. Then,
|
|
23
|
+
* when the response still has no content-security-policy, sets the one `csp` gives, unless that is null.
|
|
24
|
+
*/
|
|
25
|
+
export function registerSiteHeaders(app, headers, csp) {
|
|
24
26
|
const entries = Object.entries(headers);
|
|
25
27
|
app.addHook('onSend', async (_request, reply, payload) => {
|
|
26
28
|
for (const [name, value] of entries)
|
|
27
29
|
if (!reply.hasHeader(name))
|
|
28
30
|
reply.header(name, value);
|
|
31
|
+
if (csp && !reply.hasHeader('content-security-policy')) {
|
|
32
|
+
const policy = await csp();
|
|
33
|
+
if (policy !== null)
|
|
34
|
+
reply.header('content-security-policy', policy);
|
|
35
|
+
}
|
|
29
36
|
return payload;
|
|
30
37
|
});
|
|
31
38
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Core } from '../../core/core.js';
|
|
2
|
+
import type { ActiveIntegration } from '../../core/integrations/integrations.js';
|
|
3
|
+
import type { SiteDefinition } from '../../core/registry/define.js';
|
|
4
|
+
import type { IntegrationPage } from '../../core/registry/integration.js';
|
|
5
|
+
import type { ScriptApi } from '../../core/registry/render.js';
|
|
6
|
+
/** The page an integration renders for, without its `script`. */
|
|
7
|
+
export type IntegrationPageInfo = Omit<IntegrationPage, 'script'>;
|
|
8
|
+
/** The active integrations' output for one page, by slot. */
|
|
9
|
+
export interface RenderedIntegrations {
|
|
10
|
+
head: string;
|
|
11
|
+
bodyStart: string;
|
|
12
|
+
bodyEnd: string;
|
|
13
|
+
}
|
|
14
|
+
export interface SiteIntegrations {
|
|
15
|
+
/** The integrations switched on with valid settings, read once per page-cache generation. */
|
|
16
|
+
active(): Promise<ActiveIntegration[]>;
|
|
17
|
+
/** The header value, or null when the site sets or removes content-security-policy itself. */
|
|
18
|
+
policy(hashes?: readonly string[]): Promise<string | null>;
|
|
19
|
+
/** The versioned URL of the consent script a page with a gated integration loads. */
|
|
20
|
+
consentUrl: string;
|
|
21
|
+
/** The active integrations rendered for `page`; a failure to read or render them is logged and left out. */
|
|
22
|
+
render(page: IntegrationPageInfo, script: ScriptApi): Promise<RenderedIntegrations>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* `generation`: changes on every live change (the page cache's), so a change to the settings is seen on the next call.
|
|
26
|
+
* `log`: where render failures go.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createSiteIntegrations(opts: {
|
|
29
|
+
core: Core;
|
|
30
|
+
site: SiteDefinition;
|
|
31
|
+
generation: () => number;
|
|
32
|
+
consentUrl: string;
|
|
33
|
+
log: (error: unknown, message: string) => void;
|
|
34
|
+
}): SiteIntegrations;
|
|
35
|
+
/**
|
|
36
|
+
* Each of `active`'s slots rendered for `page`, in order, skipping in preview the ones without `preview: true`. A
|
|
37
|
+
* gated integration's output is wrapped in an inert `<template data-inlay-consent>`, and `head` then ends with the
|
|
38
|
+
* consent script. An integration whose render throws is logged and left out of this page.
|
|
39
|
+
*/
|
|
40
|
+
export declare function renderIntegrations(active: readonly ActiveIntegration[], page: IntegrationPageInfo, script: ScriptApi, opts: {
|
|
41
|
+
consentUrl: string;
|
|
42
|
+
log: (name: string, error: unknown) => void;
|
|
43
|
+
}): Promise<RenderedIntegrations>;
|
|
44
|
+
/**
|
|
45
|
+
* `html` with `rendered.head` before its first `</head>`, `bodyStart` right after its `<body>` and `bodyEnd` before
|
|
46
|
+
* its last `</body>`. A slot whose marker is missing is left out.
|
|
47
|
+
*/
|
|
48
|
+
export declare function injectIntegrations(html: string, rendered: RenderedIntegrations): string;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { t } from '../../core/messages/index.js';
|
|
2
|
+
import { escapeHtml, renderToString } from '../../jsx/html.js';
|
|
3
|
+
import { buildCsp } from './csp.js';
|
|
4
|
+
/**
|
|
5
|
+
* `generation`: changes on every live change (the page cache's), so a change to the settings is seen on the next call.
|
|
6
|
+
* `log`: where render failures go.
|
|
7
|
+
*/
|
|
8
|
+
export function createSiteIntegrations(opts) {
|
|
9
|
+
const { core, site, generation, consentUrl, log } = opts;
|
|
10
|
+
const ownPolicy = Object.keys(site.headers).some((name) => name.toLowerCase() === 'content-security-policy');
|
|
11
|
+
let memo = null;
|
|
12
|
+
function active() {
|
|
13
|
+
const current = generation();
|
|
14
|
+
if (memo !== null && memo.generation === current)
|
|
15
|
+
return memo.active;
|
|
16
|
+
const read = core.activeIntegrations();
|
|
17
|
+
memo = { generation: current, active: read };
|
|
18
|
+
// A failed read isn't remembered: the next call tries again.
|
|
19
|
+
read.catch(() => {
|
|
20
|
+
if (memo?.active === read)
|
|
21
|
+
memo = null;
|
|
22
|
+
});
|
|
23
|
+
return read;
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
active,
|
|
27
|
+
async policy(hashes) {
|
|
28
|
+
if (ownPolicy)
|
|
29
|
+
return null;
|
|
30
|
+
// When the settings can't be read, no integration's sources are allowed: the policy fails closed.
|
|
31
|
+
const integrations = (await active().catch(() => [])).map((a) => a.def.csp);
|
|
32
|
+
return buildCsp({ site: site.csp, integrations, ...(hashes && { hashes }) });
|
|
33
|
+
},
|
|
34
|
+
consentUrl,
|
|
35
|
+
async render(page, script) {
|
|
36
|
+
let list;
|
|
37
|
+
try {
|
|
38
|
+
list = await active();
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
log(error, t('log.integrationsUnreadable'));
|
|
42
|
+
return { head: '', bodyStart: '', bodyEnd: '' };
|
|
43
|
+
}
|
|
44
|
+
return renderIntegrations(list, page, script, {
|
|
45
|
+
consentUrl,
|
|
46
|
+
log: (name, error) => log(error, t('log.integrationFailed', { name })),
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const SLOTS = ['head', 'bodyStart', 'bodyEnd'];
|
|
52
|
+
/**
|
|
53
|
+
* Each of `active`'s slots rendered for `page`, in order, skipping in preview the ones without `preview: true`. A
|
|
54
|
+
* gated integration's output is wrapped in an inert `<template data-inlay-consent>`, and `head` then ends with the
|
|
55
|
+
* consent script. An integration whose render throws is logged and left out of this page.
|
|
56
|
+
*/
|
|
57
|
+
export async function renderIntegrations(active, page, script, opts) {
|
|
58
|
+
const out = { head: '', bodyStart: '', bodyEnd: '' };
|
|
59
|
+
let gated = false;
|
|
60
|
+
for (const { def, values } of active) {
|
|
61
|
+
if (page.preview && !def.preview)
|
|
62
|
+
continue;
|
|
63
|
+
const own = { head: '', bodyStart: '', bodyEnd: '' };
|
|
64
|
+
try {
|
|
65
|
+
for (const slot of SLOTS) {
|
|
66
|
+
const render = def[slot];
|
|
67
|
+
if (render)
|
|
68
|
+
own[slot] = await renderToString(render(values, { ...page, script }));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
opts.log(def.name, error);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
for (const slot of SLOTS) {
|
|
76
|
+
const html = own[slot];
|
|
77
|
+
if (html === '')
|
|
78
|
+
continue;
|
|
79
|
+
if (def.consent === undefined) {
|
|
80
|
+
out[slot] += html;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
out[slot] += `<template data-inlay-consent="${escapeHtml(def.consent)}">${html}</template>`;
|
|
84
|
+
gated = true;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (gated)
|
|
89
|
+
out.head += `<script src="${escapeHtml(opts.consentUrl)}" defer></script>`;
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
const BODY = /<body\b[^>]*>/i;
|
|
93
|
+
/**
|
|
94
|
+
* `html` with `rendered.head` before its first `</head>`, `bodyStart` right after its `<body>` and `bodyEnd` before
|
|
95
|
+
* its last `</body>`. A slot whose marker is missing is left out.
|
|
96
|
+
*/
|
|
97
|
+
export function injectIntegrations(html, rendered) {
|
|
98
|
+
const inserts = [];
|
|
99
|
+
const head = html.search(/<\/head>/i);
|
|
100
|
+
if (head >= 0 && rendered.head !== '')
|
|
101
|
+
inserts.push({ at: head, text: rendered.head });
|
|
102
|
+
const body = BODY.exec(html);
|
|
103
|
+
if (body && rendered.bodyStart !== '')
|
|
104
|
+
inserts.push({ at: body.index + body[0].length, text: rendered.bodyStart });
|
|
105
|
+
let end = -1;
|
|
106
|
+
for (const match of html.matchAll(/<\/body>/gi))
|
|
107
|
+
end = match.index;
|
|
108
|
+
if (end >= 0 && rendered.bodyEnd !== '')
|
|
109
|
+
inserts.push({ at: end, text: rendered.bodyEnd });
|
|
110
|
+
inserts.sort((a, b) => a.at - b.at);
|
|
111
|
+
let out = '';
|
|
112
|
+
let from = 0;
|
|
113
|
+
for (const { at, text } of inserts) {
|
|
114
|
+
out += html.slice(from, at) + text;
|
|
115
|
+
from = at;
|
|
116
|
+
}
|
|
117
|
+
return out + html.slice(from);
|
|
118
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { FastifyInstance } from 'fastify';
|
|
2
2
|
import type { Core } from '../../core/core.js';
|
|
3
|
+
/** A variant's file name: `<width>.<format>`. */
|
|
4
|
+
export declare const VARIANT: RegExp;
|
|
3
5
|
/** `GET /media/:id` (the original) and `GET /media/:id/:width.:format` (a resized variant). */
|
|
4
6
|
export declare function registerMediaRoutes(app: FastifyInstance, core: Core): void;
|
|
@@ -2,7 +2,8 @@ import { createReadStream } from 'node:fs';
|
|
|
2
2
|
import { InlayError } from '../../core/errors.js';
|
|
3
3
|
import { isRasterImage } from '../../core/media/detect.js';
|
|
4
4
|
import { t } from '../../core/messages/index.js';
|
|
5
|
-
|
|
5
|
+
/** A variant's file name: `<width>.<format>`. */
|
|
6
|
+
export const VARIANT = /^([1-9][0-9]{0,4})\.(webp|avif|jpeg|png)$/;
|
|
6
7
|
/** RFC 5987 `ext-value` encoding for `filename*`. */
|
|
7
8
|
function encodeFilename(name) {
|
|
8
9
|
return encodeURIComponent(name).replace(/['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
@@ -51,6 +51,8 @@ export interface InlayOptions {
|
|
|
51
51
|
host?: HostApi;
|
|
52
52
|
/** A loaded release's shipped files, by source as written in the site. */
|
|
53
53
|
shippedFiles?: ReadonlyMap<string, string>;
|
|
54
|
+
/** As `shippedFiles`, only the images `.md` files the site imports name. */
|
|
55
|
+
codeFiles?: ReadonlyMap<string, string>;
|
|
54
56
|
/**
|
|
55
57
|
* Nothing can be changed: every write through the core is refused with `error.previewReadOnly`, and the admin and
|
|
56
58
|
* `/api/auth` answer their write requests (other than logging in and out) with 403. Boot still applies shipped
|