@@ -20,11 +20,12 @@ import { config } from './config.js';
2020import { once } from 'node:events' ;
2121import process from 'node:process' ;
2222
23- import type { Consumer , EachMessagePayload , Producer , logLevel } from 'kafkajs' ;
23+ import type { Consumer , EachMessagePayload , Producer } from 'kafkajs' ;
2424import EventEmitter from 'eventemitter3' ;
25- import { Kafka } from 'kafkajs' ;
2625import debug from 'debug' ;
2726
27+ import Kafka from './Kafka.js' ;
28+
2829// Const info = debug('@oada/lib-kafka:info');
2930const error = debug ( '@oada/lib-kafka:error' ) ;
3031
@@ -55,7 +56,7 @@ function die(reason: Error) {
5556}
5657
5758export interface ConstructorOptions {
58- consumeTopic : string | string [ ] ;
59+ consumeTopic : string | readonly string [ ] ;
5960 // eslint-disable-next-line @typescript-eslint/ban-types
6061 produceTopic ?: string | null ;
6162 group : string ;
@@ -86,34 +87,14 @@ export interface KafkaBase {
8687 domain ?: string ;
8788}
8889
89- /**
90- * Make kafkajs logging nicer?
91- */
92- type KafkajsDebug = Record <
93- keyof Omit < typeof logLevel , 'NOTHING' > ,
94- debug . Debugger
95- > ;
96- const kafkajsDebugs = new Map < string , KafkajsDebug > ( ) ;
97- function getKafkajsDebug ( namespace : string ) : KafkajsDebug {
98- const d = kafkajsDebugs . get ( namespace ) ;
99- if ( d ) {
100- return d ;
101- }
102-
103- const newDebug = {
104- ERROR : debug ( `kafkajs:${ namespace } :error` ) ,
105- WARN : debug ( `kafkajs:${ namespace } :warn` ) ,
106- INFO : debug ( `kafkajs:${ namespace } :info` ) ,
107- DEBUG : debug ( `kafkajs:${ namespace } :debug` ) ,
108- } ;
109- kafkajsDebugs . set ( namespace , newDebug ) ;
110- return newDebug ;
90+ function isArray ( value : unknown ) : value is unknown [ ] | readonly unknown [ ] {
91+ return Array . isArray ( value ) ;
11192}
11293
11394export class Base extends EventEmitter {
11495 protected static done = Symbol ( 'kafka-base-done' ) ;
11596
116- readonly consumeTopic ;
97+ readonly consumeTopics ;
11798 readonly produceTopic ;
11899 readonly group ;
119100 readonly #kafka: Kafka ;
@@ -130,29 +111,11 @@ export class Base extends EventEmitter {
130111 } : ConstructorOptions ) {
131112 super ( ) ;
132113
133- this . consumeTopic = consumeTopic ;
114+ this . consumeTopics = isArray ( consumeTopic ) ? consumeTopic : [ consumeTopic ] ;
134115 this . produceTopic = produceTopic ;
135116 this . group = group ;
136117
137- this . #kafka = new Kafka ( {
138- /**
139- * Make kafkajs logging nicer?
140- */
141- logCreator ( ) {
142- return ( { namespace, label, log } ) => {
143- const l = label as keyof KafkajsDebug ;
144- // eslint-disable-next-line security/detect-object-injection
145- const logger = getKafkajsDebug ( namespace ) [ l ] ;
146- if ( log instanceof Error ) {
147- logger ( { err : log } , log . message ) ;
148- } else {
149- const { message, ...extra } = log ;
150- logger ( extra , message ) ;
151- }
152- } ;
153- } ,
154- brokers : config . get ( 'kafka.broker' ) ,
155- } ) ;
118+ this . #kafka = new Kafka ( ) ;
156119
157120 this . consumer =
158121 consumer ??
@@ -245,10 +208,7 @@ export class Base extends EventEmitter {
245208 await this . consumer . connect ( ) ;
246209 await this . producer . connect ( ) ;
247210
248- for ( const topic of Array . isArray ( this . consumeTopic )
249- ? this . consumeTopic
250- : [ this . consumeTopic ] ) {
251- // eslint-disable-next-line no-await-in-loop
211+ for await ( const topic of this . consumeTopics ) {
252212 await this . consumer . subscribe ( { topic } ) ;
253213 }
254214
0 commit comments