@jcbuisson/express-x 1.5.5 → 1.5.7
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/package.json +1 -1
- package/src/channels.mjs +32 -0
- package/src/common-hooks.mjs +8 -7
- package/src/context.mjs +11 -1
- package/src/index.mjs +6 -1
- package/src/server.mjs +2 -25
package/package.json
CHANGED
package/src/channels.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
export async function getChannelConnections(channelName) {
|
|
3
|
+
const connections = await app.service('Connection').findMany({})
|
|
4
|
+
return connections.filter(connection => {
|
|
5
|
+
const channelNames = JSON.parse(connection.channelNames)
|
|
6
|
+
return channelNames.includes(channelName)
|
|
7
|
+
})
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function addChannelToConnection(connection, channelName) {
|
|
11
|
+
const channelNames = JSON.parse(connection.channelNames)
|
|
12
|
+
if (!channelNames.includes(channelName)) channelNames.push(channelName)
|
|
13
|
+
await app.service('Connection').update({
|
|
14
|
+
where: { id: connection.id },
|
|
15
|
+
data: { channelNames: JSON.stringify(channelNames) },
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function removeChannelFromConnection(connection, channelName) {
|
|
20
|
+
const channelNames = JSON.parse(connection.channelNames).filter(name => name !== channelName)
|
|
21
|
+
await app.service('Connection').update({
|
|
22
|
+
where: { id },
|
|
23
|
+
data: { channelNames: JSON.stringify(channelNames) },
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function resetConnectionChannels(connectionId) {
|
|
28
|
+
await app.service('Connection').update({
|
|
29
|
+
where: { id: connectionId },
|
|
30
|
+
data: { channelNames: '[]' },
|
|
31
|
+
})
|
|
32
|
+
}
|
package/src/common-hooks.mjs
CHANGED
|
@@ -31,11 +31,12 @@ export function protect(field) {
|
|
|
31
31
|
|
|
32
32
|
export async function isAuthenticated(context) {
|
|
33
33
|
if (context.transport !== 'ws') return
|
|
34
|
-
// extract
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (
|
|
34
|
+
// extract userId from connection data
|
|
35
|
+
const userId = await getConnectionDataItem(context, 'userId')
|
|
36
|
+
if (!userId) throw Error(`Not authenticated, userId ${userId}`)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const isExpired = (delay) => async (context) => {
|
|
40
|
+
if (context.transport !== 'ws') return
|
|
41
|
+
// TODO
|
|
41
42
|
}
|
package/src/context.mjs
CHANGED
|
@@ -5,6 +5,16 @@ export async function getConnectionIP(context) {
|
|
|
5
5
|
return connection?.clientIP
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
export async function resetConnectionIP(context) {
|
|
9
|
+
const id = context.params.connectionId
|
|
10
|
+
await context.app.service('Connection')._update({
|
|
11
|
+
where: { id },
|
|
12
|
+
data: {
|
|
13
|
+
clientIP: ''
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
8
18
|
export async function getConnectionDataItem(context, key) {
|
|
9
19
|
const id = context.params.connectionId
|
|
10
20
|
const connection = await context.app.service('Connection')._findUnique({ where: { id }})
|
|
@@ -17,7 +27,7 @@ export async function setConnectionDataItem(context, key, value) {
|
|
|
17
27
|
const connection = await context.app.service('Connection')._findUnique({ where: { id }})
|
|
18
28
|
const data = JSON.parse(connection.data)
|
|
19
29
|
data[key] = value
|
|
20
|
-
await context.app.service('Connection').
|
|
30
|
+
await context.app.service('Connection')._update({
|
|
21
31
|
where: { id },
|
|
22
32
|
data: {
|
|
23
33
|
data: JSON.stringify(data)
|
package/src/index.mjs
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
|
|
2
2
|
import { expressX } from './server.mjs'
|
|
3
3
|
import { hashPassword, protect, isAuthenticated } from './common-hooks.mjs'
|
|
4
|
-
import { getConnectionIP, getConnectionDataItem, setConnectionDataItem, removeConnectionDataItem, resetConnectionData } from './context.mjs'
|
|
4
|
+
import { getConnectionIP, resetConnectionIP, getConnectionDataItem, setConnectionDataItem, removeConnectionDataItem, resetConnectionData } from './context.mjs'
|
|
5
|
+
import { resetConnectionChannels } from './channels.mjs'
|
|
5
6
|
|
|
6
7
|
export {
|
|
7
8
|
expressX,
|
|
8
9
|
|
|
9
10
|
getConnectionIP,
|
|
11
|
+
resetConnectionIP,
|
|
12
|
+
|
|
13
|
+
resetConnectionChannels,
|
|
14
|
+
|
|
10
15
|
getConnectionDataItem,
|
|
11
16
|
setConnectionDataItem,
|
|
12
17
|
removeConnectionDataItem,
|
package/src/server.mjs
CHANGED
|
@@ -3,6 +3,8 @@ import http from 'http'
|
|
|
3
3
|
import { Server } from "socket.io"
|
|
4
4
|
import express from 'express'
|
|
5
5
|
|
|
6
|
+
import { getChannelConnections, addChannelToConnection, removeChannelFromConnection } from './channels.mjs'
|
|
7
|
+
|
|
6
8
|
/*
|
|
7
9
|
* Enhance `app` express application with services and real-time features
|
|
8
10
|
*/
|
|
@@ -43,30 +45,6 @@ export function expressX(prisma, options = {}) {
|
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
async function getChannelConnections(channelName) {
|
|
47
|
-
const connections = await app.service('Connection').findMany({})
|
|
48
|
-
return connections.filter(connection => {
|
|
49
|
-
const channelNames = JSON.parse(connection.channelNames)
|
|
50
|
-
return channelNames.includes(channelName)
|
|
51
|
-
})
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async function addChannelToConnection(connection, channelName) {
|
|
55
|
-
const channelNames = JSON.parse(connection.channelNames)
|
|
56
|
-
if (!channelNames.includes(channelName)) channelNames.push(channelName)
|
|
57
|
-
await app.service('Connection').update({
|
|
58
|
-
where: { id: connection.id },
|
|
59
|
-
data: { channelNames: JSON.stringify(channelNames) },
|
|
60
|
-
})
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async function removeChannelFromConnection(connection, channelName) {
|
|
64
|
-
const channelNames = JSON.parse(connection.channelNames).filter(name => name !== channelName)
|
|
65
|
-
await app.service('Connection').update({
|
|
66
|
-
where: { id },
|
|
67
|
-
data: { channelNames: JSON.stringify(channelNames) },
|
|
68
|
-
})
|
|
69
|
-
}
|
|
70
48
|
|
|
71
49
|
// logging function - a winston logger must be configured first
|
|
72
50
|
app.log = (severity, message) => {
|
|
@@ -402,7 +380,6 @@ export function expressX(prisma, options = {}) {
|
|
|
402
380
|
app.log('verbose', `publish channels ${service.name} ${action} ${channelNames}`)
|
|
403
381
|
for (const channelName of channelNames) {
|
|
404
382
|
app.log('verbose', `service-event ${service.name} ${action} ${channelName}`)
|
|
405
|
-
// const connectionList = Object.values(app.connections).filter(cnx => cnx.channelNames.has(channelName))
|
|
406
383
|
const connectionList = getChannelConnections(channelName)
|
|
407
384
|
for (const connection of connectionList) {
|
|
408
385
|
app.log('verbose', `emit to ${connection.id} ${service.name} ${action} ${result}`)
|