@agentshouse/mdruntime 0.1.0-cff8ba8fa2542 → 0.1.0-cffcba9cd9c2f
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/README.md +24 -11
- package/dist/acceptance.d.ts +3 -0
- package/dist/acceptance.d.ts.map +1 -0
- package/dist/acceptance.js +309 -0
- package/dist/actions.d.ts +20 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +251 -0
- package/dist/adapter.d.ts +195 -88
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +7 -10
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +40 -28
- package/dist/attempt.d.ts +16 -0
- package/dist/attempt.d.ts.map +1 -0
- package/dist/attempt.js +70 -0
- package/dist/binary.d.ts +18 -0
- package/dist/binary.d.ts.map +1 -0
- package/dist/binary.js +8 -0
- package/dist/bootstrap.d.ts +37 -5
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +324 -51
- package/dist/cell.d.ts +6 -0
- package/dist/cell.d.ts.map +1 -0
- package/dist/cell.js +9 -0
- package/dist/chain.d.ts +12 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +64 -0
- package/dist/change-set.d.ts +13 -14
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +17 -56
- package/dist/change.d.ts +46 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +36 -0
- package/dist/chat-types.d.ts +5 -0
- package/dist/chat-types.d.ts.map +1 -0
- package/dist/chat-types.js +13 -0
- package/dist/client.d.ts +56 -18
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +164 -23
- package/dist/collection.d.ts +31 -14
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +61 -55
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +711 -0
- package/dist/connection.d.ts +8 -103
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +103 -2076
- package/dist/context.d.ts +2 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +7 -0
- package/dist/conversation-live.d.ts +135 -0
- package/dist/conversation-live.d.ts.map +1 -0
- package/dist/conversation-live.js +247 -0
- package/dist/conversation-row.d.ts +33 -0
- package/dist/conversation-row.d.ts.map +1 -0
- package/dist/conversation-row.js +53 -0
- package/dist/conversation.d.ts +67 -44
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +1010 -549
- package/dist/declaration.d.ts +10 -0
- package/dist/declaration.d.ts.map +1 -0
- package/dist/declaration.js +24 -0
- package/dist/digest.d.ts +7 -0
- package/dist/digest.d.ts.map +1 -0
- package/dist/digest.js +113 -0
- package/dist/discovery.d.ts +10 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +177 -0
- package/dist/durability.d.ts +19 -1
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +5 -0
- package/dist/entry.d.ts +20 -0
- package/dist/entry.d.ts.map +1 -0
- package/dist/entry.js +161 -0
- package/dist/expectation.d.ts +23 -0
- package/dist/expectation.d.ts.map +1 -0
- package/dist/expectation.js +92 -0
- package/dist/failure.d.ts +14 -0
- package/dist/failure.d.ts.map +1 -0
- package/dist/failure.js +22 -0
- package/dist/figures.d.ts +49 -0
- package/dist/figures.d.ts.map +1 -0
- package/dist/figures.js +197 -0
- package/dist/file.d.ts +2 -19
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +7 -122
- package/dist/holdings.d.ts +29 -0
- package/dist/holdings.d.ts.map +1 -0
- package/dist/holdings.js +95 -0
- package/dist/host.d.ts +28 -3
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +149 -26
- package/dist/identity.d.ts +3 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +13 -1
- package/dist/index.d.ts +39 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -0
- package/dist/install.d.ts +55 -0
- package/dist/install.d.ts.map +1 -0
- package/dist/install.js +543 -0
- package/dist/job.d.ts +5 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +7 -0
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +271 -0
- package/dist/listing.d.ts +52 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +168 -0
- package/dist/live.d.ts +23 -0
- package/dist/live.d.ts.map +1 -0
- package/dist/live.js +165 -0
- package/dist/locks.d.ts +9 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +62 -0
- package/dist/log.d.ts +1 -0
- package/dist/log.d.ts.map +1 -1
- package/dist/lost.d.ts +5 -0
- package/dist/lost.d.ts.map +1 -0
- package/dist/lost.js +61 -0
- package/dist/media.d.ts +8 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +218 -0
- package/dist/memory.d.ts +25 -8
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +296 -93
- package/dist/messages.d.ts +49 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +148 -0
- package/dist/model.d.ts +16 -9
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +9 -15
- package/dist/models.d.ts +10 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +170 -0
- package/dist/name.d.ts +4 -0
- package/dist/name.d.ts.map +1 -0
- package/dist/name.js +122 -0
- package/dist/observation.d.ts +22 -4
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +127 -26
- package/dist/open.d.ts +2 -0
- package/dist/open.d.ts.map +1 -0
- package/dist/open.js +1 -0
- package/dist/opened.d.ts +22 -0
- package/dist/opened.d.ts.map +1 -0
- package/dist/opened.js +107 -0
- package/dist/opening.d.ts +4 -0
- package/dist/opening.d.ts.map +1 -0
- package/dist/opening.js +59 -0
- package/dist/outcome.d.ts +75 -5
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +84 -1
- package/dist/overlay.d.ts +17 -5
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +168 -190
- package/dist/parts.d.ts +11 -0
- package/dist/parts.d.ts.map +1 -0
- package/dist/parts.js +21 -0
- package/dist/paths.d.ts +2 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +26 -16
- package/dist/preparation.d.ts +20 -0
- package/dist/preparation.d.ts.map +1 -0
- package/dist/preparation.js +233 -0
- package/dist/queries.d.ts +16 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +272 -0
- package/dist/query.d.ts +51 -181
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +3 -431
- package/dist/questions.d.ts +34 -0
- package/dist/questions.d.ts.map +1 -0
- package/dist/questions.js +122 -0
- package/dist/queue.d.ts +32 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +402 -0
- package/dist/reach.d.ts +21 -5
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +306 -96
- package/dist/react.d.ts +43 -19
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +22 -21
- package/dist/read.d.ts +7 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/read.js +17 -0
- package/dist/readiness.d.ts +49 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +27 -0
- package/dist/reads.d.ts +37 -0
- package/dist/reads.d.ts.map +1 -0
- package/dist/reads.js +174 -0
- package/dist/recovery.d.ts +26 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +232 -0
- package/dist/refresh.d.ts +20 -7
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +155 -59
- package/dist/refusal.d.ts +5 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +57 -0
- package/dist/replica.d.ts +53 -33
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +361 -164
- package/dist/room-draft.d.ts +59 -12
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +299 -90
- package/dist/room-drafts.d.ts +24 -0
- package/dist/room-drafts.d.ts.map +1 -0
- package/dist/room-drafts.js +217 -0
- package/dist/rows.d.ts +32 -34
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +67 -87
- package/dist/runtime.d.ts +32 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts +74 -5
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +914 -188
- package/dist/saves.d.ts +32 -0
- package/dist/saves.d.ts.map +1 -0
- package/dist/saves.js +679 -0
- package/dist/search.d.ts +15 -54
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +10 -121
- package/dist/seed.d.ts +25 -0
- package/dist/seed.d.ts.map +1 -0
- package/dist/seed.js +145 -0
- package/dist/shared.d.ts +10 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +339 -0
- package/dist/shown.d.ts +32 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +299 -0
- package/dist/socket/admission.d.ts +7 -0
- package/dist/socket/admission.d.ts.map +1 -0
- package/dist/socket/admission.js +39 -0
- package/dist/socket/answers.d.ts +6 -0
- package/dist/socket/answers.d.ts.map +1 -0
- package/dist/socket/answers.js +20 -0
- package/dist/socket/lanes.d.ts +20 -0
- package/dist/socket/lanes.d.ts.map +1 -0
- package/dist/socket/lanes.js +77 -0
- package/dist/socket/reads.d.ts +10 -0
- package/dist/socket/reads.d.ts.map +1 -0
- package/dist/socket/reads.js +31 -0
- package/dist/socket/rooms.d.ts +41 -0
- package/dist/socket/rooms.d.ts.map +1 -0
- package/dist/socket/rooms.js +90 -0
- package/dist/socket/session.d.ts +38 -0
- package/dist/socket/session.d.ts.map +1 -0
- package/dist/socket/session.js +23 -0
- package/dist/socket.d.ts +69 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +175 -0
- package/dist/source.d.ts +61 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +82 -0
- package/dist/status.d.ts +7 -4
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +3 -15
- package/dist/store/fences.d.ts +16 -0
- package/dist/store/fences.d.ts.map +1 -0
- package/dist/store/fences.js +26 -0
- package/dist/store/installs.d.ts +30 -0
- package/dist/store/installs.d.ts.map +1 -0
- package/dist/store/installs.js +34 -0
- package/dist/store/readiness.d.ts +10 -0
- package/dist/store/readiness.d.ts.map +1 -0
- package/dist/store/readiness.js +52 -0
- package/dist/store/records.d.ts +210 -0
- package/dist/store/records.d.ts.map +1 -0
- package/dist/store/records.js +428 -0
- package/dist/store/room-held.d.ts +8 -0
- package/dist/store/room-held.d.ts.map +1 -0
- package/dist/store/room-held.js +17 -0
- package/dist/store/space.d.ts +11 -0
- package/dist/store/space.d.ts.map +1 -0
- package/dist/store/space.js +41 -0
- package/dist/store/watchers.d.ts +17 -0
- package/dist/store/watchers.d.ts.map +1 -0
- package/dist/store/watchers.js +56 -0
- package/dist/store/writes.d.ts +24 -0
- package/dist/store/writes.d.ts.map +1 -0
- package/dist/store/writes.js +125 -0
- package/dist/store.d.ts +106 -92
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +142 -385
- package/dist/stored.d.ts +156 -0
- package/dist/stored.d.ts.map +1 -0
- package/dist/stored.js +649 -0
- package/dist/surface.d.ts +88 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +224 -0
- package/dist/sync.d.ts +19 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +530 -0
- package/dist/tables.d.ts +412 -0
- package/dist/tables.d.ts.map +1 -0
- package/dist/tables.js +737 -0
- package/dist/tallies.d.ts +18 -0
- package/dist/tallies.d.ts.map +1 -0
- package/dist/tallies.js +129 -0
- package/dist/transfer.d.ts +0 -5
- package/dist/transfer.d.ts.map +1 -1
- package/dist/turn.d.ts +9 -0
- package/dist/turn.d.ts.map +1 -0
- package/dist/turn.js +75 -0
- package/dist/validation.d.ts +29 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +104 -0
- package/dist/windows.d.ts +11 -0
- package/dist/windows.d.ts.map +1 -0
- package/dist/windows.js +16 -0
- package/dist/wire.d.ts +189 -37
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +77 -33
- package/package.json +27 -15
- package/AGENTS.md +0 -231
- package/dist/attachment.d.ts +0 -3
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -8
- package/dist/read-side.d.ts +0 -18
- package/dist/read-side.d.ts.map +0 -1
- package/dist/read-side.js +0 -377
package/dist/saves.js
ADDED
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
import { parseFrontmatter } from '@agentshouse/mdmodel';
|
|
2
|
+
import { binaryRecord } from './binary.js';
|
|
3
|
+
import { changeSet, mergeChangeSets } from './change-set.js';
|
|
4
|
+
import { queuedFile } from './change.js';
|
|
5
|
+
import { available, composedOf } from './collection.js';
|
|
6
|
+
import { runtimeError } from './failure.js';
|
|
7
|
+
import { attachedFile } from './file.js';
|
|
8
|
+
import { roomDraftRevision } from './identity.js';
|
|
9
|
+
import { compositionFor } from './listing.js';
|
|
10
|
+
import { scoped } from './models.js';
|
|
11
|
+
import { absentBinary, attachAnswer, binaryAnswer, binaryLinkAnswer, failure, OFFLINE, UNCERTAIN, } from './outcome.js';
|
|
12
|
+
import { sameBinary } from './overlay.js';
|
|
13
|
+
import { ancestors } from './paths.js';
|
|
14
|
+
import { connectionNow } from './preparation.js';
|
|
15
|
+
import { overlayDiff, replayed, sliceAt, withdrawable } from './queue.js';
|
|
16
|
+
import { entryAt, replaying, slicing, } from './replica.js';
|
|
17
|
+
import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, moveTarget, readSavedParts, withdrawnDrafts, } from './room-draft.js';
|
|
18
|
+
import { refuse } from './refusal.js';
|
|
19
|
+
import { visibleOf } from './room-drafts.js';
|
|
20
|
+
import { buildBatch, buildMove, joined, limiting, queuedDocuments, requeuing, } from './save.js';
|
|
21
|
+
import { pendingShown } from './shown.js';
|
|
22
|
+
import { paced, ROW_WEIGHT, stepped } from './turn.js';
|
|
23
|
+
import { validatedNeighbourhood } from './validation.js';
|
|
24
|
+
async function savedRoom(store, locator) {
|
|
25
|
+
const local = await store.open();
|
|
26
|
+
const replica = await store.replica(locator);
|
|
27
|
+
if (replica === undefined || store.fences.has(locator)) {
|
|
28
|
+
throw runtimeError('unknown-room', `Unknown Room ${locator}`);
|
|
29
|
+
}
|
|
30
|
+
return { local, replica };
|
|
31
|
+
}
|
|
32
|
+
export async function saveRoomDrafts(runtime, locator, named, writtenBy) {
|
|
33
|
+
const { store } = runtime;
|
|
34
|
+
const target = await savedRoom(store, locator);
|
|
35
|
+
const drafts = new Map((await store.drafts(locator, named.map(({ path }) => path))).map((record) => [record.path, record]));
|
|
36
|
+
const saved = [];
|
|
37
|
+
for (const { path, revision } of named) {
|
|
38
|
+
const record = drafts.get(path);
|
|
39
|
+
if (record === undefined || record.revision !== revision) {
|
|
40
|
+
return { outcome: 'refused', code: 'stale', retryable: false, file: path };
|
|
41
|
+
}
|
|
42
|
+
if (!saved.some((draft) => draft.path === path))
|
|
43
|
+
saved.push({ path, parts: record.parts });
|
|
44
|
+
}
|
|
45
|
+
return committed(runtime, locator, target, saved, new Set(saved.map(({ path }) => path)), writtenBy);
|
|
46
|
+
}
|
|
47
|
+
export async function saveParts(runtime, locator, named, writtenBy) {
|
|
48
|
+
const { store } = runtime;
|
|
49
|
+
const read = readSavedParts(named);
|
|
50
|
+
const target = await savedRoom(store, locator);
|
|
51
|
+
const saved = [];
|
|
52
|
+
const files = [];
|
|
53
|
+
const filed = read.filter(({ parts }) => parts.file !== undefined).map(({ path }) => path);
|
|
54
|
+
const visible = filed.length === 0 ? undefined : await visibleOf(store, locator, filed);
|
|
55
|
+
for (const { path, parts } of read) {
|
|
56
|
+
const file = parts.file === undefined
|
|
57
|
+
? undefined
|
|
58
|
+
: await heldFile(parts.file.blob, parts.file.base === undefined ? binaryBase(visible, path) : parts.file.base);
|
|
59
|
+
if (parts.file !== undefined && file !== undefined) {
|
|
60
|
+
files.push({ key: file.localId, blob: parts.file.blob });
|
|
61
|
+
}
|
|
62
|
+
saved.push({ path, parts: held(undefined, parts, file) });
|
|
63
|
+
}
|
|
64
|
+
return committed(runtime, locator, target, saved, new Set(), writtenBy, files);
|
|
65
|
+
}
|
|
66
|
+
async function carriedDrafts(store, locator, saved, consumed) {
|
|
67
|
+
if (!saved.some(({ parts }) => parts.move !== undefined))
|
|
68
|
+
return new Map();
|
|
69
|
+
const holding = new Map((await store.drafts(locator)).map((record) => [record.path, record]));
|
|
70
|
+
return new Map(saved.flatMap(({ path, parts }) => {
|
|
71
|
+
const record = holding.get(path);
|
|
72
|
+
if (parts.move === undefined || record === undefined || consumed.has(path))
|
|
73
|
+
return [];
|
|
74
|
+
const to = moveTarget(parts.move);
|
|
75
|
+
if (to === path || holding.has(to))
|
|
76
|
+
return [];
|
|
77
|
+
return [[path, { ...record, path: to, revision: roomDraftRevision() }]];
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
async function savingIn(store, replica, changes, extra) {
|
|
81
|
+
const record = replica.record;
|
|
82
|
+
const local = await store.open();
|
|
83
|
+
const paths = [
|
|
84
|
+
...extra,
|
|
85
|
+
...changes.flatMap((change) => change.kind === 'move' ? [change.path, change.to] : [change.path]),
|
|
86
|
+
];
|
|
87
|
+
const moved = changes.flatMap((change) => (change.kind === 'move' ? [change.path] : []));
|
|
88
|
+
const removed = changes.flatMap((change) => (change.kind === 'remove' ? [change.path] : []));
|
|
89
|
+
const referring = removed.length === 0
|
|
90
|
+
? []
|
|
91
|
+
: (await local.referrers(record.key, 'confirmed', removed)).map(({ path }) => path);
|
|
92
|
+
const confirmedLayer = await sliceAt(store, record, 'confirmed', replica.queue, [...paths, ...paths.flatMap(ancestors), ...referring], moved);
|
|
93
|
+
const { visible } = await replayed(store, record, confirmedLayer, replica.queue);
|
|
94
|
+
const composition = store.composed(record);
|
|
95
|
+
return {
|
|
96
|
+
record,
|
|
97
|
+
confirmed: confirmedLayer,
|
|
98
|
+
visible,
|
|
99
|
+
folders: new Set(await local.folders(record.key, paths)),
|
|
100
|
+
*visibleOver(queue) {
|
|
101
|
+
return (yield* replaying(confirmedLayer, queue, record.locator, composition)).visible;
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async function unmodeledIn(runtime, record) {
|
|
106
|
+
const composition = compositionFor(runtime, record);
|
|
107
|
+
if (available(composition))
|
|
108
|
+
return false;
|
|
109
|
+
const local = await runtime.store.open();
|
|
110
|
+
const roots = record.declaration.typedSelection?.roots ?? [];
|
|
111
|
+
return local.typedWithin(record.key, 'visible', roots, false);
|
|
112
|
+
}
|
|
113
|
+
function textDiffers(left, right) {
|
|
114
|
+
return ((left?.content ?? undefined) !== (right?.content ?? undefined) ||
|
|
115
|
+
!sameBinary(left?.binary ?? undefined, right?.binary ?? undefined));
|
|
116
|
+
}
|
|
117
|
+
function textOf(entry) {
|
|
118
|
+
if (entry === null)
|
|
119
|
+
return undefined;
|
|
120
|
+
return entry.binary !== null ? null : entry.content;
|
|
121
|
+
}
|
|
122
|
+
function lookupOf(local, key, view) {
|
|
123
|
+
return async (paths) => {
|
|
124
|
+
if (paths.length === 0)
|
|
125
|
+
return new Map();
|
|
126
|
+
return new Map((await local.typed(key, view, paths)).map(({ path, type, binary }) => [
|
|
127
|
+
path,
|
|
128
|
+
{ type: type ?? undefined, binary },
|
|
129
|
+
]));
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function declaredOf(entry) {
|
|
133
|
+
if (entry.binary !== null)
|
|
134
|
+
return { type: undefined, binary: true };
|
|
135
|
+
const read = parseFrontmatter(entry.content ?? '');
|
|
136
|
+
return { type: read.ok ? (read.frontmatter.type ?? 'any') : undefined, binary: false };
|
|
137
|
+
}
|
|
138
|
+
function overriddenLookup(lookup, overrides) {
|
|
139
|
+
return async (paths) => {
|
|
140
|
+
const known = new Map(await lookup(paths.filter((path) => !overrides.has(path))));
|
|
141
|
+
for (const path of paths) {
|
|
142
|
+
const entry = overrides.get(path);
|
|
143
|
+
if (entry === undefined)
|
|
144
|
+
continue;
|
|
145
|
+
if (entry === null)
|
|
146
|
+
known.delete(path);
|
|
147
|
+
else
|
|
148
|
+
known.set(path, declaredOf(entry));
|
|
149
|
+
}
|
|
150
|
+
return known;
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function validatorOf(runtime, record, confirmedLayer) {
|
|
154
|
+
return async (resulting, touched) => {
|
|
155
|
+
const local = await runtime.store.open();
|
|
156
|
+
const changes = [];
|
|
157
|
+
const overrides = new Map();
|
|
158
|
+
const pace = paced();
|
|
159
|
+
for (const path of new Set([...confirmedLayer.held, ...touched])) {
|
|
160
|
+
await pace(ROW_WEIGHT);
|
|
161
|
+
const was = entryAt(confirmedLayer, path);
|
|
162
|
+
const now = entryAt(resulting, path);
|
|
163
|
+
if (!textDiffers(was, now))
|
|
164
|
+
continue;
|
|
165
|
+
overrides.set(path, now);
|
|
166
|
+
if (textOf(was) !== textOf(now))
|
|
167
|
+
changes.push({ path, was: textOf(was), now: textOf(now) });
|
|
168
|
+
}
|
|
169
|
+
return validatedNeighbourhood(changes, compositionFor(runtime, record), {
|
|
170
|
+
lookup: overriddenLookup(lookupOf(local, record.key, 'confirmed'), overrides),
|
|
171
|
+
texts: textsOf(local, record.key, 'confirmed'),
|
|
172
|
+
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function textsOf(local, key, view) {
|
|
177
|
+
return async (paths) => new Map((await local.entries(key, view, paths)).map(({ path, content, binary }) => [
|
|
178
|
+
path,
|
|
179
|
+
binary !== null ? null : content,
|
|
180
|
+
]));
|
|
181
|
+
}
|
|
182
|
+
function referrersOf(local, key, view) {
|
|
183
|
+
return async (targets) => [
|
|
184
|
+
...new Set((await local.referrers(key, view, targets)).map(({ path }) => path)),
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
async function sideOf(runtime, locator, replica, saving) {
|
|
188
|
+
const record = replica.record;
|
|
189
|
+
return {
|
|
190
|
+
room: locator,
|
|
191
|
+
handle: record.declaration.handle,
|
|
192
|
+
baseRoomVersion: record.roomVersion,
|
|
193
|
+
composition: compositionFor(runtime, record),
|
|
194
|
+
rule: runtime.writeRule(record.declaration),
|
|
195
|
+
visible: saving.visible,
|
|
196
|
+
folders: saving.folders,
|
|
197
|
+
queued: queuedDocuments(replica.queue),
|
|
198
|
+
unmodeled: await unmodeledIn(runtime, record),
|
|
199
|
+
validate: validatorOf(runtime, record, saving.confirmed),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function movedInto(locator, saved) {
|
|
203
|
+
const into = saved.find(({ parts }) => parts.move?.room !== undefined && parts.move.room !== locator);
|
|
204
|
+
return into?.parts.move?.room ?? null;
|
|
205
|
+
}
|
|
206
|
+
async function saveCommit(store, local, files, rooms) {
|
|
207
|
+
const commits = [];
|
|
208
|
+
for (const { locator, saving, before, queue } of rooms) {
|
|
209
|
+
const replayedLayer = await replayed(store, saving.record, saving.confirmed, queue);
|
|
210
|
+
const diff = await overlayDiff(saving.record, before, replayedLayer.visible, replayedLayer.touched);
|
|
211
|
+
commits.push({
|
|
212
|
+
locator,
|
|
213
|
+
key: saving.record.key,
|
|
214
|
+
roots: saving.record.declaration.typedSelection?.roots ?? [],
|
|
215
|
+
composed: composedOf(store.composed(saving.record)),
|
|
216
|
+
overlay: diff.overlay,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
return await local.commit({
|
|
221
|
+
rooms: commits,
|
|
222
|
+
writes: rooms.map(({ write }) => write),
|
|
223
|
+
files,
|
|
224
|
+
whole: true,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async function committedMove(runtime, locator, destination, { local, replica }, saved, consumed, writtenBy, files) {
|
|
232
|
+
const { store, localSaves } = runtime;
|
|
233
|
+
const mixed = saved.find(({ parts }) => Object.keys(parts).length !== 1 || parts.move?.room !== destination);
|
|
234
|
+
if (mixed !== undefined) {
|
|
235
|
+
return { outcome: 'refused', code: 'mixed-room-move', retryable: false, file: mixed.path };
|
|
236
|
+
}
|
|
237
|
+
const moves = saved.map(({ path, parts }) => ({ path, to: moveTarget(parts.move) }));
|
|
238
|
+
const sourceSaving = await savingIn(store, replica, moves.map(({ path }) => ({ kind: 'remove', path })), saved.map(({ path }) => path));
|
|
239
|
+
const changed = saved.flatMap(({ path, parts }) => changedParts(path, parts, sourceSaving.visible));
|
|
240
|
+
if (changed.length > 0)
|
|
241
|
+
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
242
|
+
const target = await store.replica(destination);
|
|
243
|
+
if (target === undefined || store.fences.has(destination)) {
|
|
244
|
+
throw runtimeError('unknown-room', `Unknown Room ${destination}`);
|
|
245
|
+
}
|
|
246
|
+
const arriving = await savingIn(store, target, [], moves.map(({ to }) => to));
|
|
247
|
+
const built = await buildMove({
|
|
248
|
+
source: await sideOf(runtime, locator, replica, sourceSaving),
|
|
249
|
+
destination: await sideOf(runtime, destination, target, arriving),
|
|
250
|
+
moves,
|
|
251
|
+
providerLimits: (await local.facts()).providerLimits,
|
|
252
|
+
local: localSaves,
|
|
253
|
+
...(writtenBy === undefined ? {} : { writtenBy }),
|
|
254
|
+
});
|
|
255
|
+
if (built.kind === 'refused')
|
|
256
|
+
return { outcome: 'refused', ...built.refusal };
|
|
257
|
+
const id = built.leaving.id;
|
|
258
|
+
const leavingDrafts = new Map((await store.drafts(locator, saved.map(({ path }) => path))).map((record) => [record.path, record]));
|
|
259
|
+
const arrivingDrafts = new Set((await store.drafts(destination, moves.map(({ to }) => to))).map(({ path }) => path));
|
|
260
|
+
const carrying = saved.flatMap(({ path, parts }) => {
|
|
261
|
+
const record = leavingDrafts.get(path);
|
|
262
|
+
const to = moveTarget(parts.move);
|
|
263
|
+
if (record === undefined || consumed.has(path) || arrivingDrafts.has(to)) {
|
|
264
|
+
return [];
|
|
265
|
+
}
|
|
266
|
+
const moved = {
|
|
267
|
+
...record,
|
|
268
|
+
locator: destination,
|
|
269
|
+
path: to,
|
|
270
|
+
revision: roomDraftRevision(),
|
|
271
|
+
};
|
|
272
|
+
return [{ from: path, record: moved }];
|
|
273
|
+
});
|
|
274
|
+
const carried = carrying.map(({ record }) => record);
|
|
275
|
+
const marked = carried.map(({ path }) => ({ path, parts: {} }));
|
|
276
|
+
const left = [...consumed, ...carrying.map(({ from }) => from)];
|
|
277
|
+
const leavingQueue = [...replica.queue, built.leaving];
|
|
278
|
+
const arrivingQueue = [...target.queue, built.arriving];
|
|
279
|
+
const answer = await saveCommit(store, local, files, [
|
|
280
|
+
{
|
|
281
|
+
locator,
|
|
282
|
+
saving: sourceSaving,
|
|
283
|
+
before: replica,
|
|
284
|
+
queue: leavingQueue,
|
|
285
|
+
write: {
|
|
286
|
+
locator,
|
|
287
|
+
queue: leavingQueue,
|
|
288
|
+
stored: replica.queue,
|
|
289
|
+
saved: new Map([[id, saved]]),
|
|
290
|
+
consumedRoomDrafts: left,
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
locator: destination,
|
|
295
|
+
saving: arriving,
|
|
296
|
+
before: target,
|
|
297
|
+
queue: arrivingQueue,
|
|
298
|
+
write: {
|
|
299
|
+
locator: destination,
|
|
300
|
+
queue: arrivingQueue,
|
|
301
|
+
stored: target.queue,
|
|
302
|
+
saved: new Map([[id, marked]]),
|
|
303
|
+
roomDrafts: carried,
|
|
304
|
+
},
|
|
305
|
+
},
|
|
306
|
+
]);
|
|
307
|
+
if (answer === null)
|
|
308
|
+
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
309
|
+
if (answer.fenced !== null) {
|
|
310
|
+
throw runtimeError('unknown-room', `Unknown Room ${answer.fenced}`);
|
|
311
|
+
}
|
|
312
|
+
if (store.fences.has(locator) || store.fences.has(destination)) {
|
|
313
|
+
throw runtimeError('unknown-room', `Unknown Room ${locator}`);
|
|
314
|
+
}
|
|
315
|
+
const shownNow = store.changed(mergeChangeSets([
|
|
316
|
+
changeSet(locator, {
|
|
317
|
+
documents: answer.changed[locator] ?? [],
|
|
318
|
+
queue: true,
|
|
319
|
+
drafts: left,
|
|
320
|
+
unindexed: answer.indexed[locator] === false,
|
|
321
|
+
due: true,
|
|
322
|
+
}),
|
|
323
|
+
changeSet(destination, {
|
|
324
|
+
documents: answer.changed[destination] ?? [],
|
|
325
|
+
queue: true,
|
|
326
|
+
drafts: carried.map(({ path }) => path),
|
|
327
|
+
unindexed: answer.indexed[destination] === false,
|
|
328
|
+
}),
|
|
329
|
+
]));
|
|
330
|
+
await Promise.all([shownNow, pendingShown(runtime)]);
|
|
331
|
+
return { outcome: 'saved', identifier: id };
|
|
332
|
+
}
|
|
333
|
+
async function committed(runtime, locator, target, saved, consumed, writtenBy, files = []) {
|
|
334
|
+
const { store, localSaves } = runtime;
|
|
335
|
+
if (store.space.full())
|
|
336
|
+
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
337
|
+
const into = movedInto(locator, saved);
|
|
338
|
+
if (into !== null) {
|
|
339
|
+
return committedMove(runtime, locator, into, target, saved, consumed, writtenBy, files);
|
|
340
|
+
}
|
|
341
|
+
const { local, replica } = target;
|
|
342
|
+
const { providerLimits } = await local.facts();
|
|
343
|
+
const parts = saved.map(({ path, parts: held }) => changesOf(path, held));
|
|
344
|
+
const changes = parts.flatMap(({ changes: made }) => made);
|
|
345
|
+
const saving = await savingIn(store, replica, changes, [
|
|
346
|
+
...saved.map(({ path }) => path),
|
|
347
|
+
...parts.flatMap(({ bases }) => bases.map(({ path }) => path)),
|
|
348
|
+
]);
|
|
349
|
+
const changed = saved.flatMap(({ path, parts: held }) => changedParts(path, held, saving.visible));
|
|
350
|
+
if (changed.length > 0)
|
|
351
|
+
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
352
|
+
const requeue = await stepped(requeuing(replica.queue, changes, saving.visible, withdrawable(replica.states), saving.visibleOver));
|
|
353
|
+
if (requeue.kind === 'refused')
|
|
354
|
+
return { outcome: 'refused', ...requeue.refusal };
|
|
355
|
+
const foreign = [...requeue.withdrawn.keys()].some((origin) => {
|
|
356
|
+
const writer = replica.queue.find((batch) => batch.id === origin)?.writtenBy;
|
|
357
|
+
return writer?.app !== writtenBy?.app || writer?.release !== writtenBy?.release;
|
|
358
|
+
});
|
|
359
|
+
if (foreign) {
|
|
360
|
+
return {
|
|
361
|
+
outcome: 'refused',
|
|
362
|
+
code: 'unknown-revision',
|
|
363
|
+
retryable: false,
|
|
364
|
+
file: [...requeue.queued].at(-1),
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
const withdrawing = requeue.withdrawn.size > 0;
|
|
368
|
+
const input = {
|
|
369
|
+
local: localSaves,
|
|
370
|
+
baseRoomVersion: replica.record.roomVersion,
|
|
371
|
+
composition: compositionFor(runtime, replica.record),
|
|
372
|
+
rule: runtime.writeRule(replica.record.declaration),
|
|
373
|
+
providerLimits,
|
|
374
|
+
handle: replica.record.declaration.handle,
|
|
375
|
+
visible: saving.visible,
|
|
376
|
+
folders: saving.folders,
|
|
377
|
+
changes,
|
|
378
|
+
bases: parts.flatMap(({ bases }) => bases),
|
|
379
|
+
queued: queuedDocuments(replica.queue),
|
|
380
|
+
writtenBy,
|
|
381
|
+
unmodeled: await unmodeledIn(runtime, replica.record),
|
|
382
|
+
validate: validatorOf(runtime, replica.record, saving.confirmed),
|
|
383
|
+
};
|
|
384
|
+
const checked = withdrawing
|
|
385
|
+
? await buildBatch({ ...input, queued: new Set([...input.queued, ...requeue.queued]) })
|
|
386
|
+
: null;
|
|
387
|
+
if (checked?.kind === 'refused')
|
|
388
|
+
return { outcome: 'refused', ...checked.refusal };
|
|
389
|
+
const owners = [...requeue.owners].flatMap((origin) => {
|
|
390
|
+
const batch = requeue.withdrawn.get(origin);
|
|
391
|
+
return batch === undefined || batch === null ? [] : [{ origin, batch }];
|
|
392
|
+
});
|
|
393
|
+
if (owners.length > 1) {
|
|
394
|
+
return {
|
|
395
|
+
outcome: 'refused',
|
|
396
|
+
code: 'unknown-revision',
|
|
397
|
+
retryable: false,
|
|
398
|
+
file: [...requeue.queued].at(-1),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
const built = !withdrawing
|
|
402
|
+
? await buildBatch(input)
|
|
403
|
+
: requeue.changes.length === 0
|
|
404
|
+
? null
|
|
405
|
+
: await buildBatch({
|
|
406
|
+
...input,
|
|
407
|
+
visible: await stepped(saving.visibleOver(requeue.queue)),
|
|
408
|
+
changes: requeue.changes,
|
|
409
|
+
queued: queuedDocuments(requeue.queue),
|
|
410
|
+
});
|
|
411
|
+
if (built?.kind === 'refused')
|
|
412
|
+
return { outcome: 'refused', ...built.refusal };
|
|
413
|
+
const [owner] = owners;
|
|
414
|
+
const join = built === null || owner === undefined
|
|
415
|
+
? null
|
|
416
|
+
: joined(requeue.queue, owner.batch, built.batch, input.composition);
|
|
417
|
+
if (join?.kind === 'refused')
|
|
418
|
+
return { outcome: 'refused', ...join.refusal };
|
|
419
|
+
const withdrawn = new Map(requeue.withdrawn);
|
|
420
|
+
if (join !== null && owner !== undefined)
|
|
421
|
+
withdrawn.set(owner.origin, join.batch);
|
|
422
|
+
const drafted = new Map();
|
|
423
|
+
for (const [origin, replaced] of withdrawn) {
|
|
424
|
+
if (replaced === null)
|
|
425
|
+
continue;
|
|
426
|
+
const carried = withdrawnDrafts(replica.states.get(origin)?.drafts ?? [], changes);
|
|
427
|
+
drafted.set(replaced.id, replaced === join?.batch
|
|
428
|
+
? [
|
|
429
|
+
...carried.map((held) => joinedDraft(held, saved)),
|
|
430
|
+
...saved.filter((draft) => !carried.some((held) => held.path === draft.path)),
|
|
431
|
+
]
|
|
432
|
+
: carried);
|
|
433
|
+
}
|
|
434
|
+
if (built !== null && join === null)
|
|
435
|
+
drafted.set(built.batch.id, saved);
|
|
436
|
+
const batches = join !== null ? join.queue : built === null ? requeue.queue : [...requeue.queue, built.batch];
|
|
437
|
+
for (const replaced of withdrawn.values()) {
|
|
438
|
+
if (replaced === null || !batches.includes(replaced))
|
|
439
|
+
continue;
|
|
440
|
+
const limited = await stepped(limiting(batches, replaced, providerLimits, saving.visibleOver));
|
|
441
|
+
if (limited !== null)
|
|
442
|
+
return { outcome: 'refused', ...limited };
|
|
443
|
+
}
|
|
444
|
+
const carried = await carriedDrafts(store, locator, saved, consumed);
|
|
445
|
+
const left = [...consumed, ...carried.keys()];
|
|
446
|
+
const arrived = [...carried.values()];
|
|
447
|
+
const answer = await saveCommit(store, local, files, [
|
|
448
|
+
{
|
|
449
|
+
locator,
|
|
450
|
+
saving,
|
|
451
|
+
before: replica,
|
|
452
|
+
queue: batches,
|
|
453
|
+
write: {
|
|
454
|
+
locator,
|
|
455
|
+
queue: batches,
|
|
456
|
+
stored: replica.queue,
|
|
457
|
+
saved: drafted,
|
|
458
|
+
consumedRoomDrafts: left,
|
|
459
|
+
roomDrafts: arrived,
|
|
460
|
+
},
|
|
461
|
+
},
|
|
462
|
+
]);
|
|
463
|
+
if (answer === null)
|
|
464
|
+
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
465
|
+
if (answer.fenced !== null) {
|
|
466
|
+
throw runtimeError('unknown-room', `Unknown Room ${answer.fenced}`);
|
|
467
|
+
}
|
|
468
|
+
if (store.fences.has(locator))
|
|
469
|
+
throw runtimeError('unknown-room', `Unknown Room ${locator}`);
|
|
470
|
+
const shownNow = store.changed(changeSet(locator, {
|
|
471
|
+
documents: answer.changed[locator] ?? [],
|
|
472
|
+
queue: true,
|
|
473
|
+
drafts: [...left, ...arrived.map(({ path }) => path)],
|
|
474
|
+
unindexed: answer.indexed[locator] === false,
|
|
475
|
+
due: true,
|
|
476
|
+
}));
|
|
477
|
+
await Promise.all([shownNow, pendingShown(runtime)]);
|
|
478
|
+
const identifier = join?.batch.id ??
|
|
479
|
+
built?.batch.id ??
|
|
480
|
+
[...requeue.withdrawn].map(([origin, replaced]) => replaced?.id ?? origin).at(-1);
|
|
481
|
+
return { outcome: 'saved', identifier };
|
|
482
|
+
}
|
|
483
|
+
async function queuedFacts(store, locator, path, localId) {
|
|
484
|
+
for (const batch of (await store.replica(locator))?.queue ?? []) {
|
|
485
|
+
const change = batch.changes[queuedFile(batch, path, localId)];
|
|
486
|
+
if (change?.kind === 'binary')
|
|
487
|
+
return change;
|
|
488
|
+
}
|
|
489
|
+
const drafted = (await store.draft(locator, path))?.parts.file;
|
|
490
|
+
return drafted?.localId === localId ? drafted : null;
|
|
491
|
+
}
|
|
492
|
+
export function batchFile(store, batch) {
|
|
493
|
+
return async (localId) => {
|
|
494
|
+
const change = batch.changes.find((held) => held.kind === 'binary' && held.localId === localId);
|
|
495
|
+
return (await store.open()).file(localId, change?.kind === 'binary' ? change.mediaType : '');
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
async function transferBinary(runtime, request, transfer, answer) {
|
|
499
|
+
const { store, socket, journal } = runtime;
|
|
500
|
+
const { session } = socket;
|
|
501
|
+
if (socket.closed() || session.now().refusal !== null)
|
|
502
|
+
return UNCERTAIN;
|
|
503
|
+
await socket.firstDiscovery;
|
|
504
|
+
if (!session.now().roomsNamed)
|
|
505
|
+
return UNCERTAIN;
|
|
506
|
+
try {
|
|
507
|
+
await socket.authorize();
|
|
508
|
+
}
|
|
509
|
+
catch {
|
|
510
|
+
return UNCERTAIN;
|
|
511
|
+
}
|
|
512
|
+
const { closed, authorization, refusal } = session.now();
|
|
513
|
+
if (closed || authorization === undefined || refusal !== null)
|
|
514
|
+
return UNCERTAIN;
|
|
515
|
+
const since = store.fences.count();
|
|
516
|
+
journal.record(`transfer binary ${request.locator} ${request.path}`);
|
|
517
|
+
try {
|
|
518
|
+
const result = await transfer(authorization);
|
|
519
|
+
const settled = result;
|
|
520
|
+
journal.record(`outcome binary ${settled.kind}`);
|
|
521
|
+
if (store.fences.since(request.locator, since))
|
|
522
|
+
return failure({ kind: 'room-not-readable', locator: request.locator });
|
|
523
|
+
if (settled.kind === 'scoped-refusal') {
|
|
524
|
+
const room = await store.record(settled.locator);
|
|
525
|
+
if (room !== undefined)
|
|
526
|
+
await scoped(runtime, settled, room.locator, room.declaration);
|
|
527
|
+
}
|
|
528
|
+
if (settled.kind === 'revoked-authorization')
|
|
529
|
+
await refuse(runtime, 'revoked-authorization');
|
|
530
|
+
return answer(result);
|
|
531
|
+
}
|
|
532
|
+
catch {
|
|
533
|
+
journal.record('outcome binary transport-uncertainty');
|
|
534
|
+
return UNCERTAIN;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
function fetchBinary(runtime, request, name) {
|
|
538
|
+
const { store, socket } = runtime;
|
|
539
|
+
return transferBinary(runtime, request, (authorization) => store.opened.fetch(name, () => socket.binary(authorization, request)), binaryAnswer);
|
|
540
|
+
}
|
|
541
|
+
async function readBinary(runtime, request, name) {
|
|
542
|
+
if (runtime.socket.closed())
|
|
543
|
+
return UNCERTAIN;
|
|
544
|
+
const held = await runtime.store.opened.find(name);
|
|
545
|
+
if (held !== null)
|
|
546
|
+
return held;
|
|
547
|
+
return fetchBinary(runtime, request, name);
|
|
548
|
+
}
|
|
549
|
+
async function binaryPath(store, locator, path, revision) {
|
|
550
|
+
if (revision !== undefined)
|
|
551
|
+
return { kind: 'path', locator, path, revision };
|
|
552
|
+
const replica = await store.replica(locator);
|
|
553
|
+
const local = await store.open();
|
|
554
|
+
const [held] = replica === undefined ? [] : await local.entries(replica.record.key, 'visible', [path]);
|
|
555
|
+
const attached = held === undefined ? store.attached.of(locator, path) : undefined;
|
|
556
|
+
if (attached !== undefined)
|
|
557
|
+
return { kind: 'path', locator, path, revision: attached.revision };
|
|
558
|
+
if (replica === undefined || held?.binary === undefined || held.binary === null)
|
|
559
|
+
return null;
|
|
560
|
+
if (held.binary.state === 'pending')
|
|
561
|
+
return { kind: 'pending', localId: held.binary.localId };
|
|
562
|
+
const record = binaryRecord(path, held.binary, held.revision ?? undefined);
|
|
563
|
+
if (record.state !== 'accepted')
|
|
564
|
+
return null;
|
|
565
|
+
const queued = replica.queue.flatMap((batch) => batch.changes);
|
|
566
|
+
const sources = queued.flatMap((change) => (change.kind === 'move' ? [change.path] : []));
|
|
567
|
+
const confirmedLayer = await stepped(slicing(await local.entries(replica.record.key, 'confirmed', [path, ...sources]), [
|
|
568
|
+
path,
|
|
569
|
+
...sources,
|
|
570
|
+
]));
|
|
571
|
+
const holds = (at) => {
|
|
572
|
+
const kept = confirmedLayer.binaries[at];
|
|
573
|
+
return (kept?.state === 'accepted' &&
|
|
574
|
+
kept.sha256 === record.sha256 &&
|
|
575
|
+
confirmedLayer.revisions[at] === record.revision);
|
|
576
|
+
};
|
|
577
|
+
let confirmedAt = path;
|
|
578
|
+
for (let index = queued.length - 1; index >= 0 && !holds(confirmedAt); index -= 1) {
|
|
579
|
+
const change = queued[index];
|
|
580
|
+
if (change.kind === 'move' && change.to === confirmedAt)
|
|
581
|
+
confirmedAt = change.path;
|
|
582
|
+
}
|
|
583
|
+
return {
|
|
584
|
+
kind: 'path',
|
|
585
|
+
locator,
|
|
586
|
+
path: confirmedAt,
|
|
587
|
+
revision: record.revision,
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
async function askedBinary({ store, socket }, locator, path, revision) {
|
|
591
|
+
await socket.ready;
|
|
592
|
+
const drafted = (await store.draft(locator, path))?.parts.file;
|
|
593
|
+
return drafted !== undefined && drafted.localId === revision
|
|
594
|
+
? { kind: 'pending', localId: revision }
|
|
595
|
+
: binaryPath(store, locator, path, revision);
|
|
596
|
+
}
|
|
597
|
+
async function pendingBinary(store, locator, path, localId) {
|
|
598
|
+
const facts = await queuedFacts(store, locator, path, localId);
|
|
599
|
+
return facts === null
|
|
600
|
+
? absentBinary(path)
|
|
601
|
+
: {
|
|
602
|
+
kind: 'binary',
|
|
603
|
+
file: localId,
|
|
604
|
+
byteLength: facts.byteLength,
|
|
605
|
+
mediaType: facts.mediaType,
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
async function readBinaryPath(runtime, locator, path, revision) {
|
|
609
|
+
const asked = await askedBinary(runtime, locator, path, revision);
|
|
610
|
+
if (asked === null)
|
|
611
|
+
return absentBinary(path);
|
|
612
|
+
if (asked.kind === 'pending')
|
|
613
|
+
return pendingBinary(runtime.store, locator, path, asked.localId);
|
|
614
|
+
return readBinary(runtime, asked, { locator, path: asked.path, revision: asked.revision });
|
|
615
|
+
}
|
|
616
|
+
async function heldMediaType(store, locator, path) {
|
|
617
|
+
const replica = await store.record(locator);
|
|
618
|
+
const [held] = replica === undefined ? [] : await (await store.open()).entries(replica.key, 'visible', [path]);
|
|
619
|
+
return held === undefined ? store.attached.of(locator, path)?.mediaType : held.binary?.mediaType;
|
|
620
|
+
}
|
|
621
|
+
function linkOf(runtime, asked, held) {
|
|
622
|
+
return transferBinary(runtime, asked, (authorization) => runtime.socket.link(authorization, asked), (result) => binaryLinkAnswer(result, held));
|
|
623
|
+
}
|
|
624
|
+
async function linkBinaryPath(runtime, locator, path, revision) {
|
|
625
|
+
const { store, socket } = runtime;
|
|
626
|
+
await socket.ready;
|
|
627
|
+
const asked = await binaryPath(store, locator, path, revision);
|
|
628
|
+
if (asked === null || asked.kind === 'pending')
|
|
629
|
+
return absentBinary(path);
|
|
630
|
+
return linkOf(runtime, asked, revision === undefined ? await heldMediaType(store, locator, path) : undefined);
|
|
631
|
+
}
|
|
632
|
+
async function showBinaryPath(runtime, locator, path, revision) {
|
|
633
|
+
const { store } = runtime;
|
|
634
|
+
const asked = await askedBinary(runtime, locator, path, revision);
|
|
635
|
+
if (asked === null)
|
|
636
|
+
return absentBinary(path);
|
|
637
|
+
if (asked.kind === 'pending')
|
|
638
|
+
return pendingBinary(store, locator, path, asked.localId);
|
|
639
|
+
if (runtime.socket.closed())
|
|
640
|
+
return UNCERTAIN;
|
|
641
|
+
const name = { locator, path: asked.path, revision: asked.revision };
|
|
642
|
+
const kept = await store.opened.find(name);
|
|
643
|
+
if (kept !== null)
|
|
644
|
+
return kept;
|
|
645
|
+
const mediaType = await heldMediaType(store, locator, path);
|
|
646
|
+
if (mediaType?.startsWith('image/') === true)
|
|
647
|
+
return fetchBinary(runtime, asked, name);
|
|
648
|
+
return linkOf(runtime, asked, revision === undefined ? mediaType : undefined);
|
|
649
|
+
}
|
|
650
|
+
async function attachBinary(runtime, locator, file) {
|
|
651
|
+
const { store, socket } = runtime;
|
|
652
|
+
await socket.ready;
|
|
653
|
+
await socket.firstDiscovery;
|
|
654
|
+
if (socket.closed() ||
|
|
655
|
+
!socket.session.now().roomsNamed ||
|
|
656
|
+
(await connectionNow(runtime)) === 'unreachable' ||
|
|
657
|
+
socket.session.now().refusal !== null)
|
|
658
|
+
return OFFLINE;
|
|
659
|
+
const request = await attachedFile(locator, file);
|
|
660
|
+
return transferBinary(runtime, { locator, path: file.name }, async (held) => {
|
|
661
|
+
const result = await socket.attach(held, request);
|
|
662
|
+
if (result.kind === 'attached') {
|
|
663
|
+
store.attached.hold(locator, result.path, {
|
|
664
|
+
revision: result.revision,
|
|
665
|
+
mediaType: request.mediaType,
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
return result;
|
|
669
|
+
}, attachAnswer);
|
|
670
|
+
}
|
|
671
|
+
export function binaryFileSurface(runtime) {
|
|
672
|
+
return {
|
|
673
|
+
part: runtime.store.part,
|
|
674
|
+
read: (room, path, revision) => readBinaryPath(runtime, room, path, revision),
|
|
675
|
+
link: (room, path, revision) => linkBinaryPath(runtime, room, path, revision),
|
|
676
|
+
show: (room, path, revision) => showBinaryPath(runtime, room, path, revision),
|
|
677
|
+
attach: (room, file) => attachBinary(runtime, room, file),
|
|
678
|
+
};
|
|
679
|
+
}
|