@alexknips/nestjs-kafka-events
Version:
Lightweight, tested, straight-forward wrapper around KafkaJS and Confluent's Schema Registry.
105 lines • 5.13 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KafkaSerializer = void 0;
const confluent_schema_registry_1 = require("@kafkajs/confluent-schema-registry");
const common_1 = require("@nestjs/common");
const topic_subject_helper_1 = require("../helpers/topic-subject.helper");
const loggers_1 = require("../loggers");
let KafkaSerializer = class KafkaSerializer {
constructor(kafkaLogger) {
this.kafkaLogger = kafkaLogger;
this.schemas = new Map();
}
async initialize(configuration, topics) {
this.schemaRegistry = new confluent_schema_registry_1.SchemaRegistry(configuration.api, configuration === null || configuration === void 0 ? void 0 : configuration.options);
await this.fetchAllSchemaIds(topics);
}
async fetchAllSchemaIds(topics) {
var e_1, _a;
try {
for (var topics_1 = __asyncValues(topics), topics_1_1; topics_1_1 = await topics_1.next(), !topics_1_1.done;) {
const topic = topics_1_1.value;
await this.fetchSchemaIds(topic);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (topics_1_1 && !topics_1_1.done && (_a = topics_1.return)) await _a.call(topics_1);
}
finally { if (e_1) throw e_1.error; }
}
}
async fetchSchemaIds(topic) {
try {
const keyId = await (async () => {
try {
const x = (await this.schemaRegistry.getLatestSchemaId((0, topic_subject_helper_1.getSchemaRegistryKeySubjectByTopic)(topic))) || null;
return x;
}
catch (error) {
return null;
}
})();
const valueId = await this.schemaRegistry.getLatestSchemaId((0, topic_subject_helper_1.getSchemaRegistryValueSubjectByTopic)(topic));
this.schemas.set(topic, {
keyId,
valueId,
});
}
catch (reject) {
this.kafkaLogger.error(`Error while fetching schema ids for topic ${topic}`, reject);
throw reject;
}
}
async serialize(value) {
const ids = this.schemas.get(value.topic);
if (!ids) {
this.kafkaLogger.error(`Trying to serialize message in topic ${value.topic} failed: No schema ids found.`);
return undefined;
}
try {
const message = {
value: await this.schemaRegistry.encode(ids.valueId, value.event),
partition: value === null || value === void 0 ? void 0 : value.partition,
headers: value === null || value === void 0 ? void 0 : value.headers,
timestamp: value === null || value === void 0 ? void 0 : value.timestamp,
};
if (ids === null || ids === void 0 ? void 0 : ids.keyId) {
message['key'] = await this.schemaRegistry.encode(ids.keyId, value.key);
}
else {
if (value.key && typeof value.key === 'string') {
message['key'] = value.key;
}
}
return message;
}
catch (reject) {
this.kafkaLogger.error(`Error while serializing message: ${JSON.stringify(value)}`, reject);
throw reject;
}
}
};
KafkaSerializer = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [loggers_1.KafkaLogger])
], KafkaSerializer);
exports.KafkaSerializer = KafkaSerializer;
//# sourceMappingURL=kafka.serializer.js.map