@jcbuisson/express-x 1.0.4 → 1.0.6

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,8 +1,9 @@
1
1
  {
2
2
  "name": "@jcbuisson/express-x",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "",
5
- "main": "src/index.js",
5
+ "type": "module",
6
+ "main": "src/index.mjs",
6
7
  "repository": {
7
8
  "type": "git",
8
9
  "url": "git@gitlab.com:buisson31/express-x.git"
@@ -1,8 +1,7 @@
1
1
 
2
- const http = require('http')
3
- const socketio = require('socket.io')
4
-
5
- const { PrismaClient } = require('@prisma/client')
2
+ import http from 'http'
3
+ import { Server } from "socket.io"
4
+ import { PrismaClient } from '@prisma/client'
6
5
 
7
6
  /*
8
7
  * Enhance `app` express application with Feathers-like services
@@ -158,7 +157,7 @@ function expressX(app) {
158
157
  * Add websocket transport for services
159
158
  */
160
159
  const server = new http.Server(app)
161
- const io = new socketio.Server(server)
160
+ const io = new Server(server)
162
161
 
163
162
  io.on('connection', function(socket) {
164
163
  console.log('Client connected to the WebSocket')
@@ -192,37 +191,43 @@ function expressX(app) {
192
191
  const service = services[name]
193
192
  try {
194
193
  const serviceMethod = service['__' + action]
195
-
196
- const context = {
197
- app,
198
- transport: 'ws',
199
- connection,
200
- name,
201
- action,
202
- }
203
- const result = await serviceMethod(context, ...args)
204
-
205
- io.emit('client-response', {
206
- uid,
207
- result,
208
- })
209
- // pub/sub: send event on associated channels
210
- const publishFunc = service.publishCallback
211
- if (publishFunc) {
212
- const channelNames = await publishFunc(result, app)
213
- console.log('publish channels', name, action, channelNames)
214
- for (const channelName of channelNames) {
215
- console.log('service-event', name, action, channelName)
216
- const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
217
- for (const connection of connectionList) {
218
- console.log('emit to', connection.id)
219
- connection.socket.emit('service-event', {
220
- name,
221
- action,
222
- result,
223
- })
194
+ if (serviceMethod) {
195
+ const context = {
196
+ app,
197
+ transport: 'ws',
198
+ connection,
199
+ name,
200
+ action,
201
+ }
202
+ const result = await serviceMethod(context, ...args)
203
+
204
+ io.emit('client-response', {
205
+ uid,
206
+ result,
207
+ })
208
+ // pub/sub: send event on associated channels
209
+ const publishFunc = service.publishCallback
210
+ if (publishFunc) {
211
+ const channelNames = await publishFunc(result, app)
212
+ console.log('publish channels', name, action, channelNames)
213
+ for (const channelName of channelNames) {
214
+ console.log('service-event', name, action, channelName)
215
+ const connectionList = Object.values(connections).filter(cnx => cnx.channelNames.has(channelName))
216
+ for (const connection of connectionList) {
217
+ console.log('emit to', connection.id)
218
+ connection.socket.emit('service-event', {
219
+ name,
220
+ action,
221
+ result,
222
+ })
223
+ }
224
224
  }
225
225
  }
226
+ } else {
227
+ io.emit('client-response', {
228
+ uid,
229
+ error: `there is no method named '${action}' for service '${name}'`,
230
+ })
226
231
  }
227
232
  } catch(error) {
228
233
  io.emit('client-response', {
@@ -262,4 +267,4 @@ function expressX(app) {
262
267
  return app
263
268
  }
264
269
 
265
- module.exports = expressX
270
+ export default expressX