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