@decocms/blocks 8.1.0-next.6 → 8.1.0
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/v8/cli/check/index.d.ts +6 -2
- package/dist/v8/cli/check/index.d.ts.map +1 -1
- package/dist/v8/cli/check/index.js +34 -7
- package/dist/v8/cli/check/index.js.map +1 -1
- package/dist/v8/cli/color.d.ts +14 -0
- package/dist/v8/cli/color.d.ts.map +1 -0
- package/dist/v8/cli/color.js +14 -0
- package/dist/v8/cli/color.js.map +1 -0
- 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/log.d.ts +4 -0
- package/dist/v8/cli/log.d.ts.map +1 -1
- package/dist/v8/cli/log.js +9 -0
- package/dist/v8/cli/log.js.map +1 -1
- package/dist/v8/cli/run.d.ts +1 -0
- package/dist/v8/cli/run.d.ts.map +1 -1
- package/dist/v8/cli/run.js +18 -4
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cli/schema/index.d.ts.map +1 -1
- package/dist/v8/cli/schema/index.js +5 -3
- package/dist/v8/cli/schema/index.js.map +1 -1
- package/dist/v8/cli/schema/tsProgram.d.ts +1 -0
- package/dist/v8/cli/schema/tsProgram.d.ts.map +1 -1
- package/dist/v8/cli/schema/tsProgram.js +1 -0
- package/dist/v8/cli/schema/tsProgram.js.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.d.ts.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.js +50 -7
- package/dist/v8/cli/schema/typeToSchema.js.map +1 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +16 -19
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +4 -5
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +46 -44
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/draftChanges.d.ts +37 -14
- package/dist/v8/draftChanges.d.ts.map +1 -1
- package/dist/v8/draftChanges.js +66 -44
- 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/matchRoute.d.ts +12 -10
- package/dist/v8/matchRoute.d.ts.map +1 -1
- package/dist/v8/matchRoute.js +60 -86
- package/dist/v8/matchRoute.js.map +1 -1
- package/dist/v8/remoteLoader.d.ts +4 -4
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +106 -96
- package/dist/v8/remoteLoader.js.map +1 -1
- package/dist/v8/routeTrie.d.ts +43 -0
- package/dist/v8/routeTrie.d.ts.map +1 -0
- package/dist/v8/routeTrie.js +88 -0
- package/dist/v8/routeTrie.js.map +1 -0
- package/dist/v8/telemetry.d.ts +7 -7
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +24 -100
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +59 -17
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/v8/cli/check/index.ts +32 -7
- package/src/v8/cli/color.ts +29 -0
- package/src/v8/cli/content.ts +85 -5
- package/src/v8/cli/log.ts +12 -0
- package/src/v8/cli/run.ts +19 -4
- package/src/v8/cli/schema/index.ts +5 -3
- package/src/v8/cli/schema/tsProgram.ts +2 -1
- package/src/v8/cli/schema/typeToSchema.ts +47 -7
- package/src/v8/cms.ts +17 -20
- package/src/v8/content.ts +54 -46
- package/src/v8/draftChanges.ts +79 -43
- package/src/v8/identity.ts +0 -10
- package/src/v8/matchRoute.ts +70 -106
- package/src/v8/remoteLoader.ts +112 -111
- package/src/v8/routeTrie.ts +103 -0
- package/src/v8/telemetry.ts +27 -108
- package/src/v8/types.ts +60 -15
package/src/v8/content.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The content side of a CMS: one source (the content module or a `Loader`)
|
|
3
|
-
* the caches every client shares
|
|
4
|
-
*
|
|
2
|
+
* The content side of a CMS: one source (the content module or a `Loader`)
|
|
3
|
+
* and the caches every client shares. Clients only ever see whole
|
|
4
|
+
* `{ revision, blocks }` snapshots.
|
|
5
5
|
*
|
|
6
6
|
* A draft is the release with a draft's changes layered over it (see
|
|
7
|
-
* ./draftChanges.ts).
|
|
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.
|
|
7
|
+
* ./draftChanges.ts).
|
|
11
8
|
*/
|
|
12
9
|
import { formatDraftPointer, parseDraftPointer } from "./draft.ts";
|
|
13
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
DEFAULT_DRAFT_HOSTS,
|
|
12
|
+
type DraftChanges,
|
|
13
|
+
fetchDraftChanges,
|
|
14
|
+
LOCAL_VERSION,
|
|
15
|
+
layerDraft,
|
|
16
|
+
} from "./draftChanges.ts";
|
|
14
17
|
import { errors, isResolutionError } from "./errors.ts";
|
|
15
18
|
import { isPlainObject } from "./json.ts";
|
|
16
19
|
import type { Loader, Snapshot } from "./types.ts";
|
|
17
20
|
|
|
18
|
-
const SERVED_REVISIONS = 16;
|
|
19
21
|
/** Fetched drafts kept per CMS. */
|
|
20
22
|
const CACHED_DRAFTS = 3;
|
|
21
|
-
/** How long a fetched draft is reused before it's fetched again, so its token is checked again. */
|
|
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. */
|
|
24
|
+
/** A fetched draft body, its ETag, and the views of it over each release it was layered on. */
|
|
25
25
|
interface CachedDraft {
|
|
26
|
-
changes:
|
|
27
|
-
|
|
26
|
+
changes: DraftChanges;
|
|
27
|
+
etag: string | null;
|
|
28
28
|
views: WeakMap<Snapshot, Snapshot>;
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -55,7 +55,9 @@ export function isSnapshot(content: unknown): content is Snapshot {
|
|
|
55
55
|
isPlainObject(content) &&
|
|
56
56
|
typeof content.revision === "string" &&
|
|
57
57
|
isPlainObject(content.blocks) &&
|
|
58
|
-
(content.aliases === undefined || isPlainObject(content.aliases))
|
|
58
|
+
(content.aliases === undefined || isPlainObject(content.aliases)) &&
|
|
59
|
+
(content.schemaHash === undefined || typeof content.schemaHash === "string") &&
|
|
60
|
+
(content.committedAt === undefined || typeof content.committedAt === "string")
|
|
59
61
|
);
|
|
60
62
|
}
|
|
61
63
|
|
|
@@ -64,12 +66,15 @@ export class ContentStore {
|
|
|
64
66
|
#release: Promise<Snapshot> | undefined;
|
|
65
67
|
#updating: Promise<{ updated: boolean }> | undefined;
|
|
66
68
|
readonly #drafts = new BoundedMap<string, CachedDraft>(CACHED_DRAFTS);
|
|
67
|
-
|
|
69
|
+
/** Draft reads in flight, so concurrent renders of one draft share one request. */
|
|
70
|
+
readonly #draftReads = new Map<string, Promise<CachedDraft>>();
|
|
71
|
+
readonly #draftHosts: readonly string[];
|
|
68
72
|
/** The release this store last handed a client, for a loader that can't peek. */
|
|
69
73
|
#latest: Snapshot | undefined;
|
|
70
74
|
|
|
71
|
-
constructor(source: Snapshot | Loader) {
|
|
75
|
+
constructor(source: Snapshot | Loader, draftHosts: readonly string[] = DEFAULT_DRAFT_HOSTS) {
|
|
72
76
|
this.#source = source;
|
|
77
|
+
this.#draftHosts = draftHosts;
|
|
73
78
|
}
|
|
74
79
|
|
|
75
80
|
/** Whether the source can change while the process runs (has `update()`). */
|
|
@@ -98,12 +103,13 @@ export class ContentStore {
|
|
|
98
103
|
}
|
|
99
104
|
|
|
100
105
|
/**
|
|
101
|
-
* The draft a pointer names: its changes, fetched from a
|
|
106
|
+
* The draft a pointer names: its changes, fetched from a draft host,
|
|
102
107
|
* layered over the release. A pointer that doesn't parse, or any failure, is
|
|
103
108
|
* `LOADER_FAILED`, never a silent fallback to the release. A pointer whose
|
|
104
109
|
* version is `local` names no draft: the release, with nothing fetched. The
|
|
105
110
|
* changes are keyed by the pointer without its `__variant` parameters (every
|
|
106
|
-
* variant of one draft shares one
|
|
111
|
+
* variant of one draft shares one body) and revalidated on every read: a
|
|
112
|
+
* `304` reuses the body held, a `200` replaces it.
|
|
107
113
|
*/
|
|
108
114
|
draft(pointer: string): Promise<Snapshot> {
|
|
109
115
|
const parsed = parseDraftPointer(pointer);
|
|
@@ -113,28 +119,38 @@ export class ContentStore {
|
|
|
113
119
|
if (parsed.version === LOCAL_VERSION) return this.release();
|
|
114
120
|
const { host, path, version } = parsed;
|
|
115
121
|
const key = formatDraftPointer({ host, path, version });
|
|
116
|
-
let
|
|
117
|
-
if (
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
122
|
+
let read = this.#draftReads.get(key);
|
|
123
|
+
if (read === undefined) {
|
|
124
|
+
const held = this.#drafts.get(key);
|
|
125
|
+
read = fetchDraftChanges(parsed, { domains: this.#draftHosts, etag: held?.etag }).then(
|
|
126
|
+
(result) => {
|
|
127
|
+
if (result.status === 304 && held !== undefined) return held;
|
|
128
|
+
if (result.status === 304) throw new Error("draft changes: 304 without a held body");
|
|
129
|
+
const entry: CachedDraft = {
|
|
130
|
+
changes: result.changes,
|
|
131
|
+
etag: result.etag,
|
|
132
|
+
views: new WeakMap(),
|
|
133
|
+
};
|
|
134
|
+
this.#drafts.set(key, entry);
|
|
135
|
+
return entry;
|
|
136
|
+
},
|
|
137
|
+
);
|
|
138
|
+
const pending = read;
|
|
139
|
+
this.#draftReads.set(key, pending);
|
|
140
|
+
const settle = () => {
|
|
141
|
+
if (this.#draftReads.get(key) === pending) this.#draftReads.delete(key);
|
|
124
142
|
};
|
|
125
|
-
|
|
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;
|
|
143
|
+
pending.then(settle, settle);
|
|
131
144
|
}
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
145
|
+
const changes = read.catch((error: unknown) => {
|
|
146
|
+
throw errors.loaderFailed("the draft's changes couldn't be fetched", error);
|
|
147
|
+
});
|
|
148
|
+
return Promise.all([this.release(), changes]).then(([base, entry]) => {
|
|
149
|
+
let view = entry.views.get(base);
|
|
135
150
|
if (view === undefined) {
|
|
136
|
-
view
|
|
137
|
-
|
|
151
|
+
// OPEN: the view is keyed by the body's ETag (the pointer outlives later saves); the version without one.
|
|
152
|
+
view = layerDraft(base, entry.changes, entry.etag ?? version);
|
|
153
|
+
entry.views.set(base, view);
|
|
138
154
|
}
|
|
139
155
|
return view;
|
|
140
156
|
});
|
|
@@ -149,12 +165,6 @@ export class ContentStore {
|
|
|
149
165
|
return peekRelease(this.#source) ?? this.#latest;
|
|
150
166
|
}
|
|
151
167
|
|
|
152
|
-
/** A release revision this store has served, or the release when it's unknown (drafts included). */
|
|
153
|
-
revision(revision: string): Promise<Snapshot> {
|
|
154
|
-
const served = this.#served.get(revision);
|
|
155
|
-
return served === undefined ? this.release() : Promise.resolve(served);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
168
|
/** Asks the source for newer content; never throws, and concurrent calls share one check. */
|
|
159
169
|
update(): Promise<{ updated: boolean }> {
|
|
160
170
|
const source = this.#source;
|
|
@@ -197,7 +207,6 @@ export class ContentStore {
|
|
|
197
207
|
this.#drafts.clear();
|
|
198
208
|
return;
|
|
199
209
|
}
|
|
200
|
-
if (isSnapshot(previous)) this.#served.delete(previous.revision);
|
|
201
210
|
this.#source = source;
|
|
202
211
|
this.#release = undefined;
|
|
203
212
|
this.#latest = undefined;
|
|
@@ -220,7 +229,6 @@ export class ContentStore {
|
|
|
220
229
|
|
|
221
230
|
#serve(snapshot: Snapshot): Snapshot {
|
|
222
231
|
this.#latest = snapshot;
|
|
223
|
-
this.#served.set(snapshot.revision, snapshot);
|
|
224
232
|
return snapshot;
|
|
225
233
|
}
|
|
226
234
|
}
|
package/src/v8/draftChanges.ts
CHANGED
|
@@ -2,38 +2,43 @@
|
|
|
2
2
|
* Draft changes (see /next/content-delivery#draft-previews): what a draft
|
|
3
3
|
* pointer's address answers, and how the CMS layers it over production.
|
|
4
4
|
*
|
|
5
|
-
* - The pointer's host must fall under one of the
|
|
6
|
-
*
|
|
7
|
-
* `local.studio.decocms.com`) and the loopback hosts;
|
|
8
|
-
* replaces the list. Any other host
|
|
9
|
-
*
|
|
5
|
+
* - The pointer's host must fall under one of the draft hosts (the same rule
|
|
6
|
+
* as v7's preview API domains): by default `*.decocms.com` (Studio, the
|
|
7
|
+
* delivery CDN, `local.studio.decocms.com`) and the loopback hosts;
|
|
8
|
+
* `createCMS({ preview: { draftHosts } })` replaces the list. Any other host
|
|
9
|
+
* is refused before anything is fetched.
|
|
10
|
+
* - `GET <scheme>://<host><path>?v=<version>`: the scheme comes from the
|
|
10
11
|
* domain that admitted the host, never from the pointer: plain `http` only
|
|
11
12
|
* for loopback hosts. The pointer's `__variant` parameters are already gone (the parser
|
|
12
13
|
* lifts them out), no cookies or credentials are sent, and redirects aren't
|
|
13
14
|
* followed (a response that was redirected anyway is refused).
|
|
14
|
-
* -
|
|
15
|
-
* `
|
|
16
|
-
*
|
|
15
|
+
* - Every read revalidates: with a body this process already holds, it sends
|
|
16
|
+
* `If-None-Match` with that body's ETag, and a `304` reuses the body. Only
|
|
17
|
+
* a 200 or that 304 is accepted, within 10 s; a 200 is up to 16 MiB, shaped
|
|
18
|
+
* exactly `{ set: { <name>: <block JSON> }, delete: [<name>…] }` with `set`
|
|
19
|
+
* and `delete` disjoint.
|
|
17
20
|
*/
|
|
18
21
|
import { readBoundedJson, timeoutSignal } from "./boundedJson.ts";
|
|
19
|
-
import { readEnv } from "./identity.ts";
|
|
20
22
|
import { isPlainObject } from "./json.ts";
|
|
21
23
|
import type { DraftPointer, Snapshot } from "./types.ts";
|
|
22
24
|
|
|
23
25
|
/** The version a pointer has when it names no draft: `deco serve`'s working tree. */
|
|
24
26
|
export const LOCAL_VERSION = "local";
|
|
25
27
|
|
|
26
|
-
const FORMAT = 1;
|
|
27
28
|
const TIMEOUT_MS = 10_000;
|
|
28
29
|
const MAX_BYTES = 16 * 1024 * 1024;
|
|
29
30
|
|
|
30
31
|
/** What a draft changed compared with production: whole blocks, and deletions. */
|
|
31
32
|
export interface DraftChanges {
|
|
32
|
-
format: typeof FORMAT;
|
|
33
33
|
set: Record<string, unknown>;
|
|
34
34
|
delete: string[];
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/** A draft read: new changes (and their ETag), or `304`, the held body is current. */
|
|
38
|
+
export type DraftRead =
|
|
39
|
+
| { status: 200; changes: DraftChanges; etag: string | null }
|
|
40
|
+
| { status: 304 };
|
|
41
|
+
|
|
37
42
|
/**
|
|
38
43
|
* The domains a draft pointer's host may fall under, the defaults v7 shipped.
|
|
39
44
|
* Deco operates every one of them, so the defaults add no SSRF surface. An
|
|
@@ -42,37 +47,48 @@ export interface DraftChanges {
|
|
|
42
47
|
* matches that exact host. The first entry that matches decides whether a
|
|
43
48
|
* port and plain `http` are allowed.
|
|
44
49
|
*/
|
|
45
|
-
export const
|
|
50
|
+
export const DEFAULT_DRAFT_HOSTS: readonly string[] = [
|
|
46
51
|
"local.studio.decocms.com", // the Studio dev origin (https, with a port)
|
|
47
52
|
"localhost",
|
|
48
53
|
"127.0.0.1",
|
|
49
54
|
".localhost",
|
|
50
|
-
".decocms.com", // Studio
|
|
55
|
+
".decocms.com", // Studio, its preview deployments and delivery.decocms.com
|
|
51
56
|
];
|
|
52
57
|
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
/**
|
|
59
|
+
* `createCMS`'s `preview.draftHosts`, trimmed and lowercased, or the defaults
|
|
60
|
+
* when it's left out. Throws a `TypeError` on anything but a list of
|
|
61
|
+
* non-empty strings.
|
|
62
|
+
*/
|
|
63
|
+
export function parseDraftHosts(preview: { draftHosts?: unknown } | undefined): readonly string[] {
|
|
64
|
+
const domains = preview?.draftHosts;
|
|
65
|
+
if (domains === undefined) return DEFAULT_DRAFT_HOSTS;
|
|
66
|
+
if (
|
|
67
|
+
!Array.isArray(domains) ||
|
|
68
|
+
domains.some((domain) => typeof domain !== "string" || domain.trim() === "")
|
|
69
|
+
) {
|
|
70
|
+
throw new TypeError("createCMS: `preview.draftHosts` must be a list of hosts");
|
|
71
|
+
}
|
|
72
|
+
return domains.map((domain: string) => domain.trim().toLowerCase());
|
|
60
73
|
}
|
|
61
74
|
|
|
62
75
|
/**
|
|
63
|
-
* The origin a pointer's host is fetched from, or `null` when no
|
|
64
|
-
*
|
|
76
|
+
* The origin a pointer's host is fetched from, or `null` when no draft host
|
|
77
|
+
* admits it. Loopback domains (and `local.studio.decocms.com`) may
|
|
65
78
|
* carry a port; a public domain may not, so a pointer can't aim the fetch at
|
|
66
79
|
* an odd port. Loopback hosts are `http`; everything else is `https`.
|
|
67
80
|
*/
|
|
68
|
-
export function previewApiOrigin(
|
|
81
|
+
export function previewApiOrigin(
|
|
82
|
+
authority: string,
|
|
83
|
+
domains: readonly string[] = DEFAULT_DRAFT_HOSTS,
|
|
84
|
+
): string | null {
|
|
69
85
|
const lower = authority.toLowerCase();
|
|
70
86
|
const end = lower.startsWith("[") ? lower.indexOf("]") + 1 : -1;
|
|
71
87
|
const host = end > 0 ? lower.slice(0, end) : lower.split(":")[0]!;
|
|
72
88
|
const rest = end > 0 ? lower.slice(end) : lower.slice(host.length);
|
|
73
89
|
const port = rest.startsWith(":") ? rest.slice(1) : undefined;
|
|
74
90
|
if (!host) return null;
|
|
75
|
-
const domain =
|
|
91
|
+
const domain = domains.find((d) =>
|
|
76
92
|
d.startsWith(".") ? host.length > d.length && host.endsWith(d) : host === d,
|
|
77
93
|
);
|
|
78
94
|
if (domain === undefined) return null;
|
|
@@ -92,26 +108,39 @@ function isLoopbackDomain(domain: string): boolean {
|
|
|
92
108
|
|
|
93
109
|
/**
|
|
94
110
|
* The URL the SDK fetches for a pointer: the origin its host is admitted
|
|
95
|
-
* under, the version as `v`. `null` when no
|
|
111
|
+
* under, the version as `v`. `null` when no draft host admits the host.
|
|
96
112
|
*/
|
|
97
|
-
export function draftChangesUrl(
|
|
98
|
-
|
|
113
|
+
export function draftChangesUrl(
|
|
114
|
+
pointer: DraftPointer,
|
|
115
|
+
domains: readonly string[] = DEFAULT_DRAFT_HOSTS,
|
|
116
|
+
): string | null {
|
|
117
|
+
const origin = previewApiOrigin(pointer.host, domains);
|
|
99
118
|
if (origin === null) return null;
|
|
100
119
|
const separator = pointer.path.includes("?") ? "&" : "?";
|
|
101
120
|
return `${origin}${pointer.path}${separator}v=${encodeURIComponent(pointer.version)}`;
|
|
102
121
|
}
|
|
103
122
|
|
|
104
|
-
/**
|
|
105
|
-
|
|
106
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Reads a draft's changes, revalidating `etag` (the ETag of the body the
|
|
125
|
+
* caller holds) when given. Rejects on a host no draft host admits
|
|
126
|
+
* (without fetching), on a `304` to a request that sent no ETag, and on any
|
|
127
|
+
* other failure.
|
|
128
|
+
*/
|
|
129
|
+
export async function fetchDraftChanges(
|
|
130
|
+
pointer: DraftPointer,
|
|
131
|
+
options: { domains?: readonly string[]; etag?: string | null } = {},
|
|
132
|
+
): Promise<DraftRead> {
|
|
133
|
+
const url = draftChangesUrl(pointer, options.domains);
|
|
107
134
|
if (url === null) {
|
|
108
135
|
throw new Error(
|
|
109
|
-
`draft pointer names "${pointer.host}", which isn't under a
|
|
110
|
-
"(
|
|
136
|
+
`draft pointer names "${pointer.host}", which isn't under a draft host ` +
|
|
137
|
+
"(createCMS({ preview: { draftHosts } })); nothing was fetched",
|
|
111
138
|
);
|
|
112
139
|
}
|
|
140
|
+
const headers: Record<string, string> = { accept: "application/json" };
|
|
141
|
+
if (options.etag) headers["if-none-match"] = options.etag;
|
|
113
142
|
const response = await fetch(url, {
|
|
114
|
-
headers
|
|
143
|
+
headers,
|
|
115
144
|
redirect: "manual",
|
|
116
145
|
signal: timeoutSignal(TIMEOUT_MS),
|
|
117
146
|
});
|
|
@@ -124,18 +153,24 @@ export async function fetchDraftChanges(pointer: DraftPointer): Promise<DraftCha
|
|
|
124
153
|
await response.body?.cancel();
|
|
125
154
|
throw new Error("draft changes: the pointer's address redirected; nothing was read");
|
|
126
155
|
}
|
|
156
|
+
if (response.status === 304 && options.etag) {
|
|
157
|
+
await response.body?.cancel();
|
|
158
|
+
return { status: 304 };
|
|
159
|
+
}
|
|
127
160
|
if (response.status !== 200) {
|
|
128
161
|
await response.body?.cancel();
|
|
129
162
|
throw new Error(`draft changes: HTTP ${response.status}`);
|
|
130
163
|
}
|
|
131
|
-
|
|
164
|
+
const etag = response.headers.get("etag");
|
|
165
|
+
const changes = parseDraftChanges(await readBoundedJson(response, "draft changes", MAX_BYTES));
|
|
166
|
+
return { status: 200, changes, etag };
|
|
132
167
|
}
|
|
133
168
|
|
|
134
|
-
/** Validates a draft changes response; throws on
|
|
169
|
+
/** Validates a draft changes response; throws on any unexpected shape. */
|
|
135
170
|
export function parseDraftChanges(body: unknown): DraftChanges {
|
|
136
171
|
const invalid = (why: string) => new Error(`draft changes: ${why}`);
|
|
137
|
-
if (!isPlainObject(body)
|
|
138
|
-
if (Object.keys(body).some((key) => key !== "
|
|
172
|
+
if (!isPlainObject(body)) throw invalid("expected an object");
|
|
173
|
+
if (Object.keys(body).some((key) => key !== "set" && key !== "delete")) {
|
|
139
174
|
throw invalid("unexpected field");
|
|
140
175
|
}
|
|
141
176
|
const { set, delete: deleted } = body;
|
|
@@ -143,7 +178,6 @@ export function parseDraftChanges(body: unknown): DraftChanges {
|
|
|
143
178
|
if (!Array.isArray(deleted) || deleted.some((name) => typeof name !== "string")) {
|
|
144
179
|
throw invalid("delete must be a list of names");
|
|
145
180
|
}
|
|
146
|
-
if (new Set(deleted).size !== deleted.length) throw invalid("delete repeats a name");
|
|
147
181
|
if (deleted.some((name) => Object.hasOwn(set, name))) throw invalid("set and delete overlap");
|
|
148
182
|
return body as unknown as DraftChanges;
|
|
149
183
|
}
|
|
@@ -152,9 +186,11 @@ export function parseDraftChanges(body: unknown): DraftChanges {
|
|
|
152
186
|
* The draft as a snapshot: production's entries, with the changed ones
|
|
153
187
|
* replaced whole and the deleted ones gone. A shallow copy: every unchanged
|
|
154
188
|
* entry is production's own object, and production is never mutated. Its
|
|
155
|
-
* revision is an opaque identity of the pair,
|
|
189
|
+
* revision, `<base revision>~<view>`, is an opaque identity of the pair,
|
|
190
|
+
* never a release's; `view` names the draft body (its ETag), so each save
|
|
191
|
+
* re-keys what's cached by revision.
|
|
156
192
|
*/
|
|
157
|
-
export function layerDraft(base: Snapshot, changes: DraftChanges,
|
|
193
|
+
export function layerDraft(base: Snapshot, changes: DraftChanges, view: string): Snapshot {
|
|
158
194
|
const blocks: Record<string, unknown> = { ...base.blocks };
|
|
159
195
|
for (const name of changes.delete) delete blocks[name];
|
|
160
196
|
for (const [name, entry] of Object.entries(changes.set)) {
|
|
@@ -166,7 +202,7 @@ export function layerDraft(base: Snapshot, changes: DraftChanges, version: strin
|
|
|
166
202
|
configurable: true,
|
|
167
203
|
});
|
|
168
204
|
}
|
|
169
|
-
const
|
|
170
|
-
if (base.aliases !== undefined)
|
|
171
|
-
return
|
|
205
|
+
const layered: Snapshot = { revision: `${base.revision}~${view}`, blocks };
|
|
206
|
+
if (base.aliases !== undefined) layered.aliases = base.aliases;
|
|
207
|
+
return layered;
|
|
172
208
|
}
|
package/src/v8/identity.ts
CHANGED
|
@@ -46,16 +46,6 @@ export function fnv1a(input: string): string {
|
|
|
46
46
|
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
/** Reads an environment variable where there is one (Node, Bun, Workers with nodejs_compat). */
|
|
50
|
-
export function readEnv(name: string): string | undefined {
|
|
51
|
-
try {
|
|
52
|
-
return (globalThis as { process?: { env?: Record<string, string | undefined> } }).process
|
|
53
|
-
?.env?.[name];
|
|
54
|
-
} catch {
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
49
|
/** Clears every global instance whose registry key starts with `prefix`. */
|
|
60
50
|
export function clearGlobals(prefix: string): void {
|
|
61
51
|
const store = globalThis as unknown as Record<symbol, unknown>;
|