@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
@@ -1,175 +1,170 @@
1
1
  /**
2
- * `remoteLoader`: hosted releases from `delivery.decocms.com`, over a fallback
3
- * (the content module, or any loader). See /next/hosted-releases-internals.
4
- * Drafts aren't a loader's job: the CMS layers a draft's changes over what
5
- * `load()` returns (see ./draftChanges.ts).
2
+ * `remoteLoader`: hosted releases from `delivery.decocms.com`, read at
3
+ * runtime only (see /next/hosted-releases-internals). Content is never
4
+ * bundled: with `site`, the release comes from the CDN or not at all. Sites
5
+ * talk only to the CDN, with plain HTTP (ETag, `If-None-Match`, 304).
6
6
  *
7
- * - `load()` never touches the network: it returns the release this process
8
- * last swapped in, or the fallback's content until then. Nothing persists
9
- * across restarts: every boot starts from the fallback.
10
- * - `update()` (the CMS calls it in the background, on first use and then
11
- * every interval, and once per draft request, which waits for it, so a
12
- * draft always shows over the latest release: `cms.checkRelease()`) reads
13
- * `sites/<site>/latest.json`,
14
- * `{ revision, schemaHash, publishedAt, previewHosts }`. Whoever is newer wins: when it
15
- * names a revision other than the one this process last swapped in, its
16
- * `schemaHash` equals the fallback's, and its `publishedAt` is later than
17
- * the fallback's `committedAt` (the commit time of the git HEAD
18
- * `deco content` built it from), it downloads
19
- * `sites/<site>/revisions/<revision>.json`,
20
- * `{ revision, schemaHash, blocks, previewHosts }`, and swaps it in whole. Otherwise, and on any error, memory stays as it is.
21
- * A fallback without a `schemaHash` never swaps; one without a `committedAt`
22
- * (a custom loader, an older content module, a build outside git) counts as
23
- * the oldest. Studio writes `publishedAt` on Publish, on "Make current" (a
24
- * rollback) and on Resync, so a rollback wins over the bundles of earlier
25
- * commits and a deploy of a later commit wins over the rollback. Commit
26
- * time, not build time: a slow build of an older commit that finishes after
27
- * a publish still loses to it.
28
- * Pointers aren't ordered among themselves, and the fallback's content is
29
- * never compared: only the two timestamps are.
30
- * - `previewHosts` (host patterns) is where the site may be previewed: Studio
31
- * knows the site's addresses and writes them into the release metadata
32
- * whenever it writes `latest.json` or a revision (delivery is trusted).
33
- * Previews are checked against the current release's list: the last
34
- * `latest.json` read, whether or not its revision was swapped in (a newer
35
- * bundle still previews where Studio says). With no list to check
36
- * (PO 2026-10-22), previews are allowed on every host: a site with no
37
- * `latest.json` yet (a 404), or one whose `latest.json` has no
38
- * `previewHosts` (published before Studio wrote the list; its next publish
39
- * writes one). A read that fails (a network error, a 5xx, a malformed
40
- * `latest.json`) isn't "no list": until a read succeeds, previews stay
41
- * closed, and a later failure keeps the last answer (a 404 once a list
42
- * was read is a failure too: a published site stays published).
43
- * A preview request before this process has read `latest.json` reads it
44
- * then (only `latest.json`, once, shared); a read older than the interval
45
- * is read again the same way, so `dev: true` follows Studio too.
46
- * - With `dev: true`, it never swaps, so local files win. `createCMS` passes
47
- * its `dev` option, or `NODE_ENV === "development"` when the site omits it;
48
- * this loader reads no environment itself.
49
- * - A release larger than `MAX_SNAPSHOT_BYTES` is refused while it downloads,
50
- * before it's buffered whole (a Worker isolate has 128 MB).
51
- * - A client can ask for a published release by its commit SHA
52
- * (`cms.client({ version })`): it's read from
53
- * `sites/<site>/revisions/<sha>.json` on first use and kept in memory, a
54
- * few at a time (the least recently used dropped first), since a revision
55
- * never changes. One built with another schema than the bundled content is
56
- * an error, never half a page.
57
- * - Without `site` it's a plain loader over the fallback.
7
+ * - **The index.** `sites/<site>/latest.json` is the current revision's
8
+ * index (`{ revision, sequence, previewHosts, blocks: { <name>: { version,
9
+ * meta?, data? } } }`). The first use loads it (calls made meanwhile share
10
+ * the load; a failed load isn't kept and rejects with a
11
+ * `ContentUnavailableError`, a 404 included: never an empty site). A poll
12
+ * (the CMS starts one in the background every interval, and a draft
13
+ * request's release check is one) sends `If-None-Match` on the held ETag, so
14
+ * an unchanged index is a 304: nothing is downloaded or parsed. A read is
15
+ * adopted only when its `sequence` is higher than the held one, so a
16
+ * slightly stale CDN answer never moves the isolate backwards; the index
17
+ * that was current is kept as the previous one (one only). A failed poll
18
+ * keeps the held index.
19
+ * - **Blocks.** `sites/<site>/blocks/<hash>.json`, immutable, fetched with
20
+ * plain `fetch()` (it reads through the CDN cache) when a request needs one,
21
+ * into an LRU per isolate keyed by hash and bounded by bytes (32 MiB of
22
+ * JSON text). Concurrent requests share one fetch per hash; a failed fetch
23
+ * (a 404 included) isn't kept.
24
+ * - **Revisions by id** (`cms.client({ version: <id> })`): the
25
+ * `revisions/<id>/index.json` of that revision, read on first use and kept,
26
+ * three at a time, since a revision never changes. Bodies by hash as usual.
27
+ * - Each fetch has 10 s (headers and body), sends no credential, and refuses
28
+ * a body over 16 MiB while it downloads. Unknown keys are ignored.
29
+ * - `previewHosts`: as before, from the last `latest.json` read whose index
30
+ * was adopted (a 304, or a stale answer with a lower or equal `sequence`,
31
+ * keeps the list it had). A site with no `latest.json` yet (a 404), or one without
32
+ * `previewHosts`, previews on every host; until a read succeeds, previews
33
+ * stay closed, and a later failure keeps the last answer.
34
+ * - Without `site` it's a plain loader over the content it's given.
58
35
  *
59
36
  * Instances are process-wide singletons, like `createCMS`'s.
60
37
  */
