@decocms/blocks 8.7.0 → 8.8.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/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/protocol/conformance/cases/changes.d.ts.map +1 -1
- package/dist/protocol/conformance/cases/changes.js +14 -3
- package/dist/protocol/conformance/cases/changes.js.map +1 -1
- package/dist/protocol/conformance/cases/guards.d.ts.map +1 -1
- package/dist/protocol/conformance/cases/guards.js +19 -42
- package/dist/protocol/conformance/cases/guards.js.map +1 -1
- package/dist/protocol/conformance/cases/overlay.d.ts +3 -0
- package/dist/protocol/conformance/cases/overlay.d.ts.map +1 -0
- package/dist/protocol/conformance/cases/overlay.js +164 -0
- package/dist/protocol/conformance/cases/overlay.js.map +1 -0
- package/dist/protocol/conformance/cases/reads.js +5 -5
- 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 +15 -3
- package/dist/protocol/conformance/cases/writes.js.map +1 -1
- package/dist/protocol/conformance/context.d.ts +0 -10
- package/dist/protocol/conformance/context.d.ts.map +1 -1
- package/dist/protocol/conformance/context.js.map +1 -1
- package/dist/protocol/conformance/index.d.ts.map +1 -1
- package/dist/protocol/conformance/index.js +2 -0
- package/dist/protocol/conformance/index.js.map +1 -1
- package/dist/protocol/server/content.d.ts +8 -1
- package/dist/protocol/server/content.d.ts.map +1 -1
- package/dist/protocol/server/content.js +18 -3
- package/dist/protocol/server/content.js.map +1 -1
- package/dist/protocol/server/core.d.ts +0 -6
- package/dist/protocol/server/core.d.ts.map +1 -1
- package/dist/protocol/server/core.js +0 -2
- package/dist/protocol/server/core.js.map +1 -1
- package/dist/protocol/server/methods/apply.d.ts +16 -5
- package/dist/protocol/server/methods/apply.d.ts.map +1 -1
- package/dist/protocol/server/methods/apply.js +42 -38
- package/dist/protocol/server/methods/apply.js.map +1 -1
- package/dist/protocol/server/methods/changes.d.ts.map +1 -1
- package/dist/protocol/server/methods/changes.js +31 -3
- package/dist/protocol/server/methods/changes.js.map +1 -1
- package/dist/protocol/server/methods/read.d.ts.map +1 -1
- package/dist/protocol/server/methods/read.js +2 -23
- package/dist/protocol/server/methods/read.js.map +1 -1
- package/dist/protocol/storage/fs/index.d.ts.map +1 -1
- package/dist/protocol/storage/fs/index.js +0 -18
- package/dist/protocol/storage/fs/index.js.map +1 -1
- package/dist/protocol/storage/memory.d.ts +11 -3
- package/dist/protocol/storage/memory.d.ts.map +1 -1
- package/dist/protocol/storage/memory.js +114 -45
- package/dist/protocol/storage/memory.js.map +1 -1
- package/dist/protocol/storage.d.ts +68 -18
- package/dist/protocol/storage.d.ts.map +1 -1
- package/dist/protocol/storage.js +7 -0
- package/dist/protocol/storage.js.map +1 -1
- package/dist/protocol/types.d.ts +51 -8
- package/dist/protocol/types.d.ts.map +1 -1
- package/dist/protocol/types.js +5 -3
- package/dist/protocol/types.js.map +1 -1
- package/dist/v8/builtins/index.d.ts +1 -4
- package/dist/v8/builtins/index.d.ts.map +1 -1
- package/dist/v8/builtins/index.js +1 -6
- package/dist/v8/builtins/index.js.map +1 -1
- package/dist/v8/builtins/legacy.d.ts +4 -4
- package/dist/v8/builtins/legacy.d.ts.map +1 -1
- package/dist/v8/builtins/legacy.js +4 -5
- package/dist/v8/builtins/legacy.js.map +1 -1
- package/dist/v8/cli/builtins.d.ts.map +1 -1
- package/dist/v8/cli/builtins.js +0 -1
- package/dist/v8/cli/builtins.js.map +1 -1
- package/dist/v8/cli/check/index.d.ts +1 -1
- package/dist/v8/cli/check/index.d.ts.map +1 -1
- package/dist/v8/cli/check/index.js +13 -33
- package/dist/v8/cli/check/index.js.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.d.ts.map +1 -1
- package/dist/v8/cli/schema/builtinSchemas.js +0 -8
- package/dist/v8/cli/schema/builtinSchemas.js.map +1 -1
- package/dist/v8/cli/schema/generate.d.ts.map +1 -1
- package/dist/v8/cli/schema/generate.js +1 -8
- package/dist/v8/cli/schema/generate.js.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.d.ts +0 -1
- package/dist/v8/cli/schema/typeToSchema.d.ts.map +1 -1
- package/dist/v8/cli/schema/typeToSchema.js +5 -30
- package/dist/v8/cli/schema/typeToSchema.js.map +1 -1
- package/dist/v8/client.d.ts +1 -1
- package/dist/v8/client.d.ts.map +1 -1
- package/dist/v8/cms.d.ts.map +1 -1
- package/dist/v8/cms.js +5 -13
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/index.d.ts +1 -1
- package/dist/v8/index.d.ts.map +1 -1
- package/dist/v8/resolver.d.ts +1 -1
- package/dist/v8/resolver.d.ts.map +1 -1
- package/dist/v8/telemetry.d.ts +0 -2
- package/dist/v8/telemetry.d.ts.map +1 -1
- package/dist/v8/telemetry.js +5 -20
- package/dist/v8/telemetry.js.map +1 -1
- package/dist/v8/types.d.ts +0 -8
- package/dist/v8/types.d.ts.map +1 -1
- package/package.json +1 -6
- package/src/index.ts +1 -2
- package/src/protocol/conformance/cases/changes.ts +16 -4
- package/src/protocol/conformance/cases/guards.ts +19 -55
- package/src/protocol/conformance/cases/overlay.ts +195 -0
- package/src/protocol/conformance/cases/reads.ts +4 -4
- package/src/protocol/conformance/cases/writes.ts +15 -3
- package/src/protocol/conformance/context.ts +0 -7
- package/src/protocol/conformance/index.ts +2 -0
- package/src/protocol/server/content.ts +30 -4
- package/src/protocol/server/core.ts +0 -3
- package/src/protocol/server/methods/apply.ts +43 -38
- package/src/protocol/server/methods/changes.ts +42 -4
- package/src/protocol/server/methods/read.ts +2 -24
- package/src/protocol/storage/fs/index.ts +0 -18
- package/src/protocol/storage/memory.ts +124 -46
- package/src/protocol/storage.ts +69 -18
- package/src/protocol/types.ts +51 -8
- package/src/v8/builtins/index.ts +1 -6
- package/src/v8/builtins/legacy.ts +4 -5
- package/src/v8/cli/builtins.ts +0 -1
- package/src/v8/cli/check/index.ts +15 -34
- package/src/v8/cli/schema/builtinSchemas.ts +0 -8
- package/src/v8/cli/schema/generate.ts +1 -8
- package/src/v8/cli/schema/typeToSchema.ts +5 -31
- package/src/v8/client.ts +1 -1
- package/src/v8/cms.ts +5 -14
- package/src/v8/index.ts +0 -1
- package/src/v8/resolver.ts +1 -1
- package/src/v8/telemetry.ts +5 -20
- package/src/v8/types.ts +0 -5
- package/dist/protocol/ciphertext.d.ts +0 -7
- package/dist/protocol/ciphertext.d.ts.map +0 -1
- package/dist/protocol/ciphertext.js +0 -7
- package/dist/protocol/ciphertext.js.map +0 -1
- package/dist/protocol/secrets.d.ts +0 -22
- package/dist/protocol/secrets.d.ts.map +0 -1
- package/dist/protocol/secrets.js +0 -207
- package/dist/protocol/secrets.js.map +0 -1
- package/dist/v8/builtins/secret.d.ts +0 -7
- package/dist/v8/builtins/secret.d.ts.map +0 -1
- package/dist/v8/builtins/secret.js +0 -68
- package/dist/v8/builtins/secret.js.map +0 -1
- package/dist/v8/ciphertext.d.ts +0 -58
- package/dist/v8/ciphertext.d.ts.map +0 -1
- package/dist/v8/ciphertext.js +0 -134
- package/dist/v8/ciphertext.js.map +0 -1
- package/dist/v8/secrets.d.ts +0 -9
- package/dist/v8/secrets.d.ts.map +0 -1
- package/dist/v8/secrets.js +0 -14
- package/dist/v8/secrets.js.map +0 -1
- package/src/protocol/ciphertext.ts +0 -6
- package/src/protocol/secrets.ts +0 -239
- package/src/v8/builtins/secret.ts +0 -82
- package/src/v8/ciphertext.ts +0 -158
- package/src/v8/secrets.ts +0 -17
|
@@ -6,17 +6,28 @@
|
|
|
6
6
|
* commit, or none does.
|
|
7
7
|
* 2. Durable when it returns.
|
|
8
8
|
* 3. `set` wins when a name is in both `set` and `delete`.
|
|
9
|
-
* 4. Validated first: names, value shapes
|
|
10
|
-
*
|
|
9
|
+
* 4. Validated first: names, value shapes and sizes are checked before
|
|
10
|
+
* anything is written, and every violation is reported. The schema isn't
|
|
11
|
+
* read: content is checked against it by `deco check`.
|
|
11
12
|
* 5. Preconditions are optional: a failed `ifMatch` writes nothing and
|
|
12
13
|
* returns a Conflict; without one the last writer wins.
|
|
13
|
-
*
|
|
14
14
|
* 6. Two spellings of one name are one entry: a guard compares against the
|
|
15
15
|
* entry under any spelling, and the result reports `null` for every other
|
|
16
16
|
* spelling the commit deleted.
|
|
17
|
+
* 7. On an overlay storage (one with `writeSnapshot()`), everything above
|
|
18
|
+
* runs against the overlay alone, so a write never reads the base: a
|
|
19
|
+
* guard compares with the overlay's version, and a delete always reaches
|
|
20
|
+
* the storage, which records it over the base. A deletion the overlay
|
|
21
|
+
* records (a tombstone) is in the overlay: a guard sees its tombstone
|
|
22
|
+
* version, so `ifMatch: null` on it fails and the tombstone version (from
|
|
23
|
+
* `changes.list`, a Conflict's `actual`, or the delete's own result)
|
|
24
|
+
* passes; the result reports that version for each name the commit
|
|
25
|
+
* recorded a tombstone for (a storage may record one even for a name the
|
|
26
|
+
* base lacks), and `null` for one it dropped.
|
|
17
27
|
*
|
|
18
|
-
* A commit attempt that finds storage moved
|
|
19
|
-
* backoff, rechecking every guard, up to
|
|
28
|
+
* A commit attempt that finds storage moved is retried against a new
|
|
29
|
+
* snapshot after a jittered backoff, rechecking every guard, up to
|
|
30
|
+
* `maxCommitAttempts` times.
|
|
20
31
|
*/
|
|
21
32
|
import {
|
|
22
33
|
type BlockViolation,
|
|
@@ -37,11 +48,9 @@ import {
|
|
|
37
48
|
serializeBlock,
|
|
38
49
|
spellingKey,
|
|
39
50
|
} from "../../keys.ts";
|
|
40
|
-
import {
|
|
41
|
-
import type { BlocksApplyParams, BlocksApplyResult, DecoMeta } from "../../types.ts";
|
|
51
|
+
import type { BlocksApplyParams, BlocksApplyResult } from "../../types.ts";
|
|
42
52
|
import { type LoadedContent, type LoadedEntry, loadCurrentContent } from "../content.ts";
|
|
43
53
|
import { type Core, MAX_BLOCK_BYTES, MAX_OPS_PER_APPLY } from "../core.ts";
|
|
44
|
-
import { parseSchema } from "./read.ts";
|
|
45
54
|
|
|
46
55
|
const utf8 = new TextEncoder();
|
|
47
56
|
|
|
@@ -66,7 +75,7 @@ function normalize(params: BlocksApplyParams): NormalizedApply {
|
|
|
66
75
|
}
|
|
67
76
|
|
|
68
77
|
/** Checks everything that doesn't depend on stored content. */
|
|
69
|
-
function validateStatic(apply: NormalizedApply
|
|
78
|
+
function validateStatic(apply: NormalizedApply) {
|
|
70
79
|
const violations: BlockViolation[] = [];
|
|
71
80
|
const bodies = new Map<string, string>();
|
|
72
81
|
const bySpelling = new Map<string, string>();
|
|
@@ -96,7 +105,6 @@ function validateStatic(apply: NormalizedApply, meta: DecoMeta | null) {
|
|
|
96
105
|
continue;
|
|
97
106
|
}
|
|
98
107
|
bodies.set(name, body);
|
|
99
|
-
violations.push(...checkSecrets(name, value, meta));
|
|
100
108
|
}
|
|
101
109
|
for (const name of apply.delete) {
|
|
102
110
|
for (const v of checkDeletedName(name))
|
|
@@ -123,7 +131,8 @@ function validateAgainst(
|
|
|
123
131
|
apply: NormalizedApply,
|
|
124
132
|
): BlockViolation[] {
|
|
125
133
|
const violations: BlockViolation[] = [];
|
|
126
|
-
|
|
134
|
+
// A name only an overlay's tombstone holds is gone once published: not a collision.
|
|
135
|
+
const existing = [...content.entries].filter(([, e]) => !e.deleted).map(([name]) => name);
|
|
127
136
|
for (const [name] of apply.set) {
|
|
128
137
|
// Another spelling of an existing entry updates that entry; it isn't a new name.
|
|
129
138
|
if (bySpelling.has(spellingKey(name))) continue;
|
|
@@ -147,13 +156,15 @@ interface Plan {
|
|
|
147
156
|
* spelling. Deleting any spelling of an entry deletes every spelling. Only
|
|
148
157
|
* guarded entries become commit expectations: without `ifMatch`, the last
|
|
149
158
|
* writer wins, so a concurrent save of the same entry must not turn into a
|
|
150
|
-
* retry storm.
|
|
159
|
+
* retry storm. On an overlay, a delete always names its file, even one the
|
|
160
|
+
* overlay doesn't hold: the base may have it.
|
|
151
161
|
*/
|
|
152
162
|
function plan(
|
|
153
163
|
content: LoadedContent,
|
|
154
164
|
bySpelling: EntriesBySpelling,
|
|
155
165
|
apply: NormalizedApply,
|
|
156
166
|
bodies: Map<string, string>,
|
|
167
|
+
overlay: boolean,
|
|
157
168
|
): Plan {
|
|
158
169
|
const versionOf = new Map(
|
|
159
170
|
content.snapshot.files.filter((f) => isBlockFileName(f.file)).map((f) => [f.file, f.version]),
|
|
@@ -171,7 +182,7 @@ function plan(
|
|
|
171
182
|
}
|
|
172
183
|
for (const name of apply.delete) {
|
|
173
184
|
const file = blockFileName(name);
|
|
174
|
-
if (versionOf.has(file)) deletes.add(file);
|
|
185
|
+
if (overlay || versionOf.has(file)) deletes.add(file);
|
|
175
186
|
if (bySpelling.has(spellingKey(name))) for (const other of groupFiles(name)) deletes.add(other);
|
|
176
187
|
}
|
|
177
188
|
for (const file of Object.keys(put)) deletes.delete(file);
|
|
@@ -220,8 +231,13 @@ function resultFromVersions(
|
|
|
220
231
|
touched.add(spellingKey(name));
|
|
221
232
|
}
|
|
222
233
|
for (const name of apply.delete) {
|
|
223
|
-
|
|
224
|
-
|
|
234
|
+
// An overlay's tombstone, when the commit recorded one: the entry's first, as in content.ts.
|
|
235
|
+
const key = spellingKey(name);
|
|
236
|
+
const tombstone = deletedFiles
|
|
237
|
+
.filter((file) => fullyDecodeFileName(file).name === key && file in fileVersions)
|
|
238
|
+
.sort()[0];
|
|
239
|
+
versions[name] = tombstone === undefined ? null : fileVersions[tombstone]!;
|
|
240
|
+
touched.add(key);
|
|
225
241
|
}
|
|
226
242
|
for (const file of deletedFiles) {
|
|
227
243
|
const name = blockNameFromFile(file);
|
|
@@ -230,15 +246,6 @@ function resultFromVersions(
|
|
|
230
246
|
return { revision, versions };
|
|
231
247
|
}
|
|
232
248
|
|
|
233
|
-
async function loadSchema(core: Core) {
|
|
234
|
-
const stored = await core.storage.readSchema();
|
|
235
|
-
if (stored === null) return { version: null, meta: null };
|
|
236
|
-
if (core.parsedSchema?.version !== stored.version) {
|
|
237
|
-
core.parsedSchema = { version: stored.version, meta: parseSchema(stored.text) };
|
|
238
|
-
}
|
|
239
|
-
return { version: stored.version, meta: core.parsedSchema.meta };
|
|
240
|
-
}
|
|
241
|
-
|
|
242
249
|
export async function blocksApply(
|
|
243
250
|
core: Core,
|
|
244
251
|
params: BlocksApplyParams,
|
|
@@ -246,33 +253,31 @@ export async function blocksApply(
|
|
|
246
253
|
const description = await core.description();
|
|
247
254
|
if (description.readOnly) throw readOnly();
|
|
248
255
|
const apply = normalize(params);
|
|
256
|
+
const { violations: invalid, bodies } = validateStatic(apply);
|
|
257
|
+
const storage = core.storage;
|
|
258
|
+
const overlay = typeof storage.writeSnapshot === "function";
|
|
259
|
+
const take = overlay ? () => storage.writeSnapshot!() : undefined;
|
|
249
260
|
|
|
250
261
|
for (let attempt = 1; attempt <= core.maxCommitAttempts; attempt++) {
|
|
251
|
-
const
|
|
252
|
-
const { violations, bodies } = validateStatic(apply, schema.meta);
|
|
253
|
-
const { snapshot, content } = await loadCurrentContent(core.storage, {
|
|
262
|
+
const { snapshot, content } = await loadCurrentContent(storage, {
|
|
254
263
|
readAll: false,
|
|
255
264
|
cache: core.cache,
|
|
265
|
+
take,
|
|
256
266
|
});
|
|
257
267
|
const bySpelling = entriesBySpelling(content!);
|
|
258
|
-
violations
|
|
268
|
+
const violations = [...invalid, ...validateAgainst(content!, bySpelling, apply)];
|
|
259
269
|
if (violations.length > 0) throw invalidBlock(violations);
|
|
260
270
|
|
|
261
271
|
const mismatches = checkIfMatch(bySpelling, apply);
|
|
262
272
|
if (mismatches) throw conflict({ entries: mismatches });
|
|
263
273
|
|
|
264
|
-
const { put, delete: deletes, expected } = plan(content!, bySpelling, apply, bodies);
|
|
274
|
+
const { put, delete: deletes, expected } = plan(content!, bySpelling, apply, bodies, overlay);
|
|
265
275
|
if (Object.keys(put).length === 0 && deletes.length === 0) {
|
|
266
|
-
|
|
276
|
+
// Nothing to write. The result's revision is blocks.list's, which an overlay's isn't.
|
|
277
|
+
const revision = overlay ? (await storage.snapshot()).revision : snapshot.revision;
|
|
278
|
+
return resultFromVersions(apply, revision, {}, []);
|
|
267
279
|
}
|
|
268
|
-
|
|
269
|
-
const result = await core.storage.commit({
|
|
270
|
-
base: snapshot,
|
|
271
|
-
put,
|
|
272
|
-
delete: deletes,
|
|
273
|
-
expected,
|
|
274
|
-
...(apply.set.length > 0 ? { expectedSchemaVersion: schema.version } : {}),
|
|
275
|
-
});
|
|
280
|
+
const result = await storage.commit({ base: snapshot, put, delete: deletes, expected });
|
|
276
281
|
if (result.status === "committed") {
|
|
277
282
|
return resultFromVersions(apply, result.revision, result.versions, deletes);
|
|
278
283
|
}
|
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
limitExceeded,
|
|
28
28
|
readOnly,
|
|
29
29
|
unavailable,
|
|
30
|
+
type VersionMismatch,
|
|
30
31
|
} from "../../errors.ts";
|
|
31
32
|
import {
|
|
32
33
|
blockFileName,
|
|
@@ -60,7 +61,7 @@ import type {
|
|
|
60
61
|
import { type ParsedBody, parseBody } from "../bodyCache.ts";
|
|
61
62
|
import { type LoadedContent, loadCurrentContent } from "../content.ts";
|
|
62
63
|
import { type Core, MAX_BLOCK_BYTES, MAX_NAMES_PER_GET, MAX_OPS_PER_APPLY } from "../core.ts";
|
|
63
|
-
import {
|
|
64
|
+
import { type EntriesBySpelling, entriesBySpelling } from "./apply.ts";
|
|
64
65
|
|
|
65
66
|
const keyOf = (file: string) => fullyDecodeFileName(file).name;
|
|
66
67
|
|
|
@@ -71,7 +72,10 @@ const byFile = (a: { file: string }, b: { file: string }) =>
|
|
|
71
72
|
interface EntryChange {
|
|
72
73
|
name: string;
|
|
73
74
|
kind: "set" | "delete";
|
|
74
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* The current winner's version; for an entry no file of which exists now,
|
|
77
|
+
* an overlay's tombstone version, else `null`.
|
|
78
|
+
*/
|
|
75
79
|
version: string | null;
|
|
76
80
|
baseVersion: string | null;
|
|
77
81
|
/** The current winning file, when it was readable as a candidate. */
|
|
@@ -165,6 +169,9 @@ async function storageChanges(
|
|
|
165
169
|
const answer = await core.storage.changes(base, snapshot);
|
|
166
170
|
const changes: StorageChanges = {
|
|
167
171
|
files: [...new Set(answer.files.filter(isBlockFileName))].sort(),
|
|
172
|
+
...(answer.tombstones
|
|
173
|
+
? { tombstones: answer.tombstones.filter((f) => isBlockFileName(f.file)) }
|
|
174
|
+
: {}),
|
|
168
175
|
baseFiles: answer.baseFiles.filter((f) => isBlockFileName(f.file)),
|
|
169
176
|
};
|
|
170
177
|
core.changesMemo = { revision, changes };
|
|
@@ -197,6 +204,12 @@ async function loadChanges(core: Core): Promise<ChangesState> {
|
|
|
197
204
|
else baseByKey.set(key, [file]);
|
|
198
205
|
}
|
|
199
206
|
|
|
207
|
+
// An overlay's deletion is versioned by its first tombstone, as `blocks.apply`'s guard sees it.
|
|
208
|
+
const tombstoneByKey = new Map<string, string>();
|
|
209
|
+
for (const t of [...(answer.tombstones ?? [])].sort(byFile)) {
|
|
210
|
+
if (!tombstoneByKey.has(keyOf(t.file))) tombstoneByKey.set(keyOf(t.file), t.version);
|
|
211
|
+
}
|
|
212
|
+
|
|
200
213
|
const bySpelling = entriesBySpelling(content!);
|
|
201
214
|
const changes = new Map<string, EntryChange>();
|
|
202
215
|
for (const [key, changedFiles] of changedByKey) {
|
|
@@ -210,7 +223,9 @@ async function loadChanges(core: Core): Promise<ChangesState> {
|
|
|
210
223
|
current?.name ??
|
|
211
224
|
(winner ? blockNameFromFile(winner.file) : blockNameFromFile(changedFiles[0]!)),
|
|
212
225
|
kind: exists ? "set" : "delete",
|
|
213
|
-
version:
|
|
226
|
+
version:
|
|
227
|
+
current?.entry.version ??
|
|
228
|
+
(exists ? currentFiles[0]!.version : (tombstoneByKey.get(key) ?? null)),
|
|
214
229
|
baseVersion: winner?.version ?? null,
|
|
215
230
|
currentFile: current?.entry.file ?? null,
|
|
216
231
|
baseFile: winner?.file ?? null,
|
|
@@ -403,6 +418,29 @@ function guardExpectations(
|
|
|
403
418
|
return expected;
|
|
404
419
|
}
|
|
405
420
|
|
|
421
|
+
/**
|
|
422
|
+
* `changes.discard`'s guard: each name's version as `changes.list` shows it,
|
|
423
|
+
* so an overlay's deletion is guarded by its tombstone version (no file of
|
|
424
|
+
* it exists, so the content alone would say `null`).
|
|
425
|
+
*/
|
|
426
|
+
function discardMismatches(
|
|
427
|
+
state: ChangesState,
|
|
428
|
+
ifMatch: Array<[string, string | null]>,
|
|
429
|
+
): Record<string, VersionMismatch> | null {
|
|
430
|
+
const mismatches: Record<string, VersionMismatch> = {};
|
|
431
|
+
let failed = false;
|
|
432
|
+
for (const [name, expected] of ifMatch) {
|
|
433
|
+
const key = spellingKey(name);
|
|
434
|
+
const actual =
|
|
435
|
+
state.bySpelling.get(key)?.entry.version ?? state.changes.get(key)?.version ?? null;
|
|
436
|
+
if (actual !== expected) {
|
|
437
|
+
mismatches[name] = { expected, actual };
|
|
438
|
+
failed = true;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return failed ? mismatches : null;
|
|
442
|
+
}
|
|
443
|
+
|
|
406
444
|
export async function changesDiscard(
|
|
407
445
|
core: Core,
|
|
408
446
|
params: ChangesDiscardParams,
|
|
@@ -420,7 +458,7 @@ export async function changesDiscard(
|
|
|
420
458
|
|
|
421
459
|
for (let attempt = 1; attempt <= core.maxCommitAttempts; attempt++) {
|
|
422
460
|
const state = await loadChanges(core);
|
|
423
|
-
const mismatches =
|
|
461
|
+
const mismatches = discardMismatches(state, ifMatch);
|
|
424
462
|
if (mismatches) throw conflict({ entries: mismatches });
|
|
425
463
|
|
|
426
464
|
const restore = new Map<string, StorageFile>();
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The three read methods: `describe`, `schema.get` and `blocks.list`.
|
|
3
3
|
*/
|
|
4
|
-
import { publicKeyDerFromPem } from "../../ciphertext.ts";
|
|
5
4
|
import { unavailable } from "../../errors.ts";
|
|
6
5
|
import {
|
|
7
6
|
ASSETS_URL_PREFIX,
|
|
@@ -19,30 +18,8 @@ import type { Core } from "../core.ts";
|
|
|
19
18
|
|
|
20
19
|
const DEFAULT_SERVER = { name: "deco-blocks", version: "unknown" };
|
|
21
20
|
|
|
22
|
-
/** The largest `.deco/secrets.pub` served (a 16384-bit key's PEM is under 3 KiB). */
|
|
23
|
-
const MAX_PUBLIC_KEY_BYTES = 16 * 1024;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The public key `describe` may serve: a single `PUBLIC KEY` PEM block of at
|
|
27
|
-
* most 16 KiB. Anything else (a private key committed by mistake, two
|
|
28
|
-
* blocks, stray text) is never broadcast to the site editor.
|
|
29
|
-
*/
|
|
30
|
-
function servablePublicKey(text: string | null): string | null {
|
|
31
|
-
if (text === null) return null;
|
|
32
|
-
if (new TextEncoder().encode(text).byteLength > MAX_PUBLIC_KEY_BYTES) return null;
|
|
33
|
-
if (text.includes("PRIVATE KEY")) return null;
|
|
34
|
-
return publicKeyDerFromPem(text) === null ? null : text;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
21
|
export async function describe(core: Core): Promise<DescribeResult> {
|
|
38
22
|
const description = await core.description();
|
|
39
|
-
const stored = await core.storage.readSecretsPublicKey();
|
|
40
|
-
const publicKey = servablePublicKey(stored);
|
|
41
|
-
if (stored !== null && publicKey === null) {
|
|
42
|
-
core.options.onError?.(
|
|
43
|
-
new Error(".deco/secrets.pub isn't a single PUBLIC KEY PEM block; describe reports no key"),
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
23
|
const readOnly = description.readOnly;
|
|
47
24
|
return {
|
|
48
25
|
protocol: PROTOCOL_NAME,
|
|
@@ -62,7 +39,8 @@ export async function describe(core: Core): Promise<DescribeResult> {
|
|
|
62
39
|
urlPrefix: ASSETS_URL_PREFIX,
|
|
63
40
|
maxBytes: description.assets.maxBytes,
|
|
64
41
|
},
|
|
65
|
-
secrets:
|
|
42
|
+
secrets: null,
|
|
43
|
+
...(typeof core.storage.writeSnapshot === "function" ? { overlay: true as const } : {}),
|
|
66
44
|
};
|
|
67
45
|
}
|
|
68
46
|
|
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
* Inside the app root (the folder that contains `.deco/`):
|
|
6
6
|
* - `.deco/blocks/*.json` — the saved blocks; the only thing it writes.
|
|
7
7
|
* - `.deco/schema.gen.json`, falling back to `.deco/meta.gen.json` — read only.
|
|
8
|
-
* - `.deco/secrets.pub` — read only, reported by `describe`.
|
|
9
8
|
* - `.deco/index.ts` is code and is never written.
|
|
10
9
|
* - Uploads go to `public/assets` (or `assetsDir`), never overwriting a file.
|
|
11
10
|
*
|
|
@@ -106,15 +105,6 @@ async function findRepoRoot(start: string): Promise<string> {
|
|
|
106
105
|
}
|
|
107
106
|
}
|
|
108
107
|
|
|
109
|
-
async function readTextOrNull(path: string): Promise<string | null> {
|
|
110
|
-
try {
|
|
111
|
-
return await readFile(path, "utf8");
|
|
112
|
-
} catch (error) {
|
|
113
|
-
if (isMissing(error)) return null;
|
|
114
|
-
throw error;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
108
|
async function readBytesOrNull(path: string): Promise<Uint8Array | null> {
|
|
119
109
|
try {
|
|
120
110
|
return await readFile(path);
|
|
@@ -274,8 +264,6 @@ export function createFsStorage(options: FsStorageOptions): FsStorage {
|
|
|
274
264
|
|
|
275
265
|
readSchema,
|
|
276
266
|
|
|
277
|
-
readSecretsPublicKey: () => readTextOrNull(join(decoDir, "secrets.pub")),
|
|
278
|
-
|
|
279
267
|
async commit(attempt): Promise<CommitResult> {
|
|
280
268
|
if (readOnly) throw new StorageUnavailableError("the storage is read-only");
|
|
281
269
|
for (const file of [
|
|
@@ -299,12 +287,6 @@ export function createFsStorage(options: FsStorageOptions): FsStorage {
|
|
|
299
287
|
) {
|
|
300
288
|
return { status: "stale" } as const;
|
|
301
289
|
}
|
|
302
|
-
if (attempt.expectedSchemaVersion !== undefined) {
|
|
303
|
-
const schema = await readSchema();
|
|
304
|
-
if ((schema?.version ?? null) !== attempt.expectedSchemaVersion) {
|
|
305
|
-
return { status: "stale" } as const;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
290
|
for (const [file, expected] of Object.entries(attempt.expected)) {
|
|
309
291
|
if ((await freshVersion(file)) !== expected) return { status: "stale" } as const;
|
|
310
292
|
}
|
|
@@ -13,22 +13,23 @@ import {
|
|
|
13
13
|
type CommitResult,
|
|
14
14
|
type ContentStorage,
|
|
15
15
|
type StorageDescription,
|
|
16
|
+
type StorageFile,
|
|
16
17
|
StorageInvalidFileError,
|
|
17
18
|
StorageNotFoundError,
|
|
18
19
|
type StorageSnapshot,
|
|
19
20
|
type StoredFileBody,
|
|
20
21
|
type StoredSchema,
|
|
22
|
+
TOMBSTONE_VERSION,
|
|
21
23
|
} from "../storage.ts";
|
|
22
24
|
|
|
23
25
|
/** Everything a memory storage holds; JSON-serializable, so it survives a "restart". */
|
|
24
26
|
export interface MemoryStorageState {
|
|
25
|
-
/** Saved-block files, by file name, with their content. */
|
|
27
|
+
/** Saved-block files, by file name, with their content (an overlay's own files, for an overlay). */
|
|
26
28
|
files: Record<string, string>;
|
|
27
29
|
/** The content of `schema.gen.json`, or `null` without one. */
|
|
28
30
|
schema: string | null;
|
|
29
31
|
/** The content of `meta.gen.json`, the fallback schema, or `null`. */
|
|
30
32
|
legacySchema?: string | null;
|
|
31
|
-
secretsPublicKey: string | null;
|
|
32
33
|
assets: Record<string, string>;
|
|
33
34
|
/** `false` simulates an app root with no `.deco` folder at all. */
|
|
34
35
|
hasDecoFolder: boolean;
|
|
@@ -38,6 +39,15 @@ export interface MemoryStorageState {
|
|
|
38
39
|
baseRevision: string;
|
|
39
40
|
/** The base's branch. Default `null`. */
|
|
40
41
|
baseRef: string | null;
|
|
42
|
+
/**
|
|
43
|
+
* An overlay storage (`writeSnapshot()`, `describe.overlay`), like a hosted
|
|
44
|
+
* draft: `files` is then the overlay alone, laid over `base`, and
|
|
45
|
+
* `deleted` the base files it deletes (its tombstones, which
|
|
46
|
+
* `writeSnapshot()` lists at `TOMBSTONE_VERSION`). Default `false`.
|
|
47
|
+
*/
|
|
48
|
+
overlay: boolean;
|
|
49
|
+
/** For an overlay: base files the overlay deletes. */
|
|
50
|
+
deleted: string[];
|
|
41
51
|
}
|
|
42
52
|
|
|
43
53
|
export interface MemoryStorageOptions {
|
|
@@ -59,7 +69,7 @@ export interface MemoryStorage extends ContentStorage {
|
|
|
59
69
|
setSchema(schema: string | object | null): void;
|
|
60
70
|
/** Replaces the base's files, as a publish (or an outside commit) would. */
|
|
61
71
|
setBase(files: Record<string, string>): void;
|
|
62
|
-
/** Makes the current
|
|
72
|
+
/** Makes the current content the base (emptying an overlay), as a commit of every change would. */
|
|
63
73
|
commitBase(): void;
|
|
64
74
|
/** How many commits landed. */
|
|
65
75
|
readonly commits: number;
|
|
@@ -72,12 +82,13 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
72
82
|
files: {},
|
|
73
83
|
schema: null,
|
|
74
84
|
legacySchema: null,
|
|
75
|
-
secretsPublicKey: null,
|
|
76
85
|
assets: {},
|
|
77
86
|
hasDecoFolder: true,
|
|
78
87
|
base: {},
|
|
79
88
|
baseRevision: "base-0",
|
|
80
89
|
baseRef: null,
|
|
90
|
+
overlay: false,
|
|
91
|
+
deleted: [],
|
|
81
92
|
...options.state,
|
|
82
93
|
});
|
|
83
94
|
// structuredClone keeps shared references: the base is never the files object.
|
|
@@ -91,18 +102,83 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
91
102
|
};
|
|
92
103
|
let commits = 0;
|
|
93
104
|
|
|
94
|
-
|
|
105
|
+
/** The content `snapshot()` lists: the files, or for an overlay its files laid over the base. */
|
|
106
|
+
function content(): Record<string, string> {
|
|
107
|
+
if (!state.overlay) return state.files;
|
|
108
|
+
const out: Record<string, string> = Object.create(null);
|
|
109
|
+
for (const [file, text] of Object.entries(state.base)) {
|
|
110
|
+
if (!state.deleted.includes(file)) out[file] = text;
|
|
111
|
+
}
|
|
112
|
+
return Object.assign(out, state.files);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function versionsOf(files: Record<string, string>): Promise<Map<string, string>> {
|
|
95
116
|
const entries = await Promise.all(
|
|
96
|
-
Object.entries(
|
|
97
|
-
async ([file, content]) => [file, await version(content)] as const,
|
|
98
|
-
),
|
|
117
|
+
Object.entries(files).map(async ([file, text]) => [file, await version(text)] as const),
|
|
99
118
|
);
|
|
100
119
|
return new Map(entries);
|
|
101
120
|
}
|
|
102
121
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
122
|
+
const currentVersions = () => versionsOf(content());
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The revision `snapshot()` reports. An overlay's also moves with the
|
|
126
|
+
* overlay itself, even when the content it shows doesn't (an entry saved
|
|
127
|
+
* with the base's value), so `changes.*` never answers from a stale memo.
|
|
128
|
+
*/
|
|
129
|
+
async function contentRevision(): Promise<string> {
|
|
130
|
+
const revision = await revisionOf(await currentVersions());
|
|
131
|
+
if (!state.overlay) return revision;
|
|
132
|
+
const own = await revisionOf(await versionsOf(state.files));
|
|
133
|
+
return sha256Hex(`${revision}\n${own}\n${[...state.deleted].sort().join("\n")}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function snapshotOf(
|
|
137
|
+
files: Record<string, string>,
|
|
138
|
+
revision?: () => Promise<string>,
|
|
139
|
+
): Promise<StorageSnapshot> {
|
|
140
|
+
if (!state.hasDecoFolder) throw new StorageNotFoundError("no .deco folder");
|
|
141
|
+
const versions = await versionsOf(files);
|
|
142
|
+
return {
|
|
143
|
+
revision: revision ? await revision() : await revisionOf(versions),
|
|
144
|
+
resolvedRef: null,
|
|
145
|
+
files: [...versions].map(([file, v]) => ({
|
|
146
|
+
file,
|
|
147
|
+
version: v,
|
|
148
|
+
size: new TextEncoder().encode(files[file]).byteLength,
|
|
149
|
+
})),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The overlay's tombstones: the base files it deletes. */
|
|
154
|
+
const tombstones = (): StorageFile[] =>
|
|
155
|
+
state.deleted
|
|
156
|
+
.filter((file) => file in state.base)
|
|
157
|
+
.map((file) => ({ file, version: TOMBSTONE_VERSION, deleted: true }));
|
|
158
|
+
|
|
159
|
+
/** The overlay alone, as `writeSnapshot()` lists it: its files and its tombstones. */
|
|
160
|
+
async function overlaySnapshot(): Promise<StorageSnapshot> {
|
|
161
|
+
const own = await snapshotOf(state.files);
|
|
162
|
+
return { ...own, revision: await contentRevision(), files: [...own.files, ...tombstones()] };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* An overlay's commit: `apply` writes the overlay and records deletions
|
|
167
|
+
* (returning their tombstone versions); `discard` drops files from it.
|
|
168
|
+
*/
|
|
169
|
+
function commitToOverlay(attempt: CommitAttempt): Record<string, string> {
|
|
170
|
+
const touched = [...Object.keys(attempt.put), ...attempt.delete];
|
|
171
|
+
state.deleted = state.deleted.filter((file) => !touched.includes(file));
|
|
172
|
+
for (const file of touched) delete state.files[file];
|
|
173
|
+
const recorded: Record<string, string> = {};
|
|
174
|
+
if (attempt.reason === "discard") return recorded;
|
|
175
|
+
for (const file of attempt.delete) {
|
|
176
|
+
if (!(file in state.base)) continue;
|
|
177
|
+
state.deleted.push(file);
|
|
178
|
+
recorded[file] = TOMBSTONE_VERSION;
|
|
179
|
+
}
|
|
180
|
+
for (const [file, text] of Object.entries(attempt.put)) state.files[file] = text;
|
|
181
|
+
return recorded;
|
|
106
182
|
}
|
|
107
183
|
|
|
108
184
|
async function revisionOf(versions: Map<string, string>): Promise<string> {
|
|
@@ -125,25 +201,23 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
125
201
|
if (attempt.expectedBase !== undefined && attempt.expectedBase !== state.baseRevision) {
|
|
126
202
|
return { status: "stale" };
|
|
127
203
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
)
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
const versions = await currentVersions();
|
|
204
|
+
// An overlay's apply is guarded against the overlay (its tombstones too);
|
|
205
|
+
// anything else against the content.
|
|
206
|
+
const overlayApply = state.overlay && attempt.reason !== "discard";
|
|
207
|
+
const versions = await versionsOf(overlayApply ? state.files : content());
|
|
208
|
+
if (overlayApply) for (const t of tombstones()) versions.set(t.file, t.version);
|
|
135
209
|
for (const [file, expected] of Object.entries(attempt.expected)) {
|
|
136
210
|
if ((versions.get(file) ?? null) !== expected) return { status: "stale" };
|
|
137
211
|
}
|
|
138
212
|
// Validate everything before mutating, so the commit is all or nothing.
|
|
139
213
|
const written: Record<string, string> = {};
|
|
140
|
-
for (const [file,
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const revision = await
|
|
214
|
+
for (const [file, text] of Object.entries(attempt.put)) written[file] = await version(text);
|
|
215
|
+
if (state.overlay) Object.assign(written, commitToOverlay(attempt));
|
|
216
|
+
else {
|
|
217
|
+
for (const file of attempt.delete) delete state.files[file];
|
|
218
|
+
for (const [file, text] of Object.entries(attempt.put)) state.files[file] = text;
|
|
219
|
+
}
|
|
220
|
+
const revision = await contentRevision();
|
|
147
221
|
commits++;
|
|
148
222
|
return { status: "committed", revision, versions: written };
|
|
149
223
|
}
|
|
@@ -155,25 +229,16 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
155
229
|
|
|
156
230
|
describe: () => description,
|
|
157
231
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return {
|
|
162
|
-
revision: await revisionOf(versions),
|
|
163
|
-
resolvedRef: null,
|
|
164
|
-
files: [...versions].map(([file, v]) => ({
|
|
165
|
-
file,
|
|
166
|
-
version: v,
|
|
167
|
-
size: new TextEncoder().encode(state.files[file]).byteLength,
|
|
168
|
-
})),
|
|
169
|
-
};
|
|
170
|
-
},
|
|
232
|
+
snapshot: () => snapshotOf(content(), contentRevision),
|
|
233
|
+
|
|
234
|
+
...(state.overlay ? { writeSnapshot: overlaySnapshot } : {}),
|
|
171
235
|
|
|
172
236
|
async readFiles(_snapshot, files) {
|
|
173
237
|
const out: Record<string, StoredFileBody> = Object.create(null);
|
|
238
|
+
const current = content();
|
|
174
239
|
for (const file of files) {
|
|
175
|
-
if (!(file in
|
|
176
|
-
const text =
|
|
240
|
+
if (!(file in current)) continue;
|
|
241
|
+
const text = current[file]!;
|
|
177
242
|
out[file] = { text, version: await version(text) };
|
|
178
243
|
await options.afterRead?.(file, storage);
|
|
179
244
|
}
|
|
@@ -187,10 +252,6 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
187
252
|
return { version: await version(text), text, resolvedRef: null };
|
|
188
253
|
},
|
|
189
254
|
|
|
190
|
-
async readSecretsPublicKey() {
|
|
191
|
-
return state.secretsPublicKey;
|
|
192
|
-
},
|
|
193
|
-
|
|
194
255
|
async base() {
|
|
195
256
|
return { revision: state.baseRevision, resolvedRef: state.baseRef };
|
|
196
257
|
},
|
|
@@ -203,6 +264,15 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
203
264
|
size: new TextEncoder().encode(content).byteLength,
|
|
204
265
|
})),
|
|
205
266
|
);
|
|
267
|
+
if (state.overlay) {
|
|
268
|
+
// The overlay's own files are the changes, even one equal to the base.
|
|
269
|
+
const deleted = tombstones();
|
|
270
|
+
return {
|
|
271
|
+
files: [...Object.keys(state.files), ...deleted.map((t) => t.file)],
|
|
272
|
+
tombstones: deleted,
|
|
273
|
+
baseFiles,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
206
276
|
const baseVersion = new Map(baseFiles.map((f) => [f.file, f.version]));
|
|
207
277
|
const current = new Map(snapshot.files.map((f) => [f.file, f.version]));
|
|
208
278
|
const files = new Set<string>();
|
|
@@ -239,7 +309,11 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
239
309
|
|
|
240
310
|
setFile(file, content) {
|
|
241
311
|
if (content === null) delete state.files[file];
|
|
242
|
-
else
|
|
312
|
+
else {
|
|
313
|
+
state.files[file] = content;
|
|
314
|
+
// An overlay's file replaces its tombstone.
|
|
315
|
+
state.deleted = state.deleted.filter((deleted) => deleted !== file);
|
|
316
|
+
}
|
|
243
317
|
},
|
|
244
318
|
|
|
245
319
|
setBase(files) {
|
|
@@ -248,7 +322,11 @@ export function createMemoryStorage(options: MemoryStorageOptions = {}): MemoryS
|
|
|
248
322
|
},
|
|
249
323
|
|
|
250
324
|
commitBase() {
|
|
251
|
-
storage.setBase(
|
|
325
|
+
storage.setBase(content());
|
|
326
|
+
if (state.overlay) {
|
|
327
|
+
state.files = {};
|
|
328
|
+
state.deleted = [];
|
|
329
|
+
}
|
|
252
330
|
},
|
|
253
331
|
|
|
254
332
|
setSchema(schema) {
|