miolo 0.2.3 → 0.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.
Files changed (78) hide show
  1. package/README.md +34 -1
  2. package/dist/cli/miolo.cli.iife.bundle.js +374 -0
  3. package/dist/cli/miolo.cli.iife.bundle.js.map +1 -0
  4. package/dist/cli/miolo.cli.iife.bundle.min.js +22 -0
  5. package/dist/cli/miolo.cli.iife.js +76 -0
  6. package/dist/cli/miolo.cli.iife.js.map +1 -0
  7. package/dist/cli/miolo.cli.iife.min.js +11 -0
  8. package/dist/cli/miolo.cli.min.mjs +11 -0
  9. package/dist/cli/miolo.cli.mjs +69 -0
  10. package/dist/cli/miolo.cli.mjs.map +1 -0
  11. package/dist/cli/miolo.cli.umd.bundle.js +375 -0
  12. package/dist/cli/miolo.cli.umd.bundle.js.map +1 -0
  13. package/dist/cli/miolo.cli.umd.bundle.min.js +22 -0
  14. package/dist/cli/miolo.cli.umd.js +77 -0
  15. package/dist/cli/miolo.cli.umd.js.map +1 -0
  16. package/dist/cli/miolo.cli.umd.min.js +11 -0
  17. package/dist/cli-react/miolo.cli-react.iife.bundle.js +1690 -0
  18. package/dist/cli-react/miolo.cli-react.iife.bundle.js.map +1 -0
  19. package/dist/cli-react/miolo.cli-react.iife.bundle.min.js +62 -0
  20. package/dist/cli-react/miolo.cli-react.iife.js +531 -0
  21. package/dist/cli-react/miolo.cli-react.iife.js.map +1 -0
  22. package/dist/cli-react/miolo.cli-react.iife.min.js +11 -0
  23. package/dist/cli-react/miolo.cli-react.min.mjs +11 -0
  24. package/dist/cli-react/miolo.cli-react.mjs +187 -0
  25. package/dist/cli-react/miolo.cli-react.mjs.map +1 -0
  26. package/dist/cli-react/miolo.cli-react.umd.bundle.js +1691 -0
  27. package/dist/cli-react/miolo.cli-react.umd.bundle.js.map +1 -0
  28. package/dist/cli-react/miolo.cli-react.umd.bundle.min.js +62 -0
  29. package/dist/cli-react/miolo.cli-react.umd.js +532 -0
  30. package/dist/cli-react/miolo.cli-react.umd.js.map +1 -0
  31. package/dist/cli-react/miolo.cli-react.umd.min.js +11 -0
  32. package/dist/server/miolo.ico +0 -0
  33. package/dist/{miolo.cjs → server/miolo.server.cjs} +320 -208
  34. package/dist/server/miolo.server.min.mjs +11 -0
  35. package/dist/{miolo.mjs → server/miolo.server.mjs} +292 -159
  36. package/dist/server/miolo.server.mjs.map +1 -0
  37. package/dist/{miolo.node.mjs → server/miolo.server.node.mjs} +321 -208
  38. package/dist/static/img/miolo.ico +0 -0
  39. package/dist/static/robots.txt +2 -0
  40. package/logo/favicon/miolo.ico +0 -0
  41. package/logo/favicon/miolo.png +0 -0
  42. package/logo/favicon/miolo_apple.png +0 -0
  43. package/logo/miolo-logo-name_200.png +0 -0
  44. package/package.json +63 -20
  45. package/dist/miolo.min.mjs +0 -11
  46. package/dist/miolo.mjs.map +0 -1
  47. package/lib/cacher/index.cjs +0 -76
  48. package/lib/cacher/verify.cjs +0 -27
  49. package/lib/config/defaults.cjs +0 -287
  50. package/lib/config/index.cjs +0 -12
  51. package/lib/emailer/index.cjs +0 -56
  52. package/lib/emailer/verify.cjs +0 -11
  53. package/lib/index.cjs +0 -40
  54. package/lib/logger/index.cjs +0 -91
  55. package/lib/logger/logger_mail.cjs +0 -56
  56. package/lib/logger/verify.cjs +0 -22
  57. package/lib/server/engines/cron/index.cjs +0 -12
  58. package/lib/server/engines/cron/syscheck.cjs +0 -41
  59. package/lib/server/engines/socket/index.cjs +0 -40
  60. package/lib/server/index.cjs +0 -134
  61. package/lib/server/middleware/auth/basic.cjs +0 -53
  62. package/lib/server/middleware/auth/guest.cjs +0 -72
  63. package/lib/server/middleware/auth/passport.cjs +0 -123
  64. package/lib/server/middleware/body.cjs +0 -50
  65. package/lib/server/middleware/catcher/index.cjs +0 -58
  66. package/lib/server/middleware/context.cjs +0 -23
  67. package/lib/server/middleware/extra.cjs +0 -27
  68. package/lib/server/middleware/headers.cjs +0 -68
  69. package/lib/server/middleware/request.cjs +0 -68
  70. package/lib/server/middleware/session/index.cjs +0 -26
  71. package/lib/server/middleware/session/store.cjs +0 -10
  72. package/lib/server/middleware/static.cjs +0 -25
  73. package/lib/server/routes/catch_js_error.cjs +0 -35
  74. package/lib/server/routes/html_render.cjs +0 -37
  75. package/lib/server/routes/robots.cjs +0 -27
  76. /package/{lib/server/routes → dist/server}/fallback_index.html +0 -0
  77. /package/{lib/server/static → dist/server}/robots.txt +0 -0
  78. /package/{lib/server/static/img/favicon.ico → dist/static/img/afialapis.ico} +0 -0
