@aitickets123654/common-kafka 1.0.7 → 1.0.9

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,4 +1,4 @@
1
- import { Kafka, Consumer, EachMessagePayload } from 'kafkajs';
1
+ import { Consumer, EachMessagePayload } from 'kafkajs';
2
2
  import { Topics } from './topics';
3
3
  interface Event {
4
4
  topic: Topics;
@@ -6,11 +6,9 @@ interface Event {
6
6
  }
7
7
  export declare abstract class Listener<T extends Event> {
8
8
  abstract topic: T['topic'];
9
- abstract groupId: string;
10
9
  abstract onMessage(data: T['data'], payload: EachMessagePayload): void;
11
- protected kafka: Kafka;
12
10
  protected consumer: Consumer;
13
- constructor(kafka: Kafka);
11
+ constructor(consumer: Consumer);
14
12
  listen(): Promise<void>;
15
13
  disconnect(): Promise<void>;
16
14
  }
@@ -1,61 +1,47 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.Listener = void 0;
13
4
  class Listener {
14
- constructor(kafka) {
15
- this.kafka = kafka;
5
+ constructor(consumer) {
6
+ this.consumer = consumer;
16
7
  }
17
- listen() {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- this.consumer = this.kafka.consumer({ groupId: this.groupId });
20
- yield this.consumer.connect();
21
- console.log(`Listener connected to Kafka (${this.topic})`);
22
- yield this.consumer.subscribe({
23
- topic: this.topic,
24
- fromBeginning: true,
25
- });
26
- yield this.consumer.run({
27
- autoCommit: false,
28
- eachMessage: (payload) => __awaiter(this, void 0, void 0, function* () {
29
- var _a;
30
- const { message } = payload;
31
- const value = (_a = message.value) === null || _a === void 0 ? void 0 : _a.toString();
32
- if (!value)
33
- return;
34
- try {
35
- const data = JSON.parse(value);
36
- yield this.onMessage(data, payload);
37
- yield this.consumer.commitOffsets([
38
- {
39
- topic: payload.topic,
40
- partition: payload.partition,
41
- offset: (Number(message.offset) + 1).toString(),
42
- },
43
- ]);
44
- }
45
- catch (err) {
46
- console.error(`Error processing message:`, err);
47
- }
48
- }),
49
- });
8
+ async listen() {
9
+ await this.consumer.connect();
10
+ await this.consumer.subscribe({
11
+ topic: this.topic,
12
+ fromBeginning: true,
50
13
  });
51
- }
52
- disconnect() {
53
- return __awaiter(this, void 0, void 0, function* () {
54
- if (this.consumer) {
55
- yield this.consumer.disconnect();
56
- console.log(`Disconnected listener (${this.topic})`);
57
- }
14
+ console.log(`Listener connected to Kafka (topic: ${this.topic})`);
15
+ await this.consumer.run({
16
+ autoCommit: false,
17
+ eachMessage: async (payload) => {
18
+ var _a;
19
+ const { message } = payload;
20
+ const value = (_a = message.value) === null || _a === void 0 ? void 0 : _a.toString();
21
+ if (!value)
22
+ return;
23
+ try {
24
+ const data = JSON.parse(value);
25
+ await this.onMessage(data, payload);
26
+ await this.consumer.commitOffsets([
27
+ {
28
+ topic: payload.topic,
29
+ partition: payload.partition,
30
+ offset: (Number(message.offset) + 1).toString(),
31
+ },
32
+ ]);
33
+ }
34
+ catch (err) {
35
+ console.error(`Error in ${this.topic}:`, err);
36
+ }
37
+ },
58
38
  });
59
39
  }
40
+ async disconnect() {
41
+ if (this.consumer) {
42
+ await this.consumer.disconnect();
43
+ console.log(`Listener disconnected (topic: ${this.topic})`);
44
+ }
45
+ }
60
46
  }
61
47
  exports.Listener = Listener;
@@ -1,4 +1,4 @@
1
- import { Kafka, Producer, Admin } from 'kafkajs';
1
+ import { Producer, Admin } from 'kafkajs';
2
2
  import { Topics } from './topics';
3
3
  interface Event {
4
4
  topic: Topics;
@@ -6,12 +6,9 @@ interface Event {
6
6
  }
7
7
  export declare abstract class Publisher<T extends Event> {
8
8
  abstract topic: T['topic'];
9
- protected kafka: Kafka;
10
9
  protected producer: Producer;
11
10
  protected admin: Admin;
12
- constructor(kafka: Kafka);
13
- connect(): Promise<void>;
14
- disconnect(): Promise<void>;
11
+ protected constructor(producer: Producer, admin: Admin);
15
12
  private ensureTopicExists;
16
13
  publish(data: T['data']): Promise<void>;
17
14
  }
@@ -1,79 +1,51 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.Publisher = void 0;
13
4
  class Publisher {
14
- constructor(kafka) {
15
- this.kafka = kafka;
16
- this.producer = kafka.producer();
17
- this.admin = kafka.admin();
5
+ constructor(producer, admin) {
6
+ this.producer = producer;
7
+ this.admin = admin;
18
8
  }
19
- connect() {
20
- return __awaiter(this, void 0, void 0, function* () {
21
- yield this.admin.connect();
22
- yield this.ensureTopicExists();
23
- yield this.producer.connect();
24
- console.log(`Publisher connected to Kafka and topic '${this.topic}' ready`);
25
- });
26
- }
27
- disconnect() {
28
- return __awaiter(this, void 0, void 0, function* () {
29
- yield this.producer.disconnect();
30
- yield this.admin.disconnect();
31
- console.log('Kafka publisher disconnected');
32
- });
33
- }
34
- ensureTopicExists() {
35
- return __awaiter(this, void 0, void 0, function* () {
36
- try {
37
- const topics = yield this.admin.listTopics();
38
- if (!topics.includes(this.topic)) {
39
- yield this.admin.createTopics({
40
- topics: [
41
- {
42
- topic: this.topic,
43
- numPartitions: 2,
44
- replicationFactor: 1,
45
- },
46
- ],
47
- waitForLeaders: true,
48
- validateOnly: false,
49
- timeout: 5000,
50
- });
51
- console.log(`Topic '${this.topic}' created successfully.`);
52
- }
53
- else {
54
- console.log(`Topic '${this.topic}' already exists.`);
55
- }
56
- }
57
- catch (err) {
58
- console.error(`Failed to ensure topic '${this.topic}':`, err);
59
- throw err;
60
- }
61
- });
62
- }
63
- publish(data) {
64
- return __awaiter(this, void 0, void 0, function* () {
65
- try {
66
- yield this.producer.send({
67
- topic: this.topic,
68
- messages: [{ value: JSON.stringify(data) }],
9
+ async ensureTopicExists() {
10
+ try {
11
+ const topics = await this.admin.listTopics();
12
+ if (!topics.includes(this.topic)) {
13
+ await this.admin.createTopics({
14
+ topics: [
15
+ {
16
+ topic: this.topic,
17
+ numPartitions: 2,
18
+ replicationFactor: 1,
19
+ },
20
+ ],
21
+ waitForLeaders: true,
22
+ validateOnly: false,
23
+ timeout: 5000,
69
24
  });
70
- console.log(`Event published to topic '${this.topic}'`);
25
+ console.log(`Topic '${this.topic}' created`);
71
26
  }
72
- catch (err) {
73
- console.error('Failed to publish event:', err);
74
- throw err;
27
+ else {
28
+ console.log(`Topic '${this.topic}' already exists`);
75
29
  }
76
- });
30
+ }
31
+ catch (err) {
32
+ console.error(`Failed to ensure topic '${this.topic}':`, err);
33
+ throw err;
34
+ }
35
+ }
36
+ async publish(data) {
37
+ await this.ensureTopicExists();
38
+ try {
39
+ await this.producer.send({
40
+ topic: this.topic,
41
+ messages: [{ value: JSON.stringify(data) }],
42
+ });
43
+ console.log(`Event published to topic '${this.topic}'`, data);
44
+ }
45
+ catch (err) {
46
+ console.error('Failed to publish event:', err);
47
+ throw err;
48
+ }
77
49
  }
78
50
  }
79
51
  exports.Publisher = Publisher;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aitickets123654/common-kafka",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",