@drarzter/kafka-client 0.9.3 → 0.10.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.
Files changed (170) hide show
  1. package/README.md +625 -8
  2. package/dist/chunk-CMO7SMVK.mjs +4814 -0
  3. package/dist/chunk-CMO7SMVK.mjs.map +1 -0
  4. package/dist/cli/dlq.d.ts +119 -0
  5. package/dist/cli/dlq.d.ts.map +1 -0
  6. package/dist/cli/index.d.ts +3 -0
  7. package/dist/cli/index.d.ts.map +1 -0
  8. package/dist/{chunk-TPIP5VV7.mjs → cli/index.js} +965 -265
  9. package/dist/cli/index.js.map +1 -0
  10. package/dist/cli/index.mjs +355 -0
  11. package/dist/cli/index.mjs.map +1 -0
  12. package/dist/client/config/from-env.d.ts +188 -0
  13. package/dist/client/config/from-env.d.ts.map +1 -0
  14. package/dist/client/config/index.d.ts +2 -0
  15. package/dist/client/config/index.d.ts.map +1 -0
  16. package/dist/client/errors.d.ts +67 -0
  17. package/dist/client/errors.d.ts.map +1 -0
  18. package/dist/client/kafka.client/admin/ops.d.ts +114 -0
  19. package/dist/client/kafka.client/admin/ops.d.ts.map +1 -0
  20. package/dist/client/kafka.client/consumer/features/delayed.d.ts +24 -0
  21. package/dist/client/kafka.client/consumer/features/delayed.d.ts.map +1 -0
  22. package/dist/client/kafka.client/consumer/features/dlq-replay.d.ts +52 -0
  23. package/dist/client/kafka.client/consumer/features/dlq-replay.d.ts.map +1 -0
  24. package/dist/client/kafka.client/consumer/features/routed.d.ts +4 -0
  25. package/dist/client/kafka.client/consumer/features/routed.d.ts.map +1 -0
  26. package/dist/client/kafka.client/consumer/features/snapshot.d.ts +10 -0
  27. package/dist/client/kafka.client/consumer/features/snapshot.d.ts.map +1 -0
  28. package/dist/client/kafka.client/consumer/features/window.d.ts +5 -0
  29. package/dist/client/kafka.client/consumer/features/window.d.ts.map +1 -0
  30. package/dist/client/kafka.client/consumer/handler.d.ts +149 -0
  31. package/dist/client/kafka.client/consumer/handler.d.ts.map +1 -0
  32. package/dist/client/kafka.client/consumer/ops.d.ts +51 -0
  33. package/dist/client/kafka.client/consumer/ops.d.ts.map +1 -0
  34. package/dist/client/kafka.client/consumer/pipeline.d.ts +167 -0
  35. package/dist/client/kafka.client/consumer/pipeline.d.ts.map +1 -0
  36. package/dist/client/kafka.client/consumer/queue.d.ts +37 -0
  37. package/dist/client/kafka.client/consumer/queue.d.ts.map +1 -0
  38. package/dist/client/kafka.client/consumer/retry-topic.d.ts +65 -0
  39. package/dist/client/kafka.client/consumer/retry-topic.d.ts.map +1 -0
  40. package/dist/client/kafka.client/consumer/setup.d.ts +63 -0
  41. package/dist/client/kafka.client/consumer/setup.d.ts.map +1 -0
  42. package/dist/client/kafka.client/consumer/start.d.ts +7 -0
  43. package/dist/client/kafka.client/consumer/start.d.ts.map +1 -0
  44. package/dist/client/kafka.client/consumer/stop.d.ts +19 -0
  45. package/dist/client/kafka.client/consumer/stop.d.ts.map +1 -0
  46. package/dist/client/kafka.client/consumer/subscribe-retry.d.ts +4 -0
  47. package/dist/client/kafka.client/consumer/subscribe-retry.d.ts.map +1 -0
  48. package/dist/client/kafka.client/context.d.ts +72 -0
  49. package/dist/client/kafka.client/context.d.ts.map +1 -0
  50. package/dist/client/kafka.client/index.d.ts +155 -0
  51. package/dist/client/kafka.client/index.d.ts.map +1 -0
  52. package/dist/client/kafka.client/infra/circuit-breaker.manager.d.ts +61 -0
  53. package/dist/client/kafka.client/infra/circuit-breaker.manager.d.ts.map +1 -0
  54. package/dist/client/kafka.client/infra/dedup.store.d.ts +28 -0
  55. package/dist/client/kafka.client/infra/dedup.store.d.ts.map +1 -0
  56. package/dist/client/kafka.client/infra/inflight.tracker.d.ts +22 -0
  57. package/dist/client/kafka.client/infra/inflight.tracker.d.ts.map +1 -0
  58. package/dist/client/kafka.client/infra/metrics.manager.d.ts +67 -0
  59. package/dist/client/kafka.client/infra/metrics.manager.d.ts.map +1 -0
  60. package/dist/client/kafka.client/producer/lifecycle.d.ts +41 -0
  61. package/dist/client/kafka.client/producer/lifecycle.d.ts.map +1 -0
  62. package/dist/client/kafka.client/producer/ops.d.ts +70 -0
  63. package/dist/client/kafka.client/producer/ops.d.ts.map +1 -0
  64. package/dist/client/kafka.client/producer/send.d.ts +21 -0
  65. package/dist/client/kafka.client/producer/send.d.ts.map +1 -0
  66. package/dist/client/kafka.client/validate-options.d.ts +11 -0
  67. package/dist/client/kafka.client/validate-options.d.ts.map +1 -0
  68. package/dist/client/message/envelope.d.ts +105 -0
  69. package/dist/client/message/envelope.d.ts.map +1 -0
  70. package/dist/client/message/schema-registry.d.ts +105 -0
  71. package/dist/client/message/schema-registry.d.ts.map +1 -0
  72. package/dist/client/message/topic.d.ts +138 -0
  73. package/dist/client/message/topic.d.ts.map +1 -0
  74. package/dist/client/message/versioned-schema.d.ts +53 -0
  75. package/dist/client/message/versioned-schema.d.ts.map +1 -0
  76. package/dist/client/outbox/index.d.ts +4 -0
  77. package/dist/client/outbox/index.d.ts.map +1 -0
  78. package/dist/client/outbox/outbox.relay.d.ts +90 -0
  79. package/dist/client/outbox/outbox.relay.d.ts.map +1 -0
  80. package/dist/client/outbox/outbox.store.d.ts +42 -0
  81. package/dist/client/outbox/outbox.store.d.ts.map +1 -0
  82. package/dist/client/outbox/outbox.types.d.ts +144 -0
  83. package/dist/client/outbox/outbox.types.d.ts.map +1 -0
  84. package/dist/client/security/acl.d.ts +108 -0
  85. package/dist/client/security/acl.d.ts.map +1 -0
  86. package/dist/client/security/index.d.ts +5 -0
  87. package/dist/client/security/index.d.ts.map +1 -0
  88. package/dist/client/security/providers.d.ts +88 -0
  89. package/dist/client/security/providers.d.ts.map +1 -0
  90. package/dist/client/security/resolve-security.d.ts +19 -0
  91. package/dist/client/security/resolve-security.d.ts.map +1 -0
  92. package/dist/client/security/security.types.d.ts +76 -0
  93. package/dist/client/security/security.types.d.ts.map +1 -0
  94. package/dist/client/transport/confluent.transport.d.ts +32 -0
  95. package/dist/client/transport/confluent.transport.d.ts.map +1 -0
  96. package/dist/client/transport/transport.interface.d.ts +216 -0
  97. package/dist/client/transport/transport.interface.d.ts.map +1 -0
  98. package/dist/client/types/admin.interface.d.ts +174 -0
  99. package/dist/client/types/admin.interface.d.ts.map +1 -0
  100. package/dist/client/types/admin.types.d.ts +140 -0
  101. package/dist/client/types/admin.types.d.ts.map +1 -0
  102. package/dist/client/types/client.d.ts +21 -0
  103. package/dist/client/types/client.d.ts.map +1 -0
  104. package/dist/client/types/common.d.ts +84 -0
  105. package/dist/client/types/common.d.ts.map +1 -0
  106. package/dist/client/types/config.types.d.ts +150 -0
  107. package/dist/client/types/config.types.d.ts.map +1 -0
  108. package/dist/client/types/consumer.interface.d.ts +115 -0
  109. package/dist/client/types/consumer.interface.d.ts.map +1 -0
  110. package/dist/{consumer.types-fFCag3VJ.d.mts → client/types/consumer.types.d.ts} +62 -383
  111. package/dist/client/types/consumer.types.d.ts.map +1 -0
  112. package/dist/client/types/dedup.types.d.ts +50 -0
  113. package/dist/client/types/dedup.types.d.ts.map +1 -0
  114. package/dist/client/types/lifecycle.interface.d.ts +72 -0
  115. package/dist/client/types/lifecycle.interface.d.ts.map +1 -0
  116. package/dist/client/types/producer.interface.d.ts +52 -0
  117. package/dist/client/types/producer.interface.d.ts.map +1 -0
  118. package/dist/client/types/producer.types.d.ts +90 -0
  119. package/dist/client/types/producer.types.d.ts.map +1 -0
  120. package/dist/client/types.d.ts +8 -0
  121. package/dist/client/types.d.ts.map +1 -0
  122. package/dist/core.d.ts +10 -314
  123. package/dist/core.d.ts.map +1 -0
  124. package/dist/core.js +1326 -74
  125. package/dist/core.js.map +1 -1
  126. package/dist/core.mjs +39 -3
  127. package/dist/index.d.ts +7 -128
  128. package/dist/index.d.ts.map +1 -0
  129. package/dist/index.js +1343 -74
  130. package/dist/index.js.map +1 -1
  131. package/dist/index.mjs +56 -3
  132. package/dist/index.mjs.map +1 -1
  133. package/dist/nest/kafka.constants.d.ts +5 -0
  134. package/dist/nest/kafka.constants.d.ts.map +1 -0
  135. package/dist/nest/kafka.decorator.d.ts +49 -0
  136. package/dist/nest/kafka.decorator.d.ts.map +1 -0
  137. package/dist/nest/kafka.explorer.d.ts +17 -0
  138. package/dist/nest/kafka.explorer.d.ts.map +1 -0
  139. package/dist/nest/kafka.health.d.ts +7 -0
  140. package/dist/nest/kafka.health.d.ts.map +1 -0
  141. package/dist/nest/kafka.module.d.ts +61 -0
  142. package/dist/nest/kafka.module.d.ts.map +1 -0
  143. package/dist/otel.d.ts +83 -5
  144. package/dist/otel.d.ts.map +1 -0
  145. package/dist/otel.js +100 -6
  146. package/dist/otel.js.map +1 -1
  147. package/dist/otel.mjs +98 -5
  148. package/dist/otel.mjs.map +1 -1
  149. package/dist/testing/client.mock.d.ts +47 -0
  150. package/dist/testing/client.mock.d.ts.map +1 -0
  151. package/dist/testing/index.d.ts +4 -0
  152. package/dist/testing/index.d.ts.map +1 -0
  153. package/dist/testing/test.container.d.ts +63 -0
  154. package/dist/testing/test.container.d.ts.map +1 -0
  155. package/dist/{testing.d.mts → testing/transport.fake.d.ts} +7 -111
  156. package/dist/testing/transport.fake.d.ts.map +1 -0
  157. package/dist/testing.d.ts +2 -318
  158. package/dist/testing.d.ts.map +1 -0
  159. package/dist/testing.js +28 -2
  160. package/dist/testing.js.map +1 -1
  161. package/dist/testing.mjs +28 -2
  162. package/dist/testing.mjs.map +1 -1
  163. package/package.json +22 -9
  164. package/dist/chunk-TPIP5VV7.mjs.map +0 -1
  165. package/dist/client-CBBUDDtu.d.ts +0 -751
  166. package/dist/client-D-SxYV2b.d.mts +0 -751
  167. package/dist/consumer.types-fFCag3VJ.d.ts +0 -958
  168. package/dist/core.d.mts +0 -314
  169. package/dist/index.d.mts +0 -128
  170. package/dist/otel.d.mts +0 -27
