UNPKG

@playtini/beta7

Version:

Microframework for Microservices

145 lines 6.35 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const confluent_schema_registry_1 = require("@playtini/confluent-schema-registry"); const Schemas = __importStar(require("../schemas")); const kafkajs_1 = require("kafkajs"); const _services_1 = require("../services"); class ServiceBroker { constructor(data) { this.schemas = {}; this.initialized = false; this.kafkaBrokers = data.kafkaBrokers; this.nodeID = data.nodeID; this.schemaRegistry = new confluent_schema_registry_1.SchemaRegistry({ host: data.schemaRegistry }); this.kafka = new kafkajs_1.Kafka({ clientId: this.nodeID, brokers: this.kafkaBrokers, }); this.producer = this.kafka.producer(); this.debug = new _services_1.LoggerService({ nodeID: this.nodeID, gelf: data.gelf, }); this.metrics = new _services_1.MetricsService(); this.debug.info('class created'); } async initSchemaRegistry() { var _a; this.debug.info('starting schema registry'); await Promise.all((_a = Object.values(Schemas)) === null || _a === void 0 ? void 0 : _a.map(async (entry) => { const { id } = await this.schemaRegistry.register({ type: confluent_schema_registry_1.SchemaType.AVRO, schema: JSON.stringify(entry.scheme.toJSON()), }); this.schemas[entry.name] = id; })); this.debug.info('schema registry started successfully'); } async encodeMessage(schemaID, payload) { this.debug.info('encoding message'); return await this.schemaRegistry.encode(schemaID, payload); } async decodeMessage(buffer) { this.debug.info('decoding message'); return await this.schemaRegistry.decode(buffer); } async registerSchema(schema, options) { this.debug.info('registering schema'); if (this.schemas[schema.name]) { this.debug.info('register schema ERROR, name already registered in registry'); throw new Error('This schema name already registered in registry'); } const { id } = await this.schemaRegistry.register({ type: confluent_schema_registry_1.SchemaType.AVRO, schema: JSON.stringify(schema.scheme.toJSON()), }, options); this.schemas[schema.name] = id; this.debug.info('schema registered successfully'); } async sendMessage(topic, messages) { this.debug.info(`sending message to topic: ${topic}`); const mappedMessages = []; const pushEncodedMessage = async (message) => { var _a, _b; if (!((_a = message === null || message === void 0 ? void 0 : message.value) === null || _a === void 0 ? void 0 : _a.timestamp)) { message.value.timestamp = Date.now(); } if (!((_b = message === null || message === void 0 ? void 0 : message.value) === null || _b === void 0 ? void 0 : _b.event_version)) { message.value.event_version = 1; } mappedMessages.push({ ...message, value: await this.encodeMessage(message.schemaID, message.value), }); }; if (Array.isArray(messages)) { for await (const message of messages) { this.metrics.produce.mark(); await pushEncodedMessage(message); } } else { this.metrics.produce.mark(); await pushEncodedMessage(messages); } await this.producer.send({ topic, messages: mappedMessages, }); this.debug.info('message successfully sent'); } async createService({ name, listeners }) { var _a; this.debug.info(`creating service ${name}`); const consumer = this.kafka.consumer({ groupId: name }); await consumer.connect(); for await (const [topicName, description] of Object.entries(listeners)) { await consumer.subscribe({ topic: new RegExp(topicName), fromBeginning: (_a = description.fromBeginning) !== null && _a !== void 0 ? _a : true, }); await consumer.run({ eachMessage: async ({ topic, partition, message }) => { var _a, _b; this.metrics.consume.mark(); if (message === null || message === void 0 ? void 0 : message.value) { const decodedMessage = await this.decodeMessage(message.value); await description.handler({ schemaName: (_b = (_a = decodedMessage === null || decodedMessage === void 0 ? void 0 : decodedMessage.constructor) === null || _a === void 0 ? void 0 : _a.type) === null || _b === void 0 ? void 0 : _b.name, ...decodedMessage, }, topic, partition); } }, }); } this.debug.info(`service ${name} started successfully`); } async start() { this.debug.info('starting broker'); await this.initSchemaRegistry(); await this.producer.connect(); this.initialized = true; this.debug.info('broker successfully started!'); } } exports.default = ServiceBroker; //# sourceMappingURL=ServiceBroker.js.map