kafka-ts 1.1.2-beta.4 → 1.1.3-beta.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.
@@ -20,7 +20,9 @@ export type ConsumerOptions = {
20
20
  fromBeginning?: boolean;
21
21
  fromTimestamp?: bigint;
22
22
  retrier?: Retrier;
23
- onBatch: (messages: Required<Message>[]) => unknown;
23
+ onBatch: (messages: Required<Message>[], context: {
24
+ resolveOffset: (message: Pick<Required<Message>, 'topic' | 'partition' | 'offset'>) => void;
25
+ }) => unknown;
24
26
  };
25
27
  export declare class Consumer extends EventEmitter<{
26
28
  offsetCommit: [];
@@ -185,7 +185,17 @@ class Consumer extends events_1.default {
185
185
  if (!messages.length) {
186
186
  return;
187
187
  }
188
- await retrier(() => options.onBatch(messages));
188
+ const resolveOffset = (message) => this.offsetManager.resolve(message.topic, message.partition, message.offset + 1n);
189
+ try {
190
+ await retrier(() => options.onBatch(messages.filter((message) => !this.offsetManager.isResolved(message)), { resolveOffset }));
191
+ }
192
+ catch (error) {
193
+ await this.consumerGroup
194
+ ?.offsetCommit(topicPartitions)
195
+ .then(() => this.offsetManager.flush(topicPartitions))
196
+ .catch();
197
+ throw error;
198
+ }
189
199
  response.responses.forEach(({ topicId, partitions }) => {
190
200
  partitions.forEach(({ partitionIndex, records }) => {
191
201
  records.forEach(({ baseOffset, lastOffsetDelta }) => {
@@ -12,7 +12,13 @@ export declare class OffsetManager {
12
12
  pendingOffsets: Record<string, Record<number, bigint>>;
13
13
  constructor(options: OffsetManagerOptions);
14
14
  getCurrentOffset(topic: string, partition: number): bigint;
15
+ getPendingOffset(topic: string, partition: number): bigint;
15
16
  resolve(topic: string, partition: number, offset: bigint): void;
17
+ isResolved(message: {
18
+ topic: string;
19
+ partition: number;
20
+ offset: bigint;
21
+ }): boolean;
16
22
  flush(topicPartitions: Record<string, Set<number>>): void;
17
23
  fetchOffsets(options: {
18
24
  fromTimestamp: bigint;
@@ -15,10 +15,17 @@ class OffsetManager {
15
15
  getCurrentOffset(topic, partition) {
16
16
  return this.currentOffsets[topic]?.[partition] ?? 0n;
17
17
  }
18
+ getPendingOffset(topic, partition) {
19
+ return this.pendingOffsets[topic]?.[partition] ?? 0n;
20
+ }
18
21
  resolve(topic, partition, offset) {
19
22
  this.pendingOffsets[topic] ??= {};
20
23
  this.pendingOffsets[topic][partition] = offset;
21
24
  }
25
+ isResolved(message) {
26
+ return (this.getCurrentOffset(message.topic, message.partition) > message.offset ||
27
+ this.getPendingOffset(message.topic, message.partition) > message.offset);
28
+ }
22
29
  flush(topicPartitions) {
23
30
  Object.entries(topicPartitions).forEach(([topic, partitions]) => {
24
31
  this.currentOffsets[topic] ??= {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kafka-ts",
3
- "version": "1.1.2-beta.4",
3
+ "version": "1.1.3-beta.0",
4
4
  "main": "dist/index.js",
5
5
  "author": "Priit Käärd",
6
6
  "license": "MIT",