@decocms/blocks 8.1.0-next.5 → 8.1.0-next.7
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/protocol/client.d.ts +0 -2
- package/dist/protocol/client.d.ts.map +1 -1
- package/dist/protocol/client.js +0 -2
- package/dist/protocol/client.js.map +1 -1
- package/dist/protocol/conformance/cases/guards.d.ts.map +1 -1
- package/dist/protocol/conformance/cases/guards.js +10 -173
- package/dist/protocol/conformance/cases/guards.js.map +1 -1
- package/dist/protocol/conformance/cases/reads.d.ts.map +1 -1
- package/dist/protocol/conformance/cases/reads.js +4 -49
- 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 +3 -21
- package/dist/protocol/conformance/cases/writes.js.map +1 -1
- package/dist/protocol/conformance/context.d.ts +1 -26
- package/dist/protocol/conformance/context.d.ts.map +1 -1
- package/dist/protocol/conformance/context.js +4 -13
- package/dist/protocol/conformance/context.js.map +1 -1
- package/dist/protocol/conformance/index.js +3 -3
- package/dist/protocol/errors.d.ts +5 -9
- package/dist/protocol/errors.d.ts.map +1 -1
- package/dist/protocol/errors.js +4 -4
- package/dist/protocol/errors.js.map +1 -1
- package/dist/protocol/index.d.ts +1 -1
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +1 -1
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/params.d.ts.map +1 -1
- package/dist/protocol/params.js +0 -4
- package/dist/protocol/params.js.map +1 -1
- package/dist/protocol/server/assets.d.ts +2 -3
- package/dist/protocol/server/assets.d.ts.map +1 -1
- package/dist/protocol/server/assets.js +2 -9
- package/dist/protocol/server/assets.js.map +1 -1
- package/dist/protocol/server/content.d.ts +2 -3
- package/dist/protocol/server/content.d.ts.map +1 -1
- package/dist/protocol/server/content.js +10 -22
- package/dist/protocol/server/content.js.map +1 -1
- package/dist/protocol/server/core.d.ts +8 -11
- package/dist/protocol/server/core.d.ts.map +1 -1
- package/dist/protocol/server/core.js +8 -24
- package/dist/protocol/server/core.js.map +1 -1
- package/dist/protocol/server/handler.d.ts.map +1 -1
- package/dist/protocol/server/handler.js +11 -23
- package/dist/protocol/server/handler.js.map +1 -1
- package/dist/protocol/server/http.d.ts +6 -4
- package/dist/protocol/server/http.d.ts.map +1 -1
- package/dist/protocol/server/http.js +1 -18
- package/dist/protocol/server/http.js.map +1 -1
- package/dist/protocol/server/index.d.ts +0 -1
- package/dist/protocol/server/index.d.ts.map +1 -1
- package/dist/protocol/server/index.js.map +1 -1
- package/dist/protocol/server/methods/apply.d.ts +2 -2
- package/dist/protocol/server/methods/apply.d.ts.map +1 -1
- package/dist/protocol/server/methods/apply.js +23 -71
- package/dist/protocol/server/methods/apply.js.map +1 -1
- package/dist/protocol/server/methods/read.d.ts +0 -6
- package/dist/protocol/server/methods/read.d.ts.map +1 -1
- package/dist/protocol/server/methods/read.js +4 -31
- package/dist/protocol/server/methods/read.js.map +1 -1
- package/dist/protocol/server/rpc.d.ts +1 -1
- package/dist/protocol/server/rpc.d.ts.map +1 -1
- package/dist/protocol/server/rpc.js +9 -39
- package/dist/protocol/server/rpc.js.map +1 -1
- package/dist/protocol/storage/fs/index.d.ts +0 -3
- package/dist/protocol/storage/fs/index.d.ts.map +1 -1
- package/dist/protocol/storage/fs/index.js +0 -5
- package/dist/protocol/storage/fs/index.js.map +1 -1
- package/dist/protocol/storage/memory.d.ts +1 -6
- package/dist/protocol/storage/memory.d.ts.map +1 -1
- package/dist/protocol/storage/memory.js +1 -30
- package/dist/protocol/storage/memory.js.map +1 -1
- package/dist/protocol/storage.d.ts +7 -50
- package/dist/protocol/storage.d.ts.map +1 -1
- package/dist/protocol/storage.js +1 -1
- package/dist/protocol/storage.js.map +1 -1
- package/dist/protocol/types.d.ts +0 -34
- package/dist/protocol/types.d.ts.map +1 -1
- package/dist/protocol/types.js +0 -10
- package/dist/protocol/types.js.map +1 -1
- package/dist/v8/canonical.d.ts +0 -7
- package/dist/v8/canonical.d.ts.map +1 -1
- package/dist/v8/canonical.js +0 -9
- package/dist/v8/canonical.js.map +1 -1
- package/dist/v8/cli/content.d.ts +24 -3
- package/dist/v8/cli/content.d.ts.map +1 -1
- package/dist/v8/cli/content.js +70 -5
- package/dist/v8/cli/content.js.map +1 -1
- package/dist/v8/cli/run.js +1 -1
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +18 -25
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +5 -7
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +47 -45
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/draft.d.ts +1 -1
- package/dist/v8/draft.js +2 -2
- package/dist/v8/draft.js.map +1 -1
- package/dist/v8/draftChanges.d.ts +52 -11
- package/dist/v8/draftChanges.d.ts.map +1 -1
- package/dist/v8/draftChanges.js +115 -38
- package/dist/v8/draftChanges.js.map +1 -1
- package/dist/v8/identity.d.ts +0 -2
- package/dist/v8/identity.d.ts.map +1 -1
- package/dist/v8/identity.js +0 -10
- package/dist/v8/identity.js.map +1 -1
- package/dist/v8/remoteLoader.d.ts +2 -4
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +106 -87
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/settings.d.ts +0 -7
- package/dist/v8/settings.d.ts.map +1 -1
- package/dist/v8/settings.js +0 -10
- package/dist/v8/settings.js.map +1 -1
- package/dist/v8/telemetry.d.ts +7 -3
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +28 -69
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +46 -18
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/protocol/client.ts +0 -3
- package/src/protocol/conformance/cases/guards.ts +10 -188
- package/src/protocol/conformance/cases/reads.ts +7 -65
- package/src/protocol/conformance/cases/writes.ts +7 -36
- package/src/protocol/conformance/context.ts +6 -31
- package/src/protocol/conformance/index.ts +3 -3
- package/src/protocol/errors.ts +5 -10
- package/src/protocol/index.ts +0 -2
- package/src/protocol/params.ts +0 -4
- package/src/protocol/server/assets.ts +2 -12
- package/src/protocol/server/content.ts +10 -24
- package/src/protocol/server/core.ts +12 -34
- package/src/protocol/server/handler.ts +11 -30
- package/src/protocol/server/http.ts +1 -19
- package/src/protocol/server/index.ts +0 -1
- package/src/protocol/server/methods/apply.ts +22 -82
- package/src/protocol/server/methods/read.ts +4 -36
- package/src/protocol/server/rpc.ts +7 -47
- package/src/protocol/storage/fs/index.ts +0 -8
- package/src/protocol/storage/memory.ts +1 -36
- package/src/protocol/storage.ts +7 -39
- package/src/protocol/types.ts +0 -37
- package/src/v8/canonical.ts +0 -11
- package/src/v8/cli/content.ts +85 -5
- package/src/v8/cli/run.ts +1 -1
- package/src/v8/cms.ts +18 -26
- package/src/v8/content.ts +55 -48
- package/src/v8/draft.ts +2 -2
- package/src/v8/draftChanges.ts +132 -41
- package/src/v8/identity.ts +0 -10
- package/src/v8/remoteLoader.ts +111 -106
- package/src/v8/settings.ts +1 -15
- package/src/v8/telemetry.ts +33 -69
- package/src/v8/types.ts +47 -16
- package/dist/protocol/server/auth.d.ts +0 -31
- package/dist/protocol/server/auth.d.ts.map +0 -1
- package/dist/protocol/server/auth.js +0 -32
- package/dist/protocol/server/auth.js.map +0 -1
- package/src/protocol/server/auth.ts +0 -49
package/src/v8/cms.ts
CHANGED
|
@@ -13,15 +13,15 @@ import { secretBlock } from "./builtins/secret.ts";
|
|
|
13
13
|
import { CMSClient } from "./client.ts";
|
|
14
14
|
import { ContentStore, isLoader, isSnapshot } from "./content.ts";
|
|
15
15
|
import { draftCookieFor, endsPreview, parseDraftPointer, readDraftPointer } from "./draft.ts";
|
|
16
|
+
import { parseDraftHosts } from "./draftChanges.ts";
|
|
16
17
|
import { allowsHost, type HostPattern, parseHostPattern } from "./hosts.ts";
|
|
17
|
-
import { clearGlobals, contentIdentity, fnv1a
|
|
18
|
+
import { clearGlobals, contentIdentity, fnv1a } from "./identity.ts";
|
|
18
19
|
import { isPlainObject } from "./json.ts";
|
|
19
20
|
import { remoteLoader, resetRemoteLoaders } from "./remoteLoader.ts";
|
|
20
21
|
import {
|
|
21
22
|
defaultSettings,
|
|
22
23
|
isStatic,
|
|
23
24
|
parseCodeHosts,
|
|
24
|
-
parseCodeSources,
|
|
25
25
|
readSettings,
|
|
26
26
|
type SettingsCaps,
|
|
27
27
|
} from "./settings.ts";
|
|
@@ -56,6 +56,7 @@ interface Fingerprint {
|
|
|
56
56
|
telemetry: string;
|
|
57
57
|
preview: string;
|
|
58
58
|
secrets: string;
|
|
59
|
+
token: string;
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
/**
|
|
@@ -73,8 +74,6 @@ class CMSInstance {
|
|
|
73
74
|
readonly #builtIns: Readonly<Blocks>;
|
|
74
75
|
readonly #telemetry: TelemetryPipeline | undefined;
|
|
75
76
|
readonly #caps: SettingsCaps;
|
|
76
|
-
/** The hosts a draft pointer may name (`preview.sources`): code only, never content. */
|
|
77
|
-
readonly #sources: readonly HostPattern[];
|
|
78
77
|
/** Settings of a `CMS` block with nothing to run, per release snapshot. */
|
|
79
78
|
#staticSettings = new WeakMap<Snapshot, EffectiveSettings>();
|
|
80
79
|
/** The release and time telemetry last read its section. */
|
|
@@ -90,14 +89,13 @@ class CMSInstance {
|
|
|
90
89
|
constructor(config: CMSConfig, interval: number) {
|
|
91
90
|
this.config = config;
|
|
92
91
|
this.#interval = interval;
|
|
93
|
-
this.#store = new ContentStore(contentOf(config));
|
|
92
|
+
this.#store = new ContentStore(contentOf(config), parseDraftHosts(config.preview));
|
|
94
93
|
this.fingerprint = fingerprintOf(config, interval);
|
|
95
94
|
this.#caps = {
|
|
96
95
|
hosts: parseCodeHosts(config.preview),
|
|
97
96
|
limits: telemetryLimits(config.telemetry),
|
|
98
97
|
};
|
|
99
|
-
|
|
100
|
-
const destination = resolveDestination(config.telemetry, config.site);
|
|
98
|
+
const destination = resolveDestination(config.telemetry, config.site, config.token);
|
|
101
99
|
if (destination !== null) {
|
|
102
100
|
this.#telemetry = new TelemetryPipeline(destination);
|
|
103
101
|
setCurrentTelemetry(this.#telemetry);
|
|
@@ -151,16 +149,12 @@ class CMSInstance {
|
|
|
151
149
|
*/
|
|
152
150
|
forDraft(blocks: Blocks, pointer: string): Client {
|
|
153
151
|
this.#scheduleUpdate();
|
|
154
|
-
const load = () => this.#store.draft(pointer
|
|
152
|
+
const load = () => this.#store.draft(pointer);
|
|
155
153
|
const variants = parseDraftPointer(pointer)?.variants;
|
|
156
154
|
if (variants === undefined) return this.#client(blocks, load);
|
|
157
155
|
return this.#client(blocks, () => load().then((snapshot) => forceVariants(snapshot, variants)));
|
|
158
156
|
}
|
|
159
157
|
|
|
160
|
-
forRevision(blocks: Blocks, revision: string): Client {
|
|
161
|
-
return this.#client(blocks, () => this.#store.revision(revision));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
158
|
update(): Promise<{ updated: boolean }> {
|
|
165
159
|
this.#nextCheck = this.#due();
|
|
166
160
|
return this.#store.update();
|
|
@@ -306,10 +300,6 @@ class CMSHandle implements CMS {
|
|
|
306
300
|
return this.#instance.forDraft(this.#blocks, pointer);
|
|
307
301
|
}
|
|
308
302
|
|
|
309
|
-
forRevision(revision: string): Client {
|
|
310
|
-
return this.#instance.forRevision(this.#blocks, revision);
|
|
311
|
-
}
|
|
312
|
-
|
|
313
303
|
update(): Promise<{ updated: boolean }> {
|
|
314
304
|
return this.#instance.update();
|
|
315
305
|
}
|
|
@@ -369,10 +359,10 @@ export function resetForTests(): void {
|
|
|
369
359
|
setCurrentTelemetry(undefined);
|
|
370
360
|
}
|
|
371
361
|
|
|
372
|
-
/** With `site
|
|
362
|
+
/** With `site`, the content is the fallback of hosted releases. */
|
|
373
363
|
function contentOf(config: CMSConfig): Snapshot | Loader {
|
|
374
|
-
if (!config.site
|
|
375
|
-
return remoteLoader(config.content, { site: config.site
|
|
364
|
+
if (!config.site) return config.content;
|
|
365
|
+
return remoteLoader(config.content, { site: config.site });
|
|
376
366
|
}
|
|
377
367
|
|
|
378
368
|
function validate(config: CMSConfig): void {
|
|
@@ -387,13 +377,16 @@ function validate(config: CMSConfig): void {
|
|
|
387
377
|
"createCMS: `content` must be the content module ({ revision, blocks }) or a loader with load()",
|
|
388
378
|
);
|
|
389
379
|
}
|
|
380
|
+
// The token is the site's credential: it means nothing without the site it belongs to.
|
|
381
|
+
if (config.token && !config.site) {
|
|
382
|
+
throw new TypeError("createCMS: token needs site: pass both, or site alone");
|
|
383
|
+
}
|
|
390
384
|
parseCodeHosts(config.preview);
|
|
391
|
-
|
|
385
|
+
parseDraftHosts(config.preview);
|
|
392
386
|
}
|
|
393
387
|
|
|
394
388
|
function resolveInterval(configured: number | undefined): number {
|
|
395
|
-
const
|
|
396
|
-
const raw = configured ?? (env ? Number(env) : MIN_INTERVAL);
|
|
389
|
+
const raw = configured ?? MIN_INTERVAL;
|
|
397
390
|
if (!Number.isFinite(raw)) return MIN_INTERVAL;
|
|
398
391
|
if (raw < MIN_INTERVAL) {
|
|
399
392
|
console.warn(
|
|
@@ -406,8 +399,7 @@ function resolveInterval(configured: number | undefined): number {
|
|
|
406
399
|
|
|
407
400
|
/**
|
|
408
401
|
* The instance key: the content's identity (never its revision, so a hot
|
|
409
|
-
* reload keeps the instance) and, with
|
|
410
|
-
* hash of the token, which never appears in the global symbol registry.
|
|
402
|
+
* reload keeps the instance) and, with hosted releases, the site.
|
|
411
403
|
*
|
|
412
404
|
* A content module is identified by the `.deco` folder it was generated from
|
|
413
405
|
* (its `root`). One without a `root` is identified by the object itself, so
|
|
@@ -416,8 +408,7 @@ function resolveInterval(configured: number | undefined): number {
|
|
|
416
408
|
*/
|
|
417
409
|
function identityOf(config: CMSConfig): string {
|
|
418
410
|
const content = contentIdentity(config.content);
|
|
419
|
-
|
|
420
|
-
return `${content}|site:${config.site}|token:${fnv1a(config.token)}`;
|
|
411
|
+
return config.site ? `${content}|site:${config.site}` : content;
|
|
421
412
|
}
|
|
422
413
|
|
|
423
414
|
function fingerprintOf(config: CMSConfig, interval: number): Fingerprint {
|
|
@@ -426,6 +417,7 @@ function fingerprintOf(config: CMSConfig, interval: number): Fingerprint {
|
|
|
426
417
|
telemetry: stableJson(config.telemetry ?? null),
|
|
427
418
|
preview: stableJson(config.preview ?? null),
|
|
428
419
|
secrets: config.secrets?.key ? fnv1a(config.secrets.key) : "",
|
|
420
|
+
token: config.token ? fnv1a(config.token) : "",
|
|
429
421
|
};
|
|
430
422
|
}
|
|
431
423
|
|
package/src/v8/content.ts
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The content side of a CMS: one source (the content module or a `Loader`)
|
|
3
|
-
* the caches every client shares
|
|
4
|
-
*
|
|
2
|
+
* The content side of a CMS: one source (the content module or a `Loader`)
|
|
3
|
+
* and the caches every client shares. Clients only ever see whole
|
|
4
|
+
* `{ revision, blocks }` snapshots.
|
|
5
5
|
*
|
|
6
6
|
* A draft is the release with a draft's changes layered over it (see
|
|
7
|
-
* ./draftChanges.ts).
|
|
8
|
-
* `forRevision(revision)` takes a revision string a client may hand back, so
|
|
9
|
-
* it must only ever reach published content, never a draft someone loaded
|
|
10
|
-
* with a pointer.
|
|
7
|
+
* ./draftChanges.ts).
|
|
11
8
|
*/
|
|
12
9
|
import { formatDraftPointer, parseDraftPointer } from "./draft.ts";
|
|
13
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
DEFAULT_DRAFT_HOSTS,
|
|
12
|
+
type DraftChanges,
|
|
13
|
+
fetchDraftChanges,
|
|
14
|
+
LOCAL_VERSION,
|
|
15
|
+
layerDraft,
|
|
16
|
+
} from "./draftChanges.ts";
|
|
14
17
|
import { errors, isResolutionError } from "./errors.ts";
|
|
15
|
-
import type { HostPattern } from "./hosts.ts";
|
|
16
18
|
import { isPlainObject } from "./json.ts";
|
|
17
19
|
import type { Loader, Snapshot } from "./types.ts";
|
|
18
20
|
|
|
19
|
-
const SERVED_REVISIONS = 16;
|
|
20
21
|
/** Fetched drafts kept per CMS. */
|
|
21
22
|
const CACHED_DRAFTS = 3;
|
|
22
|
-
/** How long a fetched draft is reused before it's fetched again, so its token is checked again. */
|
|
23
|
-
const DRAFT_TTL_MS = 60_000;
|
|
24
23
|
|
|
25
|
-
/** A fetched draft, and the views of it over each release it was layered on. */
|
|
24
|
+
/** A fetched draft body, its ETag, and the views of it over each release it was layered on. */
|
|
26
25
|
interface CachedDraft {
|
|
27
|
-
changes:
|
|
28
|
-
|
|
26
|
+
changes: DraftChanges;
|
|
27
|
+
etag: string | null;
|
|
29
28
|
views: WeakMap<Snapshot, Snapshot>;
|
|
30
29
|
}
|
|
31
30
|
|
|
@@ -56,7 +55,9 @@ export function isSnapshot(content: unknown): content is Snapshot {
|
|
|
56
55
|
isPlainObject(content) &&
|
|
57
56
|
typeof content.revision === "string" &&
|
|
58
57
|
isPlainObject(content.blocks) &&
|
|
59
|
-
(content.aliases === undefined || isPlainObject(content.aliases))
|
|
58
|
+
(content.aliases === undefined || isPlainObject(content.aliases)) &&
|
|
59
|
+
(content.schemaHash === undefined || typeof content.schemaHash === "string") &&
|
|
60
|
+
(content.committedAt === undefined || typeof content.committedAt === "string")
|
|
60
61
|
);
|
|
61
62
|
}
|
|
62
63
|
|
|
@@ -65,12 +66,15 @@ export class ContentStore {
|
|
|
65
66
|
#release: Promise<Snapshot> | undefined;
|
|
66
67
|
#updating: Promise<{ updated: boolean }> | undefined;
|
|
67
68
|
readonly #drafts = new BoundedMap<string, CachedDraft>(CACHED_DRAFTS);
|
|
68
|
-
|
|
69
|
+
/** Draft reads in flight, so concurrent renders of one draft share one request. */
|
|
70
|
+
readonly #draftReads = new Map<string, Promise<CachedDraft>>();
|
|
71
|
+
readonly #draftHosts: readonly string[];
|
|
69
72
|
/** The release this store last handed a client, for a loader that can't peek. */
|
|
70
73
|
#latest: Snapshot | undefined;
|
|
71
74
|
|
|
72
|
-
constructor(source: Snapshot | Loader) {
|
|
75
|
+
constructor(source: Snapshot | Loader, draftHosts: readonly string[] = DEFAULT_DRAFT_HOSTS) {
|
|
73
76
|
this.#source = source;
|
|
77
|
+
this.#draftHosts = draftHosts;
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
/** Whether the source can change while the process runs (has `update()`). */
|
|
@@ -99,14 +103,15 @@ export class ContentStore {
|
|
|
99
103
|
}
|
|
100
104
|
|
|
101
105
|
/**
|
|
102
|
-
* The draft a pointer names: its changes, fetched from a host
|
|
106
|
+
* The draft a pointer names: its changes, fetched from a draft host,
|
|
103
107
|
* layered over the release. A pointer that doesn't parse, or any failure, is
|
|
104
108
|
* `LOADER_FAILED`, never a silent fallback to the release. A pointer whose
|
|
105
109
|
* version is `local` names no draft: the release, with nothing fetched. The
|
|
106
110
|
* changes are keyed by the pointer without its `__variant` parameters (every
|
|
107
|
-
* variant of one draft shares one
|
|
111
|
+
* variant of one draft shares one body) and revalidated on every read: a
|
|
112
|
+
* `304` reuses the body held, a `200` replaces it.
|
|
108
113
|
*/
|
|
109
|
-
draft(pointer: string
|
|
114
|
+
draft(pointer: string): Promise<Snapshot> {
|
|
110
115
|
const parsed = parseDraftPointer(pointer);
|
|
111
116
|
if (parsed === null) {
|
|
112
117
|
return Promise.reject(errors.loaderFailed(`invalid draft pointer "${truncate(pointer)}"`));
|
|
@@ -114,28 +119,38 @@ export class ContentStore {
|
|
|
114
119
|
if (parsed.version === LOCAL_VERSION) return this.release();
|
|
115
120
|
const { host, path, version } = parsed;
|
|
116
121
|
const key = formatDraftPointer({ host, path, version });
|
|
117
|
-
let
|
|
118
|
-
if (
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
let read = this.#draftReads.get(key);
|
|
123
|
+
if (read === undefined) {
|
|
124
|
+
const held = this.#drafts.get(key);
|
|
125
|
+
read = fetchDraftChanges(parsed, { domains: this.#draftHosts, etag: held?.etag }).then(
|
|
126
|
+
(result) => {
|
|
127
|
+
if (result.status === 304 && held !== undefined) return held;
|
|
128
|
+
if (result.status === 304) throw new Error("draft changes: 304 without a held body");
|
|
129
|
+
const entry: CachedDraft = {
|
|
130
|
+
changes: result.changes,
|
|
131
|
+
etag: result.etag,
|
|
132
|
+
views: new WeakMap(),
|
|
133
|
+
};
|
|
134
|
+
this.#drafts.set(key, entry);
|
|
135
|
+
return entry;
|
|
136
|
+
},
|
|
137
|
+
);
|
|
138
|
+
const pending = read;
|
|
139
|
+
this.#draftReads.set(key, pending);
|
|
140
|
+
const settle = () => {
|
|
141
|
+
if (this.#draftReads.get(key) === pending) this.#draftReads.delete(key);
|
|
125
142
|
};
|
|
126
|
-
|
|
127
|
-
// A failure isn't reused: the next client fetches again.
|
|
128
|
-
fetched.changes.catch(() => {
|
|
129
|
-
if (this.#drafts.get(key) === fetched) this.#drafts.delete(key);
|
|
130
|
-
});
|
|
131
|
-
entry = fetched;
|
|
143
|
+
pending.then(settle, settle);
|
|
132
144
|
}
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
145
|
+
const changes = read.catch((error: unknown) => {
|
|
146
|
+
throw errors.loaderFailed("the draft's changes couldn't be fetched", error);
|
|
147
|
+
});
|
|
148
|
+
return Promise.all([this.release(), changes]).then(([base, entry]) => {
|
|
149
|
+
let view = entry.views.get(base);
|
|
136
150
|
if (view === undefined) {
|
|
137
|
-
view
|
|
138
|
-
|
|
151
|
+
// OPEN: the view is keyed by the body's ETag (the pointer outlives later saves); the version without one.
|
|
152
|
+
view = layerDraft(base, entry.changes, entry.etag ?? version);
|
|
153
|
+
entry.views.set(base, view);
|
|
139
154
|
}
|
|
140
155
|
return view;
|
|
141
156
|
});
|
|
@@ -150,12 +165,6 @@ export class ContentStore {
|
|
|
150
165
|
return peekRelease(this.#source) ?? this.#latest;
|
|
151
166
|
}
|
|
152
167
|
|
|
153
|
-
/** A release revision this store has served, or the release when it's unknown (drafts included). */
|
|
154
|
-
revision(revision: string): Promise<Snapshot> {
|
|
155
|
-
const served = this.#served.get(revision);
|
|
156
|
-
return served === undefined ? this.release() : Promise.resolve(served);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
168
|
/** Asks the source for newer content; never throws, and concurrent calls share one check. */
|
|
160
169
|
update(): Promise<{ updated: boolean }> {
|
|
161
170
|
const source = this.#source;
|
|
@@ -198,7 +207,6 @@ export class ContentStore {
|
|
|
198
207
|
this.#drafts.clear();
|
|
199
208
|
return;
|
|
200
209
|
}
|
|
201
|
-
if (isSnapshot(previous)) this.#served.delete(previous.revision);
|
|
202
210
|
this.#source = source;
|
|
203
211
|
this.#release = undefined;
|
|
204
212
|
this.#latest = undefined;
|
|
@@ -221,7 +229,6 @@ export class ContentStore {
|
|
|
221
229
|
|
|
222
230
|
#serve(snapshot: Snapshot): Snapshot {
|
|
223
231
|
this.#latest = snapshot;
|
|
224
|
-
this.#served.set(snapshot.revision, snapshot);
|
|
225
232
|
return snapshot;
|
|
226
233
|
}
|
|
227
234
|
}
|
package/src/v8/draft.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
import type { DraftPointer, ForcedVariant, RequestLike } from "./types.ts";
|
|
13
13
|
|
|
14
14
|
/** The draft cookie's name. Not exported from the package: reading it directly would skip the host check. */
|
|
15
|
-
const DRAFT_COOKIE = "
|
|
15
|
+
const DRAFT_COOKIE = "__deco_draft";
|
|
16
16
|
|
|
17
17
|
const DRAFT_PARAM = "__draft";
|
|
18
18
|
const DRAFT_OFF = "off";
|
|
@@ -157,7 +157,7 @@ function normalizeIPv6Host(authority: string): string | null {
|
|
|
157
157
|
|
|
158
158
|
/**
|
|
159
159
|
* The draft pointer a request carries: `?__draft=` from the URL first, then
|
|
160
|
-
* the `
|
|
160
|
+
* the `__deco_draft` cookie. `null` when neither is present, or when the URL
|
|
161
161
|
* says `?__draft=off`. The value is returned as is; `cms.forDraft` validates it.
|
|
162
162
|
*/
|
|
163
163
|
export function readDraftPointer(request: RequestLike): string | null {
|
package/src/v8/draftChanges.ts
CHANGED
|
@@ -2,76 +2,175 @@
|
|
|
2
2
|
* Draft changes (see /next/content-delivery#draft-previews): what a draft
|
|
3
3
|
* pointer's address answers, and how the CMS layers it over production.
|
|
4
4
|
*
|
|
5
|
-
* - The pointer's host must
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* - The pointer's host must fall under one of the draft hosts (the same rule
|
|
6
|
+
* as v7's preview API domains): by default `*.decocms.com` (Studio, the
|
|
7
|
+
* delivery CDN, `local.studio.decocms.com`) and the loopback hosts;
|
|
8
|
+
* `createCMS({ preview: { draftHosts } })` replaces the list. Any other host
|
|
9
|
+
* is refused before anything is fetched.
|
|
10
|
+
* - `GET <scheme>://<host><path>?v=<version>`: the scheme comes from the
|
|
11
|
+
* domain that admitted the host, never from the pointer: plain `http` only
|
|
12
|
+
* for loopback hosts. The pointer's `__variant` parameters are already gone (the parser
|
|
9
13
|
* lifts them out), no cookies or credentials are sent, and redirects aren't
|
|
10
14
|
* followed (a response that was redirected anyway is refused).
|
|
11
|
-
* -
|
|
12
|
-
* `
|
|
13
|
-
*
|
|
15
|
+
* - Every read revalidates: with a body this process already holds, it sends
|
|
16
|
+
* `If-None-Match` with that body's ETag, and a `304` reuses the body. Only
|
|
17
|
+
* a 200 or that 304 is accepted, within 10 s; a 200 is up to 16 MiB, shaped
|
|
18
|
+
* exactly `{ set: { <name>: <block JSON> }, delete: [<name>…] }` with `set`
|
|
19
|
+
* and `delete` disjoint.
|
|
14
20
|
*/
|
|
15
21
|
import { readBoundedJson, timeoutSignal } from "./boundedJson.ts";
|
|
16
|
-
import { allowsHost, type HostPattern } from "./hosts.ts";
|
|
17
22
|
import { isPlainObject } from "./json.ts";
|
|
18
23
|
import type { DraftPointer, Snapshot } from "./types.ts";
|
|
19
24
|
|
|
20
25
|
/** The version a pointer has when it names no draft: `deco serve`'s working tree. */
|
|
21
26
|
export const LOCAL_VERSION = "local";
|
|
22
27
|
|
|
23
|
-
const FORMAT = 1;
|
|
24
28
|
const TIMEOUT_MS = 10_000;
|
|
25
29
|
const MAX_BYTES = 16 * 1024 * 1024;
|
|
26
30
|
|
|
27
31
|
/** What a draft changed compared with production: whole blocks, and deletions. */
|
|
28
32
|
export interface DraftChanges {
|
|
29
|
-
format: typeof FORMAT;
|
|
30
33
|
set: Record<string, unknown>;
|
|
31
34
|
delete: string[];
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
/**
|
|
35
|
-
export
|
|
36
|
-
|
|
37
|
+
/** A draft read: new changes (and their ETag), or `304`, the held body is current. */
|
|
38
|
+
export type DraftRead =
|
|
39
|
+
| { status: 200; changes: DraftChanges; etag: string | null }
|
|
40
|
+
| { status: 304 };
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The domains a draft pointer's host may fall under, the defaults v7 shipped.
|
|
44
|
+
* Deco operates every one of them, so the defaults add no SSRF surface. An
|
|
45
|
+
* entry starting with a dot matches any host that ends with it, on a label
|
|
46
|
+
* boundary (`evil-decocms.com` doesn't pass `.decocms.com`); any other entry
|
|
47
|
+
* matches that exact host. The first entry that matches decides whether a
|
|
48
|
+
* port and plain `http` are allowed.
|
|
49
|
+
*/
|
|
50
|
+
export const DEFAULT_DRAFT_HOSTS: readonly string[] = [
|
|
51
|
+
"local.studio.decocms.com", // the Studio dev origin (https, with a port)
|
|
52
|
+
"localhost",
|
|
53
|
+
"127.0.0.1",
|
|
54
|
+
".localhost",
|
|
55
|
+
".decocms.com", // Studio, its preview deployments and delivery.decocms.com
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* `createCMS`'s `preview.draftHosts`, trimmed and lowercased, or the defaults
|
|
60
|
+
* when it's left out. Throws a `TypeError` on anything but a list of
|
|
61
|
+
* non-empty strings.
|
|
62
|
+
*/
|
|
63
|
+
export function parseDraftHosts(preview: { draftHosts?: unknown } | undefined): readonly string[] {
|
|
64
|
+
const domains = preview?.draftHosts;
|
|
65
|
+
if (domains === undefined) return DEFAULT_DRAFT_HOSTS;
|
|
66
|
+
if (
|
|
67
|
+
!Array.isArray(domains) ||
|
|
68
|
+
domains.some((domain) => typeof domain !== "string" || domain.trim() === "")
|
|
69
|
+
) {
|
|
70
|
+
throw new TypeError("createCMS: `preview.draftHosts` must be a list of hosts");
|
|
71
|
+
}
|
|
72
|
+
return domains.map((domain: string) => domain.trim().toLowerCase());
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The origin a pointer's host is fetched from, or `null` when no draft host
|
|
77
|
+
* admits it. Loopback domains (and `local.studio.decocms.com`) may
|
|
78
|
+
* carry a port; a public domain may not, so a pointer can't aim the fetch at
|
|
79
|
+
* an odd port. Loopback hosts are `http`; everything else is `https`.
|
|
80
|
+
*/
|
|
81
|
+
export function previewApiOrigin(
|
|
82
|
+
authority: string,
|
|
83
|
+
domains: readonly string[] = DEFAULT_DRAFT_HOSTS,
|
|
84
|
+
): string | null {
|
|
85
|
+
const lower = authority.toLowerCase();
|
|
86
|
+
const end = lower.startsWith("[") ? lower.indexOf("]") + 1 : -1;
|
|
87
|
+
const host = end > 0 ? lower.slice(0, end) : lower.split(":")[0]!;
|
|
88
|
+
const rest = end > 0 ? lower.slice(end) : lower.slice(host.length);
|
|
89
|
+
const port = rest.startsWith(":") ? rest.slice(1) : undefined;
|
|
90
|
+
if (!host) return null;
|
|
91
|
+
const domain = domains.find((d) =>
|
|
92
|
+
d.startsWith(".") ? host.length > d.length && host.endsWith(d) : host === d,
|
|
93
|
+
);
|
|
94
|
+
if (domain === undefined) return null;
|
|
95
|
+
const insecure = isLoopbackDomain(domain);
|
|
96
|
+
if (port !== undefined && !insecure && domain !== "local.studio.decocms.com") return null;
|
|
97
|
+
return `${insecure ? "http" : "https"}://${host}${port === undefined ? "" : `:${port}`}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function isLoopbackDomain(domain: string): boolean {
|
|
101
|
+
return (
|
|
102
|
+
domain === "localhost" ||
|
|
103
|
+
domain === "127.0.0.1" ||
|
|
104
|
+
domain === "[::1]" ||
|
|
105
|
+
domain.endsWith(".localhost")
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The URL the SDK fetches for a pointer: the origin its host is admitted
|
|
111
|
+
* under, the version as `v`. `null` when no draft host admits the host.
|
|
112
|
+
*/
|
|
113
|
+
export function draftChangesUrl(
|
|
114
|
+
pointer: DraftPointer,
|
|
115
|
+
domains: readonly string[] = DEFAULT_DRAFT_HOSTS,
|
|
116
|
+
): string | null {
|
|
117
|
+
const origin = previewApiOrigin(pointer.host, domains);
|
|
118
|
+
if (origin === null) return null;
|
|
37
119
|
const separator = pointer.path.includes("?") ? "&" : "?";
|
|
38
|
-
return `${
|
|
120
|
+
return `${origin}${pointer.path}${separator}v=${encodeURIComponent(pointer.version)}`;
|
|
39
121
|
}
|
|
40
122
|
|
|
41
|
-
/**
|
|
123
|
+
/**
|
|
124
|
+
* Reads a draft's changes, revalidating `etag` (the ETag of the body the
|
|
125
|
+
* caller holds) when given. Rejects on a host no draft host admits
|
|
126
|
+
* (without fetching), on a `304` to a request that sent no ETag, and on any
|
|
127
|
+
* other failure.
|
|
128
|
+
*/
|
|
42
129
|
export async function fetchDraftChanges(
|
|
43
130
|
pointer: DraftPointer,
|
|
44
|
-
|
|
45
|
-
): Promise<
|
|
46
|
-
const url = draftChangesUrl(pointer);
|
|
47
|
-
if (
|
|
131
|
+
options: { domains?: readonly string[]; etag?: string | null } = {},
|
|
132
|
+
): Promise<DraftRead> {
|
|
133
|
+
const url = draftChangesUrl(pointer, options.domains);
|
|
134
|
+
if (url === null) {
|
|
48
135
|
throw new Error(
|
|
49
|
-
`draft pointer names "${pointer.host}", which isn't
|
|
136
|
+
`draft pointer names "${pointer.host}", which isn't under a draft host ` +
|
|
137
|
+
"(createCMS({ preview: { draftHosts } })); nothing was fetched",
|
|
50
138
|
);
|
|
51
139
|
}
|
|
140
|
+
const headers: Record<string, string> = { accept: "application/json" };
|
|
141
|
+
if (options.etag) headers["if-none-match"] = options.etag;
|
|
52
142
|
const response = await fetch(url, {
|
|
53
|
-
headers
|
|
143
|
+
headers,
|
|
54
144
|
redirect: "manual",
|
|
55
145
|
signal: timeoutSignal(TIMEOUT_MS),
|
|
56
146
|
});
|
|
57
147
|
// Some fetch polyfills (React Native, whatwg-fetch) ignore `redirect: "manual"`
|
|
58
148
|
// and follow anyway: refuse a response that came from anywhere else.
|
|
59
|
-
if (
|
|
149
|
+
if (
|
|
150
|
+
response.redirected ||
|
|
151
|
+
(response.url && new URL(response.url).origin !== new URL(url).origin)
|
|
152
|
+
) {
|
|
60
153
|
await response.body?.cancel();
|
|
61
154
|
throw new Error("draft changes: the pointer's address redirected; nothing was read");
|
|
62
155
|
}
|
|
156
|
+
if (response.status === 304 && options.etag) {
|
|
157
|
+
await response.body?.cancel();
|
|
158
|
+
return { status: 304 };
|
|
159
|
+
}
|
|
63
160
|
if (response.status !== 200) {
|
|
64
161
|
await response.body?.cancel();
|
|
65
162
|
throw new Error(`draft changes: HTTP ${response.status}`);
|
|
66
163
|
}
|
|
67
|
-
|
|
164
|
+
const etag = response.headers.get("etag");
|
|
165
|
+
const changes = parseDraftChanges(await readBoundedJson(response, "draft changes", MAX_BYTES));
|
|
166
|
+
return { status: 200, changes, etag };
|
|
68
167
|
}
|
|
69
168
|
|
|
70
|
-
/** Validates a draft changes response; throws on
|
|
169
|
+
/** Validates a draft changes response; throws on any unexpected shape. */
|
|
71
170
|
export function parseDraftChanges(body: unknown): DraftChanges {
|
|
72
171
|
const invalid = (why: string) => new Error(`draft changes: ${why}`);
|
|
73
|
-
if (!isPlainObject(body)
|
|
74
|
-
if (Object.keys(body).some((key) => key !== "
|
|
172
|
+
if (!isPlainObject(body)) throw invalid("expected an object");
|
|
173
|
+
if (Object.keys(body).some((key) => key !== "set" && key !== "delete")) {
|
|
75
174
|
throw invalid("unexpected field");
|
|
76
175
|
}
|
|
77
176
|
const { set, delete: deleted } = body;
|
|
@@ -79,7 +178,6 @@ export function parseDraftChanges(body: unknown): DraftChanges {
|
|
|
79
178
|
if (!Array.isArray(deleted) || deleted.some((name) => typeof name !== "string")) {
|
|
80
179
|
throw invalid("delete must be a list of names");
|
|
81
180
|
}
|
|
82
|
-
if (new Set(deleted).size !== deleted.length) throw invalid("delete repeats a name");
|
|
83
181
|
if (deleted.some((name) => Object.hasOwn(set, name))) throw invalid("set and delete overlap");
|
|
84
182
|
return body as unknown as DraftChanges;
|
|
85
183
|
}
|
|
@@ -88,9 +186,11 @@ export function parseDraftChanges(body: unknown): DraftChanges {
|
|
|
88
186
|
* The draft as a snapshot: production's entries, with the changed ones
|
|
89
187
|
* replaced whole and the deleted ones gone. A shallow copy: every unchanged
|
|
90
188
|
* entry is production's own object, and production is never mutated. Its
|
|
91
|
-
* revision is an opaque identity of the pair,
|
|
189
|
+
* revision, `<base revision>~<view>`, is an opaque identity of the pair,
|
|
190
|
+
* never a release's; `view` names the draft body (its ETag), so each save
|
|
191
|
+
* re-keys what's cached by revision.
|
|
92
192
|
*/
|
|
93
|
-
export function layerDraft(base: Snapshot, changes: DraftChanges,
|
|
193
|
+
export function layerDraft(base: Snapshot, changes: DraftChanges, view: string): Snapshot {
|
|
94
194
|
const blocks: Record<string, unknown> = { ...base.blocks };
|
|
95
195
|
for (const name of changes.delete) delete blocks[name];
|
|
96
196
|
for (const [name, entry] of Object.entries(changes.set)) {
|
|
@@ -102,16 +202,7 @@ export function layerDraft(base: Snapshot, changes: DraftChanges, version: strin
|
|
|
102
202
|
configurable: true,
|
|
103
203
|
});
|
|
104
204
|
}
|
|
105
|
-
const
|
|
106
|
-
if (base.aliases !== undefined)
|
|
107
|
-
return
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function isLoopback(authority: string): boolean {
|
|
111
|
-
const host = authority.startsWith("[")
|
|
112
|
-
? authority.slice(0, authority.indexOf("]") + 1)
|
|
113
|
-
: authority.split(":")[0]!;
|
|
114
|
-
return (
|
|
115
|
-
host === "localhost" || host.endsWith(".localhost") || host === "127.0.0.1" || host === "[::1]"
|
|
116
|
-
);
|
|
205
|
+
const layered: Snapshot = { revision: `${base.revision}~${view}`, blocks };
|
|
206
|
+
if (base.aliases !== undefined) layered.aliases = base.aliases;
|
|
207
|
+
return layered;
|
|
117
208
|
}
|
package/src/v8/identity.ts
CHANGED
|
@@ -46,16 +46,6 @@ export function fnv1a(input: string): string {
|
|
|
46
46
|
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
/** Reads an environment variable where there is one (Node, Bun, Workers with nodejs_compat). */
|
|
50
|
-
export function readEnv(name: string): string | undefined {
|
|
51
|
-
try {
|
|
52
|
-
return (globalThis as { process?: { env?: Record<string, string | undefined> } }).process
|
|
53
|
-
?.env?.[name];
|
|
54
|
-
} catch {
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
49
|
/** Clears every global instance whose registry key starts with `prefix`. */
|
|
60
50
|
export function clearGlobals(prefix: string): void {
|
|
61
51
|
const store = globalThis as unknown as Record<symbol, unknown>;
|