corebasic 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/libs/kafka.js +18 -6
- package/package.json +1 -1
package/libs/kafka.js
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
const { Kafka } = require('kafkajs')
|
|
1
|
+
const { Kafka, logLevel } = require('kafkajs')
|
|
2
2
|
|
|
3
3
|
let kafka, producer, admin
|
|
4
4
|
|
|
5
|
+
let appName
|
|
5
6
|
let topicPrefix = ''
|
|
6
7
|
|
|
7
8
|
function start(arg) {
|
|
8
|
-
|
|
9
|
+
appName = process.env.APP_DEPLOYMENT_NAME ?? arg?.clientId
|
|
10
|
+
topicPrefix = appName ? `${topicPrefix}.` : ''
|
|
11
|
+
appName = appName ?? 'app'
|
|
9
12
|
kafka = new Kafka({
|
|
10
|
-
|
|
13
|
+
logLevel: arg?.logLevel ?? logLevel.INFO,
|
|
14
|
+
clientId: arg?.clientId ?? (process.env.APP_DEPLOYMENT_NAME ?? appName),
|
|
11
15
|
brokers: arg?.brokers ?? ['redpanda-0.redpanda.redpanda.svc.cluster.local:9093'], // ['107.155.108.78:9092'] ['127.0.0.1:29092']
|
|
12
|
-
sasl: {
|
|
16
|
+
sasl: arg?.sasl == false ? undefined : {
|
|
13
17
|
mechanism: 'SCRAM-SHA-512',
|
|
14
18
|
username: process.env.KAFKA_USERNAME,
|
|
15
19
|
password: process.env.KAFKA_PASSWORD
|
|
@@ -32,7 +36,10 @@ async function createTopic(topic, partition, replicas) {
|
|
|
32
36
|
let consumers = []
|
|
33
37
|
|
|
34
38
|
const start_consumer = async function (topic, groupId, callback) {
|
|
35
|
-
|
|
39
|
+
callback = typeof groupId === "string" ? callback : groupId
|
|
40
|
+
groupId = typeof groupId === "string" ? groupId : appName
|
|
41
|
+
|
|
42
|
+
const consumer = kafka.consumer({ groupId: groupId })
|
|
36
43
|
consumers.push(consumer)
|
|
37
44
|
|
|
38
45
|
await consumer.connect()
|
|
@@ -41,7 +48,11 @@ const start_consumer = async function (topic, groupId, callback) {
|
|
|
41
48
|
await consumer.run({
|
|
42
49
|
eachMessage: async ({ topic, partition, message }) => {
|
|
43
50
|
// callback(topic, message.value.toString(), partition) // toString() returns array so won't parse if json.
|
|
44
|
-
|
|
51
|
+
try {
|
|
52
|
+
callback(topic, JSON.parse(message.value), partition)
|
|
53
|
+
} catch (ex) {
|
|
54
|
+
callback(topic, message.value, partition)
|
|
55
|
+
}
|
|
45
56
|
},
|
|
46
57
|
})
|
|
47
58
|
}
|
|
@@ -92,6 +103,7 @@ module.exports = {
|
|
|
92
103
|
createTopic,
|
|
93
104
|
send,
|
|
94
105
|
receive,
|
|
106
|
+
logLevel,
|
|
95
107
|
}
|
|
96
108
|
|
|
97
109
|
|