@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/remoteLoader.ts
CHANGED
|
@@ -1,73 +1,82 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `remoteLoader`: hosted releases from
|
|
3
|
-
* content module, or any loader). See /next/hosted-releases-internals
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* `remoteLoader`: hosted releases from `delivery.decocms.com`, over a fallback
|
|
3
|
+
* (the content module, or any loader). See /next/hosted-releases-internals.
|
|
4
|
+
* Drafts aren't a loader's job: the CMS layers a draft's changes over what
|
|
5
|
+
* `load()` returns (see ./draftChanges.ts).
|
|
6
6
|
*
|
|
7
|
-
* - `load()` never touches the network: it returns the
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
7
|
+
* - `load()` never touches the network: it returns the release this process
|
|
8
|
+
* last swapped in, or the fallback's content until then. Nothing persists
|
|
9
|
+
* across restarts: every boot starts from the fallback.
|
|
10
|
+
* - `update()` (the CMS calls it in the background, on first use and then
|
|
11
|
+
* every interval) reads `sites/<site>/latest.json`,
|
|
12
|
+
* `{ revision, schemaHash, publishedAt }`. Whoever is newer wins: when it
|
|
13
|
+
* names a revision other than the one this process last swapped in, its
|
|
14
|
+
* `schemaHash` equals the fallback's, and its `publishedAt` is later than
|
|
15
|
+
* the fallback's `committedAt` (the commit time of the git HEAD
|
|
16
|
+
* `deco content` built it from), it downloads
|
|
17
|
+
* `sites/<site>/revisions/<revision>.json`, `{ revision, schemaHash, blocks }`,
|
|
18
|
+
* and swaps it in whole. Otherwise, and on any error, memory stays as it is.
|
|
19
|
+
* A fallback without a `schemaHash` never swaps; one without a `committedAt`
|
|
20
|
+
* (a custom loader, an older content module, a build outside git) counts as
|
|
21
|
+
* the oldest. Studio writes `publishedAt` on Publish, on "Make current" (a
|
|
22
|
+
* rollback) and on Resync, so a rollback wins over the bundles of earlier
|
|
23
|
+
* commits and a deploy of a later commit wins over the rollback. Commit
|
|
24
|
+
* time, not build time: a slow build of an older commit that finishes after
|
|
25
|
+
* a publish still loses to it.
|
|
26
|
+
* Pointers aren't ordered among themselves, and the fallback's content is
|
|
27
|
+
* never compared: only the two timestamps are.
|
|
28
|
+
* - In development (`NODE_ENV=development`), it never swaps, so local files win.
|
|
17
29
|
* - A release larger than `MAX_SNAPSHOT_BYTES` is refused while it downloads,
|
|
18
30
|
* before it's buffered whole (a Worker isolate has 128 MB).
|
|
19
|
-
* - Without `site`
|
|
31
|
+
* - Without `site` it's a plain loader over the fallback.
|
|
20
32
|
*
|
|
21
33
|
* Instances are process-wide singletons, like `createCMS`'s.
|
|
22
34
|
*/
|
|
23
35
|
import { readBoundedJson, timeoutSignal } from "./boundedJson.ts";
|
|
24
|
-
import { computeContentRevision } from "./canonical.ts";
|
|
25
36
|
import { isSnapshot, PEEK_RELEASE, peekRelease } from "./content.ts";
|
|
26
|
-
import { clearGlobals, contentIdentity
|
|
37
|
+
import { clearGlobals, contentIdentity } from "./identity.ts";
|
|
27
38
|
import { isPlainObject } from "./json.ts";
|
|
28
39
|
import type { Loader, Snapshot } from "./types.ts";
|
|
29
40
|
|
|
30
|
-
/** The hosted delivery origin:
|
|
41
|
+
/** The hosted delivery origin: release pointers and revisions. */
|
|
31
42
|
const HOSTED_DELIVERY_ORIGIN = "https://delivery.decocms.com";
|
|
43
|
+
/** test-only: replaces the delivery origin in local end-to-end runs. Not documented, not exported. */
|
|
44
|
+
const TEST_DELIVERY_ORIGIN = Symbol.for("decocms.blocks.test.deliveryOrigin");
|
|
32
45
|
|
|
33
46
|
const INSTANCE_PREFIX = "decocms.blocks.remote:";
|
|
34
|
-
const MANIFEST_FORMAT = 1;
|
|
35
47
|
const FETCH_TIMEOUT_MS = 10_000;
|
|
36
48
|
/** The largest release accepted, in bytes of JSON. */
|
|
37
49
|
const MAX_SNAPSHOT_BYTES = 64 * 1024 * 1024;
|
|
50
|
+
// OPEN: a revision is the git commit SHA; only SHA-1 (40 hex) object names are accepted.
|
|
51
|
+
const REVISION = /^[0-9a-f]{40}$/;
|
|
52
|
+
const SCHEMA_HASH = /^[0-9a-f]{64}$/;
|
|
38
53
|
|
|
39
54
|
interface RemoteLoaderOptions {
|
|
40
55
|
site?: string;
|
|
41
|
-
token?: string;
|
|
42
56
|
/** ms between release checks, used when `createCMS` has no `interval` of its own. */
|
|
43
57
|
interval?: number;
|
|
44
58
|
}
|
|
45
59
|
|
|
46
|
-
|
|
47
|
-
|
|
60
|
+
/** `sites/<site>/latest.json`: the release a site serves. */
|
|
61
|
+
interface Latest {
|
|
48
62
|
revision: string;
|
|
49
|
-
|
|
63
|
+
schemaHash: string;
|
|
64
|
+
publishedAt: string;
|
|
50
65
|
}
|
|
51
66
|
|
|
52
67
|
class RemoteLoader implements Loader {
|
|
53
68
|
readonly #site: string;
|
|
54
|
-
readonly #token: string;
|
|
55
69
|
/** The `interval` this loader was created with; `createCMS` reads it. */
|
|
56
70
|
readonly interval: number | undefined;
|
|
57
71
|
#fallback: Snapshot | Loader;
|
|
58
|
-
#fallbackRevision: string | undefined;
|
|
59
72
|
/** The fallback's content as last loaded, for a fallback that is itself a loader. */
|
|
60
73
|
#fallbackSnapshot: Snapshot | undefined;
|
|
74
|
+
/** The release this process last swapped in; `undefined` serves the fallback. */
|
|
61
75
|
#current: Snapshot | undefined;
|
|
62
|
-
#generation = -1;
|
|
63
76
|
|
|
64
|
-
constructor(
|
|
65
|
-
fallback: Snapshot | Loader,
|
|
66
|
-
options: { site: string; token: string; interval?: number },
|
|
67
|
-
) {
|
|
77
|
+
constructor(fallback: Snapshot | Loader, options: { site: string; interval?: number }) {
|
|
68
78
|
this.#fallback = fallback;
|
|
69
79
|
this.#site = options.site;
|
|
70
|
-
this.#token = options.token;
|
|
71
80
|
this.interval = options.interval;
|
|
72
81
|
}
|
|
73
82
|
|
|
@@ -75,7 +84,6 @@ class RemoteLoader implements Loader {
|
|
|
75
84
|
adopt(fallback: Snapshot | Loader): void {
|
|
76
85
|
if (fallback === this.#fallback) return;
|
|
77
86
|
this.#fallback = fallback;
|
|
78
|
-
this.#fallbackRevision = undefined;
|
|
79
87
|
this.#fallbackSnapshot = undefined;
|
|
80
88
|
}
|
|
81
89
|
|
|
@@ -91,27 +99,19 @@ class RemoteLoader implements Loader {
|
|
|
91
99
|
|
|
92
100
|
async update(): Promise<{ updated: boolean }> {
|
|
93
101
|
if (isDevelopment()) return { updated: false };
|
|
94
|
-
const
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
let next: Snapshot | undefined;
|
|
109
|
-
if (fallbackRevision === undefined || manifest.revision !== fallbackRevision) {
|
|
110
|
-
next = await this.#release(manifest);
|
|
111
|
-
}
|
|
112
|
-
// A slower, earlier check must not undo a newer publish or rollback.
|
|
113
|
-
if (manifest.generation < this.#generation) return { updated: false };
|
|
114
|
-
this.#generation = manifest.generation;
|
|
102
|
+
const fallback = this.#fallbackSnapshot ?? (await this.#loadFallback());
|
|
103
|
+
if (fallback.schemaHash === undefined) return { updated: false };
|
|
104
|
+
const site = encodeURIComponent(this.#site);
|
|
105
|
+
const latest = await this.#latest(site);
|
|
106
|
+
// Another schema: keep what this process serves (the fallback, or the last swap).
|
|
107
|
+
if (latest.schemaHash !== fallback.schemaHash) return { updated: false };
|
|
108
|
+
// The bundle is newer: keep what this process serves.
|
|
109
|
+
// OPEN: a process that already swapped a release in keeps it (memory stays as it is).
|
|
110
|
+
if (!isNewer(latest.publishedAt, fallback.committedAt)) return { updated: false };
|
|
111
|
+
if (latest.revision === this.#current?.revision) return { updated: false };
|
|
112
|
+
const blocks = await this.#revision(site, latest);
|
|
113
|
+
const next: Snapshot = { revision: latest.revision, blocks, schemaHash: latest.schemaHash };
|
|
114
|
+
if (fallback.aliases !== undefined) next.aliases = fallback.aliases;
|
|
115
115
|
this.#current = next;
|
|
116
116
|
return { updated: true };
|
|
117
117
|
}
|
|
@@ -119,68 +119,54 @@ class RemoteLoader implements Loader {
|
|
|
119
119
|
async #loadFallback(): Promise<Snapshot> {
|
|
120
120
|
const fallback = this.#fallback;
|
|
121
121
|
const snapshot = isSnapshot(fallback) ? fallback : await (fallback as Loader).load();
|
|
122
|
-
if (fallback === this.#fallback)
|
|
123
|
-
this.#fallbackRevision = snapshot.revision;
|
|
124
|
-
this.#fallbackSnapshot = snapshot;
|
|
125
|
-
}
|
|
122
|
+
if (fallback === this.#fallback) this.#fallbackSnapshot = snapshot;
|
|
126
123
|
return snapshot;
|
|
127
124
|
}
|
|
128
125
|
|
|
129
|
-
async #
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const response = await fetchWithTimeout(
|
|
136
|
-
`${HOSTED_DELIVERY_ORIGIN}/sites/${site}/channels/production.json`,
|
|
137
|
-
this.#auth(),
|
|
138
|
-
);
|
|
139
|
-
if (!response.ok) throw new Error(`channel manifest: HTTP ${response.status}`);
|
|
126
|
+
async #latest(site: string): Promise<Latest> {
|
|
127
|
+
const response = await fetchWithTimeout(`${deliveryOrigin()}/sites/${site}/latest.json`);
|
|
128
|
+
if (!response.ok) {
|
|
129
|
+
await response.body?.cancel();
|
|
130
|
+
throw new Error(`latest.json: HTTP ${response.status}`);
|
|
131
|
+
}
|
|
140
132
|
const body: unknown = await response.json();
|
|
141
|
-
const prefix = `/sites/${site}/revisions/`;
|
|
142
133
|
if (
|
|
143
134
|
!isPlainObject(body) ||
|
|
144
|
-
body.format !== MANIFEST_FORMAT ||
|
|
145
|
-
!Number.isSafeInteger(body.generation) ||
|
|
146
135
|
typeof body.revision !== "string" ||
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
body.
|
|
136
|
+
!REVISION.test(body.revision) ||
|
|
137
|
+
typeof body.schemaHash !== "string" ||
|
|
138
|
+
!SCHEMA_HASH.test(body.schemaHash) ||
|
|
139
|
+
typeof body.publishedAt !== "string" ||
|
|
140
|
+
Number.isNaN(Date.parse(body.publishedAt))
|
|
151
141
|
) {
|
|
152
|
-
throw new Error("
|
|
142
|
+
throw new Error("latest.json: unexpected format");
|
|
153
143
|
}
|
|
154
|
-
return body as unknown as
|
|
144
|
+
return body as unknown as Latest;
|
|
155
145
|
}
|
|
156
146
|
|
|
157
|
-
async #
|
|
147
|
+
async #revision(site: string, latest: Latest): Promise<Record<string, unknown>> {
|
|
148
|
+
const label = `revision ${latest.revision}`;
|
|
158
149
|
const response = await fetchWithTimeout(
|
|
159
|
-
`${
|
|
160
|
-
this.#auth(),
|
|
161
|
-
);
|
|
162
|
-
if (!response.ok) throw new Error(`release ${manifest.revision}: HTTP ${response.status}`);
|
|
163
|
-
const snapshot = await readBoundedJson(
|
|
164
|
-
response,
|
|
165
|
-
`release ${manifest.revision}`,
|
|
166
|
-
MAX_SNAPSHOT_BYTES,
|
|
150
|
+
`${deliveryOrigin()}/sites/${site}/revisions/${latest.revision}.json`,
|
|
167
151
|
);
|
|
152
|
+
if (!response.ok) {
|
|
153
|
+
await response.body?.cancel();
|
|
154
|
+
throw new Error(`${label}: HTTP ${response.status}`);
|
|
155
|
+
}
|
|
156
|
+
const body = await readBoundedJson(response, label, MAX_SNAPSHOT_BYTES);
|
|
168
157
|
if (
|
|
169
|
-
!
|
|
170
|
-
|
|
171
|
-
|
|
158
|
+
!isPlainObject(body) ||
|
|
159
|
+
body.revision !== latest.revision ||
|
|
160
|
+
body.schemaHash !== latest.schemaHash ||
|
|
161
|
+
!isPlainObject(body.blocks)
|
|
172
162
|
) {
|
|
173
|
-
throw new Error(
|
|
163
|
+
throw new Error(`${label}: unexpected format, or not the revision latest.json names`);
|
|
174
164
|
}
|
|
175
|
-
return
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
#auth(): Record<string, string> {
|
|
179
|
-
return { authorization: `Bearer ${this.#token}` };
|
|
165
|
+
return body.blocks;
|
|
180
166
|
}
|
|
181
167
|
}
|
|
182
168
|
|
|
183
|
-
/** A loader over the fallback alone: `remoteLoader` without `site
|
|
169
|
+
/** A loader over the fallback alone: `remoteLoader` without `site`. */
|
|
184
170
|
class LocalLoader implements Loader {
|
|
185
171
|
#fallback: Snapshot | Loader;
|
|
186
172
|
|
|
@@ -203,16 +189,14 @@ class LocalLoader implements Loader {
|
|
|
203
189
|
}
|
|
204
190
|
|
|
205
191
|
/**
|
|
206
|
-
* Hosted releases over a fallback. `createCMS` builds it for you
|
|
207
|
-
*
|
|
208
|
-
* environment without the variables), it's a loader over the fallback alone.
|
|
192
|
+
* Hosted releases over a fallback. `createCMS` builds it for you when `site`
|
|
193
|
+
* is set. Without `site`, it's a loader over the fallback alone.
|
|
209
194
|
*/
|
|
210
195
|
export function remoteLoader(fallback: Snapshot | Loader, options: RemoteLoaderOptions): Loader {
|
|
211
|
-
const { site
|
|
212
|
-
const hosted = Boolean(site
|
|
196
|
+
const { site } = options ?? {};
|
|
197
|
+
const hosted = Boolean(site);
|
|
213
198
|
const key = Symbol.for(
|
|
214
|
-
`${INSTANCE_PREFIX}${contentIdentity(fallback)}`
|
|
215
|
-
(hosted ? `|site:${site}|token:${fnv1a(token!)}` : "|local"),
|
|
199
|
+
`${INSTANCE_PREFIX}${contentIdentity(fallback)}${hosted ? `|site:${site}` : "|local"}`,
|
|
216
200
|
);
|
|
217
201
|
const store = globalThis as unknown as Record<symbol, RemoteLoader | LocalLoader | undefined>;
|
|
218
202
|
const existing = store[key];
|
|
@@ -227,7 +211,7 @@ export function remoteLoader(fallback: Snapshot | Loader, options: RemoteLoaderO
|
|
|
227
211
|
return existing;
|
|
228
212
|
}
|
|
229
213
|
const instance = hosted
|
|
230
|
-
? new RemoteLoader(fallback, { site: site!,
|
|
214
|
+
? new RemoteLoader(fallback, { site: site!, interval: options.interval })
|
|
231
215
|
: new LocalLoader(fallback);
|
|
232
216
|
store[key] = instance;
|
|
233
217
|
return instance;
|
|
@@ -237,10 +221,31 @@ export function resetRemoteLoaders(): void {
|
|
|
237
221
|
clearGlobals(INSTANCE_PREFIX);
|
|
238
222
|
}
|
|
239
223
|
|
|
240
|
-
|
|
241
|
-
|
|
224
|
+
/**
|
|
225
|
+
* Whether a release published at `publishedAt` is newer than content built
|
|
226
|
+
* from a commit made at `committedAt`. Content without a `committedAt` is the
|
|
227
|
+
* oldest.
|
|
228
|
+
* OPEN: a `committedAt` that doesn't parse as a date counts as missing.
|
|
229
|
+
*/
|
|
230
|
+
function isNewer(publishedAt: string, committedAt: string | undefined): boolean {
|
|
231
|
+
const committed = committedAt === undefined ? Number.NaN : Date.parse(committedAt);
|
|
232
|
+
return Number.isNaN(committed) || Date.parse(publishedAt) > committed;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** No credentials: delivery is public. */
|
|
236
|
+
function fetchWithTimeout(url: string): Promise<Response> {
|
|
237
|
+
return fetch(url, { signal: timeoutSignal(FETCH_TIMEOUT_MS) });
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function deliveryOrigin(): string {
|
|
241
|
+
const override = (globalThis as Record<symbol, unknown>)[TEST_DELIVERY_ORIGIN];
|
|
242
|
+
return typeof override === "string" ? override : HOSTED_DELIVERY_ORIGIN;
|
|
242
243
|
}
|
|
243
244
|
|
|
245
|
+
/**
|
|
246
|
+
* The SDK's only environment read: local development never swaps in hosted
|
|
247
|
+
* releases, so local files win.
|
|
248
|
+
*/
|
|
244
249
|
function isDevelopment(): boolean {
|
|
245
250
|
try {
|
|
246
251
|
// Written out so bundlers that define process.env.NODE_ENV replace it.
|
package/src/v8/settings.ts
CHANGED
|
@@ -46,23 +46,9 @@ export function parseCodeHosts(preview: CMSConfig["preview"]): HostPattern[] | u
|
|
|
46
46
|
return parseCodePatterns(preview, "hosts");
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
/** The hosts a draft pointer may name when code sets no `preview.sources`. */
|
|
50
|
-
export const DEFAULT_SOURCES: readonly string[] = ["studio.decocms.com"];
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Code's `preview.sources`, parsed, or the default (`studio.decocms.com`).
|
|
54
|
-
* Throws a `TypeError` like {@link parseCodeHosts}.
|
|
55
|
-
*/
|
|
56
|
-
export function parseCodeSources(preview: CMSConfig["preview"]): HostPattern[] {
|
|
57
|
-
return (
|
|
58
|
-
parseCodePatterns(preview, "sources") ??
|
|
59
|
-
DEFAULT_SOURCES.map((raw) => parseHostPattern(raw) as HostPattern)
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
49
|
function parseCodePatterns(
|
|
64
50
|
preview: CMSConfig["preview"],
|
|
65
|
-
field: "hosts"
|
|
51
|
+
field: "hosts",
|
|
66
52
|
): HostPattern[] | undefined {
|
|
67
53
|
if (preview === undefined || preview === null) return undefined;
|
|
68
54
|
if (typeof preview !== "object") {
|
package/src/v8/telemetry.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
* Telemetry: OpenTelemetry over OTLP/HTTP (JSON, gzipped), with no SDK. See
|
|
3
3
|
* /next/telemetry and /next/telemetry-internals.
|
|
4
4
|
*
|
|
5
|
-
* - **Where** comes from code: `createCMS({ telemetry
|
|
6
|
-
* `
|
|
7
|
-
*
|
|
5
|
+
* - **Where** comes from code only: `createCMS({ telemetry: { endpoint,
|
|
6
|
+
* headers } })`, else the hosted Deco CMS collector when `createCMS` has a
|
|
7
|
+
* `token` (sent as a Bearer token), else nowhere. `false` sends nothing.
|
|
8
|
+
* Nothing is read from the environment.
|
|
8
9
|
* - **How much** comes from content: the `telemetry` section of the CMS
|
|
9
10
|
* settings (`cms.settings()`, the release's `CMS` block), capped by
|
|
10
11
|
* `telemetry.limits`. The CMS reads it outside any request, when the
|
|
@@ -19,10 +20,9 @@
|
|
|
19
20
|
import { hasBackgroundHook, later, runInBackground } from "./background.ts";
|
|
20
21
|
import { rate, TELEMETRY_DEFAULTS } from "./builtins/data.ts";
|
|
21
22
|
import { isResolutionError } from "./errors.ts";
|
|
22
|
-
import { readEnv } from "./identity.ts";
|
|
23
23
|
import type { CMSError, Snapshot, Telemetry, TelemetryConfig } from "./types.ts";
|
|
24
24
|
|
|
25
|
-
/** The hosted Deco CMS collector, for `
|
|
25
|
+
/** The hosted Deco CMS collector, for `createCMS({ token })`. */
|
|
26
26
|
const HOSTED_TELEMETRY_ENDPOINT = "https://otel.decocms.com";
|
|
27
27
|
|
|
28
28
|
const SINK = Symbol.for("decocms.blocks.telemetry");
|
|
@@ -49,10 +49,15 @@ export function telemetryLimits(config: false | TelemetryConfig | undefined): {
|
|
|
49
49
|
|
|
50
50
|
type Attributes = Record<string, string | number | boolean | undefined>;
|
|
51
51
|
|
|
52
|
+
type Signal = "metrics" | "logs" | "traces";
|
|
53
|
+
|
|
52
54
|
interface Destination {
|
|
55
|
+
/** The base URL each signal's `/v1/<signal>` is appended to. */
|
|
53
56
|
endpoint: string;
|
|
54
57
|
headers: Record<string, string>;
|
|
55
58
|
site?: string;
|
|
59
|
+
/** `telemetry.resource`: merged over the default resource attributes. */
|
|
60
|
+
resource: Record<string, string>;
|
|
56
61
|
limits: { errorSampleRate: number; traceSampleRate: number };
|
|
57
62
|
}
|
|
58
63
|
|
|
@@ -91,30 +96,28 @@ interface LogRecord {
|
|
|
91
96
|
}
|
|
92
97
|
|
|
93
98
|
/**
|
|
94
|
-
* Where telemetry goes, or `null` for nowhere
|
|
95
|
-
* `
|
|
99
|
+
* Where telemetry goes, or `null` for nowhere: `false` is off; a non-empty
|
|
100
|
+
* `endpoint` is that collector, with `headers`; otherwise a `token` is the
|
|
101
|
+
* hosted Deco CMS collector, with the token as a Bearer credential.
|
|
96
102
|
*/
|
|
97
103
|
export function resolveDestination(
|
|
98
104
|
config: false | TelemetryConfig | undefined,
|
|
99
105
|
site?: string,
|
|
106
|
+
token?: string,
|
|
100
107
|
): Destination | null {
|
|
101
108
|
if (config === false) return null;
|
|
102
|
-
|
|
103
|
-
const endpoint = readEnv("OTEL_EXPORTER_OTLP_ENDPOINT");
|
|
104
|
-
if (!endpoint) return null;
|
|
105
|
-
const headers = parseKeyValues(readEnv("OTEL_EXPORTER_OTLP_HEADERS"));
|
|
106
|
-
return { endpoint, headers, site, limits: DEFAULT_LIMITS };
|
|
107
|
-
}
|
|
109
|
+
// OPEN: no per-signal URLs (v7's OTEL_EXPORTER_OTLP_<SIGNAL>_ENDPOINT): every signal goes to `<endpoint>/v1/<signal>`.
|
|
108
110
|
const limits = telemetryLimits(config);
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return { endpoint: config.endpoint, headers: { ...config.headers }, site, limits };
|
|
111
|
+
const resource = { ...config?.resource };
|
|
112
|
+
if (typeof config?.endpoint === "string" && config.endpoint) {
|
|
113
|
+
return { endpoint: config.endpoint, headers: { ...config.headers }, site, resource, limits };
|
|
112
114
|
}
|
|
113
|
-
if (!
|
|
115
|
+
if (!token) return null;
|
|
114
116
|
return {
|
|
115
117
|
endpoint: HOSTED_TELEMETRY_ENDPOINT,
|
|
116
|
-
headers: { authorization: `Bearer ${
|
|
117
|
-
site
|
|
118
|
+
headers: { authorization: `Bearer ${token}` },
|
|
119
|
+
site,
|
|
120
|
+
resource,
|
|
118
121
|
limits,
|
|
119
122
|
};
|
|
120
123
|
}
|
|
@@ -273,7 +276,7 @@ export class TelemetryPipeline {
|
|
|
273
276
|
const byName = new Map<string, Histogram[]>();
|
|
274
277
|
for (const h of histograms) byName.set(h.name, [...(byName.get(h.name) ?? []), h]);
|
|
275
278
|
sends.push(
|
|
276
|
-
this.#send("
|
|
279
|
+
this.#send("metrics", {
|
|
277
280
|
resourceMetrics: [
|
|
278
281
|
{
|
|
279
282
|
resource,
|
|
@@ -307,7 +310,7 @@ export class TelemetryPipeline {
|
|
|
307
310
|
}
|
|
308
311
|
if (logs.length > 0) {
|
|
309
312
|
sends.push(
|
|
310
|
-
this.#send("
|
|
313
|
+
this.#send("logs", {
|
|
311
314
|
resourceLogs: [
|
|
312
315
|
{
|
|
313
316
|
resource,
|
|
@@ -331,7 +334,7 @@ export class TelemetryPipeline {
|
|
|
331
334
|
if (spans.length > 0) {
|
|
332
335
|
const kinds = { internal: 1, server: 2, client: 3 };
|
|
333
336
|
sends.push(
|
|
334
|
-
this.#send("
|
|
337
|
+
this.#send("traces", {
|
|
335
338
|
resourceSpans: [
|
|
336
339
|
{
|
|
337
340
|
resource,
|
|
@@ -388,21 +391,19 @@ export class TelemetryPipeline {
|
|
|
388
391
|
#resource(): Attributes {
|
|
389
392
|
const site = this.#destination.site;
|
|
390
393
|
return {
|
|
391
|
-
"service.name":
|
|
392
|
-
"service.version":
|
|
393
|
-
"
|
|
394
|
-
|
|
395
|
-
(readEnv("NODE_ENV") === "development" ? "development" : "production"),
|
|
394
|
+
"service.name": site || "decocms-site",
|
|
395
|
+
"service.version": "unknown",
|
|
396
|
+
// OPEN: the environment name defaults to "production"; `telemetry.resource` overrides it.
|
|
397
|
+
"deployment.environment.name": "production",
|
|
396
398
|
...(site ? { "deco.site": site } : {}),
|
|
397
399
|
...(this.#release ? { "deco.release": this.#release } : {}),
|
|
398
|
-
//
|
|
399
|
-
|
|
400
|
-
...parseKeyValues(readEnv("OTEL_RESOURCE_ATTRIBUTES")),
|
|
400
|
+
// `telemetry.resource` wins: service.version=<commit>, deployment.environment.name=preview, …
|
|
401
|
+
...this.#destination.resource,
|
|
401
402
|
};
|
|
402
403
|
}
|
|
403
404
|
|
|
404
|
-
async #send(
|
|
405
|
-
const url = this.#destination.endpoint.replace(/\/+$/, "")
|
|
405
|
+
async #send(signal: Signal, payload: unknown): Promise<void> {
|
|
406
|
+
const url = `${this.#destination.endpoint.replace(/\/+$/, "")}/v1/${signal}`;
|
|
406
407
|
const { body, gzipped } = await gzip(JSON.stringify(payload));
|
|
407
408
|
const headers: Record<string, string> = {
|
|
408
409
|
...this.#destination.headers,
|
|
@@ -440,26 +441,6 @@ export function setCurrentTelemetry(pipeline: TelemetryPipeline | undefined): vo
|
|
|
440
441
|
// Helpers
|
|
441
442
|
// ---------------------------------------------------------------------------
|
|
442
443
|
|
|
443
|
-
/** Where hosts put the deployed commit, tried in order (`service.version`). */
|
|
444
|
-
const COMMIT_VARIABLES = [
|
|
445
|
-
"DECO_COMMIT_SHA",
|
|
446
|
-
"WORKERS_CI_COMMIT_SHA",
|
|
447
|
-
"CF_PAGES_COMMIT_SHA",
|
|
448
|
-
"VERCEL_GIT_COMMIT_SHA",
|
|
449
|
-
"GITHUB_SHA",
|
|
450
|
-
"RENDER_GIT_COMMIT",
|
|
451
|
-
"SOURCE_VERSION",
|
|
452
|
-
"COMMIT_SHA",
|
|
453
|
-
];
|
|
454
|
-
|
|
455
|
-
function firstEnv(names: readonly string[]): string | undefined {
|
|
456
|
-
for (const name of names) {
|
|
457
|
-
const value = readEnv(name);
|
|
458
|
-
if (value) return value;
|
|
459
|
-
}
|
|
460
|
-
return undefined;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
444
|
const QUERY = /(https?:\/\/[^\s?#"'<>]+)\?[^\s#"'<>]*/gi;
|
|
464
445
|
const CREDENTIAL = /\b(Bearer|Basic)\s+[\w~+/.=-]+/gi;
|
|
465
446
|
const SENSITIVE =
|
|
@@ -492,23 +473,6 @@ function randomHex(bytes: number): string {
|
|
|
492
473
|
return Array.from(values, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
493
474
|
}
|
|
494
475
|
|
|
495
|
-
/** `k1=v1,k2=v2` with URL-encoded values, the format of the OTEL_* environment variables. */
|
|
496
|
-
function parseKeyValues(raw: string | undefined): Record<string, string> {
|
|
497
|
-
const out: Record<string, string> = {};
|
|
498
|
-
for (const pair of raw?.split(",") ?? []) {
|
|
499
|
-
const at = pair.indexOf("=");
|
|
500
|
-
if (at <= 0) continue;
|
|
501
|
-
try {
|
|
502
|
-
out[decodeURIComponent(pair.slice(0, at).trim())] = decodeURIComponent(
|
|
503
|
-
pair.slice(at + 1).trim(),
|
|
504
|
-
);
|
|
505
|
-
} catch {
|
|
506
|
-
// A malformed pair is skipped.
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
return out;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
476
|
function encodeAttributes(attributes: Attributes, scrub: (text: string) => string) {
|
|
513
477
|
return Object.entries(attributes)
|
|
514
478
|
.filter(([, value]) => value !== undefined)
|
package/src/v8/types.ts
CHANGED
|
@@ -28,6 +28,19 @@ export type Snapshot = {
|
|
|
28
28
|
blocks: Record<string, unknown>;
|
|
29
29
|
/** The alias table `deco content` writes: old type name → your type. */
|
|
30
30
|
aliases?: Record<string, string>;
|
|
31
|
+
/**
|
|
32
|
+
* The hash of the `.deco/schema.gen.json` this content was built with, which
|
|
33
|
+
* `deco content` writes. A hosted release is swapped in only when its
|
|
34
|
+
* `schemaHash` equals the bundled content's; without one, never.
|
|
35
|
+
*/
|
|
36
|
+
schemaHash?: string;
|
|
37
|
+
/**
|
|
38
|
+
* The commit time of the git HEAD `deco content` built this content from
|
|
39
|
+
* (committer date, ISO 8601). A hosted release is swapped in only when its
|
|
40
|
+
* `publishedAt` is later; without one (a custom loader, an older content
|
|
41
|
+
* module, a build outside git), the bundled content counts as the oldest.
|
|
42
|
+
*/
|
|
43
|
+
committedAt?: string;
|
|
31
44
|
};
|
|
32
45
|
|
|
33
46
|
/**
|
|
@@ -48,11 +61,11 @@ export interface Loader {
|
|
|
48
61
|
|
|
49
62
|
/** The parts of a draft pointer, `<host[:port]><path[?query]>@<version>`. */
|
|
50
63
|
export interface DraftPointer {
|
|
51
|
-
/** `host[:port]`
|
|
64
|
+
/** `host[:port]` that serves the draft's changes. */
|
|
52
65
|
host: string;
|
|
53
66
|
/** Starts with `/`; opaque to the app. Never carries the `__variant` parameters. */
|
|
54
67
|
path: string;
|
|
55
|
-
/** Opaque:
|
|
68
|
+
/** Opaque: identifies the editor's last save. `"local"` names no draft (`deco serve`). */
|
|
56
69
|
version: string;
|
|
57
70
|
/** The variants this preview forces (the query's `__variant` parameters); absent when none. */
|
|
58
71
|
variants?: ForcedVariant[];
|
|
@@ -212,12 +225,23 @@ export type Match<T> =
|
|
|
212
225
|
// The CMS and its clients
|
|
213
226
|
// ---------------------------------------------------------------------------
|
|
214
227
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
228
|
+
/**
|
|
229
|
+
* Where telemetry goes. With `endpoint`, to that OTLP/HTTP collector, with
|
|
230
|
+
* `headers`; without one, to the hosted Deco CMS collector when `createCMS`
|
|
231
|
+
* has a `token`, and nowhere otherwise.
|
|
232
|
+
*/
|
|
233
|
+
export interface TelemetryConfig {
|
|
234
|
+
/** An OTLP/HTTP base URL; each signal is sent to `<endpoint>/v1/<signal>`. */
|
|
235
|
+
endpoint?: string;
|
|
236
|
+
/** Headers sent with every batch to `endpoint`. */
|
|
237
|
+
headers?: Record<string, string>;
|
|
238
|
+
/**
|
|
239
|
+
* Resource attributes, merged over the defaults (`service.name`,
|
|
240
|
+
* `service.version`, `deployment.environment.name`, …).
|
|
241
|
+
*/
|
|
242
|
+
resource?: Record<string, string>;
|
|
219
243
|
limits?: { errorSampleRate?: number; traceSampleRate?: number };
|
|
220
|
-
}
|
|
244
|
+
}
|
|
221
245
|
|
|
222
246
|
export interface CMSConfig {
|
|
223
247
|
/** Your block functions; the CMS uses `{ ...builtIns, ...blocks }`. */
|
|
@@ -235,16 +259,23 @@ export interface CMSConfig {
|
|
|
235
259
|
*/
|
|
236
260
|
hosts?: string[];
|
|
237
261
|
/**
|
|
238
|
-
* The hosts a draft pointer may
|
|
239
|
-
*
|
|
262
|
+
* The hosts a draft pointer may point at; replaces the defaults (v7's
|
|
263
|
+
* list: `.decocms.com` and the loopback hosts). An entry starting with a
|
|
264
|
+
* dot matches any subdomain; any other entry, that exact host.
|
|
240
265
|
*/
|
|
241
|
-
|
|
266
|
+
draftHosts?: string[];
|
|
242
267
|
};
|
|
243
268
|
/** The private key that decrypts `secret` blocks. */
|
|
244
269
|
secrets?: { key?: string };
|
|
245
|
-
/**
|
|
270
|
+
/**
|
|
271
|
+
* Your site ID (Studio's site slug): turns on hosted releases from
|
|
272
|
+
* `delivery.decocms.com`. Drafts don't need it.
|
|
273
|
+
*/
|
|
246
274
|
site?: string;
|
|
247
|
-
/**
|
|
275
|
+
/**
|
|
276
|
+
* Your site token (server-only secret): sends telemetry to the hosted Deco
|
|
277
|
+
* CMS collector. Releases and drafts don't use it. Needs `site`.
|
|
278
|
+
*/
|
|
248
279
|
token?: string;
|
|
249
280
|
}
|
|
250
281
|
|
|
@@ -285,11 +316,11 @@ export interface CMS {
|
|
|
285
316
|
forRelease(): Client;
|
|
286
317
|
/**
|
|
287
318
|
* A client reading the draft a pointer names: its changes, fetched from a
|
|
288
|
-
* host
|
|
319
|
+
* draft host (`*.decocms.com` and loopback by default;
|
|
320
|
+
* `createCMS({ preview: { draftHosts } })` replaces the list), over this
|
|
321
|
+
* server's production content.
|
|
289
322
|
*/
|
|
290
323
|
forDraft(pointer: string): Client;
|
|
291
|
-
/** A client pinned to a revision this CMS has served; an unknown revision behaves like the release. */
|
|
292
|
-
forRevision(revision: string): Client;
|
|
293
324
|
/** Ask the content source for newer content now; never throws. */
|
|
294
325
|
update(): Promise<{ updated: boolean }>;
|
|
295
326
|
/**
|
|
@@ -299,7 +330,7 @@ export interface CMS {
|
|
|
299
330
|
*/
|
|
300
331
|
settings(): Promise<EffectiveSettings>;
|
|
301
332
|
/**
|
|
302
|
-
* The request's draft pointer: `?__draft=` first, then the `
|
|
333
|
+
* The request's draft pointer: `?__draft=` first, then the `__deco_draft`
|
|
303
334
|
* cookie. `null` when neither is present, for `?__draft=off`, and on a host
|
|
304
335
|
* outside `settings().preview.hosts`, where the request gets the release.
|
|
305
336
|
*/
|