@@ -1,5 +1,5 @@
1
1
  /**
2
- * miolo v0.2.3
2
+ * miolo v0.3.0
3
3
  *
4
4
  * Copyright (c) Donato Lorenzo <donato@afialapis.com>
5
5
  *
@@ -11,17 +11,18 @@
11
11
  'use strict';
12
12
 
13
13
  var Koa = require('koa');
14
+ var router = require('calustra/router');
14
15
  var merge = require('assign-deep');
15
16
  var path = require('node:path');
16
17
  var node_url = require('node:url');
17
- var nodemailer = require('nodemailer');
18
- var tinguir = require('tinguir');
19
- var util = require('node:util');
20
- var winston = require('winston');
21
18
  var os = require('node:os');
22
19
  var diskspace = require('diskspace');
20
+ var tinguir = require('tinguir');
23
21
  var cron = require('cron');
24
- var calustraRouter = require('calustra-router');
22
+ var calustra = require('calustra');
23
+ var nodemailer = require('nodemailer');
24
+ var util = require('node:util');
25
+ var winston = require('winston');
25
26
  var koa_cors = require('@koa/cors');
26
27
  var koa_proxy = require('koa-proxies');
27
28
  var koa_body_parser = require('koa-better-body');
@@ -34,9 +35,11 @@ var koa_favicon = require('koa-favicon');
34
35
  var node_perf_hooks = require('node:perf_hooks');
35
36
  var koa_session = require('koa-session');
36
37
  var RedisStore = require('koa-redis');
37
- var fs = require('node:fs');
38
38
  var Router = require('@koa/router');
39
- var redis = require('redis');
39
+ var node_fs = require('node:fs');
40
+ var React = require('react');
41
+ var server = require('react-router-dom/server');
42
+ var server$1 = require('react-dom/server');
40
43
  var jwt = require('jwt-simple');
41
44
  var auth = require('basic-auth');
42
45
  var passport$1 = require('koa-passport');
@@ -388,9 +391,9 @@ function _extends() {
388
391
  return _extends.apply(this, arguments);
389
392
  }
390
393
 
391
- var __my_filename = node_url.fileURLToPath(require('url').pathToFileURL(__filename).toString());
392
- var __my_dirname = path.dirname(__my_filename);
393
- var favicon = path.resolve(__my_dirname, '../server/static/ig/favicon.ico');
394
+ var __my_filename$3 = node_url.fileURLToPath(require('url').pathToFileURL(__filename).toString());
395
+ var __my_dirname$3 = path.dirname(__my_filename$3);
396
+ var favicon = path.resolve(__my_dirname$3, '../static/img/miolo.ico');
394
397
  var base_config = {
395
398
  name: 'miolo',
396
399
  http: {
@@ -447,27 +450,25 @@ var base_config = {
447
450
  },
448
451
 
449
452
  db: {
450
- connection: {
451
- database: {
452
- dialect: 'postgres',
453
- host: 'localhost',
454
- port: 5432,
455
- database: 'miolo',
456
- user: 'postgres',
457
- password: 'postgres',
458
- max: 5,
459
- // Maximum number of connection in pool
460
- min: 0,
461
- // Minimum number of connection in pool
462
- idleTimeoutMillis: 10000 // The maximum time, in milliseconds, that a connection can be idle before being released. Use with combination of evict for proper working, for more details read https://github.com/coopernurse/node-pool/issues/178#issuecomment-327110870,
463
- },
464
-
465
- options: {
466
- log: 'silly' // will be updated on the fly with miolo logger
467
- }
453
+ config: {
454
+ dialect: 'postgres',
455
+ host: 'localhost',
456
+ port: 5432,
457
+ database: 'miolo',
458
+ user: 'postgres',
459
+ password: 'postgres',
460
+ max: 5,
461
+ // Maximum number of connection in pool
462
+ min: 0,
463
+ // Minimum number of connection in pool
464
+ idleTimeoutMillis: 10000 // The maximum time, in milliseconds, that a connection can be idle before being released. Use with combination of evict for proper working, for more details read https://github.com/coopernurse/node-pool/issues/178#issuecomment-327110870,
468
465
  },
469
466
 
470
- tables: []
467
+ options: {
468
+ log: 'silly',
469
+ // will be updated on the fly with miolo logger
470
+ tables: []
471
+ }
471
472
  },
472
473
  routes: {
473
474
  bodyField: undefined,
@@ -513,12 +514,12 @@ var base_config = {
513
514
  queries: undefined
514
515
  },
515
516
  catcher: '/sys/jserror',
516
- cacher: {
517
- redis: {
518
- host: '127.0.0.1',
519
- port: 6379
520
- }
521
- },
517
+ //cacher: {
518
+ // redis: {
519
+ // host: '127.0.0.1',
520
+ // port: 6379
521
+ // },
522
+ //},
522
523
  log: {
523
524
  level: 'debug',
524
525
  format: {
@@ -670,6 +671,43 @@ function init_config(config) {
670
671
  return merge(base_config, config);
671
672
  }
672
673
 
674
+ function init_sys_check_and_log(logger) {
675
+ function toMB(bytes) {
676
+ if (!bytes) return 0;
677
+ return bytes / 1000000;
678
+ }
679
+ function toGB(kbytes) {
680
+ if (!kbytes) return 0;
681
+ return kbytes / 1000000;
682
+ }
683
+ function sys_check_and_log() {
684
+ var used = Math.round(toMB(os.freemem()), 2);
685
+ var total = Math.round(toMB(os.totalmem()), 2);
686
+ var perc = Math.round(used * 100 / total, 2);
687
+ if (perc > 80) {
688
+ logger.error("[SERVER][" + tinguir.cyan('SysCheck') + "] RAM " + tinguir.yellow(used) + " MB used of " + tinguir.green(total) + " MB (" + tinguir.yellow(perc) + " %)");
689
+ } else {
690
+ logger.info("[SERVER][" + tinguir.cyan('SysCheck') + "] RAM " + tinguir.yellow(used) + " MB used of " + tinguir.green(total) + " MB (" + tinguir.yellow(perc) + " %)");
691
+ }
692
+ diskspace.check('/', function (err, result) {
693
+ var used = Math.round(toGB(result.used), 2);
694
+ var total = Math.round(toGB(result.total), 2);
695
+ var free = Math.round(toGB(result.free), 2);
696
+ if (free < 1) {
697
+ logger.error("[SERVER][" + tinguir.cyan('SysCheck') + "] DISK " + tinguir.yellow(used) + " GB used of " + tinguir.green(total) + " GB (" + tinguir.yellow(free) + " GB free)");
698
+ } else {
699
+ logger.info("[SERVER][" + tinguir.cyan('SysCheck') + "] DISK " + tinguir.yellow(used) + " GB used of " + tinguir.green(total) + " GB (" + tinguir.yellow(free) + " GB free)");
700
+ }
701
+ });
702
+ }
703
+ return sys_check_and_log;
704
+ }
705
+
706
+ function init_cron(logger) {
707
+ var sys_check_and_log = init_sys_check_and_log(logger);
708
+ new cron.CronJob('30 * * * *', sys_check_and_log, null, true, 'Europe/Madrid');
709
+ }
710
+
673
711
  function init_emailer(options, defaults, silent) {
674
712
  if (silent === void 0) {
675
713
  silent = false;
@@ -844,42 +882,52 @@ var init_logger = function init_logger(config, emailer) {
844
882
  return logger;
845
883
  };
846
884
 
847
- function init_sys_check_and_log(logger) {
848
- function toMB(bytes) {
849
- if (!bytes) return 0;
850
- return bytes / 1000000;
851
- }
852
- function toGB(kbytes) {
853
- if (!kbytes) return 0;
854
- return kbytes / 1000000;
885
+ var init_context_middleware = function init_context_middleware(app, config) {
886
+ var emailer = init_emailer(config.mail.options, config.mail.defaults);
887
+ var logger = init_logger(config.log, emailer);
888
+ config.db.options.log = logger;
889
+ var getConnectionWrap = function getConnectionWrap() {
890
+ return calustra.getConnection(config.db.config, config.db.options);
891
+ };
892
+ var getModelWrap = function getModelWrap(name) {
893
+ var conn = calustra.getConnection(config.db.config, config.db.options);
894
+ return conn.getModel(name);
895
+ };
896
+ var db = {
897
+ getConnection: getConnectionWrap,
898
+ getModel: getModelWrap
899
+ };
900
+
901
+ // Assign miolo stuff to ctx
902
+ var mioloContext = {
903
+ config: _extends({}, config),
904
+ emailer: emailer,
905
+ logger: logger,
906
+ db: db
907
+ };
908
+ function context_middleware(_x, _x2) {
909
+ return _context_middleware.apply(this, arguments);
855
910
  }
856
- function sys_check_and_log() {
857
- var used = Math.round(toMB(os.freemem()), 2);
858
- var total = Math.round(toMB(os.totalmem()), 2);
859
- var perc = Math.round(used * 100 / total, 2);
860
- if (perc > 80) {
861
- logger.error("[SERVER][" + tinguir.cyan('SysCheck') + "] RAM " + tinguir.yellow(used) + " MB used of " + tinguir.green(total) + " MB (" + tinguir.yellow(perc) + " %)");
862
- } else {
863
- logger.info("[SERVER][" + tinguir.cyan('SysCheck') + "] RAM " + tinguir.yellow(used) + " MB used of " + tinguir.green(total) + " MB (" + tinguir.yellow(perc) + " %)");
864
- }
865
- diskspace.check('/', function (err, result) {
866
- var used = Math.round(toGB(result.used), 2);
867
- var total = Math.round(toGB(result.total), 2);
868
- var free = Math.round(toGB(result.free), 2);
869
- if (free < 1) {
870
- logger.error("[SERVER][" + tinguir.cyan('SysCheck') + "] DISK " + tinguir.yellow(used) + " GB used of " + tinguir.green(total) + " GB (" + tinguir.yellow(free) + " GB free)");
871
- } else {
872
- logger.info("[SERVER][" + tinguir.cyan('SysCheck') + "] DISK " + tinguir.yellow(used) + " GB used of " + tinguir.green(total) + " GB (" + tinguir.yellow(free) + " GB free)");
873
- }
874
- });
911
+ function _context_middleware() {
912
+ _context_middleware = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(ctx, next) {
913
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
914
+ while (1) switch (_context.prev = _context.next) {
915
+ case 0:
916
+ // Assign miolo stuff to ctx
917
+ ctx.miolo = mioloContext;
918
+ _context.next = 3;
919
+ return next();
920
+ case 3:
921
+ case "end":
922
+ return _context.stop();
923
+ }
924
+ }, _callee);
925
+ }));
926
+ return _context_middleware.apply(this, arguments);
875
927
  }
876
- return sys_check_and_log;
877
- }
878
-
879
- function init_cron(logger) {
880
- var sys_check_and_log = init_sys_check_and_log(logger);
881
- new cron.CronJob('30 * * * *', sys_check_and_log, null, true, 'Europe/Madrid');
882
- }
928
+ app.use(context_middleware);
929
+ app.context.miolo = mioloContext;
930
+ };
883
931
 
884
932
  var _if_options = function _if_options(http, field, callback) {
885
933
  try {
@@ -911,7 +959,8 @@ var _proxy_options = function _proxy_options(options) {
911
959
  };
912
960
  return [tpath, toptions];
913
961
  };
914
- var init_headers_middleware = function init_headers_middleware(app, http, logger) {
962
+ var init_headers_middleware = function init_headers_middleware(app, http) {
963
+ var logger = app.context.miolo.logger;
915
964
  _if_options(http, 'cors', function (options) {
916
965
  if (options == 'simple') {
917
966
  logger.debug("Setting CORS the simple way");
@@ -957,7 +1006,13 @@ var init_body_middleware = function init_body_middleware(app) {
957
1006
  return content_type == 'application/json' || content_type == 'text/html';
958
1007
  },
959
1008
  //threshold: 2048,
960
- flush: node_zlib.Z_SYNC_FLUSH
1009
+ gzip: {
1010
+ flush: node_zlib.constants.Z_SYNC_FLUSH
1011
+ },
1012
+ deflate: {
1013
+ flush: node_zlib.constants.Z_SYNC_FLUSH
1014
+ },
1015
+ br: false // disable brotli
961
1016
  }));
962
1017
 
963
1018
  // body parser - koa-better-body
@@ -971,7 +1026,7 @@ var init_body_middleware = function init_body_middleware(app) {
971
1026
  // https://github.com/rkusa/koa-passport/issues/33#issuecomment-122611613
972
1027
  // https://github.com/tunnckoCore/opensource/tree/master/%40packages/koa-better-body#options
973
1028
  // {fields: 'body', files: 'body',}
974
- // But this will cause errors on other packagers (for example, calustra-router)
1029
+ // But this will cause errors on other packagers (for example, calustra/router)
975
1030
  // 2.- but still need to know how dangerous it is to touch request.body (as by default,
976
1031
  // koa-better-body is saving some async function in request.body)
977
1032
  // We patch the .body on the `request` middleware
@@ -992,7 +1047,8 @@ var init_body_middleware = function init_body_middleware(app) {
992
1047
  */
