import { Environment } from "@marcbachmann/cel-js"; import { ExpressionError } from "../errors.js"; export const isExpression = (value: string): boolean => value.trimStart().startsWith("${{"); /** CEL's dynamic type: rule-specific values are type-checked when the expression runs. */ export const CEL_DYNAMIC_TYPE = "dyn"; type Variables = Record; type Predicate = (context: Record) => boolean; type ExpressionOptions = { resultType?: "bool" | "string" }; /** Compile once and report failures with the configuration path. */ export const compileExpression = ( input: string, path: string, variables: Variables, options: ExpressionOptions = {}, ): ((context: Record) => unknown) => { const failure = (error: unknown) => new ExpressionError(`Invalid expression at ${path}: ${error instanceof Error ? error.message : String(error)}`, { cause: error, }); try { const wrapped = input.trim(); if (!wrapped.startsWith("${{") || !wrapped.endsWith("}}")) { throw new Error("expected an expression wrapped in ${{ ... }}"); } const source = wrapped.slice(3, -2).trim(); const environment = new Environment({ unlistedVariablesAreDyn: false, enableOptionalTypes: true, }); for (const [name, type] of Object.entries(variables)) { environment.registerVariable(name, type); } const expression = environment.parse(source); const checked = expression.check(); if (!checked.valid) { throw checked.error; } if (options.resultType && checked.type !== options.resultType && checked.type !== CEL_DYNAMIC_TYPE) { throw new TypeError(`expression must return a ${options.resultType === "bool" ? "boolean" : "string"}`); } return (context) => { try { const result: unknown = expression(context); if (options.resultType && typeof result !== (options.resultType === "bool" ? "boolean" : "string")) { throw new TypeError(`expression must return a ${options.resultType === "bool" ? "boolean" : "string"}`); } return result; } catch (error) { throw failure(error); } }; } catch (error) { throw failure(error); } }; /** Compile a boolean predicate; compilation and evaluation errors propagate. */ export const compilePredicate = (input: string, path: string, variables: Variables): Predicate => { const expression = compileExpression(input, path, variables, { resultType: "bool" }); return (context) => expression(context) === true; }; export type NameContext = ReturnType; /** Capture one UTC instant and environment for every name in a configuration. */ export const createNameContext = () => { const now = new Date(); const iso = now.toISOString(); return { env: Object.fromEntries( Object.entries(process.env).filter((entry): entry is [string, string] => entry[1] !== undefined), ), date: { iso, isoDate: iso.slice(0, 10), year: now.getUTCFullYear(), month: now.getUTCMonth() + 1, day: now.getUTCDate(), unixMs: now.getTime(), }, }; }; export const resolveName = (value: string | undefined, path: string, context: NameContext): string | undefined => { if (value === undefined || !isExpression(value)) { return value; } return compileExpression(value, path, { env: "map", date: "map" }, { resultType: "string" })(context) as string; };