@eventferry/kafka 2.0.0 → 3.1.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,"sources":["../src/index.ts","../src/kafkajs-driver.ts","../src/confluent-driver.ts","../src/publisher.ts"],"sourcesContent":["export * from \"./driver.js\";\nexport * from \"./kafkajs-driver.js\";\nexport * from \"./confluent-driver.js\";\nexport * from \"./publisher.js\";\n","import type { PublishableMessage, PublishResult } from \"@eventferry/core\";\nimport type {\n KafkaConnectionConfig,\n KafkaDriver,\n ProducerBehaviorConfig,\n} from \"./driver.js\";\n\n// Loosely-typed structural references to the kafkajs API so this file\n// compiles without kafkajs installed (it's an optional peer dep).\ninterface KjsProducer {\n connect(): Promise<void>;\n disconnect(): Promise<void>;\n // kafkajs: sendBatch takes { topicMessages }, send takes a single { topic, messages }.\n sendBatch(args: unknown): Promise<unknown>;\n transaction(): Promise<KjsTransaction>;\n}\ninterface KjsTransaction {\n sendBatch(args: unknown): Promise<unknown>;\n commit(): Promise<void>;\n abort(): Promise<void>;\n}\ninterface KjsKafka {\n producer(args?: unknown): KjsProducer;\n}\n\nexport interface KafkaJsDriverOptions\n extends KafkaConnectionConfig,\n ProducerBehaviorConfig {}\n\n/**\n * Driver backed by the pure-JS `kafkajs` client. Simple, zero native deps.\n */\nexport class KafkaJsDriver implements KafkaDriver {\n readonly transactional: boolean;\n private producer: KjsProducer | null = null;\n private readonly opts: KafkaJsDriverOptions;\n\n constructor(opts: KafkaJsDriverOptions) {\n this.opts = opts;\n this.transactional = opts.transactional ?? false;\n if (this.transactional && !opts.transactionalId) {\n throw new Error(\n \"KafkaJsDriver: transactionalId is required when transactional=true\",\n );\n }\n }\n\n async connect(): Promise<void> {\n this.producer = await this.createProducer();\n await this.producer.connect();\n }\n\n /**\n * Construct the underlying kafkajs producer. Overridable as a test seam so\n * the send/transaction logic can be exercised without a real broker.\n */\n protected async createProducer(): Promise<KjsProducer> {\n const mod = await importKafkaJs();\n const kafka: KjsKafka = new mod.Kafka({\n clientId: this.opts.clientId ?? \"eventferry\",\n brokers: this.opts.brokers,\n ssl: this.opts.ssl,\n sasl: this.opts.sasl,\n });\n return kafka.producer({\n idempotent: this.opts.idempotent ?? true,\n maxInFlightRequests: this.transactional ? 1 : undefined,\n transactionalId: this.transactional ? this.opts.transactionalId : undefined,\n });\n }\n\n async disconnect(): Promise<void> {\n await this.producer?.disconnect();\n this.producer = null;\n }\n\n async sendBatch(messages: PublishableMessage[]): Promise<PublishResult[]> {\n if (!this.producer) throw new Error(\"KafkaJsDriver not connected\");\n const topicMessages = groupByTopic(messages, this.opts.compression);\n\n if (this.transactional) {\n const txn = await this.producer.transaction();\n try {\n await txn.sendBatch({ topicMessages, acks: this.opts.acks ?? -1 });\n await txn.commit();\n return messages.map((m) => ({ recordId: m.recordId, ok: true }));\n } catch (err) {\n await txn.abort().catch(() => undefined);\n const error = err instanceof Error ? err : new Error(String(err));\n return messages.map((m) => ({ recordId: m.recordId, ok: false, error }));\n }\n }\n\n try {\n await this.producer.sendBatch({ topicMessages, acks: this.opts.acks ?? -1 });\n return messages.map((m) => ({ recordId: m.recordId, ok: true }));\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n return messages.map((m) => ({ recordId: m.recordId, ok: false, error }));\n }\n }\n}\n\nfunction groupByTopic(messages: PublishableMessage[], compression?: string) {\n const byTopic = new Map<string, unknown[]>();\n for (const m of messages) {\n const arr = byTopic.get(m.topic) ?? [];\n arr.push({\n key: m.key,\n value: m.value,\n headers: m.headers,\n });\n byTopic.set(m.topic, arr);\n }\n return [...byTopic.entries()].map(([topic, msgs]) => ({\n topic,\n messages: msgs,\n ...(compression && compression !== \"none\" ? { compression } : {}),\n }));\n}\n\nasync function importKafkaJs(): Promise<{ Kafka: new (cfg: unknown) => KjsKafka }> {\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (await import(\"kafkajs\")) as any;\n } catch {\n throw new Error(\n 'Driver \"kafkajs\" selected but the \"kafkajs\" package is not installed. Run: npm i kafkajs',\n );\n }\n}\n","import type { PublishableMessage, PublishResult } from \"@eventferry/core\";\nimport type {\n KafkaConnectionConfig,\n KafkaDriver,\n ProducerBehaviorConfig,\n} from \"./driver.js\";\n\n// Structural typing of the confluent KafkaJS-compatible API surface so this\n// file compiles without the optional native dep installed.\ninterface CkProducer {\n connect(): Promise<void>;\n disconnect(): Promise<void>;\n send(args: unknown): Promise<unknown>;\n transaction(): Promise<CkTransaction>;\n}\ninterface CkTransaction {\n send(args: unknown): Promise<unknown>;\n commit(): Promise<void>;\n abort(): Promise<void>;\n}\ninterface CkKafka {\n producer(args?: unknown): CkProducer;\n}\n\nexport interface ConfluentDriverOptions\n extends KafkaConnectionConfig,\n ProducerBehaviorConfig {}\n\n/**\n * Driver backed by `@confluentinc/kafka-javascript` (librdkafka wrapper).\n * Higher throughput; uses the KafkaJS-compatible promisified API so the\n * adapter mirrors the kafkajs driver closely.\n */\nexport class ConfluentDriver implements KafkaDriver {\n readonly transactional: boolean;\n private producer: CkProducer | null = null;\n private readonly opts: ConfluentDriverOptions;\n\n constructor(opts: ConfluentDriverOptions) {\n this.opts = opts;\n this.transactional = opts.transactional ?? false;\n if (this.transactional && !opts.transactionalId) {\n throw new Error(\n \"ConfluentDriver: transactionalId is required when transactional=true\",\n );\n }\n }\n\n async connect(): Promise<void> {\n this.producer = await this.createProducer();\n await this.producer.connect();\n }\n\n /**\n * Construct the underlying confluent producer. Overridable as a test seam so\n * the send/transaction logic can be exercised without a real broker.\n */\n protected async createProducer(): Promise<CkProducer> {\n const mod = await importConfluent();\n const kafka: CkKafka = new mod.KafkaJS.Kafka({\n kafkaJS: {\n clientId: this.opts.clientId ?? \"eventferry\",\n brokers: this.opts.brokers,\n ssl: this.opts.ssl,\n sasl: this.opts.sasl,\n },\n });\n return kafka.producer({\n kafkaJS: {\n idempotent: this.opts.idempotent ?? true,\n ...(this.transactional\n ? { transactionalId: this.opts.transactionalId }\n : {}),\n },\n });\n }\n\n async disconnect(): Promise<void> {\n await this.producer?.disconnect();\n this.producer = null;\n }\n\n async sendBatch(messages: PublishableMessage[]): Promise<PublishResult[]> {\n if (!this.producer) throw new Error(\"ConfluentDriver not connected\");\n const topicMessages = groupByTopic(messages);\n const acks = this.opts.acks ?? -1;\n const compression = this.opts.compression;\n\n const doSends = async (target: CkProducer | CkTransaction) => {\n for (const tm of topicMessages) {\n await target.send({\n topic: tm.topic,\n messages: tm.messages,\n acks,\n ...(compression && compression !== \"none\" ? { compression } : {}),\n });\n }\n };\n\n if (this.transactional) {\n const txn = await this.producer.transaction();\n try {\n await doSends(txn);\n await txn.commit();\n return messages.map((m) => ({ recordId: m.recordId, ok: true }));\n } catch (err) {\n await txn.abort().catch(() => undefined);\n const error = err instanceof Error ? err : new Error(String(err));\n return messages.map((m) => ({ recordId: m.recordId, ok: false, error }));\n }\n }\n\n try {\n await doSends(this.producer);\n return messages.map((m) => ({ recordId: m.recordId, ok: true }));\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n return messages.map((m) => ({ recordId: m.recordId, ok: false, error }));\n }\n }\n}\n\nfunction groupByTopic(messages: PublishableMessage[]) {\n const byTopic = new Map<string, unknown[]>();\n for (const m of messages) {\n const arr = byTopic.get(m.topic) ?? [];\n arr.push({ key: m.key, value: m.value, headers: m.headers });\n byTopic.set(m.topic, arr);\n }\n return [...byTopic.entries()].map(([topic, msgs]) => ({\n topic,\n messages: msgs,\n }));\n}\n\nasync function importConfluent(): Promise<{\n KafkaJS: { Kafka: new (cfg: unknown) => CkKafka };\n}> {\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (await import(\"@confluentinc/kafka-javascript\")) as any;\n } catch {\n throw new Error(\n 'Driver \"confluent\" selected but \"@confluentinc/kafka-javascript\" is not installed. Run: npm i @confluentinc/kafka-javascript',\n );\n }\n}\n","import type {\n PublishableMessage,\n Publisher,\n PublishResult,\n} from \"@eventferry/core\";\nimport { ConfluentDriver } from \"./confluent-driver.js\";\nimport type {\n DriverKind,\n KafkaConnectionConfig,\n KafkaDriver,\n ProducerBehaviorConfig,\n} from \"./driver.js\";\nimport { KafkaJsDriver } from \"./kafkajs-driver.js\";\n\nexport interface KafkaPublisherOptions\n extends KafkaConnectionConfig,\n ProducerBehaviorConfig {\n /** Which underlying client to use. Default \"kafkajs\". */\n driver?: DriverKind;\n /**\n * Provide a fully custom driver instance instead of the built-ins.\n * Useful for testing or unsupported clients.\n */\n customDriver?: KafkaDriver;\n}\n\n/**\n * The Publisher the Relay talks to. Wraps a pluggable KafkaDriver and\n * adds dead-letter routing. Works against Kafka and Redpanda identically\n * (Redpanda is Kafka-API compatible).\n */\nexport class KafkaPublisher implements Publisher {\n private readonly driver: KafkaDriver;\n\n constructor(opts: KafkaPublisherOptions) {\n this.driver = opts.customDriver ?? selectDriver(opts);\n }\n\n connect(): Promise<void> {\n return this.driver.connect();\n }\n\n disconnect(): Promise<void> {\n return this.driver.disconnect();\n }\n\n publish(messages: PublishableMessage[]): Promise<PublishResult[]> {\n return this.driver.sendBatch(messages);\n }\n\n /**\n * Send a single dead-lettered message. The message already carries the\n * DLQ topic (the Relay rewrites it), plus the failure reason as a header.\n */\n async publishToDlq(message: PublishableMessage, error: Error): Promise<void> {\n const dlqMessage: PublishableMessage = {\n ...message,\n headers: {\n ...message.headers,\n \"dlq-reason\": error.message,\n \"dlq-original-topic\": message.headers[\"original-topic\"] ?? \"\",\n \"dlq-failed-at\": new Date().toISOString(),\n },\n };\n const [result] = await this.driver.sendBatch([dlqMessage]);\n if (result && !result.ok) {\n throw result.error ?? new Error(\"DLQ publish failed\");\n }\n }\n\n /** Whether the configured driver provides atomic (EOS) batch sends. */\n get transactional(): boolean {\n return this.driver.transactional;\n }\n}\n\nfunction selectDriver(opts: KafkaPublisherOptions): KafkaDriver {\n const kind = opts.driver ?? \"kafkajs\";\n switch (kind) {\n case \"kafkajs\":\n return new KafkaJsDriver(opts);\n case \"confluent\":\n return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AS,IAAI,MAAM,oBAAoB;AAAA,IACtD;AAAA,EACF;AAAA;AAAA,EAGA,IAAI,gBAAyB;AAC3B,WAAO,KAAK,OAAO;AAAA,EACrB;AACF;AAEA,SAAS,aAAa,MAA0C;AAC9D,QAAM,OAAO,KAAK,UAAU;AAC5B,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH,aAAO,IAAI,cAAc,IAAI;AAAA,IAC/B,KAAK;AACH,aAAO,IAAI,gBAAgB,IAAI;AAAA,IACjC;AACE,YAAM,IAAI,MAAM,mBAAmB,IAAI,GAAG;AAAA,EAC9C;AACF;","names":["groupByTopic"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/kafkajs-classifier.ts","../src/kafkajs-driver.ts","../src/confluent-classifier.ts","../src/confluent-config.ts","../src/confluent-driver.ts","../src/publisher.ts"],"sourcesContent":["export * from \"./driver.js\";\nexport * from \"./kafkajs-driver.js\";\nexport * from \"./kafkajs-classifier.js\";\nexport * from \"./confluent-driver.js\";\nexport * from \"./confluent-classifier.js\";\nexport * from \"./confluent-config.js\";\nexport * from \"./publisher.js\";\n","import type { PublishErrorKind } from \"@eventferry/core\";\n\n/**\n * Classify a kafkajs producer error into a {@link PublishErrorKind} so the\n * core relay can decide whether to retry, short-circuit to the DLQ, or pause\n * polling.\n *\n * Mapping verified against `kafkajs/src/errors.js` (v2.x). Protocol error\n * codes match the Kafka Protocol error-code registry. Library-specific\n * subclasses (`KafkaJSRequestTimeoutError`, `KafkaJSConnectionError`,\n * `KafkaJSNonRetriableError`) are matched by the `name` property kafkajs\n * sets on its Error subclasses.\n *\n * Unknown errors fall back to `\"retriable\"` — the safe bias. At worst we\n * retry an error that should have been skipped; in practice we'd rather\n * over-retry than mis-classify a transient blip as terminal.\n */\nexport function classifyKafkajsError(err: unknown): PublishErrorKind {\n if (!err || typeof err !== \"object\") return \"retriable\";\n const e = err as { name?: string; type?: string; code?: number };\n\n // Class-based first — these don't carry a protocol error code.\n if (e.name === \"KafkaJSConnectionError\") return \"retriable\";\n if (e.name === \"KafkaJSRequestTimeoutError\") return \"retriable\";\n if (e.name === \"KafkaJSNonRetriableError\") return \"fatal\";\n\n // Protocol error type (string) — kafkajs's KafkaJSProtocolError exposes\n // both `type` (uppercase string) and `code` (number). Use `type` first\n // for readability and fall back to `code` for codes that lack a stable\n // string label.\n const type = typeof e.type === \"string\" ? e.type : undefined;\n if (type) {\n if (RETRIABLE_TYPES.has(type)) return \"retriable\";\n if (POISON_TYPES.has(type)) return \"poison\";\n if (FATAL_TYPES.has(type)) return \"fatal\";\n }\n\n if (typeof e.code === \"number\") {\n const k = CODE_TO_KIND.get(e.code);\n if (k) return k;\n }\n\n return \"retriable\";\n}\n\nconst RETRIABLE_TYPES = new Set<string>([\n \"NOT_LEADER_FOR_PARTITION\",\n \"LEADER_NOT_AVAILABLE\",\n \"UNKNOWN_TOPIC_OR_PARTITION\",\n \"NETWORK_EXCEPTION\",\n \"REQUEST_TIMED_OUT\",\n \"REPLICA_NOT_AVAILABLE\",\n \"NOT_ENOUGH_REPLICAS\",\n \"NOT_ENOUGH_REPLICAS_AFTER_APPEND\",\n \"FENCED_LEADER_EPOCH\",\n \"UNKNOWN_LEADER_EPOCH\",\n \"BROKER_NOT_AVAILABLE\",\n \"COORDINATOR_LOAD_IN_PROGRESS\",\n \"COORDINATOR_NOT_AVAILABLE\",\n]);\n\nconst POISON_TYPES = new Set<string>([\n \"CORRUPT_MESSAGE\",\n \"MESSAGE_TOO_LARGE\",\n \"INVALID_RECORD\",\n \"UNSUPPORTED_COMPRESSION_TYPE\",\n \"INVALID_REQUIRED_ACKS\",\n \"INVALID_PARTITIONS\",\n]);\n\nconst FATAL_TYPES = new Set<string>([\n \"INVALID_PRODUCER_EPOCH\",\n \"PRODUCER_FENCED\",\n \"TOPIC_AUTHORIZATION_FAILED\",\n \"CLUSTER_AUTHORIZATION_FAILED\",\n \"TRANSACTIONAL_ID_AUTHORIZATION_FAILED\",\n \"SASL_AUTHENTICATION_FAILED\",\n \"INVALID_TRANSACTION_STATE\",\n \"UNSUPPORTED_VERSION\",\n]);\n\n/** Numeric fallback for clusters that only return the wire code. */\nconst CODE_TO_KIND: ReadonlyMap<number, PublishErrorKind> = new Map([\n [2, \"poison\"], // CORRUPT_MESSAGE\n [3, \"retriable\"], // UNKNOWN_TOPIC_OR_PARTITION\n [5, \"retriable\"], // LEADER_NOT_AVAILABLE\n [6, \"retriable\"], // NOT_LEADER_FOR_PARTITION\n [7, \"retriable\"], // REQUEST_TIMED_OUT\n [9, \"retriable\"], // REPLICA_NOT_AVAILABLE\n [10, \"poison\"], // MESSAGE_TOO_LARGE\n [13, \"retriable\"], // NETWORK_EXCEPTION\n [19, \"retriable\"], // NOT_ENOUGH_REPLICAS\n [29, \"fatal\"], // TOPIC_AUTHORIZATION_FAILED\n [31, \"fatal\"], // CLUSTER_AUTHORIZATION_FAILED\n [47, \"fatal\"], // INVALID_PRODUCER_EPOCH\n [58, \"fatal\"], // SASL_AUTHENTICATION_FAILED\n [74, \"retriable\"], // FENCED_LEADER_EPOCH\n [76, \"poison\"], // UNSUPPORTED_COMPRESSION_TYPE\n [87, \"poison\"], // INVALID_RECORD\n]);\n","import type { PublishableMessage, PublishResult } from \"@eventferry/core\";\nimport { classifyKafkajsError } from \"./kafkajs-classifier.js\";\nimport type {\n KafkaConnectionConfig,\n KafkaDriver,\n KafkaJsPartitionerChoice,\n ProducerBehaviorConfig,\n} from \"./driver.js\";\n\n// Loosely-typed structural references to the kafkajs API so this file\n// compiles without kafkajs installed (it's an optional peer dep).\ninterface KjsProducer {\n connect(): Promise<void>;\n disconnect(): Promise<void>;\n // kafkajs: sendBatch takes { topicMessages }, send takes a single { topic, messages }.\n sendBatch(args: unknown): Promise<unknown>;\n transaction(): Promise<KjsTransaction>;\n}\ninterface KjsTransaction {\n sendBatch(args: unknown): Promise<unknown>;\n commit(): Promise<void>;\n abort(): Promise<void>;\n}\ninterface KjsKafka {\n producer(args?: unknown): KjsProducer;\n}\n// kafkajs's `Partitioners` namespace: three factory functions; we pluck them\n// at runtime rather than depending on the kafkajs types.\ninterface KjsPartitionersNamespace {\n DefaultPartitioner: () => unknown;\n LegacyPartitioner: () => unknown;\n JavaCompatiblePartitioner: () => unknown;\n}\n\nexport interface KafkaJsDriverOptions\n extends KafkaConnectionConfig,\n ProducerBehaviorConfig {}\n\n/**\n * kafkajs producer-level knobs we expose on the typed API that kafkajs does\n * NOT actually support. On this driver these are warn-and-ignore; users\n * who need them should switch to the confluent driver.\n */\nconst UNSUPPORTED_BY_KAFKAJS = [\n \"lingerMs\",\n \"batchSize\",\n \"deliveryTimeoutMs\",\n \"maxRequestSize\",\n] as const;\n\n/**\n * Driver backed by the pure-JS `kafkajs` client. Simple, zero native deps.\n */\nexport class KafkaJsDriver implements KafkaDriver {\n readonly transactional: boolean;\n private producer: KjsProducer | null = null;\n private readonly opts: KafkaJsDriverOptions;\n\n constructor(opts: KafkaJsDriverOptions) {\n this.opts = opts;\n this.transactional = opts.transactional ?? false;\n if (this.transactional && !opts.transactionalId) {\n throw new Error(\n \"KafkaJsDriver: transactionalId is required when transactional=true\",\n );\n }\n warnUnsupportedKafkajsOptions(opts);\n }\n\n async connect(): Promise<void> {\n this.producer = await this.createProducer();\n await this.producer.connect();\n }\n\n /**\n * Construct the underlying kafkajs producer. Overridable as a test seam so\n * the send/transaction logic can be exercised without a real broker.\n */\n protected async createProducer(): Promise<KjsProducer> {\n const mod = await importKafkaJs();\n const kafka: KjsKafka = new mod.Kafka({\n clientId: this.opts.clientId ?? \"eventferry\",\n brokers: this.opts.brokers,\n // kafkajs accepts `ssl: tls.ConnectionOptions` directly — Buffer + PEM\n // string both supported. Our TlsConfig is a structural subset of that\n // (`rejectUnauthorized` intentionally omitted; the cluster CA goes via\n // `ca`). No translation needed.\n ssl: this.opts.ssl,\n // SASL: PLAIN / SCRAM-SHA-256 / SCRAM-SHA-512 / OAUTHBEARER. kafkajs's\n // shape matches ours; for OAUTHBEARER kafkajs reads only `value` from\n // the provider's returned token (other fields are ignored).\n sasl: this.opts.sasl,\n });\n const createPartitioner = resolveCreatePartitioner(\n mod.Partitioners,\n this.opts.partitioner,\n this.transactional,\n );\n return kafka.producer({\n idempotent: this.opts.idempotent ?? true,\n // Idempotent / transactional producers cap maxInFlight at 5. When the\n // user picks transactional we force 1 to keep strict ordering across\n // retries on classic (non-idempotent) clusters that haven't migrated\n // to the broker-side fence.\n maxInFlightRequests: this.transactional\n ? 1\n : this.opts.maxInFlightRequests,\n transactionalId: this.transactional\n ? this.opts.transactionalId\n : undefined,\n // kafkajs accepts these directly when set; undefined falls through to\n // the kafkajs default.\n requestTimeout: this.opts.requestTimeoutMs,\n transactionTimeout: this.opts.transactionTimeoutMs,\n // Setting any partitioner choice silences kafkajs's\n // KafkaJSPartitionerNotSpecified warning.\n createPartitioner,\n });\n }\n\n async disconnect(): Promise<void> {\n await this.producer?.disconnect();\n this.producer = null;\n }\n\n async sendBatch(messages: PublishableMessage[]): Promise<PublishResult[]> {\n if (!this.producer) throw new Error(\"KafkaJsDriver not connected\");\n const topicMessages = groupByTopic(messages, this.opts.compression);\n\n if (this.transactional) {\n const txn = await this.producer.transaction();\n try {\n await txn.sendBatch({ topicMessages, acks: this.opts.acks ?? -1 });\n await txn.commit();\n return messages.map((m) => ({ recordId: m.recordId, ok: true }));\n } catch (err) {\n await txn.abort().catch(() => undefined);\n return failedResults(messages, err);\n }\n }\n\n try {\n await this.producer.sendBatch({ topicMessages, acks: this.opts.acks ?? -1 });\n return messages.map((m) => ({ recordId: m.recordId, ok: true }));\n } catch (err) {\n return failedResults(messages, err);\n }\n }\n}\n\nfunction failedResults(\n messages: PublishableMessage[],\n err: unknown,\n): PublishResult[] {\n const error = err instanceof Error ? err : new Error(String(err));\n const errorKind = classifyKafkajsError(err);\n return messages.map((m) => ({\n recordId: m.recordId,\n ok: false,\n error,\n errorKind,\n }));\n}\n\nfunction groupByTopic(messages: PublishableMessage[], compression?: string) {\n const byTopic = new Map<string, unknown[]>();\n for (const m of messages) {\n const arr = byTopic.get(m.topic) ?? [];\n arr.push({\n key: m.key,\n value: m.value,\n headers: m.headers,\n // Per-message partition override. When set, kafkajs routes the record\n // to this exact partition; when undefined, the configured partitioner\n // chooses. We keep the key here too because compacted topics need it\n // even when partition is pinned.\n ...(m.partition !== undefined ? { partition: m.partition } : {}),\n });\n byTopic.set(m.topic, arr);\n }\n return [...byTopic.entries()].map(([topic, msgs]) => ({\n topic,\n messages: msgs,\n ...(compression && compression !== \"none\" ? { compression } : {}),\n }));\n}\n\n/**\n * Resolve the `createPartitioner` factory kafkajs expects on\n * `producer({...})`. Returns `undefined` to fall through to the kafkajs\n * default when no choice is made AND the producer is non-transactional\n * (transactional producers don't trigger the no-partitioner warning).\n */\nfunction resolveCreatePartitioner(\n partitioners: KjsPartitionersNamespace | undefined,\n choice: KafkaJsPartitionerChoice | undefined,\n transactional: boolean,\n): (() => unknown) | undefined {\n if (!partitioners) return undefined;\n // Default to the java-compatible partitioner when the caller didn't pick.\n // It matches the Java client (murmur2) and silences the noisy warning;\n // for transactional producers we leave the kafkajs default alone since\n // EOS ordering is partitioner-agnostic and the warning doesn't fire there.\n const effective: KafkaJsPartitionerChoice =\n choice ?? (transactional ? \"default\" : \"java-compatible\");\n switch (effective) {\n case \"java-compatible\":\n return partitioners.JavaCompatiblePartitioner;\n case \"legacy\":\n return partitioners.LegacyPartitioner;\n case \"default\":\n return partitioners.DefaultPartitioner;\n }\n}\n\n/** Process-wide dedup so we never warn for the same option twice. */\nconst warnedKafkajsKeys = new Set<string>();\n\nfunction warnUnsupportedKafkajsOptions(opts: KafkaJsDriverOptions): void {\n for (const key of UNSUPPORTED_BY_KAFKAJS) {\n if (opts[key] === undefined) continue;\n if (warnedKafkajsKeys.has(key)) continue;\n warnedKafkajsKeys.add(key);\n console.warn(\n `[@eventferry/kafka] '${key}' is not configurable on the kafkajs driver and was ignored. ` +\n `Switch to the confluent driver (driver: \"confluent\") for fine-grained tuning, ` +\n `or remove the option to silence this warning.`,\n );\n }\n}\n\n/** Internal — used by tests. Resets the dedup so warnings can be observed in isolation. */\nexport function _resetKafkajsWarnDedup(): void {\n warnedKafkajsKeys.clear();\n}\n\nasync function importKafkaJs(): Promise<{\n Kafka: new (cfg: unknown) => KjsKafka;\n Partitioners: KjsPartitionersNamespace;\n}> {\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (await import(\"kafkajs\")) as any;\n } catch {\n throw new Error(\n 'Driver \"kafkajs\" selected but the \"kafkajs\" package is not installed. Run: npm i kafkajs',\n );\n }\n}\n","import type { PublishErrorKind } from \"@eventferry/core\";\n\n/**\n * Classify a `@confluentinc/kafka-javascript` (librdkafka) producer error\n * into a {@link PublishErrorKind} so the core relay can decide whether to\n * retry, short-circuit to the DLQ, or pause polling.\n *\n * librdkafka exposes errors as numeric `RD_KAFKA_RESP_ERR_*` codes — negative\n * codes are library-internal (transport, queue-full, ssl), non-negative\n * codes are wire-protocol errors that match the Kafka protocol's error-code\n * registry. The confluent driver surfaces these on `err.code` (alongside\n * an `err.name` for the symbolic form).\n *\n * Unknown errors fall back to `\"retriable\"` — the safe bias.\n */\nexport function classifyConfluentError(err: unknown): PublishErrorKind {\n if (!err || typeof err !== \"object\") return \"retriable\";\n const e = err as { code?: number; name?: string };\n\n if (typeof e.code === \"number\") {\n const k = CODE_TO_KIND.get(e.code);\n if (k) return k;\n }\n\n if (typeof e.name === \"string\") {\n const k = NAME_TO_KIND.get(e.name);\n if (k) return k;\n }\n\n return \"retriable\";\n}\n\n/**\n * Authoritative mapping for the most-common librdkafka producer error codes.\n * Sources: `librdkafka/src/rdkafka.h` (`RD_KAFKA_RESP_ERR_*` enum) and the\n * Kafka Protocol error-code registry. Adding a code here is a one-line\n * change — start narrow, broaden as production exposes new codes.\n */\nconst CODE_TO_KIND: ReadonlyMap<number, PublishErrorKind> = new Map([\n // Library-internal (negative codes)\n [-184, \"backpressure\"], // ERR__QUEUE_FULL — our outbound buffer is full\n [-185, \"retriable\"], // ERR__TIMED_OUT\n [-187, \"retriable\"], // ERR__ALL_BROKERS_DOWN\n [-188, \"poison\"], // ERR__UNKNOWN_TOPIC — topic doesn't exist on broker\n [-190, \"poison\"], // ERR__UNKNOWN_PARTITION\n [-192, \"retriable\"], // ERR__MSG_TIMED_OUT\n [-195, \"retriable\"], // ERR__TRANSPORT\n [-198, \"poison\"], // ERR__BAD_COMPRESSION\n [-144, \"fatal\"], // ERR__FENCED — producer fenced by another with same txn id\n [-150, \"fatal\"], // ERR__FATAL — unrecoverable librdkafka error\n [-169, \"fatal\"], // ERR__AUTHENTICATION\n [-181, \"fatal\"], // ERR__SSL\n [-196, \"retriable\"], // ERR__FAIL — catch-all, safe-default to retriable\n\n // Wire-protocol (non-negative codes — Kafka error-code registry)\n [2, \"poison\"], // CORRUPT_MESSAGE\n [3, \"retriable\"], // UNKNOWN_TOPIC_OR_PARTITION\n [5, \"retriable\"], // LEADER_NOT_AVAILABLE\n [6, \"retriable\"], // NOT_LEADER_FOR_PARTITION\n [7, \"retriable\"], // REQUEST_TIMED_OUT\n [9, \"retriable\"], // REPLICA_NOT_AVAILABLE\n [10, \"poison\"], // MESSAGE_TOO_LARGE\n [13, \"retriable\"], // NETWORK_EXCEPTION\n [19, \"retriable\"], // NOT_ENOUGH_REPLICAS\n [29, \"fatal\"], // TOPIC_AUTHORIZATION_FAILED\n [31, \"fatal\"], // CLUSTER_AUTHORIZATION_FAILED\n [47, \"fatal\"], // INVALID_PRODUCER_EPOCH\n [58, \"fatal\"], // SASL_AUTHENTICATION_FAILED\n [74, \"retriable\"], // FENCED_LEADER_EPOCH\n [76, \"poison\"], // UNSUPPORTED_COMPRESSION_TYPE\n [87, \"poison\"], // INVALID_RECORD\n [89, \"quota\"], // THROTTLING_QUOTA_EXCEEDED\n]);\n\n/** Symbolic name fallback for clients that surface `err.name` only. */\nconst NAME_TO_KIND: ReadonlyMap<string, PublishErrorKind> = new Map([\n [\"ERR__QUEUE_FULL\", \"backpressure\"],\n [\"ERR__FENCED\", \"fatal\"],\n [\"ERR__FATAL\", \"fatal\"],\n [\"ERR__AUTHENTICATION\", \"fatal\"],\n [\"ERR__SSL\", \"fatal\"],\n [\"ERR__UNKNOWN_TOPIC\", \"poison\"],\n [\"ERR__UNKNOWN_PARTITION\", \"poison\"],\n [\"ERR__BAD_COMPRESSION\", \"poison\"],\n [\"ERR_TOPIC_AUTHORIZATION_FAILED\", \"fatal\"],\n [\"ERR_CLUSTER_AUTHORIZATION_FAILED\", \"fatal\"],\n [\"ERR_INVALID_PRODUCER_EPOCH\", \"fatal\"],\n [\"ERR_SASL_AUTHENTICATION_FAILED\", \"fatal\"],\n [\"ERR_CORRUPT_MESSAGE\", \"poison\"],\n [\"ERR_MSG_SIZE_TOO_LARGE\", \"poison\"],\n [\"ERR_INVALID_RECORD\", \"poison\"],\n [\"ERR_UNSUPPORTED_COMPRESSION_TYPE\", \"poison\"],\n [\"ERR_THROTTLING_QUOTA_EXCEEDED\", \"quota\"],\n]);\n","import type {\n KafkaConnectionConfig,\n ProducerBehaviorConfig,\n TlsConfig,\n} from \"./driver.js\";\n\n/**\n * Translate eventferry's normalized `KafkaConnectionConfig` into the shape\n * expected by `@confluentinc/kafka-javascript`'s `KafkaJS.Kafka` constructor.\n *\n * Returns an object with two parts:\n * - `kafkaJS`: the kafkajs-compatible config layer (clientId, brokers, and\n * simple ssl/sasl when no advanced TLS is needed).\n * - top-level keys: librdkafka-style config (e.g. `ssl.ca.pem`,\n * `security.protocol`) used when the user supplies a {@link TlsConfig}.\n *\n * Why a separate translator: the kafkajs-compat layer accepts the simple\n * `ssl: true` boolean but the verified path for mTLS (CA + cert + key) is\n * librdkafka's `ssl.*.pem` keys. The translator picks the right surface\n * based on what the caller supplied. Buffer inputs are coerced to strings —\n * librdkafka accepts PEM strings, NOT Buffers.\n */\nexport interface ConfluentClientConfig {\n kafkaJS: Record<string, unknown>;\n // librdkafka top-level keys; kept as a Record so we can spread them.\n librdkafka: Record<string, unknown>;\n}\n\nexport function buildConfluentClientConfig(\n opts: KafkaConnectionConfig & ProducerBehaviorConfig,\n): ConfluentClientConfig {\n const kafkaJS: Record<string, unknown> = {\n clientId: opts.clientId ?? \"eventferry\",\n brokers: opts.brokers,\n };\n const librdkafka: Record<string, unknown> = {};\n\n // ── Producer tuning passthrough (librdkafka config keys) ─────────────\n if (opts.lingerMs !== undefined) librdkafka[\"linger.ms\"] = opts.lingerMs;\n if (opts.batchSize !== undefined) librdkafka[\"batch.size\"] = opts.batchSize;\n if (opts.maxInFlightRequests !== undefined) {\n librdkafka[\"max.in.flight.requests.per.connection\"] =\n opts.maxInFlightRequests;\n }\n if (opts.requestTimeoutMs !== undefined) {\n librdkafka[\"request.timeout.ms\"] = opts.requestTimeoutMs;\n }\n if (opts.deliveryTimeoutMs !== undefined) {\n librdkafka[\"delivery.timeout.ms\"] = opts.deliveryTimeoutMs;\n }\n if (opts.maxRequestSize !== undefined) {\n librdkafka[\"message.max.bytes\"] = opts.maxRequestSize;\n }\n if (opts.transactionTimeoutMs !== undefined) {\n librdkafka[\"transaction.timeout.ms\"] = opts.transactionTimeoutMs;\n }\n\n const tlsRequested = opts.ssl === true || isTlsConfig(opts.ssl);\n const saslRequested = !!opts.sasl;\n\n if (saslRequested && tlsRequested) {\n librdkafka[\"security.protocol\"] = \"sasl_ssl\";\n } else if (tlsRequested) {\n librdkafka[\"security.protocol\"] = \"ssl\";\n } else if (saslRequested) {\n librdkafka[\"security.protocol\"] = \"sasl_plaintext\";\n } // else: leave as default (plaintext)\n\n if (isTlsConfig(opts.ssl)) {\n // Custom TLS — explicit librdkafka PEM keys. Buffers are coerced to\n // strings (librdkafka does not accept Buffer).\n const tls = opts.ssl;\n if (tls.ca !== undefined) {\n librdkafka[\"ssl.ca.pem\"] = stringifyPem(tls.ca);\n }\n if (tls.cert !== undefined) {\n librdkafka[\"ssl.certificate.pem\"] = stringifyPem(tls.cert);\n }\n if (tls.key !== undefined) {\n librdkafka[\"ssl.key.pem\"] = stringifyPem(tls.key);\n }\n if (tls.passphrase !== undefined) {\n librdkafka[\"ssl.key.password\"] = tls.passphrase;\n }\n // servername (SNI) — librdkafka derives SNI from `ssl.endpoint.identification.algorithm`;\n // explicit SNI override is not documented in the v1.x kafkaJS-compat surface, so we\n // honor it as a no-op for now and document the limitation in the gap analysis.\n } else if (opts.ssl === true) {\n // Simple TLS — kafkajs-compat boolean is sufficient.\n kafkaJS[\"ssl\"] = true;\n }\n\n if (opts.sasl) {\n // SASL — kafkajs-compat shape works for both password mechanisms and\n // OAUTHBEARER (the confluent client implements the provider callback).\n kafkaJS[\"sasl\"] = opts.sasl;\n }\n\n return { kafkaJS, librdkafka };\n}\n\nfunction isTlsConfig(v: unknown): v is TlsConfig {\n return typeof v === \"object\" && v !== null;\n}\n\nfunction stringifyPem(input: string | Buffer | Array<string | Buffer>): string {\n if (Array.isArray(input)) {\n return input.map((x) => (typeof x === \"string\" ? x : x.toString(\"utf8\"))).join(\"\\n\");\n }\n return typeof input === \"string\" ? input : input.toString(\"utf8\");\n}\n","import type { PublishableMessage, PublishResult } from \"@eventferry/core\";\nimport { classifyConfluentError } from \"./confluent-classifier.js\";\nimport { buildConfluentClientConfig } from \"./confluent-config.js\";\nimport type {\n KafkaConnectionConfig,\n KafkaDriver,\n ProducerBehaviorConfig,\n} from \"./driver.js\";\n\n// Structural typing of the confluent KafkaJS-compatible API surface so this\n// file compiles without the optional native dep installed.\ninterface CkProducer {\n connect(): Promise<void>;\n disconnect(): Promise<void>;\n send(args: unknown): Promise<unknown>;\n transaction(): Promise<CkTransaction>;\n}\ninterface CkTransaction {\n send(args: unknown): Promise<unknown>;\n commit(): Promise<void>;\n abort(): Promise<void>;\n}\ninterface CkKafka {\n producer(args?: unknown): CkProducer;\n}\n\nexport interface ConfluentDriverOptions\n extends KafkaConnectionConfig,\n ProducerBehaviorConfig {}\n\n/**\n * Driver backed by `@confluentinc/kafka-javascript` (librdkafka wrapper).\n * Higher throughput; uses the KafkaJS-compatible promisified API so the\n * adapter mirrors the kafkajs driver closely.\n */\nexport class ConfluentDriver implements KafkaDriver {\n readonly transactional: boolean;\n private producer: CkProducer | null = null;\n private readonly opts: ConfluentDriverOptions;\n\n constructor(opts: ConfluentDriverOptions) {\n this.opts = opts;\n this.transactional = opts.transactional ?? false;\n if (this.transactional && !opts.transactionalId) {\n throw new Error(\n \"ConfluentDriver: transactionalId is required when transactional=true\",\n );\n }\n }\n\n async connect(): Promise<void> {\n this.producer = await this.createProducer();\n await this.producer.connect();\n }\n\n /**\n * Construct the underlying confluent producer. Overridable as a test seam so\n * the send/transaction logic can be exercised without a real broker.\n */\n protected async createProducer(): Promise<CkProducer> {\n const mod = await importConfluent();\n const { kafkaJS, librdkafka } = buildConfluentClientConfig(this.opts);\n const kafka: CkKafka = new mod.KafkaJS.Kafka({\n kafkaJS,\n ...librdkafka,\n });\n return kafka.producer({\n kafkaJS: {\n idempotent: this.opts.idempotent ?? true,\n ...(this.transactional\n ? { transactionalId: this.opts.transactionalId }\n : {}),\n },\n });\n }\n\n async disconnect(): Promise<void> {\n await this.producer?.disconnect();\n this.producer = null;\n }\n\n async sendBatch(messages: PublishableMessage[]): Promise<PublishResult[]> {\n if (!this.producer) throw new Error(\"ConfluentDriver not connected\");\n const topicMessages = groupByTopic(messages);\n const acks = this.opts.acks ?? -1;\n const compression = this.opts.compression;\n\n const doSends = async (target: CkProducer | CkTransaction) => {\n for (const tm of topicMessages) {\n await target.send({\n topic: tm.topic,\n messages: tm.messages,\n acks,\n ...(compression && compression !== \"none\" ? { compression } : {}),\n });\n }\n };\n\n if (this.transactional) {\n const txn = await this.producer.transaction();\n try {\n await doSends(txn);\n await txn.commit();\n return messages.map((m) => ({ recordId: m.recordId, ok: true }));\n } catch (err) {\n await txn.abort().catch(() => undefined);\n return failedResults(messages, err);\n }\n }\n\n try {\n await doSends(this.producer);\n return messages.map((m) => ({ recordId: m.recordId, ok: true }));\n } catch (err) {\n return failedResults(messages, err);\n }\n }\n}\n\nfunction failedResults(\n messages: PublishableMessage[],\n err: unknown,\n): PublishResult[] {\n const error = err instanceof Error ? err : new Error(String(err));\n const errorKind = classifyConfluentError(err);\n return messages.map((m) => ({\n recordId: m.recordId,\n ok: false,\n error,\n errorKind,\n }));\n}\n\nfunction groupByTopic(messages: PublishableMessage[]) {\n const byTopic = new Map<string, unknown[]>();\n for (const m of messages) {\n const arr = byTopic.get(m.topic) ?? [];\n arr.push({\n key: m.key,\n value: m.value,\n headers: m.headers,\n // Per-message partition override. librdkafka honors an explicit\n // partition value; undefined leaves the default partitioner in charge.\n ...(m.partition !== undefined ? { partition: m.partition } : {}),\n });\n byTopic.set(m.topic, arr);\n }\n return [...byTopic.entries()].map(([topic, msgs]) => ({\n topic,\n messages: msgs,\n }));\n}\n\nasync function importConfluent(): Promise<{\n KafkaJS: { Kafka: new (cfg: unknown) => CkKafka };\n}> {\n try {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (await import(\"@confluentinc/kafka-javascript\")) as any;\n } catch {\n throw new Error(\n 'Driver \"confluent\" selected but \"@confluentinc/kafka-javascript\" is not installed. Run: npm i @confluentinc/kafka-javascript',\n );\n }\n}\n","import type {\n PublishableMessage,\n Publisher,\n PublishResult,\n} from \"@eventferry/core\";\nimport { ConfluentDriver } from \"./confluent-driver.js\";\nimport type {\n DriverKind,\n KafkaConnectionConfig,\n KafkaDriver,\n ProducerBehaviorConfig,\n} from \"./driver.js\";\nimport { KafkaJsDriver } from \"./kafkajs-driver.js\";\n\nexport interface KafkaPublisherOptions\n extends KafkaConnectionConfig,\n ProducerBehaviorConfig {\n /** Which underlying client to use. Default \"kafkajs\". */\n driver?: DriverKind;\n /**\n * Provide a fully custom driver instance instead of the built-ins.\n * Useful for testing or unsupported clients.\n */\n customDriver?: KafkaDriver;\n}\n\n/**\n * The Publisher the Relay talks to. Wraps a pluggable KafkaDriver and\n * adds dead-letter routing. Works against Kafka and Redpanda identically\n * (Redpanda is Kafka-API compatible).\n */\nexport class KafkaPublisher implements Publisher {\n private readonly driver: KafkaDriver;\n\n constructor(opts: KafkaPublisherOptions) {\n this.driver = opts.customDriver ?? selectDriver(opts);\n }\n\n connect(): Promise<void> {\n return this.driver.connect();\n }\n\n disconnect(): Promise<void> {\n return this.driver.disconnect();\n }\n\n publish(messages: PublishableMessage[]): Promise<PublishResult[]> {\n return this.driver.sendBatch(messages);\n }\n\n /**\n * Send a single dead-lettered message. The message already carries the\n * DLQ topic (the Relay rewrites it), plus the failure reason as a header.\n */\n async publishToDlq(message: PublishableMessage, error: Error): Promise<void> {\n const dlqMessage: PublishableMessage = {\n ...message,\n headers: {\n ...message.headers,\n \"dlq-reason\": error.message,\n \"dlq-original-topic\": message.headers[\"original-topic\"] ?? \"\",\n \"dlq-failed-at\": new Date().toISOString(),\n },\n };\n const [result] = await this.driver.sendBatch([dlqMessage]);\n if (result && !result.ok) {\n throw result.error ?? new Error(\"DLQ publish failed\");\n }\n }\n\n /** Whether the configured driver provides atomic (EOS) batch sends. */\n get transactional(): boolean {\n return this.driver.transactional;\n }\n}\n\nfunction selectDriver(opts: KafkaPublisherOptions): KafkaDriver {\n const kind = opts.driver ?? \"kafkajs\";\n switch (kind) {\n case \"kafkajs\":\n return new KafkaJsDriver(opts);\n case \"confluent\":\n return new ConfluentDriver(opts);\n default:\n throw new Error(`Unknown driver \"${kind}\"`);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACiBO,SAAS,qBAAqB,KAAgC;AACnE,MAAI,CAAC,OAAO,OAAO,QAAQ,SAAU,QAAO;AAC5C,QAAM,IAAI;AAGV,MAAI,EAAE,SAAS,yBAA0B,QAAO;AAChD,MAAI,EAAE,SAAS,6BAA8B,QAAO;AACpD,MAAI,EAAE,SAAS,2BAA4B,QAAO;AAMlD,QAAM,OAAO,OAAO,EAAE,SAAS,WAAW,EAAE,OAAO;AACnD,MAAI,MAAM;AACR,QAAI,gBAAgB,IAAI,IAA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AAK,OAAO;AAAA,EACrB;AACF;AAEA,SAAS,aAAa,MAA0C;AAC9D,QAAM,OAAO,KAAK,UAAU;AAC5B,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH,aAAO,IAAI,cAAc,IAAI;AAAA,IAC/B,KAAK;AACH,aAAO,IAAI,gBAAgB,IAAI;AAAA,IACjC;AACE,YAAM,IAAI,MAAM,mBAAmB,IAAI,GAAG;AAAA,EAC9C;AACF;","names":["CODE_TO_KIND","groupByTopic","failedResults"]}
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { PublishableMessage, PublishResult, Publisher } from '@eventferry/core';
1
+ import { PublishableMessage, PublishResult, PublishErrorKind, Publisher } from '@eventferry/core';
2
2
 
3
3
  /**
4
4
  * Low-level driver contract. Each concrete driver (kafkajs, confluent)
@@ -20,17 +20,108 @@ interface KafkaDriver {
20
20
  */
21
21
  readonly transactional: boolean;
22
22
  }
