utilitas 1989.9.21 → 1989.9.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/bot.mjs +1 -0
- package/lib/cache.mjs +1 -0
- package/lib/dbio.mjs +1 -0
- package/lib/email.mjs +1 -0
- package/lib/event.mjs +3 -1
- package/lib/sentinel.mjs +1 -0
- package/lib/shell.mjs +1 -0
- package/lib/sms.mjs +1 -0
- package/lib/tape.mjs +72 -35
- package/lib/uoid.mjs +1 -0
- package/package.json +1 -1
package/lib/bot.mjs
CHANGED
package/lib/cache.mjs
CHANGED
package/lib/dbio.mjs
CHANGED
package/lib/email.mjs
CHANGED
package/lib/event.mjs
CHANGED
|
@@ -88,7 +88,8 @@ const bulk = async (absDir, options) => {
|
|
|
88
88
|
return await Promise.all(pmsRun);
|
|
89
89
|
};
|
|
90
90
|
|
|
91
|
-
const end = async () => {
|
|
91
|
+
const end = async (name) => {
|
|
92
|
+
if (name) { delete jobs[name]; if (jobs.length) { return; } }
|
|
92
93
|
clearInterval(timer);
|
|
93
94
|
timer = -1;
|
|
94
95
|
const now = Date.now();
|
|
@@ -102,6 +103,7 @@ const end = async () => {
|
|
|
102
103
|
log('Terminated.');
|
|
103
104
|
};
|
|
104
105
|
|
|
106
|
+
export default loop;
|
|
105
107
|
export {
|
|
106
108
|
bulk,
|
|
107
109
|
end,
|
package/lib/sentinel.mjs
CHANGED
package/lib/shell.mjs
CHANGED
package/lib/sms.mjs
CHANGED
package/lib/tape.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as bot from './bot.mjs';
|
|
2
|
+
import * as event from './event.mjs';
|
|
1
3
|
import * as utilitas from './utilitas.mjs';
|
|
2
4
|
|
|
3
5
|
// https://github.com/winstonjs/winston#logging-levels
|
|
@@ -5,12 +7,12 @@ import * as utilitas from './utilitas.mjs';
|
|
|
5
7
|
// Handle, report, or silently ignore connection errors and failures
|
|
6
8
|
const handleError = (err) => { process.stdout.write(`${err.message}\n`); };
|
|
7
9
|
const consoleMap = { log: 'verbose', info: 0, debug: 0, warn: 0, error: 0 };
|
|
8
|
-
const
|
|
10
|
+
const TAPE = 'TAPE';
|
|
11
|
+
const modLog = (content) => { return utilitas.modLog(content, TAPE); };
|
|
9
12
|
const getLogger = async () => { return (await init()).logger; };
|
|
10
|
-
const [
|
|
11
|
-
// why keeping providerPapertrail ?
|
|
13
|
+
const [BOT, RSYSLOG, PAPERTRAIL] = ['BOT', 'RSYSLOG', 'PAPERTRAIL'];
|
|
12
14
|
|
|
13
|
-
let winston, papertrail, papertrailConnection,
|
|
15
|
+
let chatIds, tarLevel, botBuffer, winston, papertrail, papertrailConnection,
|
|
14
16
|
papertrailTransport, logger, silent, provider;
|
|
15
17
|
|
|
16
18
|
// Do something after the connection to the Papertrail server is established
|
|
@@ -25,8 +27,7 @@ const hookConsole = () => {
|
|
|
25
27
|
console[bakAct] = console[act];
|
|
26
28
|
console[act] = function() {
|
|
27
29
|
const str = [...arguments].map(utilitas.ensureString).join(' ');
|
|
28
|
-
|
|
29
|
-
logger && logger.log(tar, str);
|
|
30
|
+
logger && logger.log(tar, str.replace(/\u001b\[\d+m/g, ''));
|
|
30
31
|
console[bakAct].apply(console, arguments);
|
|
31
32
|
};
|
|
32
33
|
}
|
|
@@ -43,51 +44,87 @@ const releaseConsole = () => {
|
|
|
43
44
|
|
|
44
45
|
const getDefaultOptions = async (provider, options) => {
|
|
45
46
|
const result = { program: (await utilitas.which()).name };
|
|
46
|
-
switch (provider) {
|
|
47
|
-
case
|
|
48
|
-
result.disableTls = true;
|
|
49
|
-
result.logFormat = (level, message) => {
|
|
50
|
-
return message.replace(/\u001b\[\d+m/g, '');
|
|
51
|
-
}
|
|
47
|
+
switch (provider) { // result.logFormat = (level, message) => { return message; }
|
|
48
|
+
case RSYSLOG: result.disableTls = true;
|
|
52
49
|
}
|
|
53
50
|
return Object.assign(result, options || {});
|
|
54
51
|
};
|
|
55
52
|
|
|
53
|
+
const sysLogInit = async (options) => {
|
|
54
|
+
options = await getDefaultOptions(provider, options);
|
|
55
|
+
winston = await import('winston');
|
|
56
|
+
papertrail = await import('winston-papertrail-mproved');
|
|
57
|
+
papertrailConnection = new papertrail.PapertrailConnection(options);
|
|
58
|
+
papertrailConnection.on('error', handleError);
|
|
59
|
+
papertrailConnection.on('connect', handleConnect);
|
|
60
|
+
papertrailTransport = new papertrail.PapertrailTransport(
|
|
61
|
+
papertrailConnection, options
|
|
62
|
+
);
|
|
63
|
+
logger = new winston.createLogger({ transports: [papertrailTransport] });
|
|
64
|
+
return {
|
|
65
|
+
logger,
|
|
66
|
+
dependencies: { winston, papertrail, papertrailConnection, papertrailTransport },
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const botLoggerInit = (options) => {
|
|
71
|
+
chatIds = utilitas.ensureArray(options?.chatId);
|
|
72
|
+
utilitas.assert(chatIds.length, 'ChatId is required.', 501);
|
|
73
|
+
handleConnect(`Sending logs via bot, chatId: ${chatIds.join(', ')}.`);
|
|
74
|
+
logger = botLogger;
|
|
75
|
+
event.loop(botLoggerSync, 5, 10, 0, TAPE, { silent: true });
|
|
76
|
+
return { logger, dependencies: { bot, event } };
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const botLoggerSync = async () => {
|
|
80
|
+
const fetched = (botBuffer?.length ? botBuffer.splice(0) : []).join('\n');
|
|
81
|
+
if (!fetched.length) { return; }
|
|
82
|
+
for (let id of chatIds) {
|
|
83
|
+
try { await bot.send(id, fetched); }
|
|
84
|
+
catch (e) { process.stdout.write(`Tape error: ${e.message}\n`); }
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const botLogger = {
|
|
89
|
+
log: (level, message) => {
|
|
90
|
+
if (tarLevel !== 'verbose' && level === 'verbose') { return; }
|
|
91
|
+
(botBuffer = botBuffer || []).push([level, message]);
|
|
92
|
+
},
|
|
93
|
+
end: () => { chatIds = null; botBuffer = null; event.end(TAPE); },
|
|
94
|
+
};
|
|
95
|
+
|
|
56
96
|
// use options.level = 'verbose' to send console.log logs
|
|
57
97
|
const init = async (options) => {
|
|
98
|
+
let result;
|
|
58
99
|
if (options) {
|
|
59
|
-
silent = !!options
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
100
|
+
silent = !!options?.silent;
|
|
101
|
+
tarLevel = options?.level;
|
|
102
|
+
provider = utilitas.ensureString(options?.provider, { case: 'UP' });
|
|
103
|
+
switch (provider) {
|
|
104
|
+
case BOT:
|
|
105
|
+
result = await botLoggerInit(options);
|
|
106
|
+
break;
|
|
107
|
+
case RSYSLOG:
|
|
108
|
+
case PAPERTRAIL:
|
|
109
|
+
result = await sysLogInit(options);
|
|
110
|
+
break;
|
|
111
|
+
default:
|
|
112
|
+
utilitas.throwError(
|
|
113
|
+
`Invalid tape provider: '${options?.provider}'.`, 501
|
|
114
|
+
);
|
|
115
|
+
}
|
|
75
116
|
options.noHook || hookConsole();
|
|
76
117
|
}
|
|
77
118
|
utilitas.assert(logger, 'Logger client has not been initialized.', 501);
|
|
78
|
-
return
|
|
79
|
-
winston, papertrail, papertrailConnection, papertrailTransport, logger
|
|
80
|
-
};
|
|
119
|
+
return result;
|
|
81
120
|
};
|
|
82
121
|
|
|
83
122
|
const end = async () => {
|
|
84
123
|
releaseConsole();
|
|
85
|
-
setTimeout(() => {
|
|
86
|
-
logger && logger.end();
|
|
87
|
-
modLog('Terminated.');
|
|
88
|
-
}, 1000);
|
|
124
|
+
setTimeout(() => { logger?.end?.(); modLog('Terminated.'); }, 1000);
|
|
89
125
|
};
|
|
90
126
|
|
|
127
|
+
export default init;
|
|
91
128
|
export {
|
|
92
129
|
end,
|
|
93
130
|
getLogger,
|
package/lib/uoid.mjs
CHANGED