@@ -0,0 +1,72 @@
1
+ import type { ClientId, KafkaMetrics } from "./common";
2
+ /** Lifecycle and observability methods of `IKafkaClient`. */
3
+ export interface IKafkaLifecycle {
4
+ /**
5
+ * @example
6
+ * ```ts
7
+ * const id = kafka.getClientId(); // e.g. 'my-service'
8
+ * ```
9
+ */
10
+ getClientId(): ClientId;
11
+ /**
12
+ * Return a snapshot of internal event counters (retry / DLQ / dedup).
13
+ * - `getMetrics()` — aggregate across all topics.
14
+ * - `getMetrics(topic)` — counters for a specific topic only; returns all-zero
15
+ * if no events have been observed for that topic yet.
16
+ *
17
+ * Counters accumulate since client creation or the last `resetMetrics()` call.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * const { processedCount, dlqCount, retryCount } = kafka.getMetrics();
22
+ * console.log(`Processed: ${processedCount}, DLQ: ${dlqCount}`);
23
+ * ```
24
+ */
25
+ getMetrics(topic?: string): Readonly<KafkaMetrics>;
26
+ /**
27
+ * Reset internal event counters to zero.
28
+ * - `resetMetrics()` — reset all topics.
29
+ * - `resetMetrics(topic)` — reset a single topic only.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * kafka.resetMetrics();
34
+ * kafka.resetMetrics('orders.created');
35
+ * ```
36
+ */
37
+ resetMetrics(topic?: string): void;
38
+ /**
39
+ * Connect the producer, recover the Lamport clock (when `clockRecovery` is
40
+ * configured), and start the lag-throttle poller (when `lagThrottle` is set).
41
+ * Must be called before any send. NestJS apps call this automatically inside
42
+ * `KafkaModule.register()` / `registerAsync()`.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * await kafka.connectProducer();
47
+ * ```
48
+ */
49
+ connectProducer(): Promise<void>;
50
+ /**
51
+ * Drain in-flight handlers, then disconnect all producers, consumers, and admin.
52
+ * @param drainTimeoutMs Max ms to wait for in-flight handlers (default 30 000).
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * await kafka.disconnect();
57
+ * ```
58
+ */
59
+ disconnect(drainTimeoutMs?: number): Promise<void>;
60
+ /**
61
+ * Register SIGTERM / SIGINT signal handlers that drain in-flight messages before
62
+ * disconnecting. Call once after constructing the client in non-NestJS apps.
63
+ * NestJS apps get drain automatically via `onModuleDestroy` → `disconnect()`.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * kafka.enableGracefulShutdown(['SIGTERM', 'SIGINT'], 30_000);
68
+ * ```
69
+ */
70
+ enableGracefulShutdown(signals?: NodeJS.Signals[], drainTimeoutMs?: number): void;
71
+ }
72
+ //# sourceMappingURL=lifecycle.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycle.interface.d.ts","sourceRoot":"","sources":["../../../src/client/types/lifecycle.interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAEvD,6DAA6D;AAC7D,MAAM,WAAW,eAAe;IAC9B;;;;;OAKG;IACH,WAAW,IAAI,QAAQ,CAAC;IAExB;;;;;;;;;;;;;OAaG;IACH,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAC;IAEnD;;;;;;;;;;OAUG;IACH,YAAY,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAEnC;;;;;;;;;;OAUG;IACH,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjC;;;;;;;;OAQG;IACH,UAAU,CAAC,cAAc,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnD;;;;;;;;;OASG;IACH,sBAAsB,CACpB,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,EAAE,EAC1B,cAAc,CAAC,EAAE,MAAM,GACtB,IAAI,CAAC;CACT"}
@@ -0,0 +1,52 @@
1
+ import type { TopicMapConstraint, MessageHeaders } from "./common";
2
+ import type { SendOptions, BatchMessageItem, BatchSendOptions } from "./producer.types";
3
+ import type { TransactionContext } from "./consumer.types";
4
+ import type { TopicDescriptor } from "../message/topic";
5
+ /** Producer methods of `IKafkaClient`. */
6
+ export interface IKafkaProducer<T extends TopicMapConstraint<T>> {
7
+ /**
8
+ * @example
9
+ * ```ts
10
+ * await kafka.sendMessage('orders.created', { orderId: '123', amount: 99 });
11
+ * ```
12
+ */
13
+ sendMessage<K extends keyof T>(topic: K, message: T[K], options?: SendOptions): Promise<void>;
14
+ sendMessage<D extends TopicDescriptor<string & keyof T, T[string & keyof T]>>(descriptor: D, message: D["__type"], options?: SendOptions): Promise<void>;
15
+ /**
16
+ * Send a null-value (tombstone) message to a topic.
17
+ * Tombstones are used with log-compacted topics to signal that a key's record
18
+ * should be removed during the next compaction cycle.
19
+ *
20
+ * Unlike `sendMessage`, tombstones carry no payload, no envelope headers, and
21
+ * skip schema validation. Only the partition `key` and optional custom `headers`
22
+ * are forwarded to Kafka.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * await kafka.sendTombstone('users.state', 'user-42');
27
+ * ```
28
+ */
29
+ sendTombstone(topic: string, key: string, headers?: MessageHeaders): Promise<void>;
30
+ /**
31
+ * @example
32
+ * ```ts
33
+ * await kafka.sendBatch('orders.created', [
34
+ * { value: { orderId: '1', amount: 10 }, key: 'order-1' },
35
+ * { value: { orderId: '2', amount: 20 }, key: 'order-2' },
36
+ * ]);
37
+ * ```
38
+ */
39
+ sendBatch<K extends keyof T>(topic: K, messages: Array<BatchMessageItem<T[K]>>, options?: BatchSendOptions): Promise<void>;
40
+ sendBatch<D extends TopicDescriptor<string & keyof T, T[string & keyof T]>>(descriptor: D, messages: Array<BatchMessageItem<D["__type"]>>, options?: BatchSendOptions): Promise<void>;
41
+ /**
42
+ * @example
43
+ * ```ts
44
+ * await kafka.transaction(async (tx) => {
45
+ * await tx.send('orders.created', { orderId: '123', amount: 99 });
46
+ * await tx.send('inventory.reserved', { itemId: 'a', qty: 1 });
47
+ * });
48
+ * ```
49
+ */
50
+ transaction(fn: (ctx: TransactionContext<T>) => Promise<void>): Promise<void>;
51
+ }
52
+ //# sourceMappingURL=producer.interface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"producer.interface.d.ts","sourceRoot":"","sources":["../../../src/client/types/producer.interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AACnE,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACxF,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAExD,0CAA0C;AAC1C,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,kBAAkB,CAAC,CAAC,CAAC;IAC7D;;;;;OAKG;IACH,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,EAC3B,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,EACb,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB,WAAW,CAAC,CAAC,SAAS,eAAe,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,EAC1E,UAAU,EAAE,CAAC,EACb,OAAO,EAAE,CAAC,CAAC,QAAQ,CAAC,EACpB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB;;;;;;;;;;;;;OAaG;IACH,aAAa,CACX,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB;;;;;;;;OAQG;IACH,SAAS,CAAC,CAAC,SAAS,MAAM,CAAC,EACzB,KAAK,EAAE,CAAC,EACR,QAAQ,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EACvC,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB,SAAS,CAAC,CAAC,SAAS,eAAe,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,EACxE,UAAU,EAAE,CAAC,EACb,QAAQ,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAC9C,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB;;;;;;;;OAQG;IACH,WAAW,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,kBAAkB,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/E"}
@@ -0,0 +1,90 @@
1
+ import type { CompressionType, MessageHeaders } from "./common";
2
+ /**
3
+ * Options for sending a single message.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * await kafka.sendMessage('orders.created', { orderId: '123', amount: 99 }, {
8
+ * key: 'order-123',
9
+ * headers: { 'x-source': 'checkout-service' },
10
+ * compression: 'snappy',
11
+ * });
12
+ * ```
13
+ */
14
+ export interface SendOptions {
15
+ /** Partition key for message routing. */
16
+ key?: string;
17
+ /** Custom headers attached to the message (merged with auto-generated envelope headers). */
18
+ headers?: MessageHeaders;
19
+ /** Override the auto-propagated correlation ID (default: inherited from ALS context or new UUID). */
20
+ correlationId?: string;
21
+ /** Schema version for the payload. Default: `1`. */
22
+ schemaVersion?: number;
23
+ /** Override the auto-generated event ID (UUID v4). */
24
+ eventId?: string;
25
+ /**
26
+ * Compression codec for this message.
27
+ * Applied at the producer record level — all messages in a single `send` call share the same codec.
28
+ * Default: `'none'`.
29
+ */
30
+ compression?: CompressionType;
31
+ /**
32
+ * Delay delivery by at least this many milliseconds.
33
+ *
34
+ * The message is produced to the `<topic>.delayed` staging topic with
35
+ * `x-delayed-until` / `x-delayed-target` headers instead of the target
36
+ * topic. A relay started via `startDelayedRelay()` holds it until the
37
+ * deadline, then forwards it to the target topic.
38
+ *
39
+ * Delivery time is a lower bound (at-least semantics) — actual delivery
40
+ * depends on the relay being up and partition-level head-of-line waits.
41
+ */
42
+ deliverAfterMs?: number;
43
+ }
44
+ /**
45
+ * Shape of each item in a `sendBatch` call.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * await kafka.sendBatch('orders.created', [
50
+ * { value: { orderId: '1', amount: 10 }, key: 'order-1' },
51
+ * { value: { orderId: '2', amount: 20 }, key: 'order-2', headers: { 'x-priority': 'high' } },
52
+ * ]);
53
+ * ```
54
+ */
55
+ export interface BatchMessageItem<V> {
56
+ value: V;
57
+ /**
58
+ * Kafka partition key for this message.
59
+ * Kafka hashes the key to deterministically route the message to a partition.
60
+ * Messages with the same key always land on the same partition — use this to
61
+ * guarantee ordering per entity (e.g. `userId`, `orderId`).
62
+ */
63
+ key?: string;
64
+ headers?: MessageHeaders;
65
+ correlationId?: string;
66
+ schemaVersion?: number;
67
+ eventId?: string;
68
+ }
69
+ /**
70
+ * Options for a `sendBatch` call (applies to all messages in the batch).
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * await kafka.sendBatch('metrics', messages, { compression: 'zstd' });
75
+ * ```
76
+ */
77
+ export interface BatchSendOptions {
78
+ /**
79
+ * Compression codec for this batch.
80
+ * Applied at the producer record level — all messages in the batch share the same codec.
81
+ * Default: `'none'`.
82
+ */
83
+ compression?: CompressionType;
84
+ /**
85
+ * Delay delivery of the whole batch by at least this many milliseconds.
86
+ * See `SendOptions.deliverAfterMs` for semantics.
87
+ */
88
+ deliverAfterMs?: number;
89
+ }
90
+ //# sourceMappingURL=producer.types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"producer.types.d.ts","sourceRoot":"","sources":["../../../src/client/types/producer.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAEhE;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,WAAW;IAC1B,yCAAyC;IACzC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,4FAA4F;IAC5F,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,qGAAqG;IACrG,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,oDAAoD;IACpD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,WAAW,CAAC,EAAE,eAAe,CAAC;IAC9B;;;;;;;;;;OAUG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;;;;GAUG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC;IACjC,KAAK,EAAE,CAAC,CAAC;IACT;;;;;OAKG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,WAAW,CAAC,EAAE,eAAe,CAAC;IAC9B;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB"}
@@ -0,0 +1,8 @@
1
+ export * from "./types/common";
2
+ export * from "./types/producer.types";
3
+ export * from "./types/dedup.types";
4
+ export * from "./types/consumer.types";
5
+ export * from "./types/admin.types";
6
+ export * from "./types/config.types";
7
+ export * from "./types/client";
8
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/client/types.ts"],"names":[],"mappings":"AAEA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC"}
package/dist/core.d.ts CHANGED
@@ -1,314 +1,10 @@
1
- import { q as KafkaLogger, K as KafkaInstrumentation, s as MessageLostContext, F as TtlExpiredContext, T as TopicMapConstraint, C as ClientId, G as GroupId, b as TopicDescriptor, v as SendOptions, M as MessageHeaders, B as BatchMessageItem, d as BatchSendOptions, z as TransactionContext, k as EventEnvelope, a as ConsumerOptions, h as ConsumerHandle, c as BatchMeta, J as WindowMeta, W as WindowConsumerOptions, t as RoutingOptions, A as TransactionalHandlerContext, r as KafkaMetrics } from './consumer.types-fFCag3VJ.js';
2
- export { e as BeforeConsumeResult, f as CircuitBreakerOptions, g as CompressionType, i as ConsumerInterceptor, D as DeduplicationOptions, j as DlqReason, E as EnvelopeHeaderOptions, H as HEADER_CORRELATION_ID, l as HEADER_EVENT_ID, m as HEADER_LAMPORT_CLOCK, n as HEADER_SCHEMA_VERSION, o as HEADER_TIMESTAMP, p as HEADER_TRACEPARENT, I as InferSchema, R as RetryOptions, S as SchemaLike, u as SchemaParseContext, w as SubscribeRetryOptions, x as TTopicMessageMap, y as TopicsFrom, L as buildEnvelopeHeaders, N as decodeHeaders, O as extractEnvelope, P as getEnvelopeContext, Q as runWithEnvelopeContext, U as topic } from './consumer.types-fFCag3VJ.js';
3
- import { K as KafkaTransport, I as IKafkaClient, D as DlqReplayOptions, R as ReadSnapshotOptions, t as CheckpointResult, z as RestoreCheckpointOptions, s as CheckpointRestoreResult, r as KafkaHealthResult, u as ConsumerGroupSummary, T as TopicDescription } from './client-CBBUDDtu.js';
4
- export { C as CheckpointEntry, v as IKafkaAdmin, w as IKafkaConsumer, x as IKafkaLifecycle, y as IKafkaProducer, A as TopicPartitionInfo } from './client-CBBUDDtu.js';
5
-
6
- /**
7
- * Options for `KafkaClient` constructor.
8
- *
9
- * @example
10
- * ```ts
11
- * const kafka = new KafkaClient(kafkaConfig, 'my-service', {
12
- * transactionalId: `my-service-tx-${replicaIndex}`,
13
- * lagThrottle: { maxLag: 10_000, pollIntervalMs: 3_000 },
14
- * clockRecovery: { topics: ['orders.created'] },
15
- * onMessageLost: (ctx) => alerting.fire('kafka.message-lost', ctx),
16
- * instrumentation: [otelInstrumentation()],
17
- * });
18
- * ```
19
- */
20
- interface KafkaClientOptions {
21
- /** Auto-create topics via admin before the first `sendMessage`, `sendBatch`, or `transaction` for each topic. Useful for development — not recommended in production. */
22
- autoCreateTopics?: boolean;
23
- /** When `true`, string topic keys are validated against any schema previously registered via a TopicDescriptor. Default: `true`. */
24
- strictSchemas?: boolean;
25
- /** Custom logger. Defaults to console with `[KafkaClient:<clientId>]` prefix. */
26
- logger?: KafkaLogger;
27
- /** Number of partitions for auto-created topics. Default: `1`. */
28
- numPartitions?: number;
29
- /** Client-wide instrumentation hooks (e.g. OTel). Applied to both send and consume paths. */
30
- instrumentation?: KafkaInstrumentation[];
31
- /**
32
- * Override the transactional producer ID used by `transaction()`.
33
- * Defaults to `${clientId}-tx`.
34
- *
35
- * The transactional ID must be **unique per producer instance** across the
36
- * entire Kafka cluster. Two `KafkaClient` instances with the same ID will
37
- * cause Kafka to fence one of the producers — the fenced producer will fail
38
- * on the next `transaction()` call. Set a distinct value per replica when
39
- * running multiple instances of the same service.
40
- */
41
- transactionalId?: string;
42
- /**
43
- * Called when a message is dropped without being sent to a DLQ.
44
- * Fires when the handler throws after all retries, or schema validation fails — and `dlq` is not enabled.
45
- * Use this to alert, log to external systems, or trigger fallback logic.
46
- */
47
- onMessageLost?: (ctx: MessageLostContext) => void | Promise<void>;
48
- /**
49
- * Called when a message is dropped due to TTL expiration (`messageTtlMs`).
50
- * Fires instead of `onMessageLost` for expired messages when `dlq` is not enabled.
51
- * When `dlq: true`, expired messages go to the DLQ and this callback is NOT called.
52
- *
53
- * **Client-wide fallback**: if `ConsumerOptions.onTtlExpired` is set on the consumer,
54
- * it takes precedence over this client-level callback.
55
- */
56
- onTtlExpired?: (ctx: TtlExpiredContext) => void | Promise<void>;
57
- /**
58
- * Called whenever a consumer group rebalance occurs.
59
- * - `'assign'` — new partitions were granted to this instance.
60
- * - `'revoke'` — partitions were taken away (e.g. another consumer joined).
61
- *
62
- * Applied to every consumer created by this client. If you need per-consumer
63
- * rebalance handling, use separate `KafkaClient` instances.
64
- */
65
- onRebalance?: (type: "assign" | "revoke", partitions: Array<{
66
- topic: string;
67
- partition: number;
68
- }>) => void;
69
- /**
70
- * Recover the Lamport clock from the last message in the given topics on `connectProducer()`.
71
- *
72
- * On startup the producer creates a short-lived consumer, seeks each partition to its
73
- * last message (`highWatermark − 1`), reads the `x-lamport-clock` header, then
74
- * initialises `_lamportClock` to the maximum value found. This guarantees monotonic
75
- * clock values across restarts without an external store.
76
- *
77
- * Topics that do not exist or are empty are silently skipped.
78
- */
79
- clockRecovery?: {
80
- /** Topic names to scan for the highest Lamport clock. */
81
- topics: string[];
82
- };
83
- /**
84
- * Delay `sendMessage` / `sendBatch` / `sendTombstone` when the observed lag of a
85
- * consumer group exceeds `maxLag`. Resumes immediately when lag drops below the threshold.
86
- *
87
- * Lag is polled via `getConsumerLag()` every `pollIntervalMs` in the background;
88
- * no admin call is made on each individual send.
89
- *
90
- * When `maxWaitMs` is exceeded the send is unblocked with a warning — this is
91
- * best-effort throttling, not hard back-pressure.
92
- *
93
- * Requires `connectProducer()` to have been called to start the polling loop.
94
- */
95
- lagThrottle?: {
96
- /** Consumer group whose lag is monitored. Defaults to the client's default group. */
97
- groupId?: string;
98
- /** Lag threshold (number of messages) above which sends are delayed. */
99
- maxLag: number;
100
- /** How often to poll `getConsumerLag()`. Default: `5000` ms. */
101
- pollIntervalMs?: number;
102
- /**
103
- * Maximum time (ms) a send will wait while throttled before proceeding anyway.
104
- * Default: `30_000` ms.
105
- */
106
- maxWaitMs?: number;
107
- };
108
- /**
109
- * Custom transport implementation.
110
- *
111
- * By default `KafkaClient` uses `ConfluentTransport` which wraps
112
- * `@confluentinc/kafka-javascript` (librdkafka). Inject a different
113
- * `KafkaTransport` to target an alternative broker library, or to supply
114
- * a deterministic fake in unit tests without mocking the confluentinc module.
115
- *
116
- * @example
117
- * ```ts
118
- * // In tests — no jest.mock() needed
119
- * const kafka = new KafkaClient('svc', 'grp', [], {
120
- * transport: new FakeTransport(),
121
- * });
122
- * ```
123
- */
124
- transport?: KafkaTransport;
125
- }
126
-
127
- /**
128
- * Type-safe Kafka client.
129
- * Wraps @confluentinc/kafka-javascript (librdkafka) with JSON serialization,
130
- * retries, DLQ, transactions, and interceptors.
131
- *
132
- * @typeParam T - Topic-to-message type mapping for compile-time safety.
133
- */
134
- declare class KafkaClient<T extends TopicMapConstraint<T>> implements IKafkaClient<T> {
135
- readonly clientId: ClientId;
136
- private readonly ctx;
137
- /**
138
- * Create a new KafkaClient.
139
- * @param clientId Unique client identifier (used in Kafka metadata and logs).
140
- * @param groupId Default consumer group ID for this client.
141
- * @param brokers Array of broker addresses, e.g. `['localhost:9092']`.
142
- * @param options Optional client-wide configuration.
143
- * @example
144
- * ```ts
145
- * const kafka = new KafkaClient('my-service', 'my-service-group', ['localhost:9092'], {
146
- * lagThrottle: { maxLag: 10_000 },
147
- * onMessageLost: (ctx) => logger.error('Message lost', ctx),
148
- * });
149
- * await kafka.connectProducer();
150
- * ```
151
- */
152
- constructor(clientId: ClientId, groupId: GroupId, brokers: string[], options?: KafkaClientOptions);
153
- /** @inheritDoc */
154
- sendMessage<D extends TopicDescriptor<string & keyof T, T[string & keyof T]>>(descriptor: D, message: D["__type"], options?: SendOptions): Promise<void>;
155
- sendMessage<K extends keyof T>(topic: K, message: T[K], options?: SendOptions): Promise<void>;
156
- /** @inheritDoc */
157
- sendTombstone(topic: string, key: string, headers?: MessageHeaders): Promise<void>;
158
- /** @inheritDoc */
159
- sendBatch<D extends TopicDescriptor<string & keyof T, T[string & keyof T]>>(descriptor: D, messages: Array<BatchMessageItem<D["__type"]>>, options?: BatchSendOptions): Promise<void>;
160
- sendBatch<K extends keyof T>(topic: K, messages: Array<BatchMessageItem<T[K]>>, options?: BatchSendOptions): Promise<void>;
161
- /** @inheritDoc */
162
- transaction(fn: (ctx: TransactionContext<T>) => Promise<void>): Promise<void>;
163
- /** @inheritDoc */
164
- connectProducer(): Promise<void>;
165
- /** @internal */
166
- disconnectProducer(): Promise<void>;
167
- /** @inheritDoc */
168
- startConsumer<K extends Array<keyof T>>(topics: K, handleMessage: (envelope: EventEnvelope<T[K[number]]>) => Promise<void>, options?: ConsumerOptions<T>): Promise<ConsumerHandle>;
169
- startConsumer<D extends TopicDescriptor<string & keyof T, T[string & keyof T]>>(topics: D[], handleMessage: (envelope: EventEnvelope<D["__type"]>) => Promise<void>, options?: ConsumerOptions<T>): Promise<ConsumerHandle>;
170
- /** @inheritDoc */
171
- startBatchConsumer<K extends Array<keyof T>>(topics: K, handleBatch: (envelopes: EventEnvelope<T[K[number]]>[], meta: BatchMeta) => Promise<void>, options?: ConsumerOptions<T>): Promise<ConsumerHandle>;
172
- startBatchConsumer<D extends TopicDescriptor<string & keyof T, T[string & keyof T]>>(topics: D[], handleBatch: (envelopes: EventEnvelope<D["__type"]>[], meta: BatchMeta) => Promise<void>, options?: ConsumerOptions<T>): Promise<ConsumerHandle>;
173
- /** @inheritDoc */
174
- consume<K extends keyof T & string>(topic: K, options?: ConsumerOptions<T>): AsyncIterableIterator<EventEnvelope<T[K]>>;
175
- /** @inheritDoc */
176
- startWindowConsumer<K extends keyof T & string>(topic: K, handler: (envelopes: EventEnvelope<T[K]>[], meta: WindowMeta) => Promise<void>, options: WindowConsumerOptions<T>): Promise<ConsumerHandle>;
177
- /** @inheritDoc */
178
- startRoutedConsumer<K extends Array<keyof T>>(topics: K, routing: RoutingOptions<T[K[number]]>, options?: ConsumerOptions<T>): Promise<ConsumerHandle>;
179
- /** @inheritDoc */
180
- startTransactionalConsumer<K extends Array<keyof T>>(topics: K, handler: (envelope: EventEnvelope<T[K[number]]>, tx: TransactionalHandlerContext<T>) => Promise<void>, options?: ConsumerOptions<T>): Promise<ConsumerHandle>;
181
- /** @inheritDoc */
182
- stopConsumer(groupId?: string): Promise<void>;
183
- /** @inheritDoc */
184
- pauseConsumer(groupId: string | undefined, assignments: Array<{
185
- topic: string;
186
- partitions: number[];
187
- }>): void;
188
- /** @inheritDoc */
189
- resumeConsumer(groupId: string | undefined, assignments: Array<{
190
- topic: string;
191
- partitions: number[];
192
- }>): void;
193
- /** @inheritDoc */
194
- replayDlq(topic: string, options?: DlqReplayOptions): Promise<{
195
- replayed: number;
196
- skipped: number;
197
- }>;
198
- /** @inheritDoc */
199
- readSnapshot<K extends keyof T & string>(topic: K, options?: ReadSnapshotOptions): Promise<Map<string, EventEnvelope<T[K]>>>;
200
- /** @inheritDoc */
201
- checkpointOffsets(groupId: string | undefined, checkpointTopic: string): Promise<CheckpointResult>;
202
- /** @inheritDoc */
203
- restoreFromCheckpoint(groupId: string | undefined, checkpointTopic: string, options?: RestoreCheckpointOptions): Promise<CheckpointRestoreResult>;
204
- /** @inheritDoc */
205
- resetOffsets(groupId: string | undefined, topic: string, position: "earliest" | "latest"): Promise<void>;
206
- /** @inheritDoc */
207
- seekToOffset(groupId: string | undefined, assignments: Array<{
208
- topic: string;
209
- partition: number;
210
- offset: string;
211
- }>): Promise<void>;
212
- /** @inheritDoc */
213
- seekToTimestamp(groupId: string | undefined, assignments: Array<{
214
- topic: string;
215
- partition: number;
216
- timestamp: number;
217
- }>): Promise<void>;
218
- /** @inheritDoc */
219
- getConsumerLag(groupId?: string): Promise<Array<{
220
- topic: string;
221
- partition: number;
222
- lag: number;
223
- }>>;
224
- /** @inheritDoc */
225
- checkStatus(): Promise<KafkaHealthResult>;
226
- /** @inheritDoc */
227
- listConsumerGroups(): Promise<ConsumerGroupSummary[]>;
228
- /** @inheritDoc */
229
- describeTopics(topics?: string[]): Promise<TopicDescription[]>;
230
- /** @inheritDoc */
231
- deleteRecords(topic: string, partitions: Array<{
232
- partition: number;
233
- offset: string;
234
- }>): Promise<void>;
235
- /** @inheritDoc */
236
- getCircuitState(topic: string, partition: number, groupId?: string): {
237
- status: "closed" | "open" | "half-open";
238
- failures: number;
239
- windowSize: number;
240
- } | undefined;
241
- /** @inheritDoc */
242
- getMetrics(topic?: string): Readonly<KafkaMetrics>;
243
- /** @inheritDoc */
244
- resetMetrics(topic?: string): void;
245
- getClientId(): ClientId;
246
- /** @inheritDoc */
247
- disconnect(drainTimeoutMs?: number): Promise<void>;
248
- /** NestJS lifecycle hook — called automatically on module teardown. */
249
- onModuleDestroy(): Promise<void>;
250
- /** @inheritDoc */
251
- enableGracefulShutdown(signals?: NodeJS.Signals[], drainTimeoutMs?: number): void;
252
- }
253
-
254
- /**
255
- * Error thrown when a consumer message handler fails.
256
- * @example
257
- * ```ts
258
- * await kafka.startConsumer(['orders'], async (envelope) => {
259
- * try { await process(envelope); }
260
- * catch (err) {
261
- * if (err instanceof KafkaProcessingError) {
262
- * console.error(err.topic, err.originalMessage);
263
- * }
264
- * }
265
- * });
266
- * ```
267
- */
268
- declare class KafkaProcessingError extends Error {
269
- readonly topic: string;
270
- readonly originalMessage: unknown;
271
- readonly cause?: Error;
272
- constructor(message: string, topic: string, originalMessage: unknown, options?: {
273
- cause?: Error;
274
- });
275
- }
276
- /**
277
- * Error thrown when schema validation fails on send or consume.
278
- * @example
279
- * ```ts
280
- * try { await kafka.sendMessage('orders.created', invalidPayload); }
281
- * catch (err) {
282
- * if (err instanceof KafkaValidationError) {
283
- * console.error('Validation failed for topic:', err.topic);
284
- * }
285
- * }
286
- * ```
287
- */
288
- declare class KafkaValidationError extends Error {
289
- readonly topic: string;
290
- readonly originalMessage: unknown;
291
- readonly cause?: Error;
292
- constructor(topic: string, originalMessage: unknown, options?: {
293
- cause?: Error;
294
- });
295
- }
296
- /**
297
- * Error thrown when all retry attempts are exhausted for a message.
298
- * @example
299
- * ```ts
300
- * const kafka = new KafkaClient(config, groupId, { onMessageLost: (ctx) => {
301
- * if (ctx.error instanceof KafkaRetryExhaustedError) {
302
- * console.error(`Exhausted after ${ctx.error.attempts} attempts on ${ctx.error.topic}`);
303
- * }
304
- * }});
305
- * ```
306
- */
307
- declare class KafkaRetryExhaustedError extends KafkaProcessingError {
308
- readonly attempts: number;
309
- constructor(topic: string, originalMessage: unknown, attempts: number, options?: {
310
- cause?: Error;
311
- });
312
- }
313
-
314
- export { BatchMessageItem, BatchMeta, BatchSendOptions, CheckpointRestoreResult, CheckpointResult, ClientId, ConsumerGroupSummary, ConsumerHandle, ConsumerOptions, DlqReplayOptions, EventEnvelope, GroupId, IKafkaClient, KafkaClient, type KafkaClientOptions, KafkaHealthResult, KafkaInstrumentation, KafkaLogger, KafkaMetrics, KafkaProcessingError, KafkaRetryExhaustedError, KafkaValidationError, MessageHeaders, MessageLostContext, ReadSnapshotOptions, RestoreCheckpointOptions, RoutingOptions, SendOptions, TopicDescription, TopicDescriptor, TopicMapConstraint, TransactionContext, TransactionalHandlerContext, TtlExpiredContext, WindowConsumerOptions, WindowMeta };
1
+ export * from "./client/kafka.client";
2
+ export * from "./client/message/topic";
3
+ export * from "./client/message/versioned-schema";
4
+ export * from "./client/message/schema-registry";
5
+ export * from "./client/errors";
6
+ export * from "./client/message/envelope";
7
+ export * from "./client/outbox";
8
+ export * from "./client/security";
9
+ export * from "./client/config";
10
+ //# sourceMappingURL=core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,mCAAmC,CAAC;AAClD,cAAc,kCAAkC,CAAC;AACjD,cAAc,iBAAiB,CAAC;AAChC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC"}