@vulkano/core 1.3.0 → 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.
@@ -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 || {},
@@ -4,7 +4,7 @@
4
4
 
5
5
  const express = require('express');
6
6
  const frameguard = require('frameguard');
7
- // const { Server } = require('socket.io');
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
- // const { createClient } = require('redis');
17
- // const { createAdapter } = require('@socket.io/redis-adapter');
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')({
@@ -23,21 +23,29 @@ const AllControllers = require('include-all')({
23
23
  optional: true
24
24
  });
25
25
 
26
+ // Views Config
26
27
  const viewsConfig = require('./views');
27
28
 
28
- const responses = require('./responses');
29
-
30
- const JWT = require('../libs/Jwt');
29
+ // JWT Middleware
30
+ const jwtMiddleware = require('../libs/Jwt');
31
31
 
32
+ // Express Config
32
33
  const expressConfig = require('./express')();
33
34
 
35
+ // Express Responses
36
+ const responses = require('./responses');
37
+
34
38
  module.exports = {
35
39
 
36
40
  routes: {},
37
41
 
38
42
  start: async function loadServerApplication(cb) {
39
43
 
40
- const jwtMiddleware = JWT;
44
+ // Get ENV Vars
45
+ const {
46
+ JWT_SECRET_KEY,
47
+ COOKIES_SECRET_KEY
48
+ } = process.env || {};
41
49
 
42
50
  const vulkano = express();
43
51
 
@@ -73,7 +81,7 @@ module.exports = {
73
81
  // COOKIES - File: app/config/express/cookies.js
74
82
  // ---------------
75
83
  if (expressConfig.cookies && expressConfig.cookies.enabled) {
76
- const cookiesSecretKey = process.env.COOKIES_SECRET_KEY || expressConfig.cookies.key || expressConfig.cookies.secret || '';
84
+ const cookiesSecretKey = COOKIES_SECRET_KEY || expressConfig.cookies.key || expressConfig.cookies.secret || '';
77
85
  if (!cookiesSecretKey) {
78
86
  console.log(' \x1b[33mWARNING\x1b[0m: Set the secret key in the config/express/cookie.js file or COOKIES_SECRET_KEY in the .env file.');
79
87
  }
@@ -171,7 +179,7 @@ module.exports = {
171
179
  // ---------------
172
180
  if (expressConfig.jwt && expressConfig.jwt.enabled) {
173
181
 
174
- const jwtSecretKey = process.env.JWT_SECRET_KEY || expressConfig.jwt.key || expressConfig.jwt.secret || '';
182
+ const jwtSecretKey = JWT_SECRET_KEY || expressConfig.jwt.key || expressConfig.jwt.secret || '';
175
183
  if (!jwtSecretKey) {
176
184
  console.log(' \x1b[41mERROR\x1b[0m: Can not get key in config/express/jwt.js file or JWT_SECRET_KEY in .env file');
177
185
  return;
@@ -299,6 +307,14 @@ module.exports = {
299
307
  }
300
308
  });
301
309
 
310
+ // ---------------
311
+ // PUBLIC PATH - File: app/config/settings.js
312
+ // (if are using Vite)
313
+ // ---------------
314
+ if (!app.viteProxy) {
315
+ vulkano.use(express.static(PUBLIC_PATH));
316
+ }
317
+
302
318
  // ---------------
303
319
  // ROUTES
304
320
  // ---------------
@@ -423,14 +439,163 @@ module.exports = {
423
439
  });
424
440
 
425
441
  // ---------------
426
- // PUBLIC PATH - File: app/config/settings.js
442
+ // PUBLIC PATH
443
+ // (if not are using Vite)
427
444
  // ---------------
428
- vulkano.use(express.static(PUBLIC_PATH));
445
+ if (app.viteProxy) {
446
+ vulkano.use(express.static(PUBLIC_PATH));
447
+ }
429
448
 
430
449
  app.vulkano = vulkano;
431
450
  app.server = server;
432
451
 
433
- cb();
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
+ });
434
599
 
435
600
  }
436
601
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vulkano/core",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "A MVC framework using Express 4",
5
5
  "license": "MIT",
6
6
  "author": "Vulkano Team",