@decocms/blocks 8.1.0-next.4 → 8.1.0-next.6
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 +1 -3
- 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 +24 -53
- 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 -20
- package/dist/protocol/conformance/cases/writes.js.map +1 -1
- package/dist/protocol/conformance/contentHashFixtures.d.ts +0 -29
- package/dist/protocol/conformance/contentHashFixtures.d.ts.map +1 -1
- package/dist/protocol/conformance/contentHashFixtures.js +0 -30
- package/dist/protocol/conformance/contentHashFixtures.js.map +1 -1
- package/dist/protocol/conformance/context.d.ts +4 -29
- 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.d.ts +1 -1
- package/dist/protocol/conformance/index.d.ts.map +1 -1
- package/dist/protocol/conformance/index.js +4 -4
- package/dist/protocol/conformance/index.js.map +1 -1
- 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 +3 -5
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +3 -5
- 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 +7 -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 +23 -34
- package/dist/protocol/types.d.ts.map +1 -1
- package/dist/protocol/types.js +10 -10
- package/dist/protocol/types.js.map +1 -1
- package/dist/v8/boundedJson.d.ts +11 -0
- package/dist/v8/boundedJson.d.ts.map +1 -0
- package/dist/v8/boundedJson.js +44 -0
- package/dist/v8/boundedJson.js.map +1 -0
- package/dist/v8/canonical.d.ts +0 -35
- package/dist/v8/canonical.d.ts.map +1 -1
- package/dist/v8/canonical.js +0 -28
- package/dist/v8/canonical.js.map +1 -1
- package/dist/v8/cli/check/index.d.ts +5 -0
- package/dist/v8/cli/check/index.d.ts.map +1 -1
- package/dist/v8/cli/check/index.js +21 -1
- package/dist/v8/cli/check/index.js.map +1 -1
- package/dist/v8/cli/run.js +1 -1
- package/dist/v8/cli/schema/generate.d.ts +3 -1
- package/dist/v8/cli/schema/generate.d.ts.map +1 -1
- package/dist/v8/cli/schema/generate.js +2 -1
- package/dist/v8/cli/schema/generate.js.map +1 -1
- package/dist/v8/cli/serve/server.d.ts +1 -1
- package/dist/v8/cli/serve/server.d.ts.map +1 -1
- package/dist/v8/cli/serve/server.js +63 -23
- package/dist/v8/cli/serve/server.js.map +1 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +7 -6
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +6 -5
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +49 -39
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/draft.d.ts +1 -1
- package/dist/v8/draft.d.ts.map +1 -1
- package/dist/v8/draft.js +24 -2
- package/dist/v8/draft.js.map +1 -1
- package/dist/v8/draftChanges.d.ts +44 -0
- package/dist/v8/draftChanges.d.ts.map +1 -0
- package/dist/v8/draftChanges.js +157 -0
- package/dist/v8/draftChanges.js.map +1 -0
- package/dist/v8/remoteLoader.d.ts +1 -1
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +19 -256
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/settings.d.ts.map +1 -1
- package/dist/v8/settings.js +9 -6
- package/dist/v8/settings.js.map +1 -1
- package/dist/v8/telemetry.d.ts +4 -0
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +49 -14
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +20 -12
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/protocol/client.ts +1 -4
- package/src/protocol/conformance/cases/guards.ts +10 -188
- package/src/protocol/conformance/cases/reads.ts +29 -69
- package/src/protocol/conformance/cases/writes.ts +7 -35
- package/src/protocol/conformance/contentHashFixtures.ts +0 -57
- package/src/protocol/conformance/context.ts +9 -34
- package/src/protocol/conformance/index.ts +4 -9
- package/src/protocol/errors.ts +5 -10
- package/src/protocol/index.ts +2 -10
- 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 +7 -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 +21 -38
- package/src/v8/boundedJson.ts +47 -0
- package/src/v8/canonical.ts +0 -47
- package/src/v8/cli/check/index.ts +25 -1
- package/src/v8/cli/run.ts +1 -1
- package/src/v8/cli/schema/generate.ts +4 -1
- package/src/v8/cli/serve/server.ts +65 -29
- package/src/v8/cms.ts +7 -8
- package/src/v8/content.ts +55 -42
- package/src/v8/draft.ts +20 -2
- package/src/v8/draftChanges.ts +172 -0
- package/src/v8/remoteLoader.ts +23 -289
- package/src/v8/settings.ts +13 -6
- package/src/v8/telemetry.ts +58 -13
- package/src/v8/types.ts +23 -13
- 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/content.ts
CHANGED
|
@@ -3,24 +3,31 @@
|
|
|
3
3
|
* the caches every client shares, and the release revisions this process has
|
|
4
4
|
* served. Clients only ever see whole `{ revision, blocks }` snapshots.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* A draft is the release with a draft's changes layered over it (see
|
|
7
|
+
* ./draftChanges.ts). Drafts are never recorded as served:
|
|
8
|
+
* `forRevision(revision)` takes a revision string a client may hand back, so
|
|
9
|
+
* it must only ever reach published content, never a draft someone loaded
|
|
10
|
+
* with a pointer.
|
|
9
11
|
*/
|
|
10
12
|
import { formatDraftPointer, parseDraftPointer } from "./draft.ts";
|
|
13
|
+
import { type DraftChanges, fetchDraftChanges, LOCAL_VERSION, layerDraft } from "./draftChanges.ts";
|
|
11
14
|
import { errors, isResolutionError } from "./errors.ts";
|
|
12
15
|
import { isPlainObject } from "./json.ts";
|
|
13
16
|
import type { Loader, Snapshot } from "./types.ts";
|
|
14
17
|
|
|
15
18
|
const SERVED_REVISIONS = 16;
|
|
16
|
-
/**
|
|
19
|
+
/** Fetched drafts kept per CMS. */
|
|
17
20
|
const CACHED_DRAFTS = 3;
|
|
18
|
-
/**
|
|
19
|
-
* How long a loaded draft is reused before `load(pointer)` runs again, so a
|
|
20
|
-
* loader's own checks (a hosted grant's expiry) apply on a warm server too.
|
|
21
|
-
*/
|
|
21
|
+
/** How long a fetched draft is reused before it's fetched again, so its token is checked again. */
|
|
22
22
|
const DRAFT_TTL_MS = 60_000;
|
|
23
23
|
|
|
24
|
+
/** A fetched draft, and the views of it over each release it was layered on. */
|
|
25
|
+
interface CachedDraft {
|
|
26
|
+
changes: Promise<DraftChanges>;
|
|
27
|
+
at: number;
|
|
28
|
+
views: WeakMap<Snapshot, Snapshot>;
|
|
29
|
+
}
|
|
30
|
+
|
|
24
31
|
export function isLoader(content: unknown): content is Loader {
|
|
25
32
|
return (
|
|
26
33
|
(typeof content === "object" || typeof content === "function") &&
|
|
@@ -56,9 +63,7 @@ export class ContentStore {
|
|
|
56
63
|
#source: Snapshot | Loader;
|
|
57
64
|
#release: Promise<Snapshot> | undefined;
|
|
58
65
|
#updating: Promise<{ updated: boolean }> | undefined;
|
|
59
|
-
readonly #drafts = new BoundedMap<string,
|
|
60
|
-
CACHED_DRAFTS,
|
|
61
|
-
);
|
|
66
|
+
readonly #drafts = new BoundedMap<string, CachedDraft>(CACHED_DRAFTS);
|
|
62
67
|
readonly #served = new BoundedMap<string, Snapshot>(SERVED_REVISIONS);
|
|
63
68
|
/** The release this store last handed a client, for a loader that can't peek. */
|
|
64
69
|
#latest: Snapshot | undefined;
|
|
@@ -80,9 +85,9 @@ export class ContentStore {
|
|
|
80
85
|
release(): Promise<Snapshot> {
|
|
81
86
|
const source = this.#source;
|
|
82
87
|
if (!isLoader(source)) return Promise.resolve(this.#serve(source));
|
|
83
|
-
if (!this.updatable) return this.#load(source
|
|
88
|
+
if (!this.updatable) return this.#load(source).then((snapshot) => this.#serve(snapshot));
|
|
84
89
|
if (this.#release === undefined) {
|
|
85
|
-
const pending = this.#load(source
|
|
90
|
+
const pending = this.#load(source).then((snapshot) => this.#serve(snapshot));
|
|
86
91
|
this.#release = pending;
|
|
87
92
|
// A failed load isn't cached: the next client tries again.
|
|
88
93
|
pending.catch(() => {
|
|
@@ -93,33 +98,46 @@ export class ContentStore {
|
|
|
93
98
|
}
|
|
94
99
|
|
|
95
100
|
/**
|
|
96
|
-
* The draft a pointer names
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
+
* The draft a pointer names: its changes, fetched from a preview API domain,
|
|
102
|
+
* layered over the release. A pointer that doesn't parse, or any failure, is
|
|
103
|
+
* `LOADER_FAILED`, never a silent fallback to the release. A pointer whose
|
|
104
|
+
* version is `local` names no draft: the release, with nothing fetched. The
|
|
105
|
+
* changes are keyed by the pointer without its `__variant` parameters (every
|
|
106
|
+
* variant of one draft shares one fetch) and reused for a minute.
|
|
101
107
|
*/
|
|
102
108
|
draft(pointer: string): Promise<Snapshot> {
|
|
103
|
-
const source = this.#source;
|
|
104
|
-
if (!isLoader(source)) return this.release();
|
|
105
109
|
const parsed = parseDraftPointer(pointer);
|
|
106
110
|
if (parsed === null) {
|
|
107
111
|
return Promise.reject(errors.loaderFailed(`invalid draft pointer "${truncate(pointer)}"`));
|
|
108
112
|
}
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
113
|
+
if (parsed.version === LOCAL_VERSION) return this.release();
|
|
114
|
+
const { host, path, version } = parsed;
|
|
115
|
+
const key = formatDraftPointer({ host, path, version });
|
|
116
|
+
let entry = this.#drafts.get(key);
|
|
117
|
+
if (entry === undefined || Date.now() - entry.at >= DRAFT_TTL_MS) {
|
|
118
|
+
const fetched: CachedDraft = {
|
|
119
|
+
changes: fetchDraftChanges(parsed).catch((error: unknown) => {
|
|
120
|
+
throw errors.loaderFailed("the draft's changes couldn't be fetched", error);
|
|
121
|
+
}),
|
|
122
|
+
at: Date.now(),
|
|
123
|
+
views: new WeakMap(),
|
|
124
|
+
};
|
|
125
|
+
this.#drafts.set(key, fetched);
|
|
126
|
+
// A failure isn't reused: the next client fetches again.
|
|
127
|
+
fetched.changes.catch(() => {
|
|
128
|
+
if (this.#drafts.get(key) === fetched) this.#drafts.delete(key);
|
|
129
|
+
});
|
|
130
|
+
entry = fetched;
|
|
131
|
+
}
|
|
132
|
+
const { changes, views } = entry;
|
|
133
|
+
return Promise.all([this.release(), changes]).then(([base, draft]) => {
|
|
134
|
+
let view = views.get(base);
|
|
135
|
+
if (view === undefined) {
|
|
136
|
+
view = layerDraft(base, draft, version);
|
|
137
|
+
views.set(base, view);
|
|
138
|
+
}
|
|
139
|
+
return view;
|
|
121
140
|
});
|
|
122
|
-
return entry.pending;
|
|
123
141
|
}
|
|
124
142
|
|
|
125
143
|
/**
|
|
@@ -151,8 +169,7 @@ export class ContentStore {
|
|
|
151
169
|
this.#release = undefined;
|
|
152
170
|
// `#latest` stays: settings keep the release that was serving until
|
|
153
171
|
// the next one loads, never the defaults (which may allow every host).
|
|
154
|
-
//
|
|
155
|
-
// draft; that copy is as stale as the release now.
|
|
172
|
+
// Drafts are fetched again, so a preview follows the release too.
|
|
156
173
|
this.#drafts.clear();
|
|
157
174
|
}
|
|
158
175
|
return { updated };
|
|
@@ -175,7 +192,6 @@ export class ContentStore {
|
|
|
175
192
|
const previous = this.#source;
|
|
176
193
|
if (source === previous) {
|
|
177
194
|
// The same loader (a hosted remoteLoader that adopted new fallback content): re-read it.
|
|
178
|
-
// Cached drafts were layered over the old content, so they go too.
|
|
179
195
|
this.#release = undefined;
|
|
180
196
|
this.#latest = undefined;
|
|
181
197
|
this.#drafts.clear();
|
|
@@ -188,16 +204,13 @@ export class ContentStore {
|
|
|
188
204
|
this.#drafts.clear();
|
|
189
205
|
}
|
|
190
206
|
|
|
191
|
-
async #load(loader: Loader
|
|
207
|
+
async #load(loader: Loader): Promise<Snapshot> {
|
|
192
208
|
let snapshot: unknown;
|
|
193
209
|
try {
|
|
194
|
-
snapshot =
|
|
210
|
+
snapshot = await loader.load();
|
|
195
211
|
} catch (error) {
|
|
196
212
|
if (isResolutionError(error)) throw error;
|
|
197
|
-
throw errors.loaderFailed(
|
|
198
|
-
pointer === null ? "the content loader failed" : "the draft couldn't load",
|
|
199
|
-
error,
|
|
200
|
-
);
|
|
213
|
+
throw errors.loaderFailed("the content loader failed", error);
|
|
201
214
|
}
|
|
202
215
|
if (!isSnapshot(snapshot)) {
|
|
203
216
|
throw errors.loaderFailed("the content loader returned something other than a snapshot");
|
package/src/v8/draft.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
import type { DraftPointer, ForcedVariant, RequestLike } from "./types.ts";
|
|
13
13
|
|
|
14
14
|
/** The draft cookie's name. Not exported from the package: reading it directly would skip the host check. */
|
|
15
|
-
const DRAFT_COOKIE = "
|
|
15
|
+
const DRAFT_COOKIE = "__deco_draft";
|
|
16
16
|
|
|
17
17
|
const DRAFT_PARAM = "__draft";
|
|
18
18
|
const DRAFT_OFF = "off";
|
|
@@ -128,6 +128,7 @@ function plainVariant({ block, path, index }: ForcedVariant): ForcedVariant {
|
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
function normalizeHost(authority: string): string | null {
|
|
131
|
+
if (authority.startsWith("[")) return normalizeIPv6Host(authority);
|
|
131
132
|
const parts = authority.toLowerCase().split(":");
|
|
132
133
|
if (parts.length > 2) return null;
|
|
133
134
|
const [host, port] = parts;
|
|
@@ -137,9 +138,26 @@ function normalizeHost(authority: string): string | null {
|
|
|
137
138
|
return `${host}:${port}`;
|
|
138
139
|
}
|
|
139
140
|
|
|
141
|
+
/** `[addr]` or `[addr]:port`, the address in the compressed, lowercase form a URL gives it. */
|
|
142
|
+
function normalizeIPv6Host(authority: string): string | null {
|
|
143
|
+
const end = authority.indexOf("]");
|
|
144
|
+
const address = authority.slice(0, end + 1);
|
|
145
|
+
const rest = authority.slice(end + 1);
|
|
146
|
+
if (end === -1 || !/^\[[0-9a-fA-F:.]+\]$/.test(address)) return null;
|
|
147
|
+
if (rest !== "" && (!rest.startsWith(":") || !PORT_RE.test(rest.slice(1)))) return null;
|
|
148
|
+
if (rest !== "" && Number(rest.slice(1)) > 65535) return null;
|
|
149
|
+
let host: string;
|
|
150
|
+
try {
|
|
151
|
+
host = new URL(`http://${address}/`).hostname;
|
|
152
|
+
} catch {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
return `${host}${rest}`;
|
|
156
|
+
}
|
|
157
|
+
|
|
140
158
|
/**
|
|
141
159
|
* The draft pointer a request carries: `?__draft=` from the URL first, then
|
|
142
|
-
* the `
|
|
160
|
+
* the `__deco_draft` cookie. `null` when neither is present, or when the URL
|
|
143
161
|
* says `?__draft=off`. The value is returned as is; `cms.forDraft` validates it.
|
|
144
162
|
*/
|
|
145
163
|
export function readDraftPointer(request: RequestLike): string | null {
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Draft changes (see /next/content-delivery#draft-previews): what a draft
|
|
3
|
+
* pointer's address answers, and how the CMS layers it over production.
|
|
4
|
+
*
|
|
5
|
+
* - The pointer's host must fall under one of the preview API domains (the
|
|
6
|
+
* same rule as v7): by default `*.decocms.com` (Studio, its PR previews,
|
|
7
|
+
* `local.studio.decocms.com`) and the loopback hosts; `DECO_PREVIEW_API_DOMAINS`
|
|
8
|
+
* replaces the list. Any other host is refused before anything is fetched.
|
|
9
|
+
* - `GET <scheme>://<host><path>&v=<version>`: the scheme comes from the
|
|
10
|
+
* domain that admitted the host, never from the pointer: plain `http` only
|
|
11
|
+
* for loopback hosts. The pointer's `__variant` parameters are already gone (the parser
|
|
12
|
+
* lifts them out), no cookies or credentials are sent, and redirects aren't
|
|
13
|
+
* followed (a response that was redirected anyway is refused).
|
|
14
|
+
* - Only a 200 is accepted, within 10 s and up to 16 MiB, shaped exactly
|
|
15
|
+
* `{ format: 1, set: { <name>: <block JSON> }, delete: [<name>…] }` with
|
|
16
|
+
* `set` and `delete` disjoint.
|
|
17
|
+
*/
|
|
18
|
+
import { readBoundedJson, timeoutSignal } from "./boundedJson.ts";
|
|
19
|
+
import { readEnv } from "./identity.ts";
|
|
20
|
+
import { isPlainObject } from "./json.ts";
|
|
21
|
+
import type { DraftPointer, Snapshot } from "./types.ts";
|
|
22
|
+
|
|
23
|
+
/** The version a pointer has when it names no draft: `deco serve`'s working tree. */
|
|
24
|
+
export const LOCAL_VERSION = "local";
|
|
25
|
+
|
|
26
|
+
const FORMAT = 1;
|
|
27
|
+
const TIMEOUT_MS = 10_000;
|
|
28
|
+
const MAX_BYTES = 16 * 1024 * 1024;
|
|
29
|
+
|
|
30
|
+
/** What a draft changed compared with production: whole blocks, and deletions. */
|
|
31
|
+
export interface DraftChanges {
|
|
32
|
+
format: typeof FORMAT;
|
|
33
|
+
set: Record<string, unknown>;
|
|
34
|
+
delete: string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The domains a draft pointer's host may fall under, the defaults v7 shipped.
|
|
39
|
+
* Deco operates every one of them, so the defaults add no SSRF surface. An
|
|
40
|
+
* entry starting with a dot matches any host that ends with it, on a label
|
|
41
|
+
* boundary (`evil-decocms.com` doesn't pass `.decocms.com`); any other entry
|
|
42
|
+
* matches that exact host. The first entry that matches decides whether a
|
|
43
|
+
* port and plain `http` are allowed.
|
|
44
|
+
*/
|
|
45
|
+
export const DEFAULT_PREVIEW_API_DOMAINS: readonly string[] = [
|
|
46
|
+
"local.studio.decocms.com", // the Studio dev origin (https, with a port)
|
|
47
|
+
"localhost",
|
|
48
|
+
"127.0.0.1",
|
|
49
|
+
".localhost",
|
|
50
|
+
".decocms.com", // Studio and its preview deployments
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
/** `DECO_PREVIEW_API_DOMAINS` (a comma list) when set, else the defaults. */
|
|
54
|
+
function previewApiDomains(): readonly string[] {
|
|
55
|
+
const configured = (readEnv("DECO_PREVIEW_API_DOMAINS") ?? "")
|
|
56
|
+
.split(",")
|
|
57
|
+
.map((domain) => domain.trim().toLowerCase())
|
|
58
|
+
.filter(Boolean);
|
|
59
|
+
return configured.length > 0 ? configured : DEFAULT_PREVIEW_API_DOMAINS;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The origin a pointer's host is fetched from, or `null` when no preview API
|
|
64
|
+
* domain admits it. Loopback domains (and `local.studio.decocms.com`) may
|
|
65
|
+
* carry a port; a public domain may not, so a pointer can't aim the fetch at
|
|
66
|
+
* an odd port. Loopback hosts are `http`; everything else is `https`.
|
|
67
|
+
*/
|
|
68
|
+
export function previewApiOrigin(authority: string): string | null {
|
|
69
|
+
const lower = authority.toLowerCase();
|
|
70
|
+
const end = lower.startsWith("[") ? lower.indexOf("]") + 1 : -1;
|
|
71
|
+
const host = end > 0 ? lower.slice(0, end) : lower.split(":")[0]!;
|
|
72
|
+
const rest = end > 0 ? lower.slice(end) : lower.slice(host.length);
|
|
73
|
+
const port = rest.startsWith(":") ? rest.slice(1) : undefined;
|
|
74
|
+
if (!host) return null;
|
|
75
|
+
const domain = previewApiDomains().find((d) =>
|
|
76
|
+
d.startsWith(".") ? host.length > d.length && host.endsWith(d) : host === d,
|
|
77
|
+
);
|
|
78
|
+
if (domain === undefined) return null;
|
|
79
|
+
const insecure = isLoopbackDomain(domain);
|
|
80
|
+
if (port !== undefined && !insecure && domain !== "local.studio.decocms.com") return null;
|
|
81
|
+
return `${insecure ? "http" : "https"}://${host}${port === undefined ? "" : `:${port}`}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function isLoopbackDomain(domain: string): boolean {
|
|
85
|
+
return (
|
|
86
|
+
domain === "localhost" ||
|
|
87
|
+
domain === "127.0.0.1" ||
|
|
88
|
+
domain === "[::1]" ||
|
|
89
|
+
domain.endsWith(".localhost")
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* The URL the SDK fetches for a pointer: the origin its host is admitted
|
|
95
|
+
* under, the version as `v`. `null` when no preview API domain admits the host.
|
|
96
|
+
*/
|
|
97
|
+
export function draftChangesUrl(pointer: DraftPointer): string | null {
|
|
98
|
+
const origin = previewApiOrigin(pointer.host);
|
|
99
|
+
if (origin === null) return null;
|
|
100
|
+
const separator = pointer.path.includes("?") ? "&" : "?";
|
|
101
|
+
return `${origin}${pointer.path}${separator}v=${encodeURIComponent(pointer.version)}`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Fetches a draft's changes; rejects on a host no preview API domain admits (without fetching) or on any failure. */
|
|
105
|
+
export async function fetchDraftChanges(pointer: DraftPointer): Promise<DraftChanges> {
|
|
106
|
+
const url = draftChangesUrl(pointer);
|
|
107
|
+
if (url === null) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`draft pointer names "${pointer.host}", which isn't under a preview API domain ` +
|
|
110
|
+
"(DECO_PREVIEW_API_DOMAINS); nothing was fetched",
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
const response = await fetch(url, {
|
|
114
|
+
headers: { accept: "application/json" },
|
|
115
|
+
redirect: "manual",
|
|
116
|
+
signal: timeoutSignal(TIMEOUT_MS),
|
|
117
|
+
});
|
|
118
|
+
// Some fetch polyfills (React Native, whatwg-fetch) ignore `redirect: "manual"`
|
|
119
|
+
// and follow anyway: refuse a response that came from anywhere else.
|
|
120
|
+
if (
|
|
121
|
+
response.redirected ||
|
|
122
|
+
(response.url && new URL(response.url).origin !== new URL(url).origin)
|
|
123
|
+
) {
|
|
124
|
+
await response.body?.cancel();
|
|
125
|
+
throw new Error("draft changes: the pointer's address redirected; nothing was read");
|
|
126
|
+
}
|
|
127
|
+
if (response.status !== 200) {
|
|
128
|
+
await response.body?.cancel();
|
|
129
|
+
throw new Error(`draft changes: HTTP ${response.status}`);
|
|
130
|
+
}
|
|
131
|
+
return parseDraftChanges(await readBoundedJson(response, "draft changes", MAX_BYTES));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Validates a draft changes response; throws on an unknown format or any unexpected shape. */
|
|
135
|
+
export function parseDraftChanges(body: unknown): DraftChanges {
|
|
136
|
+
const invalid = (why: string) => new Error(`draft changes: ${why}`);
|
|
137
|
+
if (!isPlainObject(body) || body.format !== FORMAT) throw invalid("unknown format");
|
|
138
|
+
if (Object.keys(body).some((key) => key !== "format" && key !== "set" && key !== "delete")) {
|
|
139
|
+
throw invalid("unexpected field");
|
|
140
|
+
}
|
|
141
|
+
const { set, delete: deleted } = body;
|
|
142
|
+
if (!isPlainObject(set)) throw invalid("set must be an object");
|
|
143
|
+
if (!Array.isArray(deleted) || deleted.some((name) => typeof name !== "string")) {
|
|
144
|
+
throw invalid("delete must be a list of names");
|
|
145
|
+
}
|
|
146
|
+
if (new Set(deleted).size !== deleted.length) throw invalid("delete repeats a name");
|
|
147
|
+
if (deleted.some((name) => Object.hasOwn(set, name))) throw invalid("set and delete overlap");
|
|
148
|
+
return body as unknown as DraftChanges;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The draft as a snapshot: production's entries, with the changed ones
|
|
153
|
+
* replaced whole and the deleted ones gone. A shallow copy: every unchanged
|
|
154
|
+
* entry is production's own object, and production is never mutated. Its
|
|
155
|
+
* revision is an opaque identity of the pair, never a release's.
|
|
156
|
+
*/
|
|
157
|
+
export function layerDraft(base: Snapshot, changes: DraftChanges, version: string): Snapshot {
|
|
158
|
+
const blocks: Record<string, unknown> = { ...base.blocks };
|
|
159
|
+
for (const name of changes.delete) delete blocks[name];
|
|
160
|
+
for (const [name, entry] of Object.entries(changes.set)) {
|
|
161
|
+
// defineProperty, so an entry named "__proto__" stays an entry.
|
|
162
|
+
Object.defineProperty(blocks, name, {
|
|
163
|
+
value: entry,
|
|
164
|
+
enumerable: true,
|
|
165
|
+
writable: true,
|
|
166
|
+
configurable: true,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const view: Snapshot = { revision: `${base.revision}~${version}`, blocks };
|
|
170
|
+
if (base.aliases !== undefined) view.aliases = base.aliases;
|
|
171
|
+
return view;
|
|
172
|
+
}
|