corebasic 1.0.50 → 1.0.52
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/features.js +44 -34
- package/package.json +1 -1
package/libs/features.js
CHANGED
|
@@ -1,57 +1,38 @@
|
|
|
1
1
|
import * as Elabase from './elabase.js'
|
|
2
2
|
import * as Kafka from './kafka.js'
|
|
3
|
-
import
|
|
4
|
-
const require = createRequire(import.meta.url);
|
|
3
|
+
import * as Utils from './utils.js'
|
|
5
4
|
|
|
6
5
|
|
|
7
6
|
let features = {}
|
|
8
7
|
let apis = {}
|
|
9
8
|
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
features = require(file)
|
|
9
|
+
export const start = async (app, url, file) => {
|
|
10
|
+
features = await Utils.fileToJson(url, file)
|
|
13
11
|
|
|
14
|
-
|
|
12
|
+
|
|
13
|
+
// Registering handlers
|
|
14
|
+
for (let name in features) {
|
|
15
|
+
let feature = features[name]
|
|
15
16
|
apis[feature.api] = apis[feature.api] ?? {}
|
|
16
17
|
apis[feature.api][name] = feature
|
|
17
18
|
|
|
18
|
-
let featureName = name
|
|
19
|
-
|
|
20
19
|
name = name.split('.')
|
|
21
20
|
let handler = name.pop()
|
|
22
21
|
name = `src/${name.join('/')}`
|
|
23
22
|
|
|
24
23
|
if (name === 'src/transactions/query')
|
|
25
|
-
|
|
24
|
+
continue
|
|
26
25
|
|
|
27
26
|
feature.handler = (await import(`${new URL(`${name}.js`, url).toString().replace('file://', '')}`))[handler]
|
|
27
|
+
}
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
Kafka.receive(`Features.${topic}`, `${featureName}.${topic}`, async (topic, message) => {
|
|
32
|
-
|
|
33
|
-
const timer = ms => new Promise(res => setTimeout(res, ms)) // A promise that resolves after "ms" Milliseconds
|
|
34
|
-
topic = topic.replace('Features.','')
|
|
35
|
-
while (true) {
|
|
36
|
-
try {
|
|
37
|
-
await features[message.feature].handler(topic, message)
|
|
38
|
-
await Elabase.insert("Features.txns", {_id: message.txn, status: "Processed"})
|
|
39
|
-
break
|
|
40
|
-
} catch {
|
|
41
|
-
console.warn(`Feature: ${featureName}, error in executing handler during Kafka.receive(topic: ${topic})`)
|
|
42
|
-
}
|
|
43
|
-
await timer(10000);
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
Object.entries(apis).forEach(([api,features]) => {
|
|
29
|
+
// Registering apis
|
|
30
|
+
for (let api in apis) {
|
|
51
31
|
let method = api.split(' ')[0].toLowerCase()
|
|
52
32
|
let url = api.split(' ')[1]
|
|
53
33
|
if (api === 'GET /transactions/:id')
|
|
54
|
-
|
|
34
|
+
continue
|
|
35
|
+
|
|
55
36
|
app[method](url, async (req, res) => {
|
|
56
37
|
let feature = features[req.body.feature]
|
|
57
38
|
let topic = feature.topic
|
|
@@ -68,9 +49,38 @@ export function start(app, url) {
|
|
|
68
49
|
res.status(err.status ?? 500).json(err)
|
|
69
50
|
}
|
|
70
51
|
})
|
|
71
|
-
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
// Get unique kafka topics list from features
|
|
56
|
+
let kafkaTopics = []
|
|
57
|
+
for (let name in features) {
|
|
58
|
+
let feature = features[name]
|
|
59
|
+
if (feature.api.split(' ')[0].toLowerCase() === "get" )
|
|
60
|
+
continue
|
|
61
|
+
kafkaTopics = [... new Set(kafkaTopics.concat([feature.topic]))]
|
|
62
|
+
}
|
|
72
63
|
|
|
64
|
+
// Subscribe to each topic
|
|
65
|
+
for (let topic of kafkaTopics) {
|
|
66
|
+
Kafka.receive(`Features.${topic}`, async (topic, message) => {
|
|
67
|
+
|
|
68
|
+
const timer = ms => new Promise(res => setTimeout(res, ms)) // A promise that resolves after "ms" Milliseconds
|
|
69
|
+
topic = topic.replace('Features.','')
|
|
70
|
+
while (true) {
|
|
71
|
+
try {
|
|
72
|
+
await features[message.feature].handler(topic, message)
|
|
73
|
+
await Elabase.insert("Features.txns", {_id: message.txn, status: "Processed"})
|
|
74
|
+
break
|
|
75
|
+
} catch {
|
|
76
|
+
console.warn(`Feature: ${message.feature}, error in executing handler during Kafka.receive(topic: ${topic})`)
|
|
77
|
+
}
|
|
78
|
+
await timer(10000);
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
}
|
|
73
82
|
|
|
83
|
+
// Transaction status api
|
|
74
84
|
app.get('/transactions/:id', async (req, res) => {
|
|
75
85
|
try {
|
|
76
86
|
let items = await Elabase.query("Features.txns", {_id: req.params.id})
|
|
@@ -90,7 +100,7 @@ const commandAction = async (req, res, topic) => {
|
|
|
90
100
|
try {
|
|
91
101
|
let {data, feature, app, user, client, version} = req.body
|
|
92
102
|
let params = req.params
|
|
93
|
-
await Kafka.send(`Features.${topic}`, { data, params, feature, app, user, client, version, txn, id: _id, date: new Date().getTime() },
|
|
103
|
+
await Kafka.send(`Features.${topic}`, { data, params, feature, app, user, client, version, txn, id: _id, date: new Date().getTime() }, user)
|
|
94
104
|
|
|
95
105
|
} catch {
|
|
96
106
|
throw {status: 500, message: "Failed to queue the transaction"}
|