23
+ /**
24
+ * TLS configuration for client connections. Pass a full {@link TlsConfig}
25
+ * when the cluster requires CA pinning, mutual TLS (client cert + key), or a
26
+ * specific SNI host. Plain `ssl: true` keeps the previous behavior (one-way
27
+ * TLS using the driver's default trust store).
28
+ *
29
+ * `rejectUnauthorized` is intentionally NOT a knob here — TLS verification is
30
+ * non-negotiable. Dev clusters with self-signed certs pass their CA via `ca`.
31
+ */
32
+ interface TlsConfig {
33
+ /** PEM-encoded CA bundle. Buffers and strings both accepted. */
34
+ ca?: string | Buffer | Array<string | Buffer>;
35
+ /** PEM-encoded client certificate (required for mTLS). */
36
+ cert?: string | Buffer;
37
+ /** PEM-encoded private key for the client certificate (required for mTLS). */
38
+ key?: string | Buffer;
39
+ /** Passphrase for an encrypted private key. */
40
+ passphrase?: string;
41
+ /** SNI host. Useful when broker address doesn't match the cert SAN. */
42
+ servername?: string;
43
+ }
44
+ /**
45
+ * Username + password SASL: PLAIN and SCRAM-SHA-256/512. The conventional
46
+ * "API key + secret" shape used by Confluent Cloud, Aiven, on-prem SCRAM.
47
+ */
48
+ interface SaslPasswordConfig {
49
+ mechanism: "plain" | "scram-sha-256" | "scram-sha-512";
50
+ username: string;
51
+ password: string;
52
+ }
53
+ /**
54
+ * Token returned by an OAUTHBEARER provider.
55
+ *
56
+ * Driver asymmetry (verified against `kafkajs/types/index.d.ts` and
57
+ * `@confluentinc/kafka-javascript/types/kafkajs.d.ts`):
58
+ *
59
+ * - `kafkajs` reads only `value`. Other fields are silently ignored.
60
+ * - `@confluentinc/kafka-javascript` REQUIRES `value` + `principal` + `lifetime`
61
+ * and accepts an optional `extensions` map. Passing only `{ value }` throws.
62
+ *
63
+ * Cross-driver portable providers MUST populate all four. eventferry treats
64
+ * `principal` / `lifetime` / `extensions` as optional in the type to support
65
+ * kafkajs-only setups; supplying them is a no-op there.
66
+ */
67
+ interface OauthBearerToken {
68
+ /** The bearer token string (JWT, opaque, …). */
69
+ value: string;
70
+ /** Principal name. REQUIRED on the confluent driver. */
71
+ principal?: string;
72
+ /** Lifetime in MILLISECONDS. REQUIRED on the confluent driver. */
73
+ lifetime?: number;
74
+ /** SASL extensions to send alongside the token (e.g. for OIDC scopes). */
75
+ extensions?: Record<string, string>;
76
+ }
77
+ /**
78
+ * SASL/OAUTHBEARER: bring-your-own token provider. The function is invoked
79
+ * by the underlying client on demand (NOT on a fixed timer); cache the
80
+ * token in your provider if you want to amortise issuance cost.
81
+ *
82
+ * Required for Azure Event Hubs, Confluent Cloud with OAuth/SSO, and any
83
+ * OIDC-fronted Kafka. For AWS MSK IAM, wrap the AWS SigV4 signer in this
84
+ * callback.
85
+ */
86
+ interface SaslOauthbearerConfig {
87
+ mechanism: "oauthbearer";
88
+ oauthBearerProvider: () => Promise<OauthBearerToken>;
89
+ }
90
+ /**
91
+ * Discriminated union over the SASL mechanisms eventferry supports today.
92
+ * Add new mechanisms by extending this union and mapping them in each driver.
93
+ */
94
+ type SaslConfig = SaslPasswordConfig | SaslOauthbearerConfig;
23
95
  /** Shared connection config accepted by both drivers. */
