@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
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
*
|
|
5
5
|
* The content protocol is `@decocms/blocks/protocol`'s `createContentHandler`
|
|
6
6
|
* (at `/rpc`) and `createAssetHandler` (at `/assets/<name>`) over the
|
|
7
|
-
* filesystem storage; they own
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* filesystem storage; they own `Content-Type`, size limits and upload rules.
|
|
8
|
+
* `deco serve` has no authentication and answers any origin: any website open
|
|
9
|
+
* in the browser can read and write the content through it, so it is meant
|
|
10
|
+
* to run only while editing. This file is the Node HTTP layer:
|
|
10
11
|
*
|
|
11
|
-
* - Listens on 127.0.0.1 unless `--host` says otherwise (with a warning
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* - Listens on 127.0.0.1 unless `--host` says otherwise (with a warning:
|
|
13
|
+
* the network can reach it then).
|
|
14
|
+
* - CORS is answered for any `Origin` (reflected, with `Vary: Origin`), and
|
|
15
|
+
* so are Chrome's Private/Local Network Access preflights.
|
|
15
16
|
* - Every save regenerates `.deco/blocks.gen.ts`.
|
|
16
17
|
*/
|
|
17
|
-
import { randomBytes } from "node:crypto";
|
|
18
18
|
import fs from "node:fs";
|
|
19
19
|
import http from "node:http";
|
|
20
20
|
import type { AddressInfo } from "node:net";
|
|
@@ -30,15 +30,8 @@ import { readSavedBlocks, writeContent } from "../content.ts";
|
|
|
30
30
|
import { consoleReporter, type Reporter } from "../log.ts";
|
|
31
31
|
import { CliError, decoPaths, findDecoRoot, packageVersion } from "../root.ts";
|
|
32
32
|
|
|
33
|
-
/** The site editor's origins: the browser origins allowed by default. */
|
|
34
|
-
const STUDIO_ORIGINS = [
|
|
35
|
-
"https://studio.decocms.com",
|
|
36
|
-
"https://admin.decocms.com",
|
|
37
|
-
"https://admin.deco.cx",
|
|
38
|
-
];
|
|
39
|
-
|
|
40
33
|
/** Where the site editor link points. */
|
|
41
|
-
const STUDIO_ORIGIN =
|
|
34
|
+
const STUDIO_ORIGIN = "https://studio.decocms.com";
|
|
42
35
|
|
|
43
36
|
const DEFAULT_PORT = 4545;
|
|
44
37
|
const DEFAULT_HOST = "127.0.0.1";
|
|
@@ -52,19 +45,15 @@ export interface ServeOptions {
|
|
|
52
45
|
host?: string;
|
|
53
46
|
/** The local app the site editor previews: `localhost:8001` or a full loopback URL. */
|
|
54
47
|
preview?: string;
|
|
55
|
-
token?: string;
|
|
56
|
-
allowOrigins?: string[];
|
|
57
48
|
/** The upload folder, relative to the folder that contains `.deco`. */
|
|
58
49
|
assets?: string;
|
|
59
50
|
readOnly?: boolean;
|
|
60
51
|
reporter?: Reporter;
|
|
61
|
-
env?: NodeJS.ProcessEnv;
|
|
62
52
|
}
|
|
63
53
|
|
|
64
54
|
export interface RunningServer {
|
|
65
55
|
/** The content protocol endpoint, `http://127.0.0.1:4545/rpc`. */
|
|
66
56
|
endpoint: string;
|
|
67
|
-
token: string;
|
|
68
57
|
siteEditorUrl: string;
|
|
69
58
|
port: number;
|
|
70
59
|
close(): Promise<void>;
|
|
@@ -111,24 +100,11 @@ function previewUrl(input: string): string {
|
|
|
111
100
|
/** Start the server; resolves once it listens. */
|
|
112
101
|
export async function startServer(options: ServeOptions = {}): Promise<RunningServer> {
|
|
113
102
|
const reporter = options.reporter ?? consoleReporter;
|
|
114
|
-
const env = options.env ?? process.env;
|
|
115
103
|
const paths = decoPaths(findDecoRoot(options));
|
|
116
|
-
const host = options.host ?? DEFAULT_HOST;
|
|
104
|
+
const host = (options.host ?? DEFAULT_HOST).toLowerCase();
|
|
117
105
|
const requestedPort = options.port ?? DEFAULT_PORT;
|
|
118
106
|
const readOnly = options.readOnly ?? false;
|
|
119
|
-
if (options.token !== undefined && options.token.trim() === "") {
|
|
120
|
-
throw new CliError("--token can't be empty: pass a token, or leave it out for a random one");
|
|
121
|
-
}
|
|
122
|
-
if (options.token === undefined && env.DECO_SERVE_TOKEN?.trim() === "") {
|
|
123
|
-
throw new CliError(
|
|
124
|
-
"DECO_SERVE_TOKEN can't be empty: set a token, or unset it for a random one",
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
const token = options.token ?? env.DECO_SERVE_TOKEN ?? randomBytes(32).toString("base64url");
|
|
128
107
|
const preview = previewUrl(options.preview ?? defaultPreviewUrl(paths.root));
|
|
129
|
-
const allowedOrigins = new Set(
|
|
130
|
-
[...STUDIO_ORIGINS, ...(options.allowOrigins ?? [])].map((o) => o.replace(/\/+$/, "")),
|
|
131
|
-
);
|
|
132
108
|
|
|
133
109
|
if (!Number.isInteger(requestedPort) || requestedPort < 0 || requestedPort > 65535) {
|
|
134
110
|
throw new CliError(`--port must be a port number, got ${requestedPort}`);
|
|
@@ -161,19 +137,11 @@ export async function startServer(options: ServeOptions = {}): Promise<RunningSe
|
|
|
161
137
|
},
|
|
162
138
|
};
|
|
163
139
|
const rpc = createContentHandler(storage, {
|
|
164
|
-
token,
|
|
165
140
|
server: { name: "deco-cli", version: packageVersion() },
|
|
166
141
|
preview: { url: preview },
|
|
167
142
|
onError: (error) => reporter.warn(String((error as Error)?.message ?? error)),
|
|
168
143
|
});
|
|
169
|
-
const assets = createAssetHandler(storage
|
|
170
|
-
|
|
171
|
-
let port = requestedPort;
|
|
172
|
-
const allowedHosts = () => {
|
|
173
|
-
const hosts = new Set([`127.0.0.1:${port}`, `localhost:${port}`, `[::1]:${port}`]);
|
|
174
|
-
if (!LOOPBACK.has(host)) hosts.add(`${host.includes(":") ? `[${host}]` : host}:${port}`);
|
|
175
|
-
return hosts;
|
|
176
|
-
};
|
|
144
|
+
const assets = createAssetHandler(storage);
|
|
177
145
|
|
|
178
146
|
const server = http.createServer(async (req, res) => {
|
|
179
147
|
const origin = req.headers.origin;
|
|
@@ -184,21 +152,14 @@ export async function startServer(options: ServeOptions = {}): Promise<RunningSe
|
|
|
184
152
|
};
|
|
185
153
|
|
|
186
154
|
try {
|
|
187
|
-
// DNS rebinding: only our own address.
|
|
188
|
-
if (!allowedHosts().has(String(req.headers.host ?? "").toLowerCase())) {
|
|
189
|
-
return refuse(403, ErrorCode.Forbidden, "unexpected Host header");
|
|
190
|
-
}
|
|
191
155
|
if (origin !== undefined) {
|
|
192
|
-
if (!allowedOrigins.has(origin)) {
|
|
193
|
-
return refuse(403, ErrorCode.Forbidden, `origin ${origin} isn't allowed`);
|
|
194
|
-
}
|
|
195
156
|
cors["access-control-allow-origin"] = origin;
|
|
196
157
|
cors.vary = "Origin";
|
|
197
158
|
}
|
|
198
159
|
if (req.method === "OPTIONS") {
|
|
199
160
|
const preflight: Record<string, string> = {
|
|
200
161
|
"access-control-allow-methods": "POST, PUT, OPTIONS",
|
|
201
|
-
"access-control-allow-headers": "
|
|
162
|
+
"access-control-allow-headers": "content-type",
|
|
202
163
|
"access-control-max-age": "600",
|
|
203
164
|
};
|
|
204
165
|
// Chrome's Local Network Access / Private Network Access preflight.
|
|
@@ -232,15 +193,20 @@ export async function startServer(options: ServeOptions = {}): Promise<RunningSe
|
|
|
232
193
|
);
|
|
233
194
|
server.listen(requestedPort, host, () => resolve());
|
|
234
195
|
});
|
|
235
|
-
port = (server.address() as AddressInfo).port;
|
|
196
|
+
const port = (server.address() as AddressInfo).port;
|
|
236
197
|
|
|
237
198
|
const displayHost = host.includes(":") ? `[${host}]` : host;
|
|
238
199
|
const endpoint = `http://${displayHost}:${port}/rpc`;
|
|
239
|
-
|
|
200
|
+
// The site editor connects only through loopback; a wildcard address
|
|
201
|
+
// listens there too, so its link uses 127.0.0.1.
|
|
202
|
+
const wildcard = host === "0.0.0.0" || host === "::";
|
|
203
|
+
const linkEndpoint = wildcard ? `http://127.0.0.1:${port}/rpc` : endpoint;
|
|
204
|
+
const siteEditorUrl = `${STUDIO_ORIGIN}/site-editor#endpoint=${encodeURIComponent(linkEndpoint)}`;
|
|
240
205
|
|
|
241
206
|
if (!LOOPBACK.has(host)) {
|
|
242
207
|
reporter.warn(
|
|
243
|
-
`warning: listening on ${host}: other machines can reach this server
|
|
208
|
+
`warning: listening on ${host}: other machines on the network can reach this server and read and write the content` +
|
|
209
|
+
(wildcard ? "" : "; the site editor connects only through 127.0.0.1/localhost"),
|
|
244
210
|
);
|
|
245
211
|
}
|
|
246
212
|
const schemaFile = fs.existsSync(paths.schema)
|
|
@@ -263,7 +229,6 @@ export async function startServer(options: ServeOptions = {}): Promise<RunningSe
|
|
|
263
229
|
|
|
264
230
|
return {
|
|
265
231
|
endpoint,
|
|
266
|
-
token,
|
|
267
232
|
siteEditorUrl,
|
|
268
233
|
port,
|
|
269
234
|
close: () =>
|
package/src/v8/cms.ts
CHANGED
|
@@ -7,28 +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";
|
|
15
|
+
import { draftCookieFor, endsPreview, parseDraftPointer, readDraftPointer } from "./draft.ts";
|
|
16
|
+
import { allowsHost, type HostPattern, parseHostPattern } from "./hosts.ts";
|
|
14
17
|
import { clearGlobals, contentIdentity, fnv1a, readEnv } from "./identity.ts";
|
|
18
|
+
import { isPlainObject } from "./json.ts";
|
|
15
19
|
import { remoteLoader, resetRemoteLoaders } from "./remoteLoader.ts";
|
|
16
|
-
import {
|
|
17
|
-
|
|
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";
|
|
43
|
+
import { forceVariants } from "./variants.ts";
|
|
18
44
|
|
|
19
45
|
const INSTANCE_PREFIX = "decocms.blocks.cms:";
|
|
20
46
|
const MIN_INTERVAL = 60_000;
|
|
21
47
|
/** Each check runs one interval, plus or minus up to this much, after the previous one. */
|
|
22
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;
|
|
23
51
|
|
|
24
52
|
/** Options that must agree between two `createCMS` calls sharing an instance. */
|
|
25
53
|
interface Fingerprint {
|
|
26
54
|
interval: number;
|
|
27
55
|
telemetry: string;
|
|
56
|
+
preview: string;
|
|
28
57
|
secrets: string;
|
|
29
58
|
}
|
|
30
59
|
|
|
31
|
-
|
|
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 {
|
|
32
66
|
readonly config: Readonly<CMSConfig>;
|
|
33
67
|
readonly fingerprint: Fingerprint;
|
|
34
68
|
readonly #store: ContentStore;
|
|
@@ -37,19 +71,28 @@ class CMSInstance implements CMS {
|
|
|
37
71
|
/** The built-ins with a `secret` that holds this instance's key (the first call's). */
|
|
38
72
|
readonly #builtIns: Readonly<Blocks>;
|
|
39
73
|
readonly #telemetry: TelemetryPipeline | undefined;
|
|
40
|
-
#
|
|
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>();
|
|
41
81
|
/**
|
|
42
|
-
* When the next check is due. The first is on the first `forRelease()`, not
|
|
82
|
+
* When the next check is due. The first is on the first `forRelease()` or `forDraft()`, not
|
|
43
83
|
* at construction: Workers read `Date.now()` as 0 at module scope.
|
|
44
84
|
*/
|
|
45
85
|
#nextCheck: number | undefined;
|
|
46
86
|
|
|
47
87
|
constructor(config: CMSConfig, interval: number) {
|
|
48
88
|
this.config = config;
|
|
49
|
-
this.#blocks = config.blocks;
|
|
50
89
|
this.#interval = interval;
|
|
51
90
|
this.#store = new ContentStore(contentOf(config));
|
|
52
91
|
this.fingerprint = fingerprintOf(config, interval);
|
|
92
|
+
this.#caps = {
|
|
93
|
+
hosts: parseCodeHosts(config.preview),
|
|
94
|
+
limits: telemetryLimits(config.telemetry),
|
|
95
|
+
};
|
|
53
96
|
const destination = resolveDestination(config.telemetry, config.site);
|
|
54
97
|
if (destination !== null) {
|
|
55
98
|
this.#telemetry = new TelemetryPipeline(destination);
|
|
@@ -61,24 +104,58 @@ class CMSInstance implements CMS {
|
|
|
61
104
|
this.#builtIns = Object.freeze({ ...builtIns, secret });
|
|
62
105
|
}
|
|
63
106
|
|
|
64
|
-
forRelease(): Client {
|
|
107
|
+
forRelease(blocks: Blocks): Client {
|
|
65
108
|
this.#scheduleUpdate();
|
|
66
109
|
const telemetry = this.#telemetry;
|
|
67
|
-
if (telemetry === undefined) return this.#client(() => this.#store.release());
|
|
68
|
-
return this.#client(() =>
|
|
69
|
-
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) => {
|
|
70
113
|
telemetry.useRelease(snapshot);
|
|
114
|
+
await this.#followTelemetrySettings(blocks, telemetry, snapshot);
|
|
71
115
|
return snapshot;
|
|
72
116
|
}),
|
|
73
117
|
);
|
|
74
118
|
}
|
|
75
119
|
|
|
76
|
-
|
|
77
|
-
|
|
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());
|
|
78
127
|
}
|
|
79
128
|
|
|
80
|
-
|
|
81
|
-
|
|
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
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The draft, with the variants the pointer forces (even over a source with no
|
|
145
|
+
* drafts). A draft client is a production client reading a draft pointer, so
|
|
146
|
+
* it schedules the release check like any other client.
|
|
147
|
+
*/
|
|
148
|
+
forDraft(blocks: Blocks, pointer: string): Client {
|
|
149
|
+
this.#scheduleUpdate();
|
|
150
|
+
const variants = parseDraftPointer(pointer)?.variants;
|
|
151
|
+
if (variants === undefined) return this.#client(blocks, () => this.#store.draft(pointer));
|
|
152
|
+
return this.#client(blocks, () =>
|
|
153
|
+
this.#store.draft(pointer).then((snapshot) => forceVariants(snapshot, variants)),
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
forRevision(blocks: Blocks, revision: string): Client {
|
|
158
|
+
return this.#client(blocks, () => this.#store.revision(revision));
|
|
82
159
|
}
|
|
83
160
|
|
|
84
161
|
update(): Promise<{ updated: boolean }> {
|
|
@@ -86,16 +163,85 @@ class CMSInstance implements CMS {
|
|
|
86
163
|
return this.#store.update();
|
|
87
164
|
}
|
|
88
165
|
|
|
89
|
-
/**
|
|
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
|
+
*/
|
|
90
181
|
adopt(config: CMSConfig): void {
|
|
91
|
-
this.#blocks = config.blocks;
|
|
92
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;
|
|
93
239
|
}
|
|
94
240
|
|
|
95
|
-
#client(load: () => Promise<Snapshot>): Client {
|
|
241
|
+
#client(blocks: Blocks, load: () => Promise<Snapshot>): Client {
|
|
96
242
|
return new CMSClient({
|
|
97
243
|
load,
|
|
98
|
-
blocks
|
|
244
|
+
blocks,
|
|
99
245
|
builtIns: this.#builtIns,
|
|
100
246
|
onCollision: (name) => this.#warnCollision(name),
|
|
101
247
|
telemetry: this.#telemetry?.forClient(),
|
|
@@ -127,9 +273,67 @@ class CMSInstance implements CMS {
|
|
|
127
273
|
}
|
|
128
274
|
}
|
|
129
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
|
+
|
|
130
333
|
/**
|
|
131
|
-
* Creates the CMS
|
|
132
|
-
* 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.
|
|
133
337
|
*/
|
|
134
338
|
export function createCMS(config: CMSConfig): CMS {
|
|
135
339
|
validate(config);
|
|
@@ -139,14 +343,20 @@ export function createCMS(config: CMSConfig): CMS {
|
|
|
139
343
|
const key = Symbol.for(INSTANCE_PREFIX + identityOf(config));
|
|
140
344
|
const store = globalThis as unknown as Record<symbol, CMSInstance | undefined>;
|
|
141
345
|
const existing = store[key];
|
|
142
|
-
|
|
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
|
+
) {
|
|
143
353
|
warnOnConflict(existing.fingerprint, fingerprintOf(config, interval));
|
|
144
354
|
existing.adopt(config);
|
|
145
|
-
return existing;
|
|
355
|
+
return existing.handle(config.blocks);
|
|
146
356
|
}
|
|
147
357
|
const instance = new CMSInstance(config, interval);
|
|
148
358
|
store[key] = instance;
|
|
149
|
-
return instance;
|
|
359
|
+
return instance.handle(config.blocks);
|
|
150
360
|
}
|
|
151
361
|
|
|
152
362
|
/** Clears every stored CMS instance, so a test starts clean. */
|
|
@@ -174,6 +384,7 @@ function validate(config: CMSConfig): void {
|
|
|
174
384
|
"createCMS: `content` must be the content module ({ revision, blocks }) or a loader with load()",
|
|
175
385
|
);
|
|
176
386
|
}
|
|
387
|
+
parseCodeHosts(config.preview);
|
|
177
388
|
}
|
|
178
389
|
|
|
179
390
|
function resolveInterval(configured: number | undefined): number {
|
|
@@ -209,6 +420,7 @@ function fingerprintOf(config: CMSConfig, interval: number): Fingerprint {
|
|
|
209
420
|
return {
|
|
210
421
|
interval,
|
|
211
422
|
telemetry: stableJson(config.telemetry ?? null),
|
|
423
|
+
preview: stableJson(config.preview ?? null),
|
|
212
424
|
secrets: config.secrets?.key ? fnv1a(config.secrets.key) : "",
|
|
213
425
|
};
|
|
214
426
|
}
|