@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.
- 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/content.ts
CHANGED
|
@@ -1,32 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The content side of a CMS: one source (the content module
|
|
3
|
-
* and
|
|
4
|
-
* `
|
|
2
|
+
* The content side of a CMS: one source (the content module, a `Loader`, or
|
|
3
|
+
* hosted releases) and what every client shares. Clients are handed a
|
|
4
|
+
* `Selection`: the current index (and the previous one, for hosted releases)
|
|
5
|
+
* that a request pins (see ./pin.ts).
|
|
5
6
|
*
|
|
6
|
-
* A draft is the
|
|
7
|
-
* ./draftChanges.ts)
|
|
7
|
+
* A draft is the current index with a draft's files laid over it (see
|
|
8
|
+
* ./draftChanges.ts), read for each draft request and never kept: the
|
|
9
|
+
* draft reads of one request share one read of its files, and nothing read
|
|
10
|
+
* by name from `drafts/` serves a later request.
|
|
8
11
|
*/
|
|
9
|
-
import {
|
|
12
|
+
import { keepAlive } from "./background.ts";
|
|
13
|
+
import { type IndexedContent, localIndex } from "./contentIndex.ts";
|
|
14
|
+
import { parseDraftPointer } from "./draft.ts";
|
|
10
15
|
import {
|
|
11
16
|
DEFAULT_DRAFT_HOSTS,
|
|
12
|
-
type
|
|
13
|
-
|
|
17
|
+
type DraftState,
|
|
18
|
+
fetchDraft,
|
|
14
19
|
LOCAL_VERSION,
|
|
15
|
-
|
|
20
|
+
overlayDraft,
|
|
16
21
|
} from "./draftChanges.ts";
|
|
17
22
|
import { errors, isResolutionError } from "./errors.ts";
|
|
23
|
+
import {
|
|
24
|
+
type HostedSource,
|
|
25
|
+
isHostedSource,
|
|
26
|
+
previewHostsOf,
|
|
27
|
+
type ReleasePreviewHosts,
|
|
28
|
+
} from "./hostedSource.ts";
|
|
18
29
|
import { isPlainObject } from "./json.ts";
|
|
19
|
-
import type {
|
|
20
|
-
|
|
21
|
-
/** Fetched drafts kept per CMS. */
|
|
22
|
-
const CACHED_DRAFTS = 3;
|
|
23
|
-
|
|
24
|
-
/** A fetched draft body, its ETag, and the views of it over each release it was layered on. */
|
|
25
|
-
interface CachedDraft {
|
|
26
|
-
changes: DraftChanges;
|
|
27
|
-
etag: string | null;
|
|
28
|
-
views: WeakMap<Snapshot, Snapshot>;
|
|
29
|
-
}
|
|
30
|
+
import type { Selection } from "./pin.ts";
|
|
31
|
+
import type { DraftPointer, Loader, Snapshot } from "./types.ts";
|
|
30
32
|
|
|
31
33
|
export function isLoader(content: unknown): content is Loader {
|
|
32
34
|
return (
|
|
@@ -37,8 +39,8 @@ export function isLoader(content: unknown): content is Loader {
|
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
/**
|
|
40
|
-
* An internal hook a built-in loader
|
|
41
|
-
*
|
|
42
|
+
* An internal hook a built-in local loader has: the snapshot it would return
|
|
43
|
+
* right now, from memory, or `undefined` when that takes a load.
|
|
42
44
|
* `cms.settings()` reads through it, so it never fetches.
|
|
43
45
|
*/
|
|
44
46
|
export const PEEK_RELEASE: unique symbol = Symbol.for("decocms.blocks.peekRelease");
|
|
@@ -50,59 +52,19 @@ export function peekRelease(source: Snapshot | Loader): Snapshot | undefined {
|
|
|
50
52
|
return typeof peek === "function" ? peek.call(source) : undefined;
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
/**
|
|
54
|
-
* An internal hook `remoteLoader` has: a published release by its commit SHA
|
|
55
|
-
* (`sites/<site>/revisions/<sha>.json`), kept in memory once read. Rejects
|
|
56
|
-
* with `LOADER_FAILED`, never falls back to another release.
|
|
57
|
-
*/
|
|
58
|
-
export const READ_REVISION: unique symbol = Symbol.for("decocms.blocks.readRevision");
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* An internal hook `remoteLoader` has: the current release's preview hosts
|
|
62
|
-
* from the release metadata (`latest.json`), read once when this process
|
|
63
|
-
* hasn't read them yet (see `ReleasePreviewHosts`).
|
|
64
|
-
*/
|
|
65
|
-
export const PREVIEW_HOSTS: unique symbol = Symbol.for("decocms.blocks.previewHosts");
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* What the current release says about where it may be previewed (PO 2026-10-22):
|
|
69
|
-
* - a list: the hosts Studio wrote (`previewHosts`), enforced;
|
|
70
|
-
* - `"every"`: a remote site with no list published, so every host: no
|
|
71
|
-
* `latest.json` yet (a 404), or one without `previewHosts` (published
|
|
72
|
-
* before Studio wrote the list; its next publish writes one);
|
|
73
|
-
* - `"local"`: content with no remote site (no release metadata at all):
|
|
74
|
-
* `createCMS`'s `preview.hosts` when set, else every host;
|
|
75
|
-
* - `"unknown"`: the metadata couldn't be read (a network error, a 5xx, a
|
|
76
|
-
* malformed `latest.json`). That isn't "missing": previews stay closed.
|
|
77
|
-
*/
|
|
78
|
-
export type ReleasePreviewHosts = readonly string[] | "every" | "local" | "unknown";
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* A release's `previewHosts` as Studio wrote them: the strings of a list;
|
|
82
|
-
* `undefined` when the field is absent (no list written); an empty list (no
|
|
83
|
-
* host) for a field that isn't a list, so a malformed one never opens previews.
|
|
84
|
-
*/
|
|
85
|
-
export function previewHostsOf(value: unknown): string[] | undefined {
|
|
86
|
-
if (value === undefined) return undefined;
|
|
87
|
-
if (!Array.isArray(value)) return [];
|
|
88
|
-
return value.filter((host): host is string => typeof host === "string");
|
|
89
|
-
}
|
|
90
|
-
|
|
91
55
|
export function isSnapshot(content: unknown): content is Snapshot {
|
|
92
56
|
return (
|
|
93
57
|
isPlainObject(content) &&
|
|
94
58
|
typeof content.revision === "string" &&
|
|
95
59
|
isPlainObject(content.blocks) &&
|
|
96
60
|
(content.aliases === undefined || isPlainObject(content.aliases)) &&
|
|
97
|
-
(content.schemaHash === undefined || typeof content.schemaHash === "string") &&
|
|
98
|
-
(content.committedAt === undefined || typeof content.committedAt === "string") &&
|
|
99
61
|
(content.previewHosts === undefined || Array.isArray(content.previewHosts))
|
|
100
62
|
);
|
|
101
63
|
}
|
|
102
64
|
|
|
103
65
|
export class ContentStore {
|
|
104
66
|
#source: Snapshot | Loader;
|
|
105
|
-
#release: Promise<
|
|
67
|
+
#release: Promise<IndexedContent> | undefined;
|
|
106
68
|
#updating: Promise<{ updated: boolean }> | undefined;
|
|
107
69
|
/** The check a draft read waits on when one was already running when it arrived (see `checkRelease`). */
|
|
108
70
|
#queued: Promise<string> | undefined;
|
|
@@ -115,11 +77,10 @@ export class ContentStore {
|
|
|
115
77
|
*/
|
|
116
78
|
#checksDone = 0;
|
|
117
79
|
#checkPrefix: string | undefined;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
readonly #draftReads = new Map<string, Promise<CachedDraft>>();
|
|
80
|
+
/** Draft reads shared under one release check, by the check's number, then the pointer (`#draftFiles`). */
|
|
81
|
+
readonly #draftReads = new Map<number, Map<string, Promise<Map<string, DraftState>>>>();
|
|
121
82
|
readonly #draftHosts: readonly string[];
|
|
122
|
-
/** The
|
|
83
|
+
/** The snapshot this store last loaded, for a loader that can't peek. */
|
|
123
84
|
#latest: Snapshot | undefined;
|
|
124
85
|
|
|
125
86
|
constructor(source: Snapshot | Loader, draftHosts: readonly string[] = DEFAULT_DRAFT_HOSTS) {
|
|
@@ -127,20 +88,40 @@ export class ContentStore {
|
|
|
127
88
|
this.#draftHosts = draftHosts;
|
|
128
89
|
}
|
|
129
90
|
|
|
130
|
-
/** Whether the source can change while the process runs (
|
|
91
|
+
/** Whether the source can change while the process runs (hosted, or a loader with `update()`). */
|
|
131
92
|
get updatable(): boolean {
|
|
132
|
-
|
|
93
|
+
const source = this.#source;
|
|
94
|
+
if (isHostedSource(source)) return true;
|
|
95
|
+
return isLoader(source) && typeof source.update === "function";
|
|
133
96
|
}
|
|
134
97
|
|
|
135
98
|
/**
|
|
136
|
-
* The current release
|
|
137
|
-
*
|
|
138
|
-
* is
|
|
99
|
+
* The current release, as an index. Hosted: the index held (the first use
|
|
100
|
+
* loads it), with the previous one. A snapshot is the release; a loader
|
|
101
|
+
* with `update()` is loaded once (concurrent calls share the one load in
|
|
102
|
+
* flight; a failed load isn't kept) and reloaded after an update; a loader
|
|
103
|
+
* without `update()` is asked on every call, since it's always current by
|
|
104
|
+
* construction.
|
|
139
105
|
*/
|
|
140
|
-
release(): Promise<
|
|
106
|
+
release(): Promise<Selection> {
|
|
141
107
|
const source = this.#source;
|
|
142
|
-
if (
|
|
143
|
-
|
|
108
|
+
if (isHostedSource(source)) {
|
|
109
|
+
return source.current().then(
|
|
110
|
+
(current): Selection => {
|
|
111
|
+
const previous = source.previous();
|
|
112
|
+
return previous === undefined ? { current } : { current, previous };
|
|
113
|
+
},
|
|
114
|
+
(error: unknown) => {
|
|
115
|
+
throw isResolutionError(error)
|
|
116
|
+
? error
|
|
117
|
+
: errors.loaderFailed("the content loader failed", error);
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
if (!isLoader(source)) return Promise.resolve({ current: this.#serve(source) });
|
|
122
|
+
if (!this.updatable) {
|
|
123
|
+
return this.#load(source).then((snapshot) => ({ current: this.#serve(snapshot) }));
|
|
124
|
+
}
|
|
144
125
|
if (this.#release === undefined) {
|
|
145
126
|
const pending = this.#load(source).then((snapshot) => this.#serve(snapshot));
|
|
146
127
|
this.#release = pending;
|
|
@@ -149,143 +130,108 @@ export class ContentStore {
|
|
|
149
130
|
if (this.#release === pending) this.#release = undefined;
|
|
150
131
|
});
|
|
151
132
|
}
|
|
152
|
-
return this.#release;
|
|
133
|
+
return this.#release.then((current) => ({ current }));
|
|
153
134
|
}
|
|
154
135
|
|
|
155
136
|
/**
|
|
156
|
-
* The draft a pointer names: its
|
|
157
|
-
*
|
|
137
|
+
* The draft a pointer names: its files, read from a draft host, laid over
|
|
138
|
+
* the current index. A pointer that doesn't parse, or any failure, is
|
|
158
139
|
* `LOADER_FAILED`, never a silent fallback to the release. A pointer whose
|
|
159
|
-
* version is `local` names no draft: the release, with nothing fetched.
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
140
|
+
* version is `local` names no draft: the release, with nothing fetched.
|
|
141
|
+
* Every draft request reads the draft again (nothing from `drafts/` is kept
|
|
142
|
+
* for a later request; the reads of one request, under its `releaseCheck`,
|
|
143
|
+
* share one), and never the previous index.
|
|
163
144
|
*
|
|
164
|
-
* Before
|
|
145
|
+
* Before overlaying, a draft read checks the source for a newer release and
|
|
165
146
|
* waits for that check (see `checkRelease`), so a draft always shows over
|
|
166
147
|
* the latest release, even in a process that hasn't picked it up yet. The
|
|
167
|
-
* check runs beside the draft's own
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
148
|
+
* check runs beside the draft's own reads. With `releaseCheck` (the id
|
|
149
|
+
* `checkRelease` resolved with, for this request), it waits for that check
|
|
150
|
+
* instead of making its own; an id this store didn't make is a check of its
|
|
151
|
+
* own.
|
|
171
152
|
*/
|
|
172
|
-
draft(pointer: string, releaseCheck?: string | null): Promise<
|
|
153
|
+
draft(pointer: string, releaseCheck?: string | null): Promise<Selection> {
|
|
173
154
|
const parsed = parseDraftPointer(pointer);
|
|
174
155
|
if (parsed === null) {
|
|
175
156
|
return Promise.reject(errors.loaderFailed(`invalid draft pointer "${truncate(pointer)}"`));
|
|
176
157
|
}
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const held = this.#drafts.get(key);
|
|
183
|
-
read = fetchDraftChanges(parsed, { domains: this.#draftHosts, etag: held?.etag }).then(
|
|
184
|
-
(result) => {
|
|
185
|
-
if (result.status === 304 && held !== undefined) return held;
|
|
186
|
-
if (result.status === 304) throw new Error("draft changes: 304 without a held body");
|
|
187
|
-
const entry: CachedDraft = {
|
|
188
|
-
changes: result.changes,
|
|
189
|
-
etag: result.etag,
|
|
190
|
-
views: new WeakMap(),
|
|
191
|
-
};
|
|
192
|
-
this.#drafts.set(key, entry);
|
|
193
|
-
return entry;
|
|
194
|
-
},
|
|
195
|
-
);
|
|
196
|
-
const pending = read;
|
|
197
|
-
this.#draftReads.set(key, pending);
|
|
198
|
-
const settle = () => {
|
|
199
|
-
if (this.#draftReads.get(key) === pending) this.#draftReads.delete(key);
|
|
200
|
-
};
|
|
201
|
-
pending.then(settle, settle);
|
|
158
|
+
const { variants } = parsed;
|
|
159
|
+
const withVariants = (selection: Selection): Selection =>
|
|
160
|
+
variants === undefined ? selection : { ...selection, variants };
|
|
161
|
+
if (parsed.version === LOCAL_VERSION) {
|
|
162
|
+
return this.release().then(({ current }) => withVariants({ current }));
|
|
202
163
|
}
|
|
203
|
-
const
|
|
204
|
-
throw errors.loaderFailed("the draft's changes couldn't be fetched", error);
|
|
205
|
-
});
|
|
164
|
+
const files = this.#draftFiles(parsed, releaseCheck);
|
|
206
165
|
const release = this.waitForCheck(releaseCheck).then(() => this.release());
|
|
207
|
-
return Promise.all([release,
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
// OPEN: the view is keyed by the body's ETag (the pointer outlives later saves); the version without one.
|
|
211
|
-
view = layerDraft(base, entry.changes, entry.etag ?? version);
|
|
212
|
-
entry.views.set(base, view);
|
|
213
|
-
}
|
|
214
|
-
return view;
|
|
215
|
-
});
|
|
166
|
+
return Promise.all([release, files]).then(([base, states]) =>
|
|
167
|
+
withVariants({ current: overlayDraft(base.current, states, parsed.version) }),
|
|
168
|
+
);
|
|
216
169
|
}
|
|
217
170
|
|
|
218
171
|
/**
|
|
219
|
-
* A published release by its
|
|
220
|
-
* (`remoteLoader`, which `createCMS` builds with `site`), or the
|
|
221
|
-
* memory when it is that revision. Anything else is
|
|
222
|
-
* a silent fallback to the current release.
|
|
172
|
+
* A published release by its revision id: from the source's hosted
|
|
173
|
+
* releases (`remoteLoader`, which `createCMS` builds with `site`), or the
|
|
174
|
+
* content in memory when it is that revision. Anything else is
|
|
175
|
+
* `LOADER_FAILED`, never a silent fallback to the current release.
|
|
223
176
|
*/
|
|
224
|
-
revision(
|
|
177
|
+
revision(id: string): Promise<Selection> {
|
|
225
178
|
const source = this.#source;
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
179
|
+
if (isHostedSource(source)) {
|
|
180
|
+
return source.revision(id).then(
|
|
181
|
+
(current): Selection => ({ current }),
|
|
182
|
+
(error: unknown) => {
|
|
183
|
+
if (isResolutionError(error)) throw error;
|
|
184
|
+
throw errors.loaderFailed(`the release ${id} couldn't be read`, error);
|
|
185
|
+
},
|
|
186
|
+
);
|
|
234
187
|
}
|
|
235
|
-
return this.release().then((
|
|
236
|
-
if (
|
|
188
|
+
return this.release().then((selection) => {
|
|
189
|
+
if (selection.current.revision === id) return { current: selection.current };
|
|
237
190
|
throw errors.loaderFailed(
|
|
238
|
-
`the release ${
|
|
191
|
+
`the release ${id} isn't available: published releases are read from hosted releases (createCMS with site)`,
|
|
239
192
|
);
|
|
240
193
|
});
|
|
241
194
|
}
|
|
242
195
|
|
|
243
196
|
/**
|
|
244
|
-
* The current release as it is in memory, without loading
|
|
245
|
-
* content module, what a built-in loader holds, or the
|
|
246
|
-
* last loaded. `undefined` before
|
|
197
|
+
* The current release's index as it is in memory, without loading
|
|
198
|
+
* anything: the content module, what a built-in loader holds, or the
|
|
199
|
+
* release this store last loaded. `undefined` before the first load.
|
|
247
200
|
*/
|
|
248
|
-
current():
|
|
249
|
-
|
|
201
|
+
current(): IndexedContent | undefined {
|
|
202
|
+
const source = this.#source;
|
|
203
|
+
if (isHostedSource(source)) return source.peek();
|
|
204
|
+
const snapshot = peekRelease(source) ?? this.#latest;
|
|
205
|
+
return snapshot === undefined ? undefined : localIndex(snapshot);
|
|
250
206
|
}
|
|
251
207
|
|
|
252
208
|
/**
|
|
253
209
|
* The current release's preview hosts (see `ReleasePreviewHosts`): what the
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
210
|
+
* hosted release metadata says (read once if it hasn't been yet; a failed
|
|
211
|
+
* read is `"unknown"`), else, for a source with no release metadata (no
|
|
212
|
+
* remote site), the snapshot in memory's own `previewHosts`, or `"local"`
|
|
213
|
+
* without one. `peek` reads memory only, never the network.
|
|
258
214
|
*/
|
|
259
215
|
async previewHosts(peek = false): Promise<ReleasePreviewHosts> {
|
|
260
216
|
const source = this.#source;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
PREVIEW_HOSTS
|
|
264
|
-
]
|
|
265
|
-
: undefined;
|
|
266
|
-
if (typeof read === "function") {
|
|
267
|
-
return read.call(source, peek).catch((): ReleasePreviewHosts => "unknown");
|
|
217
|
+
if (isHostedSource(source)) {
|
|
218
|
+
return source.previewHosts(peek).catch((): ReleasePreviewHosts => "unknown");
|
|
268
219
|
}
|
|
269
|
-
|
|
220
|
+
const snapshot = peekRelease(source) ?? this.#latest;
|
|
221
|
+
return previewHostsOf(snapshot?.previewHosts) ?? "local";
|
|
270
222
|
}
|
|
271
223
|
|
|
272
224
|
/** Asks the source for newer content; never throws, and concurrent calls share one check. */
|
|
273
225
|
update(): Promise<{ updated: boolean }> {
|
|
274
226
|
const source = this.#source;
|
|
275
|
-
if (!
|
|
276
|
-
return Promise.resolve({ updated: false });
|
|
277
|
-
}
|
|
227
|
+
if (!this.updatable) return Promise.resolve({ updated: false });
|
|
278
228
|
this.#updating ??= (async () => {
|
|
279
229
|
try {
|
|
280
|
-
|
|
230
|
+
if (isHostedSource(source)) return await (source as HostedSource).poll();
|
|
231
|
+
const result = await (source as Loader).update?.();
|
|
281
232
|
const updated = result?.updated === true;
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
// `#latest` stays: settings keep the release that was serving until
|
|
285
|
-
// the next one loads, never the defaults (which allow no host).
|
|
286
|
-
// Drafts are fetched again, so a preview follows the release too.
|
|
287
|
-
this.#drafts.clear();
|
|
288
|
-
}
|
|
233
|
+
// `#latest` stays: settings keep the release that was serving until the next one loads.
|
|
234
|
+
if (updated) this.#release = undefined;
|
|
289
235
|
return { updated };
|
|
290
236
|
} catch {
|
|
291
237
|
return { updated: false };
|
|
@@ -340,7 +286,57 @@ export class ContentStore {
|
|
|
340
286
|
}
|
|
341
287
|
|
|
342
288
|
#startCheck(): Promise<string> {
|
|
343
|
-
return this.update().then(() =>
|
|
289
|
+
return this.update().then(() => {
|
|
290
|
+
const n = ++this.#checksDone;
|
|
291
|
+
// Reads under checks older than the one before this are dropped: their requests are over or nearly.
|
|
292
|
+
for (const check of this.#draftReads.keys()) {
|
|
293
|
+
if (check < n - 1) this.#draftReads.delete(check);
|
|
294
|
+
}
|
|
295
|
+
return this.#checkId(n);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* A draft's files (its index, then every file it lists). Reads under one
|
|
301
|
+
* release check share one read: the check's id is handed to every draft
|
|
302
|
+
* read of the request that made it (the host check, the redirects, the
|
|
303
|
+
* assignments and the page), so a preview request reads the draft folder
|
|
304
|
+
* once and every part of it sees the same save. A read under a check starts
|
|
305
|
+
* only after the check ended, so it is never older than any request that
|
|
306
|
+
* shares it. A failed read isn't shared, and reads under old checks are
|
|
307
|
+
* dropped once newer checks end (`#startCheck`): nothing from `drafts/`
|
|
308
|
+
* serves a later request. Without a check id this store made (or for a
|
|
309
|
+
* source that can't change, whose checks are all one), every read is its
|
|
310
|
+
* own.
|
|
311
|
+
*/
|
|
312
|
+
#draftFiles(
|
|
313
|
+
pointer: DraftPointer,
|
|
314
|
+
releaseCheck: string | null | undefined,
|
|
315
|
+
): Promise<Map<string, DraftState>> {
|
|
316
|
+
const read = () =>
|
|
317
|
+
fetchDraft(pointer, { domains: this.#draftHosts }).catch((error: unknown) => {
|
|
318
|
+
throw errors.loaderFailed("the draft's changes couldn't be fetched", error);
|
|
319
|
+
});
|
|
320
|
+
const check = this.updatable ? this.#checkNumber(releaseCheck) : undefined;
|
|
321
|
+
if (check === undefined) return read();
|
|
322
|
+
const key = `${pointer.host}${pointer.path}@${pointer.version}`;
|
|
323
|
+
let reads = this.#draftReads.get(check);
|
|
324
|
+
if (reads === undefined) {
|
|
325
|
+
reads = new Map();
|
|
326
|
+
this.#draftReads.set(check, reads);
|
|
327
|
+
}
|
|
328
|
+
const shared = reads.get(key);
|
|
329
|
+
if (shared !== undefined) return shared;
|
|
330
|
+
const pending = read();
|
|
331
|
+
reads.set(key, pending);
|
|
332
|
+
// Requests under one check share it: kept alive past the request that
|
|
333
|
+
// started it (see ./blockStore.ts), so none of them awaits it forever.
|
|
334
|
+
keepAlive(pending);
|
|
335
|
+
pending.catch(() => {
|
|
336
|
+
if (this.#draftReads.get(check)?.get(key) === pending)
|
|
337
|
+
this.#draftReads.get(check)?.delete(key);
|
|
338
|
+
});
|
|
339
|
+
return pending;
|
|
344
340
|
}
|
|
345
341
|
|
|
346
342
|
#checkId(n: number): string {
|
|
@@ -350,31 +346,29 @@ export class ContentStore {
|
|
|
350
346
|
|
|
351
347
|
/** Whether `id` names a check this store made (and so one that ended). */
|
|
352
348
|
#madeCheck(id: string): boolean {
|
|
349
|
+
return this.#checkNumber(id) !== undefined;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** The number of the check `id` names, when this store made it; otherwise `undefined`. */
|
|
353
|
+
#checkNumber(id: string | null | undefined): number | undefined {
|
|
353
354
|
const prefix = this.#checkPrefix;
|
|
354
|
-
if (prefix === undefined || !id.startsWith(prefix)) return
|
|
355
|
+
if (typeof id !== "string" || prefix === undefined || !id.startsWith(prefix)) return undefined;
|
|
355
356
|
const digits = id.slice(prefix.length);
|
|
356
|
-
|
|
357
|
+
if (!/^\d{1,15}$/.test(digits)) return undefined;
|
|
358
|
+
const n = Number(digits);
|
|
359
|
+
return n <= this.#checksDone ? n : undefined;
|
|
357
360
|
}
|
|
358
361
|
|
|
359
362
|
/**
|
|
360
363
|
* Swaps the content in place, for a hot reload that hands the same CMS a new
|
|
361
364
|
* content module. Any new object swaps, even with the same `revision`: an
|
|
362
|
-
* edited JSON file
|
|
363
|
-
* module carries new `blocks` under the old revision.
|
|
365
|
+
* edited JSON file can carry new `blocks` under the old revision.
|
|
364
366
|
*/
|
|
365
367
|
replace(source: Snapshot | Loader): void {
|
|
366
|
-
const previous = this.#source;
|
|
367
|
-
if (source === previous) {
|
|
368
|
-
// The same loader (a hosted remoteLoader that adopted new fallback content): re-read it.
|
|
369
|
-
this.#release = undefined;
|
|
370
|
-
this.#latest = undefined;
|
|
371
|
-
this.#drafts.clear();
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
368
|
this.#source = source;
|
|
369
|
+
this.#draftReads.clear();
|
|
375
370
|
this.#release = undefined;
|
|
376
371
|
this.#latest = undefined;
|
|
377
|
-
this.#drafts.clear();
|
|
378
372
|
}
|
|
379
373
|
|
|
380
374
|
async #load(loader: Loader): Promise<Snapshot> {
|
|
@@ -391,56 +385,12 @@ export class ContentStore {
|
|
|
391
385
|
return snapshot;
|
|
392
386
|
}
|
|
393
387
|
|
|
394
|
-
#serve(snapshot: Snapshot):
|
|
388
|
+
#serve(snapshot: Snapshot): IndexedContent {
|
|
395
389
|
this.#latest = snapshot;
|
|
396
|
-
return snapshot;
|
|
390
|
+
return localIndex(snapshot);
|
|
397
391
|
}
|
|
398
392
|
}
|
|
399
393
|
|
|
400
394
|
function truncate(value: string): string {
|
|
401
395
|
return value.length > 80 ? `${value.slice(0, 80)}…` : value;
|
|
402
396
|
}
|
|
403
|
-
|
|
404
|
-
/**
|
|
405
|
-
* A Map that forgets its least recently used entries past `limit`, counted by
|
|
406
|
-
* `size` (one per entry unless given).
|
|
407
|
-
*/
|
|
408
|
-
export class BoundedMap<K, V> {
|
|
409
|
-
readonly #map = new Map<K, V>();
|
|
410
|
-
#total = 0;
|
|
411
|
-
constructor(
|
|
412
|
-
readonly limit: number,
|
|
413
|
-
readonly size: (value: V) => number = () => 1,
|
|
414
|
-
) {}
|
|
415
|
-
|
|
416
|
-
get(key: K): V | undefined {
|
|
417
|
-
const value = this.#map.get(key);
|
|
418
|
-
if (value !== undefined) {
|
|
419
|
-
this.#map.delete(key);
|
|
420
|
-
this.#map.set(key, value);
|
|
421
|
-
}
|
|
422
|
-
return value;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
set(key: K, value: V): void {
|
|
426
|
-
this.delete(key);
|
|
427
|
-
this.#map.set(key, value);
|
|
428
|
-
this.#total += this.size(value);
|
|
429
|
-
for (const [oldest, old] of this.#map) {
|
|
430
|
-
if (this.#total <= this.limit || oldest === key) break;
|
|
431
|
-
this.#map.delete(oldest);
|
|
432
|
-
this.#total -= this.size(old);
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
delete(key: K): void {
|
|
437
|
-
if (!this.#map.has(key)) return;
|
|
438
|
-
this.#total -= this.size(this.#map.get(key) as V);
|
|
439
|
-
this.#map.delete(key);
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
clear(): void {
|
|
443
|
-
this.#map.clear();
|
|
444
|
-
this.#total = 0;
|
|
445
|
-
}
|
|
446
|
-
}
|