@adecore/intelligent-ui 0.39.0 → 0.40.1
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/dist/compiler.js +10 -12
- package/dist/expression.js +18 -23
- package/dist/inputs.d.ts +1 -0
- package/dist/inputs.js +23 -20
- package/dist/links.js +6 -12
- package/dist/places.js +6 -12
- package/dist/rethrow.d.ts +1 -0
- package/dist/rethrow.js +7 -0
- package/dist/runtime.js +12 -12
- package/dist/syntax.js +5 -12
- package/package.json +4 -4
- package/src/actions.ts +7 -0
- package/src/budget.ts +9 -0
- package/src/compiler.ts +111 -15
- package/src/event-time.ts +14 -0
- package/src/event-warning.ts +2 -0
- package/src/expression.ts +146 -23
- package/src/fallback.ts +6 -0
- package/src/inputs.ts +37 -20
- package/src/links.ts +10 -13
- package/src/places.ts +9 -12
- package/src/prop-alias.ts +1 -0
- package/src/prop-list.ts +2 -0
- package/src/query.ts +15 -0
- package/src/rethrow.ts +8 -0
- package/src/runtime.ts +95 -19
- package/src/stream.ts +9 -0
- package/src/syntax.ts +97 -12
- package/src/text.ts +9 -0
package/dist/compiler.js
CHANGED
|
@@ -5,6 +5,7 @@ import { unreadableEventTime } from './event-warning.js';
|
|
|
5
5
|
import { uiNodeFallback, uiPlainText } from './fallback.js';
|
|
6
6
|
import { aliasedProp } from './prop-alias.js';
|
|
7
7
|
import { evaluateUiExpression, sameUiValue } from './expression.js';
|
|
8
|
+
import { rethrowBudget } from './rethrow.js';
|
|
8
9
|
import { parseUiSyntax, uiDiagnostic } from './syntax.js';
|
|
9
10
|
import {} from 'zod';
|
|
10
11
|
// The info string a fence carries when the host names none of its own.
|
|
@@ -14,15 +15,17 @@ export const UI_REPLY_LIMITS = { blocks: 16, characters: 262144, nodes: 2048, mi
|
|
|
14
15
|
export const UI_HOST_LIMITS = { queries: 8, links: 64, places: 50, refreshMilliseconds: 10_000 };
|
|
15
16
|
/* A Place that names an address, whose tag may hold quoted text with a `>` before it. */
|
|
16
17
|
const ADDRESSED_PLACE = /<Place\b(?:[^>"']|"[^"]*"|'[^']*')*?\baddress\s*=/;
|
|
18
|
+
let linkTag;
|
|
17
19
|
/* A cheap test for whether `text` may declare a query or name a link target, before anything is read for it. */
|
|
18
20
|
export function uiMayReferenceHost(text) {
|
|
19
21
|
if (text.includes('@Query')) {
|
|
20
22
|
return true;
|
|
21
23
|
}
|
|
22
|
-
|
|
24
|
+
linkTag ??= new RegExp(`<(?:${Object.entries(UI_CATALOG)
|
|
23
25
|
.filter(([, entry]) => entry.group === 'links')
|
|
24
|
-
.map(([name]) => name)
|
|
25
|
-
|
|
26
|
+
.map(([name]) => name)
|
|
27
|
+
.join('|')})\\b`);
|
|
28
|
+
return linkTag.test(text) || ADDRESSED_PLACE.test(text);
|
|
26
29
|
}
|
|
27
30
|
/* The children of a Map as its fallback numbers them: each Place that drew, also inside a Show or an Each, in order, and anything else as it is. */
|
|
28
31
|
function numberedPlaces(children) {
|
|
@@ -36,12 +39,6 @@ function numberedPlaces(children) {
|
|
|
36
39
|
.join('');
|
|
37
40
|
return visit(children);
|
|
38
41
|
}
|
|
39
|
-
/* A spent budget ends the whole block; every other failure only marks the node or declaration it came from. */
|
|
40
|
-
function rethrowBudget(error) {
|
|
41
|
-
if (error instanceof UiFailure && error.code === 'budget_exceeded') {
|
|
42
|
-
throw error;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
42
|
function isQuery(expression) {
|
|
46
43
|
return expression.kind === 'call' && expression.name === 'Query';
|
|
47
44
|
}
|
|
@@ -134,9 +131,10 @@ export function compileUiBlock(source, options) {
|
|
|
134
131
|
block.diagnostics.push(uiDiagnostic(error, start, end, nodeId));
|
|
135
132
|
}
|
|
136
133
|
};
|
|
134
|
+
const local = uiLocalState(block);
|
|
137
135
|
// A pressed action must set the same value whatever state it is pressed in, so a choice can check it.
|
|
138
136
|
const checkedAction = (action) => {
|
|
139
|
-
uiActionTarget(action,
|
|
137
|
+
uiActionTarget(action, local);
|
|
140
138
|
if (action.kind === 'call' && action.name === 'Set' && !isConstant(action.args[1])) {
|
|
141
139
|
throw new UiFailure('refused_action', '@Set in an action takes a literal value.');
|
|
142
140
|
}
|
|
@@ -213,7 +211,7 @@ export function compileUiBlock(source, options) {
|
|
|
213
211
|
}
|
|
214
212
|
else {
|
|
215
213
|
node.expressions[key] = expression;
|
|
216
|
-
if ('binding' in entry && entry.binding === key && expression.kind === 'reference' &&
|
|
214
|
+
if ('binding' in entry && entry.binding === key && expression.kind === 'reference' && local(expression.name)) {
|
|
217
215
|
node.bindings[key] = expression.name;
|
|
218
216
|
}
|
|
219
217
|
}
|
|
@@ -311,7 +309,7 @@ export function compileUiBlock(source, options) {
|
|
|
311
309
|
const declarations = Object.entries(syntax.declarations).sort(([, left], [, right]) => Number(isQuery(left)) - Number(isQuery(right)));
|
|
312
310
|
for (const [name, expression] of declarations) {
|
|
313
311
|
try {
|
|
314
|
-
if (expression
|
|
312
|
+
if (isQuery(expression)) {
|
|
315
313
|
block.queries[name] = compileQuery(expression, block, budget, options.querySchemas);
|
|
316
314
|
}
|
|
317
315
|
else {
|
package/dist/expression.js
CHANGED
|
@@ -17,6 +17,7 @@ const PRECEDENCE = {
|
|
|
17
17
|
'%': 6
|
|
18
18
|
};
|
|
19
19
|
const HELPERS = new Set(['Count', 'Filter', 'Sum', 'Join', 'Round', 'Query', 'Set', 'Reset']);
|
|
20
|
+
const ESCAPES = { n: '\n', r: '\r', t: '\t', b: '\b', f: '\f', '\\': '\\', '"': '"', "'": "'", '/': '/' };
|
|
20
21
|
class ExpressionParser {
|
|
21
22
|
position = 0;
|
|
22
23
|
token;
|
|
@@ -82,16 +83,7 @@ class ExpressionParser {
|
|
|
82
83
|
this.take(')');
|
|
83
84
|
}
|
|
84
85
|
else if (token.value === '[') {
|
|
85
|
-
|
|
86
|
-
while (this.token.value !== ']') {
|
|
87
|
-
items.push(this.expression(0, depth));
|
|
88
|
-
if (this.token.value !== ',') {
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
this.take(',');
|
|
92
|
-
}
|
|
93
|
-
this.take(']');
|
|
94
|
-
result = { kind: 'array', items };
|
|
86
|
+
result = { kind: 'array', items: this.list(']', depth) };
|
|
95
87
|
}
|
|
96
88
|
else if (token.value === '{') {
|
|
97
89
|
const entries = [];
|
|
@@ -122,16 +114,7 @@ class ExpressionParser {
|
|
|
122
114
|
throw new UiFailure('refused_call', `Unknown helper @${name}.`);
|
|
123
115
|
}
|
|
124
116
|
this.take('(');
|
|
125
|
-
|
|
126
|
-
while (this.token.value !== ')') {
|
|
127
|
-
args.push(this.expression(0, depth));
|
|
128
|
-
if (this.token.value !== ',') {
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
this.take(',');
|
|
132
|
-
}
|
|
133
|
-
this.take(')');
|
|
134
|
-
result = { kind: 'call', name, args };
|
|
117
|
+
result = { kind: 'call', name, args: this.list(')', depth) };
|
|
135
118
|
}
|
|
136
119
|
else if (token.type === 'name' && ['true', 'false', 'null'].includes(token.value)) {
|
|
137
120
|
result = { kind: 'literal', value: token.value === 'null' ? null : token.value === 'true' };
|
|
@@ -160,6 +143,19 @@ class ExpressionParser {
|
|
|
160
143
|
}
|
|
161
144
|
return result;
|
|
162
145
|
}
|
|
146
|
+
/* Expressions separated by commas up to `close`, which it takes too. */
|
|
147
|
+
list(close, depth) {
|
|
148
|
+
const items = [];
|
|
149
|
+
while (this.token.value !== close) {
|
|
150
|
+
items.push(this.expression(0, depth));
|
|
151
|
+
if (this.token.value !== ',') {
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
this.take(',');
|
|
155
|
+
}
|
|
156
|
+
this.take(close);
|
|
157
|
+
return items;
|
|
158
|
+
}
|
|
163
159
|
read() {
|
|
164
160
|
this.budget.step();
|
|
165
161
|
while (/\s/.test(this.source[this.position] ?? '') && this.position < this.source.length) {
|
|
@@ -184,7 +180,6 @@ class ExpressionParser {
|
|
|
184
180
|
}
|
|
185
181
|
if (character === '\\') {
|
|
186
182
|
character = this.source[this.position++];
|
|
187
|
-
const escapes = { n: '\n', r: '\r', t: '\t', b: '\b', f: '\f', '\\': '\\', '"': '"', "'": "'", '/': '/' };
|
|
188
183
|
if (character === 'u') {
|
|
189
184
|
const hex = this.source.slice(this.position, this.position + 4);
|
|
190
185
|
if (!/^[a-f\d]{4}$/i.test(hex)) {
|
|
@@ -193,8 +188,8 @@ class ExpressionParser {
|
|
|
193
188
|
value += String.fromCharCode(Number.parseInt(hex, 16));
|
|
194
189
|
this.position += 4;
|
|
195
190
|
}
|
|
196
|
-
else if (Object.hasOwn(
|
|
197
|
-
value +=
|
|
191
|
+
else if (Object.hasOwn(ESCAPES, character)) {
|
|
192
|
+
value += ESCAPES[character];
|
|
198
193
|
}
|
|
199
194
|
else {
|
|
200
195
|
throw new UiFailure('invalid_expression', 'Invalid string escape.');
|
package/dist/inputs.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ import type { UiBlock, UiNode } from './compiler.ts';
|
|
|
2
2
|
import { type UiValue } from './expression.ts';
|
|
3
3
|
import type { UiViewNode } from './runtime.ts';
|
|
4
4
|
export declare function uiCompiledNodes(nodes: readonly UiNode[], into?: Map<string, UiNode>): Map<string, UiNode>;
|
|
5
|
+
export declare function eachDrawnNode(nodes: readonly UiViewNode[], visit: (node: UiViewNode) => void): void;
|
|
5
6
|
export declare function uiVisibleInputs(block: UiBlock, nodes: readonly UiViewNode[], scope: Readonly<Record<string, UiValue>>): Set<string>;
|
package/dist/inputs.js
CHANGED
|
@@ -7,6 +7,16 @@ export function uiCompiledNodes(nodes, into = new Map()) {
|
|
|
7
7
|
}
|
|
8
8
|
return into;
|
|
9
9
|
}
|
|
10
|
+
/* Every node that drew, parents first; a node that failed or never closed hides its children too. */
|
|
11
|
+
export function eachDrawnNode(nodes, visit) {
|
|
12
|
+
for (const node of nodes) {
|
|
13
|
+
if (node.error || !node.complete) {
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
visit(node);
|
|
17
|
+
eachDrawnNode(node.children, visit);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
10
20
|
/* Whether a control's value is one its own visible options offer. */
|
|
11
21
|
function offered(node) {
|
|
12
22
|
if (node.type === 'Segmented' || node.type === 'Select') {
|
|
@@ -45,28 +55,21 @@ function actionValue(action, block) {
|
|
|
45
55
|
export function uiVisibleInputs(block, nodes, scope) {
|
|
46
56
|
const written = uiCompiledNodes(block.nodes);
|
|
47
57
|
const inputs = new Set();
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (source && offered(node)) {
|
|
55
|
-
for (const prop of Object.keys(node.bindings)) {
|
|
56
|
-
if (source.bindings[prop]) {
|
|
57
|
-
inputs.add(source.bindings[prop]);
|
|
58
|
-
}
|
|
58
|
+
eachDrawnNode(nodes, (node) => {
|
|
59
|
+
const source = written.get(node.sourceId ?? node.id);
|
|
60
|
+
if (source && offered(node)) {
|
|
61
|
+
for (const prop of Object.keys(node.bindings)) {
|
|
62
|
+
if (source.bindings[prop]) {
|
|
63
|
+
inputs.add(source.bindings[prop]);
|
|
59
64
|
}
|
|
60
65
|
}
|
|
61
|
-
const actionProp = uiActionProp(node.type);
|
|
62
|
-
const action = source && actionProp ? source.expressions[actionProp] : undefined;
|
|
63
|
-
const pressed = action && node.props.disabled !== true ? actionValue(action, block) : undefined;
|
|
64
|
-
if (pressed && sameUiValue(pressed.value, scope[pressed.target])) {
|
|
65
|
-
inputs.add(pressed.target);
|
|
66
|
-
}
|
|
67
|
-
visit(node.children);
|
|
68
66
|
}
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
const actionProp = uiActionProp(node.type);
|
|
68
|
+
const action = source && actionProp ? source.expressions[actionProp] : undefined;
|
|
69
|
+
const pressed = action && node.props.disabled !== true ? actionValue(action, block) : undefined;
|
|
70
|
+
if (pressed && sameUiValue(pressed.value, scope[pressed.target])) {
|
|
71
|
+
inputs.add(pressed.target);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
71
74
|
return inputs;
|
|
72
75
|
}
|
package/dist/links.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { eachDrawnNode } from './inputs.js';
|
|
2
3
|
import { evaluateUiBlock } from './runtime.js';
|
|
3
4
|
import { uiValidatedState } from './query.js';
|
|
4
5
|
export const UiLinkTargetSchema = z.discriminatedUnion('type', [
|
|
@@ -25,18 +26,11 @@ export const UiLinkResolutionSchema = z.object({
|
|
|
25
26
|
export function uiLinkTargets(block, input = {}, queries = {}, limits = {}) {
|
|
26
27
|
const state = uiValidatedState(block, input, queries, limits);
|
|
27
28
|
const targets = Object.create(null);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
const checked = UiLinkTargetSchema.safeParse({ type: node.type, ...node.props });
|
|
34
|
-
if (checked.success) {
|
|
35
|
-
targets[node.id] = checked.data;
|
|
36
|
-
}
|
|
37
|
-
visit(node.children);
|
|
29
|
+
eachDrawnNode(evaluateUiBlock(block, state, limits).nodes, (node) => {
|
|
30
|
+
const checked = UiLinkTargetSchema.safeParse({ type: node.type, ...node.props });
|
|
31
|
+
if (checked.success) {
|
|
32
|
+
targets[node.id] = checked.data;
|
|
38
33
|
}
|
|
39
|
-
};
|
|
40
|
-
visit(evaluateUiBlock(block, state, limits).nodes);
|
|
34
|
+
});
|
|
41
35
|
return targets;
|
|
42
36
|
}
|
package/dist/places.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { eachDrawnNode } from './inputs.js';
|
|
1
2
|
import { uiValidatedState } from './query.js';
|
|
2
3
|
import { evaluateUiBlock } from './runtime.js';
|
|
3
4
|
function nameOf(node) {
|
|
@@ -15,18 +16,11 @@ function nameOf(node) {
|
|
|
15
16
|
export function uiPlaceQueries(block, input = {}, queries = {}, limits = {}) {
|
|
16
17
|
const state = uiValidatedState(block, input, queries, limits);
|
|
17
18
|
const places = Object.create(null);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
const { address } = node.props;
|
|
24
|
-
if (node.type === 'Place' && typeof address === 'string' && address.trim() !== '') {
|
|
25
|
-
places[node.id] = { address: address.trim(), name: nameOf(node) };
|
|
26
|
-
}
|
|
27
|
-
visit(node.children);
|
|
19
|
+
eachDrawnNode(evaluateUiBlock(block, state, limits).nodes, (node) => {
|
|
20
|
+
const { address } = node.props;
|
|
21
|
+
if (node.type === 'Place' && typeof address === 'string' && address.trim() !== '') {
|
|
22
|
+
places[node.id] = { address: address.trim(), name: nameOf(node) };
|
|
28
23
|
}
|
|
29
|
-
};
|
|
30
|
-
visit(evaluateUiBlock(block, state, limits).nodes);
|
|
24
|
+
});
|
|
31
25
|
return places;
|
|
32
26
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function rethrowBudget(error: unknown): void;
|
package/dist/rethrow.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { UiFailure } from './budget.js';
|
|
2
|
+
/* A spent budget ends the whole block; every other failure only marks the part it came from. */
|
|
3
|
+
export function rethrowBudget(error) {
|
|
4
|
+
if (error instanceof UiFailure && error.code === 'budget_exceeded') {
|
|
5
|
+
throw error;
|
|
6
|
+
}
|
|
7
|
+
}
|
package/dist/runtime.js
CHANGED
|
@@ -100,7 +100,13 @@ export function evaluateUiBlock(block, state = new UiState(block), limits = {})
|
|
|
100
100
|
const budget = new UiBudget(limits);
|
|
101
101
|
const result = { nodes: [], diagnostics: [] };
|
|
102
102
|
const scope = state.scope();
|
|
103
|
+
const local = uiLocalState(block);
|
|
103
104
|
let exhausted = false;
|
|
105
|
+
const report = (error, node) => {
|
|
106
|
+
if (result.diagnostics.length < budget.limits.diagnostics) {
|
|
107
|
+
result.diagnostics.push(uiDiagnostic(error, node.start, node.end, node.id));
|
|
108
|
+
}
|
|
109
|
+
};
|
|
104
110
|
const children = (nodes, variables, depth, suffix) => {
|
|
105
111
|
const output = [];
|
|
106
112
|
for (const node of nodes) {
|
|
@@ -155,9 +161,7 @@ export function evaluateUiBlock(block, state = new UiState(block), limits = {})
|
|
|
155
161
|
// The compiler already warned about a time it could read from the source; this one waited on an Each or a query.
|
|
156
162
|
if (unreadable !== null &&
|
|
157
163
|
![...block.diagnostics, ...result.diagnostics].some((item) => item.code === 'unreadable_time' && item.nodeId === node.id)) {
|
|
158
|
-
|
|
159
|
-
result.diagnostics.push(uiDiagnostic(unreadable, node.start, node.end, node.id));
|
|
160
|
-
}
|
|
164
|
+
report(unreadable, node);
|
|
161
165
|
}
|
|
162
166
|
if (node.type === 'Show') {
|
|
163
167
|
return props.when ? children(node.children, variables, depth + 1, suffix) : [];
|
|
@@ -180,7 +184,7 @@ export function evaluateUiBlock(block, state = new UiState(block), limits = {})
|
|
|
180
184
|
}
|
|
181
185
|
const bindings = Object.create(null);
|
|
182
186
|
for (const [key, name] of Object.entries(node.bindings)) {
|
|
183
|
-
if (!('binding' in entry) || entry.binding !== key || !
|
|
187
|
+
if (!('binding' in entry) || entry.binding !== key || !local(name)) {
|
|
184
188
|
throw new UiFailure('refused_binding', 'The binding must target declared local input state.');
|
|
185
189
|
}
|
|
186
190
|
bindings[key] = {
|
|
@@ -199,7 +203,7 @@ export function evaluateUiBlock(block, state = new UiState(block), limits = {})
|
|
|
199
203
|
view.bindings = bindings;
|
|
200
204
|
const action = actionProp === undefined ? undefined : node.expressions[actionProp];
|
|
201
205
|
if (action) {
|
|
202
|
-
uiActionTarget(action,
|
|
206
|
+
uiActionTarget(action, local);
|
|
203
207
|
view.onAction = () => {
|
|
204
208
|
if (!node.complete || props.disabled === true) {
|
|
205
209
|
return;
|
|
@@ -228,9 +232,7 @@ export function evaluateUiBlock(block, state = new UiState(block), limits = {})
|
|
|
228
232
|
let kept = 0;
|
|
229
233
|
view.children = view.children.filter((child) => !kinds.includes(child.type) || ++kept <= limit.max);
|
|
230
234
|
view.omitted = counted - limit.max;
|
|
231
|
-
|
|
232
|
-
result.diagnostics.push(uiDiagnostic(new UiFailure('too_many_children', `${node.type} shows its first ${limit.max} of ${kinds.join(' or ')}.`), node.start, node.end, node.id));
|
|
233
|
-
}
|
|
235
|
+
report(new UiFailure('too_many_children', `${node.type} shows its first ${limit.max} of ${kinds.join(' or ')}.`), node);
|
|
234
236
|
}
|
|
235
237
|
}
|
|
236
238
|
return [view];
|
|
@@ -240,9 +242,7 @@ export function evaluateUiBlock(block, state = new UiState(block), limits = {})
|
|
|
240
242
|
exhausted = true;
|
|
241
243
|
}
|
|
242
244
|
view.error = error instanceof UiFailure ? error.code : 'invalid_value';
|
|
243
|
-
|
|
244
|
-
result.diagnostics.push(uiDiagnostic(error, node.start, node.end, node.id));
|
|
245
|
-
}
|
|
245
|
+
report(error, node);
|
|
246
246
|
return [view];
|
|
247
247
|
}
|
|
248
248
|
};
|
|
@@ -287,7 +287,6 @@ export function uiInputValues(block, state) {
|
|
|
287
287
|
visit(block.nodes);
|
|
288
288
|
return values;
|
|
289
289
|
}
|
|
290
|
-
/* The host supplies its stored block; client data can change only declared inputs and Button targets. */
|
|
291
290
|
function hasError(nodes) {
|
|
292
291
|
return nodes.some((node) => node.error !== undefined || hasError(node.children));
|
|
293
292
|
}
|
|
@@ -304,6 +303,7 @@ export function uiChoiceLabel(choice) {
|
|
|
304
303
|
}
|
|
305
304
|
return labelText(choice.children).replace(/\s+/g, ' ').trim() || null;
|
|
306
305
|
}
|
|
306
|
+
/* The host supplies its stored block; client data can change only declared inputs and Button targets. */
|
|
307
307
|
export function resolveUiChoice(block, choiceId, input = {}, queries = {}, limits = {}) {
|
|
308
308
|
if (!block.complete || block.catalogVersion !== UI_CATALOG_VERSION) {
|
|
309
309
|
throw new UiFailure('refused_choice', 'Only a completed supported block can send a choice.');
|
package/dist/syntax.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { safeKey, UiBudget, UiFailure } from './budget.js';
|
|
2
2
|
import { parseUiExpression } from './expression.js';
|
|
3
|
+
import { rethrowBudget } from './rethrow.js';
|
|
3
4
|
const WARNING_CODES = new Set(['refused_prop', 'renamed_prop', 'too_many_series', 'too_many_children', 'unreadable_time']);
|
|
4
5
|
export function uiDiagnostic(error, start, end, nodeId) {
|
|
5
6
|
const code = error instanceof UiFailure ? error.code : 'invalid_syntax';
|
|
@@ -62,9 +63,7 @@ class UiParser {
|
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
catch (error) {
|
|
65
|
-
|
|
66
|
-
throw error;
|
|
67
|
-
}
|
|
66
|
+
rethrowBudget(error);
|
|
68
67
|
this.diagnose(error, start);
|
|
69
68
|
if (this.position <= start) {
|
|
70
69
|
this.position = start + 1;
|
|
@@ -141,9 +140,7 @@ class UiParser {
|
|
|
141
140
|
this.result.declarations[name] = parseUiExpression(value, this.budget);
|
|
142
141
|
}
|
|
143
142
|
catch (error) {
|
|
144
|
-
|
|
145
|
-
throw error;
|
|
146
|
-
}
|
|
143
|
+
rethrowBudget(error);
|
|
147
144
|
if (this.final || this.position < this.source.length) {
|
|
148
145
|
this.diagnose(error, start);
|
|
149
146
|
}
|
|
@@ -228,9 +225,7 @@ class UiParser {
|
|
|
228
225
|
}
|
|
229
226
|
}
|
|
230
227
|
catch (error) {
|
|
231
|
-
|
|
232
|
-
throw error;
|
|
233
|
-
}
|
|
228
|
+
rethrowBudget(error);
|
|
234
229
|
this.diagnose(error, propStart, node.id);
|
|
235
230
|
node.invalid = true;
|
|
236
231
|
this.skipToken();
|
|
@@ -309,9 +304,7 @@ class UiParser {
|
|
|
309
304
|
node.props[name] = parseUiExpression(`${value}${quote}`, this.budget);
|
|
310
305
|
}
|
|
311
306
|
catch (error) {
|
|
312
|
-
|
|
313
|
-
throw error;
|
|
314
|
-
}
|
|
307
|
+
rethrowBudget(error);
|
|
315
308
|
}
|
|
316
309
|
node.end = this.position;
|
|
317
310
|
return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adecore/intelligent-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.1",
|
|
4
4
|
"description": "A streaming UI catalog, compiler and bounded expression interpreter without a framework or DOM.",
|
|
5
5
|
"license": "FSL-1.1-MIT",
|
|
6
6
|
"author": "Bas Milius",
|
|
@@ -99,11 +99,11 @@
|
|
|
99
99
|
"test": "bun test --conditions=source"
|
|
100
100
|
},
|
|
101
101
|
"devDependencies": {
|
|
102
|
-
"@types/bun": "^1.4.
|
|
103
|
-
"@types/node": "^24.
|
|
102
|
+
"@types/bun": "^1.4.4",
|
|
103
|
+
"@types/node": "^24.19.2",
|
|
104
104
|
"typescript": "^7.0.2"
|
|
105
105
|
},
|
|
106
106
|
"dependencies": {
|
|
107
|
-
"zod": "^4.6.
|
|
107
|
+
"zod": "^4.6.7"
|
|
108
108
|
}
|
|
109
109
|
}
|
package/src/actions.ts
CHANGED
|
@@ -8,7 +8,9 @@ export function uiActionProp(type: string): string | undefined {
|
|
|
8
8
|
if (!isUiComponent(type)) {
|
|
9
9
|
return undefined;
|
|
10
10
|
}
|
|
11
|
+
|
|
11
12
|
const entry = UI_CATALOG[type];
|
|
13
|
+
|
|
12
14
|
return 'action' in entry ? entry.action : undefined;
|
|
13
15
|
}
|
|
14
16
|
|
|
@@ -20,16 +22,21 @@ export function uiActionTarget(action: UiExpression, local: (name: string) => bo
|
|
|
20
22
|
if (action.kind !== 'call' || (action.name !== 'Set' && action.name !== 'Reset')) {
|
|
21
23
|
throw new UiFailure('refused_action', 'Expected @Set or @Reset.');
|
|
22
24
|
}
|
|
25
|
+
|
|
23
26
|
if (action.name === 'Reset' && action.args.length === 0) {
|
|
24
27
|
return null;
|
|
25
28
|
}
|
|
29
|
+
|
|
26
30
|
if (action.args.length !== (action.name === 'Set' ? 2 : 1)) {
|
|
27
31
|
throw new UiFailure('refused_action', 'Invalid state action arguments.');
|
|
28
32
|
}
|
|
33
|
+
|
|
29
34
|
const reference = action.args[0];
|
|
35
|
+
|
|
30
36
|
if (reference.kind !== 'reference' || !local(reference.name)) {
|
|
31
37
|
throw new UiFailure('refused_binding', 'The action needs a local state variable.');
|
|
32
38
|
}
|
|
39
|
+
|
|
33
40
|
return reference.name;
|
|
34
41
|
}
|
|
35
42
|
|
package/src/budget.ts
CHANGED
|
@@ -45,16 +45,19 @@ export class UiBudget {
|
|
|
45
45
|
constructor(limits: Partial<UiLimits> = {}, now: () => number = Date.now) {
|
|
46
46
|
this.now = now;
|
|
47
47
|
this.limits = { ...UI_LIMITS, ...limits };
|
|
48
|
+
|
|
48
49
|
for (const [name, value] of Object.entries(this.limits)) {
|
|
49
50
|
if (!Number.isSafeInteger(value) || value < 0 || (value === 0 && name !== 'milliseconds')) {
|
|
50
51
|
throw new UiFailure('invalid_limits', 'Budgets must be positive safe integers, except milliseconds may be zero.');
|
|
51
52
|
}
|
|
52
53
|
}
|
|
54
|
+
|
|
53
55
|
this.started = this.limits.milliseconds > 0 ? now() : 0;
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
step(count = 1): void {
|
|
57
59
|
this.steps += count;
|
|
60
|
+
|
|
58
61
|
if (this.steps > this.limits.steps || (this.limits.milliseconds > 0 && this.now() - this.started > this.limits.milliseconds)) {
|
|
59
62
|
throw new UiFailure('budget_exceeded', 'The operation exceeded its work budget.');
|
|
60
63
|
}
|
|
@@ -63,6 +66,7 @@ export class UiBudget {
|
|
|
63
66
|
iteration(count = 1): void {
|
|
64
67
|
this.iterations += count;
|
|
65
68
|
this.step(count);
|
|
69
|
+
|
|
66
70
|
if (this.iterations > this.limits.iterations) {
|
|
67
71
|
throw new UiFailure('budget_exceeded', 'The operation exceeded its iteration budget.');
|
|
68
72
|
}
|
|
@@ -71,6 +75,7 @@ export class UiBudget {
|
|
|
71
75
|
node(): void {
|
|
72
76
|
this.nodes += 1;
|
|
73
77
|
this.step();
|
|
78
|
+
|
|
74
79
|
if (this.nodes > this.limits.nodes) {
|
|
75
80
|
throw new UiFailure('budget_exceeded', 'The operation exceeded its node budget.');
|
|
76
81
|
}
|
|
@@ -78,6 +83,7 @@ export class UiBudget {
|
|
|
78
83
|
|
|
79
84
|
depth(depth: number): void {
|
|
80
85
|
this.step();
|
|
86
|
+
|
|
81
87
|
if (depth > this.limits.depth) {
|
|
82
88
|
throw new UiFailure('budget_exceeded', 'The operation exceeded its nesting budget.');
|
|
83
89
|
}
|
|
@@ -85,9 +91,11 @@ export class UiBudget {
|
|
|
85
91
|
|
|
86
92
|
string(value: string): string {
|
|
87
93
|
this.step(value.length);
|
|
94
|
+
|
|
88
95
|
if (value.length > this.limits.stringLength) {
|
|
89
96
|
throw new UiFailure('budget_exceeded', 'The operation exceeded its string budget.');
|
|
90
97
|
}
|
|
98
|
+
|
|
91
99
|
return value;
|
|
92
100
|
}
|
|
93
101
|
}
|
|
@@ -96,5 +104,6 @@ export function safeKey(key: string): string {
|
|
|
96
104
|
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
|
|
97
105
|
throw new UiFailure('refused_access', `Access to ${key} is refused.`);
|
|
98
106
|
}
|
|
107
|
+
|
|
99
108
|
return key;
|
|
100
109
|
}
|