@decocms/blocks 8.9.0 → 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/client.ts CHANGED
@@ -1,13 +1,28 @@
1
1
  /**
2
- * A client: one revision, loaded on first use and read for the rest of its
3
- * life, plus a memo of block results, and the inputs it was created with
4
- * (the version, the visitor, the date, the tests and campaigns it was given).
5
- * Create one per request.
2
+ * A client: one request's reads. It pins one index on its first content
3
+ * access (see ./pin.ts) and reads it, with every body it fetched, for the rest
4
+ * of its life, plus a memo of block results, and the inputs it was created
5
+ * with (the version, the visitor, the date, the tests and campaigns it was
6
+ * given). Create one per request.
7
+ *
8
+ * The binding flow (see /next/routing): `routes()` → `matchRoute` →
9
+ * `prefetch(name)` → `redirects()` → `matchRoute` → `resolve(name)`. The
10
+ * page's subtree and the redirects block go out in one parallel wave.
6
11
  */
12
+ import { keepAlive } from "./background.ts";
13
+ import {
14
+ canonicalType,
15
+ namesOfType,
16
+ REDIRECTS_TYPE,
17
+ redirectsBlockOf,
18
+ routesOf,
19
+ walk,
20
+ } from "./contentIndex.ts";
7
21
  import { CAMPAIGN_OVERRIDES, EXPERIMENT_OVERRIDES, type Overrides } from "./current.ts";
8
22
  import { errors, isResolutionError, ResolutionError } from "./errors.ts";
9
23
  import type { Visitor } from "./experiments.ts";
10
- import { isBlock } from "./json.ts";
24
+ import { isPlainObject } from "./json.ts";
25
+ import { choosePin, type Pin, rootsOf, type Selection } from "./pin.ts";
11
26
  import { Resolver } from "./resolver.ts";
12
27
  import type { ClientTelemetry } from "./telemetry.ts";
