UNPKG

@dtu-olp-2024/kafka-nocobase

Version:

A NocoBase plugin for Apache Kafka integration, supporting message publishing, topic management, and event-driven workflows. Built for the DTU GreenHope project.

1 lines 298 kB
(function(){var t={8693:function(t,e,r){const o=r(2520);const s=r(6662);const n=r(5233);const i=r(1018);const a=r(5516);const c=r(1208);const u=r(5834);const p=r(3331);const d=r(6149);const l=r(8164);const f=r(1867);const{isRebalancing:h,isKafkaJSError:m,...g}=r(1331);const{LEVELS:I}=r(762);t.exports={Kafka:o,PartitionAssigners:s,AssignerProtocol:n,Partitioners:i,logLevel:I,CompressionTypes:a.Types,CompressionCodecs:a.Codecs,ConfigResourceTypes:c,AclResourceTypes:p,AclOperationTypes:d,AclPermissionTypes:l,ResourcePatternTypes:f,ConfigSource:u,...g}},8362:function(t,e,r){const o=r(4091);const s=r(4569);const n=r(7498);const i=r(1884);const a=r(547);const{events:c,wrap:u,unwrap:p}=r(7112);const{LEVELS:d}=r(762);const{KafkaJSNonRetriableError:l,KafkaJSDeleteGroupsError:f,KafkaJSBrokerNotFound:h,KafkaJSDeleteTopicRecordsError:m,KafkaJSAggregateError:g}=r(1331);const{staleMetadata:I}=r(1133);const y=r(1208);const w=r(3331);const T=r(6149);const E=r(8164);const S=r(1867);const{EARLIEST_OFFSET:b,LATEST_OFFSET:A}=r(8691);const{CONNECT:O,DISCONNECT:C}=c;const N=-1;const{values:R,keys:k,entries:x}=Object;const v=R(c);const _=k(c).map((t=>`admin.events.${t}`)).join(", ");const retryOnLeaderNotAvailable=(t,e={})=>{const callback=async()=>{try{return await t()}catch(t){if(t.type!=="LEADER_NOT_AVAILABLE"){throw t}return false}};return s(callback,e)};const isConsumerGroupRunning=t=>["Empty","Dead"].includes(t.state);const findTopicPartitions=async(t,e)=>{await t.addTargetTopic(e);await t.refreshMetadataIfNecessary();return t.findTopicPartitionMetadata(e).map((({partitionId:t})=>t)).sort()};const indexByPartition=t=>t.reduce(((t,{partition:e,...r})=>Object.assign(t,{[e]:{...r}})),{});t.exports=({logger:t,instrumentationEmitter:e,retry:r,cluster:s})=>{const k=t.namespace("Admin");const L=e||new a;const connect=async()=>{await s.connect();L.emit(O)};const disconnect=async()=>{await s.disconnect();L.emit(C)};const listTopics=async()=>{const{topicMetadata:t}=await s.metadata();const e=t.map((t=>t.topic));return e};const createTopics=async({topics:t,validateOnly:e,timeout:n,waitForLeaders:i=true})=>{if(!t||!Array.isArray(t)){throw new l(`Invalid topics array ${t}`)}if(t.filter((({topic:t})=>typeof t!=="string")).length>0){throw new l("Invalid topics array, the topic names have to be a valid string")}const a=new Set(t.map((({topic:t})=>t)));if(a.size<t.length){throw new l("Invalid topics array, it cannot have multiple entries for the same topic")}for(const{topic:e,configEntries:r}of t){if(r==null){continue}if(!Array.isArray(r)){throw new l(`Invalid configEntries for topic "${e}", must be an array`)}r.forEach(((t,r)=>{if(typeof t!=="object"||t==null){throw new l(`Invalid configEntries for topic "${e}". Entry ${r} must be an object`)}for(const o of["name","value"]){if(!Object.prototype.hasOwnProperty.call(t,o)||typeof t[o]!=="string"){throw new l(`Invalid configEntries for topic "${e}". Entry ${r} must have a valid "${o}" property`)}}}))}const c=o(r);return c((async(r,o,c)=>{try{await s.refreshMetadata();const r=await s.findControllerBroker();await r.createTopics({topics:t,validateOnly:e,timeout:n});if(i){const t=Array.from(a.values());await retryOnLeaderNotAvailable((async()=>await r.metadata(t)),{delay:100,maxWait:n,timeoutMessage:"Timed out while waiting for topic leaders"})}return true}catch(t){if(t.type==="NOT_CONTROLLER"){k.warn("Could not create topics",{error:t.message,retryCount:o,retryTime:c});throw t}if(t instanceof g){if(t.errors.every((t=>t.type==="TOPIC_ALREADY_EXISTS"))){return false}}r(t)}}))};const createPartitions=async({topicPartitions:t,validateOnly:e,timeout:n})=>{if(!t||!Array.isArray(t)){throw new l(`Invalid topic partitions array ${t}`)}if(t.length===0){throw new l(`Empty topic partitions array`)}if(t.filter((({topic:t})=>typeof t!=="string")).length>0){throw new l("Invalid topic partitions array, the topic names have to be a valid string")}const i=new Set(t.map((({topic:t})=>t)));if(i.size<t.length){throw new l("Invalid topic partitions array, it cannot have multiple entries for the same topic")}const a=o(r);return a((async(r,o,i)=>{try{await s.refreshMetadata();const r=await s.findControllerBroker();await r.createPartitions({topicPartitions:t,validateOnly:e,timeout:n})}catch(t){if(t.type==="NOT_CONTROLLER"){k.warn("Could not create topics",{error:t.message,retryCount:o,retryTime:i});throw t}r(t)}}))};const deleteTopics=async({topics:t,timeout:e})=>{if(!t||!Array.isArray(t)){throw new l(`Invalid topics array ${t}`)}if(t.filter((t=>typeof t!=="string")).length>0){throw new l("Invalid topics array, the names must be a valid string")}const n=o(r);return n((async(r,o,n)=>{try{await s.refreshMetadata();const r=await s.findControllerBroker();await r.deleteTopics({topics:t,timeout:e});for(const e of t){s.targetTopics.delete(e)}await s.refreshMetadata()}catch(t){if(["NOT_CONTROLLER","UNKNOWN_TOPIC_OR_PARTITION"].includes(t.type)){k.warn("Could not delete topics",{error:t.message,retryCount:o,retryTime:n});throw t}if(t.type==="REQUEST_TIMED_OUT"){k.error('Could not delete topics, check if "delete.topic.enable" is set to "true" (the default value is "false") or increase the timeout',{error:t.message,retryCount:o,retryTime:n})}r(t)}}))};const fetchTopicOffsets=async t=>{if(!t||typeof t!=="string"){throw new l(`Invalid topic ${t}`)}const e=o(r);return e((async(e,r,o)=>{try{await s.addTargetTopic(t);await s.refreshMetadataIfNecessary();const e=s.findTopicPartitionMetadata(t);const r=await s.fetchTopicsOffset([{topic:t,fromBeginning:false,partitions:e.map((t=>({partition:t.partitionId})))}]);const o=await s.fetchTopicsOffset([{topic:t,fromBeginning:true,partitions:e.map((t=>({partition:t.partitionId})))}]);const{partitions:n}=r.pop();const{partitions:i}=o.pop();return n.map((({partition:t,offset:e})=>({partition:t,offset:e,high:e,low:i.find((({partition:e})=>e===t)).offset})))}catch(t){if(t.type==="UNKNOWN_TOPIC_OR_PARTITION"){await s.refreshMetadata();throw t}e(t)}}))};const fetchTopicOffsetsByTimestamp=async(t,e)=>{if(!t||typeof t!=="string"){throw new l(`Invalid topic ${t}`)}const n=o(r);return n((async(r,o,n)=>{try{await s.addTargetTopic(t);await s.refreshMetadataIfNecessary();const r=s.findTopicPartitionMetadata(t);const o=r.map((t=>({partition:t.partitionId})));const n=await s.fetchTopicsOffset([{topic:t,fromBeginning:false,partitions:o}]);const{partitions:i}=n.pop();const a=await s.fetchTopicsOffset([{topic:t,fromTimestamp:e,partitions:o}]);const{partitions:c}=a.pop();return c.map((({partition:t,offset:e})=>({partition:t,offset:parseInt(e,10)>=0?e:i.find((({partition:e})=>e===t)).offset})))}catch(t){if(t.type==="UNKNOWN_TOPIC_OR_PARTITION"){await s.refreshMetadata();throw t}r(t)}}))};const fetchOffsets=async({groupId:t,topics:e,resolveOffsets:r=false})=>{if(!t){throw new l(`Invalid groupId ${t}`)}if(!e){e=[]}if(!Array.isArray(e)){throw new l("Expected topics array to be set")}const o=await s.findGroupCoordinator({groupId:t});const n=await Promise.all(e.map((async t=>{const e=await findTopicPartitions(s,t);const r=e.map((t=>({partition:t})));return{topic:t,partitions:r}})));let{responses:i}=await o.offsetFetch({groupId:t,topics:n});if(r){i=await Promise.all(i.map((async({topic:e,partitions:r})=>{const o=indexByPartition(await fetchTopicOffsets(e));const s=r.map((({offset:t,partition:e,...r})=>{let s=t;if(Number(t)===b){s=o[e].low}if(Number(t)===A){s=o[e].high}return{partition:e,offset:s,...r}}));await setOffsets({groupId:t,topic:e,partitions:s});return{topic:e,partitions:s}})))}return i.map((({topic:t,partitions:e})=>{const r=e.map((({partition:t,offset:e,metadata:r})=>({partition:t,offset:e,metadata:r||null})));return{topic:t,partitions:r}}))};const resetOffsets=async({groupId:t,topic:e,earliest:r=false})=>{if(!t){throw new l(`Invalid groupId ${t}`)}if(!e){throw new l(`Invalid topic ${e}`)}const o=await findTopicPartitions(s,e);const n=o.map((t=>({partition:t,offset:s.defaultOffset({fromBeginning:r})})));return setOffsets({groupId:t,topic:e,partitions:n})};const setOffsets=async({groupId:e,topic:r,partitions:o})=>{if(!e){throw new l(`Invalid groupId ${e}`)}if(!r){throw new l(`Invalid topic ${r}`)}if(!o||o.length===0){throw new l(`Invalid partitions`)}const n=i({logger:t.namespace("Admin",d.NOTHING),cluster:s,groupId:e});await n.subscribe({topic:r,fromBeginning:true});const a=await n.describeGroup();if(!isConsumerGroupRunning(a)){throw new l(`The consumer group must have no running instances, current state: ${a.state}`)}return new Promise(((t,e)=>{n.on(n.events.FETCH,(async()=>n.stop().then(t).catch(e)));n.run({eachBatchAutoResolve:false,eachBatch:async()=>true}).catch(e);n.pause([{topic:r}]);for(const t of o){n.seek({topic:r,...t})}}))};const isBrokerConfig=t=>[y.BROKER,y.BROKER_LOGGER].includes(t);const groupResourcesByBroker=({resources:t,defaultBroker:e})=>n(t,(async({type:t,name:r})=>isBrokerConfig(t)?await s.findBroker({nodeId:String(r)}):e));const describeConfigs=async({resources:t,includeSynonyms:e})=>{if(!t||!Array.isArray(t)){throw new l(`Invalid resources array ${t}`)}if(t.length===0){throw new l("Resources array cannot be empty")}const n=Object.values(y);const i=t.find((t=>!n.includes(t.type)));if(i){throw new l(`Invalid resource type ${i.type}: ${JSON.stringify(i)}`)}const a=t.find((t=>!t.name||typeof t.name!=="string"));if(a){throw new l(`Invalid resource name ${a.name}: ${JSON.stringify(a)}`)}const c=t.find((t=>!Array.isArray(t.configNames)&&t.configNames!=null));if(c){const{configNames:t}=c;throw new l(`Invalid resource configNames ${t}: ${JSON.stringify(c)}`)}const u=o(r);return u((async(r,o,n)=>{try{await s.refreshMetadata();const r=await s.findControllerBroker();const o=await groupResourcesByBroker({resources:t,defaultBroker:r});const describeConfigsAction=async t=>{const s=t||r;return s.describeConfigs({resources:o.get(s),includeSynonyms:e})};const n=Array.from(o.keys());const i=await Promise.all(n.map(describeConfigsAction));const a=i.reduce(((t,{resources:e})=>[...t,...e]),[]);return{resources:a}}catch(t){if(t.type==="NOT_CONTROLLER"){k.warn("Could not describe configs",{error:t.message,retryCount:o,retryTime:n});throw t}r(t)}}))};const alterConfigs=async({resources:t,validateOnly:e})=>{if(!t||!Array.isArray(t)){throw new l(`Invalid resources array ${t}`)}if(t.length===0){throw new l("Resources array cannot be empty")}const n=Object.values(y);const i=t.find((t=>!n.includes(t.type)));if(i){throw new l(`Invalid resource type ${i.type}: ${JSON.stringify(i)}`)}const a=t.find((t=>!t.name||typeof t.name!=="string"));if(a){throw new l(`Invalid resource name ${a.name}: ${JSON.stringify(a)}`)}const c=t.find((t=>!Array.isArray(t.configEntries)));if(c){const{configEntries:t}=c;throw new l(`Invalid resource configEntries ${t}: ${JSON.stringify(c)}`)}const u=t.find((t=>t.configEntries.some((t=>typeof t.name!=="string"||typeof t.value!=="string"))));if(u){throw new l(`Invalid resource config value: ${JSON.stringify(u)}`)}const p=o(r);return p((async(r,o,n)=>{try{await s.refreshMetadata();const r=await s.findControllerBroker();const o=await groupResourcesByBroker({resources:t,defaultBroker:r});const alterConfigsAction=async t=>{const s=t||r;return s.alterConfigs({resources:o.get(s),validateOnly:!!e})};const n=Array.from(o.keys());const i=await Promise.all(n.map(alterConfigsAction));const a=i.reduce(((t,{resources:e})=>[...t,...e]),[]);return{resources:a}}catch(t){if(t.type==="NOT_CONTROLLER"){k.warn("Could not alter configs",{error:t.message,retryCount:o,retryTime:n});throw t}r(t)}}))};const fetchTopicMetadata=async({topics:t=[]}={})=>{if(t){t.forEach((t=>{if(!t||typeof t!=="string"){throw new l(`Invalid topic ${t}`)}}))}const e=await s.metadata({topics:t});return{topics:e.topicMetadata.map((t=>({name:t.topic,partitions:t.partitionMetadata})))}};const describeCluster=async()=>{const{brokers:t,clusterId:e,controllerId:r}=await s.metadata({topics:[]});const o=t.map((({nodeId:t,host:e,port:r})=>({nodeId:t,host:e,port:r})));const n=r==null||r===N?null:r;return{brokers:o,controller:n,clusterId:e}};const listGroups=async()=>{await s.refreshMetadata();let t=[];for(var e in s.brokerPool.brokers){const r=await s.findBroker({nodeId:e});const o=await r.listGroups();t=t.concat(o.groups)}return{groups:t}};const describeGroups=async t=>{const e=await Promise.all(t.map((async t=>{const e=await s.findGroupCoordinator({groupId:t});return{coordinator:e,groupId:t}})));const n=Object.values(e.reduce(((t,{coordinator:e,groupId:r})=>{const o=t[e.nodeId];if(o){t[e.nodeId]={...o,groupIds:[...o.groupIds,r]}}else{t[e.nodeId]={coordinator:e,groupIds:[r]}}return t}),{}));const i=await Promise.all(n.map((async({coordinator:t,groupIds:e})=>{const s=o(r);const{groups:n}=await s((()=>t.describeGroups({groupIds:e})));return n})));const a=[].concat.apply([],i);return{groups:a}};const deleteGroups=async t=>{if(!t||!Array.isArray(t)){throw new l(`Invalid groupIds array ${t}`)}const e=t.some((t=>typeof t!=="string"));if(e){throw new l(`Invalid groupId name: ${JSON.stringify(e)}`)}const n=o(r);let i=[];let a=t.slice();return n((async(t,e,r)=>{try{if(a.length===0)return[];await s.refreshMetadata();const t={};const e={};for(const r of a){const o=await s.findGroupCoordinator({groupId:r});if(t[o.nodeId]===undefined)t[o.nodeId]=[];t[o.nodeId].push(r);e[o.nodeId]=o}const r=await Promise.all(Object.keys(e).map((async r=>await e[r].deleteGroups(t[r]))));const o=r.flatMap((({results:t})=>t.map((({groupId:t,errorCode:e,error:r})=>({groupId:t,errorCode:e,error:r}))))).filter((({errorCode:t})=>t!==0));a=o.map((({groupId:t})=>t));if(o.length>0)throw new f("Error in DeleteGroups",o);i=r.flatMap((({results:t})=>t));return i}catch(o){if(o.type==="NOT_CONTROLLER"||o.type==="COORDINATOR_NOT_AVAILABLE"){k.warn("Could not delete groups",{error:o.message,retryCount:e,retryTime:r});throw o}t(o)}}))};const deleteTopicRecords=async({topic:t,partitions:e})=>{if(!t||typeof t!=="string"){throw new l(`Invalid topic "${t}"`)}if(!e||e.length===0){throw new l(`Invalid partitions`)}const n=s.findLeaderForPartitions(t,e.map((t=>t.partition)));const i=R(n).flat();const a=await fetchTopicOffsets(t);const c=[];e.forEach((({partition:e,offset:r})=>{if(!i.includes(e)){c.push({partition:e,offset:r,error:new h("Could not find the leader for the partition",{retriable:false})});return}const{low:o}=a.find((t=>t.partition===e))||{high:undefined,low:undefined};if(parseInt(r)<parseInt(o)&&parseInt(r)!==-1){k.warn("The requested offset is before the earliest offset maintained on the partition - no records will be deleted from this partition",{topic:t,partition:e,offset:r})}}));if(c.length>0){throw new m({topic:t,partitions:c})}const u=x(n).reduce(((r,[o,s])=>{r[o]={topic:t,partitions:e.filter((t=>s.includes(t.partition)))};return r}),{});const p=o(r);return p((async e=>{try{const e=[];const r=x(u).map((([t,{topic:e,partitions:r}])=>async()=>{const o=await s.findBroker({nodeId:t});await o.deleteRecords({topics:[{topic:e,partitions:r}]});delete u[t]}));await Promise.all(r.map((t=>t().catch((t=>{if(t.name==="KafkaJSDeleteTopicRecordsError"){t.partitions.forEach((({partition:t,offset:r,error:o})=>{e.push({partition:t,offset:r,error:o})}))}else{throw t}})))));if(e.length>0){throw new m({topic:t,partitions:e})}}catch(t){if(t.retriable&&t.partitions.some((({error:t})=>I(t)||t.name==="KafkaJSMetadataNotLoaded"))){await s.refreshMetadata()}throw t}}))};const createAcls=async({acl:t})=>{if(!t||!Array.isArray(t)){throw new l(`Invalid ACL array ${t}`)}if(t.length===0){throw new l("Empty ACL array")}if(t.some((({principal:t})=>typeof t!=="string"))){throw new l("Invalid ACL array, the principals have to be a valid string")}if(t.some((({host:t})=>typeof t!=="string"))){throw new l("Invalid ACL array, the hosts have to be a valid string")}if(t.some((({resourceName:t})=>typeof t!=="string"))){throw new l("Invalid ACL array, the resourceNames have to be a valid string")}let e;const n=Object.values(T);e=t.find((t=>!n.includes(t.operation)));if(e){throw new l(`Invalid operation type ${e.operation}: ${JSON.stringify(e)}`)}const i=Object.values(S);e=t.find((t=>!i.includes(t.resourcePatternType)));if(e){throw new l(`Invalid resource pattern type ${e.resourcePatternType}: ${JSON.stringify(e)}`)}const a=Object.values(E);e=t.find((t=>!a.includes(t.permissionType)));if(e){throw new l(`Invalid permission type ${e.permissionType}: ${JSON.stringify(e)}`)}const c=Object.values(w);e=t.find((t=>!c.includes(t.resourceType)));if(e){throw new l(`Invalid resource type ${e.resourceType}: ${JSON.stringify(e)}`)}const u=o(r);return u((async(e,r,o)=>{try{await s.refreshMetadata();const e=await s.findControllerBroker();await e.createAcls({acl:t});return true}catch(t){if(t.type==="NOT_CONTROLLER"){k.warn("Could not create ACL",{error:t.message,retryCount:r,retryTime:o});throw t}e(t)}}))};const describeAcls=async({resourceType:t,resourceName:e,resourcePatternType:n,principal:i,host:a,operation:c,permissionType:u})=>{if(typeof i!=="string"&&typeof i!=="undefined"){throw new l("Invalid principal, the principal have to be a valid string")}if(typeof a!=="string"&&typeof a!=="undefined"){throw new l("Invalid host, the host have to be a valid string")}if(typeof e!=="string"&&typeof e!=="undefined"){throw new l("Invalid resourceName, the resourceName have to be a valid string")}const p=Object.values(T);if(!p.includes(c)){throw new l(`Invalid operation type ${c}`)}const d=Object.values(S);if(!d.includes(n)){throw new l(`Invalid resource pattern filter type ${n}`)}const f=Object.values(E);if(!f.includes(u)){throw new l(`Invalid permission type ${u}`)}const h=Object.values(w);if(!h.includes(t)){throw new l(`Invalid resource type ${t}`)}const m=o(r);return m((async(r,o,p)=>{try{await s.refreshMetadata();const r=await s.findControllerBroker();const{resources:o}=await r.describeAcls({resourceType:t,resourceName:e,resourcePatternType:n,principal:i,host:a,operation:c,permissionType:u});return{resources:o}}catch(t){if(t.type==="NOT_CONTROLLER"){k.warn("Could not describe ACL",{error:t.message,retryCount:o,retryTime:p});throw t}r(t)}}))};const deleteAcls=async({filters:t})=>{if(!t||!Array.isArray(t)){throw new l(`Invalid ACL Filter array ${t}`)}if(t.length===0){throw new l("Empty ACL Filter array")}if(t.some((({principal:t})=>typeof t!=="string"&&typeof t!=="undefined"))){throw new l("Invalid ACL Filter array, the principals have to be a valid string")}if(t.some((({host:t})=>typeof t!=="string"&&typeof t!=="undefined"))){throw new l("Invalid ACL Filter array, the hosts have to be a valid string")}if(t.some((({resourceName:t})=>typeof t!=="string"&&typeof t!=="undefined"))){throw new l("Invalid ACL Filter array, the resourceNames have to be a valid string")}let e;const n=Object.values(T);e=t.find((t=>!n.includes(t.operation)));if(e){throw new l(`Invalid operation type ${e.operation}: ${JSON.stringify(e)}`)}const i=Object.values(S);e=t.find((t=>!i.includes(t.resourcePatternType)));if(e){throw new l(`Invalid resource pattern type ${e.resourcePatternType}: ${JSON.stringify(e)}`)}const a=Object.values(E);e=t.find((t=>!a.includes(t.permissionType)));if(e){throw new l(`Invalid permission type ${e.permissionType}: ${JSON.stringify(e)}`)}const c=Object.values(w);e=t.find((t=>!c.includes(t.resourceType)));if(e){throw new l(`Invalid resource type ${e.resourceType}: ${JSON.stringify(e)}`)}const u=o(r);return u((async(e,r,o)=>{try{await s.refreshMetadata();const e=await s.findControllerBroker();const{filterResponses:r}=await e.deleteAcls({filters:t});return{filterResponses:r}}catch(t){if(t.type==="NOT_CONTROLLER"){k.warn("Could not delete ACL",{error:t.message,retryCount:r,retryTime:o});throw t}e(t)}}))};const alterPartitionReassignments=async({topics:t,timeout:e})=>{if(!t||!Array.isArray(t)){throw new l(`Invalid topics array ${t}`)}if(t.filter((({topic:t})=>typeof t!=="string")).length>0){throw new l("Invalid topics array, the topic names have to be a valid string")}const n=new Set(t.map((({topic:t})=>t)));if(n.size<t.length){throw new l("Invalid topics array, it cannot have multiple entries for the same topic")}for(const{topic:e,partitionAssignment:r}of t){if(!r||!Array.isArray(r)){throw new l(`Invalid partitions array: ${r} for topic: ${e}`)}for(const{partition:t,replicas:o}of r){if(t===null||t===undefined||typeof t!=="number"||t<0){throw new l(`Invalid partitions index: ${t} for topic: ${e}`)}if(!o||!Array.isArray(o)){throw new l(`Invalid replica assignment: ${o} for topic: ${e} on partition: ${t}`)}if(o.filter((t=>typeof t!=="number"||t<0)).length>=1){throw new l(`Invalid replica assignment: ${o} for topic: ${e} on partition: ${t}. Replicas must be a non negative number`)}}}const i=o(r);return i((async(r,o,n)=>{try{await s.refreshMetadata();const r=await s.findControllerBroker();await r.alterPartitionReassignments({topics:t,timeout:e})}catch(t){if(t.type==="NOT_CONTROLLER"){k.warn("Could not reassign partitions",{error:t.message,retryCount:o,retryTime:n});throw t}r(t)}}))};const listPartitionReassignments=async({topics:t=null,timeout:e})=>{if(t){if(!Array.isArray(t)){throw new l(`Invalid topics array ${t}`)}if(t.filter((({topic:t})=>typeof t!=="string")).length>0){throw new l("Invalid topics array, the topic names have to be a valid string")}const e=new Set(t.map((({topic:t})=>t)));if(e.size<t.length){throw new l("Invalid topics array, it cannot have multiple entries for the same topic")}for(const{topic:e,partitions:r}of t){if(!r||!Array.isArray(r)){throw new l(`Invalid partition array: ${r} for topic: ${e}`)}if(r.filter((t=>typeof t!=="number"||t<0)).length>=1){throw new l(`Invalid partition array: ${r} for topic: ${e}. The partition indices have to be a valid number greater than 0.`)}}}const n=o(r);return n((async(r,o,n)=>{try{await s.refreshMetadata();const r=await s.findControllerBroker();const o=await r.listPartitionReassignments({topics:t,timeout:e});return{topics:o.topics}}catch(t){if(t.type==="NOT_CONTROLLER"){k.warn("Could not reassign partitions",{error:t.message,retryCount:o,retryTime:n});throw t}r(t)}}))};const on=(t,e)=>{if(!v.includes(t)){throw new l(`Event name should be one of ${_}`)}return L.addListener(p(t),(r=>{r.type=u(r.type);Promise.resolve(e(r)).catch((e=>{k.error(`Failed to execute listener: ${e.message}`,{eventName:t,stack:e.stack})}))}))};const getLogger=()=>k;return{connect:connect,disconnect:disconnect,listTopics:listTopics,createTopics:createTopics,deleteTopics:deleteTopics,createPartitions:createPartitions,fetchTopicMetadata:fetchTopicMetadata,describeCluster:describeCluster,events:c,fetchOffsets:fetchOffsets,fetchTopicOffsets:fetchTopicOffsets,fetchTopicOffsetsByTimestamp:fetchTopicOffsetsByTimestamp,setOffsets:setOffsets,resetOffsets:resetOffsets,describeConfigs:describeConfigs,alterConfigs:alterConfigs,on:on,logger:getLogger,listGroups:listGroups,describeGroups:describeGroups,deleteGroups:deleteGroups,describeAcls:describeAcls,deleteAcls:deleteAcls,createAcls:createAcls,deleteTopicRecords:deleteTopicRecords,alterPartitionReassignments:alterPartitionReassignments,listPartitionReassignments:listPartitionReassignments}}},7112:function(t,e,r){const o=r(5780);const s=r(4976);const n=r(1347);const i=n("admin");const a={CONNECT:i("connect"),DISCONNECT:i("disconnect"),REQUEST:i(s.NETWORK_REQUEST),REQUEST_TIMEOUT:i(s.NETWORK_REQUEST_TIMEOUT),REQUEST_QUEUE_SIZE:i(s.NETWORK_REQUEST_QUEUE_SIZE)};const c={[a.REQUEST]:s.NETWORK_REQUEST,[a.REQUEST_TIMEOUT]:s.NETWORK_REQUEST_TIMEOUT,[a.REQUEST_QUEUE_SIZE]:s.NETWORK_REQUEST_QUEUE_SIZE};const u=o(c);const unwrap=t=>c[t]||t;const wrap=t=>u[t]||t;t.exports={events:a,wrap:wrap,unwrap:unwrap}},6267:function(t,e,r){const o=r(6249);const{Types:s}=r(5516);const{requests:n,lookup:i}=r(7966);const{KafkaJSNonRetriableError:a}=r(1331);const c=r(1062);const u=r(9085);const p={SEND_REQUEST:Symbol("private:Broker:sendRequest")};const notInitializedLookup=()=>{throw new Error("Broker not connected")};t.exports=class Broker{constructor({connectionPool:t,logger:e,nodeId:r=null,versions:s=null,authenticationTimeout:n=1e4,allowAutoTopicCreation:i=true}){this.connectionPool=t;this.nodeId=r;this.rootLogger=e;this.logger=e.namespace("Broker");this.versions=s;this.authenticationTimeout=n;this.allowAutoTopicCreation=i;const a=2*this.connectionPool.connectionTimeout+this.authenticationTimeout;this.brokerAddress=`${this.connectionPool.host}:${this.connectionPool.port}`;this.lock=new o({timeout:a,description:`connect to broker ${this.brokerAddress}`});this.lookupRequest=notInitializedLookup}isConnected(){return this.connectionPool.sasl?this.connectionPool.isConnected()&&this.connectionPool.isAuthenticated():this.connectionPool.isConnected()}async connect(){await this.lock.acquire();try{if(this.isConnected()){return}const t=await this.connectionPool.getConnection();if(!this.versions){this.versions=await this.apiVersions()}this.connectionPool.setVersions(this.versions);this.lookupRequest=i(this.versions);if(t.getSupportAuthenticationProtocol()===null){let t=false;try{this.lookupRequest(c.SaslAuthenticate,n.SaslAuthenticate);t=true}catch(e){t=false}this.connectionPool.setSupportAuthenticationProtocol(t);this.logger.debug(`Verified support for SaslAuthenticate`,{broker:this.brokerAddress,supportAuthenticationProtocol:t})}await t.authenticate()}finally{await this.lock.release()}}async disconnect(){await this.connectionPool.destroy()}async apiVersions(){let t;const e=n.ApiVersions.versions.map(Number).sort().reverse();for(const r of e){try{const e=n.ApiVersions.protocol({version:r});t=await this[p.SEND_REQUEST]({...e(),requestTimeout:this.connectionPool.connectionTimeout});break}catch(t){if(t.type!=="UNSUPPORTED_VERSION"){throw t}}}if(!t){throw new a("API Versions not supported")}return t.apiVersions.reduce(((t,e)=>Object.assign(t,{[e.apiKey]:{minVersion:e.minVersion,maxVersion:e.maxVersion}})),{})}async metadata(t=[]){const e=this.lookupRequest(c.Metadata,n.Metadata);const r=u(t);return await this[p.SEND_REQUEST](e({topics:r,allowAutoTopicCreation:this.allowAutoTopicCreation}))}async produce({topicData:t,transactionalId:e,producerId:r,producerEpoch:o,acks:i=-1,timeout:a=3e4,compression:u=s.None}){const d=this.lookupRequest(c.Produce,n.Produce);return await this[p.SEND_REQUEST](d({acks:i,timeout:a,compression:u,topicData:t,transactionalId:e,producerId:r,producerEpoch:o}))}async fetch({replicaId:t,isolationLevel:e,maxWaitTime:r=5e3,minBytes:o=1,maxBytes:s=10485760,topics:i,rackId:a=""}){const d=this.lookupRequest(c.Fetch,n.Fetch);const l=i.reduce(((t,{topic:e,partitions:r})=>{r.forEach((r=>{t.push({topic:e,partition:r})}));return t}),[]);const f=u(l);const h=f.reduce(((t,{topic:e,partition:r})=>{const o=t[t.length-1];if(o!=null&&o.topic===e){t[t.length-1].partitions.push(r)}else{t.push({topic:e,partitions:[r]})}return t}),[]);return await this[p.SEND_REQUEST](d({replicaId:t,isolationLevel:e,maxWaitTime:r,minBytes:o,maxBytes:s,topics:h,rackId:a}))}async heartbeat({groupId:t,groupGenerationId:e,memberId:r}){const o=this.lookupRequest(c.Heartbeat,n.Heartbeat);return await this[p.SEND_REQUEST](o({groupId:t,groupGenerationId:e,memberId:r}))}async findGroupCoordinator({groupId:t,coordinatorType:e}){const r=this.lookupRequest(c.GroupCoordinator,n.GroupCoordinator);return await this[p.SEND_REQUEST](r({groupId:t,coordinatorType:e}))}async joinGroup({groupId:t,sessionTimeout:e,rebalanceTimeout:r,memberId:o="",protocolType:s="consumer",groupProtocols:i}){const a=this.lookupRequest(c.JoinGroup,n.JoinGroup);const makeRequest=(n=o)=>this[p.SEND_REQUEST](a({groupId:t,sessionTimeout:e,rebalanceTimeout:r,memberId:n,protocolType:s,groupProtocols:i}));try{return await makeRequest()}catch(t){if(t.name==="KafkaJSMemberIdRequired"){return makeRequest(t.memberId)}throw t}}async leaveGroup({groupId:t,memberId:e}){const r=this.lookupRequest(c.LeaveGroup,n.LeaveGroup);return await this[p.SEND_REQUEST](r({groupId:t,memberId:e}))}async syncGroup({groupId:t,generationId:e,memberId:r,groupAssignment:o}){const s=this.lookupRequest(c.SyncGroup,n.SyncGroup);return await this[p.SEND_REQUEST](s({groupId:t,generationId:e,memberId:r,groupAssignment:o}))}async listOffsets({replicaId:t,isolationLevel:e,topics:r}){const o=this.lookupRequest(c.ListOffsets,n.ListOffsets);const s=await this[p.SEND_REQUEST](o({replicaId:t,isolationLevel:e,topics:r}));for(const t of s.responses){t.partitions=t.partitions.map((({offsets:t,...e})=>t?{...e,offset:t.pop()}:e))}return s}async offsetCommit({groupId:t,groupGenerationId:e,memberId:r,retentionTime:o,topics:s}){const i=this.lookupRequest(c.OffsetCommit,n.OffsetCommit);return await this[p.SEND_REQUEST](i({groupId:t,groupGenerationId:e,memberId:r,retentionTime:o,topics:s}))}async offsetFetch({groupId:t,topics:e}){const r=this.lookupRequest(c.OffsetFetch,n.OffsetFetch);return await this[p.SEND_REQUEST](r({groupId:t,topics:e}))}async describeGroups({groupIds:t}){const e=this.lookupRequest(c.DescribeGroups,n.DescribeGroups);return await this[p.SEND_REQUEST](e({groupIds:t}))}async createTopics({topics:t,validateOnly:e=false,timeout:r=5e3}){const o=this.lookupRequest(c.CreateTopics,n.CreateTopics);return await this[p.SEND_REQUEST](o({topics:t,validateOnly:e,timeout:r}))}async createPartitions({topicPartitions:t,validateOnly:e=false,timeout:r=5e3}){const o=this.lookupRequest(c.CreatePartitions,n.CreatePartitions);return await this[p.SEND_REQUEST](o({topicPartitions:t,validateOnly:e,timeout:r}))}async deleteTopics({topics:t,timeout:e=5e3}){const r=this.lookupRequest(c.DeleteTopics,n.DeleteTopics);return await this[p.SEND_REQUEST](r({topics:t,timeout:e}))}async describeConfigs({resources:t,includeSynonyms:e=false}){const r=this.lookupRequest(c.DescribeConfigs,n.DescribeConfigs);return await this[p.SEND_REQUEST](r({resources:t,includeSynonyms:e}))}async alterConfigs({resources:t,validateOnly:e=false}){const r=this.lookupRequest(c.AlterConfigs,n.AlterConfigs);return await this[p.SEND_REQUEST](r({resources:t,validateOnly:e}))}async initProducerId({transactionalId:t,transactionTimeout:e}){const r=this.lookupRequest(c.InitProducerId,n.InitProducerId);return await this[p.SEND_REQUEST](r({transactionalId:t,transactionTimeout:e}))}async addPartitionsToTxn({transactionalId:t,producerId:e,producerEpoch:r,topics:o}){const s=this.lookupRequest(c.AddPartitionsToTxn,n.AddPartitionsToTxn);return await this[p.SEND_REQUEST](s({transactionalId:t,producerId:e,producerEpoch:r,topics:o}))}async addOffsetsToTxn({transactionalId:t,producerId:e,producerEpoch:r,groupId:o}){const s=this.lookupRequest(c.AddOffsetsToTxn,n.AddOffsetsToTxn);return await this[p.SEND_REQUEST](s({transactionalId:t,producerId:e,producerEpoch:r,groupId:o}))}async txnOffsetCommit({transactionalId:t,groupId:e,producerId:r,producerEpoch:o,topics:s}){const i=this.lookupRequest(c.TxnOffsetCommit,n.TxnOffsetCommit);return await this[p.SEND_REQUEST](i({transactionalId:t,groupId:e,producerId:r,producerEpoch:o,topics:s}))}async endTxn({transactionalId:t,producerId:e,producerEpoch:r,transactionResult:o}){const s=this.lookupRequest(c.EndTxn,n.EndTxn);return await this[p.SEND_REQUEST](s({transactionalId:t,producerId:e,producerEpoch:r,transactionResult:o}))}async listGroups(){const t=this.lookupRequest(c.ListGroups,n.ListGroups);return await this[p.SEND_REQUEST](t())}async deleteGroups(t){const e=this.lookupRequest(c.DeleteGroups,n.DeleteGroups);return await this[p.SEND_REQUEST](e(t))}async deleteRecords({topics:t}){const e=this.lookupRequest(c.DeleteRecords,n.DeleteRecords);return await this[p.SEND_REQUEST](e({topics:t}))}async createAcls({acl:t}){const e=this.lookupRequest(c.CreateAcls,n.CreateAcls);return await this[p.SEND_REQUEST](e({creations:t}))}async describeAcls({resourceType:t,resourceName:e,resourcePatternType:r,principal:o,host:s,operation:i,permissionType:a}){const u=this.lookupRequest(c.DescribeAcls,n.DescribeAcls);return await this[p.SEND_REQUEST](u({resourceType:t,resourceName:e,resourcePatternType:r,principal:o,host:s,operation:i,permissionType:a}))}async deleteAcls({filters:t}){const e=this.lookupRequest(c.DeleteAcls,n.DeleteAcls);return await this[p.SEND_REQUEST](e({filters:t}))}async alterPartitionReassignments({topics:t,timeout:e}){const r=this.lookupRequest(c.AlterPartitionReassignments,n.AlterPartitionReassignments);return await this[p.SEND_REQUEST](r({topics:t,timeout:e}))}async listPartitionReassignments({topics:t=null,timeout:e}){const r=this.lookupRequest(c.ListPartitionReassignments,n.ListPartitionReassignments);return await this[p.SEND_REQUEST](r({topics:t,timeout:e}))}async[p.SEND_REQUEST](t){try{return await this.connectionPool.send(t)}catch(t){if(t.name==="KafkaJSConnectionClosedError"){await this.disconnect()}throw t}}}},8802:function(t,e,r){const{request:o,response:s}=r(7109);const{KafkaJSSASLAuthenticationError:n}=r(1331);const awsIAMAuthenticatorProvider=t=>({host:e,port:r,logger:i,saslAuthenticate:a})=>({authenticate:async()=>{if(!t.authorizationIdentity){throw new n("SASL AWS-IAM: Missing authorizationIdentity")}if(!t.accessKeyId){throw new n("SASL AWS-IAM: Missing accessKeyId")}if(!t.secretAccessKey){throw new n("SASL AWS-IAM: Missing secretAccessKey")}if(!t.sessionToken){t.sessionToken=""}const c=`${e}:${r}`;try{i.debug("Authenticate with SASL AWS-IAM",{broker:c});await a({request:o(t),response:s});i.debug("SASL AWS-IAM authentication successful",{broker:c})}catch(t){const e=new n(`SASL AWS-IAM authentication failed: ${t.message}`);i.error(e.message,{broker:c});throw e}}});t.exports=awsIAMAuthenticatorProvider},9727:function(t,e,r){const{requests:o,lookup:s}=r(7966);const n=r(1062);const i=r(9947);const a=r(1780);const c=r(9263);const u=r(8802);const p=r(8333);const{KafkaJSSASLAuthenticationError:d}=r(1331);const l={AWS:u,PLAIN:i,OAUTHBEARER:p,"SCRAM-SHA-256":a,"SCRAM-SHA-512":c};const f="0";t.exports=class SASLAuthenticator{constructor(t,e,r,i){this.connection=t;this.logger=e;this.sessionLifetime=f;const a=s(r);this.saslHandshake=a(n.SaslHandshake,o.SaslHandshake);this.protocolAuthentication=i?a(n.SaslAuthenticate,o.SaslAuthenticate):null}async authenticate(){const t=this.connection.sasl.mechanism.toUpperCase();const e=await this.connection.send(this.saslHandshake({mechanism:t}));if(!e.enabledMechanisms.includes(t)){throw new d(`SASL ${t} mechanism is not supported by the server`)}const saslAuthenticate=async({request:t,response:e})=>{if(this.protocolAuthentication){const r=await t.encode();const o=await this.connection.send(this.protocolAuthentication({authBytes:r}));this.sessionLifetime=o.sessionLifetimeMs||f;if(!e){return}const{authBytes:s}=o;const n=await e.decode(s);return e.parse(n)}return this.connection.sendAuthRequest({request:t,response:e})};if(!this.connection.sasl.authenticationProvider&&Object.keys(l).includes(t)){this.connection.sasl.authenticationProvider=l[t](this.connection.sasl)}await this.connection.sasl.authenticationProvider({host:this.connection.host,port:this.connection.port,logger:this.logger.namespace(`SaslAuthenticator-${t}`),saslAuthenticate:saslAuthenticate}).authenticate()}}},8333:function(t,e,r){const{request:o}=r(4846);const{KafkaJSSASLAuthenticationError:s}=r(1331);const oauthBearerAuthenticatorProvider=t=>({host:e,port:r,logger:n,saslAuthenticate:i})=>({authenticate:async()=>{const{oauthBearerProvider:a}=t;if(a==null){throw new s("SASL OAUTHBEARER: Missing OAuth bearer token provider")}const c=await a();if(c.value==null){throw new s("SASL OAUTHBEARER: Invalid OAuth bearer token")}const u=`${e}:${r}`;try{n.debug("Authenticate with SASL OAUTHBEARER",{broker:u});await i({request:await o(t,c)});n.debug("SASL OAUTHBEARER authentication successful",{broker:u})}catch(t){const e=new s(`SASL OAUTHBEARER authentication failed: ${t.message}`);n.error(e.message,{broker:u});throw e}}});t.exports=oauthBearerAuthenticatorProvider},9947:function(t,e,r){const{request:o,response:s}=r(2166);const{KafkaJSSASLAuthenticationError:n}=r(1331);const plainAuthenticatorProvider=t=>({host:e,port:r,logger:i,saslAuthenticate:a})=>({authenticate:async()=>{if(t.username==null||t.password==null){throw new n("SASL Plain: Invalid username or password")}const c=`${e}:${r}`;try{i.debug("Authenticate with SASL PLAIN",{broker:c});await a({request:o(t),response:s});i.debug("SASL PLAIN authentication successful",{broker:c})}catch(t){const e=new n(`SASL PLAIN authentication failed: ${t.message}`);i.error(e.message,{broker:c});throw e}}});t.exports=plainAuthenticatorProvider},1202:function(t,e,r){const o=r(6113);const s=r(3738);const{KafkaJSSASLAuthenticationError:n,KafkaJSNonRetriableError:i}=r(1331);const a="n,,";const c=/=/g;const u=/,/g;const p=/\+/g;const d=/\//g;const l=/=+$/;const f="Client Key";const h="Server Key";const m={SHA256:{length:32,type:"sha256",minIterations:4096},SHA512:{length:64,type:"sha512",minIterations:4096}};const encode64=t=>Buffer.from(t).toString("base64");class SCRAM{static sanitizeString(t){return t.replace(c,"=3D").replace(u,"=2C")}static nonce(){return o.randomBytes(16).toString("base64").replace(p,"-").replace(d,"_").replace(l,"").toString("ascii")}static hi(t,e,r,s){return new Promise(((n,i)=>{o.pbkdf2(t,e,r,s.length,s.type,((t,e)=>t?i(t):n(e)))}))}static xor(t,e){const r=Buffer.from(t);const o=Buffer.from(e);const s=Buffer.byteLength(r);if(s!==Buffer.byteLength(o)){throw new i("Buffers must be of the same length")}const n=[];for(let t=0;t<s;t++){n.push(r[t]^o[t])}return Buffer.from(n)}constructor(t,e,r,o,s,n){this.sasl=t;this.host=e;this.port=r;this.logger=o;this.saslAuthenticate=s;this.digestDefinition=n;const i=n.type.toUpperCase();this.PREFIX=`SASL SCRAM ${i} authentication`;this.currentNonce=SCRAM.nonce()}async authenticate(){const{PREFIX:t}=this;const e=`${this.host}:${this.port}`;if(this.sasl.username==null||this.sasl.password==null){throw new n(`${this.PREFIX}: Invalid username or password`)}try{this.logger.debug("Exchanging first client message",{broker:e});const r=await this.sendClientFirstMessage();this.logger.debug("Sending final message",{broker:e});const o=await this.sendClientFinalMessage(r);if(o.e){throw new Error(o.e)}const s=await this.serverKey(r);const n=this.serverSignature(s,r);if(o.v!==n){throw new Error("Invalid server signature in server final message")}this.logger.debug(`${t} successful`,{broker:e})}catch(r){const o=new n(`${t} failed: ${r.message}`);this.logger.error(o.message,{broker:e});throw o}}async sendClientFirstMessage(){const t=`${a}${this.firstMessageBare()}`;const e=s.firstMessage.request({clientFirstMessage:t});const r=s.firstMessage.response;return this.saslAuthenticate({request:e,response:r})}async sendClientFinalMessage(t){const{PREFIX:e}=this;const r=parseInt(t.i,10);const{minIterations:o}=this.digestDefinition;if(!t.r.startsWith(this.currentNonce)){throw new n(`${e} failed: Invalid server nonce, it does not start with the client nonce`)}if(r<o){throw new n(`${e} failed: Requested iterations ${r} is less than the minimum ${o}`)}const i=this.finalMessageWithoutProof(t);const a=await this.clientProof(t);const c=`${i},p=${a}`;const u=s.finalMessage.request({finalMessage:c});const p=s.finalMessage.response;return this.saslAuthenticate({request:u,response:p})}async clientProof(t){const e=await this.clientKey(t);const r=this.H(e);const o=this.clientSignature(r,t);return encode64(SCRAM.xor(e,o))}async clientKey(t){const e=await this.saltPassword(t);return this.HMAC(e,f)}async serverKey(t){const e=await this.saltPassword(t);return this.HMAC(e,h)}clientSignature(t,e){return this.HMAC(t,this.authMessage(e))}serverSignature(t,e){return encode64(this.HMAC(t,this.authMessage(e)))}authMessage(t){return[this.firstMessageBare(),t.original,this.finalMessageWithoutProof(t)].join(",")}async saltPassword(t){const e=Buffer.from(t.s,"base64");const r=parseInt(t.i,10);return SCRAM.hi(this.encodedPassword(),e,r,this.digestDefinition)}firstMessageBare(){return`n=${this.encodedUsername()},r=${this.currentNonce}`}finalMessageWithoutProof(t){const e=t.r;return`c=${encode64(a)},r=${e}`}encodedUsername(){const{username:t}=this.sasl;return SCRAM.sanitizeString(t).toString("utf-8")}encodedPassword(){const{password:t}=this.sasl;return t.toString("utf-8")}H(t){return o.createHash(this.digestDefinition.type).update(t).digest()}HMAC(t,e){return o.createHmac(this.digestDefinition.type,t).update(e).digest()}}t.exports={DIGESTS:m,SCRAM:SCRAM}},1780:function(t,e,r){const{SCRAM:o,DIGESTS:s}=r(1202);const scram256AuthenticatorProvider=t=>({host:e,port:r,logger:n,saslAuthenticate:i})=>{const a=new o(t,e,r,n,i,s.SHA256);return{authenticate:async()=>await a.authenticate()}};t.exports=scram256AuthenticatorProvider},9263:function(t,e,r){const{SCRAM:o,DIGESTS:s}=r(1202);const scram512AuthenticatorProvider=t=>({host:e,port:r,logger:n,saslAuthenticate:i})=>{const a=new o(t,e,r,n,i,s.SHA512);return{authenticate:async()=>await a.authenticate()}};t.exports=scram512AuthenticatorProvider},964:function(t,e,r){const o=r(6267);const s=r(4091);const n=r(9085);const i=r(9408);const{KafkaJSBrokerNotFound:a,KafkaJSProtocolError:c}=r(1331);const{keys:u,assign:p,values:d}=Object;const hasBrokerBeenReplaced=(t,{host:e,port:r,rack:o})=>t.connectionPool.host!==e||t.connectionPool.port!==r||t.connectionPool.rack!==o;t.exports=class BrokerPool{constructor({connectionPoolBuilder:t,logger:e,retry:r,allowAutoTopicCreation:n,authenticationTimeout:i,metadataMaxAge:a}){this.rootLogger=e;this.connectionPoolBuilder=t;this.metadataMaxAge=a||0;this.logger=e.namespace("BrokerPool");this.retrier=s(p({},r));this.createBroker=t=>new o({allowAutoTopicCreation:n,authenticationTimeout:i,...t});this.brokers={};this.seedBroker=undefined;this.metadata=null;this.metadataExpireAt=null;this.versions=null}hasConnectedBrokers(){const t=d(this.brokers);return!!t.find((t=>t.isConnected()))||(this.seedBroker?this.seedBroker.isConnected():false)}async createSeedBroker(){if(this.seedBroker){await this.seedBroker.disconnect()}const t=await this.connectionPoolBuilder.build();this.seedBroker=this.createBroker({connectionPool:t,logger:this.rootLogger})}async connect(){if(this.hasConnectedBrokers()){return}if(!this.seedBroker){await this.createSeedBroker()}return this.retrier((async(t,e,r)=>{try{await this.seedBroker.connect();this.versions=this.seedBroker.versions}catch(o){if(o.name==="KafkaJSConnectionError"||o.type==="ILLEGAL_SASL_STATE"){await this.createSeedBroker();this.logger.error(`Failed to connect to seed broker, trying another broker from the list: ${o.message}`,{retryCount:e,retryTime:r})}else{this.logger.error(o.message,{retryCount:e,retryTime:r})}if(o.retriable)throw o;t(o)}}))}async disconnect(){this.seedBroker&&await this.seedBroker.disconnect();await Promise.all(d(this.brokers).map((t=>t.disconnect())));this.brokers={};this.metadata=null;this.versions=null}removeBroker({host:t,port:e}){const r=d(this.brokers).find((r=>r.connectionPool.host===t&&r.connectionPool.port===e));if(r){delete this.brokers[r.nodeId];this.metadataExpireAt=null;if(this.seedBroker.nodeId===r.nodeId){this.seedBroker=n(d(this.brokers))[0]}}}async refreshMetadata(t){const e=await this.findConnectedBroker();const{host:r,port:o}=this.seedBroker.connectionPool;return this.retrier((async(s,n,a)=>{try{this.metadata=await e.metadata(t);this.metadataExpireAt=Date.now()+this.metadataMaxAge;const s=[];this.brokers=await this.metadata.brokers.reduce((async(t,{nodeId:e,host:n,port:i,rack:a})=>{const c=await t;if(c[e]){if(!hasBrokerBeenReplaced(c[e],{host:n,port:i,rack:a})){return c}s.push(c[e])}if(n===r&&i===o){this.seedBroker.nodeId=e;this.seedBroker.connectionPool.rack=a;return p(c,{[e]:this.seedBroker})}return p(c,{[e]:this.createBroker({logger:this.rootLogger,versions:this.versions,connectionPool:await this.connectionPoolBuilder.build({host:n,port:i,rack:a}),nodeId:e})})}),this.brokers);const n=this.metadata.brokers.map((({nodeId:t})=>`${t}`)).sort();const a=u(this.brokers).sort();const c=i(a,n);const d=c.map((t=>{const e=this.brokers[t];return e.disconnect().then((()=>{delete this.brokers[t]}))}));const l=s.map((t=>t.disconnect()));await Promise.all([...d,...l])}catch(t){if(t.type==="LEADER_NOT_AVAILABLE"){throw t}s(t)}}))}async refreshMetadataIfNecessary(t){const e=this.metadata==null||this.metadataExpireAt==null||Date.now()>this.metadataExpireAt||!t.every((t=>this.metadata.topicMetadata.some((e=>e.topic===t))));if(e){return this.refreshMetadata(t)}}getNodeIds(){return u(this.brokers)}async findBroker({nodeId:t}){const e=this.brokers[t];if(!e){throw new a(`Broker ${t} not found in the cached metadata`)}await this.connectBroker(e);return e}async withBroker(t){const e=n(u(this.brokers));if(e.length===0){throw new a("No brokers in the broker pool")}for(const r of e){const e=await this.findBroker({nodeId:r});try{return await t({nodeId:r,broker:e})}catch(t){}}return null}async findConnectedBroker(){const t=n(u(this.brokers));const e=t.find((t=>this.brokers[t].isConnected()));if(e){return await this.findBroker({nodeId:e})}for(const e of t){try{return await this.findBroker({nodeId:e})}catch(t){}}await this.connect();return this.seedBroker}async connectBroker(t){if(t.isConnected()){return}return this.retrier((async(e,r,o)=>{try{await t.connect()}catch(s){if(s.name==="KafkaJSConnectionError"||s.type==="ILLEGAL_SASL_STATE"){await t.disconnect()}if(s.name==="KafkaJSConnectionError"){return e(s)}if(s.type==="ILLEGAL_SASL_STATE"){t.connectionPool=await this.connectionPoolBuilder.build({host:t.connectionPool.host,port:t.connectionPool.port,rack:t.connectionPool.rack});this.logger.error(`Failed to connect to broker, reconnecting`,{retryCount:r,retryTime:o});throw new c(s,{retriable:true})}if(s.retriable)throw s;this.logger.error(s,{retryCount:r,retryTime:o,stack:s.stack});e(s)}}))}}},9145:function(t,e,r){const{KafkaJSConnectionError:o,KafkaJSNonRetriableError:s}=r(1331);const n=r(3853);t.exports=({socketFactory:t,brokers:e,ssl:r,sasl:i,clientId:a,requestTimeout:c,enforceRequestTimeout:u,connectionTimeout:p,maxInFlightRequests:d,logger:l,instrumentationEmitter:f=null,reauthenticationThreshold:h})=>{let m=0;const isValidBroker=t=>t&&typeof t==="string"&&t.length>0;const validateBrokers=t=>{if(!t){throw new s(`Failed to connect: brokers should not be null`)}if(Array.isArray(t)){if(!t.length){throw new s(`Failed to connect: brokers array is empty`)}t.forEach(((t,e)=>{if(!isValidBroker(t)){throw new s(`Failed to connect: broker at index ${e} is invalid "${typeof t}"`)}}))}};const getBrokers=async()=>{let t;if(typeof e==="function"){try{t=await e()}catch(t){const e=new o(`Failed to connect: "config.brokers" threw: ${t.message}`);e.stack=`${e.name}\n Caused by: ${t.stack}`;throw e}}else{t=e}validateBrokers(t);return t};return{build:async({host:e,port:o,rack:s}={})=>{if(!e){const t=await getBrokers();const r=t[m++%t.length];e=r.split(":")[0];o=Number(r.split(":")[1])}return new n({host:e,port:o,rack:s,sasl:i,ssl:r,clientId:a,socketFactory:t,connectionTimeout:p,requestTimeout:c,enforceRequestTimeout:u,maxInFlightRequests:d,instrumentationEmitter:f,logger:l,reauthenticationThreshold:h})}}}},4428:function(t,e,r){const o=r(964);const s=r(6249);const n=r(9964);const i=r(4091);const a=r(9145);const{EARLIEST_OFFSET:c,LATEST_OFFSET:u}=r(8691);const{KafkaJSError:p,KafkaJSBrokerNotFound:d,KafkaJSMetadataNotLoaded:l,KafkaJSTopicMetadataNotLoaded:f,KafkaJSGroupCoordinatorNotFound:h}=r(1331);const m=r(8853);const{keys:g}=Object;const mergeTopics=(t,{topic:e,partitions:r})=>({...t,[e]:[...t[e]||[],...r]});const I={CONNECT:Symbol("private:Cluster:connect"),REFRESH_METADATA:Symbol("private:Cluster:refreshMetadata"),REFRESH_METADATA_IF_NECESSARY:Symbol("private:Cluster:refreshMetadataIfNecessary"),FIND_CONTROLLER_BROKER:Symbol("private:Cluster:findControllerBroker")};t.exports=class Cluster{constructor({logger:t,socketFactory:e,brokers:r,ssl:c,sasl:u,clientId:p,connectionTimeout:f,authenticationTimeout:h,reauthenticationThreshold:m,requestTimeout:g=3e4,enforceRequestTimeout:y,metadataMaxAge:w,retry:T,allowAutoTopicCreation:E,maxInFlightRequests:S,isolationLevel:b,instrumentationEmitter:A=null,offsets:O=new Map}){this.rootLogger=t;this.logger=t.namespace("Cluster");this.retrier=i(T);this.connectionPoolBuilder=a({logger:t,instrumentationEmitter:A,socketFactory:e,brokers:r,ssl:c,sasl:u,clientId:p,connectionTimeout:f,requestTimeout:g,enforceRequestTimeout:y,maxInFlightRequests:S,reauthenticationThreshold:m});this.targetTopics=new Set;this.mutatingTargetTopics=new s({description:`updating target topics`,timeout:g});this.isolationLevel=b;this.brokerPool=new o({connectionPoolBuilder:this.connectionPoolBuilder,logger:this.rootLogger,retry:T,allowAutoTopicCreation:E,authenticationTimeout:h,metadataMaxAge:w});this.committedOffsetsByGroup=O;this[I.CONNECT]=n((async()=>await this.brokerPool.connect()));this[I.REFRESH_METADATA]=n((async()=>await this.brokerPool.refreshMetadata(Array.from(this.targetTopics))));this[I.REFRESH_METADATA_IF_NECESSARY]=n((async()=>await this.brokerPool.refreshMetadataIfNecessary(Array.from(this.targetTopics))));this[I.FIND_CONTROLLER_BROKER]=n((async()=>{const{metadata:t}=this.brokerPool;if(!t||t.controllerId==null){throw new l("Topic metadata not loaded")}const e=await this.findBroker({nodeId:t.controllerId});if(!e){throw new d(`Controller broker with id ${t.controllerId} not found in the cached metadata`)}return e}))}isConnected(){return this.brokerPool.hasConnectedBrokers()}async connect(){await this[I.CONNECT]()}async disconnect(){await this.brokerPool.disconnect()}removeBroker({host:t,port:e}){this.brokerPool.removeBroker({host:t,port:e})}async refreshMetadata(){await this[I.REFRESH_METADATA]()}async refreshMetadataIfNecessary(){await this[I.REFRESH_METADATA_IF_NECESSARY]()}async metadata({topics:t=[]}={}){return this.retrier((async(e,r,o)=>{try{await this.brokerPool.refreshMetadataIfNecessary(t);return this.brokerPool.withBroker((async({broker:e})=>e.metadata(t)))}catch(t){if(t.type==="LEADER_NOT_AVAILABLE"){throw t}e(t)}}))}async addTargetTopic(t){return this.addMultipleTargetTopics([t])}async addMultipleTargetTopics(t){await this.mutatingTargetTopics.acquire();try{const e=this.targetTopics.size;const r=new Set(this.targetTopics);for(const e of t){this