@autofleet/kafka 0.3.1 → 0.4.0

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.
@@ -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 });\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 + ` 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(options: PublishBatchOptions): Promise<RecordMetadata[]> {\n if (!options.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: options.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 ${options.topic}`, {\n topic: options.topic,\n count: messages.length,\n });\n\n return messages.map((msg, idx) => ({\n topic: options.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 ${options.topic}`, { error });\n throw new KafkaError(`[${this.name}] Failed to publish batch to topic ${options.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(options: PublishBatchOptions): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${options.topic}`, {\n messageCount: options.messages.length,\n });\n return Promise.resolve(options.messages.map(() => ({\n topic: options.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} from './types';\n\n/**\n * Manager for multiple Kafka producers\n * Provides lifecycle management, health tracking, and operational guarantees\n */\nexport class KafkaManager<TProducers extends string = string> {\n private readonly producers = new Map<string, IProducer>();\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 this.logger.info('Kafka: Initialized KafkaManager', {\n enabled: this.enabled,\n producerCount: Object.keys(options.producers).length,\n producers: Object.keys(options.producers),\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 this.logger.info('Kafka: Created mock producers (Kafka disabled)');\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 */\n static create<T extends Record<string, ProducerConfig>>(\n options: Omit<KafkaManagerOptions, 'producers'> & { producers: T; },\n ): KafkaManager<keyof T & string> {\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 private 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 * Publish a message using a named producer\n */\n async publish<T = unknown>(\n producerName: TProducers,\n topic: string,\n value: T,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n return this.getProducer(producerName).publish(topic, value, options);\n }\n\n /**\n * Publish a batch of messages using a named producer\n */\n async publishBatch(\n producerName: TProducers,\n options: PublishBatchOptions,\n ): Promise<RecordMetadata[]> {\n return this.getProducer(producerName).publishBatch(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: T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OAAQ,KAAK,mBAAmB,OAChC,MACD,CAAC,CACK,KAAK,mBAAmB,OAKnC,GAAI,CACF,IAAM,EAAU,GAAS,SAAW,KAAK,qBACnC,EAAW,MAAM,KAAK,KAAK,UAAU,QAAQ,CAAC,CAAC,IAAI,GACvD,EAAE,KAAK,CAAE,UAAS,MAAO,GAAM,CAAC,CACjC,CAID,OAHA,MAAM,QAAQ,IAAI,EAAS,CAE3B,KAAK,mBAAqB,CAAE,OAAQ,GAAM,UAAW,EAAK,CACnD,SACA,EAAO,CAKd,MAJA,MAAK,mBAAqB,CAAE,OAAQ,GAAO,UAAW,EAAK,CAC3D,KAAK,OAAO,KAAK,gCAAiC,CAChD,MAAQ,EAAgB,QACzB,CAAC,CACK,IAQX,qBAA4D,CAC1D,IAAMC,EAA2C,EAAE,CACnD,IAAK,GAAM,CAAC,EAAM,KAAa,KAAK,UAAU,SAAS,CAAE,CACvD,IAAM,EAAS,EAAS,WAAW,CACnC,EAAO,GAAQ,CACb,UAAW,EAAO,YAClB,cAAe,EAAO,oBACtB,UAAW,EAAO,WAAW,SAAW,KACzC,CAEH,OAAO,EAMT,MAAM,QACJ,EACA,EACA,EACA,EAC2B,CAC3B,OAAO,KAAK,YAAY,EAAa,CAAC,QAAQ,EAAO,EAAO,EAAQ,CAMtE,MAAM,aACJ,EACA,EAC2B,CAC3B,OAAO,KAAK,YAAY,EAAa,CAAC,aAAa,EAAQ,CAM7D,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":["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 });\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 + ` 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(options: PublishBatchOptions): Promise<RecordMetadata[]> {\n if (!options.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: options.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 ${options.topic}`, {\n topic: options.topic,\n count: messages.length,\n });\n\n return messages.map((msg, idx) => ({\n topic: options.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 ${options.topic}`, { error });\n throw new KafkaError(`[${this.name}] Failed to publish batch to topic ${options.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(options: PublishBatchOptions): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${options.topic}`, {\n messageCount: options.messages.length,\n });\n return Promise.resolve(options.messages.map(() => ({\n topic: options.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} from './types';\n\n/**\n * Manager for multiple Kafka producers\n * Provides lifecycle management, health tracking, and operational guarantees\n */\nexport class KafkaManager<TProducers extends string = string> {\n private readonly producers = new Map<string, IProducer>();\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 this.logger.info('Kafka: Initialized KafkaManager', {\n enabled: this.enabled,\n producerCount: Object.keys(options.producers).length,\n producers: Object.keys(options.producers),\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 this.logger.info('Kafka: Created mock producers (Kafka disabled)');\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 */\n static create<T extends Record<string, ProducerConfig>>(\n options: Omit<KafkaManagerOptions, 'producers'> & { producers: T; },\n ): KafkaManager<keyof T & string> {\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 * Publish a message using a named producer\n */\n async publish<T = unknown>(\n producerName: TProducers,\n topic: string,\n value: T,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n return this.getProducer(producerName).publish(topic, value, options);\n }\n\n /**\n * Publish a batch of messages using a named producer\n */\n async publishBatch(\n producerName: TProducers,\n options: PublishBatchOptions,\n ): Promise<RecordMetadata[]> {\n return this.getProducer(producerName).publishBatch(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,KAA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OAHA,MAAM,QAAQ,IAAI,EAAS,CAE3B,KAAK,mBAAqB,CAAE,OAAQ,GAAM,UAAW,EAAK,CACnD,SACA,EAAO,CAKd,MAJA,MAAK,mBAAqB,CAAE,OAAQ,GAAO,UAAW,EAAK,CAC3D,KAAK,OAAO,KAAK,gCAAiC,CAChD,MAAQ,EAAgB,QACzB,CAAC,CACK,IAQX,qBAA4D,CAC1D,IAAMC,EAA2C,EAAE,CACnD,IAAK,GAAM,CAAC,EAAM,KAAa,KAAK,UAAU,SAAS,CAAE,CACvD,IAAM,EAAS,EAAS,WAAW,CACnC,EAAO,GAAQ,CACb,UAAW,EAAO,YAClB,cAAe,EAAO,oBACtB,UAAW,EAAO,WAAW,SAAW,KACzC,CAEH,OAAO,EAMT,MAAM,QACJ,EACA,EACA,EACA,EAC2B,CAC3B,OAAO,KAAK,YAAY,EAAa,CAAC,QAAQ,EAAO,EAAO,EAAQ,CAMtE,MAAM,aACJ,EACA,EAC2B,CAC3B,OAAO,KAAK,YAAY,EAAa,CAAC,aAAa,EAAQ,CAM7D,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
@@ -181,7 +181,7 @@ declare class KafkaManager<TProducers extends string = string> {
181
181
  producers: T;
182
182
  }): KafkaManager<keyof T & string>;
183
183
  private setupGracefulShutdown;
184
- private gracefulShutdown;
184
+ gracefulShutdown(signal: string): Promise<void>;
185
185
  /**
186
186
  * Check if Kafka is enabled
187
187
  */
package/dist/index.d.ts CHANGED
@@ -181,7 +181,7 @@ declare class KafkaManager<TProducers extends string = string> {
181
181
  producers: T;
182
182
  }): KafkaManager<keyof T & string>;
183
183
  private setupGracefulShutdown;
184
- private gracefulShutdown;
184
+ gracefulShutdown(signal: string): Promise<void>;
185
185
  /**
186
186
  * Check if Kafka is enabled
187
187
  */
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 });\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 + ` 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(options: PublishBatchOptions): Promise<RecordMetadata[]> {\n if (!options.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: options.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 ${options.topic}`, {\n topic: options.topic,\n count: messages.length,\n });\n\n return messages.map((msg, idx) => ({\n topic: options.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 ${options.topic}`, { error });\n throw new KafkaError(`[${this.name}] Failed to publish batch to topic ${options.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(options: PublishBatchOptions): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${options.topic}`, {\n messageCount: options.messages.length,\n });\n return Promise.resolve(options.messages.map(() => ({\n topic: options.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} from './types';\n\n/**\n * Manager for multiple Kafka producers\n * Provides lifecycle management, health tracking, and operational guarantees\n */\nexport class KafkaManager<TProducers extends string = string> {\n private readonly producers = new Map<string, IProducer>();\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 this.logger.info('Kafka: Initialized KafkaManager', {\n enabled: this.enabled,\n producerCount: Object.keys(options.producers).length,\n producers: Object.keys(options.producers),\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 this.logger.info('Kafka: Created mock producers (Kafka disabled)');\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 */\n static create<T extends Record<string, ProducerConfig>>(\n options: Omit<KafkaManagerOptions, 'producers'> & { producers: T; },\n ): KafkaManager<keyof T & string> {\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 private 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 * Publish a message using a named producer\n */\n async publish<T = unknown>(\n producerName: TProducers,\n topic: string,\n value: T,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n return this.getProducer(producerName).publish(topic, value, options);\n }\n\n /**\n * Publish a batch of messages using a named producer\n */\n async publishBatch(\n producerName: TProducers,\n options: PublishBatchOptions,\n ): Promise<RecordMetadata[]> {\n return this.getProducer(producerName).publishBatch(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 disconnect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Q,KAAK,mBAAmB,OAChC,MACD,CAAC,CACK,KAAK,mBAAmB,OAKnC,GAAI,CACF,IAAM,EAAU,GAAS,SAAW,KAAK,qBACnC,EAAW,MAAM,KAAK,KAAK,UAAU,QAAQ,CAAC,CAAC,IAAI,GACvD,EAAE,KAAK,CAAE,UAAS,MAAO,GAAM,CAAC,CACjC,CAID,OAHA,MAAM,QAAQ,IAAI,EAAS,CAE3B,KAAK,mBAAqB,CAAE,OAAQ,GAAM,UAAW,EAAK,CACnD,SACA,EAAO,CAKd,MAJA,MAAK,mBAAqB,CAAE,OAAQ,GAAO,UAAW,EAAK,CAC3D,KAAK,OAAO,KAAK,gCAAiC,CAChD,MAAQ,EAAgB,QACzB,CAAC,CACK,IAQX,qBAA4D,CAC1D,IAAMC,EAA2C,EAAE,CACnD,IAAK,GAAM,CAAC,EAAM,KAAa,KAAK,UAAU,SAAS,CAAE,CACvD,IAAM,EAAS,EAAS,WAAW,CACnC,EAAO,GAAQ,CACb,UAAW,EAAO,YAClB,cAAe,EAAO,oBACtB,UAAW,EAAO,WAAW,SAAW,KACzC,CAEH,OAAO,EAMT,MAAM,QACJ,EACA,EACA,EACA,EAC2B,CAC3B,OAAO,KAAK,YAAY,EAAa,CAAC,QAAQ,EAAO,EAAO,EAAQ,CAMtE,MAAM,aACJ,EACA,EAC2B,CAC3B,OAAO,KAAK,YAAY,EAAa,CAAC,aAAa,EAAQ,CAM7D,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":["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 });\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 + ` 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(options: PublishBatchOptions): Promise<RecordMetadata[]> {\n if (!options.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: options.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 ${options.topic}`, {\n topic: options.topic,\n count: messages.length,\n });\n\n return messages.map((msg, idx) => ({\n topic: options.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 ${options.topic}`, { error });\n throw new KafkaError(`[${this.name}] Failed to publish batch to topic ${options.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(options: PublishBatchOptions): Promise<RecordMetadata[]> {\n this.logger.debug(`Kafka: [${this.name}] Mock mode - skipping batch publish to topic: ${options.topic}`, {\n messageCount: options.messages.length,\n });\n return Promise.resolve(options.messages.map(() => ({\n topic: options.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} from './types';\n\n/**\n * Manager for multiple Kafka producers\n * Provides lifecycle management, health tracking, and operational guarantees\n */\nexport class KafkaManager<TProducers extends string = string> {\n private readonly producers = new Map<string, IProducer>();\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 this.logger.info('Kafka: Initialized KafkaManager', {\n enabled: this.enabled,\n producerCount: Object.keys(options.producers).length,\n producers: Object.keys(options.producers),\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 this.logger.info('Kafka: Created mock producers (Kafka disabled)');\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 */\n static create<T extends Record<string, ProducerConfig>>(\n options: Omit<KafkaManagerOptions, 'producers'> & { producers: T; },\n ): KafkaManager<keyof T & string> {\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 * Publish a message using a named producer\n */\n async publish<T = unknown>(\n producerName: TProducers,\n topic: string,\n value: T,\n options?: PublishOptions,\n ): Promise<RecordMetadata[]> {\n return this.getProducer(producerName).publish(topic, value, options);\n }\n\n /**\n * Publish a batch of messages using a named producer\n */\n async publishBatch(\n producerName: TProducers,\n options: PublishBatchOptions,\n ): Promise<RecordMetadata[]> {\n return this.getProducer(producerName).publishBatch(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,OAA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,IAAI,EAAS,CAE3B,KAAK,mBAAqB,CAAE,OAAQ,GAAM,UAAW,EAAK,CACnD,SACA,EAAO,CAKd,MAJA,MAAK,mBAAqB,CAAE,OAAQ,GAAO,UAAW,EAAK,CAC3D,KAAK,OAAO,KAAK,gCAAiC,CAChD,MAAQ,EAAgB,QACzB,CAAC,CACK,IAQX,qBAA4D,CAC1D,IAAMC,EAA2C,EAAE,CACnD,IAAK,GAAM,CAAC,EAAM,KAAa,KAAK,UAAU,SAAS,CAAE,CACvD,IAAM,EAAS,EAAS,WAAW,CACnC,EAAO,GAAQ,CACb,UAAW,EAAO,YAClB,cAAe,EAAO,oBACtB,UAAW,EAAO,WAAW,SAAW,KACzC,CAEH,OAAO,EAMT,MAAM,QACJ,EACA,EACA,EACA,EAC2B,CAC3B,OAAO,KAAK,YAAY,EAAa,CAAC,QAAQ,EAAO,EAAO,EAAQ,CAMtE,MAAM,aACJ,EACA,EAC2B,CAC3B,OAAO,KAAK,YAAY,EAAa,CAAC,aAAa,EAAQ,CAM7D,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.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "Internal wrapper for Apache Kafka producer",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/node": "^20.14.11",
42
- "@autofleet/logger": "^4.2.33"
42
+ "@autofleet/logger": "^4.2.36"
43
43
  },
44
44
  "keywords": [
45
45
  "kafka",