doxum 0.1.21 → 0.1.23

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.
Files changed (72) hide show
  1. package/README.md +35 -7
  2. package/dist/{advanced-FgdTwu5b.d.ts → advanced-CLJCWkHa.d.cts} +3 -3
  3. package/dist/{advanced-COFJtWMT.js → advanced-CRNHfJBu.js} +482 -446
  4. package/dist/advanced-CRNHfJBu.js.map +1 -0
  5. package/dist/{advanced-BRL5pN0y.cjs → advanced-Cle5g3B4.cjs} +615 -459
  6. package/dist/advanced-Cle5g3B4.cjs.map +1 -0
  7. package/dist/{advanced-DfKc7TZR.d.cts → advanced-d6J61er9.d.ts} +3 -3
  8. package/dist/advanced.cjs +1 -1
  9. package/dist/advanced.d.cts +1 -1
  10. package/dist/advanced.d.ts +1 -1
  11. package/dist/advanced.js +1 -1
  12. package/dist/context-BcLKGfq8.cjs +149 -0
  13. package/dist/context-BcLKGfq8.cjs.map +1 -0
  14. package/dist/context-DFEaDUv5.js +84 -0
  15. package/dist/context-DFEaDUv5.js.map +1 -0
  16. package/dist/{contract-CdmDHRUq.d.cts → contract-SYJFP0lM.d.cts} +62 -88
  17. package/dist/{contract-KG5XnQZt.d.ts → contract-ULPQmiEs.d.ts} +62 -88
  18. package/dist/driver-BD3DQOW6.cjs +382 -0
  19. package/dist/driver-BD3DQOW6.cjs.map +1 -0
  20. package/dist/{driver-CLYJOGDQ.js → driver-yFfvDMX5.js} +155 -26
  21. package/dist/driver-yFfvDMX5.js.map +1 -0
  22. package/dist/index.cjs +2370 -406
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +6 -5
  25. package/dist/index.d.ts +6 -5
  26. package/dist/index.js +2293 -330
  27. package/dist/index.js.map +1 -1
  28. package/dist/local-sync.cjs +5 -5
  29. package/dist/local-sync.cjs.map +1 -1
  30. package/dist/local-sync.d.cts +1 -1
  31. package/dist/local-sync.d.ts +1 -1
  32. package/dist/local-sync.js +2 -2
  33. package/dist/local-sync.js.map +1 -1
  34. package/dist/react.cjs +2 -69
  35. package/dist/react.cjs.map +1 -1
  36. package/dist/react.d.cts +0 -1
  37. package/dist/react.d.ts +1 -2
  38. package/dist/react.js +3 -70
  39. package/dist/react.js.map +1 -1
  40. package/package.json +1 -6
  41. package/skills/doxum-runtime/SKILL.md +7 -1
  42. package/skills/doxum-runtime/references/guide.en.md +17 -6
  43. package/skills/doxum-runtime/references/guide.zh-CN.md +15 -5
  44. package/skills/doxum-runtime/references/invariants.en.md +5 -3
  45. package/skills/doxum-runtime/references/invariants.zh-CN.md +4 -3
  46. package/skills/doxum-runtime/references/projections.en.md +5 -0
  47. package/skills/doxum-runtime/references/projections.zh-CN.md +5 -0
  48. package/dist/access-BeSV5ve-.js +0 -816
  49. package/dist/access-BeSV5ve-.js.map +0 -1
  50. package/dist/access-Bienf28h.cjs +0 -911
  51. package/dist/access-Bienf28h.cjs.map +0 -1
  52. package/dist/advanced-BRL5pN0y.cjs.map +0 -1
  53. package/dist/advanced-COFJtWMT.js.map +0 -1
  54. package/dist/driver-CLYJOGDQ.js.map +0 -1
  55. package/dist/driver-Djr3BVvo.cjs +0 -241
  56. package/dist/driver-Djr3BVvo.cjs.map +0 -1
  57. package/dist/integration-C4Isp8lJ.js +0 -26
  58. package/dist/integration-C4Isp8lJ.js.map +0 -1
  59. package/dist/integration-Z6MsfFI7.cjs +0 -31
  60. package/dist/integration-Z6MsfFI7.cjs.map +0 -1
  61. package/dist/integration.cjs +0 -12
  62. package/dist/integration.d.cts +0 -17
  63. package/dist/integration.d.ts +0 -17
  64. package/dist/integration.js +0 -4
  65. package/dist/issue-DjK1xp5K.cjs +0 -881
  66. package/dist/issue-DjK1xp5K.cjs.map +0 -1
  67. package/dist/issue-W0hT8u8D.js +0 -642
  68. package/dist/issue-W0hT8u8D.js.map +0 -1
  69. package/dist/notification-Bkjau6B6.js +0 -232
  70. package/dist/notification-Bkjau6B6.js.map +0 -1
  71. package/dist/notification-DJfouMEb.cjs +0 -309
  72. package/dist/notification-DJfouMEb.cjs.map +0 -1
