corebasic 1.0.67 → 1.0.68

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/dipper.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import axios from 'axios'
2
+ // import {curly} from 'node-libcurl'
2
3
 
3
4
  function hashStr(str) {
4
5
  let hash = 0;
@@ -35,6 +36,20 @@ function shard(key) {
35
36
 
36
37
  export let shard_hits = {}
37
38
 
39
+ async function postDip(url, arg) {
40
+ return (await axios.post(url, arg)).data
41
+ // curl
42
+ // -----
43
+ // const { data } = await curly.post(url, {
44
+ // postFields: JSON.stringify(arg),
45
+ // httpHeader: [
46
+ // 'Content-Type: application/json',
47
+ // 'Accept: application/json'
48
+ // ],
49
+ // })
50
+ // return data
51
+ }
52
+
38
53
  export default async function dipper(arg) {
39
54
 
40
55
  let result = []
@@ -45,7 +60,7 @@ export default async function dipper(arg) {
45
60
  let result = []
46
61
  for (let url of shards) {
47
62
  shard_hits[url] = (shard_hits[url] ?? 0) + 1
48
- result.push((await axios.post(`http://${url}/execute`, arg)).data)
63
+ result.push(await postDip(`http://${url}/execute`, arg))
49
64
  }
50
65
 
51
66
  } else {
@@ -55,7 +70,7 @@ export default async function dipper(arg) {
55
70
  let key = shard(id)
56
71
  let url = shards[key]
57
72
  shard_hits[url] = (shard_hits[url] ?? 0) + 1
58
- result.push((await axios.post(`http://${url}/execute`, arg)).data)
73
+ result.push(await postDip(`http://${url}/execute`, arg))
59
74
  }
60
75
  }
61
76
 
package/libs/features.js CHANGED
@@ -8,14 +8,14 @@ let apis = {}
8
8
 
9
9
  export const start = async (app, url, file) => {
10
10
  features = await Utils.fileToJson(url, file)
11
-
11
+
12
12
 
13
13
  // Registering handlers
14
14
  for (let name in features) {
15
15
  let feature = features[name]
16
16
  apis[feature.api] = apis[feature.api] ?? {}
17
17
  apis[feature.api][name] = feature
18
-
18
+
19
19
  name = name.split('.')
20
20
  let handler = name.pop()
21
21
  name = `src/${name.join('/')}`
@@ -75,7 +75,7 @@ export const start = async (app, url, file) => {
75
75
  } catch {
76
76
  console.warn(`Feature: ${message.feature}, error in executing handler during Kafka.receive(topic: ${topic})`)
77
77
  }
78
- await timer(10000);
78
+ await timer(10000);
79
79
  }
80
80
  })
81
81
  }
@@ -93,7 +93,7 @@ export const start = async (app, url, file) => {
93
93
  }
94
94
  })
95
95
  }
96
-
96
+
97
97
  const commandAction = async (req, res, topic) => {
98
98
  let txn = req.body.txn ?? Util.uid()
99
99
  let _id = req.body.data._id ?? txn
@@ -0,0 +1,46 @@
1
+
2
+ import axios from 'axios'
3
+ import { createClient } from 'redis';
4
+
5
+ // url: 'redis://alice:foobared@localhost:6380'
6
+ const consumer = createClient({ url: 'redis://localhost:6380' });
7
+ await consumer.connect();
8
+ const publisher = createClient({ url: 'redis://localhost:6380' });
9
+ await publisher.connect();
10
+
11
+ consumer.on('error', err => console.log('Redis Client Error', err));
12
+
13
+
14
+ let users_pool = {
15
+
16
+ }
17
+
18
+ async function disconnect(user) {
19
+ delete users_pool[user]
20
+ await consumer.unsubscribe(user);
21
+ }
22
+ async function connect({user,req,res}) {
23
+ users_pool[user] = {req,res}
24
+ await consumer.subscribe(user, (message, channel) => {
25
+ if (users_pool[user])
26
+ users_pool[user].res.write(JSON.stringify({message, channel}) + "\n")
27
+ });
28
+ }
29
+
30
+ export function start(app) {
31
+ app.get('/messages/:user', async (req, res) => {
32
+ await connect({user: req.params.user, req, res})
33
+ req.on("close", async () => await disconnect(req.params.user)) // request closed unexpectedly
34
+ req.on("end", async () => await disconnect(req.params.user)) // request ended normally
35
+ })
36
+ }
37
+
38
+
39
+ export async function publish(channel, message) {
40
+ await publisher.publish(channel, typeof message === "object" ? JSON.stringify(message) : message);
41
+ }
42
+
43
+
44
+
45
+
46
+
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.67",
4
+ "version": "1.0.68",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {
@@ -15,6 +15,7 @@
15
15
  "axios": "^1.4.0",
16
16
  "jsonminify": "^0.4.2",
17
17
  "jsonwebtoken": "^9.0.1",
18
- "otp-generator": "^4.0.1"
18
+ "otp-generator": "^4.0.1",
19
+ "redis": "^4.6.8"
19
20
  }
20
21
  }