utilitas 1989.9.21 → 1989.9.25
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 +84 -37
- 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,13 +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
|
|
14
|
-
papertrailTransport, logger, silent, provider;
|
|
15
|
+
let chatIds, tarLevel, botBuffer, logger, silent, provider;
|
|
15
16
|
|
|
16
17
|
// Do something after the connection to the Papertrail server is established
|
|
17
18
|
const handleConnect = (data) => {
|
|
@@ -25,8 +26,7 @@ const hookConsole = () => {
|
|
|
25
26
|
console[bakAct] = console[act];
|
|
26
27
|
console[act] = function() {
|
|
27
28
|
const str = [...arguments].map(utilitas.ensureString).join(' ');
|
|
28
|
-
|
|
29
|
-
logger && logger.log(tar, str);
|
|
29
|
+
logger && logger.log(tar, str.replace(/\u001b\[\d+m/g, ''));
|
|
30
30
|
console[bakAct].apply(console, arguments);
|
|
31
31
|
};
|
|
32
32
|
}
|
|
@@ -43,53 +43,100 @@ const releaseConsole = () => {
|
|
|
43
43
|
|
|
44
44
|
const getDefaultOptions = async (provider, options) => {
|
|
45
45
|
const result = { program: (await utilitas.which()).name };
|
|
46
|
-
switch (provider) {
|
|
47
|
-
|
|
48
|
-
result.disableTls = true;
|
|
49
|
-
result.logFormat = (level, message) => {
|
|
50
|
-
return message.replace(/\u001b\[\d+m/g, '');
|
|
51
|
-
}
|
|
52
|
-
}
|
|
46
|
+
switch (provider) { case RSYSLOG: result.disableTls = true; }
|
|
47
|
+
// result.logFormat = (level, message) => { return message; }
|
|
53
48
|
return Object.assign(result, options || {});
|
|
54
49
|
};
|
|
55
50
|
|
|
51
|
+
const sysLogInit = async (options) => {
|
|
52
|
+
options = await getDefaultOptions(provider, options);
|
|
53
|
+
const winston = await import('winston');
|
|
54
|
+
const papertrail = await import('winston-papertrail-mproved');
|
|
55
|
+
const papertrailConnection = new papertrail.PapertrailConnection(options);
|
|
56
|
+
papertrailConnection.on('error', handleError);
|
|
57
|
+
papertrailConnection.on('connect', handleConnect);
|
|
58
|
+
const papertrailTransport = new papertrail.PapertrailTransport(
|
|
59
|
+
papertrailConnection, options
|
|
60
|
+
);
|
|
61
|
+
logger = new winston.createLogger({ transports: [papertrailTransport] });
|
|
62
|
+
return {
|
|
63
|
+
logger,
|
|
64
|
+
dependencies: { winston, papertrail, papertrailConnection, papertrailTransport },
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const addChatId = (id) => {
|
|
69
|
+
return chatIds && id && !chatIds.includes(id) ? chatIds.push(id) : null;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const removeChatId = (id) => {
|
|
73
|
+
return chatIds && id && chatIds.includes(id) ? delete chatIds[id] : false;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const botLoggerInit = async (options) => {
|
|
77
|
+
chatIds = utilitas.ensureArray(options?.chatId);
|
|
78
|
+
utilitas.assert(chatIds.length, 'ChatId is required.', 501);
|
|
79
|
+
handleConnect(`Sending logs via bot, chatId: ${chatIds.join(', ')}.`);
|
|
80
|
+
logger = botLogger;
|
|
81
|
+
await event.loop(
|
|
82
|
+
botLoggerSync, ~~options?.interval || 5, ~~options?.tout || 10,
|
|
83
|
+
~~options?.delay, TAPE, { silent: true }
|
|
84
|
+
);
|
|
85
|
+
return { logger, dependencies: { bot, event } };
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const botLoggerSync = async () => {
|
|
89
|
+
let f = (botBuffer?.length ? botBuffer.splice(0) : []).map(x => x[1]).join('\n');
|
|
90
|
+
if (!f.length) { return; }
|
|
91
|
+
for (let id of chatIds) {
|
|
92
|
+
try { await bot.send(id, f); } catch (err) { handleError(err); }
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const botLogger = {
|
|
97
|
+
log: (level, message) => {
|
|
98
|
+
if (tarLevel !== 'verbose' && level === 'verbose') { return; }
|
|
99
|
+
(botBuffer = botBuffer || []).push([level, message]);
|
|
100
|
+
},
|
|
101
|
+
end: () => { chatIds = null; botBuffer = null; event.end(TAPE); },
|
|
102
|
+
};
|
|
103
|
+
|
|
56
104
|
// use options.level = 'verbose' to send console.log logs
|
|
57
105
|
const init = async (options) => {
|
|
106
|
+
let result;
|
|
58
107
|
if (options) {
|
|
59
|
-
silent = !!options
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
108
|
+
silent = !!options?.silent;
|
|
109
|
+
tarLevel = options?.level;
|
|
110
|
+
provider = utilitas.ensureString(options?.provider, { case: 'UP' });
|
|
111
|
+
switch (provider) {
|
|
112
|
+
case BOT:
|
|
113
|
+
result = await botLoggerInit(options);
|
|
114
|
+
break;
|
|
115
|
+
case RSYSLOG:
|
|
116
|
+
case PAPERTRAIL:
|
|
117
|
+
result = await sysLogInit(options);
|
|
118
|
+
break;
|
|
119
|
+
default:
|
|
120
|
+
utilitas.throwError(
|
|
121
|
+
`Invalid tape provider: '${options?.provider}'.`, 501
|
|
122
|
+
);
|
|
123
|
+
}
|
|
75
124
|
options.noHook || hookConsole();
|
|
76
125
|
}
|
|
77
126
|
utilitas.assert(logger, 'Logger client has not been initialized.', 501);
|
|
78
|
-
return
|
|
79
|
-
winston, papertrail, papertrailConnection, papertrailTransport, logger
|
|
80
|
-
};
|
|
127
|
+
return result;
|
|
81
128
|
};
|
|
82
129
|
|
|
83
130
|
const end = async () => {
|
|
84
131
|
releaseConsole();
|
|
85
|
-
setTimeout(() => {
|
|
86
|
-
logger && logger.end();
|
|
87
|
-
modLog('Terminated.');
|
|
88
|
-
}, 1000);
|
|
132
|
+
setTimeout(() => { logger?.end?.(); modLog('Terminated.'); }, 1000);
|
|
89
133
|
};
|
|
90
134
|
|
|
135
|
+
export default init;
|
|
91
136
|
export {
|
|
137
|
+
addChatId,
|
|
92
138
|
end,
|
|
93
139
|
getLogger,
|
|
94
140
|
init,
|
|
141
|
+
removeChatId,
|
|
95
142
|
};
|
package/lib/uoid.mjs
CHANGED