@decocms/blocks 8.9.1 → 8.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (257) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/protocol/channel.d.ts +98 -0
  6. package/dist/protocol/channel.d.ts.map +1 -0
  7. package/dist/protocol/channel.js +58 -0
  8. package/dist/protocol/channel.js.map +1 -0
  9. package/dist/protocol/client.js +2 -2
  10. package/dist/protocol/client.js.map +1 -1
  11. package/dist/protocol/conformance/blockMetaVectors.json +214 -0
  12. package/dist/protocol/conformance/contentHashFixtures.d.ts +22 -0
  13. package/dist/protocol/conformance/contentHashFixtures.d.ts.map +1 -1
  14. package/dist/protocol/conformance/contentHashFixtures.js +55 -0
  15. package/dist/protocol/conformance/contentHashFixtures.js.map +1 -1
  16. package/dist/protocol/conformance/index.d.ts +2 -1
  17. package/dist/protocol/conformance/index.d.ts.map +1 -1
  18. package/dist/protocol/conformance/index.js +2 -1
  19. package/dist/protocol/conformance/index.js.map +1 -1
  20. package/dist/protocol/conformance/vectors.d.ts +33 -0
  21. package/dist/protocol/conformance/vectors.d.ts.map +1 -0
  22. package/dist/protocol/conformance/vectors.js +3 -0
  23. package/dist/protocol/conformance/vectors.js.map +1 -0
  24. package/dist/protocol/index.d.ts +11 -1
  25. package/dist/protocol/index.d.ts.map +1 -1
  26. package/dist/protocol/index.js +11 -1
  27. package/dist/protocol/index.js.map +1 -1
  28. package/dist/protocol/keys.d.ts +1 -1
  29. package/dist/protocol/keys.js +1 -1
  30. package/dist/v8/background.d.ts +13 -0
  31. package/dist/v8/background.d.ts.map +1 -1
  32. package/dist/v8/background.js +41 -0
  33. package/dist/v8/background.js.map +1 -1
  34. package/dist/v8/blockMeta.d.ts +27 -0
  35. package/dist/v8/blockMeta.d.ts.map +1 -0
  36. package/dist/v8/blockMeta.js +75 -0
  37. package/dist/v8/blockMeta.js.map +1 -0
  38. package/dist/v8/blockStore.d.ts +43 -0
  39. package/dist/v8/blockStore.d.ts.map +1 -0
  40. package/dist/v8/blockStore.js +95 -0
  41. package/dist/v8/blockStore.js.map +1 -0
  42. package/dist/v8/boundedJson.d.ts +5 -0
  43. package/dist/v8/boundedJson.d.ts.map +1 -1
  44. package/dist/v8/boundedJson.js +10 -2
  45. package/dist/v8/boundedJson.js.map +1 -1
  46. package/dist/v8/boundedMap.d.ts +19 -0
  47. package/dist/v8/boundedMap.d.ts.map +1 -0
  48. package/dist/v8/boundedMap.js +52 -0
  49. package/dist/v8/boundedMap.js.map +1 -0
  50. package/dist/v8/builtins/data.d.ts +18 -2
  51. package/dist/v8/builtins/data.d.ts.map +1 -1
  52. package/dist/v8/builtins/data.js +17 -1
  53. package/dist/v8/builtins/data.js.map +1 -1
  54. package/dist/v8/builtins/index.d.ts.map +1 -1
  55. package/dist/v8/builtins/index.js +2 -1
  56. package/dist/v8/builtins/index.js.map +1 -1
  57. package/dist/v8/builtins/legacy.d.ts +6 -3
  58. package/dist/v8/builtins/legacy.d.ts.map +1 -1
  59. package/dist/v8/builtins/legacy.js +6 -3
  60. package/dist/v8/builtins/legacy.js.map +1 -1
  61. package/dist/v8/canonical.d.ts +8 -0
  62. package/dist/v8/canonical.d.ts.map +1 -1
  63. package/dist/v8/canonical.js +15 -0
  64. package/dist/v8/canonical.js.map +1 -1
  65. package/dist/v8/cli/builtins.d.ts.map +1 -1
  66. package/dist/v8/cli/builtins.js +1 -0
  67. package/dist/v8/cli/builtins.js.map +1 -1
  68. package/dist/v8/cli/check/index.d.ts +10 -0
  69. package/dist/v8/cli/check/index.d.ts.map +1 -1
  70. package/dist/v8/cli/check/index.js +91 -7
  71. package/dist/v8/cli/check/index.js.map +1 -1
  72. package/dist/v8/cli/content.d.ts +12 -25
  73. package/dist/v8/cli/content.d.ts.map +1 -1
  74. package/dist/v8/cli/content.js +46 -75
  75. package/dist/v8/cli/content.js.map +1 -1
  76. package/dist/v8/cli/index.d.ts +1 -2
  77. package/dist/v8/cli/index.d.ts.map +1 -1
  78. package/dist/v8/cli/index.js +1 -2
  79. package/dist/v8/cli/index.js.map +1 -1
  80. package/dist/v8/cli/remote/commands.d.ts +71 -0
  81. package/dist/v8/cli/remote/commands.d.ts.map +1 -0
  82. package/dist/v8/cli/remote/commands.js +644 -0
  83. package/dist/v8/cli/remote/commands.js.map +1 -0
  84. package/dist/v8/cli/remote/diff.d.ts +15 -0
  85. package/dist/v8/cli/remote/diff.d.ts.map +1 -0
  86. package/dist/v8/cli/remote/diff.js +102 -0
  87. package/dist/v8/cli/remote/diff.js.map +1 -0
  88. package/dist/v8/cli/remote/env.d.ts +35 -0
  89. package/dist/v8/cli/remote/env.d.ts.map +1 -0
  90. package/dist/v8/cli/remote/env.js +84 -0
  91. package/dist/v8/cli/remote/env.js.map +1 -0
  92. package/dist/v8/cli/remote/http.d.ts +149 -0
  93. package/dist/v8/cli/remote/http.d.ts.map +1 -0
  94. package/dist/v8/cli/remote/http.js +381 -0
  95. package/dist/v8/cli/remote/http.js.map +1 -0
  96. package/dist/v8/cli/remote/local.d.ts +90 -0
  97. package/dist/v8/cli/remote/local.d.ts.map +1 -0
  98. package/dist/v8/cli/remote/local.js +309 -0
  99. package/dist/v8/cli/remote/local.js.map +1 -0
  100. package/dist/v8/cli/root.d.ts +29 -0
  101. package/dist/v8/cli/root.d.ts.map +1 -1
  102. package/dist/v8/cli/root.js +28 -0
  103. package/dist/v8/cli/root.js.map +1 -1
  104. package/dist/v8/cli/run.d.ts +11 -1
  105. package/dist/v8/cli/run.d.ts.map +1 -1
  106. package/dist/v8/cli/run.js +281 -124
  107. package/dist/v8/cli/run.js.map +1 -1
  108. package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
  109. package/dist/v8/cli/schema/builtinSchemas.js +28 -10
  110. package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
  111. package/dist/v8/cli/schema/generate.js +1 -1
  112. package/dist/v8/cli/schema/generate.js.map +1 -1
  113. package/dist/v8/client.d.ts +9 -9
  114. package/dist/v8/client.d.ts.map +1 -1
  115. package/dist/v8/client.js +138 -50
  116. package/dist/v8/client.js.map +1 -1
  117. package/dist/v8/cms.d.ts +2 -1
  118. package/dist/v8/cms.d.ts.map +1 -1
  119. package/dist/v8/cms.js +55 -46
  120. package/dist/v8/cms.js.map +1 -1
  121. package/dist/v8/content.d.ts +39 -78
  122. package/dist/v8/content.d.ts.map +1 -1
  123. package/dist/v8/content.js +155 -195
  124. package/dist/v8/content.js.map +1 -1
  125. package/dist/v8/contentIndex.d.ts +71 -0
  126. package/dist/v8/contentIndex.d.ts.map +1 -0
  127. package/dist/v8/contentIndex.js +137 -0
  128. package/dist/v8/contentIndex.js.map +1 -0
  129. package/dist/v8/draft.d.ts +7 -7
  130. package/dist/v8/draft.d.ts.map +1 -1
  131. package/dist/v8/draft.js +10 -9
  132. package/dist/v8/draft.js.map +1 -1
  133. package/dist/v8/draftChanges.d.ts +59 -32
  134. package/dist/v8/draftChanges.d.ts.map +1 -1
  135. package/dist/v8/draftChanges.js +139 -75
  136. package/dist/v8/draftChanges.js.map +1 -1
  137. package/dist/v8/errors.d.ts +21 -0
  138. package/dist/v8/errors.d.ts.map +1 -1
  139. package/dist/v8/errors.js +22 -0
  140. package/dist/v8/errors.js.map +1 -1
  141. package/dist/v8/hostedSource.d.ts +57 -0
  142. package/dist/v8/hostedSource.d.ts.map +1 -0
  143. package/dist/v8/hostedSource.js +18 -0
  144. package/dist/v8/hostedSource.js.map +1 -0
  145. package/dist/v8/identity.d.ts +1 -1
  146. package/dist/v8/identity.js +1 -1
  147. package/dist/v8/index.d.ts +1 -0
  148. package/dist/v8/index.d.ts.map +1 -1
  149. package/dist/v8/index.js +1 -0
  150. package/dist/v8/index.js.map +1 -1
  151. package/dist/v8/matchRoute.js +7 -4
  152. package/dist/v8/matchRoute.js.map +1 -1
  153. package/dist/v8/pin.d.ts +34 -0
  154. package/dist/v8/pin.d.ts.map +1 -0
  155. package/dist/v8/pin.js +156 -0
  156. package/dist/v8/pin.js.map +1 -0
  157. package/dist/v8/preview/StudioBridge.d.ts +8 -8
  158. package/dist/v8/preview/StudioBridge.d.ts.map +1 -1
  159. package/dist/v8/preview/StudioBridge.js +8 -10
  160. package/dist/v8/preview/StudioBridge.js.map +1 -1
  161. package/dist/v8/preview/StudioChannel.d.ts +51 -0
  162. package/dist/v8/preview/StudioChannel.d.ts.map +1 -0
  163. package/dist/v8/preview/StudioChannel.js +104 -0
  164. package/dist/v8/preview/StudioChannel.js.map +1 -0
  165. package/dist/v8/preview/index.d.ts +3 -0
  166. package/dist/v8/preview/index.d.ts.map +1 -1
  167. package/dist/v8/preview/index.js +3 -0
  168. package/dist/v8/preview/index.js.map +1 -1
  169. package/dist/v8/remoteLoader.d.ts +42 -5
  170. package/dist/v8/remoteLoader.d.ts.map +1 -1
  171. package/dist/v8/remoteLoader.js +317 -259
  172. package/dist/v8/remoteLoader.js.map +1 -1
  173. package/dist/v8/resolver.d.ts +22 -8
  174. package/dist/v8/resolver.d.ts.map +1 -1
  175. package/dist/v8/resolver.js +37 -32
  176. package/dist/v8/resolver.js.map +1 -1
  177. package/dist/v8/revisionId.d.ts +11 -0
  178. package/dist/v8/revisionId.d.ts.map +1 -0
  179. package/dist/v8/revisionId.js +13 -0
  180. package/dist/v8/revisionId.js.map +1 -0
  181. package/dist/v8/settings.d.ts +6 -6
  182. package/dist/v8/settings.d.ts.map +1 -1
  183. package/dist/v8/settings.js +8 -7
  184. package/dist/v8/settings.js.map +1 -1
  185. package/dist/v8/telemetry.d.ts +4 -2
  186. package/dist/v8/telemetry.d.ts.map +1 -1
  187. package/dist/v8/telemetry.js +2 -2
  188. package/dist/v8/telemetry.js.map +1 -1
  189. package/dist/v8/types.d.ts +52 -40
  190. package/dist/v8/types.d.ts.map +1 -1
  191. package/dist/v8/variants.d.ts +7 -2
  192. package/dist/v8/variants.d.ts.map +1 -1
  193. package/dist/v8/variants.js +21 -24
  194. package/dist/v8/variants.js.map +1 -1
  195. package/dist/v8/vite/index.d.ts +2 -2
  196. package/dist/v8/vite/index.js +1 -1
  197. package/dist/v8/vite/index.js.map +1 -1
  198. package/package.json +1 -1
  199. package/src/index.ts +2 -0
  200. package/src/protocol/channel.ts +127 -0
  201. package/src/protocol/client.ts +2 -2
  202. package/src/protocol/conformance/blockMetaVectors.json +214 -0
  203. package/src/protocol/conformance/contentHashFixtures.ts +74 -0
  204. package/src/protocol/conformance/index.ts +9 -1
  205. package/src/protocol/conformance/vectors.ts +36 -0
  206. package/src/protocol/index.ts +32 -0
  207. package/src/protocol/keys.ts +1 -1
  208. package/src/v8/background.ts +42 -0
  209. package/src/v8/blockMeta.ts +100 -0
  210. package/src/v8/blockStore.ts +124 -0
  211. package/src/v8/boundedJson.ts +20 -2
  212. package/src/v8/boundedMap.ts +53 -0
  213. package/src/v8/builtins/data.ts +26 -2
  214. package/src/v8/builtins/index.ts +2 -1
  215. package/src/v8/builtins/legacy.ts +6 -3
  216. package/src/v8/canonical.ts +15 -0
  217. package/src/v8/cli/builtins.ts +1 -0
  218. package/src/v8/cli/check/index.ts +97 -7
  219. package/src/v8/cli/content.ts +49 -91
  220. package/src/v8/cli/index.ts +1 -2
  221. package/src/v8/cli/remote/commands.ts +834 -0
  222. package/src/v8/cli/remote/diff.ts +100 -0
  223. package/src/v8/cli/remote/env.ts +107 -0
  224. package/src/v8/cli/remote/http.ts +484 -0
  225. package/src/v8/cli/remote/local.ts +343 -0
  226. package/src/v8/cli/root.ts +40 -0
  227. package/src/v8/cli/run.ts +333 -136
  228. package/src/v8/cli/schema/builtinSchemas.ts +29 -10
  229. package/src/v8/cli/schema/generate.ts +1 -1
  230. package/src/v8/client.ts +162 -60
  231. package/src/v8/cms.ts +62 -53
  232. package/src/v8/content.ts +179 -229
  233. package/src/v8/contentIndex.ts +183 -0
  234. package/src/v8/draft.ts +15 -14
  235. package/src/v8/draftChanges.ts +151 -85
  236. package/src/v8/errors.ts +37 -0
  237. package/src/v8/hostedSource.ts +67 -0
  238. package/src/v8/identity.ts +1 -1
  239. package/src/v8/index.ts +1 -0
  240. package/src/v8/matchRoute.ts +7 -4
  241. package/src/v8/pin.ts +176 -0
  242. package/src/v8/preview/StudioBridge.ts +8 -10
  243. package/src/v8/preview/StudioChannel.ts +116 -0
  244. package/src/v8/preview/index.ts +3 -0
  245. package/src/v8/remoteLoader.ts +350 -286
  246. package/src/v8/resolver.ts +56 -33
  247. package/src/v8/revisionId.ts +14 -0
  248. package/src/v8/settings.ts +10 -9
  249. package/src/v8/telemetry.ts +3 -3
  250. package/src/v8/types.ts +52 -40
  251. package/src/v8/variants.ts +30 -26
  252. package/src/v8/vite/index.ts +3 -3
  253. package/dist/v8/cli/serve/server.d.ts +0 -29
  254. package/dist/v8/cli/serve/server.d.ts.map +0 -1
  255. package/dist/v8/cli/serve/server.js +0 -296
  256. package/dist/v8/cli/serve/server.js.map +0 -1
  257. package/src/v8/cli/serve/server.ts +0 -348
