@delurio/cli 0.9.0 → 0.11.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.
Files changed (86) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/NOTICE +7 -0
  3. package/README.md +16 -15
  4. package/SPEC.md +37 -11
  5. package/js/changes.d.ts +17 -0
  6. package/js/cli.js +6 -0
  7. package/{dist → js}/edges.d.ts +2 -2
  8. package/js/edges.js +40 -0
  9. package/js/expr.js +25 -0
  10. package/js/graph.js +59 -0
  11. package/{dist → js}/html.d.ts +6 -6
  12. package/js/html.js +13 -0
  13. package/js/internal.js +408 -0
  14. package/js/kit.js +54 -0
  15. package/{dist → js}/model.d.ts +2 -2
  16. package/js/model.js +136 -0
  17. package/js/native.cjs +46 -0
  18. package/js/native.d.cts +322 -0
  19. package/{dist → js}/nav.d.ts +1 -1
  20. package/js/nav.js +70 -0
  21. package/{dist → js}/needs.d.ts +3 -2
  22. package/js/needs.js +30 -0
  23. package/{dist → js}/spine.d.ts +3 -3
  24. package/js/spine.js +22 -0
  25. package/{dist → js}/starts.d.ts +1 -3
  26. package/{dist → js}/story.d.ts +2 -2
  27. package/js/story.js +22 -0
  28. package/{dist → js}/wireframe.d.ts +4 -2
  29. package/js/wireframe.js +29 -0
  30. package/js/yaml.d.ts +20 -0
  31. package/js/yaml.js +40 -0
  32. package/package.json +41 -25
  33. package/schemas/domain.schema.json +3 -0
  34. package/dist/changes.d.ts +0 -25
  35. package/dist/changes.js +0 -101
  36. package/dist/check.d.ts +0 -23
  37. package/dist/check.js +0 -574
  38. package/dist/cli.d.ts +0 -2
  39. package/dist/cli.js +0 -194
  40. package/dist/compare.d.ts +0 -34
  41. package/dist/compare.js +0 -143
  42. package/dist/diff-flow.d.ts +0 -33
  43. package/dist/diff-flow.js +0 -547
  44. package/dist/diff-nav.d.ts +0 -20
  45. package/dist/diff-nav.js +0 -401
  46. package/dist/diff.d.ts +0 -25
  47. package/dist/diff.js +0 -331
  48. package/dist/domain-names.d.ts +0 -18
  49. package/dist/domain-names.js +0 -124
  50. package/dist/domain.d.ts +0 -25
  51. package/dist/domain.js +0 -110
  52. package/dist/edges.js +0 -120
  53. package/dist/expr.js +0 -157
  54. package/dist/graph.js +0 -236
  55. package/dist/html.js +0 -311
  56. package/dist/index-page.d.ts +0 -12
  57. package/dist/index-page.js +0 -50
  58. package/dist/invokes.d.ts +0 -10
  59. package/dist/invokes.js +0 -67
  60. package/dist/kit.js +0 -710
  61. package/dist/mermaid.d.ts +0 -3
  62. package/dist/mermaid.js +0 -50
  63. package/dist/model.js +0 -608
  64. package/dist/nav-page.d.ts +0 -15
  65. package/dist/nav-page.js +0 -283
  66. package/dist/nav.js +0 -578
  67. package/dist/needs.js +0 -311
  68. package/dist/pan.d.ts +0 -2
  69. package/dist/pan.js +0 -252
  70. package/dist/schemas.d.ts +0 -10
  71. package/dist/schemas.js +0 -266
  72. package/dist/spine.js +0 -156
  73. package/dist/starts.js +0 -88
  74. package/dist/story.js +0 -109
  75. package/dist/tutorial-check.d.ts +0 -9
  76. package/dist/tutorial-check.js +0 -297
  77. package/dist/ui-check.d.ts +0 -8
  78. package/dist/ui-check.js +0 -136
  79. package/dist/watch.d.ts +0 -5
  80. package/dist/watch.js +0 -114
  81. package/dist/wireframe.js +0 -273
  82. package/dist/yaml.d.ts +0 -19
  83. package/dist/yaml.js +0 -134
  84. /package/{dist → js}/expr.d.ts +0 -0
  85. /package/{dist → js}/graph.d.ts +0 -0
  86. /package/{dist → js}/kit.d.ts +0 -0
