@mostra/runtime 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +39 -2
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +236 -0
- package/dist/bin.js.map +1 -0
- package/dist/builder/child/main.d.ts +1 -0
- package/dist/builder/child/main.js +1408 -0
- package/dist/builder/child/main.js.map +1 -0
- package/dist/child-xrHmytks.js +49 -0
- package/dist/child-xrHmytks.js.map +1 -0
- package/dist/deps/child/install.d.ts +1 -0
- package/dist/deps/child/install.js +33 -0
- package/dist/deps/child/install.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/instance-D7_UVdoN.js +15752 -0
- package/dist/instance-D7_UVdoN.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -3
- package/src/actions/index.ts +622 -0
- package/src/addressing/index.ts +281 -0
- package/src/bin.ts +240 -0
- package/src/builder/child/dev.ts +383 -0
- package/src/builder/child/fields.ts +363 -0
- package/src/builder/child/main.ts +47 -0
- package/src/builder/child/plugins.ts +195 -0
- package/src/builder/child/scan.ts +459 -0
- package/src/builder/child/spa.ts +348 -0
- package/src/builder/coverage.ts +72 -0
- package/src/builder/index.ts +406 -0
- package/src/builder/types.ts +180 -0
- package/src/deps/child/install.ts +30 -0
- package/src/deps/index.ts +918 -0
- package/src/deps/installer.ts +321 -0
- package/src/deps/lock.ts +82 -0
- package/src/deps/scan.ts +266 -0
- package/src/deps/specifier.ts +64 -0
- package/src/deps/warm.ts +57 -0
- package/src/dev/index.ts +518 -0
- package/src/faults.ts +65 -0
- package/src/gate.ts +55 -0
- package/src/gateway/actions.ts +268 -0
- package/src/gateway/browser.ts +650 -0
- package/src/gateway/capabilities.ts +739 -0
- package/src/gateway/controls.ts +91 -0
- package/src/gateway/dev.ts +119 -0
- package/src/gateway/http.ts +229 -0
- package/src/gateway/index.ts +1209 -0
- package/src/gateway/integrity.ts +117 -0
- package/src/gateway/listener.ts +217 -0
- package/src/gateway/routes.ts +197 -0
- package/src/gateway/serve.ts +138 -0
- package/src/gc/index.ts +451 -0
- package/src/home/acquire.ts +276 -0
- package/src/home/index.ts +16 -0
- package/src/home/paths.ts +192 -0
- package/src/home/platform.ts +34 -0
- package/src/home/record.ts +76 -0
- package/src/home/settings.ts +113 -0
- package/src/index.ts +30 -0
- package/src/instance/index.ts +942 -0
- package/src/interaction/ephemeral.ts +300 -0
- package/src/interaction/events.ts +355 -0
- package/src/interaction/fields.ts +392 -0
- package/src/interaction/index.ts +1009 -0
- package/src/interaction/outbox.ts +493 -0
- package/src/interaction/policy.ts +26 -0
- package/src/interaction/results.ts +341 -0
- package/src/interaction/runs.ts +313 -0
- package/src/interaction/signals.ts +120 -0
- package/src/interaction/submissions.ts +585 -0
- package/src/interaction/subscriptions.ts +258 -0
- package/src/interaction/viewers.ts +173 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +583 -0
- package/src/ledger/requests.ts +105 -0
- package/src/ledger/types.ts +28 -0
- package/src/live/index.ts +573 -0
- package/src/log.ts +316 -0
- package/src/processes/child.ts +67 -0
- package/src/processes/environment.ts +64 -0
- package/src/processes/identity.ts +191 -0
- package/src/processes/index.ts +19 -0
- package/src/processes/manager.ts +543 -0
- package/src/protocol-server/client.ts +94 -0
- package/src/protocol-server/index.ts +91 -0
- package/src/protocol-server/server.ts +509 -0
- package/src/protocol-server/streams.ts +81 -0
- package/src/publish/checks.ts +92 -0
- package/src/publish/digest.ts +87 -0
- package/src/publish/index.ts +23 -0
- package/src/publish/manifest.ts +409 -0
- package/src/publish/methods.ts +96 -0
- package/src/publish/publisher.ts +913 -0
- package/src/publish/source.ts +272 -0
- package/src/publish/tar.ts +110 -0
- package/src/registry/index.ts +481 -0
- package/src/registry/store.ts +136 -0
- package/src/secrets/index.ts +223 -0
- package/src/service/idle.ts +64 -0
- package/src/service/index.ts +3 -0
- package/src/service/unit.ts +250 -0
- package/src/verify/fields.ts +125 -0
- package/src/verify/index.ts +913 -0
- package/src/verify/regression.ts +157 -0
- package/src/verify/runner.ts +230 -0
- package/src/verify/sandbox.ts +407 -0
- package/src/verify/typecheck.ts +371 -0
- package/src/workers/index.ts +17 -0
- package/src/workers/launcher.ts +260 -0
- package/src/workers/pool.ts +708 -0
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fields and documents (D22, D52, D63, D106; ARCHITECTURE §6). A document is a viewer's draft or
|
|
3
|
+
* an artifact's shared state: a map from field ID to value, each field carrying the document
|
|
4
|
+
* revision that last wrote it, under a revision that only grows. A mutation applies its ops
|
|
5
|
+
* together: an op whose field changed after the mutation's base revision is a conflict and leaves
|
|
6
|
+
* the field alone, the others apply under one new revision, and the result is recorded under the
|
|
7
|
+
* mutation's ID so a retry replays it.
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import {
|
|
11
|
+
type Actor,
|
|
12
|
+
canonicalJson,
|
|
13
|
+
createDiagnostic,
|
|
14
|
+
type FieldConflict,
|
|
15
|
+
type FieldOp,
|
|
16
|
+
type FieldScope,
|
|
17
|
+
type FieldState,
|
|
18
|
+
isFieldId,
|
|
19
|
+
type MutationResult,
|
|
20
|
+
RpcError,
|
|
21
|
+
} from "@mostra/protocol";
|
|
22
|
+
import type { LedgerDb } from "../ledger/index.ts";
|
|
23
|
+
import { appendEvent } from "./events.ts";
|
|
24
|
+
|
|
25
|
+
/** The largest field value, as UTF-8 JSON (DATA_MODEL §5). */
|
|
26
|
+
export const FIELD_VALUE_MAX_BYTES: number = 1024 * 1024;
|
|
27
|
+
|
|
28
|
+
/** The largest document: every live field value together (DATA_MODEL §5). */
|
|
29
|
+
export const DOCUMENT_MAX_BYTES: number = 16 * 1024 * 1024;
|
|
30
|
+
|
|
31
|
+
/** A `document` row. */
|
|
32
|
+
export interface DocumentRow {
|
|
33
|
+
readonly id: number;
|
|
34
|
+
readonly artifact_id: string;
|
|
35
|
+
readonly scope: FieldScope;
|
|
36
|
+
readonly viewer_id: string | null;
|
|
37
|
+
readonly revision: number;
|
|
38
|
+
readonly epoch: number;
|
|
39
|
+
readonly size: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A `field` row. */
|
|
43
|
+
export interface FieldRow {
|
|
44
|
+
readonly document_id: number;
|
|
45
|
+
readonly field_id: string;
|
|
46
|
+
readonly value_json: string;
|
|
47
|
+
readonly deleted: number;
|
|
48
|
+
readonly field_rev: number;
|
|
49
|
+
readonly updated_by_json: string | null;
|
|
50
|
+
readonly updated_at: string;
|
|
51
|
+
readonly sensitive: number;
|
|
52
|
+
readonly label: string | null;
|
|
53
|
+
readonly required: number | null;
|
|
54
|
+
/** The number of the version whose view last wrote the field (D109); null for agents and hosts. */
|
|
55
|
+
readonly version_number: number | null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The document for an artifact's state (`viewerId` null) or a viewer's draft, created when
|
|
60
|
+
* `create` is set and it doesn't exist yet.
|
|
61
|
+
*/
|
|
62
|
+
export function documentFor(
|
|
63
|
+
db: LedgerDb,
|
|
64
|
+
artifactId: string,
|
|
65
|
+
viewerId: string | null,
|
|
66
|
+
create: true,
|
|
67
|
+
): DocumentRow;
|
|
68
|
+
export function documentFor(
|
|
69
|
+
db: LedgerDb,
|
|
70
|
+
artifactId: string,
|
|
71
|
+
viewerId: string | null,
|
|
72
|
+
create?: false,
|
|
73
|
+
): DocumentRow | undefined;
|
|
74
|
+
export function documentFor(
|
|
75
|
+
db: LedgerDb,
|
|
76
|
+
artifactId: string,
|
|
77
|
+
viewerId: string | null,
|
|
78
|
+
create = false,
|
|
79
|
+
): DocumentRow | undefined {
|
|
80
|
+
const find = (): DocumentRow | undefined =>
|
|
81
|
+
viewerId === null
|
|
82
|
+
? db.get<DocumentRow>(
|
|
83
|
+
"SELECT * FROM document WHERE artifact_id = ? AND scope = 'artifact'",
|
|
84
|
+
artifactId,
|
|
85
|
+
)
|
|
86
|
+
: db.get<DocumentRow>(
|
|
87
|
+
"SELECT * FROM document WHERE artifact_id = ? AND scope = 'viewer' AND viewer_id = ?",
|
|
88
|
+
artifactId,
|
|
89
|
+
viewerId,
|
|
90
|
+
);
|
|
91
|
+
const found = find();
|
|
92
|
+
if (found || !create) return found;
|
|
93
|
+
db.run(
|
|
94
|
+
"INSERT INTO document (artifact_id, scope, viewer_id) VALUES (?, ?, ?)",
|
|
95
|
+
artifactId,
|
|
96
|
+
viewerId === null ? "artifact" : "viewer",
|
|
97
|
+
viewerId,
|
|
98
|
+
);
|
|
99
|
+
return find() as DocumentRow;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A live field as the wire carries it. */
|
|
103
|
+
export function fieldState(row: FieldRow): FieldState {
|
|
104
|
+
return {
|
|
105
|
+
value: JSON.parse(row.value_json) as unknown,
|
|
106
|
+
revision: String(row.field_rev),
|
|
107
|
+
...(row.updated_by_json !== null
|
|
108
|
+
? { updatedBy: JSON.parse(row.updated_by_json) as Actor }
|
|
109
|
+
: {}),
|
|
110
|
+
updatedAt: row.updated_at,
|
|
111
|
+
...(row.label !== null ? { label: row.label } : {}),
|
|
112
|
+
...(row.required !== null ? { required: row.required === 1 } : {}),
|
|
113
|
+
...(row.sensitive === 1 ? { sensitive: true } : {}),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* A document's live fields (deleted ones left out), by ID. A draft field the page's version
|
|
119
|
+
* doesn't cover is still live: orphans are derived per view, never stored (D109).
|
|
120
|
+
*/
|
|
121
|
+
export function liveFields(
|
|
122
|
+
db: LedgerDb,
|
|
123
|
+
documentId: number | undefined,
|
|
124
|
+
): Record<string, FieldState> {
|
|
125
|
+
if (documentId === undefined) return {};
|
|
126
|
+
const rows = db.all<FieldRow>(
|
|
127
|
+
"SELECT * FROM field WHERE document_id = ? AND deleted = 0 ORDER BY field_id",
|
|
128
|
+
documentId,
|
|
129
|
+
);
|
|
130
|
+
return Object.fromEntries(rows.map((row) => [row.field_id, fieldState(row)]));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** What a mutation needs besides its ops. */
|
|
134
|
+
export interface MutationInput {
|
|
135
|
+
readonly artifactId: string;
|
|
136
|
+
readonly scope: FieldScope;
|
|
137
|
+
/** The draft's viewer, for `scope: "viewer"`. */
|
|
138
|
+
readonly viewerId?: string;
|
|
139
|
+
readonly mutationId: string;
|
|
140
|
+
/** The revision the writer last saw; absent writes unconditionally (agents and hosts, D106). */
|
|
141
|
+
readonly baseRevision?: number;
|
|
142
|
+
readonly ops: readonly FieldOp[];
|
|
143
|
+
readonly actor: Actor;
|
|
144
|
+
/** The view a viewer's mutation came from, recorded as its cause. */
|
|
145
|
+
readonly viewId?: string;
|
|
146
|
+
/** The number of that view's version, recorded on each field it writes (D109). */
|
|
147
|
+
readonly versionNumber?: number;
|
|
148
|
+
/** The submit channel a choice came through (D40, D108), recorded as its cause. */
|
|
149
|
+
readonly channel?: "shell" | "artifact";
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** What {@link applyMutation} did, for the caller's follow-ups (mirrors, streams). */
|
|
153
|
+
export interface MutationOutcome {
|
|
154
|
+
readonly result: MutationResult;
|
|
155
|
+
/** Whether the document changed (a new revision committed). */
|
|
156
|
+
readonly changed: boolean;
|
|
157
|
+
/** Whether the result was replayed from an earlier attempt. */
|
|
158
|
+
readonly replayed: boolean;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function invalid(message: string): RpcError {
|
|
162
|
+
return new RpcError(createDiagnostic("MOSTRA_HOST_INVALID_PARAMS", { message }), -32602);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function tooLarge(message: string): RpcError {
|
|
166
|
+
return new RpcError(createDiagnostic("MOSTRA_FIELD_TOO_LARGE", { message }));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Checks the ops before anything is read: IDs, one change each, JSON values within limits. */
|
|
170
|
+
function checkOps(ops: readonly FieldOp[]): Map<string, string | undefined> {
|
|
171
|
+
if (ops.length === 0) throw invalid("A mutation needs at least one op.");
|
|
172
|
+
const encoded = new Map<string, string | undefined>();
|
|
173
|
+
for (const op of ops) {
|
|
174
|
+
if (!isFieldId(op.id)) {
|
|
175
|
+
throw new RpcError(
|
|
176
|
+
createDiagnostic("MOSTRA_FIELD_ID_INVALID", {
|
|
177
|
+
message: `${JSON.stringify(op.id).slice(0, 80)} isn't a field ID.`,
|
|
178
|
+
}),
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
if (encoded.has(op.id)) throw invalid(`The field ${op.id} appears twice in one mutation.`);
|
|
182
|
+
const deletes = op.delete === true;
|
|
183
|
+
if (deletes === (op.value !== undefined)) {
|
|
184
|
+
throw invalid(`The op for ${op.id} needs exactly one of value and delete.`);
|
|
185
|
+
}
|
|
186
|
+
if (deletes) {
|
|
187
|
+
encoded.set(op.id, undefined);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
let json: string;
|
|
191
|
+
try {
|
|
192
|
+
// The canonical form checks it is JSON that I-JSON can hold; the stored text keeps the
|
|
193
|
+
// writer's order, which reads back as the same value.
|
|
194
|
+
canonicalJson(op.value);
|
|
195
|
+
json = JSON.stringify(op.value);
|
|
196
|
+
} catch {
|
|
197
|
+
throw invalid(`The value of ${op.id} isn't JSON.`);
|
|
198
|
+
}
|
|
199
|
+
if (Buffer.byteLength(json, "utf8") > FIELD_VALUE_MAX_BYTES) {
|
|
200
|
+
throw tooLarge(`The value of ${op.id} is larger than 1 MiB of JSON.`);
|
|
201
|
+
}
|
|
202
|
+
encoded.set(op.id, json);
|
|
203
|
+
}
|
|
204
|
+
return encoded;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The digest a mutation ID is recorded with: everything the mutation asked, as canonical JSON. */
|
|
208
|
+
function mutationDigest(input: MutationInput): string {
|
|
209
|
+
return createHash("sha256")
|
|
210
|
+
.update(
|
|
211
|
+
canonicalJson({
|
|
212
|
+
artifactId: input.artifactId,
|
|
213
|
+
scope: input.scope,
|
|
214
|
+
viewerId: input.viewerId ?? null,
|
|
215
|
+
baseRevision: input.baseRevision ?? null,
|
|
216
|
+
ops: input.ops,
|
|
217
|
+
}),
|
|
218
|
+
)
|
|
219
|
+
.digest("hex");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function conflictOf(row: FieldRow): FieldConflict {
|
|
223
|
+
return {
|
|
224
|
+
id: row.field_id,
|
|
225
|
+
revision: String(row.field_rev),
|
|
226
|
+
...(row.deleted === 1 ? { deleted: true as const } : { value: JSON.parse(row.value_json) }),
|
|
227
|
+
...(row.updated_by_json !== null
|
|
228
|
+
? { updatedBy: JSON.parse(row.updated_by_json) as Actor }
|
|
229
|
+
: {}),
|
|
230
|
+
updatedAt: row.updated_at,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The result a mutation ID recorded, when this exact mutation was applied before, or `undefined`
|
|
236
|
+
* for a new one; the same ID with other contents is `MOSTRA_FIELD_MUTATION_ID_REUSED` (D106).
|
|
237
|
+
* Callers check it before any other rule, so a retry of a committed mutation always replays.
|
|
238
|
+
*/
|
|
239
|
+
export function replayedMutation(db: LedgerDb, input: MutationInput): MutationResult | undefined {
|
|
240
|
+
const recorded = db.get<{ params_digest: string; result_json: string }>(
|
|
241
|
+
"SELECT params_digest, result_json FROM mutation WHERE id = ?",
|
|
242
|
+
input.mutationId,
|
|
243
|
+
);
|
|
244
|
+
if (!recorded) return undefined;
|
|
245
|
+
if (recorded.params_digest !== mutationDigest(input)) {
|
|
246
|
+
throw new RpcError(
|
|
247
|
+
createDiagnostic("MOSTRA_FIELD_MUTATION_ID_REUSED", {
|
|
248
|
+
message: `The mutation ID ${input.mutationId} was used for another change.`,
|
|
249
|
+
}),
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
return JSON.parse(recorded.result_json) as MutationResult;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Applies a mutation inside the caller's write transaction (D22, IN-01, LL-01). The mutation ID
|
|
257
|
+
* replays its recorded result; the same ID with other contents is
|
|
258
|
+
* `MOSTRA_FIELD_MUTATION_ID_REUSED`. Each changed field takes the new revision; each conflict
|
|
259
|
+
* names what the field holds now. Changes append `draft.changed` or `state.changed`, conflicts
|
|
260
|
+
* `draft.conflict` or `state.conflict`, with sensitive values left out (D52).
|
|
261
|
+
*/
|
|
262
|
+
export function applyMutation(db: LedgerDb, input: MutationInput): MutationOutcome {
|
|
263
|
+
if ((input.scope === "viewer") !== (input.viewerId !== undefined)) {
|
|
264
|
+
throw new Error("A draft mutation names its viewer, and only a draft mutation does.");
|
|
265
|
+
}
|
|
266
|
+
const encoded = checkOps(input.ops);
|
|
267
|
+
const digest = mutationDigest(input);
|
|
268
|
+
const replay = replayedMutation(db, input);
|
|
269
|
+
if (replay) return { result: replay, changed: false, replayed: true };
|
|
270
|
+
|
|
271
|
+
const document = documentFor(db, input.artifactId, input.viewerId ?? null, true);
|
|
272
|
+
const revision = document.revision + 1;
|
|
273
|
+
const now = new Date().toISOString();
|
|
274
|
+
const applied: string[] = [];
|
|
275
|
+
const conflicts: FieldConflict[] = [];
|
|
276
|
+
const changes: Record<string, unknown>[] = [];
|
|
277
|
+
let size = document.size;
|
|
278
|
+
const writes: (() => void)[] = [];
|
|
279
|
+
for (const op of input.ops) {
|
|
280
|
+
const row = db.get<FieldRow>(
|
|
281
|
+
"SELECT * FROM field WHERE document_id = ? AND field_id = ?",
|
|
282
|
+
document.id,
|
|
283
|
+
op.id,
|
|
284
|
+
);
|
|
285
|
+
if (input.baseRevision !== undefined && row && row.field_rev > input.baseRevision) {
|
|
286
|
+
conflicts.push(conflictOf(row));
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
applied.push(op.id);
|
|
290
|
+
const json = encoded.get(op.id);
|
|
291
|
+
const oldSize = row && row.deleted === 0 ? Buffer.byteLength(row.value_json, "utf8") : 0;
|
|
292
|
+
const sensitive = op.sensitive ?? (row ? row.sensitive === 1 : false);
|
|
293
|
+
if (json === undefined) {
|
|
294
|
+
// Deleting what isn't there changes nothing and needs no new revision.
|
|
295
|
+
if (!row || row.deleted === 1) continue;
|
|
296
|
+
size -= oldSize;
|
|
297
|
+
writes.push(() =>
|
|
298
|
+
db.run(
|
|
299
|
+
"UPDATE field SET value_json = 'null', deleted = 1, field_rev = ?, updated_by_json = ?, updated_at = ?, version_number = ? WHERE document_id = ? AND field_id = ?",
|
|
300
|
+
revision,
|
|
301
|
+
JSON.stringify(input.actor),
|
|
302
|
+
now,
|
|
303
|
+
input.versionNumber ?? null,
|
|
304
|
+
document.id,
|
|
305
|
+
op.id,
|
|
306
|
+
),
|
|
307
|
+
);
|
|
308
|
+
changes.push({ id: op.id, deleted: true });
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
size += Buffer.byteLength(json, "utf8") - oldSize;
|
|
312
|
+
const label = op.label ?? row?.label ?? null;
|
|
313
|
+
const required = op.required !== undefined ? (op.required ? 1 : 0) : (row?.required ?? null);
|
|
314
|
+
writes.push(() =>
|
|
315
|
+
db.run(
|
|
316
|
+
`INSERT INTO field (document_id, field_id, value_json, deleted, field_rev, updated_by_json, updated_at, sensitive, label, required, version_number)
|
|
317
|
+
VALUES (?, ?, ?, 0, ?, ?, ?, ?, ?, ?, ?)
|
|
318
|
+
ON CONFLICT (document_id, field_id) DO UPDATE SET value_json = excluded.value_json, deleted = 0,
|
|
319
|
+
field_rev = excluded.field_rev, updated_by_json = excluded.updated_by_json,
|
|
320
|
+
updated_at = excluded.updated_at, sensitive = excluded.sensitive, label = excluded.label,
|
|
321
|
+
required = excluded.required, version_number = excluded.version_number`,
|
|
322
|
+
document.id,
|
|
323
|
+
op.id,
|
|
324
|
+
json,
|
|
325
|
+
revision,
|
|
326
|
+
JSON.stringify(input.actor),
|
|
327
|
+
now,
|
|
328
|
+
sensitive ? 1 : 0,
|
|
329
|
+
label,
|
|
330
|
+
required,
|
|
331
|
+
input.versionNumber ?? null,
|
|
332
|
+
),
|
|
333
|
+
);
|
|
334
|
+
changes.push(
|
|
335
|
+
sensitive
|
|
336
|
+
? { id: op.id, sensitive: true }
|
|
337
|
+
: { id: op.id, value: JSON.parse(json) as unknown },
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
if (size > DOCUMENT_MAX_BYTES) {
|
|
341
|
+
throw tooLarge(
|
|
342
|
+
`This change would make the ${input.scope === "viewer" ? "draft" : "artifact state"} larger than 16 MiB.`,
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const changed = writes.length > 0;
|
|
347
|
+
for (const write of writes) write();
|
|
348
|
+
if (changed) {
|
|
349
|
+
db.run("UPDATE document SET revision = ?, size = ? WHERE id = ?", revision, size, document.id);
|
|
350
|
+
}
|
|
351
|
+
const current = changed ? revision : document.revision;
|
|
352
|
+
const causation = {
|
|
353
|
+
mutationId: input.mutationId,
|
|
354
|
+
...(input.viewId !== undefined ? { viewId: input.viewId } : {}),
|
|
355
|
+
...(input.channel !== undefined ? { channel: input.channel } : {}),
|
|
356
|
+
};
|
|
357
|
+
const subject =
|
|
358
|
+
input.scope === "viewer" ? { viewerId: input.viewerId as string, epoch: document.epoch } : {};
|
|
359
|
+
if (changed) {
|
|
360
|
+
appendEvent(db, {
|
|
361
|
+
artifactId: input.artifactId,
|
|
362
|
+
type: input.scope === "viewer" ? "draft.changed" : "state.changed",
|
|
363
|
+
actor: input.actor,
|
|
364
|
+
causation,
|
|
365
|
+
payload: { ...subject, revision: String(current), changes },
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
if (conflicts.length > 0) {
|
|
369
|
+
appendEvent(db, {
|
|
370
|
+
artifactId: input.artifactId,
|
|
371
|
+
type: input.scope === "viewer" ? "draft.conflict" : "state.conflict",
|
|
372
|
+
actor: input.actor,
|
|
373
|
+
causation,
|
|
374
|
+
payload: {
|
|
375
|
+
...subject,
|
|
376
|
+
revision: String(current),
|
|
377
|
+
base: String(input.baseRevision),
|
|
378
|
+
fields: conflicts.map((conflict) => ({ id: conflict.id, revision: conflict.revision })),
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
const result: MutationResult = { revision: String(current), applied, conflicts };
|
|
383
|
+
db.run(
|
|
384
|
+
"INSERT INTO mutation (id, document_id, params_digest, result_json, applied_at) VALUES (?, ?, ?, ?, ?)",
|
|
385
|
+
input.mutationId,
|
|
386
|
+
document.id,
|
|
387
|
+
digest,
|
|
388
|
+
JSON.stringify(result),
|
|
389
|
+
now,
|
|
390
|
+
);
|
|
391
|
+
return { result, changed, replayed: false };
|
|
392
|
+
}
|