package/src/v8/content.ts CHANGED
@@ -1,32 +1,34 @@
1
1
  /**
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.
2
+ * The content side of a CMS: one source (the content module, a `Loader`, or
3
+ * hosted releases) and what every client shares. Clients are handed a
4
+ * `Selection`: the current index (and the previous one, for hosted releases)
5
+ * that a request pins (see ./pin.ts).
5
6
  *
6
- * A draft is the release with a draft's changes layered over it (see
7
- * ./draftChanges.ts).
7
+ * A draft is the current index with a draft's files laid over it (see
8
+ * ./draftChanges.ts), read for each draft request and never kept: the
9
+ * draft reads of one request share one read of its files, and nothing read
10
+ * by name from `drafts/` serves a later request.
8
11
  */
9
- import { formatDraftPointer, parseDraftPointer } from "./draft.ts";
12
+ import { keepAlive } from "./background.ts";
13
+ import { type IndexedContent, localIndex } from "./contentIndex.ts";
14
+ import { parseDraftPointer } from "./draft.ts";
10
15
  import {
11
16
  DEFAULT_DRAFT_HOSTS,
12
- type DraftChanges,
13
- fetchDraftChanges,
17
+ type DraftState,
18
+ fetchDraft,
14
19
  LOCAL_VERSION,
15
- layerDraft,
20
+ overlayDraft,
16
21
  } from "./draftChanges.ts";
