@jcbuisson/express-x 2.1.2 → 2.1.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": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
@@ -35,25 +35,30 @@ export function protect(field) {
35
35
  }
36
36
 
37
37
  /*
38
- * Does nothing for calls which are not client-side
39
- * Check if the 'expireAt' key in socket.data is met
40
- * If it is met, throw an error (which will be sent back to the calling server or client) and reset socket.data
41
- * If not, do nothing. If needed, an application-level hook may automatically extend the expiration data at each service call
38
+ * Throw an error for a client service method call when socket.data.expiresAt is missing or overdue
42
39
  */
43
40
  export const isNotExpired = async (context) => {
44
- if (context.caller !== 'client') return
45
-
46
- const expireAt = context.socket.data.expireAt
47
- if (expireAt) {
48
- const expireAtDate = new Date(expireAt)
41
+ // do nothing if it's not a client call from a ws connexion
42
+ if (!context.socket) return
43
+ const expiresAt = context.socket.data.expiresAt
44
+ if (expiresAt) {
45
+ const expiresAtDate = new Date(expiresAt)
49
46
  const now = new Date()
50
- if (now > expireAtDate) {
47
+ if (now > expiresAtDate) {
51
48
  // expiration date is met: clear socket.data & throw exception
52
- const { clientIP } = socket.data
53
- socket.data = { clientIP }
49
+ context.socket.data = {}
54
50
  throw new Error('session-expired')
55
51
  }
56
52
  } else {
57
53
  throw new Error('session-expired')
58
54
  }
59
55
  }
56
+
57
+ /*
58
+ * Throw an error for a client service method call when socket.data does not contain user
59
+ */
60
+ export const isAuthenticated = async (context) => {
61
+ // do nothing if it's not a client call from a ws connexion
62
+ if (!context.socket) return
63
+ if (!context.socket.data.user) throw new Error('not-authenticated')
64
+ }
package/src/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  import { expressX } from './server.mjs'
3
- import { addTimestamp, hashPassword, protect, isNotExpired } from './common-hooks.mjs'
3
+ import { addTimestamp, hashPassword, protect, isAuthenticated, isNotExpired } from './common-hooks.mjs'
4
4
 
5
5
  export {
6
6
  expressX,
@@ -8,5 +8,6 @@ export {
8
8
  addTimestamp,
9
9
  hashPassword,
10
10
  protect,
11
+ isAuthenticated,
11
12
  isNotExpired,
12
13
  }
package/src/server.mjs CHANGED
@@ -3,6 +3,8 @@ import { createServer } from "http"
3
3
  import { Server } from "socket.io"
4
4
 
5
5
 
6
+ // UTILISER L'ACKNOWLEDGEMENT : https://socket.io/docs/v4/#acknowledgements
7
+
6
8
  export function expressX(config) {
7
9
 
8
10
  const services = {}
@@ -12,15 +14,15 @@ export function expressX(config) {
12
14
  const socketDisconnectListeners = []
13
15
 
14
16
 
15
- function addSocketConnectListener(func) {
17
+ function addConnectListener(func) {
16
18
  socketConnectListeners.push(func)
17
19
  }
18
20
 
19
- function addSocketDisconnectingListener(func) {
21
+ function addDisconnectingListener(func) {
20
22
  socketDisconnectingListeners.push(func)
21
23
  }
22
24
 
23
- function addSocketDisconnectListener(func) {
25
+ function addDisconnectListener(func) {
24
26
  socketDisconnectListeners.push(func)
25
27
  }
26
28
 
@@ -57,18 +59,17 @@ export function expressX(config) {
57
59
  } else {
58
60
  // new or unrecoverable connection
59
61
  // (page open, page refresh/reload)
60
- socket.data.clientIP = socket.handshake.address
61
62
  }
62
63
 
63
64
  app.log('verbose', `Client connected ${socket.id}`)
64
65
 
65
- // emit 'connection' event for app (expressjs extends EventEmitter)
66
- app.emit('connection', socket)
66
+ // // emit 'connection' event for app (expressjs extends EventEmitter)
67
+ // app.emit('connection', socket)
67
68
 
68
69
  socketConnectListeners.forEach(listener => listener(socket))
69
70
 
70
- // send 'connected' event to client
71
- socket.emit('connected', socket.id)
71
+ // // send 'connected' event to client
72
+ // socket.emit('connected', socket.id)
72
73
 
73
74
  socket.on('disconnecting', (reason) => {
74
75
  app.log('verbose', `Client disconnecting ${socket.id}, ${reason}`)
@@ -114,13 +115,14 @@ export function expressX(config) {
114
115
  result,
115
116
  })
116
117
  } catch(err) {
117
- console.log('!!!!!!error', err.code, err)
118
+ console.log('!!!!!!error', err)
118
119
  app.log('verbose', err.stack)
119
120
  socket.emit('client-response', {
120
121
  uid,
121
122
  error: {
122
123
  code: err.code || 'unknown-error',
123
- message: err.stack,
124
+ message: err.message,
125
+ stack: err.stack,
124
126
  }
125
127
  })
126
128
  }
@@ -140,8 +142,9 @@ export function expressX(config) {
140
142
  uid,
141
143
  error: {
142
144
  code: err.code || 'unknown-error',
143
- message: err.stack,
144
- }
145
+ message: err.message,
146
+ stack: err.stack,
147
+ }
145
148
  })
146
149
  }
147
150
  } else {
@@ -173,8 +176,8 @@ export function expressX(config) {
173
176
  // put args into context
174
177
  context.args = args
175
178
 
176
- // if a hook or the method throws an error, it will be caught by `socket.on('client-request'` (ws)
177
- // or by express (http) and the client will get a rejected promise
179
+ // if a hook or the method throws an error, it will be caught by `socket.on('client-request'`
180
+ // and the client will get a rejected promise
178
181
 
179
182
  // call 'before' hooks, possibly modifying `context`
180
183
  const beforeAppHooks = appHooks?.before || []
@@ -291,9 +294,9 @@ export function expressX(config) {
291
294
  joinChannel,
292
295
  leaveChannel,
293
296
  sendAppEvent,
294
- addSocketConnectListener,
295
- addSocketDisconnectingListener,
296
- addSocketDisconnectListener,
297
+ addConnectListener,
298
+ addDisconnectingListener,
299
+ addDisconnectListener,
297
300
  })
298
301
 
299
302
  }