@decocms/blocks 8.1.0-next.2 → 8.1.0-next.4

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 (125) 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/conformance/contentHashFixtures.d.ts +29 -0
  6. package/dist/protocol/conformance/contentHashFixtures.d.ts.map +1 -1
  7. package/dist/protocol/conformance/contentHashFixtures.js +30 -0
  8. package/dist/protocol/conformance/contentHashFixtures.js.map +1 -1
  9. package/dist/protocol/conformance/index.d.ts +1 -1
  10. package/dist/protocol/conformance/index.d.ts.map +1 -1
  11. package/dist/protocol/conformance/index.js +1 -1
  12. package/dist/protocol/conformance/index.js.map +1 -1
  13. package/dist/protocol/index.d.ts +5 -2
  14. package/dist/protocol/index.d.ts.map +1 -1
  15. package/dist/protocol/index.js +5 -2
  16. package/dist/protocol/index.js.map +1 -1
  17. package/dist/protocol/secrets.d.ts.map +1 -1
  18. package/dist/protocol/secrets.js +9 -2
  19. package/dist/protocol/secrets.js.map +1 -1
  20. package/dist/protocol/server/assets.d.ts +1 -1
  21. package/dist/protocol/server/assets.js +1 -1
  22. package/dist/v8/builtins/data.d.ts +21 -5
  23. package/dist/v8/builtins/data.d.ts.map +1 -1
  24. package/dist/v8/builtins/data.js +47 -6
  25. package/dist/v8/builtins/data.js.map +1 -1
  26. package/dist/v8/builtins/index.d.ts.map +1 -1
  27. package/dist/v8/builtins/index.js +2 -3
  28. package/dist/v8/builtins/index.js.map +1 -1
  29. package/dist/v8/canonical.d.ts +28 -0
  30. package/dist/v8/canonical.d.ts.map +1 -1
  31. package/dist/v8/canonical.js +19 -0
  32. package/dist/v8/canonical.js.map +1 -1
  33. package/dist/v8/cli/check/index.d.ts.map +1 -1
  34. package/dist/v8/cli/check/index.js +13 -1
  35. package/dist/v8/cli/check/index.js.map +1 -1
  36. package/dist/v8/cli/index.d.ts +2 -1
  37. package/dist/v8/cli/index.d.ts.map +1 -1
  38. package/dist/v8/cli/index.js +2 -1
  39. package/dist/v8/cli/index.js.map +1 -1
  40. package/dist/v8/cli/run.d.ts.map +1 -1
  41. package/dist/v8/cli/run.js +2 -9
  42. package/dist/v8/cli/run.js.map +1 -1
  43. package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
  44. package/dist/v8/cli/schema/builtinSchemas.js +48 -28
  45. package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
  46. package/dist/v8/cli/schema/generate.d.ts.map +1 -1
  47. package/dist/v8/cli/schema/generate.js +2 -2
  48. package/dist/v8/cli/schema/generate.js.map +1 -1
  49. package/dist/v8/cli/schema/tsProgram.d.ts +16 -0
  50. package/dist/v8/cli/schema/tsProgram.d.ts.map +1 -1
  51. package/dist/v8/cli/schema/tsProgram.js +48 -0
  52. package/dist/v8/cli/schema/tsProgram.js.map +1 -1
  53. package/dist/v8/cli/schema/typeToSchema.d.ts +13 -0
  54. package/dist/v8/cli/schema/typeToSchema.d.ts.map +1 -1
  55. package/dist/v8/cli/schema/typeToSchema.js +85 -2
  56. package/dist/v8/cli/schema/typeToSchema.js.map +1 -1
  57. package/dist/v8/cli/serve/server.d.ts +0 -4
  58. package/dist/v8/cli/serve/server.d.ts.map +1 -1
  59. package/dist/v8/cli/serve/server.js +20 -46
  60. package/dist/v8/cli/serve/server.js.map +1 -1
  61. package/dist/v8/cms.d.ts +5 -2
  62. package/dist/v8/cms.d.ts.map +1 -1
  63. package/dist/v8/cms.js +188 -20
  64. package/dist/v8/cms.js.map +1 -1
  65. package/dist/v8/content.d.ts +32 -2
  66. package/dist/v8/content.d.ts.map +1 -1
  67. package/dist/v8/content.js +82 -19
  68. package/dist/v8/content.js.map +1 -1
  69. package/dist/v8/draft.d.ts +13 -12
  70. package/dist/v8/draft.d.ts.map +1 -1
  71. package/dist/v8/draft.js +75 -9
  72. package/dist/v8/draft.js.map +1 -1
  73. package/dist/v8/hosts.d.ts +55 -0
  74. package/dist/v8/hosts.d.ts.map +1 -0
  75. package/dist/v8/hosts.js +197 -0
  76. package/dist/v8/hosts.js.map +1 -0
  77. package/dist/v8/index.d.ts +4 -3
  78. package/dist/v8/index.d.ts.map +1 -1
  79. package/dist/v8/index.js +3 -2
  80. package/dist/v8/index.js.map +1 -1
  81. package/dist/v8/remoteLoader.d.ts.map +1 -1
  82. package/dist/v8/remoteLoader.js +222 -26
  83. package/dist/v8/remoteLoader.js.map +1 -1
  84. package/dist/v8/settings.d.ts +27 -0
  85. package/dist/v8/settings.d.ts.map +1 -0
  86. package/dist/v8/settings.js +130 -0
  87. package/dist/v8/settings.js.map +1 -0
  88. package/dist/v8/telemetry.d.ts +13 -3
  89. package/dist/v8/telemetry.d.ts.map +1 -1
  90. package/dist/v8/telemetry.js +31 -32
  91. package/dist/v8/telemetry.js.map +1 -1
  92. package/dist/v8/types.d.ts +69 -3
  93. package/dist/v8/types.d.ts.map +1 -1
  94. package/dist/v8/variants.d.ts +3 -0
  95. package/dist/v8/variants.d.ts.map +1 -0
  96. package/dist/v8/variants.js +65 -0
  97. package/dist/v8/variants.js.map +1 -0
  98. package/package.json +1 -1
  99. package/src/index.ts +3 -3
  100. package/src/protocol/conformance/contentHashFixtures.ts +57 -0
  101. package/src/protocol/conformance/index.ts +6 -1
  102. package/src/protocol/index.ts +8 -1
  103. package/src/protocol/secrets.ts +10 -2
  104. package/src/protocol/server/assets.ts +1 -1
  105. package/src/v8/builtins/data.ts +56 -7
  106. package/src/v8/builtins/index.ts +2 -3
  107. package/src/v8/canonical.ts +36 -0
  108. package/src/v8/cli/check/index.ts +23 -1
  109. package/src/v8/cli/index.ts +2 -1
  110. package/src/v8/cli/run.ts +3 -8
  111. package/src/v8/cli/schema/builtinSchemas.ts +49 -28
  112. package/src/v8/cli/schema/generate.ts +2 -1
  113. package/src/v8/cli/schema/tsProgram.ts +44 -0
  114. package/src/v8/cli/schema/typeToSchema.ts +93 -2
  115. package/src/v8/cli/serve/server.ts +20 -55
  116. package/src/v8/cms.ts +235 -23
  117. package/src/v8/content.ts +84 -18
  118. package/src/v8/draft.ts +88 -15
  119. package/src/v8/hosts.ts +184 -0
  120. package/src/v8/index.ts +6 -8
  121. package/src/v8/remoteLoader.ts +258 -23
  122. package/src/v8/settings.ts +157 -0
  123. package/src/v8/telemetry.ts +43 -40
  124. package/src/v8/types.ts +62 -3
  125. package/src/v8/variants.ts +61 -0
