UNPKG

@confluentinc/schemaregistry

Version:
78 lines (77 loc) 2.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CelExecutor = void 0; const rule_registry_1 = require("../../serde/rule-registry"); const json_stringify_deterministic_1 = __importDefault(require("json-stringify-deterministic")); const lru_cache_1 = require("lru-cache"); const cel_1 = require("@bufbuild/cel"); const strings_1 = require("@bufbuild/cel/ext/strings"); class CelExecutor { constructor() { this.config = null; this.env = (0, cel_1.celEnv)({ funcs: strings_1.STRINGS_EXT_FUNCS }); this.cache = new lru_cache_1.LRUCache({ max: 1000 }); } static register() { const executor = new CelExecutor(); rule_registry_1.RuleRegistry.registerRuleExecutor(executor); return executor; } configure(clientConfig, config) { this.config = config; } type() { return "CEL"; } async transform(ctx, msg) { const args = { message: msg }; return await this.execute(ctx, msg, args); } async execute(ctx, msg, args) { let expr = ctx.rule.expr; if (expr == null) { return msg; } const index = expr.indexOf(';'); if (index >= 0) { const guard = expr.substring(0, index); if (guard.trim().length != 0) { const guardResult = await this.executeRule(ctx, guard, msg, args); if (guardResult === false) { // skip the expr if (ctx.rule.kind === 'CONDITION') { return true; } return msg; } } expr = expr.substring(index + 1); } return await this.executeRule(ctx, expr, msg, args); } async executeRule(ctx, expr, obj, args) { const schema = ctx.target.schema; const scriptType = ctx.target.schemaType; const rule = { rule: expr, scriptType: scriptType, schema: schema }; const ruleJson = (0, json_stringify_deterministic_1.default)(rule); let program = this.cache.get(ruleJson); if (program == null) { const parsedExpr = (0, cel_1.parse)(expr); program = (0, cel_1.plan)(this.env, parsedExpr); this.cache.set(ruleJson, program); } return program(args); } async close() { } } exports.CelExecutor = CelExecutor;