13
28
  import type {
@@ -15,15 +30,17 @@ import type {
15
30
  Blocks,
16
31
  Client,
17
32
  CMSError,
33
+ LegacyRedirect,
18
34
  ListOptions,
35
+ Redirect,
19
36
  ResolveOptions,
20
37
  Result,
21
- Snapshot,
38
+ Route,
22
39
  } from "./types.ts";
23
40
 
24
41
  interface ClientOptions {
25
- /** Loads this client's snapshot; called at most once. */
26
- load: () => Promise<Snapshot>;
42
+ /** Loads what this client reads; called at most once. */
43
+ load: () => Promise<Selection>;
27
44
  blocks: Blocks;
28
45
  /** The built-in blocks. */
29
46
  builtIns: Readonly<Blocks>;
@@ -41,14 +58,20 @@ interface ClientOptions {
41
58
  telemetry?: ClientTelemetry;
42
59
  }
43
60
 
44
- type Loaded = { resolver: Resolver; snapshot: Snapshot } | { error: CMSError };
61
+ type Selected = { selection: Selection } | { error: CMSError };
62
+ type Pinned = { pin: Pin; resolver: Resolver } | { error: CMSError };
63
+
64
+ /** Types `list()` warns about once per process: routing reads `routes()` and `redirects()`. */
65
+ const ROUTED_TYPES = new Set(["page", "redirect", REDIRECTS_TYPE]);
66
+ const LIST_WARNED = Symbol.for("decocms.blocks.listWarned");
45
67
 
46
68
  export class CMSClient implements Client {
47
69
  readonly #options: ClientOptions;
48
70
  readonly #reported = new WeakSet<CMSError>();
49
71
  readonly #visitor: Visitor | null;
50
- #loaded: Promise<Loaded> | undefined;
51
- /** The revision loaded, once it is. */
72
+ #selected: Promise<Selected> | undefined;
73
+ #pinned: Promise<Pinned> | undefined;
74
+ /** The revision pinned, once it is. */
52
75
  #revision: string | undefined;
53
76
 
54
77
  constructor(options: ClientOptions) {
@@ -78,20 +101,62 @@ export class CMSClient implements Client {
78
101
  return this.#options.campaigns;
79
102
  }
80
103
 
104
+ async routes(): Promise<Result<Route[]>> {
105
+ // A pinned client answers from its pin; otherwise the current index, without pinning.
106
+ const pinned = this.#pinned === undefined ? undefined : await this.#pinned;
107
+ if (pinned !== undefined) {
108
+ if ("error" in pinned) return this.#fail(pinned.error);
109
+ return [routesOf(pinned.pin.content), null];
110
+ }
111
+ const selected = await this.#select();
112
+ if ("error" in selected) return this.#fail(selected.error);
113
+ return [routesOf(selected.selection.current), null];
114
+ }
115
+
116
+ prefetch(name: string): void {
117
+ if (typeof name !== "string") return;
118
+ // Kept alive: a request may end (a redirect, a cached page) before its
119
+ // prefetch settles, and later requests share the fetches it started.
120
+ keepAlive(
121
+ this.#pin(name).then((pinned) => {
122
+ if ("error" in pinned) return;
123
+ return pinned.pin.wave(walk(pinned.pin.content, rootsOf(pinned.pin.content, name)));
124
+ }),
125
+ );
126
+ }
127
+
128
+ async redirects(): Promise<Result<Redirect[]>> {
129
+ const pinned = await this.#pin();
130
+ if ("error" in pinned) return this.#fail(pinned.error);
131
+ const { pin, resolver } = pinned;
132
+ const name = redirectsBlockOf(pin.content);
133
+ if (name === null) return [[], null];
134
+ try {
135
+ await pin.wave(walk(pin.content, [name]));
136
+ const value = await resolver.resolveEntry(name);
137
+ return [rulesOf(value), null];
138
+ } catch (error) {
139
+ return this.#fail(asCMSError(error));
140
+ }
141
+ }
142
+
81
143
  async resolve<T = unknown>(target: unknown, options: ResolveOptions = {}): Promise<Result<T>> {
82
- const loaded = await this.#load();
83
- if ("error" in loaded) return this.#fail(loaded.error);
84
- const { resolver } = loaded;
144
+ const pinned = await this.#pin(typeof target === "string" ? target : undefined);
145
+ if ("error" in pinned) return this.#fail(pinned.error);
146
+ const { pin, resolver } = pinned;
85
147
  const run = options.run !== false;
86
148
  try {
87
149
  if (typeof target === "string") {
88
- if (resolver.entry(target) === undefined) return this.#fail(errors.notFound(target));
89
- const value = run ? await resolver.resolveEntry(target) : resolver.expandEntry(target);
150
+ if (!resolver.has(target)) return this.#fail(errors.notFound(target));
151
+ await pin.wave(walk(pin.content, [target]));
152
+ const value = run
153
+ ? await resolver.resolveEntry(target)
154
+ : await resolver.expandEntry(target);
90
155
  return [value as T, null];
91
156
  }
92
157
  const value = run
93
158
  ? await resolver.resolve(target, [], [], false)
94
- : resolver.expand(target, [], [], false);
159
+ : await resolver.expand(target, [], [], false);
95
160
  return [value as T, null];
96
161
  } catch (error) {
97
162
  return this.#fail(asCMSError(error));
@@ -99,26 +164,22 @@ export class CMSClient implements Client {
99
164
  }
100
165
 
101
166
  async list<T = Block>(type: string, options: ListOptions<T> = {}): Promise<Result<T[]>> {
102
- const loaded = await this.#load();
103
- if ("error" in loaded) return this.#fail(loaded.error);
104
- const { resolver, snapshot } = loaded;
105
- const wanted = resolver.canonicalType(type);
106
- const names = Object.keys(snapshot.blocks)
107
- .sort(byCodeUnit)
108
- .filter((name) => {
109
- const entry = snapshot.blocks[name];
110
- return isBlock(entry) && resolver.canonicalType(entry.__resolveType) === wanted;
111
- });
167
+ const pinned = await this.#pin();
168
+ if ("error" in pinned) return this.#fail(pinned.error);
169
+ const { pin, resolver } = pinned;
170
+ warnRoutedList(canonicalType(pin.content, type));
171
+ const names = namesOfType(pin.content, type);
112
172
 
113
173
  let entries: T[];
114
174
  try {
175
+ await pin.wave(walk(pin.content, names));
115
176
  const values = await Promise.all(
116
177
  names.map((name) =>
117
- options.run === true
118
- ? resolver.resolveEntry(name).catch((error) => {
119
- throw atEntry(name, error);
120
- })
121
- : resolver.expandEntry(name),
178
+ (options.run === true ? resolver.resolveEntry(name) : resolver.expandEntry(name)).catch(
179
+ (error) => {
180
+ throw atEntry(name, error);
181
+ },
182
+ ),
122
183
  ),
123
184
  );
124
185
  entries = values.filter((value) => value !== undefined) as T[];
@@ -135,34 +196,42 @@ export class CMSClient implements Client {
135
196
  }
136
197
 
137
198
  async revision(): Promise<string> {
138
- const loaded = await this.#load();
139
- if ("error" in loaded) throw loaded.error;
140
- return loaded.snapshot.revision;
141
- }
142
-
143
- #load(): Promise<Loaded> {
144
- this.#loaded ??= this.#options.load().then(
145
- (snapshot): Loaded => {
146
- this.#revision = snapshot.revision;
147
- return {
148
- snapshot,
149
- resolver: new Resolver({
150
- snapshot,
151
- blocks: this.#options.blocks,
152
- builtIns: this.#options.builtIns,
153
- client: this,
154
- onCollision: this.#options.onCollision,
155
- onBlock: this.#options.telemetry?.block,
156
- }),
157
- };
158
- },
159
- (error): Loaded => ({
199
+ const pinned = await this.#pin();
200
+ if ("error" in pinned) throw pinned.error;
201
+ return pinned.pin.content.revision;
202
+ }
203
+
204
+ #select(): Promise<Selected> {
205
+ this.#selected ??= this.#options.load().then(
206
+ (selection): Selected => ({ selection }),
207
+ (error): Selected => ({
160
208
  error: isResolutionError(error)
161
209
  ? error
162
210
  : errors.loaderFailed("the content loader failed", error),
163
211
  }),
164
212
  );
165
- return this.#loaded;
213
+ return this.#selected;
214
+ }
215
+
216
+ /** Pins on the first content access; `name` is the page it will read, when known. */
217
+ #pin(name?: string): Promise<Pinned> {
218
+ this.#pinned ??= this.#select().then((selected): Pinned => {
219
+ if ("error" in selected) return selected;
220
+ const pin = choosePin(selected.selection, name);
221
+ this.#revision = pin.content.revision;
222
+ return {
223
+ pin,
224
+ resolver: new Resolver({
225
+ content: pin,
226
+ blocks: this.#options.blocks,
227
+ builtIns: this.#options.builtIns,
228
+ client: this,
229
+ onCollision: this.#options.onCollision,
230
+ onBlock: this.#options.telemetry?.block,
231
+ }),
232
+ };
233
+ });
234
+ return this.#pinned;
166
235
  }
167
236
 
168
237
  /** Returns the error result, reporting each error to telemetry once per client. */
@@ -175,6 +244,44 @@ export class CMSClient implements Client {
175
244
  }
176
245
  }
177
246
 
247
+ /** The redirects block's value: its rules, each an object (both shapes `matchRoute` reads). */
248
+ function rulesOf(value: unknown): Redirect[] {
249
+ const list = Array.isArray(value)
250
+ ? value
251
+ : isPlainObject(value) && Array.isArray(value.redirects)
252
+ ? value.redirects
253
+ : [];
254
+ return list.filter((rule): rule is Redirect | LegacyRedirect =>
255
+ isPlainObject(rule),
256
+ ) as Redirect[];
257
+ }
258
+
259
+ /**
260
+ * `list("page")` and `list("redirect")` read every page or rule: routing reads
261
+ * `routes()` and `redirects()` from the index instead. Warns once per process
262
+ * and type, outside production.
263
+ */
264
+ function warnRoutedList(type: string): void {
265
+ if (!ROUTED_TYPES.has(type)) return;
266
+ const env = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process
267
+ ?.env;
268
+ if (env?.NODE_ENV === "production") return;
269
+ const store = globalThis as unknown as Record<symbol, Set<string> | undefined>;
270
+ const warned = store[LIST_WARNED] ?? new Set<string>();
271
+ store[LIST_WARNED] = warned;
272
+ if (warned.has(type)) return;
273
+ warned.add(type);
274
+ console.warn(
275
+ `[decocms/blocks] client.list("${type}") fetches every ${type} block; route with ` +
276
+ "client.routes() and client.redirects(), which read the index (see /next/routing).",
277
+ );
278
+ }
279
+
280
+ /** Forgets which list types warned, so a test starts clean. */
281
+ export function resetListWarnings(): void {
282
+ delete (globalThis as unknown as Record<symbol, unknown>)[LIST_WARNED];
283
+ }
284
+
178
285
  /**
179
286
  * The client's own frozen copy of a visitor, so a caller changing its object
180
287
  * later changes no client; `null` for anything that isn't one.
@@ -197,11 +304,6 @@ function copyVisitor(visitor: Visitor | null | undefined): Visitor | null {
197
304
  return Object.freeze(copy);
198
305
  }
199
306
 
200
- /** Plain code-unit order, the same on every server and locale. */
201
- function byCodeUnit(a: string, b: string): number {
202
- return a < b ? -1 : a > b ? 1 : 0;
203
- }
204
-
205
307
  function asCMSError(error: unknown): CMSError {
206
308
  return isResolutionError(error) ? error : errors.blockFailed("<unknown>", [], error);
207
309
  }
package/src/v8/cms.ts CHANGED
@@ -9,14 +9,16 @@
9
9
  import { runInBackground } from "./background.ts";
10
10
  import { SETTINGS_BLOCK, SETTINGS_TYPE } from "./builtins/data.ts";
11
11
  import { builtIns } from "./builtins/index.ts";
12
- import { CMSClient } from "./client.ts";
12
+ import { CMSClient, resetListWarnings } from "./client.ts";
13
13
  import { ContentStore, isLoader, isSnapshot } from "./content.ts";
14
+ import type { IndexedContent } from "./contentIndex.ts";
14
15
  import type { Overrides } from "./current.ts";
15
16
  import { draftCookieFor, endsPreview, parseClientVersion, readVersion } from "./draft.ts";
16
17
  import { parseDraftHosts } from "./draftChanges.ts";
17
18
  import { allowsHost, type HostPattern, parseHostPattern } from "./hosts.ts";
18
19
  import { clearGlobals, contentIdentity, fnv1a } from "./identity.ts";
19
20
  import { isPlainObject } from "./json.ts";
21
+ import type { Selection } from "./pin.ts";
20
22
  import { remoteLoader, resetRemoteLoaders } from "./remoteLoader.ts";
21
23
  import {
22
24
  deepFreeze,
@@ -47,7 +49,6 @@ import type {
47
49
  RequestVersion,
48
50
  Snapshot,
49
51
  } from "./types.ts";
50
- import { forceVariants } from "./variants.ts";
51
52
 
52
53
  const INSTANCE_PREFIX = "decocms.blocks.cms:";
53
54
  const MIN_INTERVAL = 60_000;
@@ -77,12 +78,12 @@ class CMSInstance {
77
78
  readonly #warned = new Set<string>();
78
79
  readonly #telemetry: TelemetryPipeline | undefined;
79
80
  readonly #caps: SettingsCaps;
80
- /** Settings of a `CMS` block with nothing to run, per release snapshot. */
81
- #staticSettings = new WeakMap<Snapshot, SavedSettings>();
81
+ /** Settings of a `CMS` block with nothing to run, per release index. */
82
+ #staticSettings = new WeakMap<IndexedContent, SavedSettings>();
82
83
  /** Code's `preview.hosts`, parsed: added to the release's own preview hosts, or the only list without a site. */
83
84
  readonly #codeHosts: readonly HostPattern[] | undefined;
84
85
  /** The release and time telemetry last read its section. */
85
- #telemetryRead: { snapshot: Snapshot; at: number } | undefined;
86
+ #telemetryRead: { content: IndexedContent; at: number } | undefined;
86
87
  /** One handle per block map, so the same config returns the same object. */
87
88
  readonly #handles = new WeakMap<Blocks, CMS>();
88
89
  /**
@@ -107,7 +108,7 @@ class CMSInstance {
107
108
 
108
109
  /**
109
110
  * A client for one request: the version it reads (the current release, a
110
- * published release by its commit SHA, or a draft pointer), the visitor it
111
+ * published release by its revision id, or a draft pointer), the visitor it
111
112
  * decides tests for and the date it checks campaigns at (default: now),
112
113
  * all passed in, with the tests and campaigns it is given on top.
113
114
  */
@@ -129,23 +130,20 @@ class CMSInstance {
129
130
  if (parsed === null)
130
131
  return this.#client(blocks, () => this.#store.draft(version, check), inputs);
131
132
  if (parsed.kind === "revision") {
132
- return this.#client(blocks, () => this.#store.revision(parsed.sha), inputs);
133
+ return this.#client(blocks, () => this.#store.revision(parsed.id), inputs);
133
134
  }
134
- const { variants } = parsed.pointer;
135
- const load = () => this.#store.draft(version, check);
136
- if (variants === undefined) return this.#client(blocks, load, inputs);
137
- return this.#client(blocks, () => load().then((s) => forceVariants(s, variants)), inputs);
135
+ return this.#client(blocks, () => this.#store.draft(version, check), inputs);
138
136
  }
139
137
 
140
138
  /** Loads the current release, keeping telemetry on the release's settings. */
141
- #release(blocks: Blocks): () => Promise<Snapshot> {
139
+ #release(blocks: Blocks): () => Promise<Selection> {
142
140
  const telemetry = this.#telemetry;
143
141
  if (telemetry === undefined) return () => this.#store.release();
144
142
  return () =>
145
- this.#store.release().then(async (snapshot) => {
146
- telemetry.useRelease(snapshot);
147
- await this.#followTelemetrySettings(blocks, telemetry, snapshot);
148
- return snapshot;
143
+ this.#store.release().then(async (selection) => {
144
+ telemetry.useRelease(selection.current);
145
+ await this.#followTelemetrySettings(blocks, telemetry, selection.current);
146
+ return selection;
149
147
  });
150
148
  }
