@telorun/templating 0.105.0 → 0.107.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/dist/builtins.d.ts +9 -0
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +10 -0
- package/dist/cel/catalog.d.ts +3 -0
- package/dist/cel/catalog.d.ts.map +1 -1
- package/dist/cel/catalog.js +82 -15
- package/dist/cel/compile.d.ts +9 -1
- package/dist/cel/compile.d.ts.map +1 -1
- package/dist/cel/compile.js +40 -0
- package/dist/cel/diagnose.d.ts +2 -1
- package/dist/cel/diagnose.d.ts.map +1 -1
- package/dist/cel/environment.d.ts +4 -0
- package/dist/cel/environment.d.ts.map +1 -1
- package/dist/cel/environment.js +8 -2
- package/dist/cel/json-prefix-scan.d.ts +17 -0
- package/dist/cel/json-prefix-scan.d.ts.map +1 -0
- package/dist/cel/json-prefix-scan.js +160 -0
- package/dist/cel/value-brands.d.ts +16 -0
- package/dist/cel/value-brands.d.ts.map +1 -0
- package/dist/cel/value-brands.js +31 -0
- package/dist/cel/verdict-codes.d.ts +29 -0
- package/dist/cel/verdict-codes.d.ts.map +1 -0
- package/dist/cel/verdict-codes.js +24 -0
- package/dist/engine.d.ts +15 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engines/cel.d.ts +4 -3
- package/dist/engines/cel.d.ts.map +1 -1
- package/dist/engines/cel.js +4 -1
- package/dist/engines/hole-analysis.d.ts +7 -3
- package/dist/engines/hole-analysis.d.ts.map +1 -1
- package/dist/engines/hole-analysis.js +7 -1
- package/dist/engines/include.js +1 -1
- package/dist/engines/interpolate.d.ts.map +1 -1
- package/dist/engines/interpolate.js +6 -3
- package/dist/engines/module-path.js +1 -1
- package/dist/engines/sql.d.ts.map +1 -1
- package/dist/engines/sql.js +4 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/package.json +4 -4
- package/src/builtins.ts +12 -0
- package/src/cel/catalog.ts +93 -17
- package/src/cel/compile.ts +37 -1
- package/src/cel/diagnose.ts +4 -3
- package/src/cel/environment.ts +9 -2
- package/src/cel/json-prefix-scan.ts +152 -0
- package/src/cel/value-brands.ts +33 -0
- package/src/cel/verdict-codes.ts +47 -0
- package/src/engine.ts +16 -0
- package/src/engines/cel.ts +12 -13
- package/src/engines/hole-analysis.ts +15 -15
- package/src/engines/include.ts +1 -1
- package/src/engines/interpolate.ts +17 -6
- package/src/engines/module-path.ts +1 -1
- package/src/engines/sql.ts +11 -1
- package/src/index.ts +3 -0
package/dist/builtins.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Environment } from "@marcbachmann/cel-js";
|
|
1
2
|
import { TemplatingEngineRegistry } from "./registry.js";
|
|
2
3
|
import type { TemplatingEngine } from "./engine.js";
|
|
3
4
|
/** Single source of truth for the built-in templating engines. Every host
|
|
@@ -27,6 +28,14 @@ export declare function producedTypeOf(engineName: string): Record<string, unkno
|
|
|
27
28
|
* asking "what CEL is written here" never recognises a tag by name.
|
|
28
29
|
*/
|
|
29
30
|
export declare function celExpressionsOf(engineName: string, source: string): string[];
|
|
31
|
+
/**
|
|
32
|
+
* Whether a tagged scalar's text, written twice in one value, means one value
|
|
33
|
+
* both times — false for a tag whose engine gives no such verdict.
|
|
34
|
+
*
|
|
35
|
+
* The single reader of `TemplatingEngine.repeatable`, so a consumer comparing
|
|
36
|
+
* two tagged scalars never recognises a tag by name.
|
|
37
|
+
*/
|
|
38
|
+
export declare function repeatableSource(engineName: string, source: string, celEnv: Environment): boolean;
|
|
30
39
|
/** Memoized singleton: returns the default registry. Hosts that don't need
|
|
31
40
|
* per-instance isolation (precompile, the analyzer's tagged-value walker)
|
|
32
41
|
* should use this so they share the same registry instance the YAML tag
|
package/dist/builtins.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builtins.d.ts","sourceRoot":"","sources":["../src/builtins.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"builtins.d.ts","sourceRoot":"","sources":["../src/builtins.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAQxD,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;;gEAKgE;AAChE,eAAO,MAAM,cAAc,EAAE,SAAS,gBAAgB,EASrD,CAAC;AAEF,wBAAgB,qBAAqB,IAAI,wBAAwB,CAMhE;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAEtF;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAG7E;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAEjG;AAID;;;oBAGoB;AACpB,wBAAgB,eAAe,IAAI,wBAAwB,CAK1D"}
|
package/dist/builtins.js
CHANGED
|
@@ -53,6 +53,16 @@ export function celExpressionsOf(engineName, source) {
|
|
|
53
53
|
const regions = defaultRegistry().get(engineName)?.expressionRegions?.(source) ?? [];
|
|
54
54
|
return regions.map((r) => source.slice(r.start, r.end));
|
|
55
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Whether a tagged scalar's text, written twice in one value, means one value
|
|
58
|
+
* both times — false for a tag whose engine gives no such verdict.
|
|
59
|
+
*
|
|
60
|
+
* The single reader of `TemplatingEngine.repeatable`, so a consumer comparing
|
|
61
|
+
* two tagged scalars never recognises a tag by name.
|
|
62
|
+
*/
|
|
63
|
+
export function repeatableSource(engineName, source, celEnv) {
|
|
64
|
+
return defaultRegistry().get(engineName)?.repeatable?.(source, celEnv) ?? false;
|
|
65
|
+
}
|
|
56
66
|
let defaultRegistryCache;
|
|
57
67
|
/** Memoized singleton: returns the default registry. Hosts that don't need
|
|
58
68
|
* per-instance isolation (precompile, the analyzer's tagged-value walker)
|
package/dist/cel/catalog.d.ts
CHANGED
|
@@ -14,6 +14,9 @@ export interface CelHandlers {
|
|
|
14
14
|
/** Join a relative path onto a base with the HOST's path rules. */
|
|
15
15
|
joinPath: (base: string, relative: string) => string;
|
|
16
16
|
}
|
|
17
|
+
/** RE2's parse-error kinds, in RE2's own words — the closed vocabulary an
|
|
18
|
+
* invalid-pattern refusal ends with, so every engine words one identically. */
|
|
19
|
+
export declare const RE2_PATTERN_ERROR_KINDS: readonly string[];
|
|
17
20
|
export type CelFunctionCategory = "conversion" | "time" | "uuid" | "string" | "math" | "collection" | "json" | "encoding" | "hashing" | "formatting" | "null";
|
|
18
21
|
/** One entry in the CEL standard library — the single source of truth that both
|
|
19
22
|
* registers the function (`build`) and documents it (everything else). `telo
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/cel/catalog.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/cel/catalog.ts"],"names":[],"mappings":"AAMA;;;0DAG0D;AAC1D,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IAC9B,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IAC3B,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IAC5B,MAAM,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IAC9B,IAAI,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IAClE,YAAY,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IACpC,YAAY,EAAE,CAAC,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC;IACpC,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,CAAC;IACjC,mEAAmE;IACnE,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC;CACtD;AA0BD;gFACgF;AAChF,eAAO,MAAM,uBAAuB,EAAE,SAAS,MAAM,EAepD,CAAC;AAuCF,MAAM,MAAM,mBAAmB,GAC3B,YAAY,GACZ,MAAM,GACN,MAAM,GACN,QAAQ,GACR,MAAM,GACN,YAAY,GACZ,MAAM,GACN,UAAU,GACV,SAAS,GACT,YAAY,GACZ,MAAM,CAAC;AAEX;;;qBAGqB;AACrB,MAAM,WAAW,cAAc;IAC7B,+CAA+C;IAC/C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;0EACsE;IACtE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B;;;;yDAIqD;IACrD,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,QAAQ,CAAC,QAAQ,EAAE,mBAAmB,CAAC;IACvC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB;wBACoB;IACpB,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAChC;2EACuE;IACvE,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,WAAW,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC;IAChE;;;;;;;;;;;;;;;;OAgBG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,KAAK,MAAM,GAAG,SAAS,CAAC;CAC3E;AA4CD,wEAAwE;AACxE,MAAM,MAAM,eAAe,GAAG,IAAI,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;AA8Z5D,eAAO,MAAM,aAAa,EAAE,SAAS,cAAc,EA+zBlD,CAAC;AAEF,oEAAoE;AACpE,wBAAgB,kBAAkB,IAAI,eAAe,EAAE,CAEtD"}
|
package/dist/cel/catalog.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { formatLocale } from "d3-format";
|
|
2
|
-
import { RE2JS } from "re2js";
|
|
2
|
+
import { RE2JS, RE2JSSyntaxException } from "re2js";
|
|
3
3
|
import { v1, v3, v4, v5, v6, v7, validate as uuidValidate, version as uuidVersion } from "uuid";
|
|
4
4
|
import { PLAIN_ENCODINGS } from "@telorun/sdk";
|
|
5
|
+
import { scanJsonPrefix } from "./json-prefix-scan.js";
|
|
5
6
|
/** RE2 regex engine for the CEL `regex*` functions — `re2js`, a pure-JS port of
|
|
6
7
|
* Google's RE2. The dialect is RE2 (linear-time, no backtracking, ReDoS-safe) —
|
|
7
8
|
* never JS `RegExp` — so a manifest's regex behaves the same across runtimes.
|
|
@@ -25,31 +26,93 @@ const RE2_FLAG = {
|
|
|
25
26
|
m: RE2JS.MULTILINE,
|
|
26
27
|
s: RE2JS.DOTALL,
|
|
27
28
|
};
|
|
28
|
-
|
|
29
|
+
/** RE2's parse-error kinds, in RE2's own words — the closed vocabulary an
|
|
30
|
+
* invalid-pattern refusal ends with, so every engine words one identically. */
|
|
31
|
+
export const RE2_PATTERN_ERROR_KINDS = [
|
|
32
|
+
"missing closing )",
|
|
33
|
+
"missing closing ]",
|
|
34
|
+
"unexpected )",
|
|
35
|
+
"trailing backslash at end of expression",
|
|
36
|
+
"invalid escape sequence",
|
|
37
|
+
"invalid character class range",
|
|
38
|
+
"invalid named capture",
|
|
39
|
+
"duplicate capture group name",
|
|
40
|
+
"invalid or unsupported Perl syntax",
|
|
41
|
+
"missing argument to repetition operator",
|
|
42
|
+
"invalid nested repetition operator",
|
|
43
|
+
"invalid repeat count",
|
|
44
|
+
"expression nests too deeply",
|
|
45
|
+
"expression too large",
|
|
46
|
+
];
|
|
47
|
+
/** A refusal a catalog function makes of its arguments — the one failure a
|
|
48
|
+
* literal guard reports. It reads as a plain `Error` wherever it is printed. */
|
|
49
|
+
class CatalogRefusal extends Error {
|
|
50
|
+
}
|
|
51
|
+
Object.defineProperty(CatalogRefusal, "name", { value: "Error" });
|
|
52
|
+
const re2Flags = (fn, flags) => {
|
|
29
53
|
let bits = 0;
|
|
30
54
|
for (const c of flags ?? "") {
|
|
31
55
|
if (c === "g")
|
|
32
56
|
continue; // global is implicit in replaceAll / find-loop
|
|
33
57
|
const bit = RE2_FLAG[c];
|
|
34
58
|
if (bit === undefined)
|
|
35
|
-
throw new
|
|
59
|
+
throw new CatalogRefusal(`${fn}: unknown regex flag '${c}' (supported: i, m, s)`);
|
|
36
60
|
bits |= bit;
|
|
37
61
|
}
|
|
62
|
+
return bits;
|
|
63
|
+
};
|
|
64
|
+
/** The refusal is Telo's: the library's `error parsing regexp:` prefix and the
|
|
65
|
+
* fragment it quotes never reach it. A failure outside the vocabulary is a
|
|
66
|
+
* defect here, raised as one with the library's error as its cause. */
|
|
67
|
+
const re2Pattern = (fn, pattern, bits) => {
|
|
38
68
|
try {
|
|
39
69
|
return RE2JS.compile(pattern, bits);
|
|
40
70
|
}
|
|
41
71
|
catch (e) {
|
|
42
|
-
|
|
72
|
+
const kind = e instanceof RE2JSSyntaxException ? e.error : undefined;
|
|
73
|
+
if (kind === undefined || !RE2_PATTERN_ERROR_KINDS.includes(kind)) {
|
|
74
|
+
throw new Error(`${fn}: the RE2 compiler failed on pattern ${JSON.stringify(pattern)} with an error outside Telo's parse-error vocabulary`, { cause: e });
|
|
75
|
+
}
|
|
76
|
+
throw new CatalogRefusal(`${fn}: invalid RE2 pattern ${JSON.stringify(pattern)}: ${kind}`);
|
|
43
77
|
}
|
|
44
78
|
};
|
|
45
|
-
|
|
79
|
+
const compileRe2 = (fn, pattern, flags) => re2Pattern(fn, pattern, re2Flags(fn, flags));
|
|
80
|
+
/** Run a runtime guard for its refusal, so a `checkArgs` never restates one.
|
|
81
|
+
* Anything else it throws is the engine failing, not the argument. */
|
|
46
82
|
const literalGuard = (run) => {
|
|
47
83
|
try {
|
|
48
84
|
run();
|
|
49
85
|
return undefined;
|
|
50
86
|
}
|
|
51
87
|
catch (e) {
|
|
52
|
-
|
|
88
|
+
if (e instanceof CatalogRefusal)
|
|
89
|
+
return e.message;
|
|
90
|
+
throw e;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/** A regex function's literal guard: the flags and the pattern, each judged
|
|
94
|
+
* only when written as a literal, by the compile the runtime runs. */
|
|
95
|
+
const regexLiteralGuard = (fn, patternAt, flagsAt) => (literals) => literalGuard(() => {
|
|
96
|
+
const pattern = literals[patternAt];
|
|
97
|
+
const flags = literals[flagsAt];
|
|
98
|
+
const bits = typeof flags === "string" ? re2Flags(fn, flags) : 0;
|
|
99
|
+
if (typeof pattern === "string")
|
|
100
|
+
re2Pattern(fn, pattern, bits);
|
|
101
|
+
});
|
|
102
|
+
/** The host parser's verdict stands; Telo's scan only words its refusal. The
|
|
103
|
+
* scan reading as JSON a text the host refused is a defect here. */
|
|
104
|
+
const parseJsonText = (text) => {
|
|
105
|
+
try {
|
|
106
|
+
return JSON.parse(text);
|
|
107
|
+
}
|
|
108
|
+
catch (hostRefusal) {
|
|
109
|
+
const refusal = scanJsonPrefix(text);
|
|
110
|
+
if (refusal === undefined) {
|
|
111
|
+
throw new Error("parseJson: the host JSON parser refused a text Telo's scan reads as valid JSON", {
|
|
112
|
+
cause: hostRefusal,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
throw new Error(`parseJson: invalid JSON at offset ${refusal.offset}${refusal.endOfInput ? " (unexpected end of input)" : ""}`);
|
|
53
116
|
}
|
|
54
117
|
};
|
|
55
118
|
const num = (x) => Number(x);
|
|
@@ -102,7 +165,7 @@ const MAX_EXACT_INT = 9007199254740991n;
|
|
|
102
165
|
const formattable = (fn, x) => {
|
|
103
166
|
if (typeof x === "bigint") {
|
|
104
167
|
if (x > MAX_EXACT_INT || x < -MAX_EXACT_INT) {
|
|
105
|
-
throw new
|
|
168
|
+
throw new CatalogRefusal(`${fn}: integer ${x} exceeds 2^53-1 and cannot be formatted exactly as a double`);
|
|
106
169
|
}
|
|
107
170
|
return Number(x);
|
|
108
171
|
}
|
|
@@ -112,7 +175,7 @@ const formattable = (fn, x) => {
|
|
|
112
175
|
// to remove: it looks like an answer and prints into a document. Named here
|
|
113
176
|
// rather than coerced, the way an instant argument is.
|
|
114
177
|
if (!Number.isFinite(n)) {
|
|
115
|
-
throw new
|
|
178
|
+
throw new CatalogRefusal(`${fn}: expected a finite number, got ${JSON.stringify(x)}`);
|
|
116
179
|
}
|
|
117
180
|
return n;
|
|
118
181
|
};
|
|
@@ -136,7 +199,7 @@ const formatter = (fn, spec) => {
|
|
|
136
199
|
return cached;
|
|
137
200
|
const type = text.slice(-1);
|
|
138
201
|
if (text !== "" && /[a-zA-Z%]/.test(type) && !FORMAT_TYPES.has(type)) {
|
|
139
|
-
throw new
|
|
202
|
+
throw new CatalogRefusal(`${fn}: unknown format type '${type}' (one of ${[...FORMAT_TYPES].join("")})`);
|
|
140
203
|
}
|
|
141
204
|
// The `.precision` group — width is the digits BEFORE the dot, so this is the
|
|
142
205
|
// only `.`-digits sequence the grammar admits.
|
|
@@ -148,7 +211,7 @@ const formatter = (fn, spec) => {
|
|
|
148
211
|
built = FORMAT_LOCALE.format(text);
|
|
149
212
|
}
|
|
150
213
|
catch {
|
|
151
|
-
throw new
|
|
214
|
+
throw new CatalogRefusal(`${fn}: invalid format specifier ${JSON.stringify(text)}`);
|
|
152
215
|
}
|
|
153
216
|
if (formatterCache.size >= FORMATTER_CACHE_MAX)
|
|
154
217
|
formatterCache.clear();
|
|
@@ -169,7 +232,7 @@ const MAX_DECIMALS = 10;
|
|
|
169
232
|
const digitCount = (fn, digits) => {
|
|
170
233
|
const n = Number(digits);
|
|
171
234
|
if (!Number.isInteger(n) || n < 0 || n > MAX_DECIMALS) {
|
|
172
|
-
throw new
|
|
235
|
+
throw new CatalogRefusal(`${fn}: decimal places must be an integer 0-${MAX_DECIMALS}, got ${String(digits)}`);
|
|
173
236
|
}
|
|
174
237
|
return n;
|
|
175
238
|
};
|
|
@@ -178,11 +241,11 @@ const digitCount = (fn, digits) => {
|
|
|
178
241
|
const durationText = (minutes, minutesPerDay) => {
|
|
179
242
|
const perDay = Math.round(formattable("formatDuration", minutesPerDay));
|
|
180
243
|
if (!Number.isFinite(perDay) || perDay <= 0) {
|
|
181
|
-
throw new
|
|
244
|
+
throw new CatalogRefusal(`formatDuration: minutesPerDay must be a positive number, got ${perDay}`);
|
|
182
245
|
}
|
|
183
246
|
const total = Math.round(formattable("formatDuration", minutes));
|
|
184
247
|
if (!Number.isFinite(total)) {
|
|
185
|
-
throw new
|
|
248
|
+
throw new CatalogRefusal(`formatDuration: minutes must be a finite number`);
|
|
186
249
|
}
|
|
187
250
|
const magnitude = Math.abs(total);
|
|
188
251
|
const days = Math.floor(magnitude / perDay);
|
|
@@ -211,7 +274,7 @@ const assertZone = (fn, tz) => {
|
|
|
211
274
|
new Intl.DateTimeFormat("en-US", { timeZone: tz });
|
|
212
275
|
}
|
|
213
276
|
catch {
|
|
214
|
-
throw new
|
|
277
|
+
throw new CatalogRefusal(`${fn}: unknown IANA time zone ${JSON.stringify(tz)}`);
|
|
215
278
|
}
|
|
216
279
|
return tz;
|
|
217
280
|
};
|
|
@@ -573,6 +636,7 @@ export const CEL_FUNCTIONS = [
|
|
|
573
636
|
summary: "Replace every regex match (RE2 syntax) with a replacement ($1 backrefs); flags like 'i', 'm', 's'.",
|
|
574
637
|
deterministic: true,
|
|
575
638
|
hostBacked: false,
|
|
639
|
+
checkArgs: regexLiteralGuard("regexReplace", 1, 3),
|
|
576
640
|
build: () => (s, pattern, replacement, flags) => compileRe2("regexReplace", pattern, flags).matcher(s).replaceAll(replacement),
|
|
577
641
|
},
|
|
578
642
|
{
|
|
@@ -582,6 +646,7 @@ export const CEL_FUNCTIONS = [
|
|
|
582
646
|
summary: "First whole match of a regex (RE2 syntax), or '' when there is none.",
|
|
583
647
|
deterministic: true,
|
|
584
648
|
hostBacked: false,
|
|
649
|
+
checkArgs: regexLiteralGuard("regexExtract", 1, 2),
|
|
585
650
|
build: () => (s, pattern, flags) => {
|
|
586
651
|
const m = compileRe2("regexExtract", pattern, flags).matcher(s);
|
|
587
652
|
return m.find() ? (m.group() ?? "") : "";
|
|
@@ -594,6 +659,7 @@ export const CEL_FUNCTIONS = [
|
|
|
594
659
|
summary: "Every whole match of a regex (RE2 syntax), in order.",
|
|
595
660
|
deterministic: true,
|
|
596
661
|
hostBacked: false,
|
|
662
|
+
checkArgs: regexLiteralGuard("regexExtractAll", 1, 2),
|
|
597
663
|
build: () => (s, pattern, flags) => {
|
|
598
664
|
const m = compileRe2("regexExtractAll", pattern, flags).matcher(s);
|
|
599
665
|
const out = [];
|
|
@@ -609,6 +675,7 @@ export const CEL_FUNCTIONS = [
|
|
|
609
675
|
summary: "Capture groups of the first regex match (RE2 syntax); empty list when there is no match.",
|
|
610
676
|
deterministic: true,
|
|
611
677
|
hostBacked: false,
|
|
678
|
+
checkArgs: regexLiteralGuard("regexGroups", 1, 2),
|
|
612
679
|
build: () => (s, pattern, flags) => {
|
|
613
680
|
const m = compileRe2("regexGroups", pattern, flags).matcher(s);
|
|
614
681
|
if (!m.find())
|
|
@@ -783,7 +850,7 @@ export const CEL_FUNCTIONS = [
|
|
|
783
850
|
summary: "Parse a JSON string into a value (numbers come back as doubles).",
|
|
784
851
|
deterministic: true,
|
|
785
852
|
hostBacked: false,
|
|
786
|
-
build: () => (s) =>
|
|
853
|
+
build: () => (s) => parseJsonText(s),
|
|
787
854
|
},
|
|
788
855
|
// Paths. The one way to extend a host path: `variables.dataDir.joinPath('reports')`
|
|
789
856
|
// keeps it a `Telo.HostPath` (the analyzer types that overload), where `+`
|
package/dist/cel/compile.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { CompiledValue } from "@telorun/sdk";
|
|
2
|
-
import type
|
|
2
|
+
import { type Environment } from "@marcbachmann/cel-js";
|
|
3
|
+
/**
|
|
4
|
+
* Whether an expression's text, written twice in one value, means one value
|
|
5
|
+
* both times: it calls no catalog function whose result differs per call, and
|
|
6
|
+
* makes no call that may be a module's — a module function's determinism is its
|
|
7
|
+
* callee's, which the text does not say. Text that does not parse means no
|
|
8
|
+
* value, so it is not repeatable; `analyze` is what reports why.
|
|
9
|
+
*/
|
|
10
|
+
export declare function repeatableExpression(expr: string, env: Environment): boolean;
|
|
3
11
|
/** Compile a single CEL expression into a CompiledValue. Throws on syntax
|
|
4
12
|
* errors. The `!cel` engine compiles its whole scalar with it, and the tags
|
|
5
13
|
* with holes compile each hole.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/cel/compile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/cel/compile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAA4B,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAyDlF;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,WAAW,GAAG,OAAO,CAS5E;AAED;;;;;;;;+BAQ+B;AAC/B,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,WAAW,EAChB,WAAW,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAChC,aAAa,CAYf"}
|
package/dist/cel/compile.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ParseError } from "@marcbachmann/cel-js";
|
|
1
2
|
import { extractAccessChains } from "./analyze.js";
|
|
2
3
|
import { CEL_FUNCTIONS } from "./catalog.js";
|
|
3
4
|
import { resolveModuleCalls } from "./module-call.js";
|
|
@@ -34,6 +35,45 @@ function callsNonDeterministic(root) {
|
|
|
34
35
|
};
|
|
35
36
|
return visit(root);
|
|
36
37
|
}
|
|
38
|
+
/** True when the expression calls a method on a bare identifier (`Billing.f(x)`).
|
|
39
|
+
* Whether that identifier names a module is the declaring module's to say, and
|
|
40
|
+
* the text alone does not, so every such call may be a module call. */
|
|
41
|
+
function callsOnBareReceiver(root) {
|
|
42
|
+
const visit = (node) => {
|
|
43
|
+
if (Array.isArray(node))
|
|
44
|
+
return node.some(visit);
|
|
45
|
+
if (!node || typeof node !== "object" || !("op" in node))
|
|
46
|
+
return false;
|
|
47
|
+
const n = node;
|
|
48
|
+
if (n.op === "rcall" && Array.isArray(n.args)) {
|
|
49
|
+
const receiver = n.args[1];
|
|
50
|
+
if (receiver && typeof receiver === "object" && receiver.op === "id")
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
const args = n.args;
|
|
54
|
+
return Array.isArray(args) ? args.some(visit) : visit(args);
|
|
55
|
+
};
|
|
56
|
+
return visit(root);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Whether an expression's text, written twice in one value, means one value
|
|
60
|
+
* both times: it calls no catalog function whose result differs per call, and
|
|
61
|
+
* makes no call that may be a module's — a module function's determinism is its
|
|
62
|
+
* callee's, which the text does not say. Text that does not parse means no
|
|
63
|
+
* value, so it is not repeatable; `analyze` is what reports why.
|
|
64
|
+
*/
|
|
65
|
+
export function repeatableExpression(expr, env) {
|
|
66
|
+
let ast;
|
|
67
|
+
try {
|
|
68
|
+
ast = env.parse(expr).ast;
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof ParseError)
|
|
72
|
+
return false;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
return !callsNonDeterministic(ast) && !callsOnBareReceiver(ast);
|
|
76
|
+
}
|
|
37
77
|
/** Compile a single CEL expression into a CompiledValue. Throws on syntax
|
|
38
78
|
* errors. The `!cel` engine compiles its whole scalar with it, and the tags
|
|
39
79
|
* with holes compile each hole.
|
package/dist/cel/diagnose.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ASTNode, Environment } from "@marcbachmann/cel-js";
|
|
2
2
|
import type { CallSite, EngineDiagnostic } from "../engine.js";
|
|
3
|
+
import type { CelCallAudit } from "./verdict-codes.js";
|
|
3
4
|
/** Classifies every function call in a CEL expression against the environment's
|
|
4
5
|
* own function registry.
|
|
5
6
|
*
|
|
@@ -55,7 +56,7 @@ export interface CallAudit {
|
|
|
55
56
|
export declare function auditCalls(source: string, ast: ASTNode, env: Environment,
|
|
56
57
|
/** The flags of the module functions the expression calls, from whoever
|
|
57
58
|
* resolves them. */
|
|
58
|
-
moduleCallFlags?: ModuleCallFlags):
|
|
59
|
+
moduleCallFlags?: ModuleCallFlags): CelCallAudit;
|
|
59
60
|
/** Registered signatures for names a type-check failure mentions, so the
|
|
60
61
|
* residual says what the function actually accepts rather than only echoing
|
|
61
62
|
* what the author wrote. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"diagnose.d.ts","sourceRoot":"","sources":["../../src/cel/diagnose.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAGjE,OAAO,KAAK,EAAE,QAAQ,EAAiB,gBAAgB,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"diagnose.d.ts","sourceRoot":"","sources":["../../src/cel/diagnose.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAGjE,OAAO,KAAK,EAAE,QAAQ,EAAiB,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAiB,MAAM,oBAAoB,CAAC;AAEtE;;;;;;;;;;;;;;;4DAe4D;AAE5D,sEAAsE;AACtE,UAAU,OAAO;IACf,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,QAAQ,GAAG,UAAU,CAAC;IACrC,mEAAmE;IACnE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,OAAO,EAAE,CAAC,CAAC;CAC1D;AAYD;8DAC8D;AAC9D,MAAM,MAAM,eAAe,GAAG,CAC5B,SAAS,EAAE,MAAM,KACd;IAAE,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAA;CAAE,GAAG,SAAS,CAAC;AAInF;uDACuD;AACvD,wBAAgB,aAAa,CAAC,GAAG,EAAE,WAAW,GAAG,aAAa,CAiB7D;AAyMD,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,WAAW,EAAE,SAAS,gBAAgB,EAAE,CAAC;IAClD,QAAQ,CAAC,KAAK,EAAE,SAAS,QAAQ,EAAE,CAAC;IACpC;;;2CAGuC;IACvC,QAAQ,CAAC,cAAc,EAAE,SAAS,gBAAgB,EAAE,CAAC;IACrD;;6BAEyB;IACzB,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;CACxC;AAED;;;yBAGyB;AACzB,wBAAgB,UAAU,CACxB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,WAAW;AAChB;qBACqB;AACrB,eAAe,CAAC,EAAE,eAAe,GAChC,YAAY,CA8Ed;AAED;;6BAE6B;AAC7B,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,EAAE,GAAG,EAAE,WAAW,GAAG,MAAM,CAIpF"}
|
|
@@ -31,5 +31,9 @@ export declare function deriveSignatures(signature: string): string[];
|
|
|
31
31
|
* registered one differ for a third of the entries. Asking for the base set
|
|
32
32
|
* directly needs no matching at all. */
|
|
33
33
|
export declare function celBuiltinFunctions(): ReturnType<Environment["getDefinitions"]>["functions"];
|
|
34
|
+
/** The bare CEL language under Telo's options: cel-js's built-ins and nothing
|
|
35
|
+
* else — no catalog, no `Stream`. The dialect is built on it, and the
|
|
36
|
+
* language conformance vectors (`templating/cel-conformance/`) run against it. */
|
|
37
|
+
export declare function buildCelLanguageEnvironment(): Environment;
|
|
34
38
|
export declare function buildCelEnvironment(handlers?: Partial<CelHandlers>): Environment;
|
|
35
39
|
//# sourceMappingURL=environment.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../src/cel/environment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,OAAO,EAAiB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAG/D,YAAY,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAuBhD;;;;;;;;;;;;;;;yDAeyD;AACzD;;;;;wBAKwB;AACxB,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAyB5D;AAYD;;;;;yCAKyC;AACzC,wBAAgB,mBAAmB,IAAI,UAAU,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC,CAAC,WAAW,CAAC,CAE5F;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,GAAE,OAAO,CAAC,WAAW,CAAM,GAAG,WAAW,CAcpF"}
|
|
1
|
+
{"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../src/cel/environment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAEnD,OAAO,EAAiB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAG/D,YAAY,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAuBhD;;;;;;;;;;;;;;;yDAeyD;AACzD;;;;;wBAKwB;AACxB,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAyB5D;AAYD;;;;;yCAKyC;AACzC,wBAAgB,mBAAmB,IAAI,UAAU,CAAC,WAAW,CAAC,gBAAgB,CAAC,CAAC,CAAC,WAAW,CAAC,CAE5F;AAED;;mFAEmF;AACnF,wBAAgB,2BAA2B,IAAI,WAAW,CAEzD;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,GAAE,OAAO,CAAC,WAAW,CAAM,GAAG,WAAW,CAcpF"}
|
package/dist/cel/environment.js
CHANGED
|
@@ -85,11 +85,17 @@ const ENVIRONMENT_OPTIONS = {
|
|
|
85
85
|
* registered one differ for a third of the entries. Asking for the base set
|
|
86
86
|
* directly needs no matching at all. */
|
|
87
87
|
export function celBuiltinFunctions() {
|
|
88
|
-
return
|
|
88
|
+
return buildCelLanguageEnvironment().getDefinitions().functions;
|
|
89
|
+
}
|
|
90
|
+
/** The bare CEL language under Telo's options: cel-js's built-ins and nothing
|
|
91
|
+
* else — no catalog, no `Stream`. The dialect is built on it, and the
|
|
92
|
+
* language conformance vectors (`templating/cel-conformance/`) run against it. */
|
|
93
|
+
export function buildCelLanguageEnvironment() {
|
|
94
|
+
return new Environment(ENVIRONMENT_OPTIONS);
|
|
89
95
|
}
|
|
90
96
|
export function buildCelEnvironment(handlers = {}) {
|
|
91
97
|
const h = { ...STUB_HANDLERS, ...handlers };
|
|
92
|
-
let env =
|
|
98
|
+
let env = buildCelLanguageEnvironment();
|
|
93
99
|
for (const fn of CEL_FUNCTIONS) {
|
|
94
100
|
const impl = fn.build(h);
|
|
95
101
|
// `register` lists one cel-js signature per arity (overloaded functions).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a text stops being a prefix of any RFC 8259 JSON text — the offset
|
|
3
|
+
* `parseJson`'s refusal names, computed here so the wording never comes from the
|
|
4
|
+
* host's JSON parser.
|
|
5
|
+
*/
|
|
6
|
+
export interface JsonRefusal {
|
|
7
|
+
/** UTF-16 offset of the first offending code unit; the text's length when
|
|
8
|
+
* every prefix is viable and the text is merely incomplete. */
|
|
9
|
+
readonly offset: number;
|
|
10
|
+
readonly endOfInput: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** The refusal of `text`, or `undefined` when it is a JSON text. Iterative, so
|
|
13
|
+
* nesting depth is bounded by memory rather than by the call stack. Never
|
|
14
|
+
* stricter or laxer than RFC 8259: over 400,000 fuzzed texts it agreed with
|
|
15
|
+
* V8's `JSON.parse` on which are JSON every time. */
|
|
16
|
+
export declare function scanJsonPrefix(text: string): JsonRefusal | undefined;
|
|
17
|
+
//# sourceMappingURL=json-prefix-scan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-prefix-scan.d.ts","sourceRoot":"","sources":["../../src/cel/json-prefix-scan.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B;oEACgE;IAChE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;CAC9B;AAYD;;;sDAGsD;AACtD,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CA6HpE"}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
const QUOTE = 0x22;
|
|
2
|
+
const BACKSLASH = 0x5c;
|
|
3
|
+
const SIMPLE_ESCAPES = '"\\/bfnrt';
|
|
4
|
+
const isWhitespace = (c) => c === " " || c === "\t" || c === "\n" || c === "\r";
|
|
5
|
+
const isDigit = (c) => c !== undefined && c >= "0" && c <= "9";
|
|
6
|
+
const isHexDigit = (c) => isDigit(c) || (c >= "a" && c <= "f") || (c >= "A" && c <= "F");
|
|
7
|
+
/** The refusal of `text`, or `undefined` when it is a JSON text. Iterative, so
|
|
8
|
+
* nesting depth is bounded by memory rather than by the call stack. Never
|
|
9
|
+
* stricter or laxer than RFC 8259: over 400,000 fuzzed texts it agreed with
|
|
10
|
+
* V8's `JSON.parse` on which are JSON every time. */
|
|
11
|
+
export function scanJsonPrefix(text) {
|
|
12
|
+
const length = text.length;
|
|
13
|
+
let at = 0;
|
|
14
|
+
// Each token reader returns the offset it refuses, or `undefined` once `at`
|
|
15
|
+
// is past the token.
|
|
16
|
+
const readString = () => {
|
|
17
|
+
at++;
|
|
18
|
+
for (;;) {
|
|
19
|
+
if (at >= length)
|
|
20
|
+
return length;
|
|
21
|
+
const code = text.charCodeAt(at);
|
|
22
|
+
if (code === QUOTE) {
|
|
23
|
+
at++;
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
if (code < 0x20)
|
|
27
|
+
return at;
|
|
28
|
+
at++;
|
|
29
|
+
if (code !== BACKSLASH)
|
|
30
|
+
continue;
|
|
31
|
+
if (at >= length)
|
|
32
|
+
return length;
|
|
33
|
+
const escape = text[at];
|
|
34
|
+
if (escape === "u") {
|
|
35
|
+
at++;
|
|
36
|
+
for (let digit = 0; digit < 4; digit++, at++) {
|
|
37
|
+
if (at >= length)
|
|
38
|
+
return length;
|
|
39
|
+
if (!isHexDigit(text[at]))
|
|
40
|
+
return at;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else if (SIMPLE_ESCAPES.includes(escape))
|
|
44
|
+
at++;
|
|
45
|
+
else
|
|
46
|
+
return at;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const readDigits = () => {
|
|
50
|
+
if (!isDigit(text[at]))
|
|
51
|
+
return at;
|
|
52
|
+
while (isDigit(text[at]))
|
|
53
|
+
at++;
|
|
54
|
+
return undefined;
|
|
55
|
+
};
|
|
56
|
+
const readNumber = () => {
|
|
57
|
+
if (text[at] === "-")
|
|
58
|
+
at++;
|
|
59
|
+
if (text[at] === "0")
|
|
60
|
+
at++;
|
|
61
|
+
else {
|
|
62
|
+
const refused = readDigits();
|
|
63
|
+
if (refused !== undefined)
|
|
64
|
+
return refused;
|
|
65
|
+
}
|
|
66
|
+
if (text[at] === ".") {
|
|
67
|
+
at++;
|
|
68
|
+
const refused = readDigits();
|
|
69
|
+
if (refused !== undefined)
|
|
70
|
+
return refused;
|
|
71
|
+
}
|
|
72
|
+
if (text[at] === "e" || text[at] === "E") {
|
|
73
|
+
at++;
|
|
74
|
+
if (text[at] === "+" || text[at] === "-")
|
|
75
|
+
at++;
|
|
76
|
+
return readDigits();
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
};
|
|
80
|
+
const readLiteral = (literal) => {
|
|
81
|
+
for (const expected of literal) {
|
|
82
|
+
if (text[at] !== expected)
|
|
83
|
+
return at;
|
|
84
|
+
at++;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
};
|
|
88
|
+
const refusedAt = (offset) => ({ offset, endOfInput: offset >= length });
|
|
89
|
+
const open = [];
|
|
90
|
+
let state = "value";
|
|
91
|
+
for (;;) {
|
|
92
|
+
while (isWhitespace(text[at]))
|
|
93
|
+
at++;
|
|
94
|
+
const c = text[at];
|
|
95
|
+
if (state === "after") {
|
|
96
|
+
const container = open[open.length - 1];
|
|
97
|
+
if (container === undefined)
|
|
98
|
+
return at === length ? undefined : refusedAt(at);
|
|
99
|
+
if (c === ",") {
|
|
100
|
+
at++;
|
|
101
|
+
state = container === "{" ? "key" : "value";
|
|
102
|
+
}
|
|
103
|
+
else if (c === (container === "{" ? "}" : "]")) {
|
|
104
|
+
at++;
|
|
105
|
+
open.pop();
|
|
106
|
+
}
|
|
107
|
+
else
|
|
108
|
+
return refusedAt(at);
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (state === "key" || state === "keyOrClose") {
|
|
112
|
+
if (state === "keyOrClose" && c === "}") {
|
|
113
|
+
at++;
|
|
114
|
+
open.pop();
|
|
115
|
+
state = "after";
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (c !== '"')
|
|
119
|
+
return refusedAt(at);
|
|
120
|
+
const refused = readString();
|
|
121
|
+
if (refused !== undefined)
|
|
122
|
+
return refusedAt(refused);
|
|
123
|
+
while (isWhitespace(text[at]))
|
|
124
|
+
at++;
|
|
125
|
+
if (text[at] !== ":")
|
|
126
|
+
return refusedAt(at);
|
|
127
|
+
at++;
|
|
128
|
+
state = "value";
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (state === "valueOrClose" && c === "]") {
|
|
132
|
+
at++;
|
|
133
|
+
open.pop();
|
|
134
|
+
state = "after";
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (c === "{" || c === "[") {
|
|
138
|
+
at++;
|
|
139
|
+
open.push(c);
|
|
140
|
+
state = c === "{" ? "keyOrClose" : "valueOrClose";
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
let refused;
|
|
144
|
+
if (c === '"')
|
|
145
|
+
refused = readString();
|
|
146
|
+
else if (c === "-" || isDigit(c))
|
|
147
|
+
refused = readNumber();
|
|
148
|
+
else if (c === "t")
|
|
149
|
+
refused = readLiteral("true");
|
|
150
|
+
else if (c === "f")
|
|
151
|
+
refused = readLiteral("false");
|
|
152
|
+
else if (c === "n")
|
|
153
|
+
refused = readLiteral("null");
|
|
154
|
+
else
|
|
155
|
+
refused = at;
|
|
156
|
+
if (refused !== undefined)
|
|
157
|
+
return refusedAt(refused);
|
|
158
|
+
state = "after";
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Environment } from "@marcbachmann/cel-js";
|
|
2
|
+
/**
|
|
3
|
+
* Register every nominal value brand on an analysis environment, with what a
|
|
4
|
+
* brand may do: convert to its base with the base's own conversion
|
|
5
|
+
* (`int(ports.http)`, `string(variables.dataDir)`), render as text
|
|
6
|
+
* (`string(ports.http)`), and — for a host path — be
|
|
7
|
+
* extended with `.joinPath(relative)` and stay one.
|
|
8
|
+
*
|
|
9
|
+
* Static only: at runtime a branded value IS its base (an int, a string), so the
|
|
10
|
+
* catalog's own overloads — `string.joinPath` among them — are what evaluate. The
|
|
11
|
+
* implementations below exist because registration requires one; a brand's
|
|
12
|
+
* values never reach them. `buildCelEnvironment` registers none of this: a
|
|
13
|
+
* caller typing a site registers the brands on its own clone.
|
|
14
|
+
*/
|
|
15
|
+
export declare function registerValueBrands(env: Environment): void;
|
|
16
|
+
//# sourceMappingURL=value-brands.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"value-brands.d.ts","sourceRoot":"","sources":["../../src/cel/value-brands.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAGxD;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,WAAW,GAAG,IAAI,CAgB1D"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { VALUE_TYPES, valueBrandBases } from "@telorun/sdk";
|
|
2
|
+
/**
|
|
3
|
+
* Register every nominal value brand on an analysis environment, with what a
|
|
4
|
+
* brand may do: convert to its base with the base's own conversion
|
|
5
|
+
* (`int(ports.http)`, `string(variables.dataDir)`), render as text
|
|
6
|
+
* (`string(ports.http)`), and — for a host path — be
|
|
7
|
+
* extended with `.joinPath(relative)` and stay one.
|
|
8
|
+
*
|
|
9
|
+
* Static only: at runtime a branded value IS its base (an int, a string), so the
|
|
10
|
+
* catalog's own overloads — `string.joinPath` among them — are what evaluate. The
|
|
11
|
+
* implementations below exist because registration requires one; a brand's
|
|
12
|
+
* values never reach them. `buildCelEnvironment` registers none of this: a
|
|
13
|
+
* caller typing a site registers the brands on its own clone.
|
|
14
|
+
*/
|
|
15
|
+
export function registerValueBrands(env) {
|
|
16
|
+
for (const [brand, base] of Object.entries(valueBrandBases())) {
|
|
17
|
+
env.registerType(brand, { fields: {} });
|
|
18
|
+
env.registerFunction(`${base}(${brand}): ${base}`, (value) => value);
|
|
19
|
+
// Every base CEL can render as text renders its brand too — the runtime
|
|
20
|
+
// value IS the base — so `string(ports.http)` and a port in an
|
|
21
|
+
// `!interpolate` hole convert as the integer they are.
|
|
22
|
+
if (base !== "string") {
|
|
23
|
+
env.registerFunction(`string(${brand}): string`, (value) => String(value));
|
|
24
|
+
}
|
|
25
|
+
if (VALUE_TYPES.get(brand)?.fromHost !== undefined) {
|
|
26
|
+
env.registerFunction(`${brand}.joinPath(string): ${brand}`, () => {
|
|
27
|
+
throw new Error("joinPath() is evaluated by the runtime, not the analyzer.");
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|