@jcbuisson/express-x 2.1.12 → 2.1.13
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 +6 -11
- package/src/server.mjs +12 -5
package/package.json
CHANGED
package/src/common-hooks.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
import bcrypt from 'bcryptjs'
|
|
3
3
|
|
|
4
|
+
import { EXError } from './server.mjs'
|
|
5
|
+
|
|
4
6
|
/*
|
|
5
7
|
* Add a timestamp property of name `field` with current time as value
|
|
6
8
|
*/
|
|
@@ -34,13 +36,6 @@ export function protect(field) {
|
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
class NotAuthenticatedError extends Error {
|
|
38
|
-
constructor(message) {
|
|
39
|
-
super(message)
|
|
40
|
-
this.code = 'not-authenticated'
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
39
|
export const isNotExpired = async (context) => {
|
|
45
40
|
// do nothing if it's not a client call from a ws connexion
|
|
46
41
|
if (!context.socket) return
|
|
@@ -61,10 +56,10 @@ export const isNotExpired = async (context) => {
|
|
|
61
56
|
// send an event to the client (typical client handling: logout)
|
|
62
57
|
context.socket.emit('expired')
|
|
63
58
|
// throw exception
|
|
64
|
-
throw new
|
|
59
|
+
throw new EXError('not-authenticated', "Session expired")
|
|
65
60
|
}
|
|
66
61
|
} else {
|
|
67
|
-
throw new
|
|
62
|
+
throw new EXError('not-authenticated', "No expiresAt in socket.data")
|
|
68
63
|
}
|
|
69
64
|
}
|
|
70
65
|
|
|
@@ -74,7 +69,7 @@ export const isNotExpired = async (context) => {
|
|
|
74
69
|
export const isAuthenticated = async (context) => {
|
|
75
70
|
// do nothing if it's not a client call from a ws connexion
|
|
76
71
|
if (!context.socket) return
|
|
77
|
-
if (!context.socket.data.user) throw new
|
|
72
|
+
if (!context.socket.data.user) throw new EXError('not-authenticated', 'no user in socket.data')
|
|
78
73
|
}
|
|
79
74
|
|
|
80
75
|
/*
|
|
@@ -83,4 +78,4 @@ export const isAuthenticated = async (context) => {
|
|
|
83
78
|
export const extendExpiration = (duration) => async (context) => {
|
|
84
79
|
const now = new Date()
|
|
85
80
|
context.socket.data.expiresAt = new Date(now.getTime() + duration)
|
|
86
|
-
}
|
|
81
|
+
}
|
package/src/server.mjs
CHANGED
|
@@ -5,6 +5,13 @@ import { Server } from "socket.io"
|
|
|
5
5
|
|
|
6
6
|
// UTILISER L'ACKNOWLEDGEMENT : https://socket.io/docs/v4/#acknowledgements
|
|
7
7
|
|
|
8
|
+
export default class EXError extends Error {
|
|
9
|
+
constructor(code, message) {
|
|
10
|
+
super(message)
|
|
11
|
+
this.code = code
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
8
15
|
export function expressX(config) {
|
|
9
16
|
|
|
10
17
|
const services = {}
|
|
@@ -162,7 +169,7 @@ export function expressX(config) {
|
|
|
162
169
|
/*
|
|
163
170
|
* create a service `name` with given `methods`
|
|
164
171
|
*/
|
|
165
|
-
function createService(
|
|
172
|
+
function createService(name, methods) {
|
|
166
173
|
const service = {}
|
|
167
174
|
|
|
168
175
|
for (const methodName in methods) {
|
|
@@ -201,7 +208,7 @@ export function expressX(config) {
|
|
|
201
208
|
if (service.publishFunction) {
|
|
202
209
|
// collect channel names to socket is member of
|
|
203
210
|
const channelNames = await service.publishFunction(context)
|
|
204
|
-
app.log('verbose', `publish channels ${
|
|
211
|
+
app.log('verbose', `publish channels ${name} ${methodName} ${channelNames}`)
|
|
205
212
|
// send event on all these channels
|
|
206
213
|
if (channelNames.length > 0) {
|
|
207
214
|
let sender = io.to(channelNames[0])
|
|
@@ -209,7 +216,7 @@ export function expressX(config) {
|
|
|
209
216
|
sender = sender.to(channelNames[i])
|
|
210
217
|
}
|
|
211
218
|
sender.emit('service-event', {
|
|
212
|
-
name
|
|
219
|
+
name,
|
|
213
220
|
action: methodName,
|
|
214
221
|
result,
|
|
215
222
|
})
|
|
@@ -224,7 +231,7 @@ export function expressX(config) {
|
|
|
224
231
|
const context = {
|
|
225
232
|
app,
|
|
226
233
|
caller: 'server',
|
|
227
|
-
serviceName,
|
|
234
|
+
serviceName: service._name,
|
|
228
235
|
methodName,
|
|
229
236
|
args,
|
|
230
237
|
}
|
|
@@ -244,7 +251,7 @@ export function expressX(config) {
|
|
|
244
251
|
}
|
|
245
252
|
|
|
246
253
|
// cache service in `services`
|
|
247
|
-
services[
|
|
254
|
+
services[name] = service
|
|
248
255
|
return service
|
|
249
256
|
}
|
|
250
257
|
|