@agentshouse/mdruntime 0.1.0-ce6bc9239a3ee → 0.1.0-ce80727f9590f
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 +21 -19
- package/dist/acceptance.d.ts +2 -25
- package/dist/acceptance.d.ts.map +1 -1
- package/dist/acceptance.js +119 -204
- package/dist/act-lanes.d.ts +30 -0
- package/dist/act-lanes.d.ts.map +1 -0
- package/dist/act-lanes.js +225 -0
- package/dist/actions.d.ts +26 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +321 -0
- package/dist/adapter.d.ts +114 -57
- package/dist/adapter.d.ts.map +1 -1
- package/dist/attached-model.d.ts +0 -1
- package/dist/attached-model.d.ts.map +1 -1
- package/dist/attached-model.js +0 -5
- package/dist/authored.d.ts +88 -0
- package/dist/authored.d.ts.map +1 -0
- package/dist/authored.js +345 -0
- package/dist/bootstrap.d.ts +1 -1
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +6 -18
- package/dist/cell.d.ts +6 -0
- package/dist/cell.d.ts.map +1 -0
- package/dist/cell.js +9 -0
- package/dist/change-set.d.ts +4 -0
- package/dist/change-set.d.ts.map +1 -1
- package/dist/change-set.js +6 -0
- package/dist/change.d.ts +4 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +7 -2
- package/dist/chat-types.d.ts +2 -2
- package/dist/chat-types.d.ts.map +1 -1
- package/dist/chat-types.js +11 -2
- package/dist/client.d.ts +25 -22
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +45 -58
- package/dist/collection.d.ts +26 -4
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +28 -10
- package/dist/column.d.ts +5 -0
- package/dist/column.d.ts.map +1 -0
- package/dist/column.js +9 -0
- package/dist/compile.d.ts +2 -0
- package/dist/compile.d.ts.map +1 -1
- package/dist/compile.js +31 -25
- package/dist/connection.d.ts +4 -116
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +103 -2994
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +1 -1
- package/dist/conversation-live.d.ts +150 -0
- package/dist/conversation-live.d.ts.map +1 -0
- package/dist/conversation-live.js +259 -0
- package/dist/conversation-row.d.ts +8 -1
- package/dist/conversation-row.d.ts.map +1 -1
- package/dist/conversation-row.js +22 -3
- package/dist/conversation.d.ts +57 -45
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +974 -1031
- package/dist/declaration.d.ts +10 -0
- package/dist/declaration.d.ts.map +1 -0
- package/dist/declaration.js +24 -0
- package/dist/discovery.d.ts +12 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +175 -0
- package/dist/download.d.ts +6 -0
- package/dist/download.d.ts.map +1 -0
- package/dist/download.js +42 -0
- package/dist/durability.d.ts +14 -0
- package/dist/durability.d.ts.map +1 -1
- package/dist/durability.js +7 -0
- package/dist/entry.d.ts +5 -0
- package/dist/entry.d.ts.map +1 -1
- package/dist/entry.js +23 -12
- package/dist/expectation.d.ts +26 -13
- package/dist/expectation.d.ts.map +1 -1
- package/dist/expectation.js +72 -19
- package/dist/failure.d.ts +3 -0
- package/dist/failure.d.ts.map +1 -1
- package/dist/failure.js +8 -0
- package/dist/figures.d.ts +1 -0
- package/dist/figures.d.ts.map +1 -1
- package/dist/figures.js +6 -4
- package/dist/file.d.ts +1 -1
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +3 -2
- 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 +20 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +83 -19
- package/dist/house-query.d.ts +21 -0
- package/dist/house-query.d.ts.map +1 -0
- package/dist/house-query.js +41 -0
- package/dist/index.d.ts +28 -12
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -6
- package/dist/install.d.ts +22 -48
- package/dist/install.d.ts.map +1 -1
- package/dist/install.js +161 -161
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +290 -0
- package/dist/listing.d.ts +60 -0
- package/dist/listing.d.ts.map +1 -0
- package/dist/listing.js +168 -0
- package/dist/live.d.ts +5 -3
- package/dist/live.d.ts.map +1 -1
- package/dist/live.js +74 -33
- package/dist/loaded.d.ts +5 -0
- package/dist/loaded.d.ts.map +1 -0
- package/dist/loaded.js +40 -0
- package/dist/locks.d.ts +7 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +34 -0
- package/dist/lost.d.ts +5 -0
- package/dist/lost.d.ts.map +1 -0
- package/dist/lost.js +49 -0
- package/dist/media.d.ts +19 -2
- package/dist/media.d.ts.map +1 -1
- package/dist/media.js +105 -27
- package/dist/memory.d.ts +9 -3
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +94 -32
- package/dist/messages.d.ts +12 -6
- package/dist/messages.d.ts.map +1 -1
- package/dist/messages.js +35 -39
- package/dist/modal.d.ts +2 -0
- package/dist/modal.d.ts.map +1 -0
- package/dist/modal.js +11 -0
- package/dist/model.d.ts +4 -3
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +6 -5
- package/dist/models.d.ts +10 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +153 -0
- package/dist/observation.d.ts +29 -12
- package/dist/observation.d.ts.map +1 -1
- package/dist/observation.js +217 -85
- package/dist/online.d.ts +7 -0
- package/dist/online.d.ts.map +1 -0
- package/dist/online.js +29 -0
- package/dist/opened.d.ts +1 -1
- package/dist/opened.d.ts.map +1 -1
- package/dist/opened.js +5 -5
- package/dist/opening.d.ts +4 -0
- package/dist/opening.d.ts.map +1 -0
- package/dist/opening.js +70 -0
- package/dist/outcome.d.ts +16 -2
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +32 -2
- package/dist/overlay.d.ts +6 -2
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +37 -24
- package/dist/page-metadata.d.ts +8 -0
- package/dist/page-metadata.d.ts.map +1 -0
- package/dist/page-metadata.js +19 -0
- package/dist/parts.d.ts +5 -58
- package/dist/parts.d.ts.map +1 -1
- package/dist/parts.js +28 -110
- package/dist/paths.d.ts +1 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +22 -0
- package/dist/preparation.d.ts +41 -0
- package/dist/preparation.d.ts.map +1 -0
- package/dist/preparation.js +326 -0
- package/dist/presence.d.ts +58 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +231 -0
- package/dist/queries.d.ts +16 -0
- package/dist/queries.d.ts.map +1 -0
- package/dist/queries.js +287 -0
- package/dist/query.d.ts +0 -1
- package/dist/query.d.ts.map +1 -1
- package/dist/questions.d.ts +3 -5
- package/dist/questions.d.ts.map +1 -1
- package/dist/questions.js +4 -6
- package/dist/queue.d.ts +40 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +480 -0
- package/dist/reach.d.ts +15 -5
- package/dist/reach.d.ts.map +1 -1
- package/dist/reach.js +164 -33
- package/dist/react.d.ts +17 -2
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +33 -1
- package/dist/readiness.d.ts +9 -4
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +3 -5
- package/dist/reads.d.ts +5 -2
- package/dist/reads.d.ts.map +1 -1
- package/dist/reads.js +49 -7
- package/dist/recovery.d.ts +4 -1
- package/dist/recovery.d.ts.map +1 -1
- package/dist/recovery.js +31 -8
- package/dist/refresh.js +2 -2
- package/dist/refusal.d.ts +5 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +55 -0
- package/dist/replica.d.ts +5 -10
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +17 -18
- package/dist/retried.d.ts +30 -0
- package/dist/retried.d.ts.map +1 -0
- package/dist/retried.js +72 -0
- package/dist/room-draft.d.ts +31 -8
- package/dist/room-draft.d.ts.map +1 -1
- package/dist/room-draft.js +273 -66
- package/dist/room-drafts.d.ts +7 -12
- package/dist/room-drafts.d.ts.map +1 -1
- package/dist/room-drafts.js +44 -40
- package/dist/room-list.d.ts +10 -0
- package/dist/room-list.d.ts.map +1 -0
- package/dist/room-list.js +45 -0
- package/dist/rows.d.ts +2 -1
- package/dist/rows.d.ts.map +1 -1
- package/dist/rows.js +9 -10
- package/dist/running.d.ts +5 -0
- package/dist/running.d.ts.map +1 -0
- package/dist/running.js +10 -0
- package/dist/runtime.d.ts +36 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +1 -0
- package/dist/save.d.ts +42 -86
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +326 -225
- package/dist/saves.d.ts +8 -70
- package/dist/saves.d.ts.map +1 -1
- package/dist/saves.js +199 -761
- package/dist/seed.d.ts +3 -8
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +21 -17
- package/dist/shared.d.ts +1 -6
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +13 -95
- package/dist/shown.d.ts +35 -0
- package/dist/shown.d.ts.map +1 -0
- package/dist/shown.js +326 -0
- package/dist/socket/lanes.d.ts +28 -0
- package/dist/socket/lanes.d.ts.map +1 -0
- package/dist/socket/lanes.js +103 -0
- package/dist/socket/session.d.ts +41 -0
- package/dist/socket/session.d.ts.map +1 -0
- package/dist/socket/session.js +26 -0
- package/dist/socket.d.ts +30 -5
- package/dist/socket.d.ts.map +1 -1
- package/dist/socket.js +75 -6
- package/dist/source.d.ts +5 -7
- package/dist/source.d.ts.map +1 -1
- package/dist/source.js +18 -6
- package/dist/store/installs.d.ts +2 -2
- package/dist/store/installs.d.ts.map +1 -1
- package/dist/store/installs.js +10 -4
- package/dist/store/readiness.d.ts +11 -0
- package/dist/store/readiness.d.ts.map +1 -0
- package/dist/store/readiness.js +68 -0
- package/dist/store/records.d.ts +22 -8
- package/dist/store/records.d.ts.map +1 -1
- package/dist/store/records.js +84 -24
- package/dist/store/rooms-shown.d.ts +8 -0
- package/dist/store/rooms-shown.d.ts.map +1 -0
- package/dist/store/rooms-shown.js +13 -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 +2 -0
- package/dist/store/watchers.d.ts.map +1 -1
- package/dist/store/watchers.js +23 -3
- package/dist/store.d.ts +50 -9
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +78 -17
- package/dist/stored.d.ts +25 -14
- package/dist/stored.d.ts.map +1 -1
- package/dist/stored.js +99 -36
- package/dist/surface.d.ts +94 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +255 -0
- package/dist/sync.d.ts +21 -0
- package/dist/sync.d.ts.map +1 -0
- package/dist/sync.js +557 -0
- package/dist/tables.d.ts +28 -59
- package/dist/tables.d.ts.map +1 -1
- package/dist/tables.js +106 -331
- package/dist/tallies.d.ts.map +1 -1
- package/dist/tallies.js +6 -7
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +4 -10
- package/dist/wire.d.ts +68 -8
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +26 -9
- package/package.json +3 -2
- package/dist/attempt.d.ts +0 -16
- package/dist/attempt.d.ts.map +0 -1
- package/dist/attempt.js +0 -70
- package/dist/job.d.ts +0 -33
- package/dist/job.d.ts.map +0 -1
- package/dist/job.js +0 -54
- package/dist/log.d.ts +0 -6
- package/dist/log.d.ts.map +0 -1
- package/dist/log.js +0 -14
- package/dist/store/fences.d.ts +0 -16
- package/dist/store/fences.d.ts.map +0 -1
- package/dist/store/fences.js +0 -26
package/dist/saves.js
CHANGED
|
@@ -1,432 +1,161 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isImageType, parse } from '@agentshouse/mdmodel';
|
|
2
2
|
import { binaryRecord } from './binary.js';
|
|
3
|
-
import { changeSet
|
|
4
|
-
import {
|
|
5
|
-
import { available,
|
|
3
|
+
import { changeSet } from './change-set.js';
|
|
4
|
+
import { queuedFile } from './change.js';
|
|
5
|
+
import { available, textType } from './collection.js';
|
|
6
6
|
import { runtimeError } from './failure.js';
|
|
7
7
|
import { attachedFile } from './file.js';
|
|
8
8
|
import { roomDraftRevision } from './identity.js';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
9
|
+
import { compositionFor } from './listing.js';
|
|
10
|
+
import { scoped } from './models.js';
|
|
11
|
+
import { absentBinary, attachAnswer, binaryAnswer, binaryLinkAnswer, failure, linkedAnswer, OFFLINE, UNCERTAIN, } from './outcome.js';
|
|
11
12
|
import { sameBinary } from './overlay.js';
|
|
12
13
|
import { ancestors } from './paths.js';
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
14
|
+
import { connectionNow } from './preparation.js';
|
|
15
|
+
import { append, sliceAt, withdrawable } from './queue.js';
|
|
16
|
+
import { entryAt, replaying, slicing, } from './replica.js';
|
|
17
|
+
import { binaryBase, changesOf, held, heldFile, joinedOver, moveTarget, readSavedParts, } from './room-draft.js';
|
|
18
|
+
import { refuse } from './refusal.js';
|
|
15
19
|
import { visibleOf } from './room-drafts.js';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
20
|
+
import { destination, planSave, } from './save.js';
|
|
21
|
+
import { pendingShown } from './shown.js';
|
|
18
22
|
import { paced, ROW_WEIGHT, stepped } from './turn.js';
|
|
19
23
|
import { validatedNeighbourhood } from './validation.js';
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
export async function sliceAt(store, record, view, queue, extra = [], moved = []) {
|
|
27
|
-
const local = await store.open();
|
|
28
|
-
const needed = new Set([...touchedBy(queue), ...extra]);
|
|
29
|
-
const movedPaths = [...new Set([...movedBy(queue, record.locator), ...moved])];
|
|
30
|
-
if (movedPaths.length > 0) {
|
|
31
|
-
for (const { path } of await local.referrers(record.key, view, movedPaths)) {
|
|
32
|
-
needed.add(path);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
const paths = [...needed];
|
|
36
|
-
return stepped(slicing(await local.entries(record.key, view, paths), paths));
|
|
37
|
-
}
|
|
38
|
-
export async function landedWhole(store, locator, batch, confirmedLayer, current) {
|
|
39
|
-
if (!landed(batch, confirmedLayer, current))
|
|
40
|
-
return false;
|
|
41
|
-
const partner = partnerOf(batch, locator);
|
|
42
|
-
if (partner === null)
|
|
43
|
-
return true;
|
|
44
|
-
const other = await store.replica(partner);
|
|
45
|
-
const half = other?.queue.find((one) => one.id === batch.id);
|
|
46
|
-
if (other === undefined || half === undefined)
|
|
47
|
-
return false;
|
|
48
|
-
return landed(half, await sliceAt(store, other.record, 'confirmed', [half]));
|
|
49
|
-
}
|
|
50
|
-
async function carriedBack(store, locator, unlanded, written) {
|
|
51
|
-
const back = new Map();
|
|
52
|
-
const taken = [];
|
|
53
|
-
for (const batch of unlanded) {
|
|
54
|
-
const between = batch.between;
|
|
55
|
-
if (between === undefined)
|
|
56
|
-
continue;
|
|
57
|
-
const source = between.source.room;
|
|
58
|
-
const destination = between.destination.room;
|
|
59
|
-
const local = await store.open();
|
|
60
|
-
const arrived = (await local.pending(destination, batch.id)).state?.drafts ?? [];
|
|
61
|
-
for (const { path: to } of arrived) {
|
|
62
|
-
const record = await store.draft(destination, to);
|
|
63
|
-
const path = between.moves.find((move) => move.to === to)?.path;
|
|
64
|
-
if (record === undefined || path === undefined)
|
|
65
|
-
continue;
|
|
66
|
-
const [into, key] = source === locator ? [written, path] : [back, documentKey(source, path)];
|
|
67
|
-
const existing = into.get(key) ?? (await store.draft(source, path));
|
|
68
|
-
taken.push({ locator: destination, path: to });
|
|
69
|
-
into.set(key, {
|
|
70
|
-
locator: source,
|
|
71
|
-
path,
|
|
72
|
-
revision: roomDraftRevision(),
|
|
73
|
-
parts: returnedInto(existing?.parts, record.parts),
|
|
74
|
-
returned: existing?.returned ?? null,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
24
|
+
const FULL = { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
25
|
+
async function savedRoom({ store, presence }, locator) {
|
|
26
|
+
const replica = await store.replica(locator);
|
|
27
|
+
if (replica === undefined || presence.lost(locator)) {
|
|
28
|
+
throw runtimeError('unknown-room', `Unknown Room ${locator}`);
|
|
77
29
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
})),
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
export function preparedAfter({ prepare }, locator, answer) {
|
|
96
|
-
if (answer.indexed[locator] === false)
|
|
97
|
-
prepare([locator]);
|
|
98
|
-
}
|
|
99
|
-
export function replayed(store, record, confirmedLayer, queue) {
|
|
100
|
-
return stepped(replaying(confirmedLayer, queue, record.locator, store.composed(record)));
|
|
101
|
-
}
|
|
102
|
-
export async function overlayDiff(record, before, visible, touched) {
|
|
103
|
-
const kept = before !== undefined && before.record.key === record.key ? before.overlaid : new Set();
|
|
104
|
-
return {
|
|
105
|
-
overlay: {
|
|
106
|
-
set: await stepped(overlayOf(visible, touched)),
|
|
107
|
-
cleared: [...kept].filter((path) => !touched.has(path)),
|
|
108
|
-
},
|
|
109
|
-
touched,
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
export async function settle(saver, local, locator, work, record, queue, settling = {}) {
|
|
113
|
-
return together(saver, local, [
|
|
114
|
-
await settlement(saver, local, locator, work, record, queue, settling),
|
|
115
|
-
]);
|
|
116
|
-
}
|
|
117
|
-
async function unpairedIn(store, locator, batch) {
|
|
118
|
-
const { state } = await (await store.open()).pending(locator, batch.id);
|
|
119
|
-
if (state === undefined)
|
|
120
|
-
return false;
|
|
121
|
-
return !leavingIn(batch, locator) || withdrawable(new Map([[batch.id, state]]))(batch);
|
|
122
|
-
}
|
|
123
|
-
export async function together(saver, local, settlements) {
|
|
124
|
-
const settled = new Set(settlements.map(({ locator }) => locator));
|
|
125
|
-
const all = [...settlements];
|
|
126
|
-
for (let wave = settlements; wave.length > 0;) {
|
|
127
|
-
const partnered = new Map();
|
|
128
|
-
for (const { locator, left } of wave) {
|
|
129
|
-
for (const batch of left) {
|
|
130
|
-
const partner = partnerOf(batch, locator);
|
|
131
|
-
if (partner === null ||
|
|
132
|
-
settled.has(partner) ||
|
|
133
|
-
!(await unpairedIn(saver.store, partner, batch))) {
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
|
-
partnered.set(partner, [...(partnered.get(partner) ?? []), batch]);
|
|
30
|
+
return replica;
|
|
31
|
+
}
|
|
32
|
+
export function saveRoomDrafts(runtime, locator, named, writtenBy) {
|
|
33
|
+
const { store } = runtime;
|
|
34
|
+
return store.exclusive(async () => {
|
|
35
|
+
if (store.space.full())
|
|
36
|
+
return FULL;
|
|
37
|
+
const replica = await savedRoom(runtime, locator);
|
|
38
|
+
const drafts = new Map((await store.drafts(locator, named.map(({ path }) => path))).map((record) => [record.path, record]));
|
|
39
|
+
const saved = [];
|
|
40
|
+
for (const { path, revision } of named) {
|
|
41
|
+
const record = drafts.get(path);
|
|
42
|
+
if (record === undefined || record.revision !== revision) {
|
|
43
|
+
return { outcome: 'refused', code: 'stale', retryable: false, file: path };
|
|
137
44
|
}
|
|
45
|
+
if (!saved.some((draft) => draft.path === path))
|
|
46
|
+
saved.push({ path, parts: record.parts });
|
|
138
47
|
}
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
all.push(...partners);
|
|
145
|
-
wave = partners;
|
|
146
|
-
}
|
|
147
|
-
const answer = await local.commit({
|
|
148
|
-
rooms: all.flatMap(({ commit }) => commit.rooms),
|
|
149
|
-
writes: all.flatMap(({ commit }) => commit.writes),
|
|
48
|
+
const result = await loaded(runtime, locator, replica, saved, new Set(saved.map(({ path }) => path)), [], writtenBy);
|
|
49
|
+
if (result.outcome !== 'refused' || result.code !== 'changed-underneath')
|
|
50
|
+
return result;
|
|
51
|
+
return joinedUnderneath(runtime, locator, saved.map(({ path }) => drafts.get(path)), result);
|
|
150
52
|
});
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const previousQueue = work.queue;
|
|
158
|
-
const overrides = settling.changes;
|
|
159
|
-
const read = await sliceAt(store, record, view, queue, [
|
|
160
|
-
...(before !== undefined && before.record.key === record.key ? before.overlaid : []),
|
|
161
|
-
...(overrides?.set.map(({ path }) => path) ?? []),
|
|
162
|
-
...(overrides?.removed ?? []),
|
|
163
|
-
]);
|
|
164
|
-
const confirmedLayer = overrides === undefined
|
|
165
|
-
? read
|
|
166
|
-
: await stepped(slicingWith(read, overrides.set, overrides.removed));
|
|
167
|
-
const composition = store.composed(record);
|
|
168
|
-
const shown = queue.filter((batch) => {
|
|
169
|
-
const held = store.landed.of(locator, batch.id);
|
|
170
|
-
return (held !== undefined &&
|
|
171
|
-
Object.entries(held).some(([path, revision]) => confirmedLayer.revisions[path] !== revision));
|
|
53
|
+
}
|
|
54
|
+
async function joinedUnderneath({ store }, locator, records, refused) {
|
|
55
|
+
const visible = (await visibleOf(store, locator, records.map(({ path }) => path)));
|
|
56
|
+
const joined = records.flatMap((record) => {
|
|
57
|
+
const join = joinedOver(record.parts, record.marks, record.path, visible);
|
|
58
|
+
return join === null ? [] : [{ ...record, ...join, revision: roomDraftRevision() }];
|
|
172
59
|
});
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
path: saved.path,
|
|
197
|
-
revision: roomDraftRevision(),
|
|
198
|
-
parts: returnedInto(existing?.parts, saved.parts),
|
|
199
|
-
returned: reason,
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
const carried = await carriedBack(store, locator, [
|
|
204
|
-
...[...unqueued].reverse().flatMap(({ batch, landed: held }) => (held ? [] : [batch])),
|
|
205
|
-
...(settling.leaving ?? []).flatMap(({ batch, returned }) => returned === null ? [] : [batch]),
|
|
206
|
-
], written);
|
|
207
|
-
const replayedLayer = await replayed(store, record, confirmedLayer, kept);
|
|
208
|
-
const { overlay } = await overlayDiff(record, before, replayedLayer.visible, replayedLayer.touched);
|
|
209
|
-
const commit = {
|
|
210
|
-
rooms: [
|
|
211
|
-
{
|
|
212
|
-
locator,
|
|
213
|
-
key: record.key,
|
|
214
|
-
record: storedRecord(record),
|
|
215
|
-
roots: record.typedSelection?.roots ?? [],
|
|
216
|
-
composed: composedOf(composition),
|
|
217
|
-
...settling.commit,
|
|
218
|
-
...(overrides === undefined
|
|
219
|
-
? {}
|
|
220
|
-
: { confirmed: { set: overrides.set, removed: overrides.removed } }),
|
|
221
|
-
overlay,
|
|
222
|
-
},
|
|
223
|
-
],
|
|
224
|
-
writes: [
|
|
225
|
-
{
|
|
226
|
-
locator,
|
|
227
|
-
queue: kept,
|
|
228
|
-
stored: previousQueue,
|
|
229
|
-
founded,
|
|
230
|
-
roomDrafts: [...written.values()],
|
|
231
|
-
consumedRoomDrafts: carried.taken,
|
|
232
|
-
},
|
|
233
|
-
...carried.writes,
|
|
234
|
-
],
|
|
235
|
-
};
|
|
236
|
-
const staying = new Set(kept.map((batch) => batch.id));
|
|
237
|
-
return {
|
|
238
|
-
locator,
|
|
239
|
-
left: previousQueue.filter((batch) => !staying.has(batch.id)),
|
|
240
|
-
commit,
|
|
241
|
-
apply: (answer) => {
|
|
242
|
-
for (const batch of previousQueue) {
|
|
243
|
-
if (!staying.has(batch.id))
|
|
244
|
-
store.landed.drop(locator, batch.id);
|
|
245
|
-
}
|
|
246
|
-
const drafts = [...written.keys(), ...carried.taken];
|
|
247
|
-
for (const { batch, landed: held } of unqueued) {
|
|
248
|
-
const outcome = localSaves ? 'dropped' : held ? 'landed' : 'withdrawn';
|
|
249
|
-
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
60
|
+
if (joined.length === 0)
|
|
61
|
+
return refused;
|
|
62
|
+
await (await store.open()).commit({ rooms: [], writes: [{ locator, roomDrafts: joined }] });
|
|
63
|
+
await store.changed(changeSet(locator, { drafts: joined.map(({ path }) => path) }));
|
|
64
|
+
return refused;
|
|
65
|
+
}
|
|
66
|
+
export function saveParts(runtime, locator, named, writtenBy) {
|
|
67
|
+
const { store } = runtime;
|
|
68
|
+
return store.exclusive(async () => {
|
|
69
|
+
const read = readSavedParts(named);
|
|
70
|
+
if (store.space.full())
|
|
71
|
+
return FULL;
|
|
72
|
+
const replica = await savedRoom(runtime, locator);
|
|
73
|
+
const saved = [];
|
|
74
|
+
const files = [];
|
|
75
|
+
const filed = read.filter(({ parts }) => parts.file !== undefined).map(({ path }) => path);
|
|
76
|
+
const visible = filed.length === 0 ? undefined : await visibleOf(store, locator, filed);
|
|
77
|
+
for (const { path, parts } of read) {
|
|
78
|
+
const file = parts.file === undefined
|
|
79
|
+
? undefined
|
|
80
|
+
: await heldFile(parts.file.blob, parts.file.base === undefined ? binaryBase(visible, path) : parts.file.base);
|
|
81
|
+
if (parts.file !== undefined && file !== undefined) {
|
|
82
|
+
files.push({ key: file.localId, blob: parts.file.blob });
|
|
250
83
|
}
|
|
251
|
-
|
|
252
|
-
if (settling.commit?.published !== undefined ||
|
|
253
|
-
settling.commit?.refreshed === true ||
|
|
254
|
-
overrides !== undefined) {
|
|
255
|
-
confirmedAgain(store, locator);
|
|
256
|
-
}
|
|
257
|
-
preparedAfter(saver, locator, answer);
|
|
258
|
-
return mergeChangeSets([
|
|
259
|
-
changeSet(locator, {
|
|
260
|
-
documents: changed === undefined ? [] : changed,
|
|
261
|
-
room: settling.room,
|
|
262
|
-
queue: queueChanged,
|
|
263
|
-
drafts,
|
|
264
|
-
}),
|
|
265
|
-
...carried.changes,
|
|
266
|
-
]);
|
|
267
|
-
},
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
export async function unpairing(saver, local, locator, batches) {
|
|
271
|
-
const { store, journal, localSaves, submitLater } = saver;
|
|
272
|
-
const ids = new Set(batches.map(({ id }) => id));
|
|
273
|
-
const leaving = batches.map((batch) => ({
|
|
274
|
-
batch,
|
|
275
|
-
returned: !localSaves && leavingIn(batch, locator) ? returnedBy('withdrawn') : null,
|
|
276
|
-
}));
|
|
277
|
-
const outcome = localSaves ? 'dropped' : 'unpaired';
|
|
278
|
-
const work = await store.work(locator);
|
|
279
|
-
const current = work.replica;
|
|
280
|
-
if (current !== undefined) {
|
|
281
|
-
const settled = await settlement(saver, local, locator, work, current.record, current.queue.filter((one) => !ids.has(one.id)), { leaving });
|
|
282
|
-
return {
|
|
283
|
-
...settled,
|
|
284
|
-
apply(answer) {
|
|
285
|
-
for (const batch of batches)
|
|
286
|
-
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
287
|
-
const set = settled.apply(answer);
|
|
288
|
-
submitLater(locator);
|
|
289
|
-
return set;
|
|
290
|
-
},
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
const queue = work.queue.filter((one) => !ids.has(one.id));
|
|
294
|
-
const written = new Map();
|
|
295
|
-
for (const { batch, returned } of leaving) {
|
|
296
|
-
if (returned === null)
|
|
297
|
-
continue;
|
|
298
|
-
for (const saved of work.states.get(batch.id)?.drafts ?? []) {
|
|
299
|
-
written.set(saved.path, {
|
|
300
|
-
locator,
|
|
301
|
-
path: saved.path,
|
|
302
|
-
revision: roomDraftRevision(),
|
|
303
|
-
parts: returnedInto((written.get(saved.path) ?? (await store.draft(locator, saved.path)))?.parts, saved.parts),
|
|
304
|
-
returned,
|
|
305
|
-
});
|
|
84
|
+
saved.push({ path, parts: held(undefined, parts, file) });
|
|
306
85
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
return {
|
|
310
|
-
locator,
|
|
311
|
-
left: batches,
|
|
312
|
-
commit: {
|
|
313
|
-
rooms: [],
|
|
314
|
-
writes: [
|
|
315
|
-
{
|
|
316
|
-
locator,
|
|
317
|
-
queue,
|
|
318
|
-
stored: work.queue,
|
|
319
|
-
roomDrafts: [...written.values()],
|
|
320
|
-
consumedRoomDrafts: carried.taken,
|
|
321
|
-
},
|
|
322
|
-
...carried.writes,
|
|
323
|
-
],
|
|
324
|
-
},
|
|
325
|
-
apply() {
|
|
326
|
-
for (const batch of batches)
|
|
327
|
-
journal.record(`outcome ${locator} ${batch.id} ${outcome}`);
|
|
328
|
-
submitLater(locator);
|
|
329
|
-
return mergeChangeSets([
|
|
330
|
-
changeSet(locator, {
|
|
331
|
-
queue: true,
|
|
332
|
-
drafts: [...written.keys(), ...carried.taken],
|
|
333
|
-
}),
|
|
334
|
-
...carried.changes,
|
|
335
|
-
]);
|
|
336
|
-
},
|
|
337
|
-
};
|
|
86
|
+
return loaded(runtime, locator, replica, saved, new Set(), files, writtenBy);
|
|
87
|
+
});
|
|
338
88
|
}
|
|
339
|
-
async function
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
return
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
const { store } =
|
|
349
|
-
const
|
|
350
|
-
const drafts = new Map((await store.drafts(locator, named.map(({ path }) => path))).map((record) => [record.path, record]));
|
|
351
|
-
const saved = [];
|
|
352
|
-
for (const { path, revision } of named) {
|
|
353
|
-
const record = drafts.get(path);
|
|
354
|
-
if (record === undefined || record.revision !== revision) {
|
|
355
|
-
return { outcome: 'refused', code: 'stale', retryable: false, file: path };
|
|
356
|
-
}
|
|
357
|
-
if (!saved.some((draft) => draft.path === path))
|
|
358
|
-
saved.push({ path, parts: record.parts });
|
|
359
|
-
}
|
|
360
|
-
return committed(saver, locator, target, saved, new Set(saved.map(({ path }) => path)), writtenBy);
|
|
361
|
-
}
|
|
362
|
-
export async function saveParts(saver, locator, named, writtenBy) {
|
|
363
|
-
const { store } = saver;
|
|
364
|
-
const read = readSavedParts(named);
|
|
365
|
-
const target = await savedRoom(store, locator);
|
|
366
|
-
const saved = [];
|
|
367
|
-
const files = [];
|
|
368
|
-
const filed = read.filter(({ parts }) => parts.file !== undefined).map(({ path }) => path);
|
|
369
|
-
const visible = filed.length === 0 ? undefined : await visibleOf(store, locator, filed);
|
|
370
|
-
for (const { path, parts } of read) {
|
|
371
|
-
const file = parts.file === undefined
|
|
372
|
-
? undefined
|
|
373
|
-
: await heldFile(parts.file.blob, parts.file.base === undefined ? binaryBase(visible, path) : parts.file.base);
|
|
374
|
-
if (parts.file !== undefined && file !== undefined) {
|
|
375
|
-
files.push({ key: file.localId, blob: parts.file.blob });
|
|
376
|
-
}
|
|
377
|
-
saved.push({ path, parts: held(undefined, parts, file) });
|
|
378
|
-
}
|
|
379
|
-
return committed(saver, locator, target, saved, new Set(), writtenBy, files);
|
|
380
|
-
}
|
|
381
|
-
async function carriedDrafts(store, locator, saved, consumed) {
|
|
382
|
-
if (!saved.some(({ parts }) => parts.move !== undefined))
|
|
383
|
-
return new Map();
|
|
384
|
-
const holding = new Map((await store.drafts(locator)).map((record) => [record.path, record]));
|
|
385
|
-
return new Map(saved.flatMap(({ path, parts }) => {
|
|
386
|
-
const record = holding.get(path);
|
|
387
|
-
if (parts.move === undefined || record === undefined || consumed.has(path))
|
|
388
|
-
return [];
|
|
389
|
-
const to = moveTarget(parts.move);
|
|
390
|
-
if (to === path || holding.has(to))
|
|
391
|
-
return [];
|
|
392
|
-
return [[path, { ...record, path: to, revision: roomDraftRevision() }]];
|
|
393
|
-
}));
|
|
394
|
-
}
|
|
395
|
-
async function savingIn(store, replica, changes, extra, leaving = []) {
|
|
396
|
-
const record = replica.record;
|
|
89
|
+
async function unmodeledIn(runtime, record) {
|
|
90
|
+
const composition = compositionFor(runtime, record);
|
|
91
|
+
if (available(composition))
|
|
92
|
+
return false;
|
|
93
|
+
const local = await runtime.store.open();
|
|
94
|
+
const roots = record.declaration.typedSelection?.roots ?? [];
|
|
95
|
+
return local.typedWithin(record.key, 'visible', roots, false);
|
|
96
|
+
}
|
|
97
|
+
async function savingRoom(runtime, replica, changes, extra, drafted) {
|
|
98
|
+
const { store } = runtime;
|
|
99
|
+
const { record, queue, states } = replica;
|
|
397
100
|
const local = await store.open();
|
|
398
101
|
const paths = [
|
|
399
102
|
...extra,
|
|
400
103
|
...changes.flatMap((change) => change.kind === 'move' ? [change.path, change.to] : [change.path]),
|
|
401
104
|
];
|
|
402
|
-
const moved = [
|
|
403
|
-
...leaving,
|
|
404
|
-
...changes.flatMap((change) => (change.kind === 'move' ? [change.path] : [])),
|
|
405
|
-
];
|
|
105
|
+
const moved = changes.flatMap((change) => (change.kind === 'move' ? [change.path] : []));
|
|
406
106
|
const removed = changes.flatMap((change) => (change.kind === 'remove' ? [change.path] : []));
|
|
407
107
|
const referring = removed.length === 0
|
|
408
108
|
? []
|
|
409
109
|
: (await local.referrers(record.key, 'confirmed', removed)).map(({ path }) => path);
|
|
410
|
-
const
|
|
411
|
-
const { visible } = await replayed(store, record, confirmedLayer, replica.queue);
|
|
110
|
+
const confirmed = await sliceAt(store, record, 'confirmed', queue, [...paths, ...paths.flatMap(ancestors), ...referring], moved);
|
|
412
111
|
const composition = store.composed(record);
|
|
112
|
+
const changeable = withdrawable(states);
|
|
413
113
|
return {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
114
|
+
held: { replica, confirmed },
|
|
115
|
+
room: {
|
|
116
|
+
record,
|
|
117
|
+
rule: runtime.writeRule(record.declaration),
|
|
118
|
+
composition: compositionFor(runtime, record),
|
|
119
|
+
queue,
|
|
120
|
+
changeable: new Set(queue.filter(changeable).map(({ id }) => id)),
|
|
121
|
+
saved: new Map([...states].map(([id, state]) => [id, state.drafts])),
|
|
122
|
+
*replay(over) {
|
|
123
|
+
return (yield* replaying(confirmed, over, record.locator, composition)).visible;
|
|
124
|
+
},
|
|
125
|
+
folders: new Set(await local.folders(record.key, paths)),
|
|
126
|
+
modelled: !(await unmodeledIn(runtime, record)),
|
|
127
|
+
drafts: new Map(drafted.length === 0
|
|
128
|
+
? []
|
|
129
|
+
: (await store.drafts(record.locator, drafted)).map((held) => [held.path, held])),
|
|
130
|
+
validate: validatorOf(runtime, record, confirmed),
|
|
420
131
|
},
|
|
421
132
|
};
|
|
422
133
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
const
|
|
428
|
-
const
|
|
429
|
-
|
|
134
|
+
function targetsOf(saved) {
|
|
135
|
+
return saved.flatMap(({ parts }) => (parts.move === undefined ? [] : [moveTarget(parts.move)]));
|
|
136
|
+
}
|
|
137
|
+
async function loaded(runtime, locator, replica, saved, consumed, files, writtenBy) {
|
|
138
|
+
const { store, localSaves } = runtime;
|
|
139
|
+
const into = destination(locator, saved);
|
|
140
|
+
const target = into === null ? undefined : await savedRoom(runtime, into);
|
|
141
|
+
const paths = saved.map(({ path }) => path);
|
|
142
|
+
const targets = targetsOf(saved);
|
|
143
|
+
const parts = saved.map(({ path, parts: held }) => changesOf(path, held));
|
|
144
|
+
const source = target === undefined
|
|
145
|
+
? await savingRoom(runtime, replica, parts.flatMap(({ changes }) => changes), [...paths, ...parts.flatMap(({ bases }) => bases.map(({ path }) => path))], targets.length === 0 ? [] : [...paths, ...targets])
|
|
146
|
+
: await savingRoom(runtime, replica, paths.map((path) => ({ kind: 'remove', path })), paths, paths);
|
|
147
|
+
const arriving = target === undefined ? undefined : await savingRoom(runtime, target, [], targets, targets);
|
|
148
|
+
const { providerLimits } = await (await store.open()).facts();
|
|
149
|
+
const plan = await planSave({ saved, consumed, writtenBy, local: localSaves, providerLimits }, source.room, arriving?.room);
|
|
150
|
+
return append(runtime, {
|
|
151
|
+
plan,
|
|
152
|
+
files,
|
|
153
|
+
held: new Map([source, ...(arriving === undefined ? [] : [arriving])].map(({ room, held }) => [
|
|
154
|
+
room.record.locator,
|
|
155
|
+
held,
|
|
156
|
+
])),
|
|
157
|
+
after: () => pendingShown(runtime),
|
|
158
|
+
});
|
|
430
159
|
}
|
|
431
160
|
function textDiffers(left, right) {
|
|
432
161
|
return ((left?.content ?? undefined) !== (right?.content ?? undefined) ||
|
|
@@ -450,8 +179,7 @@ function lookupOf(local, key, view) {
|
|
|
450
179
|
function declaredOf(entry) {
|
|
451
180
|
if (entry.binary !== null)
|
|
452
181
|
return { type: undefined, binary: true };
|
|
453
|
-
|
|
454
|
-
return { type: read.ok ? (read.frontmatter.type ?? 'any') : undefined, binary: false };
|
|
182
|
+
return { type: textType(parse(entry.content ?? '')), binary: false };
|
|
455
183
|
}
|
|
456
184
|
function overriddenLookup(lookup, overrides) {
|
|
457
185
|
return async (paths) => {
|
|
@@ -468,9 +196,9 @@ function overriddenLookup(lookup, overrides) {
|
|
|
468
196
|
return known;
|
|
469
197
|
};
|
|
470
198
|
}
|
|
471
|
-
function validatorOf(
|
|
199
|
+
function validatorOf(runtime, record, confirmedLayer) {
|
|
472
200
|
return async (resulting, touched) => {
|
|
473
|
-
const local = await store.open();
|
|
201
|
+
const local = await runtime.store.open();
|
|
474
202
|
const changes = [];
|
|
475
203
|
const overrides = new Map();
|
|
476
204
|
const pace = paced();
|
|
@@ -484,7 +212,7 @@ function validatorOf({ store, compositionFor }, record, confirmedLayer) {
|
|
|
484
212
|
if (textOf(was) !== textOf(now))
|
|
485
213
|
changes.push({ path, was: textOf(was), now: textOf(now) });
|
|
486
214
|
}
|
|
487
|
-
return validatedNeighbourhood(changes, compositionFor(record), {
|
|
215
|
+
return validatedNeighbourhood(changes, compositionFor(runtime, record), {
|
|
488
216
|
lookup: overriddenLookup(lookupOf(local, record.key, 'confirmed'), overrides),
|
|
489
217
|
texts: textsOf(local, record.key, 'confirmed'),
|
|
490
218
|
referrers: referrersOf(local, record.key, 'confirmed'),
|
|
@@ -502,305 +230,6 @@ function referrersOf(local, key, view) {
|
|
|
502
230
|
...new Set((await local.referrers(key, view, targets)).map(({ path }) => path)),
|
|
503
231
|
];
|
|
504
232
|
}
|
|
505
|
-
async function sideOf(saver, locator, replica, saving) {
|
|
506
|
-
const record = replica.record;
|
|
507
|
-
return {
|
|
508
|
-
room: locator,
|
|
509
|
-
handle: record.handle,
|
|
510
|
-
baseRoomVersion: record.roomVersion,
|
|
511
|
-
composition: saver.compositionFor(record),
|
|
512
|
-
operations: record.operations,
|
|
513
|
-
providerOwnedPaths: record.providerOwnedPaths,
|
|
514
|
-
visible: saving.visible,
|
|
515
|
-
folders: saving.folders,
|
|
516
|
-
queued: queuedDocuments(replica.queue),
|
|
517
|
-
unmodeled: await unmodeledIn(saver, record),
|
|
518
|
-
validate: validatorOf(saver, record, saving.confirmed),
|
|
519
|
-
};
|
|
520
|
-
}
|
|
521
|
-
function movedInto(locator, saved) {
|
|
522
|
-
const into = saved.find(({ parts }) => parts.move?.room !== undefined && parts.move.room !== locator);
|
|
523
|
-
return into?.parts.move?.room ?? null;
|
|
524
|
-
}
|
|
525
|
-
async function saveCommit(store, local, files, rooms) {
|
|
526
|
-
const commits = [];
|
|
527
|
-
for (const { locator, saving, before, queue } of rooms) {
|
|
528
|
-
const replayedLayer = await replayed(store, saving.record, saving.confirmed, queue);
|
|
529
|
-
const diff = await overlayDiff(saving.record, before, replayedLayer.visible, replayedLayer.touched);
|
|
530
|
-
commits.push({
|
|
531
|
-
locator,
|
|
532
|
-
key: saving.record.key,
|
|
533
|
-
roots: saving.record.typedSelection?.roots ?? [],
|
|
534
|
-
composed: composedOf(store.composed(saving.record)),
|
|
535
|
-
overlay: diff.overlay,
|
|
536
|
-
});
|
|
537
|
-
}
|
|
538
|
-
try {
|
|
539
|
-
return await local.commit({
|
|
540
|
-
rooms: commits,
|
|
541
|
-
writes: rooms.map(({ write }) => write),
|
|
542
|
-
files,
|
|
543
|
-
whole: true,
|
|
544
|
-
});
|
|
545
|
-
}
|
|
546
|
-
catch {
|
|
547
|
-
return null;
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
async function committedMove(saver, locator, destination, { local, replica }, saved, consumed, writtenBy, files) {
|
|
551
|
-
const { store, localSaves, publishChangeSet } = saver;
|
|
552
|
-
const mixed = saved.find(({ parts }) => Object.keys(parts).length !== 1 || parts.move?.room !== destination);
|
|
553
|
-
if (mixed !== undefined) {
|
|
554
|
-
return { outcome: 'refused', code: 'mixed-room-move', retryable: false, file: mixed.path };
|
|
555
|
-
}
|
|
556
|
-
const moves = saved.map(({ path, parts }) => ({ path, to: moveTarget(parts.move) }));
|
|
557
|
-
const sourceSaving = await savingIn(store, replica, moves.map(({ path }) => ({ kind: 'remove', path })), saved.map(({ path }) => path), moves.map(({ path }) => path));
|
|
558
|
-
const changed = saved.flatMap(({ path, parts }) => changedParts(path, parts, sourceSaving.visible));
|
|
559
|
-
if (changed.length > 0)
|
|
560
|
-
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
561
|
-
const target = await store.replica(destination);
|
|
562
|
-
if (target === undefined || store.fences.has(destination)) {
|
|
563
|
-
throw runtimeError('unknown-room', `Unknown Room ${destination}`);
|
|
564
|
-
}
|
|
565
|
-
const arriving = await savingIn(store, target, [], moves.map(({ to }) => to));
|
|
566
|
-
const built = await buildMove({
|
|
567
|
-
source: await sideOf(saver, locator, replica, sourceSaving),
|
|
568
|
-
destination: await sideOf(saver, destination, target, arriving),
|
|
569
|
-
moves,
|
|
570
|
-
providerLimits: (await local.facts()).providerLimits,
|
|
571
|
-
local: localSaves,
|
|
572
|
-
...(writtenBy === undefined ? {} : { writtenBy }),
|
|
573
|
-
});
|
|
574
|
-
if (built.kind === 'refused')
|
|
575
|
-
return { outcome: 'refused', ...built.refusal };
|
|
576
|
-
const id = built.leaving.id;
|
|
577
|
-
const leavingDrafts = new Map((await store.drafts(locator, saved.map(({ path }) => path))).map((record) => [record.path, record]));
|
|
578
|
-
const arrivingDrafts = new Set((await store.drafts(destination, moves.map(({ to }) => to))).map(({ path }) => path));
|
|
579
|
-
const carrying = saved.flatMap(({ path, parts }) => {
|
|
580
|
-
const record = leavingDrafts.get(path);
|
|
581
|
-
const to = moveTarget(parts.move);
|
|
582
|
-
if (record === undefined || consumed.has(path) || arrivingDrafts.has(to)) {
|
|
583
|
-
return [];
|
|
584
|
-
}
|
|
585
|
-
const moved = {
|
|
586
|
-
...record,
|
|
587
|
-
locator: destination,
|
|
588
|
-
path: to,
|
|
589
|
-
revision: roomDraftRevision(),
|
|
590
|
-
};
|
|
591
|
-
return [{ from: path, record: moved }];
|
|
592
|
-
});
|
|
593
|
-
const carried = carrying.map(({ record }) => record);
|
|
594
|
-
const marked = carried.map(({ path }) => ({ path, parts: {} }));
|
|
595
|
-
const left = [...consumed, ...carrying.map(({ from }) => from)];
|
|
596
|
-
const leavingQueue = [...replica.queue, built.leaving];
|
|
597
|
-
const arrivingQueue = [...target.queue, built.arriving];
|
|
598
|
-
const answer = await saveCommit(store, local, files, [
|
|
599
|
-
{
|
|
600
|
-
locator,
|
|
601
|
-
saving: sourceSaving,
|
|
602
|
-
before: replica,
|
|
603
|
-
queue: leavingQueue,
|
|
604
|
-
write: {
|
|
605
|
-
locator,
|
|
606
|
-
queue: leavingQueue,
|
|
607
|
-
stored: replica.queue,
|
|
608
|
-
saved: new Map([[id, saved]]),
|
|
609
|
-
consumedRoomDrafts: left,
|
|
610
|
-
},
|
|
611
|
-
},
|
|
612
|
-
{
|
|
613
|
-
locator: destination,
|
|
614
|
-
saving: arriving,
|
|
615
|
-
before: target,
|
|
616
|
-
queue: arrivingQueue,
|
|
617
|
-
write: {
|
|
618
|
-
locator: destination,
|
|
619
|
-
queue: arrivingQueue,
|
|
620
|
-
stored: target.queue,
|
|
621
|
-
saved: new Map([[id, marked]]),
|
|
622
|
-
roomDrafts: carried,
|
|
623
|
-
},
|
|
624
|
-
},
|
|
625
|
-
]);
|
|
626
|
-
if (answer === null)
|
|
627
|
-
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
628
|
-
if (answer.fenced !== null) {
|
|
629
|
-
throw runtimeError('unknown-room', `Unknown Room ${answer.fenced}`);
|
|
630
|
-
}
|
|
631
|
-
if (store.fences.has(locator) || store.fences.has(destination)) {
|
|
632
|
-
throw runtimeError('unknown-room', `Unknown Room ${locator}`);
|
|
633
|
-
}
|
|
634
|
-
preparedAfter(saver, locator, answer);
|
|
635
|
-
preparedAfter(saver, destination, answer);
|
|
636
|
-
const shownNow = publishChangeSet(mergeChangeSets([
|
|
637
|
-
changeSet(locator, {
|
|
638
|
-
documents: answer.changed[locator] ?? [],
|
|
639
|
-
queue: true,
|
|
640
|
-
drafts: left,
|
|
641
|
-
}),
|
|
642
|
-
changeSet(destination, {
|
|
643
|
-
documents: answer.changed[destination] ?? [],
|
|
644
|
-
queue: true,
|
|
645
|
-
drafts: carried.map(({ path }) => path),
|
|
646
|
-
}),
|
|
647
|
-
]));
|
|
648
|
-
await Promise.all([shownNow, queuedSave(saver, locator)]);
|
|
649
|
-
return { outcome: 'saved', identifier: id };
|
|
650
|
-
}
|
|
651
|
-
function queuedSave({ pendingShown, submitLater }, locator) {
|
|
652
|
-
const shownNow = pendingShown();
|
|
653
|
-
submitLater(locator);
|
|
654
|
-
return shownNow;
|
|
655
|
-
}
|
|
656
|
-
async function committed(saver, locator, target, saved, consumed, writtenBy, files = []) {
|
|
657
|
-
const { store, localSaves, publishChangeSet, compositionFor } = saver;
|
|
658
|
-
if (saver.full())
|
|
659
|
-
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
660
|
-
const into = movedInto(locator, saved);
|
|
661
|
-
if (into !== null) {
|
|
662
|
-
return committedMove(saver, locator, into, target, saved, consumed, writtenBy, files);
|
|
663
|
-
}
|
|
664
|
-
const { local, replica } = target;
|
|
665
|
-
const { providerLimits } = await local.facts();
|
|
666
|
-
const parts = saved.map(({ path, parts: held }) => changesOf(path, held));
|
|
667
|
-
const changes = parts.flatMap(({ changes: made }) => made);
|
|
668
|
-
const saving = await savingIn(store, replica, changes, [
|
|
669
|
-
...saved.map(({ path }) => path),
|
|
670
|
-
...parts.flatMap(({ bases }) => bases.map(({ path }) => path)),
|
|
671
|
-
]);
|
|
672
|
-
const changed = saved.flatMap(({ path, parts: held }) => changedParts(path, held, saving.visible));
|
|
673
|
-
if (changed.length > 0)
|
|
674
|
-
return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
|
|
675
|
-
const requeue = await stepped(requeuing(replica.queue, changes, saving.visible, withdrawable(replica.states), saving.visibleOver));
|
|
676
|
-
if (requeue.kind === 'refused')
|
|
677
|
-
return { outcome: 'refused', ...requeue.refusal };
|
|
678
|
-
const foreign = [...requeue.withdrawn.keys()].some((origin) => {
|
|
679
|
-
const writer = replica.queue.find((batch) => batch.id === origin)?.writtenBy;
|
|
680
|
-
return writer?.app !== writtenBy?.app || writer?.release !== writtenBy?.release;
|
|
681
|
-
});
|
|
682
|
-
if (foreign) {
|
|
683
|
-
return {
|
|
684
|
-
outcome: 'refused',
|
|
685
|
-
code: 'unknown-revision',
|
|
686
|
-
retryable: false,
|
|
687
|
-
file: [...requeue.queued].at(-1),
|
|
688
|
-
};
|
|
689
|
-
}
|
|
690
|
-
const withdrawing = requeue.withdrawn.size > 0;
|
|
691
|
-
const input = {
|
|
692
|
-
local: localSaves,
|
|
693
|
-
baseRoomVersion: replica.record.roomVersion,
|
|
694
|
-
composition: compositionFor(replica.record),
|
|
695
|
-
operations: replica.record.operations,
|
|
696
|
-
providerOwnedPaths: replica.record.providerOwnedPaths,
|
|
697
|
-
providerLimits,
|
|
698
|
-
handle: replica.record.handle,
|
|
699
|
-
visible: saving.visible,
|
|
700
|
-
folders: saving.folders,
|
|
701
|
-
changes,
|
|
702
|
-
bases: parts.flatMap(({ bases }) => bases),
|
|
703
|
-
queued: queuedDocuments(replica.queue),
|
|
704
|
-
writtenBy,
|
|
705
|
-
unmodeled: await unmodeledIn(saver, replica.record),
|
|
706
|
-
validate: validatorOf(saver, replica.record, saving.confirmed),
|
|
707
|
-
};
|
|
708
|
-
const checked = withdrawing
|
|
709
|
-
? await buildBatch({ ...input, queued: new Set([...input.queued, ...requeue.queued]) })
|
|
710
|
-
: null;
|
|
711
|
-
if (checked?.kind === 'refused')
|
|
712
|
-
return { outcome: 'refused', ...checked.refusal };
|
|
713
|
-
const owners = [...requeue.owners].flatMap((origin) => {
|
|
714
|
-
const batch = requeue.withdrawn.get(origin);
|
|
715
|
-
return batch === undefined || batch === null ? [] : [{ origin, batch }];
|
|
716
|
-
});
|
|
717
|
-
if (owners.length > 1) {
|
|
718
|
-
return {
|
|
719
|
-
outcome: 'refused',
|
|
720
|
-
code: 'unknown-revision',
|
|
721
|
-
retryable: false,
|
|
722
|
-
file: [...requeue.queued].at(-1),
|
|
723
|
-
};
|
|
724
|
-
}
|
|
725
|
-
const built = !withdrawing
|
|
726
|
-
? await buildBatch(input)
|
|
727
|
-
: requeue.changes.length === 0
|
|
728
|
-
? null
|
|
729
|
-
: await buildBatch({
|
|
730
|
-
...input,
|
|
731
|
-
visible: await stepped(saving.visibleOver(requeue.queue)),
|
|
732
|
-
changes: requeue.changes,
|
|
733
|
-
queued: queuedDocuments(requeue.queue),
|
|
734
|
-
});
|
|
735
|
-
if (built?.kind === 'refused')
|
|
736
|
-
return { outcome: 'refused', ...built.refusal };
|
|
737
|
-
const [owner] = owners;
|
|
738
|
-
const join = built === null || owner === undefined ? null : joined(requeue.queue, owner.batch, built.batch);
|
|
739
|
-
if (join?.kind === 'refused')
|
|
740
|
-
return { outcome: 'refused', ...join.refusal };
|
|
741
|
-
const withdrawn = new Map(requeue.withdrawn);
|
|
742
|
-
if (join !== null && owner !== undefined)
|
|
743
|
-
withdrawn.set(owner.origin, join.batch);
|
|
744
|
-
const drafted = new Map();
|
|
745
|
-
for (const [origin, replaced] of withdrawn) {
|
|
746
|
-
if (replaced === null)
|
|
747
|
-
continue;
|
|
748
|
-
const carried = withdrawnDrafts(replica.states.get(origin)?.drafts ?? [], changes);
|
|
749
|
-
drafted.set(replaced.id, replaced === join?.batch
|
|
750
|
-
? [
|
|
751
|
-
...carried.map((held) => joinedDraft(held, saved)),
|
|
752
|
-
...saved.filter((draft) => !carried.some((held) => held.path === draft.path)),
|
|
753
|
-
]
|
|
754
|
-
: carried);
|
|
755
|
-
}
|
|
756
|
-
if (built !== null && join === null)
|
|
757
|
-
drafted.set(built.batch.id, saved);
|
|
758
|
-
const batches = join !== null ? join.queue : built === null ? requeue.queue : [...requeue.queue, built.batch];
|
|
759
|
-
for (const replaced of withdrawn.values()) {
|
|
760
|
-
if (replaced === null || !batches.includes(replaced))
|
|
761
|
-
continue;
|
|
762
|
-
const limited = await stepped(limiting(batches, replaced, providerLimits, saving.visibleOver));
|
|
763
|
-
if (limited !== null)
|
|
764
|
-
return { outcome: 'refused', ...limited };
|
|
765
|
-
}
|
|
766
|
-
const carried = await carriedDrafts(store, locator, saved, consumed);
|
|
767
|
-
const left = [...consumed, ...carried.keys()];
|
|
768
|
-
const arrived = [...carried.values()];
|
|
769
|
-
const answer = await saveCommit(store, local, files, [
|
|
770
|
-
{
|
|
771
|
-
locator,
|
|
772
|
-
saving,
|
|
773
|
-
before: replica,
|
|
774
|
-
queue: batches,
|
|
775
|
-
write: {
|
|
776
|
-
locator,
|
|
777
|
-
queue: batches,
|
|
778
|
-
stored: replica.queue,
|
|
779
|
-
saved: drafted,
|
|
780
|
-
consumedRoomDrafts: left,
|
|
781
|
-
roomDrafts: arrived,
|
|
782
|
-
},
|
|
783
|
-
},
|
|
784
|
-
]);
|
|
785
|
-
if (answer === null)
|
|
786
|
-
return { outcome: 'refused', code: 'durable-write', retryable: true };
|
|
787
|
-
if (answer.fenced !== null) {
|
|
788
|
-
throw runtimeError('unknown-room', `Unknown Room ${answer.fenced}`);
|
|
789
|
-
}
|
|
790
|
-
if (store.fences.has(locator))
|
|
791
|
-
throw runtimeError('unknown-room', `Unknown Room ${locator}`);
|
|
792
|
-
preparedAfter(saver, locator, answer);
|
|
793
|
-
const shownNow = publishChangeSet(changeSet(locator, {
|
|
794
|
-
documents: answer.changed[locator] ?? [],
|
|
795
|
-
queue: true,
|
|
796
|
-
drafts: [...left, ...arrived.map(({ path }) => path)],
|
|
797
|
-
}));
|
|
798
|
-
await Promise.all([shownNow, queuedSave(saver, locator)]);
|
|
799
|
-
const identifier = join?.batch.id ??
|
|
800
|
-
built?.batch.id ??
|
|
801
|
-
[...requeue.withdrawn].map(([origin, replaced]) => replaced?.id ?? origin).at(-1);
|
|
802
|
-
return { outcome: 'saved', identifier };
|
|
803
|
-
}
|
|
804
233
|
async function queuedFacts(store, locator, path, localId) {
|
|
805
234
|
for (const batch of (await store.replica(locator))?.queue ?? []) {
|
|
806
235
|
const change = batch.changes[queuedFile(batch, path, localId)];
|
|
@@ -816,55 +245,53 @@ export function batchFile(store, batch) {
|
|
|
816
245
|
return (await store.open()).file(localId, change?.kind === 'binary' ? change.mediaType : '');
|
|
817
246
|
};
|
|
818
247
|
}
|
|
819
|
-
async function transferBinary(
|
|
820
|
-
const { store,
|
|
821
|
-
|
|
248
|
+
async function transferBinary(runtime, request, transfer, answer) {
|
|
249
|
+
const { store, socket, presence } = runtime;
|
|
250
|
+
const { session } = socket;
|
|
251
|
+
if (socket.closed() || session.now().refusal !== null)
|
|
822
252
|
return UNCERTAIN;
|
|
823
|
-
await
|
|
824
|
-
if (!
|
|
253
|
+
await socket.firstDiscovery;
|
|
254
|
+
if (!session.now().roomsNamed)
|
|
825
255
|
return UNCERTAIN;
|
|
826
256
|
try {
|
|
827
|
-
await
|
|
257
|
+
await socket.authorize();
|
|
828
258
|
}
|
|
829
259
|
catch {
|
|
830
260
|
return UNCERTAIN;
|
|
831
261
|
}
|
|
832
|
-
const authorization =
|
|
833
|
-
if (closed
|
|
262
|
+
const { closed, authorization, refusal } = session.now();
|
|
263
|
+
if (closed || authorization === undefined || refusal !== null)
|
|
834
264
|
return UNCERTAIN;
|
|
835
|
-
const since =
|
|
836
|
-
journal.record(`transfer binary ${request.locator} ${request.path}`);
|
|
265
|
+
const since = presence.losses();
|
|
837
266
|
try {
|
|
838
267
|
const result = await transfer(authorization);
|
|
839
268
|
const settled = result;
|
|
840
|
-
|
|
841
|
-
if (store.fences.since(request.locator, since))
|
|
269
|
+
if (request.locator !== null && presence.lostSince(request.locator, since))
|
|
842
270
|
return failure({ kind: 'room-not-readable', locator: request.locator });
|
|
843
271
|
if (settled.kind === 'scoped-refusal') {
|
|
844
272
|
const room = await store.record(settled.locator);
|
|
845
273
|
if (room !== undefined)
|
|
846
|
-
await
|
|
274
|
+
await scoped(runtime, settled, room.locator, room.declaration);
|
|
847
275
|
}
|
|
848
276
|
if (settled.kind === 'revoked-authorization')
|
|
849
|
-
await
|
|
277
|
+
await refuse(runtime, 'revoked-authorization');
|
|
850
278
|
return answer(result);
|
|
851
279
|
}
|
|
852
280
|
catch {
|
|
853
|
-
journal.record('outcome binary transport-uncertainty');
|
|
854
281
|
return UNCERTAIN;
|
|
855
282
|
}
|
|
856
283
|
}
|
|
857
|
-
function fetchBinary(
|
|
858
|
-
const { store, socket } =
|
|
859
|
-
return transferBinary(
|
|
284
|
+
function fetchBinary(runtime, request, name) {
|
|
285
|
+
const { store, socket } = runtime;
|
|
286
|
+
return transferBinary(runtime, request, (authorization) => store.opened.fetch(name, () => socket.binary(authorization, request)), binaryAnswer);
|
|
860
287
|
}
|
|
861
|
-
async function readBinary(
|
|
862
|
-
if (
|
|
288
|
+
async function readBinary(runtime, request, name) {
|
|
289
|
+
if (runtime.socket.closed())
|
|
863
290
|
return UNCERTAIN;
|
|
864
|
-
const held = await
|
|
291
|
+
const held = await runtime.store.opened.find(name);
|
|
865
292
|
if (held !== null)
|
|
866
293
|
return held;
|
|
867
|
-
return fetchBinary(
|
|
294
|
+
return fetchBinary(runtime, request, name);
|
|
868
295
|
}
|
|
869
296
|
async function binaryPath(store, locator, path, revision) {
|
|
870
297
|
if (revision !== undefined)
|
|
@@ -907,8 +334,8 @@ async function binaryPath(store, locator, path, revision) {
|
|
|
907
334
|
revision: record.revision,
|
|
908
335
|
};
|
|
909
336
|
}
|
|
910
|
-
async function askedBinary({ store,
|
|
911
|
-
await ready;
|
|
337
|
+
async function askedBinary({ store, socket }, locator, path, revision) {
|
|
338
|
+
await socket.ready;
|
|
912
339
|
const drafted = (await store.draft(locator, path))?.parts.file;
|
|
913
340
|
return drafted !== undefined && drafted.localId === revision
|
|
914
341
|
? { kind: 'pending', localId: revision }
|
|
@@ -925,75 +352,86 @@ async function pendingBinary(store, locator, path, localId) {
|
|
|
925
352
|
mediaType: facts.mediaType,
|
|
926
353
|
};
|
|
927
354
|
}
|
|
928
|
-
async function readBinaryPath(
|
|
929
|
-
const asked = await askedBinary(
|
|
355
|
+
async function readBinaryPath(runtime, locator, path, revision) {
|
|
356
|
+
const asked = await askedBinary(runtime, locator, path, revision);
|
|
930
357
|
if (asked === null)
|
|
931
358
|
return absentBinary(path);
|
|
932
359
|
if (asked.kind === 'pending')
|
|
933
|
-
return pendingBinary(
|
|
934
|
-
return readBinary(
|
|
360
|
+
return pendingBinary(runtime.store, locator, path, asked.localId);
|
|
361
|
+
return readBinary(runtime, asked, { locator, path: asked.path, revision: asked.revision });
|
|
935
362
|
}
|
|
936
363
|
async function heldMediaType(store, locator, path) {
|
|
937
364
|
const replica = await store.record(locator);
|
|
938
365
|
const [held] = replica === undefined ? [] : await (await store.open()).entries(replica.key, 'visible', [path]);
|
|
939
366
|
return held === undefined ? store.attached.of(locator, path)?.mediaType : held.binary?.mediaType;
|
|
940
367
|
}
|
|
941
|
-
function linkOf(
|
|
942
|
-
return transferBinary(
|
|
368
|
+
function linkOf(runtime, asked, held) {
|
|
369
|
+
return transferBinary(runtime, asked, (authorization) => runtime.socket.link(authorization, asked), (result) => binaryLinkAnswer(result, held));
|
|
943
370
|
}
|
|
944
|
-
async function linkBinaryPath(
|
|
945
|
-
const { store } =
|
|
946
|
-
await
|
|
371
|
+
async function linkBinaryPath(runtime, locator, path, revision) {
|
|
372
|
+
const { store, socket } = runtime;
|
|
373
|
+
await socket.ready;
|
|
947
374
|
const asked = await binaryPath(store, locator, path, revision);
|
|
948
375
|
if (asked === null || asked.kind === 'pending')
|
|
949
376
|
return absentBinary(path);
|
|
950
|
-
return linkOf(
|
|
377
|
+
return linkOf(runtime, asked, revision === undefined ? await heldMediaType(store, locator, path) : undefined);
|
|
951
378
|
}
|
|
952
|
-
async function showBinaryPath(
|
|
953
|
-
const { store } =
|
|
954
|
-
const asked = await askedBinary(
|
|
379
|
+
async function showBinaryPath(runtime, locator, path, revision) {
|
|
380
|
+
const { store } = runtime;
|
|
381
|
+
const asked = await askedBinary(runtime, locator, path, revision);
|
|
955
382
|
if (asked === null)
|
|
956
383
|
return absentBinary(path);
|
|
957
384
|
if (asked.kind === 'pending')
|
|
958
385
|
return pendingBinary(store, locator, path, asked.localId);
|
|
959
|
-
if (
|
|
386
|
+
if (runtime.socket.closed())
|
|
960
387
|
return UNCERTAIN;
|
|
961
388
|
const name = { locator, path: asked.path, revision: asked.revision };
|
|
962
389
|
const kept = await store.opened.find(name);
|
|
963
390
|
if (kept !== null)
|
|
964
391
|
return kept;
|
|
965
392
|
const mediaType = await heldMediaType(store, locator, path);
|
|
966
|
-
if (mediaType
|
|
967
|
-
return fetchBinary(
|
|
968
|
-
return linkOf(
|
|
969
|
-
}
|
|
970
|
-
async function
|
|
971
|
-
const { store, socket } =
|
|
972
|
-
await
|
|
973
|
-
await
|
|
974
|
-
if (
|
|
975
|
-
!
|
|
976
|
-
(await
|
|
977
|
-
|
|
393
|
+
if (mediaType !== undefined && isImageType(mediaType))
|
|
394
|
+
return fetchBinary(runtime, asked, name);
|
|
395
|
+
return linkOf(runtime, asked, revision === undefined ? mediaType : undefined);
|
|
396
|
+
}
|
|
397
|
+
async function attachFile(runtime, locator, file, answer) {
|
|
398
|
+
const { store, socket } = runtime;
|
|
399
|
+
await socket.ready;
|
|
400
|
+
await socket.firstDiscovery;
|
|
401
|
+
if (socket.closed() ||
|
|
402
|
+
!socket.session.now().roomsNamed ||
|
|
403
|
+
(await connectionNow(runtime)) === 'unreachable' ||
|
|
404
|
+
socket.session.now().refusal !== null)
|
|
978
405
|
return OFFLINE;
|
|
979
406
|
const request = await attachedFile(locator, file);
|
|
980
|
-
return transferBinary(
|
|
407
|
+
return transferBinary(runtime, { locator }, async (held) => {
|
|
981
408
|
const result = await socket.attach(held, request);
|
|
982
|
-
if (result.kind === 'attached') {
|
|
409
|
+
if (result.kind === 'attached' && locator !== null) {
|
|
983
410
|
store.attached.hold(locator, result.path, {
|
|
984
411
|
revision: result.revision,
|
|
985
412
|
mediaType: request.mediaType,
|
|
986
413
|
});
|
|
987
414
|
}
|
|
988
415
|
return result;
|
|
989
|
-
},
|
|
416
|
+
}, answer);
|
|
417
|
+
}
|
|
418
|
+
async function keyedAttachment(runtime, link) {
|
|
419
|
+
await runtime.socket.ready;
|
|
420
|
+
const asked = { kind: 'key', link };
|
|
421
|
+
return transferBinary(runtime, { locator: null }, (authorization) => runtime.socket.link(authorization, asked), (result) => binaryLinkAnswer(result));
|
|
422
|
+
}
|
|
423
|
+
export function conversationFiles(runtime) {
|
|
424
|
+
return {
|
|
425
|
+
attach: (file) => attachFile(runtime, null, file, linkedAnswer),
|
|
426
|
+
attachment: (link) => keyedAttachment(runtime, link),
|
|
427
|
+
};
|
|
990
428
|
}
|
|
991
|
-
export function binaryFileSurface(
|
|
429
|
+
export function binaryFileSurface(runtime) {
|
|
992
430
|
return {
|
|
993
|
-
part:
|
|
994
|
-
read: (room, path, revision) => readBinaryPath(
|
|
995
|
-
link: (room, path, revision) => linkBinaryPath(
|
|
996
|
-
show: (room, path, revision) => showBinaryPath(
|
|
997
|
-
attach: (room, file) =>
|
|
431
|
+
part: runtime.store.part,
|
|
432
|
+
read: (room, path, revision) => readBinaryPath(runtime, room, path, revision),
|
|
433
|
+
link: (room, path, revision) => linkBinaryPath(runtime, room, path, revision),
|
|
434
|
+
show: (room, path, revision) => showBinaryPath(runtime, room, path, revision),
|
|
435
|
+
attach: (room, file) => attachFile(runtime, room, file, attachAnswer),
|
|
998
436
|
};
|
|
999
437
|
}
|