sqrl-cli-functions
Version:
SQRL (A Safe, Stateful Rules Language for Event Streams) - functions intended for command line tools
74 lines (73 loc) • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CliManipulator = void 0;
const sqrl_1 = require("sqrl");
const sqrl_common_1 = require("sqrl-common");
const bufferHumanJson_1 = require("./bufferHumanJson");
class CliManipulator extends sqrl_1.Manipulator {
constructor() {
super(...arguments);
this.sqrlKeys = new Set();
this.kafkaOutput = {};
this.blocked = false;
this.whitelisted = false;
this.blockedRules = [];
this.whitelistedRules = [];
this.loggedErrors = [];
this.loggedFeatures = {};
this.logged = [];
this.callbacks = [];
}
addKafka(topic, message) {
this.kafkaOutput[topic] = this.kafkaOutput[topic] || [];
this.kafkaOutput[topic].push(message);
}
setBlocked(context) {
this.blocked = true;
if (context) {
this.blockedRules.push(...context.firedRules);
}
}
setWhitelisted(context) {
this.whitelisted = true;
if (context) {
this.whitelistedRules.push(...context.firedRules);
}
}
wasBlocked() {
return this.blocked && !this.whitelisted;
}
getCurrentHumanOutput() {
return {
kafka: (0, sqrl_common_1.mapObject)(this.kafkaOutput, (messages, topic) => {
return messages.map((msg) => {
return (0, bufferHumanJson_1.bufferHumanJson)(msg);
});
}),
};
}
trackSqrlKey(key) {
this.sqrlKeys.add(key.getDebugString());
}
log(message) {
this.logged.push(message);
}
logFeature(name, value) {
this.loggedFeatures[name] = value;
}
logError(err) {
this.loggedErrors.push(err);
}
throwFirstError() {
if (this.loggedErrors.length) {
throw this.loggedErrors[0];
}
}
async mutate(ctx) {
await Promise.all(this.callbacks.map((cb) => cb(ctx)));
}
addCallback(cb) {
this.callbacks.push(cb);
}
}
exports.CliManipulator = CliManipulator;