@@ -11,19 +11,35 @@
11
11
  * ignored; a revision equal to the fallback's is served from the fallback
12
12
  * without a download; anything else is fetched, verified against its
13
13
  * content hash and swapped in whole. Any error keeps memory as it was.
14
- * - `load(pointer)` fetches the draft a pointer names and waits for it. Only
15
- * pointers to the delivery host are fetched; anything else is refused.
14
+ * - `load(pointer)` loads a draft overlay (see /next/content-delivery#exact-draft-previews)
15
+ * and waits for it: it captures the production snapshot `load()` would
16
+ * return right now, fetches the overlay manifest the pointer's version names
17
+ * and only the changed-block assets not already cached, and layers their
18
+ * replacements and tombstones over the captured snapshot. It never fetches a
19
+ * production revision to align. An authorized manifest is cached only as long
20
+ * as its response's `max-age` (the grant's remaining life), so an expired
21
+ * grant stops working on a warm server too. Only pointers into this site's drafts on the
22
+ * delivery host are fetched; anything else is refused. Any failure rejects:
23
+ * a draft never falls back to published content.
16
24
  * - In development (`NODE_ENV=development`), releases stay on the fallback so
17
25
  * local files win; drafts still load.
18
- * - A release or draft larger than `MAX_SNAPSHOT_BYTES` is refused while it
19
- * downloads, before it's buffered whole (a Worker isolate has 128 MB).
26
+ * - A release, or any one draft asset, larger than `MAX_SNAPSHOT_BYTES` is
27
+ * refused while it downloads, before it's buffered whole (a Worker isolate
28
+ * has 128 MB); a draft whose manifest and changed blocks together exceed it
29
+ * is refused as each asset is counted.
20
30
  * - Without `site` or `token` it's a plain loader over the fallback: no
