@@ -206,17 +206,9 @@ export abstract class AbstractKafkaConsumer<
206206 }
207207
208208 if ( this . options . batchProcessingEnabled && this . messageBatchStream ) {
209- this . messageBatchStream . on ( 'data' , async ( messageBatch ) =>
210- this . consume ( messageBatch . topic , messageBatch . messages ) ,
211- )
212- this . messageBatchStream . on ( 'error' , ( error ) => this . handlerError ( error ) )
209+ this . handleSyncStreamBatch ( this . messageBatchStream ) . catch ( ( error ) => this . handlerError ( error ) )
213210 } else {
214- // biome-ignore lint/style/noNonNullAssertion: consumerStream is always created
215- const stream = this . consumerStream !
216-
217- // we are not waiting for the stream to complete
218- // because init() must return promised void
219- this . handleSyncStream ( stream ) . catch ( ( error ) => this . handlerError ( error ) )
211+ this . handleSyncStream ( this . consumerStream ) . catch ( ( error ) => this . handlerError ( error ) )
220212 }
221213
222214 this . consumerStream . on ( 'error' , ( error ) => this . handlerError ( error ) )
@@ -232,6 +224,16 @@ export abstract class AbstractKafkaConsumer<
232224 )
233225 }
234226 }
227+ private async handleSyncStreamBatch (
228+ stream : KafkaMessageBatchStream < DeserializedMessage < SupportedMessageValues < TopicsConfig > > > ,
229+ ) : Promise < void > {
230+ for await ( const messageBatch of stream ) {
231+ await this . consume (
232+ messageBatch . topic ,
233+ messageBatch . messages as DeserializedMessage < SupportedMessageValues < TopicsConfig > > ,
234+ )
235+ }
236+ }
235237
236238 async close ( ) : Promise < void > {
237239 if ( ! this . consumerStream && ! this . messageBatchStream ) {
0 commit comments