@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
|
@@ -4,10 +4,6 @@
|
|
|
4
4
|
* - Every request has an `id`; one without is rejected rather than run.
|
|
5
5
|
* - A batch runs in order, returns results in order, holds at most 10 calls,
|
|
6
6
|
* and isn't atomic.
|
|
7
|
-
* - The aggregate response is bounded: once it would pass
|
|
8
|
-
* `maxBatchResponseBytes`, later reads answer LimitExceeded instead.
|
|
9
|
-
* - A write's response is never replaced, alone or in a batch: the write has
|
|
10
|
-
* landed, and answering LimitExceeded would tell the client it hadn't.
|
|
11
7
|
*/
|
|
12
8
|
import {
|
|
13
9
|
type ContentProtocolError,
|
|
@@ -22,8 +18,6 @@ import { type Core, toProtocolError } from "./core.ts";
|
|
|
22
18
|
import { blocksApply } from "./methods/apply.ts";
|
|
23
19
|
import { blocksList, describe, schemaGet } from "./methods/read.ts";
|
|
24
20
|
|
|
25
|
-
const utf8 = new TextEncoder();
|
|
26
|
-
|
|
27
21
|
type Envelope = { id: RpcId; method: MethodName; params: unknown };
|
|
28
22
|
|
|
29
23
|
const isMethod = (method: string): method is MethodName =>
|
|
@@ -52,7 +46,7 @@ function parseEnvelope(
|
|
|
52
46
|
return { id, method: raw.method, params: raw.params };
|
|
53
47
|
}
|
|
54
48
|
|
|
55
|
-
async function run(core: Core, envelope: Envelope
|
|
49
|
+
async function run(core: Core, envelope: Envelope): Promise<unknown> {
|
|
56
50
|
switch (envelope.method) {
|
|
57
51
|
case "describe":
|
|
58
52
|
validateParams("describe", envelope.params);
|
|
@@ -62,24 +56,18 @@ async function run(core: Core, envelope: Envelope, scope: string): Promise<unkno
|
|
|
62
56
|
case "blocks.list":
|
|
63
57
|
return blocksList(core, validateParams("blocks.list", envelope.params));
|
|
64
58
|
case "blocks.apply":
|
|
65
|
-
return blocksApply(core, validateParams("blocks.apply", envelope.params)
|
|
59
|
+
return blocksApply(core, validateParams("blocks.apply", envelope.params));
|
|
66
60
|
}
|
|
67
61
|
}
|
|
68
62
|
|
|
69
|
-
/** True for a well-formed `blocks.apply` request. */
|
|
70
|
-
function isWrite(value: unknown): boolean {
|
|
71
|
-
const envelope = parseEnvelope(value);
|
|
72
|
-
return !("error" in envelope) && envelope.method === "blocks.apply";
|
|
73
|
-
}
|
|
74
|
-
|
|
75
63
|
const errorResponse = (id: RpcId | null, error: ContentProtocolError) =>
|
|
76
64
|
JSON.stringify({ jsonrpc: "2.0", id, error: error.toJSON() });
|
|
77
65
|
|
|
78
|
-
async function call(core: Core, value: unknown
|
|
66
|
+
async function call(core: Core, value: unknown): Promise<string> {
|
|
79
67
|
const envelope = parseEnvelope(value);
|
|
80
68
|
if ("error" in envelope) return errorResponse(envelope.id, envelope.error);
|
|
81
69
|
try {
|
|
82
|
-
const result = await run(core, envelope
|
|
70
|
+
const result = await run(core, envelope);
|
|
83
71
|
return JSON.stringify({ jsonrpc: "2.0", id: envelope.id, result });
|
|
84
72
|
} catch (error) {
|
|
85
73
|
const known = toProtocolError(error);
|
|
@@ -93,17 +81,8 @@ async function call(core: Core, value: unknown, scope: string): Promise<string>
|
|
|
93
81
|
* Runs a parsed request body (one request object or a batch array) and
|
|
94
82
|
* returns the serialized response body.
|
|
95
83
|
*/
|
|
96
|
-
export async function dispatch(core: Core, body: unknown
|
|
97
|
-
if (!Array.isArray(body))
|
|
98
|
-
const response = await call(core, body, scope);
|
|
99
|
-
if (isWrite(body) || utf8.encode(response).byteLength <= maxResponseBytes) return response;
|
|
100
|
-
return errorResponse(
|
|
101
|
-
parseEnvelope(body).id,
|
|
102
|
-
limitExceeded(`the response is over ${maxResponseBytes} bytes`, {
|
|
103
|
-
limit: "maxBatchResponseBytes",
|
|
104
|
-
}),
|
|
105
|
-
);
|
|
106
|
-
}
|
|
84
|
+
export async function dispatch(core: Core, body: unknown): Promise<string> {
|
|
85
|
+
if (!Array.isArray(body)) return call(core, body);
|
|
107
86
|
if (body.length === 0) return errorResponse(null, invalidRequest("an empty batch"));
|
|
108
87
|
if (body.length > MAX_BATCH_CALLS) {
|
|
109
88
|
return errorResponse(
|
|
@@ -112,25 +91,6 @@ export async function dispatch(core: Core, body: unknown, scope: string, maxResp
|
|
|
112
91
|
);
|
|
113
92
|
}
|
|
114
93
|
const responses: string[] = [];
|
|
115
|
-
|
|
116
|
-
for (const item of body) {
|
|
117
|
-
const envelope = parseEnvelope(item);
|
|
118
|
-
const id = envelope.id;
|
|
119
|
-
const overBudget = () =>
|
|
120
|
-
errorResponse(
|
|
121
|
-
id,
|
|
122
|
-
limitExceeded(`the batch response is over ${maxResponseBytes} bytes`, {
|
|
123
|
-
limit: "maxBatchResponseBytes",
|
|
124
|
-
}),
|
|
125
|
-
);
|
|
126
|
-
// Writes always run, so a batch never drops a save; reads stop once over budget.
|
|
127
|
-
const write = !("error" in envelope) && envelope.method === "blocks.apply";
|
|
128
|
-
let response =
|
|
129
|
-
!write && total > maxResponseBytes ? overBudget() : await call(core, item, scope);
|
|
130
|
-
const bytes = utf8.encode(response).byteLength + (responses.length ? 1 : 0);
|
|
131
|
-
if (total + bytes > maxResponseBytes && !write) response = overBudget();
|
|
132
|
-
total += utf8.encode(response).byteLength + (responses.length ? 1 : 0);
|
|
133
|
-
responses.push(response);
|
|
134
|
-
}
|
|
94
|
+
for (const item of body) responses.push(await call(core, item));
|
|
135
95
|
return `[${responses.join(",")}]`;
|
|
136
96
|
}
|
|
@@ -35,7 +35,6 @@ import {
|
|
|
35
35
|
type StoredFileBody,
|
|
36
36
|
type StoredSchema,
|
|
37
37
|
} from "../../storage.ts";
|
|
38
|
-
import type { Limits } from "../../types.ts";
|
|
39
38
|
import { gitBlobHash, revisionOf } from "./hash.ts";
|
|
40
39
|
import { withCommitLock } from "./lock.ts";
|
|
41
40
|
import { applyFileChange, sweepStaleTransactions } from "./transaction.ts";
|
|
@@ -57,8 +56,6 @@ export interface FsStorageOptions {
|
|
|
57
56
|
assetsMaxBytes?: number;
|
|
58
57
|
/** Overrides the poll interval (default 2000 ms). */
|
|
59
58
|
pollIntervalMs?: number;
|
|
60
|
-
/** Lowers the protocol's limits. */
|
|
61
|
-
limits?: Partial<Limits>;
|
|
62
59
|
/** How long a commit waits for another process's lock (default 10 s). */
|
|
63
60
|
lockTimeoutMs?: number;
|
|
64
61
|
}
|
|
@@ -227,16 +224,13 @@ export function createFsStorage(options: FsStorageOptions): FsStorage {
|
|
|
227
224
|
kind: "working-tree",
|
|
228
225
|
root: relativeRoot,
|
|
229
226
|
readOnly,
|
|
230
|
-
refs: null,
|
|
231
227
|
assets: readOnly
|
|
232
228
|
? null
|
|
233
229
|
: {
|
|
234
230
|
dir: toPosix(relative(base, assetsDir)) || ".",
|
|
235
231
|
maxBytes: options.assetsMaxBytes ?? DEFAULT_ASSETS_MAX_BYTES,
|
|
236
232
|
},
|
|
237
|
-
idempotency: null,
|
|
238
233
|
pollIntervalMs: options.pollIntervalMs ?? 2000,
|
|
239
|
-
limits: options.limits,
|
|
240
234
|
};
|
|
241
235
|
},
|
|
242
236
|
|
|
@@ -270,8 +264,6 @@ export function createFsStorage(options: FsStorageOptions): FsStorage {
|
|
|
270
264
|
|
|
271
265
|
async commit(attempt): Promise<CommitResult> {
|
|
272
266
|
if (readOnly) throw new StorageUnavailableError("the storage is read-only");
|
|
273
|
-
if (attempt.receipt)
|
|
274
|
-
throw new StorageUnavailableError("the filesystem storage keeps no receipts");
|
|
275
267
|
for (const file of [
|
|
276
268
|
...Object.keys(attempt.put),
|
|
277
269
|
...attempt.delete,
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* An in-memory `ContentStorage` (internal; not a package subpath).
|
|
3
3
|
*
|
|
4
|
-
* A reference implementation of the storage contract,
|
|
5
|
-
* request-key receipts persisted atomically with each commit, and a fixture
|
|
4
|
+
* A reference implementation of the storage contract, and a fixture
|
|
6
5
|
* for tests: its state can be dumped and restored to simulate a restart, and
|
|
7
6
|
* hooks let a test change files between a snapshot and a commit. Browser-safe.
|
|
8
7
|
*/
|
|
@@ -18,12 +17,9 @@ import {
|
|
|
18
17
|
StorageNotFoundError,
|
|
19
18
|
type StorageSnapshot,
|
|
20
19
|
type StoredFileBody,
|
|
21
|
-
type StoredReceipt,
|
|
22
20
|
type StoredSchema,
|
|
23
21
|
} from "../storage.ts";
|
|
24
22
|
|
|
25
|
-
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
26
|
-
|
|
27
23
|
/** Everything a memory storage holds; JSON-serializable, so it survives a "restart". */
|
|
28
24
|
export interface MemoryStorageState {
|
|
29
25
|
/** Saved-block files, by file name, with their content. */
|
|
@@ -33,7 +29,6 @@ export interface MemoryStorageState {
|
|
|
33
29
|
/** The content of `meta.gen.json`, the fallback schema, or `null`. */
|
|
34
30
|
legacySchema?: string | null;
|
|
35
31
|
secretsPublicKey: string | null;
|
|
36
|
-
receipts: Record<string, StoredReceipt & { createdAt: number }>;
|
|
37
32
|
assets: Record<string, string>;
|
|
38
33
|
/** `false` simulates an app root with no `.deco` folder at all. */
|
|
39
34
|
hasDecoFolder: boolean;
|
|
@@ -43,8 +38,6 @@ export interface MemoryStorageOptions {
|
|
|
43
38
|
state?: Partial<MemoryStorageState>;
|
|
44
39
|
/** Overrides parts of what `describe` reports. */
|
|
45
40
|
description?: Partial<StorageDescription>;
|
|
46
|
-
/** Clock for receipt retention (default `Date.now`). */
|
|
47
|
-
now?: () => number;
|
|
48
41
|
/** Runs after each file body is read; a test can change files here, mid-read. */
|
|
49
42
|
afterRead?: (file: string, storage: MemoryStorage) => void | Promise<void>;
|
|
50
43
|
/** Runs before each commit attempt's checks; a test can change files here. */
|
|
@@ -65,13 +58,11 @@ export interface MemoryStorage extends ContentStorage {
|
|
|
65
58
|
const version = (content: string) => sha256Hex(content);
|
|
66
59
|
|
|
67
60
|
export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryStorage {
|
|
68
|
-
const now = options.now ?? Date.now;
|
|
69
61
|
const state: MemoryStorageState = structuredClone({
|
|
70
62
|
files: {},
|
|
71
63
|
schema: null,
|
|
72
64
|
legacySchema: null,
|
|
73
65
|
secretsPublicKey: null,
|
|
74
|
-
receipts: {},
|
|
75
66
|
assets: {},
|
|
76
67
|
hasDecoFolder: true,
|
|
77
68
|
...options.state,
|
|
@@ -80,9 +71,7 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
80
71
|
kind: "git",
|
|
81
72
|
root: ".",
|
|
82
73
|
readOnly: false,
|
|
83
|
-
refs: null,
|
|
84
74
|
assets: { dir: "public/assets", maxBytes: 25 * 1024 * 1024 },
|
|
85
|
-
idempotency: { retentionMs: DAY_MS },
|
|
86
75
|
...options.description,
|
|
87
76
|
};
|
|
88
77
|
let commits = 0;
|
|
@@ -118,7 +107,6 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
118
107
|
if (!isBlockFileName(file)) throw new StorageInvalidFileError(file);
|
|
119
108
|
}
|
|
120
109
|
if (!state.hasDecoFolder) throw new StorageNotFoundError("no .deco folder");
|
|
121
|
-
if (attempt.receipt && state.receipts[attempt.receipt.key]) return { status: "stale" };
|
|
122
110
|
if (
|
|
123
111
|
attempt.expectedSchemaVersion !== undefined &&
|
|
124
112
|
(await schemaVersion()) !== attempt.expectedSchemaVersion
|
|
@@ -138,15 +126,6 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
138
126
|
for (const [file, v] of Object.entries(written)) versions.set(file, v);
|
|
139
127
|
for (const file of attempt.delete) if (!(file in attempt.put)) versions.delete(file);
|
|
140
128
|
const revision = await revisionOf(versions);
|
|
141
|
-
if (attempt.receipt) {
|
|
142
|
-
state.receipts[attempt.receipt.key] = {
|
|
143
|
-
digest: attempt.receipt.digest,
|
|
144
|
-
revision,
|
|
145
|
-
versions: written,
|
|
146
|
-
deleted: [...attempt.delete],
|
|
147
|
-
createdAt: now(),
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
129
|
commits++;
|
|
151
130
|
return { status: "committed", revision, versions: written };
|
|
152
131
|
}
|
|
@@ -201,20 +180,6 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
201
180
|
return run;
|
|
202
181
|
},
|
|
203
182
|
|
|
204
|
-
async getReceipt(key) {
|
|
205
|
-
const receipt = state.receipts[key];
|
|
206
|
-
if (!receipt) return null;
|
|
207
|
-
if (
|
|
208
|
-
description.idempotency &&
|
|
209
|
-
now() - receipt.createdAt > description.idempotency.retentionMs
|
|
210
|
-
) {
|
|
211
|
-
delete state.receipts[key];
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
const { createdAt: _createdAt, ...stored } = receipt;
|
|
215
|
-
return stored;
|
|
216
|
-
},
|
|
217
|
-
|
|
218
183
|
async putAsset(name, body) {
|
|
219
184
|
let candidate = name;
|
|
220
185
|
while (candidate in state.assets) candidate = suffixedAssetName(name);
|
package/src/protocol/storage.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
*
|
|
11
11
|
* Browser-safe: types and small error classes only.
|
|
12
12
|
*/
|
|
13
|
-
import type {
|
|
13
|
+
import type { StorageKind } from "./types.ts";
|
|
14
14
|
|
|
15
15
|
/** What a storage tells `describe`. */
|
|
16
16
|
export interface StorageDescription {
|
|
@@ -18,19 +18,10 @@ export interface StorageDescription {
|
|
|
18
18
|
/** The app root, relative to the repository root (`"."` when they're the same). */
|
|
19
19
|
root: string;
|
|
20
20
|
readOnly: boolean;
|
|
21
|
-
/** Branch support; `null` when the storage has no refs (`ref` params are then Unsupported). */
|
|
22
|
-
refs: null | { default: string; autoCreate: boolean };
|
|
23
21
|
/** Where uploads go; `null` when read-only or uploads go elsewhere. */
|
|
24
22
|
assets: null | { dir: string; maxBytes: number };
|
|
25
|
-
/**
|
|
26
|
-
* Durable request-key receipts. Advertise only when `getReceipt` is
|
|
27
|
-
* implemented and `commit` persists `attempt.receipt` with the mutation.
|
|
28
|
-
*/
|
|
29
|
-
idempotency: null | { retentionMs: number };
|
|
30
23
|
/** Overrides the poll interval (default: 2000 for a working tree, 30000 for git). */
|
|
31
24
|
pollIntervalMs?: number;
|
|
32
|
-
/** Lowers the protocol's default limits. Higher values are ignored. */
|
|
33
|
-
limits?: Partial<Limits>;
|
|
34
25
|
}
|
|
35
26
|
|
|
36
27
|
/** One saved-block file in a snapshot. */
|
|
@@ -47,7 +38,7 @@ export interface StorageFile {
|
|
|
47
38
|
export interface StorageSnapshot {
|
|
48
39
|
/** An opaque revision of the whole set, compared only for equality. */
|
|
49
40
|
revision: string;
|
|
50
|
-
/** The
|
|
41
|
+
/** The branch actually read, for a storage that reads one; `null` for a working tree. */
|
|
51
42
|
resolvedRef: string | null;
|
|
52
43
|
files: StorageFile[];
|
|
53
44
|
}
|
|
@@ -72,21 +63,8 @@ export interface StoredSchema {
|
|
|
72
63
|
resolvedRef: string | null;
|
|
73
64
|
}
|
|
74
65
|
|
|
75
|
-
/** A durable receipt for one `blocks.apply` request key. */
|
|
76
|
-
export interface StoredReceipt {
|
|
77
|
-
/** The canonical digest of the request the key was first used with. */
|
|
78
|
-
digest: string;
|
|
79
|
-
/** The revision that commit produced. */
|
|
80
|
-
revision: string;
|
|
81
|
-
/** The versions of the files that commit wrote. */
|
|
82
|
-
versions: Record<string, string>;
|
|
83
|
-
/** The files that commit deleted (`attempt.delete`), so a replay reports the same names. */
|
|
84
|
-
deleted?: string[];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
66
|
/** One atomic commit attempt. */
|
|
88
67
|
export interface CommitAttempt {
|
|
89
|
-
ref?: string;
|
|
90
68
|
/** The snapshot the core validated against. */
|
|
91
69
|
base: StorageSnapshot;
|
|
92
70
|
/** Files to write, by file name, with their full content. */
|
|
@@ -101,19 +79,11 @@ export interface CommitAttempt {
|
|
|
101
79
|
expected: Record<string, string | null>;
|
|
102
80
|
/**
|
|
103
81
|
* The schema version the core validated against (`null` = no schema), when
|
|
104
|
-
* the write depends on it (
|
|
82
|
+
* the write depends on it (the secret guard checks `set` against it). When set,
|
|
105
83
|
* a storage MUST reject the attempt as stale if the schema's version at
|
|
106
84
|
* commit time differs, checked atomically with `expected`.
|
|
107
85
|
*/
|
|
108
86
|
expectedSchemaVersion?: string | null;
|
|
109
|
-
/**
|
|
110
|
-
* Persist this receipt atomically with the mutation (only when idempotency
|
|
111
|
-
* is advertised). The key also reserves the request: a storage MUST reject
|
|
112
|
-
* the attempt as stale when a receipt for `receipt.key` already exists,
|
|
113
|
-
* including one written concurrently by another replica, so two requests
|
|
114
|
-
* with the same key never both commit.
|
|
115
|
-
*/
|
|
116
|
-
receipt?: { key: string; digest: string };
|
|
117
87
|
}
|
|
118
88
|
|
|
119
89
|
export type CommitResult =
|
|
@@ -129,9 +99,9 @@ export interface ContentStorage {
|
|
|
129
99
|
describe(): StorageDescription | Promise<StorageDescription>;
|
|
130
100
|
/**
|
|
131
101
|
* Lists the saved-block files. Throws `StorageNotFoundError` when there's no
|
|
132
|
-
* `.deco` folder at all
|
|
102
|
+
* `.deco` folder at all.
|
|
133
103
|
*/
|
|
134
|
-
snapshot(
|
|
104
|
+
snapshot(): Promise<StorageSnapshot>;
|
|
135
105
|
/**
|
|
136
106
|
* Reads file bodies (UTF-8 text) from a snapshot, each with the version of
|
|
137
107
|
* the bytes read. A file that vanished is left out. A storage that can't
|
|
@@ -140,18 +110,16 @@ export interface ContentStorage {
|
|
|
140
110
|
*/
|
|
141
111
|
readFiles(snapshot: StorageSnapshot, files: string[]): Promise<Record<string, StoredFileBody>>;
|
|
142
112
|
/** Reads `schema.gen.json`, falling back to `meta.gen.json`; `null` when neither exists. */
|
|
143
|
-
readSchema(
|
|
113
|
+
readSchema(): Promise<StoredSchema | null>;
|
|
144
114
|
/** Reads `<root>/.deco/secrets.pub`; `null` without one. */
|
|
145
115
|
readSecretsPublicKey(): Promise<string | null>;
|
|
146
116
|
/** One all-or-nothing commit attempt; durable when it resolves. */
|
|
147
117
|
commit(attempt: CommitAttempt): Promise<CommitResult>;
|
|
148
|
-
/** Looks up a receipt by its scoped key; `null` when unknown or expired. */
|
|
149
|
-
getReceipt?(key: string): Promise<StoredReceipt | null>;
|
|
150
118
|
/** Stores an uploaded file, never overwriting one; returns the stored file name. */
|
|
151
119
|
putAsset?(name: string, body: Uint8Array): Promise<{ name: string }>;
|
|
152
120
|
}
|
|
153
121
|
|
|
154
|
-
/** Thrown by a storage when there's no `.deco` folder
|
|
122
|
+
/** Thrown by a storage when there's no `.deco` folder. */
|
|
155
123
|
export class StorageNotFoundError extends Error {
|
|
156
124
|
constructor(message: string) {
|
|
157
125
|
super(message);
|
package/src/protocol/types.ts
CHANGED
|
@@ -30,33 +30,6 @@ export const MAX_BATCH_CALLS = 10;
|
|
|
30
30
|
/** The URL prefix every uploaded asset is stored under in a field. */
|
|
31
31
|
export const ASSETS_URL_PREFIX = "/assets/";
|
|
32
32
|
|
|
33
|
-
export interface Limits {
|
|
34
|
-
/** Names (set plus delete) in one `blocks.apply`. */
|
|
35
|
-
maxOpsPerApply: number;
|
|
36
|
-
/** Bytes of one stored entry. */
|
|
37
|
-
maxBlockBytes: number;
|
|
38
|
-
/** Bytes of one HTTP request body, uncompressed. */
|
|
39
|
-
maxRequestBytes: number;
|
|
40
|
-
/** Uncompressed bytes of the whole block list. */
|
|
41
|
-
maxListBytes: number;
|
|
42
|
-
/** Uncompressed bytes of the schema. */
|
|
43
|
-
maxSchemaBytes: number;
|
|
44
|
-
/** Uncompressed bytes of a whole batch response. */
|
|
45
|
-
maxBatchResponseBytes: number;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const MiB = 1024 * 1024;
|
|
49
|
-
|
|
50
|
-
/** The protocol's default limits. A storage or a server can lower them. */
|
|
51
|
-
export const DEFAULT_LIMITS: Readonly<Limits> = Object.freeze({
|
|
52
|
-
maxOpsPerApply: 500,
|
|
53
|
-
maxBlockBytes: 1 * MiB,
|
|
54
|
-
maxRequestBytes: 8 * MiB,
|
|
55
|
-
maxListBytes: 16 * MiB,
|
|
56
|
-
maxSchemaBytes: 16 * MiB,
|
|
57
|
-
maxBatchResponseBytes: 32 * MiB,
|
|
58
|
-
});
|
|
59
|
-
|
|
60
33
|
export type StorageKind = "working-tree" | "git";
|
|
61
34
|
|
|
62
35
|
export interface DescribeResult {
|
|
@@ -69,12 +42,8 @@ export interface DescribeResult {
|
|
|
69
42
|
/** The app root: the folder that contains `.deco/`, relative to the repository root. */
|
|
70
43
|
root: string;
|
|
71
44
|
schemaFormat: typeof SCHEMA_FORMAT;
|
|
72
|
-
/** Branches; `null` on the local server. */
|
|
73
|
-
refs: null | { default: string; autoCreate: boolean };
|
|
74
|
-
writes: { idempotency: null | { retentionMs: number }; schemaPreconditions: boolean };
|
|
75
45
|
/** Local: 2000; git: 30000, plus on window focus. */
|
|
76
46
|
pollIntervalMs: number;
|
|
77
|
-
limits: Limits;
|
|
78
47
|
/**
|
|
79
48
|
* The app the site editor shows in its Preview tab (and where "open the real
|
|
80
49
|
* page" points); the local server reports `deco serve --preview`. `null`: no preview.
|
|
@@ -94,7 +63,6 @@ export interface DecoMeta {
|
|
|
94
63
|
}
|
|
95
64
|
|
|
96
65
|
export interface ReadParams {
|
|
97
|
-
ref?: string;
|
|
98
66
|
ifNoneMatch?: string;
|
|
99
67
|
}
|
|
100
68
|
|
|
@@ -142,11 +110,6 @@ export type BlocksListResult =
|
|
|
142
110
|
};
|
|
143
111
|
|
|
144
112
|
export interface BlocksApplyParams {
|
|
145
|
-
ref?: string;
|
|
146
|
-
/** Retry the same logical write; only when advertised. */
|
|
147
|
-
requestKey?: string;
|
|
148
|
-
/** Reject if the schema changed; only when advertised. */
|
|
149
|
-
ifSchemaMatch?: string;
|
|
150
113
|
/** Whole-entry replace (create or update). */
|
|
151
114
|
set?: Record<string, Record<string, unknown>>;
|
|
152
115
|
/** A missing name counts as deleted. */
|
package/src/v8/canonical.ts
CHANGED
|
@@ -104,14 +104,3 @@ export const CONTENT_HASH_FORMAT = 1;
|
|
|
104
104
|
export async function computeContentRevision(blocks: Record<string, unknown>): Promise<string> {
|
|
105
105
|
return sha256Hex(canonicalJson(blocks));
|
|
106
106
|
}
|
|
107
|
-
|
|
108
|
-
/** The domain prefix of `blocks.apply` request digests, so they never equal another hash. */
|
|
109
|
-
export const APPLY_DIGEST_DOMAIN = "deco-content/blocks.apply@1\n";
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* The digest a request key is bound to: SHA-256 over the domain prefix and
|
|
113
|
-
* the canonical JSON of every supplied parameter.
|
|
114
|
-
*/
|
|
115
|
-
export function applyRequestDigest(params: object): Promise<string> {
|
|
116
|
-
return sha256Hex(APPLY_DIGEST_DOMAIN + canonicalJson(params));
|
|
117
|
-
}
|
package/src/v8/cli/content.ts
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
* module, `.deco/blocks.gen.ts` (spec: content › The content module). It never
|
|
4
4
|
* reads the block map.
|
|
5
5
|
*/
|
|
6
|
+
import { execFileSync } from "node:child_process";
|
|
6
7
|
import fs from "node:fs";
|
|
7
8
|
import path from "node:path";
|
|
8
|
-
import { computeContentRevision } from "../../protocol/canonical.ts";
|
|
9
|
+
import { canonicalJson, computeContentRevision, sha256Hex } from "../../protocol/canonical.ts";
|
|
9
10
|
import {
|
|
10
11
|
blockNameFromFile,
|
|
11
12
|
checkBlockName,
|
|
@@ -154,7 +155,8 @@ function inlinedFiles(saved: SavedBlocks): string[] {
|
|
|
154
155
|
/**
|
|
155
156
|
* The `.deco` folder's identity for `createCMS` (one instance per folder, so
|
|
156
157
|
* a hot-reloaded module keeps its instance): its path from the repository
|
|
157
|
-
* root, or `.deco` outside a repository. Relative, so
|
|
158
|
+
* root, or `.deco` outside a repository. Relative, so it doesn't depend on
|
|
159
|
+
* where the repository is checked out.
|
|
158
160
|
*/
|
|
159
161
|
export function contentRoot(deco: string): string {
|
|
160
162
|
for (let dir = path.dirname(deco); ; dir = path.dirname(dir)) {
|
|
@@ -165,8 +167,66 @@ export function contentRoot(deco: string): string {
|
|
|
165
167
|
}
|
|
166
168
|
}
|
|
167
169
|
|
|
168
|
-
/**
|
|
169
|
-
|
|
170
|
+
/**
|
|
171
|
+
* The `schemaHash` of a `.deco/schema.gen.json`: the SHA-256 of its parsed
|
|
172
|
+
* JSON in canonical form (`sha256Hex(canonicalJson(JSON.parse(text)))`, the
|
|
173
|
+
* `@decocms/blocks/protocol` helpers), so Studio computes the same value
|
|
174
|
+
* from the file at a commit. Hosted releases swap only when it matches.
|
|
175
|
+
*/
|
|
176
|
+
async function computeSchemaHash(text: string): Promise<string> {
|
|
177
|
+
return sha256Hex(canonicalJson(JSON.parse(text)));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** `schema.gen.json`'s hash, or `undefined` when there's no such file. */
|
|
181
|
+
async function readSchemaHash(schemaFile: string): Promise<string | undefined> {
|
|
182
|
+
let text: string;
|
|
183
|
+
try {
|
|
184
|
+
text = fs.readFileSync(schemaFile, "utf8");
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
return await computeSchemaHash(text);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
throw new CliError(
|
|
193
|
+
`.deco/schema.gen.json: not valid JSON (${(error as Error).message}); run deco schema`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The commit time of git HEAD in `dir` (committer date, ISO 8601), or
|
|
200
|
+
* undefined when git is missing, `dir` isn't in a repository, or it has no
|
|
201
|
+
* commits. Commit time, not build time: a slow build of an older commit that
|
|
202
|
+
* finishes after a publish must not win over it.
|
|
203
|
+
*/
|
|
204
|
+
export function readCommittedAt(dir: string): string | undefined {
|
|
205
|
+
try {
|
|
206
|
+
const out = execFileSync("git", ["log", "-1", "--format=%cI"], {
|
|
207
|
+
cwd: dir,
|
|
208
|
+
encoding: "utf8",
|
|
209
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
210
|
+
}).trim();
|
|
211
|
+
return out === "" || Number.isNaN(Date.parse(out)) ? undefined : out;
|
|
212
|
+
} catch {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The source of `.deco/blocks.gen.ts` for a set of saved blocks, with the
|
|
219
|
+
* `schemaHash` of the schema it was built with when there is one, and
|
|
220
|
+
* `committedAt`: the commit time of git HEAD it was built from (ISO 8601),
|
|
221
|
+
* when there is one. A hosted release replaces the bundled content only when
|
|
222
|
+
* it was published later (see ../remoteLoader.ts).
|
|
223
|
+
*/
|
|
224
|
+
export async function renderContentModule(
|
|
225
|
+
saved: SavedBlocks,
|
|
226
|
+
root = ".deco",
|
|
227
|
+
schemaHash?: string,
|
|
228
|
+
committedAt?: string,
|
|
229
|
+
): Promise<string> {
|
|
170
230
|
const names = Object.keys(saved.blocks).sort();
|
|
171
231
|
const revision = await computeContentRevision(saved.blocks);
|
|
172
232
|
const taken = new Set<string>();
|
|
@@ -194,11 +254,15 @@ export async function renderContentModule(saved: SavedBlocks, root = ".deco"): P
|
|
|
194
254
|
"",
|
|
195
255
|
"const content: {",
|
|
196
256
|
" revision: string;",
|
|
257
|
+
...(schemaHash === undefined ? [] : [" schemaHash: string;"]),
|
|
258
|
+
...(committedAt === undefined ? [] : [" committedAt: string;"]),
|
|
197
259
|
" blocks: Record<string, unknown>;",
|
|
198
260
|
" aliases: Record<string, string>;",
|
|
199
261
|
" root: string;",
|
|
200
262
|
"} = {",
|
|
201
263
|
` revision: ${JSON.stringify(revision)},`,
|
|
264
|
+
...(schemaHash === undefined ? [] : [` schemaHash: ${JSON.stringify(schemaHash)},`]),
|
|
265
|
+
...(committedAt === undefined ? [] : [` committedAt: ${JSON.stringify(committedAt)},`]),
|
|
202
266
|
" blocks: {",
|
|
203
267
|
...entries,
|
|
204
268
|
" },",
|
|
@@ -217,7 +281,14 @@ export interface ContentResult {
|
|
|
217
281
|
root: string;
|
|
218
282
|
file: string;
|
|
219
283
|
revision: string;
|
|
284
|
+
/** The hash of `.deco/schema.gen.json`, written into the module; absent without one. */
|
|
285
|
+
schemaHash?: string;
|
|
220
286
|
count: number;
|
|
287
|
+
/**
|
|
288
|
+
* The commit time of git HEAD in the root, written into the module as
|
|
289
|
+
* `committedAt`; absent when git isn't available there.
|
|
290
|
+
*/
|
|
291
|
+
committedAt?: string;
|
|
221
292
|
/** False when the file already had this content. */
|
|
222
293
|
changed: boolean;
|
|
223
294
|
/** Files written into the module instead of imported (see `inlinedFiles`). */
|
|
@@ -246,12 +317,16 @@ export async function writeContent(paths: DecoPaths): Promise<ContentResult> {
|
|
|
246
317
|
if (errors.length > 0) {
|
|
247
318
|
throw new CliError(errors.map((d) => `.deco/blocks/${d.file}: ${d.message}`).join("\n"));
|
|
248
319
|
}
|
|
249
|
-
const
|
|
320
|
+
const schemaHash = await readSchemaHash(paths.schema);
|
|
321
|
+
const committedAt = readCommittedAt(paths.root);
|
|
322
|
+
const source = await renderContentModule(saved, contentRoot(paths.deco), schemaHash, committedAt);
|
|
250
323
|
const changed = writeIfChanged(paths.content, source);
|
|
251
324
|
return {
|
|
252
325
|
root: paths.root,
|
|
253
326
|
file: paths.content,
|
|
254
327
|
revision: await computeContentRevision(saved.blocks),
|
|
328
|
+
...(schemaHash === undefined ? {} : { schemaHash }),
|
|
329
|
+
...(committedAt === undefined ? {} : { committedAt }),
|
|
255
330
|
count: Object.keys(saved.blocks).length,
|
|
256
331
|
changed,
|
|
257
332
|
inlined: inlinedFiles(saved),
|
|
@@ -273,6 +348,11 @@ export async function content(options: ContentOptions = {}): Promise<number> {
|
|
|
273
348
|
const paths = decoPaths(findDecoRoot(options));
|
|
274
349
|
const result = await writeContent(paths);
|
|
275
350
|
for (const d of result.diagnostics) reporter.warn(`.deco/blocks/${d.file}: ${d.message}`);
|
|
351
|
+
if (result.committedAt === undefined) {
|
|
352
|
+
reporter.info(
|
|
353
|
+
"no git commit found, so .deco/blocks.gen.ts has no committedAt: hosted releases with the same schema will replace it",
|
|
354
|
+
);
|
|
355
|
+
}
|
|
276
356
|
reporter.info(
|
|
277
357
|
`${result.changed ? "wrote" : "unchanged"} .deco/blocks.gen.ts (${result.count} blocks, revision ${result.revision.slice(0, 12)})`,
|
|
278
358
|
);
|
package/src/v8/cli/run.ts
CHANGED
|
@@ -124,7 +124,7 @@ async function watchLoop(
|
|
|
124
124
|
*/
|
|
125
125
|
const PUBLISH_SIGNPOST = `There is no publish command: Git is the source of truth for content, so publishing is committing.
|
|
126
126
|
Commit the changes in .deco/blocks (and push them). A deploy ships the commit; with the hosted
|
|
127
|
-
Deco CMS,
|
|
127
|
+
Deco CMS, Studio's Publish (or Resync) makes the commit a release without a deploy.`;
|
|
128
128
|
|
|
129
129
|
/** Run one `deco` invocation; returns the exit code. */
|
|
130
130
|
export async function runCli(argv: string[], options: RunOptions = {}): Promise<number> {
|