corebasic 1.0.128 → 1.0.130

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 CHANGED
@@ -39,6 +39,12 @@ let appId = Utils.uid()
39
39
  let SERVICE_ADDRESS = process.env.APP_ENDPOINT || 'http://127.0.0.1:3000'
40
40
 
41
41
 
42
+ export const get = feature => {
43
+ const throwError = () => {throw {message: `Feature ${feature} not found in internal or external list during Features.subscribe call`}}
44
+ let {api, service = SERVICE_ADDRESS, topic} = getFeature(feature) ?? SLYP_FEATURES_LIST[feature] ?? throwError()
45
+ return {api, service, topic}
46
+ }
47
+
42
48
  export const send = async (meta, feature, data, params) => {
43
49
  const throwError = () => {throw {message: `Feature ${feature} not found in internal or external list during inter feature call`}}
44
50
  let {api, service = SERVICE_ADDRESS} = getFeature(feature) ?? SLYP_FEATURES_LIST[feature] ?? throwError()
@@ -73,8 +79,8 @@ export const send = async (meta, feature, data, params) => {
73
79
  let appids = {}
74
80
  async function announce() {
75
81
  let exp_features = {}
76
- for (let [key, {api}] of Object.entries(features))
77
- exp_features[key] = {api, service: SERVICE_ADDRESS}
82
+ for (let [key, {api,subscribe}] of Object.entries(features))
83
+ exp_features[key] = {api, service: SERVICE_ADDRESS, subscribe}
78
84
 
79
85
  await Messaging.subscribe(`${process.env.REDIS_CHANNEL_PREFIX}_SLYP_FEATURES_LIST`, async (message, channel) => {
80
86
  let msg = JSON.parse(message)
@@ -83,6 +89,7 @@ async function announce() {
83
89
  SLYP_FEATURES_LIST = {...SLYP_FEATURES_LIST, ...msg}
84
90
  delete SLYP_FEATURES_LIST.uid
85
91
  await Messaging.produce(`${process.env.REDIS_CHANNEL_PREFIX}_SLYP_FEATURES_LIST`, JSON.stringify({...exp_features, uid: appId }))
92
+ await subscribe()
86
93
  }
87
94
  })
88
95
  await Messaging.produce(`${process.env.REDIS_CHANNEL_PREFIX}_SLYP_FEATURES_LIST`, JSON.stringify({...exp_features, uid: appId }))
@@ -206,6 +213,8 @@ export const start = async (app, url, file) => {
206
213
  })
207
214
  }
208
215
 
216
+ await subscribe()
217
+
209
218
  // Transaction status api
210
219
  app.get('/transactions/:id', async (req, res) => { // Front end calls this as a regular feature call. so `req.meta` exists
211
220
  try {
@@ -247,3 +256,61 @@ const commandAction = async (req, res, topic) => {
247
256
  res.json({ data: { txn: message.txn, success: true, status: "Queued", featureQueued: true } })
248
257
  }
249
258
 
259
+
260
+
261
+ let subscriptions = {
262
+ // "coins.query.hello": "invoices.command.add", // Consumer : Feature of Topic
263
+ }
264
+ let subscribed_consumers = {
265
+ }
266
+ async function subscribe() {
267
+ let Features = {get,send}
268
+
269
+ for (let [key, {subscribe}] of Object.entries(features))
270
+ if (!Utils.isEmpty(subscribe) && subscriptions[key] !== subscribe && subscribed_consumers[key]) {
271
+ try { await subscribed_consumers[key].disconnect() } catch (_) {}
272
+ delete subscribed_consumers[key]
273
+ }
274
+ for (let key in SLYP_FEATURES_LIST)
275
+ if (!Utils.isEmpty(SLYP_FEATURES_LIST[key].subscribe) && subscriptions[key] !== SLYP_FEATURES_LIST[key].subscribe && subscribed_consumers[key]) {
276
+ try { await subscribed_consumers[key].disconnect() } catch (_) {}
277
+ delete subscribed_consumers[key]
278
+ }
279
+
280
+
281
+ let exp_features = {}
282
+ for (let key in SLYP_FEATURES_LIST)
283
+ if (!Utils.isEmpty(SLYP_FEATURES_LIST[key].subscribe)) {
284
+ subscriptions[key] = SLYP_FEATURES_LIST[key].subscribe
285
+ }
286
+ for (let [key, {subscribe}] of Object.entries(features))
287
+ if (!Utils.isEmpty(subscribe)) {
288
+ subscriptions[key] = subscribe
289
+ }
290
+
291
+
292
+ for (let consumer in subscriptions) {
293
+ let publisher = subscriptions[consumer]
294
+ let {topic} = Features.get(publisher)
295
+
296
+ if (subscribed_consumers[consumer])
297
+ continue
298
+
299
+ let kafka_consumer = Kafka.receive(`Features.${topic}`, consumer, async (topic, message) => {
300
+ topic = topic.replace(/^.*Features./,'')
301
+
302
+ const timer = ms => new Promise(res => setTimeout(res, ms)) // A promise that resolves after "ms" Milliseconds
303
+
304
+ while (true) {
305
+ try {
306
+ await Features.send(message.meta, consumer, message.data, message.params)
307
+ break
308
+ } catch {
309
+ console.warn(`Feature: ${consumer}, error in executing handler during Features.subscribe(topic: ${topic})`)
310
+ }
311
+ await timer(10000);
312
+ }
313
+ })
314
+ subscribed_consumers[consumer] = kafka_consumer
315
+ }
316
+ }
package/libs/kafka.js CHANGED
@@ -62,6 +62,7 @@ const start_consumer = async function (topic, groupId, callback) {
62
62
  await consumer.commitOffsets([{ topic, partition, offset: (Number(message.offset) + 1).toString() }]);
63
63
  },
64
64
  })
65
+ return consumer
65
66
  }
66
67
 
67
68
 
@@ -97,7 +98,7 @@ const start_producer = async function (topic, message, key, options) {
97
98
 
98
99
  export const receive = async function(topic, groupId, callback) {
99
100
  topic = topicPrefix + topic
100
- await start_consumer(topic, groupId, callback)
101
+ return (await start_consumer(topic, groupId, callback))
101
102
  }
102
103
 
103
104
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.128",
4
+ "version": "1.0.130",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {