@decocms/blocks 8.1.2 → 8.2.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/protocol/client.d.ts +17 -4
- package/dist/protocol/client.d.ts.map +1 -1
- package/dist/protocol/client.js +46 -10
- package/dist/protocol/client.js.map +1 -1
- package/dist/protocol/conformance/cases/changes.d.ts +3 -0
- package/dist/protocol/conformance/cases/changes.d.ts.map +1 -0
- package/dist/protocol/conformance/cases/changes.js +295 -0
- package/dist/protocol/conformance/cases/changes.js.map +1 -0
- package/dist/protocol/conformance/context.d.ts +14 -0
- 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 +4 -1
- package/dist/protocol/conformance/index.js.map +1 -1
- package/dist/protocol/errors.d.ts +26 -0
- package/dist/protocol/errors.d.ts.map +1 -1
- package/dist/protocol/errors.js.map +1 -1
- package/dist/protocol/index.d.ts +4 -2
- package/dist/protocol/index.d.ts.map +1 -1
- package/dist/protocol/index.js +4 -2
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/params.d.ts +4 -2
- package/dist/protocol/params.d.ts.map +1 -1
- package/dist/protocol/params.js +11 -1
- package/dist/protocol/params.js.map +1 -1
- package/dist/protocol/server/assets.d.ts +1 -1
- package/dist/protocol/server/assets.js +1 -1
- package/dist/protocol/server/core.d.ts +12 -1
- package/dist/protocol/server/core.d.ts.map +1 -1
- package/dist/protocol/server/core.js +8 -0
- package/dist/protocol/server/core.js.map +1 -1
- package/dist/protocol/server/index.d.ts +1 -1
- package/dist/protocol/server/index.js +1 -1
- package/dist/protocol/server/methods/apply.d.ts +37 -0
- package/dist/protocol/server/methods/apply.d.ts.map +1 -1
- package/dist/protocol/server/methods/apply.js +4 -3
- package/dist/protocol/server/methods/apply.js.map +1 -1
- package/dist/protocol/server/methods/changes.d.ts +6 -0
- package/dist/protocol/server/methods/changes.d.ts.map +1 -0
- package/dist/protocol/server/methods/changes.js +381 -0
- package/dist/protocol/server/methods/changes.js.map +1 -0
- package/dist/protocol/server/rpc.d.ts.map +1 -1
- package/dist/protocol/server/rpc.js +7 -0
- package/dist/protocol/server/rpc.js.map +1 -1
- package/dist/protocol/storage/fs/git.d.ts +51 -0
- package/dist/protocol/storage/fs/git.d.ts.map +1 -0
- package/dist/protocol/storage/fs/git.js +274 -0
- package/dist/protocol/storage/fs/git.js.map +1 -0
- package/dist/protocol/storage/fs/index.d.ts +2 -0
- package/dist/protocol/storage/fs/index.d.ts.map +1 -1
- package/dist/protocol/storage/fs/index.js +34 -0
- package/dist/protocol/storage/fs/index.js.map +1 -1
- package/dist/protocol/storage/memory.d.ts +10 -0
- package/dist/protocol/storage/memory.d.ts.map +1 -1
- package/dist/protocol/storage/memory.js +45 -0
- package/dist/protocol/storage/memory.js.map +1 -1
- package/dist/protocol/storage.d.ts +49 -1
- package/dist/protocol/storage.d.ts.map +1 -1
- package/dist/protocol/storage.js.map +1 -1
- package/dist/protocol/types.d.ts +105 -4
- package/dist/protocol/types.d.ts.map +1 -1
- package/dist/protocol/types.js +16 -3
- package/dist/protocol/types.js.map +1 -1
- package/dist/v8/cli/run.d.ts.map +1 -1
- package/dist/v8/cli/run.js +2 -1
- package/dist/v8/cli/run.js.map +1 -1
- package/dist/v8/cli/serve/server.d.ts +2 -0
- package/dist/v8/cli/serve/server.d.ts.map +1 -1
- package/dist/v8/cli/serve/server.js +37 -2
- package/dist/v8/cli/serve/server.js.map +1 -1
- package/package.json +1 -1
- package/src/protocol/client.ts +81 -13
- package/src/protocol/conformance/cases/changes.ts +362 -0
- package/src/protocol/conformance/context.ts +12 -0
- package/src/protocol/conformance/index.ts +4 -1
- package/src/protocol/errors.ts +17 -0
- package/src/protocol/index.ts +4 -2
- package/src/protocol/params.ts +26 -3
- package/src/protocol/server/assets.ts +1 -1
- package/src/protocol/server/core.ts +9 -0
- package/src/protocol/server/index.ts +1 -1
- package/src/protocol/server/methods/apply.ts +8 -4
- package/src/protocol/server/methods/changes.ts +489 -0
- package/src/protocol/server/rpc.ts +7 -0
- package/src/protocol/storage/fs/git.ts +307 -0
- package/src/protocol/storage/fs/index.ts +40 -0
- package/src/protocol/storage/memory.ts +57 -0
- package/src/protocol/storage.ts +51 -1
- package/src/protocol/types.ts +106 -4
- package/src/v8/cli/run.ts +2 -1
- package/src/v8/cli/serve/server.ts +46 -2
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `changes.list`, `changes.get` and `changes.discard`: a source's content is
|
|
3
|
+
* its base plus its changes, and these three read and drop the changes.
|
|
4
|
+
*
|
|
5
|
+
* - Entries, not files: every spelling of a name (`spellingKey`) is one
|
|
6
|
+
* entry, on the base side as on the current side, and an entry is changed
|
|
7
|
+
* when any file of its spelling group differs from the base.
|
|
8
|
+
* - The storage says which files differ (`changes()`) and reads base bodies
|
|
9
|
+
* (`readBaseFiles()`); the core turns files into entries, resolving the
|
|
10
|
+
* winning spelling on each side exactly as `blocks.list` does.
|
|
11
|
+
* - `changes.discard` is a write: it restores the base files of the named
|
|
12
|
+
* entries as the storage reads them, and deletes their files the base
|
|
13
|
+
* doesn't have, in one commit guarded like `blocks.apply` (optional
|
|
14
|
+
* `ifMatch`, Conflict, retries), plus the base revision (`expectedBase`),
|
|
15
|
+
* so a base that moves meanwhile is re-read rather than half-restored.
|
|
16
|
+
*/
|
|
17
|
+
import { sha256Hex } from "../../canonical.ts";
|
|
18
|
+
import {
|
|
19
|
+
type BlockViolation,
|
|
20
|
+
conflict,
|
|
21
|
+
invalidBlock,
|
|
22
|
+
invalidParams,
|
|
23
|
+
limitExceeded,
|
|
24
|
+
readOnly,
|
|
25
|
+
unavailable,
|
|
26
|
+
} from "../../errors.ts";
|
|
27
|
+
import {
|
|
28
|
+
blockFileName,
|
|
29
|
+
blockNameFromFile,
|
|
30
|
+
checkDeletedName,
|
|
31
|
+
entryHasPath,
|
|
32
|
+
fullyDecodeFileName,
|
|
33
|
+
isBlockFileName,
|
|
34
|
+
resolveSpellings,
|
|
35
|
+
spellingKey,
|
|
36
|
+
} from "../../keys.ts";
|
|
37
|
+
import type {
|
|
38
|
+
StorageBase,
|
|
39
|
+
StorageChanges,
|
|
40
|
+
StorageFile,
|
|
41
|
+
StorageSnapshot,
|
|
42
|
+
StoredFileBody,
|
|
43
|
+
} from "../../storage.ts";
|
|
44
|
+
import type {
|
|
45
|
+
BlocksApplyResult,
|
|
46
|
+
Change,
|
|
47
|
+
ChangeDetail,
|
|
48
|
+
ChangeDiagnostic,
|
|
49
|
+
ChangesBase,
|
|
50
|
+
ChangesDiscardParams,
|
|
51
|
+
ChangesGetParams,
|
|
52
|
+
ChangesGetResult,
|
|
53
|
+
ChangesListResult,
|
|
54
|
+
ReadParams,
|
|
55
|
+
} from "../../types.ts";
|
|
56
|
+
import { type ParsedBody, parseBody } from "../bodyCache.ts";
|
|
57
|
+
import { type LoadedContent, loadCurrentContent } from "../content.ts";
|
|
58
|
+
import { type Core, MAX_BLOCK_BYTES, MAX_NAMES_PER_GET, MAX_OPS_PER_APPLY } from "../core.ts";
|
|
59
|
+
import { checkIfMatch, type EntriesBySpelling, entriesBySpelling } from "./apply.ts";
|
|
60
|
+
|
|
61
|
+
const keyOf = (file: string) => fullyDecodeFileName(file).name;
|
|
62
|
+
|
|
63
|
+
const byFile = (a: { file: string }, b: { file: string }) =>
|
|
64
|
+
a.file < b.file ? -1 : a.file > b.file ? 1 : 0;
|
|
65
|
+
|
|
66
|
+
/** One entry that differs from the base, with the files behind it. */
|
|
67
|
+
interface EntryChange {
|
|
68
|
+
name: string;
|
|
69
|
+
kind: "set" | "delete";
|
|
70
|
+
/** The current winner's version; `null` when no file of the entry exists now. */
|
|
71
|
+
version: string | null;
|
|
72
|
+
baseVersion: string | null;
|
|
73
|
+
/** The current winning file, when it was readable as a candidate. */
|
|
74
|
+
currentFile: string | null;
|
|
75
|
+
/** The base's winning file, when the base has the entry. */
|
|
76
|
+
baseFile: string | null;
|
|
77
|
+
/** The entry's files that differ from the base. */
|
|
78
|
+
changedFiles: string[];
|
|
79
|
+
/** The entry's files in the base. */
|
|
80
|
+
baseFiles: StorageFile[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface ChangesState {
|
|
84
|
+
base: StorageBase;
|
|
85
|
+
revision: string;
|
|
86
|
+
snapshot: StorageSnapshot;
|
|
87
|
+
content: LoadedContent;
|
|
88
|
+
bySpelling: EntriesBySpelling;
|
|
89
|
+
/** Changed entries, by spelling key. */
|
|
90
|
+
changes: Map<string, EntryChange>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const toChangesBase = (base: StorageBase): ChangesBase => ({
|
|
94
|
+
revision: base.revision,
|
|
95
|
+
resolvedRef: base.resolvedRef,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/** The changes' revision: it moves when the base, its branch or the content moves. */
|
|
99
|
+
function changesRevision(base: StorageBase, snapshot: StorageSnapshot): Promise<string> {
|
|
100
|
+
return sha256Hex(`${base.revision}\n${base.resolvedRef ?? ""}\n${snapshot.revision}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Parses base bodies, caching them like current ones (base versions identify content too). */
|
|
104
|
+
async function readBaseBodies(
|
|
105
|
+
core: Core,
|
|
106
|
+
base: StorageBase,
|
|
107
|
+
files: StorageFile[],
|
|
108
|
+
): Promise<Map<string, ParsedBody>> {
|
|
109
|
+
const out = new Map<string, ParsedBody>();
|
|
110
|
+
const toRead: StorageFile[] = [];
|
|
111
|
+
for (const file of files) {
|
|
112
|
+
if (file.size !== undefined && file.size > MAX_BLOCK_BYTES) {
|
|
113
|
+
out.set(file.file, {
|
|
114
|
+
ok: false,
|
|
115
|
+
kind: "too-large",
|
|
116
|
+
bytes: file.size,
|
|
117
|
+
message: `the file is over ${MAX_BLOCK_BYTES} bytes`,
|
|
118
|
+
});
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
toRead.push(file);
|
|
122
|
+
}
|
|
123
|
+
if (toRead.length === 0) return out;
|
|
124
|
+
const bodies = await core.storage.readBaseFiles(
|
|
125
|
+
base,
|
|
126
|
+
toRead.map((f) => f.file),
|
|
127
|
+
);
|
|
128
|
+
for (const file of toRead) {
|
|
129
|
+
const read = bodies[file.file];
|
|
130
|
+
if (read === undefined) continue; // The base moved: the caller notices.
|
|
131
|
+
// Parsed like a current body, which is read without its BOM.
|
|
132
|
+
out.set(file.file, parseBody(read.text.replace(/^\uFEFF/, ""), MAX_BLOCK_BYTES));
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The base's winning file of one spelling group. */
|
|
138
|
+
async function baseWinner(
|
|
139
|
+
core: Core,
|
|
140
|
+
base: StorageBase,
|
|
141
|
+
files: StorageFile[],
|
|
142
|
+
): Promise<StorageFile | null> {
|
|
143
|
+
if (files.length <= 1) return files[0] ?? null;
|
|
144
|
+
const bodies = await readBaseBodies(core, base, files);
|
|
145
|
+
const candidates = files.map((file) => {
|
|
146
|
+
const body = bodies.get(file.file);
|
|
147
|
+
return { ...file, hasPath: body?.ok === true && entryHasPath(body.value) };
|
|
148
|
+
});
|
|
149
|
+
const [resolved] = resolveSpellings(candidates).values();
|
|
150
|
+
return resolved?.winner ?? null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Asks the storage which files differ, unless the memo already knows for this revision. */
|
|
154
|
+
async function storageChanges(
|
|
155
|
+
core: Core,
|
|
156
|
+
base: StorageBase,
|
|
157
|
+
snapshot: StorageSnapshot,
|
|
158
|
+
revision: string,
|
|
159
|
+
): Promise<StorageChanges> {
|
|
160
|
+
if (core.changesMemo?.revision === revision) return core.changesMemo.changes;
|
|
161
|
+
const answer = await core.storage.changes(base, snapshot);
|
|
162
|
+
const changes: StorageChanges = {
|
|
163
|
+
files: [...new Set(answer.files.filter(isBlockFileName))].sort(),
|
|
164
|
+
baseFiles: answer.baseFiles.filter((f) => isBlockFileName(f.file)),
|
|
165
|
+
};
|
|
166
|
+
core.changesMemo = { revision, changes };
|
|
167
|
+
return changes;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Reads the base, the content and what differs, as entries. */
|
|
171
|
+
async function loadChanges(core: Core): Promise<ChangesState> {
|
|
172
|
+
const base = await core.storage.base();
|
|
173
|
+
const { snapshot, content } = await loadCurrentContent(core.storage, {
|
|
174
|
+
readAll: false,
|
|
175
|
+
cache: core.cache,
|
|
176
|
+
});
|
|
177
|
+
const revision = await changesRevision(base, snapshot);
|
|
178
|
+
const answer = await storageChanges(core, base, snapshot, revision);
|
|
179
|
+
|
|
180
|
+
const changedByKey = new Map<string, string[]>();
|
|
181
|
+
for (const file of answer.files) {
|
|
182
|
+
const key = keyOf(file);
|
|
183
|
+
const group = changedByKey.get(key);
|
|
184
|
+
if (group) group.push(file);
|
|
185
|
+
else changedByKey.set(key, [file]);
|
|
186
|
+
}
|
|
187
|
+
const baseByKey = new Map<string, StorageFile[]>();
|
|
188
|
+
for (const file of answer.baseFiles) {
|
|
189
|
+
const key = keyOf(file.file);
|
|
190
|
+
if (!changedByKey.has(key)) continue;
|
|
191
|
+
const group = baseByKey.get(key);
|
|
192
|
+
if (group) group.push(file);
|
|
193
|
+
else baseByKey.set(key, [file]);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const bySpelling = entriesBySpelling(content!);
|
|
197
|
+
const changes = new Map<string, EntryChange>();
|
|
198
|
+
for (const [key, changedFiles] of changedByKey) {
|
|
199
|
+
const baseFiles = (baseByKey.get(key) ?? []).sort(byFile);
|
|
200
|
+
const current = bySpelling.get(key);
|
|
201
|
+
const currentFiles = content!.groups.get(key) ?? [];
|
|
202
|
+
const winner = await baseWinner(core, base, baseFiles);
|
|
203
|
+
const exists = currentFiles.length > 0;
|
|
204
|
+
changes.set(key, {
|
|
205
|
+
name:
|
|
206
|
+
current?.name ??
|
|
207
|
+
(winner ? blockNameFromFile(winner.file) : blockNameFromFile(changedFiles[0]!)),
|
|
208
|
+
kind: exists ? "set" : "delete",
|
|
209
|
+
version: current?.entry.version ?? (exists ? currentFiles[0]!.version : null),
|
|
210
|
+
baseVersion: winner?.version ?? null,
|
|
211
|
+
currentFile: current?.entry.file ?? null,
|
|
212
|
+
baseFile: winner?.file ?? null,
|
|
213
|
+
changedFiles,
|
|
214
|
+
baseFiles,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
return { base, revision, snapshot, content: content!, bySpelling, changes };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export async function changesList(core: Core, params: ReadParams): Promise<ChangesListResult> {
|
|
221
|
+
const state = await loadChanges(core);
|
|
222
|
+
const base = toChangesBase(state.base);
|
|
223
|
+
if (params.ifNoneMatch !== undefined && params.ifNoneMatch === state.revision) {
|
|
224
|
+
return { notModified: true, revision: state.revision, base };
|
|
225
|
+
}
|
|
226
|
+
// Null-prototype, so an entry named like an Object.prototype key stays an entry.
|
|
227
|
+
const changes: Record<string, Change> = Object.create(null);
|
|
228
|
+
const sorted = [...state.changes.values()].sort((a, b) =>
|
|
229
|
+
a.name < b.name ? -1 : a.name > b.name ? 1 : 0,
|
|
230
|
+
);
|
|
231
|
+
for (const change of sorted) {
|
|
232
|
+
changes[change.name] = {
|
|
233
|
+
kind: change.kind,
|
|
234
|
+
version: change.version,
|
|
235
|
+
baseVersion: change.baseVersion,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
return { notModified: false, revision: state.revision, base, changes };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function checkNames(names: string[], limit: number, limitName: string, method: string) {
|
|
242
|
+
const unique = [...new Set(names)];
|
|
243
|
+
if (unique.length > limit) {
|
|
244
|
+
throw limitExceeded(`${unique.length} names in one ${method}; the limit is ${limit}`, {
|
|
245
|
+
limit: limitName,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return unique;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** How many times `changes.get` reads when files keep changing under it. */
|
|
252
|
+
const MAX_GET_READS = 3;
|
|
253
|
+
|
|
254
|
+
export async function changesGet(core: Core, params: ChangesGetParams): Promise<ChangesGetResult> {
|
|
255
|
+
const names = checkNames(params.names, MAX_NAMES_PER_GET, "maxNamesPerGet", "changes.get");
|
|
256
|
+
const empty = names.findIndex((name) => name.length === 0);
|
|
257
|
+
if (empty !== -1) throw invalidParams(`names.${empty}: the name is empty`);
|
|
258
|
+
|
|
259
|
+
for (let read = 1; read <= MAX_GET_READS; read++) {
|
|
260
|
+
const state = await loadChanges(core);
|
|
261
|
+
const plans: Array<{
|
|
262
|
+
name: string;
|
|
263
|
+
kind: ChangeDetail["kind"];
|
|
264
|
+
version: string | null;
|
|
265
|
+
baseVersion: string | null;
|
|
266
|
+
currentFile: string | null;
|
|
267
|
+
baseFile: string | null;
|
|
268
|
+
}> = [];
|
|
269
|
+
for (const name of names) {
|
|
270
|
+
const key = spellingKey(name);
|
|
271
|
+
const change = state.changes.get(key);
|
|
272
|
+
if (change) {
|
|
273
|
+
plans.push({
|
|
274
|
+
name,
|
|
275
|
+
kind: change.kind,
|
|
276
|
+
version: change.version,
|
|
277
|
+
baseVersion: change.baseVersion,
|
|
278
|
+
currentFile: change.currentFile,
|
|
279
|
+
baseFile: change.baseFile,
|
|
280
|
+
});
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const current = state.bySpelling.get(key);
|
|
284
|
+
if (!current) continue; // In neither side.
|
|
285
|
+
plans.push({
|
|
286
|
+
name,
|
|
287
|
+
kind: null,
|
|
288
|
+
version: current.entry.version,
|
|
289
|
+
baseVersion: current.entry.version,
|
|
290
|
+
currentFile: current.entry.file,
|
|
291
|
+
baseFile: null,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const currentFiles = [...new Set(plans.flatMap((p) => (p.currentFile ? [p.currentFile] : [])))];
|
|
296
|
+
const current = await readCurrentBodies(core, state.snapshot, currentFiles);
|
|
297
|
+
if (current === null) continue; // A file changed while being read: read again.
|
|
298
|
+
const baseFiles = plans.flatMap((p) => {
|
|
299
|
+
if (!p.baseFile) return [];
|
|
300
|
+
const change = state.changes.get(spellingKey(p.name));
|
|
301
|
+
return change?.baseFiles.filter((f) => f.file === p.baseFile) ?? [];
|
|
302
|
+
});
|
|
303
|
+
const baseBodies = await readBaseBodies(core, state.base, baseFiles);
|
|
304
|
+
|
|
305
|
+
const entries: Record<string, ChangeDetail> = Object.create(null);
|
|
306
|
+
const diagnostics: ChangeDiagnostic[] = [];
|
|
307
|
+
const side = (
|
|
308
|
+
body: ParsedBody | undefined,
|
|
309
|
+
file: string | null,
|
|
310
|
+
which: ChangeDiagnostic["side"],
|
|
311
|
+
) => {
|
|
312
|
+
if (file === null || body === undefined) return null;
|
|
313
|
+
if (body.ok) return body.value;
|
|
314
|
+
diagnostics.push({ file, kind: body.kind, message: body.message, side: which });
|
|
315
|
+
return null;
|
|
316
|
+
};
|
|
317
|
+
for (const plan of plans) {
|
|
318
|
+
const after = side(
|
|
319
|
+
plan.currentFile ? current.get(plan.currentFile) : undefined,
|
|
320
|
+
plan.currentFile,
|
|
321
|
+
"after",
|
|
322
|
+
);
|
|
323
|
+
// An unchanged entry's base is its current body: no base read.
|
|
324
|
+
const before =
|
|
325
|
+
plan.kind === null
|
|
326
|
+
? after
|
|
327
|
+
: side(
|
|
328
|
+
plan.baseFile ? baseBodies.get(plan.baseFile) : undefined,
|
|
329
|
+
plan.baseFile,
|
|
330
|
+
"before",
|
|
331
|
+
);
|
|
332
|
+
entries[plan.name] = {
|
|
333
|
+
kind: plan.kind,
|
|
334
|
+
before,
|
|
335
|
+
after,
|
|
336
|
+
version: plan.version,
|
|
337
|
+
baseVersion: plan.baseVersion,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
diagnostics.sort((a, b) => byFile(a, b) || (a.side < b.side ? -1 : a.side > b.side ? 1 : 0));
|
|
341
|
+
return { base: toChangesBase(state.base), entries, diagnostics };
|
|
342
|
+
}
|
|
343
|
+
throw unavailable("saved blocks kept changing while being read; retry shortly", 250);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** Reads current bodies; `null` when one changed since the snapshot. */
|
|
347
|
+
async function readCurrentBodies(
|
|
348
|
+
core: Core,
|
|
349
|
+
snapshot: StorageSnapshot,
|
|
350
|
+
files: string[],
|
|
351
|
+
): Promise<Map<string, ParsedBody> | null> {
|
|
352
|
+
const out = new Map<string, ParsedBody>();
|
|
353
|
+
const versionOf = new Map(snapshot.files.map((f) => [f.file, f]));
|
|
354
|
+
const toRead: string[] = [];
|
|
355
|
+
for (const file of files) {
|
|
356
|
+
const info = versionOf.get(file);
|
|
357
|
+
if (!info) return null;
|
|
358
|
+
if (info.size !== undefined && info.size > MAX_BLOCK_BYTES) {
|
|
359
|
+
out.set(file, {
|
|
360
|
+
ok: false,
|
|
361
|
+
kind: "too-large",
|
|
362
|
+
bytes: info.size,
|
|
363
|
+
message: `the file is over ${MAX_BLOCK_BYTES} bytes`,
|
|
364
|
+
});
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
const hit = core.cache.get(file, info.version);
|
|
368
|
+
if (hit) out.set(file, hit);
|
|
369
|
+
else toRead.push(file);
|
|
370
|
+
}
|
|
371
|
+
if (toRead.length === 0) return out;
|
|
372
|
+
const bodies: Record<string, StoredFileBody> = await core.storage.readFiles(snapshot, toRead);
|
|
373
|
+
for (const file of toRead) {
|
|
374
|
+
const read = bodies[file];
|
|
375
|
+
if (read === undefined) return null;
|
|
376
|
+
const body = parseBody(read.text, MAX_BLOCK_BYTES);
|
|
377
|
+
core.cache.set(file, read.version, body);
|
|
378
|
+
if (read.version !== versionOf.get(file)!.version) return null;
|
|
379
|
+
out.set(file, body);
|
|
380
|
+
}
|
|
381
|
+
return out;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The files a guarded entry has now (every spelling), as commit expectations. */
|
|
385
|
+
function guardExpectations(
|
|
386
|
+
state: ChangesState,
|
|
387
|
+
ifMatch: Array<[string, string | null]>,
|
|
388
|
+
): Record<string, string | null> {
|
|
389
|
+
const versionOf = new Map(
|
|
390
|
+
state.snapshot.files.filter((f) => isBlockFileName(f.file)).map((f) => [f.file, f.version]),
|
|
391
|
+
);
|
|
392
|
+
const expected: Record<string, string | null> = Object.create(null);
|
|
393
|
+
for (const [name] of ifMatch) {
|
|
394
|
+
const group = (state.content.groups.get(spellingKey(name)) ?? []).map((f) => f.file);
|
|
395
|
+
for (const file of [blockFileName(name), ...group]) {
|
|
396
|
+
if (isBlockFileName(file)) expected[file] = versionOf.get(file) ?? null;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return expected;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export async function changesDiscard(
|
|
403
|
+
core: Core,
|
|
404
|
+
params: ChangesDiscardParams,
|
|
405
|
+
): Promise<BlocksApplyResult> {
|
|
406
|
+
const description = await core.description();
|
|
407
|
+
if (description.readOnly) throw readOnly();
|
|
408
|
+
const names = checkNames(params.names, MAX_OPS_PER_APPLY, "maxOpsPerDiscard", "changes.discard");
|
|
409
|
+
const violations: BlockViolation[] = [];
|
|
410
|
+
for (const name of names) {
|
|
411
|
+
for (const v of checkDeletedName(name))
|
|
412
|
+
violations.push({ name, rule: v.reason, message: v.message });
|
|
413
|
+
}
|
|
414
|
+
if (violations.length > 0) throw invalidBlock(violations);
|
|
415
|
+
const ifMatch = Object.entries(params.ifMatch ?? {});
|
|
416
|
+
|
|
417
|
+
for (let attempt = 1; attempt <= core.maxCommitAttempts; attempt++) {
|
|
418
|
+
const state = await loadChanges(core);
|
|
419
|
+
const mismatches = checkIfMatch(state.bySpelling, { ifMatch });
|
|
420
|
+
if (mismatches) throw conflict({ entries: mismatches });
|
|
421
|
+
|
|
422
|
+
const restore = new Map<string, StorageFile>();
|
|
423
|
+
const deletes: string[] = [];
|
|
424
|
+
const discarded: EntryChange[] = [];
|
|
425
|
+
for (const name of names) {
|
|
426
|
+
const change = state.changes.get(spellingKey(name));
|
|
427
|
+
if (!change || discarded.includes(change)) continue;
|
|
428
|
+
discarded.push(change);
|
|
429
|
+
const inBase = new Map(change.baseFiles.map((f) => [f.file, f]));
|
|
430
|
+
for (const file of change.changedFiles) {
|
|
431
|
+
const baseFile = inBase.get(file);
|
|
432
|
+
if (baseFile) restore.set(file, baseFile);
|
|
433
|
+
else deletes.push(file);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const currentVersions = () => {
|
|
438
|
+
const versions: Record<string, string | null> = Object.create(null);
|
|
439
|
+
for (const name of names) {
|
|
440
|
+
versions[name] = state.bySpelling.get(spellingKey(name))?.entry.version ?? null;
|
|
441
|
+
}
|
|
442
|
+
return versions;
|
|
443
|
+
};
|
|
444
|
+
if (restore.size === 0 && deletes.length === 0) {
|
|
445
|
+
return { revision: state.snapshot.revision, versions: currentVersions() };
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const bodies = await core.storage.readBaseFiles(state.base, [...restore.keys()]);
|
|
449
|
+
const put: Record<string, string> = Object.create(null);
|
|
450
|
+
let baseMoved = false;
|
|
451
|
+
for (const file of restore.keys()) {
|
|
452
|
+
const body = bodies[file];
|
|
453
|
+
if (body === undefined) baseMoved = true;
|
|
454
|
+
else put[file] = body.text;
|
|
455
|
+
}
|
|
456
|
+
if (!baseMoved) {
|
|
457
|
+
const result = await core.storage.commit({
|
|
458
|
+
base: state.snapshot,
|
|
459
|
+
put,
|
|
460
|
+
delete: deletes,
|
|
461
|
+
expected: guardExpectations(state, ifMatch),
|
|
462
|
+
expectedBase: state.base.revision,
|
|
463
|
+
reason: "discard",
|
|
464
|
+
});
|
|
465
|
+
if (result.status === "committed") {
|
|
466
|
+
const versions = currentVersions();
|
|
467
|
+
const snapshotVersion = new Map(state.snapshot.files.map((f) => [f.file, f.version]));
|
|
468
|
+
for (const name of names) {
|
|
469
|
+
const change = state.changes.get(spellingKey(name));
|
|
470
|
+
if (!change) continue;
|
|
471
|
+
const file = change.baseFile;
|
|
472
|
+
versions[name] =
|
|
473
|
+
file === null ? null : (result.versions[file] ?? snapshotVersion.get(file) ?? null);
|
|
474
|
+
}
|
|
475
|
+
const touched = new Set(discarded.map((c) => spellingKey(c.name)));
|
|
476
|
+
for (const file of deletes) {
|
|
477
|
+
const name = blockNameFromFile(file);
|
|
478
|
+
if (!(name in versions) && touched.has(keyOf(file))) versions[name] = null;
|
|
479
|
+
}
|
|
480
|
+
return { revision: result.revision, versions };
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (attempt < core.maxCommitAttempts) await core.backoff(attempt);
|
|
484
|
+
}
|
|
485
|
+
throw unavailable(
|
|
486
|
+
`storage kept changing; gave up after ${core.maxCommitAttempts} commit attempts`,
|
|
487
|
+
250,
|
|
488
|
+
);
|
|
489
|
+
}
|
|
@@ -16,6 +16,7 @@ import { validateParams } from "../params.ts";
|
|
|
16
16
|
import { MAX_BATCH_CALLS, METHOD_NAMES, type MethodName, type RpcId } from "../types.ts";
|
|
17
17
|
import { type Core, toProtocolError } from "./core.ts";
|
|
18
18
|
import { blocksApply } from "./methods/apply.ts";
|
|
19
|
+
import { changesDiscard, changesGet, changesList } from "./methods/changes.ts";
|
|
19
20
|
import { blocksList, describe, schemaGet } from "./methods/read.ts";
|
|
20
21
|
|
|
21
22
|
type Envelope = { id: RpcId; method: MethodName; params: unknown };
|
|
@@ -57,6 +58,12 @@ async function run(core: Core, envelope: Envelope): Promise<unknown> {
|
|
|
57
58
|
return blocksList(core, validateParams("blocks.list", envelope.params));
|
|
58
59
|
case "blocks.apply":
|
|
59
60
|
return blocksApply(core, validateParams("blocks.apply", envelope.params));
|
|
61
|
+
case "changes.list":
|
|
62
|
+
return changesList(core, validateParams("changes.list", envelope.params));
|
|
63
|
+
case "changes.get":
|
|
64
|
+
return changesGet(core, validateParams("changes.get", envelope.params));
|
|
65
|
+
case "changes.discard":
|
|
66
|
+
return changesDiscard(core, validateParams("changes.discard", envelope.params));
|
|
60
67
|
}
|
|
61
68
|
}
|
|
62
69
|
|