kafka-crab-js 1.0.0 → 1.0.2
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 +4 -68
- package/index.js +14 -116
- package/js-binding.d.ts +5 -3
- package/js-binding.js +15 -14
- package/kafka-client.d.ts +34 -0
- package/kafka-client.js +48 -0
- package/kafka-stream-readable.d.ts +31 -0
- package/kafka-stream-readable.js +67 -0
- package/package.json +14 -7
package/index.d.ts
CHANGED
|
@@ -1,68 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
/**
|
|
6
|
-
* Creates a KafkaStreamReadable instance
|
|
7
|
-
* @param { KafkaConfiguration } config
|
|
8
|
-
*/
|
|
9
|
-
constructor(config: KafkaConfiguration);
|
|
10
|
-
kafkaConfiguration: KafkaConfiguration;
|
|
11
|
-
kafkaClientConfig: KafkaClientConfig;
|
|
12
|
-
/**
|
|
13
|
-
* Creates a KafkaProducer instance
|
|
14
|
-
* @param { ProducerConfiguration | undefined } producerConfiguration
|
|
15
|
-
* @returns {KafkaProducer}
|
|
16
|
-
*/
|
|
17
|
-
createProducer(producerConfiguration: ProducerConfiguration | undefined): KafkaProducer;
|
|
18
|
-
/**
|
|
19
|
-
* Creates a KafkaConsumer instance
|
|
20
|
-
* @param {ConsumerConfiguration } consumerConfiguration
|
|
21
|
-
* @returns {KafkaConsumer}
|
|
22
|
-
*/
|
|
23
|
-
createConsumer(consumerConfiguration: ConsumerConfiguration): KafkaConsumer;
|
|
24
|
-
/**
|
|
25
|
-
* Creates a KafkaStreamReadable instance
|
|
26
|
-
* @param { ConsumerConfiguration } consumerConfiguration
|
|
27
|
-
* @returns {KafkaStreamReadable}
|
|
28
|
-
*/
|
|
29
|
-
createStreamConsumer(consumerConfiguration: ConsumerConfiguration): KafkaStreamReadable;
|
|
30
|
-
}
|
|
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
|
-
}
|
|
57
|
-
import { ProducerConfiguration } from "./js-binding";
|
|
58
|
-
import { ConsumerConfiguration } from "./js-binding";
|
|
59
|
-
import { KafkaConfiguration } from "./js-binding";
|
|
60
|
-
import { KafkaConsumer } from "./js-binding";
|
|
61
|
-
import { CommitMode } from "./js-binding";
|
|
62
|
-
import { PartitionPosition } from "./js-binding";
|
|
63
|
-
import { SecurityProtocol } from "./js-binding";
|
|
64
|
-
import { KafkaClientConfig } from "./js-binding";
|
|
65
|
-
import { TopicPartitionConfig } from "./js-binding";
|
|
66
|
-
import { KafkaProducer } from "./js-binding";
|
|
67
|
-
import { Readable } from "stream";
|
|
68
|
-
export { ProducerConfiguration, ConsumerConfiguration, KafkaConfiguration, KafkaConsumer, CommitMode, PartitionPosition, SecurityProtocol, KafkaClientConfig, TopicPartitionConfig, KafkaProducer };
|
|
1
|
+
export type { ConsumerConfiguration, KafkaConfiguration, KafkaCrabError, Message, MessageProducer, OffsetModel, PartitionOffset, ProducerConfiguration, ProducerRecord, RecordMetadata, RetryStrategy, TopicPartitionConfig, } from './js-binding';
|
|
2
|
+
export { CommitMode, KafkaClientConfig, KafkaConsumer, KafkaProducer, PartitionPosition, SecurityProtocol, } from './js-binding';
|
|
3
|
+
export { KafkaClient } from './kafka-client';
|
|
4
|
+
export { KafkaStreamReadable } from './kafka-stream-readable';
|
package/index.js
CHANGED
|
@@ -1,116 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* KafkaClient class
|
|
61
|
-
*/
|
|
62
|
-
class KafkaClient {
|
|
63
|
-
/**
|
|
64
|
-
* Creates a KafkaStreamReadable instance
|
|
65
|
-
* @param { KafkaConfiguration } config
|
|
66
|
-
*/
|
|
67
|
-
constructor(config) {
|
|
68
|
-
this.kafkaConfiguration = config
|
|
69
|
-
this.kafkaClientConfig = new KafkaClientConfig(config)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Creates a KafkaProducer instance
|
|
74
|
-
* @param { ProducerConfiguration | undefined } producerConfiguration
|
|
75
|
-
* @returns {KafkaProducer}
|
|
76
|
-
*/
|
|
77
|
-
createProducer(producerConfiguration) {
|
|
78
|
-
if (producerConfiguration) {
|
|
79
|
-
return this.kafkaClientConfig.createProducer(producerConfiguration)
|
|
80
|
-
}
|
|
81
|
-
return this.kafkaClientConfig.createProducer({})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Creates a KafkaConsumer instance
|
|
86
|
-
* @param {ConsumerConfiguration } consumerConfiguration
|
|
87
|
-
* @returns {KafkaConsumer}
|
|
88
|
-
*/
|
|
89
|
-
createConsumer(consumerConfiguration) {
|
|
90
|
-
return this.kafkaClientConfig.createConsumer(consumerConfiguration)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Creates a KafkaStreamReadable instance
|
|
95
|
-
* @param { ConsumerConfiguration } consumerConfiguration
|
|
96
|
-
* @returns {KafkaStreamReadable}
|
|
97
|
-
*/
|
|
98
|
-
createStreamConsumer(consumerConfiguration) {
|
|
99
|
-
return new KafkaStreamReadable(this.kafkaClientConfig.createConsumer(consumerConfiguration))
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
module.exports = {
|
|
104
|
-
KafkaClient,
|
|
105
|
-
KafkaStreamReadable,
|
|
106
|
-
ProducerConfiguration,
|
|
107
|
-
ConsumerConfiguration,
|
|
108
|
-
KafkaConfiguration,
|
|
109
|
-
KafkaConsumer,
|
|
110
|
-
CommitMode,
|
|
111
|
-
PartitionPosition,
|
|
112
|
-
SecurityProtocol,
|
|
113
|
-
KafkaClientConfig,
|
|
114
|
-
TopicPartitionConfig,
|
|
115
|
-
KafkaProducer,
|
|
116
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KafkaStreamReadable = exports.KafkaClient = exports.SecurityProtocol = exports.PartitionPosition = exports.KafkaProducer = exports.KafkaConsumer = exports.KafkaClientConfig = exports.CommitMode = void 0;
|
|
4
|
+
var js_binding_1 = require("./js-binding");
|
|
5
|
+
Object.defineProperty(exports, "CommitMode", { enumerable: true, get: function () { return js_binding_1.CommitMode; } });
|
|
6
|
+
Object.defineProperty(exports, "KafkaClientConfig", { enumerable: true, get: function () { return js_binding_1.KafkaClientConfig; } });
|
|
7
|
+
Object.defineProperty(exports, "KafkaConsumer", { enumerable: true, get: function () { return js_binding_1.KafkaConsumer; } });
|
|
8
|
+
Object.defineProperty(exports, "KafkaProducer", { enumerable: true, get: function () { return js_binding_1.KafkaProducer; } });
|
|
9
|
+
Object.defineProperty(exports, "PartitionPosition", { enumerable: true, get: function () { return js_binding_1.PartitionPosition; } });
|
|
10
|
+
Object.defineProperty(exports, "SecurityProtocol", { enumerable: true, get: function () { return js_binding_1.SecurityProtocol; } });
|
|
11
|
+
var kafka_client_1 = require("./kafka-client");
|
|
12
|
+
Object.defineProperty(exports, "KafkaClient", { enumerable: true, get: function () { return kafka_client_1.KafkaClient; } });
|
|
13
|
+
var kafka_stream_readable_1 = require("./kafka-stream-readable");
|
|
14
|
+
Object.defineProperty(exports, "KafkaStreamReadable", { enumerable: true, get: function () { return kafka_stream_readable_1.KafkaStreamReadable; } });
|
package/js-binding.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface RetryStrategy {
|
|
|
13
13
|
}
|
|
14
14
|
export enum CommitMode {
|
|
15
15
|
Sync = 0,
|
|
16
|
-
Async = 1
|
|
16
|
+
Async = 1,
|
|
17
17
|
}
|
|
18
18
|
export interface ConsumerConfiguration {
|
|
19
19
|
groupId: string
|
|
@@ -25,7 +25,7 @@ export interface ConsumerConfiguration {
|
|
|
25
25
|
export enum PartitionPosition {
|
|
26
26
|
Beginning = 'Beginning',
|
|
27
27
|
End = 'End',
|
|
28
|
-
Stored = 'Stored'
|
|
28
|
+
Stored = 'Stored',
|
|
29
29
|
}
|
|
30
30
|
export interface OffsetModel {
|
|
31
31
|
offset?: number
|
|
@@ -44,7 +44,7 @@ export enum SecurityProtocol {
|
|
|
44
44
|
Plaintext = 'Plaintext',
|
|
45
45
|
Ssl = 'Ssl',
|
|
46
46
|
SaslPlaintext = 'SaslPlaintext',
|
|
47
|
-
SaslSsl = 'SaslSsl'
|
|
47
|
+
SaslSsl = 'SaslSsl',
|
|
48
48
|
}
|
|
49
49
|
export interface KafkaConfiguration {
|
|
50
50
|
brokers: string
|
|
@@ -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>
|
package/js-binding.js
CHANGED
|
@@ -63,7 +63,7 @@ switch (platform) {
|
|
|
63
63
|
switch (arch) {
|
|
64
64
|
case 'x64':
|
|
65
65
|
localFileExisted = existsSync(
|
|
66
|
-
join(__dirname, 'kafka-crab-js.win32-x64-msvc.node')
|
|
66
|
+
join(__dirname, 'kafka-crab-js.win32-x64-msvc.node'),
|
|
67
67
|
)
|
|
68
68
|
try {
|
|
69
69
|
if (localFileExisted) {
|
|
@@ -77,7 +77,7 @@ switch (platform) {
|
|
|
77
77
|
break
|
|
78
78
|
case 'ia32':
|
|
79
79
|
localFileExisted = existsSync(
|
|
80
|
-
join(__dirname, 'kafka-crab-js.win32-ia32-msvc.node')
|
|
80
|
+
join(__dirname, 'kafka-crab-js.win32-ia32-msvc.node'),
|
|
81
81
|
)
|
|
82
82
|
try {
|
|
83
83
|
if (localFileExisted) {
|
|
@@ -91,7 +91,7 @@ switch (platform) {
|
|
|
91
91
|
break
|
|
92
92
|
case 'arm64':
|
|
93
93
|
localFileExisted = existsSync(
|
|
94
|
-
join(__dirname, 'kafka-crab-js.win32-arm64-msvc.node')
|
|
94
|
+
join(__dirname, 'kafka-crab-js.win32-arm64-msvc.node'),
|
|
95
95
|
)
|
|
96
96
|
try {
|
|
97
97
|
if (localFileExisted) {
|
|
@@ -132,7 +132,7 @@ switch (platform) {
|
|
|
132
132
|
break
|
|
133
133
|
case 'arm64':
|
|
134
134
|
localFileExisted = existsSync(
|
|
135
|
-
join(__dirname, 'kafka-crab-js.darwin-arm64.node')
|
|
135
|
+
join(__dirname, 'kafka-crab-js.darwin-arm64.node'),
|
|
136
136
|
)
|
|
137
137
|
try {
|
|
138
138
|
if (localFileExisted) {
|
|
@@ -168,7 +168,7 @@ switch (platform) {
|
|
|
168
168
|
case 'x64':
|
|
169
169
|
if (isMusl()) {
|
|
170
170
|
localFileExisted = existsSync(
|
|
171
|
-
join(__dirname, 'kafka-crab-js.linux-x64-musl.node')
|
|
171
|
+
join(__dirname, 'kafka-crab-js.linux-x64-musl.node'),
|
|
172
172
|
)
|
|
173
173
|
try {
|
|
174
174
|
if (localFileExisted) {
|
|
@@ -181,7 +181,7 @@ switch (platform) {
|
|
|
181
181
|
}
|
|
182
182
|
} else {
|
|
183
183
|
localFileExisted = existsSync(
|
|
184
|
-
join(__dirname, 'kafka-crab-js.linux-x64-gnu.node')
|
|
184
|
+
join(__dirname, 'kafka-crab-js.linux-x64-gnu.node'),
|
|
185
185
|
)
|
|
186
186
|
try {
|
|
187
187
|
if (localFileExisted) {
|
|
@@ -197,7 +197,7 @@ switch (platform) {
|
|
|
197
197
|
case 'arm64':
|
|
198
198
|
if (isMusl()) {
|
|
199
199
|
localFileExisted = existsSync(
|
|
200
|
-
join(__dirname, 'kafka-crab-js.linux-arm64-musl.node')
|
|
200
|
+
join(__dirname, 'kafka-crab-js.linux-arm64-musl.node'),
|
|
201
201
|
)
|
|
202
202
|
try {
|
|
203
203
|
if (localFileExisted) {
|
|
@@ -210,7 +210,7 @@ switch (platform) {
|
|
|
210
210
|
}
|
|
211
211
|
} else {
|
|
212
212
|
localFileExisted = existsSync(
|
|
213
|
-
join(__dirname, 'kafka-crab-js.linux-arm64-gnu.node')
|
|
213
|
+
join(__dirname, 'kafka-crab-js.linux-arm64-gnu.node'),
|
|
214
214
|
)
|
|
215
215
|
try {
|
|
216
216
|
if (localFileExisted) {
|
|
@@ -226,7 +226,7 @@ switch (platform) {
|
|
|
226
226
|
case 'arm':
|
|
227
227
|
if (isMusl()) {
|
|
228
228
|
localFileExisted = existsSync(
|
|
229
|
-
join(__dirname, 'kafka-crab-js.linux-arm-musleabihf.node')
|
|
229
|
+
join(__dirname, 'kafka-crab-js.linux-arm-musleabihf.node'),
|
|
230
230
|
)
|
|
231
231
|
try {
|
|
232
232
|
if (localFileExisted) {
|
|
@@ -239,7 +239,7 @@ switch (platform) {
|
|
|
239
239
|
}
|
|
240
240
|
} else {
|
|
241
241
|
localFileExisted = existsSync(
|
|
242
|
-
join(__dirname, 'kafka-crab-js.linux-arm-gnueabihf.node')
|
|
242
|
+
join(__dirname, 'kafka-crab-js.linux-arm-gnueabihf.node'),
|
|
243
243
|
)
|
|
244
244
|
try {
|
|
245
245
|
if (localFileExisted) {
|
|
@@ -255,7 +255,7 @@ switch (platform) {
|
|
|
255
255
|
case 'riscv64':
|
|
256
256
|
if (isMusl()) {
|
|
257
257
|
localFileExisted = existsSync(
|
|
258
|
-
join(__dirname, 'kafka-crab-js.linux-riscv64-musl.node')
|
|
258
|
+
join(__dirname, 'kafka-crab-js.linux-riscv64-musl.node'),
|
|
259
259
|
)
|
|
260
260
|
try {
|
|
261
261
|
if (localFileExisted) {
|
|
@@ -268,7 +268,7 @@ switch (platform) {
|
|
|
268
268
|
}
|
|
269
269
|
} else {
|
|
270
270
|
localFileExisted = existsSync(
|
|
271
|
-
join(__dirname, 'kafka-crab-js.linux-riscv64-gnu.node')
|
|
271
|
+
join(__dirname, 'kafka-crab-js.linux-riscv64-gnu.node'),
|
|
272
272
|
)
|
|
273
273
|
try {
|
|
274
274
|
if (localFileExisted) {
|
|
@@ -283,7 +283,7 @@ switch (platform) {
|
|
|
283
283
|
break
|
|
284
284
|
case 's390x':
|
|
285
285
|
localFileExisted = existsSync(
|
|
286
|
-
join(__dirname, 'kafka-crab-js.linux-s390x-gnu.node')
|
|
286
|
+
join(__dirname, 'kafka-crab-js.linux-s390x-gnu.node'),
|
|
287
287
|
)
|
|
288
288
|
try {
|
|
289
289
|
if (localFileExisted) {
|
|
@@ -310,7 +310,8 @@ if (!nativeBinding) {
|
|
|
310
310
|
throw new Error(`Failed to load native binding`)
|
|
311
311
|
}
|
|
312
312
|
|
|
313
|
-
const { KafkaConsumer, CommitMode, PartitionPosition, SecurityProtocol, KafkaClientConfig, KafkaProducer } =
|
|
313
|
+
const { KafkaConsumer, CommitMode, PartitionPosition, SecurityProtocol, KafkaClientConfig, KafkaProducer } =
|
|
314
|
+
nativeBinding
|
|
314
315
|
|
|
315
316
|
module.exports.KafkaConsumer = KafkaConsumer
|
|
316
317
|
module.exports.CommitMode = CommitMode
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ConsumerConfiguration, KafkaConfiguration, ProducerConfiguration } from './js-binding.js';
|
|
2
|
+
import { KafkaStreamReadable } from './kafka-stream-readable';
|
|
3
|
+
/**
|
|
4
|
+
* KafkaClient class
|
|
5
|
+
*/
|
|
6
|
+
export declare class KafkaClient {
|
|
7
|
+
private readonly kafkaConfiguration;
|
|
8
|
+
private readonly kafkaClientConfig;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a KafkaClient instance
|
|
11
|
+
* @throws {Error} If the configuration is invalid
|
|
12
|
+
*/
|
|
13
|
+
constructor(kafkaConfiguration: KafkaConfiguration);
|
|
14
|
+
/**
|
|
15
|
+
* Creates a KafkaProducer instance
|
|
16
|
+
* @param {ProducerConfiguration} [producerConfiguration] - Optional producer configuration
|
|
17
|
+
* @returns {KafkaProducer} A KafkaProducer instance
|
|
18
|
+
*/
|
|
19
|
+
createProducer(producerConfiguration: ProducerConfiguration): import("./js-binding.js").KafkaProducer;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a KafkaConsumer instance
|
|
22
|
+
* @param {ConsumerConfiguration} consumerConfiguration - Consumer configuration
|
|
23
|
+
* @returns {KafkaConsumer} A KafkaConsumer instance
|
|
24
|
+
* @throws {Error} If the configuration is invalid
|
|
25
|
+
*/
|
|
26
|
+
createConsumer(consumerConfiguration: ConsumerConfiguration): import("./js-binding.js").KafkaConsumer;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a KafkaStreamReadable instance
|
|
29
|
+
* @param {ConsumerConfiguration} consumerConfiguration - Consumer configuration
|
|
30
|
+
* @returns {KafkaStreamReadable} A KafkaStreamReadable instance
|
|
31
|
+
* @throws {Error} If the configuration is invalid
|
|
32
|
+
*/
|
|
33
|
+
createStreamConsumer(consumerConfiguration: ConsumerConfiguration): KafkaStreamReadable;
|
|
34
|
+
}
|
package/kafka-client.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KafkaClient = void 0;
|
|
4
|
+
const js_binding_js_1 = require("./js-binding.js");
|
|
5
|
+
const kafka_stream_readable_1 = require("./kafka-stream-readable");
|
|
6
|
+
/**
|
|
7
|
+
* KafkaClient class
|
|
8
|
+
*/
|
|
9
|
+
class KafkaClient {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a KafkaClient instance
|
|
12
|
+
* @throws {Error} If the configuration is invalid
|
|
13
|
+
*/
|
|
14
|
+
constructor(kafkaConfiguration) {
|
|
15
|
+
this.kafkaConfiguration = kafkaConfiguration;
|
|
16
|
+
this.kafkaClientConfig = new js_binding_js_1.KafkaClientConfig(kafkaConfiguration);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a KafkaProducer instance
|
|
20
|
+
* @param {ProducerConfiguration} [producerConfiguration] - Optional producer configuration
|
|
21
|
+
* @returns {KafkaProducer} A KafkaProducer instance
|
|
22
|
+
*/
|
|
23
|
+
createProducer(producerConfiguration) {
|
|
24
|
+
if (producerConfiguration) {
|
|
25
|
+
return this.kafkaClientConfig.createProducer(producerConfiguration);
|
|
26
|
+
}
|
|
27
|
+
return this.kafkaClientConfig.createProducer({});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates a KafkaConsumer instance
|
|
31
|
+
* @param {ConsumerConfiguration} consumerConfiguration - Consumer configuration
|
|
32
|
+
* @returns {KafkaConsumer} A KafkaConsumer instance
|
|
33
|
+
* @throws {Error} If the configuration is invalid
|
|
34
|
+
*/
|
|
35
|
+
createConsumer(consumerConfiguration) {
|
|
36
|
+
return this.kafkaClientConfig.createConsumer(consumerConfiguration);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates a KafkaStreamReadable instance
|
|
40
|
+
* @param {ConsumerConfiguration} consumerConfiguration - Consumer configuration
|
|
41
|
+
* @returns {KafkaStreamReadable} A KafkaStreamReadable instance
|
|
42
|
+
* @throws {Error} If the configuration is invalid
|
|
43
|
+
*/
|
|
44
|
+
createStreamConsumer(consumerConfiguration) {
|
|
45
|
+
return new kafka_stream_readable_1.KafkaStreamReadable(this.kafkaClientConfig.createConsumer(consumerConfiguration));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.KafkaClient = KafkaClient;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Readable } from 'stream';
|
|
2
|
+
import { KafkaConsumer, TopicPartitionConfig } from './js-binding';
|
|
3
|
+
/**
|
|
4
|
+
* KafkaStreamReadable class
|
|
5
|
+
* @extends Readable
|
|
6
|
+
*/
|
|
7
|
+
export declare class KafkaStreamReadable extends Readable {
|
|
8
|
+
private readonly kafkaConsumer;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a KafkaStreamReadable instance
|
|
11
|
+
*/
|
|
12
|
+
constructor(kafkaConsumer: KafkaConsumer);
|
|
13
|
+
/**
|
|
14
|
+
* Subscribes to topics
|
|
15
|
+
*/
|
|
16
|
+
subscribe(topics: string | Array<TopicPartitionConfig>): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Unsubscribe from topics
|
|
19
|
+
*/
|
|
20
|
+
unsubscribe(): void;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the raw Kafka consumer
|
|
23
|
+
* @returns {KafkaConsumer} The Kafka consumer instance
|
|
24
|
+
*/
|
|
25
|
+
rawConsumer(): KafkaConsumer;
|
|
26
|
+
/**
|
|
27
|
+
* Internal method called by the Readable stream to fetch data
|
|
28
|
+
* @private
|
|
29
|
+
*/
|
|
30
|
+
_read(): Promise<void>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KafkaStreamReadable = void 0;
|
|
4
|
+
const stream_1 = require("stream");
|
|
5
|
+
/**
|
|
6
|
+
* KafkaStreamReadable class
|
|
7
|
+
* @extends Readable
|
|
8
|
+
*/
|
|
9
|
+
class KafkaStreamReadable extends stream_1.Readable {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a KafkaStreamReadable instance
|
|
12
|
+
*/
|
|
13
|
+
constructor(kafkaConsumer) {
|
|
14
|
+
super({ objectMode: true });
|
|
15
|
+
this.kafkaConsumer = kafkaConsumer;
|
|
16
|
+
if (!kafkaConsumer) {
|
|
17
|
+
throw new Error('A valid KafkaConsumer instance is required.');
|
|
18
|
+
}
|
|
19
|
+
this.kafkaConsumer = kafkaConsumer;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Subscribes to topics
|
|
23
|
+
*/
|
|
24
|
+
async subscribe(topics) {
|
|
25
|
+
if (!topics || (Array.isArray(topics) && topics.length === 0)) {
|
|
26
|
+
throw new Error('Topics must be a non-empty string or array.');
|
|
27
|
+
}
|
|
28
|
+
await this.kafkaConsumer.subscribe(topics);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Unsubscribe from topics
|
|
32
|
+
*/
|
|
33
|
+
unsubscribe() {
|
|
34
|
+
this.kafkaConsumer.unsubscribe();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Returns the raw Kafka consumer
|
|
38
|
+
* @returns {KafkaConsumer} The Kafka consumer instance
|
|
39
|
+
*/
|
|
40
|
+
rawConsumer() {
|
|
41
|
+
return this.kafkaConsumer;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Internal method called by the Readable stream to fetch data
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
47
|
+
async _read() {
|
|
48
|
+
try {
|
|
49
|
+
const message = await this.kafkaConsumer.recv(); // Call the napi-rs method
|
|
50
|
+
if (message) {
|
|
51
|
+
this.push(message); // Push message into the stream
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
this.push(null); // No more data, end of stream
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (error instanceof Error) {
|
|
59
|
+
this.destroy(error);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
this.destroy();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.KafkaStreamReadable = KafkaStreamReadable;
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kafka-crab-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
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-client.js",
|
|
11
|
+
"kafka-client.d.ts",
|
|
12
|
+
"kafka-stream-readable.js",
|
|
13
|
+
"kafka-stream-readable.d.ts",
|
|
10
14
|
"js-binding.js",
|
|
11
15
|
"js-binding.d.ts"
|
|
12
16
|
],
|
|
@@ -42,15 +46,18 @@
|
|
|
42
46
|
"ava": "6.2.0",
|
|
43
47
|
"dprint": "^0.47.5",
|
|
44
48
|
"nanoid": "5.0.8",
|
|
45
|
-
"typescript": "5.6.3"
|
|
49
|
+
"typescript": "5.6.3",
|
|
50
|
+
"copyfiles": "^2.4.1",
|
|
51
|
+
"@types/copyfiles": "^2.4.4",
|
|
52
|
+
"rimraf": "^6.0.1"
|
|
46
53
|
},
|
|
47
54
|
"ava": {
|
|
48
55
|
"timeout": "3m"
|
|
49
56
|
},
|
|
50
57
|
"scripts": {
|
|
51
58
|
"artifacts": "napi artifacts",
|
|
52
|
-
"build": "napi build --js js-binding.js --dts js-binding.d.ts --no-const-enum --platform --release",
|
|
53
|
-
"postbuild": "tsc",
|
|
59
|
+
"build": "napi build --js js-src/js-binding.js --dts js-src/js-binding.d.ts --no-const-enum --platform --release",
|
|
60
|
+
"postbuild": "rimraf dist && tsc && copyfiles -f js-src/js-binding.d.ts js-src/js-binding.js ./dist/*.js ./dist/*.d.ts .",
|
|
54
61
|
"build:debug": "napi build --platform",
|
|
55
62
|
"prepublishOnly": "napi prepublish -t npm",
|
|
56
63
|
"test": "ava",
|
|
@@ -60,8 +67,8 @@
|
|
|
60
67
|
},
|
|
61
68
|
"packageManager": "pnpm@9.14.2",
|
|
62
69
|
"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.
|
|
70
|
+
"kafka-crab-js-darwin-arm64": "1.0.2",
|
|
71
|
+
"kafka-crab-js-darwin-x64": "1.0.2",
|
|
72
|
+
"kafka-crab-js-linux-x64-gnu": "1.0.2"
|
|
66
73
|
}
|
|
67
74
|
}
|