@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.
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/protocol/channel.d.ts +98 -0
- package/dist/protocol/channel.d.ts.map +1 -0
- package/dist/protocol/channel.js +58 -0
- package/dist/protocol/channel.js.map +1 -0
- package/dist/protocol/client.js +2 -2
- package/dist/protocol/client.js.map +1 -1
- package/dist/protocol/conformance/blockMetaVectors.json +214 -0
- package/dist/protocol/conformance/contentHashFixtures.d.ts +22 -0
- package/dist/protocol/conformance/contentHashFixtures.d.ts.map +1 -1
- package/dist/protocol/conformance/contentHashFixtures.js +55 -0
- package/dist/protocol/conformance/contentHashFixtures.js.map +1 -1
- package/dist/protocol/conformance/index.d.ts +2 -1
- package/dist/protocol/conformance/index.d.ts.map +1 -1
- package/dist/protocol/conformance/index.js +2 -1
- package/dist/protocol/conformance/index.js.map +1 -1
- package/dist/protocol/conformance/vectors.d.ts +33 -0
- package/dist/protocol/conformance/vectors.d.ts.map +1 -0
- package/dist/protocol/conformance/vectors.js +3 -0
- package/dist/protocol/conformance/vectors.js.map +1 -0
- package/dist/protocol/index.d.ts +11 -1
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +11 -1
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/keys.d.ts +1 -1
- package/dist/protocol/keys.js +1 -1
- package/dist/v8/background.d.ts +13 -0
- package/dist/v8/background.d.ts.map +1 -1
- package/dist/v8/background.js +41 -0
- package/dist/v8/background.js.map +1 -1
- package/dist/v8/blockMeta.d.ts +27 -0
- package/dist/v8/blockMeta.d.ts.map +1 -0
- package/dist/v8/blockMeta.js +75 -0
- package/dist/v8/blockMeta.js.map +1 -0
- package/dist/v8/blockStore.d.ts +43 -0
- package/dist/v8/blockStore.d.ts.map +1 -0
- package/dist/v8/blockStore.js +95 -0
- package/dist/v8/blockStore.js.map +1 -0
- package/dist/v8/boundedJson.d.ts +5 -0
- package/dist/v8/boundedJson.d.ts.map +1 -1
- package/dist/v8/boundedJson.js +10 -2
- package/dist/v8/boundedJson.js.map +1 -1
- package/dist/v8/boundedMap.d.ts +19 -0
- package/dist/v8/boundedMap.d.ts.map +1 -0
- package/dist/v8/boundedMap.js +52 -0
- package/dist/v8/boundedMap.js.map +1 -0
- package/dist/v8/builtins/data.d.ts +18 -2
- package/dist/v8/builtins/data.d.ts.map +1 -1
- package/dist/v8/builtins/data.js +17 -1
- package/dist/v8/builtins/data.js.map +1 -1
- package/dist/v8/builtins/index.d.ts.map +1 -1
- package/dist/v8/builtins/index.js +2 -1
- package/dist/v8/builtins/index.js.map +1 -1
- package/dist/v8/builtins/legacy.d.ts +6 -3
- package/dist/v8/builtins/legacy.d.ts.map +1 -1
- package/dist/v8/builtins/legacy.js +6 -3
- package/dist/v8/builtins/legacy.js.map +1 -1
- package/dist/v8/canonical.d.ts +8 -0
- package/dist/v8/canonical.d.ts.map +1 -1
- package/dist/v8/canonical.js +15 -0
- package/dist/v8/canonical.js.map +1 -1
- package/dist/v8/cli/builtins.d.ts.map +1 -1
- package/dist/v8/cli/builtins.js +1 -0
- package/dist/v8/cli/builtins.js.map +1 -1
- package/dist/v8/cli/check/index.d.ts +10 -0
- package/dist/v8/cli/check/index.d.ts.map +1 -1
- package/dist/v8/cli/check/index.js +91 -7
- package/dist/v8/cli/check/index.js.map +1 -1
- package/dist/v8/cli/content.d.ts +12 -25
- package/dist/v8/cli/content.d.ts.map +1 -1
- package/dist/v8/cli/content.js +46 -75
- package/dist/v8/cli/content.js.map +1 -1
- package/dist/v8/cli/index.d.ts +1 -2
- package/dist/v8/cli/index.d.ts.map +1 -1
- package/dist/v8/cli/index.js +1 -2
- package/dist/v8/cli/index.js.map +1 -1
- package/dist/v8/cli/remote/commands.d.ts +71 -0
- package/dist/v8/cli/remote/commands.d.ts.map +1 -0
- package/dist/v8/cli/remote/commands.js +644 -0
- package/dist/v8/cli/remote/commands.js.map +1 -0
- package/dist/v8/cli/remote/diff.d.ts +15 -0
- package/dist/v8/cli/remote/diff.d.ts.map +1 -0
- package/dist/v8/cli/remote/diff.js +102 -0
- package/dist/v8/cli/remote/diff.js.map +1 -0
- package/dist/v8/cli/remote/env.d.ts +35 -0
- package/dist/v8/cli/remote/env.d.ts.map +1 -0
- package/dist/v8/cli/remote/env.js +84 -0
- package/dist/v8/cli/remote/env.js.map +1 -0
- package/dist/v8/cli/remote/http.d.ts +149 -0
- package/dist/v8/cli/remote/http.d.ts.map +1 -0
- package/dist/v8/cli/remote/http.js +381 -0
- package/dist/v8/cli/remote/http.js.map +1 -0
- package/dist/v8/cli/remote/local.d.ts +90 -0
- package/dist/v8/cli/remote/local.d.ts.map +1 -0
- package/dist/v8/cli/remote/local.js +309 -0
- package/dist/v8/cli/remote/local.js.map +1 -0
- package/dist/v8/cli/root.d.ts +29 -0
- package/dist/v8/cli/root.d.ts.map +1 -1
- package/dist/v8/cli/root.js +28 -0
- package/dist/v8/cli/root.js.map +1 -1
- package/dist/v8/cli/run.d.ts +11 -1
- package/dist/v8/cli/run.d.ts.map +1 -1
- package/dist/v8/cli/run.js +281 -124
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.js +28 -10
- package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
- package/dist/v8/cli/schema/generate.js +1 -1
- package/dist/v8/cli/schema/generate.js.map +1 -1
- package/dist/v8/client.d.ts +9 -9
- package/dist/v8/client.d.ts.map +1 -1
- package/dist/v8/client.js +138 -50
- package/dist/v8/client.js.map +1 -1
- package/dist/v8/cms.d.ts +2 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +55 -46
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +39 -78
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +155 -195
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/contentIndex.d.ts +71 -0
- package/dist/v8/contentIndex.d.ts.map +1 -0
- package/dist/v8/contentIndex.js +137 -0
- package/dist/v8/contentIndex.js.map +1 -0
- package/dist/v8/draft.d.ts +7 -7
- package/dist/v8/draft.d.ts.map +1 -1
- package/dist/v8/draft.js +10 -9
- package/dist/v8/draft.js.map +1 -1
- package/dist/v8/draftChanges.d.ts +59 -32
- package/dist/v8/draftChanges.d.ts.map +1 -1
- package/dist/v8/draftChanges.js +139 -75
- package/dist/v8/draftChanges.js.map +1 -1
- package/dist/v8/errors.d.ts +21 -0
- package/dist/v8/errors.d.ts.map +1 -1
- package/dist/v8/errors.js +22 -0
- package/dist/v8/errors.js.map +1 -1
- package/dist/v8/hostedSource.d.ts +57 -0
- package/dist/v8/hostedSource.d.ts.map +1 -0
- package/dist/v8/hostedSource.js +18 -0
- package/dist/v8/hostedSource.js.map +1 -0
- package/dist/v8/identity.d.ts +1 -1
- package/dist/v8/identity.js +1 -1
- package/dist/v8/index.d.ts +1 -0
- package/dist/v8/index.d.ts.map +1 -1
- package/dist/v8/index.js +1 -0
- package/dist/v8/index.js.map +1 -1
- package/dist/v8/matchRoute.js +7 -4
- package/dist/v8/matchRoute.js.map +1 -1
- package/dist/v8/pin.d.ts +34 -0
- package/dist/v8/pin.d.ts.map +1 -0
- package/dist/v8/pin.js +156 -0
- package/dist/v8/pin.js.map +1 -0
- package/dist/v8/preview/StudioBridge.d.ts +8 -8
- package/dist/v8/preview/StudioBridge.d.ts.map +1 -1
- package/dist/v8/preview/StudioBridge.js +8 -10
- package/dist/v8/preview/StudioBridge.js.map +1 -1
- package/dist/v8/preview/StudioChannel.d.ts +51 -0
- package/dist/v8/preview/StudioChannel.d.ts.map +1 -0
- package/dist/v8/preview/StudioChannel.js +104 -0
- package/dist/v8/preview/StudioChannel.js.map +1 -0
- package/dist/v8/preview/index.d.ts +3 -0
- package/dist/v8/preview/index.d.ts.map +1 -1
- package/dist/v8/preview/index.js +3 -0
- package/dist/v8/preview/index.js.map +1 -1
- package/dist/v8/remoteLoader.d.ts +42 -5
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +317 -259
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/resolver.d.ts +22 -8
- package/dist/v8/resolver.d.ts.map +1 -1
- package/dist/v8/resolver.js +37 -32
- package/dist/v8/resolver.js.map +1 -1
- package/dist/v8/revisionId.d.ts +11 -0
- package/dist/v8/revisionId.d.ts.map +1 -0
- package/dist/v8/revisionId.js +13 -0
- package/dist/v8/revisionId.js.map +1 -0
- package/dist/v8/settings.d.ts +6 -6
- package/dist/v8/settings.d.ts.map +1 -1
- package/dist/v8/settings.js +8 -7
- package/dist/v8/settings.js.map +1 -1
- package/dist/v8/telemetry.d.ts +4 -2
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +2 -2
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +52 -40
- package/dist/v8/types.d.ts.map +1 -1
- package/dist/v8/variants.d.ts +7 -2
- package/dist/v8/variants.d.ts.map +1 -1
- package/dist/v8/variants.js +21 -24
- package/dist/v8/variants.js.map +1 -1
- package/dist/v8/vite/index.d.ts +2 -2
- package/dist/v8/vite/index.js +1 -1
- package/dist/v8/vite/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/protocol/channel.ts +127 -0
- package/src/protocol/client.ts +2 -2
- package/src/protocol/conformance/blockMetaVectors.json +214 -0
- package/src/protocol/conformance/contentHashFixtures.ts +74 -0
- package/src/protocol/conformance/index.ts +9 -1
- package/src/protocol/conformance/vectors.ts +36 -0
- package/src/protocol/index.ts +32 -0
- package/src/protocol/keys.ts +1 -1
- package/src/v8/background.ts +42 -0
- package/src/v8/blockMeta.ts +100 -0
- package/src/v8/blockStore.ts +124 -0
- package/src/v8/boundedJson.ts +20 -2
- package/src/v8/boundedMap.ts +53 -0
- package/src/v8/builtins/data.ts +26 -2
- package/src/v8/builtins/index.ts +2 -1
- package/src/v8/builtins/legacy.ts +6 -3
- package/src/v8/canonical.ts +15 -0
- package/src/v8/cli/builtins.ts +1 -0
- package/src/v8/cli/check/index.ts +97 -7
- package/src/v8/cli/content.ts +49 -91
- package/src/v8/cli/index.ts +1 -2
- package/src/v8/cli/remote/commands.ts +834 -0
- package/src/v8/cli/remote/diff.ts +100 -0
- package/src/v8/cli/remote/env.ts +107 -0
- package/src/v8/cli/remote/http.ts +484 -0
- package/src/v8/cli/remote/local.ts +343 -0
- package/src/v8/cli/root.ts +40 -0
- package/src/v8/cli/run.ts +333 -136
- package/src/v8/cli/schema/builtinSchemas.ts +29 -10
- package/src/v8/cli/schema/generate.ts +1 -1
- package/src/v8/client.ts +162 -60
- package/src/v8/cms.ts +62 -53
- package/src/v8/content.ts +179 -229
- package/src/v8/contentIndex.ts +183 -0
- package/src/v8/draft.ts +15 -14
- package/src/v8/draftChanges.ts +151 -85
- package/src/v8/errors.ts +37 -0
- package/src/v8/hostedSource.ts +67 -0
- package/src/v8/identity.ts +1 -1
- package/src/v8/index.ts +1 -0
- package/src/v8/matchRoute.ts +7 -4
- package/src/v8/pin.ts +176 -0
- package/src/v8/preview/StudioBridge.ts +8 -10
- package/src/v8/preview/StudioChannel.ts +116 -0
- package/src/v8/preview/index.ts +3 -0
- package/src/v8/remoteLoader.ts +350 -286
- package/src/v8/resolver.ts +56 -33
- package/src/v8/revisionId.ts +14 -0
- package/src/v8/settings.ts +10 -9
- package/src/v8/telemetry.ts +3 -3
- package/src/v8/types.ts +52 -40
- package/src/v8/variants.ts +30 -26
- package/src/v8/vite/index.ts +3 -3
- package/dist/v8/cli/serve/server.d.ts +0 -29
- package/dist/v8/cli/serve/server.d.ts.map +0 -1
- package/dist/v8/cli/serve/server.js +0 -296
- package/dist/v8/cli/serve/server.js.map +0 -1
- package/src/v8/cli/serve/server.ts +0 -348
package/dist/v8/remoteLoader.js
CHANGED
|
@@ -1,175 +1,170 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `remoteLoader`: hosted releases from `delivery.decocms.com`,
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
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
|
-
* - `
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* `
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* `
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
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 {
|
|
62
|
-
import {
|
|
63
|
-
import {
|
|
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
|
-
|
|
47
|
+
import { isRevisionId } from "./revisionId.js";
|
|
48
|
+
/** The hosted delivery origin. */
|
|
67
49
|
const HOSTED_DELIVERY_ORIGIN = "https://delivery.decocms.com";
|
|
68
|
-
/**
|
|
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
|
|
73
|
-
const
|
|
74
|
-
/**
|
|
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
|
-
|
|
88
|
-
/** The
|
|
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
|
-
#
|
|
93
|
-
/**
|
|
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
|
|
97
|
-
* `"every"`: a 404, or no
|
|
98
|
-
*
|
|
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(
|
|
106
|
-
this.#fallback = fallback;
|
|
91
|
+
constructor(options) {
|
|
107
92
|
this.#site = options.site;
|
|
108
93
|
this.interval = options.interval;
|
|
109
|
-
this
|
|
94
|
+
this.blocks = new BlockStore((hash, name) => this.#fetchBlock(hash, name));
|
|
110
95
|
}
|
|
111
|
-
/**
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
return this.#current ?? peekRelease(this.#fallback) ?? this.#fallbackSnapshot;
|
|
100
|
+
peek() {
|
|
101
|
+
return this.#current?.content;
|
|
121
102
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
return Promise.resolve(this.#current);
|
|
125
|
-
return this.#loadFallback();
|
|
103
|
+
previous() {
|
|
104
|
+
return this.#previous;
|
|
126
105
|
}
|
|
127
|
-
async
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
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
|
|
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.#
|
|
165
|
+
this.#hostsRead ??= this.#read()
|
|
171
166
|
.then(() => undefined, () => {
|
|
172
|
-
// A 404 is an answer (`#
|
|
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
|
-
/**
|
|
187
|
-
|
|
188
|
-
if (
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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 #
|
|
251
|
-
const
|
|
252
|
-
const
|
|
253
|
-
if (
|
|
254
|
-
|
|
255
|
-
|
|
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
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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 (
|
|
226
|
+
if (status === 404 && !Array.isArray(this.#hosts?.list)) {
|
|
265
227
|
this.#hosts = { list: "every", at: Date.now() };
|
|
266
228
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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
|
-
//
|
|
281
|
-
|
|
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
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
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
|
-
|
|
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
|
|
301
|
+
/** A loader over the content it's given alone: `remoteLoader` without `site`. */
|
|
299
302
|
class LocalLoader {
|
|
300
|
-
#
|
|
301
|
-
constructor(
|
|
302
|
-
this.#
|
|
303
|
+
#content;
|
|
304
|
+
constructor(content) {
|
|
305
|
+
this.#content = content;
|
|
303
306
|
}
|
|
304
|
-
adopt(
|
|
305
|
-
this.#
|
|
307
|
+
adopt(content) {
|
|
308
|
+
this.#content = content;
|
|
306
309
|
}
|
|
307
310
|
[PEEK_RELEASE]() {
|
|
308
|
-
return peekRelease(this.#
|
|
311
|
+
return peekRelease(this.#content);
|
|
309
312
|
}
|
|
310
313
|
load() {
|
|
311
|
-
const
|
|
312
|
-
return
|
|
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
|
|
317
|
-
*
|
|
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(
|
|
326
|
+
export function remoteLoader(content, options) {
|
|
320
327
|
const { site } = options ?? {};
|
|
321
328
|
const hosted = Boolean(site);
|
|
322
|
-
|
|
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
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
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(
|
|
335
|
-
: new LocalLoader(
|
|
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
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
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
|
|
349
|
-
|
|
350
|
-
|
|
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
|
-
|
|
353
|
-
|
|
354
|
-
|
|
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];
|