miolo 0.1.0 → 0.2.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 (37) hide show
  1. package/dist/miolo.cjs +1 -1
  2. package/dist/miolo.min.mjs +1 -1
  3. package/dist/miolo.mjs +1 -1
  4. package/dist/miolo.node.mjs +1 -1
  5. package/lib/cacher/index.cjs +76 -0
  6. package/lib/cacher/verify.cjs +27 -0
  7. package/lib/config/defaults.cjs +287 -0
  8. package/lib/config/index.cjs +12 -0
  9. package/lib/emailer/index.cjs +56 -0
  10. package/lib/emailer/verify.cjs +11 -0
  11. package/lib/index.cjs +40 -0
  12. package/lib/logger/index.cjs +91 -0
  13. package/lib/logger/logger_mail.cjs +56 -0
  14. package/lib/logger/verify.cjs +22 -0
  15. package/lib/server/engines/cron/index.cjs +12 -0
  16. package/lib/server/engines/cron/syscheck.cjs +41 -0
  17. package/lib/server/engines/socket/index.cjs +40 -0
  18. package/lib/server/index.cjs +134 -0
  19. package/lib/server/middleware/auth/basic.cjs +53 -0
  20. package/lib/server/middleware/auth/guest.cjs +72 -0
  21. package/lib/server/middleware/auth/passport.cjs +123 -0
  22. package/lib/server/middleware/body.cjs +50 -0
  23. package/lib/server/middleware/catcher/index.cjs +58 -0
  24. package/lib/server/middleware/context.cjs +23 -0
  25. package/lib/server/middleware/extra.cjs +27 -0
  26. package/lib/server/middleware/headers.cjs +68 -0
  27. package/lib/server/middleware/request.cjs +68 -0
  28. package/lib/server/middleware/session/index.cjs +26 -0
  29. package/lib/server/middleware/session/store.cjs +10 -0
  30. package/lib/server/middleware/static.cjs +25 -0
  31. package/lib/server/routes/catch_js_error.cjs +35 -0
  32. package/lib/server/routes/fallback_index.html +24 -0
  33. package/lib/server/routes/html_render.cjs +37 -0
  34. package/lib/server/routes/robots.cjs +27 -0
  35. package/lib/server/static/img/favicon.ico +0 -0
  36. package/lib/server/static/robots.txt +2 -0
  37. package/package.json +1 -1
package/dist/miolo.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * miolo v0.1.0
2
+ * miolo v0.2.0
3
3
  *
4
4
  * Copyright (c) Donato Lorenzo <donato@afialapis.com>
5
5
  *
