@decocms/blocks 8.1.0-next.2 → 8.1.0-next.4
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/conformance/contentHashFixtures.d.ts +29 -0
- package/dist/protocol/conformance/contentHashFixtures.d.ts.map +1 -1
- package/dist/protocol/conformance/contentHashFixtures.js +30 -0
- package/dist/protocol/conformance/contentHashFixtures.js.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 +1 -1
- package/dist/protocol/conformance/index.js.map +1 -1
- package/dist/protocol/index.d.ts +5 -2
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +5 -2
- 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/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 +28 -0
- package/dist/v8/canonical.d.ts.map +1 -1
- package/dist/v8/canonical.js +19 -0
- package/dist/v8/canonical.js.map +1 -1
- package/dist/v8/cli/check/index.d.ts.map +1 -1
- package/dist/v8/cli/check/index.js +13 -1
- 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 +2 -9
- 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.map +1 -1
- package/dist/v8/cli/schema/generate.js +2 -2
- package/dist/v8/cli/schema/generate.js.map +1 -1
- package/dist/v8/cli/schema/tsProgram.d.ts +16 -0
- package/dist/v8/cli/schema/tsProgram.d.ts.map +1 -1
- package/dist/v8/cli/schema/tsProgram.js +48 -0
- package/dist/v8/cli/schema/tsProgram.js.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.d.ts +13 -0
- package/dist/v8/cli/schema/typeToSchema.d.ts.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.js +85 -2
- package/dist/v8/cli/schema/typeToSchema.js.map +1 -1
- package/dist/v8/cli/serve/server.d.ts +0 -4
- package/dist/v8/cli/serve/server.d.ts.map +1 -1
- package/dist/v8/cli/serve/server.js +20 -46
- 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 +188 -20
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +32 -2
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +82 -19
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/draft.d.ts +13 -12
- package/dist/v8/draft.d.ts.map +1 -1
- package/dist/v8/draft.js +75 -9
- package/dist/v8/draft.js.map +1 -1
- 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.map +1 -1
- package/dist/v8/remoteLoader.js +222 -26
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/settings.d.ts +27 -0
- package/dist/v8/settings.d.ts.map +1 -0
- package/dist/v8/settings.js +130 -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 +69 -3
- package/dist/v8/types.d.ts.map +1 -1
- package/dist/v8/variants.d.ts +3 -0
- package/dist/v8/variants.d.ts.map +1 -0
- package/dist/v8/variants.js +65 -0
- package/dist/v8/variants.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +3 -3
- package/src/protocol/conformance/contentHashFixtures.ts +57 -0
- package/src/protocol/conformance/index.ts +6 -1
- package/src/protocol/index.ts +8 -1
- package/src/protocol/secrets.ts +10 -2
- package/src/protocol/server/assets.ts +1 -1
- package/src/v8/builtins/data.ts +56 -7
- package/src/v8/builtins/index.ts +2 -3
- package/src/v8/canonical.ts +36 -0
- package/src/v8/cli/check/index.ts +23 -1
- package/src/v8/cli/index.ts +2 -1
- package/src/v8/cli/run.ts +3 -8
- package/src/v8/cli/schema/builtinSchemas.ts +49 -28
- package/src/v8/cli/schema/generate.ts +2 -1
- package/src/v8/cli/schema/tsProgram.ts +44 -0
- package/src/v8/cli/schema/typeToSchema.ts +93 -2
- package/src/v8/cli/serve/server.ts +20 -55
- package/src/v8/cms.ts +235 -23
- package/src/v8/content.ts +84 -18
- package/src/v8/draft.ts +88 -15
- package/src/v8/hosts.ts +184 -0
- package/src/v8/index.ts +6 -8
- package/src/v8/remoteLoader.ts +258 -23
- package/src/v8/settings.ts +157 -0
- package/src/v8/telemetry.ts +43 -40
- package/src/v8/types.ts +62 -3
- package/src/v8/variants.ts +61 -0
package/src/v8/telemetry.ts
CHANGED
|
@@ -5,8 +5,10 @@
|
|
|
5
5
|
* - **Where** comes from code: `createCMS({ telemetry })`, or the standard
|
|
6
6
|
* `OTEL_EXPORTER_OTLP_ENDPOINT`/`OTEL_EXPORTER_OTLP_HEADERS` when it's left
|
|
7
7
|
* out. `false` sends nothing.
|
|
8
|
-
* - **How much** comes from content: the `
|
|
9
|
-
* release
|
|
8
|
+
* - **How much** comes from content: the `telemetry` section of the CMS
|
|
9
|
+
* settings (`cms.settings()`, the release's `CMS` block), capped by
|
|
10
|
+
* `telemetry.limits`. The CMS reads it outside any request, when the
|
|
11
|
+
* release changes and then at most once a minute.
|
|
10
12
|
* - Metrics are aggregated in memory (delta histograms); error logs and traces
|
|
11
13
|
* are sampled. Batches leave in the background (after the response on
|
|
12
14
|
* Workers, on an unref'd timer elsewhere); a send that fails is retried
|
|
@@ -15,10 +17,10 @@
|
|
|
15
17
|
* cookies or authorization values, and no value a secret block decrypted.
|
|
16
18
|
*/
|
|
17
19
|
import { hasBackgroundHook, later, runInBackground } from "./background.ts";
|
|
20
|
+
import { rate, TELEMETRY_DEFAULTS } from "./builtins/data.ts";
|
|
18
21
|
import { isResolutionError } from "./errors.ts";
|
|
19
22
|
import { readEnv } from "./identity.ts";
|
|
20
|
-
import {
|
|
21
|
-
import type { CMSError, Snapshot, TelemetryConfig } from "./types.ts";
|
|
23
|
+
import type { CMSError, Snapshot, Telemetry, TelemetryConfig } from "./types.ts";
|
|
22
24
|
|
|
23
25
|
/** The hosted Deco CMS collector, for `telemetry: { site, token }`. */
|
|
24
26
|
const HOSTED_TELEMETRY_ENDPOINT = "https://otel.decocms.com";
|
|
@@ -31,9 +33,20 @@ const MAX_SECRETS = 1_000;
|
|
|
31
33
|
const RETRY_STATUSES = new Set([429, 502, 503, 504]);
|
|
32
34
|
/** OpenTelemetry's recommended `http.*.request.duration` buckets, in seconds. */
|
|
33
35
|
const BOUNDS = [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10];
|
|
34
|
-
const DEFAULTS = { enabled: true, metrics: true, errorSampleRate: 0.05, traceSampleRate: 0 };
|
|
35
36
|
const DEFAULT_LIMITS = { errorSampleRate: 0.1, traceSampleRate: 0 };
|
|
36
37
|
|
|
38
|
+
/** The caps on content's sample rates: `telemetry.limits`, defaults 0.1 and 0. */
|
|
39
|
+
export function telemetryLimits(config: false | TelemetryConfig | undefined): {
|
|
40
|
+
errorSampleRate: number;
|
|
41
|
+
traceSampleRate: number;
|
|
42
|
+
} {
|
|
43
|
+
const limits = config ? config.limits : undefined;
|
|
44
|
+
return {
|
|
45
|
+
errorSampleRate: rate(limits?.errorSampleRate, DEFAULT_LIMITS.errorSampleRate),
|
|
46
|
+
traceSampleRate: rate(limits?.traceSampleRate, DEFAULT_LIMITS.traceSampleRate),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
37
50
|
type Attributes = Record<string, string | number | boolean | undefined>;
|
|
38
51
|
|
|
39
52
|
interface Destination {
|
|
@@ -92,10 +105,7 @@ export function resolveDestination(
|
|
|
92
105
|
const headers = parseKeyValues(readEnv("OTEL_EXPORTER_OTLP_HEADERS"));
|
|
93
106
|
return { endpoint, headers, site, limits: DEFAULT_LIMITS };
|
|
94
107
|
}
|
|
95
|
-
const limits =
|
|
96
|
-
errorSampleRate: rate(config.limits?.errorSampleRate, DEFAULT_LIMITS.errorSampleRate),
|
|
97
|
-
traceSampleRate: rate(config.limits?.traceSampleRate, DEFAULT_LIMITS.traceSampleRate),
|
|
98
|
-
};
|
|
108
|
+
const limits = telemetryLimits(config);
|
|
99
109
|
if ("endpoint" in config) {
|
|
100
110
|
if (typeof config.endpoint !== "string" || !config.endpoint) return null;
|
|
101
111
|
return { endpoint: config.endpoint, headers: { ...config.headers }, site, limits };
|
|
@@ -111,8 +121,7 @@ export function resolveDestination(
|
|
|
111
121
|
|
|
112
122
|
export class TelemetryPipeline {
|
|
113
123
|
readonly #destination: Destination;
|
|
114
|
-
#settings = { ...
|
|
115
|
-
#settingsRevision: string | undefined;
|
|
124
|
+
#settings: Required<Telemetry> = { ...TELEMETRY_DEFAULTS };
|
|
116
125
|
#release: string | undefined;
|
|
117
126
|
#histograms = new Map<string, Histogram>();
|
|
118
127
|
#logs: LogRecord[] = [];
|
|
@@ -124,7 +133,7 @@ export class TelemetryPipeline {
|
|
|
124
133
|
|
|
125
134
|
constructor(destination: Destination) {
|
|
126
135
|
this.#destination = destination;
|
|
127
|
-
this
|
|
136
|
+
this.apply(TELEMETRY_DEFAULTS);
|
|
128
137
|
}
|
|
129
138
|
|
|
130
139
|
/** Never send `value`: it's replaced with `[redacted]` in every log, span and label. */
|
|
@@ -132,13 +141,29 @@ export class TelemetryPipeline {
|
|
|
132
141
|
if (value.length > 0 && this.#secrets.size < MAX_SECRETS) this.#secrets.add(value);
|
|
133
142
|
}
|
|
134
143
|
|
|
135
|
-
/** The release being served: its revision labels batches
|
|
144
|
+
/** The release being served: its revision labels batches. */
|
|
136
145
|
useRelease(snapshot: Snapshot): void {
|
|
137
146
|
this.#release = snapshot.revision;
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The `telemetry` section to follow (from `cms.settings()`). Rates are
|
|
151
|
+
* capped by the destination's limits here too, whatever the caller passed.
|
|
152
|
+
*/
|
|
153
|
+
apply(section: Telemetry): void {
|
|
154
|
+
const { limits } = this.#destination;
|
|
155
|
+
this.#settings = {
|
|
156
|
+
enabled: typeof section.enabled === "boolean" ? section.enabled : TELEMETRY_DEFAULTS.enabled,
|
|
157
|
+
metrics: typeof section.metrics === "boolean" ? section.metrics : TELEMETRY_DEFAULTS.metrics,
|
|
158
|
+
errorSampleRate: Math.min(
|
|
159
|
+
rate(section.errorSampleRate, TELEMETRY_DEFAULTS.errorSampleRate),
|
|
160
|
+
limits.errorSampleRate,
|
|
161
|
+
),
|
|
162
|
+
traceSampleRate: Math.min(
|
|
163
|
+
rate(section.traceSampleRate, TELEMETRY_DEFAULTS.traceSampleRate),
|
|
164
|
+
limits.traceSampleRate,
|
|
165
|
+
),
|
|
166
|
+
};
|
|
142
167
|
}
|
|
143
168
|
|
|
144
169
|
histogram(name: string, attributes: Attributes, seconds: number): void {
|
|
@@ -188,7 +213,7 @@ export class TelemetryPipeline {
|
|
|
188
213
|
/**
|
|
189
214
|
* The hooks one client reports through. The trace decision is made once per
|
|
190
215
|
* client, on its first block, after its release is loaded, so the first
|
|
191
|
-
* client of a release already uses that release's `
|
|
216
|
+
* client of a release already uses that release's `telemetry` section.
|
|
192
217
|
*/
|
|
193
218
|
forClient(): ClientTelemetry {
|
|
194
219
|
let decided = false;
|
|
@@ -340,22 +365,6 @@ export class TelemetryPipeline {
|
|
|
340
365
|
return scrubText(out);
|
|
341
366
|
}
|
|
342
367
|
|
|
343
|
-
#apply(block: Record<string, unknown> | undefined): void {
|
|
344
|
-
const { limits } = this.#destination;
|
|
345
|
-
this.#settings = {
|
|
346
|
-
enabled: typeof block?.enabled === "boolean" ? block.enabled : DEFAULTS.enabled,
|
|
347
|
-
metrics: typeof block?.metrics === "boolean" ? block.metrics : DEFAULTS.metrics,
|
|
348
|
-
errorSampleRate: Math.min(
|
|
349
|
-
rate(block?.errorSampleRate, DEFAULTS.errorSampleRate),
|
|
350
|
-
limits.errorSampleRate,
|
|
351
|
-
),
|
|
352
|
-
traceSampleRate: Math.min(
|
|
353
|
-
rate(block?.traceSampleRate, DEFAULTS.traceSampleRate),
|
|
354
|
-
limits.traceSampleRate,
|
|
355
|
-
),
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
|
|
359
368
|
#schedule(): void {
|
|
360
369
|
if (this.#scheduled) return;
|
|
361
370
|
this.#scheduled = true;
|
|
@@ -483,12 +492,6 @@ function randomHex(bytes: number): string {
|
|
|
483
492
|
return Array.from(values, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
484
493
|
}
|
|
485
494
|
|
|
486
|
-
function rate(value: unknown, fallback: number): number {
|
|
487
|
-
return typeof value === "number" && Number.isFinite(value)
|
|
488
|
-
? Math.min(1, Math.max(0, value))
|
|
489
|
-
: fallback;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
495
|
/** `k1=v1,k2=v2` with URL-encoded values, the format of the OTEL_* environment variables. */
|
|
493
496
|
function parseKeyValues(raw: string | undefined): Record<string, string> {
|
|
494
497
|
const out: Record<string, string> = {};
|
package/src/v8/types.ts
CHANGED
|
@@ -47,10 +47,23 @@ export interface Loader {
|
|
|
47
47
|
export interface DraftPointer {
|
|
48
48
|
/** `host[:port]` of the content source that holds the draft. */
|
|
49
49
|
host: string;
|
|
50
|
-
/** Starts with `/`; opaque to the app. */
|
|
50
|
+
/** Starts with `/`; opaque to the app. Never carries the `__variant` parameters. */
|
|
51
51
|
path: string;
|
|
52
52
|
/** Opaque and immutable: the branch head or ETag. */
|
|
53
53
|
version: string;
|
|
54
|
+
/** The variants this preview forces (the query's `__variant` parameters); absent when none. */
|
|
55
|
+
variants?: ForcedVariant[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A variant a preview forces: the multivariate block at `path` (dot-separated
|
|
60
|
+
* keys and indexes, `""` for the saved block itself) inside the saved block
|
|
61
|
+
* `block` shows its variant `index` instead of evaluating rules.
|
|
62
|
+
*/
|
|
63
|
+
export interface ForcedVariant {
|
|
64
|
+
block: string;
|
|
65
|
+
path: string;
|
|
66
|
+
index: number;
|
|
54
67
|
}
|
|
55
68
|
|
|
56
69
|
// ---------------------------------------------------------------------------
|
|
@@ -117,7 +130,28 @@ export interface Variant<T> {
|
|
|
117
130
|
value: Lazy<T>;
|
|
118
131
|
}
|
|
119
132
|
|
|
120
|
-
/**
|
|
133
|
+
/**
|
|
134
|
+
* The built-in `cms-settings` block's props: the type of the well-known saved
|
|
135
|
+
* block `CMS`. Every field is optional, and any field can have variants. Read
|
|
136
|
+
* it through `cms.settings()`, which fills in the defaults and applies code's
|
|
137
|
+
* caps (see /next/built-in-blocks#cms-settings).
|
|
138
|
+
*/
|
|
139
|
+
export interface CMSSettings {
|
|
140
|
+
/** Host patterns previews are allowed on, within `createCMS`'s `preview.hosts`. */
|
|
141
|
+
preview?: { hosts?: string[] };
|
|
142
|
+
telemetry?: Telemetry;
|
|
143
|
+
analytics?: Analytics;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** What `cms.settings()` returns: every section, defaults filled in, caps applied. */
|
|
147
|
+
export interface EffectiveSettings {
|
|
148
|
+
/** `["*"]` means every host; an empty list, none. */
|
|
149
|
+
preview: { hosts: string[] };
|
|
150
|
+
telemetry: Required<Telemetry>;
|
|
151
|
+
analytics: Required<Analytics>;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The `telemetry` section of the CMS settings. */
|
|
121
155
|
export interface Telemetry {
|
|
122
156
|
/** Default `true`; `false` switches telemetry off. */
|
|
123
157
|
enabled?: boolean;
|
|
@@ -129,7 +163,7 @@ export interface Telemetry {
|
|
|
129
163
|
traceSampleRate?: number;
|
|
130
164
|
}
|
|
131
165
|
|
|
132
|
-
/** The
|
|
166
|
+
/** The `analytics` section of the CMS settings, and `AnalyticsScript`'s props. */
|
|
133
167
|
export interface Analytics {
|
|
134
168
|
/** An endpoint that accepts the One Dollar Stats format; default: the hosted Deco CMS collector. */
|
|
135
169
|
collector?: string;
|
|
@@ -137,6 +171,9 @@ export interface Analytics {
|
|
|
137
171
|
enabled?: boolean;
|
|
138
172
|
}
|
|
139
173
|
|
|
174
|
+
/** Anything with a `url` and `headers`: a fetch `Request`, a `NextRequest`, a framework wrapper. */
|
|
175
|
+
export type RequestLike = Request | { url: string; headers: { get(name: string): string | null } };
|
|
176
|
+
|
|
140
177
|
// ---------------------------------------------------------------------------
|
|
141
178
|
// Results and errors
|
|
142
179
|
// ---------------------------------------------------------------------------
|
|
@@ -188,6 +225,11 @@ export interface CMSConfig {
|
|
|
188
225
|
interval?: number;
|
|
189
226
|
/** Where telemetry goes; see /next/telemetry. */
|
|
190
227
|
telemetry?: false | TelemetryConfig;
|
|
228
|
+
/**
|
|
229
|
+
* The most content may allow previews on, in the host pattern format
|
|
230
|
+
* (/next/api-reference#host-patterns). Without it, content may allow any host.
|
|
231
|
+
*/
|
|
232
|
+
preview?: { hosts?: string[] };
|
|
191
233
|
/** The private key that decrypts `secret` blocks. */
|
|
192
234
|
secrets?: { key?: string };
|
|
193
235
|
/** Your site's ID, for hosted releases and drafts. */
|
|
@@ -237,4 +279,21 @@ export interface CMS {
|
|
|
237
279
|
forRevision(revision: string): Client;
|
|
238
280
|
/** Ask the content source for newer content now; never throws. */
|
|
239
281
|
update(): Promise<{ updated: boolean }>;
|
|
282
|
+
/**
|
|
283
|
+
* The release's CMS settings (the saved block `CMS`), defaults filled in and
|
|
284
|
+
* code's caps applied. Reads the release already in memory, never a draft;
|
|
285
|
+
* never fetches, never rejects.
|
|
286
|
+
*/
|
|
287
|
+
settings(): Promise<EffectiveSettings>;
|
|
288
|
+
/**
|
|
289
|
+
* The request's draft pointer: `?__draft=` first, then the `deco-draft`
|
|
290
|
+
* cookie. `null` when neither is present, for `?__draft=off`, and on a host
|
|
291
|
+
* outside `settings().preview.hosts`, where the request gets the release.
|
|
292
|
+
*/
|
|
293
|
+
draftPointer(request: RequestLike): Promise<string | null>;
|
|
294
|
+
/**
|
|
295
|
+
* The `Set-Cookie` value that starts a preview (a valid `?__draft=` on an
|
|
296
|
+
* allowed host) or ends one (`?__draft=off`, on any host); `null` otherwise.
|
|
297
|
+
*/
|
|
298
|
+
draftCookie(request: RequestLike): Promise<string | null>;
|
|
240
299
|
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Forced variants (see /next/releases-and-drafts#preview-a-variant): the
|
|
3
|
+
* variants a draft pointer's `__variant` parameters name, applied to a
|
|
4
|
+
* snapshot for one preview. Each addressed multivariate keeps only the forced
|
|
5
|
+
* variant, with the rule `true`, so it shows that variant's value and no other
|
|
6
|
+
* rule runs. The snapshot is copied along the addressed paths only; the shared
|
|
7
|
+
* one is never touched.
|
|
8
|
+
*
|
|
9
|
+
* An address that doesn't reach a multivariate block with that variant (a
|
|
10
|
+
* renamed block, a moved section, an index past the end) is ignored, so a
|
|
11
|
+
* stale preview link renders the content as saved.
|
|
12
|
+
*/
|
|
13
|
+
import { LEGACY_ALIASES } from "./builtins/legacy.ts";
|
|
14
|
+
import { isPlainObject, own } from "./json.ts";
|
|
15
|
+
import type { ForcedVariant, Snapshot } from "./types.ts";
|
|
16
|
+
|
|
17
|
+
export function forceVariants(
|
|
18
|
+
snapshot: Snapshot,
|
|
19
|
+
variants: readonly ForcedVariant[] | undefined,
|
|
20
|
+
): Snapshot {
|
|
21
|
+
if (variants === undefined || variants.length === 0) return snapshot;
|
|
22
|
+
let blocks: Record<string, unknown> | undefined;
|
|
23
|
+
for (const { block, path, index } of variants) {
|
|
24
|
+
const entry = own(blocks ?? snapshot.blocks, block);
|
|
25
|
+
if (entry === undefined) continue;
|
|
26
|
+
const keys = path === "" ? [] : path.split(".");
|
|
27
|
+
const next = forceAt(entry, keys, index, snapshot);
|
|
28
|
+
if (next === entry) continue;
|
|
29
|
+
blocks ??= { ...snapshot.blocks };
|
|
30
|
+
blocks[block] = next;
|
|
31
|
+
}
|
|
32
|
+
return blocks === undefined ? snapshot : { ...snapshot, blocks };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function forceAt(node: unknown, keys: string[], index: number, snapshot: Snapshot): unknown {
|
|
36
|
+
if (keys.length === 0) return forceNode(node, index, snapshot);
|
|
37
|
+
const [key, ...rest] = keys as [string, ...string[]];
|
|
38
|
+
if (Array.isArray(node)) {
|
|
39
|
+
const i = Number(key);
|
|
40
|
+
if (!Number.isInteger(i) || String(i) !== key || i < 0 || i >= node.length) return node;
|
|
41
|
+
const child = forceAt(node[i], rest, index, snapshot);
|
|
42
|
+
if (child === node[i]) return node;
|
|
43
|
+
const copy = node.slice();
|
|
44
|
+
copy[i] = child;
|
|
45
|
+
return copy;
|
|
46
|
+
}
|
|
47
|
+
if (!isPlainObject(node) || !Object.hasOwn(node, key)) return node;
|
|
48
|
+
const child = forceAt(node[key], rest, index, snapshot);
|
|
49
|
+
return child === node[key] ? node : { ...node, [key]: child };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A multivariate block (by its own name or an alias) with only `variants[index]`, ruled `true`. */
|
|
53
|
+
function forceNode(node: unknown, index: number, snapshot: Snapshot): unknown {
|
|
54
|
+
if (!isPlainObject(node) || typeof node.__resolveType !== "string") return node;
|
|
55
|
+
const type = node.__resolveType;
|
|
56
|
+
const canonical = own(snapshot.aliases, type) ?? own(LEGACY_ALIASES, type) ?? type;
|
|
57
|
+
if (canonical !== "multivariate" || !Array.isArray(node.variants)) return node;
|
|
58
|
+
const variant = node.variants[index];
|
|
59
|
+
if (!isPlainObject(variant)) return node;
|
|
60
|
+
return { ...node, variants: [{ ...variant, rule: true }] };
|
|
61
|
+
}
|