@decocms/blocks 8.1.0-next.6 → 8.1.0-next.7

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 (41) hide show
  1. package/dist/v8/cli/content.d.ts +24 -3
  2. package/dist/v8/cli/content.d.ts.map +1 -1
  3. package/dist/v8/cli/content.js +70 -5
  4. package/dist/v8/cli/content.js.map +1 -1
  5. package/dist/v8/cli/run.js +1 -1
  6. package/dist/v8/cli/run.js.map +1 -1
  7. package/dist/v8/cms.d.ts.map +1 -1
  8. package/dist/v8/cms.js +16 -19
  9. package/dist/v8/cms.js.map +1 -1
  10. package/dist/v8/content.d.ts +4 -5
  11. package/dist/v8/content.d.ts.map +1 -1
  12. package/dist/v8/content.js +46 -44
  13. package/dist/v8/content.js.map +1 -1
  14. package/dist/v8/draftChanges.d.ts +37 -14
  15. package/dist/v8/draftChanges.d.ts.map +1 -1
  16. package/dist/v8/draftChanges.js +66 -44
  17. package/dist/v8/draftChanges.js.map +1 -1
  18. package/dist/v8/identity.d.ts +0 -2
  19. package/dist/v8/identity.d.ts.map +1 -1
  20. package/dist/v8/identity.js +0 -10
  21. package/dist/v8/identity.js.map +1 -1
  22. package/dist/v8/remoteLoader.d.ts +2 -4
  23. package/dist/v8/remoteLoader.d.ts.map +1 -1
  24. package/dist/v8/remoteLoader.js +106 -87
  25. package/dist/v8/remoteLoader.js.map +1 -1
  26. package/dist/v8/telemetry.d.ts +7 -7
  27. package/dist/v8/telemetry.d.ts.map +1 -1
  28. package/dist/v8/telemetry.js +24 -100
  29. package/dist/v8/telemetry.js.map +1 -1
  30. package/dist/v8/types.d.ts +47 -16
  31. package/dist/v8/types.d.ts.map +1 -1
  32. package/package.json +1 -1
  33. package/src/v8/cli/content.ts +85 -5
  34. package/src/v8/cli/run.ts +1 -1
  35. package/src/v8/cms.ts +17 -20
  36. package/src/v8/content.ts +54 -46
  37. package/src/v8/draftChanges.ts +79 -43
  38. package/src/v8/identity.ts +0 -10
  39. package/src/v8/remoteLoader.ts +111 -106
  40. package/src/v8/telemetry.ts +27 -108
  41. package/src/v8/types.ts +48 -14
@@ -23,6 +23,19 @@ export type Snapshot = {
23
23
  blocks: Record<string, unknown>;
24
24
  /** The alias table `deco content` writes: old type name → your type. */
25
25
  aliases?: Record<string, string>;
26
+ /**
27
+ * The hash of the `.deco/schema.gen.json` this content was built with, which
28
+ * `deco content` writes. A hosted release is swapped in only when its
29
+ * `schemaHash` equals the bundled content's; without one, never.
30
+ */
31
+ schemaHash?: string;
32
+ /**
33
+ * The commit time of the git HEAD `deco content` built this content from
34
+ * (committer date, ISO 8601). A hosted release is swapped in only when its
35
+ * `publishedAt` is later; without one (a custom loader, an older content
36
+ * module, a build outside git), the bundled content counts as the oldest.
37
+ */
38
+ committedAt?: string;
26
39
  };
27
40
  /**
28
41
  * The content module `deco content` writes: a snapshot plus `root`, the
@@ -44,11 +57,11 @@ export interface Loader {
44
57
  }
45
58
  /** The parts of a draft pointer, `<host[:port]><path[?query]>@<version>`. */