61
- import { readBoundedJson, timeoutSignal } from "./boundedJson.js";
62
- import { BoundedMap, isSnapshot, PEEK_RELEASE, PREVIEW_HOSTS, peekRelease, previewHostsOf, READ_REVISION, } from "./content.js";
63
- import { errors } from "./errors.js";
38
+ import { keepAlive } from "./background.js";
39
+ import { BlockStore } from "./blockStore.js";
40
+ import { readBoundedJson, readBoundedJsonSized, timeoutSignal } from "./boundedJson.js";
41
+ import { BoundedMap } from "./boundedMap.js";
42
+ import { PEEK_RELEASE, peekRelease } from "./content.js";
43
+ import { ContentUnavailableError, errors } from "./errors.js";
44
+ import { HOSTED, previewHostsOf, } from "./hostedSource.js";
64
45
  import { clearGlobals, contentIdentity } from "./identity.js";
65
46
  import { isPlainObject } from "./json.js";
66
- /** The hosted delivery origin: release pointers and revisions. */
47
+ import { isRevisionId } from "./revisionId.js";
48
+ /** The hosted delivery origin. */
67
49
  const HOSTED_DELIVERY_ORIGIN = "https://delivery.decocms.com";
68
- /** test-only: replaces the delivery origin in local end-to-end runs. Not documented, not exported. */
50
+ /**
51
+ * test-only: replaces the delivery origin (it may carry a path prefix) in
52
+ * local end-to-end runs. Not documented, not exported.
53
+ */
69
54
  const TEST_DELIVERY_ORIGIN = Symbol.for("decocms.blocks.test.deliveryOrigin");
70
55
  const INSTANCE_PREFIX = "decocms.blocks.remote:";
71
56
  const FETCH_TIMEOUT_MS = 10_000;
72
- /** The largest release accepted, in bytes of JSON. */
73
- const MAX_SNAPSHOT_BYTES = 64 * 1024 * 1024;
74
- /** Published releases kept in memory once read (a client asked for one by its SHA). */
57
+ /** The largest index (`latest.json`, a revision's `index.json`) accepted, in bytes of JSON. */
58
+ const MAX_INDEX_BYTES = 16 * 1024 * 1024;
59
+ /** The largest block body accepted, in bytes of JSON. */
60
+ const MAX_BLOCK_BYTES = 16 * 1024 * 1024;
61
+ /** Revision indexes kept in memory once read (a client asked for one by its id). */
75
62
  const CACHED_REVISIONS = 3;
