@aitickets123654/common-kafka 1.0.29 → 1.0.32

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,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Listener = void 0;
4
+ const registry_1 = require("./config/registry");
4
5
  class Listener {
5
6
  constructor(consumer) {
6
7
  this.consumer = consumer;
@@ -15,13 +16,11 @@ class Listener {
15
16
  await this.consumer.run({
16
17
  eachBatchAutoResolve: false, // Отключаем автокоммиты — всё контролируем вручную
17
18
  eachBatch: async ({ batch, resolveOffset, heartbeat, commitOffsetsIfNecessary, uncommittedOffsets, }) => {
18
- var _a;
19
19
  const { topic, partition } = batch;
20
20
  let lastProcessedOffset = null; // запоминаем, до какого оффсета дошли
21
21
  try {
22
22
  for (const message of batch.messages) {
23
- const offset = message.offset;
24
- const value = (_a = message.value) === null || _a === void 0 ? void 0 : _a.toString();
23
+ const { offset, value } = message;
25
24
  // Пустое сообщение — пропускаем и фиксируем
26
25
  if (!value) {
27
26
  resolveOffset(offset);
@@ -30,10 +29,10 @@ class Listener {
30
29
  }
31
30
  let data;
32
31
  try {
33
- data = JSON.parse(value);
32
+ data = await registry_1.registry.decode(value);
34
33
  }
35
34
  catch (err) {
36
- console.error(`Невалидный JSON ${topic}/${partition}@${offset}`, err);
35
+ console.error(`Avro decode failed for ${topic}/${partition}@${offset}`, err);
37
36
  resolveOffset(offset); // не мешает прогрессу
38
37
  lastProcessedOffset = offset;
39
38
  continue;
@@ -47,7 +46,7 @@ class Listener {
47
46
  await heartbeat();
48
47
  }
49
48
  catch (err) {
50
- console.warn(`Ошибка при обработке ${topic}/${partition}@${offset}:`, err);
49
+ console.warn(`Error processing ${topic}/${partition}@${offset}:`, err);
51
50
  // Откатываемся на оффсет с ошибкой
52
51
  // Kafka при следующем цикле снова отдаст это сообщение
53
52
  this.consumer.seek({ topic, partition, offset });
@@ -68,7 +67,7 @@ class Listener {
68
67
  }
69
68
  }
70
69
  catch (commitErr) {
71
- console.error(`commitOffsetsIfNecessary не сработал (${topic}/${partition})`, commitErr);
70
+ console.error(`commitOffsetsIfNecessary failed (${topic}/${partition})`, commitErr);
72
71
  }
73
72
  }
74
73
  },
@@ -1,45 +1,24 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Publisher = void 0;
7
+ const registry_1 = require("./config/registry");
8
+ const schema_ids_json_1 = __importDefault(require("./config/schema-ids.json"));
4
9
  class Publisher {
5
- // protected admin: Admin;
6
- constructor(producer /*, admin: Admin*/) {
10
+ constructor(producer) {
7
11
  this.producer = producer;
8
- // this.admin = admin;
9
12
  }
10
- // private async ensureTopicExists(): Promise<void> {
11
- // try {
12
- // const topics = await this.admin.listTopics();
13
- // if (!topics.includes(this.topic)) {
14
- // await this.admin.createTopics({
15
- // topics: [
16
- // {
17
- // topic: this.topic,
18
- // numPartitions: 2,
19
- // replicationFactor: 1,
20
- // },
21
- // ],
22
- // waitForLeaders: true,
23
- // validateOnly: false,
24
- // timeout: 5000,
25
- // });
26
- // // console.log(`Topic '${this.topic}' created`);
27
- // } else {
28
- // // console.log(`Topic '${this.topic}' already exists`);
29
- // }
30
- // } catch (err) {
31
- // console.error(`Failed to ensure topic '${this.topic}':`, err);
32
- // throw err;
33
- // }
34
- // }
35
13
  async publish(data) {
36
- // await this.ensureTopicExists();
37
14
  try {
15
+ const schemaId = schema_ids_json_1.default[`${this.topic}-value`];
16
+ const encoded = await registry_1.registry.encode(schemaId, data);
38
17
  await this.producer.send({
39
18
  topic: this.topic,
40
- messages: [{ value: JSON.stringify(data) }],
19
+ messages: [{ value: encoded }],
41
20
  });
42
- // console.log(`Event published to topic '${this.topic}'`, `\ndata: ${JSON.stringify(data, null, 2)}`);
21
+ console.log(`Event published to topic '${this.topic}'`);
43
22
  }
44
23
  catch (err) {
45
24
  console.error('Failed to publish event:', err);
@@ -0,0 +1,2 @@
1
+ import { SchemaRegistry } from '@kafkajs/confluent-schema-registry';
2
+ export declare const registry: SchemaRegistry;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registry = void 0;
4
+ const confluent_schema_registry_1 = require("@kafkajs/confluent-schema-registry");
5
+ exports.registry = new confluent_schema_registry_1.SchemaRegistry({
6
+ host: process.env.SCHEMA_REGISTRY_URL || 'http://schema-registry-srv:8081',
7
+ });
@@ -0,0 +1,8 @@
1
+ {
2
+ "tickets.ticket.updated.v1-value": 1,
3
+ "tickets.ticket.created.v1-value": 2,
4
+ "payments.payment.created.v1-value": 3,
5
+ "orders.order.created.v1-value": 4,
6
+ "orders.order.cancelled.v1-value": 5,
7
+ "expiration.expiration.complete.v1-value": 6
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aitickets123654/common-kafka",
3
- "version": "1.0.29",
3
+ "version": "1.0.32",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -20,6 +20,7 @@
20
20
  "typescript": "^5.8.3"
21
21
  },
22
22
  "dependencies": {
23
+ "@kafkajs/confluent-schema-registry": "^3.9.0",
23
24
  "@types/cookie-session": "^2.0.49",
24
25
  "@types/express": "^4.17.3",
25
26
  "@types/jsonwebtoken": "^9.0.9",
@@ -1 +0,0 @@
1
- export type OrderStatus = 'created' | 'cancelled' | 'awaiting_payment' | 'complete';
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });