'use strict'; var common = require('@nestjs/common'); var core = require('@kafka-ts/core'); /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __decorate(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; } function __param(paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } const KAFKA_PRODUCERS = 'KAFKA_PRODUCERS'; exports.KafkaModel = class KafkaModel { #clientProducers; constructor(clientProducers) { this.#clientProducers = clientProducers; } publish({ clientId = core.DEFAULT_CLIENT, topicMessages = [], ...rest }) { const producer = this.#clientProducers[clientId]; if (!producer) { throw `Unknown client ${clientId}`; } if (Array.isArray(topicMessages)) { return producer.sendBatch({ ...rest, topicMessages, }); } return producer.send({ ...rest, ...topicMessages, }); } }; exports.KafkaModel = __decorate([ common.Injectable(), __param(0, common.Inject(KAFKA_PRODUCERS)), __metadata("design:paramtypes", [Object]) ], exports.KafkaModel); async function createProducers(options) { if (!Array.isArray(options)) { options = [options]; } const [clientOptions, producerOptions] = options.reduce((result, option) => { const { producerOptions, ...rest } = option; result[0].push(rest); result[1][rest.clientId || core.DEFAULT_CLIENT] = producerOptions || {}; return result; }, [[], {}]); const kafkaClients = await core.createKafkaClients(clientOptions); const kafkaProducers = await Promise.allSettled(Object.entries(kafkaClients.getAllClients()).map(async ([clientId = core.DEFAULT_CLIENT, client]) => { const { createPartitioner, ...rest } = producerOptions[clientId]; const producer = client.producer({ ...rest, createPartitioner: createPartitioner || core.Partitioners.LegacyPartitioner, }); await producer.connect(); return { clientId, producer, }; })); return kafkaProducers.reduce((result, settledKafkaProducer) => { if (settledKafkaProducer.status === 'fulfilled') { const { clientId, producer } = settledKafkaProducer.value; result[clientId] = producer; } return result; }, {}); } var KafkaProducer_1; exports.KafkaProducer = KafkaProducer_1 = class KafkaProducer { clientProducers; static async register(options) { const clientProducers = await createProducers(options); return { module: KafkaProducer_1, providers: [ { provide: KAFKA_PRODUCERS, useFactory: async () => { return clientProducers; }, }, exports.KafkaModel, ], exports: [exports.KafkaModel], }; } constructor(clientProducers) { this.clientProducers = clientProducers; } // for jest test or other usages onApplicationShutdown(_signal) { Object.values(this.clientProducers).forEach((producer) => { producer.disconnect(); }); } }; exports.KafkaProducer = KafkaProducer_1 = __decorate([ common.Module({}), __param(0, common.Inject(KAFKA_PRODUCERS)), __metadata("design:paramtypes", [Object]) ], exports.KafkaProducer);