@ccci/micro-server 1.0.148 → 1.0.149

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.
@@ -8,12 +8,14 @@ export type ApplicationOptionType = {
8
8
  websocketDir?: string;
9
9
  enableRedis?: boolean;
10
10
  enableMinio?: boolean;
11
+ enableKafka?: boolean;
11
12
  enableS3?: boolean;
12
13
  enableWebSocket?: boolean;
13
14
  enableFirebase?: boolean;
14
15
  enableEmail?: boolean;
15
- kafkaProducer?: boolean;
16
- kafkaConsumer?: boolean;
16
+ kafkaBroker?: string[];
17
+ consumersPath?: string;
18
+ producersPath?: string;
17
19
  minioHost?: string;
18
20
  minioPort?: number;
19
21
  minioAccessKey?: string;
@@ -1,12 +1,41 @@
1
- import { EachMessagePayload } from 'kafkajs';
1
+ import { type EachMessagePayload } from 'kafkajs';
2
+ /**
3
+ * BaseConsumer class to handle Kafka consumer operations such as connecting,
4
+ * subscribing to topics, consuming messages, and disconnecting.
5
+ */
2
6
  export declare class BaseConsumer {
3
7
  private kafka;
4
8
  private consumer;
5
9
  private groupId;
10
+ /**
11
+ * @param clientId - The client ID used to identify the consumer instance.
12
+ * @param brokers - An array of Kafka brokers to connect to.
13
+ * @param groupId - The consumer group ID to which this consumer belongs.
14
+ */
6
15
  constructor(clientId: string, brokers: string[], groupId: string);
16
+ /**
17
+ * Connects the consumer to the Kafka cluster.
18
+ * This method establishes the connection between the consumer and the Kafka brokers.
19
+ */
7
20
  connect(): Promise<void>;
21
+ /**
22
+ * Subscribes the consumer to a Kafka topic.
23
+ *
24
+ * @param topic - The Kafka topic to subscribe to.
25
+ * @param fromBeginning - Whether to start reading from the beginning of the topic (default: true).
26
+ */
8
27
  subscribe(topic: string, fromBeginning?: boolean): Promise<void>;
28
+ /**
29
+ * Starts consuming messages from the Kafka topic.
30
+ * The provided `eachMessageHandler` will be executed for each consumed message.
31
+ *
32
+ * @param eachMessageHandler - A callback function that processes each consumed message.
33
+ */
9
34
  run(eachMessageHandler: (payload: EachMessagePayload) => Promise<void>): Promise<void>;
35
+ /**
36
+ * Disconnects the consumer from the Kafka cluster.
37
+ * This method gracefully shuts down the consumer connection.
38
+ */
10
39
  disconnect(): Promise<void>;
11
40
  }
12
41
  //# sourceMappingURL=BaseConsumer.d.ts.map
@@ -1,12 +1,45 @@
1
+ /**
2
+ * BaseProducer class to handle Kafka producer operations such as connecting,
3
+ * producing messages, and disconnecting.
4
+ * This class uses the Singleton pattern to ensure only one instance of the producer exists.
5
+ */
1
6
  export declare class BaseProducer {
7
+ protected static instance: BaseProducer | null;
2
8
  private kafka;
3
9
  private producer;
10
+ private clientId;
11
+ /**
12
+ * Constructs a new producer instance with the given client ID and Kafka brokers.
13
+ *
14
+ * @param clientId - The client ID to uniquely identify the producer instance.
15
+ * @param brokers - An array of Kafka broker addresses to connect to.
16
+ */
4
17
  constructor(clientId: string, brokers: string[]);
18
+ /**
19
+ * Public method to get the single instance of BaseProducer (Singleton pattern).
20
+ * If no instance exists, it creates a new one. If an instance already exists, it returns the existing one.
21
+ *
22
+ * @param clientId - The client ID used to identify the producer instance.
23
+ * @param brokers - An array of Kafka brokers to connect to.
24
+ * @returns The single instance of the BaseProducer.
25
+ */
26
+ static getInstance(clientId: string, brokers: string[]): BaseProducer;
27
+ /**
28
+ * Connects the producer to the Kafka cluster.
29
+ * This method establishes the connection between the producer and the Kafka brokers.
30
+ */
5
31
  connect(): Promise<void>;
6
- sendMessage(topic: string, messages: {
7
- key: string;
8
- value: string;
9
- }[]): Promise<void>;
32
+ /**
33
+ * Sends a message to a Kafka topic.
34
+ *
35
+ * @param topic - The Kafka topic to which the message will be sent.
36
+ * @param message - The message content to send to the topic.
37
+ */
38
+ produceMessage(topic: string, message: string): Promise<void>;
39
+ /**
40
+ * Disconnects the producer from the Kafka cluster.
41
+ * This method gracefully shuts down the producer connection.
42
+ */
10
43
  disconnect(): Promise<void>;
11
44
  }
12
45
  //# sourceMappingURL=BaseProducer.d.ts.map
@@ -0,0 +1,34 @@
1
+ import { ApplicationOptionType } from '@/types/ApplicationOptionType';
2
+ /**
3
+ * KafkaServer class to manage Kafka consumers and producers.
4
+ * It automatically loads and starts consumers and producers from specified directories.
5
+ */
6
+ export declare class KafkaServer {
7
+ private consumersPath;
8
+ private producersPath;
9
+ private consumers;
10
+ private producers;
11
+ private kafkaBroker;
12
+ /**
13
+ * @param config - Configuration for Kafka.
14
+ */
15
+ constructor(config: ApplicationOptionType);
16
+ /**
17
+ * Starts all consumers automatically by dynamically importing and instantiating consumer classes
18
+ * from the specified consumers directory.
19
+ * Each consumer is connected with a clientId and groupId generated from the file name.
20
+ */
21
+ startConsumers(): Promise<void>;
22
+ /**
23
+ * Starts all producers automatically by dynamically importing and instantiating producer classes
24
+ * from the specified producers directory.
25
+ * Each producer is connected with a clientId generated from the file name.
26
+ */
27
+ startProducers(): Promise<void>;
28
+ /**
29
+ * Disconnects all consumers and producers.
30
+ * This method ensures that all active consumers and producers are disconnected.
31
+ */
32
+ disconnectAll(): Promise<void>;
33
+ }
34
+ //# sourceMappingURL=KafkaServer.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ccci/micro-server",
3
- "version": "1.0.148",
3
+ "version": "1.0.149",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",