21
31
  * releases, no drafts beyond what the fallback serves.
22
32
  *
23
33
  * Instances are process-wide singletons, like `createCMS`'s.
24
34
  */
25
- import { computeContentRevision } from "./canonical.ts";
26
- import { isSnapshot } from "./content.ts";
35
+ import {
36
+ computeBlockHash,
37
+ computeContentRevision,
38
+ computeOverlayVersion,
39
+ DRAFT_OVERLAY_FORMAT,
40
+ type DraftOverlay,
41
+ } from "./canonical.ts";
42
+ import { BoundedMap, isSnapshot, PEEK_RELEASE, peekRelease } from "./content.ts";
27
43
  import { parseDraftPointer } from "./draft.ts";
28
44
  import { clearGlobals, contentIdentity, fnv1a } from "./identity.ts";
29
45
  import { isPlainObject } from "./json.ts";
@@ -35,8 +51,15 @@ const HOSTED_DELIVERY_ORIGIN = "https://delivery.decocms.com";
35
51
  const INSTANCE_PREFIX = "decocms.blocks.remote:";
36
52
  const MANIFEST_FORMAT = 1;
37
53
  const FETCH_TIMEOUT_MS = 10_000;
38
- /** The largest release or draft accepted, in bytes of JSON. */
54
+ /** The largest release, or draft (manifest plus changed blocks), accepted, in bytes of JSON. */
39
55
  const MAX_SNAPSHOT_BYTES = 64 * 1024 * 1024;
56
+ /** Draft caches, per site: authorized overlay manifests, and changed-block bytes. */
57
+ const CACHED_OVERLAYS = 16;
58
+ const CACHED_BLOCK_BYTES = 16 * 1024 * 1024;
59
+ /** Changed-block downloads in flight at once, per draft. */
60
+ const BLOCK_FETCH_CONCURRENCY = 8;
61
+ /** Overlay versions and block hashes: SHA-256, lowercase hex. */
62
+ const HASH_RE = /^[0-9a-f]{64}$/;
40
63
 
