@@ -4,7 +4,7 @@ import { EventEmitter } from "events";
44import { v4 as uuidv4 } from "uuid" ;
55import { murmur } from "murmurhash" ;
66import { murmur2Partitioner } from "murmur2-partitioner" ;
7- import { Kafka , SASLMechanism , Admin , Producer , RecordMetadata , CompressionTypes } from "kafkajs" ;
7+ import { Kafka , KafkaConfig , SASLMechanism , Admin , Producer , RecordMetadata , CompressionTypes } from "kafkajs" ;
88import { Metadata , ProducerAnalytics , ProducerHealth , Check , ProducerRunResult , defaultAnalyticsInterval } from "../shared" ;
99import { MessageReturn , JSKafkaProducerConfig , ProducerStats , AnalyticsConfig , KafkaLogger } from "../interfaces" ;
1010import fs from "fs" ;
@@ -51,7 +51,7 @@ export class JSProducer extends EventEmitter {
5151 private _murmurHashVersion : string = DEFAULT_MURMURHASH_VERSION ;
5252 private _murmur ;
5353 private _errors = 0 ;
54-
54+
5555 defaultPartitionCount = 1 ;
5656
5757 /**
@@ -75,7 +75,7 @@ export class JSProducer extends EventEmitter {
7575 config . options = { } ;
7676 }
7777
78- const {
78+ const {
7979 "metadata.broker.list" : brokerList ,
8080 "client.id" : clientId ,
8181 "security.protocol" : securityProtocol ,
@@ -94,25 +94,28 @@ export class JSProducer extends EventEmitter {
9494 throw new Error ( "You are missing a broker or group configs" ) ;
9595 }
9696
97+ const conf = {
98+ brokers,
99+ clientId,
100+ } as KafkaConfig
97101 if ( securityProtocol ) {
98- this . kafkaClient = new Kafka ( {
99- brokers,
100- clientId,
101- ssl : {
102+ if ( securityProtocol . includes ( 'sasl' ) ) {
103+ conf . sasl = {
104+ mechanism : mechanism as SASLMechanism ,
105+ username : username as string ,
106+ password : password as string ,
107+ }
108+ }
109+ if ( securityProtocol . includes ( 'ssl' ) ) {
110+ conf . ssl = {
102111 ca : [ fs . readFileSync ( sslCALocation as string , "utf-8" ) ] ,
103112 cert : fs . readFileSync ( sslCertLocation as string , "utf-8" ) ,
104113 key : fs . readFileSync ( sslKeyLocation as string , "utf-8" ) ,
105114 passphrase : sslKeyPassword ,
106- } ,
107- sasl : {
108- mechanism : mechanism as SASLMechanism ,
109- username : username as string ,
110- password : password as string ,
111- } ,
112- } ) ;
113- } else {
114- this . kafkaClient = new Kafka ( { brokers, clientId } ) ;
115+ }
116+ }
115117 }
118+ this . kafkaClient = new Kafka ( conf ) ;
116119
117120 this . config = config ;
118121 this . _health = new ProducerHealth ( this , this . config . health ) ;
@@ -334,7 +337,7 @@ export class JSProducer extends EventEmitter {
334337 this . _totalSentMessages ++ ;
335338 const timestamp = producedAt . toString ( ) ;
336339 const acks = this . config && this . config . tconf && this . config . tconf [ "request.required.acks" ] || 1 ;
337- const compression = ( this . config . noptions )
340+ const compression = ( this . config . noptions )
338341 ? this . config . noptions [ "compression.codec" ]
339342 : CompressionTypes . None ;
340343
@@ -344,7 +347,7 @@ export class JSProducer extends EventEmitter {
344347 acks,
345348 compression,
346349 messages : [ {
347- key,
350+ key,
348351 value : convertedMessage ,
349352 partition,
350353 timestamp
0 commit comments