@jcbuisson/express-x 1.5.13 → 1.5.15
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/common-hooks.mjs +1 -1
- package/src/server.mjs +37 -2
- package/src/channels.mjs +0 -25
package/package.json
CHANGED
package/src/common-hooks.mjs
CHANGED
|
@@ -33,7 +33,7 @@ export async function isAuthenticated(context) {
|
|
|
33
33
|
if (context.transport !== 'ws') return
|
|
34
34
|
// extract userId from connection data
|
|
35
35
|
const userId = await getConnectionDataItem(context, 'userId')
|
|
36
|
-
if (!userId) throw Error(`Not authenticated
|
|
36
|
+
if (!userId) throw Error(`Not authenticated`)
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
export const isExpired = (delay) => async (context) => {
|
package/src/server.mjs
CHANGED
|
@@ -19,8 +19,16 @@ export function expressX(prisma, options = {}) {
|
|
|
19
19
|
|
|
20
20
|
const cnx2Socket = {}
|
|
21
21
|
|
|
22
|
-
function createConnection(clientIP) {
|
|
23
|
-
|
|
22
|
+
async function createConnection(clientIP) {
|
|
23
|
+
const now = new Date();
|
|
24
|
+
const expirationTime = new Date(now.getTime() + 15 * 60000)
|
|
25
|
+
const connection = await app.service('Connection').create({
|
|
26
|
+
data: {
|
|
27
|
+
clientIP,
|
|
28
|
+
expirationTime,
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
return connection
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
function getConnection(id) {
|
|
@@ -31,8 +39,10 @@ export function expressX(prisma, options = {}) {
|
|
|
31
39
|
await app.service('Connection').update({
|
|
32
40
|
where: { id },
|
|
33
41
|
data: {
|
|
42
|
+
clientIP: connection.clientIP,
|
|
34
43
|
channelNames: connection.channelNames,
|
|
35
44
|
data: connection.data,
|
|
45
|
+
expirationTime: connection.expirationTime,
|
|
36
46
|
}
|
|
37
47
|
})
|
|
38
48
|
}
|
|
@@ -394,6 +404,31 @@ export function expressX(prisma, options = {}) {
|
|
|
394
404
|
}
|
|
395
405
|
}
|
|
396
406
|
|
|
407
|
+
async function getChannelConnections(channelName) {
|
|
408
|
+
const connections = await app.service('Connection').findMany({})
|
|
409
|
+
return connections.filter(connection => {
|
|
410
|
+
const channelNames = JSON.parse(connection.channelNames)
|
|
411
|
+
return channelNames.includes(channelName)
|
|
412
|
+
})
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
async function addChannelToConnection(connection, channelName) {
|
|
416
|
+
const channelNames = JSON.parse(connection.channelNames)
|
|
417
|
+
if (!channelNames.includes(channelName)) channelNames.push(channelName)
|
|
418
|
+
await app.service('Connection').update({
|
|
419
|
+
where: { id: connection.id },
|
|
420
|
+
data: { channelNames: JSON.stringify(channelNames) },
|
|
421
|
+
})
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
async function removeChannelFromConnection(connection, channelName) {
|
|
425
|
+
const channelNames = JSON.parse(connection.channelNames).filter(name => name !== channelName)
|
|
426
|
+
await app.service('Connection').update({
|
|
427
|
+
where: { id },
|
|
428
|
+
data: { channelNames: JSON.stringify(channelNames) },
|
|
429
|
+
})
|
|
430
|
+
}
|
|
431
|
+
|
|
397
432
|
function joinChannel(channelName, connection) {
|
|
398
433
|
addChannelToConnection(connection, channelName)
|
|
399
434
|
}
|
package/src/channels.mjs
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
}
|