@jcbuisson/express-x 1.5.3 → 1.5.4

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.3",
3
+ "version": "1.5.4",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/context.mjs CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ export async function getConnectionIP(context) {
3
+ const id = context.params.connectionId
4
+ const connection = await context.app.service('Connection')._findUnique({ where: { id }})
5
+ return connection?.clientIP
6
+ }
7
+
2
8
  export async function getConnectionDataItem(context, key) {
3
9
  const id = context.params.connectionId
4
10
  const connection = await context.app.service('Connection')._findUnique({ where: { id }})
package/src/index.mjs CHANGED
@@ -1,11 +1,12 @@
1
1
 
2
2
  import { expressX } from './server.mjs'
3
3
  import { hashPassword, protect, isAuthenticated } from './common-hooks.mjs'
4
- import { getConnectionDataItem, setConnectionDataItem, removeConnectionDataItem } from './context.mjs'
4
+ import { getConnectionIP, getConnectionDataItem, setConnectionDataItem, removeConnectionDataItem } from './context.mjs'
5
5
 
6
6
  export {
7
7
  expressX,
8
8
 
9
+ getConnectionIP,
9
10
  getConnectionDataItem,
10
11
  setConnectionDataItem,
11
12
  removeConnectionDataItem,
package/src/server.mjs CHANGED
@@ -17,23 +17,15 @@ export function expressX(prisma, options = {}) {
17
17
 
18
18
  const cnx2Socket = {}
19
19
 
20
- function createConnection() {
21
- return app.service('Connection').create({})
22
- }
23
-
24
- async function deleteConnection(id) {
25
- try {
26
- await app.service('Connection')._delete({ where: { id }})
27
- } catch(err) {
28
- console.log('111')
29
- }
20
+ function createConnection(clientIP) {
21
+ return app.service('Connection').create({ data: { clientIP }})
30
22
  }
31
23
 
32
24
  function getConnection(id) {
33
25
  return app.service('Connection').findUnique({ where: { id }})
34
26
  }
35
27
 
36
- async function copyConnection(id, connection) {
28
+ async function cloneConnection(id, connection) {
37
29
  await app.service('Connection').update({
38
30
  where: { id },
39
31
  data: {
@@ -43,6 +35,14 @@ export function expressX(prisma, options = {}) {
43
35
  })
44
36
  }
45
37
 
38
+ async function deleteConnection(id) {
39
+ try {
40
+ await app.service('Connection')._delete({ where: { id }})
41
+ } catch(err) {
42
+ // it may be necessary in rare situations (not sure...)
43
+ }
44
+ }
45
+
46
46
  async function getChannelConnections(channelName) {
47
47
  const connections = await app.service('Connection').findMany({})
48
48
  return connections.filter(connection => {
@@ -68,14 +68,6 @@ export function expressX(prisma, options = {}) {
68
68
  })
69
69
  }
70
70
 
71
- app.printCnx = async (label) => {
72
- console.log(label)
73
- const connections = await app.service('Connection').findMany({})
74
- for (const connection of connections) {
75
- console.log(`CNX ${connection.id}, data ${JSON.stringify(connection.data)}`)
76
- }
77
- }
78
-
79
71
  // logging function - a winston logger must be configured first
80
72
  app.log = (severity, message) => {
81
73
  const logger = app.get('logger')
@@ -307,8 +299,9 @@ export function expressX(prisma, options = {}) {
307
299
  const io = new Server(server)
308
300
 
309
301
  io.on('connection', async function(socket) {
310
- const connection = await createConnection()
311
- app.log('verbose', `Client connected ${connection.id}`)
302
+ const clientIP = socket.request?.connection?.remoteAddress || 'unknown'
303
+ const connection = await createConnection(clientIP)
304
+ app.log('verbose', `Client connected ${connection.id} from IP ${clientIP}`)
312
305
 
313
306
  cnx2Socket[connection.id] = socket
314
307
 
@@ -322,9 +315,14 @@ export function expressX(prisma, options = {}) {
322
315
  app.log('verbose', `Client disconnected ${connection.id}`)
323
316
 
324
317
  // remove connection record after 1mn (leaves time in case of connection transfer)
325
- setTimeout(() => {
326
- app.log('verbose', `Delete connection ${connection.id}`)
327
- deleteConnection(connection.id)
318
+ setTimeout(async () => {
319
+ const connectionId = connection.id
320
+ // check if connection still exists
321
+ const cnx = await getConnection(connectionId)
322
+ if (cnx) {
323
+ app.log('verbose', `Delete connection ${connectionId}`)
324
+ await deleteConnection(connectionId)
325
+ }
328
326
  }, 10 * 1000)
329
327
  })
330
328
 
@@ -334,10 +332,9 @@ export function expressX(prisma, options = {}) {
334
332
  app.log('verbose', `cnx-transfer from ${from} to ${to}`)
335
333
  const fromConnection = await getConnection(from)
336
334
  if (!fromConnection) return
337
- await copyConnection(to, fromConnection)
335
+ await cloneConnection(to, fromConnection)
338
336
  cnx2Socket[to] = socket
339
337
  await deleteConnection(from)
340
- app.printCnx('AFTER TRANSFER')
341
338
  // send acknowledge to client
342
339
  io.emit('cnx-transfer-ack', to)
343
340
  })
@@ -428,6 +425,7 @@ export function expressX(prisma, options = {}) {
428
425
  removeChannelFromConnection(connection, channelName)
429
426
  }
430
427
 
428
+
431
429
  // enhance `app` with objects and methods
432
430
  return Object.assign(app, {
433
431
  options,