@@ -1,5 +1,5 @@
1
1
  /**
2
- * miolo v0.1.0
2
+ * miolo v0.2.0
3
3
  *
4
4
  * Copyright (c) Donato Lorenzo <donato@afialapis.com>
5
5
  *
package/dist/miolo.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * miolo v0.1.0
2
+ * miolo v0.2.0
3
3
  *
4
4
  * Copyright (c) Donato Lorenzo <donato@afialapis.com>
5
5
  *
@@ -1,5 +1,5 @@
1
1
  /**
2
- * miolo v0.1.0
2
+ * miolo v0.2.0
3
3
  *
4
4
  * Copyright (c) Donato Lorenzo <donato@afialapis.com>
5
5
  *
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.init_cacher = init_cacher;
7
+ var _redis = _interopRequireDefault(require("redis"));
8
+ var _util = require("util");
9
+ var _tinguir = require("tinguir");
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
12
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
13
+ function init_cacher(config) {
14
+ var client = _redis.default.createClient(config.redis.port, config.redis.host).on('connect', function () {
15
+ console.info("".concat((0, _tinguir.magenta)('REDIS'), " Connection established!"));
16
+ }).on('error', function (err) {
17
+ var msg;
18
+ try {
19
+ if (err instanceof _redis.default.ReplyError) msg = "".concat((0, _tinguir.magenta)('REDIS'), " ").concat((0, _tinguir.red)('Error ' + err.code), " Command: ").concat(err.command, " ").concat(err.toString());else msg = "".concat((0, _tinguir.magenta)('REDIS'), " ").concat((0, _tinguir.red)('Error ' + err.code), " ").concat(err.toString());
20
+ } catch (e) {
21
+ msg = "".concat((0, _tinguir.magenta)('REDIS'), " ").concat((0, _tinguir.red)('Error '), " ").concat(e);
22
+ }
23
+ console.error(msg);
24
+ });
25
+ var _getKey = (0, _util.promisify)(client.get).bind(client);
26
+ var _existsKey = (0, _util.promisify)(client.exists).bind(client);
27
+ var _setKey = (0, _util.promisify)(client.set).bind(client);
28
+ var _delKey = (0, _util.promisify)(client.del).bind(client);
29
+ function redisGet(_x) {
30
+ return _redisGet.apply(this, arguments);
31
+ }
32
+ function _redisGet() {
33
+ _redisGet = _asyncToGenerator(function* (key) {
34
+ return yield _getKey(key);
35
+ });
36
+ return _redisGet.apply(this, arguments);
37
+ }
38
+ function redisExists(_x2) {
39
+ return _redisExists.apply(this, arguments);
40
+ }
41
+ function _redisExists() {
42
+ _redisExists = _asyncToGenerator(function* (key) {
43
+ var r = yield _existsKey(key);
44
+ return r == 1 ? true : false;
45
+ });
46
+ return _redisExists.apply(this, arguments);
47
+ }
48
+ function redisSet(_x3, _x4) {
49
+ return _redisSet.apply(this, arguments);
50
+ }
51
+ function _redisSet() {
52
+ _redisSet = _asyncToGenerator(function* (key, value) {
53
+ var expiration = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 86400;
54
+ var r = yield _setKey(key, value, 'EX', expiration);
55
+ return r == 'OK' ? true : false;
56
+ });
57
+ return _redisSet.apply(this, arguments);
58
+ }
59
+ function redisDel(_x5) {
60
+ return _redisDel.apply(this, arguments);
61
+ }
62
+ function _redisDel() {
63
+ _redisDel = _asyncToGenerator(function* (key) {
64
+ var r = yield _delKey(key);
65
+ return r >= 1 ? true : false;
66
+ });
67
+ return _redisDel.apply(this, arguments);
68
+ }
69
+ var cacher = {
70
+ get: redisGet,
71
+ exists: redisExists,
72
+ set: redisSet,
73
+ del: redisDel
74
+ };
75
+ return cacher;
76
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.verify_cacher = verify_cacher;
7
+ var _index = require("./index.cjs");
8
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
9
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
10
+ function verify_cacher(_x) {
11
+ return _verify_cacher.apply(this, arguments);
12
+ }
13
+ function _verify_cacher() {
14
+ _verify_cacher = _asyncToGenerator(function* (config) {
15
+ var cacher = (0, _index.make_cacher)(config);
16
+ var rs = yield cacher.set('test', 'I will be expired in 10 seconds', 10);
17
+ if (!rs) console.warning('[miolo][Verify][REDIS] Verifying error: Redis could not be tested (S)!');else {
18
+ var rd = yield cacher.del('test');
19
+ if (!rd) console.warning('[miolo][Verify][REDIS] Verifying error: Redis could not be tested (D)!');else {
20
+ console.info('[miolo][Verify][REDIS] Verifying: Redis Tested OK');
21
+ return true;
22
+ }
23
+ }
24
+ return false;
25
+ });
26
+ return _verify_cacher.apply(this, arguments);
27
+ }
@@ -0,0 +1,287 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _path = _interopRequireDefault(require("path"));
8
+ var _url = require("url");
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ var __my_filename = (0, _url.fileURLToPath)(require('url').pathToFileURL(__filename).toString());
11
+ var __my_dirname = _path.default.dirname(__my_filename);
12
+ var favicon = _path.default.resolve(__my_dirname, '../server/static/ig/favicon.ico');
13
+ var _default = {
14
+ name: 'miolo',
15
+ http: {
16
+ port: 8001,
17
+ hostname: 'localhost',
18
+ static: {
19
+ favicon: favicon,
20
+ folders: {}
21
+ },
22
+ // cors can be:
23
+ // - false
24
+ // - simple (just assign Access-Control-Allow-Origin='*' and Access-Control-Expose-Headers='SourceMap,X-SourceMap'
25
+ // - true enable @koa/cors
26
+ // - {options} enable @koa/cors and use the custom options
27
+ //
28
+ cors: false,
29
+ // proxy can be:
30
+ // - false
31
+ // - true enable koa-proxies and use default options
32
+ // - {options} enable koa-proxies and use the custom options
33
+ proxy: false
34
+ },
35
+ session: {
36
+ salt: 'SUPER_SALTY_YES?',
37
+ secret: 'SUPER_SECRET_KEY_KERE',
38
+ options: {
39
+ /** (number || 'session') maxAge in ms (default is 1 days) */
40
+ /** 'session' will result in a cookie that expires when session/browser is closed */
41
+ /** Warning: If a session cookie is stolen, this cookie will never expire */
42
+ maxAge: 86400000,
43
+ /** (boolean) automatically commit headers (default true) */
44
+ //autoCommit: true,
45
+
46
+ /** (boolean) can overwrite or not (default true) */
47
+ //overwrite: true,
48
+
49
+ /** (boolean) httpOnly or not (default true) */
50
+ //httpOnly: true,
51
+
52
+ /** (boolean) signed or not (default true) */
53
+ //signed: true,
54
+
55
+ /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */
56
+ //rolling: false,
57
+
58
+ /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/
59
+ //renew: false,
60
+
61
+ /** (boolean) secure cookie*/
62
+ secure: true,
63
+ /** (string) session cookie sameSite options (default null, don't set it) */
64
+ sameSite: null // 'strict',
65
+ }
66
+ },
67
+
68
+ db: {
69
+ connection: {
70
+ database: {
71
+ dialect: 'postgres',
72
+ host: 'localhost',
73
+ port: 5432,
74
+ database: 'miolo',
75
+ user: 'postgres',
76
+ password: 'postgres',
77
+ max: 5,
78
+ // Maximum number of connection in pool
79
+ min: 0,
80
+ // Minimum number of connection in pool
81
+ 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,
82
+ },
83
+
84
+ options: {
85
+ log: 'silly' // will be updated on the fly with miolo logger
86
+ }
87
+ },
88
+
89
+ tables: []
90
+ },
91
+ routes: {
92
+ bodyField: undefined,
93
+ getUserId: ctx => {
94
+ try {
95
+ var from_pport = ctx.state.user.id;
96
+ if (from_pport != undefined) {
97
+ return from_pport;
98
+ }
99
+ } catch (e) {}
100
+ try {
101
+ var from_basic_auth = ctx.user.id;
102
+ if (from_basic_auth != undefined) {
103
+ return from_basic_auth;
104
+ }
105
+ } catch (e) {}
106
+ try {
107
+ if (ctx.user.name == 'guest') {
108
+ var from_guest_auth = ctx.user.token;
109
+ if (from_guest_auth != undefined) {
110
+ return from_guest_auth;
111
+ }
112
+ }
113
+ } catch (e) {}
114
+ var uid = ctx.headers['user-id'];
115
+ if (uid != undefined) {
116
+ return uid;
117
+ }
118
+ return undefined;
119
+ },
120
+ // authUser: {
121
+ // require: false, // true / false / 'read-only'
122
+ // action: 'redirect', // 'error'
123
+ // redirect_url: '/',
124
+ // error_code: 401
125
+ // },
126
+
127
+ crud: {
128
+ prefix: '',
129
+ routes: [] // miolo will not support '*' by now
130
+ },
131
+
132
+ queries: undefined
133
+ },
134
+ catcher: '/sys/jserror',
135
+ cacher: {
136
+ redis: {
137
+ host: '127.0.0.1',
138
+ port: 6379
139
+ }
140
+ },
141
+ log: {
142
+ level: 'debug',
143
+ format: {
144
+ locale: 'en-GB'
145
+ },
146
+ console: {
147
+ enabled: true,
148
+ level: 'silly'
149
+ },
150
+ file: {
151
+ enabled: true,
152
+ level: 'silly',
153
+ filename: '/var/log/afialapis/miolo.log'
154
+ },
155
+ mail: {
156
+ enabled: false,
157
+ level: 'warn',
158
+ name: 'miolo',
159
+ from: 'miolo@afialapis.com',
160
+ to: 'devel@afialapis.com'
161
+ }
162
+ },
163
+ mail: {
164
+ silent: true,
165
+ options: {
166
+ //
167
+ // General options
168
+ //
169
+ // port – is the port to connect to (defaults to 587 is secure is false or 465 if true)
170
+ port: 25,
171
+ // host – is the hostname or IP address to connect to (defaults to ‘localhost’)
172
+ host: 'mail.afialapis.com',
173
+ // auth – defines authentication data
174
+ // If authentication data is not present, the connection is considered authenticated from the start.
175
+ // Otherwise you would need to provide the authentication options object.
176
+ // - type indicates the authetication type, defaults to ‘login’, other option is ‘oauth2’
177
+ // - user is the username
178
+ // - pass is the password for the user if normal login is used
179
+ // authMethod – defines preferred authentication method, defaults to ‘PLAIN’
180
+ authMethod: 'PLAIN',
181
+ //
182
+ // TLS options
183
+ //
184
+ // secure – if true the connection will use TLS when connecting to server.
185
+ // If false (the default) then TLS is used if server supports the STARTTLS extension.
186
+ // In most cases set this value to true if you are connecting to port 465. For port 587 or 25 keep it false
187
+ // ** Setting secure to false does not mean that you would not use an encrypted connection. Most SMTP servers allow
188
+ // connection upgrade via STARTTLS command but to use this you have to connect using plaintext first
189
+ secure: false,
190
+ // tls – defines additional node.js TLSSocket options to be passed to the socket constructor, eg. {rejectUnauthorized: true}.
191
+ tls: {
192
+ // do not fail on invalid certs
193
+ rejectUnauthorized: false
194
+ },
195
+ // ignoreTLS – if this is true and secure is false then TLS is not used even if the server supports STARTTLS extension
196
+ // ** ignoreTLS: false,
197
+ // requireTLS – if this is true and secure is false then Nodemailer tries to use STARTTLS even
198
+ // if the server does not advertise support for it. If the connection can not be encrypted then message is not sent
199
+ // ** requireTLS: true,
200
+ //
201
+ // Connection options
202
+ //
203
+ // name – optional hostname of the client, used for identifying to the server, defaults to hostname of the machine
204
+ // ** name: ,
205
+ // localAddress – is the local interface to bind to for network connections
206
+ // ** localAddress: ,
207
+ // connectionTimeout – how many milliseconds to wait for the connection to establish
208
+ // ** connectionTimeout: ,
209
+ // greetingTimeout – how many milliseconds to wait for the greeting after connection is established
210
+ // ** greetingTimeout: ,
211
+ // socketTimeout – how many milliseconds of inactivity to allow
212
+ // ** socketTimeout: ,
213
+ //
214
+ // Debug options
215
+ //
216
+ // logger – optional bunyan compatible logger instance. If set to true then logs to console.
217
+ // If value is not set or is false then nothing is logged
218
+ logger: false,
219
+ // debug – if set to true, then logs SMTP traffic, otherwise logs only transaction events
220
+ debug: false
221
+ //
222
+ // Security options
223
+ //
224
+ // disableFileAccess – if true, then does not allow to use files as content.
225
+ // Use it when you want to use JSON data from untrusted source as the email.
226
+ // If an attachment or message node tries to fetch something from a file the sending returns an error
227
+ ////disableFileAccess: ,
228
+ // disableUrlAccess – if true, then does not allow to use Urls as content
229
+ // ** disableUrlAccess: ,
230
+
231
+ //
232
+ // Pooling options
233
+ //
234
+ // pool – see Pooled SMTP for details about connection pooling : https://nodemailer.com/smtp/pooled/
235
+ //
236
+ // Proxy options
237
+ //
238
+ // proxy – all SMTP based transports allow to use proxies for making TCP connections to servers.
239
+ // Read about proxy support in Nodemailer from here: https://nodemailer.com/smtp/proxies/
240
+
241
+ /* TESTED */
242
+ /*
243
+ port: 465,
244
+ host: 'mail.afialapis.com',
245
+ auth: {
246
+ user: 'devel@afialapis.com',
247
+ pass: '***',
248
+ type: 'login',
249
+ },
250
+ secure: true,
251
+ tls: {
252
+ rejectUnauthorized: false
253
+ } ,
254
+ */
255
+ },
256
+
257
+ defaults: {
258
+ name: 'miolo',
259
+ from: 'miolo@afialapis.com',
260
+ to: 'devel@afialapis.com'
261
+ }
262
+ },
263
+ auth: {
264
+ //basic: {
265
+ // auth_user: async (username, password) => { return {id: 1} },
266
+ // realm: '',
267
+ // paths: [],
268
+ //},
269
+ //passport: {
270
+ // get_user_id: (user, done, miolo) => done(null, user.id), // default
271
+ // find_user_by_id: (id, done, miolo) => done(null, {id: 1}), // ok=> done(null, user) err=> done(error, null)
272
+ // local_auth_user: (username, password, done, miolo) => done(null, {id: 1})
273
+ // // auth=> done(null, user) noauth=> done(null, false, {message: ''}) err=> done(error, null)
274
+ // url_login : '/login',
275
+ // url_logout: '/logout',
276
+ // url_login_redirect: undefined
277
+ // url_logout_redirect: '/'
278
+ //}
279
+ //guest: {
280
+ // make_guest_token: undefined // (session) => ''
281
+ //}
282
+ },
283
+ middlewares: [
284
+ // async (ctx) => {}
285
+ ]
286
+ };
287
+ exports.default = _default;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.init_config = init_config;
7
+ var _assignDeep = _interopRequireDefault(require("assign-deep"));
8
+ var _defaults = _interopRequireDefault(require("./defaults.cjs"));
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ function init_config(config) {
11
+ return (0, _assignDeep.default)(_defaults.default, config);
12
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.init_emailer = init_emailer;
7
+ var _nodemailer = _interopRequireDefault(require("nodemailer"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ function init_emailer(options, defaults) {
10
+ var silent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
11
+ var nmailer = _nodemailer.default.createTransport(options, defaults);
12
+ function send_email(mail, cb) {
13
+ if (silent) {
14
+ console.info('*********************************');
15
+ console.info('This mail will not be send (emailing is disabled):');
16
+ console.info(mail);
17
+ console.info('*********************************');
18
+ } else {
19
+ if (!cb) {
20
+ cb = function cb(err, info) {
21
+ if (err) {
22
+ console.error('NodeMailer error:');
23
+ console.error(err);
24
+ }
25
+ if (info) {
26
+ console.log('NodeMailer sent mail:');
27
+ console.log(info);
28
+ }
29
+ };
30
+ }
31
+ nmailer.sendMail(mail, cb);
32
+ }
33
+ }
34
+ function verify_emailer() {
35
+ console.info('[miolo][Verify][MAILER] Verifying...');
36
+ // verify connection configuration
37
+ nmailer.verify(function (error, _success) {
38
+ if (error) {
39
+ console.error('[miolo][Verify][MAILER] Verifying ERROR');
40
+ console.error(error);
41
+ } else {
42
+ console.info('[miolo][Verify][MAILER] Verifyed OK: Server is ready to take our messages');
43
+ //test_eail()
44
+ }
45
+ });
46
+ }
47
+
48
+ var emailer = {
49
+ send: send_email,
50
+ verify: verify_emailer,
51
+ options,
52
+ defaults,
53
+ silent
54
+ };
55
+ return emailer;
56
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.veryfy_emailer = veryfy_emailer;
7
+ var _index = require("./index.cjs");
8
+ function veryfy_emailer(config) {
9
+ var emailer = (0, _index.make_emailer)(config);
10
+ return emailer.verify();
11
+ }
package/lib/index.cjs CHANGED
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "getCacher", {
7
+ enumerable: true,
8
+ get: function get() {
9
+ return _cacher.init_cacher;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "getConnection", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _calustraRouter.getConnection;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "getEmailer", {
19
+ enumerable: true,
20
+ get: function get() {
21
+ return _emailer.init_emailer;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "getLogger", {
25
+ enumerable: true,
26
+ get: function get() {
27
+ return _logger.init_logger;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "miolo", {
31
+ enumerable: true,
32
+ get: function get() {
33
+ return _server.miolo;
34
+ }
35
+ });
36
+ var _server = require("./server");
37
+ var _emailer = require("./emailer");
38
+ var _cacher = require("./cacher");
39
+ var _logger = require("./logger");
40
+ var _calustraRouter = require("calustra-router");
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.init_logger = void 0;
7
+ var _tinguir = require("tinguir");
8
+ var _logger_mail = require("./logger_mail.cjs");
9
+ var _winston = require("winston");
10
+ /* https://github.com/winstonjs/winston/issues/925 */
11
+ /* https://github.com/winstonjs/winston/issues/287 */
12
+
13
+ var {
14
+ combine,
15
+ timestamp,
16
+ _label,
17
+ printf,
18
+ errors
19
+ } = _winston.format;
20
+ var init_logger = (config, emailer) => {
21
+ var LEVEL_COLORS = {
22
+ silly: _tinguir.gray,
23
+ debug: _tinguir.magenta,
24
+ verbose: _tinguir.cyan,
25
+ info: _tinguir.yellow,
26
+ warn: _tinguir.red_light,
27
+ error: _tinguir.red
28
+ };
29
+ var myFormat = printf(info => {
30
+ var _config$format;
31
+ var lc = LEVEL_COLORS[info.level];
32
+ var tm = new Date(info.timestamp);
33
+ var ts = tm.toLocaleString((config === null || config === void 0 ? void 0 : (_config$format = config.format) === null || _config$format === void 0 ? void 0 : _config$format.locale) || 'en');
34
+ //const ts= tm.toString().substr(4, 20)
35
+ var log = "[miolo] ".concat(lc(ts), " ").concat(lc(info.level), " ").concat(info.message);
36
+ return info.stack ? "".concat(log, "\n").concat(info.stack) : log;
37
+ });
38
+ var _log_transports = [];
39
+ //
40
+ // Console transport
41
+ // If we're not in production then log to the `console` with the format:
42
+ // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
43
+ //
44
+
45
+ if (config.console.enabled) {
46
+ var _config$console;
47
+ _log_transports.push(new _winston.transports.Console({
48
+ humanReadableUnhandledException: true,
49
+ level: (config === null || config === void 0 ? void 0 : (_config$console = config.console) === null || _config$console === void 0 ? void 0 : _config$console.level) || (config === null || config === void 0 ? void 0 : config.level) || 'silly',
50
+ handleExceptions: true
51
+ }));
52
+ }
53
+
54
+ //
55
+ // File transport
56
+ //
57
+ if (config.file.enabled) {
58
+ var _config$file;
59
+ _log_transports.push(new _winston.transports.File({
60
+ filename: config.file.filename,
61
+ level: (config === null || config === void 0 ? void 0 : (_config$file = config.file) === null || _config$file === void 0 ? void 0 : _config$file.level) || (config === null || config === void 0 ? void 0 : config.level) || 'info',
62
+ humanReadableUnhandledException: true,
63
+ handleExceptions: true
64
+ }));
65
+ }
66
+
67
+ //
68
+ // Mail transport
69
+ //
70
+ if (config.mail.enabled) {
71
+ var MailerLogger = (0, _logger_mail.init_logger_to_mail)(config.mail, emailer);
72
+ _winston.transports.MailerLogger = MailerLogger;
73
+ _log_transports.push(new _winston.transports.MailerLogger({
74
+ humanReadableUnhandledException: true,
75
+ handleExceptions: true
76
+ }));
77
+ }
78
+
79
+ //
80
+ // Logger
81
+ //
82
+ var logger = (0, _winston.createLogger)({
83
+ level: (config === null || config === void 0 ? void 0 : config.level) || 'silly',
84
+ format: combine(errors({
85
+ stack: true
86
+ }), timestamp(), myFormat),
87
+ transports: _log_transports
88
+ });
89
+ return logger;
90
+ };
91
+ exports.init_logger = init_logger;