package/js/internal.js ADDED
@@ -0,0 +1,408 @@
1
+ // How the modules of @delurio/cli reach the checker, which runs in the addon (crates/delurio-node): not an entry
2
+ // point of the package. Values cross as JSON; the checker's errors come back as the classes the TypeScript modules
3
+ // threw; a caller's callback runs in a trap, so that what it throws never crosses into the addon and reaches the
4
+ // caller unchanged once the call is done, as it did when the TypeScript code called it.
5
+ import native from './native.cjs';
6
+
7
+ export { native };
8
+
9
+ const MARK = '\u0000delurio\u0000';
10
+
11
+ // a graph that cannot be loaded (SPEC.md §10)
12
+ export class GraphError extends Error {
13
+ place;
14
+ constructor(message, place) {
15
+ super(message);
16
+ this.place = place;
17
+ }
18
+ }
19
+
20
+ // a flow that cannot be explored; `place` says where in flow.yaml, when it is known
21
+ export class ModelError extends Error {
22
+ place;
23
+ constructor(message, place) {
24
+ super(message);
25
+ this.place = place;
26
+ }
27
+ }
28
+
29
+ // a kit that cannot be read or holds what a kit may not
30
+ export class KitError extends Error {}
31
+
32
+ // the yaml library's error for a text that is not YAML (or holds what delurio does not read), as readYaml threw it
33
+ export class YAMLParseError extends Error {
34
+ constructor(pos, code, message) {
35
+ super();
36
+ this.name = 'YAMLParseError';
37
+ this.code = code;
38
+ this.message = message;
39
+ this.pos = pos;
40
+ }
41
+ }
42
+
43
+ // the place of an error that a reader of the graph's files threw, given to it when it has none yet
44
+ export function placed(err, place) {
45
+ if (err instanceof Error && err.place === undefined) Object.assign(err, { place });
46
+ return err;
47
+ }
48
+
49
+ // the error a call of the addon threw, as the caller meets it: the checker's own as its class, a callback's as the
50
+ // callback threw it
51
+ export function lift(e, t) {
52
+ const text = e instanceof Error ? e.message : undefined;
53
+ if (typeof text !== 'string' || !text.startsWith(MARK)) return e;
54
+ const m = JSON.parse(text.slice(MARK.length));
55
+ if (m.thrown === true) {
56
+ if (t === undefined || t === null || !t.threw) return new Error('@delurio/cli: a callback failed');
57
+ return m.place === undefined ? t.error : placed(t.error, m.place);
58
+ }
59
+ let err;
60
+ switch (m.kind) {
61
+ case 'graph':
62
+ return new GraphError(m.message, m.place);
63
+ case 'model':
64
+ return new ModelError(m.message, m.place);
65
+ case 'kit':
66
+ err = new KitError(m.message);
67
+ break;
68
+ case 'yaml': {
69
+ const at = m.line === undefined ? -1 : 0;
70
+ err = new YAMLParseError(m.pos ?? [at, at], m.code, m.message);
71
+ if (m.linePos !== undefined) err.linePos = m.linePos;
72
+ else if (m.line !== undefined) err.linePos = [{ line: m.line, col: m.col }];
73
+ break;
74
+ }
75
+ default:
76
+ err = new Error(m.message);
77
+ }
78
+ return m.place === undefined ? err : placed(err, m.place);
79
+ }
80
+
81
+ // What a caller's callbacks threw during one call of the addon: the first throw is kept, and once one has thrown no
82
+ // callback of the call runs again (the TypeScript code had stopped there).
83
+ export class Trap {
84
+ threw = false;
85
+ error = undefined;
86
+
87
+ catch(e) {
88
+ if (this.threw) return;
89
+ this.threw = true;
90
+ this.error = e;
91
+ }
92
+
93
+ // read(file): { ok: text } or { thrown: message }
94
+ reader(read) {
95
+ return (file) => {
96
+ if (this.threw) return { thrown: 'undefined' };
97
+ try {
98
+ return { ok: String(read(file)) };
99
+ } catch (e) {
100
+ this.catch(e);
101
+ return { thrown: String(e?.message) };
102
+ }
103
+ };
104
+ }
105
+
106
+ // exists(file): 1, 0, or 2 when it threw
107
+ exister(exists) {
108
+ return (file) => {
109
+ if (this.threw) return 2;
110
+ try {
111
+ return exists(file) ? 1 : 0;
112
+ } catch (e) {
113
+ this.catch(e);
114
+ return 2;
115
+ }
116
+ };
117
+ }
118
+
119
+ warner(warn) {
120
+ return (message) => {
121
+ if (this.threw) return;
122
+ try {
123
+ warn(message);
124
+ } catch (e) {
125
+ this.catch(e);
126
+ }
127
+ };
128
+ }
129
+
130
+ // lookup(name): the value as JSON
131
+ lookup(lookup) {
132
+ return (name) => {
133
+ if (this.threw) return 'null';
134
+ try {
135
+ const v = lookup(name);
136
+ return (v === undefined ? undefined : JSON.stringify(v)) ?? 'null';
137
+ } catch (e) {
138
+ this.catch(e);
139
+ return 'null';
140
+ }
141
+ };
142
+ }
143
+
144
+ // keep(at, s) of explore and cover: the addon hands the pair and the step as places in the model's tables
145
+ keeper(keep, model) {
146
+ let at = null;
147
+ return (fresh, state, parent, event, atCtx, sEvent, target, sCtx, taken) => {
148
+ if (this.threw) return 0;
149
+ try {
150
+ if (fresh || at === null) at = { state, ctx: decodeAll(model, atCtx), parent, event };
151
+ const s = { event: sEvent, target, ctx: decodeAll(model, sCtx), taken };
152
+ return keep(at, s) ? 1 : 0;
153
+ } catch (e) {
154
+ this.catch(e);
155
+ return 0;
156
+ }
157
+ };
158
+ }
159
+
160
+ // run(): its result is kept by the caller
161
+ runner(run) {
162
+ return () => {
163
+ if (this.threw) return;
164
+ try {
165
+ run();
166
+ } catch (e) {
167
+ this.catch(e);
168
+ }
169
+ };
170
+ }
171
+ }
172
+
173
+ // `run()`, a call of the addon: its error lifted, and what a callback threw thrown again once it is done
174
+ export function call(t, run) {
175
+ let out;
176
+ try {
177
+ out = run();
178
+ } catch (e) {
179
+ throw lift(e, t);
180
+ }
181
+ if (t !== null && t !== undefined && t.threw) throw t.error;
182
+ return out;
183
+ }
184
+
185
+ // the yaml library hands each warning of a document it parsed to process.emitWarning; the addon keeps them
186
+ export function warnings() {
187
+ for (const w of JSON.parse(native.takeYamlWarnings())) process.emitWarning(w.message, { type: 'YAMLWarning', code: w.code });
188
+ }
189
+
190
+ // a caller's value as JSON, its Sets as lists and its Maps as objects
191
+ const replacer = (_k, v) => (v instanceof Set ? [...v] : v instanceof Map ? Object.fromEntries(v) : v);
192
+ export const json = (v) => JSON.stringify(v, replacer);
193
+
194
+ function object(v, what) {
195
+ if (v === null || typeof v !== 'object') throw new TypeError(`@delurio/cli: ${what} is not an object: ${String(v)}`);
196
+ return v;
197
+ }
198
+
199
+ // ---- objects the addon holds for the caller's
200
+ //
201
+ // A graph, a navigation graph, a kit or a flow is read into the addon once for the object the caller holds, and read
202
+ // again when that object's own keys hold other values (a copy made with { ...g, catalog }, a key given another
203
+ // value). What lies deeper is read once: a caller that changes a loaded graph in place hands a copy instead, as
204
+ // delurio's own code does.
205
+
206
+ const graphs = new WeakMap();
207
+
208
+ export function rememberGraph(g, n) {
209
+ graphs.set(g, { n, dir: g.dir, flow: g.flow, ui: g.ui, catalog: g.catalog });
210
+ return g;
211
+ }
212
+
213
+ export function nativeGraph(g) {
214
+ const hit = graphs.get(object(g, 'the graph'));
215
+ if (hit !== undefined && hit.dir === g.dir && hit.flow === g.flow && hit.ui === g.ui && hit.catalog === g.catalog) return hit.n;
216
+ const n = call(null, () => new native.NativeGraph(JSON.stringify(g)));
217
+ rememberGraph(g, n);
218
+ return n;
219
+ }
220
+
221
+ const values = new WeakMap();
222
+
223
+ export function nativeValue(v) {
224
+ const hit = values.get(object(v, 'the value'));
225
+ if (hit !== undefined) return hit;
226
+ const n = call(null, () => new native.NativeValue(JSON.stringify(v)));
227
+ values.set(v, n);
228
+ return n;
229
+ }
230
+
231
+ const navs = new WeakMap();
232
+ const NAV_KEYS = ['dir', 'adapter', 'frames', 'menuFrames', 'id', 'title', 'status', 'summary', 'locale', 'source', 'facts', 'guards', 'pages', 'entries', 'specs', 'personas', 'goals', 'notModeled', 'catalog'];
233
+
234
+ export function rememberNav(g, n) {
235
+ navs.set(g, { n, keys: NAV_KEYS.map((k) => g[k]) });
236
+ return g;
237
+ }
238
+
239
+ export function nativeNav(g) {
240
+ const hit = navs.get(object(g, 'the navigation graph'));
241
+ if (hit !== undefined && NAV_KEYS.every((k, i) => hit.keys[i] === g[k])) return hit.n;
242
+ const n = call(null, () => new native.NativeNav(JSON.stringify(g)));
243
+ rememberNav(g, n);
244
+ return n;
245
+ }
246
+
247
+ const kits = new WeakMap();
248
+
249
+ export function rememberKit(k, n) {
250
+ kits.set(k, n);
251
+ return k;
252
+ }
253
+
254
+ export function nativeKit(k) {
255
+ if (k === null || k === undefined) return undefined;
256
+ const hit = kits.get(object(k, 'the kit'));
257
+ if (hit !== undefined) return hit;
258
+ const n = call(null, () => new native.NativeKit(JSON.stringify(k)));
259
+ kits.set(k, n);
260
+ return n;
261
+ }
262
+
263
+ // ---- models and contexts
264
+ //
265
+ // A context crosses as the places of its values in the model's tables. Each Model keeps, per variable, which place
266
+ // holds which value and back: the values of flow.context[v].values are the flow's own, so a context the model gives
267
+ // holds the very values (objects included) the flow and the caller hold.
268
+
269
+ const models = new WeakMap();
270
+
271
+ export function registerModel(m, n, domains) {
272
+ const codes = n.domainCodes();
273
+ const dec = codes.map(() => new Map());
274
+ const enc = codes.map(() => new Map());
275
+ codes.forEach((list, i) => {
276
+ const vs = Array.isArray(domains[i]) ? domains[i] : [];
277
+ list.forEach((c, k) => {
278
+ const v = vs[k];
279
+ if (!dec[i].has(c)) dec[i].set(c, v);
280
+ if (!enc[i].has(v)) enc[i].set(v, c);
281
+ });
282
+ });
283
+ models.set(m, { n, dec, enc, size: codes.length });
284
+ }
285
+
286
+ export function inner(m) {
287
+ const x = models.get(m);
288
+ if (x === undefined) throw new TypeError('@delurio/cli: a Model made with new Model(flow) is needed');
289
+ return x;
290
+ }
291
+
292
+ export function nativeModel(m) {
293
+ return inner(m).n;
294
+ }
295
+
296
+ function decodeOne(x, i, c) {
297
+ const d = x.dec[i];
298
+ if (d.has(c)) return d.get(c);
299
+ const j = x.n.valueAt(i, c);
300
+ const v = j === null || j === undefined ? undefined : JSON.parse(j);
301
+ d.set(c, v);
302
+ if (!x.enc[i].has(v)) x.enc[i].set(v, c);
303
+ return v;
304
+ }
305
+
306
+ // the values of the context at codes[at .. at + size]
307
+ export function decodeAt(m, codes, at) {
308
+ const x = inner(m);
309
+ const out = new Array(x.size);
310
+ for (let i = 0; i < x.size; i++) out[i] = decodeOne(x, i, codes[at + i]);
311
+ return out;
312
+ }
313
+
314
+ export function decodeAll(m, codes) {
315
+ return decodeAt(m, codes, 0);
316
+ }
317
+
318
+ // a context's places, one per variable (a missing value is undefined, one past the variables is left out)
319
+ export function encode(m, ctx) {
320
+ const x = inner(m);
321
+ const out = new Uint16Array(x.size);
322
+ for (let i = 0; i < x.size; i++) {
323
+ const v = ctx?.[i];
324
+ let c = x.enc[i].get(v);
325
+ if (c === undefined) {
326
+ c = call(null, () => x.n.codeOf(i, v === undefined ? undefined : JSON.stringify(v)));
327
+ x.enc[i].set(v, c);
328
+ if (!x.dec[i].has(c)) x.dec[i].set(c, v);
329
+ }
330
+ out[i] = c;
331
+ }
332
+ return out;
333
+ }
334
+
335
+ // many contexts, one after the other
336
+ export function encodeAll(m, ctxs) {
337
+ const x = inner(m);
338
+ const out = new Uint16Array(ctxs.length * x.size);
339
+ ctxs.forEach((c, k) => out.set(encode(m, c), k * x.size));
340
+ return out;
341
+ }
342
+
343
+ export function size(m) {
344
+ return inner(m).size;
345
+ }
346
+
347
+ // ---- explorations
348
+
349
+ const explorations = new WeakMap();
350
+
351
+ export function exploration(m, nx) {
352
+ const ex = {};
353
+ const materialize = () => {
354
+ const { names, state, parent, event, ctx } = nx.configs();
355
+ const n = size(m);
356
+ const out = new Array(state.length);
357
+ for (let i = 0; i < state.length; i++) {
358
+ out[i] = { state: names[state[i]], ctx: decodeAt(m, ctx, i * n), parent: parent[i], event: event[i] === 0 ? null : names[event[i] - 1] };
359
+ }
360
+ return out;
361
+ };
362
+ const settle = (value) => Object.defineProperty(ex, 'configs', { value, writable: true, enumerable: true, configurable: true });
363
+ // the pairs are made into objects only when they are read: a caller that reads only `taken` or `starts` keeps none
364
+ Object.defineProperty(ex, 'configs', {
365
+ enumerable: true,
366
+ configurable: true,
367
+ get() {
368
+ const v = materialize();
369
+ settle(v);
370
+ return v;
371
+ },
372
+ set(v) {
373
+ settle(v);
374
+ },
375
+ });
376
+ ex.starts = nx.starts();
377
+ ex.taken = new Set(nx.taken());
378
+ ex.atState = new Map(JSON.parse(nx.atState()));
379
+ ex.branchCounts = new Map(JSON.parse(nx.branchCounts()));
380
+ explorations.set(ex, nx);
381
+ return ex;
382
+ }
383
+
384
+ export function nativeExploration(ex) {
385
+ const nx = explorations.get(object(ex, 'the exploration'));
386
+ if (nx === undefined) throw new TypeError('@delurio/cli: an Exploration that explore() gave is needed');
387
+ return nx;
388
+ }
389
+
390
+ // path steps a caller hands in, as the addon reads them
391
+ export function pathSteps(m, steps) {
392
+ const list = Array.isArray(steps) ? steps : [];
393
+ return {
394
+ state: list.map((s) => String(s.state)),
395
+ event: list.map((s) => (s.event === null || s.event === undefined ? null : String(s.event))),
396
+ taken: list.map((s) => (s.taken === null || s.taken === undefined ? null : String(s.taken))),
397
+ ctx: encodeAll(
398
+ m,
399
+ list.map((s) => s.ctx ?? []),
400
+ ),
401
+ };
402
+ }
403
+
404
+ // path steps the addon gives, as objects
405
+ export function pathStepsOf(m, p) {
406
+ const n = size(m);
407
+ return p.state.map((state, k) => ({ state, ctx: decodeAt(m, p.ctx, k * n), event: p.event[k] ?? null, taken: p.taken[k] ?? null }));
408
+ }
package/js/kit.js ADDED
@@ -0,0 +1,54 @@
1
+ // A kit: a design system's templates and styles, which the screens are drawn with (SPEC.md §4). The checker is the
2
+ // Rust one (crates/delurio-core/src/kit.rs); a kit loadKit read stays in the addon for the object it gave.
3
+ import { KitError, Trap, call, json, native, nativeKit, rememberKit, warnings } from './internal.js';
4
+
5
+ export { KitError };
6
+
7
+ // a kit as loadKit gave it: each component's `intents` is a key of its own, undefined when the kit gives none
8
+ function kitOf(n) {
9
+ const k = JSON.parse(n.json());
10
+ for (const [name, c] of Object.entries(k.components)) k.components[name] = { template: c.template, roles: c.roles, intents: c.intents };
11
+ return rememberKit(k, n);
12
+ }
13
+
14
+ // The node drawn with the kit, or null when the kit has no template for the component.
15
+ export function drawWith(kit, component, f, warn) {
16
+ const t = new Trap();
17
+ return call(t, () => native.drawWith(nativeKit(kit), String(component), json(f), t.warner(warn))) ?? null;
18
+ }
19
+
20
+ // A List or ListItem is drawn without a wrapper: the node id goes on the template's one element, and on the page the
21
+ // annotation chips go inside it, last.
22
+ export function mark(html, id, chips) {
23
+ return native.mark(String(html), String(id), String(chips));
24
+ }
25
+
26
+ // the kit in a folder (its kit.yaml), or in a kit.yaml given itself
27
+ export function loadKit(path) {
28
+ try {
29
+ return kitOf(call(null, () => native.loadKit(String(path))));
30
+ } finally {
31
+ warnings();
32
+ }
33
+ }
34
+
35
+ // `--kit <path>` wins over `kit:` in ui.yaml (a path relative to the graph); `--kit none` draws the fixed components.
36
+ export function resolveKit(graphDir, uiKit, flagValue) {
37
+ try {
38
+ const n = call(null, () => native.resolveKit(String(graphDir), uiKit ?? undefined, flagValue ?? undefined));
39
+ return n === null || n === undefined ? null : kitOf(n);
40
+ } finally {
41
+ warnings();
42
+ }
43
+ }
44
+
45
+ // The CSS a kit adds: its tokens and its CSS, scoped to the wireframe, and the few rules that keep the page's own
46
+ // marks readable or fit the screens to the video stage.
47
+ export function kitCss(kit, target) {
48
+ return native.kitCss(nativeKit(kit), String(target));
49
+ }
50
+
51
+ // Runs once in the video stage, before the first frame: the screens are drawn at the largest zoom at which every one fits.
52
+ export function kitStageScript(kit) {
53
+ return native.kitStageScript(nativeKit(kit));
54
+ }
@@ -1,5 +1,5 @@
1
- import type { ErrorPlace, Flow, Scalar } from './graph.ts';
2
- import type { Value } from './expr.ts';
1
+ import type { ErrorPlace, Flow, Scalar } from './graph.js';
2
+ import type { Value } from './expr.js';
3
3
  export declare class ModelError extends Error {
4
4
  readonly place: ErrorPlace | undefined;
5
5
  constructor(message: string, place?: ErrorPlace);
package/js/model.js ADDED
@@ -0,0 +1,136 @@
1
+ // The model checker (SPEC.md §2, §10): every (state, context) pair a flow can reach, found breadth first, and the
2
+ // guards, effects and assertions it is held to. The checker is the Rust one (crates/delurio-core/src/model.rs): a
3
+ // Model holds the addon's model of its flow, and an exploration stays in the addon until its pairs are read.
4
+ import {
5
+ ModelError,
6
+ Trap,
7
+ call,
8
+ decodeAt,
9
+ encode,
10
+ encodeAll,
11
+ exploration,
12
+ inner,
13
+ native,
14
+ nativeExploration,
15
+ nativeModel,
16
+ nativeValue,
17
+ placed,
18
+ registerModel,
19
+ } from './internal.js';
20
+
21
+ export { ModelError };
22
+
23
+ export class Model {
24
+ flow;
25
+ vars;
26
+ domains;
27
+ #any;
28
+
29
+ constructor(flow) {
30
+ const n = call(null, () => new native.NativeModel(nativeValue(flow)));
31
+ this.flow = flow;
32
+ this.vars = n.vars();
33
+ this.domains = this.vars.map((v) => flow.context[v].values);
34
+ this.#any = n.anyVars();
35
+ registerModel(this, n, this.domains);
36
+ }
37
+
38
+ test(expr, state, ctx) {
39
+ return call(null, () => nativeModel(this).test(String(expr), String(state), encode(this, ctx)));
40
+ }
41
+
42
+ /**
43
+ * Whether evaluating `src` can never throw: it names only the state, variables, and guards that parse and nest no
44
+ * deeper than evaluation allows.
45
+ */
46
+ pure(src) {
47
+ return nativeModel(this).pure(String(src));
48
+ }
49
+
50
+ /** The value of `src` in `state` whatever the context holds, when the state alone decides it; undefined when the context matters. */
51
+ inState(src, state) {
52
+ const j = call(null, () => nativeModel(this).inState(String(src), String(state)));
53
+ return j === null || j === undefined ? undefined : JSON.parse(j);
54
+ }
55
+
56
+ /** `test` for one expression at many pairs: read once, then run at each. */
57
+ tester(expr) {
58
+ call(null, () => nativeModel(this).tester(String(expr)));
59
+ return (state, ctx) => this.test(expr, state, ctx);
60
+ }
61
+
62
+ value(expr, state, ctx) {
63
+ return JSON.parse(call(null, () => nativeModel(this).value(String(expr), String(state), encode(this, ctx))));
64
+ }
65
+
66
+ steps(state, ctx) {
67
+ const x = inner(this);
68
+ const out = call(null, () => x.n.steps(String(state), encode(this, ctx)));
69
+ return out.target.map((target, k) => ({ event: out.event[k] ?? null, target, ctx: decodeAt(this, out.ctx, k * x.size), taken: out.taken[k] }));
70
+ }
71
+
72
+ /**
73
+ * `err`, thrown by `steps(state, ctx)`, given the place in flow.yaml of the transition that threw it when it has
74
+ * none; else the state.
75
+ */
76
+ placeError(err, state, ctx) {
77
+ if (!(err instanceof Error) || err.place !== undefined) return err;
78
+ const p = nativeModel(this).placeOf(String(err.message), String(state), encode(this, ctx));
79
+ return p === null || p === undefined ? err : placed(err, JSON.parse(p));
80
+ }
81
+
82
+ initials() {
83
+ const x = inner(this);
84
+ const { count, ctx } = nativeModel(this).initials();
85
+ const out = new Array(count);
86
+ for (let k = 0; k < count; k++) out[k] = decodeAt(this, ctx, k * x.size);
87
+ return out;
88
+ }
89
+
90
+ scenario(ctx) {
91
+ const out = {};
92
+ for (const i of this.#any) out[this.vars[i]] = ctx[i];
93
+ return out;
94
+ }
95
+ }
96
+
97
+ function starts(m, from) {
98
+ if (from === undefined) return [undefined, undefined];
99
+ return [encodeAll(m, from), from.length];
100
+ }
101
+
102
+ // Every reachable (state, context) pair, breadth first, so the first hit of a state is a shortest path to it.
103
+ // `from` and `keep` narrow the search: the replay starts only from situations the app is bound to and takes only steps it can drive.
104
+ export function explore(m, from, keep) {
105
+ const t = new Trap();
106
+ const [codes, count] = starts(m, from);
107
+ const nx = call(t, () => native.explore(nativeModel(m), codes, count, keep === undefined || keep === null ? undefined : t.keeper(keep, m)));
108
+ return exploration(m, nx);
109
+ }
110
+
111
+ export function pathTo(m, ex, index) {
112
+ return JSON.parse(native.pathTo(nativeModel(m), nativeExploration(ex), index));
113
+ }
114
+
115
+ export function checkAssertions(m, ex) {
116
+ return JSON.parse(call(null, () => native.checkAssertions(nativeModel(m), nativeExploration(ex))));
117
+ }
118
+
119
+ export function finalPaths(m, ex) {
120
+ return JSON.parse(native.finalPaths(nativeModel(m), nativeExploration(ex)));
121
+ }
122
+
123
+ export function transitionsOf(flow) {
124
+ return native.transitionsOf(nativeValue(flow));
125
+ }
126
+
127
+ /**
128
+ * Event paths that together pass every reachable state (`states`) or take every reachable transition (`transitions`)
129
+ * of the flow (#4), from the same starts and through the same steps as `explore(m, from, keep)`.
130
+ */
131
+ export function cover(m, by, from, keep) {
132
+ const t = new Trap();
133
+ const [codes, count] = starts(m, from);
134
+ return JSON.parse(call(t, () => native.cover(nativeModel(m), String(by), codes, count, keep === undefined || keep === null ? undefined : t.keeper(keep, m))));
135
+ }
136
+
package/js/native.cjs ADDED
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+ // The addon this platform runs (crates/delurio-node, built with napi-rs): in a checkout, a local build at the
3
+ // package's root (`npm run build`: delurio.<target>.node); else the prebuilt one of @delurio/cli-<target>, the
4
+ // optional dependency npm installs only on the platform it is for. DELURIO_ADDON names another file to load.
5
+ // Under Node's permission model a native addon needs --allow-addons.
6
+ const { existsSync } = require('node:fs');
7
+ const { join } = require('node:path');
8
+
9
+ // glibc says its version in the process report; musl (Alpine) says none
10
+ function libc() {
11
+ const report = typeof process.report?.getReport === 'function' ? process.report.getReport() : null;
12
+ const header = report !== null && typeof report === 'object' ? report.header : undefined;
13
+ return header !== undefined && header.glibcVersionRuntime !== undefined ? 'gnu' : 'musl';
14
+ }
15
+
16
+ function target() {
17
+ const { platform, arch } = process;
18
+ if (platform === 'linux') return `linux-${arch}-${libc()}`;
19
+ if (platform === 'win32') return `win32-${arch}-msvc`;
20
+ return `${platform}-${arch}`;
21
+ }
22
+
23
+ function load() {
24
+ const t = target();
25
+ const tried = [];
26
+ const candidates = [];
27
+ if (process.env.DELURIO_ADDON) candidates.push(process.env.DELURIO_ADDON);
28
+ candidates.push(join(__dirname, '..', `delurio.${t}.node`));
29
+ for (const file of candidates) {
30
+ if (!existsSync(file)) {
31
+ tried.push(`${file}: not there`);
32
+ continue;
33
+ }
34
+ return require(file);
35
+ }
36
+ try {
37
+ return require(`@delurio/cli-${t}`);
38
+ } catch (err) {
39
+ tried.push(`@delurio/cli-${t}: ${err instanceof Error ? err.message.split('\n')[0] : String(err)}`);
40
+ }
41
+ throw new Error(
42
+ `@delurio/cli has no build for ${t} here. npm installs the one for this platform (@delurio/cli-${t}) as an optional dependency: install without --no-optional / --omit=optional, and under Node's permission model allow it with --allow-addons. Tried:\n ${tried.join('\n ')}`,
43
+ );
44
+ }
45
+
46
+ module.exports = load();