@autofleet/kafka 0.6.3 → 0.7.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- let e=require(`node:timers/promises`);var t=class extends Error{constructor(...e){super(...e),this.name=`KafkaError`}};const n=`x-timestamp`,r=`autofleet-kafka-producer`;var i=class{constructor(e,t,n){this.name=e,this.config=t,this.logger=n,this.producer=null,this._isConnected=!1,this.initPromise=null,this._lastPingAt=null,this._lastPingSucceededAt=null,this._lastError=null,this._clusterId=null,this._brokerCount=0}async#e(){let{Producer:e,stringSerializers:t}=await import(`@platformatic/kafka`);this.producer=new e({bootstrapBrokers:this.config.brokers,clientId:this.config.clientId||`${r}-${this.name}`,serializers:t,autocreateTopics:this.config.autoCreateTopics??!1,sasl:this.config.sasl,tls:this.config.tls}),this.logger.info(`Kafka: [${this.name}] Initialized producer`,{clientId:this.config.clientId||`${r}-${this.name}`,brokers:this.config.brokers}),this.initPromise=null}async initialize(){this.producer||(this.initPromise??=this.#e(),await this.initPromise)}get isConnected(){return this._isConnected}getHealth(){return{name:this.name,enabled:!0,isConnected:this._isConnected,lastPingAt:this._lastPingAt,lastPingSucceededAt:this._lastPingSucceededAt,lastError:this._lastError,clusterId:this._clusterId,brokerCount:this._brokerCount,brokers:this.config.brokers}}buildConnectionErrorMessage(e){let t=this.config.brokers.join(`, `),n=this.config.brokers[0],[i,a]=n?n.split(`:`):[``,``];return`[${this.name}] Failed to connect to Kafka brokers: ${e.message}\n\nPossible causes:\n 1. Brokers are unreachable: ${t}\n 2. Network connectivity issues\n 3. Firewall blocking ports\n 4. ${this.config.sasl?`SASL authentication failed`:`Authentication not configured but required`}\n\nTroubleshooting:\n - Verify brokers are running and accessible\n`+(i&&a?` - Test connectivity: nc -zv ${i} ${a}\n`:``)+` - Check network policies and firewall rules\n ${this.config.sasl?`- Verify SASL credentials are correct
2
- `:``}\nCurrent configuration:\n Brokers: ${t}\n Client ID: ${this.config.clientId||`${r}-${this.name}`}\n SASL: ${this.config.sasl?`enabled (${this.config.sasl.mechanism})`:`disabled`}\n SASL Password: ${this.config.sasl?this.config.sasl.password?`****`:`not set`:`N/A`}\n Auto-create topics: ${this.config.autoCreateTopics??!1}`}async ping(n){if(this._lastPingAt=Date.now(),!(this._isConnected&&!n?.force)){await this.initialize();try{let t=n?.timeout??5e3,r=Error(`Connection timeout after ${t}ms`),i=(0,e.setTimeout)(t).then(()=>{throw r}),a=await Promise.race([this.producer.metadata({topics:[]}),i]);this._isConnected=!0,this._lastPingSucceededAt=Date.now(),this._clusterId=a.id,this._brokerCount=a.brokers.size,this._lastError=null,this.logger.info(`Kafka: [${this.name}] Successfully connected to cluster`,{clusterId:a.id,brokers:a.brokers.size,duration:Date.now()-this._lastPingAt})}catch(e){let n=e;throw this._isConnected=!1,this._lastError={message:n.message,timestamp:Date.now(),stack:n.stack},this.logger.error(`Kafka: [${this.name}] Failed to connect to brokers`,{error:n.message,duration:Date.now()-this._lastPingAt}),new t(this.buildConnectionErrorMessage(n),{cause:n})}}}async publish(e,r,i){if(!e)throw new t(`[${this.name}] Topic name is required`);await this.ping();try{let t={[n]:Date.now().toString(),...i?.headers},a=await this.producer.send({messages:[{topic:e,value:JSON.stringify(r),key:i?.key,partition:i?.partition,headers:t}]});return this.logger.debug(`Kafka: [${this.name}] Published message to topic ${e}`,{topic:e}),[{topic:e,partition:i?.partition??0,offset:a?.offsets?.[0]?.offset?.toString()??`0`}]}catch(n){let i=n;throw this.logger.error(`Kafka: [${this.name}] Error publishing to topic ${e}`,{error:n,value:r}),new t(`[${this.name}] Failed to publish to topic ${e}: ${i.message||`Unknown error`}`,{cause:n})}}async publishBatch(e,r){if(!e)throw new t(`[${this.name}] Topic name is required`);if(!r.messages||r.messages.length===0)throw new t(`[${this.name}] At least one message is required`);await this.ping();try{let t=r.messages.map(t=>({topic:e,value:JSON.stringify(t.value),key:t.key,partition:t.partition,headers:{[n]:Date.now().toString(),...t.headers}})),i=await this.producer.send({messages:t});return this.logger.debug(`Kafka: [${this.name}] Published ${t.length} messages to topic ${e}`,{topic:e,count:t.length}),t.map((t,n)=>({topic:e,partition:t.partition??0,offset:i?.offsets?.[n]?.offset?.toString()??n.toString()}))}catch(n){let r=n;throw this.logger.error(`Kafka: [${this.name}] Error publishing batch to topic ${e}`,{error:n}),new t(`[${this.name}] Failed to publish batch to topic ${e}: ${r.message||`Unknown error`}`,{cause:n})}}async disconnect(){if(!this.producer||!this._isConnected){this.logger.debug(`Kafka: [${this.name}] Producer already disconnected`);return}this.logger.info(`Kafka: [${this.name}] Disconnecting producer`);try{await this.producer.close(),this._isConnected=!1,this.logger.info(`Kafka: [${this.name}] Producer disconnected successfully`)}catch(e){throw this.logger.error(`Kafka: [${this.name}] Error disconnecting producer`,{error:e}),new t(`[${this.name}] Failed to disconnect producer: ${e.message}`,{cause:e})}}},a=class{constructor(e,t,n){this.name=e,this.brokers=t,this.logger=n,this.isConnected=!0}getHealth(){return{name:this.name,enabled:!1,isConnected:!0,lastPingAt:null,lastPingSucceededAt:null,lastError:null,clusterId:`mock-cluster`,brokerCount:0,brokers:this.brokers}}async ping(){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping ping`),Promise.resolve()}async publish(e,t,n){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping publish to topic: ${e}`,{value:t}),Promise.resolve([{topic:e,partition:0,offset:`0`}])}async publishBatch(e,t){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${e}`,{messageCount:t.messages.length}),Promise.resolve(t.messages.map(()=>({topic:e,partition:0,offset:`0`})))}async disconnect(){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping disconnect`),Promise.resolve()}},o=class e{constructor(e){if(this.producers=new Map,this.topics=new Map,this.gracefulShutdownStarted=!1,this.fatalError=null,this.lastReadinessCheck=null,this.logger=e.logger,this.enabled=e.enabled??!0,this.healthCheckTimeoutMs=e.healthCheckTimeoutMs??5e3,this.healthCheckCacheMs=e.healthCheckCacheMs??1e3,this.bootstrapTimeoutMs=e.bootstrapTimeoutMs??3e4,this.strictBootstrap=e.strictBootstrap??!0,e.topics)for(let[t,n]of Object.entries(e.topics))this.topics.set(t,n);if(this.logger.info(`Kafka: Initialized KafkaManager`,{enabled:this.enabled,producerCount:Object.keys(e.producers??{}).length,producers:Object.keys(e.producers??{}),topicCount:this.topics.size,topics:Array.from(this.topics.keys())}),this.enabled)for(let[n,r]of Object.entries(e.producers??{})){if(!r.brokers||r.brokers.length===0)throw new t(`[${n}] At least one broker is required`);this.producers.set(n,new i(n,r,this.logger))}else{for(let[t,n]of Object.entries(e.producers??{}))this.producers.set(t,new a(t,n.brokers,this.logger));this.producers.size>0&&this.logger.info(`Kafka: Created mock producers (Kafka disabled)`)}e.dontGracefulShutdown||this.setupGracefulShutdown()}static create(n){if(n.topics){let e=Object.keys(n.producers??{});for(let[r,i]of Object.entries(n.topics))if(!e.includes(i.producer))throw new t(`Topic '${r}' references unknown producer '${i.producer}'. Available producers: ${e.join(`, `)}`)}return new e(n)}setupGracefulShutdown(){this.logger.info(`Kafka: [graceful-shutdown] adding graceful shutdown for process.pid ${process.pid}`),process.on(`SIGTERM`,async()=>{await this.gracefulShutdown(`SIGTERM`)}),process.on(`SIGINT`,async()=>{await this.gracefulShutdown(`SIGINT`)})}async gracefulShutdown(e){if(!this.gracefulShutdownStarted){this.gracefulShutdownStarted=!0,this.logger.info(`Kafka: [graceful-shutdown] received ${e}! Disconnecting all producers...`);try{await this.disconnect(),this.logger.info(`Kafka: [graceful-shutdown] all producers disconnected successfully`)}catch(e){throw this.logger.error(`Kafka: [graceful-shutdown] error during shutdown`,{error:e}),e}}}get isEnabled(){return this.enabled}get producerNames(){return Array.from(this.producers.keys())}hasProducer(e){return this.producers.has(e)}getProducer(e){let n=this.producers.get(e);if(!n)throw new t(`Producer '${e}' not found. Available producers: ${this.producerNames.join(`, `)}`);return n}async bootstrap(e){let n=Date.now(),r=e?.timeoutMs??this.bootstrapTimeoutMs,i=e?.strict??this.strictBootstrap,a=e?.producers??this.producerNames;this.logger.info(`Kafka: Starting bootstrap`,{producers:a,timeout:r,strict:i});let o={},s=[];for(let e of a){let t=this.producers.get(e);if(!t){o[e]={success:!1,duration:0,error:`Producer '${e}' not found`},s.push(`Producer '${e}' not found`);continue}let n=Date.now();try{await t.ping({timeout:r,force:!0});let i=t.getHealth();o[e]={success:!0,duration:Date.now()-n,clusterId:i.clusterId??void 0,brokerCount:i.brokerCount},this.logger.info(`Kafka: [${e}] Bootstrap successful`,{duration:Date.now()-n,clusterId:i.clusterId,brokerCount:i.brokerCount})}catch(t){let r=t;o[e]={success:!1,duration:Date.now()-n,error:r.message},s.push(`[${e}] ${r.message}`),this.logger.error(`Kafka: [${e}] Bootstrap failed`,{duration:Date.now()-n,error:r.message})}}let c=Date.now()-n,l=Object.values(o).filter(e=>e.success).length,u=Object.keys(o).length,d=i?l===u:l>0,f={success:d,duration:c,results:o};if(d)this.logger.info(`Kafka: Bootstrap completed successfully`,{duration:c,successCount:l,totalCount:u});else{let e=`Kafka bootstrap failed (${l}/${u} producers succeeded)\n\nFailures:\n${s.map(e=>` - ${e}`).join(`
3
- `)}\n\nConfiguration:\n Strict mode: ${i}\n Timeout: ${r}ms\n Producers attempted: ${a.join(`, `)}`;throw this.logger.error(`Kafka: Bootstrap failed`,{duration:c,successCount:l,totalCount:u,errors:s}),new t(e)}return f}getHealth(){let e={};for(let[t,n]of this.producers.entries())e[t]=n.getHealth();return e}getProducerHealth(e){return this.getProducer(e).getHealth()}isLive(){return!this.fatalError&&!this.gracefulShutdownStarted}async ping(){let e=Array.from(this.producers.values(),e=>e.ping());await Promise.all(e)}async pingProducer(e){await this.getProducer(e).ping()}async isReady(e){if(!this.enabled)return!0;let t=Date.now();if(!(e?.force??!1)&&this.lastReadinessCheck){let e=t-this.lastReadinessCheck.timestamp;if(e<this.healthCheckCacheMs)return this.logger.debug(`Kafka: Using cached readiness result`,{result:this.lastReadinessCheck.result,age:e}),this.lastReadinessCheck.result}try{let n=e?.timeout??this.healthCheckTimeoutMs,r=Array.from(this.producers.values()).map(e=>e.ping({timeout:n,force:!0}));return await Promise.all(r),this.lastReadinessCheck={result:!0,timestamp:t},!0}catch(e){return this.lastReadinessCheck={result:!1,timestamp:t},this.logger.warn(`Kafka: Readiness check failed`,{error:e.message}),!1}}getConnectionStatus(){let e={};for(let[t,n]of this.producers.entries()){let r=n.getHealth();e[t]={connected:r.isConnected,lastSuccessAt:r.lastPingSucceededAt,lastError:r.lastError?.message??null}}return e}getTopicDefinition(e){let n=this.topics.get(e);if(!n)throw new t(`Topic '${e}' not found. Available topics: ${Array.from(this.topics.keys()).join(`, `)}`);return n}async publish(e,n,r){let i=this.getTopicDefinition(e);if(!r?.skipValidation)try{i.schema.parse(n)}catch(r){throw this.logger.error(`Kafka: Validation failed for topic '${e}'`,{error:r,value:n}),new t(`Validation failed for topic '${e}': ${r.message}`,{cause:r})}return this.getProducer(i.producer).publish(e,n,r)}async publishBatch(e,n){let r=this.getTopicDefinition(e);if(!n.skipValidation)for(let i=0;i<n.messages.length;i++)try{r.schema.parse(n.messages[i].value)}catch(r){throw this.logger.error(`Kafka: Validation failed for topic '${e}' message ${i}`,{error:r,value:n.messages[i].value}),new t(`Validation failed for topic '${e}' message ${i}: ${r.message}`,{cause:r})}return this.getProducer(r.producer).publishBatch(e,n)}async disconnect(){let e=Array.from(this.producers.values()).map(e=>e.disconnect());await Promise.all(e),this.logger.info(`Kafka: All producers disconnected`)}async disconnectProducer(e){await this.getProducer(e).disconnect()}};exports.KafkaError=t,exports.KafkaManager=o;
1
+ var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`node:timers/promises`),l=require(`google-auth-library`),u=require(`node:crypto`);u=s(u);var d=class extends Error{constructor(...e){super(...e),this.name=`KafkaError`}};const f=`x-timestamp`,p=`autofleet-kafka-producer`,m=60,h=e=>Buffer.from(e,`utf8`).toString(`base64`).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/g,``),g=e=>u.default.createHash(`sha256`).update(e).digest(`hex`).slice(0,12),_=h(JSON.stringify({typ:`JWT`,alg:`GOOG_OAUTH2_TOKEN`})),v=(e={})=>{let t=e.logger,n=e.refreshSkewSec??60,r=e.enableMetadataLookup??!0,i=new l.GoogleAuth({scopes:[`https://www.googleapis.com/auth/cloud-platform`]}),a=null,o=e.serviceAccountEmail??null,s=null,c=null,u=async()=>(a??=i.getClient(),a),d=async()=>{try{let e=await fetch(`http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/email`,{headers:{"Metadata-Flavor":`Google`}});return e.ok?(await e.text()).trim():null}catch(e){return t?.warn?.(`ManagedKafkaAuth: metadata email lookup failed`,{err:String(e)}),null}},f=async()=>{if(o)return o;try{let e=await i.getCredentials();if(e?.client_email)return o=e.client_email,o}catch(e){t?.warn?.(`ManagedKafkaAuth: getCredentials failed`,{err:String(e)})}if(r){let e=await d();if(e)return o=e,o}if(process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL)return o=process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,o;throw Error(`ManagedKafkaAuth: cannot determine service account email (set GOOGLE_SERVICE_ACCOUNT_EMAIL)`)};return async()=>{let e=Math.floor(Date.now()/1e3);if(s&&c&&e<c-n)return s;let r=await u(),{token:i}=await r.getAccessToken();if(!i)throw Error(`ManagedKafkaAuth: no access token from ADC`);let a=r.credentials?.expiry_date;if(!a)throw Error(`ManagedKafkaAuth: no expiry_date on ADC credentials`);let o=await f(),l=Math.floor(a/1e3),d=`${_}.${h(JSON.stringify({exp:l,iss:`Google`,iat:e,sub:o}))}.${h(i)}`;return s=d,c=l,t?.info?.(`ManagedKafkaAuth: token generated`,{sub:o,iat:e,exp:l,fp:g(d)}),d}};var y=class{constructor(e,t,n){this.name=e,this.config=t,this.logger=n,this.producer=null,this._isConnected=!1,this.initPromise=null,this._lastPingAt=null,this._lastPingSucceededAt=null,this._lastError=null,this._clusterId=null,this._brokerCount=0}async#e(){let{Producer:e,stringSerializers:t}=await import(`@platformatic/kafka`),n=this.config.oauthBearerConfig??{logger:this.logger};this.producer=new e({bootstrapBrokers:this.config.brokers,clientId:this.config.clientId||`${p}-${this.name}`,serializers:t,autocreateTopics:this.config.autoCreateTopics??!1,sasl:this.config.sasl??{mechanism:`OAUTHBEARER`,token:v(n)},tls:this.config.tls??{}}),this.logger.info(`Kafka: [${this.name}] Initialized producer`,{clientId:this.config.clientId||`${p}-${this.name}`,brokers:this.config.brokers}),this.initPromise=null}async initialize(){this.producer||(this.initPromise??=this.#e(),await this.initPromise)}get isConnected(){return this._isConnected}getHealth(){return{name:this.name,enabled:!0,isConnected:this._isConnected,lastPingAt:this._lastPingAt,lastPingSucceededAt:this._lastPingSucceededAt,lastError:this._lastError,clusterId:this._clusterId,brokerCount:this._brokerCount,brokers:this.config.brokers}}buildConnectionErrorMessage(e){let t=this.config.brokers.join(`, `),n=this.config.brokers[0],[r,i]=n?n.split(`:`):[``,``];return`[${this.name}] Failed to connect to Kafka brokers: ${e.message}\n\nPossible causes:\n 1. Brokers are unreachable: ${t}\n 2. Network connectivity issues\n 3. Firewall blocking ports\n 4. ${this.config.sasl?`SASL authentication failed`:`Authentication not configured but required`}\n\nTroubleshooting:\n - Verify brokers are running and accessible\n`+(r&&i?` - Test connectivity: nc -zv ${r} ${i}\n`:``)+` - Check network policies and firewall rules\n ${this.config.sasl?`- Verify SASL credentials are correct
2
+ `:``}\nCurrent configuration:\n Brokers: ${t}\n Client ID: ${this.config.clientId||`${p}-${this.name}`}\n SASL: ${this.config.sasl?`enabled (${this.config.sasl.mechanism})`:`disabled`}\n SASL Password: ${this.config.sasl?this.config.sasl.password?`****`:`not set`:`N/A`}\n Auto-create topics: ${this.config.autoCreateTopics??!1}`}async ping(e){if(this._lastPingAt=Date.now(),!(this._isConnected&&!e?.force)){await this.initialize();try{let t=e?.timeout??5e3,n=Error(`Connection timeout after ${t}ms`),r=(0,c.setTimeout)(t).then(()=>{throw n}),i=await Promise.race([this.producer.metadata({topics:[]}),r]);this._isConnected=!0,this._lastPingSucceededAt=Date.now(),this._clusterId=i.id,this._brokerCount=i.brokers.size,this._lastError=null,this.logger.info(`Kafka: [${this.name}] Successfully connected to cluster`,{clusterId:i.id,brokers:i.brokers.size,duration:Date.now()-this._lastPingAt})}catch(e){let t=e;throw this._isConnected=!1,this._lastError={message:t.message,timestamp:Date.now(),stack:t.stack},this.logger.error(`Kafka: [${this.name}] Failed to connect to brokers`,{error:t.message,duration:Date.now()-this._lastPingAt}),new d(this.buildConnectionErrorMessage(t),{cause:t})}}}async publish(e,t,n){if(!e)throw new d(`[${this.name}] Topic name is required`);await this.ping();try{let r={[f]:Date.now().toString(),...n?.headers},i=await this.producer.send({messages:[{topic:e,value:JSON.stringify(t),key:n?.key,partition:n?.partition,headers:r}]});return this.logger.debug(`Kafka: [${this.name}] Published message to topic ${e}`,{topic:e}),[{topic:e,partition:n?.partition??0,offset:i?.offsets?.[0]?.offset?.toString()??`0`}]}catch(n){let r=n;throw this.logger.error(`Kafka: [${this.name}] Error publishing to topic ${e}`,{error:n,value:t}),new d(`[${this.name}] Failed to publish to topic ${e}: ${r.message||`Unknown error`}`,{cause:n})}}async publishBatch(e,t){if(!e)throw new d(`[${this.name}] Topic name is required`);if(!t.messages||t.messages.length===0)throw new d(`[${this.name}] At least one message is required`);await this.ping();try{let n=t.messages.map(t=>({topic:e,value:JSON.stringify(t.value),key:t.key,partition:t.partition,headers:{[f]:Date.now().toString(),...t.headers}})),r=await this.producer.send({messages:n});return this.logger.debug(`Kafka: [${this.name}] Published ${n.length} messages to topic ${e}`,{topic:e,count:n.length}),n.map((t,n)=>({topic:e,partition:t.partition??0,offset:r?.offsets?.[n]?.offset?.toString()??n.toString()}))}catch(t){let n=t;throw this.logger.error(`Kafka: [${this.name}] Error publishing batch to topic ${e}`,{error:t}),new d(`[${this.name}] Failed to publish batch to topic ${e}: ${n.message||`Unknown error`}`,{cause:t})}}async disconnect(){if(!this.producer||!this._isConnected){this.logger.debug(`Kafka: [${this.name}] Producer already disconnected`);return}this.logger.info(`Kafka: [${this.name}] Disconnecting producer`);try{await this.producer.close(),this._isConnected=!1,this.logger.info(`Kafka: [${this.name}] Producer disconnected successfully`)}catch(e){throw this.logger.error(`Kafka: [${this.name}] Error disconnecting producer`,{error:e}),new d(`[${this.name}] Failed to disconnect producer: ${e.message}`,{cause:e})}}},b=class{constructor(e,t,n){this.name=e,this.brokers=t,this.logger=n,this.isConnected=!0}getHealth(){return{name:this.name,enabled:!1,isConnected:!0,lastPingAt:null,lastPingSucceededAt:null,lastError:null,clusterId:`mock-cluster`,brokerCount:0,brokers:this.brokers}}async ping(){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping ping`),Promise.resolve()}async publish(e,t,n){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping publish to topic: ${e}`,{value:t}),Promise.resolve([{topic:e,partition:0,offset:`0`}])}async publishBatch(e,t){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${e}`,{messageCount:t.messages.length}),Promise.resolve(t.messages.map(()=>({topic:e,partition:0,offset:`0`})))}async disconnect(){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping disconnect`),Promise.resolve()}},x=class e{constructor(e){if(this.producers=new Map,this.topics=new Map,this.gracefulShutdownStarted=!1,this.fatalError=null,this.lastReadinessCheck=null,this.logger=e.logger,this.enabled=e.enabled??!0,this.healthCheckTimeoutMs=e.healthCheckTimeoutMs??5e3,this.healthCheckCacheMs=e.healthCheckCacheMs??1e3,this.bootstrapTimeoutMs=e.bootstrapTimeoutMs??3e4,this.strictBootstrap=e.strictBootstrap??!0,e.topics)for(let[t,n]of Object.entries(e.topics))this.topics.set(t,n);if(this.logger.info(`Kafka: Initialized KafkaManager`,{enabled:this.enabled,producerCount:Object.keys(e.producers??{}).length,producers:Object.keys(e.producers??{}),topicCount:this.topics.size,topics:Array.from(this.topics.keys())}),this.enabled)for(let[t,n]of Object.entries(e.producers??{})){if(!n.brokers||n.brokers.length===0)throw new d(`[${t}] At least one broker is required`);this.producers.set(t,new y(t,n,this.logger))}else{for(let[t,n]of Object.entries(e.producers??{}))this.producers.set(t,new b(t,n.brokers,this.logger));this.producers.size>0&&this.logger.info(`Kafka: Created mock producers (Kafka disabled)`)}e.dontGracefulShutdown||this.setupGracefulShutdown()}static create(t){if(t.topics){let e=Object.keys(t.producers??{});for(let[n,r]of Object.entries(t.topics))if(!e.includes(r.producer))throw new d(`Topic '${n}' references unknown producer '${r.producer}'. Available producers: ${e.join(`, `)}`)}return new e(t)}setupGracefulShutdown(){this.logger.info(`Kafka: [graceful-shutdown] adding graceful shutdown for process.pid ${process.pid}`),process.on(`SIGTERM`,async()=>{await this.gracefulShutdown(`SIGTERM`)}),process.on(`SIGINT`,async()=>{await this.gracefulShutdown(`SIGINT`)})}async gracefulShutdown(e){if(!this.gracefulShutdownStarted){this.gracefulShutdownStarted=!0,this.logger.info(`Kafka: [graceful-shutdown] received ${e}! Disconnecting all producers...`);try{await this.disconnect(),this.logger.info(`Kafka: [graceful-shutdown] all producers disconnected successfully`)}catch(e){throw this.logger.error(`Kafka: [graceful-shutdown] error during shutdown`,{error:e}),e}}}get isEnabled(){return this.enabled}get producerNames(){return Array.from(this.producers.keys())}hasProducer(e){return this.producers.has(e)}getProducer(e){let t=this.producers.get(e);if(!t)throw new d(`Producer '${e}' not found. Available producers: ${this.producerNames.join(`, `)}`);return t}async bootstrap(e){let t=Date.now(),n=e?.timeoutMs??this.bootstrapTimeoutMs,r=e?.strict??this.strictBootstrap,i=e?.producers??this.producerNames;this.logger.info(`Kafka: Starting bootstrap`,{producers:i,timeout:n,strict:r});let a={},o=[];for(let e of i){let t=this.producers.get(e);if(!t){a[e]={success:!1,duration:0,error:`Producer '${e}' not found`},o.push(`Producer '${e}' not found`);continue}let r=Date.now();try{await t.ping({timeout:n,force:!0});let i=t.getHealth();a[e]={success:!0,duration:Date.now()-r,clusterId:i.clusterId??void 0,brokerCount:i.brokerCount},this.logger.info(`Kafka: [${e}] Bootstrap successful`,{duration:Date.now()-r,clusterId:i.clusterId,brokerCount:i.brokerCount})}catch(t){let n=t;a[e]={success:!1,duration:Date.now()-r,error:n.message},o.push(`[${e}] ${n.message}`),this.logger.error(`Kafka: [${e}] Bootstrap failed`,{duration:Date.now()-r,error:n.message})}}let s=Date.now()-t,c=Object.values(a).filter(e=>e.success).length,l=Object.keys(a).length,u=r?c===l:c>0,f={success:u,duration:s,results:a};if(u)this.logger.info(`Kafka: Bootstrap completed successfully`,{duration:s,successCount:c,totalCount:l});else{let e=`Kafka bootstrap failed (${c}/${l} producers succeeded)\n\nFailures:\n${o.map(e=>` - ${e}`).join(`
3
+ `)}\n\nConfiguration:\n Strict mode: ${r}\n Timeout: ${n}ms\n Producers attempted: ${i.join(`, `)}`;throw this.logger.error(`Kafka: Bootstrap failed`,{duration:s,successCount:c,totalCount:l,errors:o}),new d(e)}return f}getHealth(){let e={};for(let[t,n]of this.producers.entries())e[t]=n.getHealth();return e}getProducerHealth(e){return this.getProducer(e).getHealth()}isLive(){return!this.fatalError&&!this.gracefulShutdownStarted}async ping(){let e=Array.from(this.producers.values(),e=>e.ping());await Promise.all(e)}async pingProducer(e){await this.getProducer(e).ping()}async isReady(e){if(!this.enabled)return!0;let t=Date.now();if(!(e?.force??!1)&&this.lastReadinessCheck){let e=t-this.lastReadinessCheck.timestamp;if(e<this.healthCheckCacheMs)return this.logger.debug(`Kafka: Using cached readiness result`,{result:this.lastReadinessCheck.result,age:e}),this.lastReadinessCheck.result}try{let n=e?.timeout??this.healthCheckTimeoutMs,r=Array.from(this.producers.values()).map(e=>e.ping({timeout:n,force:!0}));return await Promise.all(r),this.lastReadinessCheck={result:!0,timestamp:t},!0}catch(e){return this.lastReadinessCheck={result:!1,timestamp:t},this.logger.warn(`Kafka: Readiness check failed`,{error:e.message}),!1}}getConnectionStatus(){let e={};for(let[t,n]of this.producers.entries()){let r=n.getHealth();e[t]={connected:r.isConnected,lastSuccessAt:r.lastPingSucceededAt,lastError:r.lastError?.message??null}}return e}getTopicDefinition(e){let t=this.topics.get(e);if(!t)throw new d(`Topic '${e}' not found. Available topics: ${Array.from(this.topics.keys()).join(`, `)}`);return t}async publish(e,t,n){let r=this.getTopicDefinition(e);if(!n?.skipValidation)try{r.schema.parse(t)}catch(n){throw this.logger.error(`Kafka: Validation failed for topic '${e}'`,{error:n,value:t}),new d(`Validation failed for topic '${e}': ${n.message}`,{cause:n})}return this.getProducer(r.producer).publish(e,t,n)}async publishBatch(e,t){let n=this.getTopicDefinition(e);if(!t.skipValidation)for(let r=0;r<t.messages.length;r++)try{n.schema.parse(t.messages[r].value)}catch(n){throw this.logger.error(`Kafka: Validation failed for topic '${e}' message ${r}`,{error:n,value:t.messages[r].value}),new d(`Validation failed for topic '${e}' message ${r}: ${n.message}`,{cause:n})}return this.getProducer(n.producer).publishBatch(e,t)}async disconnect(){let e=Array.from(this.producers.values()).map(e=>e.disconnect());await Promise.all(e),this.logger.info(`Kafka: All producers disconnected`)}async disconnectProducer(e){await this.getProducer(e).disconnect()}};exports.KafkaError=d,exports.KafkaManager=x;
4
4
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["name: string","config: ProducerConfig","logger: LoggerInstanceManager","#loadKafkaAndSetup","headers: Record<string, string>","name: string","brokers: string[]","logger: LoggerInstanceManager","results: BootstrapResult['results']","errors: string[]","result: BootstrapResult","health: Record<string, ProducerHealth>","status: Record<string, ConnectionStatus>"],"sources":["../src/kafkaError.ts","../src/consts.ts","../src/producer.ts","../src/mockProducer.ts","../src/manager.ts"],"sourcesContent":["export default class KafkaError extends Error {\n name = 'KafkaError';\n}\n","export const TIMESTAMP_HEADER = 'x-timestamp';\n\nexport const DEFAULT_CLIENT_ID = 'autofleet-kafka-producer';\n","import { setTimeout } from 'node:timers/promises';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\nimport type { Producer } from '@platformatic/kafka/dist/clients/producer/index.ts';\nimport KafkaError from './kafkaError';\nimport {\n DEFAULT_CLIENT_ID,\n TIMESTAMP_HEADER,\n} from './consts';\nimport type {\n ProducerConfig,\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n IProducer,\n} from './types';\n\n/**\n * Wrapper for a single Kafka producer instance with lazy initialization\n * Tracks health metadata including connection state, timestamps, and errors\n */\nexport class ProducerWrapper implements IProducer {\n private producer: Producer<string, string, string, string> | null = null;\n private _isConnected = false;\n private initPromise: Promise<void> | null = null;\n\n // Health tracking metadata\n private _lastPingAt: number | null = null;\n private _lastPingSucceededAt: number | null = null;\n private _lastError: { message: string; timestamp: number; stack?: string; } | null = null;\n private _clusterId: string | null = null;\n private _brokerCount = 0;\n\n constructor(\n private readonly name: string,\n private readonly config: ProducerConfig,\n private readonly logger: LoggerInstanceManager,\n ) {}\n\n async #loadKafkaAndSetup(): Promise<void> {\n const { Producer, stringSerializers } = await import('@platformatic/kafka');\n\n this.producer = new Producer({\n bootstrapBrokers: this.config.brokers,\n clientId: this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`,\n serializers: stringSerializers,\n autocreateTopics: this.config.autoCreateTopics ?? false,\n sasl: this.config.sasl,\n tls: this.config.tls,\n });\n\n this.logger.info(`Kafka: [${this.name}] Initialized producer`, {\n clientId: this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`,\n brokers: this.config.brokers,\n });\n\n // Clear promise to allow garbage collection\n this.initPromise = null;\n }\n\n private async initialize(): Promise<void> {\n if (this.producer) {\n return;\n }\n\n this.initPromise ??= this.#loadKafkaAndSetup();\n await this.initPromise;\n }\n\n get isConnected(): boolean {\n return this._isConnected;\n }\n\n /**\n * Get comprehensive health snapshot for this producer\n */\n getHealth(): ProducerHealth {\n return {\n name: this.name,\n enabled: true,\n isConnected: this._isConnected,\n lastPingAt: this._lastPingAt,\n lastPingSucceededAt: this._lastPingSucceededAt,\n lastError: this._lastError,\n clusterId: this._clusterId,\n brokerCount: this._brokerCount,\n brokers: this.config.brokers,\n };\n }\n\n /**\n * Build an actionable error message with troubleshooting guidance\n */\n private buildConnectionErrorMessage(error: Error): string {\n const brokerList = this.config.brokers.join(', ');\n const firstBroker = this.config.brokers[0];\n const [host, port] = firstBroker ? firstBroker.split(':') : ['', ''];\n\n return `[${this.name}] Failed to connect to Kafka brokers: ${error.message}\\n\\n`\n + `Possible causes:\\n`\n + ` 1. Brokers are unreachable: ${brokerList}\\n`\n + ` 2. Network connectivity issues\\n`\n + ` 3. Firewall blocking ports\\n`\n + ` 4. ${this.config.sasl ? 'SASL authentication failed' : 'Authentication not configured but required'}\\n\\n`\n + `Troubleshooting:\\n`\n + ` - Verify brokers are running and accessible\\n`\n + (host && port ? ` - Test connectivity: nc -zv ${host} ${port}\\n` : '')\n + ` - Check network policies and firewall rules\\n`\n + ` ${this.config.sasl ? '- Verify SASL credentials are correct\\n' : ''}\\n`\n + `Current configuration:\\n`\n + ` Brokers: ${brokerList}\\n`\n + ` Client ID: ${this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`}\\n`\n + ` SASL: ${this.config.sasl ? `enabled (${this.config.sasl.mechanism})` : 'disabled'}\\n`\n + ` SASL Password: ${this.config.sasl ? (this.config.sasl.password ? '****' : 'not set') : 'N/A'}\\n`\n + ` Auto-create topics: ${this.config.autoCreateTopics ?? false}`;\n }\n\n /**\n * Ping the Kafka cluster to verify connectivity\n * @param options.timeout - Maximum time to wait for connection (ms)\n * @param options.force - Force reconnection even if already connected\n */\n async ping(options?: { timeout?: number; force?: boolean; }): Promise<void> {\n this._lastPingAt = Date.now();\n\n // Skip if already connected and not forcing revalidation\n if (this._isConnected && !options?.force) {\n return;\n }\n\n await this.initialize();\n\n try {\n // Create error before promise to preserve stack trace\n const timeoutMs = options?.timeout ?? 5000;\n const timeoutError = new Error(`Connection timeout after ${timeoutMs}ms`);\n const timeoutPromise = setTimeout(timeoutMs).then(() => {\n throw timeoutError;\n });\n\n // Race between metadata fetch and timeout\n const metadata = await Promise.race([\n this.producer!.metadata({ topics: [] }),\n timeoutPromise,\n ]);\n\n this._isConnected = true;\n this._lastPingSucceededAt = Date.now();\n this._clusterId = metadata.id;\n this._brokerCount = metadata.brokers.size;\n this._lastError = null; // Clear any previous errors\n\n this.logger.info(`Kafka: [${this.name}] Successfully connected to cluster`, {\n clusterId: metadata.id,\n brokers: metadata.brokers.size,\n duration: Date.now() - this._lastPingAt,\n });\n } catch (error) {\n const err = error as Error;\n this._isConnected = false;\n this._lastError = {\n message: err.message,\n timestamp: Date.now(),\n stack: err.stack,\n };\n\n this.logger.error(`Kafka: [${this.name}] Failed to connect to brokers`, {\n error: err.message,\n duration: Date.now() - this._lastPingAt,\n });\n\n throw new KafkaError(this.buildConnectionErrorMessage(err), { cause: err });\n }\n }\n\n async publish<T = unknown>(\n topic: string,\n value: T,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n if (!topic) {\n throw new KafkaError(`[${this.name}] Topic name is required`);\n }\n\n await this.ping();\n\n try {\n const headers: Record<string, string> = {\n [TIMESTAMP_HEADER]: Date.now().toString(),\n ...options?.headers,\n };\n\n const result = await this.producer!.send({\n messages: [{\n topic,\n value: JSON.stringify(value),\n key: options?.key,\n partition: options?.partition,\n headers,\n }],\n });\n\n this.logger.debug(`Kafka: [${this.name}] Published message to topic ${topic}`, {\n topic,\n });\n\n return [{\n topic,\n partition: options?.partition ?? 0,\n offset: result?.offsets?.[0]?.offset?.toString() ?? '0',\n }];\n } catch (error) {\n const err = error as { message?: string; };\n this.logger.error(`Kafka: [${this.name}] Error publishing to topic ${topic}`, { error, value });\n throw new KafkaError(`[${this.name}] Failed to publish to topic ${topic}: ${err.message || 'Unknown error'}`, { cause: error });\n }\n }\n\n async publishBatch<T = unknown>(topic: string, options: PublishBatchOptions<T>): Promise<RecordMetadata[]> {\n if (!topic) {\n throw new KafkaError(`[${this.name}] Topic name is required`);\n }\n\n if (!options.messages || options.messages.length === 0) {\n throw new KafkaError(`[${this.name}] At least one message is required`);\n }\n\n await this.ping();\n\n try {\n const messages = options.messages.map(msg => ({\n topic,\n value: JSON.stringify(msg.value),\n key: msg.key,\n partition: msg.partition,\n headers: {\n [TIMESTAMP_HEADER]: Date.now().toString(),\n ...msg.headers,\n },\n }));\n\n const result = await this.producer!.send({ messages });\n\n this.logger.debug(`Kafka: [${this.name}] Published ${messages.length} messages to topic ${topic}`, {\n topic,\n count: messages.length,\n });\n\n return messages.map((msg, idx) => ({\n topic,\n partition: msg.partition ?? 0,\n offset: result?.offsets?.[idx]?.offset?.toString() ?? idx.toString(),\n }));\n } catch (error) {\n const err = error as { message?: string; };\n this.logger.error(`Kafka: [${this.name}] Error publishing batch to topic ${topic}`, { error });\n throw new KafkaError(`[${this.name}] Failed to publish batch to topic ${topic}: ${err.message || 'Unknown error'}`, { cause: error });\n }\n }\n\n async disconnect(): Promise<void> {\n if (!this.producer || !this._isConnected) {\n this.logger.debug(`Kafka: [${this.name}] Producer already disconnected`);\n return;\n }\n\n this.logger.info(`Kafka: [${this.name}] Disconnecting producer`);\n\n try {\n await this.producer.close();\n this._isConnected = false;\n this.logger.info(`Kafka: [${this.name}] Producer disconnected successfully`);\n } catch (error) {\n this.logger.error(`Kafka: [${this.name}] Error disconnecting producer`, { error });\n throw new KafkaError(`[${this.name}] Failed to disconnect producer: ${(error as Error).message}`, { cause: error });\n }\n }\n}\n","import type { LoggerInstanceManager } from '@autofleet/logger';\nimport type {\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n IProducer,\n} from './types';\n\n/**\n * Mock producer for when Kafka is disabled\n */\nexport class MockProducer implements IProducer {\n constructor(private readonly name: string, private readonly brokers: string[], private readonly logger: LoggerInstanceManager) {}\n\n readonly isConnected = true;\n\n /**\n * Get health snapshot for mock producer\n */\n getHealth(): ProducerHealth {\n return {\n name: this.name,\n enabled: false, // Mock mode means Kafka is disabled\n isConnected: true, // Mock is always \"connected\"\n lastPingAt: null,\n lastPingSucceededAt: null,\n lastError: null,\n clusterId: 'mock-cluster',\n brokerCount: 0,\n brokers: this.brokers,\n };\n }\n\n async ping(): Promise<void> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping ping`);\n return Promise.resolve();\n }\n\n async publish<T = unknown>(topic: string, value: T, _options?: PublishOptions): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping publish to topic: ${topic}`, { value });\n return Promise.resolve([{\n topic,\n partition: 0,\n offset: '0',\n }]);\n }\n\n async publishBatch<T = unknown>(topic: string, options: PublishBatchOptions<T>): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${topic}`, {\n messageCount: options.messages.length,\n });\n return Promise.resolve(options.messages.map(() => ({\n topic,\n partition: 0,\n offset: '0',\n })));\n }\n\n async disconnect(): Promise<void> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping disconnect`);\n return Promise.resolve();\n }\n}\n","import type { LoggerInstanceManager } from '@autofleet/logger';\nimport KafkaError from './kafkaError';\nimport { ProducerWrapper } from './producer';\nimport { MockProducer } from './mockProducer';\nimport type {\n KafkaManagerOptions,\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n BootstrapOptions,\n BootstrapResult,\n ReadinessOptions,\n ConnectionStatus,\n IProducer,\n ProducerConfig,\n TopicsConfig,\n TopicDefinition,\n TopicPayload,\n} from './types';\n\n/**\n * Manager for multiple Kafka producers\n * Provides lifecycle management, health tracking, and operational guarantees\n *\n * @template TProducers - Union of producer names\n * @template TTopics - Typed topics configuration with Zod schemas\n */\nexport class KafkaManager<TProducers extends string = string, TTopics extends TopicsConfig = {}> {\n private readonly producers = new Map<string, IProducer>();\n private readonly topics = new Map<string, TopicDefinition>();\n private readonly logger: LoggerInstanceManager;\n private readonly enabled: boolean;\n private gracefulShutdownStarted = false;\n private fatalError: Error | null = null;\n\n // Configuration options\n private readonly healthCheckTimeoutMs: number;\n private readonly healthCheckCacheMs: number;\n private readonly bootstrapTimeoutMs: number;\n private readonly strictBootstrap: boolean;\n\n // Health check cache\n private lastReadinessCheck: { result: boolean; timestamp: number; } | null = null;\n\n private constructor(options: KafkaManagerOptions) {\n this.logger = options.logger;\n this.enabled = options.enabled ?? true;\n this.healthCheckTimeoutMs = options.healthCheckTimeoutMs ?? 5000;\n this.healthCheckCacheMs = options.healthCheckCacheMs ?? 1000;\n this.bootstrapTimeoutMs = options.bootstrapTimeoutMs ?? 30_000;\n this.strictBootstrap = options.strictBootstrap ?? true;\n\n // Store topic definitions\n if (options.topics) {\n for (const [topicName, definition] of Object.entries(options.topics)) {\n this.topics.set(topicName, definition);\n }\n }\n\n this.logger.info('Kafka: Initialized KafkaManager', {\n enabled: this.enabled,\n producerCount: Object.keys(options.producers ?? {}).length,\n producers: Object.keys(options.producers ?? {}),\n topicCount: this.topics.size,\n topics: Array.from(this.topics.keys()),\n });\n\n // Create producers\n if (!this.enabled) {\n // Create mock producers\n for (const [name, config] of Object.entries(options.producers ?? {})) {\n this.producers.set(name, new MockProducer(name, config.brokers, this.logger));\n }\n if (this.producers.size > 0) {\n this.logger.info('Kafka: Created mock producers (Kafka disabled)');\n }\n } else {\n // Create real producers (they will initialize lazily)\n for (const [name, config] of Object.entries(options.producers ?? {})) {\n if (!config.brokers || config.brokers.length === 0) {\n throw new KafkaError(`[${name}] At least one broker is required`);\n }\n\n this.producers.set(name, new ProducerWrapper(name, config, this.logger));\n }\n }\n\n if (!options.dontGracefulShutdown) {\n this.setupGracefulShutdown();\n }\n }\n\n /**\n * Create a new KafkaManager instance with multiple named producers\n * Producers are initialized lazily on first use\n * Producer names are type-safe based on the configuration\n * Topics configuration provides type-safe publishing with Zod validation\n */\n static create<\n P extends Record<string, ProducerConfig>,\n T extends TopicsConfig = {},\n >(\n options: Omit<KafkaManagerOptions, 'producers' | 'topics'> & {\n producers?: P;\n topics?: T;\n },\n ): KafkaManager<keyof P & string, T> {\n // Validate that topic producers exist\n if (options.topics) {\n const producerNames = Object.keys(options.producers ?? {});\n for (const [topicName, topicDef] of Object.entries(options.topics)) {\n if (!producerNames.includes(topicDef.producer)) {\n throw new KafkaError(\n `Topic '${topicName}' references unknown producer '${topicDef.producer}'. `\n + `Available producers: ${producerNames.join(', ')}`,\n );\n }\n }\n }\n\n return new KafkaManager(options);\n }\n\n private setupGracefulShutdown(): void {\n this.logger.info(`Kafka: [graceful-shutdown] adding graceful shutdown for process.pid ${process.pid}`);\n\n process.on('SIGTERM', async () => {\n await this.gracefulShutdown('SIGTERM');\n });\n\n process.on('SIGINT', async () => {\n await this.gracefulShutdown('SIGINT');\n });\n }\n\n public async gracefulShutdown(signal: string): Promise<void> {\n if (this.gracefulShutdownStarted) {\n return;\n }\n\n this.gracefulShutdownStarted = true;\n\n this.logger.info(`Kafka: [graceful-shutdown] received ${signal}! Disconnecting all producers...`);\n\n try {\n await this.disconnect();\n this.logger.info('Kafka: [graceful-shutdown] all producers disconnected successfully');\n } catch (error) {\n this.logger.error('Kafka: [graceful-shutdown] error during shutdown', { error });\n throw error;\n }\n }\n\n /**\n * Check if Kafka is enabled\n */\n get isEnabled(): boolean {\n return this.enabled;\n }\n\n /**\n * Get all producer names\n */\n get producerNames(): TProducers[] {\n return Array.from(this.producers.keys()) as TProducers[];\n }\n\n /**\n * Check if a producer exists\n */\n hasProducer(name: TProducers): boolean {\n return this.producers.has(name);\n }\n\n /**\n * Get a specific producer\n */\n private getProducer(name: TProducers): IProducer {\n const producer = this.producers.get(name);\n if (!producer) {\n throw new KafkaError(`Producer '${name}' not found. Available producers: ${this.producerNames.join(', ')}`);\n }\n return producer;\n }\n\n /**\n * Explicitly bootstrap all (or specific) producers\n * Connects to brokers, validates connectivity, and returns detailed results\n *\n * @example\n * // Bootstrap all producers with defaults (30s timeout, strict mode)\n * await kafka.bootstrap();\n *\n * @example\n * // Bootstrap with custom timeout and non-strict mode\n * const result = await kafka.bootstrap({ timeoutMs: 10000, strict: false });\n * if (!result.success) {\n * console.error('Some producers failed:', result.results);\n * }\n *\n * @example\n * // Bootstrap specific producers only\n * await kafka.bootstrap({ producers: ['main'] });\n */\n async bootstrap(options?: BootstrapOptions): Promise<BootstrapResult> {\n const startTime = Date.now();\n const timeoutMs = options?.timeoutMs ?? this.bootstrapTimeoutMs;\n const strict = options?.strict ?? this.strictBootstrap;\n const producersToBootstrap = options?.producers ?? this.producerNames;\n\n this.logger.info('Kafka: Starting bootstrap', {\n producers: producersToBootstrap,\n timeout: timeoutMs,\n strict,\n });\n\n const results: BootstrapResult['results'] = {};\n const errors: string[] = [];\n\n // Bootstrap each producer\n for (const name of producersToBootstrap) {\n const producer = this.producers.get(name);\n if (!producer) {\n results[name] = {\n success: false,\n duration: 0,\n error: `Producer '${name}' not found`,\n };\n errors.push(`Producer '${name}' not found`);\n continue;\n }\n\n const producerStartTime = Date.now();\n try {\n await producer.ping({ timeout: timeoutMs, force: true });\n const health = producer.getHealth();\n\n results[name] = {\n success: true,\n duration: Date.now() - producerStartTime,\n clusterId: health.clusterId ?? undefined,\n brokerCount: health.brokerCount,\n };\n\n this.logger.info(`Kafka: [${name}] Bootstrap successful`, {\n duration: Date.now() - producerStartTime,\n clusterId: health.clusterId,\n brokerCount: health.brokerCount,\n });\n } catch (error) {\n const err = error as Error;\n results[name] = {\n success: false,\n duration: Date.now() - producerStartTime,\n error: err.message,\n };\n errors.push(`[${name}] ${err.message}`);\n\n this.logger.error(`Kafka: [${name}] Bootstrap failed`, {\n duration: Date.now() - producerStartTime,\n error: err.message,\n });\n }\n }\n\n const totalDuration = Date.now() - startTime;\n const successCount = Object.values(results).filter(r => r.success).length;\n const totalCount = Object.keys(results).length;\n const success = strict ? successCount === totalCount : successCount > 0;\n\n const result: BootstrapResult = {\n success,\n duration: totalDuration,\n results,\n };\n\n if (success) {\n this.logger.info('Kafka: Bootstrap completed successfully', {\n duration: totalDuration,\n successCount,\n totalCount,\n });\n } else {\n const errorMessage = `Kafka bootstrap failed (${successCount}/${totalCount} producers succeeded)\\n\\n`\n + `Failures:\\n${errors.map(e => ` - ${e}`).join('\\n')}\\n\\n`\n + `Configuration:\\n`\n + ` Strict mode: ${strict}\\n`\n + ` Timeout: ${timeoutMs}ms\\n`\n + ` Producers attempted: ${producersToBootstrap.join(', ')}`;\n\n this.logger.error('Kafka: Bootstrap failed', {\n duration: totalDuration,\n successCount,\n totalCount,\n errors,\n });\n\n throw new KafkaError(errorMessage);\n }\n\n return result;\n }\n\n /**\n * Get detailed health snapshot for all producers\n * Returns comprehensive metadata including timestamps, errors, cluster info\n *\n * @example\n * const health = kafka.getHealth();\n * console.log(health.main.lastPingSucceededAt); // timestamp\n * console.log(health.main.clusterId); // 'prod-kafka-01'\n */\n getHealth(): Record<TProducers, ProducerHealth> {\n const health: Record<string, ProducerHealth> = {};\n for (const [name, producer] of this.producers.entries()) {\n health[name] = producer.getHealth();\n }\n return health as Record<TProducers, ProducerHealth>;\n }\n\n /**\n * Get detailed health for a specific producer\n */\n getProducerHealth(name: TProducers): ProducerHealth {\n const producer = this.getProducer(name);\n return producer.getHealth();\n }\n\n /**\n * Lightweight liveness check - does NOT perform Kafka operations\n * Only checks internal state for fatal errors\n * Suitable for Kubernetes liveness probes\n *\n * Returns false if:\n * - Manager is in a fatal state\n * - Graceful shutdown has started\n *\n * @example\n * app.get('/health/live', (req, res) => {\n * res.status(kafka.isLive() ? 200 : 503).json({ live: kafka.isLive() });\n * });\n */\n isLive(): boolean {\n return !this.fatalError && !this.gracefulShutdownStarted;\n }\n\n /**\n * Ping all producers to verify connectivity\n */\n async ping(): Promise<void> {\n const promises = Array.from(this.producers.values(), p => p.ping());\n await Promise.all(promises);\n }\n\n /**\n * Ping a specific producer\n */\n async pingProducer(name: TProducers): Promise<void> {\n await this.getProducer(name).ping();\n }\n\n /**\n * Check if Kafka is ready to handle traffic\n * Revalidates connectivity if cache is stale\n *\n * @param options.timeout - Override default health check timeout\n * @param options.force - Force revalidation, ignore cache\n *\n * @example\n * // Use cached result if fresh (within healthCheckCacheMs)\n * const ready = await kafka.isReady();\n *\n * @example\n * // Force immediate revalidation\n * const ready = await kafka.isReady({ force: true });\n */\n async isReady(options?: ReadinessOptions): Promise<boolean> {\n if (!this.enabled) {\n return true;\n }\n\n const now = Date.now();\n const force = options?.force ?? false;\n\n // Use cached result if available and fresh\n if (!force && this.lastReadinessCheck) {\n const age = now - this.lastReadinessCheck.timestamp;\n if (age < this.healthCheckCacheMs) {\n this.logger.debug('Kafka: Using cached readiness result', {\n result: this.lastReadinessCheck.result,\n age,\n });\n return this.lastReadinessCheck.result;\n }\n }\n\n // Revalidate connectivity\n try {\n const timeout = options?.timeout ?? this.healthCheckTimeoutMs;\n const promises = Array.from(this.producers.values()).map(p =>\n p.ping({ timeout, force: true }),\n );\n await Promise.all(promises);\n\n this.lastReadinessCheck = { result: true, timestamp: now };\n return true;\n } catch (error) {\n this.lastReadinessCheck = { result: false, timestamp: now };\n this.logger.warn('Kafka: Readiness check failed', {\n error: (error as Error).message,\n });\n return false;\n }\n }\n\n /**\n * Check connection status of all producers\n * Returns detailed status including last success time and errors\n */\n getConnectionStatus(): Record<TProducers, ConnectionStatus> {\n const status: Record<string, ConnectionStatus> = {};\n for (const [name, producer] of this.producers.entries()) {\n const health = producer.getHealth();\n status[name] = {\n connected: health.isConnected,\n lastSuccessAt: health.lastPingSucceededAt,\n lastError: health.lastError?.message ?? null,\n };\n }\n return status as Record<TProducers, ConnectionStatus>;\n }\n\n /**\n * Get a topic definition\n */\n private getTopicDefinition<TName extends keyof TTopics & string>(topicName: TName): TopicDefinition {\n const topic = this.topics.get(topicName);\n if (!topic) {\n throw new KafkaError(\n `Topic '${topicName}' not found. Available topics: ${Array.from(this.topics.keys()).join(', ')}`,\n );\n }\n return topic;\n }\n\n /**\n * Publish a message to a typed topic\n * Automatically validates payload against Zod schema and routes to correct producer\n *\n * @template TName - Topic name (inferred from topics config)\n * @param topicName - Name of the topic to publish to\n * @param value - Payload (type-checked against topic's Zod schema)\n * @param options - Optional publish options including headers, key, partition\n *\n * @example\n * await kafka.publish('order.created', {\n * orderId: '123',\n * amount: 99.99,\n * customerId: '456',\n * });\n */\n async publish<TName extends keyof TTopics & string>(\n topicName: TName,\n value: TopicPayload<TTopics, TName>,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n const topicDef = this.getTopicDefinition(topicName);\n\n // Validate payload against schema unless skipped\n if (!options?.skipValidation) {\n try {\n topicDef.schema.parse(value);\n } catch (error) {\n this.logger.error(`Kafka: Validation failed for topic '${topicName}'`, { error, value });\n throw new KafkaError(\n `Validation failed for topic '${topicName}': ${(error as Error).message}`,\n { cause: error },\n );\n }\n }\n\n // Get producer and publish\n const producer = this.getProducer(topicDef.producer as TProducers);\n return producer.publish(topicName, value, options);\n }\n\n /**\n * Publish a batch of messages to a typed topic\n * Automatically validates payloads against Zod schema and routes to correct producer\n *\n * @template TName - Topic name (inferred from topics config)\n * @param topicName - Name of the topic to publish to\n * @param options - Batch options with array of messages\n *\n * @example\n * await kafka.publishBatch('order.created', {\n * messages: [\n * { value: { orderId: '1', amount: 10, customerId: 'a' } },\n * { value: { orderId: '2', amount: 20, customerId: 'b' } },\n * ],\n * });\n */\n async publishBatch<TName extends keyof TTopics & string>(\n topicName: TName,\n options: PublishBatchOptions<TopicPayload<TTopics, TName>>,\n ): Promise<RecordMetadata[]> {\n const topicDef = this.getTopicDefinition(topicName);\n\n // Validate all payloads against schema unless skipped\n if (!options.skipValidation) {\n for (let i = 0; i < options.messages.length; i++) {\n try {\n topicDef.schema.parse(options.messages[i].value);\n } catch (error) {\n this.logger.error(`Kafka: Validation failed for topic '${topicName}' message ${i}`, {\n error,\n value: options.messages[i].value,\n });\n throw new KafkaError(\n `Validation failed for topic '${topicName}' message ${i}: ${(error as Error).message}`,\n { cause: error },\n );\n }\n }\n }\n\n // Get producer and publish batch\n const producer = this.getProducer(topicDef.producer as TProducers);\n return producer.publishBatch(topicName, options);\n }\n\n /**\n * Disconnect all producers\n */\n async disconnect(): Promise<void> {\n const promises = Array.from(this.producers.values()).map(p => p.disconnect());\n await Promise.all(promises);\n this.logger.info('Kafka: All producers disconnected');\n }\n\n /**\n * Disconnect a specific producer\n */\n async disconnectProducer(name: TProducers): Promise<void> {\n await this.getProducer(name).disconnect();\n }\n}\n"],"mappings":"sCAAA,IAAqB,EAArB,cAAwC,KAAM,yCACrC,eCDT,MAAa,EAAmB,cAEnB,EAAoB,2BCmBjC,IAAa,EAAb,KAAkD,CAYhD,YACE,EACA,EACA,EACA,CAHiB,KAAA,KAAA,EACA,KAAA,OAAA,EACA,KAAA,OAAA,gBAdiD,uBAC7C,oBACqB,sBAGP,+BACS,qBACuC,qBACjD,uBACb,EAQvB,MAAA,GAA0C,CACxC,GAAM,CAAE,WAAU,qBAAsB,MAAM,OAAO,uBAErD,KAAK,SAAW,IAAI,EAAS,CAC3B,iBAAkB,KAAK,OAAO,QAC9B,SAAU,KAAK,OAAO,UAAY,GAAG,EAAkB,GAAG,KAAK,OAC/D,YAAa,EACb,iBAAkB,KAAK,OAAO,kBAAoB,GAClD,KAAM,KAAK,OAAO,KAClB,IAAK,KAAK,OAAO,IAClB,CAAC,CAEF,KAAK,OAAO,KAAK,WAAW,KAAK,KAAK,wBAAyB,CAC7D,SAAU,KAAK,OAAO,UAAY,GAAG,EAAkB,GAAG,KAAK,OAC/D,QAAS,KAAK,OAAO,QACtB,CAAC,CAGF,KAAK,YAAc,KAGrB,MAAc,YAA4B,CACpC,KAAK,WAIT,KAAK,cAAgB,MAAA,GAAyB,CA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M,OACzC,EAAO,CAKd,MAJA,KAAK,OAAO,MAAM,uCAAuC,EAAU,YAAY,IAAK,CAClF,QACA,MAAO,EAAQ,SAAS,GAAG,MAC5B,CAAC,CACI,IAAI,EACR,gCAAgC,EAAU,YAAY,EAAE,IAAK,EAAgB,UAC7E,CAAE,MAAO,EAAO,CACjB,CAOP,OADiB,KAAK,YAAY,EAAS,SAAuB,CAClD,aAAa,EAAW,EAAQ,CAMlD,MAAM,YAA4B,CAChC,IAAM,EAAW,MAAM,KAAK,KAAK,UAAU,QAAQ,CAAC,CAAC,IAAI,GAAK,EAAE,YAAY,CAAC,CAC7E,MAAM,QAAQ,IAAI,EAAS,CAC3B,KAAK,OAAO,KAAK,oCAAoC,CAMvD,MAAM,mBAAmB,EAAiC,CACxD,MAAM,KAAK,YAAY,EAAK,CAAC,YAAY"}
1
+ {"version":3,"file":"index.cjs","names":["crypto","GoogleAuth","clientPromise: Promise<any> | null","cachedEmail: string | null","cachedWrappedToken: string | null","cachedWrappedTokenExpSec: number | null","name: string","config: ProducerConfig","logger: LoggerInstanceManager","#loadKafkaAndSetup","headers: Record<string, string>","name: string","brokers: string[]","logger: LoggerInstanceManager","results: BootstrapResult['results']","errors: string[]","result: BootstrapResult","health: Record<string, ProducerHealth>","status: Record<string, ConnectionStatus>"],"sources":["../src/kafkaError.ts","../src/consts.ts","../src/auth.ts","../src/producer.ts","../src/mockProducer.ts","../src/manager.ts"],"sourcesContent":["export default class KafkaError extends Error {\n name = 'KafkaError';\n}\n","export const TIMESTAMP_HEADER = 'x-timestamp';\n\nexport const DEFAULT_CLIENT_ID = 'autofleet-kafka-producer';\n","import { GoogleAuth } from 'google-auth-library';\nimport type { OauthBearerProviderOptions } from './types';\nimport crypto from 'node:crypto';\n\nconst DEFAULT_SKEW_SEC = 60;\n\nconst b64urlUtf8 = (s: string) =>\n Buffer.from(s, 'utf8')\n .toString('base64')\n .replace(/\\+/g, '-')\n .replace(/\\//g, '_')\n .replace(/=+$/g, '');\n\nconst fp12 = (s: string) =>\n crypto.createHash('sha256').update(s).digest('hex').slice(0, 12);\n\nconst HEADER_B64 = b64urlUtf8(JSON.stringify({ typ: 'JWT', alg: 'GOOG_OAUTH2_TOKEN' }));\n\n// This is an implementation of the OAUTHBEARER mechanisem for google managed kafka\n// https://github.com/googleapis/managedkafka/blob/main/kafka-auth-local-server/kafka_gcp_credentials_server.py\nexport const createManagedKafkaOauthBearerProvider = (opts: OauthBearerProviderOptions = {}) => {\n const logger = opts.logger;\n const refreshSkewSec = opts.refreshSkewSec ?? DEFAULT_SKEW_SEC;\n const enableMetadataLookup = opts.enableMetadataLookup ?? true;\n\n const auth = new GoogleAuth({\n scopes: ['https://www.googleapis.com/auth/cloud-platform'],\n });\n\n let clientPromise: Promise<any> | null = null;\n let cachedEmail: string | null = opts.serviceAccountEmail ?? null;\n\n let cachedWrappedToken: string | null = null;\n let cachedWrappedTokenExpSec: number | null = null;\n\n const getClient = async () => {\n clientPromise ??= auth.getClient();\n return clientPromise;\n };\n\n const metadataEmailLookup = async () => {\n try {\n const res = await fetch(\n 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/email',\n { headers: { 'Metadata-Flavor': 'Google' } },\n );\n if (!res.ok) return null;\n return (await res.text()).trim();\n } catch (e) {\n logger?.warn?.('ManagedKafkaAuth: metadata email lookup failed', { err: String(e) });\n return null;\n }\n };\n\n const getEmail = async () => {\n if (cachedEmail) return cachedEmail;\n\n try {\n const creds = await auth.getCredentials();\n if (creds?.client_email) {\n cachedEmail = creds.client_email;\n return cachedEmail;\n }\n } catch (e) {\n logger?.warn?.('ManagedKafkaAuth: getCredentials failed', { err: String(e) });\n }\n\n if (enableMetadataLookup) {\n const email = await metadataEmailLookup();\n if (email) {\n cachedEmail = email;\n return cachedEmail;\n }\n }\n\n if (process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL) {\n cachedEmail = process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL;\n return cachedEmail;\n }\n\n throw new Error('ManagedKafkaAuth: cannot determine service account email (set GOOGLE_SERVICE_ACCOUNT_EMAIL)');\n };\n\n return async (): Promise<string> => {\n const nowSec = Math.floor(Date.now() / 1000);\n\n // reuse cached wrapped token if still valid\n if (\n cachedWrappedToken\n && cachedWrappedTokenExpSec\n && nowSec < (cachedWrappedTokenExpSec - refreshSkewSec)\n ) {\n return cachedWrappedToken;\n }\n\n const client = await getClient();\n\n const { token } = await client.getAccessToken();\n if (!token) throw new Error('ManagedKafkaAuth: no access token from ADC');\n\n const expiryMs = client.credentials?.expiry_date;\n if (!expiryMs) throw new Error('ManagedKafkaAuth: no expiry_date on ADC credentials');\n\n const sub = await getEmail();\n const expSec = Math.floor(expiryMs / 1000);\n\n const payloadB64 = b64urlUtf8(JSON.stringify({\n exp: expSec,\n iss: 'Google',\n iat: nowSec,\n sub,\n }));\n\n const wrapped = `${HEADER_B64}.${payloadB64}.${b64urlUtf8(token)}`;\n\n cachedWrappedToken = wrapped;\n cachedWrappedTokenExpSec = expSec;\n\n logger?.info?.('ManagedKafkaAuth: token generated', {\n sub,\n iat: nowSec,\n exp: expSec,\n fp: fp12(wrapped),\n });\n\n return wrapped;\n };\n};\n","import { setTimeout } from 'node:timers/promises';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\nimport type { Producer } from '@platformatic/kafka/dist/clients/producer/index.ts';\nimport KafkaError from './kafkaError';\nimport {\n DEFAULT_CLIENT_ID,\n TIMESTAMP_HEADER,\n} from './consts';\nimport type {\n ProducerConfig,\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n IProducer,\n} from './types';\nimport { createManagedKafkaOauthBearerProvider } from './auth';\n\n/**\n * Wrapper for a single Kafka producer instance with lazy initialization\n * Tracks health metadata including connection state, timestamps, and errors\n */\nexport class ProducerWrapper implements IProducer {\n private producer: Producer<string, string, string, string> | null = null;\n private _isConnected = false;\n private initPromise: Promise<void> | null = null;\n\n // Health tracking metadata\n private _lastPingAt: number | null = null;\n private _lastPingSucceededAt: number | null = null;\n private _lastError: { message: string; timestamp: number; stack?: string; } | null = null;\n private _clusterId: string | null = null;\n private _brokerCount = 0;\n\n constructor(\n private readonly name: string,\n private readonly config: ProducerConfig,\n private readonly logger: LoggerInstanceManager,\n ) {}\n\n async #loadKafkaAndSetup(): Promise<void> {\n const { Producer, stringSerializers } = await import('@platformatic/kafka');\n\n const oauthBearerConfig = this.config.oauthBearerConfig ?? { logger: this.logger };\n\n this.producer = new Producer({\n bootstrapBrokers: this.config.brokers,\n clientId: this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`,\n serializers: stringSerializers,\n autocreateTopics: this.config.autoCreateTopics ?? false,\n sasl: this.config.sasl ?? {\n mechanism: 'OAUTHBEARER',\n token: createManagedKafkaOauthBearerProvider(oauthBearerConfig),\n },\n tls: this.config.tls ?? {},\n });\n\n this.logger.info(`Kafka: [${this.name}] Initialized producer`, {\n clientId: this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`,\n brokers: this.config.brokers,\n });\n\n // Clear promise to allow garbage collection\n this.initPromise = null;\n }\n\n private async initialize(): Promise<void> {\n if (this.producer) {\n return;\n }\n\n this.initPromise ??= this.#loadKafkaAndSetup();\n await this.initPromise;\n }\n\n get isConnected(): boolean {\n return this._isConnected;\n }\n\n /**\n * Get comprehensive health snapshot for this producer\n */\n getHealth(): ProducerHealth {\n return {\n name: this.name,\n enabled: true,\n isConnected: this._isConnected,\n lastPingAt: this._lastPingAt,\n lastPingSucceededAt: this._lastPingSucceededAt,\n lastError: this._lastError,\n clusterId: this._clusterId,\n brokerCount: this._brokerCount,\n brokers: this.config.brokers,\n };\n }\n\n /**\n * Build an actionable error message with troubleshooting guidance\n */\n private buildConnectionErrorMessage(error: Error): string {\n const brokerList = this.config.brokers.join(', ');\n const firstBroker = this.config.brokers[0];\n const [host, port] = firstBroker ? firstBroker.split(':') : ['', ''];\n\n return `[${this.name}] Failed to connect to Kafka brokers: ${error.message}\\n\\n`\n + `Possible causes:\\n`\n + ` 1. Brokers are unreachable: ${brokerList}\\n`\n + ` 2. Network connectivity issues\\n`\n + ` 3. Firewall blocking ports\\n`\n + ` 4. ${this.config.sasl ? 'SASL authentication failed' : 'Authentication not configured but required'}\\n\\n`\n + `Troubleshooting:\\n`\n + ` - Verify brokers are running and accessible\\n`\n + (host && port ? ` - Test connectivity: nc -zv ${host} ${port}\\n` : '')\n + ` - Check network policies and firewall rules\\n`\n + ` ${this.config.sasl ? '- Verify SASL credentials are correct\\n' : ''}\\n`\n + `Current configuration:\\n`\n + ` Brokers: ${brokerList}\\n`\n + ` Client ID: ${this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`}\\n`\n + ` SASL: ${this.config.sasl ? `enabled (${this.config.sasl.mechanism})` : 'disabled'}\\n`\n + ` SASL Password: ${this.config.sasl ? (this.config.sasl.password ? '****' : 'not set') : 'N/A'}\\n`\n + ` Auto-create topics: ${this.config.autoCreateTopics ?? false}`;\n }\n\n /**\n * Ping the Kafka cluster to verify connectivity\n * @param options.timeout - Maximum time to wait for connection (ms)\n * @param options.force - Force reconnection even if already connected\n */\n async ping(options?: { timeout?: number; force?: boolean; }): Promise<void> {\n this._lastPingAt = Date.now();\n\n // Skip if already connected and not forcing revalidation\n if (this._isConnected && !options?.force) {\n return;\n }\n\n await this.initialize();\n\n try {\n // Create error before promise to preserve stack trace\n const timeoutMs = options?.timeout ?? 5000;\n const timeoutError = new Error(`Connection timeout after ${timeoutMs}ms`);\n const timeoutPromise = setTimeout(timeoutMs).then(() => {\n throw timeoutError;\n });\n\n // Race between metadata fetch and timeout\n const metadata = await Promise.race([\n this.producer!.metadata({ topics: [] }),\n timeoutPromise,\n ]);\n\n this._isConnected = true;\n this._lastPingSucceededAt = Date.now();\n this._clusterId = metadata.id;\n this._brokerCount = metadata.brokers.size;\n this._lastError = null; // Clear any previous errors\n\n this.logger.info(`Kafka: [${this.name}] Successfully connected to cluster`, {\n clusterId: metadata.id,\n brokers: metadata.brokers.size,\n duration: Date.now() - this._lastPingAt,\n });\n } catch (error) {\n const err = error as Error;\n this._isConnected = false;\n this._lastError = {\n message: err.message,\n timestamp: Date.now(),\n stack: err.stack,\n };\n\n this.logger.error(`Kafka: [${this.name}] Failed to connect to brokers`, {\n error: err.message,\n duration: Date.now() - this._lastPingAt,\n });\n\n throw new KafkaError(this.buildConnectionErrorMessage(err), { cause: err });\n }\n }\n\n async publish<T = unknown>(\n topic: string,\n value: T,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n if (!topic) {\n throw new KafkaError(`[${this.name}] Topic name is required`);\n }\n\n await this.ping();\n\n try {\n const headers: Record<string, string> = {\n [TIMESTAMP_HEADER]: Date.now().toString(),\n ...options?.headers,\n };\n\n const result = await this.producer!.send({\n messages: [{\n topic,\n value: JSON.stringify(value),\n key: options?.key,\n partition: options?.partition,\n headers,\n }],\n });\n\n this.logger.debug(`Kafka: [${this.name}] Published message to topic ${topic}`, {\n topic,\n });\n\n return [{\n topic,\n partition: options?.partition ?? 0,\n offset: result?.offsets?.[0]?.offset?.toString() ?? '0',\n }];\n } catch (error) {\n const err = error as { message?: string; };\n this.logger.error(`Kafka: [${this.name}] Error publishing to topic ${topic}`, { error, value });\n throw new KafkaError(`[${this.name}] Failed to publish to topic ${topic}: ${err.message || 'Unknown error'}`, { cause: error });\n }\n }\n\n async publishBatch<T = unknown>(topic: string, options: PublishBatchOptions<T>): Promise<RecordMetadata[]> {\n if (!topic) {\n throw new KafkaError(`[${this.name}] Topic name is required`);\n }\n\n if (!options.messages || options.messages.length === 0) {\n throw new KafkaError(`[${this.name}] At least one message is required`);\n }\n\n await this.ping();\n\n try {\n const messages = options.messages.map(msg => ({\n topic,\n value: JSON.stringify(msg.value),\n key: msg.key,\n partition: msg.partition,\n headers: {\n [TIMESTAMP_HEADER]: Date.now().toString(),\n ...msg.headers,\n },\n }));\n\n const result = await this.producer!.send({ messages });\n\n this.logger.debug(`Kafka: [${this.name}] Published ${messages.length} messages to topic ${topic}`, {\n topic,\n count: messages.length,\n });\n\n return messages.map((msg, idx) => ({\n topic,\n partition: msg.partition ?? 0,\n offset: result?.offsets?.[idx]?.offset?.toString() ?? idx.toString(),\n }));\n } catch (error) {\n const err = error as { message?: string; };\n this.logger.error(`Kafka: [${this.name}] Error publishing batch to topic ${topic}`, { error });\n throw new KafkaError(`[${this.name}] Failed to publish batch to topic ${topic}: ${err.message || 'Unknown error'}`, { cause: error });\n }\n }\n\n async disconnect(): Promise<void> {\n if (!this.producer || !this._isConnected) {\n this.logger.debug(`Kafka: [${this.name}] Producer already disconnected`);\n return;\n }\n\n this.logger.info(`Kafka: [${this.name}] Disconnecting producer`);\n\n try {\n await this.producer.close();\n this._isConnected = false;\n this.logger.info(`Kafka: [${this.name}] Producer disconnected successfully`);\n } catch (error) {\n this.logger.error(`Kafka: [${this.name}] Error disconnecting producer`, { error });\n throw new KafkaError(`[${this.name}] Failed to disconnect producer: ${(error as Error).message}`, { cause: error });\n }\n }\n}\n","import type { LoggerInstanceManager } from '@autofleet/logger';\nimport type {\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n IProducer,\n} from './types';\n\n/**\n * Mock producer for when Kafka is disabled\n */\nexport class MockProducer implements IProducer {\n constructor(private readonly name: string, private readonly brokers: string[], private readonly logger: LoggerInstanceManager) {}\n\n readonly isConnected = true;\n\n /**\n * Get health snapshot for mock producer\n */\n getHealth(): ProducerHealth {\n return {\n name: this.name,\n enabled: false, // Mock mode means Kafka is disabled\n isConnected: true, // Mock is always \"connected\"\n lastPingAt: null,\n lastPingSucceededAt: null,\n lastError: null,\n clusterId: 'mock-cluster',\n brokerCount: 0,\n brokers: this.brokers,\n };\n }\n\n async ping(): Promise<void> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping ping`);\n return Promise.resolve();\n }\n\n async publish<T = unknown>(topic: string, value: T, _options?: PublishOptions): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping publish to topic: ${topic}`, { value });\n return Promise.resolve([{\n topic,\n partition: 0,\n offset: '0',\n }]);\n }\n\n async publishBatch<T = unknown>(topic: string, options: PublishBatchOptions<T>): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${topic}`, {\n messageCount: options.messages.length,\n });\n return Promise.resolve(options.messages.map(() => ({\n topic,\n partition: 0,\n offset: '0',\n })));\n }\n\n async disconnect(): Promise<void> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping disconnect`);\n return Promise.resolve();\n }\n}\n","import type { LoggerInstanceManager } from '@autofleet/logger';\nimport KafkaError from './kafkaError';\nimport { ProducerWrapper } from './producer';\nimport { MockProducer } from './mockProducer';\nimport type {\n KafkaManagerOptions,\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n BootstrapOptions,\n BootstrapResult,\n ReadinessOptions,\n ConnectionStatus,\n IProducer,\n ProducerConfig,\n TopicsConfig,\n TopicDefinition,\n TopicPayload,\n} from './types';\n\n/**\n * Manager for multiple Kafka producers\n * Provides lifecycle management, health tracking, and operational guarantees\n *\n * @template TProducers - Union of producer names\n * @template TTopics - Typed topics configuration with Zod schemas\n */\nexport class KafkaManager<TProducers extends string = string, TTopics extends TopicsConfig = {}> {\n private readonly producers = new Map<string, IProducer>();\n private readonly topics = new Map<string, TopicDefinition>();\n private readonly logger: LoggerInstanceManager;\n private readonly enabled: boolean;\n private gracefulShutdownStarted = false;\n private fatalError: Error | null = null;\n\n // Configuration options\n private readonly healthCheckTimeoutMs: number;\n private readonly healthCheckCacheMs: number;\n private readonly bootstrapTimeoutMs: number;\n private readonly strictBootstrap: boolean;\n\n // Health check cache\n private lastReadinessCheck: { result: boolean; timestamp: number; } | null = null;\n\n private constructor(options: KafkaManagerOptions) {\n this.logger = options.logger;\n this.enabled = options.enabled ?? true;\n this.healthCheckTimeoutMs = options.healthCheckTimeoutMs ?? 5000;\n this.healthCheckCacheMs = options.healthCheckCacheMs ?? 1000;\n this.bootstrapTimeoutMs = options.bootstrapTimeoutMs ?? 30_000;\n this.strictBootstrap = options.strictBootstrap ?? true;\n\n // Store topic definitions\n if (options.topics) {\n for (const [topicName, definition] of Object.entries(options.topics)) {\n this.topics.set(topicName, definition);\n }\n }\n\n this.logger.info('Kafka: Initialized KafkaManager', {\n enabled: this.enabled,\n producerCount: Object.keys(options.producers ?? {}).length,\n producers: Object.keys(options.producers ?? {}),\n topicCount: this.topics.size,\n topics: Array.from(this.topics.keys()),\n });\n\n // Create producers\n if (!this.enabled) {\n // Create mock producers\n for (const [name, config] of Object.entries(options.producers ?? {})) {\n this.producers.set(name, new MockProducer(name, config.brokers, this.logger));\n }\n if (this.producers.size > 0) {\n this.logger.info('Kafka: Created mock producers (Kafka disabled)');\n }\n } else {\n // Create real producers (they will initialize lazily)\n for (const [name, config] of Object.entries(options.producers ?? {})) {\n if (!config.brokers || config.brokers.length === 0) {\n throw new KafkaError(`[${name}] At least one broker is required`);\n }\n\n this.producers.set(name, new ProducerWrapper(name, config, this.logger));\n }\n }\n\n if (!options.dontGracefulShutdown) {\n this.setupGracefulShutdown();\n }\n }\n\n /**\n * Create a new KafkaManager instance with multiple named producers\n * Producers are initialized lazily on first use\n * Producer names are type-safe based on the configuration\n * Topics configuration provides type-safe publishing with Zod validation\n */\n static create<\n P extends Record<string, ProducerConfig>,\n T extends TopicsConfig = {},\n >(\n options: Omit<KafkaManagerOptions, 'producers' | 'topics'> & {\n producers?: P;\n topics?: T;\n },\n ): KafkaManager<keyof P & string, T> {\n // Validate that topic producers exist\n if (options.topics) {\n const producerNames = Object.keys(options.producers ?? {});\n for (const [topicName, topicDef] of Object.entries(options.topics)) {\n if (!producerNames.includes(topicDef.producer)) {\n throw new KafkaError(\n `Topic '${topicName}' references unknown producer '${topicDef.producer}'. `\n + `Available producers: ${producerNames.join(', ')}`,\n );\n }\n }\n }\n\n return new KafkaManager(options);\n }\n\n private setupGracefulShutdown(): void {\n this.logger.info(`Kafka: [graceful-shutdown] adding graceful shutdown for process.pid ${process.pid}`);\n\n process.on('SIGTERM', async () => {\n await this.gracefulShutdown('SIGTERM');\n });\n\n process.on('SIGINT', async () => {\n await this.gracefulShutdown('SIGINT');\n });\n }\n\n public async gracefulShutdown(signal: string): Promise<void> {\n if (this.gracefulShutdownStarted) {\n return;\n }\n\n this.gracefulShutdownStarted = true;\n\n this.logger.info(`Kafka: [graceful-shutdown] received ${signal}! Disconnecting all producers...`);\n\n try {\n await this.disconnect();\n this.logger.info('Kafka: [graceful-shutdown] all producers disconnected successfully');\n } catch (error) {\n this.logger.error('Kafka: [graceful-shutdown] error during shutdown', { error });\n throw error;\n }\n }\n\n /**\n * Check if Kafka is enabled\n */\n get isEnabled(): boolean {\n return this.enabled;\n }\n\n /**\n * Get all producer names\n */\n get producerNames(): TProducers[] {\n return Array.from(this.producers.keys()) as TProducers[];\n }\n\n /**\n * Check if a producer exists\n */\n hasProducer(name: TProducers): boolean {\n return this.producers.has(name);\n }\n\n /**\n * Get a specific producer\n */\n private getProducer(name: TProducers): IProducer {\n const producer = this.producers.get(name);\n if (!producer) {\n throw new KafkaError(`Producer '${name}' not found. Available producers: ${this.producerNames.join(', ')}`);\n }\n return producer;\n }\n\n /**\n * Explicitly bootstrap all (or specific) producers\n * Connects to brokers, validates connectivity, and returns detailed results\n *\n * @example\n * // Bootstrap all producers with defaults (30s timeout, strict mode)\n * await kafka.bootstrap();\n *\n * @example\n * // Bootstrap with custom timeout and non-strict mode\n * const result = await kafka.bootstrap({ timeoutMs: 10000, strict: false });\n * if (!result.success) {\n * console.error('Some producers failed:', result.results);\n * }\n *\n * @example\n * // Bootstrap specific producers only\n * await kafka.bootstrap({ producers: ['main'] });\n */\n async bootstrap(options?: BootstrapOptions): Promise<BootstrapResult> {\n const startTime = Date.now();\n const timeoutMs = options?.timeoutMs ?? this.bootstrapTimeoutMs;\n const strict = options?.strict ?? this.strictBootstrap;\n const producersToBootstrap = options?.producers ?? this.producerNames;\n\n this.logger.info('Kafka: Starting bootstrap', {\n producers: producersToBootstrap,\n timeout: timeoutMs,\n strict,\n });\n\n const results: BootstrapResult['results'] = {};\n const errors: string[] = [];\n\n // Bootstrap each producer\n for (const name of producersToBootstrap) {\n const producer = this.producers.get(name);\n if (!producer) {\n results[name] = {\n success: false,\n duration: 0,\n error: `Producer '${name}' not found`,\n };\n errors.push(`Producer '${name}' not found`);\n continue;\n }\n\n const producerStartTime = Date.now();\n try {\n await producer.ping({ timeout: timeoutMs, force: true });\n const health = producer.getHealth();\n\n results[name] = {\n success: true,\n duration: Date.now() - producerStartTime,\n clusterId: health.clusterId ?? undefined,\n brokerCount: health.brokerCount,\n };\n\n this.logger.info(`Kafka: [${name}] Bootstrap successful`, {\n duration: Date.now() - producerStartTime,\n clusterId: health.clusterId,\n brokerCount: health.brokerCount,\n });\n } catch (error) {\n const err = error as Error;\n results[name] = {\n success: false,\n duration: Date.now() - producerStartTime,\n error: err.message,\n };\n errors.push(`[${name}] ${err.message}`);\n\n this.logger.error(`Kafka: [${name}] Bootstrap failed`, {\n duration: Date.now() - producerStartTime,\n error: err.message,\n });\n }\n }\n\n const totalDuration = Date.now() - startTime;\n const successCount = Object.values(results).filter(r => r.success).length;\n const totalCount = Object.keys(results).length;\n const success = strict ? successCount === totalCount : successCount > 0;\n\n const result: BootstrapResult = {\n success,\n duration: totalDuration,\n results,\n };\n\n if (success) {\n this.logger.info('Kafka: Bootstrap completed successfully', {\n duration: totalDuration,\n successCount,\n totalCount,\n });\n } else {\n const errorMessage = `Kafka bootstrap failed (${successCount}/${totalCount} producers succeeded)\\n\\n`\n + `Failures:\\n${errors.map(e => ` - ${e}`).join('\\n')}\\n\\n`\n + `Configuration:\\n`\n + ` Strict mode: ${strict}\\n`\n + ` Timeout: ${timeoutMs}ms\\n`\n + ` Producers attempted: ${producersToBootstrap.join(', ')}`;\n\n this.logger.error('Kafka: Bootstrap failed', {\n duration: totalDuration,\n successCount,\n totalCount,\n errors,\n });\n\n throw new KafkaError(errorMessage);\n }\n\n return result;\n }\n\n /**\n * Get detailed health snapshot for all producers\n * Returns comprehensive metadata including timestamps, errors, cluster info\n *\n * @example\n * const health = kafka.getHealth();\n * console.log(health.main.lastPingSucceededAt); // timestamp\n * console.log(health.main.clusterId); // 'prod-kafka-01'\n */\n getHealth(): Record<TProducers, ProducerHealth> {\n const health: Record<string, ProducerHealth> = {};\n for (const [name, producer] of this.producers.entries()) {\n health[name] = producer.getHealth();\n }\n return health as Record<TProducers, ProducerHealth>;\n }\n\n /**\n * Get detailed health for a specific producer\n */\n getProducerHealth(name: TProducers): ProducerHealth {\n const producer = this.getProducer(name);\n return producer.getHealth();\n }\n\n /**\n * Lightweight liveness check - does NOT perform Kafka operations\n * Only checks internal state for fatal errors\n * Suitable for Kubernetes liveness probes\n *\n * Returns false if:\n * - Manager is in a fatal state\n * - Graceful shutdown has started\n *\n * @example\n * app.get('/health/live', (req, res) => {\n * res.status(kafka.isLive() ? 200 : 503).json({ live: kafka.isLive() });\n * });\n */\n isLive(): boolean {\n return !this.fatalError && !this.gracefulShutdownStarted;\n }\n\n /**\n * Ping all producers to verify connectivity\n */\n async ping(): Promise<void> {\n const promises = Array.from(this.producers.values(), p => p.ping());\n await Promise.all(promises);\n }\n\n /**\n * Ping a specific producer\n */\n async pingProducer(name: TProducers): Promise<void> {\n await this.getProducer(name).ping();\n }\n\n /**\n * Check if Kafka is ready to handle traffic\n * Revalidates connectivity if cache is stale\n *\n * @param options.timeout - Override default health check timeout\n * @param options.force - Force revalidation, ignore cache\n *\n * @example\n * // Use cached result if fresh (within healthCheckCacheMs)\n * const ready = await kafka.isReady();\n *\n * @example\n * // Force immediate revalidation\n * const ready = await kafka.isReady({ force: true });\n */\n async isReady(options?: ReadinessOptions): Promise<boolean> {\n if (!this.enabled) {\n return true;\n }\n\n const now = Date.now();\n const force = options?.force ?? false;\n\n // Use cached result if available and fresh\n if (!force && this.lastReadinessCheck) {\n const age = now - this.lastReadinessCheck.timestamp;\n if (age < this.healthCheckCacheMs) {\n this.logger.debug('Kafka: Using cached readiness result', {\n result: this.lastReadinessCheck.result,\n age,\n });\n return this.lastReadinessCheck.result;\n }\n }\n\n // Revalidate connectivity\n try {\n const timeout = options?.timeout ?? this.healthCheckTimeoutMs;\n const promises = Array.from(this.producers.values()).map(p =>\n p.ping({ timeout, force: true }),\n );\n await Promise.all(promises);\n\n this.lastReadinessCheck = { result: true, timestamp: now };\n return true;\n } catch (error) {\n this.lastReadinessCheck = { result: false, timestamp: now };\n this.logger.warn('Kafka: Readiness check failed', {\n error: (error as Error).message,\n });\n return false;\n }\n }\n\n /**\n * Check connection status of all producers\n * Returns detailed status including last success time and errors\n */\n getConnectionStatus(): Record<TProducers, ConnectionStatus> {\n const status: Record<string, ConnectionStatus> = {};\n for (const [name, producer] of this.producers.entries()) {\n const health = producer.getHealth();\n status[name] = {\n connected: health.isConnected,\n lastSuccessAt: health.lastPingSucceededAt,\n lastError: health.lastError?.message ?? null,\n };\n }\n return status as Record<TProducers, ConnectionStatus>;\n }\n\n /**\n * Get a topic definition\n */\n private getTopicDefinition<TName extends keyof TTopics & string>(topicName: TName): TopicDefinition {\n const topic = this.topics.get(topicName);\n if (!topic) {\n throw new KafkaError(\n `Topic '${topicName}' not found. Available topics: ${Array.from(this.topics.keys()).join(', ')}`,\n );\n }\n return topic;\n }\n\n /**\n * Publish a message to a typed topic\n * Automatically validates payload against Zod schema and routes to correct producer\n *\n * @template TName - Topic name (inferred from topics config)\n * @param topicName - Name of the topic to publish to\n * @param value - Payload (type-checked against topic's Zod schema)\n * @param options - Optional publish options including headers, key, partition\n *\n * @example\n * await kafka.publish('order.created', {\n * orderId: '123',\n * amount: 99.99,\n * customerId: '456',\n * });\n */\n async publish<TName extends keyof TTopics & string>(\n topicName: TName,\n value: TopicPayload<TTopics, TName>,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n const topicDef = this.getTopicDefinition(topicName);\n\n // Validate payload against schema unless skipped\n if (!options?.skipValidation) {\n try {\n topicDef.schema.parse(value);\n } catch (error) {\n this.logger.error(`Kafka: Validation failed for topic '${topicName}'`, { error, value });\n throw new KafkaError(\n `Validation failed for topic '${topicName}': ${(error as Error).message}`,\n { cause: error },\n );\n }\n }\n\n // Get producer and publish\n const producer = this.getProducer(topicDef.producer as TProducers);\n return producer.publish(topicName, value, options);\n }\n\n /**\n * Publish a batch of messages to a typed topic\n * Automatically validates payloads against Zod schema and routes to correct producer\n *\n * @template TName - Topic name (inferred from topics config)\n * @param topicName - Name of the topic to publish to\n * @param options - Batch options with array of messages\n *\n * @example\n * await kafka.publishBatch('order.created', {\n * messages: [\n * { value: { orderId: '1', amount: 10, customerId: 'a' } },\n * { value: { orderId: '2', amount: 20, customerId: 'b' } },\n * ],\n * });\n */\n async publishBatch<TName extends keyof TTopics & string>(\n topicName: TName,\n options: PublishBatchOptions<TopicPayload<TTopics, TName>>,\n ): Promise<RecordMetadata[]> {\n const topicDef = this.getTopicDefinition(topicName);\n\n // Validate all payloads against schema unless skipped\n if (!options.skipValidation) {\n for (let i = 0; i < options.messages.length; i++) {\n try {\n topicDef.schema.parse(options.messages[i].value);\n } catch (error) {\n this.logger.error(`Kafka: Validation failed for topic '${topicName}' message ${i}`, {\n error,\n value: options.messages[i].value,\n });\n throw new KafkaError(\n `Validation failed for topic '${topicName}' message ${i}: ${(error as Error).message}`,\n { cause: error },\n );\n }\n }\n }\n\n // Get producer and publish batch\n const producer = this.getProducer(topicDef.producer as TProducers);\n return producer.publishBatch(topicName, options);\n }\n\n /**\n * Disconnect all producers\n */\n async disconnect(): Promise<void> {\n const promises = Array.from(this.producers.values()).map(p => p.disconnect());\n await Promise.all(promises);\n this.logger.info('Kafka: All producers disconnected');\n }\n\n /**\n * Disconnect a specific producer\n */\n async disconnectProducer(name: TProducers): Promise<void> {\n await this.getProducer(name).disconnect();\n }\n}\n"],"mappings":"qkBAAA,IAAqB,EAArB,cAAwC,KAAM,yCACrC,eCDT,MAAa,EAAmB,cAEnB,EAAoB,2BCE3B,EAAmB,GAEnB,EAAc,GACl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AQ,EAAW,EAAO,EAAQ,CAmBpD,MAAM,aACJ,EACA,EAC2B,CAC3B,IAAM,EAAW,KAAK,mBAAmB,EAAU,CAGnD,GAAI,CAAC,EAAQ,eACX,IAAK,IAAI,EAAI,EAAG,EAAI,EAAQ,SAAS,OAAQ,IAC3C,GAAI,CACF,EAAS,OAAO,MAAM,EAAQ,SAAS,GAAG,MAAM,OACzC,EAAO,CAKd,MAJA,KAAK,OAAO,MAAM,uCAAuC,EAAU,YAAY,IAAK,CAClF,QACA,MAAO,EAAQ,SAAS,GAAG,MAC5B,CAAC,CACI,IAAI,EACR,gCAAgC,EAAU,YAAY,EAAE,IAAK,EAAgB,UAC7E,CAAE,MAAO,EAAO,CACjB,CAOP,OADiB,KAAK,YAAY,EAAS,SAAuB,CAClD,aAAa,EAAW,EAAQ,CAMlD,MAAM,YAA4B,CAChC,IAAM,EAAW,MAAM,KAAK,KAAK,UAAU,QAAQ,CAAC,CAAC,IAAI,GAAK,EAAE,YAAY,CAAC,CAC7E,MAAM,QAAQ,IAAI,EAAS,CAC3B,KAAK,OAAO,KAAK,oCAAoC,CAMvD,MAAM,mBAAmB,EAAiC,CACxD,MAAM,KAAK,YAAY,EAAK,CAAC,YAAY"}
package/dist/index.d.cts CHANGED
@@ -21,6 +21,12 @@ type TopicsConfig = Record<string, TopicDefinition>;
21
21
  * Extract payload type for a specific topic
