kafka-ts 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api/fetch.js CHANGED
@@ -4,7 +4,6 @@ exports.FETCH = void 0;
4
4
  const api_1 = require("../utils/api");
5
5
  const decoder_1 = require("../utils/decoder");
6
6
  const error_1 = require("../utils/error");
7
- const logger_1 = require("../utils/logger");
8
7
  exports.FETCH = (0, api_1.createApi)({
9
8
  apiKey: 1,
10
9
  apiVersion: 15,
@@ -85,8 +84,7 @@ const decodeRecordBatch = (decoder) => {
85
84
  continue;
86
85
  }
87
86
  if (!recordBatchDecoder.canReadBytes(batchLength)) {
88
- // likely running into maxBytes limit
89
- logger_1.log.debug('Record batch is incomplete, skipping last batch.');
87
+ // running into maxBytes limit
90
88
  recordBatchDecoder.read();
91
89
  continue;
92
90
  }
@@ -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",
3
+ "version": "1.1.3",
4
4
  "main": "dist/index.js",
5
5
  "author": "Priit Käärd",
6
6
  "license": "MIT",
@@ -23,7 +23,7 @@
23
23
  "@types/node": "^20.12.12",
24
24
  "prettier": "^3.3.3",
25
25
  "typescript": "^5.4.5",
26
- "vitest": "^1.6.0"
26
+ "vitest": "^1.6.1"
27
27
  },
28
28
  "keywords": [
29
29
  "kafka",