@o-a/cms-agent 0.1.7 → 0.2.1

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 (50) hide show
  1. package/README.md +7 -16
  2. package/dist/boot.d.ts +2 -0
  3. package/dist/boot.js +3 -1
  4. package/dist/config.d.ts +0 -1
  5. package/dist/config.js +0 -1
  6. package/dist/create-site/cli.js +0 -0
  7. package/dist/create-site/generate-site.js +1 -1
  8. package/dist/create-site/mint-token-cli.js +0 -0
  9. package/dist/create-site/template/AGENTS.md +191 -0
  10. package/dist/create-site/template/vhost/Dockerfile +1 -1
  11. package/dist/media/filename.js +4 -1
  12. package/dist/migrations/index.d.ts +1 -1
  13. package/dist/migrations/index.js +24 -1
  14. package/dist/renderer/render-cache.d.ts +10 -0
  15. package/dist/renderer/render-cache.js +11 -0
  16. package/dist/renderer/render-page.d.ts +2 -0
  17. package/dist/renderer/render-page.js +40 -1
  18. package/dist/routes/admin-redirect.d.ts +5 -0
  19. package/dist/routes/admin-redirect.js +26 -0
  20. package/dist/routes/capabilities.js +2 -2
  21. package/dist/routes/media-public.js +6 -0
  22. package/dist/routes/preview-revision.js +3 -19
  23. package/dist/routes/preview.js +0 -18
  24. package/dist/routes/public.d.ts +2 -0
  25. package/dist/routes/public.js +25 -30
  26. package/dist/routes/search-public.d.ts +6 -0
  27. package/dist/routes/search-public.js +104 -0
  28. package/dist/routes/search.js +4 -0
  29. package/dist/routes/sitemap.js +4 -10
  30. package/dist/schemas/page.schema.json +6 -0
  31. package/dist/search/drivers/node-sqlite-driver.d.ts +5 -1
  32. package/dist/search/drivers/node-sqlite-driver.js +2 -2
  33. package/dist/search/query-content.d.ts +32 -0
  34. package/dist/search/query-content.js +207 -0
  35. package/dist/search/rebuild-index.js +248 -55
  36. package/dist/server-config.d.ts +1 -0
  37. package/dist/server-config.js +28 -1
  38. package/dist/server.js +22 -0
  39. package/dist/services/content-read.js +5 -10
  40. package/dist/services/delete-content.js +2 -13
  41. package/dist/services/manage-redirects.js +5 -15
  42. package/dist/services/migration-runner.js +55 -13
  43. package/dist/services/publish.js +8 -14
  44. package/dist/services/rate-limit-config.d.ts +1 -1
  45. package/dist/services/rate-limit-config.js +6 -4
  46. package/dist/services/theme-schemas.js +2 -2
  47. package/dist/services/validation.d.ts +0 -1
  48. package/dist/services/validation.js +0 -11
  49. package/package.json +2 -2
  50. package/dist/schemas/post.schema.json +0 -25
@@ -1,10 +1,20 @@
1
- import { mkdirSync, readFileSync, unlinkSync } from 'node:fs';
1
+ import { randomUUID } from 'node:crypto';
2
+ import { mkdirSync, readFileSync, renameSync, unlinkSync } from 'node:fs';
2
3
  import { join } from 'node:path';
4
+ import { setImmediate as yieldToEventLoop } from 'node:timers/promises';
3
5
  import { listFilesRecursively } from "../services/fs-walk.js";
4
- import { postPathToUrl } from "../services/post-urls.js";
6
+ import { loadThemeSchemas } from "../services/theme-schemas.js";
5
7
  import { pagePathToUrl } from "../services/urls.js";
6
8
  import { enqueue } from "../services/write-queue.js";
7
9
  import { openNodeSqliteDriver } from "./drivers/node-sqlite-driver.js";
