@vulkano/core 1.2.0 → 1.3.1

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')({
@@ -24,111 +23,39 @@ const AllControllers = require('include-all')({
24
23
  optional: true
25
24
  });
26
25
 
26
+ // Views Config
27
27
  const viewsConfig = require('./views');
28
28
 
29
- const responses = require('./responses');
29
+ // JWT Middleware
30
+ const jwtMiddleware = require('../libs/Jwt');
31
+
32
+ // Express Config
33
+ const expressConfig = require('./express')();
30
34
 
31
- const JWT = require('../libs/Jwt');
35
+ // Express Responses
36
+ const responses = require('./responses');
32
37
 
33
38
  module.exports = {
34
39
 
35
40
  routes: {},
36
41
 
37
- start: function loadServerApplication(cb) {
38
-
39
- const jwtMiddleware = JWT;
40
-
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 || {};
42
+ start: async function loadServerApplication(cb) {
68
43
 
44
+ // Get ENV Vars
69
45
  const {
70
- NODE_PORT: ENV_NODE_PORT,
71
- PORT: ENV_PORT
46
+ JWT_SECRET_KEY,
47
+ COOKIES_SECRET_KEY
72
48
  } = process.env || {};
73
49
 
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();
50
+ const vulkano = express();
124
51
 
125
52
  // Settings
126
- server.enable('trust proxy');
53
+ vulkano.enable('trust proxy');
127
54
 
128
55
  // ---------------
129
56
  // PORT - File: app/config/express/settings.js
130
57
  // ---------------
131
- server.set('port', expressConfig.port);
58
+ vulkano.set('port', expressConfig.port);
132
59
 
133
60
  // ---------------
134
61
  // MULTER - File: app/config/express/multer.js
@@ -138,53 +65,48 @@ module.exports = {
138
65
  // ---------------
139
66
  // MORGAN - File: app/config/express/morgan.js
140
67
  // ---------------
141
- server.use(morgan(expressConfig.morgan.format, expressConfig.morgan));
68
+ vulkano.use(morgan(expressConfig.morgan.format, expressConfig.morgan));
142
69
 
143
70
  // ---------------
144
71
  // USER AGENT
145
72
  // ---------------
146
- server.use(useragent.express());
73
+ vulkano.use(useragent.express());
147
74
 
148
75
  // ---------------
149
76
  // COMPRESSION - File: app/config/express/compression.js
150
77
  // ---------------
151
- server.use(compression( expressConfig.compression || {} ));
78
+ vulkano.use(compression( expressConfig.compression || {} ));
152
79
 
153
80
  // ---------------
154
81
  // COOKIES - File: app/config/express/cookies.js
155
82
  // ---------------
156
83
  if (expressConfig.cookies && expressConfig.cookies.enabled) {
157
- 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 || '';
158
85
  if (!cookiesSecretKey) {
159
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.');
160
87
  }
161
- server.use(cookieParser(cookiesSecretKey));
88
+ vulkano.use(cookieParser(cookiesSecretKey));
162
89
  }
163
90
 
164
91
  // ---------------
165
92
  // EXPRESS JSON - File: app/config/express/json.js
166
93
  // ---------------
167
- server.use(express.json(expressConfig.json));
94
+ vulkano.use(express.json(expressConfig.json));
168
95
 
169
96
  // ---------------
170
97
  // EXPRESS FORM DATA - File: app/config/express/urlencoded.js
171
98
  // ---------------
172
- server.use(express.urlencoded(expressConfig.urlencoded));
99
+ vulkano.use(express.urlencoded(expressConfig.urlencoded));
173
100
 
174
101
  // ---------------
175
102
  // HELMET - File: app/config/express/helmet.js
176
103
  // ---------------
177
- server.use(helmet(expressConfig.helmet));
104
+ vulkano.use(helmet(expressConfig.helmet));
178
105
 
179
106
  // ---------------
180
107
  // RESPONSES
181
108
  // ---------------
182
- server.use(responses);
183
-
184
- // ---------------
185
- // PUBLIC PATH - File: app/config/settings.js
186
- // ---------------
187
- server.use(express.static(PUBLIC_PATH));
109
+ vulkano.use(responses);
188
110
 
189
111
  // ---------------
190
112
  // FRAMEGUARD - File: app/config/express/frameguard.js
@@ -192,17 +114,17 @@ module.exports = {
192
114
  if (expressConfig.frameguard) {
193
115
  if (Array.isArray(expressConfig.frameguard)) {
194
116
  expressConfig.frameguard.forEach( (frame) => {
195
- server.use(frameguard(frame));
117
+ vulkano.use(frameguard(frame));
196
118
  });
197
119
  } else {
198
- server.use(frameguard(expressConfig.frameguard));
120
+ vulkano.use(frameguard(expressConfig.frameguard));
199
121
  }
200
122
  }
201
123
 
202
124
  // ---------------
203
125
  // PROTOCOL & POWERED BY - File: app/config/settings.js
204
126
  // ---------------
205
- server.use( (req, res, next) => {
127
+ vulkano.use( (req, res, next) => {
206
128
 
207
129
  const proto = req.secure ? 'https' : 'http';
208
130
  const forwarded = req.headers['x-forwaded-proto'] || null;
@@ -220,7 +142,7 @@ module.exports = {
220
142
  // ---------------
221
143
  // REQUEST OPTIONS - File: app/config/express/cors.js
222
144
  // ---------------
223
- server.options('*', (req, res) => {
145
+ vulkano.options('*', (req, res) => {
224
146
 
225
147
  // ---------------
226
148
  // CORS
@@ -244,8 +166,9 @@ module.exports = {
244
166
  // ---------------
245
167
  // TIMEOUT - File: app/config/settings.js
246
168
  // ---------------
247
- server.use(timeout( expressConfig.timeout || 120000 ));
248
- server.use( (req, res, next) => {
169
+ vulkano.use(timeout( expressConfig.timeout || 120000 ));
170
+
171
+ vulkano.use( (req, res, next) => {
249
172
  if (!req.timedout) {
250
173
  next();
251
174
  }
@@ -256,19 +179,19 @@ module.exports = {
256
179
  // ---------------
257
180
  if (expressConfig.jwt && expressConfig.jwt.enabled) {
258
181
 
259
- 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 || '';
260
183
  if (!jwtSecretKey) {
261
184
  console.log(' \x1b[41mERROR\x1b[0m: Can not get key in config/express/jwt.js file or JWT_SECRET_KEY in .env file');
262
185
  return;
263
186
  }
264
187
 
265
188
  // JWT (secret key)
266
- server.use(expressConfig.jwt.path || '*', jwtMiddleware.init().unless({
189
+ vulkano.use(expressConfig.jwt.path || '*', jwtMiddleware.init().unless({
267
190
  path: expressConfig.jwt.ignore || []
268
191
  }));
269
192
 
270
193
  // JWT Handler error
271
- server.use((err, req, res, next) => {
194
+ vulkano.use((err, req, res, next) => {
272
195
  if (err && err.name === 'UnauthorizedError') {
273
196
  res.status(401).jsonp({ success: false, error: 'Invalid token' });
274
197
  } else {
@@ -283,7 +206,7 @@ module.exports = {
283
206
  // ---------------
284
207
  if (expressConfig.cors && expressConfig.cors.enabled) {
285
208
 
286
- server.use(expressConfig.cors.path, (req, res, next) => {
209
+ vulkano.use(expressConfig.cors.path, (req, res, next) => {
287
210
 
288
211
  // Enable CORS.
289
212
  let tmpCorsHeaders = ['X-Requested-With', 'X-HTTP-Method-Override', 'Content-Type', 'Accept'];
@@ -312,7 +235,7 @@ module.exports = {
312
235
  ...(app.server.views || {})
313
236
  };
314
237
 
315
- server.set('views', views.path);
238
+ vulkano.set('views', views.path);
316
239
 
317
240
  const {
318
241
  settings: nunjucksSettingsUser
@@ -322,7 +245,7 @@ module.exports = {
322
245
  autoescape: true,
323
246
  watch: !app.PRODUCTION,
324
247
  ...(nunjucksSettingsUser || {}),
325
- express: server
248
+ express: vulkano
326
249
  };
327
250
 
328
251
  const envNunjucks = nunjucks.configure(views.path, nunjucksSettings);
@@ -363,6 +286,35 @@ module.exports = {
363
286
  });
364
287
  }
365
288
 
289
+ app.nunjucks = nunjucks;
290
+
291
+ // ---------------
292
+ // Middlewares
293
+ // ---------------
294
+
295
+ // Middleware File (compatibility)
296
+ const middleware = app.config.middleware || ((req, res, next) => {
297
+ next();
298
+ });
299
+
300
+ // Middleware Folder
301
+ const middlewares = app.config.middlewares || {};
302
+
303
+ Object.keys(middlewares).forEach( (item) => {
304
+ const middlewareFunction = middlewares[item];
305
+ if (typeof middlewareFunction === 'function') {
306
+ vulkano.use(middlewareFunction);
307
+ }
308
+ });
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
+
366
318
  // ---------------
367
319
  // ROUTES
368
320
  // ---------------
@@ -393,9 +345,9 @@ module.exports = {
393
345
  handler = routes[route];
394
346
 
395
347
  if (method === 'post') {
396
- server[method](pathToRoute, upload.any(), middleware, handler);
348
+ vulkano[method](pathToRoute, upload.any(), middleware, handler);
397
349
  } else {
398
- server[method](pathToRoute, middleware, handler);
350
+ vulkano[method](pathToRoute, middleware, handler);
399
351
  }
400
352
 
401
353
  });
@@ -443,9 +395,9 @@ module.exports = {
443
395
 
444
396
  if (toExecute) {
445
397
  if (option === 'post') {
446
- server[option](pathToRun || '/', upload.any(), middleware, toExecute);
398
+ vulkano[option](pathToRun || '/', upload.any(), middleware, toExecute);
447
399
  } else {
448
- server[option](pathToRun || '/', middleware, toExecute);
400
+ vulkano[option](pathToRun || '/', middleware, toExecute);
449
401
  }
450
402
  } else {
451
403
  console.error('\x1b[31mError:', 'Controller not found in', (module) ? `${module}.${controller}.${action}` : `${controller}.${action}`, '\x1b[0m');
@@ -453,10 +405,12 @@ module.exports = {
453
405
 
454
406
  });
455
407
 
408
+ const server = await vulkano.listen(expressConfig.port);
409
+
456
410
  // ---------------
457
411
  // ERROR 404
458
412
  // ---------------
459
- server.use((req, res) => {
413
+ vulkano.use((req, res) => {
460
414
  if (+res.statusCode >= 500 && +res.statusCode < 600) {
461
415
  throw new Error();
462
416
  }
@@ -466,14 +420,14 @@ module.exports = {
466
420
  // ---------------
467
421
  // ERROR 5XX
468
422
  // ---------------
469
- server.use((err, req, res) => {
423
+ vulkano.use((err, req, res) => {
470
424
  const status = err.status || res.statusCode || 500;
471
425
  res.status(status);
472
426
  if (!res.xhr) {
473
427
  if (+status > 400 && +status < 500) {
474
- res.render(`${server.get('views')}/_shared/errors/404.html`, { content: err.stack });
428
+ res.render(`${vulkano.get('views')}/_shared/errors/404.html`, { content: err.stack });
475
429
  } else {
476
- res.render(`${server.get('views')}/_shared/errors/500.html`, { content: err.stack });
430
+ res.render(`${vulkano.get('views')}/_shared/errors/500.html`, { content: err.stack });
477
431
  }
478
432
  } else {
479
433
  res.jsonp({
@@ -485,154 +439,17 @@ module.exports = {
485
439
  });
486
440
 
487
441
  // ---------------
488
- // SOCKETS
442
+ // PUBLIC PATH
443
+ // (if not are using Vite)
489
444
  // ---------------
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);
511
-
512
- let pubClient = null;
513
- let subClient = null;
514
-
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
-
445
+ if (app.viteProxy) {
446
+ vulkano.use(express.static(PUBLIC_PATH));
624
447
  }
625
448
 
626
- // ---------------
627
- // START SERVER
628
- // ---------------
629
- server.listen(server.get('port'), () => {
630
- app.server = {
631
- ...app.server,
632
- ...server
633
- };
634
- cb();
635
- });
449
+ app.vulkano = vulkano;
450
+ app.server = server;
451
+
452
+ cb();
636
453
 
637
454
  }
638
455
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vulkano/core",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "A MVC framework using Express 4",
5
5
  "license": "MIT",
6
6
  "author": "Vulkano Team",