domma-cms 1.61.0 → 1.61.2

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.
Files changed (132) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/CLAUDE.md +1 -1
  3. package/docs/DommaFeatures.md +3 -1
  4. package/docs/cache.md +24 -0
  5. package/docs/configuration.md +1 -1
  6. package/package.json +1 -1
  7. package/plugins/_lib/admin/mail/body-menu.js +1 -1
  8. package/plugins/_lib/admin/mail/cms-host.js +1 -1
  9. package/plugins/_lib/admin/mail/compose-window.js +1 -1
  10. package/plugins/_lib/admin/mail/contacts.js +1 -1
  11. package/plugins/_lib/admin/mail/diagnostics-section.js +1 -1
  12. package/plugins/_lib/admin/mail/drag-drop.js +1 -1
  13. package/plugins/_lib/admin/mail/folder-tree.js +1 -1
  14. package/plugins/_lib/admin/mail/font-controls.js +1 -1
  15. package/plugins/_lib/admin/mail/fonts.js +1 -1
  16. package/plugins/_lib/admin/mail/host-context.js +1 -1
  17. package/plugins/_lib/admin/mail/identity.js +1 -1
  18. package/plugins/_lib/admin/mail/image-senders-section.js +1 -1
  19. package/plugins/_lib/admin/mail/keyboard.js +1 -1
  20. package/plugins/_lib/admin/mail/mail.html +1 -1
  21. package/plugins/_lib/admin/mail/message-actions.js +1 -1
  22. package/plugins/_lib/admin/mail/oauth-panel.js +1 -1
  23. package/plugins/_lib/admin/mail/panes.js +1 -1
  24. package/plugins/_lib/admin/mail/reader-kit.js +1 -1
  25. package/plugins/_lib/admin/mail/reader-runtime.js +27 -1
  26. package/plugins/_lib/admin/mail/reader-view.js +1 -1
  27. package/plugins/_lib/admin/mail/resizable.js +1 -1
  28. package/plugins/_lib/admin/mail/restore-spam.js +1 -1
  29. package/plugins/_lib/admin/mail/rules.js +1 -1
  30. package/plugins/_lib/admin/mail/scheduling.js +1 -1
  31. package/plugins/_lib/admin/mail/section-kit.js +1 -1
  32. package/plugins/_lib/admin/mail/security-note.js +1 -1
  33. package/plugins/_lib/admin/mail/settings-section.js +1 -1
  34. package/plugins/_lib/admin/mail/templates.js +1 -1
  35. package/plugins/_lib/admin/mail/threads.js +1 -1
  36. package/plugins/_lib/admin/mail/vacation.js +1 -1
  37. package/plugins/_lib/admin/mail/when-closed.js +1 -1
  38. package/plugins/_lib/admin/ui/dates.js +1 -1
  39. package/plugins/_lib/admin/ui/resizable.js +1 -1
  40. package/plugins/_lib/domma-mail.lock.json +80 -80
  41. package/plugins/_lib/mail/accountFileStore.js +1 -1
  42. package/plugins/_lib/mail/accountRepository.js +1 -1
  43. package/plugins/_lib/mail/accountRoutes.js +1 -1
  44. package/plugins/_lib/mail/bodyTokenProvider.js +1 -1
  45. package/plugins/_lib/mail/bodyTokens.js +1 -1
  46. package/plugins/_lib/mail/compose.js +1 -1
  47. package/plugins/_lib/mail/defaults.js +1 -1
  48. package/plugins/_lib/mail/envelope.js +1 -1
  49. package/plugins/_lib/mail/folderActions.js +1 -1
  50. package/plugins/_lib/mail/folderRuntime.js +1 -1
  51. package/plugins/_lib/mail/folders.js +1 -1
  52. package/plugins/_lib/mail/imapPoolRuntime.js +1 -1
  53. package/plugins/_lib/mail/mbox.js +1 -1
  54. package/plugins/_lib/mail/message-action.js +1 -1
  55. package/plugins/_lib/mail/messageSecurity.js +1 -1
  56. package/plugins/_lib/mail/messageSecurityProvider.js +1 -1
  57. package/plugins/_lib/mail/networkAddresses.js +1 -1
  58. package/plugins/_lib/mail/networkPolicy.js +1 -1
  59. package/plugins/_lib/mail/oauthAccounts.js +1 -1
  60. package/plugins/_lib/mail/oauthRouteRuntime.js +1 -1
  61. package/plugins/_lib/mail/oauthRuntime.js +1 -1
  62. package/plugins/_lib/mail/pollSchedule.js +1 -1
  63. package/plugins/_lib/mail/poller.js +1 -1
  64. package/plugins/_lib/mail/pollerRuntime.js +1 -1
  65. package/plugins/_lib/mail/priority.js +1 -1
  66. package/plugins/_lib/mail/publicMailbox.js +1 -1
  67. package/plugins/_lib/mail/readRuntime.js +1 -1
  68. package/plugins/_lib/mail/render.js +1 -1
  69. package/plugins/_lib/mail/ruleRunner.js +1 -1
  70. package/plugins/_lib/mail/schedulerRuntime.js +1 -1
  71. package/plugins/_lib/mail/secretProvider.js +1 -1
  72. package/plugins/_lib/mail/secretbox.js +1 -1
  73. package/plugins/_lib/mail/senderAuth.js +1 -1
  74. package/plugins/_lib/mail/sieve.js +1 -1
  75. package/plugins/_lib/mail/smtpService.js +1 -1
  76. package/plugins/_lib/mail/snoozeRuntime.js +1 -1
  77. package/plugins/_lib/mail/store.js +1 -1
  78. package/plugins/_lib/mail/subscriptions.js +1 -1
  79. package/plugins/_lib/mail/syncPlan.js +1 -1
  80. package/plugins/_lib/mail/syncRuntime.js +1 -1
  81. package/plugins/_lib/mail/syncSelection.js +1 -1
  82. package/plugins/_lib/mail/unreadBadge.js +1 -1
  83. package/plugins/_lib/mail/unsubscribe.js +1 -1
  84. package/plugins/_lib/mail/vacationRuntime.js +1 -1
  85. package/plugins/_lib/mail/write.js +1 -1
  86. package/plugins/docs/CHANGELOG.md +5 -0
  87. package/plugins/docs/CLAUDE.md +30 -0
  88. package/plugins/docs/admin/views/docs.js +2 -1
  89. package/plugins/docs/admin/views/editor.js +5 -3
  90. package/plugins/docs/docs/guide.md +11 -0
  91. package/plugins/docs/lib/store.js +46 -2
  92. package/plugins/docs/plugin.js +70 -28
  93. package/plugins/docs/plugin.json +14 -1
  94. package/plugins/docs/tests/restricted.test.js +240 -0
  95. package/plugins/free-tier.lock.json +15 -13
  96. package/plugins/mail-reader/CLAUDE.md +6 -0
  97. package/plugins/mail-reader/plugin.js +9 -0
  98. package/plugins/mail-reader/plugin.json +1 -1
  99. package/public/js/effects.js +1 -1
  100. package/public/js/site.js +1 -1
  101. package/server/config.js +37 -0
  102. package/server/routes/api/chats.js +3 -1
  103. package/server/routes/api/components.js +2 -1
  104. package/server/routes/api/content-api.js +1 -1
  105. package/server/routes/api/content-public.js +2 -2
  106. package/server/routes/api/content-writes.js +1 -1
  107. package/server/routes/api/effects.js +4 -9
  108. package/server/routes/api/layouts.js +1 -1
  109. package/server/routes/api/pages.js +6 -5
  110. package/server/routes/api/search.js +3 -3
  111. package/server/routes/api/sidebar-counts.js +2 -0
  112. package/server/routes/api/theme.js +2 -1
  113. package/server/routes/api/views.js +2 -1
  114. package/server/routes/public.js +4 -4
  115. package/server/server.js +12 -6
  116. package/server/services/cache/adminChange.js +28 -0
  117. package/server/services/content.js +73 -10
  118. package/server/services/effectsSettings.js +25 -0
  119. package/server/services/health.js +2 -2
  120. package/server/services/htmlResponse.js +33 -4
  121. package/server/services/listTags.js +1 -1
  122. package/server/services/markdown.js +56 -22
  123. package/server/services/pluginGuides.js +2 -2
  124. package/server/services/plugins.js +25 -2
  125. package/server/services/projects.js +39 -4
  126. package/server/services/publicAssets.js +488 -0
  127. package/server/services/renderer.js +10 -8
  128. package/server/services/seo/index.js +3 -3
  129. package/server/services/sitemap.js +1 -1
  130. package/server/services/versions.js +2 -2
  131. package/server/services/wpImport/index.js +2 -2
  132. package/server/templates/page.html +3 -2
