@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/src/v8/remoteLoader.ts
CHANGED
|
@@ -1,214 +1,207 @@
|
|
|
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
|
-
|
|
38
|
+
|
|
39
|
+
import { keepAlive } from "./background.ts";
|
|
40
|
+
import type { BlockIndex, BlockMeta, IndexEntry } from "./blockMeta.ts";
|
|
41
|
+
import { BlockStore, type FetchedBody } from "./blockStore.ts";
|
|
42
|
+
import { readBoundedJson, readBoundedJsonSized, timeoutSignal } from "./boundedJson.ts";
|
|
43
|
+
import { BoundedMap } from "./boundedMap.ts";
|
|
44
|
+
import { PEEK_RELEASE, peekRelease } from "./content.ts";
|
|
45
|
+
import type { IndexedContent } from "./contentIndex.ts";
|
|
46
|
+
import { ContentUnavailableError, errors } from "./errors.ts";
|
|
62
47
|
import {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
PEEK_RELEASE,
|
|
66
|
-
PREVIEW_HOSTS,
|
|
67
|
-
peekRelease,
|
|
48
|
+
HOSTED,
|
|
49
|
+
type HostedSource,
|
|
68
50
|
previewHostsOf,
|
|
69
|
-
READ_REVISION,
|
|
70
51
|
type ReleasePreviewHosts,
|
|
71
|
-
} from "./
|
|
72
|
-
import { errors } from "./errors.ts";
|
|
52
|
+
} from "./hostedSource.ts";
|
|
73
53
|
import { clearGlobals, contentIdentity } from "./identity.ts";
|
|
74
54
|
import { isPlainObject } from "./json.ts";
|
|
55
|
+
import { isRevisionId } from "./revisionId.ts";
|
|
75
56
|
import type { Loader, Snapshot } from "./types.ts";
|
|
76
57
|
|
|
77
|
-
/** The hosted delivery origin
|
|
58
|
+
/** The hosted delivery origin. */
|
|
78
59
|
const HOSTED_DELIVERY_ORIGIN = "https://delivery.decocms.com";
|
|
79
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* test-only: replaces the delivery origin (it may carry a path prefix) in
|
|
62
|
+
* local end-to-end runs. Not documented, not exported.
|
|
63
|
+
*/
|
|
80
64
|
const TEST_DELIVERY_ORIGIN = Symbol.for("decocms.blocks.test.deliveryOrigin");
|
|
81
65
|
|
|
82
66
|
const INSTANCE_PREFIX = "decocms.blocks.remote:";
|
|
83
67
|
const FETCH_TIMEOUT_MS = 10_000;
|
|
84
|
-
/** The largest
|
|
85
|
-
const
|
|
86
|
-
/**
|
|
68
|
+
/** The largest index (`latest.json`, a revision's `index.json`) accepted, in bytes of JSON. */
|
|
69
|
+
const MAX_INDEX_BYTES = 16 * 1024 * 1024;
|
|
70
|
+
/** The largest block body accepted, in bytes of JSON. */
|
|
71
|
+
const MAX_BLOCK_BYTES = 16 * 1024 * 1024;
|
|
72
|
+
/** Revision indexes kept in memory once read (a client asked for one by its id). */
|
|
87
73
|
const CACHED_REVISIONS = 3;
|
|
88
|
-
// OPEN: a revision is the git commit SHA; only SHA-1 (40 hex) object names are accepted.
|
|
89
|
-
const REVISION = /^[0-9a-f]{40}$/;
|
|
90
|
-
const SCHEMA_HASH = /^[0-9a-f]{64}$/;
|
|
91
74
|
/** How long a failed read of the preview hosts waits before a preview request tries again. */
|
|
92
75
|
const PREVIEW_HOSTS_RETRY_MS = 10_000;
|
|
93
76
|
/** How old the preview hosts may get before a preview request reads them again (without `interval`). */
|
|
94
77
|
const PREVIEW_HOSTS_MAX_AGE_MS = 60_000;
|
|
78
|
+
/** A block hash: `blockVersion`, base64url of 16 bytes. */
|
|
79
|
+
const HASH_RE = /^[A-Za-z0-9_-]{22}$/;
|
|
80
|
+
/** The one entry whose body an index carries inline. */
|
|
81
|
+
const INLINE_BLOCK = "CMS";
|
|
95
82
|
|
|
96
83
|
interface RemoteLoaderOptions {
|
|
97
84
|
site?: string;
|
|
98
85
|
/** ms between release checks, used when `createCMS` has no `interval` of its own. */
|
|
99
86
|
interval?: number;
|
|
100
|
-
/** Local development: never swap in a release, so local files win. */
|
|
101
|
-
dev?: boolean;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** `sites/<site>/latest.json`: the release a site serves. */
|
|
105
|
-
interface Latest {
|
|
106
|
-
revision: string;
|
|
107
|
-
schemaHash: string;
|
|
108
|
-
publishedAt: string;
|
|
109
|
-
/** Where the site may be previewed; `undefined` when Studio wrote none. */
|
|
110
|
-
previewHosts: string[] | undefined;
|
|
111
87
|
}
|
|
112
88
|
|
|
113
|
-
/** A
|
|
114
|
-
interface
|
|
115
|
-
|
|
116
|
-
|
|
89
|
+
/** A parsed `latest.json` and its ETag. */
|
|
90
|
+
interface LatestRead {
|
|
91
|
+
content: IndexedContent;
|
|
92
|
+
etag: string | null;
|
|
117
93
|
previewHosts: string[] | undefined;
|
|
118
94
|
}
|
|
119
95
|
|
|
120
|
-
class RemoteLoader implements Loader {
|
|
96
|
+
class RemoteLoader implements Loader, HostedSource {
|
|
97
|
+
readonly [HOSTED] = true as const;
|
|
121
98
|
readonly #site: string;
|
|
122
|
-
/** The `interval` this loader was created with; `createCMS` reads it. */
|
|
123
99
|
readonly interval: number | undefined;
|
|
124
|
-
|
|
125
|
-
/** The
|
|
126
|
-
#
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
readonly #revisions = new BoundedMap<string, Promise<Snapshot>>(CACHED_REVISIONS);
|
|
100
|
+
readonly blocks: BlockStore;
|
|
101
|
+
/** The index this process holds; `undefined` until the first load succeeds. */
|
|
102
|
+
#current: { content: IndexedContent; etag: string | null } | undefined;
|
|
103
|
+
#previous: IndexedContent | undefined;
|
|
104
|
+
/** A read of `latest.json` in flight: the first load, a poll or a preview-hosts read. */
|
|
105
|
+
#reading: Promise<boolean> | undefined;
|
|
106
|
+
readonly #revisions = new BoundedMap<string, Promise<IndexedContent>>(CACHED_REVISIONS);
|
|
132
107
|
/**
|
|
133
|
-
* The preview hosts the
|
|
134
|
-
* `"every"`: a 404, or no
|
|
135
|
-
*
|
|
108
|
+
* The preview hosts of the held release, from the last read of
|
|
109
|
+
* `latest.json` that was adopted (its list, or `"every"`: a 404, or no
|
|
110
|
+
* `previewHosts` in it), and when it was last confirmed. A failed read, or
|
|
111
|
+
* a stale one, leaves the list as it was.
|
|
136
112
|
*/
|
|
137
113
|
#hosts: { list: readonly string[] | "every"; at: number } | undefined;
|
|
138
|
-
/** A read of `latest.json` for its preview hosts, in flight. */
|
|
139
114
|
#hostsRead: Promise<void> | undefined;
|
|
140
|
-
/** When that read last failed. */
|
|
141
115
|
#hostsFailedAt = Number.NEGATIVE_INFINITY;
|
|
142
116
|
|
|
143
|
-
constructor(
|
|
144
|
-
fallback: Snapshot | Loader,
|
|
145
|
-
options: { site: string; interval?: number; dev?: boolean },
|
|
146
|
-
) {
|
|
147
|
-
this.#fallback = fallback;
|
|
117
|
+
constructor(options: { site: string; interval?: number }) {
|
|
148
118
|
this.#site = options.site;
|
|
149
119
|
this.interval = options.interval;
|
|
150
|
-
this
|
|
120
|
+
this.blocks = new BlockStore((hash, name) => this.#fetchBlock(hash, name));
|
|
151
121
|
}
|
|
152
122
|
|
|
153
|
-
/**
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
123
|
+
/** Hosted content is read lazily, by index and hash; there is no whole snapshot to load. */
|
|
124
|
+
load(): Promise<Snapshot> {
|
|
125
|
+
return Promise.reject(
|
|
126
|
+
errors.loaderFailed(
|
|
127
|
+
"hosted releases are read through createCMS (an index plus blocks by hash), not as one snapshot",
|
|
128
|
+
),
|
|
129
|
+
);
|
|
158
130
|
}
|
|
159
131
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return this.#current ?? peekRelease(this.#fallback) ?? this.#fallbackSnapshot;
|
|
132
|
+
peek(): IndexedContent | undefined {
|
|
133
|
+
return this.#current?.content;
|
|
163
134
|
}
|
|
164
135
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return this.#loadFallback();
|
|
136
|
+
previous(): IndexedContent | undefined {
|
|
137
|
+
return this.#previous;
|
|
168
138
|
}
|
|
169
139
|
|
|
170
|
-
async
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
140
|
+
async current(): Promise<IndexedContent> {
|
|
141
|
+
const held = this.#current;
|
|
142
|
+
if (held !== undefined) return held.content;
|
|
143
|
+
await this.#read();
|
|
144
|
+
const loaded = this.#current as { content: IndexedContent } | undefined;
|
|
145
|
+
if (loaded === undefined) {
|
|
146
|
+
throw new ContentUnavailableError("format", "latest.json: no index was read");
|
|
147
|
+
}
|
|
148
|
+
return loaded.content;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async poll(): Promise<{ updated: boolean }> {
|
|
152
|
+
if (this.#current === undefined) {
|
|
153
|
+
await this.current();
|
|
154
|
+
return { updated: false };
|
|
155
|
+
}
|
|
156
|
+
return { updated: await this.#read() };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async revision(id: string): Promise<IndexedContent> {
|
|
160
|
+
if (!isRevisionId(id)) throw errors.loaderFailed(`"${id}" is not a release's revision id`);
|
|
161
|
+
for (const content of [this.#current?.content, this.#previous]) {
|
|
162
|
+
if (content?.revision === id) return content;
|
|
163
|
+
}
|
|
164
|
+
const held = this.#revisions.get(id);
|
|
165
|
+
if (held !== undefined) return held;
|
|
166
|
+
const label = `revision ${id}`;
|
|
167
|
+
const pending = request(
|
|
168
|
+
`${deliveryOrigin()}/sites/${this.#sitePath()}/revisions/${id}/index.json`,
|
|
169
|
+
label,
|
|
170
|
+
).then(async (response) => {
|
|
171
|
+
const body = await readBody(response, label, MAX_INDEX_BYTES);
|
|
172
|
+
const content = this.#parseIndex(body, label);
|
|
173
|
+
if (content.revision !== id) {
|
|
174
|
+
throw new ContentUnavailableError("format", `${label}: names another revision`);
|
|
175
|
+
}
|
|
176
|
+
return content;
|
|
177
|
+
});
|
|
178
|
+
this.#revisions.set(id, pending);
|
|
179
|
+
// Shared with later clients: kept alive past the request that started it.
|
|
180
|
+
keepAlive(pending);
|
|
181
|
+
// A failed read isn't kept, so the next client tries again.
|
|
182
|
+
pending.catch(() => {
|
|
183
|
+
if (this.#revisions.get(id) === pending) this.#revisions.delete(id);
|
|
184
|
+
});
|
|
185
|
+
return pending;
|
|
192
186
|
}
|
|
193
187
|
|
|
194
188
|
/**
|
|
195
|
-
* The current release's preview hosts
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* pause), so previews stay closed rather than open on an error.
|
|
189
|
+
* The current release's preview hosts: the last successful read of
|
|
190
|
+
* `latest.json`, reading it first when there is none yet or it is older than
|
|
191
|
+
* the interval (`peek`: never). `"every"` when the site has no `latest.json`
|
|
192
|
+
* (a 404) or it names no `previewHosts`; `"unknown"` while no read has
|
|
193
|
+
* succeeded (a failed one is retried after a pause).
|
|
201
194
|
*/
|
|
202
|
-
async
|
|
195
|
+
async previewHosts(peek = false): Promise<ReleasePreviewHosts> {
|
|
203
196
|
const known = this.#hosts;
|
|
204
197
|
const maxAge = this.interval ?? PREVIEW_HOSTS_MAX_AGE_MS;
|
|
205
198
|
const stale = known === undefined || Date.now() - known.at >= maxAge;
|
|
206
199
|
if (!peek && stale && Date.now() - this.#hostsFailedAt >= PREVIEW_HOSTS_RETRY_MS) {
|
|
207
|
-
this.#hostsRead ??= this.#
|
|
200
|
+
this.#hostsRead ??= this.#read()
|
|
208
201
|
.then(
|
|
209
202
|
() => undefined,
|
|
210
203
|
() => {
|
|
211
|
-
// A 404 is an answer (`#
|
|
204
|
+
// A 404 is an answer (`#readLatest` recorded it), not a failure.
|
|
212
205
|
if (this.#hosts === known) this.#hostsFailedAt = Date.now();
|
|
213
206
|
},
|
|
214
207
|
)
|
|
@@ -222,173 +215,197 @@ class RemoteLoader implements Loader {
|
|
|
222
215
|
return list === "every" ? list : list.slice();
|
|
223
216
|
}
|
|
224
217
|
|
|
225
|
-
/**
|
|
226
|
-
|
|
227
|
-
if (
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const snapshot = await this.#readRevision(sha);
|
|
237
|
-
if (snapshot.schemaHash !== fallback.schemaHash) {
|
|
238
|
-
throw errors.loaderFailed(
|
|
239
|
-
`the release ${sha} was published with another schema than this code's ` +
|
|
240
|
-
`(${snapshot.schemaHash} ≠ ${fallback.schemaHash}): preview it on a deploy of its own code`,
|
|
241
|
-
);
|
|
218
|
+
/** One read of `latest.json` at a time (shared); resolves with whether it adopted a new index. */
|
|
219
|
+
#read(): Promise<boolean> {
|
|
220
|
+
if (this.#reading === undefined) {
|
|
221
|
+
const pending = this.#readAndAdopt();
|
|
222
|
+
this.#reading = pending;
|
|
223
|
+
const settle = () => {
|
|
224
|
+
if (this.#reading === pending) this.#reading = undefined;
|
|
225
|
+
};
|
|
226
|
+
pending.then(settle, settle);
|
|
227
|
+
// Shared with later requests: kept alive past the request that started it.
|
|
228
|
+
keepAlive(pending);
|
|
242
229
|
}
|
|
243
|
-
|
|
244
|
-
return { ...snapshot, aliases: fallback.aliases };
|
|
230
|
+
return this.#reading;
|
|
245
231
|
}
|
|
246
232
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
const
|
|
250
|
-
if (
|
|
251
|
-
const
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
233
|
+
async #readAndAdopt(): Promise<boolean> {
|
|
234
|
+
const held = this.#current;
|
|
235
|
+
const read = await this.#readLatest(held?.etag ?? null);
|
|
236
|
+
if (read === null) return false;
|
|
237
|
+
const current = this.#current;
|
|
238
|
+
const at = Date.now();
|
|
239
|
+
// The newest index wins, by sequence: a stale CDN answer never moves the
|
|
240
|
+
// isolate back, its preview hosts included (the held list counts as read now).
|
|
241
|
+
if (current !== undefined && read.content.sequence <= current.content.sequence) {
|
|
242
|
+
this.#hosts =
|
|
243
|
+
this.#hosts === undefined
|
|
244
|
+
? { list: read.previewHosts ?? "every", at }
|
|
245
|
+
: { list: this.#hosts.list, at };
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
// The adopted release's metadata. No `previewHosts` (none written): every host, as with no latest.json.
|
|
249
|
+
this.#hosts = { list: read.previewHosts ?? "every", at };
|
|
250
|
+
if (current !== undefined) this.#previous = current.content;
|
|
251
|
+
this.#current = { content: read.content, etag: read.etag };
|
|
252
|
+
return current !== undefined;
|
|
266
253
|
}
|
|
267
254
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
|
|
255
|
+
/** `latest.json`, revalidating `etag`: `null` for a 304. */
|
|
256
|
+
async #readLatest(etag: string | null): Promise<LatestRead | null> {
|
|
257
|
+
const label = "latest.json";
|
|
258
|
+
const response = await request(
|
|
259
|
+
`${deliveryOrigin()}/sites/${this.#sitePath()}/latest.json`,
|
|
260
|
+
label,
|
|
261
|
+
etag,
|
|
262
|
+
(status) => {
|
|
263
|
+
// Not published yet: no list, so previews are allowed on every host (PO 2026-10-22).
|
|
264
|
+
// A site this process has read a list for can't be unpublished: a 404 then
|
|
265
|
+
// (a CDN fault, a publish caught mid-write) is a failed read, so it keeps the list.
|
|
266
|
+
if (status === 404 && !Array.isArray(this.#hosts?.list)) {
|
|
267
|
+
this.#hosts = { list: "every", at: Date.now() };
|
|
268
|
+
}
|
|
269
|
+
},
|
|
272
270
|
);
|
|
273
|
-
if (
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
}
|
|
277
|
-
const body = await readBoundedJson(response, label, MAX_SNAPSHOT_BYTES);
|
|
278
|
-
if (
|
|
279
|
-
!isPlainObject(body) ||
|
|
280
|
-
body.revision !== sha ||
|
|
281
|
-
typeof body.schemaHash !== "string" ||
|
|
282
|
-
!isPlainObject(body.blocks)
|
|
283
|
-
) {
|
|
284
|
-
throw new Error(`${label}: unexpected format, or not ${expected}`);
|
|
271
|
+
if (response.status === 304) {
|
|
272
|
+
if (this.#hosts !== undefined) this.#hosts = { list: this.#hosts.list, at: Date.now() };
|
|
273
|
+
return null;
|
|
285
274
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
};
|
|
275
|
+
const body = await readBody(response, label, MAX_INDEX_BYTES);
|
|
276
|
+
const content = this.#parseIndex(body, label);
|
|
277
|
+
const previewHosts = previewHostsOf((body as Record<string, unknown>).previewHosts);
|
|
278
|
+
// Its preview hosts are taken only with its index (`#readAndAdopt`).
|
|
279
|
+
return { content, etag: response.headers.get("etag"), previewHosts };
|
|
291
280
|
}
|
|
292
281
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
const
|
|
296
|
-
if (
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
async #latest(site: string): Promise<Latest> {
|
|
301
|
-
const response = await fetchWithTimeout(`${deliveryOrigin()}/sites/${site}/latest.json`);
|
|
302
|
-
if (!response.ok) {
|
|
303
|
-
await response.body?.cancel();
|
|
304
|
-
// Not published yet: no list, so previews are allowed on every host (PO 2026-10-22).
|
|
305
|
-
// A site this process has read a list for can't be unpublished: a 404 then
|
|
306
|
-
// (a CDN fault, a publish caught mid-write) is a failed read, so it keeps the list.
|
|
307
|
-
if (response.status === 404 && !Array.isArray(this.#hosts?.list)) {
|
|
308
|
-
this.#hosts = { list: "every", at: Date.now() };
|
|
309
|
-
}
|
|
310
|
-
throw new Error(`latest.json: HTTP ${response.status}`);
|
|
282
|
+
/** An index body (`latest.json` or a revision's `index.json`), checked. */
|
|
283
|
+
#parseIndex(body: unknown, label: string): IndexedContent {
|
|
284
|
+
const format = (why: string) => new ContentUnavailableError("format", `${label}: ${why}`);
|
|
285
|
+
if (!isPlainObject(body) || !isRevisionId(body.revision)) throw format("unexpected format");
|
|
286
|
+
const sequence = body.sequence;
|
|
287
|
+
if (typeof sequence !== "number" || !Number.isSafeInteger(sequence) || sequence < 1) {
|
|
288
|
+
throw format("sequence must be a positive integer");
|
|
311
289
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
typeof
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
290
|
+
if (!isPlainObject(body.blocks)) throw format("blocks must be an object");
|
|
291
|
+
const blocks = Object.create(null) as BlockIndex;
|
|
292
|
+
for (const [name, raw] of Object.entries(body.blocks)) {
|
|
293
|
+
if (!isPlainObject(raw) || typeof raw.version !== "string" || !HASH_RE.test(raw.version)) {
|
|
294
|
+
throw format(`the entry "${name}" has no version`);
|
|
295
|
+
}
|
|
296
|
+
const entry: IndexEntry = { version: raw.version };
|
|
297
|
+
const meta = metaOf(raw.meta);
|
|
298
|
+
if (meta !== undefined) entry.meta = meta;
|
|
299
|
+
if (name === INLINE_BLOCK && raw.data !== undefined) entry.data = raw.data;
|
|
300
|
+
blocks[name] = entry;
|
|
323
301
|
}
|
|
324
|
-
|
|
325
|
-
// The current release's metadata, whether or not its revision is swapped in.
|
|
326
|
-
// No `previewHosts` (published before Studio wrote the list): every host, as with no latest.json.
|
|
327
|
-
this.#hosts = { list: previewHosts ?? "every", at: Date.now() };
|
|
328
|
-
return {
|
|
329
|
-
revision: body.revision,
|
|
330
|
-
schemaHash: body.schemaHash,
|
|
331
|
-
publishedAt: body.publishedAt,
|
|
332
|
-
previewHosts,
|
|
333
|
-
};
|
|
302
|
+
return { revision: body.revision, sequence, blocks, source: this.blocks };
|
|
334
303
|
}
|
|
335
304
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
305
|
+
/** `blocks/<hash>.json`. */
|
|
306
|
+
async #fetchBlock(hash: string, name: string): Promise<FetchedBody> {
|
|
307
|
+
const label = `block "${name}" (${hash})`;
|
|
308
|
+
if (!HASH_RE.test(hash)) throw new ContentUnavailableError("format", `${label}: not a hash`);
|
|
309
|
+
const response = await request(
|
|
310
|
+
`${deliveryOrigin()}/sites/${this.#sitePath()}/blocks/${hash}.json`,
|
|
311
|
+
label,
|
|
312
|
+
);
|
|
313
|
+
try {
|
|
314
|
+
const { value, bytes } = await readBoundedJsonSized(response, label, MAX_BLOCK_BYTES);
|
|
315
|
+
return { body: value, bytes };
|
|
316
|
+
} catch (error) {
|
|
317
|
+
throw unavailable(label, error, "format");
|
|
340
318
|
}
|
|
341
|
-
return body;
|
|
342
319
|
}
|
|
320
|
+
|
|
321
|
+
#sitePath(): string {
|
|
322
|
+
return encodeURIComponent(this.#site);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** An entry's `meta`, kept to its known keys and shapes; `undefined` when none. */
|
|
327
|
+
function metaOf(raw: unknown): BlockMeta | undefined {
|
|
328
|
+
if (!isPlainObject(raw)) return undefined;
|
|
329
|
+
const meta: BlockMeta = {};
|
|
330
|
+
if (typeof raw.type === "string") meta.type = raw.type;
|
|
331
|
+
if (typeof raw.path === "string") meta.path = raw.path;
|
|
332
|
+
if (Array.isArray(raw.refs)) {
|
|
333
|
+
const refs = raw.refs.filter((ref): ref is string => typeof ref === "string");
|
|
334
|
+
if (refs.length > 0) meta.refs = refs;
|
|
335
|
+
}
|
|
336
|
+
return Object.keys(meta).length > 0 ? meta : undefined;
|
|
343
337
|
}
|
|
344
338
|
|
|
345
|
-
/** A loader over the
|
|
339
|
+
/** A loader over the content it's given alone: `remoteLoader` without `site`. */
|
|
346
340
|
class LocalLoader implements Loader {
|
|
347
|
-
#
|
|
341
|
+
#content: Snapshot | Loader;
|
|
348
342
|
|
|
349
|
-
constructor(
|
|
350
|
-
this.#
|
|
343
|
+
constructor(content: Snapshot | Loader) {
|
|
344
|
+
this.#content = content;
|
|
351
345
|
}
|
|
352
346
|
|
|
353
|
-
adopt(
|
|
354
|
-
this.#
|
|
347
|
+
adopt(content: Snapshot | Loader): void {
|
|
348
|
+
this.#content = content;
|
|
355
349
|
}
|
|
356
350
|
|
|
357
351
|
[PEEK_RELEASE](): Snapshot | undefined {
|
|
358
|
-
return peekRelease(this.#
|
|
352
|
+
return peekRelease(this.#content);
|
|
359
353
|
}
|
|
360
354
|
|
|
361
355
|
load(): Promise<Snapshot> {
|
|
362
|
-
const
|
|
363
|
-
return
|
|
356
|
+
const content = this.#content;
|
|
357
|
+
return typeof (content as Loader).load === "function"
|
|
358
|
+
? (content as Loader).load()
|
|
359
|
+
: Promise.resolve(content as Snapshot);
|
|
364
360
|
}
|
|
365
361
|
}
|
|
366
362
|
|
|
367
363
|
/**
|
|
368
|
-
* Hosted releases
|
|
369
|
-
*
|
|
364
|
+
* Hosted releases: `createCMS` builds it for you when `site` is set. With
|
|
365
|
+
* `site`, content comes only from the CDN, so `content` must be `undefined`
|
|
366
|
+
* (nothing is bundled). Without `site`, it's a loader over
|
|
367
|
+
* `content` alone.
|
|
370
368
|
*/
|
|
371
|
-
export function remoteLoader(
|
|
369
|
+
export function remoteLoader(
|
|
370
|
+
content: Snapshot | Loader | undefined,
|
|
371
|
+
options: RemoteLoaderOptions,
|
|
372
|
+
): Loader {
|
|
372
373
|
const { site } = options ?? {};
|
|
373
374
|
const hosted = Boolean(site);
|
|
375
|
+
if (hosted && content !== undefined) {
|
|
376
|
+
throw new TypeError(
|
|
377
|
+
"remoteLoader: with site, content comes from the delivery CDN at runtime; pass undefined as content",
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
if (!hosted && content === undefined) {
|
|
381
|
+
throw new TypeError("remoteLoader: without site, pass the content to serve");
|
|
382
|
+
}
|
|
374
383
|
const key = Symbol.for(
|
|
375
|
-
|
|
384
|
+
hosted
|
|
385
|
+
? `${INSTANCE_PREFIX}site:${site}`
|
|
386
|
+
: `${INSTANCE_PREFIX}${contentIdentity(content as Snapshot | Loader)}|local`,
|
|
376
387
|
);
|
|
377
388
|
const store = globalThis as unknown as Record<symbol, RemoteLoader | LocalLoader | undefined>;
|
|
378
389
|
const existing = store[key];
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
390
|
+
// Duck-typed, so an instance a second copy of this module made is reused too.
|
|
391
|
+
if (existing instanceof Object && typeof existing.load === "function") {
|
|
392
|
+
if (hosted && (existing as Partial<HostedSource>)[HOSTED] === true) {
|
|
393
|
+
if ((existing as RemoteLoader).interval !== options.interval) {
|
|
394
|
+
console.warn(
|
|
395
|
+
"[decocms/blocks] remoteLoader was called again for the same site with different options " +
|
|
396
|
+
"(interval); keeping the first instance's options.",
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
return existing;
|
|
400
|
+
}
|
|
401
|
+
if (!hosted && typeof (existing as LocalLoader).adopt === "function") {
|
|
402
|
+
(existing as LocalLoader).adopt(content as Snapshot | Loader);
|
|
403
|
+
return existing;
|
|
385
404
|
}
|
|
386
|
-
existing.adopt(fallback);
|
|
387
|
-
return existing;
|
|
388
405
|
}
|
|
389
406
|
const instance = hosted
|
|
390
|
-
? new RemoteLoader(
|
|
391
|
-
: new LocalLoader(
|
|
407
|
+
? new RemoteLoader({ site: site as string, interval: options.interval })
|
|
408
|
+
: new LocalLoader(content as Snapshot | Loader);
|
|
392
409
|
store[key] = instance;
|
|
393
410
|
return instance;
|
|
394
411
|
}
|
|
@@ -398,19 +415,66 @@ export function resetRemoteLoaders(): void {
|
|
|
398
415
|
}
|
|
399
416
|
|
|
400
417
|
/**
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
418
|
+
* GETs `url` (no credentials, 10 s for headers; `If-None-Match: etag` when
|
|
419
|
+
* given). Resolves with a 2xx, or with a 304 to a request that sent an ETag.
|
|
420
|
+
* Every other outcome is a `ContentUnavailableError`: the timeout, the
|
|
421
|
+
* network, any other HTTP status (`onStatus` sees it first).
|
|
405
422
|
*/
|
|
406
|
-
function
|
|
407
|
-
|
|
408
|
-
|
|
423
|
+
async function request(
|
|
424
|
+
url: string,
|
|
425
|
+
label: string,
|
|
426
|
+
etag?: string | null,
|
|
427
|
+
onStatus?: (status: number) => void,
|
|
428
|
+
): Promise<Response> {
|
|
429
|
+
let response: Response;
|
|
430
|
+
const headers: Record<string, string> = {};
|
|
431
|
+
if (etag) headers["if-none-match"] = etag;
|
|
432
|
+
try {
|
|
433
|
+
response = await fetch(url, { headers, signal: timeoutSignal(FETCH_TIMEOUT_MS) });
|
|
434
|
+
} catch (error) {
|
|
435
|
+
throw unavailable(label, error, "network");
|
|
436
|
+
}
|
|
437
|
+
if (response.status === 304 && etag) {
|
|
438
|
+
void response.body?.cancel().catch(() => {});
|
|
439
|
+
return response;
|
|
440
|
+
}
|
|
441
|
+
if (!response.ok) {
|
|
442
|
+
void response.body?.cancel().catch(() => {});
|
|
443
|
+
onStatus?.(response.status);
|
|
444
|
+
throw new ContentUnavailableError("http", `${label}: HTTP ${response.status}`, {
|
|
445
|
+
status: response.status,
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
return response;
|
|
409
449
|
}
|
|
410
450
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
451
|
+
async function readBody(response: Response, label: string, maxBytes: number): Promise<unknown> {
|
|
452
|
+
try {
|
|
453
|
+
return await readBoundedJson(response, label, maxBytes);
|
|
454
|
+
} catch (error) {
|
|
455
|
+
throw unavailable(label, error, "format");
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** A failed fetch or body read as a `ContentUnavailableError`; a timeout is always `"timeout"`. */
|
|
460
|
+
function unavailable(
|
|
461
|
+
label: string,
|
|
462
|
+
error: unknown,
|
|
463
|
+
otherwise: "network" | "format",
|
|
464
|
+
): ContentUnavailableError {
|
|
465
|
+
if (error instanceof ContentUnavailableError) return error;
|
|
466
|
+
const name = error instanceof Error ? error.name : undefined;
|
|
467
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
468
|
+
if (name === "TimeoutError" || name === "AbortError") {
|
|
469
|
+
return new ContentUnavailableError(
|
|
470
|
+
"timeout",
|
|
471
|
+
`${label}: no answer within ${FETCH_TIMEOUT_MS} ms`,
|
|
472
|
+
{ cause: error },
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
// A connection that drops mid-body is the network's, not the body's.
|
|
476
|
+
const reason = otherwise === "format" && error instanceof TypeError ? "network" : otherwise;
|
|
477
|
+
return new ContentUnavailableError(reason, `${label}: ${message}`, { cause: error });
|
|
414
478
|
}
|
|
415
479
|
|
|
416
480
|
function deliveryOrigin(): string {
|