corebasic 1.0.67 → 1.0.69
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/index.js +3 -1
- package/libs/dipper.js +17 -2
- package/libs/features.js +7 -4
- package/libs/messaging.js +50 -0
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import * as Utils from './libs/utils.js'
|
|
|
7
7
|
import * as Session from './libs/session.js'
|
|
8
8
|
import * as Auth from './libs/auth.js'
|
|
9
9
|
import * as Features from './libs/features.js'
|
|
10
|
+
import * as Messaging from './libs/messaging.js'
|
|
10
11
|
|
|
11
12
|
export {
|
|
12
13
|
Elabase,
|
|
@@ -15,5 +16,6 @@ export {
|
|
|
15
16
|
Utils,
|
|
16
17
|
Session,
|
|
17
18
|
Auth,
|
|
18
|
-
Features
|
|
19
|
+
Features,
|
|
20
|
+
Messaging
|
|
19
21
|
}
|
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(
|
|
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(
|
|
73
|
+
result.push(await postDip(`http://${url}/execute`, arg))
|
|
59
74
|
}
|
|
60
75
|
}
|
|
61
76
|
|
package/libs/features.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as Elabase from './elabase.js'
|
|
2
2
|
import * as Kafka from './kafka.js'
|
|
3
3
|
import * as Utils from './utils.js'
|
|
4
|
+
import * as Messaging from './messaging.js'
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
let features = {}
|
|
@@ -8,14 +9,14 @@ let apis = {}
|
|
|
8
9
|
|
|
9
10
|
export const start = async (app, url, file) => {
|
|
10
11
|
features = await Utils.fileToJson(url, file)
|
|
11
|
-
|
|
12
|
+
|
|
12
13
|
|
|
13
14
|
// Registering handlers
|
|
14
15
|
for (let name in features) {
|
|
15
16
|
let feature = features[name]
|
|
16
17
|
apis[feature.api] = apis[feature.api] ?? {}
|
|
17
18
|
apis[feature.api][name] = feature
|
|
18
|
-
|
|
19
|
+
|
|
19
20
|
name = name.split('.')
|
|
20
21
|
let handler = name.pop()
|
|
21
22
|
name = `src/${name.join('/')}`
|
|
@@ -75,7 +76,7 @@ export const start = async (app, url, file) => {
|
|
|
75
76
|
} catch {
|
|
76
77
|
console.warn(`Feature: ${message.feature}, error in executing handler during Kafka.receive(topic: ${topic})`)
|
|
77
78
|
}
|
|
78
|
-
await timer(10000);
|
|
79
|
+
await timer(10000);
|
|
79
80
|
}
|
|
80
81
|
})
|
|
81
82
|
}
|
|
@@ -92,8 +93,10 @@ export const start = async (app, url, file) => {
|
|
|
92
93
|
res.status(500).json({ message: "Failed to retreive data" })
|
|
93
94
|
}
|
|
94
95
|
})
|
|
96
|
+
|
|
97
|
+
try { await Messaging.start(app) } catch { console.log('Messaging failed to start. Maybe missing redis') }
|
|
95
98
|
}
|
|
96
|
-
|
|
99
|
+
|
|
97
100
|
const commandAction = async (req, res, topic) => {
|
|
98
101
|
let txn = req.body.txn ?? Util.uid()
|
|
99
102
|
let _id = req.body.data._id ?? txn
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
|
|
2
|
+
import axios from 'axios'
|
|
3
|
+
import { createClient } from 'redis';
|
|
4
|
+
|
|
5
|
+
// url: 'redis://alice:foobared@localhost:6380'
|
|
6
|
+
let consumer, publisher
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
let users_pool = {
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function disconnect(user) {
|
|
15
|
+
delete users_pool[user]
|
|
16
|
+
await consumer.unsubscribe(user);
|
|
17
|
+
}
|
|
18
|
+
async function connect({user,req,res}) {
|
|
19
|
+
users_pool[user] = {req,res}
|
|
20
|
+
await consumer.subscribe(user, (message, channel) => {
|
|
21
|
+
if (users_pool[user])
|
|
22
|
+
users_pool[user].res.write(JSON.stringify({message, channel}) + "\n")
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function start(app) {
|
|
27
|
+
consumer = createClient({ url: 'redis://localhost:6380' });
|
|
28
|
+
await consumer.connect();
|
|
29
|
+
publisher = createClient({ url: 'redis://localhost:6380' });
|
|
30
|
+
await publisher.connect();
|
|
31
|
+
|
|
32
|
+
consumer.on('error', err => console.log('Redis Client Error', err));
|
|
33
|
+
|
|
34
|
+
app.get('/messages/:user', async (req, res) => {
|
|
35
|
+
await connect({user: req.params.user, req, res})
|
|
36
|
+
req.on("close", async () => await disconnect(req.params.user)) // request closed unexpectedly
|
|
37
|
+
req.on("end", async () => await disconnect(req.params.user)) // request ended normally
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
export async function publish(channel, message) {
|
|
43
|
+
if (publisher)
|
|
44
|
+
await publisher.publish(channel, typeof message === "object" ? JSON.stringify(message) : message);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "corebasic",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.69",
|
|
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
|
}
|