corebasic 1.0.129 → 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
@@ -79,8 +79,8 @@ export const send = async (meta, feature, data, params) => {
79
79
  let appids = {}
80
80
  async function announce() {
81
81
  let exp_features = {}
82
- for (let [key, {api}] of Object.entries(features))
83
- 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}
84
84
 
85
85
  await Messaging.subscribe(`${process.env.REDIS_CHANNEL_PREFIX}_SLYP_FEATURES_LIST`, async (message, channel) => {
86
86
  let msg = JSON.parse(message)
@@ -89,6 +89,7 @@ async function announce() {
89
89
  SLYP_FEATURES_LIST = {...SLYP_FEATURES_LIST, ...msg}
90
90
  delete SLYP_FEATURES_LIST.uid
91
91
  await Messaging.produce(`${process.env.REDIS_CHANNEL_PREFIX}_SLYP_FEATURES_LIST`, JSON.stringify({...exp_features, uid: appId }))
92
+ await subscribe()
92
93
  }
93
94
  })
94
95
  await Messaging.produce(`${process.env.REDIS_CHANNEL_PREFIX}_SLYP_FEATURES_LIST`, JSON.stringify({...exp_features, uid: appId }))
@@ -212,6 +213,8 @@ export const start = async (app, url, file) => {
212
213
  })
213
214
  }
214
215
 
216
+ await subscribe()
217
+
215
218
  // Transaction status api
216
219
  app.get('/transactions/:id', async (req, res) => { // Front end calls this as a regular feature call. so `req.meta` exists
217
220
  try {
@@ -253,3 +256,61 @@ const commandAction = async (req, res, topic) => {
253
256
  res.json({ data: { txn: message.txn, success: true, status: "Queued", featureQueued: true } })
254
257
  }
255
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.129",
4
+ "version": "1.0.130",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {