@jcbuisson/express-x 2.1.2 → 2.1.5
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 +1 -1
- package/src/common-hooks.mjs +27 -14
- package/src/index.mjs +2 -1
- package/src/server.mjs +20 -17
package/package.json
CHANGED
package/src/common-hooks.mjs
CHANGED
|
@@ -34,26 +34,39 @@ export function protect(field) {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
|
|
38
|
+
class NotAuthenticatedError extends Error {
|
|
39
|
+
constructor(message) {
|
|
40
|
+
super(message)
|
|
41
|
+
this.code = 'not-authenticated'
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
37
45
|
/*
|
|
38
|
-
*
|
|
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
|
|
46
|
+
* Throw an error for a client service method call when socket.data.expiresAt is missing or overdue
|
|
42
47
|
*/
|
|
43
48
|
export const isNotExpired = async (context) => {
|
|
44
|
-
if
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
const
|
|
49
|
+
// do nothing if it's not a client call from a ws connexion
|
|
50
|
+
if (!context.socket) return
|
|
51
|
+
const expiresAt = context.socket.data.expiresAt
|
|
52
|
+
if (expiresAt) {
|
|
53
|
+
const expiresAtDate = new Date(expiresAt)
|
|
49
54
|
const now = new Date()
|
|
50
|
-
if (now >
|
|
55
|
+
if (now > expiresAtDate) {
|
|
51
56
|
// expiration date is met: clear socket.data & throw exception
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
throw new Error('session-expired')
|
|
57
|
+
context.socket.data = {}
|
|
58
|
+
throw new NotAuthenticatedError("Session expired")
|
|
55
59
|
}
|
|
56
60
|
} else {
|
|
57
|
-
throw new
|
|
61
|
+
throw new NotAuthenticatedError("no expiresAt in socket.data")
|
|
58
62
|
}
|
|
59
63
|
}
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
* Throw an error for a client service method call when socket.data does not contain user
|
|
67
|
+
*/
|
|
68
|
+
export const isAuthenticated = async (context) => {
|
|
69
|
+
// do nothing if it's not a client call from a ws connexion
|
|
70
|
+
if (!context.socket) return
|
|
71
|
+
if (!context.socket.data.user) throw new NotAuthenticatedError('no user in socket.data')
|
|
72
|
+
}
|
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
|
|
17
|
+
function addConnectListener(func) {
|
|
16
18
|
socketConnectListeners.push(func)
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
function
|
|
21
|
+
function addDisconnectingListener(func) {
|
|
20
22
|
socketDisconnectingListeners.push(func)
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
function
|
|
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.code, err.message)
|
|
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.
|
|
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.
|
|
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'`
|
|
177
|
-
//
|
|
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
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
+
addConnectListener,
|
|
298
|
+
addDisconnectingListener,
|
|
299
|
+
addDisconnectListener,
|
|
297
300
|
})
|
|
298
301
|
|
|
299
302
|
}
|