@jcbuisson/express-x 2.1.16 → 2.1.18

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.mjs +23 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "2.1.16",
3
+ "version": "2.1.18",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -65,13 +65,13 @@ export function expressX(config) {
65
65
 
66
66
  app.log('verbose', `Client connected ${socket.id}`)
67
67
 
68
- // // emit 'connection' event for app (expressjs extends EventEmitter)
69
- // app.emit('connection', socket)
68
+ // emit 'connection' event for app (expressjs extends EventEmitter)
69
+ app.emit('connection', socket)
70
70
 
71
71
  socketConnectListeners.forEach(listener => listener(socket))
72
72
 
73
- // // send 'connected' event to client
74
- // socket.emit('connected', socket.id)
73
+ // send 'connected' event to client
74
+ socket.emit('connected', socket.id)
75
75
 
76
76
  socket.on('disconnecting', (reason) => {
77
77
  app.log('verbose', `Client disconnecting ${socket.id}, ${reason}`)
@@ -207,9 +207,9 @@ export function expressX(config) {
207
207
  if (service.publishFunction) {
208
208
  // collect channel names to socket is member of
209
209
  const channelNames = await service.publishFunction(context)
210
- app.log('verbose', `publish channels ${name} ${methodName} ${channelNames}`)
211
210
  // send event on all these channels
212
211
  if (channelNames.length > 0) {
212
+ app.log('verbose', `publish channels ${name} ${methodName} ${channelNames}`)
213
213
  let sender = io.to(channelNames[0])
214
214
  for (let i = 1; i < channelNames.length; i++) {
215
215
  sender = sender.to(channelNames[i])
@@ -358,11 +358,13 @@ export const isNotExpired = async (context) => {
358
358
  context.socket.leave(room)
359
359
  }
360
360
  // send an event to the client (typical client handling: logout)
361
- context.socket.emit('expired')
361
+ context.socket.emit('not-authenticated')
362
362
  // throw exception
363
363
  throw new EXError('not-authenticated', "Session expired")
364
364
  }
365
365
  } else {
366
+ // send an event to the client (typical client handling: logout)
367
+ context.socket.emit('not-authenticated')
366
368
  throw new EXError('not-authenticated', "No expiresAt in socket.data")
367
369
  }
368
370
  }
@@ -373,8 +375,16 @@ export const isNotExpired = async (context) => {
373
375
  export const isAuthenticated = async (context) => {
374
376
  // do nothing if it's not a client call from a ws connexion
375
377
  if (context.caller !== 'client') return
376
- if (!context.socket?.data) throw new EXError('not-authenticated', 'no data in socket')
377
- if (!context.socket.data?.user) throw new EXError('not-authenticated', 'no user in socket.data')
378
+ if (!context.socket?.data) {
379
+ // send an event to the client (typical client handling: logout)
380
+ context.socket.emit('not-authenticated')
381
+ throw new EXError('not-authenticated', 'no data in socket')
382
+ }
383
+ if (!context.socket.data?.user) {
384
+ // send an event to the client (typical client handling: logout)
385
+ context.socket.emit('not-authenticated')
386
+ throw new EXError('not-authenticated', 'no user in socket.data')
387
+ }
378
388
  }
379
389
 
380
390
  /*
@@ -383,6 +393,10 @@ export const isAuthenticated = async (context) => {
383
393
  export const extendExpiration = (duration) => async (context) => {
384
394
  const now = new Date()
385
395
  if (context.caller !== 'client') return
386
- if (!context.socket?.data) throw new EXError('not-authenticated', 'no data in socket')
396
+ if (!context.socket?.data) {
397
+ // send an event to the client (typical client handling: logout)
398
+ context.socket.emit('not-authenticated')
399
+ throw new EXError('not-authenticated', 'no data in socket')
400
+ }
387
401
  context.socket.data.expiresAt = new Date(now.getTime() + duration)
388
402
  }