@jcbuisson/express-x 1.5.6 → 1.5.8

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.5.6",
3
+ "version": "1.5.8",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
@@ -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/context.mjs CHANGED
@@ -1,8 +1,18 @@
1
1
 
2
- export async function getConnectionIP(context) {
2
+ export async function getConnection(context) {
3
3
  const id = context.params.connectionId
4
4
  const connection = await context.app.service('Connection')._findUnique({ where: { id }})
5
- return connection?.clientIP
5
+ return connection
6
+ }
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
+ })
6
16
  }
7
17
 
8
18
  export async function getConnectionDataItem(context, key) {
@@ -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').update({
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,16 @@
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 { getConnection, 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
- getConnectionIP,
10
+ getConnection,
11
+ resetConnectionIP,
12
+ resetConnectionChannels,
13
+
10
14
  getConnectionDataItem,
11
15
  setConnectionDataItem,
12
16
  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}`)