41
64
  interface RemoteLoaderOptions {
42
65
  site?: string;
@@ -51,6 +74,17 @@ interface Manifest {
51
74
  snapshot: string;
52
75
  }
53
76
 
77
+ /** A downloaded, verified JSON asset and its size in bytes. */
78
+ interface Asset<T> {
79
+ value: T;
80
+ bytes: number;
81
+ }
82
+
83
+ /** An authorized overlay manifest, usable until its grant's `max-age` runs out. */
84
+ interface AuthorizedOverlay extends Asset<DraftOverlay> {
85
+ expiresAt: number;
86
+ }
87
+
54
88
  class RemoteLoader implements Loader {
55
89
  readonly #site: string;
56
90
  readonly #token: string;
@@ -58,8 +92,15 @@ class RemoteLoader implements Loader {
58
92
  readonly interval: number | undefined;
59
93
  #fallback: Snapshot | Loader;
60
94
  #fallbackRevision: string | undefined;
95
+ /** The fallback's content as last loaded, for a fallback that is itself a loader. */
96
+ #fallbackSnapshot: Snapshot | undefined;
61
97
  #current: Snapshot | undefined;
62
98
  #generation = -1;
99
+ /** Authorized manifests, by overlay version and grant: a version alone unlocks nothing. */
100
+ readonly #overlays = new BoundedMap<string, Promise<AuthorizedOverlay>>(CACHED_OVERLAYS);
101
+ /** Verified changed blocks, by block hash, bounded by their bytes. */
102
+ readonly #blocks = new BoundedMap<string, Asset<unknown>>(CACHED_BLOCK_BYTES, (b) => b.bytes);
103
+ readonly #pendingBlocks = new Map<string, Promise<Asset<unknown>>>();
63
104
 
64
105
  constructor(
65
106
  fallback: Snapshot | Loader,
@@ -76,6 +117,12 @@ class RemoteLoader implements Loader {
76
117
  if (fallback === this.#fallback) return;
77
118
  this.#fallback = fallback;
78
119
  this.#fallbackRevision = undefined;
120
+ this.#fallbackSnapshot = undefined;
121
+ }
122
+
123
+ /** What `load()` would return now, from memory only (see `peekRelease`). */
124
+ [PEEK_RELEASE](): Snapshot | undefined {
125
+ return this.#current ?? peekRelease(this.#fallback) ?? this.#fallbackSnapshot;
79
126
  }
80
127
 
81
128
  load(pointer?: string | null): Promise<Snapshot> {
@@ -114,7 +161,10 @@ class RemoteLoader implements Loader {
114
161
  async #loadFallback(): Promise<Snapshot> {
115
162
  const fallback = this.#fallback;
116
163
  const snapshot = isSnapshot(fallback) ? fallback : await (fallback as Loader).load();
117
- if (fallback === this.#fallback) this.#fallbackRevision = snapshot.revision;
164
+ if (fallback === this.#fallback) {
165
+ this.#fallbackRevision = snapshot.revision;
166
+ this.#fallbackSnapshot = snapshot;
167
+ }
118
168
  return snapshot;
119
169
  }
120
170
 
@@ -152,7 +202,9 @@ class RemoteLoader implements Loader {
152
202
  this.#auth(),
153
203
  );
154
204
  if (!response.ok) throw new Error(`release ${manifest.revision}: HTTP ${response.status}`);
155
- const snapshot = await readBoundedJson(response, `release ${manifest.revision}`);
205
+ const [snapshot] = await readBoundedJson(response, `release ${manifest.revision}`, {
206
+ remaining: MAX_SNAPSHOT_BYTES,
207
+ });
156
208
  if (
157
209
  !isSnapshot(snapshot) ||
158
210
  snapshot.revision !== manifest.revision ||
@@ -169,15 +221,112 @@ class RemoteLoader implements Loader {
169
221
  if (pointer.host !== new URL(HOSTED_DELIVERY_ORIGIN).host) {
170
222
  throw new Error(`draft pointer names an unexpected host "${pointer.host}"`);
171
223
  }
172
- // If-Match: serve exactly this version or fail, never the branch's newer head.
173
- const response = await fetchWithTimeout(`https://${pointer.host}${pointer.path}`, {
174
- ...this.#auth(),
175
- "if-match": pointer.version,
224
+ const site = `/sites/${encodeURIComponent(this.#site)}`;
225
+ const query = pointer.path.indexOf("?");
226
+ const pathname = query === -1 ? pointer.path : pointer.path.slice(0, query);
227
+ if (pathname !== `${site}/drafts`) throw new Error("draft pointer names another site's drafts");
228
+ if (!HASH_RE.test(pointer.version)) throw new Error("draft pointer names no overlay version");
229
+ // The grant (the pointer's query) goes, opaque, on every draft asset read.
230
+ const grant = query === -1 ? "" : pointer.path.slice(query);
231
+
232
+ // Capture production once, before any network: this draft is layered over it.
233
+ const base = this.#current ?? (await this.#loadFallback());
234
+ const budget: Budget = { remaining: MAX_SNAPSHOT_BYTES };
235
+ const manifest = charge(
236
+ budget,
237
+ await this.#authorizedOverlay(
238
+ `${site}/drafts/${pointer.version}.json${grant}`,
239
+ pointer.version,
240
+ ),
241
+ );
242
+ const hashes = [...new Set(Object.values(manifest.set))];
243
+ const values = new Map<string, unknown>();
244
+ let next = 0;
245
+ const worker = async () => {
246
+ while (next < hashes.length) {
247
+ const hash = hashes[next++]!;
248
+ values.set(
249
+ hash,
250
+ await this.#block(`${site}/draft-blocks/${hash}.json${grant}`, hash, budget),
251
+ );
252
+ }
253
+ };
254
+ const workers = Math.min(BLOCK_FETCH_CONCURRENCY, hashes.length);
255
+ await Promise.all(Array.from({ length: workers }, worker));
256
+ return composeOverlay(base, manifest, values, pointer.version);
257
+ }
258
+
259
+ /**
260
+ * The manifest a pointer's grant authorizes: cached by version and grant
261
+ * until the response's `max-age` runs out (none, or 0, isn't cached), so an
262
+ * expired grant is asked about again. Concurrent callers share a download.
263
+ */
264
+ async #authorizedOverlay(path: string, version: string): Promise<AuthorizedOverlay> {
265
+ const asked = Date.now();
266
+ const cached = this.#overlays.get(path);
267
+ if (cached !== undefined) {
268
+ const overlay = await cached;
269
+ if (overlay.expiresAt >= asked) return overlay;
270
+ if (this.#overlays.get(path) === cached) this.#overlays.delete(path);
271
+ }
272
+ const pending = this.#overlay(path, version);
273
+ this.#overlays.set(path, pending);
274
+ const forget = () => {
275
+ if (this.#overlays.get(path) === pending) this.#overlays.delete(path);
276
+ };
277
+ pending.then((overlay) => {
278
+ if (overlay.expiresAt <= Date.now()) forget();
279
+ }, forget);
280
+ return pending;
281
+ }
282
+
283
+ async #overlay(path: string, version: string): Promise<AuthorizedOverlay> {
284
+ const response = await fetchWithTimeout(`${HOSTED_DELIVERY_ORIGIN}${path}`, this.#auth());
285
+ if (!response.ok) throw new Error(`draft overlay ${version}: HTTP ${response.status}`);
286
+ const maxAge = cacheLifetimeMs(response.headers.get("cache-control"));
287
+ const [body, bytes] = await readBoundedJson(response, `draft overlay ${version}`, {
288
+ remaining: MAX_SNAPSHOT_BYTES,
176
289
  });
177
- if (!response.ok) throw new Error(`draft: HTTP ${response.status}`);
178
- const snapshot = await readBoundedJson(response, "draft");
179
- if (!isSnapshot(snapshot)) throw new Error("draft: not a snapshot");
180
- return snapshot;
290
+ const overlay = parseOverlay(body);
291
+ if ((await computeOverlayVersion(overlay)) !== version) {
292
+ throw new Error(`draft overlay ${version}: content doesn't match its version`);
293
+ }
294
+ return { value: overlay, bytes, expiresAt: Date.now() + maxAge };
295
+ }
296
+
297
+ /**
298
+ * A changed block, charged to this draft's budget: from the cache, or
299
+ * downloaded once and verified against its hash. Concurrent drafts share
300
+ * one download, which is bounded on its own and then charged to each of
301
+ * them, so one draft's budget never fails another. Only hashes an
302
+ * authorized manifest lists get here.
303
+ */
304
+ async #block(path: string, hash: string, budget: Budget): Promise<unknown> {
305
+ const cached = this.#blocks.get(hash);
306
+ if (cached !== undefined) return charge(budget, cached);
307
+ let pending = this.#pendingBlocks.get(hash);
308
+ if (pending === undefined) {
309
+ const download = (async () => {
310
+ const response = await fetchWithTimeout(`${HOSTED_DELIVERY_ORIGIN}${path}`, this.#auth());
311
+ if (!response.ok) throw new Error(`draft block ${hash}: HTTP ${response.status}`);
312
+ const [value, bytes] = await readBoundedJson(response, `draft block ${hash}`, {
313
+ remaining: MAX_SNAPSHOT_BYTES,
314
+ });
315
+ if ((await computeBlockHash(value)) !== hash) {
316
+ throw new Error(`draft block ${hash}: content doesn't match its hash`);
317
+ }
318
+ const block = { value, bytes };
319
+ this.#blocks.set(hash, block);
320
+ return block;
321
+ })();
322
+ pending = download;
323
+ this.#pendingBlocks.set(hash, download);
324
+ void download.then(
325
+ () => this.#pendingBlocks.delete(hash),
326
+ () => this.#pendingBlocks.delete(hash),
327
+ );
328
+ }
329
+ return charge(budget, await pending);
181
330
  }
182
331
 
183
332
  #auth(): Record<string, string> {
@@ -197,6 +346,10 @@ class LocalLoader implements Loader {
197
346
  this.#fallback = fallback;
198
347
  }
199
348
 
349
+ [PEEK_RELEASE](): Snapshot | undefined {
350
+ return peekRelease(this.#fallback);
351
+ }
352
+
200
353
  load(pointer?: string | null): Promise<Snapshot> {
201
354
  const fallback = this.#fallback;
202
355
  return isSnapshot(fallback) ? Promise.resolve(fallback) : fallback.load(pointer);
@@ -238,14 +391,45 @@ export function resetRemoteLoaders(): void {
238
391
  clearGlobals(INSTANCE_PREFIX);
239
392
  }
240
393
 
241
- /** Parses a JSON body, refusing it as soon as it's larger than `MAX_SNAPSHOT_BYTES`. */
242
- async function readBoundedJson(response: Response, label: string): Promise<unknown> {
394
+ /** What a download may still read, in bytes; shared by every asset of one draft. */
395
+ interface Budget {
396
+ remaining: number;
397
+ }
398
+
399
+ /** Charges an asset's bytes to a draft's budget, and returns its value. */
400
+ function charge<T>(budget: Budget, asset: Asset<T>): T {
401
+ budget.remaining -= asset.bytes;
402
+ if (budget.remaining < 0) throw new Error(`draft: larger than ${MAX_SNAPSHOT_BYTES} bytes`);
403
+ return asset.value;
404
+ }
405
+
406
+ /** A response's private cache lifetime in ms: its `max-age`, or 0 without one or with `no-store`. */
407
+ function cacheLifetimeMs(header: string | null): number {
408
+ if (header === null || /(?:^|,)\s*no-store\b/i.test(header)) return 0;
409
+ const seconds = /(?:^|,)\s*max-age=(\d+)/i.exec(header)?.[1];
410
+ return seconds === undefined ? 0 : Number(seconds) * 1000;
411
+ }
412
+
413
+ /**
414
+ * Parses a JSON body and returns it with its size, refusing it as soon as it
415
+ * would exceed the budget (charged as bytes arrive, so parallel reads share it).
416
+ */
417
+ async function readBoundedJson(
418
+ response: Response,
419
+ label: string,
420
+ budget: Budget,
421
+ ): Promise<[unknown, number]> {
243
422
  const tooLarge = () => new Error(`${label}: larger than ${MAX_SNAPSHOT_BYTES} bytes`);
244
- if (Number(response.headers.get("content-length")) > MAX_SNAPSHOT_BYTES) {
423
+ if (Number(response.headers.get("content-length")) > budget.remaining) {
245
424
  await response.body?.cancel();
246
425
  throw tooLarge();
247
426
  }
248
- if (response.body === null) return response.json();
427
+ if (response.body === null) {
428
+ const text = await response.text();
429
+ budget.remaining -= text.length;
430
+ if (budget.remaining < 0) throw tooLarge();
431
+ return [JSON.parse(text), text.length];
432
+ }
249
433
  const reader = response.body.getReader();
250
434
  const decoder = new TextDecoder();
251
435
  const parts: string[] = [];
@@ -254,14 +438,65 @@ async function readBoundedJson(response: Response, label: string): Promise<unkno
254
438
  const { done, value } = await reader.read();
255
439
  if (done) break;
256
440
  size += value.byteLength;
257
- if (size > MAX_SNAPSHOT_BYTES) {
441
+ budget.remaining -= value.byteLength;
442
+ if (budget.remaining < 0) {
258
443
  await reader.cancel();
259
444
  throw tooLarge();
260
445
  }
261
446
  parts.push(decoder.decode(value, { stream: true }));
262
447
  }
263
448
  parts.push(decoder.decode());
264
- return JSON.parse(parts.join(""));
449
+ return [JSON.parse(parts.join("")), size];
450
+ }
451
+
452
+ /** Validates a draft overlay manifest; throws on an unknown format or any unexpected shape. */
453
+ function parseOverlay(body: unknown): DraftOverlay {
454
+ const invalid = (why: string) => new Error(`draft overlay: ${why}`);
455
+ if (!isPlainObject(body) || body.format !== DRAFT_OVERLAY_FORMAT) {
456
+ throw invalid("unknown format");
457
+ }
458
+ if (Object.keys(body).some((key) => key !== "format" && key !== "set" && key !== "delete")) {
459
+ throw invalid("unexpected field");
460
+ }
461
+ const { set, delete: deleted } = body;
462
+ if (!isPlainObject(set)) throw invalid("set must be an object");
463
+ for (const hash of Object.values(set)) {
464
+ if (typeof hash !== "string" || !HASH_RE.test(hash)) throw invalid("set names a bad hash");
465
+ }
466
+ if (!Array.isArray(deleted) || deleted.some((name) => typeof name !== "string")) {
467
+ throw invalid("delete must be a list of names");
468
+ }
469
+ if (new Set(deleted).size !== deleted.length) throw invalid("delete repeats a name");
470
+ if (deleted.some((name) => Object.hasOwn(set, name))) throw invalid("set and delete overlap");
471
+ return body as unknown as DraftOverlay;
472
+ }
473
+
474
+ /**
475
+ * The draft as a snapshot: the captured production entries, with the
476
+ * overlay's replacements and without its tombstones. A shallow copy: every
477
+ * unchanged entry is production's own object, and production is never
478
+ * mutated. Its revision is an opaque identity of the pair, not a content hash.
479
+ */
480
+ function composeOverlay(
481
+ base: Snapshot,
482
+ overlay: DraftOverlay,
483
+ values: Map<string, unknown>,
484
+ version: string,
485
+ ): Snapshot {
486
+ const blocks: Record<string, unknown> = { ...base.blocks };
487
+ for (const name of overlay.delete) delete blocks[name];
488
+ for (const [name, hash] of Object.entries(overlay.set)) {
489
+ // defineProperty, so an entry named "__proto__" stays an entry.
490
+ Object.defineProperty(blocks, name, {
491
+ value: values.get(hash),
492
+ enumerable: true,
493
+ writable: true,
494
+ configurable: true,
495
+ });
496
+ }
497
+ const view: Snapshot = { revision: `${base.revision}~${version}`, blocks };
498
+ if (base.aliases !== undefined) view.aliases = base.aliases;
499
+ return view;
265
500
  }
266
501
 
267
502
  function fetchWithTimeout(url: string, headers: Record<string, string>): Promise<Response> {
@@ -0,0 +1,157 @@
1
+ /**
2
+ * The CMS settings (see /next/built-in-blocks#cms-settings and
3
+ * /next/api-reference#cms-settings): the saved block `CMS`, of the built-in
4
+ * type `cms-settings`, read from the release, with the defaults filled in and
5
+ * code's caps applied.
6
+ *
7
+ * - The caller hands in the release snapshot already in memory; nothing here
8
+ * loads content, so reading settings never fetches.
9
+ * - A missing block, a block of another type, or a section that fails to
10
+ * resolve gives that section's defaults, still capped. Nothing rejects.
11
+ * - Telemetry rates are the lower of content's and `telemetry.limits`.
12
+ * - Preview hosts: content's entries that fall within code's `preview.hosts`
13
+ * (all of them when code sets none); without the field, code's list, or
14
+ * every host.
15
+ */
16
+ import {
17
+ analyticsSection,
18
+ SETTINGS_BLOCK,
19
+ SETTINGS_TYPE,
20
+ telemetrySection,
21
+ } from "./builtins/data.ts";
22
+ import {
23
+ EVERY_HOST,
24
+ formatHostPattern,
25
+ type HostPattern,
26
+ parseHostPattern,
27
+ patternWithin,
28
+ } from "./hosts.ts";
29
+ import { isPlainObject } from "./json.ts";
30
+ import type { Client, CMSConfig, EffectiveSettings, Snapshot } from "./types.ts";
31
+
32
+ /** What code allows: the caps every section's content is held to. */
33
+ export interface SettingsCaps {
34
+ /** Code's `preview.hosts`, parsed; `undefined` when code sets none. */
35
+ hosts: readonly HostPattern[] | undefined;
36
+ limits: { errorSampleRate: number; traceSampleRate: number };
37
+ }
38
+
39
+ const SECTIONS = ["preview", "telemetry", "analytics"] as const;
40
+
41
+ /**
42
+ * Code's `preview.hosts`, parsed. Throws a `TypeError` on anything that isn't
43
+ * a list of host patterns: that's a bug in the app, not in content.
44
+ */
45
+ export function parseCodeHosts(preview: CMSConfig["preview"]): HostPattern[] | undefined {
46
+ if (preview === undefined || preview === null) return undefined;
47
+ if (typeof preview !== "object") {
48
+ throw new TypeError("createCMS: `preview` must be an object, such as { hosts: [...] }");
49
+ }
50
+ const { hosts } = preview;
51
+ if (hosts === undefined) return undefined;
52
+ if (!Array.isArray(hosts)) {
53
+ throw new TypeError("createCMS: `preview.hosts` must be a list of host patterns");
54
+ }
55
+ return hosts.map((raw) => {
56
+ const pattern = parseHostPattern(raw);
57
+ if (pattern === null) {
58
+ throw new TypeError(
59
+ `createCMS: ${JSON.stringify(raw)} in preview.hosts isn't a host pattern ` +
60
+ "(see /next/api-reference#host-patterns)",
61
+ );
62
+ }
63
+ return pattern;
64
+ });
65
+ }
66
+
67
+ /** The settings with nothing saved: every default, capped. */
68
+ export function defaultSettings(caps: SettingsCaps): EffectiveSettings {
69
+ return effectiveSettings({}, caps);
70
+ }
71
+
72
+ /**
73
+ * The settings a release holds. `client` reads that same release; it resolves
74
+ * the `CMS` block whole and, if that fails, each section on its own, so one
75
+ * failing section falls back to its defaults alone.
76
+ */
77
+ export async function readSettings(
78
+ snapshot: Snapshot | undefined,
79
+ client: () => Client,
80
+ caps: SettingsCaps,
81
+ ): Promise<EffectiveSettings> {
82
+ try {
83
+ const entry = snapshot?.blocks[SETTINGS_BLOCK];
84
+ if (!isPlainObject(entry) || entry.__resolveType !== SETTINGS_TYPE) {
85
+ return defaultSettings(caps);
86
+ }
87
+ const reader = client();
88
+ const [whole] = await reader.resolve<Record<string, unknown>>(SETTINGS_BLOCK);
89
+ if (isPlainObject(whole)) return effectiveSettings(whole, caps);
90
+ const sections: Record<string, unknown> = {};
91
+ await Promise.all(
92
+ SECTIONS.map(async (name) => {
93
+ if (entry[name] === undefined) return;
94
+ const [value] = await reader.resolve(entry[name]);
95
+ sections[name] = value ?? undefined; // a section that fails gets its defaults
96
+ }),
97
+ );
98
+ return effectiveSettings(sections, caps);
99
+ } catch {
100
+ return defaultSettings(caps);
101
+ }
102
+ }
103
+
104
+ /** Whether a saved block holds nothing to run, so its settings are the same on every call. */
105
+ export function isStatic(entry: Record<string, unknown>): boolean {
106
+ return Object.entries(entry).every(
107
+ ([key, value]) => key === "__resolveType" || !holdsBlock(value),
108
+ );
109
+ }
110
+
111
+ function holdsBlock(value: unknown): boolean {
112
+ if (Array.isArray(value)) return value.some(holdsBlock);
113
+ if (!isPlainObject(value)) return false;
114
+ return "__resolveType" in value || Object.values(value).some(holdsBlock);
115
+ }
116
+
117
+ /**
118
+ * The settings, deep-frozen: one object per release is handed to every caller,
119
+ * so a caller that writes to it would change it for every request.
120
+ */
121
+ function effectiveSettings(value: Record<string, unknown>, caps: SettingsCaps): EffectiveSettings {
122
+ const telemetry = telemetrySection(value.telemetry);
123
+ return deepFreeze({
124
+ preview: { hosts: effectiveHosts(value.preview, caps.hosts) },
125
+ telemetry: {
126
+ ...telemetry,
127
+ errorSampleRate: Math.min(telemetry.errorSampleRate, caps.limits.errorSampleRate),
128
+ traceSampleRate: Math.min(telemetry.traceSampleRate, caps.limits.traceSampleRate),
129
+ },
130
+ analytics: analyticsSection(value.analytics),
131
+ });
132
+ }
133
+
134
+ function deepFreeze<T>(value: T): T {
135
+ if (value !== null && typeof value === "object" && !Object.isFrozen(value)) {
136
+ Object.freeze(value);
137
+ for (const child of Object.values(value)) deepFreeze(child);
138
+ }
139
+ return value;
140
+ }
141
+
142
+ function effectiveHosts(preview: unknown, code: readonly HostPattern[] | undefined): string[] {
143
+ const saved = isPlainObject(preview) && Array.isArray(preview.hosts) ? preview.hosts : undefined;
144
+ if (saved === undefined) return code ? unique(code.map(formatHostPattern)) : [EVERY_HOST];
145
+ const kept: string[] = [];
146
+ for (const raw of saved) {
147
+ const pattern = parseHostPattern(raw);
148
+ if (pattern === null) continue; // not a pattern: left out
149
+ if (code && !code.some((cap) => patternWithin(pattern, cap))) continue;
150
+ kept.push(formatHostPattern(pattern));
151
+ }
152
+ return unique(kept);
153
+ }
154
+
155
+ function unique(values: string[]): string[] {
156
+ return [...new Set(values)];
157
+ }