@@ -1,816 +0,0 @@
1
- import { B as profile, C as validate, D as index, E as equal, F as matches, I as moveSelection, M as installList, N as keys, O as indexedKeys, P as listSequence, T as isRecord, a as compiledShape, b as contains, g as resolveLocated, j as installKeys, k as insert$2, m as resolveChild, n as fail, u as nodeAt, w as isPlainObject, x as parent, y as children, z as valid } from "./issue-W0hT8u8D.js";
2
- //#region core/src/schema-value.ts
3
- var ParseError = class extends TypeError {
4
- issues;
5
- constructor(issue) {
6
- super(`${issue.message} at ${JSON.stringify(issue.address)}`);
7
- this.name = "ParseError";
8
- this.issues = Object.freeze([Object.freeze({
9
- ...issue,
10
- address: Object.freeze([...issue.address])
11
- })]);
12
- }
13
- };
14
- const issue = (address, message, code = "invalid-value") => ({
15
- address,
16
- message,
17
- code
18
- });
19
- const checkScalar = (validator, value, address, strict) => {
20
- if (!validator) return strict ? issue(address, "A validator is required to parse an opaque value.", "missing-validator") : void 0;
21
- try {
22
- const result = typeof validator === "function" ? validator(value) : validator["~standard"].validate(value);
23
- if (result && (typeof result === "object" || typeof result === "function") && "then" in result && typeof result.then === "function") return issue(address, "Validators must be synchronous.");
24
- if (typeof validator !== "function") {
25
- if (!isRecord(result)) return issue(address, "Malformed validator result.");
26
- if (Array.isArray(result.issues)) {
27
- const first = result.issues[0];
28
- return issue([...address, ...(first?.path ?? []).map((part) => String(typeof part === "object" ? part.key : part))], first?.message ?? "Value validation failed.");
29
- }
30
- if (!Object.hasOwn(result, "value")) return issue(address, "Malformed validator result.");
31
- }
32
- return;
33
- } catch (error) {
34
- return issue(address, error instanceof Error ? error.message : "Value validation failed.");
35
- }
36
- };
37
- const checkKey = (validator, value, address) => {
38
- if (typeof value !== "string") return issue(address, "Collection keys must be strings.", "invalid-key");
39
- const failure = checkScalar(validator, value, address, false);
40
- return failure ? {
41
- ...failure,
42
- code: "invalid-key"
43
- } : void 0;
44
- };
45
- const checkValue = (node, value, address = [], strict = false) => {
46
- if (node.optional && value === void 0) return void 0;
47
- if (node.kind === "field") return checkScalar(node.validator, value, address, strict);
48
- if (node.kind === "object" || node.kind === "variant") {
49
- if (!isPlainObject(value)) return issue(address, "Expected an object.");
50
- if (node.kind === "variant") {
51
- const tag = value[node.tag];
52
- if (!Object.hasOwn(value, node.tag) || typeof tag !== "string" || !Object.hasOwn(node.variants, tag)) return issue([...address, node.tag], "Unknown variant tag.");
53
- }
54
- const { members } = compiledShape(node, value);
55
- for (const key of Reflect.ownKeys(value)) {
56
- if (node.kind === "variant" && key === node.tag) continue;
57
- if (typeof key !== "string" || !members.has(key)) return issue([...address, String(key)], "Property is not declared in the object schema.");
58
- }
59
- for (const [key, member] of members) {
60
- const child = member.node;
61
- if (!Object.prototype.hasOwnProperty.call(value, key) && !child.optional) return issue([...address, key], "Required property is missing.");
62
- const failure = checkValue(child, value[key], [...address, key], strict);
63
- if (failure) return failure;
64
- }
65
- return;
66
- }
67
- if (node.kind === "table" || node.kind === "map") {
68
- if (!isPlainObject(value)) return issue(address, "Expected a collection object.");
69
- const entries = node.kind === "table" ? value.byId : value;
70
- if (!isPlainObject(entries)) return issue(address, "Expected a byId object.");
71
- if (node.kind === "table") {
72
- if (!Array.isArray(value.ids) || value.ids.length !== Object.keys(entries).length) return issue(address, "Table ids and entries must agree.");
73
- const seen = /* @__PURE__ */ new Set();
74
- for (const id of value.ids) {
75
- const failure = checkKey(node.key, id, address);
76
- if (failure) return failure;
77
- if (seen.has(id) || !Object.prototype.hasOwnProperty.call(entries, id)) return issue(address, "Table ids must be unique and reference existing entries.");
78
- seen.add(id);
79
- }
80
- }
81
- for (const id of Object.keys(entries)) {
82
- const failure = checkKey(node.key, id, [...address, id]) ?? checkValue(node.value, entries[id], [...address, id], strict);
83
- if (failure) return failure;
84
- }
85
- return;
86
- }
87
- if (node.kind === "list") {
88
- if (!Array.isArray(value)) return issue(address, "Expected a list.");
89
- const seen = /* @__PURE__ */ new Set();
90
- const order = keys(value, node.keyOf);
91
- for (let i = 0; i < value.length; i++) {
92
- const failure = checkValue(node.value, value[i], [...address, String(i)], strict);
93
- if (failure) return failure;
94
- let key;
95
- try {
96
- key = order.at(i);
97
- } catch {
98
- return issue(address, "List keyOf failed.");
99
- }
100
- if (typeof key !== "string" || seen.has(key)) return issue(address, "List keys must be unique strings.", "invalid-key");
101
- seen.add(key);
102
- }
103
- return;
104
- }
105
- if (!validate(value)) return issue(address, "Invalid tree structure.", "invalid-tree");
106
- if (isRecord(value) && isRecord(value.nodes)) for (const id of Object.keys(value.nodes)) {
107
- const entry = value.nodes[id];
108
- if (isRecord(entry) && Object.prototype.hasOwnProperty.call(entry, "value")) {
109
- const failure = checkValue(node.value, entry.value, [...address, id], strict);
110
- if (failure) return failure;
111
- }
112
- }
113
- };
114
- /** Copy editable structure; atomic payloads retain their immutable ownership contract. */
115
- const copyValue = (node, value) => {
116
- if (value === void 0 || node.kind === "field") return value;
117
- profile.copy.structure();
118
- if ((node.kind === "object" || node.kind === "variant") && isRecord(value)) {
119
- const result = Object.create(Object.getPrototypeOf(value));
120
- const { members } = compiledShape(node, value);
121
- for (const key of Object.getOwnPropertyNames(value)) Object.defineProperty(result, key, {
122
- value: node.kind === "variant" && key === node.tag ? value[key] : copyValue(members.get(key).node, value[key]),
123
- writable: true,
124
- enumerable: Object.getOwnPropertyDescriptor(value, key)?.enumerable,
125
- configurable: true
126
- });
127
- return result;
128
- }
129
- if ((node.kind === "table" || node.kind === "map") && isRecord(value)) {
130
- const entries = node.kind === "table" ? value.byId : value;
131
- const result = Object.fromEntries(Object.keys(entries).map((id) => [id, copyValue(node.value, entries[id])]));
132
- return node.kind === "table" ? {
133
- ids: [...value.ids],
134
- byId: result
135
- } : result;
136
- }
137
- if (node.kind === "list") return [...value];
138
- if (node.kind === "tree" && isRecord(value)) {
139
- const entries = value.nodes;
140
- return {
141
- ...value,
142
- nodes: Object.fromEntries(Object.entries(entries).map(([id, n]) => [id, {
143
- ...n,
144
- children: [...n.children]
145
- }]))
146
- };
147
- }
148
- return value;
149
- };
150
- const equalValue = (node, left, right) => {
151
- if (Object.is(left, right)) return true;
152
- if (node.kind === "field" || left === void 0 || right === void 0) return false;
153
- if ((node.kind === "object" || node.kind === "variant") && isRecord(left) && isRecord(right)) {
154
- if (node.kind === "variant" && left[node.tag] !== right[node.tag]) return false;
155
- for (const [key, member] of compiledShape(node, left).members) {
156
- if (Object.hasOwn(left, key) !== Object.hasOwn(right, key)) return false;
157
- if (!equalValue(member.node, left[key], right[key])) return false;
158
- }
159
- return true;
160
- }
161
- if (node.kind === "list" && Array.isArray(left) && Array.isArray(right)) return left.length === right.length && left.every((v, i) => equalValue(node.value, v, right[i]));
162
- if (node.kind === "table" && isRecord(left) && isRecord(right)) return equal(left.ids, right.ids) && equalValue({
163
- kind: "map",
164
- value: node.value
165
- }, left.byId, right.byId);
166
- if (node.kind === "tree" && isRecord(left) && isRecord(right)) {
167
- if (left.rootId !== right.rootId) return false;
168
- const a = left.nodes, b = right.nodes;
169
- return Object.keys(a).length === Object.keys(b).length && Object.keys(a).every((id) => Object.hasOwn(b, id) && a[id].parentId === b[id].parentId && equal(a[id].children, b[id].children) && Object.hasOwn(a[id], "value") === Object.hasOwn(b[id], "value") && equalValue(node.value, a[id].value, b[id].value));
170
- }
171
- if (node.kind === "map" && isRecord(left) && isRecord(right)) {
172
- const keys = Object.keys(left);
173
- return keys.length === Object.keys(right).length && keys.every((key) => Object.hasOwn(right, key) && equalValue(node.value, left[key], right[key]));
174
- }
175
- return false;
176
- };
177
- const parse = (node, input) => {
178
- const valueNode = node;
179
- const failure = checkValue(valueNode, input, [], true);
180
- if (failure) throw new ParseError(failure);
181
- return copyValue(valueNode, input);
182
- };
183
- //#endregion
184
- //#region core/src/mutation/operations/map.ts
185
- function put(session, container, id, value) {
186
- if (container.node.kind !== "map") return fail(container.at, "invalid-collection", "Expected a map.");
187
- session.writeMember(container, id, value, "set");
188
- }
189
- function remove$3(session, container, id) {
190
- if (container.node.kind !== "map") return fail(container.at, "invalid-collection", "Expected a map.");
191
- session.writeMember(container, id, void 0, "remove");
192
- }
193
- //#endregion
194
- //#region core/src/mutation/operations/table.ts
195
- function create(session, container, entries, position) {
196
- const { at, node, value } = container;
197
- if (node.kind !== "table") return fail(at, "invalid-collection", "Expected a table.");
198
- const table = value;
199
- if (!valid(table.ids, position)) return fail(at, "invalid-anchor", "Unknown table anchor.");
200
- const ids = /* @__PURE__ */ new Set();
201
- for (const entry of entries) {
202
- if (Object.hasOwn(table.byId, entry.id) || ids.has(entry.id)) return fail(at, "duplicate-entity", "Table key already exists.");
203
- ids.add(entry.id);
204
- }
205
- if (!entries.length) return;
206
- const index$1 = index(table.ids, position);
207
- for (const entry of entries) session.writeMember(container, entry.id, entry.value, "set");
208
- const length = table.ids.length;
209
- table.ids.length += entries.length;
210
- table.ids.copyWithin(index$1 + entries.length, index$1, length);
211
- for (let i = 0; i < entries.length; i++) table.ids[index$1 + i] = entries[i].id;
212
- }
213
- function remove$2(session, container, ids) {
214
- const { at, node, value } = container;
215
- if (node.kind !== "table") return fail(at, "invalid-collection", "Expected a table.");
216
- const table = value;
217
- for (const id of ids) if (!Object.hasOwn(table.byId, id)) return fail(at, "missing-entity", "Table key does not exist.");
218
- if (!ids.length) return;
219
- const removed = new Set(ids);
220
- for (const id of removed) session.writeMember(container, id, void 0, "remove");
221
- table.ids = table.ids.filter((id) => !removed.has(id));
222
- }
223
- function replace$3(session, container, id, value) {
224
- const { at, node, value: tableValue } = container;
225
- if (node.kind !== "table") return fail(at, "invalid-collection", "Expected a table.");
226
- const table = tableValue;
227
- if (!Object.hasOwn(table.byId, id)) return fail(at.concat(id), "missing-entity", "Table key does not exist.");
228
- session.writeMember(container, id, value, "set");
229
- }
230
- //#endregion
231
- //#region core/src/mutation/operations/list.ts
232
- function insert$1(session, container, value, position) {
233
- const { at, node, value: current } = container;
234
- if (node.kind !== "list") return fail(at, "invalid-list-key", "Expected a list.");
235
- session.validate(node.value, value, at);
236
- const id = node.keyOf(value);
237
- if (typeof id !== "string") return fail(at, "invalid-list-key", "List key must be a string.");
238
- const items = current, keys$1 = keys(items, node.keyOf);
239
- if (keys$1.index(id) >= 0) return fail(at, "duplicate-list-item", "List key already exists.");
240
- if (!valid(keys$1, position)) return fail(at, "invalid-anchor", "Unknown list anchor.");
241
- session.recorder.order(container);
242
- session.recorder.member(container, id, session.definition(container, id), -1);
243
- insert$2(items, index(keys$1, position), value);
244
- session.invalidate();
245
- }
246
- function replace$2(session, container, id, value) {
247
- const { at, node, value: items } = container;
248
- if (node.kind !== "list") return fail(at, "invalid-list-key", "Expected a list.");
249
- const index = indexedKeys(items, node.keyOf).index(id);
250
- if (index < 0) return fail(at, "missing-list-item", "List key does not exist.");
251
- session.writeMember(container, id, value, "set", index);
252
- }
253
- function remove$1(session, container, id) {
254
- const { at, node, value } = container;
255
- if (node.kind !== "list") return fail(at, "missing-list-item", "List key does not exist.");
256
- const index = keys(value, node.keyOf).index(id);
257
- if (index < 0) return fail(at, "missing-list-item", "List key does not exist.");
258
- session.writeMember(container, id, void 0, "remove", index);
259
- }
260
- //#endregion
261
- //#region core/src/mutation/operations/tree.ts
262
- const node = (tree, id) => Object.hasOwn(tree.nodes, id) ? tree.nodes[id] : void 0;
263
- const positionIndex = (index, length, at) => {
264
- if (index !== void 0 && (!Number.isInteger(index) || index < 0)) return fail(at, "invalid-tree-index", "Tree index must be nonnegative.");
265
- return Math.min(index ?? length, length);
266
- };
267
- const insert = (session, container, id, value, position) => {
268
- const { at, node: schema, value: tree } = container;
269
- if (typeof id !== "string") return fail(at, "invalid-key", "Tree keys must be strings.");
270
- if (contains(tree, id)) return fail(at, "duplicate-tree-node", "Tree node already exists.");
271
- const parentId = position?.parentId;
272
- const parent = parentId === void 0 ? void 0 : node(tree, parentId);
273
- if (tree.rootId !== void 0 && !parent || tree.rootId === void 0 && parentId !== void 0) return fail(at, "missing-tree-parent", "A nonempty tree requires an existing parent.");
274
- const index = positionIndex(position?.index, parent?.children.length ?? 0, at);
275
- session.validate(schema.value, value, at.concat(id));
276
- session.recorder.tree(container, id);
277
- if (parentId !== void 0) session.recorder.tree(container, parentId);
278
- Object.defineProperty(tree.nodes, id, {
279
- value: {
280
- ...parentId === void 0 ? {} : { parentId },
281
- children: [],
282
- value
283
- },
284
- writable: true,
285
- configurable: true,
286
- enumerable: true
287
- });
288
- if (parent) parent.children.splice(index, 0, id);
289
- else tree.rootId = id;
290
- session.invalidate();
291
- };
292
- const replace$1 = (session, container, id, value) => {
293
- const { at, node: schema, value: tree } = container;
294
- const entry = node(tree, id);
295
- if (!entry) return fail(at, "missing-tree-node", "Tree node does not exist.");
296
- if (Object.is(entry.value, value) && Object.hasOwn(entry, "value")) return;
297
- session.validate(schema.value, value, at.concat(id));
298
- session.recorder.tree(container, id);
299
- entry.value = value;
300
- };
301
- const remove = (session, container, id) => {
302
- const { at, value: tree } = container;
303
- const entry = node(tree, id);
304
- if (!entry) return fail(at, "missing-tree-node", "Tree node does not exist.");
305
- const ids = [], stack = [id];
306
- while (stack.length) {
307
- const current = stack.pop();
308
- ids.push(current);
309
- for (const child of tree.nodes[current].children) stack.push(child);
310
- }
311
- for (const key of ids) session.recorder.tree(container, key);
312
- if (entry.parentId !== void 0) session.recorder.tree(container, entry.parentId);
313
- if (entry.parentId === void 0) delete tree.rootId;
314
- else {
315
- const children = tree.nodes[entry.parentId].children;
316
- children.splice(children.indexOf(id), 1);
317
- }
318
- for (const key of ids) delete tree.nodes[key];
319
- session.invalidate();
320
- };
321
- const move$1 = (session, container, id, position) => {
322
- const { at, value: tree } = container;
323
- const entry = node(tree, id), parentId = position?.parentId;
324
- if (!entry) return fail(at, "missing-tree-node", "Tree node does not exist.");
325
- if (entry.parentId === void 0 || parentId === void 0 || !contains(tree, parentId)) return fail(at, "missing-tree-parent", "Moving requires a non-root node and an existing parent.");
326
- for (let ancestor = parentId; ancestor !== void 0; ancestor = tree.nodes[ancestor].parentId) if (ancestor === id) return fail(at, "tree-cycle", "Cannot move a node under its descendant.");
327
- const old = tree.nodes[entry.parentId], next = tree.nodes[parentId];
328
- const index = positionIndex(position?.index, next.children.length - (old === next ? 1 : 0), at);
329
- const previous = old.children.indexOf(id);
330
- if (old === next && previous === index) return;
331
- session.recorder.tree(container, id);
332
- session.recorder.tree(container, entry.parentId);
333
- session.recorder.tree(container, parentId);
334
- old.children.splice(previous, 1);
335
- next.children.splice(index, 0, id);
336
- entry.parentId = parentId;
337
- session.invalidate();
338
- };
339
- //#endregion
340
- //#region core/src/mutation/operations/order.ts
341
- const assertOrdered = (container) => {
342
- const { at, node } = container;
343
- if (node.kind !== "table" && node.kind !== "list") return fail(at, "invalid-collection", "Expected an ordered container.");
344
- return node;
345
- };
346
- const plannedOrder = (at, current, selection, position) => {
347
- const plan = moveSelection(current, selection, position);
348
- switch (plan.kind) {
349
- case "ok": return plan.order;
350
- case "duplicate": return fail(at, "invalid-key", "Moved keys must be unique.");
351
- case "missing": return fail(at, "missing-entity", "Ordered key does not exist.");
352
- case "invalid-anchor": return fail(at, "invalid-anchor", "Unknown order anchor.");
353
- }
354
- };
355
- function move(session, container, ids, position) {
356
- const node = assertOrdered(container);
357
- const selection = typeof ids === "string" ? [ids] : ids;
358
- if (!selection.length) return;
359
- if (node.kind === "table") {
360
- const order = container.value.ids;
361
- const next = plannedOrder(container.at, order, selection, position);
362
- if (equal(order, next)) return;
363
- session.recorder.order(container);
364
- installKeys(order, next);
365
- } else {
366
- const items = container.value;
367
- const sequence = listSequence(items, node.keyOf);
368
- const next = plannedOrder(container.at, sequence.order, selection, position);
369
- if (equal(sequence.order, next)) return;
370
- session.recorder.order(container);
371
- installList(items, node.keyOf, sequence, next);
372
- }
373
- session.invalidate();
374
- }
375
- function reorder(session, container, next) {
376
- const node = assertOrdered(container);
377
- if (node.kind === "table") {
378
- const current = container.value.ids;
379
- if (!matches(next, current)) return fail(container.at, "invalid-collection", "Order must contain every key exactly once.");
380
- if (equal(current, next)) return;
381
- session.recorder.order(container);
382
- installKeys(current, next);
383
- } else {
384
- const items = container.value;
385
- const sequence = listSequence(items, node.keyOf);
386
- if (!matches(next, sequence.order)) return fail(container.at, "invalid-collection", "Order must contain every key exactly once.");
387
- if (equal(sequence.order, next)) return;
388
- session.recorder.order(container);
389
- installList(items, node.keyOf, sequence, next);
390
- }
391
- session.invalidate();
392
- }
393
- //#endregion
394
- //#region core/src/access/scope.ts
395
- const locations = /* @__PURE__ */ new WeakMap();
396
- const locationOf = (value) => locations.get(value);
397
- const addressOf = (target) => {
398
- if (target.at) return target.at;
399
- profile.access("addresses");
400
- return target.at = addressOf(target.parent).concat(target.key);
401
- };
402
- const assertActive = (context) => {
403
- if (context.active && !context.active()) throw new Error("Document access scope has expired.");
404
- };
405
- const collect = (context, at, kind = "value", id) => {
406
- context.dependencies?.record(kind === "value" ? {
407
- kind,
408
- at
409
- } : {
410
- kind,
411
- at,
412
- ...id === void 0 ? {} : { id }
413
- });
414
- };
415
- const snapshot = (value) => {
416
- const location = value && typeof value === "object" ? locationOf(value) : void 0;
417
- if (!location) return value;
418
- const { context } = location;
419
- const at = addressOf(location.target);
420
- profile.access("snapshots");
421
- collect(context, at);
422
- const resolved = resolve(context, location.target);
423
- if (!resolved.node) throw new Error("The selected schema address no longer exists.");
424
- return copyValue(resolved.node, resolved.value);
425
- };
426
- const replace = (container, key, value) => {
427
- if (!locationOf(container)) throw new TypeError("replace requires scoped document access.");
428
- if (typeof key !== "string") throw new TypeError("Document keys must be strings.");
429
- Reflect.set(container, key, value);
430
- };
431
- const resolve = (context, target) => {
432
- assertActive(context);
433
- const generation = context.session?.generation ?? 0;
434
- if (target.generation !== generation) {
435
- profile.access("resolutions");
436
- if (!target.parent && !target.at.length) {
437
- target.node = context.state.schema;
438
- target.value = context.state.document;
439
- } else {
440
- const parent = target.parent && resolve(context, target.parent);
441
- const location = parent ? resolveChild(parent.node, parent.value, target.key) : resolveLocated(context.state.schema, context.state.document, target.at);
442
- target.node = location?.node ?? nodeAt(context.state.schema, addressOf(target), context.state.document);
443
- target.value = location && Object.hasOwn(location.parent, location.key) ? location.parent[location.key] : void 0;
444
- }
445
- target.generation = generation;
446
- }
447
- return target;
448
- };
449
- const createAccess = (context, initial = []) => {
450
- profile.access("scopes");
451
- const mutable = () => {
452
- assertActive(context);
453
- if (!context.session) throw new TypeError("Cannot modify read-only document access.");
454
- return context.session;
455
- };
456
- const writableContainer = (target, session) => {
457
- if (target.container?.generation === session.generation && "layout" in target.container) return target.container;
458
- return target.container = session.bind(addressOf(target), target.node, target.value);
459
- };
460
- const childAt = (target, key) => {
461
- profile.access("addresses");
462
- return addressOf(target).concat(key);
463
- };
464
- const access = (target) => {
465
- if (target.proxy) return target.proxy;
466
- const proxy = new Proxy(target, handler);
467
- profile.access("proxies");
468
- locations.set(proxy, {
469
- context,
470
- target
471
- });
472
- target.proxy = proxy;
473
- return proxy;
474
- };
475
- const createTarget = (at, parent, key) => ({
476
- at,
477
- parent,
478
- key,
479
- node: void 0,
480
- value: void 0,
481
- generation: -1,
482
- children: void 0,
483
- proxy: void 0
484
- });
485
- const childAccess = (target, member, node, value) => {
486
- const key = typeof member === "string" ? member : member.key;
487
- if (node.kind === "field" || value === void 0) {
488
- if (context.dependencies) collect(context, childAt(target, key));
489
- return value;
490
- }
491
- let children = target.children;
492
- let child;
493
- if (typeof member !== "string") {
494
- if (children?.kind !== "fixed" || children.schema !== target.node) children = {
495
- kind: "fixed",
496
- schema: target.node,
497
- slots: new Array(compiledShape(target.node, target.value).members.size)
498
- };
499
- child = children.slots[member.slot] ??= createTarget(void 0, target, key);
500
- } else {
501
- if (children?.kind !== "dynamic") children = {
502
- kind: "dynamic",
503
- keys: /* @__PURE__ */ new Map()
504
- };
505
- const retained = children.keys.get(key);
506
- child = retained ?? createTarget(void 0, target, key);
507
- if (!retained) children.keys.set(key, child);
508
- }
509
- target.children = children;
510
- if (child.generation === target.generation) return child.proxy;
511
- child.node = node;
512
- child.value = value;
513
- child.generation = target.generation;
514
- return access(child);
515
- };
516
- const collectionValue = (target, node) => {
517
- const current = resolve(context, target);
518
- if (current.node !== node || current.value === void 0) throw new TypeError("The collection method belongs to a replaced schema branch.");
519
- return current.value;
520
- };
521
- const writableCollection = (target, node) => {
522
- const session = mutable();
523
- collectionValue(target, node);
524
- return writableContainer(target, session);
525
- };
526
- const writableTree = (target, node) => {
527
- const session = mutable();
528
- collectionValue(target, node);
529
- if (target.container?.generation === session.generation && !("layout" in target.container)) return target.container;
530
- return target.container = session.bindTree(addressOf(target), target.node, target.value);
531
- };
532
- const replaceCollection = (target, node, value) => {
533
- const session = mutable();
534
- collectionValue(target, node);
535
- session.replace(addressOf(target), value);
536
- };
537
- const mapMethod = (target, property, node) => {
538
- if (property === "ids") return () => {
539
- const current = collectionValue(target, node);
540
- if (context.dependencies) collect(context, addressOf(target), "collection");
541
- return Object.keys(current);
542
- };
543
- if (property === "has") return (id) => {
544
- const current = collectionValue(target, node);
545
- if (context.dependencies) collect(context, addressOf(target), "collection", id);
546
- return Object.hasOwn(current, id);
547
- };
548
- if (property === "get") return (id) => {
549
- const current = collectionValue(target, node);
550
- if (context.dependencies) collect(context, addressOf(target), "collection", id);
551
- if (!Object.hasOwn(current, id)) return void 0;
552
- if (node.value.kind === "field") {
553
- if (context.dependencies) collect(context, childAt(target, id));
554
- return current[id];
555
- }
556
- return childAccess(target, id, node.value, current[id]);
557
- };
558
- if (property === "put") return (id, value) => {
559
- const writable = writableCollection(target, node);
560
- put(context.session, writable, id, value);
561
- };
562
- if (property === "remove") return (id) => {
563
- const writable = writableCollection(target, node);
564
- remove$3(context.session, writable, id);
565
- };
566
- if (property === "replace") return (value) => replaceCollection(target, node, value);
567
- };
568
- const orderedMethod = (target, property, node) => {
569
- const at = addressOf(target);
570
- if (property === "ids") return () => {
571
- const current = collectionValue(target, node);
572
- collect(context, at, "collection");
573
- return node.kind === "table" ? [...current.ids] : current.map(node.keyOf);
574
- };
575
- if (property === "has") return (id) => {
576
- const current = collectionValue(target, node);
577
- collect(context, at, "collection", id);
578
- return node.kind === "table" ? Object.hasOwn(current.byId, id) : indexedKeys(current, node.keyOf).index(id) >= 0;
579
- };
580
- if (property === "get") return (id) => {
581
- const current = collectionValue(target, node);
582
- collect(context, at, "collection", id);
583
- const location = resolveChild(node, current, id);
584
- if (!location || !Object.hasOwn(location.parent, location.key)) return void 0;
585
- return childAccess(target, id, location.node, location.parent[location.key]);
586
- };
587
- if (property === "move") return (ids, position) => {
588
- const writable = writableCollection(target, node);
589
- move(context.session, writable, ids, position);
590
- };
591
- if (property === "reorder") return (ids) => {
592
- const writable = writableCollection(target, node);
593
- reorder(context.session, writable, ids);
594
- };
595
- };
596
- const tableMethod = (target, property, node) => {
597
- if (property === "create") return (input, valueOrPosition, position) => {
598
- const writable = writableCollection(target, node);
599
- create(context.session, writable, Array.isArray(input) ? input : [{
600
- id: input,
601
- value: valueOrPosition
602
- }], Array.isArray(input) ? valueOrPosition : position);
603
- };
604
- if (property === "remove") return (ids) => {
605
- const writable = writableCollection(target, node);
606
- remove$2(context.session, writable, typeof ids === "string" ? [ids] : ids);
607
- };
608
- if (property === "replace") return (...args) => {
609
- const [valueOrId, value] = args;
610
- if (args.length === 1) return replaceCollection(target, node, valueOrId);
611
- const writable = writableCollection(target, node);
612
- replace$3(context.session, writable, valueOrId, value);
613
- };
614
- return orderedMethod(target, property, node);
615
- };
616
- const listMethod = (target, property, node) => {
617
- if (property === "insert") return (value, position) => {
618
- const writable = writableCollection(target, node);
619
- insert$1(context.session, writable, value, position);
620
- };
621
- if (property === "remove") return (id) => {
622
- const writable = writableCollection(target, node);
623
- remove$1(context.session, writable, id);
624
- };
625
- if (property === "replace") return (...args) => {
626
- const [valueOrId, value] = args;
627
- if (args.length === 1) return replaceCollection(target, node, valueOrId);
628
- const writable = writableCollection(target, node);
629
- replace$2(context.session, writable, valueOrId, value);
630
- };
631
- return orderedMethod(target, property, node);
632
- };
633
- const treeMethod = (target, property, node) => {
634
- const at = addressOf(target);
635
- if (property === "insert") return (id, value, position) => {
636
- const writable = writableTree(target, node);
637
- insert(context.session, writable, id, value, position);
638
- };
639
- if (property === "remove") return (id) => {
640
- const writable = writableTree(target, node);
641
- remove(context.session, writable, id);
642
- };
643
- if (property === "move") return (id, position) => {
644
- const writable = writableTree(target, node);
645
- move$1(context.session, writable, id, position);
646
- };
647
- if (property === "replace") return (...args) => {
648
- const [valueOrId, value] = args;
649
- if (args.length === 1) return replaceCollection(target, node, valueOrId);
650
- const writable = writableTree(target, node);
651
- replace$1(context.session, writable, valueOrId, value);
652
- };
653
- const current = () => {
654
- collect(context, at);
655
- return collectionValue(target, node);
656
- };
657
- if (property === "rootId") return () => current().rootId;
658
- if (property === "get") return (id) => {
659
- const value = current();
660
- return contains(value, id) ? value.nodes[id].value : void 0;
661
- };
662
- if (property === "has") return (id) => contains(current(), id);
663
- if (property === "parent") return (id) => parent(current(), id);
664
- if (property === "children") return (id) => {
665
- const children$1 = children(current(), id);
666
- return children$1 ? [...children$1] : void 0;
667
- };
668
- };
669
- const handler = {
670
- get: (target, property) => {
671
- const { node, value } = resolve(context, target);
672
- if (!node || value === void 0) return void 0;
673
- if (typeof property !== "string") return void 0;
674
- if (node.kind === "map") return mapMethod(target, property, node);
675
- if (node.kind === "table") return tableMethod(target, property, node);
676
- if (node.kind === "list") return listMethod(target, property, node);
677
- if (node.kind === "tree") return treeMethod(target, property, node);
678
- if (node.kind === "variant" && property === node.tag) {
679
- collect(context, childAt(target, property));
680
- return value[property];
681
- }
682
- const member = compiledShape(node, value)?.members.get(property);
683
- if (!member) return void 0;
684
- if (member.field) {
685
- if (context.dependencies) collect(context, childAt(target, property));
686
- return value[property];
687
- }
688
- return childAccess(target, node.kind === "object" ? member : property, member.node, value[property]);
689
- },
690
- set: (target, property, value) => {
691
- const session = mutable(), { node } = resolve(context, target);
692
- if (typeof property !== "string" || !node || node.kind !== "object" && node.kind !== "variant") throw new TypeError("Invalid structural assignment.");
693
- if (node.kind === "variant" && node.tag === property) throw new TypeError("Variant discriminants are read-only.");
694
- session.assignMember(writableContainer(target, session), property, value);
695
- return true;
696
- },
697
- deleteProperty: (target, property) => {
698
- const session = mutable(), { node } = resolve(context, target);
699
- if (typeof property !== "string" || node?.kind === "map" || node?.kind === "variant" && node.tag === property) throw new TypeError("Invalid structural deletion.");
700
- session.removeMember(writableContainer(target, session), property);
701
- return true;
702
- },
703
- has: (target, property) => {
704
- const { node, value } = resolve(context, target);
705
- if (typeof property !== "string") return false;
706
- if (node?.kind === "map") return false;
707
- collect(context, addressOf(target));
708
- return value !== void 0 && Object.hasOwn(value, property);
709
- },
710
- ownKeys: (target) => {
711
- const { node, value } = resolve(context, target);
712
- if (node?.kind === "map") return [];
713
- if (context.dependencies) collect(context, addressOf(target));
714
- return value && typeof value === "object" ? Object.keys(value) : [];
715
- },
716
- getOwnPropertyDescriptor: (target, property) => {
717
- const { node, value } = resolve(context, target);
718
- if (node?.kind === "map") return void 0;
719
- return value && Object.hasOwn(value, property) ? {
720
- enumerable: true,
721
- configurable: true
722
- } : void 0;
723
- },
724
- defineProperty: () => {
725
- assertActive(context);
726
- throw new TypeError("Use schema assignments.");
727
- },
728
- setPrototypeOf: () => {
729
- assertActive(context);
730
- throw new TypeError("Document prototypes are immutable.");
731
- },
732
- preventExtensions: () => {
733
- assertActive(context);
734
- throw new TypeError("Document access cannot be frozen.");
735
- }
736
- };
737
- const root = resolve(context, createTarget(initial, void 0, ""));
738
- if (!root.node || root.node.kind === "field" || root.value === void 0) {
739
- if (context.dependencies) collect(context, initial);
740
- return root.value;
741
- }
742
- return access(root);
743
- };
744
- const collectionAccess = (context, at) => {
745
- const value = createAccess(context, at);
746
- if (value === void 0) return {
747
- get: () => {
748
- assertActive(context);
749
- },
750
- has: () => {
751
- assertActive(context);
752
- return false;
753
- },
754
- ids: () => {
755
- assertActive(context);
756
- return [];
757
- }
758
- };
759
- return value;
760
- };
761
- //#endregion
762
- //#region core/src/runtime/contract.ts
763
- var DocumentReentrancyError = class extends Error {
764
- constructor() {
765
- super("Document writes cannot be re-entered during an update or notification.");
766
- this.name = "DocumentReentrancyError";
767
- }
768
- };
769
- var DocumentDisposedError = class extends Error {
770
- constructor() {
771
- super("Doxum runtime has been disposed.");
772
- this.name = "DocumentDisposedError";
773
- }
774
- };
775
- var TransactionRejected = class extends Error {
776
- issues;
777
- constructor(input) {
778
- super("Document update rejected.");
779
- this.name = "TransactionRejected";
780
- const issues = Array.isArray(input) ? input : [input];
781
- this.issues = Object.freeze(issues.map((issue) => Object.freeze({
782
- code: issue.code,
783
- message: issue.message,
784
- source: "application",
785
- ...issue.address === void 0 ? {} : { address: Object.freeze([...issue.address]) }
786
- })));
787
- }
788
- };
789
- //#endregion
790
- //#region core/src/runtime/access.ts
791
- const states = /* @__PURE__ */ new WeakMap();
792
- const bindRuntimeAccess = (runtime, state) => {
793
- states.set(runtime, state);
794
- };
795
- const accessOf = (runtime) => {
796
- const state = states.get(runtime);
797
- if (!state) throw new Error("Unknown Doxum runtime.");
798
- return state;
799
- };
800
- const readWith = (runtime, run, dependencies) => {
801
- const state = accessOf(runtime);
802
- if (state.disposed) throw new DocumentDisposedError();
803
- state.projectionLocks = (state.projectionLocks ?? 0) + 1;
804
- try {
805
- return run(createAccess({
806
- state,
807
- dependencies
808
- }));
809
- } finally {
810
- state.projectionLocks--;
811
- }
812
- };
813
- //#endregion
814
- export { DocumentReentrancyError as a, createAccess as c, ParseError as d, checkKey as f, parse as g, equalValue as h, DocumentDisposedError as i, replace as l, copyValue as m, bindRuntimeAccess as n, TransactionRejected as o, checkValue as p, readWith as r, collectionAccess as s, accessOf as t, snapshot as u };
815
-
816
- //# sourceMappingURL=access-BeSV5ve-.js.map