10
+ // How many content files the rebuild loop processes between yields to
11
+ // the event loop (see the yield's own comment below for why this
12
+ // exists at all). Large enough that setImmediate's own overhead is
13
+ // negligible next to the real per-file work (a parse plus several
14
+ // SQLite inserts); small enough that no single slice runs long enough
15
+ // to meaningfully stall another request. Not a config knob - no
16
+ // evidence yet this needs to be tunable per site.
17
+ const YIELD_EVERY_N_FILES = 25;
8
18
  function collectStrings(value, out) {
9
19
  if (typeof value === 'string') {
10
20
  out.push(value);
@@ -39,73 +49,256 @@ function extractBody(instances) {
39
49
  walk(instances);
40
50
  return strings.join(' ');
41
51
  }
42
- async function rebuildIndexJob(config) {
43
- mkdirSync(config.dataRoot, { recursive: true });
44
- // Delete-then-recreate, not existsSync-then-unlinkSync (a TOCTOU
45
- // gap): this makes every rebuild start from a genuinely clean file,
46
- // which is also what makes "delete the index and rebuild produces
47
- // equivalent results" (G2) a structural consequence rather than a
48
- // special case.
52
+ // Epoch milliseconds for a date-like string - undefined if it doesn't
53
+ // parse, so a malformed date is silently skipped rather than indexed
54
+ // as a nonsensical NaN row (the same "malformed input skipped, not a
55
+ // rebuild failure" tolerance every other part of this pipeline
56
+ // already has for a bad file or a missing theme type).
57
+ function parseDateValue(value) {
58
+ if (typeof value !== 'string') {
59
+ return undefined;
60
+ }
61
+ const parsed = Date.parse(value);
62
+ return Number.isFinite(parsed) ? parsed : undefined;
63
+ }
64
+ // Turns one raw value into ApiFieldRow entries for a given field key -
65
+ // shared by both theme-flagged fields (extractInstanceApiFields below)
66
+ // and the built-in post envelope fields (extractEnvelopeApiFields),
67
+ // since both need the identical "what does this value actually mean
68
+ // for indexing" logic. Three shapes:
69
+ // - an array: one row per scalar element, all under the same
70
+ // fieldKey (e.g. a post's own "tags") - a plain "eq" filter then
71
+ // matches via the exact same mechanism a single-valued field already
72
+ // uses, no separate array-aware query logic needed anywhere else.
73
+ // - a date-like string (isDateField true - a theme field schema'd
74
+ // "type": "string", "format": "date", reusing the existing format
75
+ // convention, or the post envelope's own publishDate): stored as an
76
+ // epoch-ms number in valueNumber, not text, so range operators work
77
+ // on it through the same numeric path a flagged price field uses.
78
+ // - a plain scalar (string/number/boolean): stored in its own typed
79
+ // column. Anything else (object, null, undefined) has nothing
80
+ // sensible to store or compare and is silently skipped, the same way
81
+ // a malformed schema block already is elsewhere in this pipeline.
82
+ function pushFieldValue(blockType, instanceId, fieldKey, value, isDateField, out) {
83
+ if (Array.isArray(value)) {
84
+ for (const item of value) {
85
+ pushFieldValue(blockType, instanceId, fieldKey, item, isDateField, out);
86
+ }
87
+ return;
88
+ }
89
+ if (typeof value === 'string' && isDateField) {
90
+ const epoch = parseDateValue(value);
91
+ if (epoch !== undefined) {
92
+ out.push({ blockType, instanceId, fieldKey, valueText: null, valueNumber: epoch, valueBool: null });
93
+ }
94
+ return;
95
+ }
96
+ if (typeof value === 'string') {
97
+ out.push({ blockType, instanceId, fieldKey, valueText: value, valueNumber: null, valueBool: null });
98
+ }
99
+ else if (typeof value === 'number') {
100
+ out.push({ blockType, instanceId, fieldKey, valueText: null, valueNumber: value, valueBool: null });
101
+ }
102
+ else if (typeof value === 'boolean') {
103
+ out.push({ blockType, instanceId, fieldKey, valueText: null, valueNumber: null, valueBool: value ? 1 : 0 });
104
+ }
105
+ }
106
+ // Reads schema.properties for the given instance's own type, keeping
107
+ // only properties explicitly flagged "api": true (an unvalidated,
108
+ // theme-authored JSON Schema keyword - same status as "format"/
109
+ // "allowedBlocks", see docs/guide-theme-authoring.md and
110
+ // services/validation.ts's own allowedBlockTypesOf) - and pairs each
111
+ // with its actual value out of instance.settings via pushFieldValue.
112
+ function extractInstanceApiFields(instance, schemaMap, out) {
113
+ const type = typeof instance.type === 'string' ? instance.type : undefined;
114
+ const id = typeof instance.id === 'string' ? instance.id : undefined;
115
+ if (!type || !id) {
116
+ return;
117
+ }
118
+ const properties = schemaMap[type]?.properties;
119
+ if (!properties || typeof properties !== 'object') {
120
+ return;
121
+ }
122
+ const settings = (instance.settings && typeof instance.settings === 'object' ? instance.settings : {});
123
+ for (const [key, propSchema] of Object.entries(properties)) {
124
+ const schema = propSchema;
125
+ if (schema?.api !== true) {
126
+ continue;
127
+ }
128
+ const isDateField = schema.type === 'string' && schema.format === 'date';
129
+ pushFieldValue(type, id, key, settings[key], isDateField, out);
130
+ }
131
+ }
132
+ // Built-in envelope fields, auto-indexed with no "api": true needed -
133
+ // author/publishDate/tags are optional on every page (page.schema.json),
134
+ // so indexing is presence-based rather than gated on a specific "type"
135
+ // value: any page carrying one of these fields gets it indexed,
136
+ // regardless of what its own type string is. block_type '__page__' is a
137
+ // sentinel (never a real theme type, which always matches a *.liquid
138
+ // filename) marking these rows as envelope-level rather than a real
139
+ // section/block instance; instanceId is the page's own url - stable
140
+ // and unique enough, since there's exactly one envelope per page.
141
+ function extractEnvelopeApiFields(page, url) {
142
+ const rows = [];
143
+ pushFieldValue('__page__', url, 'author', page.author, false, rows);
144
+ pushFieldValue('__page__', url, 'publishDate', page.publishDate, true, rows);
145
+ pushFieldValue('__page__', url, 'tags', page.tags, false, rows);
146
+ return rows;
147
+ }
148
+ // Top-level page.sections entries are sections; every level of nested
149
+ // .blocks (arbitrarily deep - instance.schema.json's blocks is self-
150
+ // referential, same reasoning extractBody's own walk above already
151
+ // documents) is a block, so which theme-schema map applies flips
152
+ // exactly once, at the top, and stays fixed for everything nested
153
+ // underneath.
154
+ function extractApiFields(sections, sectionSchemas, blockSchemas) {
155
+ const rows = [];
156
+ const walk = (list, schemaMap) => {
157
+ if (!list) {
158
+ return;
159
+ }
160
+ for (const instance of list) {
161
+ extractInstanceApiFields(instance, schemaMap, rows);
162
+ walk(instance.blocks, blockSchemas);
163
+ }
164
+ };
165
+ walk(sections, sectionSchemas);
166
+ return rows;
167
+ }
168
+ function unlinkIfExists(path) {
49
169
  try {
50
- unlinkSync(config.searchIndexPath);
170
+ unlinkSync(path);
51
171
  }
52
172
  catch (error) {
53
173
  if (error.code !== 'ENOENT') {
54
174
  throw error;
55
175
  }
56
176
  }
57
- const driver = openNodeSqliteDriver(config.searchIndexPath);
177
+ }
178
+ // A fresh sqlite file is never actually left in WAL mode by this
179
+ // module (nothing here turns that on), but cleaning up any stray
180
+ // -wal/-shm sidecar files defensively costs nothing and avoids ever
181
+ // leaving one behind next to an abandoned temp build.
182
+ function cleanupSqliteArtifacts(path) {
183
+ unlinkIfExists(path);
184
+ unlinkIfExists(`${path}-wal`);
185
+ unlinkIfExists(`${path}-shm`);
186
+ }
187
+ async function rebuildIndexJob(config) {
188
+ mkdirSync(config.dataRoot, { recursive: true });
189
+ // Built into a fresh temp file, then renamed atomically over the
190
+ // real path (below) - not the old delete-then-recreate-in-place
191
+ // approach, which left a real window where a concurrent read saw
192
+ // either a missing file or one that exists but has no tables in it
193
+ // yet. POSIX rename() is atomic: a reader always sees either the
194
+ // complete old index or the complete new one, never an in-between
195
+ // state, and never has to retry an open that landed in the gap.
196
+ const tmpPath = `${config.searchIndexPath}.tmp-${randomUUID()}`;
197
+ cleanupSqliteArtifacts(tmpPath);
198
+ const themeSchemas = loadThemeSchemas(config.themeRoot);
58
199
  try {
59
- driver.exec('CREATE VIRTUAL TABLE pages_fts USING fts5(url UNINDEXED, title, body)');
60
- const insert = driver.prepare('INSERT INTO pages_fts (url, title, body) VALUES (?, ?, ?)');
61
- // Posts are genuinely public, URL-addressable content search
62
- // should cover, same as pages - only the root and the URL mapping
63
- // differ. Menus are deliberately never walked here at all: they
64
- // have no public URL to point a search result at.
65
- const collections = [
66
- { root: config.pagesRoot, toUrl: pagePathToUrl },
67
- { root: config.postsRoot, toUrl: postPathToUrl },
68
- ];
69
- driver.exec('BEGIN');
70
- for (const { root, toUrl } of collections) {
71
- for (const relativePath of listFilesRecursively(root, root, '.json')) {
72
- let page;
73
- try {
74
- page = JSON.parse(readFileSync(join(root, relativePath), 'utf-8'));
200
+ const driver = openNodeSqliteDriver(tmpPath);
201
+ try {
202
+ driver.exec('CREATE VIRTUAL TABLE pages_fts USING fts5(url UNINDEXED, title, body, page_type UNINDEXED)');
203
+ // A plain table, not FTS5 - page_fields holds typed, exact/range-
204
+ // comparable values (a price, a rating), the opposite of pages_fts's
205
+ // own free-text matching. One row per exposed field per instance
206
+ // (not one column per field name): a page can carry several
207
+ // instances of the same block type (several "product" blocks on one
208
+ // listing page), each with its own value, and different pages may
209
+ // expose entirely different field sets - a fixed column-per-field
210
+ // schema can't accommodate either. No foreign key back to
211
+ // pages_fts.url - this whole index is disposable and rebuilt wholly
212
+ // from scratch every time, same as pages_fts itself.
213
+ driver.exec('CREATE TABLE page_fields (url TEXT NOT NULL, block_type TEXT NOT NULL, instance_id TEXT NOT NULL, field_key TEXT NOT NULL, value_text TEXT, value_number REAL, value_bool INTEGER)');
214
+ // Composite, not a bare field_key index - field_key alone only
215
+ // narrows to one field's rows; leading with it here still serves
216
+ // that same narrowing (the leftmost-column rule), but the second
217
+ // column also covers the typed value comparison itself (an
218
+ // equality or range check) without a further per-row scan. url
219
+ // supports the self-join a multi-field query ANDs together
220
+ // (queryContent, query-content.ts) - with no index there, ANDing a
221
+ // second filter means scanning page_fields in full for every row
222
+ // the first filter matched.
223
+ driver.exec('CREATE INDEX page_fields_key_number ON page_fields (field_key, value_number)');
224
+ driver.exec('CREATE INDEX page_fields_key_text ON page_fields (field_key, value_text)');
225
+ driver.exec('CREATE INDEX page_fields_url ON page_fields (url)');
226
+ const insert = driver.prepare('INSERT INTO pages_fts (url, title, body, page_type) VALUES (?, ?, ?, ?)');
227
+ const insertField = driver.prepare('INSERT INTO page_fields (url, block_type, instance_id, field_key, value_text, value_number, value_bool) VALUES (?, ?, ?, ?, ?, ?, ?)');
228
+ // Menus are deliberately never walked here at all: they have no
229
+ // public URL to point a search result at.
230
+ const collections = [{ root: config.pagesRoot, toUrl: pagePathToUrl }];
231
+ driver.exec('BEGIN');
232
+ let filesExamined = 0;
233
+ for (const { root, toUrl } of collections) {
234
+ for (const relativePath of listFilesRecursively(root, root, '.json')) {
235
+ // A genuine macrotask yield (setImmediate, not a microtask like
236
+ // Promise.resolve()/queueMicrotask - Node drains every queued
237
+ // microtask before the event loop ever reaches its I/O phases,
238
+ // so a chain of only-microtask yields still fully blocks an
239
+ // incoming HTTP request from being processed). Without this,
240
+ // this loop's entire body - potentially thousands of files -
241
+ // runs as one uninterruptible synchronous block: since Node is
242
+ // single-threaded, that means every other request the server
243
+ // is handling (auth, content reads, publishes) stalls for the
244
+ // rebuild's whole duration, not just other search queries.
245
+ // Counted once per file examined regardless of whether it
246
+ // ends up skipped below, so the cadence tracks total work
247
+ // done, not just files actually indexed.
248
+ filesExamined += 1;
249
+ if (filesExamined % YIELD_EVERY_N_FILES === 0) {
250
+ await yieldToEventLoop();
251
+ }
252
+ let page;
253
+ try {
254
+ page = JSON.parse(readFileSync(join(root, relativePath), 'utf-8'));
255
+ }
256
+ catch {
257
+ // A malformed individual file is skipped, not an all-or-nothing
258
+ // abort: the index is explicitly disposable/best-effort, and
259
+ // aborting the whole rebuild over one bad file would leave no
260
+ // working index at all - strictly worse than skipping one page.
261
+ continue;
262
+ }
263
+ // Never walks draftsRoot at all, and skips unpublished content
264
+ // here - both halves of "drafts and unpublished content are
265
+ // absent from the index" (G3) are true by construction, not by
266
+ // a filter that could be gotten wrong.
267
+ if (page.published === false) {
268
+ continue;
269
+ }
270
+ const url = toUrl(relativePath);
271
+ const title = typeof page.title === 'string' ? page.title : '';
272
+ const pageType = typeof page.type === 'string' ? page.type : '';
273
+ const body = extractBody(page.sections);
274
+ insert.run(url, title, body, pageType);
275
+ const apiFields = [
276
+ ...extractApiFields(page.sections, themeSchemas.sections, themeSchemas.blocks),
277
+ ...extractEnvelopeApiFields(page, url),
278
+ ];
279
+ for (const row of apiFields) {
280
+ insertField.run(url, row.blockType, row.instanceId, row.fieldKey, row.valueText, row.valueNumber, row.valueBool);
281
+ }
75
282
  }
76
- catch {
77
- // A malformed individual file is skipped, not an all-or-nothing
78
- // abort: the index is explicitly disposable/best-effort, and
79
- // aborting the whole rebuild over one bad file would leave no
80
- // working index at all - strictly worse than skipping one page.
81
- continue;
82
- }
83
- // Never walks draftsRoot at all, and skips unpublished content
84
- // here - both halves of "drafts and unpublished content are
85
- // absent from the index" (G3) are true by construction, not by
86
- // a filter that could be gotten wrong.
87
- if (page.published === false) {
88
- continue;
89
- }
90
- const url = toUrl(relativePath);
91
- const title = typeof page.title === 'string' ? page.title : '';
92
- const body = extractBody(page.sections);
93
- insert.run(url, title, body);
94
283
  }
284
+ driver.exec('COMMIT');
95
285
  }
96
- driver.exec('COMMIT');
286
+ finally {
287
+ driver.close();
288
+ }
289
+ renameSync(tmpPath, config.searchIndexPath);
97
290
  }
98
- finally {
99
- driver.close();
291
+ catch (error) {
292
+ cleanupSqliteArtifacts(tmpPath);
293
+ throw error;
100
294
  }
101
295
  }
102
- // Queued via enqueue(), not because constraint 6 literally demands it
103
- // for a non-authoritative index, but because of a same-process race
104
- // specific to this delete-then-recreate design: two concurrent
105
- // rebuilds can interleave so one's unlink races another's open+CREATE,
106
- // or one can unlink the file out from under another's in-progress
107
- // transaction. enqueue() is a generic, domain-agnostic primitive, so
108
- // reusing it for self-exclusion costs nothing.
296
+ // Queued via enqueue() - two concurrent rebuilds could otherwise both
297
+ // build their own temp file and both attempt the final rename; the
298
+ // second rename would still win cleanly (rename() just replaces
299
+ // whatever is there), but the first rebuild's now-orphaned temp file
300
+ // would never get cleaned up. enqueue() is a generic, domain-agnostic
301
+ // primitive, so reusing it for self-exclusion costs nothing.
109
302
  export function rebuildIndex(config) {
110
303
  return enqueue(() => rebuildIndexJob(config));
111
304
  }
@@ -18,5 +18,6 @@ export interface ServerConfig {
18
18
  ipAllowlist: string[];
19
19
  checkpointIntervalMs: number;
20
20
  media: MediaConfig;
21
+ adminBaseUrl: string | undefined;
21
22
  }
22
23
  export declare function loadServerConfig(siteRoot: string): ServerConfig;
@@ -128,6 +128,31 @@ function parseIpAllowlist(value) {
128
128
  });
129
129
  return value;
130
130
  }
131
+ // Undefined -> undefined: absence means the /admin redirect feature
132
+ // is off entirely, not a default target to redirect to (there's no
133
+ // sensible default admin URL to assume). Validated as a real absolute
134
+ // http(s) URL, not just any non-empty string, since it becomes a
135
+ // redirect target - rejects `javascript:`/`ftp:`/anything else before
136
+ // it can ever reach a Location header.
137
+ function parseAdminBaseUrl(value) {
138
+ if (value === undefined) {
139
+ return undefined;
140
+ }
141
+ if (typeof value !== 'string' || value.length === 0) {
142
+ throw new StartupCheckError('invalid-site-config', `site.config.json's "adminBaseUrl" must be a non-empty string, got ${JSON.stringify(value)}`);
143
+ }
144
+ let parsed;
145
+ try {
146
+ parsed = new URL(value);
147
+ }
148
+ catch {
149
+ throw new StartupCheckError('invalid-site-config', `site.config.json's "adminBaseUrl" must be a valid URL, got ${JSON.stringify(value)}`);
150
+ }
151
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
152
+ throw new StartupCheckError('invalid-site-config', `site.config.json's "adminBaseUrl" must be an http or https URL, got ${JSON.stringify(value)}`);
153
+ }
154
+ return value;
155
+ }
131
156
  function parseCheckpointIntervalMs(value) {
132
157
  if (value === undefined) {
133
158
  return DEFAULT_CHECKPOINT_INTERVAL_MS;
@@ -182,6 +207,7 @@ export function loadServerConfig(siteRoot) {
182
207
  ipAllowlist: [],
183
208
  checkpointIntervalMs: DEFAULT_CHECKPOINT_INTERVAL_MS,
184
209
  media: { maxUploadBytes: DEFAULT_MEDIA_MAX_UPLOAD_BYTES },
210
+ adminBaseUrl: undefined,
185
211
  };
186
212
  }
187
213
  let parsed;
@@ -206,5 +232,6 @@ export function loadServerConfig(siteRoot) {
206
232
  const ipAllowlist = parseIpAllowlist(record.ipAllowlist);
207
233
  const checkpointIntervalMs = parseCheckpointIntervalMs(record.checkpointIntervalMs);
208
234
  const media = parseMedia(record.media);
209
- return { port: resolvePort(port), tokens, rateLimit, trustProxy, ipAllowlist, checkpointIntervalMs, media };
235
+ const adminBaseUrl = parseAdminBaseUrl(record.adminBaseUrl);
236
+ return { port: resolvePort(port), tokens, rateLimit, trustProxy, ipAllowlist, checkpointIntervalMs, media, adminBaseUrl };
210
237
  }
package/dist/server.js CHANGED
@@ -2,10 +2,12 @@ import rateLimitPlugin from '@fastify/rate-limit';
2
2
  import Fastify, {} from 'fastify';
3
3
  import { bootSite } from "./boot.js";
4
4
  import { loadServerConfig } from "./server-config.js";
5
+ import { adminRedirectRoutes } from "./routes/admin-redirect.js";
5
6
  import { assetsRoutes } from "./routes/assets.js";
6
7
  import { v1Routes } from "./routes/index.js";
7
8
  import { mediaPublicRoutes } from "./routes/media-public.js";
8
9
  import { publicRoutes } from "./routes/public.js";
10
+ import { searchPublicRoutes } from "./routes/search-public.js";
9
11
  import { sitemapRoutes } from "./routes/sitemap.js";
10
12
  import { CHECKPOINT_AUTHOR, runCheckpoint } from "./services/checkpoint.js";
11
13
  import { startDevTunnel } from "./services/dev-tunnel.js";
@@ -76,6 +78,7 @@ export function buildServer(booted, serverConfig, options = {}) {
76
78
  themeTemplates: booted.themeTemplates,
77
79
  layouts: booted.layouts,
78
80
  engine: booted.engine,
81
+ renderCache: booted.renderCache,
79
82
  });
80
83
  // A more specific static-prefixed route than the public catch-all's
81
84
  // own /* wildcard - Fastify prefers it regardless of registration
@@ -98,6 +101,25 @@ export function buildServer(booted, serverConfig, options = {}) {
98
101
  // also always wins over a same-named static file under
99
102
  // theme/root/sitemap.xml (see routes/public.ts's root-mirror check).
100
103
  app.register(sitemapRoutes, { config: booted.config });
104
+ // Same "more specific than the public catch-all" reasoning again - a
105
+ // stable, public, front-end-facing contract (a theme's own JS calls
106
+ // it directly), not part of the versioned /v1 admin/integration
107
+ // surface, so it lives alongside these other unprefixed routes
108
+ // rather than inside v1Routes (search.ts's /v1/search/rebuild is the
109
+ // authenticated write side that does). GET /search.json, not
110
+ // GET /search: a single reserved path, not a whole prefix, so a
111
+ // site's own content page can still live at the bare /search URL
112
+ // (granite-starter's search-demo section does exactly that).
113
+ app.register(searchPublicRoutes, { config: booted.config });
114
+ // Conditional, unlike every other registration above - this is what
115
+ // makes GET /admin genuinely opt-in rather than a reserved
116
+ // namespace: with no adminBaseUrl configured, the route is never
117
+ // registered at all, so an unconfigured site can still use "admin"
118
+ // as an ordinary page path. See routes/admin-redirect.ts's own
119
+ // comment for the full reasoning.
120
+ if (serverConfig.adminBaseUrl) {
121
+ app.register(adminRedirectRoutes, { adminBaseUrl: serverConfig.adminBaseUrl });
122
+ }
101
123
  return app;
102
124
  }
103
125
  // Neither a recurring timer nor process signal handling exists
@@ -4,7 +4,6 @@ import { computeEtag } from "./etag.js";
4
4
  import { listFilesRecursively } from "./fs-walk.js";
5
5
  import { sanitisePath } from "./path-safety.js";
6
6
  import { pagePathToUrl } from "./urls.js";
7
- import { postPathToUrl } from "./post-urls.js";
8
7
  export class ContentReadError extends Error {
9
8
  reason;
10
9
  constructor(reason, message) {
@@ -29,7 +28,7 @@ export function readContentFile(root, relativePath) {
29
28
  return { bytes, etag: computeEtag(bytes) };
30
29
  }
31
30
  // Both contentRoot-relative live paths and draftsRoot-relative draft
32
- // paths share the identical pages/posts/menus subroot prefix shape
31
+ // paths share the identical pages/menus subroot prefix shape
33
32
  // (content/drafts/ mirrors content/'s own layout), so one function
34
33
  // handles both with no branching on which root an entry came from.
35
34
  // Menus have no public URL - there is no preview route for them.
@@ -37,9 +36,6 @@ function computeContentUrl(relativePath) {
37
36
  if (relativePath.startsWith('pages/')) {
38
37
  return pagePathToUrl(relativePath.slice('pages/'.length));
39
38
  }
40
- if (relativePath.startsWith('posts/')) {
41
- return postPathToUrl(relativePath.slice('posts/'.length));
42
- }
43
39
  return null;
44
40
  }
45
41
  // The later of the live file's and the draft's own mtime, whichever
@@ -87,20 +83,19 @@ function readSummary(fullPath) {
87
83
  // anywhere in the build plan - this is the only place a draft-only
88
84
  // page (never yet published) is discoverable at all.
89
85
  //
90
- // Deliberately three named walks (pages/posts/menus), not one broad
91
- // walk of contentRoot: since Group N nested draftsRoot and redirectsPath
86
+ // Deliberately two named walks (pages/menus), not one broad walk of
87
+ // contentRoot: since Group N nested draftsRoot and redirectsPath
92
88
  // inside contentRoot (content/drafts/, content/redirects.json), a
93
89
  // single broad contentRoot walk would descend into content/drafts/
94
90
  // too, double-listing draft files under two different relative-path
95
91
  // keys, and would pick up redirects.json as if it were a page. An
96
92
  // inclusion-based walk sidesteps both problems structurally, with no
97
- // exclusion list to maintain. base stays config.contentRoot for all
98
- // three (not each subroot) so the resulting relative paths ("pages/x.json")
93
+ // exclusion list to maintain. base stays config.contentRoot for both
94
+ // (not each subroot) so the resulting relative paths ("pages/x.json")
99
95
  // line up with draftPaths below for the hasDraft/hasLive union logic.
100
96
  export function listContent(config, filters) {
101
97
  const contentPaths = new Set([
102
98
  ...listFilesRecursively(config.pagesRoot, config.contentRoot, '.json'),
103
- ...listFilesRecursively(config.postsRoot, config.contentRoot, '.json'),
104
99
  ...listFilesRecursively(config.menusRoot, config.contentRoot, '.json'),
105
100
  ]);
106
101
  const draftPaths = new Set(listFilesRecursively(config.draftsRoot, config.draftsRoot, '.json'));
@@ -2,27 +2,16 @@ import { existsSync, readFileSync, rmSync, statSync, unlinkSync, writeFileSync }
2
2
  import { listFilesRecursively } from "./fs-walk.js";
3
3
  import { commitPaths } from "./git.js";
4
4
  import { sanitisePath } from "./path-safety.js";
5
- import { postPathToUrl } from "./post-urls.js";
6
5
  import { RedirectError, addRedirect, isValidRedirectTarget, loadRedirects, serialiseRedirects, } from "./redirects.js";
7
6
  import { pagePathToUrl } from "./urls.js";
8
7
  import { enqueue } from "./write-queue.js";
9
8
  const PAGES_PREFIX = 'pages/';
10
- const POSTS_PREFIX = 'posts/';
11
- // Widens the redirect-on-delete gate to posts alongside pages, without
12
- // touching the has-children subtree check below (PAGES_PREFIX-only,
13
- // unchanged - posts never have nested children by construction). A
14
- // tiny duplicated helper, not a shared "collection" abstraction,
15
- // matching this codebase's existing precedent (e.g. prepareSaveDraft/
16
- // prepareDiscardDraft mirror rather than share a base with publish.ts/
17
- // move.ts). Returns null for anything else (menus have no public URL
18
- // at all, so redirects are meaningless there).
9
+ // Returns null for anything else (menus have no public URL at all, so
10
+ // redirects are meaningless there).
19
11
  function urlForDeletedEntry(relativePath) {
20
12
  if (relativePath.startsWith(PAGES_PREFIX)) {
21
13
  return pagePathToUrl(relativePath.slice(PAGES_PREFIX.length));
22
14
  }
23
- if (relativePath.startsWith(POSTS_PREFIX)) {
24
- return postPathToUrl(relativePath.slice(POSTS_PREFIX.length));
25
- }
26
15
  return null;
27
16
  }
28
17
  export class DeleteContentError extends Error {
@@ -1,7 +1,6 @@
1
1
  import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2
2
  import { commitPaths } from "./git.js";
3
3
  import { sanitisePath } from "./path-safety.js";
4
- import { isBlogUrl, urlToPostPath } from "./post-urls.js";
5
4
  import { RedirectError, addRedirect, isValidRedirectTarget, loadRedirectsStrict, removeRedirectForPath, serialiseRedirects, } from "./redirects.js";
6
5
  import { urlToPagePath } from "./urls.js";
7
6
  import { enqueue } from "./write-queue.js";
@@ -13,22 +12,13 @@ export class ManageRedirectError extends Error {
13
12
  this.reason = reason;
14
13
  }
15
14
  }
16
- // Not required for correctness (resolve-url.ts/resolve-blog-url.ts
17
- // already guarantee live content always wins over a redirect at the
18
- // same URL), but rejecting here avoids a marketing manager creating a
19
- // redirect that would silently never fire.
15
+ // Not required for correctness (resolve-url.ts already guarantees live
16
+ // content always wins over a redirect at the same URL), but rejecting
17
+ // here avoids a marketing manager creating a redirect that would
18
+ // silently never fire.
20
19
  function liveContentExistsAt(config, url) {
21
20
  const pageFile = sanitisePath(config.pagesRoot, urlToPagePath(url));
22
- if (existsSync(pageFile)) {
23
- return true;
24
- }
25
- if (isBlogUrl(url) && existsSync(config.postsRoot)) {
26
- const postRelative = urlToPostPath(url);
27
- if (postRelative !== null && existsSync(sanitisePath(config.postsRoot, postRelative))) {
28
- return true;
29
- }
30
- }
31
- return false;
21
+ return existsSync(pageFile);
32
22
  }
33
23
  function validateFromAndTo(from, to) {
34
24
  if (!isValidRedirectTarget(from)) {