corebasic 1.0.72 → 1.0.74
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/messaging.js +18 -9
- package/libs/utils.js +20 -3
- package/package.json +1 -1
package/libs/messaging.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import axios from 'axios'
|
|
3
3
|
import { createClient } from 'redis';
|
|
4
|
+
import * as Utils from './utils.js'
|
|
4
5
|
|
|
5
6
|
// url: 'redis://alice:foobared@localhost:6380'
|
|
6
7
|
let consumer, publisher
|
|
@@ -11,21 +12,28 @@ let users_pool = {
|
|
|
11
12
|
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
async function disconnect(user) {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
async function disconnect(user, uid) {
|
|
16
|
+
if (users_pool[user].listeners[uid]) {
|
|
17
|
+
await consumer.unsubscribe(user, users_pool[user].listeners[uid]);
|
|
18
|
+
users_pool[user].listeners.count --
|
|
19
|
+
}
|
|
20
|
+
delete users_pool[user].listeners[uid]
|
|
21
|
+
if (!users_pool[user].listeners.count)
|
|
22
|
+
delete users_pool[user]
|
|
23
|
+
|
|
17
24
|
// await consumer.unsubscribe(user);
|
|
18
25
|
}
|
|
19
|
-
async function connect({user,req,res}) {
|
|
26
|
+
async function connect({user,req,res, uid}) {
|
|
20
27
|
const listener = (message, channel) => {
|
|
21
28
|
if (users_pool[user]) {
|
|
22
29
|
try {message = JSON.parse(message)} catch {}
|
|
23
30
|
users_pool[user].res.write(JSON.stringify({message, channel}) + "\n")
|
|
24
31
|
}
|
|
25
32
|
}
|
|
26
|
-
users_pool[user] = {req,res,
|
|
33
|
+
users_pool[user] = users_pool[user] ?? {req,res,listeners: {count: 0}}
|
|
34
|
+
users_pool[user].listeners[uid] = listener
|
|
27
35
|
await consumer.subscribe(user, listener);
|
|
28
|
-
|
|
36
|
+
users_pool[user].listeners.count ++
|
|
29
37
|
}
|
|
30
38
|
|
|
31
39
|
export async function start(app) {
|
|
@@ -37,9 +45,10 @@ export async function start(app) {
|
|
|
37
45
|
consumer.on('error', err => console.log('Redis Client Error', err));
|
|
38
46
|
|
|
39
47
|
app.get('/messages/:user', async (req, res) => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
req.on("
|
|
48
|
+
let uid = Utils.uid()
|
|
49
|
+
await connect({user: req.params.user, req, res, uid})
|
|
50
|
+
req.on("close", async () => await disconnect(req.params.user, uid)) // request closed unexpectedly
|
|
51
|
+
req.on("end", async () => await disconnect(req.params.user, uid)) // request ended normally
|
|
43
52
|
})
|
|
44
53
|
}
|
|
45
54
|
|
package/libs/utils.js
CHANGED
|
@@ -163,7 +163,7 @@ export async function fileToJson(path, file) {
|
|
|
163
163
|
|
|
164
164
|
|
|
165
165
|
function execute(pipeline, errfn) {
|
|
166
|
-
startPipeline(pipeline, 0, undefined, errfn)
|
|
166
|
+
startPipeline(pipeline, 0, undefined, errfn)
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
function startPipeline(pipeline, index, data, errfn) {
|
|
@@ -177,9 +177,26 @@ function startPipeline(pipeline, index, data, errfn) {
|
|
|
177
177
|
// S3 Object Storage
|
|
178
178
|
// -----------------
|
|
179
179
|
|
|
180
|
-
export async function getUrl(path, expiry = 3600) { // expiry default: 1 Hour
|
|
180
|
+
export async function getUrl(path, expiry = 3600) { // expiry default: 1 Hour
|
|
181
181
|
return await getSignedUrl(S3, new GetObjectCommand({ Bucket: bucketName, Key: path }), { expiresIn: expiry })
|
|
182
182
|
}
|
|
183
|
-
export async function putUrl(path, expiry = 3600) { // expiry default: 1 Hour
|
|
183
|
+
export async function putUrl(path, expiry = 3600) { // expiry default: 1 Hour
|
|
184
184
|
return await getSignedUrl(S3, new PutObjectCommand({ Bucket: bucketName, Key: path }), { expiresIn: expiry })
|
|
185
185
|
}
|
|
186
|
+
|
|
187
|
+
// ---------------------------
|
|
188
|
+
// Express Middleware Exclude
|
|
189
|
+
// ---------------------------
|
|
190
|
+
export const excludeMiddleware = function(middleware, ...paths) {
|
|
191
|
+
return function(req, res, next) {
|
|
192
|
+
const pathCheck = paths.some(path => {
|
|
193
|
+
let url = req.path.split('/')
|
|
194
|
+
let cmp = path.split('/')
|
|
195
|
+
if (url.length !== cmp.length)
|
|
196
|
+
return false
|
|
197
|
+
let f = cmp.map((p,index) => p.startsWith(":") ? url[index] : p).join('/')
|
|
198
|
+
return f === req.path
|
|
199
|
+
})
|
|
200
|
+
pathCheck ? next() : middleware(req, res, next);
|
|
201
|
+
}
|
|
202
|
+
}
|