46
59
  export interface DraftPointer {
47
- /** `host[:port]` of the Studio API that serves the draft's changes. */
60
+ /** `host[:port]` that serves the draft's changes. */
48
61
  host: string;
49
62
  /** Starts with `/`; opaque to the app. Never carries the `__variant` parameters. */
50
63
  path: string;
51
- /** Opaque: the commit of the editor's last save. `"local"` names no draft (`deco serve`). */
64
+ /** Opaque: identifies the editor's last save. `"local"` names no draft (`deco serve`). */
52
65
  version: string;
53
66
  /** The variants this preview forces (the query's `__variant` parameters); absent when none. */
54
67
  variants?: ForcedVariant[];
@@ -185,18 +198,26 @@ export type Match<T> = {
185
198
  } | {
186
199
  kind: "not-found";
187
200
  };
188
- export type TelemetryConfig = ({
189
- site: string;
190
- token: string;
191
- } | {
192
- endpoint: string;
201
+ /**
202
+ * Where telemetry goes. With `endpoint`, to that OTLP/HTTP collector, with
203
+ * `headers`; without one, to the hosted Deco CMS collector when `createCMS`
204
+ * has a `token`, and nowhere otherwise.
205
+ */
206
+ export interface TelemetryConfig {
207
+ /** An OTLP/HTTP base URL; each signal is sent to `<endpoint>/v1/<signal>`. */
208
+ endpoint?: string;
209
+ /** Headers sent with every batch to `endpoint`. */
193
210
  headers?: Record<string, string>;
194
- }) & {
211
+ /**
212
+ * Resource attributes, merged over the defaults (`service.name`,
213
+ * `service.version`, `deployment.environment.name`, …).
214
+ */
215
+ resource?: Record<string, string>;
195
216
  limits?: {
196
217
  errorSampleRate?: number;
197
218
  traceSampleRate?: number;
198
219
  };
199
- };
220
+ }
200
221
  export interface CMSConfig {
201
222
  /** Your block functions; the CMS uses `{ ...builtIns, ...blocks }`. */
202
223
  blocks: Blocks;
@@ -212,14 +233,26 @@ export interface CMSConfig {
212
233
  * (/next/api-reference#host-patterns). Without it, content may allow any host.
213
234
  */
214
235
  hosts?: string[];
236
+ /**
237
+ * The hosts a draft pointer may point at; replaces the defaults (v7's
238
+ * list: `.decocms.com` and the loopback hosts). An entry starting with a
239
+ * dot matches any subdomain; any other entry, that exact host.
240
+ */
241
+ draftHosts?: string[];
215
242
  };
216
243
  /** The private key that decrypts `secret` blocks. */
217
244
  secrets?: {
218
245
  key?: string;
219
246
  };
220
- /** Your site's ID, for hosted releases. Drafts don't need it. */
247
+ /**
248
+ * Your site ID (Studio's site slug): turns on hosted releases from
249
+ * `delivery.decocms.com`. Drafts don't need it.
250
+ */
221
251
  site?: string;
222
- /** Your site token (secret), for hosted releases. Drafts don't need it. */
252
+ /**
253
+ * Your site token (server-only secret): sends telemetry to the hosted Deco
254
+ * CMS collector. Releases and drafts don't use it. Needs `site`.
255
+ */
223
256
  token?: string;
224
257
  }
225
258
  export interface ResolveOptions {
@@ -256,13 +289,11 @@ export interface CMS {
256
289
  forRelease(): Client;
257
290
  /**
258
291
  * A client reading the draft a pointer names: its changes, fetched from a
259
- * preview API domain (`*.decocms.com` and loopback by default;
260
- * `DECO_PREVIEW_API_DOMAINS` replaces the list), over this server's
261
- * production content.
292
+ * draft host (`*.decocms.com` and loopback by default;
293
+ * `createCMS({ preview: { draftHosts } })` replaces the list), over this
294
+ * server's production content.
262
295
  */
263
296
  forDraft(pointer: string): Client;
264
- /** A client pinned to a revision this CMS has served; an unknown revision behaves like the release. */
265
- forRevision(revision: string): Client;
266
297
  /** Ask the content source for newer content now; never throws. */
267
298
  update(): Promise<{
268
299
  updated: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/v8/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAMvC;;;;GAIG;AACH,MAAM,MAAM,KAAK,GAAG;IAAE,aAAa,EAAE,MAAM,CAAC;IAAC,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC;AAExE,mCAAmC;AACnC,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;AAExE,oEAAoE;AACpE,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;AAEnD,2FAA2F;AAC3F,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,gDAAgD;IAChD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,wEAAwE;IACxE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEzD;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB,IAAI,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC1B,MAAM,CAAC,IAAI,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CAC1C;AAED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,CAAC,CAAC;CAC3D"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "8.1.0-next.6",
3
+ "version": "8.1.0-next.7",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -3,9 +3,10 @@
3
3
  * module, `.deco/blocks.gen.ts` (spec: content › The content module). It never
4
4
  * reads the block map.
5
5
  */
6
+ import { execFileSync } from "node:child_process";
6
7
  import fs from "node:fs";
7
8
  import path from "node:path";
8
- import { computeContentRevision } from "../../protocol/canonical.ts";
9
+ import { canonicalJson, computeContentRevision, sha256Hex } from "../../protocol/canonical.ts";
9
10
  import {
10
11
  blockNameFromFile,
11
12
  checkBlockName,
@@ -154,7 +155,8 @@ function inlinedFiles(saved: SavedBlocks): string[] {
154
155
  /**
155
156
  * The `.deco` folder's identity for `createCMS` (one instance per folder, so
156
157
  * a hot-reloaded module keeps its instance): its path from the repository
157
- * root, or `.deco` outside a repository. Relative, so builds are reproducible.
158
+ * root, or `.deco` outside a repository. Relative, so it doesn't depend on
159
+ * where the repository is checked out.
158
160
  */
159
161
  export function contentRoot(deco: string): string {
160
162
  for (let dir = path.dirname(deco); ; dir = path.dirname(dir)) {
@@ -165,8 +167,66 @@ export function contentRoot(deco: string): string {
165
167
  }
166
168
  }
167
169
 
168
- /** The source of `.deco/blocks.gen.ts` for a set of saved blocks. */
169
- export async function renderContentModule(saved: SavedBlocks, root = ".deco"): Promise<string> {
170
+ /**
171
+ * The `schemaHash` of a `.deco/schema.gen.json`: the SHA-256 of its parsed
172
+ * JSON in canonical form (`sha256Hex(canonicalJson(JSON.parse(text)))`, the
173
+ * `@decocms/blocks/protocol` helpers), so Studio computes the same value
174
+ * from the file at a commit. Hosted releases swap only when it matches.
175
+ */
176
+ async function computeSchemaHash(text: string): Promise<string> {
177
+ return sha256Hex(canonicalJson(JSON.parse(text)));
178
+ }
179
+
180
+ /** `schema.gen.json`'s hash, or `undefined` when there's no such file. */
181
+ async function readSchemaHash(schemaFile: string): Promise<string | undefined> {
182
+ let text: string;
183
+ try {
184
+ text = fs.readFileSync(schemaFile, "utf8");
185
+ } catch (error) {
186
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
187
+ throw error;
188
+ }
189
+ try {
190
+ return await computeSchemaHash(text);
191
+ } catch (error) {
192
+ throw new CliError(
193
+ `.deco/schema.gen.json: not valid JSON (${(error as Error).message}); run deco schema`,
194
+ );
195
+ }
196
+ }
197
+
198
+ /**
199
+ * The commit time of git HEAD in `dir` (committer date, ISO 8601), or
200
+ * undefined when git is missing, `dir` isn't in a repository, or it has no
201
+ * commits. Commit time, not build time: a slow build of an older commit that
202
+ * finishes after a publish must not win over it.
203
+ */
204
+ export function readCommittedAt(dir: string): string | undefined {
205
+ try {
206
+ const out = execFileSync("git", ["log", "-1", "--format=%cI"], {
207
+ cwd: dir,
208
+ encoding: "utf8",
209
+ stdio: ["ignore", "pipe", "ignore"],
210
+ }).trim();
211
+ return out === "" || Number.isNaN(Date.parse(out)) ? undefined : out;
212
+ } catch {
213
+ return undefined;
214
+ }
215
+ }
216
+
217
+ /**
218
+ * The source of `.deco/blocks.gen.ts` for a set of saved blocks, with the
219
+ * `schemaHash` of the schema it was built with when there is one, and
220
+ * `committedAt`: the commit time of git HEAD it was built from (ISO 8601),
221
+ * when there is one. A hosted release replaces the bundled content only when
222
+ * it was published later (see ../remoteLoader.ts).
223
+ */
224
+ export async function renderContentModule(
225
+ saved: SavedBlocks,
226
+ root = ".deco",
227
+ schemaHash?: string,
228
+ committedAt?: string,
229
+ ): Promise<string> {
170
230
  const names = Object.keys(saved.blocks).sort();
171
231
  const revision = await computeContentRevision(saved.blocks);
172
232
  const taken = new Set<string>();
@@ -194,11 +254,15 @@ export async function renderContentModule(saved: SavedBlocks, root = ".deco"): P
194
254
  "",
195
255
  "const content: {",
196
256
  " revision: string;",
257
+ ...(schemaHash === undefined ? [] : [" schemaHash: string;"]),
258
+ ...(committedAt === undefined ? [] : [" committedAt: string;"]),
197
259
  " blocks: Record<string, unknown>;",
198
260
  " aliases: Record<string, string>;",
199
261
  " root: string;",
200
262
  "} = {",
201
263
  ` revision: ${JSON.stringify(revision)},`,
264
+ ...(schemaHash === undefined ? [] : [` schemaHash: ${JSON.stringify(schemaHash)},`]),
265
+ ...(committedAt === undefined ? [] : [` committedAt: ${JSON.stringify(committedAt)},`]),
202
266
  " blocks: {",
203
267
  ...entries,
204
268
  " },",
@@ -217,7 +281,14 @@ export interface ContentResult {
217
281
  root: string;
218
282
  file: string;
219
283
  revision: string;
284
+ /** The hash of `.deco/schema.gen.json`, written into the module; absent without one. */
285
+ schemaHash?: string;
220
286
  count: number;
287
+ /**
288
+ * The commit time of git HEAD in the root, written into the module as
289
+ * `committedAt`; absent when git isn't available there.
290
+ */
291
+ committedAt?: string;
221
292
  /** False when the file already had this content. */
222
293
  changed: boolean;
223
294
  /** Files written into the module instead of imported (see `inlinedFiles`). */
@@ -246,12 +317,16 @@ export async function writeContent(paths: DecoPaths): Promise<ContentResult> {
246
317
  if (errors.length > 0) {
247
318
  throw new CliError(errors.map((d) => `.deco/blocks/${d.file}: ${d.message}`).join("\n"));
248
319
  }
249
- const source = await renderContentModule(saved, contentRoot(paths.deco));
320
+ const schemaHash = await readSchemaHash(paths.schema);
321
+ const committedAt = readCommittedAt(paths.root);
322
+ const source = await renderContentModule(saved, contentRoot(paths.deco), schemaHash, committedAt);
250
323
  const changed = writeIfChanged(paths.content, source);
251
324
  return {
252
325
  root: paths.root,
253
326
  file: paths.content,
254
327
  revision: await computeContentRevision(saved.blocks),
328
+ ...(schemaHash === undefined ? {} : { schemaHash }),
329
+ ...(committedAt === undefined ? {} : { committedAt }),
255
330
  count: Object.keys(saved.blocks).length,
256
331
  changed,
257
332
  inlined: inlinedFiles(saved),
@@ -273,6 +348,11 @@ export async function content(options: ContentOptions = {}): Promise<number> {
273
348
  const paths = decoPaths(findDecoRoot(options));
274
349
  const result = await writeContent(paths);
275
350
  for (const d of result.diagnostics) reporter.warn(`.deco/blocks/${d.file}: ${d.message}`);
351
+ if (result.committedAt === undefined) {
352
+ reporter.info(
353
+ "no git commit found, so .deco/blocks.gen.ts has no committedAt: hosted releases with the same schema will replace it",
354
+ );
355
+ }
276
356
  reporter.info(
277
357
  `${result.changed ? "wrote" : "unchanged"} .deco/blocks.gen.ts (${result.count} blocks, revision ${result.revision.slice(0, 12)})`,
278
358
  );
package/src/v8/cli/run.ts CHANGED
@@ -124,7 +124,7 @@ async function watchLoop(
124
124
  */
125
125
  const PUBLISH_SIGNPOST = `There is no publish command: Git is the source of truth for content, so publishing is committing.
126
126
  Commit the changes in .deco/blocks (and push them). A deploy ships the commit; with the hosted
127
- Deco CMS, a commit becomes a release without a deploy.`;
127
+ Deco CMS, Studio's Publish (or Resync) makes the commit a release without a deploy.`;
128
128
 
129
129
  /** Run one `deco` invocation; returns the exit code. */
130
130
  export async function runCli(argv: string[], options: RunOptions = {}): Promise<number> {
package/src/v8/cms.ts CHANGED
@@ -13,8 +13,9 @@ import { secretBlock } from "./builtins/secret.ts";
13
13
  import { CMSClient } from "./client.ts";
14
14
  import { ContentStore, isLoader, isSnapshot } from "./content.ts";
15
15
  import { draftCookieFor, endsPreview, parseDraftPointer, readDraftPointer } from "./draft.ts";
16
+ import { parseDraftHosts } from "./draftChanges.ts";
16
17
  import { allowsHost, type HostPattern, parseHostPattern } from "./hosts.ts";
17
- import { clearGlobals, contentIdentity, fnv1a, readEnv } from "./identity.ts";
18
+ import { clearGlobals, contentIdentity, fnv1a } from "./identity.ts";
18
19
  import { isPlainObject } from "./json.ts";
19
20
  import { remoteLoader, resetRemoteLoaders } from "./remoteLoader.ts";
20
21
  import {
@@ -55,6 +56,7 @@ interface Fingerprint {
55
56
  telemetry: string;
56
57
  preview: string;
57
58
  secrets: string;
59
+ token: string;
58
60
  }
59
61
 
60
62
  /**
@@ -87,13 +89,13 @@ class CMSInstance {
87
89
  constructor(config: CMSConfig, interval: number) {
88
90
  this.config = config;
89
91
  this.#interval = interval;
90
- this.#store = new ContentStore(contentOf(config));
92
+ this.#store = new ContentStore(contentOf(config), parseDraftHosts(config.preview));
91
93
  this.fingerprint = fingerprintOf(config, interval);
92
94
  this.#caps = {
93
95
  hosts: parseCodeHosts(config.preview),
94
96
  limits: telemetryLimits(config.telemetry),
95
97
  };
96
- const destination = resolveDestination(config.telemetry, config.site);
98
+ const destination = resolveDestination(config.telemetry, config.site, config.token);
97
99
  if (destination !== null) {
98
100
  this.#telemetry = new TelemetryPipeline(destination);
99
101
  setCurrentTelemetry(this.#telemetry);
@@ -153,10 +155,6 @@ class CMSInstance {
153
155
  return this.#client(blocks, () => load().then((snapshot) => forceVariants(snapshot, variants)));
154
156
  }
155
157
 
156
- forRevision(blocks: Blocks, revision: string): Client {
157
- return this.#client(blocks, () => this.#store.revision(revision));
158
- }
159
-
160
158
  update(): Promise<{ updated: boolean }> {
161
159
  this.#nextCheck = this.#due();
162
160
  return this.#store.update();
@@ -302,10 +300,6 @@ class CMSHandle implements CMS {
302
300
  return this.#instance.forDraft(this.#blocks, pointer);
303
301
  }
304
302
 
305
- forRevision(revision: string): Client {
306
- return this.#instance.forRevision(this.#blocks, revision);
307
- }
308
-
309
303
  update(): Promise<{ updated: boolean }> {
310
304
  return this.#instance.update();
311
305
  }
@@ -365,10 +359,10 @@ export function resetForTests(): void {
365
359
  setCurrentTelemetry(undefined);
366
360
  }
367
361
 
368
- /** With `site` and `token`, the content is the fallback of hosted releases. */
362
+ /** With `site`, the content is the fallback of hosted releases. */
369
363
  function contentOf(config: CMSConfig): Snapshot | Loader {
370
- if (!config.site || !config.token) return config.content;
371
- return remoteLoader(config.content, { site: config.site, token: config.token });
364
+ if (!config.site) return config.content;
365
+ return remoteLoader(config.content, { site: config.site });
372
366
  }
373
367
 
374
368
  function validate(config: CMSConfig): void {
@@ -383,12 +377,16 @@ function validate(config: CMSConfig): void {
383
377
  "createCMS: `content` must be the content module ({ revision, blocks }) or a loader with load()",
384
378
  );
385
379
  }
380
+ // The token is the site's credential: it means nothing without the site it belongs to.
381
+ if (config.token && !config.site) {
382
+ throw new TypeError("createCMS: token needs site: pass both, or site alone");
383
+ }
386
384
  parseCodeHosts(config.preview);
385
+ parseDraftHosts(config.preview);
387
386
  }
388
387
 
389
388
  function resolveInterval(configured: number | undefined): number {
390
- const env = readEnv("DECO_CONTENT_INTERVAL");
391
- const raw = configured ?? (env ? Number(env) : MIN_INTERVAL);
389
+ const raw = configured ?? MIN_INTERVAL;
392
390
  if (!Number.isFinite(raw)) return MIN_INTERVAL;
393
391
  if (raw < MIN_INTERVAL) {
394
392
  console.warn(
@@ -401,8 +399,7 @@ function resolveInterval(configured: number | undefined): number {
401
399
 
402
400
  /**
403
401
  * The instance key: the content's identity (never its revision, so a hot
404
- * reload keeps the instance) and, with the hosted Deco CMS, the site and a
405
- * hash of the token, which never appears in the global symbol registry.
402
+ * reload keeps the instance) and, with hosted releases, the site.
406
403
  *
407
404
  * A content module is identified by the `.deco` folder it was generated from
408
405
  * (its `root`). One without a `root` is identified by the object itself, so
@@ -411,8 +408,7 @@ function resolveInterval(configured: number | undefined): number {
411
408
  */
412
409
  function identityOf(config: CMSConfig): string {
413
410
  const content = contentIdentity(config.content);
414
- if (!config.site || !config.token) return content;
415
- return `${content}|site:${config.site}|token:${fnv1a(config.token)}`;
411
+ return config.site ? `${content}|site:${config.site}` : content;
416
412
  }
417
413
 
418
414
  function fingerprintOf(config: CMSConfig, interval: number): Fingerprint {
@@ -421,6 +417,7 @@ function fingerprintOf(config: CMSConfig, interval: number): Fingerprint {
421
417
  telemetry: stableJson(config.telemetry ?? null),
422
418
  preview: stableJson(config.preview ?? null),
423
419
  secrets: config.secrets?.key ? fnv1a(config.secrets.key) : "",
420
+ token: config.token ? fnv1a(config.token) : "",
424
421
  };
425
422
  }
426
423
 
package/src/v8/content.ts CHANGED
@@ -1,30 +1,30 @@
1
1
  /**
2
- * The content side of a CMS: one source (the content module or a `Loader`),
3
- * the caches every client shares, and the release revisions this process has
4
- * served. Clients only ever see whole `{ revision, blocks }` snapshots.
2
+ * The content side of a CMS: one source (the content module or a `Loader`)
3
+ * and the caches every client shares. Clients only ever see whole
4
+ * `{ revision, blocks }` snapshots.
5
5
  *
6
6
  * A draft is the release with a draft's changes layered over it (see
7
- * ./draftChanges.ts). Drafts are never recorded as served:
8
- * `forRevision(revision)` takes a revision string a client may hand back, so
9
- * it must only ever reach published content, never a draft someone loaded
10
- * with a pointer.
7
+ * ./draftChanges.ts).
11
8
  */
12
9
  import { formatDraftPointer, parseDraftPointer } from "./draft.ts";
13
- import { type DraftChanges, fetchDraftChanges, LOCAL_VERSION, layerDraft } from "./draftChanges.ts";
10
+ import {
11
+ DEFAULT_DRAFT_HOSTS,
12
+ type DraftChanges,
13
+ fetchDraftChanges,
14
+ LOCAL_VERSION,
15
+ layerDraft,
16
+ } from "./draftChanges.ts";
14
17
  import { errors, isResolutionError } from "./errors.ts";
15
18
  import { isPlainObject } from "./json.ts";
16
19
  import type { Loader, Snapshot } from "./types.ts";
17
20
 
18
- const SERVED_REVISIONS = 16;
19
21
  /** Fetched drafts kept per CMS. */
20
22
  const CACHED_DRAFTS = 3;
21
- /** How long a fetched draft is reused before it's fetched again, so its token is checked again. */
22
- const DRAFT_TTL_MS = 60_000;
23
23
 
24
- /** A fetched draft, and the views of it over each release it was layered on. */
24
+ /** A fetched draft body, its ETag, and the views of it over each release it was layered on. */
25
25
  interface CachedDraft {
26
- changes: Promise<DraftChanges>;
27
- at: number;
26
+ changes: DraftChanges;
27
+ etag: string | null;
28
28
  views: WeakMap<Snapshot, Snapshot>;
29
29
  }
30
30
 
@@ -55,7 +55,9 @@ export function isSnapshot(content: unknown): content is Snapshot {
55
55
  isPlainObject(content) &&
56
56
  typeof content.revision === "string" &&
57
57
  isPlainObject(content.blocks) &&
58
- (content.aliases === undefined || isPlainObject(content.aliases))
58
+ (content.aliases === undefined || isPlainObject(content.aliases)) &&
59
+ (content.schemaHash === undefined || typeof content.schemaHash === "string") &&
60
+ (content.committedAt === undefined || typeof content.committedAt === "string")
59
61
  );
60
62
  }
61
63
 
@@ -64,12 +66,15 @@ export class ContentStore {
64
66
  #release: Promise<Snapshot> | undefined;
65
67
  #updating: Promise<{ updated: boolean }> | undefined;
66
68
  readonly #drafts = new BoundedMap<string, CachedDraft>(CACHED_DRAFTS);
67
- readonly #served = new BoundedMap<string, Snapshot>(SERVED_REVISIONS);
69
+ /** Draft reads in flight, so concurrent renders of one draft share one request. */
70
+ readonly #draftReads = new Map<string, Promise<CachedDraft>>();
71
+ readonly #draftHosts: readonly string[];
68
72
  /** The release this store last handed a client, for a loader that can't peek. */
69
73
  #latest: Snapshot | undefined;
70
74
 
71
- constructor(source: Snapshot | Loader) {
75
+ constructor(source: Snapshot | Loader, draftHosts: readonly string[] = DEFAULT_DRAFT_HOSTS) {
72
76
  this.#source = source;
77
+ this.#draftHosts = draftHosts;
73
78
  }
74
79
 
75
80
  /** Whether the source can change while the process runs (has `update()`). */
@@ -98,12 +103,13 @@ export class ContentStore {
98
103
  }
99
104
 
100
105
  /**
101
- * The draft a pointer names: its changes, fetched from a preview API domain,
106
+ * The draft a pointer names: its changes, fetched from a draft host,
102
107
  * layered over the release. A pointer that doesn't parse, or any failure, is
103
108
  * `LOADER_FAILED`, never a silent fallback to the release. A pointer whose
104
109
  * version is `local` names no draft: the release, with nothing fetched. The
105
110
  * changes are keyed by the pointer without its `__variant` parameters (every
106
- * variant of one draft shares one fetch) and reused for a minute.
111
+ * variant of one draft shares one body) and revalidated on every read: a
112
+ * `304` reuses the body held, a `200` replaces it.
107
113
  */
108
114
  draft(pointer: string): Promise<Snapshot> {
109
115
  const parsed = parseDraftPointer(pointer);
@@ -113,28 +119,38 @@ export class ContentStore {
113
119
  if (parsed.version === LOCAL_VERSION) return this.release();
114
120
  const { host, path, version } = parsed;
115
121
  const key = formatDraftPointer({ host, path, version });
116
- let entry = this.#drafts.get(key);
117
- if (entry === undefined || Date.now() - entry.at >= DRAFT_TTL_MS) {
118
- const fetched: CachedDraft = {
119
- changes: fetchDraftChanges(parsed).catch((error: unknown) => {
120
- throw errors.loaderFailed("the draft's changes couldn't be fetched", error);
121
- }),
122
- at: Date.now(),
123
- views: new WeakMap(),
122
+ let read = this.#draftReads.get(key);
123
+ if (read === undefined) {
124
+ const held = this.#drafts.get(key);
125
+ read = fetchDraftChanges(parsed, { domains: this.#draftHosts, etag: held?.etag }).then(
126
+ (result) => {
127
+ if (result.status === 304 && held !== undefined) return held;
128
+ if (result.status === 304) throw new Error("draft changes: 304 without a held body");
129
+ const entry: CachedDraft = {
130
+ changes: result.changes,
131
+ etag: result.etag,
132
+ views: new WeakMap(),
133
+ };
134
+ this.#drafts.set(key, entry);
135
+ return entry;
136
+ },
137
+ );
138
+ const pending = read;
139
+ this.#draftReads.set(key, pending);
140
+ const settle = () => {
141
+ if (this.#draftReads.get(key) === pending) this.#draftReads.delete(key);
124
142
  };
125
- this.#drafts.set(key, fetched);
126
- // A failure isn't reused: the next client fetches again.
127
- fetched.changes.catch(() => {
128
- if (this.#drafts.get(key) === fetched) this.#drafts.delete(key);
129
- });
130
- entry = fetched;
143
+ pending.then(settle, settle);
131
144
  }
132
- const { changes, views } = entry;
133
- return Promise.all([this.release(), changes]).then(([base, draft]) => {
134
- let view = views.get(base);
145
+ const changes = read.catch((error: unknown) => {
146
+ throw errors.loaderFailed("the draft's changes couldn't be fetched", error);
147
+ });
148
+ return Promise.all([this.release(), changes]).then(([base, entry]) => {
149
+ let view = entry.views.get(base);
135
150
  if (view === undefined) {
136
- view = layerDraft(base, draft, version);
137
- views.set(base, view);
151
+ // OPEN: the view is keyed by the body's ETag (the pointer outlives later saves); the version without one.
152
+ view = layerDraft(base, entry.changes, entry.etag ?? version);
153
+ entry.views.set(base, view);
138
154
  }
139
155
  return view;
140
156
  });
@@ -149,12 +165,6 @@ export class ContentStore {
149
165
  return peekRelease(this.#source) ?? this.#latest;
150
166
  }
151
167
 
152
- /** A release revision this store has served, or the release when it's unknown (drafts included). */
153
- revision(revision: string): Promise<Snapshot> {
154
- const served = this.#served.get(revision);
155
- return served === undefined ? this.release() : Promise.resolve(served);
156
- }
157
-
158
168
  /** Asks the source for newer content; never throws, and concurrent calls share one check. */
159
169
  update(): Promise<{ updated: boolean }> {
160
170
  const source = this.#source;
@@ -197,7 +207,6 @@ export class ContentStore {
197
207
  this.#drafts.clear();
198
208
  return;
199
209
  }
200
- if (isSnapshot(previous)) this.#served.delete(previous.revision);
201
210
  this.#source = source;
202
211
  this.#release = undefined;
203
212
  this.#latest = undefined;
@@ -220,7 +229,6 @@ export class ContentStore {
220
229
 
221
230
  #serve(snapshot: Snapshot): Snapshot {
222
231
  this.#latest = snapshot;
223
- this.#served.set(snapshot.revision, snapshot);
224
232
  return snapshot;
225
233
  }
226
234
  }