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