@@ -52,7 +52,8 @@ import {getEffectiveRoles} from './userRoles.js';
52
52
  import {isDcmStaff} from './staffAccess.js';
53
53
  import * as defaultUsersService from './users.js';
54
54
  import {registerLoginCheck, registerPasswordCheck} from './authChecks.js';
55
- import {invalidateTags as cacheInvalidateTags} from './cache/index.js';
55
+ import * as cache from './cache/index.js';
56
+ const cacheInvalidateTags = cache.invalidateTags;
56
57
  import {CORE_TOOLS, getToolStates, toolOpenAt, unmetRequirements} from './tools.js';
57
58
  import {adminPortalFor} from './adminPortal.js';
58
59
 
@@ -714,6 +715,28 @@ export async function getPluginSettings(name) {
714
715
  * @returns {Promise<{ head: string, bodyEnd: string }>}
715
716
  */
716
717
  export async function getInjectionSnippets() {
718
+ if (!cache.isEnabled()) return readInjectionSnippets();
719
+ const now = Date.now();
720
+ if (!_injectionMemo || now - _injectionMemo.at >= INJECTION_MEMO_TTL_MS) {
721
+ const promise = readInjectionSnippets();
722
+ _injectionMemo = {at: now, promise};
723
+ promise.catch(() => { if (_injectionMemo?.promise === promise) _injectionMemo = null; });
724
+ }
725
+ return {...await _injectionMemo.promise};
726
+ }
727
+
728
+ // Every page drawn asks for these, and the answer meant reading every plugin's
729
+ // manifest and snippet files again - a few hundred file operations a page on a
730
+ // site with dozens of plugins. Kept while the response cache is on, dropped
731
+ // with it (switching a plugin on or off is an admin change, which clears the
732
+ // cache), and for no more than a few seconds, so files changed by hand show
733
+ // without a restart.
734
+ const INJECTION_MEMO_TTL_MS = 10_000;
735
+ /** @type {{at: number, promise: Promise<{head: string, headLate: string, bodyEnd: string}>}|null} */
736
+ let _injectionMemo = null;
737
+ cache.onInvalidate(() => { _injectionMemo = null; });
738
+
739
+ async function readInjectionSnippets() {
717
740
  const manifests = await discoverPlugins();
718
741
  const states = getPluginStates();
719
742
 
@@ -849,7 +872,7 @@ export async function setupPlugin(pluginName, services) {
849
872
  for (const filePath of mdFiles) {
850
873
  try {
851
874
  const raw = await fs.readFile(filePath, 'utf8');
852
- const parsed = matter(raw);
875
+ const parsed = matter(raw, {});
853
876
  const rel = path.relative(pagesDir, filePath);
854
877
  let urlPath = '/' + rel.replace(/\\/g, '/').replace(/\.md$/, '').replace(/\/index$/, '');
855
878
  if (urlPath !== '/' && urlPath.endsWith('/')) urlPath = urlPath.slice(0, -1);
@@ -21,6 +21,7 @@
21
21
 
22
22
  import {createEntry, deleteEntry, listEntries, updateEntry} from './collections.js';
23
23
  import {getRoleLevel} from './roles.js';
24
+ import * as cache from './cache/index.js';
24
25
 
25
26
  /** Slug of the preset collection that stores project records. */
26
27
  export const PROJECTS_COLLECTION_SLUG = 'projects';
@@ -77,6 +78,29 @@ export function validateSlug(slug) {
77
78
  * @throws {Error} If the projects collection schema is missing.
78
79
  */
79
80
  export async function listProjects() {
81
+ if (!cache.isEnabled()) return readProjects();
82
+ const now = Date.now();
83
+ if (!_projectsMemo || now - _projectsMemo.at >= PROJECTS_MEMO_TTL_MS) {
84
+ const promise = readProjects();
85
+ _projectsMemo = {at: now, promise};
86
+ promise.catch(() => { if (_projectsMemo?.promise === promise) _projectsMemo = null; });
87
+ }
88
+ return structuredClone(await _projectsMemo.promise);
89
+ }
90
+
91
+ // Every public page request asks which project the page is in and whether that
92
+ // project is switched on, and each answer used to read the whole projects
93
+ // collection. The list is kept while the response cache is on and dropped
94
+ // whenever ANYTHING leaves that cache - an entry write invalidates its
95
+ // collection's tag - so switching a project off still takes effect at once.
96
+ // The short TTL bounds a change the hooks never hear about (a database edited
97
+ // from outside). Cloned out: a caller that edits a row edits its own copy.
98
+ const PROJECTS_MEMO_TTL_MS = 30_000;
99
+ /** @type {{at: number, promise: Promise<Array<object>>}|null} */
100
+ let _projectsMemo = null;
101
+ cache.onInvalidate(() => { _projectsMemo = null; });
102
+
103
+ async function readProjects() {
80
104
  const {entries} = await listEntries(PROJECTS_COLLECTION_SLUG, {limit: 0});
81
105
  const projects = entries.map(e => normalise(e));
82
106
  return projects.sort((a, b) => {
@@ -95,9 +119,7 @@ export async function listProjects() {
95
119
  */
96
120
  export async function getProject(slug) {
97
121
  if (!validateSlug(slug)) return null;
98
- const {entries} = await listEntries(PROJECTS_COLLECTION_SLUG, {limit: 0});
99
- const entry = entries.find(e => e.data?.slug === slug);
100
- return entry ? normalise(entry) : null;
122
+ return (await listProjects()).find(p => p.slug === slug) || null;
101
123
  }
102
124
 
103
125
  /**
@@ -384,7 +406,20 @@ export function resolveArtefactProject(artefact) {
384
406
  */
385
407
  export async function getProjectForPage(urlPath, explicitProject) {
386
408
  if (typeof explicitProject === 'string' && explicitProject) return explicitProject;
387
- const projects = await listProjects();
409
+ return projectForPage(await listProjects(), urlPath, explicitProject);
410
+ }
411
+
412
+ /**
413
+ * getProjectForPage() over a project list the caller already holds - a page
414
+ * listing resolves hundreds of pages against one read of the projects.
415
+ *
416
+ * @param {Array<object>} projects - listProjects()
417
+ * @param {string} urlPath
418
+ * @param {string|null|undefined} explicitProject
419
+ * @returns {string|null}
420
+ */
421
+ export function projectForPage(projects, urlPath, explicitProject) {
422
+ if (typeof explicitProject === 'string' && explicitProject) return explicitProject;
388
423
  const coreFallback = projects.some(p => p.slug === CORE_PROJECT_SLUG) ? CORE_PROJECT_SLUG : null;
389
424
  if (explicitProject === null) return coreFallback;
390
425
  let best = null;
@@ -0,0 +1,488 @@
1
+ /**
2
+ * Long-lived browser caching for what a public page links: its stylesheets,
3
+ * its scripts, and the site's custom CSS.
4
+ *
5
+ * @fastify/static serves every file `Cache-Control: public, max-age=0`, so a
6
+ * visitor going from one page to the next asked the server about every asset
7
+ * again - some twenty-five conditional requests a page, each answered 304.
8
+ * Nothing was downloaded twice, but every one of them is a round trip before
9
+ * the page can finish.
10
+ *
11
+ * Here a rendered page's local stylesheet and script URLs gain a fingerprint,
12
+ * `_pv=<hash of the file>`. A request whose `_pv` matches the file as it is
13
+ * NOW is answered
14
+ * `Cache-Control: public, max-age=31536000, immutable`, so the browser never
15
+ * asks again; change the file and the next page drawn links a new URL.
16
+ *
17
+ * It cannot serve the wrong thing. The file is still served by @fastify/static
18
+ * (and services/assetCache.js) exactly as before - this only decides the
19
+ * cache-control. A `_pv` that does not match - a page kept in the response
20
+ * cache or in a browser tab from before the file changed - gets the current
21
+ * file with the usual `max-age=0`, never a 404 and never a long cache under
22
+ * the wrong name.
23
+ *
24
+ * Modules are different: a module is identified by its URL, and site.js
25
+ * imports forms.js by the same `?v=` URL the page links it with - a
26
+ * fingerprint on one and not the other would run the module twice. So a
27
+ * `<script type="module">` tag whose URL something also imports is never
28
+ * rewritten (one that nothing imports is, and is mapped too). The page gets an
29
+ * import map: every module its entry scripts import, however deep, mapped
30
+ * from the URL it is imported by to that URL with its fingerprint. The
31
+ * browser applies the map to every import of that URL, so all importers still
32
+ * share one instance, and the thirty-odd modules behind site.js stop being
33
+ * asked about on every page. The entry tags' own URLs are left out of the map
34
+ * (so a module that is both linked and imported stays one), a browser too old
35
+ * for import maps ignores it and loads everything as before, and a page that
36
+ * brings its own import map is left alone.
37
+ *
38
+ * Left alone on purpose:
39
+ * - Images. A page's scripts draw images of their own from data (a collection
40
+ * browser's cards, the navbar's logo) by the plain URL; the same picture
41
+ * under two URLs is downloaded twice, which costs a first visit far more
42
+ * than a 304 costs a second.
43
+ * - Anything that is not a .css, .js or .mjs file under public/, Domma's dist
44
+ * or a plugin's public/ folder.
45
+ *
46
+ * The site's custom CSS (content/custom.css) used to be written into every
47
+ * page inside a <style> tag. Past a few kilobytes that is most of the page -
48
+ * sent again with every page view, and held once per page in the response
49
+ * cache. customCssTag() links it as `/content/custom.css` instead, served
50
+ * here with the same fingerprint; a small one stays inline, where it costs
51
+ * less than a request.
52
+ */
53
+ import fs from 'node:fs/promises';
54
+ import path from 'node:path';
55
+ import {createHash} from 'node:crypto';
56
+ import {encode, pickEncoding} from './htmlResponse.js';
57
+
58
+ export const IMMUTABLE = 'public, max-age=31536000, immutable';
59
+ export const VERSION_PARAM = '_pv';
60
+ /** The URL the site's custom CSS is served at once it is too big to inline. */
61
+ export const CUSTOM_CSS_URL = '/content/custom.css';
62
+ /** Custom CSS up to this many bytes stays inline: a request costs more than it saves. */
63
+ export const CUSTOM_CSS_INLINE_MAX = 4096;
64
+
65
+ const TEXT_TYPES = new Set(['.css', '.js', '.mjs']);
66
+
67
+ /** @type {{root: string, dommaDist: string, pluginsDir: string, customCss: string}|null} */
68
+ let _roots = null;
69
+ /** @type {Map<string, {signature: string, version: string, specs?: string[]}>} file -> its version (and, for a script, what it imports), while the file is unchanged */
70
+ const _versions = new Map();
71
+ const VERSIONS_MAX = 5000;
72
+ /** @type {{signature: string, body: Buffer, version: string, encoded: Map<string, Promise<Buffer>>}|null} */
73
+ let _customCss = null;
74
+
75
+ /**
76
+ * Where the files are. Until this is called nothing is fingerprinted and
77
+ * custom CSS stays inline - a render outside a running server (a test, a
78
+ * script) is unchanged.
79
+ *
80
+ * @param {{root: string, dommaDist: string, pluginsDir?: string}|null} roots
81
+ */
82
+ export function configurePublicAssets(roots) {
83
+ _versions.clear();
84
+ _customCss = null;
85
+ if (!roots) { _roots = null; return; }
86
+ _roots = {
87
+ root: path.resolve(roots.root),
88
+ dommaDist: path.resolve(roots.dommaDist),
89
+ pluginsDir: path.resolve(roots.pluginsDir || path.join(roots.root, 'plugins')),
90
+ customCss: path.resolve(roots.root, 'content', 'custom.css')
91
+ };
92
+ }
93
+
94
+ export function publicAssetsConfigured() {
95
+ return _roots !== null;
96
+ }
97
+
98
+ /**
99
+ * The file a public URL path is served from, or null when it is not one this
100
+ * module fingerprints. Mirrors the static mounts in server.js.
101
+ *
102
+ * @param {string} pathname - decoded, no query
103
+ * @returns {{file: string}|null}
104
+ */
105
+ export function resolvePublicAsset(pathname) {
106
+ if (!_roots || typeof pathname !== 'string' || !pathname.startsWith('/')) return null;
107
+ if (pathname.includes('\\') || pathname.includes('\0') || pathname.split('/').includes('..')) return null;
108
+ if (!TEXT_TYPES.has(path.extname(pathname).toLowerCase())) return null;
109
+ if (pathname === CUSTOM_CSS_URL) return {file: _roots.customCss};
110
+ let base;
111
+ let relative;
112
+ if (pathname.startsWith('/dist/domma/')) { base = _roots.dommaDist; relative = pathname.slice(12); }
113
+ else if (pathname.startsWith('/public/')) { base = path.join(_roots.root, 'public'); relative = pathname.slice(8); }
114
+ else if (/^\/plugins\/[^/]+\/public\//.test(pathname)) { base = _roots.pluginsDir; relative = pathname.slice(9); }
115
+ else return null;
116
+ const file = path.resolve(base, relative);
117
+ return file.startsWith(base + path.sep) ? {file} : null;
118
+ }
119
+
120
+ /**
121
+ * A file's version as it is now, or null when it cannot be read. Kept per
122
+ * file while its mtime and size are unchanged, so a file is hashed once.
123
+ *
124
+ * @param {string} file
125
+ * @returns {Promise<string|null>}
126
+ */
127
+ async function versionOf(file) {
128
+ return (await moduleInfo(file))?.version ?? null;
129
+ }
130
+
131
+ /** Read a text asset once: its version and, for a script, the specifiers it imports. */
132
+ async function describe(file, signature) {
133
+ let body;
134
+ try { body = await fs.readFile(file); } catch { return null; }
135
+ const entry = {signature, version: createHash('sha256').update(body).digest('hex').slice(0, 16)};
136
+ if (/\.m?js$/i.test(file)) entry.specs = importSpecifiers(body.toString('utf8'));
137
+ _versions.set(file, entry);
138
+ while (_versions.size > VERSIONS_MAX) _versions.delete(_versions.keys().next().value);
139
+ return entry;
140
+ }
141
+
142
+ /** A file's version (and a script's imports), as it is now; null when it cannot be read. */
143
+ async function moduleInfo(file) {
144
+ let st;
145
+ try { st = await fs.stat(file); } catch { return null; }
146
+ if (!st.isFile()) return null;
147
+ const signature = `${st.mtimeMs}:${st.size}`;
148
+ const kept = _versions.get(file);
149
+ return kept && kept.signature === signature ? kept : describe(file, signature);
150
+ }
151
+
152
+ const IMPORT_FROM = /\b(?:import|export)\b[^'"`;()]*?\bfrom\s*(['"])([^'"\n]+)\1/g;
153
+ const IMPORT_BARE = /\bimport\s*(['"])([^'"\n]+)\1/g;
154
+ const IMPORT_CALL = /\bimport\s*\(\s*(['"])([^'"\n]+)\1\s*\)/g;
155
+
156
+ /**
157
+ * The literal specifiers a script imports - `import ... from`, `export ... from`,
158
+ * `import '...'` and `import('...')` - that name a path (`/`, `./`, `../`).
159
+ * Read with patterns, not a parser: one found in a comment only adds a map
160
+ * entry nothing asks for.
161
+ *
162
+ * @param {string} source
163
+ * @returns {string[]}
164
+ */
165
+ export function importSpecifiers(source) {
166
+ const out = new Set();
167
+ for (const re of [IMPORT_FROM, IMPORT_BARE, IMPORT_CALL]) {
168
+ for (const m of source.matchAll(re)) {
169
+ if (/^\.{0,2}\//.test(m[2]) && !m[2].startsWith('//')) out.add(m[2]);
170
+ }
171
+ }
172
+ return [...out];
173
+ }
174
+
175
+ /** A specifier as the browser resolves it from a module at `fromPath`: `path?query`, or null. */
176
+ function resolveSpecifier(spec, fromPath) {
177
+ try {
178
+ const u = new URL(spec, `http://local${fromPath}`);
179
+ if (u.host !== 'local' || u.hash) return null;
180
+ return u.pathname + u.search;
181
+ } catch { return null; }
182
+ }
183
+
184
+ const MODULE_GRAPH_MAX = 400;
185
+ // Opening tags only, each on its own: pairing a tag with the next </script> would let a stray
186
+ // "<script>" in a comment swallow the real tag after it, and that tag's module would go unnoticed.
187
+ const SCRIPT_OPEN = /<script\b([^>]*)>/gi;
188
+
189
+ /**
190
+ * The import map for a page: every module its module scripts import, however
191
+ * deep, from the URL it is imported by to that URL fingerprinted. '' when
192
+ * there is nothing to map or the page must be left alone (it has an import
193
+ * map of its own, or a module script from somewhere this cannot follow).
194
+ *
195
+ * @param {string} html
196
+ * @returns {Promise<{tag: string, rewrite: Set<string>}>} the <script type="importmap"> tag ('' for none), and
197
+ * the module script URLs whose own tags may be fingerprinted
198
+ */
199
+ async function importMapFor(html) {
200
+ const none = {tag: '', rewrite: new Set()};
201
+ if (!html.includes('module') || /<script\b[^>]*\btype\s*=\s*["']?importmap/i.test(html)) return none;
202
+ const classics = new Set();
203
+ const entries = new Set();
204
+ /** URLs something imports - as against URLs the page itself links. */
205
+ const imported = new Set();
206
+ const queue = [];
207
+ for (const m of html.matchAll(SCRIPT_OPEN)) {
208
+ const tag = `<script${m[1]}>`;
209
+ const type = (read(tag, 'type')?.value || '').trim().toLowerCase();
210
+ const src = read(tag, 'src')?.value;
211
+ if (type !== 'module') {
212
+ // A classic script can still import(): effects.js loads its modules that way. Followed, never mapped itself.
213
+ const url = (!type || type === 'text/javascript' || type === 'application/javascript') && src ? src.replaceAll('&amp;', '&') : '';
214
+ if (url.startsWith('/') && !url.startsWith('//') && !url.includes('#')) { classics.add(url); queue.push(url); }
215
+ continue;
216
+ }
217
+ if (src) {
218
+ const url = src.replaceAll('&amp;', '&');
219
+ // A module from somewhere else could import ours by another route: leave the page alone.
220
+ if (!url.startsWith('/') || url.startsWith('//') || url.includes('#')) return none;
221
+ entries.add(url);
222
+ queue.push(url);
223
+ } else {
224
+ const from = m.index + m[0].length;
225
+ const close = html.indexOf('</script', from);
226
+ for (const spec of importSpecifiers(html.slice(from, close < 0 ? from : close))) {
227
+ if (!spec.startsWith('/')) return none; // relative to the page: not followed
228
+ const url = resolveSpecifier(spec, '/');
229
+ if (url) { imported.add(url); queue.push(url); }
230
+ }
231
+ }
232
+ }
233
+ if (!queue.length) return none;
234
+ /** @type {Map<string, string>} the URL a module is imported by -> its version */
235
+ const found = new Map();
236
+ const walked = new Set();
237
+ while (queue.length && walked.size < MODULE_GRAPH_MAX) {
238
+ const url = queue.pop();
239
+ let pathname;
240
+ try { pathname = decodeURIComponent(url.split('?')[0]); } catch { continue; }
241
+ const asset = resolvePublicAsset(pathname);
242
+ if (!asset || !/\.m?js$/i.test(asset.file)) continue;
243
+ const info = await moduleInfo(asset.file);
244
+ if (!info) continue;
245
+ if (!url.includes(`${VERSION_PARAM}=`)) found.set(url, info.version);
246
+ if (walked.has(asset.file)) continue;
247
+ walked.add(asset.file);
248
+ for (const spec of info.specs || []) {
249
+ const next = resolveSpecifier(spec, pathname);
250
+ if (next) { imported.add(next); queue.push(next); }
251
+ }
252
+ }
253
+ for (const url of classics) found.delete(url);
254
+ // A module the page links AND something imports by that same URL keeps its URL everywhere:
255
+ // it must stay one module, in a browser without import maps too. A linked module nothing
256
+ // imports is fingerprinted in its tag, and mapped as well - so an import this could not see
257
+ // (one built from strings) still lands on the same instance wherever import maps work.
258
+ const rewrite = new Set();
259
+ for (const url of entries) {
260
+ if (imported.has(url)) found.delete(url);
261
+ else if (found.has(url)) rewrite.add(url);
262
+ }
263
+ if (!found.size) return none;
264
+ const imports = {};
265
+ for (const url of [...found.keys()].sort()) {
266
+ imports[url] = `${url}${url.includes('?') ? '&' : '?'}${VERSION_PARAM}=${found.get(url)}`;
267
+ }
268
+ return {tag: `<script type="importmap">${JSON.stringify({imports}).replace(/</g, '\\u003c')}</script>`, rewrite};
269
+ }
270
+
271
+ /** Where an import map must go: before the first script or module preload, and inside the head. */
272
+ function importMapOffset(html) {
273
+ const head = html.search(/<\/head\s*>/i);
274
+ if (head < 0) return -1;
275
+ const first = html.search(/<script\b|<link\b[^>]*\brel\s*=\s*["']?modulepreload/i);
276
+ return first >= 0 && first < head ? first : head;
277
+ }
278
+
279
+ /**
280
+ * The version a URL as written in a page (attribute text, `&amp;` and all)
281
+ * should carry, or null to leave it alone.
282
+ */
283
+ async function versionForAttr(raw) {
284
+ if (!raw.startsWith('/') || raw.startsWith('//') || raw.includes('#')) return null;
285
+ if (raw.includes(`${VERSION_PARAM}=`) || raw.includes('_av=')) return null;
286
+ let pathname;
287
+ try { pathname = decodeURIComponent(raw.split('?')[0]); } catch { return null; }
288
+ const found = resolvePublicAsset(pathname);
289
+ return found ? versionOf(found.file) : null;
290
+ }
291
+
292
+ const withVersion = (raw, version) => `${raw}${raw.includes('?') ? '&amp;' : '?'}${VERSION_PARAM}=${version}`;
293
+
294
+ const TAG = /<(script|link)\b[^>]*>/gi;
295
+ const ATTRS = {
296
+ src: /\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)')/i,
297
+ href: /\shref\s*=\s*(?:"([^"]*)"|'([^']*)')/i,
298
+ type: /\stype\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i,
299
+ rel: /\srel\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i
300
+ };
301
+ const read = (tag, name) => {
302
+ const m = ATTRS[name].exec(tag);
303
+ if (!m) return null;
304
+ const value = m[1] ?? m[2] ?? m[3] ?? '';
305
+ return {value, start: m.index + m[0].indexOf(value, m[0].indexOf('=') + 1)};
306
+ };
307
+
308
+ /** The URLs in one tag that may be fingerprinted: `{start, end, raw}` offsets within the tag. */
309
+ function urlsIn(tag, name) {
310
+ const out = [];
311
+ const push = (attr) => { if (attr && attr.value) out.push({start: attr.start, end: attr.start + attr.value.length, raw: attr.value}); };
312
+ if (name === 'script') {
313
+ const type = (read(tag, 'type')?.value || '').trim().toLowerCase();
314
+ if (type === 'module') {
315
+ // Only where the import map says so (see importMapFor).
316
+ const src = read(tag, 'src');
317
+ if (src && src.value) out.push({start: src.start, end: src.start + src.value.length, raw: src.value, module: true});
318
+ return out;
319
+ }
320
+ if (type && type !== 'text/javascript' && type !== 'application/javascript') return out;
321
+ push(read(tag, 'src'));
322
+ } else if (name === 'link') {
323
+ const rel = (read(tag, 'rel')?.value || '').toLowerCase().split(/\s+/);
324
+ if (!rel.includes('stylesheet')) return out;
325
+ push(read(tag, 'href'));
326
+ }
327
+ return out;
328
+ }
329
+
330
+ /**
331
+ * A rendered page with its local stylesheets and classic scripts
332
+ * fingerprinted, and an import map for its modules. Unchanged when configurePublicAssets() has not run, and on
333
+ * any failure - a page must never fail to render over its cache headers.
334
+ *
335
+ * @param {string} html
336
+ * @returns {Promise<string>}
337
+ */
338
+ export async function fingerprintHtml(html) {
339
+ if (!_roots || typeof html !== 'string' || !html) return html;
340
+ try {
341
+ /** @type {{start: number, end: number, raw: string}[]} absolute offsets */
342
+ const spots = [];
343
+ for (const m of html.matchAll(TAG)) {
344
+ for (const u of urlsIn(m[0], m[1].toLowerCase())) {
345
+ spots.push({start: m.index + u.start, end: m.index + u.end, raw: u.raw, module: u.module});
346
+ }
347
+ }
348
+ const importMap = await importMapFor(html);
349
+ const mapAt = importMap.tag ? importMapOffset(html) : -1;
350
+ if (mapAt >= 0) spots.push({start: mapAt, end: mapAt, raw: '', insert: `${importMap.tag}\n`});
351
+ // A module tag is fingerprinted only with the map in the page that covers it.
352
+ const moduleOk = (raw) => mapAt >= 0 && importMap.rewrite.has(raw.replaceAll('&amp;', '&'));
353
+ for (let i = spots.length - 1; i >= 0; i--) if (spots[i].module && !moduleOk(spots[i].raw)) spots.splice(i, 1);
354
+ if (!spots.length) return html;
355
+ spots.sort((a, b) => a.start - b.start);
356
+ const unique = [...new Set(spots.filter(s => !s.insert).map(s => s.raw))];
357
+ const versions = new Map(await Promise.all(unique.map(async raw => [raw, await versionForAttr(raw)])));
358
+ let out = '';
359
+ let last = 0;
360
+ for (const s of spots) {
361
+ if (s.insert) {
362
+ out += html.slice(last, s.start) + s.insert;
363
+ last = s.start;
364
+ continue;
365
+ }
366
+ const version = versions.get(s.raw);
367
+ if (!version || html.slice(s.start, s.end) !== s.raw) continue;
368
+ out += html.slice(last, s.start) + withVersion(s.raw, version);
369
+ last = s.end;
370
+ }
371
+ return out ? out + html.slice(last) : html;
372
+ } catch {
373
+ return html;
374
+ }
375
+ }
376
+
377
+ /**
378
+ * The site's custom CSS for a page's head: a <link> to CUSTOM_CSS_URL once it
379
+ * is big enough to be worth a request of its own (and this module is
380
+ * configured), otherwise the <style> tag it always was. `inline: true` keeps
381
+ * the <style> whatever the size - a preview is drawn where the page's own
382
+ * requests may not be allowed.
383
+ *
384
+ * @param {string} css - content/custom.css
385
+ * @param {{inline?: boolean}} [opts]
386
+ * @returns {string}
387
+ */
388
+ export function customCssTag(css, {inline = false} = {}) {
389
+ const text = String(css || '');
390
+ if (!text.trim()) return '';
391
+ // A relative url() or @import resolves against the PAGE while the CSS is inline; linked, it
392
+ // would resolve against /content/. Such a stylesheet stays inline, exactly as it was.
393
+ if (!inline && _roots && Buffer.byteLength(text) > CUSTOM_CSS_INLINE_MAX && !RELATIVE_CSS_URL.test(text)) {
394
+ return `<link rel="stylesheet" href="${CUSTOM_CSS_URL}">`;
395
+ }
396
+ return `<style>${text.replace(/<\/style>/gi, '<\\/style>')}</style>`;
397
+ }
398
+
399
+ /** A url(...) or @import "..." that is not absolute (/, a scheme, //), a data: URI or a #fragment. */
400
+ const RELATIVE_CSS_URL = /(?:url\(\s*|@import\s+)(?:["']\s*)?(?!\/|[a-z][a-z0-9+.-]*:|#|["']?\s*\)|url\()[^\s"')]/i;
401
+
402
+ async function readCustomCss() {
403
+ let st;
404
+ try { st = await fs.stat(_roots.customCss); } catch { return null; }
405
+ const signature = `${st.mtimeMs}:${st.size}`;
406
+ if (_customCss && _customCss.signature === signature) return _customCss;
407
+ let body;
408
+ try { body = await fs.readFile(_roots.customCss); } catch { return null; }
409
+ const version = createHash('sha256').update(body).digest('hex').slice(0, 16);
410
+ _versions.set(_roots.customCss, {signature, version});
411
+ _customCss = {signature, body, version, encoded: new Map()};
412
+ return _customCss;
413
+ }
414
+
415
+ /**
416
+ * Wire it into the server: the immutable cache-control for a matching
417
+ * fingerprint, and the route that serves the custom CSS.
418
+ *
419
+ * @param {import('fastify').FastifyInstance} app
420
+ * @param {{root: string, dommaDist: string, pluginsDir?: string}} roots
421
+ * @returns {{serve: () => void}} call serve() once the plugins are registered
422
+ */
423
+ export function registerPublicAssets(app, roots) {
424
+ configurePublicAssets(roots);
425
+
426
+ /** Does this request's `_pv` name the file as it is now? */
427
+ async function matches(rawUrl) {
428
+ const q = rawUrl.indexOf('?');
429
+ if (q < 0 || !rawUrl.includes(`${VERSION_PARAM}=`, q)) return false;
430
+ let pathname;
431
+ let asked;
432
+ try {
433
+ pathname = decodeURIComponent(rawUrl.slice(0, q));
434
+ asked = new URLSearchParams(rawUrl.slice(q + 1)).get(VERSION_PARAM);
435
+ } catch { return false; }
436
+ if (!asked) return false;
437
+ const found = resolvePublicAsset(pathname);
438
+ if (!found) return false;
439
+ return (await versionOf(found.file)) === asked;
440
+ }
441
+
442
+ app.decorateRequest('dmImmutableAsset', false);
443
+ // Added by serve(), after the plugins, as adminAssets' is: a plugin's onRequest hook that
444
+ // answers a request itself (site-manager proxying another site's host) must run first, or
445
+ // this would judge that site's fingerprint against THIS install's files.
446
+ const serve = () => {
447
+ app.addHook('onRequest', async (request) => {
448
+ if (request.method !== 'GET' && request.method !== 'HEAD') return;
449
+ if (!request.raw.url.includes(`${VERSION_PARAM}=`)) return;
450
+ if (request.raw.url.startsWith(CUSTOM_CSS_URL)) return; // its route decides for itself
451
+ if (await matches(request.raw.url)) request.dmImmutableAsset = true;
452
+ });
453
+ app.addHook('onSend', async (request, reply, payload) => {
454
+ if (request.dmImmutableAsset && (reply.statusCode === 200 || reply.statusCode === 304)) {
455
+ reply.header('cache-control', IMMUTABLE);
456
+ }
457
+ return payload;
458
+ });
459
+ };
460
+
461
+ app.route({
462
+ method: ['GET', 'HEAD'],
463
+ url: CUSTOM_CSS_URL,
464
+ handler: async (request, reply) => {
465
+ const css = await readCustomCss();
466
+ if (!css) return reply.code(404).type('text/plain; charset=utf-8').send('Not found\n');
467
+ const asked = (request.query || {})[VERSION_PARAM];
468
+ const encoding = css.body.length >= 1024 ? pickEncoding(request.headers['accept-encoding']) : null;
469
+ const etag = `"${css.version}${encoding ? `-${encoding}` : ''}"`;
470
+ reply.header('cache-control', asked === css.version ? IMMUTABLE : 'public, max-age=0');
471
+ reply.header('etag', etag);
472
+ reply.header('vary', 'accept-encoding');
473
+ reply.type('text/css; charset=utf-8');
474
+ const seen = String(request.headers['if-none-match'] || '').split(',').map(t => t.trim().replace(/^W\//, ''));
475
+ if (seen.some(t => t === `"${css.version}"` || t.startsWith(`"${css.version}-`))) return reply.code(304).send();
476
+ if (!encoding) return reply.send(css.body);
477
+ if (!css.encoded.has(encoding)) {
478
+ const made = encode(css.body, encoding);
479
+ css.encoded.set(encoding, made);
480
+ made.catch(() => css.encoded.delete(encoding));
481
+ }
482
+ const body = await css.encoded.get(encoding);
483
+ reply.header('content-encoding', encoding);
484
+ return reply.send(body);
485
+ }
486
+ });
487
+ return {serve};
488
+ }
@@ -25,6 +25,8 @@ import {cssClasses} from '../../public/js/css-classes.mjs';
25
25
  import {fontFamilyValue, fontStylesheetHref, isFontStylesheet} from '../../public/js/font-families.mjs';
26
26
  import {CMS_WEB_FONTS} from '../../public/js/text-style.mjs';
27
27
  import {parseMarkdown} from './markdown.js';
28
+ import {customCssTag as buildCustomCssTag, fingerprintHtml} from './publicAssets.js';
29
+ import {effectsSettingsScript} from './effectsSettings.js';
28
30
 
29
31
  const VALID_LAYOUT_WIDTHS = new Set(['narrow', 'normal', 'wide', 'full']);
30
32
  const CUSTOM_CSS_PATH = new URL('../../content/custom.css', import.meta.url).pathname;
@@ -553,9 +555,8 @@ export async function renderPage(page, opts = {}) {
553
555
 
554
556
  const {navbarFontLink, navbarStyleTag} = buildNavbarStyleTag(navigation, themeView.cfg.font);
555
557
 
556
- const customCssTag = customCss.trim()
557
- ? `<style>${customCss.replace(/<\/style>/gi, '<\\/style>')}</style>`
558
- : '';
558
+ // Linked once it is big; inline in a preview, which may be drawn where the page's own requests are refused.
559
+ const customCssTag = buildCustomCssTag(customCss, {inline: Boolean(opts.preview || opts.noTrack)});
559
560
 
560
561
  // Compose window.__CMS_FOOTER__ from the menus mapped to footer-primary / footer-legal.
561
562
  const footer = {
@@ -643,6 +644,7 @@ export async function renderPage(page, opts = {}) {
643
644
  footerFallbackHtml,
644
645
  footerScript: footerJson ? `window.__CMS_FOOTER__ = ${footerJson};` : '',
645
646
  ctxMenusScript: ctxMenusScript,
647
+ effectsScript: effectsSettingsScript(),
646
648
  ctxMenusModule: ctxMenusModule,
647
649
  imageZoomModule: imageZoomModule(page.html, overlayMenus, footerBlocksHtml),
648
650
  // Every `<` escaped, not only `</script>`: a label holding `<!--<script` would
@@ -698,7 +700,8 @@ export async function renderPage(page, opts = {}) {
698
700
 
699
701
  const finalVars = applyTransforms('render:beforeRender', vars, {page});
700
702
  const html = interpolate(template, finalVars);
701
- return applyTransforms('render:afterRender', html, {page});
703
+ // Last of all, so what a Tool adds is covered too: local assets fingerprinted for long caching (publicAssets.js).
704
+ return fingerprintHtml(applyTransforms('render:afterRender', html, {page}));
702
705
  }
703
706
 
704
707
  /**
@@ -1355,9 +1358,7 @@ export async function renderBlogPage(templatePath, data = {}, seoMeta = {}) {
1355
1358
  const fontLink = contentFontLinks(`${renderedFragment}${overlayMenus}${footerBlocksHtml}`, themeView.fontLink);
1356
1359
  const {navbarFontLink, navbarStyleTag} = buildNavbarStyleTag(navigation, themeView.cfg.font);
1357
1360
 
1358
- const customCssTag = customCss.trim()
1359
- ? `<style>${customCss.replace(/<\/style>/gi, '<\\/style>')}</style>`
1360
- : '';
1361
+ const customCssTag = buildCustomCssTag(customCss);
1361
1362
 
1362
1363
  // Compose window.__CMS_FOOTER__ from the menus mapped to footer-primary / footer-legal.
1363
1364
  const footer = {
@@ -1433,6 +1434,7 @@ export async function renderBlogPage(templatePath, data = {}, seoMeta = {}) {
1433
1434
  footerFallbackHtml,
1434
1435
  footerScript: footerJson ? `window.__CMS_FOOTER__ = ${footerJson};` : '',
1435
1436
  ctxMenusScript: ctxMenusScript,
1437
+ effectsScript: effectsSettingsScript(),
1436
1438
  ctxMenusModule: ctxMenusModule,
1437
1439
  imageZoomModule: imageZoomModule(renderedFragment, overlayMenus, footerBlocksHtml),
1438
1440
  // Every `<` escaped, as on the page path above.
@@ -1472,7 +1474,7 @@ export async function renderBlogPage(templatePath, data = {}, seoMeta = {}) {
1472
1474
 
1473
1475
  // The same last step as a page: a Tool's render:afterRender sees a blog post or a Tool's page too. Not a
1474
1476
  // bare page: its address is a credential, so nothing of a Tool's goes on it.
1475
- return bare ? interpolate(template, vars) : applyTransforms('render:afterRender', interpolate(template, vars), {page: null});
1477
+ return fingerprintHtml(bare ? interpolate(template, vars) : applyTransforms('render:afterRender', interpolate(template, vars), {page: null}));
1476
1478
  }
1477
1479
 
1478
1480
  /**