24
96
  interface KafkaConnectionConfig {
25
97
  brokers: string[];
26
98
  clientId?: string;
27
- ssl?: boolean;
28
- sasl?: {
29
- mechanism: "plain" | "scram-sha-256" | "scram-sha-512";
30
- username: string;
31
- password: string;
32
- };
99
+ /**
100
+ * TLS configuration. `true` enables one-way TLS using the driver's default
101
+ * trust store; a {@link TlsConfig} object lets you supply a custom CA,
102
+ * client cert (for mTLS), and SNI host.
103
+ */
104
+ ssl?: boolean | TlsConfig;
105
+ sasl?: SaslConfig;
33
106
  }
107
+ /**
108
+ * Choice of partitioner. Only honored by the kafkajs driver — the confluent
109
+ * driver uses librdkafka's `consistent_random` (key-aware sticky) and
110
+ * partitioner override is out of scope for this release.
111
+ *
112
+ * - `"java-compatible"` (recommended for greenfield): kafkajs's
113
+ * `Partitioners.JavaCompatiblePartitioner`. Matches the Java client's
114
+ * murmur2-based hash so producers across language boundaries land on the
115
+ * same partition for the same key.
116
+ * - `"legacy"`: kafkajs's pre-v2 partitioner. Use when migrating an existing
117
+ * topic where hash continuity matters.
118
+ * - `"default"`: kafkajs's current default. Equivalent to legacy in v2 but
119
+ * may change with major kafkajs releases.
120
+ *
121
+ * Setting this also silences the noisy `KafkaJSPartitionerNotSpecified`
122
+ * warning kafkajs emits when no partitioner choice is made explicitly.
123
+ */
124
+ type KafkaJsPartitionerChoice = "default" | "legacy" | "java-compatible";
34
125
  interface ProducerBehaviorConfig {
35
126
  /** Enable idempotent producer (dedup + ordering). Default true. */
36
127
  idempotent?: boolean;
@@ -45,6 +136,46 @@ interface ProducerBehaviorConfig {
45
136
  acks?: number;
46
137
  /** Compression codec. Driver maps to its native enum. */
47
138
  compression?: "none" | "gzip" | "snappy" | "lz4" | "zstd";
139
+ /**
140
+ * (confluent only) How long the producer waits to accumulate records before
141
+ * flushing a partition batch. Default 0 (ship-immediately). Increase to
142
+ * 10–50ms for higher throughput at the cost of latency.
143
+ */
144
+ lingerMs?: number;
145
+ /** (confluent only) Maximum bytes per partition batch before forced flush. */
146
+ batchSize?: number;
147
+ /**
148
+ * Max concurrent unacknowledged producer requests. MUST be ≤5 when
149
+ * `idempotent: true`. Higher = throughput; lower = stricter ordering on
150
+ * non-idempotent producers (no other path preserves order on retry).
151
+ */
152
+ maxInFlightRequests?: number;
153
+ /** Per-request broker-ack timeout. Default 30 s. */
154
+ requestTimeoutMs?: number;
155
+ /**
156
+ * (confluent only) End-to-end timeout for a record from produce() call to
157
+ * terminal success / failure (includes retries). Defaults to 120 s.
158
+ * If this exceeds the relay's `claimTimeoutMs`, the reaper may double-
159
+ * publish a slow record — set both coherently.
160
+ */
161
+ deliveryTimeoutMs?: number;
162
+ /**
163
+ * (confluent only) Max bytes of a single record (after compression).
164
+ * MUST be ≤ broker's `message.max.bytes`. Defaults to 1 MB.
165
+ */
166
+ maxRequestSize?: number;
167
+ /**
168
+ * Broker-side ceiling on how long a transaction can stay open before
169
+ * auto-abort. Maps to `transaction.timeout.ms`. Default 60 s; capped by
170
+ * the broker's `transaction.max.timeout.ms`.
171
+ */
172
+ transactionTimeoutMs?: number;
173
+ /**
174
+ * (kafkajs only) Choice of partitioner. See
175
+ * {@link KafkaJsPartitionerChoice} for the options. Setting any value
176
+ * silences kafkajs's `KafkaJSPartitionerNotSpecified` warning.
177
+ */
178
+ partitioner?: KafkaJsPartitionerChoice;
48
179
  }
49
180
  type DriverKind = "kafkajs" | "confluent";
50
181
 
@@ -78,6 +209,25 @@ declare class KafkaJsDriver implements KafkaDriver {
78
209
  disconnect(): Promise<void>;
79
210
  sendBatch(messages: PublishableMessage[]): Promise<PublishResult[]>;
80
211
  }
212
+ /** Internal — used by tests. Resets the dedup so warnings can be observed in isolation. */
213
+ declare function _resetKafkajsWarnDedup(): void;
214
+
215
+ /**
216
+ * Classify a kafkajs producer error into a {@link PublishErrorKind} so the
217
+ * core relay can decide whether to retry, short-circuit to the DLQ, or pause
218
+ * polling.
219
+ *
220
+ * Mapping verified against `kafkajs/src/errors.js` (v2.x). Protocol error
221
+ * codes match the Kafka Protocol error-code registry. Library-specific
222
+ * subclasses (`KafkaJSRequestTimeoutError`, `KafkaJSConnectionError`,
223
+ * `KafkaJSNonRetriableError`) are matched by the `name` property kafkajs
224
+ * sets on its Error subclasses.
225
+ *
226
+ * Unknown errors fall back to `"retriable"` — the safe bias. At worst we
227
+ * retry an error that should have been skipped; in practice we'd rather
228
+ * over-retry than mis-classify a transient blip as terminal.
229
+ */
230
+ declare function classifyKafkajsError(err: unknown): PublishErrorKind;
81
231
 
82
232
  interface CkProducer {
83
233
  connect(): Promise<void>;
@@ -112,6 +262,43 @@ declare class ConfluentDriver implements KafkaDriver {
112
262
  sendBatch(messages: PublishableMessage[]): Promise<PublishResult[]>;
113
263
  }
114
264
 
265
+ /**
266
+ * Classify a `@confluentinc/kafka-javascript` (librdkafka) producer error
267
+ * into a {@link PublishErrorKind} so the core relay can decide whether to
268
+ * retry, short-circuit to the DLQ, or pause polling.
269
+ *
270
+ * librdkafka exposes errors as numeric `RD_KAFKA_RESP_ERR_*` codes — negative
271
+ * codes are library-internal (transport, queue-full, ssl), non-negative
272
+ * codes are wire-protocol errors that match the Kafka protocol's error-code
273
+ * registry. The confluent driver surfaces these on `err.code` (alongside
274
+ * an `err.name` for the symbolic form).
275
+ *
276
+ * Unknown errors fall back to `"retriable"` — the safe bias.
277
+ */
278
+ declare function classifyConfluentError(err: unknown): PublishErrorKind;
279
+
280
+ /**
281
+ * Translate eventferry's normalized `KafkaConnectionConfig` into the shape
282
+ * expected by `@confluentinc/kafka-javascript`'s `KafkaJS.Kafka` constructor.
283
+ *
284
+ * Returns an object with two parts:
285
+ * - `kafkaJS`: the kafkajs-compatible config layer (clientId, brokers, and
286
+ * simple ssl/sasl when no advanced TLS is needed).
287
+ * - top-level keys: librdkafka-style config (e.g. `ssl.ca.pem`,
288
+ * `security.protocol`) used when the user supplies a {@link TlsConfig}.
289
+ *
290
+ * Why a separate translator: the kafkajs-compat layer accepts the simple
291
+ * `ssl: true` boolean but the verified path for mTLS (CA + cert + key) is
292
+ * librdkafka's `ssl.*.pem` keys. The translator picks the right surface
293
+ * based on what the caller supplied. Buffer inputs are coerced to strings —
294
+ * librdkafka accepts PEM strings, NOT Buffers.
295
+ */
296
+ interface ConfluentClientConfig {
297
+ kafkaJS: Record<string, unknown>;
298
+ librdkafka: Record<string, unknown>;
299
+ }
300
+ declare function buildConfluentClientConfig(opts: KafkaConnectionConfig & ProducerBehaviorConfig): ConfluentClientConfig;
301
+
115
302
  interface KafkaPublisherOptions extends KafkaConnectionConfig, ProducerBehaviorConfig {
116
303
  /** Which underlying client to use. Default "kafkajs". */
117
304
  driver?: DriverKind;
@@ -141,4 +328,4 @@ declare class KafkaPublisher implements Publisher {
141
328
  get transactional(): boolean;
142
329
  }
143
330
 
144
- export { ConfluentDriver, type ConfluentDriverOptions, type DriverKind, type KafkaConnectionConfig, type KafkaDriver, KafkaJsDriver, type KafkaJsDriverOptions, KafkaPublisher, type KafkaPublisherOptions, type ProducerBehaviorConfig };
331
+ export { type ConfluentClientConfig, ConfluentDriver, type ConfluentDriverOptions, type DriverKind, type KafkaConnectionConfig, type KafkaDriver, KafkaJsDriver, type KafkaJsDriverOptions, type KafkaJsPartitionerChoice, KafkaPublisher, type KafkaPublisherOptions, type OauthBearerToken, type ProducerBehaviorConfig, type SaslConfig, type SaslOauthbearerConfig, type SaslPasswordConfig, type TlsConfig, _resetKafkajsWarnDedup, buildConfluentClientConfig, classifyConfluentError, classifyKafkajsError };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PublishableMessage, PublishResult, Publisher } from '@eventferry/core';
1
+ import { PublishableMessage, PublishResult, PublishErrorKind, Publisher } from '@eventferry/core';
2
2
 
3
3
  /**
4
4
  * Low-level driver contract. Each concrete driver (kafkajs, confluent)
@@ -20,17 +20,108 @@ interface KafkaDriver {
20
20
  */
21
21
  readonly transactional: boolean;
22
22
  }
23
+ /**
24
+ * TLS configuration for client connections. Pass a full {@link TlsConfig}
25
+ * when the cluster requires CA pinning, mutual TLS (client cert + key), or a
26
+ * specific SNI host. Plain `ssl: true` keeps the previous behavior (one-way
27
+ * TLS using the driver's default trust store).
28
+ *
29
+ * `rejectUnauthorized` is intentionally NOT a knob here — TLS verification is
30
+ * non-negotiable. Dev clusters with self-signed certs pass their CA via `ca`.
31
+ */
32
+ interface TlsConfig {
33
+ /** PEM-encoded CA bundle. Buffers and strings both accepted. */
34
+ ca?: string | Buffer | Array<string | Buffer>;
35
+ /** PEM-encoded client certificate (required for mTLS). */
36
+ cert?: string | Buffer;
37
+ /** PEM-encoded private key for the client certificate (required for mTLS). */
38
+ key?: string | Buffer;
39
+ /** Passphrase for an encrypted private key. */
40
+ passphrase?: string;
41
+ /** SNI host. Useful when broker address doesn't match the cert SAN. */
42
+ servername?: string;
43
+ }
44
+ /**
45
+ * Username + password SASL: PLAIN and SCRAM-SHA-256/512. The conventional
46
+ * "API key + secret" shape used by Confluent Cloud, Aiven, on-prem SCRAM.
47
+ */
48
+ interface SaslPasswordConfig {
49
+ mechanism: "plain" | "scram-sha-256" | "scram-sha-512";
50
+ username: string;
51
+ password: string;
52
+ }
53
+ /**
54
+ * Token returned by an OAUTHBEARER provider.
55
+ *
56
+ * Driver asymmetry (verified against `kafkajs/types/index.d.ts` and
57
+ * `@confluentinc/kafka-javascript/types/kafkajs.d.ts`):
58
+ *
59
+ * - `kafkajs` reads only `value`. Other fields are silently ignored.
60
+ * - `@confluentinc/kafka-javascript` REQUIRES `value` + `principal` + `lifetime`
61
+ * and accepts an optional `extensions` map. Passing only `{ value }` throws.
62
+ *
63
+ * Cross-driver portable providers MUST populate all four. eventferry treats
64
+ * `principal` / `lifetime` / `extensions` as optional in the type to support
65
+ * kafkajs-only setups; supplying them is a no-op there.
66
+ */
67
+ interface OauthBearerToken {
68
+ /** The bearer token string (JWT, opaque, …). */
69
+ value: string;
70
+ /** Principal name. REQUIRED on the confluent driver. */
71
+ principal?: string;
72
+ /** Lifetime in MILLISECONDS. REQUIRED on the confluent driver. */
73
+ lifetime?: number;
74
+ /** SASL extensions to send alongside the token (e.g. for OIDC scopes). */
75
+ extensions?: Record<string, string>;
76
+ }
77
+ /**
78
+ * SASL/OAUTHBEARER: bring-your-own token provider. The function is invoked
79
+ * by the underlying client on demand (NOT on a fixed timer); cache the
80
+ * token in your provider if you want to amortise issuance cost.
81
+ *
82
+ * Required for Azure Event Hubs, Confluent Cloud with OAuth/SSO, and any
83
+ * OIDC-fronted Kafka. For AWS MSK IAM, wrap the AWS SigV4 signer in this
84
+ * callback.
85
+ */
86
+ interface SaslOauthbearerConfig {
87
+ mechanism: "oauthbearer";
88
+ oauthBearerProvider: () => Promise<OauthBearerToken>;
89
+ }
90
+ /**
91
+ * Discriminated union over the SASL mechanisms eventferry supports today.
92
+ * Add new mechanisms by extending this union and mapping them in each driver.
93
+ */
94
+ type SaslConfig = SaslPasswordConfig | SaslOauthbearerConfig;
23
95
  /** Shared connection config accepted by both drivers. */
24
96
  interface KafkaConnectionConfig {
25
97
  brokers: string[];
26
98
  clientId?: string;
27
- ssl?: boolean;
28
- sasl?: {
29
- mechanism: "plain" | "scram-sha-256" | "scram-sha-512";
30
- username: string;
31
- password: string;
32
- };
99
+ /**
100
+ * TLS configuration. `true` enables one-way TLS using the driver's default
101
+ * trust store; a {@link TlsConfig} object lets you supply a custom CA,
102
+ * client cert (for mTLS), and SNI host.
103
+ */
104
+ ssl?: boolean | TlsConfig;
105
+ sasl?: SaslConfig;
33
106
  }
107
+ /**
108
+ * Choice of partitioner. Only honored by the kafkajs driver — the confluent
109
+ * driver uses librdkafka's `consistent_random` (key-aware sticky) and
110
+ * partitioner override is out of scope for this release.
111
+ *
112
+ * - `"java-compatible"` (recommended for greenfield): kafkajs's
113
+ * `Partitioners.JavaCompatiblePartitioner`. Matches the Java client's
114
+ * murmur2-based hash so producers across language boundaries land on the
115
+ * same partition for the same key.
116
+ * - `"legacy"`: kafkajs's pre-v2 partitioner. Use when migrating an existing
117
+ * topic where hash continuity matters.
118
+ * - `"default"`: kafkajs's current default. Equivalent to legacy in v2 but
119
+ * may change with major kafkajs releases.
120
+ *
121
+ * Setting this also silences the noisy `KafkaJSPartitionerNotSpecified`
122
+ * warning kafkajs emits when no partitioner choice is made explicitly.
123
+ */
124
+ type KafkaJsPartitionerChoice = "default" | "legacy" | "java-compatible";
34
125
  interface ProducerBehaviorConfig {
35
126
  /** Enable idempotent producer (dedup + ordering). Default true. */
36
127
  idempotent?: boolean;
@@ -45,6 +136,46 @@ interface ProducerBehaviorConfig {
45
136
  acks?: number;
46
137
  /** Compression codec. Driver maps to its native enum. */
47
138
  compression?: "none" | "gzip" | "snappy" | "lz4" | "zstd";
139
+ /**
140
+ * (confluent only) How long the producer waits to accumulate records before
141
+ * flushing a partition batch. Default 0 (ship-immediately). Increase to
142
+ * 10–50ms for higher throughput at the cost of latency.
143
+ */
144
+ lingerMs?: number;
145
+ /** (confluent only) Maximum bytes per partition batch before forced flush. */
146
+ batchSize?: number;
147
+ /**
148
+ * Max concurrent unacknowledged producer requests. MUST be ≤5 when
149
+ * `idempotent: true`. Higher = throughput; lower = stricter ordering on
150
+ * non-idempotent producers (no other path preserves order on retry).
151
+ */
152
+ maxInFlightRequests?: number;
153
+ /** Per-request broker-ack timeout. Default 30 s. */
154
+ requestTimeoutMs?: number;
155
+ /**
156
+ * (confluent only) End-to-end timeout for a record from produce() call to
157
+ * terminal success / failure (includes retries). Defaults to 120 s.
158
+ * If this exceeds the relay's `claimTimeoutMs`, the reaper may double-
159
+ * publish a slow record — set both coherently.
160
+ */
161
+ deliveryTimeoutMs?: number;
162
+ /**
163
+ * (confluent only) Max bytes of a single record (after compression).
164
+ * MUST be ≤ broker's `message.max.bytes`. Defaults to 1 MB.
165
+ */
166
+ maxRequestSize?: number;
167
+ /**
168
+ * Broker-side ceiling on how long a transaction can stay open before
169
+ * auto-abort. Maps to `transaction.timeout.ms`. Default 60 s; capped by
170
+ * the broker's `transaction.max.timeout.ms`.
171
+ */
172
+ transactionTimeoutMs?: number;
173
+ /**
174
+ * (kafkajs only) Choice of partitioner. See
175
+ * {@link KafkaJsPartitionerChoice} for the options. Setting any value
176
+ * silences kafkajs's `KafkaJSPartitionerNotSpecified` warning.
177
+ */
178
+ partitioner?: KafkaJsPartitionerChoice;
48
179
  }
49
180
  type DriverKind = "kafkajs" | "confluent";
50
181
 
@@ -78,6 +209,25 @@ declare class KafkaJsDriver implements KafkaDriver {
78
209
  disconnect(): Promise<void>;
79
210
  sendBatch(messages: PublishableMessage[]): Promise<PublishResult[]>;
80
211
  }
212
+ /** Internal — used by tests. Resets the dedup so warnings can be observed in isolation. */
213
+ declare function _resetKafkajsWarnDedup(): void;
214
+
215
+ /**
216
+ * Classify a kafkajs producer error into a {@link PublishErrorKind} so the
217
+ * core relay can decide whether to retry, short-circuit to the DLQ, or pause
218
+ * polling.
219
+ *
220
+ * Mapping verified against `kafkajs/src/errors.js` (v2.x). Protocol error
221
+ * codes match the Kafka Protocol error-code registry. Library-specific
222
+ * subclasses (`KafkaJSRequestTimeoutError`, `KafkaJSConnectionError`,
223
+ * `KafkaJSNonRetriableError`) are matched by the `name` property kafkajs
224
+ * sets on its Error subclasses.
225
+ *
226
+ * Unknown errors fall back to `"retriable"` — the safe bias. At worst we
227
+ * retry an error that should have been skipped; in practice we'd rather
228
+ * over-retry than mis-classify a transient blip as terminal.
229
+ */
230
+ declare function classifyKafkajsError(err: unknown): PublishErrorKind;
81
231
 
82
232
  interface CkProducer {
83
233
  connect(): Promise<void>;
@@ -112,6 +262,43 @@ declare class ConfluentDriver implements KafkaDriver {
112
262
  sendBatch(messages: PublishableMessage[]): Promise<PublishResult[]>;
113
263
  }
114
264
 
265
+ /**
266
+ * Classify a `@confluentinc/kafka-javascript` (librdkafka) producer error
267
+ * into a {@link PublishErrorKind} so the core relay can decide whether to
268
+ * retry, short-circuit to the DLQ, or pause polling.
269
+ *
270
+ * librdkafka exposes errors as numeric `RD_KAFKA_RESP_ERR_*` codes — negative
271
+ * codes are library-internal (transport, queue-full, ssl), non-negative
272
+ * codes are wire-protocol errors that match the Kafka protocol's error-code
273
+ * registry. The confluent driver surfaces these on `err.code` (alongside
274
+ * an `err.name` for the symbolic form).
275
+ *
276
+ * Unknown errors fall back to `"retriable"` — the safe bias.
277
+ */
278
+ declare function classifyConfluentError(err: unknown): PublishErrorKind;
279
+
280
+ /**
281
+ * Translate eventferry's normalized `KafkaConnectionConfig` into the shape
282
+ * expected by `@confluentinc/kafka-javascript`'s `KafkaJS.Kafka` constructor.
283
+ *
284
+ * Returns an object with two parts:
285
+ * - `kafkaJS`: the kafkajs-compatible config layer (clientId, brokers, and
286
+ * simple ssl/sasl when no advanced TLS is needed).
287
+ * - top-level keys: librdkafka-style config (e.g. `ssl.ca.pem`,
288
+ * `security.protocol`) used when the user supplies a {@link TlsConfig}.
289
+ *
290
+ * Why a separate translator: the kafkajs-compat layer accepts the simple
291
+ * `ssl: true` boolean but the verified path for mTLS (CA + cert + key) is
292
+ * librdkafka's `ssl.*.pem` keys. The translator picks the right surface
293
+ * based on what the caller supplied. Buffer inputs are coerced to strings —
294
+ * librdkafka accepts PEM strings, NOT Buffers.
295
+ */
296
+ interface ConfluentClientConfig {
297
+ kafkaJS: Record<string, unknown>;
298
+ librdkafka: Record<string, unknown>;
299
+ }
300
+ declare function buildConfluentClientConfig(opts: KafkaConnectionConfig & ProducerBehaviorConfig): ConfluentClientConfig;
301
+
115
302
  interface KafkaPublisherOptions extends KafkaConnectionConfig, ProducerBehaviorConfig {
116
303
  /** Which underlying client to use. Default "kafkajs". */
117
304
  driver?: DriverKind;
@@ -141,4 +328,4 @@ declare class KafkaPublisher implements Publisher {
141
328
  get transactional(): boolean;
142
329
  }
143
330
 
144
- export { ConfluentDriver, type ConfluentDriverOptions, type DriverKind, type KafkaConnectionConfig, type KafkaDriver, KafkaJsDriver, type KafkaJsDriverOptions, KafkaPublisher, type KafkaPublisherOptions, type ProducerBehaviorConfig };
331
+ export { type ConfluentClientConfig, ConfluentDriver, type ConfluentDriverOptions, type DriverKind, type KafkaConnectionConfig, type KafkaDriver, KafkaJsDriver, type KafkaJsDriverOptions, type KafkaJsPartitionerChoice, KafkaPublisher, type KafkaPublisherOptions, type OauthBearerToken, type ProducerBehaviorConfig, type SaslConfig, type SaslOauthbearerConfig, type SaslPasswordConfig, type TlsConfig, _resetKafkajsWarnDedup, buildConfluentClientConfig, classifyConfluentError, classifyKafkajsError };