import * as kafka from "kafka-node" ; export const defaultPartitionCount = 32 ; export const defaultReplicationFactor = 3 ; /**
 * Ensures that the provided topics exist
 * The topics will be created if they do not exist
 */ export async function ensureTopics ( client , topics , partitions = defaultPartitionCount , replicationFactor = defaultReplicationFactor ) { const topicExistsError = await new Promise < kafka . TopicsNotExistError | any | undefined > ( ( resolve ) => client . topicExists ( topics , ( error ) => error ? resolve ( error ) : resolve ( undefined ) ) ) ; if ( ! topicExistsError ) { return ; } const topicsNotExistError = topicExistsError as kafka . TopicsNotExistError ; if ( typeof ( topicsNotExistError ) !== "object" || typeof ( topicsNotExistError . topics ) !== "object" ) { throw new Error ( `Failed to ensure topics. ${ topicExistsError }` ) ; } return new Promise < void > ( ( resolve , reject ) => { const newTopics = Array . isArray ( topicsNotExistError . topics ) ? topicsNotExistError . topics : [ topicsNotExistError . topics ] ; client . createTopics ( newTopics . map ( ( topic ) => { return { topic , partitions , replicationFactor } ; } ) , ( createTopicError , result ) => { if ( createTopicError ) { reject ( createTopicError ) ; } else { const topicError = result . find ( ( value ) => value . error ) ; if ( topicError ) { reject ( new Error ( `Failed to create topic "${ topicError . topic }". Error: ${ topicError . error }` ) ) ; } else { resolve ( ) ; } } } ) ; } ) ; }