@aitickets123654/common-kafka 1.0.7 → 1.0.8
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/build/events/base-listener.js +36 -49
- package/build/events/base-publisher.js +45 -62
- package/package.json +1 -1
|
@@ -1,61 +1,48 @@
|
|
|
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
5
|
constructor(kafka) {
|
|
15
6
|
this.kafka = kafka;
|
|
16
7
|
}
|
|
17
|
-
listen() {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
+
this.consumer = this.kafka.consumer({ groupId: this.groupId });
|
|
10
|
+
await this.consumer.connect();
|
|
11
|
+
console.log(`Listener connected to Kafka (${this.topic})`);
|
|
12
|
+
await this.consumer.subscribe({
|
|
13
|
+
topic: this.topic,
|
|
14
|
+
fromBeginning: true,
|
|
50
15
|
});
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
16
|
+
await this.consumer.run({
|
|
17
|
+
autoCommit: false,
|
|
18
|
+
eachMessage: async (payload) => {
|
|
19
|
+
var _a;
|
|
20
|
+
const { message } = payload;
|
|
21
|
+
const value = (_a = message.value) === null || _a === void 0 ? void 0 : _a.toString();
|
|
22
|
+
if (!value)
|
|
23
|
+
return;
|
|
24
|
+
try {
|
|
25
|
+
const data = JSON.parse(value);
|
|
26
|
+
await this.onMessage(data, payload);
|
|
27
|
+
await this.consumer.commitOffsets([
|
|
28
|
+
{
|
|
29
|
+
topic: payload.topic,
|
|
30
|
+
partition: payload.partition,
|
|
31
|
+
offset: (Number(message.offset) + 1).toString(),
|
|
32
|
+
},
|
|
33
|
+
]);
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
console.error(`Error processing message:`, err);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
58
39
|
});
|
|
59
40
|
}
|
|
41
|
+
async disconnect() {
|
|
42
|
+
if (this.consumer) {
|
|
43
|
+
await this.consumer.disconnect();
|
|
44
|
+
console.log(`Disconnected listener (${this.topic})`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
60
47
|
}
|
|
61
48
|
exports.Listener = Listener;
|
|
@@ -1,13 +1,4 @@
|
|
|
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 {
|
|
@@ -16,64 +7,56 @@ class Publisher {
|
|
|
16
7
|
this.producer = kafka.producer();
|
|
17
8
|
this.admin = kafka.admin();
|
|
18
9
|
}
|
|
19
|
-
connect() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
console.log(`Publisher connected to Kafka and topic '${this.topic}' ready`);
|
|
25
|
-
});
|
|
10
|
+
async connect() {
|
|
11
|
+
await this.admin.connect();
|
|
12
|
+
await this.ensureTopicExists();
|
|
13
|
+
await this.producer.connect();
|
|
14
|
+
console.log(`Publisher connected to Kafka and topic '${this.topic}' ready`);
|
|
26
15
|
}
|
|
27
|
-
disconnect() {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
console.log('Kafka publisher disconnected');
|
|
32
|
-
});
|
|
16
|
+
async disconnect() {
|
|
17
|
+
await this.producer.disconnect();
|
|
18
|
+
await this.admin.disconnect();
|
|
19
|
+
console.log('Kafka publisher disconnected');
|
|
33
20
|
}
|
|
34
|
-
ensureTopicExists() {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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) }],
|
|
21
|
+
async ensureTopicExists() {
|
|
22
|
+
try {
|
|
23
|
+
const topics = await this.admin.listTopics();
|
|
24
|
+
if (!topics.includes(this.topic)) {
|
|
25
|
+
await this.admin.createTopics({
|
|
26
|
+
topics: [
|
|
27
|
+
{
|
|
28
|
+
topic: this.topic,
|
|
29
|
+
numPartitions: 2,
|
|
30
|
+
replicationFactor: 1,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
waitForLeaders: true,
|
|
34
|
+
validateOnly: false,
|
|
35
|
+
timeout: 5000,
|
|
69
36
|
});
|
|
70
|
-
console.log(`
|
|
37
|
+
console.log(`Topic '${this.topic}' created successfully.`);
|
|
71
38
|
}
|
|
72
|
-
|
|
73
|
-
console.
|
|
74
|
-
throw err;
|
|
39
|
+
else {
|
|
40
|
+
console.log(`Topic '${this.topic}' already exists.`);
|
|
75
41
|
}
|
|
76
|
-
}
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
console.error(`Failed to ensure topic '${this.topic}':`, err);
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async publish(data) {
|
|
49
|
+
try {
|
|
50
|
+
await this.producer.send({
|
|
51
|
+
topic: this.topic,
|
|
52
|
+
messages: [{ value: JSON.stringify(data) }],
|
|
53
|
+
});
|
|
54
|
+
console.log(`Event published to topic '${this.topic}'`);
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
console.error('Failed to publish event:', err);
|
|
58
|
+
throw err;
|
|
59
|
+
}
|
|
77
60
|
}
|
|
78
61
|
}
|
|
79
62
|
exports.Publisher = Publisher;
|