@vulkano/core 1.2.0 → 1.3.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/app.js CHANGED
@@ -252,7 +252,7 @@ function startVulkano() {
252
252
  const serverConfig = [];
253
253
 
254
254
  const nodeVersion = process.version.match(/^v(\d+\.\d+\.\d+)/)[1];
255
- const portText = String(app.server.get('port') || 8000);
255
+ const portText = String(app.vulkano.get('port') || 8000);
256
256
  const socketText = sockets.enabled ? 'YES' : 'NO';
257
257
  const redisText = redis && redis.enabled ? 'YES' : 'NO';
258
258
 
@@ -0,0 +1,82 @@
1
+ const merge = require('deepmerge');
2
+
3
+ module.exports = function getExpressConfiguration() {
4
+
5
+ // Common config by filename
6
+ const {
7
+ cors,
8
+ jwt,
9
+ settings,
10
+ cookies,
11
+ // Folder express config files
12
+ express: expressServerConfig
13
+ } = app.config || {};
14
+
15
+ // express config by file in settings.js
16
+ const {
17
+ port: expressUserPort,
18
+ express: expressConfigInSettings
19
+ } = settings || {};
20
+
21
+ // express config by file in app/config/express/settings.js
22
+ const {
23
+ settings: expressGeneralSettings
24
+ } = expressServerConfig || {};
25
+
26
+ // express port via file in app/config/express/settings.js
27
+ const {
28
+ port: expressSettingsPort
29
+ } = expressGeneralSettings || {};
30
+
31
+ const {
32
+ NODE_PORT: ENV_NODE_PORT,
33
+ PORT: ENV_PORT
34
+ } = process.env || {};
35
+
36
+ // Express default configuration
37
+ const expressDefaultConfig = {
38
+ timeout: 120000,
39
+ poweredBy: false,
40
+ port: ENV_NODE_PORT || ENV_PORT || expressUserPort || expressSettingsPort || 8000,
41
+ cors: {},
42
+ cookies: {},
43
+ jwt: {},
44
+ multer: {
45
+ dest: 'public/files'
46
+ },
47
+ morgan: {
48
+ format: 'dev',
49
+ skip: ((req, res) => res.statusCode < 400)
50
+ },
51
+ compression: {},
52
+ json: {},
53
+ urlencoded: {
54
+ extended: true
55
+ },
56
+ helmet: {
57
+ contentSecurityPolicy: false,
58
+ crossOriginEmbedderPolicy: false
59
+ },
60
+ frameguard: null
61
+ };
62
+
63
+ // Merge all express configuration: config/file.js, config/express/file.js, config/settings.js
64
+ const expressConfig = merge.all([
65
+ {
66
+ cookies,
67
+ jwt,
68
+ cors
69
+ },
70
+ expressDefaultConfig || {},
71
+ expressServerConfig || {},
72
+ expressGeneralSettings || {},
73
+ expressConfigInSettings || {},
74
+ ]);
75
+
76
+ if (expressConfig && expressConfig.settings) {
77
+ delete expressConfig.settings;
78
+ }
79
+
80
+ return expressConfig;
81
+
82
+ };
@@ -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,9 +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 merge = require('deepmerge');
17
- const { createClient } = require('redis');
18
- const { createAdapter } = require('@socket.io/redis-adapter');
16
+ // const { createClient } = require('redis');
17
+ // const { createAdapter } = require('@socket.io/redis-adapter');
19
18
 
20
19
  // Include all api controllers
21
20
  const AllControllers = require('include-all')({
@@ -30,105 +29,25 @@ const responses = require('./responses');
30
29
 
31
30
  const JWT = require('../libs/Jwt');
32
31
 
32
+ const expressConfig = require('./express')();
33
+
33
34
  module.exports = {
34
35
 
35
36
  routes: {},
36
37
 
37
- start: function loadServerApplication(cb) {
38
+ start: async function loadServerApplication(cb) {
38
39
 
39
40
  const jwtMiddleware = JWT;
40
41
 
41
- // Common config by filename
42
- const {
43
- cors,
44
- jwt,
45
- settings,
46
- sockets,
47
- cookies,
48
- redis,
49
- // Folder express config files
50
- express: expressServerConfig
51
- } = app.config || {};
52
-
53
- // express config by file in settings.js
54
- const {
55
- port: expressUserPort,
56
- express: expressConfigInSettings
57
- } = settings || {};
58
-
59
- // express config by file in app/config/express/settings.js
60
- const {
61
- settings: expressGeneralSettings
62
- } = expressServerConfig || {};
63
-
64
- // express port via file in app/config/express/settings.js
65
- const {
66
- port: expressSettingsPort
67
- } = expressGeneralSettings || {};
68
-
69
- const {
70
- NODE_PORT: ENV_NODE_PORT,
71
- PORT: ENV_PORT
72
- } = process.env || {};
73
-
74
- // Express default configuration
75
- const expressDefaultConfig = {
76
- timeout: 120000,
77
- poweredBy: false,
78
- port: ENV_NODE_PORT || ENV_PORT || expressUserPort || expressSettingsPort || 8000,
79
- cors: {},
80
- cookies: {},
81
- jwt: {},
82
- multer: {
83
- dest: 'public/files'
84
- },
85
- morgan: {
86
- format: 'dev',
87
- skip: ((req, res) => res.statusCode < 400)
88
- },
89
- compression: {},
90
- json: {},
91
- urlencoded: {
92
- extended: true
93
- },
94
- helmet: {
95
- contentSecurityPolicy: false,
96
- crossOriginEmbedderPolicy: false
97
- },
98
- frameguard: null
99
- };
100
-
101
- // Merge all express configuration: config/file.js, config/express/file.js, config/settings.js
102
- const expressConfig = merge.all([
103
- {
104
- cookies,
105
- jwt,
106
- cors
107
- },
108
- expressDefaultConfig || {},
109
- expressServerConfig || {},
110
- expressGeneralSettings || {},
111
- expressConfigInSettings || {},
112
- ]);
113
-
114
- if (expressConfig && expressConfig.settings) {
115
- delete expressConfig.settings;
116
- }
117
-
118
- // Middleware
119
- const middleware = app.config.middleware || ((req, res, next) => {
120
- next();
121
- });
122
-
123
- const server = express();
42
+ const vulkano = express();
124
43
 
125
44
  // Settings
126
- server.enable('trust proxy');
45
+ vulkano.enable('trust proxy');
127
46
 
128
47
  // ---------------
129
48
  // PORT - File: app/config/express/settings.js
130
49
  // ---------------
131
- server.set('port', expressConfig.port);
50
+ vulkano.set('port', expressConfig.port);
132
51
 
133
52
  // ---------------
134
53
  // MULTER - File: app/config/express/multer.js
@@ -138,17 +57,17 @@ module.exports = {
138
57
  // ---------------
139
58
  // MORGAN - File: app/config/express/morgan.js
140
59
  // ---------------
141
- server.use(morgan(expressConfig.morgan.format, expressConfig.morgan));
60
+ vulkano.use(morgan(expressConfig.morgan.format, expressConfig.morgan));
142
61
 
143
62
  // ---------------
144
63
  // USER AGENT
145
64
  // ---------------
146
- server.use(useragent.express());
65
+ vulkano.use(useragent.express());
147
66
 
148
67
  // ---------------
149
68
  // COMPRESSION - File: app/config/express/compression.js
150
69
  // ---------------
151
- server.use(compression( expressConfig.compression || {} ));
70
+ vulkano.use(compression( expressConfig.compression || {} ));
152
71
 
153
72
  // ---------------
154
73
  // COOKIES - File: app/config/express/cookies.js
@@ -158,33 +77,28 @@ module.exports = {
158
77
  if (!cookiesSecretKey) {
159
78
  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.');
160
79
  }
161
- server.use(cookieParser(cookiesSecretKey));
80
+ vulkano.use(cookieParser(cookiesSecretKey));
162
81
  }
163
82
 
164
83
  // ---------------
165
84
  // EXPRESS JSON - File: app/config/express/json.js
166
85
  // ---------------
167
- server.use(express.json(expressConfig.json));
86
+ vulkano.use(express.json(expressConfig.json));
168
87
 
169
88
  // ---------------
170
89
  // EXPRESS FORM DATA - File: app/config/express/urlencoded.js
171
90
  // ---------------
172
- server.use(express.urlencoded(expressConfig.urlencoded));
91
+ vulkano.use(express.urlencoded(expressConfig.urlencoded));
173
92
 
174
93
  // ---------------
175
94
  // HELMET - File: app/config/express/helmet.js
176
95
  // ---------------
177
- server.use(helmet(expressConfig.helmet));
96
+ vulkano.use(helmet(expressConfig.helmet));
178
97
 
179
98
  // ---------------
180
99
  // RESPONSES
181
100
  // ---------------
182
- server.use(responses);
183
-
184
- // ---------------
185
- // PUBLIC PATH - File: app/config/settings.js
186
- // ---------------
187
- server.use(express.static(PUBLIC_PATH));
101
+ vulkano.use(responses);
188
102
 
189
103
  // ---------------
190
104
  // FRAMEGUARD - File: app/config/express/frameguard.js
@@ -192,17 +106,17 @@ module.exports = {
192
106
  if (expressConfig.frameguard) {
193
107
  if (Array.isArray(expressConfig.frameguard)) {
194
108
  expressConfig.frameguard.forEach( (frame) => {
195
- server.use(frameguard(frame));
109
+ vulkano.use(frameguard(frame));
196
110
  });
197
111
  } else {
198
- server.use(frameguard(expressConfig.frameguard));
112
+ vulkano.use(frameguard(expressConfig.frameguard));
199
113
  }
200
114
  }
201
115
 
202
116
  // ---------------
203
117
  // PROTOCOL & POWERED BY - File: app/config/settings.js
204
118
  // ---------------
205
- server.use( (req, res, next) => {
119
+ vulkano.use( (req, res, next) => {
206
120
 
207
121
  const proto = req.secure ? 'https' : 'http';
208
122
  const forwarded = req.headers['x-forwaded-proto'] || null;
@@ -220,7 +134,7 @@ module.exports = {
220
134
  // ---------------
221
135
  // REQUEST OPTIONS - File: app/config/express/cors.js
222
136
  // ---------------
223
- server.options('*', (req, res) => {
137
+ vulkano.options('*', (req, res) => {
224
138
 
225
139
  // ---------------
226
140
  // CORS
@@ -244,8 +158,9 @@ module.exports = {
244
158
  // ---------------
245
159
  // TIMEOUT - File: app/config/settings.js
246
160
  // ---------------
247
- server.use(timeout( expressConfig.timeout || 120000 ));
248
- server.use( (req, res, next) => {
161
+ vulkano.use(timeout( expressConfig.timeout || 120000 ));
162
+
163
+ vulkano.use( (req, res, next) => {
249
164
  if (!req.timedout) {
250
165
  next();
251
166
  }
@@ -263,12 +178,12 @@ module.exports = {
263
178
  }
264
179
 
265
180
  // JWT (secret key)
266
- server.use(expressConfig.jwt.path || '*', jwtMiddleware.init().unless({
181
+ vulkano.use(expressConfig.jwt.path || '*', jwtMiddleware.init().unless({
267
182
  path: expressConfig.jwt.ignore || []
268
183
  }));
269
184
 
270
185
  // JWT Handler error
271
- server.use((err, req, res, next) => {
186
+ vulkano.use((err, req, res, next) => {
272
187
  if (err && err.name === 'UnauthorizedError') {
273
188
  res.status(401).jsonp({ success: false, error: 'Invalid token' });
274
189
  } else {
@@ -283,7 +198,7 @@ module.exports = {
283
198
  // ---------------
284
199
  if (expressConfig.cors && expressConfig.cors.enabled) {
285
200
 
286
- server.use(expressConfig.cors.path, (req, res, next) => {
201
+ vulkano.use(expressConfig.cors.path, (req, res, next) => {
287
202
 
288
203
  // Enable CORS.
289
204
  let tmpCorsHeaders = ['X-Requested-With', 'X-HTTP-Method-Override', 'Content-Type', 'Accept'];
@@ -312,7 +227,7 @@ module.exports = {
312
227
  ...(app.server.views || {})
313
228
  };
314
229
 
315
- server.set('views', views.path);
230
+ vulkano.set('views', views.path);
316
231
 
317
232
  const {
318
233
  settings: nunjucksSettingsUser
@@ -322,7 +237,7 @@ module.exports = {
322
237
  autoescape: true,
323
238
  watch: !app.PRODUCTION,
324
239
  ...(nunjucksSettingsUser || {}),
325
- express: server
240
+ express: vulkano
326
241
  };
327
242
 
328
243
  const envNunjucks = nunjucks.configure(views.path, nunjucksSettings);
@@ -363,6 +278,27 @@ module.exports = {
363
278
  });
364
279
  }
365
280
 
281
+ app.nunjucks = nunjucks;
282
+
283
+ // ---------------
284
+ // Middlewares
285
+ // ---------------
286
+
287
+ // Middleware File (compatibility)
288
+ const middleware = app.config.middleware || ((req, res, next) => {
289
+ next();
290
+ });
291
+
292
+ // Middleware Folder
293
+ const middlewares = app.config.middlewares || {};
294
+
295
+ Object.keys(middlewares).forEach( (item) => {
296
+ const middlewareFunction = middlewares[item];
297
+ if (typeof middlewareFunction === 'function') {
298
+ vulkano.use(middlewareFunction);
299
+ }
300
+ });
301
+
366
302
  // ---------------
367
303
  // ROUTES
368
304
  // ---------------
@@ -393,9 +329,9 @@ module.exports = {
393
329
  handler = routes[route];
394
330
 
395
331
  if (method === 'post') {
396
- server[method](pathToRoute, upload.any(), middleware, handler);
332
+ vulkano[method](pathToRoute, upload.any(), middleware, handler);
397
333
  } else {
398
- server[method](pathToRoute, middleware, handler);
334
+ vulkano[method](pathToRoute, middleware, handler);
399
335
  }
400
336
 
401
337
  });
@@ -443,9 +379,9 @@ module.exports = {
443
379
 
444
380
  if (toExecute) {
445
381
  if (option === 'post') {
446
- server[option](pathToRun || '/', upload.any(), middleware, toExecute);
382
+ vulkano[option](pathToRun || '/', upload.any(), middleware, toExecute);
447
383
  } else {
448
- server[option](pathToRun || '/', middleware, toExecute);
384
+ vulkano[option](pathToRun || '/', middleware, toExecute);
449
385
  }
450
386
  } else {
451
387
  console.error('\x1b[31mError:', 'Controller not found in', (module) ? `${module}.${controller}.${action}` : `${controller}.${action}`, '\x1b[0m');
@@ -453,10 +389,12 @@ module.exports = {
453
389
 
454
390
  });
455
391
 
392
+ const server = await vulkano.listen(expressConfig.port);
393
+
456
394
  // ---------------
457
395
  // ERROR 404
458
396
  // ---------------
459
- server.use((req, res) => {
397
+ vulkano.use((req, res) => {
460
398
  if (+res.statusCode >= 500 && +res.statusCode < 600) {
461
399
  throw new Error();
462
400
  }
@@ -466,14 +404,14 @@ module.exports = {
466
404
  // ---------------
467
405
  // ERROR 5XX
468
406
  // ---------------
469
- server.use((err, req, res) => {
407
+ vulkano.use((err, req, res) => {
470
408
  const status = err.status || res.statusCode || 500;
471
409
  res.status(status);
472
410
  if (!res.xhr) {
473
411
  if (+status > 400 && +status < 500) {
474
- res.render(`${server.get('views')}/_shared/errors/404.html`, { content: err.stack });
412
+ res.render(`${vulkano.get('views')}/_shared/errors/404.html`, { content: err.stack });
475
413
  } else {
476
- res.render(`${server.get('views')}/_shared/errors/500.html`, { content: err.stack });
414
+ res.render(`${vulkano.get('views')}/_shared/errors/500.html`, { content: err.stack });
477
415
  }
478
416
  } else {
479
417
  res.jsonp({
@@ -485,154 +423,14 @@ module.exports = {
485
423
  });
486
424
 
487
425
  // ---------------
488
- // SOCKETS
426
+ // PUBLIC PATH - File: app/config/settings.js
489
427
  // ---------------
490
- if (sockets.enabled) {
491
-
492
- const socketProps = {
493
- pingTimeout: +sockets.timeout || 4000,
494
- pingInterval: +sockets.interval || 2000,
495
- transports: sockets.transports || ['websocket', 'polling']
496
- };
497
-
498
- if (sockets.cors) {
499
- if (typeof sockets.cors === 'function') {
500
- socketProps.allowRequest = sockets.cors;
501
- } else if (typeof sockets.cors === 'string') {
502
- socketProps.cors = sockets.cors || '';
503
- }
504
- }
505
-
506
- if (sockets.redis && !redis.enabled) {
507
- throw new Error('Enable the Redis config "app/config/redis.js" to connect the sockets');
508
- }
509
-
510
- const io = new Server(server.listen(expressConfig.port), socketProps);
428
+ vulkano.use(express.static(PUBLIC_PATH));
511
429
 
512
- let pubClient = null;
513
- let subClient = null;
430
+ app.vulkano = vulkano;
431
+ app.server = server;
514
432
 
515
- if (sockets.redis) {
516
-
517
- const propsToRedis = {
518
- host: redis.host,
519
- port: redis.port
520
- };
521
-
522
- if (redis.password) {
523
- propsToRedis.password = redis.password;
524
- }
525
-
526
- pubClient = createClient(propsToRedis);
527
- subClient = pubClient.duplicate();
528
-
529
- io.adapter(createAdapter(pubClient, subClient));
530
-
531
- }
532
-
533
- Promise
534
- .all([
535
- (sockets.redis ? pubClient.connect() : null),
536
- (sockets.redis ? subClient.connect() : null)
537
- ])
538
- .then(() => {
539
-
540
- io.on('connection', (socket) => {
541
-
542
- if ( typeof sockets.onConnect === 'function') {
543
- sockets.onConnect(socket);
544
- }
545
-
546
- const socketEvents = sockets.events || {};
547
-
548
- Object.keys(socketEvents).forEach( (i) => {
549
-
550
- const checkPath = socketEvents[i] || '';
551
-
552
- let toExecute = null;
553
- let module = null;
554
- let controller = null;
555
- let action = null;
556
-
557
- if (typeof checkPath === 'function') {
558
-
559
- toExecute = checkPath;
560
-
561
- } else {
562
-
563
- const fullPath = checkPath.split('.');
564
-
565
- if (fullPath.length > 2) { // Has folder
566
-
567
- [
568
- module,
569
- controller,
570
- action
571
- ] = fullPath;
572
-
573
- } else {
574
-
575
- [
576
- controller,
577
- action
578
- ] = fullPath;
579
-
580
- }
581
-
582
- try {
583
- toExecute = module
584
- ? (AllControllers[module][controller][action])
585
- : AllControllers[controller][action];
586
- } catch (e) {
587
- toExecute = null;
588
- }
589
-
590
- }
591
-
592
- if (toExecute) {
593
- socket.on(i, (body) => {
594
- toExecute({ socket, body: body || {} });
595
- });
596
- } else {
597
- console.error('\x1b[31mError:', 'Controller not found in', (module) ? `${module}.${controller}.${action}` : `${controller}.${action}`, '\x1b[0m', 'to socket event', i);
598
- }
599
-
600
- });
601
-
602
- server.set('socket', socket);
603
- app.socket = socket;
604
-
605
- });
606
-
607
- // next line is the money
608
- global.io = io;
609
- server.set('socketio', io);
610
-
611
- // middleware
612
- if (sockets.middleware) {
613
- io.use(sockets.middleware);
614
- }
615
-
616
- app.server = server;
617
-
618
- cb();
619
-
620
- });
621
-
622
- return;
623
-
624
- }
625
-
626
- // ---------------
627
- // START SERVER
628
- // ---------------
629
- server.listen(server.get('port'), () => {
630
- app.server = {
631
- ...app.server,
632
- ...server
633
- };
634
- cb();
635
- });
433
+ cb();
636
434
 
637
435
  }
638
436
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vulkano/core",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "A MVC framework using Express 4",
5
5
  "license": "MIT",
6
6
  "author": "Vulkano Team",