17
22
  import { errors, isResolutionError } from "./errors.ts";
23
+ import {
24
+ type HostedSource,
25
+ isHostedSource,
26
+ previewHostsOf,
27
+ type ReleasePreviewHosts,
28
+ } from "./hostedSource.ts";
18
29
  import { isPlainObject } from "./json.ts";
19
- import type { Loader, Snapshot } from "./types.ts";
20
-
21
- /** Fetched drafts kept per CMS. */
22
- const CACHED_DRAFTS = 3;
23
-
24
- /** A fetched draft body, its ETag, and the views of it over each release it was layered on. */
25
- interface CachedDraft {
26
- changes: DraftChanges;
27
- etag: string | null;
28
- views: WeakMap<Snapshot, Snapshot>;
29
- }
30
+ import type { Selection } from "./pin.ts";
31
+ import type { DraftPointer, Loader, Snapshot } from "./types.ts";
30
32
 
31
33
  export function isLoader(content: unknown): content is Loader {
32
34
  return (
@@ -37,8 +39,8 @@ export function isLoader(content: unknown): content is Loader {
37
39
  }
38
40
 
39
41
  /**
40
- * An internal hook a built-in loader (`remoteLoader`) has: the release it
41
- * would return right now, from memory, or `undefined` when that takes a load.
42
+ * An internal hook a built-in local loader has: the snapshot it would return
43
+ * right now, from memory, or `undefined` when that takes a load.
42
44
  * `cms.settings()` reads through it, so it never fetches.
43
45
  */
44
46
  export const PEEK_RELEASE: unique symbol = Symbol.for("decocms.blocks.peekRelease");
@@ -50,59 +52,19 @@ export function peekRelease(source: Snapshot | Loader): Snapshot | undefined {
50
52
  return typeof peek === "function" ? peek.call(source) : undefined;
51
53
  }
52
54
 
53
- /**
54
- * An internal hook `remoteLoader` has: a published release by its commit SHA
55
- * (`sites/<site>/revisions/<sha>.json`), kept in memory once read. Rejects
56
- * with `LOADER_FAILED`, never falls back to another release.
57
- */
58
- export const READ_REVISION: unique symbol = Symbol.for("decocms.blocks.readRevision");
59
-
60
- /**
61
- * An internal hook `remoteLoader` has: the current release's preview hosts
62
- * from the release metadata (`latest.json`), read once when this process
63
- * hasn't read them yet (see `ReleasePreviewHosts`).
64
- */
65
- export const PREVIEW_HOSTS: unique symbol = Symbol.for("decocms.blocks.previewHosts");
66
-
67
- /**
68
- * What the current release says about where it may be previewed (PO 2026-10-22):
69
- * - a list: the hosts Studio wrote (`previewHosts`), enforced;
70
- * - `"every"`: a remote site with no list published, so every host: no
71
- * `latest.json` yet (a 404), or one without `previewHosts` (published
72
- * before Studio wrote the list; its next publish writes one);
73
- * - `"local"`: content with no remote site (no release metadata at all):
74
- * `createCMS`'s `preview.hosts` when set, else every host;
75
- * - `"unknown"`: the metadata couldn't be read (a network error, a 5xx, a
76
- * malformed `latest.json`). That isn't "missing": previews stay closed.
77
- */
78
- export type ReleasePreviewHosts = readonly string[] | "every" | "local" | "unknown";
79
-
80
- /**
81
- * A release's `previewHosts` as Studio wrote them: the strings of a list;
82
- * `undefined` when the field is absent (no list written); an empty list (no
83
- * host) for a field that isn't a list, so a malformed one never opens previews.
84
- */
85
- export function previewHostsOf(value: unknown): string[] | undefined {
86
- if (value === undefined) return undefined;
87
- if (!Array.isArray(value)) return [];
88
- return value.filter((host): host is string => typeof host === "string");
89
- }
90
-
91
55
  export function isSnapshot(content: unknown): content is Snapshot {
92
56
  return (
93
57
  isPlainObject(content) &&
94
58
  typeof content.revision === "string" &&
95
59
  isPlainObject(content.blocks) &&
96
60
  (content.aliases === undefined || isPlainObject(content.aliases)) &&
97
- (content.schemaHash === undefined || typeof content.schemaHash === "string") &&
98
- (content.committedAt === undefined || typeof content.committedAt === "string") &&
99
61
  (content.previewHosts === undefined || Array.isArray(content.previewHosts))
100
62
  );
101
63
  }
102
64
 
103
65
  export class ContentStore {
104
66
  #source: Snapshot | Loader;
105
- #release: Promise<Snapshot> | undefined;
67
+ #release: Promise<IndexedContent> | undefined;
106
68
  #updating: Promise<{ updated: boolean }> | undefined;
107
69
  /** The check a draft read waits on when one was already running when it arrived (see `checkRelease`). */
108
70
  #queued: Promise<string> | undefined;
@@ -115,11 +77,10 @@ export class ContentStore {
115
77
  */
116
78
  #checksDone = 0;
117
79
  #checkPrefix: string | undefined;
118
- readonly #drafts = new BoundedMap<string, CachedDraft>(CACHED_DRAFTS);
119
- /** Draft reads in flight, so concurrent renders of one draft share one request. */
120
- readonly #draftReads = new Map<string, Promise<CachedDraft>>();
80
+ /** Draft reads shared under one release check, by the check's number, then the pointer (`#draftFiles`). */
81
+ readonly #draftReads = new Map<number, Map<string, Promise<Map<string, DraftState>>>>();
121
82
  readonly #draftHosts: readonly string[];
122
- /** The release this store last handed a client, for a loader that can't peek. */
83
+ /** The snapshot this store last loaded, for a loader that can't peek. */
123
84
  #latest: Snapshot | undefined;
124
85
 
125
86
  constructor(source: Snapshot | Loader, draftHosts: readonly string[] = DEFAULT_DRAFT_HOSTS) {
@@ -127,20 +88,40 @@ export class ContentStore {
127
88
  this.#draftHosts = draftHosts;
128
89
  }
129
90
 
130
- /** Whether the source can change while the process runs (has `update()`). */
91
+ /** Whether the source can change while the process runs (hosted, or a loader with `update()`). */
131
92
  get updatable(): boolean {
132
- return isLoader(this.#source) && typeof this.#source.update === "function";
93
+ const source = this.#source;
94
+ if (isHostedSource(source)) return true;
95
+ return isLoader(source) && typeof source.update === "function";
133
96
  }
134
97
 
135
98
  /**
136
- * The current release. A snapshot is the release; a loader with `update()`
137
- * is loaded once and reloaded after an update; a loader without `update()`
138
- * is asked on every call, since it's always current by construction.
99
+ * The current release, as an index. Hosted: the index held (the first use
100
+ * loads it), with the previous one. A snapshot is the release; a loader
101
+ * with `update()` is loaded once (concurrent calls share the one load in
102
+ * flight; a failed load isn't kept) and reloaded after an update; a loader
103
+ * without `update()` is asked on every call, since it's always current by
104
+ * construction.
139
105
  */
140
- release(): Promise<Snapshot> {
106
+ release(): Promise<Selection> {
141
107
  const source = this.#source;
142
- if (!isLoader(source)) return Promise.resolve(this.#serve(source));
143
- if (!this.updatable) return this.#load(source).then((snapshot) => this.#serve(snapshot));
108
+ if (isHostedSource(source)) {
109
+ return source.current().then(
110
+ (current): Selection => {
111
+ const previous = source.previous();
112
+ return previous === undefined ? { current } : { current, previous };
113
+ },
114
+ (error: unknown) => {
115
+ throw isResolutionError(error)
116
+ ? error
117
+ : errors.loaderFailed("the content loader failed", error);
118
+ },
119
+ );
120
+ }
121
+ if (!isLoader(source)) return Promise.resolve({ current: this.#serve(source) });
122
+ if (!this.updatable) {
123
+ return this.#load(source).then((snapshot) => ({ current: this.#serve(snapshot) }));
124
+ }
144
125
  if (this.#release === undefined) {
145
126
  const pending = this.#load(source).then((snapshot) => this.#serve(snapshot));
146
127
  this.#release = pending;
@@ -149,143 +130,108 @@ export class ContentStore {
149
130
  if (this.#release === pending) this.#release = undefined;
150
131
  });
151
132
  }
152
- return this.#release;
133
+ return this.#release.then((current) => ({ current }));
153
134
  }
154
135
 
155
136
  /**
156
- * The draft a pointer names: its changes, fetched from a draft host,
157
- * layered over the release. A pointer that doesn't parse, or any failure, is
137
+ * The draft a pointer names: its files, read from a draft host, laid over
138
+ * the current index. A pointer that doesn't parse, or any failure, is
158
139
  * `LOADER_FAILED`, never a silent fallback to the release. A pointer whose
159
- * version is `local` names no draft: the release, with nothing fetched. The
160
- * changes are keyed by the pointer without its `__variant` parameters (every
161
- * variant of one draft shares one body) and revalidated on every read: a
162
- * `304` reuses the body held, a `200` replaces it.
140
+ * version is `local` names no draft: the release, with nothing fetched.
141
+ * Every draft request reads the draft again (nothing from `drafts/` is kept
142
+ * for a later request; the reads of one request, under its `releaseCheck`,
143
+ * share one), and never the previous index.
163
144
  *
164
- * Before layering, a draft read checks the source for a newer release and
145
+ * Before overlaying, a draft read checks the source for a newer release and
165
146
  * waits for that check (see `checkRelease`), so a draft always shows over
166
147
  * the latest release, even in a process that hasn't picked it up yet. The
167
- * check runs beside the draft's own revalidation. With `releaseCheck` (the
168
- * id `checkRelease` resolved with, for this request), it waits for that
169
- * check instead of making its own; an id this store didn't make is a check
170
- * of its own.
148
+ * check runs beside the draft's own reads. With `releaseCheck` (the id
149
+ * `checkRelease` resolved with, for this request), it waits for that check
150
+ * instead of making its own; an id this store didn't make is a check of its
151
+ * own.
171
152
  */
172
- draft(pointer: string, releaseCheck?: string | null): Promise<Snapshot> {
153
+ draft(pointer: string, releaseCheck?: string | null): Promise<Selection> {
173
154
  const parsed = parseDraftPointer(pointer);
174
155
  if (parsed === null) {
175
156
  return Promise.reject(errors.loaderFailed(`invalid draft pointer "${truncate(pointer)}"`));
176
157
  }
177
- if (parsed.version === LOCAL_VERSION) return this.release();
178
- const { host, path, version } = parsed;
179
- const key = formatDraftPointer({ host, path, version });
180
- let read = this.#draftReads.get(key);
181
- if (read === undefined) {
182
- const held = this.#drafts.get(key);
183
- read = fetchDraftChanges(parsed, { domains: this.#draftHosts, etag: held?.etag }).then(
184
- (result) => {
185
- if (result.status === 304 && held !== undefined) return held;
186
- if (result.status === 304) throw new Error("draft changes: 304 without a held body");
187
- const entry: CachedDraft = {
188
- changes: result.changes,
189
- etag: result.etag,
190
- views: new WeakMap(),
191
- };
192
- this.#drafts.set(key, entry);
193
- return entry;
194
- },
195
- );
196
- const pending = read;
197
- this.#draftReads.set(key, pending);
198
- const settle = () => {
199
- if (this.#draftReads.get(key) === pending) this.#draftReads.delete(key);
200
- };
201
- pending.then(settle, settle);
158
+ const { variants } = parsed;
159
+ const withVariants = (selection: Selection): Selection =>
160
+ variants === undefined ? selection : { ...selection, variants };
161
+ if (parsed.version === LOCAL_VERSION) {
162
+ return this.release().then(({ current }) => withVariants({ current }));
202
163
  }
203
- const changes = read.catch((error: unknown) => {
204
- throw errors.loaderFailed("the draft's changes couldn't be fetched", error);
205
- });
164
+ const files = this.#draftFiles(parsed, releaseCheck);
206
165
  const release = this.waitForCheck(releaseCheck).then(() => this.release());
207
- return Promise.all([release, changes]).then(([base, entry]) => {
208
- let view = entry.views.get(base);
209
- if (view === undefined) {
210
- // OPEN: the view is keyed by the body's ETag (the pointer outlives later saves); the version without one.
211
- view = layerDraft(base, entry.changes, entry.etag ?? version);
212
- entry.views.set(base, view);
213
- }
214
- return view;
215
- });
166
+ return Promise.all([release, files]).then(([base, states]) =>
167
+ withVariants({ current: overlayDraft(base.current, states, parsed.version) }),
168
+ );
216
169
  }
217
170
 
218
171
  /**
219
- * A published release by its commit SHA: from the source's hosted releases
220
- * (`remoteLoader`, which `createCMS` builds with `site`), or the content in
221
- * memory when it is that revision. Anything else is `LOADER_FAILED`, never
222
- * a silent fallback to the current release.
172
+ * A published release by its revision id: from the source's hosted
173
+ * releases (`remoteLoader`, which `createCMS` builds with `site`), or the
174
+ * content in memory when it is that revision. Anything else is
175
+ * `LOADER_FAILED`, never a silent fallback to the current release.
223
176
  */
224
- revision(sha: string): Promise<Snapshot> {
177
+ revision(id: string): Promise<Selection> {
225
178
  const source = this.#source;
226
- const read = isLoader(source)
227
- ? (source as { [READ_REVISION]?: (sha: string) => Promise<Snapshot> })[READ_REVISION]
228
- : undefined;
229
- if (typeof read === "function") {
230
- return read.call(source, sha).catch((error: unknown) => {
231
- if (isResolutionError(error)) throw error;
232
- throw errors.loaderFailed(`the release ${sha} couldn't be read`, error);
233
- });
179
+ if (isHostedSource(source)) {
180
+ return source.revision(id).then(
181
+ (current): Selection => ({ current }),
182
+ (error: unknown) => {
183
+ if (isResolutionError(error)) throw error;
184
+ throw errors.loaderFailed(`the release ${id} couldn't be read`, error);
185
+ },
186
+ );
234
187
  }
235
- return this.release().then((snapshot) => {
236
- if (snapshot.revision === sha) return snapshot;
188
+ return this.release().then((selection) => {
189
+ if (selection.current.revision === id) return { current: selection.current };
237
190
  throw errors.loaderFailed(
238
- `the release ${sha} isn't available: published releases are read from hosted releases (createCMS with site)`,
191
+ `the release ${id} isn't available: published releases are read from hosted releases (createCMS with site)`,
239
192
  );
240
193
  });
241
194
  }
242
195
 
243
196
  /**
244
- * The current release as it is in memory, without loading anything: the
245
- * content module, what a built-in loader holds, or the release this store
246
- * last loaded. `undefined` before a custom loader's first load.
197
+ * The current release's index as it is in memory, without loading
198
+ * anything: the content module, what a built-in loader holds, or the
199
+ * release this store last loaded. `undefined` before the first load.
247
200
  */
248
- current(): Snapshot | undefined {
249
- return peekRelease(this.#source) ?? this.#latest;
201
+ current(): IndexedContent | undefined {
202
+ const source = this.#source;
203
+ if (isHostedSource(source)) return source.peek();
204
+ const snapshot = peekRelease(source) ?? this.#latest;
205
+ return snapshot === undefined ? undefined : localIndex(snapshot);
250
206
  }
251
207
 
252
208
  /**
253
209
  * The current release's preview hosts (see `ReleasePreviewHosts`): what the
254
- * source's release metadata says (`remoteLoader` reads it once if it hasn't
255
- * yet; a failed read is `"unknown"`), else, for a source with no release
256
- * metadata (no remote site), the release in memory's own `previewHosts`, or
257
- * `"local"` without one. `peek` reads memory only, never the network.
210
+ * hosted release metadata says (read once if it hasn't been yet; a failed
211
+ * read is `"unknown"`), else, for a source with no release metadata (no
212
+ * remote site), the snapshot in memory's own `previewHosts`, or `"local"`
213
+ * without one. `peek` reads memory only, never the network.
258
214
  */
259
215
  async previewHosts(peek = false): Promise<ReleasePreviewHosts> {
260
216
  const source = this.#source;
261
- const read = isLoader(source)
262
- ? (source as { [PREVIEW_HOSTS]?: (peek: boolean) => Promise<ReleasePreviewHosts> })[
263
- PREVIEW_HOSTS
264
- ]
265
- : undefined;
266
- if (typeof read === "function") {
267
- return read.call(source, peek).catch((): ReleasePreviewHosts => "unknown");
217
+ if (isHostedSource(source)) {
218
+ return source.previewHosts(peek).catch((): ReleasePreviewHosts => "unknown");
268
219
  }
269
- return previewHostsOf(this.current()?.previewHosts) ?? "local";
220
+ const snapshot = peekRelease(source) ?? this.#latest;
221
+ return previewHostsOf(snapshot?.previewHosts) ?? "local";
270
222
  }
271
223
 
272
224
  /** Asks the source for newer content; never throws, and concurrent calls share one check. */
273
225
  update(): Promise<{ updated: boolean }> {
274
226
  const source = this.#source;
275
- if (!isLoader(source) || typeof source.update !== "function") {
276
- return Promise.resolve({ updated: false });
277
- }
227
+ if (!this.updatable) return Promise.resolve({ updated: false });
278
228
  this.#updating ??= (async () => {
279
229
  try {
280
- const result = await source.update?.();
230
+ if (isHostedSource(source)) return await (source as HostedSource).poll();
231
+ const result = await (source as Loader).update?.();
281
232
  const updated = result?.updated === true;
282
- if (updated) {
283
- this.#release = undefined;
284
- // `#latest` stays: settings keep the release that was serving until
285
- // the next one loads, never the defaults (which allow no host).
286
- // Drafts are fetched again, so a preview follows the release too.
287
- this.#drafts.clear();
288
- }
233
+ // `#latest` stays: settings keep the release that was serving until the next one loads.
234
+ if (updated) this.#release = undefined;
289
235
  return { updated };
290
236
  } catch {
291
237
  return { updated: false };
@@ -340,7 +286,57 @@ export class ContentStore {
340
286
  }
341
287
 
342
288
  #startCheck(): Promise<string> {
343
- return this.update().then(() => this.#checkId(++this.#checksDone));
289
+ return this.update().then(() => {
290
+ const n = ++this.#checksDone;
291
+ // Reads under checks older than the one before this are dropped: their requests are over or nearly.
292
+ for (const check of this.#draftReads.keys()) {
293
+ if (check < n - 1) this.#draftReads.delete(check);
294
+ }
295
+ return this.#checkId(n);
296
+ });
297
+ }
298
+
299
+ /**
300
+ * A draft's files (its index, then every file it lists). Reads under one
301
+ * release check share one read: the check's id is handed to every draft
302
+ * read of the request that made it (the host check, the redirects, the
303
+ * assignments and the page), so a preview request reads the draft folder
304
+ * once and every part of it sees the same save. A read under a check starts
305
+ * only after the check ended, so it is never older than any request that
306
+ * shares it. A failed read isn't shared, and reads under old checks are
307
+ * dropped once newer checks end (`#startCheck`): nothing from `drafts/`
308
+ * serves a later request. Without a check id this store made (or for a
309
+ * source that can't change, whose checks are all one), every read is its
310
+ * own.
311
+ */
312
+ #draftFiles(
313
+ pointer: DraftPointer,
314
+ releaseCheck: string | null | undefined,
315
+ ): Promise<Map<string, DraftState>> {
316
+ const read = () =>
317
+ fetchDraft(pointer, { domains: this.#draftHosts }).catch((error: unknown) => {
318
+ throw errors.loaderFailed("the draft's changes couldn't be fetched", error);
319
+ });
320
+ const check = this.updatable ? this.#checkNumber(releaseCheck) : undefined;
321
+ if (check === undefined) return read();
322
+ const key = `${pointer.host}${pointer.path}@${pointer.version}`;
323
+ let reads = this.#draftReads.get(check);
324
+ if (reads === undefined) {
325
+ reads = new Map();
326
+ this.#draftReads.set(check, reads);
327
+ }
328
+ const shared = reads.get(key);
329
+ if (shared !== undefined) return shared;
330
+ const pending = read();
331
+ reads.set(key, pending);
332
+ // Requests under one check share it: kept alive past the request that
333
+ // started it (see ./blockStore.ts), so none of them awaits it forever.
334
+ keepAlive(pending);
335
+ pending.catch(() => {
336
+ if (this.#draftReads.get(check)?.get(key) === pending)
337
+ this.#draftReads.get(check)?.delete(key);
338
+ });
339
+ return pending;
344
340
  }
345
341
 
346
342
  #checkId(n: number): string {
@@ -350,31 +346,29 @@ export class ContentStore {
350
346
 
351
347
  /** Whether `id` names a check this store made (and so one that ended). */
352
348
  #madeCheck(id: string): boolean {
349
+ return this.#checkNumber(id) !== undefined;
350
+ }
351
+
352
+ /** The number of the check `id` names, when this store made it; otherwise `undefined`. */
353
+ #checkNumber(id: string | null | undefined): number | undefined {
353
354
  const prefix = this.#checkPrefix;
354
- if (prefix === undefined || !id.startsWith(prefix)) return false;
355
+ if (typeof id !== "string" || prefix === undefined || !id.startsWith(prefix)) return undefined;
355
356
  const digits = id.slice(prefix.length);
356
- return /^\d{1,15}$/.test(digits) && Number(digits) <= this.#checksDone;
357
+ if (!/^\d{1,15}$/.test(digits)) return undefined;
358
+ const n = Number(digits);
359
+ return n <= this.#checksDone ? n : undefined;
357
360
  }
358
361
 
359
362
  /**
360
363
  * Swaps the content in place, for a hot reload that hands the same CMS a new
361
364
  * content module. Any new object swaps, even with the same `revision`: an
362
- * edited JSON file reloads without rerunning `deco content`, so the new
363
- * module carries new `blocks` under the old revision.
365
+ * edited JSON file can carry new `blocks` under the old revision.
364
366
  */
365
367
  replace(source: Snapshot | Loader): void {
366
- const previous = this.#source;
367
- if (source === previous) {
368
- // The same loader (a hosted remoteLoader that adopted new fallback content): re-read it.
369
- this.#release = undefined;
370
- this.#latest = undefined;
371
- this.#drafts.clear();
372
- return;
373
- }
374
368
  this.#source = source;
369
+ this.#draftReads.clear();
375
370
  this.#release = undefined;
376
371
  this.#latest = undefined;
377
- this.#drafts.clear();
378
372
  }
379
373
 
380
374
  async #load(loader: Loader): Promise<Snapshot> {
@@ -391,56 +385,12 @@ export class ContentStore {
391
385
  return snapshot;
392
386
  }
393
387
 
394
- #serve(snapshot: Snapshot): Snapshot {
388
+ #serve(snapshot: Snapshot): IndexedContent {
395
389
  this.#latest = snapshot;
396
- return snapshot;
390
+ return localIndex(snapshot);
397
391
  }
398
392
  }
399
393
 
400
394
  function truncate(value: string): string {
401
395
  return value.length > 80 ? `${value.slice(0, 80)}…` : value;
402
396
  }
403
-
404
- /**
405
- * A Map that forgets its least recently used entries past `limit`, counted by
406
- * `size` (one per entry unless given).
407
- */
408
- export class BoundedMap<K, V> {
409
- readonly #map = new Map<K, V>();
410
- #total = 0;
411
- constructor(
412
- readonly limit: number,
413
- readonly size: (value: V) => number = () => 1,
414
- ) {}
415
-
416
- get(key: K): V | undefined {
417
- const value = this.#map.get(key);
418
- if (value !== undefined) {
419
- this.#map.delete(key);
420
- this.#map.set(key, value);
421
- }
422
- return value;
423
- }
424
-
425
- set(key: K, value: V): void {
426
- this.delete(key);
427
- this.#map.set(key, value);
428
- this.#total += this.size(value);
429
- for (const [oldest, old] of this.#map) {
430
- if (this.#total <= this.limit || oldest === key) break;
431
- this.#map.delete(oldest);
432
- this.#total -= this.size(old);
433
- }
434
- }
435
-
436
- delete(key: K): void {
437
- if (!this.#map.has(key)) return;
438
- this.#total -= this.size(this.#map.get(key) as V);
439
- this.#map.delete(key);
440
- }
441
-
442
- clear(): void {
443
- this.#map.clear();
444
- this.#total = 0;
445
- }
446
- }