993
1048
 
994
1049
  var _ONLY_WARN = [401];
995
- function init_catcher_middleware(app, logger) {
1050
+ function init_catcher_middleware(app) {
1051
+ var logger = app.context.miolo.logger;
996
1052
  function catcher_middleware(_x) {
997
1053
  return _catcher_middleware.apply(this, arguments);
998
1054
  }
@@ -1052,7 +1108,9 @@ function init_catcher_middleware(app, logger) {
1052
1108
  app.context.onerror = catcher_middleware;
1053
1109
  }
1054
1110
 
1055
- var fallback_favicon_path = path.resolve(__dirname, '../static/img/favicon.ico');
1111
+ var __my_filename$2 = node_url.fileURLToPath(require('url').pathToFileURL(__filename).toString());
1112
+ var __my_dirname$2 = path.dirname(__my_filename$2);
1113
+ var fallback_favicon_path = path.resolve(__my_dirname$2, './miolo.ico');
1056
1114
  var init_static_middleware = function init_static_middleware(app, config) {
1057
1115
  var favicon = config.favicon,
1058
1116
  folders = config.folders;
@@ -1174,7 +1232,9 @@ function init_session_middleware(app, config) {
1174
1232
  app.use(koa_session(options, app));
1175
1233
  }
1176
1234
 
1177
- var robots_txt = fs.readFileSync(path.resolve(__dirname, '../static/robots.txt'), 'utf8');
1235
+ var __my_filename$1 = node_url.fileURLToPath(require('url').pathToFileURL(__filename).toString());
1236
+ var __my_dirname$1 = path.dirname(__my_filename$1);
1237
+ var robots_txt = node_fs.readFileSync(path.resolve(__my_dirname$1, './robots.txt'), 'utf8');
1178
1238
  function init_route_robots(app) {
1179
1239
  function robots(_x) {
1180
1240
  return _robots.apply(this, arguments);
@@ -1228,8 +1288,10 @@ function init_route_catch_js_error(app, route) {
1228
1288
  app.use(catch_js_error_router.routes());
1229
1289
  }
1230
1290
 
1231
- var indexHTMLPath = path.resolve(__dirname, 'fallback_index.html');
1232
- var indexHTML = fs.readFileSync(indexHTMLPath, 'utf8');
1291
+ var __my_filename = node_url.fileURLToPath(require('url').pathToFileURL(__filename).toString());
1292
+ var __my_dirname = path.dirname(__my_filename);
1293
+ var indexHTMLPath = path.resolve(__my_dirname, 'fallback_index.html');
1294
+ var indexHTML = node_fs.readFileSync(indexHTMLPath, 'utf8');
1233
1295
  function init_route_html_render(app, html) {
1234
1296
  // Server-side render
1235
1297
  function html_render(_x) {
@@ -1268,31 +1330,33 @@ function miolo(_x, _x2, _x3) {
1268
1330
  function _miolo() {
1269
1331
  _miolo = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(sconfig, render, callback) {
1270
1332
  var _config$http, _config$auth, _config$auth2, _config$auth3;
1271
- var config, emailer, logger, app, catcher_url, _yield$import, init_guest_auth_middleware, _yield$import2, init_basic_auth_middleware, _yield$import3, init_passport_auth_middleware, extra_middlewares;
1333
+ var app, config, catcher_url, _yield$import, init_guest_auth_middleware, _yield$import2, init_basic_auth_middleware, _yield$import3, init_passport_auth_middleware, conn, extra_middlewares;
1272
1334
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1273
1335
  while (1) switch (_context.prev = _context.next) {
1274
1336
  case 0:
1275
- // Init some pieces
1276
- config = init_config(sconfig);
1277
- emailer = init_emailer(config.mail.options, config.mail.defaults);
1278
- logger = init_logger(config.log, emailer);
1279
- config.db.connection.options.log = logger;
1280
- app = new Koa(); // attach to app calustra's db methods
1281
- calustraRouter.useCalustraDbContext(app, config.db);
1282
-
1337
+ app = new Koa(); // Init some pieces
1338
+ config = init_config(sconfig); //const emailer = init_emailer(config.mail.options, config.mail.defaults)
1339
+ //const logger = init_logger(config.log, emailer)
1340
+ //config.db.connection.options.log= logger
1341
+ // // attach to app calustra's db methods
1342
+ // initCalustraDbContext(app, config.db)
1343
+ //
1344
+ // // attach to app some custom miolo methods
1345
+ // app.context.miolo = {
1346
+ // config: {...config},
1347
+ // emailer,
1348
+ // logger,
1349
+ // db: app.context.db
1350
+ // }
1351
+ //
1283
1352
  // attach to app some custom miolo methods
1284
- app.context.miolo = {
1285
- config: _extends({}, config),
1286
- emailer: emailer,
1287
- logger: logger,
1288
- db: app.context.db
1289
- };
1353
+ init_context_middleware(app, config);
1290
1354
 
1291
1355
  // Compress and body parser
1292
1356
  init_body_middleware(app);
1293
1357
 
1294
1358
  // override koa's undocumented error handler
1295
- init_catcher_middleware(app, logger);
1359
+ init_catcher_middleware(app);
1296
1360
 
1297
1361
  // Serve static files
1298
1362
  init_static_middleware(app, (_config$http = config.http) == null ? void 0 : _config$http["static"]);
@@ -1314,41 +1378,42 @@ function _miolo() {
1314
1378
 
1315
1379
  // auth middleware
1316
1380
  if (!(config != null && (_config$auth = config.auth) != null && _config$auth.guest)) {
1317
- _context.next = 21;
1381
+ _context.next = 17;
1318
1382
  break;
1319
1383
  }
1320
- _context.next = 18;
1384
+ _context.next = 14;
1321
1385
  return Promise.resolve().then(function () { return guest; });
1322
- case 18:
1386
+ case 14:
1323
1387
  _yield$import = _context.sent;
1324
1388
  init_guest_auth_middleware = _yield$import.init_guest_auth_middleware;
1325
- init_guest_auth_middleware(app, config.auth.guest, config == null ? void 0 : config.session, logger);
1326
- case 21:
1389
+ init_guest_auth_middleware(app, config.auth.guest, config == null ? void 0 : config.session);
1390
+ case 17:
1327
1391
  if (!(config != null && (_config$auth2 = config.auth) != null && _config$auth2.basic)) {
1328
- _context.next = 27;
1392
+ _context.next = 23;
1329
1393
  break;
1330
1394
  }
1331
- _context.next = 24;
1395
+ _context.next = 20;
1332
1396
  return Promise.resolve().then(function () { return basic; });
1333
- case 24:
1397
+ case 20:
1334
1398
  _yield$import2 = _context.sent;
1335
1399
  init_basic_auth_middleware = _yield$import2.init_basic_auth_middleware;
1336
1400
  init_basic_auth_middleware(app, config.auth.basic);
1337
- case 27:
1401
+ case 23:
1338
1402
  if (!(config != null && (_config$auth3 = config.auth) != null && _config$auth3.passport)) {
1339
- _context.next = 33;
1403
+ _context.next = 29;
1340
1404
  break;
1341
1405
  }
1342
- _context.next = 30;
1406
+ _context.next = 26;
1343
1407
  return Promise.resolve().then(function () { return passport; });
1344
- case 30:
1408
+ case 26:
1345
1409
  _yield$import3 = _context.sent;
1346
1410
  init_passport_auth_middleware = _yield$import3.init_passport_auth_middleware;
1347
1411
  init_passport_auth_middleware(app, config.auth.passport);
1348
- case 33:
1412
+ case 29:
1349
1413
  // Routes to /crud
1350
1414
  if (config != null && config.routes) {
1351
- calustraRouter.useCalustraRouter(app, config.db, config.routes);
1415
+ conn = app.context.miolo.db.getConnection();
1416
+ router.initCalustraRouter(app, conn, config.routes);
1352
1417
  }
1353
1418
  // Socket.io
1354
1419
  // const io= init_socket(logger)
@@ -1361,7 +1426,7 @@ function _miolo() {
1361
1426
  }
1362
1427
 
1363
1428
  // CORS and other headers
1364
- init_headers_middleware(app, config.http, logger);
1429
+ init_headers_middleware(app, config.http);
1365
1430
 
1366
1431
  // Middleware for html render
1367
1432
  if (render == undefined || render.html != undefined) {
@@ -1370,14 +1435,14 @@ function _miolo() {
1370
1435
  app.use(render.middleware);
1371
1436
  }
1372
1437
  app.listen(config.http.port, config.http.hostname, function () {
1373
- logger.info("miolo is listening on " + config.http.hostname + ":" + config.http.port);
1374
- init_cron(logger);
1438
+ app.context.miolo.logger.info("miolo is listening on " + config.http.hostname + ":" + config.http.port);
1439
+ init_cron(app.context.miolo.logger);
1375
1440
  if (callback != undefined) {
1376
1441
  callback();
1377
1442
  }
1378
1443
  });
1379
1444
  return _context.abrupt("return", app);
1380
- case 40:
1445
+ case 36:
1381
1446
  case "end":
1382
1447
  return _context.stop();
1383
1448
  }
@@ -1386,118 +1451,165 @@ function _miolo() {
1386
1451
  return _miolo.apply(this, arguments);
1387
1452
  }
1388
1453
 
1389
- function init_cacher(config) {
1390
- var client = redis.createClient(config.redis.port, config.redis.host).on('connect', function () {
1391
- console.info(tinguir.magenta('REDIS') + " Connection established!");
1392
- }).on('error', function (err) {
1393
- var msg;
1454
+ var Context = /*#__PURE__*/React.createContext();
1455
+
1456
+ var AppContext = function AppContext(_ref) {
1457
+ var context = _ref.context,
1458
+ children = _ref.children;
1459
+ var _useState = React.useState(context),
1460
+ innerContext = _useState[0],
1461
+ setInnerContext = _useState[1];
1462
+ React.useEffect(function () {
1463
+ setInnerContext(context);
1464
+ }, [context]);
1465
+ return /*#__PURE__*/React.createElement(Context.Provider, {
1466
+ value: {
1467
+ context: innerContext,
1468
+ setContext: setInnerContext
1469
+ }
1470
+ }, children);
1471
+ };
1472
+
1473
+ var AppSsr = function AppSsr(_ref) {
1474
+ var url = _ref.url,
1475
+ context = _ref.context,
1476
+ children = _ref.children;
1477
+ return /*#__PURE__*/React.createElement(server.StaticRouter, {
1478
+ location: url
1479
+ }, /*#__PURE__*/React.createElement(AppContext, {
1480
+ context: context
1481
+ }, children));
1482
+ };
1483
+
1484
+ var ErrMessage = function ErrMessage(_ref) {
1485
+ _ref.ctx;
1486
+ var error = _ref.error;
1487
+ return /*#__PURE__*/React.createElement("div", null, "[MIOLO-SERVER_TOOLS] Missing SSR renderer: " + error.toString());
1488
+ };
1489
+
1490
+ var html = "\n\t\t<!DOCTYPE html>\n\t\t<html lang=\"en\">\n\t\t<head>\n\t\t\t<meta charset=\"utf-8\">\n\t\t\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n\t\t\t<title>miolo</title>\n\t\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\t\t\t<meta name=\"description\" content=\"miolo\" />\n\t\t\t<meta name=\"keywords\" content=\"miolo web\" />\n\t\t\t<meta name=\"author\" content=\"miolo.afialapis.com\" />\n\n\t\t\t<!-- Touch Icons - iOS and Android 2.1+ 180x180 pixels in size. --> \n\t\t\t<!--<link rel=\"apple-touch-icon-precomposed\" href=\"/favicon.ico\"/>-->\n\t\t\t<!-- Firefox, Chrome, Safari, IE 11+ and Opera. 196x196 pixels in size. -->\n\t\t\t<link rel=\"icon\" href=\"/favicon.ico\"/>\n\n\t\t\t{styles}\n\n\t\t\t<script>\n\t\t\t\twindow.__CONTEXT = {context}\n\t\t\t</script>\t\t\n\t\t</head>\n\n\t\t<body>\n\t\t\t<div id=\"root\">{children}</div>\n\t\t\t{bundle}\n\t\t</body>\n\t\t</html>\n ";
1491
+
1492
+ function init_render_middleware(loader, renderer, options) {
1493
+ // parse options
1494
+ var html$1 = html,
1495
+ port = 8000,
1496
+ use_css = true;
1497
+ try {
1498
+ if ((options == null ? void 0 : options.html) != undefined) {
1499
+ html$1 = options.html;
1500
+ }
1501
+ } catch (_) {}
1502
+ try {
1503
+ if ((options == null ? void 0 : options.port) != undefined) {
1504
+ if (!isNaN(parseInt(options.port))) {
1505
+ port = parseInt(options.port);
1506
+ }
1507
+ }
1508
+ } catch (_) {}
1509
+ try {
1510
+ use_css = (options == null ? void 0 : options.css) !== false;
1511
+ } catch (_) {}
1512
+ var bundleURL = process.env.NODE_ENV === 'development' ? "<script src=\"//localhost:" + port + "/build/bundle.js\" async></script>" : '';
1513
+ var cssURL = use_css ? process.env.NODE_ENV === 'development' ? "<link href=\"//localhost:" + port + "/build/bundle.css\" rel=\"stylesheet\" media=\"all\"></link>" : '' : '';
1514
+ var def_renderer = function def_renderer(ctx) {
1394
1515
  try {
1395
- if (err instanceof redis.ReplyError) msg = tinguir.magenta('REDIS') + " " + tinguir.red('Error ' + err.code) + " Command: " + err.command + " " + err.toString();else msg = tinguir.magenta('REDIS') + " " + tinguir.red('Error ' + err.code) + " " + err.toString();
1396
- } catch (e) {
1397
- msg = tinguir.magenta('REDIS') + " " + tinguir.red('Error ') + " " + e;
1516
+ return renderer(ctx);
1517
+ } catch (error) {
1518
+ ctx.miolo.logger.error('Missing renderer in the render middleware');
1519
+ return ErrMessage({
1520
+ ctx: ctx,
1521
+ error: error
1522
+ });
1398
1523
  }
1399
- console.error(msg);
1400
- });
1401
- var _getKey = util.promisify(client.get).bind(client);
1402
- var _existsKey = util.promisify(client.exists).bind(client);
1403
- var _setKey = util.promisify(client.set).bind(client);
1404
- var _delKey = util.promisify(client.del).bind(client);
1405
- function redisGet(_x) {
1406
- return _redisGet.apply(this, arguments);
1407
- }
1408
- function _redisGet() {
1409
- _redisGet = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(key) {
1524
+ };
1525
+ var def_loader = /*#__PURE__*/function () {
1526
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(ctx) {
1527
+ var res;
1410
1528
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1411
1529
  while (1) switch (_context.prev = _context.next) {
1412
1530
  case 0:
1413
- _context.next = 2;
1414
- return _getKey(key);
1415
- case 2:
1416
- return _context.abrupt("return", _context.sent);
1417
- case 3:
1531
+ res = {};
1532
+ _context.prev = 1;
1533
+ _context.next = 4;
1534
+ return loader(ctx);
1535
+ case 4:
1536
+ res = _context.sent;
1537
+ _context.next = 11;
1538
+ break;
1539
+ case 7:
1540
+ _context.prev = 7;
1541
+ _context.t0 = _context["catch"](1);
1542
+ ctx.miolo.logger.error('Error produced by loader in the render middleware');
1543
+ ctx.miolo.logger.error(_context.t0);
1544
+ case 11:
1545
+ return _context.abrupt("return", res);
1546
+ case 12:
1418
1547
  case "end":
1419
1548
  return _context.stop();
1420
1549
  }
1421
- }, _callee);
1550
+ }, _callee, null, [[1, 7]]);
1422
1551
  }));
1423
- return _redisGet.apply(this, arguments);
1424
- }
1425
- function redisExists(_x2) {
1426
- return _redisExists.apply(this, arguments);
1552
+ return function def_loader(_x) {
1553
+ return _ref.apply(this, arguments);
1554
+ };
1555
+ }();
1556
+ function render_middleware(_x2) {
1557
+ return _render_middleware.apply(this, arguments);
1427
1558
  }
1428
- function _redisExists() {
1429
- _redisExists = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(key) {
1430
- var r;
1559
+ function _render_middleware() {
1560
+ _render_middleware = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(ctx) {
1561
+ var ssr_data, isAuthed, user, context, ssr_html, parsed_html;
1431
1562
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1432
1563
  while (1) switch (_context2.prev = _context2.next) {
1433
1564
  case 0:
1434
1565
  _context2.next = 2;
1435
- return _existsKey(key);
1566
+ return def_loader(ctx);
1436
1567
  case 2:
1437
- r = _context2.sent;
1438
- return _context2.abrupt("return", r == 1 ? true : false);
1439
- case 4:
1568
+ ssr_data = _context2.sent;
1569
+ //const ssr_comp = def_renderer(ctx)
1570
+ isAuthed = false;
1571
+ try {
1572
+ isAuthed = (ctx == null ? void 0 : ctx.isAuthenticated()) === true;
1573
+ } catch (e) {}
1574
+ try {
1575
+ if (!isAuthed) {
1576
+ isAuthed = ctx.user.name === 'guest' && ctx.user.token != undefined;
1577
+ }
1578
+ } catch (e) {}
1579
+ user = undefined;
1580
+ try {
1581
+ if (ctx.state.user != undefined) {
1582
+ user = ctx.state.user;
1583
+ }
1584
+ } catch (_) {}
1585
+ try {
1586
+ if (ctx.user != undefined) {
1587
+ user = ctx.user;
1588
+ }
1589
+ } catch (_) {}
1590
+ context = {
1591
+ user: user,
1592
+ authenticated: isAuthed,
1593
+ ssr_data: ssr_data,
1594
+ extra: ctx == null ? void 0 : ctx.extra
1595
+ };
1596
+ ssr_html = server$1.renderToString( /*#__PURE__*/React.createElement(AppSsr, {
1597
+ url: ctx.url,
1598
+ context: context
1599
+ }, function () {
1600
+ return def_renderer(ctx);
1601
+ }));
1602
+ parsed_html = html$1.replace('{context}', JSON.stringify(context, null, 2)).replace(/{bundleURL}/g, bundleURL).replace('{children}', ssr_html).replace('{styles}', cssURL).replace('{bundle}', bundleURL);
1603
+ ctx.body = parsed_html;
1604
+ case 13:
1440
1605
  case "end":
1441
1606
  return _context2.stop();
1442
1607
  }
1443
1608
  }, _callee2);
1444
1609
  }));
1445
- return _redisExists.apply(this, arguments);
1446
- }
1447
- function redisSet(_x3, _x4, _x5) {
1448
- return _redisSet.apply(this, arguments);
1449
- }
1450
- function _redisSet() {
1451
- _redisSet = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(key, value, expiration) {
1452
- var r;
1453
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1454
- while (1) switch (_context3.prev = _context3.next) {
1455
- case 0:
1456
- if (expiration === void 0) {
1457
- expiration = 86400;
1458
- }
1459
- _context3.next = 3;
1460
- return _setKey(key, value, 'EX', expiration);
1461
- case 3:
1462
- r = _context3.sent;
1463
- return _context3.abrupt("return", r == 'OK' ? true : false);
1464
- case 5:
1465
- case "end":
1466
- return _context3.stop();
1467
- }
1468
- }, _callee3);
1469
- }));
1470
- return _redisSet.apply(this, arguments);
1471
- }
1472
- function redisDel(_x6) {
1473
- return _redisDel.apply(this, arguments);
1610
+ return _render_middleware.apply(this, arguments);
1474
1611
  }
1475
- function _redisDel() {
1476
- _redisDel = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(key) {
1477
- var r;
1478
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1479
- while (1) switch (_context4.prev = _context4.next) {
1480
- case 0:
1481
- _context4.next = 2;
1482
- return _delKey(key);
1483
- case 2:
1484
- r = _context4.sent;
1485
- return _context4.abrupt("return", r >= 1 ? true : false);
1486
- case 4:
1487
- case "end":
1488
- return _context4.stop();
1489
- }
1490
- }, _callee4);
1491
- }));
1492
- return _redisDel.apply(this, arguments);
1493
- }
1494
- var cacher = {
1495
- get: redisGet,
1496
- exists: redisExists,
1497
- set: redisSet,
1498
- del: redisDel
1499
- };
1500
- return cacher;
1612
+ return render_middleware;
1501
1613
  }
