@vulkano/core 1.3.1 → 1.4.0
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/bootstrap/express.js +7 -1
- package/bootstrap/server.js +150 -4
- package/package.json +1 -1
package/bootstrap/express.js
CHANGED
|
@@ -7,6 +7,8 @@ module.exports = function getExpressConfiguration() {
|
|
|
7
7
|
cors,
|
|
8
8
|
jwt,
|
|
9
9
|
settings,
|
|
10
|
+
sockets,
|
|
11
|
+
redis,
|
|
10
12
|
cookies,
|
|
11
13
|
// Folder express config files
|
|
12
14
|
express: expressServerConfig
|
|
@@ -41,6 +43,8 @@ module.exports = function getExpressConfiguration() {
|
|
|
41
43
|
cors: {},
|
|
42
44
|
cookies: {},
|
|
43
45
|
jwt: {},
|
|
46
|
+
sockets: {},
|
|
47
|
+
redis: {},
|
|
44
48
|
multer: {
|
|
45
49
|
dest: 'public/files'
|
|
46
50
|
},
|
|
@@ -65,7 +69,9 @@ module.exports = function getExpressConfiguration() {
|
|
|
65
69
|
{
|
|
66
70
|
cookies,
|
|
67
71
|
jwt,
|
|
68
|
-
cors
|
|
72
|
+
cors,
|
|
73
|
+
sockets,
|
|
74
|
+
redis
|
|
69
75
|
},
|
|
70
76
|
expressDefaultConfig || {},
|
|
71
77
|
expressServerConfig || {},
|
package/bootstrap/server.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
const express = require('express');
|
|
6
6
|
const frameguard = require('frameguard');
|
|
7
|
-
|
|
7
|
+
const { Server } = require('socket.io');
|
|
8
8
|
const nunjucks = require('nunjucks');
|
|
9
9
|
const morgan = require('morgan');
|
|
10
10
|
const compression = require('compression');
|
|
@@ -13,8 +13,8 @@ const helmet = require('helmet');
|
|
|
13
13
|
const timeout = require('connect-timeout');
|
|
14
14
|
const useragent = require('express-useragent');
|
|
15
15
|
const cookieParser = require('cookie-parser');
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const { createClient } = require('redis');
|
|
17
|
+
const { createAdapter } = require('@socket.io/redis-adapter');
|
|
18
18
|
|
|
19
19
|
// Include all api controllers
|
|
20
20
|
const AllControllers = require('include-all')({
|
|
@@ -449,7 +449,153 @@ module.exports = {
|
|
|
449
449
|
app.vulkano = vulkano;
|
|
450
450
|
app.server = server;
|
|
451
451
|
|
|
452
|
-
|
|
452
|
+
// ---------------
|
|
453
|
+
// SOCKETS
|
|
454
|
+
// ---------------
|
|
455
|
+
const {
|
|
456
|
+
sockets,
|
|
457
|
+
redis
|
|
458
|
+
} = expressConfig || {};
|
|
459
|
+
|
|
460
|
+
if (!sockets || (sockets && !sockets.enabled)) {
|
|
461
|
+
cb();
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const socketProps = {
|
|
466
|
+
pingTimeout: +sockets.timeout || 4000,
|
|
467
|
+
pingInterval: +sockets.interval || 2000,
|
|
468
|
+
transports: sockets.transports || ['websocket', 'polling']
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
if (sockets.cors) {
|
|
472
|
+
if (typeof sockets.cors === 'function') {
|
|
473
|
+
socketProps.allowRequest = sockets.cors;
|
|
474
|
+
} else if (typeof sockets.cors === 'string') {
|
|
475
|
+
socketProps.cors = sockets.cors || '';
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (sockets.redis && !redis.enabled) {
|
|
480
|
+
throw new Error('Enable the Redis config "app/config/redis.js" to connect the sockets');
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
const io = new Server(server, socketProps);
|
|
484
|
+
|
|
485
|
+
let pubClient = null;
|
|
486
|
+
let subClient = null;
|
|
487
|
+
|
|
488
|
+
if (sockets.redis) {
|
|
489
|
+
|
|
490
|
+
if (sockets.transports.includes('polling')) {
|
|
491
|
+
throw new Error('To enable Sockets with Redis support, the transports must be set ¨websocket¨ only');
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const propsToRedis = {
|
|
495
|
+
host: redis.host,
|
|
496
|
+
port: redis.port
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
if (redis.password) {
|
|
500
|
+
propsToRedis.password = redis.password;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
pubClient = createClient(propsToRedis);
|
|
504
|
+
subClient = pubClient.duplicate();
|
|
505
|
+
|
|
506
|
+
io.adapter(createAdapter(pubClient, subClient));
|
|
507
|
+
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
Promise
|
|
511
|
+
.all([
|
|
512
|
+
(sockets.redis ? pubClient.connect() : null),
|
|
513
|
+
(sockets.redis ? subClient.connect() : null)
|
|
514
|
+
])
|
|
515
|
+
.then(() => {
|
|
516
|
+
|
|
517
|
+
io.on('connection', (socket) => {
|
|
518
|
+
|
|
519
|
+
if ( typeof sockets.onConnect === 'function') {
|
|
520
|
+
sockets.onConnect(socket);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const socketEvents = sockets.events || {};
|
|
524
|
+
|
|
525
|
+
Object.keys(socketEvents).forEach( (i) => {
|
|
526
|
+
|
|
527
|
+
const checkPath = socketEvents[i] || '';
|
|
528
|
+
|
|
529
|
+
let toExecute = null;
|
|
530
|
+
let module = null;
|
|
531
|
+
let controller = null;
|
|
532
|
+
let action = null;
|
|
533
|
+
|
|
534
|
+
if (typeof checkPath === 'function') {
|
|
535
|
+
|
|
536
|
+
toExecute = checkPath;
|
|
537
|
+
|
|
538
|
+
} else {
|
|
539
|
+
|
|
540
|
+
const fullPath = checkPath.split('.');
|
|
541
|
+
|
|
542
|
+
if (fullPath.length > 2) { // Has folder
|
|
543
|
+
|
|
544
|
+
[
|
|
545
|
+
module,
|
|
546
|
+
controller,
|
|
547
|
+
action
|
|
548
|
+
] = fullPath;
|
|
549
|
+
|
|
550
|
+
} else {
|
|
551
|
+
|
|
552
|
+
[
|
|
553
|
+
controller,
|
|
554
|
+
action
|
|
555
|
+
] = fullPath;
|
|
556
|
+
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
try {
|
|
560
|
+
toExecute = module
|
|
561
|
+
? (AllControllers[module][controller][action])
|
|
562
|
+
: AllControllers[controller][action];
|
|
563
|
+
} catch (e) {
|
|
564
|
+
toExecute = null;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
if (toExecute) {
|
|
570
|
+
socket.on(i, (body) => {
|
|
571
|
+
toExecute({ socket, body: body || {} });
|
|
572
|
+
});
|
|
573
|
+
} else {
|
|
574
|
+
console.error('\x1b[31mError:', 'Controller not found in', (module) ? `${module}.${controller}.${action}` : `${controller}.${action}`, '\x1b[0m', 'to socket event', i);
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
vulkano.set('socket', socket);
|
|
580
|
+
app.socket = socket;
|
|
581
|
+
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
// next line is the money
|
|
585
|
+
global.io = io;
|
|
586
|
+
vulkano.set('socketio', io);
|
|
587
|
+
|
|
588
|
+
// middleware
|
|
589
|
+
if (sockets.middleware && typeof sockets.middleware === 'function') {
|
|
590
|
+
io.use(sockets.middleware);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// override vulkano
|
|
594
|
+
app.vulkano = vulkano;
|
|
595
|
+
|
|
596
|
+
cb();
|
|
597
|
+
|
|
598
|
+
});
|
|
453
599
|
|
|
454
600
|
}
|
|
455
601
|
|