@niscorp/moss 0.6.0 → 0.8.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/CHANGELOG.md +144 -0
- package/DESIGN.md +77 -40
- package/DOCS.md +673 -78
- package/README.md +2 -2
- package/dist/chunk-3DCTQKPQ.cjs +943 -0
- package/dist/chunk-3DCTQKPQ.cjs.map +1 -0
- package/dist/{chunk-WKBH3UMB.js → chunk-3T422CCN.js} +9 -3
- package/dist/chunk-3T422CCN.js.map +1 -0
- package/dist/chunk-6XC2MD7S.js +906 -0
- package/dist/chunk-6XC2MD7S.js.map +1 -0
- package/dist/{chunk-D3YZIAYV.cjs → chunk-I4UOWER3.cjs} +11 -5
- package/dist/chunk-I4UOWER3.cjs.map +1 -0
- package/dist/{chunk-VBWNHF4H.cjs → chunk-PVZMCEBX.cjs} +4 -3
- package/dist/chunk-PVZMCEBX.cjs.map +1 -0
- package/dist/{chunk-NSK7QSSG.js → chunk-QAHDSRPK.js} +4 -3
- package/dist/chunk-QAHDSRPK.js.map +1 -0
- package/dist/{chunk-R66WUKGC.cjs → chunk-TMDKNZYL.cjs} +2169 -1319
- package/dist/chunk-TMDKNZYL.cjs.map +1 -0
- package/dist/{chunk-V5YMJL7Z.js → chunk-YZILOVCR.js} +2147 -1295
- package/dist/chunk-YZILOVCR.js.map +1 -0
- package/dist/document-BYAY-Qle.d.ts +381 -0
- package/dist/document-DAE71J27.d.cts +381 -0
- package/dist/{index-B411sK9M.d.cts → index-CSEo7Tys.d.cts} +5 -0
- package/dist/{index-B411sK9M.d.ts → index-CSEo7Tys.d.ts} +5 -0
- package/dist/index.cjs +17 -17
- package/dist/index.d.cts +8 -10
- package/dist/index.d.ts +8 -10
- package/dist/index.js +3 -3
- package/dist/integrations.cjs +3 -3
- package/dist/integrations.d.cts +1 -1
- package/dist/integrations.d.ts +1 -1
- package/dist/integrations.js +1 -1
- package/dist/node.cjs +8 -8
- package/dist/node.d.cts +3 -4
- package/dist/node.d.ts +3 -4
- package/dist/node.js +4 -4
- package/dist/tide.d.cts +1 -1
- package/dist/tide.d.ts +1 -1
- package/dist/unverified.cjs +3 -3
- package/dist/unverified.js +2 -2
- package/dist/vite.cjs +9 -9
- package/dist/vite.d.cts +6 -1
- package/dist/vite.d.ts +6 -1
- package/dist/vite.js +4 -4
- package/package.json +13 -13
- package/dist/app-CUnDcXQu.d.ts +0 -143
- package/dist/app-CdANzi2q.d.cts +0 -143
- package/dist/chunk-D3YZIAYV.cjs.map +0 -1
- package/dist/chunk-NSK7QSSG.js.map +0 -1
- package/dist/chunk-PWUO4RET.js +0 -374
- package/dist/chunk-PWUO4RET.js.map +0 -1
- package/dist/chunk-R2YPQHQH.cjs +0 -383
- package/dist/chunk-R2YPQHQH.cjs.map +0 -1
- package/dist/chunk-R66WUKGC.cjs.map +0 -1
- package/dist/chunk-V5YMJL7Z.js.map +0 -1
- package/dist/chunk-VBWNHF4H.cjs.map +0 -1
- package/dist/chunk-WKBH3UMB.js.map +0 -1
- package/dist/document-D7nroBww.d.cts +0 -169
- package/dist/document-DeTPGBil.d.ts +0 -169
|
@@ -0,0 +1,943 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkPVZMCEBX_cjs = require('./chunk-PVZMCEBX.cjs');
|
|
4
|
+
var chunk3RIWTXGI_cjs = require('./chunk-3RIWTXGI.cjs');
|
|
5
|
+
var charter = require('@niscorp/charter');
|
|
6
|
+
var nova = require('@niscorp/nova');
|
|
7
|
+
var reflect = require('@niscorp/nova/reflect');
|
|
8
|
+
var strata = require('@niscorp/strata');
|
|
9
|
+
var vex = require('@niscorp/vex');
|
|
10
|
+
var zod = require('zod');
|
|
11
|
+
var i18n = require('@niscorp/nova/i18n');
|
|
12
|
+
var prism = require('@niscorp/prism');
|
|
13
|
+
|
|
14
|
+
// src/manifest/app.ts
|
|
15
|
+
var functionsIn = (written) => {
|
|
16
|
+
const ids = /* @__PURE__ */ new Set();
|
|
17
|
+
return Object.entries(written).map(([key, value]) => {
|
|
18
|
+
const definition = typeof value === "function" ? { id: key, description: "", execute: value } : value;
|
|
19
|
+
if (ids.has(definition.id)) throw new Error(`moss: functions(session) returned "${definition.id}" twice.`);
|
|
20
|
+
ids.add(definition.id);
|
|
21
|
+
return definition;
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
var functionsWarning = (written) => {
|
|
25
|
+
const undescribed = [];
|
|
26
|
+
for (const value of Object.values(written)) {
|
|
27
|
+
if (typeof value === "function") return "[moss] functions(session) returned a map of handlers, which is deprecated: return a list of { id, description, execute }.";
|
|
28
|
+
if (value.description === "") undescribed.push(value.id);
|
|
29
|
+
}
|
|
30
|
+
return undescribed.length === 0 ? void 0 : `[moss] functions with no description: ${undescribed.join(", ")}. Give each a description.`;
|
|
31
|
+
};
|
|
32
|
+
var defineApp = (app) => app;
|
|
33
|
+
var actionLayoutRef = (actionId) => `action/${actionId}`;
|
|
34
|
+
var isActionLayoutRef = (name) => name.startsWith("action/");
|
|
35
|
+
var actionOfLayoutRef = (ref) => isActionLayoutRef(ref) ? ref.slice("action/".length) : void 0;
|
|
36
|
+
var GENERATED_ID_PREFIX = "generated.";
|
|
37
|
+
var FRAME_LAYOUT_REF = "frame";
|
|
38
|
+
var canvasLayoutRef = (canvasId) => `canvas/${canvasId}`;
|
|
39
|
+
var keptLayoutNameRefusal = (name) => {
|
|
40
|
+
if (isActionLayoutRef(name)) return 'a name beginning with "action/" is where the layout of that action is kept';
|
|
41
|
+
if (name === FRAME_LAYOUT_REF) return `"${FRAME_LAYOUT_REF}" is where the layout of the shell is kept`;
|
|
42
|
+
if (name.startsWith("canvas/")) return 'a name beginning with "canvas/" is where the layout of that canvas is kept';
|
|
43
|
+
return void 0;
|
|
44
|
+
};
|
|
45
|
+
var entriesOf = (app) => app.entries ?? [];
|
|
46
|
+
var wearableOf = (app) => {
|
|
47
|
+
const declared = app.wearable;
|
|
48
|
+
const source = declared ?? Object.values(app.assignments ?? {});
|
|
49
|
+
const seen = /* @__PURE__ */ new Map();
|
|
50
|
+
for (const roles of source) {
|
|
51
|
+
const key = [...roles].sort().join("\0");
|
|
52
|
+
if (!seen.has(key)) seen.set(key, roles);
|
|
53
|
+
}
|
|
54
|
+
return [...seen.values()];
|
|
55
|
+
};
|
|
56
|
+
var candidateOf = (shipped, inForce, grants) => ({
|
|
57
|
+
charter: shipped.charter,
|
|
58
|
+
actions: inForce.actions,
|
|
59
|
+
layouts: shipped.layouts ?? {},
|
|
60
|
+
fragmentIds: new Set([shipped.shell, ...Object.values(shipped.pages ?? {})].flatMap((shell2) => Object.keys(shell2?.fragments ?? {}))),
|
|
61
|
+
fragmentCalls: [shipped.shell, ...Object.values(shipped.pages ?? {})].flatMap(
|
|
62
|
+
(shell2) => Object.entries(shell2?.fragments ?? {}).map(([id, fragment]) => ({ id, calls: functionsCalledBy(fragment) }))
|
|
63
|
+
),
|
|
64
|
+
wearable: wearableOf(shipped),
|
|
65
|
+
grants,
|
|
66
|
+
entries: inForce.entries,
|
|
67
|
+
profiles: vex.scopeProfiles(shipped.behaviors ?? {})
|
|
68
|
+
});
|
|
69
|
+
var withFragments = (candidate, fragments) => ({
|
|
70
|
+
...candidate,
|
|
71
|
+
fragmentIds: /* @__PURE__ */ new Set([...candidate.fragmentIds, ...Object.keys(fragments)]),
|
|
72
|
+
fragmentCalls: [...candidate.fragmentCalls, ...Object.entries(fragments).map(([id, fragment]) => ({ id, calls: functionsCalledBy(fragment) }))]
|
|
73
|
+
});
|
|
74
|
+
var unreadActions = (actions) => Object.entries(actions).flatMap(([id, definition]) => {
|
|
75
|
+
if (strata.exceedsDepth(definition)) return [{ id, why: strata.depthRefusal() }];
|
|
76
|
+
const parsed = nova.ActionDefinitionSchema.safeParse(definition);
|
|
77
|
+
if (parsed.success) return [];
|
|
78
|
+
const first = parsed.error.issues[0];
|
|
79
|
+
return [{ id, why: first === void 0 ? "no reason given" : `${first.path.join(".") || "(root)"}: ${first.message}` }];
|
|
80
|
+
});
|
|
81
|
+
var layoutsNamedIn = (node) => {
|
|
82
|
+
if (Array.isArray(node)) return node.flatMap(layoutsNamedIn);
|
|
83
|
+
if (!reflect.isRecord(node)) return [];
|
|
84
|
+
const ref = node["ref"];
|
|
85
|
+
if (typeof ref === "string" && Object.keys(node).length === 1) return [ref];
|
|
86
|
+
return Object.entries(node).flatMap(([key, inner]) => key === "props" ? [] : layoutsNamedIn(inner));
|
|
87
|
+
};
|
|
88
|
+
var keptLayoutNamedIn = (layout2) => (typeof layout2 === "string" ? [layout2] : layoutsNamedIn(layout2)).find((name) => keptLayoutNameRefusal(name) !== void 0);
|
|
89
|
+
var layoutNameRefusal = (layout2) => {
|
|
90
|
+
const named = keptLayoutNamedIn(layout2);
|
|
91
|
+
return named === void 0 ? void 0 : `names the layout "${named}": ${keptLayoutNameRefusal(named)}`;
|
|
92
|
+
};
|
|
93
|
+
var layoutNameErrors = (actions) => Object.entries(actions).flatMap(([id, definition]) => {
|
|
94
|
+
const refusal = layoutNameRefusal(definition.layout);
|
|
95
|
+
return refusal === void 0 ? [] : [`"${id}" ${refusal}`];
|
|
96
|
+
});
|
|
97
|
+
var generatedIdErrors = (actions) => Object.entries(actions).flatMap(([listed, definition]) => {
|
|
98
|
+
const taken = [listed, definition.id].find((id) => id.startsWith(GENERATED_ID_PREFIX));
|
|
99
|
+
return taken === void 0 ? [] : [`"${taken}" begins with "${GENERATED_ID_PREFIX}", which is where what somebody generated is kept`];
|
|
100
|
+
});
|
|
101
|
+
var fragmentIdErrors = (actions, fragmentIds) => Object.entries(actions).flatMap(([listed, definition]) => {
|
|
102
|
+
const taken = [listed, definition.id].find((id) => fragmentIds.has(id));
|
|
103
|
+
return taken === void 0 ? [] : [`"${taken}" is the id of an action and of a fragment: an id names one of the two`];
|
|
104
|
+
});
|
|
105
|
+
var reachErrors = (entries, profiles) => {
|
|
106
|
+
const unknown = entries.flatMap((entry2) => entry2.reach !== void 0 && !profiles.has(entry2.reach) ? [`${entry2.fingerprint} declares reach "${entry2.reach}"`] : []);
|
|
107
|
+
return unknown.length === 0 ? [] : [...unknown, `known profiles: ${[...profiles].join(", ") || "(none)"}`];
|
|
108
|
+
};
|
|
109
|
+
var functionsCalledBy = (definition) => Object.values(definition.endpoints ?? {}).flatMap((endpoint) => "fn" in endpoint && !endpoint.fn.includes("{{") ? [endpoint.fn] : []);
|
|
110
|
+
var combinationsOf = (candidate) => {
|
|
111
|
+
const combinations = /* @__PURE__ */ new Map();
|
|
112
|
+
for (const roles of [...Object.keys(candidate.charter).map((role) => [role]), ...candidate.wearable, ["public"]]) combinations.set([...roles].sort().join("\0"), roles);
|
|
113
|
+
return [...combinations.values()];
|
|
114
|
+
};
|
|
115
|
+
var functionGrantErrors = (candidate) => {
|
|
116
|
+
const errors = /* @__PURE__ */ new Set();
|
|
117
|
+
for (const roles of combinationsOf(candidate)) {
|
|
118
|
+
let held;
|
|
119
|
+
try {
|
|
120
|
+
held = charter.resolvePrincipal(candidate.charter, Object.keys(candidate.actions), roles, "actions");
|
|
121
|
+
} catch {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (held.size === 0) continue;
|
|
125
|
+
const calls = [
|
|
126
|
+
...[...held].flatMap((id) => functionsCalledBy(candidate.actions[id] ?? {}).map((fn) => ({ by: `"${id}"`, fn }))),
|
|
127
|
+
...candidate.fragmentCalls.flatMap(({ id, calls: fns }) => fns.map((fn) => ({ by: `the fragment "${id}", which any of them may wear`, fn })))
|
|
128
|
+
];
|
|
129
|
+
const granted = charter.resolvePrincipal(candidate.charter, calls.map((call) => call.fn), roles, "functions");
|
|
130
|
+
const who = roles.map((role) => `"${role}"`).join(", ");
|
|
131
|
+
for (const call of calls) {
|
|
132
|
+
if (!granted.has(call.fn)) errors.add(`${who} hold ${call.by}, which calls "${call.fn}": their functions do not grant it`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return [...errors];
|
|
136
|
+
};
|
|
137
|
+
var unknownFunctionErrors = (candidate) => {
|
|
138
|
+
const known = candidate.functionIds;
|
|
139
|
+
if (known === void 0) return [];
|
|
140
|
+
return [
|
|
141
|
+
...Object.entries(candidate.actions).flatMap(([id, definition]) => functionsCalledBy(definition).map((fn) => ({ by: `"${id}"`, fn }))),
|
|
142
|
+
...candidate.fragmentCalls.flatMap(({ id, calls }) => calls.map((fn) => ({ by: `the fragment "${id}"`, fn })))
|
|
143
|
+
].flatMap(({ by, fn }) => known.has(fn) ? [] : [`${by} calls "${fn}", which the app has no function of`]);
|
|
144
|
+
};
|
|
145
|
+
var everyFunctionGranted = (charter$1) => Object.entries(charter$1).flatMap(
|
|
146
|
+
([role, def]) => charter.normalizeRole(def, "functions").allow.includes("*") ? [`role "${role}" allows every function ('*'): the generated actions of the people who hold it, or a role that extends it, may call any it does not deny`] : []
|
|
147
|
+
);
|
|
148
|
+
var endlessChains = (actions) => reflect.chainCycles(actions).map((cycle) => ({ path: cycle.path, through: cycle.steps.map((step) => step.action) }));
|
|
149
|
+
var verifyManifest = (candidate) => {
|
|
150
|
+
const unread = unreadActions(candidate.actions);
|
|
151
|
+
const refused = new Set(unread.map((action) => action.id));
|
|
152
|
+
const read = refused.size === 0 ? candidate : { ...candidate, actions: Object.fromEntries(Object.entries(candidate.actions).filter(([id]) => !refused.has(id))) };
|
|
153
|
+
const charter$1 = charter.verifyCharter(
|
|
154
|
+
read.charter,
|
|
155
|
+
{ actions: Object.keys(read.actions), data: read.grants, layouts: Object.keys(read.layouts) },
|
|
156
|
+
read.wearable,
|
|
157
|
+
auditClosure(read.actions, read.layouts)
|
|
158
|
+
);
|
|
159
|
+
const found = (check, details) => details.map((detail) => ({ check, detail }));
|
|
160
|
+
return {
|
|
161
|
+
errors: [
|
|
162
|
+
...found("actions", unread.map((action) => `"${action.id}" is not an action nova reads: ${action.why}`)),
|
|
163
|
+
...found("actions", layoutNameErrors(read.actions)),
|
|
164
|
+
...found("actions", generatedIdErrors(read.actions)),
|
|
165
|
+
...found("actions", fragmentIdErrors(read.actions, read.fragmentIds)),
|
|
166
|
+
...found("charter", charter$1.errors.map((error) => `${error.rule}: ${error.detail}`)),
|
|
167
|
+
...found("functions", [...unknownFunctionErrors(read), ...functionGrantErrors(read)]),
|
|
168
|
+
...found("reach", reachErrors(read.entries, read.profiles)),
|
|
169
|
+
...found("variants", verifyVariants(read))
|
|
170
|
+
],
|
|
171
|
+
said: [
|
|
172
|
+
...endlessChains(read.actions).map((chain) => `a chain of steps that never ends: ${chain.path} \u2014 nova stops it at its budget when it runs (RUNAWAY_CHAIN)`),
|
|
173
|
+
...everyFunctionGranted(read.charter)
|
|
174
|
+
],
|
|
175
|
+
charter: charter$1
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
var verifiedAtBoot = (candidate) => {
|
|
179
|
+
const report = verifyManifest(candidate);
|
|
180
|
+
for (const line of report.said) console.error(`[moss] ${line}`);
|
|
181
|
+
const refused = refusalOf(report, "boot");
|
|
182
|
+
if (refused !== void 0) throw new Error(refused);
|
|
183
|
+
return report;
|
|
184
|
+
};
|
|
185
|
+
var charterOverFunctions = (charter$1, universes, ids) => {
|
|
186
|
+
const before = charter.verifyCharter(charter$1, universes);
|
|
187
|
+
const known = new Set([...before.errors, ...before.warnings].map((issue) => issue.detail));
|
|
188
|
+
const after = charter.verifyCharter(charter$1, { ...universes, functions: ids });
|
|
189
|
+
return {
|
|
190
|
+
errors: after.errors.flatMap((issue) => known.has(issue.detail) ? [] : [`${issue.rule}: ${issue.detail}`]),
|
|
191
|
+
warnings: after.warnings.flatMap((issue) => known.has(issue.detail) ? [] : [issue.detail])
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
var verifyApp = (app, grants) => verifyManifest(candidateOf(app, { actions: app.actions, entries: entriesOf(app) }, grants));
|
|
195
|
+
var HEADLINES = {
|
|
196
|
+
boot: {
|
|
197
|
+
actions: "Actions are not ones nova reads \u2014 refusing to serve:",
|
|
198
|
+
charter: "Charter is incoherent \u2014 refusing to serve:",
|
|
199
|
+
functions: "Actions call functions the app does not have, or that their holders\u2019 charter does not grant \u2014 refusing to serve:",
|
|
200
|
+
reach: "Entries declare a reach no table offers \u2014 refusing to serve:",
|
|
201
|
+
variants: "Layout variants are incoherent \u2014 refusing to serve:"
|
|
202
|
+
},
|
|
203
|
+
publish: {
|
|
204
|
+
actions: "Publish refused \u2014 actions nova does not read:",
|
|
205
|
+
charter: "Publish refused \u2014 charter incoherent over what is published:",
|
|
206
|
+
functions: "Publish refused \u2014 actions call functions the app does not have, or that their holders\u2019 charter does not grant:",
|
|
207
|
+
reach: "Publish refused \u2014 entries declare a reach no table offers:",
|
|
208
|
+
variants: "Publish refused \u2014 layout variants incoherent:"
|
|
209
|
+
},
|
|
210
|
+
refresh: {
|
|
211
|
+
actions: "Refresh refused \u2014 actions nova does not read:",
|
|
212
|
+
charter: "Refresh refused \u2014 charter incoherent over the new artifacts:",
|
|
213
|
+
functions: "Refresh refused \u2014 actions call functions the app does not have, or that their holders\u2019 charter does not grant:",
|
|
214
|
+
reach: "Refresh refused \u2014 entries declare a reach no table offers:",
|
|
215
|
+
variants: "Refresh refused \u2014 layout variants incoherent:"
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
var refusalOf = (report, moment) => {
|
|
219
|
+
const first = report.errors[0];
|
|
220
|
+
if (first === void 0) return void 0;
|
|
221
|
+
const findings = report.errors.filter((error) => error.check === first.check).map((error) => ` ${error.detail}`);
|
|
222
|
+
return [HEADLINES[moment][first.check], ...findings].join("\n");
|
|
223
|
+
};
|
|
224
|
+
var verifyVariants = (app) => {
|
|
225
|
+
const variants = app.layouts ?? {};
|
|
226
|
+
const ids = Object.keys(variants);
|
|
227
|
+
if (ids.length === 0) return [];
|
|
228
|
+
const errors = [];
|
|
229
|
+
for (const [id, variant] of Object.entries(variants)) {
|
|
230
|
+
if (app.actions[variant.action] === void 0) {
|
|
231
|
+
errors.push(`layout variant "${id}" reshapes unknown action "${variant.action}"`);
|
|
232
|
+
}
|
|
233
|
+
const refusal = layoutNameRefusal(variant.layout);
|
|
234
|
+
if (refusal !== void 0) errors.push(`layout variant "${id}" ${refusal}`);
|
|
235
|
+
}
|
|
236
|
+
const wearers = /* @__PURE__ */ new Map();
|
|
237
|
+
for (const role of Object.keys(app.charter)) wearers.set(`role "${role}"`, [role]);
|
|
238
|
+
for (const roles of wearableOf(app)) wearers.set(`the combination [${[...roles].join(", ")}]`, roles);
|
|
239
|
+
wearers.set("the anonymous principal", ["public"]);
|
|
240
|
+
for (const [who, roles] of wearers) {
|
|
241
|
+
let granted;
|
|
242
|
+
try {
|
|
243
|
+
granted = charter.resolvePrincipal(app.charter, ids, roles, "layouts");
|
|
244
|
+
} catch {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
const byAction = /* @__PURE__ */ new Map();
|
|
248
|
+
for (const id of granted) {
|
|
249
|
+
const variant = variants[id];
|
|
250
|
+
if (variant !== void 0) byAction.set(variant.action, [...byAction.get(variant.action) ?? [], id]);
|
|
251
|
+
}
|
|
252
|
+
for (const [action, held] of byAction) {
|
|
253
|
+
if (held.length > 1) {
|
|
254
|
+
errors.push(`${who} holds ${held.length} variants of "${action}" (${held.sort().join(", ")}) \u2014 one variant per action per principal`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return errors;
|
|
259
|
+
};
|
|
260
|
+
var variantLayouts = (variants, held) => {
|
|
261
|
+
const layouts = /* @__PURE__ */ new Map();
|
|
262
|
+
for (const id of held) {
|
|
263
|
+
const variant = variants[id];
|
|
264
|
+
if (variant !== void 0) layouts.set(variant.action, variant.layout);
|
|
265
|
+
}
|
|
266
|
+
return layouts;
|
|
267
|
+
};
|
|
268
|
+
var withVariants = (actions, ids, layouts) => ids.flatMap((id) => {
|
|
269
|
+
const definition = actions[id];
|
|
270
|
+
if (definition === void 0) return [];
|
|
271
|
+
const layout2 = layouts.get(id);
|
|
272
|
+
return [[id, layout2 === void 0 ? definition : { ...definition, layout: layout2 }]];
|
|
273
|
+
});
|
|
274
|
+
var auditClosure = (definitions, variants = {}) => {
|
|
275
|
+
return (grantedIds, layoutIds = []) => {
|
|
276
|
+
const granted = withVariants(definitions, grantedIds, variantLayouts(variants, layoutIds)).map(([, definition]) => definition);
|
|
277
|
+
const catalog = granted.map((definition) => ({ id: definition.id, input: definition.input ?? {} }));
|
|
278
|
+
const channels = [
|
|
279
|
+
...new Set(
|
|
280
|
+
granted.flatMap((definition) => {
|
|
281
|
+
const found = nova.collectChannels(definition);
|
|
282
|
+
return [...found.emits, ...found.listens];
|
|
283
|
+
})
|
|
284
|
+
)
|
|
285
|
+
];
|
|
286
|
+
const issues = [];
|
|
287
|
+
for (const definition of granted) {
|
|
288
|
+
const audit = nova.auditAction(definition, { catalog, channels });
|
|
289
|
+
if (audit.ok) continue;
|
|
290
|
+
for (const issue of audit.issues) {
|
|
291
|
+
if (issue.includes("which is not in the catalog") && !issue.includes("{{")) issues.push(`${definition.id}: ${issue}`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return issues;
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
var isDeferred = (value) => typeof value === "string" && i18n.isBinding(value) || reflect.isRecord(value) && Object.keys(value).some((key) => key.startsWith("$"));
|
|
298
|
+
var layoutMisfits = (layout2, components) => {
|
|
299
|
+
const found = /* @__PURE__ */ new Set();
|
|
300
|
+
reflect.walkNodes(layout2, (record2) => {
|
|
301
|
+
const name = record2["component"];
|
|
302
|
+
if (typeof name !== "string") return;
|
|
303
|
+
const spec = components.get(name);
|
|
304
|
+
if (spec === void 0) {
|
|
305
|
+
found.add(`layout uses "${name}", which this app has no component for`);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const schema = spec.propsSchema;
|
|
309
|
+
const props = record2["props"];
|
|
310
|
+
if (!(schema instanceof zod.z.ZodObject) || !reflect.isRecord(props)) return;
|
|
311
|
+
const shape = schema.shape;
|
|
312
|
+
for (const [key, value] of Object.entries(props)) {
|
|
313
|
+
const prop = shape[key];
|
|
314
|
+
if (prop === void 0) {
|
|
315
|
+
found.add(`"${name}" has no prop "${key}"`);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (isDeferred(value)) continue;
|
|
319
|
+
const check = zod.z.safeParse(prop, value);
|
|
320
|
+
if (!check.success) found.add(`"${name}" prop "${key}" \u2014 ${check.error.issues[0]?.message ?? "invalid"}`);
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
return [...found];
|
|
324
|
+
};
|
|
325
|
+
var seam = zod.z.custom((value) => typeof value === "function", "expected a function");
|
|
326
|
+
var depthChecked = (schema) => zod.z.unknown().refine((value) => !strata.exceedsDepth(value), { message: strata.depthRefusal(), abort: true }).pipe(schema);
|
|
327
|
+
var strings = zod.z.array(zod.z.string()).readonly();
|
|
328
|
+
var record = zod.z.record(zod.z.string(), zod.z.unknown());
|
|
329
|
+
var layout = depthChecked(nova.LayoutNodeSchema);
|
|
330
|
+
var LayoutVariantSchema = zod.z.object({ action: zod.z.string(), layout }).strict();
|
|
331
|
+
var CanvasSeedSchema = zod.z.union([zod.z.string(), zod.z.object({ action: zod.z.string(), input: record.optional(), with: strings.optional() }).strict()]);
|
|
332
|
+
var CanvasSchema = zod.z.object({
|
|
333
|
+
id: zod.z.string(),
|
|
334
|
+
mode: zod.z.enum(["stack", "list"]).optional(),
|
|
335
|
+
actionLayout: zod.z.union([zod.z.string(), layout]).optional(),
|
|
336
|
+
initial: zod.z.union([CanvasSeedSchema, zod.z.array(CanvasSeedSchema)]).optional()
|
|
337
|
+
}).strict();
|
|
338
|
+
var ComponentContractSchema = zod.z.looseObject({ meta: zod.z.looseObject({ description: zod.z.string().optional() }).optional() });
|
|
339
|
+
var storedLayoutName = zod.z.string().superRefine((name, ctx) => {
|
|
340
|
+
const refusal = keptLayoutNameRefusal(name);
|
|
341
|
+
if (refusal !== void 0) ctx.addIssue({ code: "custom", message: refusal });
|
|
342
|
+
});
|
|
343
|
+
var shippedFragmentId = zod.z.string().refine((id) => !id.startsWith(GENERATED_ID_PREFIX), { message: `an id beginning with "${GENERATED_ID_PREFIX}" is where what somebody generated is kept` });
|
|
344
|
+
var shell = {
|
|
345
|
+
canvases: zod.z.array(CanvasSchema),
|
|
346
|
+
layout: layout.optional(),
|
|
347
|
+
layoutStore: zod.z.record(storedLayoutName, layout).optional(),
|
|
348
|
+
fragments: zod.z.record(shippedFragmentId, depthChecked(nova.ActionFragmentSchema)).optional(),
|
|
349
|
+
seeds: seam.optional(),
|
|
350
|
+
components: zod.z.record(zod.z.string(), ComponentContractSchema).optional(),
|
|
351
|
+
inputs: seam.optional()
|
|
352
|
+
};
|
|
353
|
+
var ShellManifestSchema = zod.z.object(shell).strict();
|
|
354
|
+
var PageManifestSchema = zod.z.object({ ...shell, path: zod.z.string(), params: zod.z.string().optional() }).strict();
|
|
355
|
+
var entry = { fingerprint: zod.z.string(), intent: zod.z.string().optional(), reach: zod.z.string().optional() };
|
|
356
|
+
var ReadEntrySchema = zod.z.object({
|
|
357
|
+
...entry,
|
|
358
|
+
shape: zod.z.unknown().optional(),
|
|
359
|
+
dsl: depthChecked(vex.QuerySchema),
|
|
360
|
+
mapping: depthChecked(prism.ConfigSchema).optional(),
|
|
361
|
+
refresh: zod.z.enum(vex.REFRESH_MODES).optional()
|
|
362
|
+
}).strict();
|
|
363
|
+
var WriteEntrySchema = zod.z.object({ ...entry, mutation: depthChecked(vex.MutationDefinitionSchema) }).strict();
|
|
364
|
+
var EntriesSchema = zod.z.array(zod.z.union([ReadEntrySchema, WriteEntrySchema]));
|
|
365
|
+
var FeatureSchema = zod.z.looseObject({
|
|
366
|
+
name: zod.z.string(),
|
|
367
|
+
tables: zod.z.object({ names: strings, sequences: zod.z.array(strata.SequenceSchema).readonly().optional() }).strict().optional(),
|
|
368
|
+
start: seam
|
|
369
|
+
});
|
|
370
|
+
var ReactionSchema = zod.z.object({ table: zod.z.string(), op: zod.z.enum(["insert", "update", "delete"]).optional(), run: seam }).strict();
|
|
371
|
+
var fields = {
|
|
372
|
+
charter: record,
|
|
373
|
+
assignments: zod.z.record(zod.z.string(), strings).optional(),
|
|
374
|
+
wearable: zod.z.array(strings).readonly().optional(),
|
|
375
|
+
actions: zod.z.record(zod.z.string(), depthChecked(nova.ActionDefinitionSchema)),
|
|
376
|
+
layouts: zod.z.record(zod.z.string(), LayoutVariantSchema).optional(),
|
|
377
|
+
behaviors: zod.z.record(zod.z.string(), record).optional(),
|
|
378
|
+
scope: seam.optional(),
|
|
379
|
+
identity: zod.z.object({ as: zod.z.string().optional(), resolve: seam }).strict().optional(),
|
|
380
|
+
phrases: seam.optional(),
|
|
381
|
+
phraseKeys: zod.z.object({ props: strings.optional(), suffixes: strings.optional(), text: zod.z.boolean().optional() }).strict().optional(),
|
|
382
|
+
features: zod.z.array(FeatureSchema).readonly().optional(),
|
|
383
|
+
entries: EntriesSchema.readonly().optional(),
|
|
384
|
+
resources: zod.z.record(zod.z.string(), zod.z.union([strings, zod.z.looseObject({ entities: strings })])).optional(),
|
|
385
|
+
shell: ShellManifestSchema.optional(),
|
|
386
|
+
pages: zod.z.record(zod.z.string(), PageManifestSchema).optional(),
|
|
387
|
+
grammars: zod.z.array(strata.SequenceSchema).readonly().optional(),
|
|
388
|
+
functions: seam.optional(),
|
|
389
|
+
onSession: seam.optional(),
|
|
390
|
+
runs: seam.optional(),
|
|
391
|
+
reactions: zod.z.array(ReactionSchema).readonly().optional()
|
|
392
|
+
};
|
|
393
|
+
var ManifestSchema = zod.z.object(fields).strict();
|
|
394
|
+
var placesOf = (issue, under) => {
|
|
395
|
+
const path = [...under, ...issue.path];
|
|
396
|
+
if (issue.code === "invalid_key") return [{ path, message: issue.issues[0]?.message ?? issue.message }];
|
|
397
|
+
if (issue.code !== "invalid_union") return [{ path, message: issue.message }];
|
|
398
|
+
const branches = issue.errors.map((branch) => branch.flatMap((inner) => placesOf(inner, path))).filter((branch) => branch.length > 0);
|
|
399
|
+
const reach = (places) => Math.max(...places.map((place) => place.path.length));
|
|
400
|
+
const closer = (best, branch) => reach(branch) > reach(best) || reach(branch) === reach(best) && branch.length < best.length ? branch : best;
|
|
401
|
+
const [first, ...others] = branches;
|
|
402
|
+
return first === void 0 ? [{ path, message: issue.message }] : others.reduce(closer, first);
|
|
403
|
+
};
|
|
404
|
+
var misfitsOf = (error, under, whole2) => error.issues.flatMap((issue) => placesOf(issue, under)).map((place) => `${place.path.map(String).join(".") || whole2}: ${place.message}`);
|
|
405
|
+
var manifestMisfits = (app) => {
|
|
406
|
+
const parsed = ManifestSchema.safeParse(app);
|
|
407
|
+
return parsed.success ? [] : misfitsOf(parsed.error, [], "(the manifest)");
|
|
408
|
+
};
|
|
409
|
+
var entryMisfits = (entries) => {
|
|
410
|
+
const parsed = EntriesSchema.safeParse(entries);
|
|
411
|
+
return parsed.success ? [] : misfitsOf(parsed.error, ["entries"], "entries");
|
|
412
|
+
};
|
|
413
|
+
var isData = (value) => {
|
|
414
|
+
if (Array.isArray(value)) return true;
|
|
415
|
+
const made = Object.getPrototypeOf(value);
|
|
416
|
+
return made === Object.prototype || made === null;
|
|
417
|
+
};
|
|
418
|
+
var freezeDeep = (data) => {
|
|
419
|
+
const seen = /* @__PURE__ */ new Set();
|
|
420
|
+
const pending = [data];
|
|
421
|
+
while (pending.length > 0) {
|
|
422
|
+
const value = pending.pop();
|
|
423
|
+
if (typeof value !== "object" || value === null || !isData(value) || seen.has(value)) continue;
|
|
424
|
+
seen.add(value);
|
|
425
|
+
Object.freeze(value);
|
|
426
|
+
for (const inner of Object.values(value)) pending.push(inner);
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
var receiveManifest = (app) => {
|
|
430
|
+
const misfits = manifestMisfits(app);
|
|
431
|
+
if (misfits.length > 0) throw new Error(["The manifest does not parse \u2014 refusing to serve:", ...misfits.map((misfit) => ` ${misfit}`)].join("\n"));
|
|
432
|
+
freezeDeep(app);
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
// src/shells/draft.ts
|
|
436
|
+
var noDraft = () => ({ steps: [], shown: 0 });
|
|
437
|
+
var draftedIn = (draft) => draft.steps[draft.shown - 1] ?? [];
|
|
438
|
+
var SHELL_GONE = "moss: this session\u2019s shell is gone, and its draft with it.";
|
|
439
|
+
var sessionDraftOn = ({ saving, liveOf, adopt }) => {
|
|
440
|
+
const standing = () => {
|
|
441
|
+
const live = liveOf();
|
|
442
|
+
if (live === void 0) throw new Error("moss: a function touched the draft before the session finished building.");
|
|
443
|
+
if (live.ended) throw new Error(SHELL_GONE);
|
|
444
|
+
return live;
|
|
445
|
+
};
|
|
446
|
+
const savingOrRefuse = () => {
|
|
447
|
+
if (saving === void 0) throw new Error("moss: this shell host was given nothing to keep a draft with.");
|
|
448
|
+
return saving;
|
|
449
|
+
};
|
|
450
|
+
let turn = Promise.resolve();
|
|
451
|
+
const inTurn = (run) => {
|
|
452
|
+
const answer = turn.then(run, run);
|
|
453
|
+
turn = answer.catch(() => void 0);
|
|
454
|
+
return answer;
|
|
455
|
+
};
|
|
456
|
+
const stepped = async (live, drafted) => {
|
|
457
|
+
const steps = [...live.draft.steps.slice(0, live.draft.shown), drafted];
|
|
458
|
+
await adopt(live, { steps, shown: steps.length });
|
|
459
|
+
return steps.length;
|
|
460
|
+
};
|
|
461
|
+
return {
|
|
462
|
+
add: (artifacts) => inTurn(async () => {
|
|
463
|
+
const live = standing();
|
|
464
|
+
const answered = await savingOrRefuse().add(artifacts, draftedIn(live.draft));
|
|
465
|
+
return { step: await stepped(live, answered.drafted), artifacts: answered.added };
|
|
466
|
+
}),
|
|
467
|
+
list: () => draftedIn(standing().draft),
|
|
468
|
+
remove: (kind, id) => inTurn(async () => {
|
|
469
|
+
const live = standing();
|
|
470
|
+
const drafted = draftedIn(live.draft);
|
|
471
|
+
if (!drafted.some((one) => one.kind === kind && one.id === id)) throw new Error(`moss: no ${String(kind)} "${id}" is in the draft.`);
|
|
472
|
+
return { step: await stepped(live, drafted.filter((one) => one.kind !== kind || one.id !== id)) };
|
|
473
|
+
}),
|
|
474
|
+
show: (step) => inTurn(async () => {
|
|
475
|
+
const live = standing();
|
|
476
|
+
const last = live.draft.steps.length;
|
|
477
|
+
if (!Number.isInteger(step) || step < 0 || step > last) throw new Error(`moss: the draft has no step ${String(step)}. Its steps: 0 to ${last}.`);
|
|
478
|
+
await adopt(live, { steps: live.draft.steps, shown: step });
|
|
479
|
+
}),
|
|
480
|
+
discard: () => inTurn(async () => adopt(standing(), noDraft())),
|
|
481
|
+
save: () => inTurn(async () => {
|
|
482
|
+
const live = standing();
|
|
483
|
+
const saved = await savingOrRefuse().save(draftedIn(live.draft));
|
|
484
|
+
live.draft = noDraft();
|
|
485
|
+
return saved;
|
|
486
|
+
})
|
|
487
|
+
};
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
// src/shells/host-state.ts
|
|
491
|
+
var layoutsOf = (made) => ({
|
|
492
|
+
layoutStore: made.layoutStore ?? {},
|
|
493
|
+
fragments: made.fragments ?? {}
|
|
494
|
+
});
|
|
495
|
+
var withLayoutInPlace = (definition, store) => {
|
|
496
|
+
const layout2 = definition.layout;
|
|
497
|
+
return layout2 === void 0 || typeof layout2 === "string" ? definition : { ...definition, layout: store.resolveReferences(layout2) };
|
|
498
|
+
};
|
|
499
|
+
var holdGranted = (functions, granted) => {
|
|
500
|
+
const callable = granted(Object.keys(functions.all));
|
|
501
|
+
for (const name of Object.keys(functions.held)) if (!callable.has(name)) Reflect.deleteProperty(functions.held, name);
|
|
502
|
+
for (const name of callable) {
|
|
503
|
+
const handler = functions.all[name];
|
|
504
|
+
if (handler !== void 0) functions.held[name] = handler;
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
var canvasesOf = (live) => Object.keys(live.shell.getState().canvases);
|
|
508
|
+
var endShell = (host, live) => {
|
|
509
|
+
live.ended = true;
|
|
510
|
+
host.living.delete(live);
|
|
511
|
+
};
|
|
512
|
+
var treeOf = (live, canvasId) => nova.canvasTreeOf(live.shell, canvasId);
|
|
513
|
+
var canvasMessage = (live, canvasId) => {
|
|
514
|
+
const message = { type: "render", canvas: canvasId, tree: treeOf(live, canvasId) };
|
|
515
|
+
return JSON.stringify(message);
|
|
516
|
+
};
|
|
517
|
+
var canvasMessageOf = (live, canvasId) => {
|
|
518
|
+
try {
|
|
519
|
+
return canvasMessage(live, canvasId);
|
|
520
|
+
} catch (error) {
|
|
521
|
+
console.error(`[moss/shells] canvas "${canvasId}" failed to render \u2014 holding its last tree:`, error);
|
|
522
|
+
return null;
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
var DELTA_CEILING = 0.6;
|
|
526
|
+
var DELTA_MAX_FRAME = 64 * 1024;
|
|
527
|
+
var shorterAsDelta = (previous, next, canvasId) => {
|
|
528
|
+
if (previous.length > DELTA_MAX_FRAME || next.length > DELTA_MAX_FRAME) return null;
|
|
529
|
+
try {
|
|
530
|
+
const ops = chunk3RIWTXGI_cjs.encodeDelta(previous, next);
|
|
531
|
+
const message = { type: "render-delta", canvas: canvasId, ops, hash: chunk3RIWTXGI_cjs.frameHash(next) };
|
|
532
|
+
const text = JSON.stringify(message);
|
|
533
|
+
return text.length <= next.length * DELTA_CEILING ? text : null;
|
|
534
|
+
} catch (error) {
|
|
535
|
+
console.error(`[moss/shells] canvas "${canvasId}" failed to encode as a delta \u2014 sending it whole:`, error);
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
var pass = (live, delta) => {
|
|
540
|
+
if (live.ended) return;
|
|
541
|
+
const canvases = canvasesOf(live);
|
|
542
|
+
for (const gone of [...live.sentByCanvas.keys()].filter((canvasId) => !canvases.includes(canvasId))) {
|
|
543
|
+
live.sentByCanvas.delete(gone);
|
|
544
|
+
const empty = JSON.stringify({ type: "render", canvas: gone, tree: [] });
|
|
545
|
+
for (const connection of live.connections) connection.send(empty);
|
|
546
|
+
}
|
|
547
|
+
for (const canvasId of canvases) {
|
|
548
|
+
const next = canvasMessageOf(live, canvasId);
|
|
549
|
+
const previous = live.sentByCanvas.get(canvasId);
|
|
550
|
+
if (next === null || previous === next) continue;
|
|
551
|
+
live.sentByCanvas.set(canvasId, next);
|
|
552
|
+
const short = delta && previous !== void 0 && live.deltaReady.size > 0 ? shorterAsDelta(previous, next, canvasId) : null;
|
|
553
|
+
for (const connection of live.connections) {
|
|
554
|
+
connection.send(short !== null && live.deltaReady.has(connection) ? short : next);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
let frame;
|
|
558
|
+
try {
|
|
559
|
+
frame = JSON.stringify({ type: "frame", tree: live.shell.getShellRenderTree() });
|
|
560
|
+
} catch (error) {
|
|
561
|
+
console.error("[moss/shells] the shell frame failed to render on a flush \u2014 leaving the last one in place:", error);
|
|
562
|
+
}
|
|
563
|
+
if (frame !== void 0 && frame !== live.sentFrame) {
|
|
564
|
+
live.sentFrame = frame;
|
|
565
|
+
for (const connection of live.connections) connection.send(frame);
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
var follow = (live, delta) => {
|
|
569
|
+
const flush = () => {
|
|
570
|
+
if (live.flushing) return;
|
|
571
|
+
live.flushing = true;
|
|
572
|
+
queueMicrotask(() => {
|
|
573
|
+
live.flushing = false;
|
|
574
|
+
pass(live, delta);
|
|
575
|
+
setTimeout(() => pass(live, delta), 0);
|
|
576
|
+
});
|
|
577
|
+
};
|
|
578
|
+
live.shell.onStateChange(flush);
|
|
579
|
+
live.shell.onDataChange(flush);
|
|
580
|
+
};
|
|
581
|
+
var attach = (live, connection, wantsDelta) => {
|
|
582
|
+
live.connections.add(connection);
|
|
583
|
+
if (wantsDelta) live.deltaReady.add(connection);
|
|
584
|
+
live.idleSince = null;
|
|
585
|
+
let frameTree;
|
|
586
|
+
try {
|
|
587
|
+
frameTree = live.shell.getShellRenderTree();
|
|
588
|
+
} catch (error) {
|
|
589
|
+
console.error("[moss/shells] the shell frame failed to render \u2014 serving an empty arrangement:", error);
|
|
590
|
+
}
|
|
591
|
+
const frameMessage = JSON.stringify({ type: "frame", tree: frameTree ?? [] });
|
|
592
|
+
connection.send(frameMessage);
|
|
593
|
+
if (frameTree === void 0) {
|
|
594
|
+
delete live.sentFrame;
|
|
595
|
+
} else {
|
|
596
|
+
if (live.sentFrame !== frameMessage) {
|
|
597
|
+
for (const other of live.connections) if (other !== connection) other.send(frameMessage);
|
|
598
|
+
}
|
|
599
|
+
live.sentFrame = frameMessage;
|
|
600
|
+
}
|
|
601
|
+
for (const canvasId of canvasesOf(live)) {
|
|
602
|
+
const next = canvasMessageOf(live, canvasId);
|
|
603
|
+
if (next === null) continue;
|
|
604
|
+
if (live.sentByCanvas.get(canvasId) !== next) {
|
|
605
|
+
for (const other of live.connections) if (other !== connection) other.send(next);
|
|
606
|
+
}
|
|
607
|
+
live.sentByCanvas.set(canvasId, next);
|
|
608
|
+
connection.send(next);
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
var resync = (live, connection) => {
|
|
612
|
+
if (!live.connections.has(connection)) return;
|
|
613
|
+
let frameTree = [];
|
|
614
|
+
try {
|
|
615
|
+
frameTree = live.shell.getShellRenderTree();
|
|
616
|
+
} catch (error) {
|
|
617
|
+
console.error("[moss/shells] the shell frame failed to render on resync \u2014 serving an empty arrangement:", error);
|
|
618
|
+
}
|
|
619
|
+
connection.send(JSON.stringify({ type: "frame", tree: frameTree }));
|
|
620
|
+
for (const canvasId of canvasesOf(live)) {
|
|
621
|
+
const current = live.sentByCanvas.get(canvasId) ?? canvasMessageOf(live, canvasId);
|
|
622
|
+
if (current === null || current === void 0) continue;
|
|
623
|
+
connection.send(current);
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
// src/shells/sessions.ts
|
|
628
|
+
var instrumentFunctions = (fns, emit, principal) => Object.fromEntries(
|
|
629
|
+
Object.entries(fns).map(([name, handler]) => [
|
|
630
|
+
name,
|
|
631
|
+
async (data, signal) => {
|
|
632
|
+
const clock = chunkPVZMCEBX_cjs.spanClock();
|
|
633
|
+
const hasPrincipal = principal !== null;
|
|
634
|
+
try {
|
|
635
|
+
const out = await handler(data, signal);
|
|
636
|
+
emit({ name: "fn.call", startUnixNano: clock.startUnixNano, endUnixNano: clock.endUnixNano(), status: "ok", attributes: { fn: name, hasPrincipal } });
|
|
637
|
+
return out;
|
|
638
|
+
} catch (err) {
|
|
639
|
+
emit({ name: "fn.call", startUnixNano: clock.startUnixNano, endUnixNano: clock.endUnixNano(), status: "error", attributes: { fn: name, hasPrincipal } });
|
|
640
|
+
throw err;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
])
|
|
644
|
+
);
|
|
645
|
+
var grant = (shellRef, token) => {
|
|
646
|
+
const message = JSON.stringify({ type: "session", token });
|
|
647
|
+
for (const connection of shellRef.live?.connections ?? []) {
|
|
648
|
+
if (connection.seal === void 0) connection.send(message);
|
|
649
|
+
else {
|
|
650
|
+
void connection.seal(token).then(
|
|
651
|
+
(sealed) => connection.send(JSON.stringify({ type: "session", sealed })),
|
|
652
|
+
(error) => console.error("[moss] a sign-in could not be sealed, and was not handed over:", error)
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
var revoke = (host, shellRef, principal) => {
|
|
658
|
+
const { config, durable } = host;
|
|
659
|
+
const forgotten = config.revokeSessions !== void 0 && principal !== null ? config.revokeSessions(principal) : Promise.resolve();
|
|
660
|
+
forgotten.catch((error) => console.error(`[moss] sign-out of ${principal ?? "anonymous"} did not revoke its sessions:`, error));
|
|
661
|
+
queueMicrotask(() => {
|
|
662
|
+
const live = shellRef.live;
|
|
663
|
+
if (live === void 0) return;
|
|
664
|
+
endShell(host, live);
|
|
665
|
+
if (principal !== null && durable.get(principal)?.live === live) durable.delete(principal);
|
|
666
|
+
for (const connection of [...live.connections]) connection.close(chunk3RIWTXGI_cjs.CLOSE_SIGNED_OUT, "signed out");
|
|
667
|
+
live.connections.clear();
|
|
668
|
+
if (principal !== null) live.shell.dispose();
|
|
669
|
+
});
|
|
670
|
+
return forgotten;
|
|
671
|
+
};
|
|
672
|
+
var functionSessionFor = (host, who, wire, shellRef) => {
|
|
673
|
+
const { config } = host;
|
|
674
|
+
const { principal } = who;
|
|
675
|
+
const artifacts = config.artifacts?.(principal);
|
|
676
|
+
const session = {
|
|
677
|
+
get artifacts() {
|
|
678
|
+
if (artifacts === void 0) throw new Error("moss: this shell host was given nothing to save with.");
|
|
679
|
+
return artifacts;
|
|
680
|
+
},
|
|
681
|
+
draft: sessionDraftOn({ saving: config.draftSaving?.(principal), liveOf: () => shellRef.live, adopt: host.adoptDraft }),
|
|
682
|
+
principal,
|
|
683
|
+
roles: who.roles,
|
|
684
|
+
identity: who.scope,
|
|
685
|
+
actions: who.actions,
|
|
686
|
+
wire,
|
|
687
|
+
get shell() {
|
|
688
|
+
if (shellRef.sessionShell === void 0) throw new Error("moss: a function touched the shell before the session finished building.");
|
|
689
|
+
return shellRef.sessionShell;
|
|
690
|
+
},
|
|
691
|
+
grant: (token) => grant(shellRef, token),
|
|
692
|
+
revoke: () => revoke(host, shellRef, principal),
|
|
693
|
+
answerIntent: (writers, asked) => config.answerIntent === void 0 ? Promise.reject(new Error("moss: this shell host was given no way to answer an intent.")) : config.answerIntent(principal, writers, asked),
|
|
694
|
+
recordRun: (run) => {
|
|
695
|
+
config.hooks.runs?.({ ...run, at: Date.now(), principal, shellId: shellRef.shell?.id ?? "" }, session);
|
|
696
|
+
},
|
|
697
|
+
functions: () => {
|
|
698
|
+
const functions = shellRef.live?.functions;
|
|
699
|
+
return Object.keys(functions?.held ?? {}).map((id) => {
|
|
700
|
+
const description = functions?.descriptions[id];
|
|
701
|
+
return description === void 0 ? { id } : { id, description };
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
};
|
|
705
|
+
return session;
|
|
706
|
+
};
|
|
707
|
+
var sessionOn = (host, cell, ephemeral, reset) => ({
|
|
708
|
+
get shell() {
|
|
709
|
+
return cell.live.sessionShell;
|
|
710
|
+
},
|
|
711
|
+
attach: (connection, options) => attach(cell.live, connection, host.delta && options?.delta === true),
|
|
712
|
+
resync: (connection) => resync(cell.live, connection),
|
|
713
|
+
detach: (connection) => {
|
|
714
|
+
const live = cell.live;
|
|
715
|
+
live.connections.delete(connection);
|
|
716
|
+
live.deltaReady.delete(connection);
|
|
717
|
+
if (live.connections.size > 0) return;
|
|
718
|
+
live.idleSince = Date.now();
|
|
719
|
+
if (!ephemeral) return;
|
|
720
|
+
endShell(host, live);
|
|
721
|
+
live.shell.dispose();
|
|
722
|
+
},
|
|
723
|
+
// An event without an origin goes to every instance of the shell, so one for
|
|
724
|
+
// a canvas the shell does not have is dropped. An origin a terminal names
|
|
725
|
+
// comes off the wire and is held to that canvas; with none named, the
|
|
726
|
+
// canvas's active instance is the origin.
|
|
727
|
+
dispatch: (canvas, event) => {
|
|
728
|
+
const { shell: shell2 } = cell.live;
|
|
729
|
+
const state = shell2.getState().canvases[canvas];
|
|
730
|
+
if (state === void 0) return;
|
|
731
|
+
const claimed = event["origin"];
|
|
732
|
+
if (claimed !== void 0 && (typeof claimed !== "string" || !state.stack.some((item) => item.id === claimed))) return;
|
|
733
|
+
const active = state.active;
|
|
734
|
+
const parsed = nova.NovaEventSchema.safeParse(claimed !== void 0 || active === void 0 ? event : { ...event, origin: active.id });
|
|
735
|
+
if (parsed.success) shell2.dispatch(parsed.data);
|
|
736
|
+
},
|
|
737
|
+
publish: (channel, payload) => cell.live.shell.publish(channel, payload),
|
|
738
|
+
back: () => cell.live.shell.back(),
|
|
739
|
+
popTo: (canvas, instance) => cell.live.shell.popTo(canvas, instance),
|
|
740
|
+
reset: () => reset(cell)
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
// src/shells/build.ts
|
|
744
|
+
var whole = /* @__PURE__ */ new WeakMap();
|
|
745
|
+
var refuse = (member) => () => {
|
|
746
|
+
throw new Error(
|
|
747
|
+
`moss: ${member} was called on a session's shell. It changes what the shell is made of without moss: what it adds is not verified, is in nobody's catalog, and is gone when the shell is rebuilt. Code that means to do that takes the whole shell: \`unverifiedShell(session)\`, from \`@niscorp/moss/unverified\`.`
|
|
748
|
+
);
|
|
749
|
+
};
|
|
750
|
+
var runtimeAsWritten = (runtime, store) => ({
|
|
751
|
+
get instance() {
|
|
752
|
+
return runtime.instance;
|
|
753
|
+
},
|
|
754
|
+
get definition() {
|
|
755
|
+
return withLayoutInPlace(runtime.definition, store);
|
|
756
|
+
},
|
|
757
|
+
getData: runtime.getData,
|
|
758
|
+
setData: runtime.setData,
|
|
759
|
+
render: runtime.render,
|
|
760
|
+
onDataChange: runtime.onDataChange,
|
|
761
|
+
onStatusChange: runtime.onStatusChange
|
|
762
|
+
});
|
|
763
|
+
var sessionShellOf = (shell2) => {
|
|
764
|
+
const guarded = {
|
|
765
|
+
...shell2,
|
|
766
|
+
layoutStore: { ...shell2.layoutStore, set: refuse("layoutStore.set"), delete: refuse("layoutStore.delete") },
|
|
767
|
+
getRuntime: (instanceId) => {
|
|
768
|
+
const runtime = shell2.getRuntime(instanceId);
|
|
769
|
+
return runtime === void 0 ? void 0 : runtimeAsWritten(runtime, shell2.layoutStore);
|
|
770
|
+
},
|
|
771
|
+
registerAction: refuse("registerAction"),
|
|
772
|
+
removeAction: refuse("removeAction"),
|
|
773
|
+
addCanvas: refuse("addCanvas"),
|
|
774
|
+
removeCanvas: refuse("removeCanvas"),
|
|
775
|
+
setCanvasLayout: refuse("setCanvasLayout"),
|
|
776
|
+
setLayout: refuse("setLayout"),
|
|
777
|
+
setPhrases: refuse("setPhrases"),
|
|
778
|
+
dispose: refuse("dispose")
|
|
779
|
+
};
|
|
780
|
+
whole.set(guarded, shell2);
|
|
781
|
+
whole.set(shell2, shell2);
|
|
782
|
+
return guarded;
|
|
783
|
+
};
|
|
784
|
+
var wholeShellOf = (sessionShell) => {
|
|
785
|
+
const shell2 = whole.get(sessionShell);
|
|
786
|
+
if (shell2 === void 0) throw new Error("moss: this is not the shell of a session moss built.");
|
|
787
|
+
return shell2;
|
|
788
|
+
};
|
|
789
|
+
var today = () => (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
|
|
790
|
+
var mountedCanvases = (declared, granted, inputs, given) => declared.map((canvas) => {
|
|
791
|
+
const declared2 = canvas.initial;
|
|
792
|
+
const candidates = declared2 === void 0 ? [] : Array.isArray(declared2) ? declared2 : [declared2];
|
|
793
|
+
const seed = candidates.find((c) => granted.has(typeof c === "string" ? c : c.action));
|
|
794
|
+
if (seed === void 0) {
|
|
795
|
+
const { initial: _dropped, ...rest } = canvas;
|
|
796
|
+
return rest;
|
|
797
|
+
}
|
|
798
|
+
const action = typeof seed === "string" ? seed : seed.action;
|
|
799
|
+
const staticInput = typeof seed === "string" ? {} : seed.input ?? {};
|
|
800
|
+
const withFragments2 = typeof seed === "string" ? void 0 : seed.with;
|
|
801
|
+
return {
|
|
802
|
+
...canvas,
|
|
803
|
+
initial: { action, input: { ...staticInput, ...inputs[canvas.id] ?? {}, ...given?.[canvas.id] ?? {} }, ...withFragments2 !== void 0 ? { with: withFragments2 } : {} }
|
|
804
|
+
};
|
|
805
|
+
});
|
|
806
|
+
var seedCanvases = (live, declaredSeeds, granted) => {
|
|
807
|
+
if (declaredSeeds === void 0) return;
|
|
808
|
+
live.seeded = Promise.resolve(declaredSeeds).then((byCanvas) => {
|
|
809
|
+
if (live.ended) return;
|
|
810
|
+
for (const [canvasId, seeds] of Object.entries(byCanvas ?? {})) {
|
|
811
|
+
for (const seed of seeds) {
|
|
812
|
+
const action = typeof seed === "string" ? seed : seed.action;
|
|
813
|
+
if (!granted.has(action)) continue;
|
|
814
|
+
const input = typeof seed === "string" ? void 0 : seed.input;
|
|
815
|
+
const withFragments2 = typeof seed === "string" ? void 0 : seed.with;
|
|
816
|
+
try {
|
|
817
|
+
live.shell.push(canvasId, action, input, withFragments2, { history: false });
|
|
818
|
+
} catch (error) {
|
|
819
|
+
console.error(`[moss/shells] the seed "${action}" on canvas "${canvasId}" could not be pushed, and was skipped:`, error);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}).catch((error) => {
|
|
824
|
+
console.error("[moss/shells] the seeds of a shell could not be derived, and none was pushed:", error);
|
|
825
|
+
});
|
|
826
|
+
};
|
|
827
|
+
var buildShell = async (host, asked) => {
|
|
828
|
+
const { config, emit, kept, delta } = host;
|
|
829
|
+
const { hooks } = config;
|
|
830
|
+
const { token, principal, opening, reset, wanted } = asked;
|
|
831
|
+
const buildClock = emit === void 0 ? void 0 : chunkPVZMCEBX_cjs.spanClock();
|
|
832
|
+
const made = await config.composition(principal);
|
|
833
|
+
if (!wanted()) return void 0;
|
|
834
|
+
const ids = Object.keys(made.actions);
|
|
835
|
+
const granted = new Set(ids);
|
|
836
|
+
const inputs = await hooks.inputs?.({ principal, actions: ids, roles: made.roles, identity: made.scope, wire: config.wire(token) }) ?? {};
|
|
837
|
+
if (!wanted()) return void 0;
|
|
838
|
+
const canvases = mountedCanvases(made.canvases, granted, inputs, opening?.inputs);
|
|
839
|
+
const wire = config.wrapWire === void 0 ? config.wire(token) : config.wrapWire(config.wire(token));
|
|
840
|
+
const userId = principal ?? "anonymous";
|
|
841
|
+
const registry = nova.createComponentRegistry();
|
|
842
|
+
for (const [name, contract] of Object.entries(made.components)) registry.register(name, contract);
|
|
843
|
+
const shellRef = {};
|
|
844
|
+
const session = functionSessionFor(host, { principal, roles: made.roles, scope: made.scope, actions: ids }, wire, shellRef);
|
|
845
|
+
const phrases = await hooks.phrases?.({ principal, identity: made.scope, wire: config.wire(token) }) ?? {};
|
|
846
|
+
if (!wanted()) return void 0;
|
|
847
|
+
const handlers = {};
|
|
848
|
+
const descriptions = {};
|
|
849
|
+
for (const { id, description, execute } of functionsIn(hooks.functions?.(session) ?? [])) {
|
|
850
|
+
handlers[id] = execute;
|
|
851
|
+
if (description !== "") descriptions[id] = description;
|
|
852
|
+
}
|
|
853
|
+
const functions = { all: emit === void 0 ? handlers : instrumentFunctions(handlers, emit, principal), held: {}, descriptions };
|
|
854
|
+
holdGranted(functions, made.grantedFunctions);
|
|
855
|
+
if (kept) hooks.onSession?.(session);
|
|
856
|
+
const layoutStore = nova.createLayoutStore();
|
|
857
|
+
for (const [refId, node] of Object.entries(made.layoutStore ?? {})) layoutStore.set(refId, node);
|
|
858
|
+
let minted = 0;
|
|
859
|
+
const idSeed = opening?.seed;
|
|
860
|
+
const shell2 = nova.createShell({
|
|
861
|
+
registry,
|
|
862
|
+
canvases,
|
|
863
|
+
layoutStore,
|
|
864
|
+
...idSeed === void 0 ? {} : { instanceIdFn: () => `act-${idSeed}-${minted += 1}` },
|
|
865
|
+
...made.frame !== void 0 ? { canvasLayout: made.frame } : {},
|
|
866
|
+
actions: { ...made.actions },
|
|
867
|
+
...made.fragments !== void 0 ? { fragments: { ...made.fragments } } : {},
|
|
868
|
+
transform: (config2, source) => prism.evaluate(config2, source !== null && typeof source === "object" && !Array.isArray(source) ? { ...source, userId, today: today() } : source),
|
|
869
|
+
fetch: wire,
|
|
870
|
+
functions: functions.held,
|
|
871
|
+
...config.limits?.endpointTimeoutMs === void 0 ? {} : { endpointTimeoutMs: config.limits.endpointTimeoutMs },
|
|
872
|
+
// An empty book is withheld: nova runs its phrase pass whenever one is given.
|
|
873
|
+
...Object.keys(phrases).length === 0 ? {} : { phrases },
|
|
874
|
+
...config.phraseKeys === void 0 ? {} : { phraseKeys: config.phraseKeys }
|
|
875
|
+
});
|
|
876
|
+
const sessionShell = sessionShellOf(shell2);
|
|
877
|
+
shellRef.shell = shell2;
|
|
878
|
+
shellRef.sessionShell = sessionShell;
|
|
879
|
+
const live = {
|
|
880
|
+
shell: shell2,
|
|
881
|
+
sessionShell,
|
|
882
|
+
actionIds: granted,
|
|
883
|
+
entries: made.entries,
|
|
884
|
+
layouts: layoutsOf(made),
|
|
885
|
+
slottedCanvases: new Set(made.slottedCanvases),
|
|
886
|
+
functions,
|
|
887
|
+
draft: noDraft(),
|
|
888
|
+
connections: /* @__PURE__ */ new Set(),
|
|
889
|
+
sentByCanvas: /* @__PURE__ */ new Map(),
|
|
890
|
+
deltaReady: /* @__PURE__ */ new Set(),
|
|
891
|
+
flushing: false,
|
|
892
|
+
ended: false,
|
|
893
|
+
builtAt: Date.now(),
|
|
894
|
+
idleSince: Date.now(),
|
|
895
|
+
seeded: Promise.resolve()
|
|
896
|
+
};
|
|
897
|
+
shellRef.live = live;
|
|
898
|
+
seedCanvases(live, hooks.seeds?.({ principal, actions: ids, roles: made.roles, wire }), granted);
|
|
899
|
+
follow(live, delta);
|
|
900
|
+
if (emit !== void 0 && buildClock !== void 0) {
|
|
901
|
+
emit({ name: "shell.build", startUnixNano: buildClock.startUnixNano, endUnixNano: buildClock.endUnixNano(), status: "ok", attributes: { reset, hasPrincipal: principal !== null } });
|
|
902
|
+
}
|
|
903
|
+
return live;
|
|
904
|
+
};
|
|
905
|
+
|
|
906
|
+
exports.FRAME_LAYOUT_REF = FRAME_LAYOUT_REF;
|
|
907
|
+
exports.GENERATED_ID_PREFIX = GENERATED_ID_PREFIX;
|
|
908
|
+
exports.SHELL_GONE = SHELL_GONE;
|
|
909
|
+
exports.actionLayoutRef = actionLayoutRef;
|
|
910
|
+
exports.actionOfLayoutRef = actionOfLayoutRef;
|
|
911
|
+
exports.attach = attach;
|
|
912
|
+
exports.buildShell = buildShell;
|
|
913
|
+
exports.candidateOf = candidateOf;
|
|
914
|
+
exports.canvasLayoutRef = canvasLayoutRef;
|
|
915
|
+
exports.canvasesOf = canvasesOf;
|
|
916
|
+
exports.charterOverFunctions = charterOverFunctions;
|
|
917
|
+
exports.defineApp = defineApp;
|
|
918
|
+
exports.draftedIn = draftedIn;
|
|
919
|
+
exports.endShell = endShell;
|
|
920
|
+
exports.endlessChains = endlessChains;
|
|
921
|
+
exports.entriesOf = entriesOf;
|
|
922
|
+
exports.entryMisfits = entryMisfits;
|
|
923
|
+
exports.freezeDeep = freezeDeep;
|
|
924
|
+
exports.functionsIn = functionsIn;
|
|
925
|
+
exports.functionsWarning = functionsWarning;
|
|
926
|
+
exports.holdGranted = holdGranted;
|
|
927
|
+
exports.layoutMisfits = layoutMisfits;
|
|
928
|
+
exports.layoutNameRefusal = layoutNameRefusal;
|
|
929
|
+
exports.layoutsOf = layoutsOf;
|
|
930
|
+
exports.receiveManifest = receiveManifest;
|
|
931
|
+
exports.refusalOf = refusalOf;
|
|
932
|
+
exports.sessionOn = sessionOn;
|
|
933
|
+
exports.treeOf = treeOf;
|
|
934
|
+
exports.variantLayouts = variantLayouts;
|
|
935
|
+
exports.verifiedAtBoot = verifiedAtBoot;
|
|
936
|
+
exports.verifyApp = verifyApp;
|
|
937
|
+
exports.verifyManifest = verifyManifest;
|
|
938
|
+
exports.wholeShellOf = wholeShellOf;
|
|
939
|
+
exports.withFragments = withFragments;
|
|
940
|
+
exports.withLayoutInPlace = withLayoutInPlace;
|
|
941
|
+
exports.withVariants = withVariants;
|
|
942
|
+
//# sourceMappingURL=chunk-3DCTQKPQ.cjs.map
|
|
943
|
+
//# sourceMappingURL=chunk-3DCTQKPQ.cjs.map
|