@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 +3 -2
- package/src/{index.js → index.mjs} +40 -35
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
3
|
-
|
|
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
|
|
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
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
|
|
270
|
+
export default expressX
|