|
| 1 | +import {RuleRegistry} from "../../serde/rule-registry"; |
| 2 | +import {RuleContext, RuleExecutor} from "../../serde/serde"; |
| 3 | +import {ClientConfig} from "../../rest-service"; |
| 4 | +import stringify from "json-stringify-deterministic"; |
| 5 | +import {LRUCache} from "lru-cache"; |
| 6 | +import {createEnv} from "@bufbuild/cel"; |
| 7 | +import {createRegistry} from "@bufbuild/protobuf"; |
| 8 | + |
| 9 | +export class CelExecutor implements RuleExecutor { |
| 10 | + config: Map<string, string> | null = null |
| 11 | + env = createEnv("", createRegistry()); |
| 12 | + cache: LRUCache<string, any> = new LRUCache({max: 1000}) |
| 13 | + |
| 14 | + static register(): CelExecutor { |
| 15 | + const executor = new CelExecutor() |
| 16 | + RuleRegistry.registerRuleExecutor(executor) |
| 17 | + return executor |
| 18 | + } |
| 19 | + |
| 20 | + configure(clientConfig: ClientConfig, config: Map<string, string>) { |
| 21 | + this.config = config |
| 22 | + } |
| 23 | + |
| 24 | + type(): string { |
| 25 | + return "CEL" |
| 26 | + } |
| 27 | + |
| 28 | + async transform(ctx: RuleContext, msg: any): Promise<any> { |
| 29 | + const args = { |
| 30 | + message: msg |
| 31 | + } |
| 32 | + return await this.execute(ctx, msg, args) |
| 33 | + } |
| 34 | + |
| 35 | + async execute(ctx: RuleContext, msg: any, args: { [key: string]: any }): Promise<any> { |
| 36 | + let expr = ctx.rule.expr |
| 37 | + if (expr == null) { |
| 38 | + return msg |
| 39 | + } |
| 40 | + const index = expr.indexOf(';') |
| 41 | + if (index >= 0) { |
| 42 | + const guard = expr.substring(0, index) |
| 43 | + if (guard.trim().length != 0) { |
| 44 | + const guardResult = await this.executeRule(ctx, guard, msg, args) |
| 45 | + if (guardResult === false) { |
| 46 | + // skip the expr |
| 47 | + if (ctx.rule.kind === 'CONDITION') { |
| 48 | + return true |
| 49 | + } |
| 50 | + return msg |
| 51 | + } |
| 52 | + } |
| 53 | + expr = expr.substring(index + 1) |
| 54 | + } |
| 55 | + return await this.executeRule(ctx, expr, msg, args) |
| 56 | + } |
| 57 | + |
| 58 | + async executeRule(ctx: RuleContext, expr: string, obj: any, args: { [key: string]: any }): Promise<any> { |
| 59 | + const schema = ctx.target.schema |
| 60 | + const scriptType = ctx.target.schemaType |
| 61 | + const rule: RuleWithArgs = { |
| 62 | + rule: expr, |
| 63 | + scriptType: scriptType, |
| 64 | + schema: schema |
| 65 | + } |
| 66 | + const ruleJson = stringify(rule) |
| 67 | + let program = this.cache.get(ruleJson) |
| 68 | + if (program == null) { |
| 69 | + const parsedExpr = this.env.parse(expr) |
| 70 | + program = this.env.plan(parsedExpr) |
| 71 | + this.cache.set(ruleJson, program) |
| 72 | + } |
| 73 | + for (const [key, value] of Object.entries(args)) { |
| 74 | + this.env.set(key, value) |
| 75 | + } |
| 76 | + return this.env.eval(program) |
| 77 | + } |
| 78 | + |
| 79 | + async close(): Promise<void> { |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +interface RuleWithArgs { |
| 84 | + rule?: string |
| 85 | + scriptType?: string |
| 86 | + schema?: string |
| 87 | +} |
0 commit comments