@xrystal/core 3.27.5 → 3.27.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.27.5",
4
+ "version": "3.27.9",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -1,5 +1,5 @@
1
1
  import Logger from '../logger';
2
- import { IProvide } from '../../utils/index';
2
+ import { IProvide } from '../../utils';
3
3
  export default class Events implements IProvide<any> {
4
4
  #private;
5
5
  constructor({ logger }: {
@@ -1,4 +1,4 @@
1
- import { LoggerLayerEnum, x } from '../../utils/index';
1
+ import { LoggerLayerEnum, x } from '../../utils';
2
2
  export default class Events {
3
3
  #logger;
4
4
  constructor({ logger }) {
@@ -1,5 +1,6 @@
1
1
  import Configs from "../configs";
2
2
  import System from "../system";
3
+ import KafkaForCoreSeeder from "./seeder";
3
4
  import { IProvide } from "../../utils";
4
5
  type KafkaInstanceType = {
5
6
  clientId: string;
@@ -15,13 +16,13 @@ export default class KafkaForCore implements IProvide<any> {
15
16
  private brokers;
16
17
  private username;
17
18
  private password;
18
- constructor({ system, configs, }: {
19
+ constructor({ system, configs, kafkaForCoreSeeder }: {
19
20
  system: System;
20
21
  configs: Configs;
22
+ kafkaForCoreSeeder: KafkaForCoreSeeder;
21
23
  });
22
24
  onInit: ({}: {}) => Promise<void>;
23
25
  kafkaLoader: ({ clientId, brokers, username, password }: KafkaInstanceType) => Promise<any>;
24
- private initializeKafkaInfrastructure;
25
26
  sendMessage: (topic: string, message: any) => Promise<void>;
26
27
  get producer(): any;
27
28
  get instance(): any;
@@ -7,10 +7,12 @@ export default class KafkaForCore {
7
7
  username = null;
8
8
  password = null;
9
9
  #system;
10
+ #kafkaForCoreSeeder;
10
11
  #configs;
11
- constructor({ system, configs, }) {
12
+ constructor({ system, configs, kafkaForCoreSeeder }) {
12
13
  this.#system = system;
13
14
  this.#configs = configs;
15
+ this.#kafkaForCoreSeeder = kafkaForCoreSeeder;
14
16
  }
15
17
  onInit = async ({}) => {
16
18
  this.clientId = this.#configs.all?.kafkaClientId;
@@ -32,7 +34,7 @@ export default class KafkaForCore {
32
34
  username: this.username,
33
35
  password: this.password
34
36
  });
35
- await this.initializeKafkaInfrastructure();
37
+ await this.#kafkaForCoreSeeder.initializeKafkaInfrastructure();
36
38
  };
37
39
  kafkaLoader = async ({ clientId, brokers, username, password }) => {
38
40
  const kafkaConfig = {
@@ -62,47 +64,6 @@ export default class KafkaForCore {
62
64
  console.error(`Kafka ${error}`);
63
65
  }
64
66
  };
65
- initializeKafkaInfrastructure = async () => {
66
- const { kafkaBrokers, kafkaTopics, isKafkaPassive, } = {
67
- isKafkaPassive: process.env.IS_KAFKA_PASSIVE === 'true' ? true : false,
68
- kafkaBrokers: process.env?.KAFKA_BROKERS,
69
- kafkaTopics: [
70
- ...new Set([
71
- ...this.#configs.all.kafkaTopics
72
- ])
73
- ],
74
- };
75
- if (isKafkaPassive === true || !kafkaBrokers)
76
- return;
77
- const brokers = String(kafkaBrokers).split(",").map(b => b.trim());
78
- const topicsToCreate = Array.isArray(kafkaTopics) ? kafkaTopics : [];
79
- if (topicsToCreate.length === 0)
80
- return;
81
- const admin = this._instance.admin();
82
- try {
83
- await admin.connect();
84
- const existingTopics = await admin.listTopics();
85
- const newTopics = topicsToCreate
86
- .filter(topic => !existingTopics.includes(topic))
87
- .map(topic => ({
88
- topic,
89
- numPartitions: 1,
90
- replicationFactor: 1
91
- }));
92
- if (newTopics.length > 0) {
93
- await admin.createTopics({
94
- waitForLeaders: true,
95
- topics: newTopics
96
- });
97
- }
98
- }
99
- catch (error) {
100
- // => Error
101
- }
102
- finally {
103
- await admin.disconnect();
104
- }
105
- };
106
67
  // => Helpers
107
68
  sendMessage = async (topic, message) => {
108
69
  if (!this.producer)
@@ -0,0 +1,13 @@
1
+ import { Kafka } from "kafkajs";
2
+ import Configs from "source/loader/configs";
3
+ import { Logger } from "../../index";
4
+ export default class KafkaForCoreSeeder {
5
+ #private;
6
+ constructor({ logger, kafka, cache, configs, }: {
7
+ logger: Logger;
8
+ kafka: Kafka;
9
+ cache: Cache;
10
+ configs: Configs;
11
+ });
12
+ initializeKafkaInfrastructure: () => Promise<void>;
13
+ }
@@ -0,0 +1,52 @@
1
+ export default class KafkaForCoreSeeder {
2
+ #configs;
3
+ #kafka;
4
+ #cache;
5
+ #logger;
6
+ constructor({ logger, kafka, cache, configs, }) {
7
+ this.#logger = logger;
8
+ this.#kafka = kafka;
9
+ this.#cache = cache;
10
+ this.#configs = configs;
11
+ }
12
+ initializeKafkaInfrastructure = async () => {
13
+ const { kafkaBrokers, kafkaTopics, isKafkaPassive, } = {
14
+ isKafkaPassive: process.env.IS_KAFKA_PASSIVE === 'true' ? true : false,
15
+ kafkaBrokers: process.env?.KAFKA_BROKERS,
16
+ kafkaTopics: [
17
+ ...new Set([
18
+ ...this.#configs.all.kafkaTopics
19
+ ])
20
+ ],
21
+ };
22
+ if (isKafkaPassive === true || !kafkaBrokers)
23
+ return;
24
+ const topicsToCreate = Array.isArray(kafkaTopics) ? kafkaTopics : [];
25
+ if (topicsToCreate.length === 0)
26
+ return;
27
+ const admin = this.#kafka.admin();
28
+ try {
29
+ await admin.connect();
30
+ const existingTopics = await admin.listTopics();
31
+ const newTopics = topicsToCreate
32
+ .filter(topic => !existingTopics.includes(topic))
33
+ .map(topic => ({
34
+ topic,
35
+ numPartitions: 1,
36
+ replicationFactor: 1
37
+ }));
38
+ if (newTopics.length > 0) {
39
+ await admin.createTopics({
40
+ waitForLeaders: true,
41
+ topics: newTopics
42
+ });
43
+ }
44
+ }
45
+ catch (error) {
46
+ // => Error
47
+ }
48
+ finally {
49
+ await admin.disconnect();
50
+ }
51
+ };
52
+ }
@@ -1,4 +1,4 @@
1
- import { IProvide } from '../../utils/index';
1
+ import { IProvide } from '../../utils';
2
2
  export declare abstract class BaseService {
3
3
  }
4
4
  export default abstract class Service extends BaseService implements IProvide<any> {