76
- // OPEN: a revision is the git commit SHA; only SHA-1 (40 hex) object names are accepted.
77
- const REVISION = /^[0-9a-f]{40}$/;
78
- const SCHEMA_HASH = /^[0-9a-f]{64}$/;
79
63
  /** How long a failed read of the preview hosts waits before a preview request tries again. */
80
64
  const PREVIEW_HOSTS_RETRY_MS = 10_000;
81
65
  /** How old the preview hosts may get before a preview request reads them again (without `interval`). */
82
66
  const PREVIEW_HOSTS_MAX_AGE_MS = 60_000;
67
+ /** A block hash: `blockVersion`, base64url of 16 bytes. */
68
+ const HASH_RE = /^[A-Za-z0-9_-]{22}$/;
69
+ /** The one entry whose body an index carries inline. */
70
+ const INLINE_BLOCK = "CMS";
83
71
  class RemoteLoader {
72
+ [HOSTED] = true;
84
73
  #site;
85
- /** The `interval` this loader was created with; `createCMS` reads it. */
86
74
  interval;
87
- #fallback;
88
- /** The fallback's content as last loaded, for a fallback that is itself a loader. */
89
- #fallbackSnapshot;
90
- /** The release this process last swapped in; `undefined` serves the fallback. */
75
+ blocks;
76
+ /** The index this process holds; `undefined` until the first load succeeds. */
91
77
  #current;
92
- #dev;
93
- /** Published releases read by SHA; immutable, so a hit is never revalidated. */
78
+ #previous;
79
+ /** A read of `latest.json` in flight: the first load, a poll or a preview-hosts read. */
80
+ #reading;
94
81
  #revisions = new BoundedMap(CACHED_REVISIONS);
95
82
  /**
96
- * The preview hosts the last read of `latest.json` found (its list, or
97
- * `"every"`: a 404, or no `previewHosts` in it), and when. A failed read
98
- * leaves it as it was.
83
+ * The preview hosts of the held release, from the last read of
84
+ * `latest.json` that was adopted (its list, or `"every"`: a 404, or no
85
+ * `previewHosts` in it), and when it was last confirmed. A failed read, or
86
+ * a stale one, leaves the list as it was.
99
87
  */
100
88
  #hosts;
101
- /** A read of `latest.json` for its preview hosts, in flight. */
102
89
  #hostsRead;
103
- /** When that read last failed. */
104
90
  #hostsFailedAt = Number.NEGATIVE_INFINITY;
105
- constructor(fallback, options) {
106
- this.#fallback = fallback;
91
+ constructor(options) {
107
92
  this.#site = options.site;
108
93
  this.interval = options.interval;
109
- this.#dev = options.dev === true;
94
+ this.blocks = new BlockStore((hash, name) => this.#fetchBlock(hash, name));
110
95
  }
111
- /** A hot reload hands the same instance new fallback content. */
112
- adopt(fallback) {
113
- if (fallback === this.#fallback)
114
- return;
115
- this.#fallback = fallback;
116
- this.#fallbackSnapshot = undefined;
96
+ /** Hosted content is read lazily, by index and hash; there is no whole snapshot to load. */
97
+ load() {
98
+ return Promise.reject(errors.loaderFailed("hosted releases are read through createCMS (an index plus blocks by hash), not as one snapshot"));
117
99
  }
118
- /** What `load()` would return now, from memory only (see `peekRelease`). */
119
- [PEEK_RELEASE]() {
120
- return this.#current ?? peekRelease(this.#fallback) ?? this.#fallbackSnapshot;
100
+ peek() {
101
+ return this.#current?.content;
121
102
  }
122
- load() {
123
- if (this.#current !== undefined)
124
- return Promise.resolve(this.#current);
125
- return this.#loadFallback();
103
+ previous() {
104
+ return this.#previous;
126
105
  }
127
- async update() {
128
- if (this.#dev)
129
- return { updated: false };
130
- const fallback = this.#fallbackSnapshot ?? (await this.#loadFallback());
131
- if (fallback.schemaHash === undefined)
132
- return { updated: false };
133
- const site = encodeURIComponent(this.#site);
134
- const latest = await this.#latest(site);
135
- // Another schema: keep what this process serves (the fallback, or the last swap).
136
- if (latest.schemaHash !== fallback.schemaHash)
137
- return { updated: false };
138
- // The bundle is newer: keep what this process serves.
139
- // OPEN: a process that already swapped a release in keeps it (memory stays as it is).
140
- if (!isNewer(latest.publishedAt, fallback.committedAt))
141
- return { updated: false };
142
- if (latest.revision === this.#current?.revision)
106
+ async current() {
107
+ const held = this.#current;
108
+ if (held !== undefined)
109
+ return held.content;
110
+ await this.#read();
111
+ const loaded = this.#current;
112
+ if (loaded === undefined) {
113
+ throw new ContentUnavailableError("format", "latest.json: no index was read");
114
+ }
115
+ return loaded.content;
116
+ }
117
+ async poll() {
118
+ if (this.#current === undefined) {
119
+ await this.current();
143
120
  return { updated: false };
144
- const revision = await this.#revision(site, latest);
145
- const next = {
146
- revision: latest.revision,
147
- blocks: revision.blocks,
148
- schemaHash: latest.schemaHash,
149
- };
150
- if (revision.previewHosts !== undefined)
151
- next.previewHosts = revision.previewHosts;
152
- if (fallback.aliases !== undefined)
153
- next.aliases = fallback.aliases;
154
- this.#current = next;
155
- return { updated: true };
121
+ }
122
+ return { updated: await this.#read() };
123
+ }
124
+ async revision(id) {
125
+ if (!isRevisionId(id))
126
+ throw errors.loaderFailed(`"${id}" is not a release's revision id`);
127
+ for (const content of [this.#current?.content, this.#previous]) {
128
+ if (content?.revision === id)
129
+ return content;
130
+ }
131
+ const held = this.#revisions.get(id);
132
+ if (held !== undefined)
133
+ return held;
134
+ const label = `revision ${id}`;
135
+ const pending = request(`${deliveryOrigin()}/sites/${this.#sitePath()}/revisions/${id}/index.json`, label).then(async (response) => {
136
+ const body = await readBody(response, label, MAX_INDEX_BYTES);
137
+ const content = this.#parseIndex(body, label);
138
+ if (content.revision !== id) {
139
+ throw new ContentUnavailableError("format", `${label}: names another revision`);
140
+ }
141
+ return content;
142
+ });
143
+ this.#revisions.set(id, pending);
144
+ // Shared with later clients: kept alive past the request that started it.
145
+ keepAlive(pending);
146
+ // A failed read isn't kept, so the next client tries again.
147
+ pending.catch(() => {
148
+ if (this.#revisions.get(id) === pending)
149
+ this.#revisions.delete(id);
150
+ });
151
+ return pending;
156
152
  }
157
153
  /**
158
- * The current release's preview hosts (see `PREVIEW_HOSTS`): the last
159
- * successful read of `latest.json`, reading it first when there is none yet
160
- * or it is older than the interval (`peek`: never). `"every"` when
161
- * the site has no `latest.json` (a 404) or it names no `previewHosts`;
162
- * `"unknown"` while no read has succeeded (a failed one is retried after a
163
- * pause), so previews stay closed rather than open on an error.
154
+ * The current release's preview hosts: the last successful read of
155
+ * `latest.json`, reading it first when there is none yet or it is older than
156
+ * the interval (`peek`: never). `"every"` when the site has no `latest.json`
157
+ * (a 404) or it names no `previewHosts`; `"unknown"` while no read has
158
+ * succeeded (a failed one is retried after a pause).
164
159
  */
165
- async [PREVIEW_HOSTS](peek = false) {
160
+ async previewHosts(peek = false) {
166
161
  const known = this.#hosts;
167
162
  const maxAge = this.interval ?? PREVIEW_HOSTS_MAX_AGE_MS;
168
163
  const stale = known === undefined || Date.now() - known.at >= maxAge;
169
164
  if (!peek && stale && Date.now() - this.#hostsFailedAt >= PREVIEW_HOSTS_RETRY_MS) {
170
- this.#hostsRead ??= this.#latest(encodeURIComponent(this.#site))
165
+ this.#hostsRead ??= this.#read()
171
166
  .then(() => undefined, () => {
172
- // A 404 is an answer (`#latest` recorded it), not a failure.
167
+ // A 404 is an answer (`#readLatest` recorded it), not a failure.
173
168
  if (this.#hosts === known)
174
169
  this.#hostsFailedAt = Date.now();
175
170
  })
@@ -183,156 +178,182 @@ class RemoteLoader {
183
178
  return "unknown";
184
179
  return list === "every" ? list : list.slice();
185
180
  }
186
- /** A published release by its commit SHA (see `READ_REVISION`). */
187
- async [READ_REVISION](sha) {
188
- if (!REVISION.test(sha))
189
- throw errors.loaderFailed(`"${sha}" is not a release's commit SHA`);
190
- if (this.#current?.revision === sha)
191
- return this.#current;
192
- const fallback = this.#fallbackSnapshot ?? (await this.#loadFallback());
193
- if (fallback.revision === sha)
194
- return fallback;
195
- if (fallback.schemaHash === undefined) {
196
- throw errors.loaderFailed(`the release ${sha} can't be checked against this code: the bundled content has no schemaHash (run deco content)`);
197
- }
198
- const snapshot = await this.#readRevision(sha);
199
- if (snapshot.schemaHash !== fallback.schemaHash) {
200
- throw errors.loaderFailed(`the release ${sha} was published with another schema than this code's ` +
201
- `(${snapshot.schemaHash} ≠ ${fallback.schemaHash}): preview it on a deploy of its own code`);
202
- }
203
- if (fallback.aliases === undefined)
204
- return snapshot;
205
- return { ...snapshot, aliases: fallback.aliases };
206
- }
207
- /** Reads a revision once; a failed read isn't kept, so the next client tries again. */
208
- #readRevision(sha) {
209
- const held = this.#revisions.get(sha);
210
- if (held !== undefined)
211
- return held;
212
- const site = encodeURIComponent(this.#site);
213
- const pending = this.#fetchRevision(site, sha, "the revision asked for").then((body) => {
214
- const snapshot = {
215
- revision: sha,
216
- blocks: body.blocks,
217
- schemaHash: body.schemaHash,
181
+ /** One read of `latest.json` at a time (shared); resolves with whether it adopted a new index. */
182
+ #read() {
183
+ if (this.#reading === undefined) {
184
+ const pending = this.#readAndAdopt();
185
+ this.#reading = pending;
186
+ const settle = () => {
187
+ if (this.#reading === pending)
188
+ this.#reading = undefined;
218
189
  };
219
- if (body.previewHosts !== undefined)
220
- snapshot.previewHosts = body.previewHosts;
221
- return snapshot;
222
- });
223
- this.#revisions.set(sha, pending);
224
- pending.catch(() => {
225
- if (this.#revisions.get(sha) === pending)
226
- this.#revisions.delete(sha);
227
- });
228
- return pending;
229
- }
230
- async #fetchRevision(site, sha, expected) {
231
- const label = `revision ${sha}`;
232
- const response = await fetchWithTimeout(`${deliveryOrigin()}/sites/${site}/revisions/${sha}.json`);
233
- if (!response.ok) {
234
- await response.body?.cancel();
235
- throw new Error(`${label}: HTTP ${response.status}`);
236
- }
237
- const body = await readBoundedJson(response, label, MAX_SNAPSHOT_BYTES);
238
- if (!isPlainObject(body) ||
239
- body.revision !== sha ||
240
- typeof body.schemaHash !== "string" ||
241
- !isPlainObject(body.blocks)) {
242
- throw new Error(`${label}: unexpected format, or not ${expected}`);
190
+ pending.then(settle, settle);
191
+ // Shared with later requests: kept alive past the request that started it.
192
+ keepAlive(pending);
243
193
  }
244
- return {
245
- blocks: body.blocks,
246
- schemaHash: body.schemaHash,
247
- previewHosts: previewHostsOf(body.previewHosts),
248
- };
194
+ return this.#reading;
249
195
  }
250
- async #loadFallback() {
251
- const fallback = this.#fallback;
252
- const snapshot = isSnapshot(fallback) ? fallback : await fallback.load();
253
- if (fallback === this.#fallback)
254
- this.#fallbackSnapshot = snapshot;
255
- return snapshot;
196
+ async #readAndAdopt() {
197
+ const held = this.#current;
198
+ const read = await this.#readLatest(held?.etag ?? null);
199
+ if (read === null)
200
+ return false;
201
+ const current = this.#current;
202
+ const at = Date.now();
203
+ // The newest index wins, by sequence: a stale CDN answer never moves the
204
+ // isolate back, its preview hosts included (the held list counts as read now).
205
+ if (current !== undefined && read.content.sequence <= current.content.sequence) {
206
+ this.#hosts =
207
+ this.#hosts === undefined
208
+ ? { list: read.previewHosts ?? "every", at }
209
+ : { list: this.#hosts.list, at };
210
+ return false;
211
+ }
212
+ // The adopted release's metadata. No `previewHosts` (none written): every host, as with no latest.json.
213
+ this.#hosts = { list: read.previewHosts ?? "every", at };
214
+ if (current !== undefined)
215
+ this.#previous = current.content;
216
+ this.#current = { content: read.content, etag: read.etag };
217
+ return current !== undefined;
256
218
  }
257
- async #latest(site) {
258
- const response = await fetchWithTimeout(`${deliveryOrigin()}/sites/${site}/latest.json`);
259
- if (!response.ok) {
260
- await response.body?.cancel();
219
+ /** `latest.json`, revalidating `etag`: `null` for a 304. */
220
+ async #readLatest(etag) {
221
+ const label = "latest.json";
222
+ const response = await request(`${deliveryOrigin()}/sites/${this.#sitePath()}/latest.json`, label, etag, (status) => {
261
223
  // Not published yet: no list, so previews are allowed on every host (PO 2026-10-22).
262
224
  // A site this process has read a list for can't be unpublished: a 404 then
263
225
  // (a CDN fault, a publish caught mid-write) is a failed read, so it keeps the list.
264
- if (response.status === 404 && !Array.isArray(this.#hosts?.list)) {
226
+ if (status === 404 && !Array.isArray(this.#hosts?.list)) {
265
227
  this.#hosts = { list: "every", at: Date.now() };
266
228
  }
267
- throw new Error(`latest.json: HTTP ${response.status}`);
268
- }
269
- const body = await response.json();
270
- if (!isPlainObject(body) ||
271
- typeof body.revision !== "string" ||
272
- !REVISION.test(body.revision) ||
273
- typeof body.schemaHash !== "string" ||
274
- !SCHEMA_HASH.test(body.schemaHash) ||
275
- typeof body.publishedAt !== "string" ||
276
- Number.isNaN(Date.parse(body.publishedAt))) {
277
- throw new Error("latest.json: unexpected format");
229
+ });
230
+ if (response.status === 304) {
231
+ if (this.#hosts !== undefined)
232
+ this.#hosts = { list: this.#hosts.list, at: Date.now() };
233
+ return null;
278
234
  }
235
+ const body = await readBody(response, label, MAX_INDEX_BYTES);
236
+ const content = this.#parseIndex(body, label);
279
237
  const previewHosts = previewHostsOf(body.previewHosts);
280
- // The current release's metadata, whether or not its revision is swapped in.
281
- // No `previewHosts` (published before Studio wrote the list): every host, as with no latest.json.
282
- this.#hosts = { list: previewHosts ?? "every", at: Date.now() };
283
- return {
284
- revision: body.revision,
285
- schemaHash: body.schemaHash,
286
- publishedAt: body.publishedAt,
287
- previewHosts,
288
- };
238
+ // Its preview hosts are taken only with its index (`#readAndAdopt`).
239
+ return { content, etag: response.headers.get("etag"), previewHosts };
289
240
  }
290
- async #revision(site, latest) {
291
- const body = await this.#fetchRevision(site, latest.revision, "the revision latest.json names");
292
- if (body.schemaHash !== latest.schemaHash) {
293
- throw new Error(`revision ${latest.revision}: not the schema latest.json names`);
241
+ /** An index body (`latest.json` or a revision's `index.json`), checked. */
242
+ #parseIndex(body, label) {
243
+ const format = (why) => new ContentUnavailableError("format", `${label}: ${why}`);
244
+ if (!isPlainObject(body) || !isRevisionId(body.revision))
245
+ throw format("unexpected format");
246
+ const sequence = body.sequence;
247
+ if (typeof sequence !== "number" || !Number.isSafeInteger(sequence) || sequence < 1) {
248
+ throw format("sequence must be a positive integer");
294
249
  }
295
- return body;
250
+ if (!isPlainObject(body.blocks))
251
+ throw format("blocks must be an object");
252
+ const blocks = Object.create(null);
253
+ for (const [name, raw] of Object.entries(body.blocks)) {
254
+ if (!isPlainObject(raw) || typeof raw.version !== "string" || !HASH_RE.test(raw.version)) {
255
+ throw format(`the entry "${name}" has no version`);
256
+ }
257
+ const entry = { version: raw.version };
258
+ const meta = metaOf(raw.meta);
259
+ if (meta !== undefined)
260
+ entry.meta = meta;
261
+ if (name === INLINE_BLOCK && raw.data !== undefined)
262
+ entry.data = raw.data;
263
+ blocks[name] = entry;
264
+ }
265
+ return { revision: body.revision, sequence, blocks, source: this.blocks };
266
+ }
267
+ /** `blocks/<hash>.json`. */
268
+ async #fetchBlock(hash, name) {
269
+ const label = `block "${name}" (${hash})`;
270
+ if (!HASH_RE.test(hash))
271
+ throw new ContentUnavailableError("format", `${label}: not a hash`);
272
+ const response = await request(`${deliveryOrigin()}/sites/${this.#sitePath()}/blocks/${hash}.json`, label);
273
+ try {
274
+ const { value, bytes } = await readBoundedJsonSized(response, label, MAX_BLOCK_BYTES);
275
+ return { body: value, bytes };
276
+ }
277
+ catch (error) {
278
+ throw unavailable(label, error, "format");
279
+ }
280
+ }
281
+ #sitePath() {
282
+ return encodeURIComponent(this.#site);
283
+ }
284
+ }
285
+ /** An entry's `meta`, kept to its known keys and shapes; `undefined` when none. */
286
+ function metaOf(raw) {
287
+ if (!isPlainObject(raw))
288
+ return undefined;
289
+ const meta = {};
290
+ if (typeof raw.type === "string")
291
+ meta.type = raw.type;
292
+ if (typeof raw.path === "string")
293
+ meta.path = raw.path;
294
+ if (Array.isArray(raw.refs)) {
295
+ const refs = raw.refs.filter((ref) => typeof ref === "string");
296
+ if (refs.length > 0)
297
+ meta.refs = refs;
296
298
  }
299
+ return Object.keys(meta).length > 0 ? meta : undefined;
297
300
  }
298
- /** A loader over the fallback alone: `remoteLoader` without `site`. */
301
+ /** A loader over the content it's given alone: `remoteLoader` without `site`. */
299
302
  class LocalLoader {
300
- #fallback;
301
- constructor(fallback) {
302
- this.#fallback = fallback;
303
+ #content;
304
+ constructor(content) {
305
+ this.#content = content;
303
306
  }
304
- adopt(fallback) {
305
- this.#fallback = fallback;
307
+ adopt(content) {
308
+ this.#content = content;
306
309
  }
307
310
  [PEEK_RELEASE]() {
308
- return peekRelease(this.#fallback);
311
+ return peekRelease(this.#content);
309
312
  }
310
313
  load() {
311
- const fallback = this.#fallback;
312
- return isSnapshot(fallback) ? Promise.resolve(fallback) : fallback.load();
314
+ const content = this.#content;
315
+ return typeof content.load === "function"
316
+ ? content.load()
317
+ : Promise.resolve(content);
313
318
  }
314
319
  }
315
320
  /**
316
- * Hosted releases over a fallback. `createCMS` builds it for you when `site`
317
- * is set. Without `site`, it's a loader over the fallback alone.
321
+ * Hosted releases: `createCMS` builds it for you when `site` is set. With
322
+ * `site`, content comes only from the CDN, so `content` must be `undefined`
323
+ * (nothing is bundled). Without `site`, it's a loader over
324
+ * `content` alone.
318
325
  */
319
- export function remoteLoader(fallback, options) {
326
+ export function remoteLoader(content, options) {
320
327
  const { site } = options ?? {};
321
328
  const hosted = Boolean(site);
322
- const key = Symbol.for(`${INSTANCE_PREFIX}${contentIdentity(fallback)}${hosted ? `|site:${site}` : "|local"}`);
329
+ if (hosted && content !== undefined) {
330
+ throw new TypeError("remoteLoader: with site, content comes from the delivery CDN at runtime; pass undefined as content");
331
+ }
332
+ if (!hosted && content === undefined) {
333
+ throw new TypeError("remoteLoader: without site, pass the content to serve");
334
+ }
335
+ const key = Symbol.for(hosted
336
+ ? `${INSTANCE_PREFIX}site:${site}`
337
+ : `${INSTANCE_PREFIX}${contentIdentity(content)}|local`);
323
338
  const store = globalThis;
324
339
  const existing = store[key];
325
- if (existing instanceof Object && typeof existing.adopt === "function") {
326
- if (existing instanceof RemoteLoader && existing.interval !== options.interval) {
327
- console.warn("[decocms/blocks] remoteLoader was called again for the same site with different options " +
328
- "(interval); keeping the first instance's options.");
340
+ // Duck-typed, so an instance a second copy of this module made is reused too.
341
+ if (existing instanceof Object && typeof existing.load === "function") {
342
+ if (hosted && existing[HOSTED] === true) {
343
+ if (existing.interval !== options.interval) {
344
+ console.warn("[decocms/blocks] remoteLoader was called again for the same site with different options " +
345
+ "(interval); keeping the first instance's options.");
346
+ }
347
+ return existing;
348
+ }
349
+ if (!hosted && typeof existing.adopt === "function") {
350
+ existing.adopt(content);
351
+ return existing;
329
352
  }
330
- existing.adopt(fallback);
331
- return existing;
332
353
  }
333
354
  const instance = hosted
334
- ? new RemoteLoader(fallback, { site: site, interval: options.interval, dev: options.dev })
335
- : new LocalLoader(fallback);
355
+ ? new RemoteLoader({ site: site, interval: options.interval })
356
+ : new LocalLoader(content);
336
357
  store[key] = instance;
337
358
  return instance;
338
359
  }
@@ -340,18 +361,55 @@ export function resetRemoteLoaders() {
340
361
  clearGlobals(INSTANCE_PREFIX);
341
362
  }
342
363
  /**
343
- * Whether a release published at `publishedAt` is newer than content built
344
- * from a commit made at `committedAt`. Content without a `committedAt` is the
345
- * oldest.
346
- * OPEN: a `committedAt` that doesn't parse as a date counts as missing.
364
+ * GETs `url` (no credentials, 10 s for headers; `If-None-Match: etag` when
365
+ * given). Resolves with a 2xx, or with a 304 to a request that sent an ETag.
366
+ * Every other outcome is a `ContentUnavailableError`: the timeout, the
367
+ * network, any other HTTP status (`onStatus` sees it first).
347
368
  */
348
- function isNewer(publishedAt, committedAt) {
349
- const committed = committedAt === undefined ? Number.NaN : Date.parse(committedAt);
350
- return Number.isNaN(committed) || Date.parse(publishedAt) > committed;
369
+ async function request(url, label, etag, onStatus) {
370
+ let response;
371
+ const headers = {};
372
+ if (etag)
373
+ headers["if-none-match"] = etag;
374
+ try {
375
+ response = await fetch(url, { headers, signal: timeoutSignal(FETCH_TIMEOUT_MS) });
376
+ }
377
+ catch (error) {
378
+ throw unavailable(label, error, "network");
379
+ }
380
+ if (response.status === 304 && etag) {
381
+ void response.body?.cancel().catch(() => { });
382
+ return response;
383
+ }
384
+ if (!response.ok) {
385
+ void response.body?.cancel().catch(() => { });
386
+ onStatus?.(response.status);
387
+ throw new ContentUnavailableError("http", `${label}: HTTP ${response.status}`, {
388
+ status: response.status,
389
+ });
390
+ }
391
+ return response;
351
392
  }
352
- /** No credentials: delivery is public. */
353
- function fetchWithTimeout(url) {
354
- return fetch(url, { signal: timeoutSignal(FETCH_TIMEOUT_MS) });
393
+ async function readBody(response, label, maxBytes) {
394
+ try {
395
+ return await readBoundedJson(response, label, maxBytes);
396
+ }
397
+ catch (error) {
398
+ throw unavailable(label, error, "format");
399
+ }
400
+ }
401
+ /** A failed fetch or body read as a `ContentUnavailableError`; a timeout is always `"timeout"`. */
402
+ function unavailable(label, error, otherwise) {
403
+ if (error instanceof ContentUnavailableError)
404
+ return error;
405
+ const name = error instanceof Error ? error.name : undefined;
406
+ const message = error instanceof Error ? error.message : String(error);
407
+ if (name === "TimeoutError" || name === "AbortError") {
408
+ return new ContentUnavailableError("timeout", `${label}: no answer within ${FETCH_TIMEOUT_MS} ms`, { cause: error });
409
+ }
410
+ // A connection that drops mid-body is the network's, not the body's.
411
+ const reason = otherwise === "format" && error instanceof TypeError ? "network" : otherwise;
412
+ return new ContentUnavailableError(reason, `${label}: ${message}`, { cause: error });
355
413
  }
356
414
  function deliveryOrigin() {
357
415
  const override = globalThis[TEST_DELIVERY_ORIGIN];