@decocms/blocks 8.6.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 +31 -21
- package/dist/v8/cms.js.map +1 -1
- package/dist/v8/content.d.ts +35 -1
- package/dist/v8/content.d.ts.map +1 -1
- package/dist/v8/content.js +81 -2
- package/dist/v8/content.js.map +1 -1
- package/dist/v8/index.d.ts +1 -1
- package/dist/v8/index.d.ts.map +1 -1
- package/dist/v8/preview/DraftBadge.d.ts +9 -1
- package/dist/v8/preview/DraftBadge.d.ts.map +1 -1
- package/dist/v8/preview/DraftBadge.js +42 -2
- package/dist/v8/preview/DraftBadge.js.map +1 -1
- package/dist/v8/remoteLoader.d.ts.map +1 -1
- package/dist/v8/remoteLoader.js +3 -1
- package/dist/v8/remoteLoader.js.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 +37 -10
- 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 +37 -22
- package/src/v8/content.ts +82 -2
- package/src/v8/index.ts +0 -1
- package/src/v8/preview/DraftBadge.tsx +72 -2
- package/src/v8/remoteLoader.ts +3 -1
- package/src/v8/resolver.ts +1 -1
- package/src/v8/telemetry.ts +5 -20
- package/src/v8/types.ts +36 -7
- 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
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `blocks.apply`'s own size guards and the
|
|
2
|
+
* `blocks.apply`'s own size guards, and the checks it leaves to `deco check`.
|
|
3
3
|
*/
|
|
4
|
-
import { encryptToCiphertext, publicKeyPemFromDer } from "../../ciphertext.ts";
|
|
5
4
|
import { ErrorCode, type InvalidBlockData } from "../../errors.ts";
|
|
6
5
|
import { MAX_BLOCK_BYTES, MAX_OPS_PER_APPLY, MAX_REQUEST_BYTES } from "../../server/core.ts";
|
|
7
6
|
import {
|
|
@@ -19,27 +18,6 @@ async function writable(ctx: ConformanceContext) {
|
|
|
19
18
|
return d;
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
/** A throwaway public key, for endpoints that don't report one. */
|
|
23
|
-
async function ephemeralPublicKey(): Promise<string> {
|
|
24
|
-
const pair = (await crypto.subtle.generateKey(
|
|
25
|
-
{
|
|
26
|
-
name: "RSA-OAEP",
|
|
27
|
-
modulusLength: 2048,
|
|
28
|
-
publicExponent: new Uint8Array([1, 0, 1]),
|
|
29
|
-
hash: "SHA-256",
|
|
30
|
-
},
|
|
31
|
-
true,
|
|
32
|
-
["encrypt", "decrypt"],
|
|
33
|
-
)) as CryptoKeyPair;
|
|
34
|
-
return publicKeyPemFromDer(new Uint8Array(await crypto.subtle.exportKey("spki", pair.publicKey)));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** A real ciphertext: encrypted with the endpoint's public key when it reports one. */
|
|
38
|
-
async function realCiphertext(ctx: ConformanceContext, value: string): Promise<string> {
|
|
39
|
-
const publicKey = (await ctx.describe()).secrets?.publicKey ?? (await ephemeralPublicKey());
|
|
40
|
-
return encryptToCiphertext(publicKey, value);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
21
|
export const guardCases: ConformanceCase[] = [
|
|
44
22
|
{
|
|
45
23
|
id: "limits/ops-per-apply",
|
|
@@ -96,41 +74,27 @@ export const guardCases: ConformanceCase[] = [
|
|
|
96
74
|
},
|
|
97
75
|
},
|
|
98
76
|
{
|
|
99
|
-
id: "
|
|
100
|
-
title: "
|
|
77
|
+
id: "writes/schema-free",
|
|
78
|
+
title: "blocks.apply saves any JSON object, whatever the schema says about it",
|
|
101
79
|
async run(ctx) {
|
|
102
80
|
await writable(ctx);
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
ctx.
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
);
|
|
115
|
-
const violation = (error.data as InvalidBlockData).violations[0];
|
|
116
|
-
assertEqual(violation?.name, plain, "the violation names the entry");
|
|
117
|
-
// Text that only looks like a ciphertext is plain text all the same.
|
|
118
|
-
for (const ciphertext of ["hunter2", "v1.hunter2", "v1.my-api-key_123", "v1.QUJD.ZGVm"]) {
|
|
119
|
-
await expectError(
|
|
120
|
-
ctx.client.blocksApply({
|
|
121
|
-
set: { [ctx.name("bad-cipher")]: block({ __resolveType: "secret", ciphertext }) },
|
|
122
|
-
}),
|
|
123
|
-
ErrorCode.InvalidBlock,
|
|
124
|
-
`the malformed ciphertext ${JSON.stringify(ciphertext)}`,
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
const ok = ctx.name("secret");
|
|
128
|
-
const ciphertext = await realCiphertext(ctx, "conformance-value");
|
|
129
|
-
await ctx.client.blocksApply({
|
|
130
|
-
set: { [ok]: block({ __resolveType: "secret", ciphertext }) },
|
|
131
|
-
});
|
|
81
|
+
// The schema is `deco check`'s business, in the site's CI: a write never reads it.
|
|
82
|
+
const values = {
|
|
83
|
+
[ctx.name("unknown-type")]: { __resolveType: "conformance/not-in-any-schema.tsx", x: 1 },
|
|
84
|
+
[ctx.name("v7-secret")]: {
|
|
85
|
+
__resolveType: "website/loaders/secret.ts",
|
|
86
|
+
encrypted: "not-a-ciphertext",
|
|
87
|
+
},
|
|
88
|
+
[ctx.name("plain-field")]: { __resolveType: "secret", ciphertext: "hunter2" },
|
|
89
|
+
[ctx.name("no-type")]: { title: 42, sections: "not a list" },
|
|
90
|
+
};
|
|
91
|
+
const applied = await ctx.client.blocksApply({ set: values });
|
|
132
92
|
const list = await ctx.client.blocksList();
|
|
133
|
-
assert(!list.notModified
|
|
93
|
+
assert(!list.notModified, "an unconditional list returns the map");
|
|
94
|
+
for (const [name, value] of Object.entries(values)) {
|
|
95
|
+
assertEqual(list.blocks[name], value, `${name} is saved as sent`);
|
|
96
|
+
assertEqual(list.versions[name], applied.versions[name], `${name}'s version`);
|
|
97
|
+
}
|
|
134
98
|
},
|
|
135
99
|
},
|
|
136
100
|
];
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Overlay endpoints (protocol 1.2, `describe.overlay`), such as a hosted
|
|
3
|
+
* draft over the published branch: `blocks.apply`'s `ifMatch` compares with
|
|
4
|
+
* the overlay alone, and `changes.list` is the overlay's own entries. A
|
|
5
|
+
* deletion the overlay records is one of them: it has a tombstone version,
|
|
6
|
+
* so `ifMatch: null` on it fails. Every case skips on an endpoint that isn't
|
|
7
|
+
* an overlay.
|
|
8
|
+
*/
|
|
9
|
+
import { ErrorCode } from "../../errors.ts";
|
|
10
|
+
import type { BlocksListResult, ChangesListResult } from "../../types.ts";
|
|
11
|
+
import { assert, assertEqual, type ConformanceContext, expectError } from "../context.ts";
|
|
12
|
+
import type { ConformanceCase } from "./types.ts";
|
|
13
|
+
|
|
14
|
+
type FullList = Extract<BlocksListResult, { notModified: false }>;
|
|
15
|
+
type FullChanges = Extract<ChangesListResult, { notModified: false }>;
|
|
16
|
+
|
|
17
|
+
async function list(ctx: ConformanceContext): Promise<FullList> {
|
|
18
|
+
const result = await ctx.client.blocksList();
|
|
19
|
+
assert(!result.notModified, "an unconditional list returns the map");
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function changes(ctx: ConformanceContext): Promise<FullChanges> {
|
|
24
|
+
const result = await ctx.client.changesList();
|
|
25
|
+
assert(!result.notModified, "an unconditional changes.list returns the changes");
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function overlay(ctx: ConformanceContext) {
|
|
30
|
+
const d = await ctx.describe();
|
|
31
|
+
if (d.overlay !== true) return ctx.skip("not an overlay endpoint");
|
|
32
|
+
if (d.readOnly) return ctx.skip("read-only endpoint");
|
|
33
|
+
return d;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The harness's base entry, which the overlay doesn't hold yet. */
|
|
37
|
+
async function baseEntry(ctx: ConformanceContext) {
|
|
38
|
+
const name = ctx.options.baseEntry?.name;
|
|
39
|
+
if (name === undefined) return ctx.skip("no baseEntry");
|
|
40
|
+
const blocks = await list(ctx);
|
|
41
|
+
assert(name in blocks.blocks, `the baseEntry "${name}" exists`);
|
|
42
|
+
assert(!(name in (await changes(ctx)).changes), `the overlay doesn't hold "${name}" yet`);
|
|
43
|
+
return { name, value: blocks.blocks[name]!, version: blocks.versions[name]! };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const overlayCases: ConformanceCase[] = [
|
|
47
|
+
{
|
|
48
|
+
id: "overlay/if-match",
|
|
49
|
+
title: "on an overlay, ifMatch null means the overlay doesn't hold the entry yet",
|
|
50
|
+
async run(ctx) {
|
|
51
|
+
await overlay(ctx);
|
|
52
|
+
const name = ctx.name("guarded");
|
|
53
|
+
const first = await ctx.client.blocksApply({
|
|
54
|
+
set: { [name]: { v: 1 } },
|
|
55
|
+
ifMatch: { [name]: null },
|
|
56
|
+
});
|
|
57
|
+
const error = await expectError(
|
|
58
|
+
ctx.client.blocksApply({ set: { [name]: { v: 2 } }, ifMatch: { [name]: null } }),
|
|
59
|
+
ErrorCode.Conflict,
|
|
60
|
+
"ifMatch null on an entry the overlay holds",
|
|
61
|
+
);
|
|
62
|
+
assertEqual(
|
|
63
|
+
(error.data as { entries?: unknown })?.entries,
|
|
64
|
+
{ [name]: { expected: null, actual: first.versions[name] } },
|
|
65
|
+
"the conflict's entries",
|
|
66
|
+
);
|
|
67
|
+
const listed = (await changes(ctx)).changes[name];
|
|
68
|
+
assertEqual(
|
|
69
|
+
listed?.version,
|
|
70
|
+
first.versions[name],
|
|
71
|
+
"changes.list reports the guard's version",
|
|
72
|
+
);
|
|
73
|
+
await ctx.client.blocksApply({
|
|
74
|
+
set: { [name]: { v: 2 } },
|
|
75
|
+
ifMatch: { [name]: listed!.version },
|
|
76
|
+
});
|
|
77
|
+
assertEqual((await list(ctx)).blocks[name], { v: 2 }, "the guarded save landed");
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: "overlay/if-match-base",
|
|
82
|
+
title: "an entry only the base has is not in the overlay: ifMatch null saves it",
|
|
83
|
+
async run(ctx) {
|
|
84
|
+
await overlay(ctx);
|
|
85
|
+
const base = await baseEntry(ctx);
|
|
86
|
+
try {
|
|
87
|
+
const modified = { ...base.value, conformance: ctx.prefix };
|
|
88
|
+
const applied = await ctx.client.blocksApply({
|
|
89
|
+
set: { [base.name]: modified },
|
|
90
|
+
ifMatch: { [base.name]: null },
|
|
91
|
+
});
|
|
92
|
+
const listed = (await changes(ctx)).changes[base.name];
|
|
93
|
+
assertEqual(listed?.kind, "set", "the saved base entry is a change");
|
|
94
|
+
assertEqual(listed?.version, applied.versions[base.name], "with the saved version");
|
|
95
|
+
await expectError(
|
|
96
|
+
ctx.client.blocksApply({
|
|
97
|
+
set: { [base.name]: base.value },
|
|
98
|
+
ifMatch: { [base.name]: null },
|
|
99
|
+
}),
|
|
100
|
+
ErrorCode.Conflict,
|
|
101
|
+
"ifMatch null once the overlay holds the entry",
|
|
102
|
+
);
|
|
103
|
+
} finally {
|
|
104
|
+
await ctx.client.changesDiscard({ names: [base.name] }).catch(() => {});
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
id: "overlay/own-entries",
|
|
110
|
+
title: "changes.list lists every entry the overlay holds, even one equal to the base",
|
|
111
|
+
async run(ctx) {
|
|
112
|
+
await overlay(ctx);
|
|
113
|
+
const base = await baseEntry(ctx);
|
|
114
|
+
try {
|
|
115
|
+
const applied = await ctx.client.blocksApply({ set: { [base.name]: base.value } });
|
|
116
|
+
const listed = (await changes(ctx)).changes[base.name];
|
|
117
|
+
assertEqual(
|
|
118
|
+
listed?.kind,
|
|
119
|
+
"set",
|
|
120
|
+
"an entry saved with the base's value is still the overlay's",
|
|
121
|
+
);
|
|
122
|
+
assertEqual(listed?.version, applied.versions[base.name], "with the saved version");
|
|
123
|
+
await ctx.client.changesDiscard({ names: [base.name] });
|
|
124
|
+
assert(!(base.name in (await changes(ctx)).changes), "a discard drops it from the overlay");
|
|
125
|
+
assertEqual((await list(ctx)).blocks[base.name], base.value, "the base shows through");
|
|
126
|
+
} finally {
|
|
127
|
+
await ctx.client.changesDiscard({ names: [base.name] }).catch(() => {});
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
id: "overlay/delete-base",
|
|
133
|
+
title: "deleting an entry only the base has records the deletion over the base",
|
|
134
|
+
async run(ctx) {
|
|
135
|
+
await overlay(ctx);
|
|
136
|
+
const base = await baseEntry(ctx);
|
|
137
|
+
try {
|
|
138
|
+
const deleted = await ctx.client.blocksApply({ delete: [base.name] });
|
|
139
|
+
const tombstone = deleted.versions[base.name];
|
|
140
|
+
assert(typeof tombstone === "string", "the deletion reports its tombstone version");
|
|
141
|
+
assert(!(base.name in (await list(ctx)).blocks), "the entry is gone from blocks.list");
|
|
142
|
+
const listed = (await changes(ctx)).changes[base.name];
|
|
143
|
+
assertEqual(listed?.kind, "delete", "a delete change");
|
|
144
|
+
assertEqual(listed?.version, tombstone, "changes.list reports the tombstone version");
|
|
145
|
+
// A discard guarded with the version changes.list showed lands.
|
|
146
|
+
await ctx.client.changesDiscard({
|
|
147
|
+
names: [base.name],
|
|
148
|
+
ifMatch: { [base.name]: tombstone! },
|
|
149
|
+
});
|
|
150
|
+
} finally {
|
|
151
|
+
await ctx.client.changesDiscard({ names: [base.name] }).catch(() => {});
|
|
152
|
+
}
|
|
153
|
+
assertEqual((await list(ctx)).blocks[base.name], base.value, "a discard brings it back");
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
id: "overlay/if-match-deleted",
|
|
158
|
+
title:
|
|
159
|
+
"an entry the overlay deletes is in the overlay: ifMatch null fails, its tombstone passes",
|
|
160
|
+
async run(ctx) {
|
|
161
|
+
await overlay(ctx);
|
|
162
|
+
const base = await baseEntry(ctx);
|
|
163
|
+
try {
|
|
164
|
+
await ctx.client.blocksApply({ delete: [base.name] });
|
|
165
|
+
const tombstone = (await changes(ctx)).changes[base.name]?.version;
|
|
166
|
+
assert(typeof tombstone === "string", "changes.list reports the tombstone version");
|
|
167
|
+
const restored = { ...base.value, conformance: ctx.prefix };
|
|
168
|
+
const error = await expectError(
|
|
169
|
+
ctx.client.blocksApply({
|
|
170
|
+
set: { [base.name]: restored },
|
|
171
|
+
ifMatch: { [base.name]: null },
|
|
172
|
+
}),
|
|
173
|
+
ErrorCode.Conflict,
|
|
174
|
+
"ifMatch null on an entry the overlay deletes",
|
|
175
|
+
);
|
|
176
|
+
assertEqual(
|
|
177
|
+
(error.data as { entries?: unknown })?.entries,
|
|
178
|
+
{ [base.name]: { expected: null, actual: tombstone } },
|
|
179
|
+
"the conflict's entries",
|
|
180
|
+
);
|
|
181
|
+
assert(!(base.name in (await list(ctx)).blocks), "the refused save wrote nothing");
|
|
182
|
+
const applied = await ctx.client.blocksApply({
|
|
183
|
+
set: { [base.name]: restored },
|
|
184
|
+
ifMatch: { [base.name]: tombstone! },
|
|
185
|
+
});
|
|
186
|
+
assertEqual((await list(ctx)).blocks[base.name], restored, "the guarded save landed");
|
|
187
|
+
const listed = (await changes(ctx)).changes[base.name];
|
|
188
|
+
assertEqual(listed?.kind, "set", "the entry is a set change again");
|
|
189
|
+
assertEqual(listed?.version, applied.versions[base.name], "with the saved version");
|
|
190
|
+
} finally {
|
|
191
|
+
await ctx.client.changesDiscard({ names: [base.name] }).catch(() => {});
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
];
|
|
@@ -30,16 +30,16 @@ export const readCases: ConformanceCase[] = [
|
|
|
30
30
|
assert(d.assets.maxBytes > 0 && typeof d.assets.dir === "string", "assets");
|
|
31
31
|
}
|
|
32
32
|
assert(d.secrets === null || typeof d.secrets.publicKey === "string", "secrets");
|
|
33
|
+
assert(d.overlay === undefined || d.overlay === true, "overlay is absent or true");
|
|
33
34
|
},
|
|
34
35
|
},
|
|
35
36
|
{
|
|
36
37
|
id: "describe/secrets",
|
|
37
|
-
title: "describe
|
|
38
|
+
title: "describe reports no secrets key: content holds no secrets",
|
|
38
39
|
async run(ctx) {
|
|
39
|
-
const expected = ctx.options.secretsPublicKey;
|
|
40
|
-
if (expected === undefined) return ctx.skip("no secretsPublicKey fixture");
|
|
41
40
|
const d = await ctx.client.describe();
|
|
42
|
-
|
|
41
|
+
if (d.version.minor < 2) return ctx.skip(`protocol 1.${d.version.minor} predates 1.2`);
|
|
42
|
+
assertEqual(d.secrets, null, "describe.secrets");
|
|
43
43
|
},
|
|
44
44
|
},
|
|
45
45
|
{
|
|
@@ -27,6 +27,16 @@ function violations(error: { data: unknown }): InvalidBlockData["violations"] {
|
|
|
27
27
|
return data!.violations;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* A deleted name's version in a `blocks.apply` result: `null`, except on an
|
|
32
|
+
* overlay, which may record a tombstone even for a name the base lacks (a
|
|
33
|
+
* hosted draft's save never reads the base) and then reports its version.
|
|
34
|
+
*/
|
|
35
|
+
async function assertDeletedVersion(ctx: ConformanceContext, version: unknown, label: string) {
|
|
36
|
+
if ((await ctx.describe()).overlay === true && typeof version === "string") return;
|
|
37
|
+
assertEqual(version, null, label);
|
|
38
|
+
}
|
|
39
|
+
|
|
30
40
|
export const writeCases: ConformanceCase[] = [
|
|
31
41
|
{
|
|
32
42
|
id: "apply/create-and-read",
|
|
@@ -54,7 +64,7 @@ export const writeCases: ConformanceCase[] = [
|
|
|
54
64
|
const [a, b, c] = [ctx.name("a"), ctx.name("b"), ctx.name("c")];
|
|
55
65
|
await ctx.client.blocksApply({ set: { [a]: { v: 1 }, [b]: { v: 2 } } });
|
|
56
66
|
const result = await ctx.client.blocksApply({ set: { [c]: { v: 3 } }, delete: [a] });
|
|
57
|
-
|
|
67
|
+
await assertDeletedVersion(ctx, result.versions[a], "a deleted entry's version is null");
|
|
58
68
|
const after = await list(ctx);
|
|
59
69
|
assert(!(a in after.blocks), "the deleted entry is gone");
|
|
60
70
|
assertEqual(after.blocks[b], { v: 2 }, "the untouched entry stays");
|
|
@@ -82,7 +92,8 @@ export const writeCases: ConformanceCase[] = [
|
|
|
82
92
|
await writable(ctx);
|
|
83
93
|
const name = ctx.name("missing");
|
|
84
94
|
const result = await ctx.client.blocksApply({ delete: [name] });
|
|
85
|
-
assertEqual(result.versions,
|
|
95
|
+
assertEqual(Object.keys(result.versions), [name], "versions");
|
|
96
|
+
await assertDeletedVersion(ctx, result.versions[name], "the missing name's version");
|
|
86
97
|
},
|
|
87
98
|
},
|
|
88
99
|
{
|
|
@@ -149,7 +160,8 @@ export const writeCases: ConformanceCase[] = [
|
|
|
149
160
|
}
|
|
150
161
|
// A name ending in a source extension can still be deleted.
|
|
151
162
|
const result = await ctx.client.blocksApply({ delete: [`${p}-mod.ts`] });
|
|
152
|
-
assertEqual(result.versions,
|
|
163
|
+
assertEqual(Object.keys(result.versions), [`${p}-mod.ts`], "deleting a .ts name");
|
|
164
|
+
await assertDeletedVersion(ctx, result.versions[`${p}-mod.ts`], "the .ts name's version");
|
|
153
165
|
},
|
|
154
166
|
},
|
|
155
167
|
{
|
|
@@ -17,13 +17,6 @@ export interface ConformanceOptions {
|
|
|
17
17
|
headers?: Record<string, string>;
|
|
18
18
|
/** A fetch implementation (defaults to the global one); use it to call a handler in process. */
|
|
19
19
|
fetch?: (request: Request) => Promise<Response>;
|
|
20
|
-
/**
|
|
21
|
-
* A block type in the endpoint's schema with a `Secret` field, so the
|
|
22
|
-
* secret guard can be checked. Leave out to skip those cases.
|
|
23
|
-
*/
|
|
24
|
-
secretField?: { blockType: string; field: string };
|
|
25
|
-
/** The `.deco/secrets.pub` content the endpoint should report, if the harness wrote one. */
|
|
26
|
-
secretsPublicKey?: string;
|
|
27
20
|
/** Whether the endpoint has a schema (default true). `false` checks the `schema: null` result instead. */
|
|
28
21
|
hasSchema?: boolean;
|
|
29
22
|
/**
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
import { assetCases } from "./cases/assets.ts";
|
|
24
24
|
import { changesCases } from "./cases/changes.ts";
|
|
25
25
|
import { guardCases } from "./cases/guards.ts";
|
|
26
|
+
import { overlayCases } from "./cases/overlay.ts";
|
|
26
27
|
import { readCases } from "./cases/reads.ts";
|
|
27
28
|
import type { ConformanceCase } from "./cases/types.ts";
|
|
28
29
|
import { wireCases } from "./cases/wire.ts";
|
|
@@ -40,6 +41,7 @@ export const conformanceCases: readonly ConformanceCase[] = [
|
|
|
40
41
|
...writeCases,
|
|
41
42
|
...guardCases,
|
|
42
43
|
...changesCases,
|
|
44
|
+
...overlayCases,
|
|
43
45
|
...assetCases,
|
|
44
46
|
];
|
|
45
47
|
|
|
@@ -5,7 +5,13 @@
|
|
|
5
5
|
* needs bodies only to break ties between spellings).
|
|
6
6
|
*/
|
|
7
7
|
import { unavailable } from "../errors.ts";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
blockNameFromFile,
|
|
10
|
+
entryHasPath,
|
|
11
|
+
fullyDecodeFileName,
|
|
12
|
+
isBlockFileName,
|
|
13
|
+
resolveSpellings,
|
|
14
|
+
} from "../keys.ts";
|
|
9
15
|
import type { ContentStorage, StorageFile, StorageSnapshot } from "../storage.ts";
|
|
10
16
|
import type { Diagnostic } from "../types.ts";
|
|
11
17
|
import { type BodyCache, type ParsedBody, parseBody } from "./bodyCache.ts";
|
|
@@ -16,13 +22,18 @@ export interface LoadedEntry {
|
|
|
16
22
|
version: string;
|
|
17
23
|
/** The parsed entry; absent when the body wasn't needed. */
|
|
18
24
|
value?: Record<string, unknown>;
|
|
25
|
+
/**
|
|
26
|
+
* `true` for an entry an overlay deletes (only in `writeSnapshot()`'s
|
|
27
|
+
* listing): no file of it exists, and `version` is its tombstone's.
|
|
28
|
+
*/
|
|
29
|
+
deleted?: true;
|
|
19
30
|
}
|
|
20
31
|
|
|
21
32
|
export interface LoadedContent {
|
|
22
33
|
snapshot: StorageSnapshot;
|
|
23
34
|
/** Every entry, by name, in file-name order of the winning files. */
|
|
24
35
|
entries: Map<string, LoadedEntry>;
|
|
25
|
-
/** Every saved-block file, valid or not, by spelling key. */
|
|
36
|
+
/** Every saved-block file, valid or not, by spelling key (tombstones too). */
|
|
26
37
|
groups: Map<string, StorageFile[]>;
|
|
27
38
|
/** Files skipped or shadowed. */
|
|
28
39
|
diagnostics: Diagnostic[];
|
|
@@ -40,6 +51,8 @@ interface Options {
|
|
|
40
51
|
/** Read every body (`blocks.list`), not only the ones that break ties. */
|
|
41
52
|
readAll: boolean;
|
|
42
53
|
cache: BodyCache;
|
|
54
|
+
/** Takes the snapshot (default: `storage.snapshot()`; `blocks.apply` on an overlay: `writeSnapshot()`). */
|
|
55
|
+
take?: () => Promise<StorageSnapshot>;
|
|
43
56
|
}
|
|
44
57
|
|
|
45
58
|
async function loadContent(
|
|
@@ -60,7 +73,9 @@ async function loadContent(
|
|
|
60
73
|
let moved = false;
|
|
61
74
|
const parsed = new Map<string, ParsedBody>();
|
|
62
75
|
const toRead: StorageFile[] = [];
|
|
63
|
-
for (const
|
|
76
|
+
for (const all of groups.values()) {
|
|
77
|
+
// A tombstone has no body: it's never read, nor a spelling that competes.
|
|
78
|
+
const group = all.filter((f) => !f.deleted);
|
|
64
79
|
if (!readAll && group.length < 2) continue;
|
|
65
80
|
for (const file of group) {
|
|
66
81
|
// An existing file over MAX_BLOCK_BYTES is unreadable, like invalid JSON: it's
|
|
@@ -106,6 +121,7 @@ async function loadContent(
|
|
|
106
121
|
let bytes = 0;
|
|
107
122
|
const candidates: Array<StorageFile & { hasPath: boolean; value?: Record<string, unknown> }> = [];
|
|
108
123
|
for (const file of files) {
|
|
124
|
+
if (file.deleted) continue;
|
|
109
125
|
const body = parsed.get(file.file);
|
|
110
126
|
if (body === undefined) {
|
|
111
127
|
// Not read: a single spelling whose body wasn't needed, or a file that vanished.
|
|
@@ -133,6 +149,16 @@ async function loadContent(
|
|
|
133
149
|
});
|
|
134
150
|
}
|
|
135
151
|
}
|
|
152
|
+
// An entry the overlay deletes (no spelling of it left) holds its first tombstone's version.
|
|
153
|
+
for (const group of groups.values()) {
|
|
154
|
+
if (group.some((f) => !f.deleted)) continue;
|
|
155
|
+
const tombstone = [...group].sort((a, b) => (a.file < b.file ? -1 : 1))[0]!;
|
|
156
|
+
entries.set(blockNameFromFile(tombstone.file), {
|
|
157
|
+
file: tombstone.file,
|
|
158
|
+
version: tombstone.version,
|
|
159
|
+
deleted: true,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
136
162
|
diagnostics.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : 0));
|
|
137
163
|
return { snapshot, entries, groups, diagnostics, bytes, moved };
|
|
138
164
|
}
|
|
@@ -152,7 +178,7 @@ export async function loadCurrentContent(
|
|
|
152
178
|
shortCircuit?: (snapshot: StorageSnapshot) => boolean,
|
|
153
179
|
): Promise<{ snapshot: StorageSnapshot; content: LoadedContent | null }> {
|
|
154
180
|
for (let read = 1; read <= MAX_SNAPSHOT_READS; read++) {
|
|
155
|
-
const snapshot = await storage.snapshot();
|
|
181
|
+
const snapshot = await (options.take ?? (() => storage.snapshot()))();
|
|
156
182
|
if (shortCircuit?.(snapshot)) return { snapshot, content: null };
|
|
157
183
|
const content = await loadContent(storage, snapshot, options);
|
|
158
184
|
if (!content.moved) return { snapshot, content };
|
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
StorageNotFoundError,
|
|
12
12
|
StorageUnavailableError,
|
|
13
13
|
} from "../storage.ts";
|
|
14
|
-
import type { DecoMeta } from "../types.ts";
|
|
15
14
|
import { BodyCache } from "./bodyCache.ts";
|
|
16
15
|
|
|
17
16
|
const MiB = 1024 * 1024;
|
|
@@ -51,8 +50,6 @@ const DEFAULT_RETRY_DELAY = { minMs: 50, maxMs: 200 };
|
|
|
51
50
|
|
|
52
51
|
export class Core {
|
|
53
52
|
readonly cache: BodyCache;
|
|
54
|
-
/** The last parsed schema, by version: the secret guard reads it on every write. */
|
|
55
|
-
parsedSchema: { version: string; meta: DecoMeta } | null = null;
|
|
56
53
|
/**
|
|
57
54
|
* The last `storage.changes()` answer, by changes revision (base plus
|
|
58
55
|
* content): a poll on unchanged content and base doesn't ask the storage
|
|
@@ -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
|
}
|