@jcbuisson/express-x 1.0.6 → 1.0.8

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 +15 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "src/index.mjs",
package/src/index.mjs CHANGED
@@ -14,6 +14,7 @@ function expressX(app) {
14
14
  const connections = {}
15
15
 
16
16
  let lastConnectionId = 1
17
+ let isDebug = false
17
18
 
18
19
  /*
19
20
  * create a service `name` based on Prisma table `entity`
@@ -73,7 +74,7 @@ function expressX(app) {
73
74
 
74
75
  // call method
75
76
  const result = await method(...context.args)
76
- // console.log('result', result)
77
+ // if (isDebug) console.log('result', result)
77
78
 
78
79
  // call 'after' hooks
79
80
  const afterMethodHooks = service?.hooks?.after && service.hooks.after[methodName] || []
@@ -160,7 +161,7 @@ function expressX(app) {
160
161
  const io = new Server(server)
161
162
 
162
163
  io.on('connection', function(socket) {
163
- console.log('Client connected to the WebSocket')
164
+ if (isDebug) console.log('Client connected to the WebSocket')
164
165
  const connection = {
165
166
  id: lastConnectionId++,
166
167
  socket,
@@ -168,6 +169,7 @@ function expressX(app) {
168
169
  }
169
170
  // store connection in cache
170
171
  connections[connection.id] = connection
172
+ if (isDebug) console.log('active connections', connections)
171
173
 
172
174
  // emit 'connection' event for app (expressjs extends EventEmitter)
173
175
  app.emit('connection', connection)
@@ -176,7 +178,7 @@ function expressX(app) {
176
178
  socket.emit('connected', connection.id)
177
179
 
178
180
  socket.on('disconnect', () => {
179
- console.log('Client disconnected', connection.id)
181
+ if (isDebug) console.log('Client disconnected', connection.id)
180
182
  delete connections[connection.id]
181
183
  })
182
184
 
@@ -186,7 +188,7 @@ function expressX(app) {
186
188
  * Emit in return a 'client-response' message
187
189
  */
188
190
  socket.on('client-request', async ({ uid, name, action, args }) => {
189
- console.log("client-request", uid, name, action, args)
191
+ if (isDebug) console.log("client-request", uid, name, action, args)
190
192
  if (name in services) {
191
193
  const service = services[name]
192
194
  try {
@@ -201,7 +203,7 @@ function expressX(app) {
201
203
  }
202
204
  const result = await serviceMethod(context, ...args)
203
205
 
204
- io.emit('client-response', {
206
+ socket.emit('client-response', {
205
207
  uid,
206
208
  result,
207
209
  })
@@ -209,12 +211,12 @@ function expressX(app) {
209
211
  const publishFunc = service.publishCallback
210
212
  if (publishFunc) {
211
213
  const channelNames = await publishFunc(result, app)
212
- console.log('publish channels', name, action, channelNames)
214
+ if (isDebug) console.log('publish channels', name, action, channelNames)
213
215
  for (const channelName of channelNames) {
214
- console.log('service-event', name, action, channelName)
216
+ if (isDebug) console.log('service-event', name, action, channelName)
215
217
  const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
216
218
  for (const connection of connectionList) {
217
- console.log('emit to', connection.id)
219
+ if (isDebug) console.log('emit to', connection.id)
218
220
  connection.socket.emit('service-event', {
219
221
  name,
220
222
  action,
@@ -253,6 +255,10 @@ function expressX(app) {
253
255
  connection.channelNames.delete(channelName)
254
256
  }
255
257
 
258
+ function setDebug(isOn) {
259
+ isDebug = isOn
260
+ }
261
+
256
262
  // enhance `app` with objects and methods
257
263
  Object.assign(app, {
258
264
  createDatabaseService,
@@ -263,6 +269,7 @@ function expressX(app) {
263
269
  server,
264
270
  joinChannel,
265
271
  leaveChannel,
272
+ setDebug,
266
273
  })
267
274
  return app
268
275
  }