@telorun/templating 0.21.0 → 0.21.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.
@@ -1 +1 @@
1
- {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/cel/compile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,KAAK,EAAW,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAIjE,eAAO,MAAM,cAAc,QAA8B,CAAC;AAC1D,eAAO,MAAM,oBAAoB,QAAqC,CAAC;AAcvE;;;;;;;;+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,CAWf;AAED;;;;+DAI+D;AAC/D,wBAAgB,aAAa,CAC3B,CAAC,EAAE,MAAM,EACT,GAAG,EAAE,WAAW,EAChB,WAAW,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAChC,OAAO,CAkCT;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC3B;IAAE,SAAS,EAAE,MAAM,EAAE,CAAC;IAAC,MAAM,EAAE,OAAO,EAAE,CAAA;CAAE,CAsB5C"}
1
+ {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/cel/compile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,KAAK,EAAW,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAKjE,eAAO,MAAM,cAAc,QAA8B,CAAC;AAC1D,eAAO,MAAM,oBAAoB,QAAqC,CAAC;AAoCvE;;;;;;;;+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;AAED;;;;+DAI+D;AAC/D,wBAAgB,aAAa,CAC3B,CAAC,EAAE,MAAM,EACT,GAAG,EAAE,WAAW,EAChB,WAAW,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAChC,OAAO,CAqCT;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC3B;IAAE,SAAS,EAAE,MAAM,EAAE,CAAC;IAAC,MAAM,EAAE,OAAO,EAAE,CAAA;CAAE,CAsB5C"}
@@ -1,5 +1,6 @@
1
1
  import { isCompiledValue } from "@telorun/sdk";
2
2
  import { extractAccessChains } from "./analyze.js";
3
+ import { CEL_FUNCTIONS } from "./catalog.js";
3
4
  import { resolveModuleCalls } from "./module-call.js";
4
5
  export const TEMPLATE_REGEX = /\$\{\{\s*([^}]+?)\s*\}\}/g;
5
6
  export const EXACT_TEMPLATE_REGEX = /^\s*\$\{\{\s*([^}]+?)\s*\}\}\s*$/;
@@ -15,6 +16,27 @@ function rootRefs(ast) {
15
16
  }
16
17
  return [...roots];
17
18
  }
