kafka-crab-js 1.0.0 → 1.0.1
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/index.d.ts +14 -37
- package/index.js +12 -50
- package/js-binding.d.ts +2 -0
- package/kafka-stream-readable.js +73 -0
- package/package.json +5 -4
package/index.d.ts
CHANGED
|
@@ -3,57 +3,35 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export class KafkaClient {
|
|
5
5
|
/**
|
|
6
|
-
* Creates a
|
|
7
|
-
* @param {
|
|
6
|
+
* Creates a KafkaClient instance
|
|
7
|
+
* @param {KafkaConfiguration} config - The Kafka configuration object
|
|
8
|
+
* @throws {Error} If the configuration is invalid
|
|
8
9
|
*/
|
|
9
10
|
constructor(config: KafkaConfiguration);
|
|
10
11
|
kafkaConfiguration: KafkaConfiguration;
|
|
11
12
|
kafkaClientConfig: KafkaClientConfig;
|
|
12
13
|
/**
|
|
13
14
|
* Creates a KafkaProducer instance
|
|
14
|
-
* @param {
|
|
15
|
-
* @returns {KafkaProducer}
|
|
15
|
+
* @param {ProducerConfiguration} [producerConfiguration] - Optional producer configuration
|
|
16
|
+
* @returns {KafkaProducer} A KafkaProducer instance
|
|
16
17
|
*/
|
|
17
|
-
createProducer(producerConfiguration
|
|
18
|
+
createProducer(producerConfiguration?: ProducerConfiguration | undefined): KafkaProducer;
|
|
18
19
|
/**
|
|
19
20
|
* Creates a KafkaConsumer instance
|
|
20
|
-
* @param {ConsumerConfiguration
|
|
21
|
-
* @returns {KafkaConsumer}
|
|
21
|
+
* @param {ConsumerConfiguration} consumerConfiguration - Consumer configuration
|
|
22
|
+
* @returns {KafkaConsumer} A KafkaConsumer instance
|
|
23
|
+
* @throws {Error} If the configuration is invalid
|
|
22
24
|
*/
|
|
23
25
|
createConsumer(consumerConfiguration: ConsumerConfiguration): KafkaConsumer;
|
|
24
26
|
/**
|
|
25
27
|
* Creates a KafkaStreamReadable instance
|
|
26
|
-
* @param {
|
|
27
|
-
* @returns {KafkaStreamReadable}
|
|
28
|
+
* @param {ConsumerConfiguration} consumerConfiguration - Consumer configuration
|
|
29
|
+
* @returns {KafkaStreamReadable} A KafkaStreamReadable instance
|
|
30
|
+
* @throws {Error} If the configuration is invalid
|
|
28
31
|
*/
|
|
29
32
|
createStreamConsumer(consumerConfiguration: ConsumerConfiguration): KafkaStreamReadable;
|
|
30
33
|
}
|
|
31
|
-
|
|
32
|
-
* KafkaStreamReadable class
|
|
33
|
-
* @extends Readable
|
|
34
|
-
*/
|
|
35
|
-
export class KafkaStreamReadable extends Readable {
|
|
36
|
-
/**
|
|
37
|
-
* Creates a KafkaStreamReadable instance
|
|
38
|
-
* @param { KafkaConsumer } kafkaConsumer
|
|
39
|
-
*/
|
|
40
|
-
constructor(kafkaConsumer: KafkaConsumer);
|
|
41
|
-
kafkaConsumer: KafkaConsumer;
|
|
42
|
-
/**
|
|
43
|
-
* Subscribes to topics
|
|
44
|
-
* @param {string | Array<TopicPartitionConfig>} topics
|
|
45
|
-
* @returns
|
|
46
|
-
*/
|
|
47
|
-
subscribe(topics: string | Array<TopicPartitionConfig>): Promise<void>;
|
|
48
|
-
/**
|
|
49
|
-
* Unsubscribe from topics
|
|
50
|
-
*/
|
|
51
|
-
unsubscribe(): void;
|
|
52
|
-
/**
|
|
53
|
-
* The internal method called by the Readable stream to fetch data
|
|
54
|
-
*/
|
|
55
|
-
_read(): Promise<void>;
|
|
56
|
-
}
|
|
34
|
+
import { KafkaStreamReadable } from "./kafka-stream-readable";
|
|
57
35
|
import { ProducerConfiguration } from "./js-binding";
|
|
58
36
|
import { ConsumerConfiguration } from "./js-binding";
|
|
59
37
|
import { KafkaConfiguration } from "./js-binding";
|
|
@@ -64,5 +42,4 @@ import { SecurityProtocol } from "./js-binding";
|
|
|
64
42
|
import { KafkaClientConfig } from "./js-binding";
|
|
65
43
|
import { TopicPartitionConfig } from "./js-binding";
|
|
66
44
|
import { KafkaProducer } from "./js-binding";
|
|
67
|
-
|
|
68
|
-
export { ProducerConfiguration, ConsumerConfiguration, KafkaConfiguration, KafkaConsumer, CommitMode, PartitionPosition, SecurityProtocol, KafkaClientConfig, TopicPartitionConfig, KafkaProducer };
|
|
45
|
+
export { KafkaStreamReadable, ProducerConfiguration, ConsumerConfiguration, KafkaConfiguration, KafkaConsumer, CommitMode, PartitionPosition, SecurityProtocol, KafkaClientConfig, TopicPartitionConfig, KafkaProducer };
|
package/index.js
CHANGED
|
@@ -13,56 +13,16 @@ const {
|
|
|
13
13
|
KafkaProducer,
|
|
14
14
|
} = require('./js-binding')
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
* KafkaStreamReadable class
|
|
18
|
-
* @extends Readable
|
|
19
|
-
*/
|
|
20
|
-
class KafkaStreamReadable extends Readable {
|
|
21
|
-
/**
|
|
22
|
-
* Creates a KafkaStreamReadable instance
|
|
23
|
-
* @param { KafkaConsumer } kafkaConsumer
|
|
24
|
-
*/
|
|
25
|
-
constructor(kafkaConsumer) {
|
|
26
|
-
super({ objectMode: true })
|
|
27
|
-
this.kafkaConsumer = kafkaConsumer
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Subscribes to topics
|
|
32
|
-
* @param {string | Array<TopicPartitionConfig>} topics
|
|
33
|
-
* @returns
|
|
34
|
-
*/
|
|
35
|
-
async subscribe(topics) {
|
|
36
|
-
return this.kafkaConsumer.subscribe(topics)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Unsubscribe from topics
|
|
41
|
-
*/
|
|
42
|
-
unsubscribe() {
|
|
43
|
-
this.kafkaConsumer.unsubscribe()
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* The internal method called by the Readable stream to fetch data
|
|
48
|
-
*/
|
|
49
|
-
async _read() {
|
|
50
|
-
const message = await this.kafkaConsumer.recv() // Call the napi-rs method
|
|
51
|
-
if (message) {
|
|
52
|
-
this.push(message) // Push message into the stream
|
|
53
|
-
} else {
|
|
54
|
-
this.push(null) // No more data, end of stream
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
16
|
+
const { KafkaStreamReadable } = require('./kafka-stream-readable')
|
|
58
17
|
|
|
59
18
|
/**
|
|
60
19
|
* KafkaClient class
|
|
61
20
|
*/
|
|
62
21
|
class KafkaClient {
|
|
63
22
|
/**
|
|
64
|
-
* Creates a
|
|
65
|
-
* @param {
|
|
23
|
+
* Creates a KafkaClient instance
|
|
24
|
+
* @param {KafkaConfiguration} config - The Kafka configuration object
|
|
25
|
+
* @throws {Error} If the configuration is invalid
|
|
66
26
|
*/
|
|
67
27
|
constructor(config) {
|
|
68
28
|
this.kafkaConfiguration = config
|
|
@@ -71,8 +31,8 @@ class KafkaClient {
|
|
|
71
31
|
|
|
72
32
|
/**
|
|
73
33
|
* Creates a KafkaProducer instance
|
|
74
|
-
* @param {
|
|
75
|
-
* @returns {KafkaProducer}
|
|
34
|
+
* @param {ProducerConfiguration} [producerConfiguration] - Optional producer configuration
|
|
35
|
+
* @returns {KafkaProducer} A KafkaProducer instance
|
|
76
36
|
*/
|
|
77
37
|
createProducer(producerConfiguration) {
|
|
78
38
|
if (producerConfiguration) {
|
|
@@ -83,8 +43,9 @@ class KafkaClient {
|
|
|
83
43
|
|
|
84
44
|
/**
|
|
85
45
|
* Creates a KafkaConsumer instance
|
|
86
|
-
* @param {ConsumerConfiguration
|
|
87
|
-
* @returns {KafkaConsumer}
|
|
46
|
+
* @param {ConsumerConfiguration} consumerConfiguration - Consumer configuration
|
|
47
|
+
* @returns {KafkaConsumer} A KafkaConsumer instance
|
|
48
|
+
* @throws {Error} If the configuration is invalid
|
|
88
49
|
*/
|
|
89
50
|
createConsumer(consumerConfiguration) {
|
|
90
51
|
return this.kafkaClientConfig.createConsumer(consumerConfiguration)
|
|
@@ -92,8 +53,9 @@ class KafkaClient {
|
|
|
92
53
|
|
|
93
54
|
/**
|
|
94
55
|
* Creates a KafkaStreamReadable instance
|
|
95
|
-
* @param {
|
|
96
|
-
* @returns {KafkaStreamReadable}
|
|
56
|
+
* @param {ConsumerConfiguration} consumerConfiguration - Consumer configuration
|
|
57
|
+
* @returns {KafkaStreamReadable} A KafkaStreamReadable instance
|
|
58
|
+
* @throws {Error} If the configuration is invalid
|
|
97
59
|
*/
|
|
98
60
|
createStreamConsumer(consumerConfiguration) {
|
|
99
61
|
return new KafkaStreamReadable(this.kafkaClientConfig.createConsumer(consumerConfiguration))
|
package/js-binding.d.ts
CHANGED
|
@@ -89,6 +89,8 @@ export interface ProducerConfiguration {
|
|
|
89
89
|
}
|
|
90
90
|
export declare class KafkaConsumer {
|
|
91
91
|
subscribe(topicConfigs: string | Array<TopicPartitionConfig>): Promise<void>
|
|
92
|
+
pause(): void
|
|
93
|
+
resume(): void
|
|
92
94
|
unsubscribe(): void
|
|
93
95
|
seek(topic: string, partition: number, offsetModel: OffsetModel, timeout?: number | undefined | null): void
|
|
94
96
|
recv(): Promise<Message>
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
const { Readable } = require('stream')
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
KafkaConsumer,
|
|
5
|
+
TopicPartitionConfig,
|
|
6
|
+
} = require('./js-binding')
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* KafkaStreamReadable class
|
|
10
|
+
* @extends Readable
|
|
11
|
+
*/
|
|
12
|
+
class KafkaStreamReadable extends Readable {
|
|
13
|
+
/**
|
|
14
|
+
* Creates a KafkaStreamReadable instance
|
|
15
|
+
* @param { KafkaConsumer } kafkaConsumer - The Kafka consumer instance
|
|
16
|
+
*/
|
|
17
|
+
constructor(kafkaConsumer) {
|
|
18
|
+
super({ objectMode: true })
|
|
19
|
+
if (!kafkaConsumer) {
|
|
20
|
+
throw new Error('A valid KafkaConsumer instance is required.')
|
|
21
|
+
}
|
|
22
|
+
this.kafkaConsumer = kafkaConsumer
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Subscribes to topics
|
|
27
|
+
* @param {string | Array<TopicPartitionConfig>} topics - Topics to subscribe to
|
|
28
|
+
* @returns {Promise<void>} - A promise that resolves when the subscription is successful
|
|
29
|
+
*/
|
|
30
|
+
async subscribe(topics) {
|
|
31
|
+
if (!topics || (Array.isArray(topics) && topics.length === 0)) {
|
|
32
|
+
throw new Error('Topics must be a non-empty string or array.')
|
|
33
|
+
}
|
|
34
|
+
await this.kafkaConsumer.subscribe(topics)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Unsubscribe from topics
|
|
39
|
+
* @returns {void}
|
|
40
|
+
*/
|
|
41
|
+
unsubscribe() {
|
|
42
|
+
this.kafkaConsumer.unsubscribe()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns the raw Kafka consumer
|
|
47
|
+
* @returns {KafkaConsumer} The Kafka consumer instance
|
|
48
|
+
*/
|
|
49
|
+
rawConsumer() {
|
|
50
|
+
return this.kafkaConsumer
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Internal method called by the Readable stream to fetch data
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
async _read() {
|
|
58
|
+
try {
|
|
59
|
+
const message = await this.kafkaConsumer.recv() // Call the napi-rs method
|
|
60
|
+
if (message) {
|
|
61
|
+
this.push(message) // Push message into the stream
|
|
62
|
+
} else {
|
|
63
|
+
this.push(null) // No more data, end of stream
|
|
64
|
+
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
this.destroy(error)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = {
|
|
72
|
+
KafkaStreamReadable,
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kafka-crab-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"module": "commonjs",
|
|
7
7
|
"files": [
|
|
8
8
|
"index.d.ts",
|
|
9
9
|
"index.js",
|
|
10
|
+
"kafka-stream-readable.js",
|
|
10
11
|
"js-binding.js",
|
|
11
12
|
"js-binding.d.ts"
|
|
12
13
|
],
|
|
@@ -60,8 +61,8 @@
|
|
|
60
61
|
},
|
|
61
62
|
"packageManager": "pnpm@9.14.2",
|
|
62
63
|
"optionalDependencies": {
|
|
63
|
-
"kafka-crab-js-darwin-arm64": "1.0.
|
|
64
|
-
"kafka-crab-js-darwin-x64": "1.0.
|
|
65
|
-
"kafka-crab-js-linux-x64-gnu": "1.0.
|
|
64
|
+
"kafka-crab-js-darwin-arm64": "1.0.1",
|
|
65
|
+
"kafka-crab-js-darwin-x64": "1.0.1",
|
|
66
|
+
"kafka-crab-js-linux-x64-gnu": "1.0.1"
|
|
66
67
|
}
|
|
67
68
|
}
|