@decocms/blocks 8.1.0-next.3 → 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/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 +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/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 +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 +180 -23
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +14 -0
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +28 -0
- 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 +8 -4
- 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 +14 -2
- 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 +56 -2
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +3 -3
- 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 +1 -1
- 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/tsProgram.ts +27 -0
- package/src/v8/cli/schema/typeToSchema.ts +18 -1
- package/src/v8/cli/serve/server.ts +20 -55
- package/src/v8/cms.ts +226 -26
- package/src/v8/content.ts +30 -0
- package/src/v8/draft.ts +14 -10
- package/src/v8/hosts.ts +184 -0
- package/src/v8/index.ts +6 -8
- package/src/v8/remoteLoader.ts +17 -2
- package/src/v8/settings.ts +157 -0
- package/src/v8/telemetry.ts +43 -40
- package/src/v8/types.ts +48 -2
package/src/v8/cms.ts
CHANGED
|
@@ -7,30 +7,62 @@
|
|
|
7
7
|
* cache. See /next/api-reference#one-instance-per-process.
|
|
8
8
|
*/
|
|
9
9
|
import { runInBackground } from "./background.ts";
|
|
10
|
+
import { SETTINGS_BLOCK, SETTINGS_TYPE } from "./builtins/data.ts";
|
|
10
11
|
import { builtIns } from "./builtins/index.ts";
|
|
11
12
|
import { secretBlock } from "./builtins/secret.ts";
|
|
12
13
|
import { CMSClient } from "./client.ts";
|
|
13
14
|
import { ContentStore, isLoader, isSnapshot } from "./content.ts";
|
|
14
|
-
import { parseDraftPointer } from "./draft.ts";
|
|
15
|
+
import { draftCookieFor, endsPreview, parseDraftPointer, readDraftPointer } from "./draft.ts";
|
|
16
|
+
import { allowsHost, type HostPattern, parseHostPattern } from "./hosts.ts";
|
|
15
17
|
import { clearGlobals, contentIdentity, fnv1a, readEnv } from "./identity.ts";
|
|
18
|
+
import { isPlainObject } from "./json.ts";
|
|
16
19
|
import { remoteLoader, resetRemoteLoaders } from "./remoteLoader.ts";
|
|
17
|
-
import {
|
|
18
|
-
|
|
20
|
+
import {
|
|
21
|
+
defaultSettings,
|
|
22
|
+
isStatic,
|
|
23
|
+
parseCodeHosts,
|
|
24
|
+
readSettings,
|
|
25
|
+
type SettingsCaps,
|
|
26
|
+
} from "./settings.ts";
|
|
27
|
+
import {
|
|
28
|
+
resolveDestination,
|
|
29
|
+
setCurrentTelemetry,
|
|
30
|
+
TelemetryPipeline,
|
|
31
|
+
telemetryLimits,
|
|
32
|
+
} from "./telemetry.ts";
|
|
33
|
+
import type {
|
|
34
|
+
Blocks,
|
|
35
|
+
Client,
|
|
36
|
+
CMS,
|
|
37
|
+
CMSConfig,
|
|
38
|
+
EffectiveSettings,
|
|
39
|
+
Loader,
|
|
40
|
+
RequestLike,
|
|
41
|
+
Snapshot,
|
|
42
|
+
} from "./types.ts";
|
|
19
43
|
import { forceVariants } from "./variants.ts";
|
|
20
44
|
|
|
21
45
|
const INSTANCE_PREFIX = "decocms.blocks.cms:";
|
|
22
46
|
const MIN_INTERVAL = 60_000;
|
|
23
47
|
/** Each check runs one interval, plus or minus up to this much, after the previous one. */
|
|
24
48
|
const JITTER = 10_000;
|
|
49
|
+
/** How often telemetry reads its settings again when the release hasn't changed (date rules). */
|
|
50
|
+
const TELEMETRY_SETTINGS_MS = 60_000;
|
|
25
51
|
|
|
26
52
|
/** Options that must agree between two `createCMS` calls sharing an instance. */
|
|
27
53
|
interface Fingerprint {
|
|
28
54
|
interval: number;
|
|
29
55
|
telemetry: string;
|
|
56
|
+
preview: string;
|
|
30
57
|
secrets: string;
|
|
31
58
|
}
|
|
32
59
|
|
|
33
|
-
|
|
60
|
+
/**
|
|
61
|
+
* The state one content root shares across `createCMS` calls: the content
|
|
62
|
+
* store, caps, telemetry and secret key. Each call gets its own handle
|
|
63
|
+
* holding its own block map (see `CMSHandle`).
|
|
64
|
+
*/
|
|
65
|
+
class CMSInstance {
|
|
34
66
|
readonly config: Readonly<CMSConfig>;
|
|
35
67
|
readonly fingerprint: Fingerprint;
|
|
36
68
|
readonly #store: ContentStore;
|
|
@@ -39,7 +71,13 @@ class CMSInstance implements CMS {
|
|
|
39
71
|
/** The built-ins with a `secret` that holds this instance's key (the first call's). */
|
|
40
72
|
readonly #builtIns: Readonly<Blocks>;
|
|
41
73
|
readonly #telemetry: TelemetryPipeline | undefined;
|
|
42
|
-
#
|
|
74
|
+
readonly #caps: SettingsCaps;
|
|
75
|
+
/** Settings of a `CMS` block with nothing to run, per release snapshot. */
|
|
76
|
+
#staticSettings = new WeakMap<Snapshot, EffectiveSettings>();
|
|
77
|
+
/** The release and time telemetry last read its section. */
|
|
78
|
+
#telemetryRead: { snapshot: Snapshot; at: number } | undefined;
|
|
79
|
+
/** One handle per block map, so the same config returns the same object. */
|
|
80
|
+
readonly #handles = new WeakMap<Blocks, CMS>();
|
|
43
81
|
/**
|
|
44
82
|
* When the next check is due. The first is on the first `forRelease()` or `forDraft()`, not
|
|
45
83
|
* at construction: Workers read `Date.now()` as 0 at module scope.
|
|
@@ -48,10 +86,13 @@ class CMSInstance implements CMS {
|
|
|
48
86
|
|
|
49
87
|
constructor(config: CMSConfig, interval: number) {
|
|
50
88
|
this.config = config;
|
|
51
|
-
this.#blocks = config.blocks;
|
|
52
89
|
this.#interval = interval;
|
|
53
90
|
this.#store = new ContentStore(contentOf(config));
|
|
54
91
|
this.fingerprint = fingerprintOf(config, interval);
|
|
92
|
+
this.#caps = {
|
|
93
|
+
hosts: parseCodeHosts(config.preview),
|
|
94
|
+
limits: telemetryLimits(config.telemetry),
|
|
95
|
+
};
|
|
55
96
|
const destination = resolveDestination(config.telemetry, config.site);
|
|
56
97
|
if (destination !== null) {
|
|
57
98
|
this.#telemetry = new TelemetryPipeline(destination);
|
|
@@ -63,34 +104,58 @@ class CMSInstance implements CMS {
|
|
|
63
104
|
this.#builtIns = Object.freeze({ ...builtIns, secret });
|
|
64
105
|
}
|
|
65
106
|
|
|
66
|
-
forRelease(): Client {
|
|
107
|
+
forRelease(blocks: Blocks): Client {
|
|
67
108
|
this.#scheduleUpdate();
|
|
68
109
|
const telemetry = this.#telemetry;
|
|
69
|
-
if (telemetry === undefined) return this.#client(() => this.#store.release());
|
|
70
|
-
return this.#client(() =>
|
|
71
|
-
this.#store.release().then((snapshot) => {
|
|
110
|
+
if (telemetry === undefined) return this.#client(blocks, () => this.#store.release());
|
|
111
|
+
return this.#client(blocks, () =>
|
|
112
|
+
this.#store.release().then(async (snapshot) => {
|
|
72
113
|
telemetry.useRelease(snapshot);
|
|
114
|
+
await this.#followTelemetrySettings(blocks, telemetry, snapshot);
|
|
73
115
|
return snapshot;
|
|
74
116
|
}),
|
|
75
117
|
);
|
|
76
118
|
}
|
|
77
119
|
|
|
120
|
+
/**
|
|
121
|
+
* The release's settings, from memory: never a draft, never a fetch. The
|
|
122
|
+
* `CMS` block resolves on each call, so a field with variants is decided
|
|
123
|
+
* where it's called; one with nothing to run is resolved once per release.
|
|
124
|
+
*/
|
|
125
|
+
settings(blocks: Blocks): Promise<EffectiveSettings> {
|
|
126
|
+
return this.#settingsOf(blocks, this.#store.current());
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async draftPointer(blocks: Blocks, request: RequestLike): Promise<string | null> {
|
|
130
|
+
const pointer = readDraftPointer(request);
|
|
131
|
+
if (pointer === null) return null;
|
|
132
|
+
return (await this.#previewAllowed(blocks, request)) ? pointer : null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async draftCookie(blocks: Blocks, request: RequestLike): Promise<string | null> {
|
|
136
|
+
const cookie = draftCookieFor(request);
|
|
137
|
+
if (cookie === null) return null;
|
|
138
|
+
// Ending a preview is allowed anywhere, so a cookie set before a host was removed still goes.
|
|
139
|
+
if (endsPreview(request)) return cookie;
|
|
140
|
+
return (await this.#previewAllowed(blocks, request)) ? cookie : null;
|
|
141
|
+
}
|
|
142
|
+
|
|
78
143
|
/**
|
|
79
144
|
* The draft, with the variants the pointer forces (even over a source with no
|
|
80
|
-
* drafts). A draft
|
|
81
|
-
*
|
|
145
|
+
* drafts). A draft client is a production client reading a draft pointer, so
|
|
146
|
+
* it schedules the release check like any other client.
|
|
82
147
|
*/
|
|
83
|
-
forDraft(pointer: string): Client {
|
|
148
|
+
forDraft(blocks: Blocks, pointer: string): Client {
|
|
84
149
|
this.#scheduleUpdate();
|
|
85
150
|
const variants = parseDraftPointer(pointer)?.variants;
|
|
86
|
-
if (variants === undefined) return this.#client(() => this.#store.draft(pointer));
|
|
87
|
-
return this.#client(() =>
|
|
151
|
+
if (variants === undefined) return this.#client(blocks, () => this.#store.draft(pointer));
|
|
152
|
+
return this.#client(blocks, () =>
|
|
88
153
|
this.#store.draft(pointer).then((snapshot) => forceVariants(snapshot, variants)),
|
|
89
154
|
);
|
|
90
155
|
}
|
|
91
156
|
|
|
92
|
-
forRevision(revision: string): Client {
|
|
93
|
-
return this.#client(() => this.#store.revision(revision));
|
|
157
|
+
forRevision(blocks: Blocks, revision: string): Client {
|
|
158
|
+
return this.#client(blocks, () => this.#store.revision(revision));
|
|
94
159
|
}
|
|
95
160
|
|
|
96
161
|
update(): Promise<{ updated: boolean }> {
|
|
@@ -98,16 +163,85 @@ class CMSInstance implements CMS {
|
|
|
98
163
|
return this.#store.update();
|
|
99
164
|
}
|
|
100
165
|
|
|
101
|
-
/**
|
|
166
|
+
/** The handle for a block map: this instance, resolving with that map. */
|
|
167
|
+
handle(blocks: Blocks): CMS {
|
|
168
|
+
let handle = this.#handles.get(blocks);
|
|
169
|
+
if (handle === undefined) {
|
|
170
|
+
handle = new CMSHandle(this, blocks);
|
|
171
|
+
this.#handles.set(blocks, handle);
|
|
172
|
+
}
|
|
173
|
+
return handle;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* A later `createCMS` call with the same key hands in its (possibly
|
|
178
|
+
* hot-reloaded) content. Its block map stays with its own handle: another
|
|
179
|
+
* bundle in the same process (Next's proxy) must not replace the app's.
|
|
180
|
+
*/
|
|
102
181
|
adopt(config: CMSConfig): void {
|
|
103
|
-
this.#blocks = config.blocks;
|
|
104
182
|
this.#store.replace(contentOf(config));
|
|
183
|
+
this.#staticSettings = new WeakMap();
|
|
184
|
+
this.#telemetryRead = undefined;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async #settingsOf(blocks: Blocks, snapshot: Snapshot | undefined): Promise<EffectiveSettings> {
|
|
188
|
+
if (snapshot === undefined) return defaultSettings(this.#caps);
|
|
189
|
+
const cached = this.#staticSettings.get(snapshot);
|
|
190
|
+
if (cached !== undefined) return cached;
|
|
191
|
+
const settings = await readSettings(
|
|
192
|
+
snapshot,
|
|
193
|
+
() =>
|
|
194
|
+
new CMSClient({
|
|
195
|
+
load: () => Promise.resolve(snapshot),
|
|
196
|
+
blocks,
|
|
197
|
+
builtIns: this.#builtIns,
|
|
198
|
+
onCollision: (name) => this.#warnCollision(name),
|
|
199
|
+
}),
|
|
200
|
+
this.#caps,
|
|
201
|
+
);
|
|
202
|
+
// A block map's own cms-settings function may answer differently per call.
|
|
203
|
+
const entry = snapshot.blocks[SETTINGS_BLOCK];
|
|
204
|
+
const fixed = !isPlainObject(entry) || isStatic(entry);
|
|
205
|
+
if (fixed && !Object.hasOwn(blocks, SETTINGS_TYPE)) {
|
|
206
|
+
this.#staticSettings.set(snapshot, settings);
|
|
207
|
+
}
|
|
208
|
+
return settings;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Whether the release's preview hosts allow this request's host. */
|
|
212
|
+
async #previewAllowed(blocks: Blocks, request: RequestLike): Promise<boolean> {
|
|
213
|
+
const { hosts } = (await this.settings(blocks)).preview;
|
|
214
|
+
const patterns = hosts
|
|
215
|
+
.map((raw) => parseHostPattern(raw))
|
|
216
|
+
.filter((pattern): pattern is HostPattern => pattern !== null);
|
|
217
|
+
return allowsHost(patterns, request.url);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Telemetry follows the release's `telemetry` section, read outside any
|
|
222
|
+
* request's choices: when the release changes (before its first client
|
|
223
|
+
* runs) and then at most once a minute, so date rules take effect.
|
|
224
|
+
*/
|
|
225
|
+
async #followTelemetrySettings(
|
|
226
|
+
blocks: Blocks,
|
|
227
|
+
telemetry: TelemetryPipeline,
|
|
228
|
+
snapshot: Snapshot,
|
|
229
|
+
): Promise<void> {
|
|
230
|
+
const last = this.#telemetryRead;
|
|
231
|
+
const now = Date.now();
|
|
232
|
+
if (last?.snapshot === snapshot && now - last.at < TELEMETRY_SETTINGS_MS) return;
|
|
233
|
+
this.#telemetryRead = { snapshot, at: now };
|
|
234
|
+
const read = this.#settingsOf(blocks, snapshot).then((settings) => {
|
|
235
|
+
if (this.#telemetryRead?.snapshot === snapshot) telemetry.apply(settings.telemetry);
|
|
236
|
+
});
|
|
237
|
+
// A new release waits for its rates; a periodic re-read doesn't hold up the request.
|
|
238
|
+
if (last?.snapshot !== snapshot) await read;
|
|
105
239
|
}
|
|
106
240
|
|
|
107
|
-
#client(load: () => Promise<Snapshot>): Client {
|
|
241
|
+
#client(blocks: Blocks, load: () => Promise<Snapshot>): Client {
|
|
108
242
|
return new CMSClient({
|
|
109
243
|
load,
|
|
110
|
-
blocks
|
|
244
|
+
blocks,
|
|
111
245
|
builtIns: this.#builtIns,
|
|
112
246
|
onCollision: (name) => this.#warnCollision(name),
|
|
113
247
|
telemetry: this.#telemetry?.forClient(),
|
|
@@ -139,9 +273,67 @@ class CMSInstance implements CMS {
|
|
|
139
273
|
}
|
|
140
274
|
}
|
|
141
275
|
|
|
276
|
+
/** `Symbol.for`, so a second copy of this module still reads it. */
|
|
277
|
+
const INSTANCE = Symbol.for("decocms.blocks.cms.instance");
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* What `createCMS` returns: the shared instance plus the block map of this
|
|
281
|
+
* call. Every handle on one content root shares its content, caps, telemetry
|
|
282
|
+
* and secret key; each resolves with the block map it was created with.
|
|
283
|
+
*/
|
|
284
|
+
class CMSHandle implements CMS {
|
|
285
|
+
readonly #instance: CMSInstance;
|
|
286
|
+
readonly #blocks: Blocks;
|
|
287
|
+
|
|
288
|
+
constructor(instance: CMSInstance, blocks: Blocks) {
|
|
289
|
+
this.#instance = instance;
|
|
290
|
+
this.#blocks = blocks;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** The shared instance (tests use it to tell whether two handles share one). */
|
|
294
|
+
get [INSTANCE](): CMSInstance {
|
|
295
|
+
return this.#instance;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
forRelease(): Client {
|
|
299
|
+
return this.#instance.forRelease(this.#blocks);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
forDraft(pointer: string): Client {
|
|
303
|
+
return this.#instance.forDraft(this.#blocks, pointer);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
forRevision(revision: string): Client {
|
|
307
|
+
return this.#instance.forRevision(this.#blocks, revision);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
update(): Promise<{ updated: boolean }> {
|
|
311
|
+
return this.#instance.update();
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
settings(): Promise<EffectiveSettings> {
|
|
315
|
+
return this.#instance.settings(this.#blocks);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
draftPointer(request: RequestLike): Promise<string | null> {
|
|
319
|
+
return this.#instance.draftPointer(this.#blocks, request);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
draftCookie(request: RequestLike): Promise<string | null> {
|
|
323
|
+
return this.#instance.draftCookie(this.#blocks, request);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** The shared instance behind a `createCMS` result; `undefined` for anything else. */
|
|
328
|
+
export function instanceOf(cms: CMS): object | undefined {
|
|
329
|
+
const instance = (cms as unknown as Record<symbol, unknown>)[INSTANCE];
|
|
330
|
+
return instance instanceof Object ? instance : undefined;
|
|
331
|
+
}
|
|
332
|
+
|
|
142
333
|
/**
|
|
143
|
-
* Creates the CMS
|
|
144
|
-
* Create it once, at module scope, and
|
|
334
|
+
* Creates the CMS: a handle on the one instance this content root has in the
|
|
335
|
+
* process, holding this call's block map. Create it once, at module scope, and
|
|
336
|
+
* ask it for a client per request.
|
|
145
337
|
*/
|
|
146
338
|
export function createCMS(config: CMSConfig): CMS {
|
|
147
339
|
validate(config);
|
|
@@ -151,14 +343,20 @@ export function createCMS(config: CMSConfig): CMS {
|
|
|
151
343
|
const key = Symbol.for(INSTANCE_PREFIX + identityOf(config));
|
|
152
344
|
const store = globalThis as unknown as Record<symbol, CMSInstance | undefined>;
|
|
153
345
|
const existing = store[key];
|
|
154
|
-
|
|
346
|
+
// An instance an older copy of this package stored (a dev reload across
|
|
347
|
+
// versions) has no handles: it's replaced rather than adopted.
|
|
348
|
+
if (
|
|
349
|
+
existing instanceof Object &&
|
|
350
|
+
typeof existing.adopt === "function" &&
|
|
351
|
+
typeof existing.handle === "function"
|
|
352
|
+
) {
|
|
155
353
|
warnOnConflict(existing.fingerprint, fingerprintOf(config, interval));
|
|
156
354
|
existing.adopt(config);
|
|
157
|
-
return existing;
|
|
355
|
+
return existing.handle(config.blocks);
|
|
158
356
|
}
|
|
159
357
|
const instance = new CMSInstance(config, interval);
|
|
160
358
|
store[key] = instance;
|
|
161
|
-
return instance;
|
|
359
|
+
return instance.handle(config.blocks);
|
|
162
360
|
}
|
|
163
361
|
|
|
164
362
|
/** Clears every stored CMS instance, so a test starts clean. */
|
|
@@ -186,6 +384,7 @@ function validate(config: CMSConfig): void {
|
|
|
186
384
|
"createCMS: `content` must be the content module ({ revision, blocks }) or a loader with load()",
|
|
187
385
|
);
|
|
188
386
|
}
|
|
387
|
+
parseCodeHosts(config.preview);
|
|
189
388
|
}
|
|
190
389
|
|
|
191
390
|
function resolveInterval(configured: number | undefined): number {
|
|
@@ -221,6 +420,7 @@ function fingerprintOf(config: CMSConfig, interval: number): Fingerprint {
|
|
|
221
420
|
return {
|
|
222
421
|
interval,
|
|
223
422
|
telemetry: stableJson(config.telemetry ?? null),
|
|
423
|
+
preview: stableJson(config.preview ?? null),
|
|
224
424
|
secrets: config.secrets?.key ? fnv1a(config.secrets.key) : "",
|
|
225
425
|
};
|
|
226
426
|
}
|
package/src/v8/content.ts
CHANGED
|
@@ -29,6 +29,20 @@ export function isLoader(content: unknown): content is Loader {
|
|
|
29
29
|
);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* An internal hook a built-in loader (`remoteLoader`) has: the release it
|
|
34
|
+
* would return right now, from memory, or `undefined` when that takes a load.
|
|
35
|
+
* `cms.settings()` reads through it, so it never fetches.
|
|
36
|
+
*/
|
|
37
|
+
export const PEEK_RELEASE: unique symbol = Symbol.for("decocms.blocks.peekRelease");
|
|
38
|
+
|
|
39
|
+
/** The release a source holds in memory: the snapshot itself, or what a loader can peek. */
|
|
40
|
+
export function peekRelease(source: Snapshot | Loader): Snapshot | undefined {
|
|
41
|
+
if (!isLoader(source)) return source;
|
|
42
|
+
const peek = (source as { [PEEK_RELEASE]?: () => Snapshot | undefined })[PEEK_RELEASE];
|
|
43
|
+
return typeof peek === "function" ? peek.call(source) : undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
32
46
|
export function isSnapshot(content: unknown): content is Snapshot {
|
|
33
47
|
return (
|
|
34
48
|
isPlainObject(content) &&
|
|
@@ -46,6 +60,8 @@ export class ContentStore {
|
|
|
46
60
|
CACHED_DRAFTS,
|
|
47
61
|
);
|
|
48
62
|
readonly #served = new BoundedMap<string, Snapshot>(SERVED_REVISIONS);
|
|
63
|
+
/** The release this store last handed a client, for a loader that can't peek. */
|
|
64
|
+
#latest: Snapshot | undefined;
|
|
49
65
|
|
|
50
66
|
constructor(source: Snapshot | Loader) {
|
|
51
67
|
this.#source = source;
|
|
@@ -106,6 +122,15 @@ export class ContentStore {
|
|
|
106
122
|
return entry.pending;
|
|
107
123
|
}
|
|
108
124
|
|
|
125
|
+
/**
|
|
126
|
+
* The current release as it is in memory, without loading anything: the
|
|
127
|
+
* content module, what a built-in loader holds, or the release this store
|
|
128
|
+
* last loaded. `undefined` before a custom loader's first load.
|
|
129
|
+
*/
|
|
130
|
+
current(): Snapshot | undefined {
|
|
131
|
+
return peekRelease(this.#source) ?? this.#latest;
|
|
132
|
+
}
|
|
133
|
+
|
|
109
134
|
/** A release revision this store has served, or the release when it's unknown (drafts included). */
|
|
110
135
|
revision(revision: string): Promise<Snapshot> {
|
|
111
136
|
const served = this.#served.get(revision);
|
|
@@ -124,6 +149,8 @@ export class ContentStore {
|
|
|
124
149
|
const updated = result?.updated === true;
|
|
125
150
|
if (updated) {
|
|
126
151
|
this.#release = undefined;
|
|
152
|
+
// `#latest` stays: settings keep the release that was serving until
|
|
153
|
+
// the next one loads, never the defaults (which may allow every host).
|
|
127
154
|
// A loader that ignores the pointer hands back the release as the
|
|
128
155
|
// draft; that copy is as stale as the release now.
|
|
129
156
|
this.#drafts.clear();
|
|
@@ -150,12 +177,14 @@ export class ContentStore {
|
|
|
150
177
|
// The same loader (a hosted remoteLoader that adopted new fallback content): re-read it.
|
|
151
178
|
// Cached drafts were layered over the old content, so they go too.
|
|
152
179
|
this.#release = undefined;
|
|
180
|
+
this.#latest = undefined;
|
|
153
181
|
this.#drafts.clear();
|
|
154
182
|
return;
|
|
155
183
|
}
|
|
156
184
|
if (isSnapshot(previous)) this.#served.delete(previous.revision);
|
|
157
185
|
this.#source = source;
|
|
158
186
|
this.#release = undefined;
|
|
187
|
+
this.#latest = undefined;
|
|
159
188
|
this.#drafts.clear();
|
|
160
189
|
}
|
|
161
190
|
|
|
@@ -177,6 +206,7 @@ export class ContentStore {
|
|
|
177
206
|
}
|
|
178
207
|
|
|
179
208
|
#serve(snapshot: Snapshot): Snapshot {
|
|
209
|
+
this.#latest = snapshot;
|
|
180
210
|
this.#served.set(snapshot.revision, snapshot);
|
|
181
211
|
return snapshot;
|
|
182
212
|
}
|
package/src/v8/draft.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Draft pointers (see /next/api-reference#draft-pointers): the string
|
|
3
|
-
* `<host[:port]><path[?query]>@<version>` that names a draft, and
|
|
4
|
-
*
|
|
3
|
+
* `<host[:port]><path[?query]>@<version>` that names a draft, and what
|
|
4
|
+
* `cms.draftPointer` and `cms.draftCookie` read and write to carry one from a
|
|
5
|
+
* `?__draft=` link into a cookie. The host check against the preview hosts is
|
|
6
|
+
* the CMS's: these read the request only.
|
|
5
7
|
*
|
|
6
8
|
* The query's reserved `__variant` parameters are the variants a preview
|
|
7
9
|
* forces (`<block>@<path>=<index>`, URL-encoded, one per multivariate); the
|
|
8
10
|
* parser lifts them out of `path` into `variants`.
|
|
9
11
|
*/
|
|
10
|
-
import type { DraftPointer, ForcedVariant } from "./types.ts";
|
|
12
|
+
import type { DraftPointer, ForcedVariant, RequestLike } from "./types.ts";
|
|
11
13
|
|
|
12
|
-
/** The draft cookie's name
|
|
13
|
-
|
|
14
|
+
/** The draft cookie's name. Not exported from the package: reading it directly would skip the host check. */
|
|
15
|
+
const DRAFT_COOKIE = "deco-draft";
|
|
14
16
|
|
|
15
17
|
const DRAFT_PARAM = "__draft";
|
|
16
18
|
const DRAFT_OFF = "off";
|
|
@@ -135,15 +137,12 @@ function normalizeHost(authority: string): string | null {
|
|
|
135
137
|
return `${host}:${port}`;
|
|
136
138
|
}
|
|
137
139
|
|
|
138
|
-
/** Anything with a `url` and `headers`: a fetch `Request`, a `NextRequest`, a framework wrapper. */
|
|
139
|
-
export type RequestLike = Request | { url: string; headers: Headers };
|
|
140
|
-
|
|
141
140
|
/**
|
|
142
141
|
* The draft pointer a request carries: `?__draft=` from the URL first, then
|
|
143
142
|
* the `deco-draft` cookie. `null` when neither is present, or when the URL
|
|
144
143
|
* says `?__draft=off`. The value is returned as is; `cms.forDraft` validates it.
|
|
145
144
|
*/
|
|
146
|
-
export function
|
|
145
|
+
export function readDraftPointer(request: RequestLike): string | null {
|
|
147
146
|
const param = draftParam(request);
|
|
148
147
|
if (param === DRAFT_OFF) return null;
|
|
149
148
|
if (param) return param;
|
|
@@ -161,7 +160,7 @@ export function draftPointer(request: RequestLike): string | null {
|
|
|
161
160
|
* is `Secure; SameSite=None; Partitioned` (CHIPS): `SameSite=Lax` would never
|
|
162
161
|
* be sent there. `HttpOnly` keeps page scripts from reading the pointer.
|
|
163
162
|
*/
|
|
164
|
-
export function
|
|
163
|
+
export function draftCookieFor(request: RequestLike): string | null {
|
|
165
164
|
const param = draftParam(request);
|
|
166
165
|
if (!param) return null;
|
|
167
166
|
if (param === DRAFT_OFF) return serializeCookie("", ["Max-Age=0"]);
|
|
@@ -169,6 +168,11 @@ export function draftCookie(request: RequestLike): string | null {
|
|
|
169
168
|
return serializeCookie(encodeURIComponent(param), []);
|
|
170
169
|
}
|
|
171
170
|
|
|
171
|
+
/** Whether the request's URL says `?__draft=off`: the cookie that ends a preview is set on any host. */
|
|
172
|
+
export function endsPreview(request: RequestLike): boolean {
|
|
173
|
+
return draftParam(request) === DRAFT_OFF;
|
|
174
|
+
}
|
|
175
|
+
|
|
172
176
|
function serializeCookie(value: string, extra: string[]): string {
|
|
173
177
|
return [
|
|
174
178
|
`${DRAFT_COOKIE}=${value}`,
|
package/src/v8/hosts.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host patterns (see /next/api-reference#host-patterns): the format of
|
|
3
|
+
* `preview.hosts`, in `createCMS` and in the `CMS` block.
|
|
4
|
+
*
|
|
5
|
+
* - Hostnames and patterns are compared lowercase, with one trailing dot
|
|
6
|
+
* removed; names outside ASCII in their punycode form.
|
|
7
|
+
* - A pattern without `*` matches that one hostname.
|
|
8
|
+
* - `*.name` matches a hostname that ends in `.name` with at least one more
|
|
9
|
+
* label in front, compared label by label from the right. `*` is only ever
|
|
10
|
+
* the whole leading label, and at least two labels follow it.
|
|
11
|
+
* - `"*"` alone matches every host.
|
|
12
|
+
* - A pattern without a port matches any port; one with a port matches only
|
|
13
|
+
* a URL that names that port.
|
|
14
|
+
* - IPv4, and IPv6 in brackets, match exactly and never take a wildcard.
|
|
15
|
+
* - Anything else (a scheme, a path, a space, an empty label) isn't a pattern.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** A parsed pattern. `port` is absent when the pattern names none. */
|
|
19
|
+
export type HostPattern =
|
|
20
|
+
| { kind: "any" }
|
|
21
|
+
| { kind: "name"; host: string; port?: string }
|
|
22
|
+
| { kind: "wildcard"; suffix: string; port?: string }
|
|
23
|
+
| { kind: "ip"; host: string; port?: string };
|
|
24
|
+
|
|
25
|
+
/** The list that allows every host. */
|
|
26
|
+
export const EVERY_HOST = "*";
|
|
27
|
+
|
|
28
|
+
const LABEL_RE = /^[a-z0-9_]([a-z0-9_-]{0,61}[a-z0-9_])?$/;
|
|
29
|
+
const PORT_RE = /^[0-9]{1,5}$/;
|
|
30
|
+
const IPV4_RE =
|
|
31
|
+
/^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}$/;
|
|
32
|
+
const NUMERIC_LABEL_RE = /^(0x[0-9a-f]*|[0-9]+)$/;
|
|
33
|
+
|
|
34
|
+
/** Parses one pattern; `null` when it isn't one. */
|
|
35
|
+
export function parseHostPattern(raw: unknown): HostPattern | null {
|
|
36
|
+
if (typeof raw !== "string" || raw.length === 0 || raw.length > 300) return null;
|
|
37
|
+
if (raw === EVERY_HOST) return { kind: "any" };
|
|
38
|
+
if (/[\s/\\?#@]/.test(raw) || raw.includes("://")) return null;
|
|
39
|
+
const split = splitPort(raw);
|
|
40
|
+
if (split === null) return null;
|
|
41
|
+
const { port } = split;
|
|
42
|
+
let host = split.host;
|
|
43
|
+
if (host.startsWith("[")) {
|
|
44
|
+
const ip = normalizeIPv6(host);
|
|
45
|
+
return ip === null ? null : withPort({ kind: "ip", host: ip }, port);
|
|
46
|
+
}
|
|
47
|
+
host = host.toLowerCase();
|
|
48
|
+
if (host.endsWith(".")) host = host.slice(0, -1);
|
|
49
|
+
if (host.startsWith("*.")) {
|
|
50
|
+
const suffix = normalizeName(host.slice(2));
|
|
51
|
+
if (suffix === null || suffix.split(".").length < 2) return null;
|
|
52
|
+
return withPort({ kind: "wildcard", suffix }, port);
|
|
53
|
+
}
|
|
54
|
+
if (IPV4_RE.test(host)) return withPort({ kind: "ip", host }, port);
|
|
55
|
+
const name = normalizeName(host);
|
|
56
|
+
return name === null ? null : withPort({ kind: "name", host: name }, port);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Formats a parsed pattern back into its normal form. */
|
|
60
|
+
export function formatHostPattern(pattern: HostPattern): string {
|
|
61
|
+
if (pattern.kind === "any") return EVERY_HOST;
|
|
62
|
+
const host = pattern.kind === "wildcard" ? `*.${pattern.suffix}` : pattern.host;
|
|
63
|
+
return pattern.port === undefined ? host : `${host}:${pattern.port}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether a request URL's host is one a pattern matches. A URL that can't be
|
|
68
|
+
* read matches only `"*"`, and neither does a hostname with an empty label.
|
|
69
|
+
*/
|
|
70
|
+
export function hostMatches(pattern: HostPattern, url: URL | null): boolean {
|
|
71
|
+
if (pattern.kind === "any") return true;
|
|
72
|
+
if (url === null) return false;
|
|
73
|
+
if (pattern.port !== undefined && pattern.port !== url.port) return false;
|
|
74
|
+
let host = url.hostname.toLowerCase();
|
|
75
|
+
if (host.endsWith(".")) host = host.slice(0, -1);
|
|
76
|
+
// An empty label (`a..example.com`) isn't a hostname any pattern names.
|
|
77
|
+
if (host.split(".").includes("")) return false;
|
|
78
|
+
switch (pattern.kind) {
|
|
79
|
+
case "name":
|
|
80
|
+
case "ip":
|
|
81
|
+
return host === pattern.host;
|
|
82
|
+
case "wildcard":
|
|
83
|
+
return host.length > pattern.suffix.length + 1 && host.endsWith(`.${pattern.suffix}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether a request may preview under this list: some pattern matches its URL's
|
|
89
|
+
* host. A URL with a username or password (`https://public.com@staging.example.com/`,
|
|
90
|
+
* which a forged Host header can produce) counts as unreadable.
|
|
91
|
+
*/
|
|
92
|
+
export function allowsHost(patterns: readonly HostPattern[], requestUrl: string): boolean {
|
|
93
|
+
let url: URL | null;
|
|
94
|
+
try {
|
|
95
|
+
url = new URL(requestUrl);
|
|
96
|
+
} catch {
|
|
97
|
+
url = null;
|
|
98
|
+
}
|
|
99
|
+
if (url !== null && (url.username !== "" || url.password !== "")) url = null;
|
|
100
|
+
return patterns.some((pattern) => hostMatches(pattern, url));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Whether every host `inner` matches is matched by `outer`: content's entry
|
|
105
|
+
* `inner` is within code's entry `outer`.
|
|
106
|
+
*/
|
|
107
|
+
export function patternWithin(inner: HostPattern, outer: HostPattern): boolean {
|
|
108
|
+
if (outer.kind === "any") return true;
|
|
109
|
+
if (inner.kind === "any") return false;
|
|
110
|
+
if (outer.port !== undefined && inner.port !== outer.port) return false;
|
|
111
|
+
switch (outer.kind) {
|
|
112
|
+
case "name":
|
|
113
|
+
case "ip":
|
|
114
|
+
return inner.kind === outer.kind && inner.host === outer.host;
|
|
115
|
+
case "wildcard":
|
|
116
|
+
if (inner.kind === "name") return inner.host.endsWith(`.${outer.suffix}`);
|
|
117
|
+
if (inner.kind === "wildcard") {
|
|
118
|
+
return inner.suffix === outer.suffix || inner.suffix.endsWith(`.${outer.suffix}`);
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function withPort<T extends HostPattern>(pattern: T, port: string | undefined): T {
|
|
125
|
+
return port === undefined ? pattern : { ...pattern, port };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** `host[:port]`, with the port checked; `null` on an empty host or a bad port. */
|
|
129
|
+
function splitPort(raw: string): { host: string; port?: string } | null {
|
|
130
|
+
let host = raw;
|
|
131
|
+
let port: string | undefined;
|
|
132
|
+
if (raw.startsWith("[")) {
|
|
133
|
+
const end = raw.indexOf("]");
|
|
134
|
+
if (end === -1) return null;
|
|
135
|
+
host = raw.slice(0, end + 1);
|
|
136
|
+
const rest = raw.slice(end + 1);
|
|
137
|
+
if (rest !== "") {
|
|
138
|
+
if (!rest.startsWith(":")) return null;
|
|
139
|
+
port = rest.slice(1);
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
const colon = raw.indexOf(":");
|
|
143
|
+
if (colon !== -1) {
|
|
144
|
+
if (raw.indexOf(":", colon + 1) !== -1) return null; // an IPv6 address without brackets
|
|
145
|
+
host = raw.slice(0, colon);
|
|
146
|
+
port = raw.slice(colon + 1);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (host === "") return null;
|
|
150
|
+
if (port !== undefined && (!PORT_RE.test(port) || Number(port) > 65535)) return null;
|
|
151
|
+
// The form a URL gives it: no leading zeros.
|
|
152
|
+
return port === undefined ? { host } : { host, port: String(Number(port)) };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A DNS name in its ASCII (punycode) form, lowercase; `null` if it isn't one. */
|
|
156
|
+
function normalizeName(name: string): string | null {
|
|
157
|
+
if (name === "" || name.includes("*")) return null;
|
|
158
|
+
let ascii = name;
|
|
159
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: the ASCII range is the point
|
|
160
|
+
if (/[^\x00-\x7f]/.test(name)) {
|
|
161
|
+
try {
|
|
162
|
+
ascii = new URL(`http://${name}/`).hostname;
|
|
163
|
+
} catch {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const labels = ascii.split(".");
|
|
168
|
+
if (labels.some((label) => !LABEL_RE.test(label))) return null;
|
|
169
|
+
// A name ending in a numeric label is an IPv4 address to a URL parser: only
|
|
170
|
+
// the canonical dotted form above is one.
|
|
171
|
+
if (NUMERIC_LABEL_RE.test(labels[labels.length - 1])) return null;
|
|
172
|
+
return ascii.length > 253 ? null : ascii;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** `[addr]` in the compressed, lowercase form a URL's hostname has. */
|
|
176
|
+
function normalizeIPv6(bracketed: string): string | null {
|
|
177
|
+
if (!/^\[[0-9a-fA-F:.]+\]$/.test(bracketed)) return null;
|
|
178
|
+
try {
|
|
179
|
+
const host = new URL(`http://${bracketed}/`).hostname;
|
|
180
|
+
return host.startsWith("[") ? host : null;
|
|
181
|
+
} catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|