@jcbuisson/express-x 1.5.2 → 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.2",
3
+ "version": "1.5.4",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
@@ -2,6 +2,8 @@
2
2
  import config from 'config'
3
3
  import bcrypt from 'bcryptjs'
4
4
 
5
+ import { getConnectionDataItem } from './context.mjs'
6
+
5
7
 
6
8
  // hash password of user record
7
9
  // name of the password field is found in `config.authentication.local.passwordField` (default: 'password')
@@ -30,9 +32,10 @@ export function protect(field) {
30
32
  export async function isAuthenticated(context) {
31
33
  if (context.transport !== 'ws') return
32
34
  // extract user from connection data
33
- const id = context.params.connectionId
34
- const connection = await context.app.service('Connection')._findUnique({ where: { id }})
35
- const data = JSON.parse(connection.data)
36
- const user = data.user
35
+ const user = await getConnectionDataItem(context, 'user')
36
+ // const id = context.params.connectionId
37
+ // const connection = await context.app.service('Connection')._findUnique({ where: { id }})
38
+ // const data = JSON.parse(connection.data)
39
+ // const user = data.user
37
40
  if (!user) throw Error(`AuthCode hook: not authenticated ${id}`)
38
41
  }
@@ -0,0 +1,39 @@
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
+
8
+ export async function getConnectionDataItem(context, key) {
9
+ const id = context.params.connectionId
10
+ const connection = await context.app.service('Connection')._findUnique({ where: { id }})
11
+ const data = JSON.parse(connection.data)
12
+ return data[key]
13
+ }
14
+
15
+ export async function setConnectionDataItem(context, key, value) {
16
+ const id = context.params.connectionId
17
+ const connection = await context.app.service('Connection')._findUnique({ where: { id }})
18
+ const data = JSON.parse(connection.data)
19
+ data[key] = value
20
+ await context.app.service('Connection').update({
21
+ where: { id },
22
+ data: {
23
+ data: JSON.stringify(data)
24
+ }
25
+ })
26
+ }
27
+
28
+ export async function removeConnectionDataItem(context, key) {
29
+ const id = context.params.connectionId
30
+ const connection = await context.app.service('Connection')._findUnique({ where: { id }})
31
+ const data = JSON.parse(connection.data)
32
+ delete data[key]
33
+ await context.app.service('Connection').update({
34
+ where: { id },
35
+ data: {
36
+ data: JSON.stringify(data)
37
+ }
38
+ })
39
+ }
package/src/index.mjs CHANGED
@@ -1,10 +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 } from './context.mjs'
4
5
 
5
6
  export {
6
7
  expressX,
7
8
 
9
+ getConnectionIP,
10
+ getConnectionDataItem,
11
+ setConnectionDataItem,
12
+ removeConnectionDataItem,
13
+
8
14
  hashPassword,
9
15
  protect,
10
16
  isAuthenticated,
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,