19
+ const NON_DETERMINISTIC = new Set(CEL_FUNCTIONS.filter((f) => !f.deterministic).map((f) => f.name));
20
+ /** True when the expression calls a catalog function whose result differs per
21
+ * call (`uuidv4()`, `nowMillis()`), so evaluating it once and reusing the value
22
+ * would change what it means. */
23
+ function callsNonDeterministic(root) {
24
+ const visit = (node) => {
25
+ if (Array.isArray(node))
26
+ return node.some(visit);
27
+ if (!node || typeof node !== "object" || !("op" in node))
28
+ return false;
29
+ const n = node;
30
+ if ((n.op === "call" || n.op === "rcall") && Array.isArray(n.args)) {
31
+ const name = n.args[0];
32
+ if (typeof name === "string" && NON_DETERMINISTIC.has(name))
33
+ return true;
34
+ }
35
+ const args = n.args;
36
+ return Array.isArray(args) ? args.some(visit) : visit(args);
37
+ };
38
+ return visit(root);
39
+ }
18
40
  /** Compile a single CEL expression (no `${{ }}` wrapping) into a CompiledValue.
19
41
  * Throws on syntax errors. Used by the `!cel` engine where the entire tagged
20
42
  * scalar is treated as one expression.
@@ -33,6 +55,7 @@ export function compileExpression(expr, env, moduleNames) {
33
55
  source: expr,
34
56
  refs: rootRefs(ast),
35
57
  calls,
58
+ ...(callsNonDeterministic(ast) ? { volatile: true } : {}),
36
59
  call: (ctx) => fn(ctx),
37
60
  };
38
61
  }
@@ -60,6 +83,7 @@ export function compileString(s, env, moduleNames) {
60
83
  parts.push(s.slice(last));
61
84
  const refs = new Set();
62
85
  const calls = new Set();
86
+ let volatile = false;
63
87
  for (const p of parts) {
64
88
  if (typeof p === "string")
65
89
  continue;
@@ -69,6 +93,8 @@ export function compileString(s, env, moduleNames) {
69
93
  if (p.calls)
70
94
  for (const c of p.calls)
71
95
  calls.add(c);
96
+ if (p.volatile)
97
+ volatile = true;
72
98
  }
73
99
  return {
74
100
  __compiled: true,
@@ -76,6 +102,7 @@ export function compileString(s, env, moduleNames) {
76
102
  parts,
77
103
  refs: [...refs],
78
104
  calls: [...calls],
105
+ ...(volatile ? { volatile: true } : {}),
79
106
  call: (ctx) => parts.map((p) => (typeof p === "string" ? p : String(p.call(ctx) ?? ""))).join(""),
80
107
  };
81
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@telorun/templating",
3
- "version": "0.21.0",
3
+ "version": "0.21.1",
4
4
  "description": "Telo Templating - Engine registry and shared CEL core for Telo manifests.",
5
5
  "keywords": [
6
6
  "telo",
@@ -46,7 +46,7 @@
46
46
  "@types/node": "^20.0.0",
47
47
  "typescript": "^5.0.0",
48
48
  "vitest": "^2.1.8",
49
- "@telorun/sdk": "0.92.0"
49
+ "@telorun/sdk": "0.97.0"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "@telorun/sdk": "*"
@@ -1,6 +1,7 @@
1
1
  import { isCompiledValue, type CompiledValue } from "@telorun/sdk";
2
2
  import type { ASTNode, Environment } from "@marcbachmann/cel-js";
3
3
  import { extractAccessChains } from "./analyze.js";
4
+ import { CEL_FUNCTIONS } from "./catalog.js";
4
5
  import { resolveModuleCalls } from "./module-call.js";
5
6
 
6
7
  export const TEMPLATE_REGEX = /\$\{\{\s*([^}]+?)\s*\}\}/g;
@@ -18,6 +19,28 @@ function rootRefs(ast: ASTNode): readonly string[] {
18
19
  return [...roots];
19
20
  }
20
21
 
22
+ const NON_DETERMINISTIC = new Set(
23
+ CEL_FUNCTIONS.filter((f) => !f.deterministic).map((f) => f.name),
24
+ );
25
+
26
+ /** True when the expression calls a catalog function whose result differs per
27
+ * call (`uuidv4()`, `nowMillis()`), so evaluating it once and reusing the value
28
+ * would change what it means. */
29
+ function callsNonDeterministic(root: ASTNode): boolean {
30
+ const visit = (node: unknown): boolean => {
31
+ if (Array.isArray(node)) return node.some(visit);
32
+ if (!node || typeof node !== "object" || !("op" in node)) return false;
33
+ const n = node as ASTNode;
34
+ if ((n.op === "call" || n.op === "rcall") && Array.isArray(n.args)) {
35
+ const name = (n.args as unknown[])[0];
36
+ if (typeof name === "string" && NON_DETERMINISTIC.has(name)) return true;
37
+ }
38
+ const args = n.args as unknown;
39
+ return Array.isArray(args) ? args.some(visit) : visit(args);
40
+ };
41
+ return visit(root);
42
+ }
43
+
21
44
  /** Compile a single CEL expression (no `${{ }}` wrapping) into a CompiledValue.
22
45
  * Throws on syntax errors. Used by the `!cel` engine where the entire tagged
23
46
  * scalar is treated as one expression.
@@ -40,6 +63,7 @@ export function compileExpression(
40
63
  source: expr,
41
64
  refs: rootRefs(ast),
42
65
  calls,
66
+ ...(callsNonDeterministic(ast) ? { volatile: true as const } : {}),
43
67
  call: (ctx: Record<string, unknown>) => fn(ctx),
44
68
  };
45
69
  }
@@ -72,10 +96,12 @@ export function compileString(
72
96
 
73
97
  const refs = new Set<string>();
74
98
  const calls = new Set<string>();
99
+ let volatile = false;
75
100
  for (const p of parts) {
76
101
  if (typeof p === "string") continue;
77
102
  if (p.refs) for (const r of p.refs) refs.add(r);
78
103
  if (p.calls) for (const c of p.calls) calls.add(c);
104
+ if (p.volatile) volatile = true;
79
105
  }
80
106
 
81
107
  return {
@@ -84,6 +110,7 @@ export function compileString(
84
110
  parts,
85
111
  refs: [...refs],
86
112
  calls: [...calls],
113
+ ...(volatile ? { volatile: true as const } : {}),
87
114
  call: (ctx: Record<string, unknown>) =>
88
115
  parts.map((p) => (typeof p === "string" ? p : String(p.call(ctx) ?? ""))).join(""),
89
116
  } satisfies CompiledValue;