@jcbuisson/express-x 1.7.5 → 1.7.7
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/server.mjs +12 -7
package/package.json
CHANGED
package/src/server.mjs
CHANGED
|
@@ -2,16 +2,19 @@
|
|
|
2
2
|
import http from 'http'
|
|
3
3
|
import { Server } from "socket.io"
|
|
4
4
|
import express from 'express'
|
|
5
|
-
import config from 'config'
|
|
6
5
|
|
|
7
6
|
/*
|
|
8
7
|
* Enhance `app` express application with services and real-time features
|
|
9
8
|
*/
|
|
10
|
-
export function expressX(prisma,
|
|
9
|
+
export function expressX(prisma, config) {
|
|
11
10
|
|
|
12
11
|
const app = express()
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
// so that config can be accessed anywhere with app.get('config')
|
|
14
|
+
app.set('config', config)
|
|
15
|
+
|
|
16
|
+
// websocket transport by default
|
|
17
|
+
if (config.WS_TRANSPORT == undefined) config.WS_TRANSPORT = true
|
|
15
18
|
|
|
16
19
|
const services = {}
|
|
17
20
|
let appHooks = []
|
|
@@ -121,7 +124,7 @@ export function expressX(prisma, options = {}) {
|
|
|
121
124
|
}
|
|
122
125
|
|
|
123
126
|
// publish event (websocket transport)
|
|
124
|
-
if (
|
|
127
|
+
if (config.WS_TRANSPORT && service.publishFunction) {
|
|
125
128
|
const channelNames = await service.publishFunction(result, app)
|
|
126
129
|
app.log('verbose', `publish channels ${service.name} ${methodName} ${channelNames}`)
|
|
127
130
|
const connections = await app.prisma.Connection.findMany({})
|
|
@@ -312,11 +315,14 @@ export function expressX(prisma, options = {}) {
|
|
|
312
315
|
*/
|
|
313
316
|
const server = new http.Server(app)
|
|
314
317
|
|
|
315
|
-
if (
|
|
318
|
+
if (config.WS_TRANSPORT) {
|
|
316
319
|
/*
|
|
317
320
|
* Add websocket transport
|
|
318
321
|
*/
|
|
319
|
-
const io = new Server(server
|
|
322
|
+
const io = new Server(server, {
|
|
323
|
+
path: config.WS_PATH || '/expressx-socket-io',
|
|
324
|
+
})
|
|
325
|
+
|
|
320
326
|
|
|
321
327
|
io.on('connection', async function(socket) {
|
|
322
328
|
const clientIP = socket.request?.connection?.remoteAddress || 'unknown'
|
|
@@ -456,7 +462,6 @@ export function expressX(prisma, options = {}) {
|
|
|
456
462
|
// enhance `app` with objects and methods
|
|
457
463
|
return Object.assign(app, {
|
|
458
464
|
prisma,
|
|
459
|
-
options,
|
|
460
465
|
getSocket, setSocket,
|
|
461
466
|
createDatabaseService,
|
|
462
467
|
createService,
|