@jcbuisson/express-x 1.5.36 → 1.6.1

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/server.mjs +34 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.5.36",
3
+ "version": "1.6.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/server.mjs CHANGED
@@ -13,6 +13,7 @@ export function expressX(prisma, options = {}) {
13
13
  if (options.ws === undefined) options.ws = { ws_prefix: "expressx" }
14
14
 
15
15
  const services = {}
16
+ let appHooks = []
16
17
 
17
18
  const cnx2Socket = {}
18
19
 
@@ -48,6 +49,14 @@ export function expressX(prisma, options = {}) {
48
49
  }
49
50
  }
50
51
 
52
+ function getSocket(connectionId) {
53
+ return cnx2Socket[connectionId]
54
+ }
55
+
56
+ function setSocket(connectionId, socket) {
57
+ cnx2Socket[connectionId] = socket
58
+ }
59
+
51
60
 
52
61
  // logging function - a winston logger must be configured first
53
62
  app.log = (severity, message) => {
@@ -89,10 +98,11 @@ export function expressX(prisma, options = {}) {
89
98
  // if a hook or the method throws an error, it will be caught by `socket.on('client-request'` (ws)
90
99
  // or by express (http) and the client will get a rejected promise
91
100
 
92
- // call 'before' hooks, modifying `context.args`
101
+ // call 'before' hooks, modifying `context`
102
+ const beforeAppHooks = appHooks?.before || []
93
103
  const beforeMethodHooks = service?.hooks?.before && service.hooks.before[methodName] || []
94
104
  const beforeAllHooks = service?.hooks?.before?.all || []
95
- for (const hook of [...beforeMethodHooks, ...beforeAllHooks]) {
105
+ for (const hook of [...beforeAppHooks, ...beforeMethodHooks, ...beforeAllHooks]) {
96
106
  await hook(context)
97
107
  }
98
108
 
@@ -100,18 +110,18 @@ export function expressX(prisma, options = {}) {
100
110
  const result = await method(...context.args)
101
111
  // put result into context
102
112
  context.result = result
103
-
104
- // call 'after' hooks
113
+
114
+ // call 'after' hooks, modifying `context`
105
115
  const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
106
116
  const afterAllHooks = service?.hooks?.after?.all || []
107
- for (const hook of [...afterMethodHooks, ...afterAllHooks]) {
117
+ const afterAppHooks = appHooks?.after || []
118
+ for (const hook of [...afterMethodHooks, ...afterAllHooks, ...afterAppHooks]) {
108
119
  await hook(context)
109
120
  }
110
121
 
111
- // publish event
112
- const publishFunc = service.publishCallback
113
- if (publishFunc) {
114
- const channelNames = await publishFunc(result, app)
122
+ // publish event (websocket transport)
123
+ if (options.ws && service.publishFunction) {
124
+ const channelNames = await service.publishFunction(result, app)
115
125
  app.log('verbose', `publish channels ${service.name} ${methodName} ${channelNames}`)
116
126
  const connections = await app.prisma.Connection.findMany({})
117
127
  for (const channelName of channelNames) {
@@ -124,12 +134,9 @@ export function expressX(prisma, options = {}) {
124
134
  for (const connection of connectionList) {
125
135
  const trimmedResult = result ? JSON.stringify(result).slice(0, 300) : ''
126
136
  app.log('verbose', `emit to ${connection.id} ${service.name} ${methodName} ${trimmedResult}`)
127
- const socket = cnx2Socket[connection.id]
128
- console.log()
129
- if (!socket) {
130
- continue // SHOULD NOT HAPPEN
131
- }
132
- socket.emit('service-event', {
137
+ const socket = getSocket(connection.id)
138
+ // emit service event
139
+ socket && socket.emit('service-event', {
133
140
  name: service.name,
134
141
  action: methodName,
135
142
  result,
@@ -137,20 +144,20 @@ export function expressX(prisma, options = {}) {
137
144
  }
138
145
  }
139
146
  }
140
-
147
+
141
148
  return context.result
142
149
  }
143
150
 
151
+ // TODO: NOT CLEAR AND PROBABLY USELESS
144
152
  // hooked version of method: `create`, etc., to be called from backend with no context
145
153
  service[methodName] = method
146
-
147
154
  // un-hooked version of method: `_create`, etc., to be called from backend with no context
148
155
  service['_' + methodName] = method
149
156
  }
150
157
 
151
158
  // attach pub/sub publish callback
152
159
  service.publish = async (func) => {
153
- service.publishCallback = func
160
+ service.publishFunction = func
154
161
  },
155
162
 
156
163
  // attach hooks
@@ -174,6 +181,11 @@ export function expressX(prisma, options = {}) {
174
181
  callback(app)
175
182
  }
176
183
 
184
+ // set application hooks
185
+ function hooks(hooks) {
186
+ appHooks = hooks
187
+ }
188
+
177
189
  /*
178
190
  * add an HTTP REST endpoint at `path`, based on `service`
179
191
  */
@@ -310,7 +322,7 @@ export function expressX(prisma, options = {}) {
310
322
  const connection = await createConnection(clientIP)
311
323
  app.log('verbose', `Client connected ${connection.id} from IP ${clientIP}`)
312
324
 
313
- cnx2Socket[connection.id] = socket
325
+ setSocket(connection.id, socket)
314
326
 
315
327
  // emit 'connection' event for app (expressjs extends EventEmitter)
316
328
  console.log('EMIT CONNECTION')
@@ -341,7 +353,7 @@ export function expressX(prisma, options = {}) {
341
353
  const fromConnection = await getConnection(from)
342
354
  if (!fromConnection) return
343
355
  await cloneConnection(to, fromConnection)
344
- cnx2Socket[to] = socket
356
+ setSocket(to, socket)
345
357
  await deleteConnection(from)
346
358
  // send acknowledge to client
347
359
  io.emit('cnx-transfer-ack', to)
@@ -425,11 +437,12 @@ export function expressX(prisma, options = {}) {
425
437
  return Object.assign(app, {
426
438
  prisma,
427
439
  options,
428
- cnx2Socket,
440
+ getSocket, setSocket,
429
441
  createDatabaseService,
430
442
  createService,
431
443
  service,
432
444
  configure,
445
+ hooks,
433
446
  addHttpRest,
434
447
  server,
435
448
  joinChannel,