151
149
 
@@ -215,15 +213,20 @@ class CMSInstance {
215
213
  this.#telemetryRead = undefined;
216
214
  }
217
215
 
218
- async #settingsOf(blocks: Blocks, snapshot: Snapshot | undefined): Promise<SavedSettings> {
219
- if (snapshot === undefined) return defaultSettings(this.#caps);
220
- const cached = this.#staticSettings.get(snapshot);
216
+ /**
217
+ * The settings of a release index: the `CMS` body comes from memory (held
218
+ * by local content, inline in a hosted index as `data`), never a fetch.
219
+ */
220
+ async #settingsOf(blocks: Blocks, content: IndexedContent | undefined): Promise<SavedSettings> {
221
+ if (content === undefined) return defaultSettings(this.#caps);
222
+ const cached = this.#staticSettings.get(content);
221
223
  if (cached !== undefined) return cached;
224
+ const entry = settingsBody(content);
222
225
  const settings = await readSettings(
223
- snapshot,
226
+ entry,
224
227
  () =>
225
228
  new CMSClient({
226
- load: () => Promise.resolve(snapshot),
229
+ load: () => Promise.resolve({ current: content }),
227
230
  blocks,
228
231
  builtIns,
229
232
  date: Date.now(),
@@ -232,10 +235,9 @@ class CMSInstance {
232
235
  this.#caps,
233
236
  );
234
237
  // A block map's own cms-settings function may answer differently per call.
235
- const entry = snapshot.blocks[SETTINGS_BLOCK];
236
238
  const fixed = !isPlainObject(entry) || isStatic(entry);
237
239
  if (fixed && !Object.hasOwn(blocks, SETTINGS_TYPE)) {
238
- this.#staticSettings.set(snapshot, settings);
240
+ this.#staticSettings.set(content, settings);
239
241
  }
240
242
  return settings;
241
243
  }
@@ -262,23 +264,23 @@ class CMSInstance {
262
264
  async #followTelemetrySettings(
263
265
  blocks: Blocks,
264
266
  telemetry: TelemetryPipeline,
265
- snapshot: Snapshot,
267
+ content: IndexedContent,
266
268
  ): Promise<void> {
267
269
  const last = this.#telemetryRead;
268
270
  const now = Date.now();
269
- if (last?.snapshot === snapshot && now - last.at < TELEMETRY_SETTINGS_MS) return;
270
- this.#telemetryRead = { snapshot, at: now };
271
- const read = this.#settingsOf(blocks, snapshot).then((settings) => {
272
- if (this.#telemetryRead?.snapshot === snapshot) telemetry.apply(settings.telemetry);
271
+ if (last?.content === content && now - last.at < TELEMETRY_SETTINGS_MS) return;
272
+ this.#telemetryRead = { content, at: now };
273
+ const read = this.#settingsOf(blocks, content).then((settings) => {
274
+ if (this.#telemetryRead?.content === content) telemetry.apply(settings.telemetry);
273
275
  });
274
276
  // A new release waits for its rates; a periodic re-read doesn't hold up the request.
275
- if (last?.snapshot !== snapshot) await read;
277
+ if (last?.content !== content) await read;
276
278
  }
277
279
 
278
280
  /** A client with its inputs (each client copies its visitor, so none is shared). */
279
281
  #client(
280
282
  blocks: Blocks,
281
- load: () => Promise<Snapshot>,
283
+ load: () => Promise<Selection>,
282
284
  inputs: {
283
285
  version: string | undefined;
284
286
  visitor: ClientOptions["visitor"];
@@ -323,6 +325,15 @@ class CMSInstance {
323
325
  }
324
326
  }
325
327
 
328
+ /** The `CMS` block's body from memory: held (local content) or inline (`data`, hosted). */
329
+ function settingsBody(content: IndexedContent): unknown {
330
+ if (content.held?.has(SETTINGS_BLOCK)) return content.held.get(SETTINGS_BLOCK);
331
+ const entry = Object.hasOwn(content.blocks, SETTINGS_BLOCK)
332
+ ? content.blocks[SETTINGS_BLOCK]
333
+ : undefined;
334
+ return entry?.data;
335
+ }
336
+
326
337
  /** `Symbol.for`, so a second copy of this module still reads it. */
327
338
  const INSTANCE = Symbol.for("decocms.blocks.cms.instance");
328
339
 
@@ -399,7 +410,7 @@ export function instanceOf(cms: CMS): object | undefined {
399
410
  export function createCMS(config: CMSConfig): CMS {
400
411
  validate(config);
401
412
  // A remoteLoader passed as `content` carries the `interval` it was created with.
402
- const own = (config.content as { interval?: unknown }).interval;
413
+ const own = (config.content as { interval?: unknown } | undefined)?.interval;
403
414
  const interval = resolveInterval(config.interval ?? (typeof own === "number" ? own : undefined));
404
415
  const key = Symbol.for(INSTANCE_PREFIX + identityOf(config));
405
416
  const store = globalThis as unknown as Record<symbol, CMSInstance | undefined>;
@@ -423,7 +434,8 @@ export function createCMS(config: CMSConfig): CMS {
423
434
  /**
424
435
  * Hands a new content module to every CMS created for its `.deco` folder, as
425
436
  * a later `createCMS` call with the same content would, but without the
426
- * site's config: each instance keeps its own options (`site`, `dev`, …).
437
+ * site's config: each instance keeps its own options. Only content without
438
+ * `site` comes from a content module (with `site`, it comes from the CDN).
427
439
  * Returns how many instances took it; none when no CMS has been created for
428
440
  * that folder yet (the first `createCMS` call then reads the new module) or
429
441
  * when the module has no `root` to match on.
@@ -441,7 +453,7 @@ export function adoptContent(content: Snapshot): number {
441
453
  let adopted = 0;
442
454
  for (const symbol of Object.getOwnPropertySymbols(globalThis)) {
443
455
  const key = Symbol.keyFor(symbol);
444
- if (key === undefined || (key !== own && !key.startsWith(`${own}|site:`))) continue;
456
+ if (key !== own) continue;
445
457
  const instance = store[symbol] as Partial<CMSInstance> | undefined;
446
458
  if (
447
459
  !(instance instanceof Object) ||
@@ -473,24 +485,14 @@ function overridesOf(given: unknown): Overrides | undefined {
473
485
  export function resetForTests(): void {
474
486
  clearGlobals(INSTANCE_PREFIX);
475
487
  resetRemoteLoaders();
488
+ resetListWarnings();
476
489
  setCurrentTelemetry(undefined);
477
490
  }
478
491
 
479
- /** With `site`, the content is the fallback of hosted releases. */
492
+ /** With `site`, hosted releases from the delivery CDN, at runtime only; without, the content given. */
480
493
  function contentOf(config: CMSConfig): Snapshot | Loader {
481
- if (!config.site) return config.content;
482
- return remoteLoader(config.content, { site: config.site, dev: config.dev ?? isDevelopment() });
483
- }
484
-
485
- /**
486
- * `NODE_ENV=development`: the SDK's one environment read (an explicit `dev`
487
- * overrides it). Guarded, so a Worker or a browser without `process` reads
488
- * `false`; spelled `process.env.NODE_ENV` so a bundler's `define` replaces it.
489
- */
490
- function isDevelopment(): boolean {
491
- return (
492
- typeof process !== "undefined" && process.env != null && process.env.NODE_ENV === "development"
493
- );
494
+ if (!config.site) return config.content as Snapshot | Loader;
495
+ return remoteLoader(undefined, { site: config.site });
494
496
  }
495
497
 
496
498
  function validate(config: CMSConfig): void {
@@ -500,9 +502,16 @@ function validate(config: CMSConfig): void {
500
502
  if (config.blocks === null || typeof config.blocks !== "object") {
501
503
  throw new TypeError("createCMS: `blocks` must be your block map (an object of functions)");
502
504
  }
503
- if (!isLoader(config.content) && !isSnapshot(config.content)) {
505
+ if (config.site) {
506
+ // Content is never bundled: with site, it comes from the delivery CDN at runtime.
507
+ if (config.content !== undefined) {
508
+ throw new TypeError(
509
+ "createCMS: with site, content comes from the delivery CDN at runtime; remove content",
510
+ );
511
+ }
512
+ } else if (!isLoader(config.content) && !isSnapshot(config.content)) {
504
513
  throw new TypeError(
505
- "createCMS: `content` must be the content module ({ revision, blocks }) or a loader with load()",
514
+ "createCMS: `content` must be a snapshot ({ revision, blocks }) or a loader with load(), or pass site",
506
515
  );
507
516
  }
508
517
  // The token is the site's credential: it means nothing without the site it belongs to.
@@ -526,8 +535,8 @@ function resolveInterval(configured: number | undefined): number {
526
535
  }
527
536
 
528
537
  /**
529
- * The instance key: the content's identity (never its revision, so a hot
530
- * reload keeps the instance) and, with hosted releases, the site.
538
+ * The instance key: with hosted releases, the site; otherwise the content's
539
+ * identity (never its revision, so a hot reload keeps the instance).
531
540
  *
532
541
  * A content module is identified by the `.deco` folder it was generated from
533
542
  * (its `root`). One without a `root` is identified by the object itself, so
@@ -535,8 +544,8 @@ function resolveInterval(configured: number | undefined): number {
535
544
  * new instance instead of updating the old.
536
545
  */
537
546
  function identityOf(config: CMSConfig): string {
538
- const content = contentIdentity(config.content);
539
- return config.site ? `${content}|site:${config.site}` : content;
547
+ if (config.site) return `site:${config.site}`;
548
+ return contentIdentity(config.content as Snapshot | Loader);
540
549
  }
541
550
 
542
551
  function fingerprintOf(config: CMSConfig, interval: number): Fingerprint {