22
22
  */
23
23
  type TopicPayload<TTopics extends TopicsConfig, TName extends keyof TTopics> = z.infer<TTopics[TName]["schema"]>;
24
+ interface OauthBearerProviderOptions {
25
+ logger?: LoggerInstanceManager;
26
+ serviceAccountEmail?: string;
27
+ refreshSkewSec?: number;
28
+ enableMetadataLookup?: boolean;
29
+ }
24
30
  interface ProducerConfig {
25
31
  /** Kafka broker addresses */
26
32
  brokers: string[];
@@ -34,6 +40,7 @@ interface ProducerConfig {
34
40
  autoCreateTopics?: boolean;
35
41
  /** TLS configuration options */
36
42
  tls?: ConnectionOptions["tls"];
43
+ oauthBearerConfig?: OauthBearerProviderOptions;
37
44
  }
38
45
  interface KafkaManagerOptions {
39
46
  /** Enable/disable Kafka - when false, returns mock implementations */
package/dist/index.d.ts CHANGED
@@ -21,6 +21,12 @@ type TopicsConfig = Record<string, TopicDefinition>;
21
21
  * Extract payload type for a specific topic
22
22
  */
23
23
  type TopicPayload<TTopics extends TopicsConfig, TName extends keyof TTopics> = z.infer<TTopics[TName]["schema"]>;
24
+ interface OauthBearerProviderOptions {
25
+ logger?: LoggerInstanceManager;
26
+ serviceAccountEmail?: string;
27
+ refreshSkewSec?: number;
28
+ enableMetadataLookup?: boolean;
29
+ }
24
30
  interface ProducerConfig {
25
31
  /** Kafka broker addresses */
26
32
  brokers: string[];
@@ -34,6 +40,7 @@ interface ProducerConfig {
34
40
  autoCreateTopics?: boolean;
35
41
  /** TLS configuration options */
36
42
  tls?: ConnectionOptions["tls"];
43
+ oauthBearerConfig?: OauthBearerProviderOptions;
37
44
  }
38
45
  interface KafkaManagerOptions {
39
46
  /** Enable/disable Kafka - when false, returns mock implementations */
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import{setTimeout as e}from"node:timers/promises";var t=class extends Error{constructor(...e){super(...e),this.name=`KafkaError`}};const n=`x-timestamp`,r=`autofleet-kafka-producer`;var i=class{constructor(e,t,n){this.name=e,this.config=t,this.logger=n,this.producer=null,this._isConnected=!1,this.initPromise=null,this._lastPingAt=null,this._lastPingSucceededAt=null,this._lastError=null,this._clusterId=null,this._brokerCount=0}async#e(){let{Producer:e,stringSerializers:t}=await import(`@platformatic/kafka`);this.producer=new e({bootstrapBrokers:this.config.brokers,clientId:this.config.clientId||`${r}-${this.name}`,serializers:t,autocreateTopics:this.config.autoCreateTopics??!1,sasl:this.config.sasl,tls:this.config.tls}),this.logger.info(`Kafka: [${this.name}] Initialized producer`,{clientId:this.config.clientId||`${r}-${this.name}`,brokers:this.config.brokers}),this.initPromise=null}async initialize(){this.producer||(this.initPromise??=this.#e(),await this.initPromise)}get isConnected(){return this._isConnected}getHealth(){return{name:this.name,enabled:!0,isConnected:this._isConnected,lastPingAt:this._lastPingAt,lastPingSucceededAt:this._lastPingSucceededAt,lastError:this._lastError,clusterId:this._clusterId,brokerCount:this._brokerCount,brokers:this.config.brokers}}buildConnectionErrorMessage(e){let t=this.config.brokers.join(`, `),n=this.config.brokers[0],[i,a]=n?n.split(`:`):[``,``];return`[${this.name}] Failed to connect to Kafka brokers: ${e.message}\n\nPossible causes:\n 1. Brokers are unreachable: ${t}\n 2. Network connectivity issues\n 3. Firewall blocking ports\n 4. ${this.config.sasl?`SASL authentication failed`:`Authentication not configured but required`}\n\nTroubleshooting:\n - Verify brokers are running and accessible\n`+(i&&a?` - Test connectivity: nc -zv ${i} ${a}\n`:``)+` - Check network policies and firewall rules\n ${this.config.sasl?`- Verify SASL credentials are correct
2
- `:``}\nCurrent configuration:\n Brokers: ${t}\n Client ID: ${this.config.clientId||`${r}-${this.name}`}\n SASL: ${this.config.sasl?`enabled (${this.config.sasl.mechanism})`:`disabled`}\n SASL Password: ${this.config.sasl?this.config.sasl.password?`****`:`not set`:`N/A`}\n Auto-create topics: ${this.config.autoCreateTopics??!1}`}async ping(n){if(this._lastPingAt=Date.now(),!(this._isConnected&&!n?.force)){await this.initialize();try{let t=n?.timeout??5e3,r=Error(`Connection timeout after ${t}ms`),i=e(t).then(()=>{throw r}),a=await Promise.race([this.producer.metadata({topics:[]}),i]);this._isConnected=!0,this._lastPingSucceededAt=Date.now(),this._clusterId=a.id,this._brokerCount=a.brokers.size,this._lastError=null,this.logger.info(`Kafka: [${this.name}] Successfully connected to cluster`,{clusterId:a.id,brokers:a.brokers.size,duration:Date.now()-this._lastPingAt})}catch(e){let n=e;throw this._isConnected=!1,this._lastError={message:n.message,timestamp:Date.now(),stack:n.stack},this.logger.error(`Kafka: [${this.name}] Failed to connect to brokers`,{error:n.message,duration:Date.now()-this._lastPingAt}),new t(this.buildConnectionErrorMessage(n),{cause:n})}}}async publish(e,r,i){if(!e)throw new t(`[${this.name}] Topic name is required`);await this.ping();try{let t={[n]:Date.now().toString(),...i?.headers},a=await this.producer.send({messages:[{topic:e,value:JSON.stringify(r),key:i?.key,partition:i?.partition,headers:t}]});return this.logger.debug(`Kafka: [${this.name}] Published message to topic ${e}`,{topic:e}),[{topic:e,partition:i?.partition??0,offset:a?.offsets?.[0]?.offset?.toString()??`0`}]}catch(n){let i=n;throw this.logger.error(`Kafka: [${this.name}] Error publishing to topic ${e}`,{error:n,value:r}),new t(`[${this.name}] Failed to publish to topic ${e}: ${i.message||`Unknown error`}`,{cause:n})}}async publishBatch(e,r){if(!e)throw new t(`[${this.name}] Topic name is required`);if(!r.messages||r.messages.length===0)throw new t(`[${this.name}] At least one message is required`);await this.ping();try{let t=r.messages.map(t=>({topic:e,value:JSON.stringify(t.value),key:t.key,partition:t.partition,headers:{[n]:Date.now().toString(),...t.headers}})),i=await this.producer.send({messages:t});return this.logger.debug(`Kafka: [${this.name}] Published ${t.length} messages to topic ${e}`,{topic:e,count:t.length}),t.map((t,n)=>({topic:e,partition:t.partition??0,offset:i?.offsets?.[n]?.offset?.toString()??n.toString()}))}catch(n){let r=n;throw this.logger.error(`Kafka: [${this.name}] Error publishing batch to topic ${e}`,{error:n}),new t(`[${this.name}] Failed to publish batch to topic ${e}: ${r.message||`Unknown error`}`,{cause:n})}}async disconnect(){if(!this.producer||!this._isConnected){this.logger.debug(`Kafka: [${this.name}] Producer already disconnected`);return}this.logger.info(`Kafka: [${this.name}] Disconnecting producer`);try{await this.producer.close(),this._isConnected=!1,this.logger.info(`Kafka: [${this.name}] Producer disconnected successfully`)}catch(e){throw this.logger.error(`Kafka: [${this.name}] Error disconnecting producer`,{error:e}),new t(`[${this.name}] Failed to disconnect producer: ${e.message}`,{cause:e})}}},a=class{constructor(e,t,n){this.name=e,this.brokers=t,this.logger=n,this.isConnected=!0}getHealth(){return{name:this.name,enabled:!1,isConnected:!0,lastPingAt:null,lastPingSucceededAt:null,lastError:null,clusterId:`mock-cluster`,brokerCount:0,brokers:this.brokers}}async ping(){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping ping`),Promise.resolve()}async publish(e,t,n){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping publish to topic: ${e}`,{value:t}),Promise.resolve([{topic:e,partition:0,offset:`0`}])}async publishBatch(e,t){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${e}`,{messageCount:t.messages.length}),Promise.resolve(t.messages.map(()=>({topic:e,partition:0,offset:`0`})))}async disconnect(){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping disconnect`),Promise.resolve()}},o=class e{constructor(e){if(this.producers=new Map,this.topics=new Map,this.gracefulShutdownStarted=!1,this.fatalError=null,this.lastReadinessCheck=null,this.logger=e.logger,this.enabled=e.enabled??!0,this.healthCheckTimeoutMs=e.healthCheckTimeoutMs??5e3,this.healthCheckCacheMs=e.healthCheckCacheMs??1e3,this.bootstrapTimeoutMs=e.bootstrapTimeoutMs??3e4,this.strictBootstrap=e.strictBootstrap??!0,e.topics)for(let[t,n]of Object.entries(e.topics))this.topics.set(t,n);if(this.logger.info(`Kafka: Initialized KafkaManager`,{enabled:this.enabled,producerCount:Object.keys(e.producers??{}).length,producers:Object.keys(e.producers??{}),topicCount:this.topics.size,topics:Array.from(this.topics.keys())}),this.enabled)for(let[n,r]of Object.entries(e.producers??{})){if(!r.brokers||r.brokers.length===0)throw new t(`[${n}] At least one broker is required`);this.producers.set(n,new i(n,r,this.logger))}else{for(let[t,n]of Object.entries(e.producers??{}))this.producers.set(t,new a(t,n.brokers,this.logger));this.producers.size>0&&this.logger.info(`Kafka: Created mock producers (Kafka disabled)`)}e.dontGracefulShutdown||this.setupGracefulShutdown()}static create(n){if(n.topics){let e=Object.keys(n.producers??{});for(let[r,i]of Object.entries(n.topics))if(!e.includes(i.producer))throw new t(`Topic '${r}' references unknown producer '${i.producer}'. Available producers: ${e.join(`, `)}`)}return new e(n)}setupGracefulShutdown(){this.logger.info(`Kafka: [graceful-shutdown] adding graceful shutdown for process.pid ${process.pid}`),process.on(`SIGTERM`,async()=>{await this.gracefulShutdown(`SIGTERM`)}),process.on(`SIGINT`,async()=>{await this.gracefulShutdown(`SIGINT`)})}async gracefulShutdown(e){if(!this.gracefulShutdownStarted){this.gracefulShutdownStarted=!0,this.logger.info(`Kafka: [graceful-shutdown] received ${e}! Disconnecting all producers...`);try{await this.disconnect(),this.logger.info(`Kafka: [graceful-shutdown] all producers disconnected successfully`)}catch(e){throw this.logger.error(`Kafka: [graceful-shutdown] error during shutdown`,{error:e}),e}}}get isEnabled(){return this.enabled}get producerNames(){return Array.from(this.producers.keys())}hasProducer(e){return this.producers.has(e)}getProducer(e){let n=this.producers.get(e);if(!n)throw new t(`Producer '${e}' not found. Available producers: ${this.producerNames.join(`, `)}`);return n}async bootstrap(e){let n=Date.now(),r=e?.timeoutMs??this.bootstrapTimeoutMs,i=e?.strict??this.strictBootstrap,a=e?.producers??this.producerNames;this.logger.info(`Kafka: Starting bootstrap`,{producers:a,timeout:r,strict:i});let o={},s=[];for(let e of a){let t=this.producers.get(e);if(!t){o[e]={success:!1,duration:0,error:`Producer '${e}' not found`},s.push(`Producer '${e}' not found`);continue}let n=Date.now();try{await t.ping({timeout:r,force:!0});let i=t.getHealth();o[e]={success:!0,duration:Date.now()-n,clusterId:i.clusterId??void 0,brokerCount:i.brokerCount},this.logger.info(`Kafka: [${e}] Bootstrap successful`,{duration:Date.now()-n,clusterId:i.clusterId,brokerCount:i.brokerCount})}catch(t){let r=t;o[e]={success:!1,duration:Date.now()-n,error:r.message},s.push(`[${e}] ${r.message}`),this.logger.error(`Kafka: [${e}] Bootstrap failed`,{duration:Date.now()-n,error:r.message})}}let c=Date.now()-n,l=Object.values(o).filter(e=>e.success).length,u=Object.keys(o).length,d=i?l===u:l>0,f={success:d,duration:c,results:o};if(d)this.logger.info(`Kafka: Bootstrap completed successfully`,{duration:c,successCount:l,totalCount:u});else{let e=`Kafka bootstrap failed (${l}/${u} producers succeeded)\n\nFailures:\n${s.map(e=>` - ${e}`).join(`
3
- `)}\n\nConfiguration:\n Strict mode: ${i}\n Timeout: ${r}ms\n Producers attempted: ${a.join(`, `)}`;throw this.logger.error(`Kafka: Bootstrap failed`,{duration:c,successCount:l,totalCount:u,errors:s}),new t(e)}return f}getHealth(){let e={};for(let[t,n]of this.producers.entries())e[t]=n.getHealth();return e}getProducerHealth(e){return this.getProducer(e).getHealth()}isLive(){return!this.fatalError&&!this.gracefulShutdownStarted}async ping(){let e=Array.from(this.producers.values(),e=>e.ping());await Promise.all(e)}async pingProducer(e){await this.getProducer(e).ping()}async isReady(e){if(!this.enabled)return!0;let t=Date.now();if(!(e?.force??!1)&&this.lastReadinessCheck){let e=t-this.lastReadinessCheck.timestamp;if(e<this.healthCheckCacheMs)return this.logger.debug(`Kafka: Using cached readiness result`,{result:this.lastReadinessCheck.result,age:e}),this.lastReadinessCheck.result}try{let n=e?.timeout??this.healthCheckTimeoutMs,r=Array.from(this.producers.values()).map(e=>e.ping({timeout:n,force:!0}));return await Promise.all(r),this.lastReadinessCheck={result:!0,timestamp:t},!0}catch(e){return this.lastReadinessCheck={result:!1,timestamp:t},this.logger.warn(`Kafka: Readiness check failed`,{error:e.message}),!1}}getConnectionStatus(){let e={};for(let[t,n]of this.producers.entries()){let r=n.getHealth();e[t]={connected:r.isConnected,lastSuccessAt:r.lastPingSucceededAt,lastError:r.lastError?.message??null}}return e}getTopicDefinition(e){let n=this.topics.get(e);if(!n)throw new t(`Topic '${e}' not found. Available topics: ${Array.from(this.topics.keys()).join(`, `)}`);return n}async publish(e,n,r){let i=this.getTopicDefinition(e);if(!r?.skipValidation)try{i.schema.parse(n)}catch(r){throw this.logger.error(`Kafka: Validation failed for topic '${e}'`,{error:r,value:n}),new t(`Validation failed for topic '${e}': ${r.message}`,{cause:r})}return this.getProducer(i.producer).publish(e,n,r)}async publishBatch(e,n){let r=this.getTopicDefinition(e);if(!n.skipValidation)for(let i=0;i<n.messages.length;i++)try{r.schema.parse(n.messages[i].value)}catch(r){throw this.logger.error(`Kafka: Validation failed for topic '${e}' message ${i}`,{error:r,value:n.messages[i].value}),new t(`Validation failed for topic '${e}' message ${i}: ${r.message}`,{cause:r})}return this.getProducer(r.producer).publishBatch(e,n)}async disconnect(){let e=Array.from(this.producers.values()).map(e=>e.disconnect());await Promise.all(e),this.logger.info(`Kafka: All producers disconnected`)}async disconnectProducer(e){await this.getProducer(e).disconnect()}};export{t as KafkaError,o as KafkaManager};
1
+ import{setTimeout as e}from"node:timers/promises";import{GoogleAuth as t}from"google-auth-library";import n from"node:crypto";var r=class extends Error{constructor(...e){super(...e),this.name=`KafkaError`}};const i=`x-timestamp`,a=`autofleet-kafka-producer`,o=e=>Buffer.from(e,`utf8`).toString(`base64`).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/g,``),s=e=>n.createHash(`sha256`).update(e).digest(`hex`).slice(0,12),c=o(JSON.stringify({typ:`JWT`,alg:`GOOG_OAUTH2_TOKEN`})),l=(e={})=>{let n=e.logger,r=e.refreshSkewSec??60,i=e.enableMetadataLookup??!0,a=new t({scopes:[`https://www.googleapis.com/auth/cloud-platform`]}),l=null,u=e.serviceAccountEmail??null,d=null,f=null,p=async()=>(l??=a.getClient(),l),m=async()=>{try{let e=await fetch(`http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/email`,{headers:{"Metadata-Flavor":`Google`}});return e.ok?(await e.text()).trim():null}catch(e){return n?.warn?.(`ManagedKafkaAuth: metadata email lookup failed`,{err:String(e)}),null}},h=async()=>{if(u)return u;try{let e=await a.getCredentials();if(e?.client_email)return u=e.client_email,u}catch(e){n?.warn?.(`ManagedKafkaAuth: getCredentials failed`,{err:String(e)})}if(i){let e=await m();if(e)return u=e,u}if(process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL)return u=process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,u;throw Error(`ManagedKafkaAuth: cannot determine service account email (set GOOGLE_SERVICE_ACCOUNT_EMAIL)`)};return async()=>{let e=Math.floor(Date.now()/1e3);if(d&&f&&e<f-r)return d;let t=await p(),{token:i}=await t.getAccessToken();if(!i)throw Error(`ManagedKafkaAuth: no access token from ADC`);let a=t.credentials?.expiry_date;if(!a)throw Error(`ManagedKafkaAuth: no expiry_date on ADC credentials`);let l=await h(),u=Math.floor(a/1e3),m=`${c}.${o(JSON.stringify({exp:u,iss:`Google`,iat:e,sub:l}))}.${o(i)}`;return d=m,f=u,n?.info?.(`ManagedKafkaAuth: token generated`,{sub:l,iat:e,exp:u,fp:s(m)}),m}};var u=class{constructor(e,t,n){this.name=e,this.config=t,this.logger=n,this.producer=null,this._isConnected=!1,this.initPromise=null,this._lastPingAt=null,this._lastPingSucceededAt=null,this._lastError=null,this._clusterId=null,this._brokerCount=0}async#e(){let{Producer:e,stringSerializers:t}=await import(`@platformatic/kafka`),n=this.config.oauthBearerConfig??{logger:this.logger};this.producer=new e({bootstrapBrokers:this.config.brokers,clientId:this.config.clientId||`${a}-${this.name}`,serializers:t,autocreateTopics:this.config.autoCreateTopics??!1,sasl:this.config.sasl??{mechanism:`OAUTHBEARER`,token:l(n)},tls:this.config.tls??{}}),this.logger.info(`Kafka: [${this.name}] Initialized producer`,{clientId:this.config.clientId||`${a}-${this.name}`,brokers:this.config.brokers}),this.initPromise=null}async initialize(){this.producer||(this.initPromise??=this.#e(),await this.initPromise)}get isConnected(){return this._isConnected}getHealth(){return{name:this.name,enabled:!0,isConnected:this._isConnected,lastPingAt:this._lastPingAt,lastPingSucceededAt:this._lastPingSucceededAt,lastError:this._lastError,clusterId:this._clusterId,brokerCount:this._brokerCount,brokers:this.config.brokers}}buildConnectionErrorMessage(e){let t=this.config.brokers.join(`, `),n=this.config.brokers[0],[r,i]=n?n.split(`:`):[``,``];return`[${this.name}] Failed to connect to Kafka brokers: ${e.message}\n\nPossible causes:\n 1. Brokers are unreachable: ${t}\n 2. Network connectivity issues\n 3. Firewall blocking ports\n 4. ${this.config.sasl?`SASL authentication failed`:`Authentication not configured but required`}\n\nTroubleshooting:\n - Verify brokers are running and accessible\n`+(r&&i?` - Test connectivity: nc -zv ${r} ${i}\n`:``)+` - Check network policies and firewall rules\n ${this.config.sasl?`- Verify SASL credentials are correct
2
+ `:``}\nCurrent configuration:\n Brokers: ${t}\n Client ID: ${this.config.clientId||`${a}-${this.name}`}\n SASL: ${this.config.sasl?`enabled (${this.config.sasl.mechanism})`:`disabled`}\n SASL Password: ${this.config.sasl?this.config.sasl.password?`****`:`not set`:`N/A`}\n Auto-create topics: ${this.config.autoCreateTopics??!1}`}async ping(t){if(this._lastPingAt=Date.now(),!(this._isConnected&&!t?.force)){await this.initialize();try{let n=t?.timeout??5e3,r=Error(`Connection timeout after ${n}ms`),i=e(n).then(()=>{throw r}),a=await Promise.race([this.producer.metadata({topics:[]}),i]);this._isConnected=!0,this._lastPingSucceededAt=Date.now(),this._clusterId=a.id,this._brokerCount=a.brokers.size,this._lastError=null,this.logger.info(`Kafka: [${this.name}] Successfully connected to cluster`,{clusterId:a.id,brokers:a.brokers.size,duration:Date.now()-this._lastPingAt})}catch(e){let t=e;throw this._isConnected=!1,this._lastError={message:t.message,timestamp:Date.now(),stack:t.stack},this.logger.error(`Kafka: [${this.name}] Failed to connect to brokers`,{error:t.message,duration:Date.now()-this._lastPingAt}),new r(this.buildConnectionErrorMessage(t),{cause:t})}}}async publish(e,t,n){if(!e)throw new r(`[${this.name}] Topic name is required`);await this.ping();try{let r={[i]:Date.now().toString(),...n?.headers},a=await this.producer.send({messages:[{topic:e,value:JSON.stringify(t),key:n?.key,partition:n?.partition,headers:r}]});return this.logger.debug(`Kafka: [${this.name}] Published message to topic ${e}`,{topic:e}),[{topic:e,partition:n?.partition??0,offset:a?.offsets?.[0]?.offset?.toString()??`0`}]}catch(n){let i=n;throw this.logger.error(`Kafka: [${this.name}] Error publishing to topic ${e}`,{error:n,value:t}),new r(`[${this.name}] Failed to publish to topic ${e}: ${i.message||`Unknown error`}`,{cause:n})}}async publishBatch(e,t){if(!e)throw new r(`[${this.name}] Topic name is required`);if(!t.messages||t.messages.length===0)throw new r(`[${this.name}] At least one message is required`);await this.ping();try{let n=t.messages.map(t=>({topic:e,value:JSON.stringify(t.value),key:t.key,partition:t.partition,headers:{[i]:Date.now().toString(),...t.headers}})),r=await this.producer.send({messages:n});return this.logger.debug(`Kafka: [${this.name}] Published ${n.length} messages to topic ${e}`,{topic:e,count:n.length}),n.map((t,n)=>({topic:e,partition:t.partition??0,offset:r?.offsets?.[n]?.offset?.toString()??n.toString()}))}catch(t){let n=t;throw this.logger.error(`Kafka: [${this.name}] Error publishing batch to topic ${e}`,{error:t}),new r(`[${this.name}] Failed to publish batch to topic ${e}: ${n.message||`Unknown error`}`,{cause:t})}}async disconnect(){if(!this.producer||!this._isConnected){this.logger.debug(`Kafka: [${this.name}] Producer already disconnected`);return}this.logger.info(`Kafka: [${this.name}] Disconnecting producer`);try{await this.producer.close(),this._isConnected=!1,this.logger.info(`Kafka: [${this.name}] Producer disconnected successfully`)}catch(e){throw this.logger.error(`Kafka: [${this.name}] Error disconnecting producer`,{error:e}),new r(`[${this.name}] Failed to disconnect producer: ${e.message}`,{cause:e})}}},d=class{constructor(e,t,n){this.name=e,this.brokers=t,this.logger=n,this.isConnected=!0}getHealth(){return{name:this.name,enabled:!1,isConnected:!0,lastPingAt:null,lastPingSucceededAt:null,lastError:null,clusterId:`mock-cluster`,brokerCount:0,brokers:this.brokers}}async ping(){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping ping`),Promise.resolve()}async publish(e,t,n){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping publish to topic: ${e}`,{value:t}),Promise.resolve([{topic:e,partition:0,offset:`0`}])}async publishBatch(e,t){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${e}`,{messageCount:t.messages.length}),Promise.resolve(t.messages.map(()=>({topic:e,partition:0,offset:`0`})))}async disconnect(){return this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping disconnect`),Promise.resolve()}},f=class e{constructor(e){if(this.producers=new Map,this.topics=new Map,this.gracefulShutdownStarted=!1,this.fatalError=null,this.lastReadinessCheck=null,this.logger=e.logger,this.enabled=e.enabled??!0,this.healthCheckTimeoutMs=e.healthCheckTimeoutMs??5e3,this.healthCheckCacheMs=e.healthCheckCacheMs??1e3,this.bootstrapTimeoutMs=e.bootstrapTimeoutMs??3e4,this.strictBootstrap=e.strictBootstrap??!0,e.topics)for(let[t,n]of Object.entries(e.topics))this.topics.set(t,n);if(this.logger.info(`Kafka: Initialized KafkaManager`,{enabled:this.enabled,producerCount:Object.keys(e.producers??{}).length,producers:Object.keys(e.producers??{}),topicCount:this.topics.size,topics:Array.from(this.topics.keys())}),this.enabled)for(let[t,n]of Object.entries(e.producers??{})){if(!n.brokers||n.brokers.length===0)throw new r(`[${t}] At least one broker is required`);this.producers.set(t,new u(t,n,this.logger))}else{for(let[t,n]of Object.entries(e.producers??{}))this.producers.set(t,new d(t,n.brokers,this.logger));this.producers.size>0&&this.logger.info(`Kafka: Created mock producers (Kafka disabled)`)}e.dontGracefulShutdown||this.setupGracefulShutdown()}static create(t){if(t.topics){let e=Object.keys(t.producers??{});for(let[n,i]of Object.entries(t.topics))if(!e.includes(i.producer))throw new r(`Topic '${n}' references unknown producer '${i.producer}'. Available producers: ${e.join(`, `)}`)}return new e(t)}setupGracefulShutdown(){this.logger.info(`Kafka: [graceful-shutdown] adding graceful shutdown for process.pid ${process.pid}`),process.on(`SIGTERM`,async()=>{await this.gracefulShutdown(`SIGTERM`)}),process.on(`SIGINT`,async()=>{await this.gracefulShutdown(`SIGINT`)})}async gracefulShutdown(e){if(!this.gracefulShutdownStarted){this.gracefulShutdownStarted=!0,this.logger.info(`Kafka: [graceful-shutdown] received ${e}! Disconnecting all producers...`);try{await this.disconnect(),this.logger.info(`Kafka: [graceful-shutdown] all producers disconnected successfully`)}catch(e){throw this.logger.error(`Kafka: [graceful-shutdown] error during shutdown`,{error:e}),e}}}get isEnabled(){return this.enabled}get producerNames(){return Array.from(this.producers.keys())}hasProducer(e){return this.producers.has(e)}getProducer(e){let t=this.producers.get(e);if(!t)throw new r(`Producer '${e}' not found. Available producers: ${this.producerNames.join(`, `)}`);return t}async bootstrap(e){let t=Date.now(),n=e?.timeoutMs??this.bootstrapTimeoutMs,i=e?.strict??this.strictBootstrap,a=e?.producers??this.producerNames;this.logger.info(`Kafka: Starting bootstrap`,{producers:a,timeout:n,strict:i});let o={},s=[];for(let e of a){let t=this.producers.get(e);if(!t){o[e]={success:!1,duration:0,error:`Producer '${e}' not found`},s.push(`Producer '${e}' not found`);continue}let r=Date.now();try{await t.ping({timeout:n,force:!0});let i=t.getHealth();o[e]={success:!0,duration:Date.now()-r,clusterId:i.clusterId??void 0,brokerCount:i.brokerCount},this.logger.info(`Kafka: [${e}] Bootstrap successful`,{duration:Date.now()-r,clusterId:i.clusterId,brokerCount:i.brokerCount})}catch(t){let n=t;o[e]={success:!1,duration:Date.now()-r,error:n.message},s.push(`[${e}] ${n.message}`),this.logger.error(`Kafka: [${e}] Bootstrap failed`,{duration:Date.now()-r,error:n.message})}}let c=Date.now()-t,l=Object.values(o).filter(e=>e.success).length,u=Object.keys(o).length,d=i?l===u:l>0,f={success:d,duration:c,results:o};if(d)this.logger.info(`Kafka: Bootstrap completed successfully`,{duration:c,successCount:l,totalCount:u});else{let e=`Kafka bootstrap failed (${l}/${u} producers succeeded)\n\nFailures:\n${s.map(e=>` - ${e}`).join(`
3
+ `)}\n\nConfiguration:\n Strict mode: ${i}\n Timeout: ${n}ms\n Producers attempted: ${a.join(`, `)}`;throw this.logger.error(`Kafka: Bootstrap failed`,{duration:c,successCount:l,totalCount:u,errors:s}),new r(e)}return f}getHealth(){let e={};for(let[t,n]of this.producers.entries())e[t]=n.getHealth();return e}getProducerHealth(e){return this.getProducer(e).getHealth()}isLive(){return!this.fatalError&&!this.gracefulShutdownStarted}async ping(){let e=Array.from(this.producers.values(),e=>e.ping());await Promise.all(e)}async pingProducer(e){await this.getProducer(e).ping()}async isReady(e){if(!this.enabled)return!0;let t=Date.now();if(!(e?.force??!1)&&this.lastReadinessCheck){let e=t-this.lastReadinessCheck.timestamp;if(e<this.healthCheckCacheMs)return this.logger.debug(`Kafka: Using cached readiness result`,{result:this.lastReadinessCheck.result,age:e}),this.lastReadinessCheck.result}try{let n=e?.timeout??this.healthCheckTimeoutMs,r=Array.from(this.producers.values()).map(e=>e.ping({timeout:n,force:!0}));return await Promise.all(r),this.lastReadinessCheck={result:!0,timestamp:t},!0}catch(e){return this.lastReadinessCheck={result:!1,timestamp:t},this.logger.warn(`Kafka: Readiness check failed`,{error:e.message}),!1}}getConnectionStatus(){let e={};for(let[t,n]of this.producers.entries()){let r=n.getHealth();e[t]={connected:r.isConnected,lastSuccessAt:r.lastPingSucceededAt,lastError:r.lastError?.message??null}}return e}getTopicDefinition(e){let t=this.topics.get(e);if(!t)throw new r(`Topic '${e}' not found. Available topics: ${Array.from(this.topics.keys()).join(`, `)}`);return t}async publish(e,t,n){let i=this.getTopicDefinition(e);if(!n?.skipValidation)try{i.schema.parse(t)}catch(n){throw this.logger.error(`Kafka: Validation failed for topic '${e}'`,{error:n,value:t}),new r(`Validation failed for topic '${e}': ${n.message}`,{cause:n})}return this.getProducer(i.producer).publish(e,t,n)}async publishBatch(e,t){let n=this.getTopicDefinition(e);if(!t.skipValidation)for(let i=0;i<t.messages.length;i++)try{n.schema.parse(t.messages[i].value)}catch(n){throw this.logger.error(`Kafka: Validation failed for topic '${e}' message ${i}`,{error:n,value:t.messages[i].value}),new r(`Validation failed for topic '${e}' message ${i}: ${n.message}`,{cause:n})}return this.getProducer(n.producer).publishBatch(e,t)}async disconnect(){let e=Array.from(this.producers.values()).map(e=>e.disconnect());await Promise.all(e),this.logger.info(`Kafka: All producers disconnected`)}async disconnectProducer(e){await this.getProducer(e).disconnect()}};export{r as KafkaError,f as KafkaManager};
4
4
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["name: string","config: ProducerConfig","logger: LoggerInstanceManager","#loadKafkaAndSetup","headers: Record<string, string>","name: string","brokers: string[]","logger: LoggerInstanceManager","results: BootstrapResult['results']","errors: string[]","result: BootstrapResult","health: Record<string, ProducerHealth>","status: Record<string, ConnectionStatus>"],"sources":["../src/kafkaError.ts","../src/consts.ts","../src/producer.ts","../src/mockProducer.ts","../src/manager.ts"],"sourcesContent":["export default class KafkaError extends Error {\n name = 'KafkaError';\n}\n","export const TIMESTAMP_HEADER = 'x-timestamp';\n\nexport const DEFAULT_CLIENT_ID = 'autofleet-kafka-producer';\n","import { setTimeout } from 'node:timers/promises';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\nimport type { Producer } from '@platformatic/kafka/dist/clients/producer/index.ts';\nimport KafkaError from './kafkaError';\nimport {\n DEFAULT_CLIENT_ID,\n TIMESTAMP_HEADER,\n} from './consts';\nimport type {\n ProducerConfig,\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n IProducer,\n} from './types';\n\n/**\n * Wrapper for a single Kafka producer instance with lazy initialization\n * Tracks health metadata including connection state, timestamps, and errors\n */\nexport class ProducerWrapper implements IProducer {\n private producer: Producer<string, string, string, string> | null = null;\n private _isConnected = false;\n private initPromise: Promise<void> | null = null;\n\n // Health tracking metadata\n private _lastPingAt: number | null = null;\n private _lastPingSucceededAt: number | null = null;\n private _lastError: { message: string; timestamp: number; stack?: string; } | null = null;\n private _clusterId: string | null = null;\n private _brokerCount = 0;\n\n constructor(\n private readonly name: string,\n private readonly config: ProducerConfig,\n private readonly logger: LoggerInstanceManager,\n ) {}\n\n async #loadKafkaAndSetup(): Promise<void> {\n const { Producer, stringSerializers } = await import('@platformatic/kafka');\n\n this.producer = new Producer({\n bootstrapBrokers: this.config.brokers,\n clientId: this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`,\n serializers: stringSerializers,\n autocreateTopics: this.config.autoCreateTopics ?? false,\n sasl: this.config.sasl,\n tls: this.config.tls,\n });\n\n this.logger.info(`Kafka: [${this.name}] Initialized producer`, {\n clientId: this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`,\n brokers: this.config.brokers,\n });\n\n // Clear promise to allow garbage collection\n this.initPromise = null;\n }\n\n private async initialize(): Promise<void> {\n if (this.producer) {\n return;\n }\n\n this.initPromise ??= this.#loadKafkaAndSetup();\n await this.initPromise;\n }\n\n get isConnected(): boolean {\n return this._isConnected;\n }\n\n /**\n * Get comprehensive health snapshot for this producer\n */\n getHealth(): ProducerHealth {\n return {\n name: this.name,\n enabled: true,\n isConnected: this._isConnected,\n lastPingAt: this._lastPingAt,\n lastPingSucceededAt: this._lastPingSucceededAt,\n lastError: this._lastError,\n clusterId: this._clusterId,\n brokerCount: this._brokerCount,\n brokers: this.config.brokers,\n };\n }\n\n /**\n * Build an actionable error message with troubleshooting guidance\n */\n private buildConnectionErrorMessage(error: Error): string {\n const brokerList = this.config.brokers.join(', ');\n const firstBroker = this.config.brokers[0];\n const [host, port] = firstBroker ? firstBroker.split(':') : ['', ''];\n\n return `[${this.name}] Failed to connect to Kafka brokers: ${error.message}\\n\\n`\n + `Possible causes:\\n`\n + ` 1. Brokers are unreachable: ${brokerList}\\n`\n + ` 2. Network connectivity issues\\n`\n + ` 3. Firewall blocking ports\\n`\n + ` 4. ${this.config.sasl ? 'SASL authentication failed' : 'Authentication not configured but required'}\\n\\n`\n + `Troubleshooting:\\n`\n + ` - Verify brokers are running and accessible\\n`\n + (host && port ? ` - Test connectivity: nc -zv ${host} ${port}\\n` : '')\n + ` - Check network policies and firewall rules\\n`\n + ` ${this.config.sasl ? '- Verify SASL credentials are correct\\n' : ''}\\n`\n + `Current configuration:\\n`\n + ` Brokers: ${brokerList}\\n`\n + ` Client ID: ${this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`}\\n`\n + ` SASL: ${this.config.sasl ? `enabled (${this.config.sasl.mechanism})` : 'disabled'}\\n`\n + ` SASL Password: ${this.config.sasl ? (this.config.sasl.password ? '****' : 'not set') : 'N/A'}\\n`\n + ` Auto-create topics: ${this.config.autoCreateTopics ?? false}`;\n }\n\n /**\n * Ping the Kafka cluster to verify connectivity\n * @param options.timeout - Maximum time to wait for connection (ms)\n * @param options.force - Force reconnection even if already connected\n */\n async ping(options?: { timeout?: number; force?: boolean; }): Promise<void> {\n this._lastPingAt = Date.now();\n\n // Skip if already connected and not forcing revalidation\n if (this._isConnected && !options?.force) {\n return;\n }\n\n await this.initialize();\n\n try {\n // Create error before promise to preserve stack trace\n const timeoutMs = options?.timeout ?? 5000;\n const timeoutError = new Error(`Connection timeout after ${timeoutMs}ms`);\n const timeoutPromise = setTimeout(timeoutMs).then(() => {\n throw timeoutError;\n });\n\n // Race between metadata fetch and timeout\n const metadata = await Promise.race([\n this.producer!.metadata({ topics: [] }),\n timeoutPromise,\n ]);\n\n this._isConnected = true;\n this._lastPingSucceededAt = Date.now();\n this._clusterId = metadata.id;\n this._brokerCount = metadata.brokers.size;\n this._lastError = null; // Clear any previous errors\n\n this.logger.info(`Kafka: [${this.name}] Successfully connected to cluster`, {\n clusterId: metadata.id,\n brokers: metadata.brokers.size,\n duration: Date.now() - this._lastPingAt,\n });\n } catch (error) {\n const err = error as Error;\n this._isConnected = false;\n this._lastError = {\n message: err.message,\n timestamp: Date.now(),\n stack: err.stack,\n };\n\n this.logger.error(`Kafka: [${this.name}] Failed to connect to brokers`, {\n error: err.message,\n duration: Date.now() - this._lastPingAt,\n });\n\n throw new KafkaError(this.buildConnectionErrorMessage(err), { cause: err });\n }\n }\n\n async publish<T = unknown>(\n topic: string,\n value: T,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n if (!topic) {\n throw new KafkaError(`[${this.name}] Topic name is required`);\n }\n\n await this.ping();\n\n try {\n const headers: Record<string, string> = {\n [TIMESTAMP_HEADER]: Date.now().toString(),\n ...options?.headers,\n };\n\n const result = await this.producer!.send({\n messages: [{\n topic,\n value: JSON.stringify(value),\n key: options?.key,\n partition: options?.partition,\n headers,\n }],\n });\n\n this.logger.debug(`Kafka: [${this.name}] Published message to topic ${topic}`, {\n topic,\n });\n\n return [{\n topic,\n partition: options?.partition ?? 0,\n offset: result?.offsets?.[0]?.offset?.toString() ?? '0',\n }];\n } catch (error) {\n const err = error as { message?: string; };\n this.logger.error(`Kafka: [${this.name}] Error publishing to topic ${topic}`, { error, value });\n throw new KafkaError(`[${this.name}] Failed to publish to topic ${topic}: ${err.message || 'Unknown error'}`, { cause: error });\n }\n }\n\n async publishBatch<T = unknown>(topic: string, options: PublishBatchOptions<T>): Promise<RecordMetadata[]> {\n if (!topic) {\n throw new KafkaError(`[${this.name}] Topic name is required`);\n }\n\n if (!options.messages || options.messages.length === 0) {\n throw new KafkaError(`[${this.name}] At least one message is required`);\n }\n\n await this.ping();\n\n try {\n const messages = options.messages.map(msg => ({\n topic,\n value: JSON.stringify(msg.value),\n key: msg.key,\n partition: msg.partition,\n headers: {\n [TIMESTAMP_HEADER]: Date.now().toString(),\n ...msg.headers,\n },\n }));\n\n const result = await this.producer!.send({ messages });\n\n this.logger.debug(`Kafka: [${this.name}] Published ${messages.length} messages to topic ${topic}`, {\n topic,\n count: messages.length,\n });\n\n return messages.map((msg, idx) => ({\n topic,\n partition: msg.partition ?? 0,\n offset: result?.offsets?.[idx]?.offset?.toString() ?? idx.toString(),\n }));\n } catch (error) {\n const err = error as { message?: string; };\n this.logger.error(`Kafka: [${this.name}] Error publishing batch to topic ${topic}`, { error });\n throw new KafkaError(`[${this.name}] Failed to publish batch to topic ${topic}: ${err.message || 'Unknown error'}`, { cause: error });\n }\n }\n\n async disconnect(): Promise<void> {\n if (!this.producer || !this._isConnected) {\n this.logger.debug(`Kafka: [${this.name}] Producer already disconnected`);\n return;\n }\n\n this.logger.info(`Kafka: [${this.name}] Disconnecting producer`);\n\n try {\n await this.producer.close();\n this._isConnected = false;\n this.logger.info(`Kafka: [${this.name}] Producer disconnected successfully`);\n } catch (error) {\n this.logger.error(`Kafka: [${this.name}] Error disconnecting producer`, { error });\n throw new KafkaError(`[${this.name}] Failed to disconnect producer: ${(error as Error).message}`, { cause: error });\n }\n }\n}\n","import type { LoggerInstanceManager } from '@autofleet/logger';\nimport type {\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n IProducer,\n} from './types';\n\n/**\n * Mock producer for when Kafka is disabled\n */\nexport class MockProducer implements IProducer {\n constructor(private readonly name: string, private readonly brokers: string[], private readonly logger: LoggerInstanceManager) {}\n\n readonly isConnected = true;\n\n /**\n * Get health snapshot for mock producer\n */\n getHealth(): ProducerHealth {\n return {\n name: this.name,\n enabled: false, // Mock mode means Kafka is disabled\n isConnected: true, // Mock is always \"connected\"\n lastPingAt: null,\n lastPingSucceededAt: null,\n lastError: null,\n clusterId: 'mock-cluster',\n brokerCount: 0,\n brokers: this.brokers,\n };\n }\n\n async ping(): Promise<void> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping ping`);\n return Promise.resolve();\n }\n\n async publish<T = unknown>(topic: string, value: T, _options?: PublishOptions): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping publish to topic: ${topic}`, { value });\n return Promise.resolve([{\n topic,\n partition: 0,\n offset: '0',\n }]);\n }\n\n async publishBatch<T = unknown>(topic: string, options: PublishBatchOptions<T>): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${topic}`, {\n messageCount: options.messages.length,\n });\n return Promise.resolve(options.messages.map(() => ({\n topic,\n partition: 0,\n offset: '0',\n })));\n }\n\n async disconnect(): Promise<void> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping disconnect`);\n return Promise.resolve();\n }\n}\n","import type { LoggerInstanceManager } from '@autofleet/logger';\nimport KafkaError from './kafkaError';\nimport { ProducerWrapper } from './producer';\nimport { MockProducer } from './mockProducer';\nimport type {\n KafkaManagerOptions,\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n BootstrapOptions,\n BootstrapResult,\n ReadinessOptions,\n ConnectionStatus,\n IProducer,\n ProducerConfig,\n TopicsConfig,\n TopicDefinition,\n TopicPayload,\n} from './types';\n\n/**\n * Manager for multiple Kafka producers\n * Provides lifecycle management, health tracking, and operational guarantees\n *\n * @template TProducers - Union of producer names\n * @template TTopics - Typed topics configuration with Zod schemas\n */\nexport class KafkaManager<TProducers extends string = string, TTopics extends TopicsConfig = {}> {\n private readonly producers = new Map<string, IProducer>();\n private readonly topics = new Map<string, TopicDefinition>();\n private readonly logger: LoggerInstanceManager;\n private readonly enabled: boolean;\n private gracefulShutdownStarted = false;\n private fatalError: Error | null = null;\n\n // Configuration options\n private readonly healthCheckTimeoutMs: number;\n private readonly healthCheckCacheMs: number;\n private readonly bootstrapTimeoutMs: number;\n private readonly strictBootstrap: boolean;\n\n // Health check cache\n private lastReadinessCheck: { result: boolean; timestamp: number; } | null = null;\n\n private constructor(options: KafkaManagerOptions) {\n this.logger = options.logger;\n this.enabled = options.enabled ?? true;\n this.healthCheckTimeoutMs = options.healthCheckTimeoutMs ?? 5000;\n this.healthCheckCacheMs = options.healthCheckCacheMs ?? 1000;\n this.bootstrapTimeoutMs = options.bootstrapTimeoutMs ?? 30_000;\n this.strictBootstrap = options.strictBootstrap ?? true;\n\n // Store topic definitions\n if (options.topics) {\n for (const [topicName, definition] of Object.entries(options.topics)) {\n this.topics.set(topicName, definition);\n }\n }\n\n this.logger.info('Kafka: Initialized KafkaManager', {\n enabled: this.enabled,\n producerCount: Object.keys(options.producers ?? {}).length,\n producers: Object.keys(options.producers ?? {}),\n topicCount: this.topics.size,\n topics: Array.from(this.topics.keys()),\n });\n\n // Create producers\n if (!this.enabled) {\n // Create mock producers\n for (const [name, config] of Object.entries(options.producers ?? {})) {\n this.producers.set(name, new MockProducer(name, config.brokers, this.logger));\n }\n if (this.producers.size > 0) {\n this.logger.info('Kafka: Created mock producers (Kafka disabled)');\n }\n } else {\n // Create real producers (they will initialize lazily)\n for (const [name, config] of Object.entries(options.producers ?? {})) {\n if (!config.brokers || config.brokers.length === 0) {\n throw new KafkaError(`[${name}] At least one broker is required`);\n }\n\n this.producers.set(name, new ProducerWrapper(name, config, this.logger));\n }\n }\n\n if (!options.dontGracefulShutdown) {\n this.setupGracefulShutdown();\n }\n }\n\n /**\n * Create a new KafkaManager instance with multiple named producers\n * Producers are initialized lazily on first use\n * Producer names are type-safe based on the configuration\n * Topics configuration provides type-safe publishing with Zod validation\n */\n static create<\n P extends Record<string, ProducerConfig>,\n T extends TopicsConfig = {},\n >(\n options: Omit<KafkaManagerOptions, 'producers' | 'topics'> & {\n producers?: P;\n topics?: T;\n },\n ): KafkaManager<keyof P & string, T> {\n // Validate that topic producers exist\n if (options.topics) {\n const producerNames = Object.keys(options.producers ?? {});\n for (const [topicName, topicDef] of Object.entries(options.topics)) {\n if (!producerNames.includes(topicDef.producer)) {\n throw new KafkaError(\n `Topic '${topicName}' references unknown producer '${topicDef.producer}'. `\n + `Available producers: ${producerNames.join(', ')}`,\n );\n }\n }\n }\n\n return new KafkaManager(options);\n }\n\n private setupGracefulShutdown(): void {\n this.logger.info(`Kafka: [graceful-shutdown] adding graceful shutdown for process.pid ${process.pid}`);\n\n process.on('SIGTERM', async () => {\n await this.gracefulShutdown('SIGTERM');\n });\n\n process.on('SIGINT', async () => {\n await this.gracefulShutdown('SIGINT');\n });\n }\n\n public async gracefulShutdown(signal: string): Promise<void> {\n if (this.gracefulShutdownStarted) {\n return;\n }\n\n this.gracefulShutdownStarted = true;\n\n this.logger.info(`Kafka: [graceful-shutdown] received ${signal}! Disconnecting all producers...`);\n\n try {\n await this.disconnect();\n this.logger.info('Kafka: [graceful-shutdown] all producers disconnected successfully');\n } catch (error) {\n this.logger.error('Kafka: [graceful-shutdown] error during shutdown', { error });\n throw error;\n }\n }\n\n /**\n * Check if Kafka is enabled\n */\n get isEnabled(): boolean {\n return this.enabled;\n }\n\n /**\n * Get all producer names\n */\n get producerNames(): TProducers[] {\n return Array.from(this.producers.keys()) as TProducers[];\n }\n\n /**\n * Check if a producer exists\n */\n hasProducer(name: TProducers): boolean {\n return this.producers.has(name);\n }\n\n /**\n * Get a specific producer\n */\n private getProducer(name: TProducers): IProducer {\n const producer = this.producers.get(name);\n if (!producer) {\n throw new KafkaError(`Producer '${name}' not found. Available producers: ${this.producerNames.join(', ')}`);\n }\n return producer;\n }\n\n /**\n * Explicitly bootstrap all (or specific) producers\n * Connects to brokers, validates connectivity, and returns detailed results\n *\n * @example\n * // Bootstrap all producers with defaults (30s timeout, strict mode)\n * await kafka.bootstrap();\n *\n * @example\n * // Bootstrap with custom timeout and non-strict mode\n * const result = await kafka.bootstrap({ timeoutMs: 10000, strict: false });\n * if (!result.success) {\n * console.error('Some producers failed:', result.results);\n * }\n *\n * @example\n * // Bootstrap specific producers only\n * await kafka.bootstrap({ producers: ['main'] });\n */\n async bootstrap(options?: BootstrapOptions): Promise<BootstrapResult> {\n const startTime = Date.now();\n const timeoutMs = options?.timeoutMs ?? this.bootstrapTimeoutMs;\n const strict = options?.strict ?? this.strictBootstrap;\n const producersToBootstrap = options?.producers ?? this.producerNames;\n\n this.logger.info('Kafka: Starting bootstrap', {\n producers: producersToBootstrap,\n timeout: timeoutMs,\n strict,\n });\n\n const results: BootstrapResult['results'] = {};\n const errors: string[] = [];\n\n // Bootstrap each producer\n for (const name of producersToBootstrap) {\n const producer = this.producers.get(name);\n if (!producer) {\n results[name] = {\n success: false,\n duration: 0,\n error: `Producer '${name}' not found`,\n };\n errors.push(`Producer '${name}' not found`);\n continue;\n }\n\n const producerStartTime = Date.now();\n try {\n await producer.ping({ timeout: timeoutMs, force: true });\n const health = producer.getHealth();\n\n results[name] = {\n success: true,\n duration: Date.now() - producerStartTime,\n clusterId: health.clusterId ?? undefined,\n brokerCount: health.brokerCount,\n };\n\n this.logger.info(`Kafka: [${name}] Bootstrap successful`, {\n duration: Date.now() - producerStartTime,\n clusterId: health.clusterId,\n brokerCount: health.brokerCount,\n });\n } catch (error) {\n const err = error as Error;\n results[name] = {\n success: false,\n duration: Date.now() - producerStartTime,\n error: err.message,\n };\n errors.push(`[${name}] ${err.message}`);\n\n this.logger.error(`Kafka: [${name}] Bootstrap failed`, {\n duration: Date.now() - producerStartTime,\n error: err.message,\n });\n }\n }\n\n const totalDuration = Date.now() - startTime;\n const successCount = Object.values(results).filter(r => r.success).length;\n const totalCount = Object.keys(results).length;\n const success = strict ? successCount === totalCount : successCount > 0;\n\n const result: BootstrapResult = {\n success,\n duration: totalDuration,\n results,\n };\n\n if (success) {\n this.logger.info('Kafka: Bootstrap completed successfully', {\n duration: totalDuration,\n successCount,\n totalCount,\n });\n } else {\n const errorMessage = `Kafka bootstrap failed (${successCount}/${totalCount} producers succeeded)\\n\\n`\n + `Failures:\\n${errors.map(e => ` - ${e}`).join('\\n')}\\n\\n`\n + `Configuration:\\n`\n + ` Strict mode: ${strict}\\n`\n + ` Timeout: ${timeoutMs}ms\\n`\n + ` Producers attempted: ${producersToBootstrap.join(', ')}`;\n\n this.logger.error('Kafka: Bootstrap failed', {\n duration: totalDuration,\n successCount,\n totalCount,\n errors,\n });\n\n throw new KafkaError(errorMessage);\n }\n\n return result;\n }\n\n /**\n * Get detailed health snapshot for all producers\n * Returns comprehensive metadata including timestamps, errors, cluster info\n *\n * @example\n * const health = kafka.getHealth();\n * console.log(health.main.lastPingSucceededAt); // timestamp\n * console.log(health.main.clusterId); // 'prod-kafka-01'\n */\n getHealth(): Record<TProducers, ProducerHealth> {\n const health: Record<string, ProducerHealth> = {};\n for (const [name, producer] of this.producers.entries()) {\n health[name] = producer.getHealth();\n }\n return health as Record<TProducers, ProducerHealth>;\n }\n\n /**\n * Get detailed health for a specific producer\n */\n getProducerHealth(name: TProducers): ProducerHealth {\n const producer = this.getProducer(name);\n return producer.getHealth();\n }\n\n /**\n * Lightweight liveness check - does NOT perform Kafka operations\n * Only checks internal state for fatal errors\n * Suitable for Kubernetes liveness probes\n *\n * Returns false if:\n * - Manager is in a fatal state\n * - Graceful shutdown has started\n *\n * @example\n * app.get('/health/live', (req, res) => {\n * res.status(kafka.isLive() ? 200 : 503).json({ live: kafka.isLive() });\n * });\n */\n isLive(): boolean {\n return !this.fatalError && !this.gracefulShutdownStarted;\n }\n\n /**\n * Ping all producers to verify connectivity\n */\n async ping(): Promise<void> {\n const promises = Array.from(this.producers.values(), p => p.ping());\n await Promise.all(promises);\n }\n\n /**\n * Ping a specific producer\n */\n async pingProducer(name: TProducers): Promise<void> {\n await this.getProducer(name).ping();\n }\n\n /**\n * Check if Kafka is ready to handle traffic\n * Revalidates connectivity if cache is stale\n *\n * @param options.timeout - Override default health check timeout\n * @param options.force - Force revalidation, ignore cache\n *\n * @example\n * // Use cached result if fresh (within healthCheckCacheMs)\n * const ready = await kafka.isReady();\n *\n * @example\n * // Force immediate revalidation\n * const ready = await kafka.isReady({ force: true });\n */\n async isReady(options?: ReadinessOptions): Promise<boolean> {\n if (!this.enabled) {\n return true;\n }\n\n const now = Date.now();\n const force = options?.force ?? false;\n\n // Use cached result if available and fresh\n if (!force && this.lastReadinessCheck) {\n const age = now - this.lastReadinessCheck.timestamp;\n if (age < this.healthCheckCacheMs) {\n this.logger.debug('Kafka: Using cached readiness result', {\n result: this.lastReadinessCheck.result,\n age,\n });\n return this.lastReadinessCheck.result;\n }\n }\n\n // Revalidate connectivity\n try {\n const timeout = options?.timeout ?? this.healthCheckTimeoutMs;\n const promises = Array.from(this.producers.values()).map(p =>\n p.ping({ timeout, force: true }),\n );\n await Promise.all(promises);\n\n this.lastReadinessCheck = { result: true, timestamp: now };\n return true;\n } catch (error) {\n this.lastReadinessCheck = { result: false, timestamp: now };\n this.logger.warn('Kafka: Readiness check failed', {\n error: (error as Error).message,\n });\n return false;\n }\n }\n\n /**\n * Check connection status of all producers\n * Returns detailed status including last success time and errors\n */\n getConnectionStatus(): Record<TProducers, ConnectionStatus> {\n const status: Record<string, ConnectionStatus> = {};\n for (const [name, producer] of this.producers.entries()) {\n const health = producer.getHealth();\n status[name] = {\n connected: health.isConnected,\n lastSuccessAt: health.lastPingSucceededAt,\n lastError: health.lastError?.message ?? null,\n };\n }\n return status as Record<TProducers, ConnectionStatus>;\n }\n\n /**\n * Get a topic definition\n */\n private getTopicDefinition<TName extends keyof TTopics & string>(topicName: TName): TopicDefinition {\n const topic = this.topics.get(topicName);\n if (!topic) {\n throw new KafkaError(\n `Topic '${topicName}' not found. Available topics: ${Array.from(this.topics.keys()).join(', ')}`,\n );\n }\n return topic;\n }\n\n /**\n * Publish a message to a typed topic\n * Automatically validates payload against Zod schema and routes to correct producer\n *\n * @template TName - Topic name (inferred from topics config)\n * @param topicName - Name of the topic to publish to\n * @param value - Payload (type-checked against topic's Zod schema)\n * @param options - Optional publish options including headers, key, partition\n *\n * @example\n * await kafka.publish('order.created', {\n * orderId: '123',\n * amount: 99.99,\n * customerId: '456',\n * });\n */\n async publish<TName extends keyof TTopics & string>(\n topicName: TName,\n value: TopicPayload<TTopics, TName>,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n const topicDef = this.getTopicDefinition(topicName);\n\n // Validate payload against schema unless skipped\n if (!options?.skipValidation) {\n try {\n topicDef.schema.parse(value);\n } catch (error) {\n this.logger.error(`Kafka: Validation failed for topic '${topicName}'`, { error, value });\n throw new KafkaError(\n `Validation failed for topic '${topicName}': ${(error as Error).message}`,\n { cause: error },\n );\n }\n }\n\n // Get producer and publish\n const producer = this.getProducer(topicDef.producer as TProducers);\n return producer.publish(topicName, value, options);\n }\n\n /**\n * Publish a batch of messages to a typed topic\n * Automatically validates payloads against Zod schema and routes to correct producer\n *\n * @template TName - Topic name (inferred from topics config)\n * @param topicName - Name of the topic to publish to\n * @param options - Batch options with array of messages\n *\n * @example\n * await kafka.publishBatch('order.created', {\n * messages: [\n * { value: { orderId: '1', amount: 10, customerId: 'a' } },\n * { value: { orderId: '2', amount: 20, customerId: 'b' } },\n * ],\n * });\n */\n async publishBatch<TName extends keyof TTopics & string>(\n topicName: TName,\n options: PublishBatchOptions<TopicPayload<TTopics, TName>>,\n ): Promise<RecordMetadata[]> {\n const topicDef = this.getTopicDefinition(topicName);\n\n // Validate all payloads against schema unless skipped\n if (!options.skipValidation) {\n for (let i = 0; i < options.messages.length; i++) {\n try {\n topicDef.schema.parse(options.messages[i].value);\n } catch (error) {\n this.logger.error(`Kafka: Validation failed for topic '${topicName}' message ${i}`, {\n error,\n value: options.messages[i].value,\n });\n throw new KafkaError(\n `Validation failed for topic '${topicName}' message ${i}: ${(error as Error).message}`,\n { cause: error },\n );\n }\n }\n }\n\n // Get producer and publish batch\n const producer = this.getProducer(topicDef.producer as TProducers);\n return producer.publishBatch(topicName, options);\n }\n\n /**\n * Disconnect all producers\n */\n async disconnect(): Promise<void> {\n const promises = Array.from(this.producers.values()).map(p => p.disconnect());\n await Promise.all(promises);\n this.logger.info('Kafka: All producers disconnected');\n }\n\n /**\n * Disconnect a specific producer\n */\n async disconnectProducer(name: TProducers): Promise<void> {\n await this.getProducer(name).disconnect();\n }\n}\n"],"mappings":"kDAAA,IAAqB,EAArB,cAAwC,KAAM,yCACrC,eCDT,MAAa,EAAmB,cAEnB,EAAoB,2BCmBjC,IAAa,EAAb,KAAkD,CAYhD,YACE,EACA,EACA,EACA,CAHiB,KAAA,KAAA,EACA,KAAA,OAAA,EACA,KAAA,OAAA,gBAdiD,uBAC7C,oBACqB,sBAGP,+BACS,qBACuC,qBACjD,uBACb,EAQvB,MAAA,GAA0C,CACxC,GAAM,CAAE,WAAU,qBAAsB,MAAM,OAAO,uBAErD,KAAK,SAAW,IAAI,EAAS,CAC3B,iBAAkB,KAAK,OAAO,QAC9B,SAAU,KAAK,OAAO,UAAY,GAAG,EAAkB,GAAG,KAAK,OAC/D,YAAa,EACb,iBAAkB,KAAK,OAAO,kBAAoB,GAClD,KAAM,KAAK,OAAO,KAClB,IAAK,KAAK,OAAO,IAClB,CAAC,CAEF,KAAK,OAAO,KAAK,WAAW,KAAK,KAAK,wBAAyB,CAC7D,SAAU,KAAK,OAAO,UAAY,GAAG,E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,CAAC,EAAQ,eACX,IAAK,IAAI,EAAI,EAAG,EAAI,EAAQ,SAAS,OAAQ,IAC3C,GAAI,CACF,EAAS,OAAO,MAAM,EAAQ,SAAS,GAAG,MAAM,OACzC,EAAO,CAKd,MAJA,KAAK,OAAO,MAAM,uCAAuC,EAAU,YAAY,IAAK,CAClF,QACA,MAAO,EAAQ,SAAS,GAAG,MAC5B,CAAC,CACI,IAAI,EACR,gCAAgC,EAAU,YAAY,EAAE,IAAK,EAAgB,UAC7E,CAAE,MAAO,EAAO,CACjB,CAOP,OADiB,KAAK,YAAY,EAAS,SAAuB,CAClD,aAAa,EAAW,EAAQ,CAMlD,MAAM,YAA4B,CAChC,IAAM,EAAW,MAAM,KAAK,KAAK,UAAU,QAAQ,CAAC,CAAC,IAAI,GAAK,EAAE,YAAY,CAAC,CAC7E,MAAM,QAAQ,IAAI,EAAS,CAC3B,KAAK,OAAO,KAAK,oCAAoC,CAMvD,MAAM,mBAAmB,EAAiC,CACxD,MAAM,KAAK,YAAY,EAAK,CAAC,YAAY"}
1
+ {"version":3,"file":"index.js","names":["clientPromise: Promise<any> | null","cachedEmail: string | null","cachedWrappedToken: string | null","cachedWrappedTokenExpSec: number | null","name: string","config: ProducerConfig","logger: LoggerInstanceManager","#loadKafkaAndSetup","headers: Record<string, string>","name: string","brokers: string[]","logger: LoggerInstanceManager","results: BootstrapResult['results']","errors: string[]","result: BootstrapResult","health: Record<string, ProducerHealth>","status: Record<string, ConnectionStatus>"],"sources":["../src/kafkaError.ts","../src/consts.ts","../src/auth.ts","../src/producer.ts","../src/mockProducer.ts","../src/manager.ts"],"sourcesContent":["export default class KafkaError extends Error {\n name = 'KafkaError';\n}\n","export const TIMESTAMP_HEADER = 'x-timestamp';\n\nexport const DEFAULT_CLIENT_ID = 'autofleet-kafka-producer';\n","import { GoogleAuth } from 'google-auth-library';\nimport type { OauthBearerProviderOptions } from './types';\nimport crypto from 'node:crypto';\n\nconst DEFAULT_SKEW_SEC = 60;\n\nconst b64urlUtf8 = (s: string) =>\n Buffer.from(s, 'utf8')\n .toString('base64')\n .replace(/\\+/g, '-')\n .replace(/\\//g, '_')\n .replace(/=+$/g, '');\n\nconst fp12 = (s: string) =>\n crypto.createHash('sha256').update(s).digest('hex').slice(0, 12);\n\nconst HEADER_B64 = b64urlUtf8(JSON.stringify({ typ: 'JWT', alg: 'GOOG_OAUTH2_TOKEN' }));\n\n// This is an implementation of the OAUTHBEARER mechanisem for google managed kafka\n// https://github.com/googleapis/managedkafka/blob/main/kafka-auth-local-server/kafka_gcp_credentials_server.py\nexport const createManagedKafkaOauthBearerProvider = (opts: OauthBearerProviderOptions = {}) => {\n const logger = opts.logger;\n const refreshSkewSec = opts.refreshSkewSec ?? DEFAULT_SKEW_SEC;\n const enableMetadataLookup = opts.enableMetadataLookup ?? true;\n\n const auth = new GoogleAuth({\n scopes: ['https://www.googleapis.com/auth/cloud-platform'],\n });\n\n let clientPromise: Promise<any> | null = null;\n let cachedEmail: string | null = opts.serviceAccountEmail ?? null;\n\n let cachedWrappedToken: string | null = null;\n let cachedWrappedTokenExpSec: number | null = null;\n\n const getClient = async () => {\n clientPromise ??= auth.getClient();\n return clientPromise;\n };\n\n const metadataEmailLookup = async () => {\n try {\n const res = await fetch(\n 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/email',\n { headers: { 'Metadata-Flavor': 'Google' } },\n );\n if (!res.ok) return null;\n return (await res.text()).trim();\n } catch (e) {\n logger?.warn?.('ManagedKafkaAuth: metadata email lookup failed', { err: String(e) });\n return null;\n }\n };\n\n const getEmail = async () => {\n if (cachedEmail) return cachedEmail;\n\n try {\n const creds = await auth.getCredentials();\n if (creds?.client_email) {\n cachedEmail = creds.client_email;\n return cachedEmail;\n }\n } catch (e) {\n logger?.warn?.('ManagedKafkaAuth: getCredentials failed', { err: String(e) });\n }\n\n if (enableMetadataLookup) {\n const email = await metadataEmailLookup();\n if (email) {\n cachedEmail = email;\n return cachedEmail;\n }\n }\n\n if (process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL) {\n cachedEmail = process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL;\n return cachedEmail;\n }\n\n throw new Error('ManagedKafkaAuth: cannot determine service account email (set GOOGLE_SERVICE_ACCOUNT_EMAIL)');\n };\n\n return async (): Promise<string> => {\n const nowSec = Math.floor(Date.now() / 1000);\n\n // reuse cached wrapped token if still valid\n if (\n cachedWrappedToken\n && cachedWrappedTokenExpSec\n && nowSec < (cachedWrappedTokenExpSec - refreshSkewSec)\n ) {\n return cachedWrappedToken;\n }\n\n const client = await getClient();\n\n const { token } = await client.getAccessToken();\n if (!token) throw new Error('ManagedKafkaAuth: no access token from ADC');\n\n const expiryMs = client.credentials?.expiry_date;\n if (!expiryMs) throw new Error('ManagedKafkaAuth: no expiry_date on ADC credentials');\n\n const sub = await getEmail();\n const expSec = Math.floor(expiryMs / 1000);\n\n const payloadB64 = b64urlUtf8(JSON.stringify({\n exp: expSec,\n iss: 'Google',\n iat: nowSec,\n sub,\n }));\n\n const wrapped = `${HEADER_B64}.${payloadB64}.${b64urlUtf8(token)}`;\n\n cachedWrappedToken = wrapped;\n cachedWrappedTokenExpSec = expSec;\n\n logger?.info?.('ManagedKafkaAuth: token generated', {\n sub,\n iat: nowSec,\n exp: expSec,\n fp: fp12(wrapped),\n });\n\n return wrapped;\n };\n};\n","import { setTimeout } from 'node:timers/promises';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\nimport type { Producer } from '@platformatic/kafka/dist/clients/producer/index.ts';\nimport KafkaError from './kafkaError';\nimport {\n DEFAULT_CLIENT_ID,\n TIMESTAMP_HEADER,\n} from './consts';\nimport type {\n ProducerConfig,\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n IProducer,\n} from './types';\nimport { createManagedKafkaOauthBearerProvider } from './auth';\n\n/**\n * Wrapper for a single Kafka producer instance with lazy initialization\n * Tracks health metadata including connection state, timestamps, and errors\n */\nexport class ProducerWrapper implements IProducer {\n private producer: Producer<string, string, string, string> | null = null;\n private _isConnected = false;\n private initPromise: Promise<void> | null = null;\n\n // Health tracking metadata\n private _lastPingAt: number | null = null;\n private _lastPingSucceededAt: number | null = null;\n private _lastError: { message: string; timestamp: number; stack?: string; } | null = null;\n private _clusterId: string | null = null;\n private _brokerCount = 0;\n\n constructor(\n private readonly name: string,\n private readonly config: ProducerConfig,\n private readonly logger: LoggerInstanceManager,\n ) {}\n\n async #loadKafkaAndSetup(): Promise<void> {\n const { Producer, stringSerializers } = await import('@platformatic/kafka');\n\n const oauthBearerConfig = this.config.oauthBearerConfig ?? { logger: this.logger };\n\n this.producer = new Producer({\n bootstrapBrokers: this.config.brokers,\n clientId: this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`,\n serializers: stringSerializers,\n autocreateTopics: this.config.autoCreateTopics ?? false,\n sasl: this.config.sasl ?? {\n mechanism: 'OAUTHBEARER',\n token: createManagedKafkaOauthBearerProvider(oauthBearerConfig),\n },\n tls: this.config.tls ?? {},\n });\n\n this.logger.info(`Kafka: [${this.name}] Initialized producer`, {\n clientId: this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`,\n brokers: this.config.brokers,\n });\n\n // Clear promise to allow garbage collection\n this.initPromise = null;\n }\n\n private async initialize(): Promise<void> {\n if (this.producer) {\n return;\n }\n\n this.initPromise ??= this.#loadKafkaAndSetup();\n await this.initPromise;\n }\n\n get isConnected(): boolean {\n return this._isConnected;\n }\n\n /**\n * Get comprehensive health snapshot for this producer\n */\n getHealth(): ProducerHealth {\n return {\n name: this.name,\n enabled: true,\n isConnected: this._isConnected,\n lastPingAt: this._lastPingAt,\n lastPingSucceededAt: this._lastPingSucceededAt,\n lastError: this._lastError,\n clusterId: this._clusterId,\n brokerCount: this._brokerCount,\n brokers: this.config.brokers,\n };\n }\n\n /**\n * Build an actionable error message with troubleshooting guidance\n */\n private buildConnectionErrorMessage(error: Error): string {\n const brokerList = this.config.brokers.join(', ');\n const firstBroker = this.config.brokers[0];\n const [host, port] = firstBroker ? firstBroker.split(':') : ['', ''];\n\n return `[${this.name}] Failed to connect to Kafka brokers: ${error.message}\\n\\n`\n + `Possible causes:\\n`\n + ` 1. Brokers are unreachable: ${brokerList}\\n`\n + ` 2. Network connectivity issues\\n`\n + ` 3. Firewall blocking ports\\n`\n + ` 4. ${this.config.sasl ? 'SASL authentication failed' : 'Authentication not configured but required'}\\n\\n`\n + `Troubleshooting:\\n`\n + ` - Verify brokers are running and accessible\\n`\n + (host && port ? ` - Test connectivity: nc -zv ${host} ${port}\\n` : '')\n + ` - Check network policies and firewall rules\\n`\n + ` ${this.config.sasl ? '- Verify SASL credentials are correct\\n' : ''}\\n`\n + `Current configuration:\\n`\n + ` Brokers: ${brokerList}\\n`\n + ` Client ID: ${this.config.clientId || `${DEFAULT_CLIENT_ID}-${this.name}`}\\n`\n + ` SASL: ${this.config.sasl ? `enabled (${this.config.sasl.mechanism})` : 'disabled'}\\n`\n + ` SASL Password: ${this.config.sasl ? (this.config.sasl.password ? '****' : 'not set') : 'N/A'}\\n`\n + ` Auto-create topics: ${this.config.autoCreateTopics ?? false}`;\n }\n\n /**\n * Ping the Kafka cluster to verify connectivity\n * @param options.timeout - Maximum time to wait for connection (ms)\n * @param options.force - Force reconnection even if already connected\n */\n async ping(options?: { timeout?: number; force?: boolean; }): Promise<void> {\n this._lastPingAt = Date.now();\n\n // Skip if already connected and not forcing revalidation\n if (this._isConnected && !options?.force) {\n return;\n }\n\n await this.initialize();\n\n try {\n // Create error before promise to preserve stack trace\n const timeoutMs = options?.timeout ?? 5000;\n const timeoutError = new Error(`Connection timeout after ${timeoutMs}ms`);\n const timeoutPromise = setTimeout(timeoutMs).then(() => {\n throw timeoutError;\n });\n\n // Race between metadata fetch and timeout\n const metadata = await Promise.race([\n this.producer!.metadata({ topics: [] }),\n timeoutPromise,\n ]);\n\n this._isConnected = true;\n this._lastPingSucceededAt = Date.now();\n this._clusterId = metadata.id;\n this._brokerCount = metadata.brokers.size;\n this._lastError = null; // Clear any previous errors\n\n this.logger.info(`Kafka: [${this.name}] Successfully connected to cluster`, {\n clusterId: metadata.id,\n brokers: metadata.brokers.size,\n duration: Date.now() - this._lastPingAt,\n });\n } catch (error) {\n const err = error as Error;\n this._isConnected = false;\n this._lastError = {\n message: err.message,\n timestamp: Date.now(),\n stack: err.stack,\n };\n\n this.logger.error(`Kafka: [${this.name}] Failed to connect to brokers`, {\n error: err.message,\n duration: Date.now() - this._lastPingAt,\n });\n\n throw new KafkaError(this.buildConnectionErrorMessage(err), { cause: err });\n }\n }\n\n async publish<T = unknown>(\n topic: string,\n value: T,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n if (!topic) {\n throw new KafkaError(`[${this.name}] Topic name is required`);\n }\n\n await this.ping();\n\n try {\n const headers: Record<string, string> = {\n [TIMESTAMP_HEADER]: Date.now().toString(),\n ...options?.headers,\n };\n\n const result = await this.producer!.send({\n messages: [{\n topic,\n value: JSON.stringify(value),\n key: options?.key,\n partition: options?.partition,\n headers,\n }],\n });\n\n this.logger.debug(`Kafka: [${this.name}] Published message to topic ${topic}`, {\n topic,\n });\n\n return [{\n topic,\n partition: options?.partition ?? 0,\n offset: result?.offsets?.[0]?.offset?.toString() ?? '0',\n }];\n } catch (error) {\n const err = error as { message?: string; };\n this.logger.error(`Kafka: [${this.name}] Error publishing to topic ${topic}`, { error, value });\n throw new KafkaError(`[${this.name}] Failed to publish to topic ${topic}: ${err.message || 'Unknown error'}`, { cause: error });\n }\n }\n\n async publishBatch<T = unknown>(topic: string, options: PublishBatchOptions<T>): Promise<RecordMetadata[]> {\n if (!topic) {\n throw new KafkaError(`[${this.name}] Topic name is required`);\n }\n\n if (!options.messages || options.messages.length === 0) {\n throw new KafkaError(`[${this.name}] At least one message is required`);\n }\n\n await this.ping();\n\n try {\n const messages = options.messages.map(msg => ({\n topic,\n value: JSON.stringify(msg.value),\n key: msg.key,\n partition: msg.partition,\n headers: {\n [TIMESTAMP_HEADER]: Date.now().toString(),\n ...msg.headers,\n },\n }));\n\n const result = await this.producer!.send({ messages });\n\n this.logger.debug(`Kafka: [${this.name}] Published ${messages.length} messages to topic ${topic}`, {\n topic,\n count: messages.length,\n });\n\n return messages.map((msg, idx) => ({\n topic,\n partition: msg.partition ?? 0,\n offset: result?.offsets?.[idx]?.offset?.toString() ?? idx.toString(),\n }));\n } catch (error) {\n const err = error as { message?: string; };\n this.logger.error(`Kafka: [${this.name}] Error publishing batch to topic ${topic}`, { error });\n throw new KafkaError(`[${this.name}] Failed to publish batch to topic ${topic}: ${err.message || 'Unknown error'}`, { cause: error });\n }\n }\n\n async disconnect(): Promise<void> {\n if (!this.producer || !this._isConnected) {\n this.logger.debug(`Kafka: [${this.name}] Producer already disconnected`);\n return;\n }\n\n this.logger.info(`Kafka: [${this.name}] Disconnecting producer`);\n\n try {\n await this.producer.close();\n this._isConnected = false;\n this.logger.info(`Kafka: [${this.name}] Producer disconnected successfully`);\n } catch (error) {\n this.logger.error(`Kafka: [${this.name}] Error disconnecting producer`, { error });\n throw new KafkaError(`[${this.name}] Failed to disconnect producer: ${(error as Error).message}`, { cause: error });\n }\n }\n}\n","import type { LoggerInstanceManager } from '@autofleet/logger';\nimport type {\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n IProducer,\n} from './types';\n\n/**\n * Mock producer for when Kafka is disabled\n */\nexport class MockProducer implements IProducer {\n constructor(private readonly name: string, private readonly brokers: string[], private readonly logger: LoggerInstanceManager) {}\n\n readonly isConnected = true;\n\n /**\n * Get health snapshot for mock producer\n */\n getHealth(): ProducerHealth {\n return {\n name: this.name,\n enabled: false, // Mock mode means Kafka is disabled\n isConnected: true, // Mock is always \"connected\"\n lastPingAt: null,\n lastPingSucceededAt: null,\n lastError: null,\n clusterId: 'mock-cluster',\n brokerCount: 0,\n brokers: this.brokers,\n };\n }\n\n async ping(): Promise<void> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping ping`);\n return Promise.resolve();\n }\n\n async publish<T = unknown>(topic: string, value: T, _options?: PublishOptions): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping publish to topic: ${topic}`, { value });\n return Promise.resolve([{\n topic,\n partition: 0,\n offset: '0',\n }]);\n }\n\n async publishBatch<T = unknown>(topic: string, options: PublishBatchOptions<T>): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${topic}`, {\n messageCount: options.messages.length,\n });\n return Promise.resolve(options.messages.map(() => ({\n topic,\n partition: 0,\n offset: '0',\n })));\n }\n\n async disconnect(): Promise<void> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping disconnect`);\n return Promise.resolve();\n }\n}\n","import type { LoggerInstanceManager } from '@autofleet/logger';\nimport KafkaError from './kafkaError';\nimport { ProducerWrapper } from './producer';\nimport { MockProducer } from './mockProducer';\nimport type {\n KafkaManagerOptions,\n PublishOptions,\n PublishBatchOptions,\n RecordMetadata,\n ProducerHealth,\n BootstrapOptions,\n BootstrapResult,\n ReadinessOptions,\n ConnectionStatus,\n IProducer,\n ProducerConfig,\n TopicsConfig,\n TopicDefinition,\n TopicPayload,\n} from './types';\n\n/**\n * Manager for multiple Kafka producers\n * Provides lifecycle management, health tracking, and operational guarantees\n *\n * @template TProducers - Union of producer names\n * @template TTopics - Typed topics configuration with Zod schemas\n */\nexport class KafkaManager<TProducers extends string = string, TTopics extends TopicsConfig = {}> {\n private readonly producers = new Map<string, IProducer>();\n private readonly topics = new Map<string, TopicDefinition>();\n private readonly logger: LoggerInstanceManager;\n private readonly enabled: boolean;\n private gracefulShutdownStarted = false;\n private fatalError: Error | null = null;\n\n // Configuration options\n private readonly healthCheckTimeoutMs: number;\n private readonly healthCheckCacheMs: number;\n private readonly bootstrapTimeoutMs: number;\n private readonly strictBootstrap: boolean;\n\n // Health check cache\n private lastReadinessCheck: { result: boolean; timestamp: number; } | null = null;\n\n private constructor(options: KafkaManagerOptions) {\n this.logger = options.logger;\n this.enabled = options.enabled ?? true;\n this.healthCheckTimeoutMs = options.healthCheckTimeoutMs ?? 5000;\n this.healthCheckCacheMs = options.healthCheckCacheMs ?? 1000;\n this.bootstrapTimeoutMs = options.bootstrapTimeoutMs ?? 30_000;\n this.strictBootstrap = options.strictBootstrap ?? true;\n\n // Store topic definitions\n if (options.topics) {\n for (const [topicName, definition] of Object.entries(options.topics)) {\n this.topics.set(topicName, definition);\n }\n }\n\n this.logger.info('Kafka: Initialized KafkaManager', {\n enabled: this.enabled,\n producerCount: Object.keys(options.producers ?? {}).length,\n producers: Object.keys(options.producers ?? {}),\n topicCount: this.topics.size,\n topics: Array.from(this.topics.keys()),\n });\n\n // Create producers\n if (!this.enabled) {\n // Create mock producers\n for (const [name, config] of Object.entries(options.producers ?? {})) {\n this.producers.set(name, new MockProducer(name, config.brokers, this.logger));\n }\n if (this.producers.size > 0) {\n this.logger.info('Kafka: Created mock producers (Kafka disabled)');\n }\n } else {\n // Create real producers (they will initialize lazily)\n for (const [name, config] of Object.entries(options.producers ?? {})) {\n if (!config.brokers || config.brokers.length === 0) {\n throw new KafkaError(`[${name}] At least one broker is required`);\n }\n\n this.producers.set(name, new ProducerWrapper(name, config, this.logger));\n }\n }\n\n if (!options.dontGracefulShutdown) {\n this.setupGracefulShutdown();\n }\n }\n\n /**\n * Create a new KafkaManager instance with multiple named producers\n * Producers are initialized lazily on first use\n * Producer names are type-safe based on the configuration\n * Topics configuration provides type-safe publishing with Zod validation\n */\n static create<\n P extends Record<string, ProducerConfig>,\n T extends TopicsConfig = {},\n >(\n options: Omit<KafkaManagerOptions, 'producers' | 'topics'> & {\n producers?: P;\n topics?: T;\n },\n ): KafkaManager<keyof P & string, T> {\n // Validate that topic producers exist\n if (options.topics) {\n const producerNames = Object.keys(options.producers ?? {});\n for (const [topicName, topicDef] of Object.entries(options.topics)) {\n if (!producerNames.includes(topicDef.producer)) {\n throw new KafkaError(\n `Topic '${topicName}' references unknown producer '${topicDef.producer}'. `\n + `Available producers: ${producerNames.join(', ')}`,\n );\n }\n }\n }\n\n return new KafkaManager(options);\n }\n\n private setupGracefulShutdown(): void {\n this.logger.info(`Kafka: [graceful-shutdown] adding graceful shutdown for process.pid ${process.pid}`);\n\n process.on('SIGTERM', async () => {\n await this.gracefulShutdown('SIGTERM');\n });\n\n process.on('SIGINT', async () => {\n await this.gracefulShutdown('SIGINT');\n });\n }\n\n public async gracefulShutdown(signal: string): Promise<void> {\n if (this.gracefulShutdownStarted) {\n return;\n }\n\n this.gracefulShutdownStarted = true;\n\n this.logger.info(`Kafka: [graceful-shutdown] received ${signal}! Disconnecting all producers...`);\n\n try {\n await this.disconnect();\n this.logger.info('Kafka: [graceful-shutdown] all producers disconnected successfully');\n } catch (error) {\n this.logger.error('Kafka: [graceful-shutdown] error during shutdown', { error });\n throw error;\n }\n }\n\n /**\n * Check if Kafka is enabled\n */\n get isEnabled(): boolean {\n return this.enabled;\n }\n\n /**\n * Get all producer names\n */\n get producerNames(): TProducers[] {\n return Array.from(this.producers.keys()) as TProducers[];\n }\n\n /**\n * Check if a producer exists\n */\n hasProducer(name: TProducers): boolean {\n return this.producers.has(name);\n }\n\n /**\n * Get a specific producer\n */\n private getProducer(name: TProducers): IProducer {\n const producer = this.producers.get(name);\n if (!producer) {\n throw new KafkaError(`Producer '${name}' not found. Available producers: ${this.producerNames.join(', ')}`);\n }\n return producer;\n }\n\n /**\n * Explicitly bootstrap all (or specific) producers\n * Connects to brokers, validates connectivity, and returns detailed results\n *\n * @example\n * // Bootstrap all producers with defaults (30s timeout, strict mode)\n * await kafka.bootstrap();\n *\n * @example\n * // Bootstrap with custom timeout and non-strict mode\n * const result = await kafka.bootstrap({ timeoutMs: 10000, strict: false });\n * if (!result.success) {\n * console.error('Some producers failed:', result.results);\n * }\n *\n * @example\n * // Bootstrap specific producers only\n * await kafka.bootstrap({ producers: ['main'] });\n */\n async bootstrap(options?: BootstrapOptions): Promise<BootstrapResult> {\n const startTime = Date.now();\n const timeoutMs = options?.timeoutMs ?? this.bootstrapTimeoutMs;\n const strict = options?.strict ?? this.strictBootstrap;\n const producersToBootstrap = options?.producers ?? this.producerNames;\n\n this.logger.info('Kafka: Starting bootstrap', {\n producers: producersToBootstrap,\n timeout: timeoutMs,\n strict,\n });\n\n const results: BootstrapResult['results'] = {};\n const errors: string[] = [];\n\n // Bootstrap each producer\n for (const name of producersToBootstrap) {\n const producer = this.producers.get(name);\n if (!producer) {\n results[name] = {\n success: false,\n duration: 0,\n error: `Producer '${name}' not found`,\n };\n errors.push(`Producer '${name}' not found`);\n continue;\n }\n\n const producerStartTime = Date.now();\n try {\n await producer.ping({ timeout: timeoutMs, force: true });\n const health = producer.getHealth();\n\n results[name] = {\n success: true,\n duration: Date.now() - producerStartTime,\n clusterId: health.clusterId ?? undefined,\n brokerCount: health.brokerCount,\n };\n\n this.logger.info(`Kafka: [${name}] Bootstrap successful`, {\n duration: Date.now() - producerStartTime,\n clusterId: health.clusterId,\n brokerCount: health.brokerCount,\n });\n } catch (error) {\n const err = error as Error;\n results[name] = {\n success: false,\n duration: Date.now() - producerStartTime,\n error: err.message,\n };\n errors.push(`[${name}] ${err.message}`);\n\n this.logger.error(`Kafka: [${name}] Bootstrap failed`, {\n duration: Date.now() - producerStartTime,\n error: err.message,\n });\n }\n }\n\n const totalDuration = Date.now() - startTime;\n const successCount = Object.values(results).filter(r => r.success).length;\n const totalCount = Object.keys(results).length;\n const success = strict ? successCount === totalCount : successCount > 0;\n\n const result: BootstrapResult = {\n success,\n duration: totalDuration,\n results,\n };\n\n if (success) {\n this.logger.info('Kafka: Bootstrap completed successfully', {\n duration: totalDuration,\n successCount,\n totalCount,\n });\n } else {\n const errorMessage = `Kafka bootstrap failed (${successCount}/${totalCount} producers succeeded)\\n\\n`\n + `Failures:\\n${errors.map(e => ` - ${e}`).join('\\n')}\\n\\n`\n + `Configuration:\\n`\n + ` Strict mode: ${strict}\\n`\n + ` Timeout: ${timeoutMs}ms\\n`\n + ` Producers attempted: ${producersToBootstrap.join(', ')}`;\n\n this.logger.error('Kafka: Bootstrap failed', {\n duration: totalDuration,\n successCount,\n totalCount,\n errors,\n });\n\n throw new KafkaError(errorMessage);\n }\n\n return result;\n }\n\n /**\n * Get detailed health snapshot for all producers\n * Returns comprehensive metadata including timestamps, errors, cluster info\n *\n * @example\n * const health = kafka.getHealth();\n * console.log(health.main.lastPingSucceededAt); // timestamp\n * console.log(health.main.clusterId); // 'prod-kafka-01'\n */\n getHealth(): Record<TProducers, ProducerHealth> {\n const health: Record<string, ProducerHealth> = {};\n for (const [name, producer] of this.producers.entries()) {\n health[name] = producer.getHealth();\n }\n return health as Record<TProducers, ProducerHealth>;\n }\n\n /**\n * Get detailed health for a specific producer\n */\n getProducerHealth(name: TProducers): ProducerHealth {\n const producer = this.getProducer(name);\n return producer.getHealth();\n }\n\n /**\n * Lightweight liveness check - does NOT perform Kafka operations\n * Only checks internal state for fatal errors\n * Suitable for Kubernetes liveness probes\n *\n * Returns false if:\n * - Manager is in a fatal state\n * - Graceful shutdown has started\n *\n * @example\n * app.get('/health/live', (req, res) => {\n * res.status(kafka.isLive() ? 200 : 503).json({ live: kafka.isLive() });\n * });\n */\n isLive(): boolean {\n return !this.fatalError && !this.gracefulShutdownStarted;\n }\n\n /**\n * Ping all producers to verify connectivity\n */\n async ping(): Promise<void> {\n const promises = Array.from(this.producers.values(), p => p.ping());\n await Promise.all(promises);\n }\n\n /**\n * Ping a specific producer\n */\n async pingProducer(name: TProducers): Promise<void> {\n await this.getProducer(name).ping();\n }\n\n /**\n * Check if Kafka is ready to handle traffic\n * Revalidates connectivity if cache is stale\n *\n * @param options.timeout - Override default health check timeout\n * @param options.force - Force revalidation, ignore cache\n *\n * @example\n * // Use cached result if fresh (within healthCheckCacheMs)\n * const ready = await kafka.isReady();\n *\n * @example\n * // Force immediate revalidation\n * const ready = await kafka.isReady({ force: true });\n */\n async isReady(options?: ReadinessOptions): Promise<boolean> {\n if (!this.enabled) {\n return true;\n }\n\n const now = Date.now();\n const force = options?.force ?? false;\n\n // Use cached result if available and fresh\n if (!force && this.lastReadinessCheck) {\n const age = now - this.lastReadinessCheck.timestamp;\n if (age < this.healthCheckCacheMs) {\n this.logger.debug('Kafka: Using cached readiness result', {\n result: this.lastReadinessCheck.result,\n age,\n });\n return this.lastReadinessCheck.result;\n }\n }\n\n // Revalidate connectivity\n try {\n const timeout = options?.timeout ?? this.healthCheckTimeoutMs;\n const promises = Array.from(this.producers.values()).map(p =>\n p.ping({ timeout, force: true }),\n );\n await Promise.all(promises);\n\n this.lastReadinessCheck = { result: true, timestamp: now };\n return true;\n } catch (error) {\n this.lastReadinessCheck = { result: false, timestamp: now };\n this.logger.warn('Kafka: Readiness check failed', {\n error: (error as Error).message,\n });\n return false;\n }\n }\n\n /**\n * Check connection status of all producers\n * Returns detailed status including last success time and errors\n */\n getConnectionStatus(): Record<TProducers, ConnectionStatus> {\n const status: Record<string, ConnectionStatus> = {};\n for (const [name, producer] of this.producers.entries()) {\n const health = producer.getHealth();\n status[name] = {\n connected: health.isConnected,\n lastSuccessAt: health.lastPingSucceededAt,\n lastError: health.lastError?.message ?? null,\n };\n }\n return status as Record<TProducers, ConnectionStatus>;\n }\n\n /**\n * Get a topic definition\n */\n private getTopicDefinition<TName extends keyof TTopics & string>(topicName: TName): TopicDefinition {\n const topic = this.topics.get(topicName);\n if (!topic) {\n throw new KafkaError(\n `Topic '${topicName}' not found. Available topics: ${Array.from(this.topics.keys()).join(', ')}`,\n );\n }\n return topic;\n }\n\n /**\n * Publish a message to a typed topic\n * Automatically validates payload against Zod schema and routes to correct producer\n *\n * @template TName - Topic name (inferred from topics config)\n * @param topicName - Name of the topic to publish to\n * @param value - Payload (type-checked against topic's Zod schema)\n * @param options - Optional publish options including headers, key, partition\n *\n * @example\n * await kafka.publish('order.created', {\n * orderId: '123',\n * amount: 99.99,\n * customerId: '456',\n * });\n */\n async publish<TName extends keyof TTopics & string>(\n topicName: TName,\n value: TopicPayload<TTopics, TName>,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n const topicDef = this.getTopicDefinition(topicName);\n\n // Validate payload against schema unless skipped\n if (!options?.skipValidation) {\n try {\n topicDef.schema.parse(value);\n } catch (error) {\n this.logger.error(`Kafka: Validation failed for topic '${topicName}'`, { error, value });\n throw new KafkaError(\n `Validation failed for topic '${topicName}': ${(error as Error).message}`,\n { cause: error },\n );\n }\n }\n\n // Get producer and publish\n const producer = this.getProducer(topicDef.producer as TProducers);\n return producer.publish(topicName, value, options);\n }\n\n /**\n * Publish a batch of messages to a typed topic\n * Automatically validates payloads against Zod schema and routes to correct producer\n *\n * @template TName - Topic name (inferred from topics config)\n * @param topicName - Name of the topic to publish to\n * @param options - Batch options with array of messages\n *\n * @example\n * await kafka.publishBatch('order.created', {\n * messages: [\n * { value: { orderId: '1', amount: 10, customerId: 'a' } },\n * { value: { orderId: '2', amount: 20, customerId: 'b' } },\n * ],\n * });\n */\n async publishBatch<TName extends keyof TTopics & string>(\n topicName: TName,\n options: PublishBatchOptions<TopicPayload<TTopics, TName>>,\n ): Promise<RecordMetadata[]> {\n const topicDef = this.getTopicDefinition(topicName);\n\n // Validate all payloads against schema unless skipped\n if (!options.skipValidation) {\n for (let i = 0; i < options.messages.length; i++) {\n try {\n topicDef.schema.parse(options.messages[i].value);\n } catch (error) {\n this.logger.error(`Kafka: Validation failed for topic '${topicName}' message ${i}`, {\n error,\n value: options.messages[i].value,\n });\n throw new KafkaError(\n `Validation failed for topic '${topicName}' message ${i}: ${(error as Error).message}`,\n { cause: error },\n );\n }\n }\n }\n\n // Get producer and publish batch\n const producer = this.getProducer(topicDef.producer as TProducers);\n return producer.publishBatch(topicName, options);\n }\n\n /**\n * Disconnect all producers\n */\n async disconnect(): Promise<void> {\n const promises = Array.from(this.producers.values()).map(p => p.disconnect());\n await Promise.all(promises);\n this.logger.info('Kafka: All producers disconnected');\n }\n\n /**\n * Disconnect a specific producer\n */\n async disconnectProducer(name: TProducers): Promise<void> {\n await this.getProducer(name).disconnect();\n }\n}\n"],"mapp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CAAE,MAAO,EAAO,CACjB,CAML,OADiB,KAAK,YAAY,EAAS,SAAuB,CAClD,QAAQ,EAAW,EAAO,EAAQ,CAmBpD,MAAM,aACJ,EACA,EAC2B,CAC3B,IAAM,EAAW,KAAK,mBAAmB,EAAU,CAGnD,GAAI,CAAC,EAAQ,eACX,IAAK,IAAI,EAAI,EAAG,EAAI,EAAQ,SAAS,OAAQ,IAC3C,GAAI,CACF,EAAS,OAAO,MAAM,EAAQ,SAAS,GAAG,MAAM,OACzC,EAAO,CAKd,MAJA,KAAK,OAAO,MAAM,uCAAuC,EAAU,YAAY,IAAK,CAClF,QACA,MAAO,EAAQ,SAAS,GAAG,MAC5B,CAAC,CACI,IAAI,EACR,gCAAgC,EAAU,YAAY,EAAE,IAAK,EAAgB,UAC7E,CAAE,MAAO,EAAO,CACjB,CAOP,OADiB,KAAK,YAAY,EAAS,SAAuB,CAClD,aAAa,EAAW,EAAQ,CAMlD,MAAM,YAA4B,CAChC,IAAM,EAAW,MAAM,KAAK,KAAK,UAAU,QAAQ,CAAC,CAAC,IAAI,GAAK,EAAE,YAAY,CAAC,CAC7E,MAAM,QAAQ,IAAI,EAAS,CAC3B,KAAK,OAAO,KAAK,oCAAoC,CAMvD,MAAM,mBAAmB,EAAiC,CACxD,MAAM,KAAK,YAAY,EAAK,CAAC,YAAY"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/kafka",
3
- "version": "0.6.3",
3
+ "version": "0.7.0-alpha.1",
4
4
  "description": "Internal wrapper for Apache Kafka producer",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -32,7 +32,8 @@
32
32
  },
33
33
  "homepage": "https://github.com/Autofleet/autorepo/tree/master/packages/kafka#readme",
34
34
  "dependencies": {
35
- "@platformatic/kafka": "^1.21.0"
35
+ "@platformatic/kafka": "^1.21.0",
36
+ "google-auth-library": "^10.5.0"
36
37
  },
37
38
  "peerDependencies": {
38
39
  "@autofleet/logger": "*",