@jcbuisson/express-x 1.5.14 → 1.5.16
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/server.mjs +30 -4
- package/src/channels.mjs +0 -25
package/package.json
CHANGED
package/src/server.mjs
CHANGED
|
@@ -3,8 +3,6 @@ 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
|
-
|
|
8
6
|
/*
|
|
9
7
|
* Enhance `app` express application with services and real-time features
|
|
10
8
|
*/
|
|
@@ -19,15 +17,16 @@ export function expressX(prisma, options = {}) {
|
|
|
19
17
|
|
|
20
18
|
const cnx2Socket = {}
|
|
21
19
|
|
|
22
|
-
function createConnection(clientIP) {
|
|
20
|
+
async function createConnection(clientIP) {
|
|
23
21
|
const now = new Date();
|
|
24
22
|
const expirationTime = new Date(now.getTime() + 15 * 60000)
|
|
25
|
-
|
|
23
|
+
const connection = await app.service('Connection').create({
|
|
26
24
|
data: {
|
|
27
25
|
clientIP,
|
|
28
26
|
expirationTime,
|
|
29
27
|
}
|
|
30
28
|
})
|
|
29
|
+
return connection
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
function getConnection(id) {
|
|
@@ -38,8 +37,10 @@ export function expressX(prisma, options = {}) {
|
|
|
38
37
|
await app.service('Connection').update({
|
|
39
38
|
where: { id },
|
|
40
39
|
data: {
|
|
40
|
+
clientIP: connection.clientIP,
|
|
41
41
|
channelNames: connection.channelNames,
|
|
42
42
|
data: connection.data,
|
|
43
|
+
expirationTime: connection.expirationTime,
|
|
43
44
|
}
|
|
44
45
|
})
|
|
45
46
|
}
|
|
@@ -401,6 +402,31 @@ export function expressX(prisma, options = {}) {
|
|
|
401
402
|
}
|
|
402
403
|
}
|
|
403
404
|
|
|
405
|
+
async function getChannelConnections(channelName) {
|
|
406
|
+
const connections = await app.service('Connection').findMany({})
|
|
407
|
+
return connections.filter(connection => {
|
|
408
|
+
const channelNames = JSON.parse(connection.channelNames)
|
|
409
|
+
return channelNames.includes(channelName)
|
|
410
|
+
})
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
async function addChannelToConnection(connection, channelName) {
|
|
414
|
+
const channelNames = JSON.parse(connection.channelNames)
|
|
415
|
+
if (!channelNames.includes(channelName)) channelNames.push(channelName)
|
|
416
|
+
await app.service('Connection').update({
|
|
417
|
+
where: { id: connection.id },
|
|
418
|
+
data: { channelNames: JSON.stringify(channelNames) },
|
|
419
|
+
})
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async function removeChannelFromConnection(connection, channelName) {
|
|
423
|
+
const channelNames = JSON.parse(connection.channelNames).filter(name => name !== channelName)
|
|
424
|
+
await app.service('Connection').update({
|
|
425
|
+
where: { id },
|
|
426
|
+
data: { channelNames: JSON.stringify(channelNames) },
|
|
427
|
+
})
|
|
428
|
+
}
|
|
429
|
+
|
|
404
430
|
function joinChannel(channelName, connection) {
|
|
405
431
|
addChannelToConnection(connection, channelName)
|
|
406
432
|
}
|
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
|
-
}
|