1502
1614
 
1503
1615
  function _guest_token_make_with_jwt(session, logger) {
@@ -1526,7 +1638,8 @@ function _get_cookie_properties(session) {
1526
1638
  httpOnly: false
1527
1639
  };
1528
1640
  }
1529
- var init_guest_auth_middleware = function init_guest_auth_middleware(app, options, session, logger) {
1641
+ var init_guest_auth_middleware = function init_guest_auth_middleware(app, options, session) {
1642
+ var logger = app.context.miolo.logger;
1530
1643
  var _make_guest_token = function _make_guest_token() {
1531
1644
  try {
1532
1645
  var make_guest_token = options.make_guest_token;
@@ -1773,9 +1886,8 @@ var passport = /*#__PURE__*/Object.freeze({
1773
1886
 
1774
1887
  Object.defineProperty(exports, 'getConnection', {
1775
1888
  enumerable: true,
1776
- get: function () { return calustraRouter.getConnection; }
1889
+ get: function () { return calustra.getConnection; }
1777
1890
  });
1778
- exports.getCacher = init_cacher;
1779
- exports.getEmailer = init_emailer;
1780
- exports.getLogger = init_logger;
1891
+ exports.AppSsr = AppSsr;
1892
+ exports.init_render_middleware = init_render_middleware;
1781
1893
  exports.miolo = miolo;