utilitas 1989.9.32 → 1989.9.36
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/index.mjs +1 -2
- package/lib/tape.mjs +55 -100
- package/package.json +4 -5
package/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// dependencies
|
|
2
2
|
export { default as colors } from 'colors/safe.js';
|
|
3
3
|
export { default as fetch } from 'node-fetch';
|
|
4
|
+
export { Tail as tail } from 'tail';
|
|
4
5
|
export * as base64url from 'base64url';
|
|
5
6
|
export * as fileType from 'file-type';
|
|
6
7
|
export * as geoIp from 'fast-geoip';
|
|
@@ -18,8 +19,6 @@ export * as telegraf from 'telegraf';
|
|
|
18
19
|
export * as telesign from 'telesignsdk';
|
|
19
20
|
export * as twilio from 'twilio';
|
|
20
21
|
export * as uuid from 'uuid';
|
|
21
|
-
export * as winston from 'winston';
|
|
22
|
-
export * as winstonPapertrail from 'winston-papertrail-mproved';
|
|
23
22
|
|
|
24
23
|
// features
|
|
25
24
|
export * as bot from './lib/bot.mjs';
|
package/lib/tape.mjs
CHANGED
|
@@ -2,78 +2,43 @@ import * as bot from './bot.mjs';
|
|
|
2
2
|
import * as event from './event.mjs';
|
|
3
3
|
import * as utilitas from './utilitas.mjs';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const consoleMap = { log: 'verbose', info: 0, debug: 0, warn: 0, error: 0 };
|
|
10
|
-
const [TAPE, maxLength, defBufCycle, maxBufCycle] = ['TAPE', 4096, 10, 100];
|
|
11
|
-
const modLog = (content) => { return utilitas.modLog(content, TAPE); };
|
|
12
|
-
const getLogger = async () => { return (await init()).logger; };
|
|
13
|
-
const [BOT, RSYSLOG, PAPERTRAIL] = ['BOT', 'RSYSLOG', 'PAPERTRAIL'];
|
|
5
|
+
const consoleMap = ['log', 'info', 'debug', 'warn', 'error'];
|
|
6
|
+
const [TAPE, BOT, maxLength, defBufCycle, maxBufCycle] = ['TAPE', 'BOT', 4096, 10, 100];
|
|
7
|
+
const stdout = (message) => { return process.stdout.write(`${message}\n`); };
|
|
8
|
+
const modLog = (c, o) => { utilitas.modLog(c, TAPE, { time: true, ...o || {} }); };
|
|
14
9
|
const getSendTxt = (arr) => { return arr.map(x => x[1]).join('\n'); };
|
|
15
10
|
const getSndSize = (arr) => { return getSendTxt(arr).length; };
|
|
16
11
|
const getBufSize = () => { return maxLength * bufferCycle; };
|
|
17
12
|
const nextLen = () => { return botBuffer?.[0]?.[1].length || (maxLength + 1); };
|
|
18
13
|
|
|
19
|
-
let
|
|
20
|
-
|
|
21
|
-
// Do something after the connection to the Papertrail server is established
|
|
22
|
-
const handleConnect = (data) => {
|
|
23
|
-
return silent || modLog(data.replace('Papertrail', provider.toLowerCase()));
|
|
24
|
-
};
|
|
14
|
+
let botBuffer, bufferCycle, chatIds, log, tarLevel;
|
|
25
15
|
|
|
26
16
|
const hookConsole = () => {
|
|
27
|
-
for (let
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
console[
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
).join(' ').replace(/\u001b\[\d+m/g, '').split('');
|
|
17
|
+
for (let tar of consoleMap) {
|
|
18
|
+
const bakTar = `_${tar}`;
|
|
19
|
+
console[bakTar] = console[tar];
|
|
20
|
+
console[tar] = function() {
|
|
21
|
+
console[bakTar].apply(console, arguments);
|
|
22
|
+
const str = [...arguments].map(utilitas.ensureString).join(' ')
|
|
23
|
+
.replace(/\u001b\[\d+m/g, '')
|
|
24
|
+
.split('\n').filter(x => x.length).join('\n').split('');
|
|
36
25
|
while (str.length) {
|
|
37
|
-
const
|
|
38
|
-
|
|
26
|
+
const message = str.splice(0, maxLength).join('').trim();
|
|
27
|
+
message.length && log?.(tar, message);
|
|
39
28
|
}
|
|
40
29
|
};
|
|
41
30
|
}
|
|
42
31
|
};
|
|
43
32
|
|
|
44
33
|
const releaseConsole = () => {
|
|
45
|
-
for (let
|
|
46
|
-
const
|
|
47
|
-
if (!console[
|
|
48
|
-
console[
|
|
49
|
-
delete console[
|
|
34
|
+
for (let tar of consoleMap) {
|
|
35
|
+
const bakTar = `_${tar}`;
|
|
36
|
+
if (!console[bakTar]) { continue; }
|
|
37
|
+
console[tar] = console[bakTar];
|
|
38
|
+
delete console[bakTar];
|
|
50
39
|
}
|
|
51
40
|
};
|
|
52
41
|
|
|
53
|
-
const getDefaultOptions = async (provider, options) => {
|
|
54
|
-
const result = { program: (await utilitas.which()).name };
|
|
55
|
-
switch (provider) { case RSYSLOG: result.disableTls = true; }
|
|
56
|
-
// result.logFormat = (level, message) => { return message; }
|
|
57
|
-
return Object.assign(result, options || {});
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const sysLogInit = async (options) => {
|
|
61
|
-
options = await getDefaultOptions(provider, options);
|
|
62
|
-
const winston = await import('winston');
|
|
63
|
-
const papertrail = await import('winston-papertrail-mproved');
|
|
64
|
-
const papertrailConnection = new papertrail.PapertrailConnection(options);
|
|
65
|
-
papertrailConnection.on('error', handleError);
|
|
66
|
-
papertrailConnection.on('connect', handleConnect);
|
|
67
|
-
const papertrailTransport = new papertrail.PapertrailTransport(
|
|
68
|
-
papertrailConnection, options
|
|
69
|
-
);
|
|
70
|
-
logger = new winston.createLogger({ transports: [papertrailTransport] });
|
|
71
|
-
return {
|
|
72
|
-
logger,
|
|
73
|
-
dependencies: { winston, papertrail, papertrailConnection, papertrailTransport },
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
|
|
77
42
|
const addChatId = (id) => {
|
|
78
43
|
return chatIds && id && !chatIds.includes(id) ? chatIds.push(id) : null;
|
|
79
44
|
};
|
|
@@ -82,77 +47,67 @@ const removeChatId = (id) => {
|
|
|
82
47
|
return chatIds && id && chatIds.includes(id) ? delete chatIds[id] : false;
|
|
83
48
|
};
|
|
84
49
|
|
|
85
|
-
const
|
|
86
|
-
chatIds = utilitas.ensureArray(options?.chatId);
|
|
87
|
-
utilitas.assert(chatIds.length, 'ChatId is required.', 501);
|
|
88
|
-
handleConnect(`Sending logs via bot, chatId: ${chatIds.join(', ')}.`);
|
|
89
|
-
bufferCycle = utilitas.ensureInt(
|
|
90
|
-
options?.bufferCycle || defBufCycle, { min: 1, max: maxBufCycle }
|
|
91
|
-
);
|
|
92
|
-
logger = botLogger;
|
|
93
|
-
await event.loop(
|
|
94
|
-
botLoggerSync, ~~options?.interval || 5, ~~options?.tout || 10,
|
|
95
|
-
~~options?.delay, TAPE, { silent: true }
|
|
96
|
-
);
|
|
97
|
-
return { logger, dependencies: { bot, event } };
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const botLoggerSync = async () => {
|
|
50
|
+
const sync = async () => {
|
|
101
51
|
let f = [];
|
|
102
52
|
while (getSndSize(f) + nextLen() <= maxLength) { f.push(botBuffer.shift()) }
|
|
103
53
|
if (!(f = getSendTxt(f)).length) { return; };
|
|
104
54
|
for (let id of chatIds) {
|
|
105
|
-
try { await bot.send(id, f); } catch (err) {
|
|
55
|
+
try { await bot.send(id, f); } catch (err) { stdout(err.message); }
|
|
106
56
|
}
|
|
107
57
|
};
|
|
108
58
|
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
while (getSndSize(botBuffer) > getBufSize()) { botBuffer.shift(); }
|
|
114
|
-
},
|
|
115
|
-
end: () => {
|
|
116
|
-
chatIds = undefined; botBuffer = undefined; event.end(TAPE);
|
|
117
|
-
},
|
|
59
|
+
const rawLog = (level, message) => {
|
|
60
|
+
if (tarLevel !== 'verbose' && level === 'log') { return; }
|
|
61
|
+
(botBuffer = botBuffer || []).push([level, message]);
|
|
62
|
+
while (getSndSize(botBuffer) > getBufSize()) { botBuffer.shift(); }
|
|
118
63
|
};
|
|
119
64
|
|
|
120
65
|
// use options.level = 'verbose' to send console.log logs
|
|
121
66
|
const init = async (options) => {
|
|
122
|
-
let result;
|
|
123
67
|
if (options) {
|
|
124
|
-
|
|
68
|
+
utilitas.assert(
|
|
69
|
+
utilitas.insensitiveCompare(options?.provider, BOT),
|
|
70
|
+
`Invalid tape provider: '${options?.provider}'.`, 501
|
|
71
|
+
);
|
|
72
|
+
utilitas.assert(
|
|
73
|
+
(chatIds = utilitas.ensureArray(options?.chatId)).length,
|
|
74
|
+
'ChatId is required.', 501
|
|
75
|
+
);
|
|
125
76
|
tarLevel = options?.level;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
case RSYSLOG: // prefer this one
|
|
132
|
-
case PAPERTRAIL:
|
|
133
|
-
result = await sysLogInit(options);
|
|
134
|
-
break;
|
|
135
|
-
default:
|
|
136
|
-
utilitas.throwError(
|
|
137
|
-
`Invalid tape provider: '${options?.provider}'.`, 501
|
|
138
|
-
);
|
|
77
|
+
bufferCycle = utilitas.ensureInt(
|
|
78
|
+
options?.bufferCycle || defBufCycle, { min: 1, max: maxBufCycle }
|
|
79
|
+
);
|
|
80
|
+
if (!options?.silent) {
|
|
81
|
+
modLog(`Sending logs via bot, chatId: ${chatIds.join(', ')}.`);
|
|
139
82
|
}
|
|
83
|
+
log = rawLog;
|
|
84
|
+
await event.loop(
|
|
85
|
+
sync, ~~options?.interval || 5, ~~options?.tout || 10,
|
|
86
|
+
~~options?.delay, TAPE, { silent: true }
|
|
87
|
+
);
|
|
140
88
|
options.noHook || hookConsole();
|
|
141
89
|
}
|
|
142
|
-
utilitas.assert(
|
|
143
|
-
return
|
|
90
|
+
utilitas.assert(log, 'Tape has not been initialized.', 501);
|
|
91
|
+
return log;
|
|
144
92
|
};
|
|
145
93
|
|
|
146
94
|
const end = async () => {
|
|
147
95
|
releaseConsole();
|
|
148
|
-
setTimeout(() => {
|
|
96
|
+
setTimeout(async () => {
|
|
97
|
+
await event.end(TAPE);
|
|
98
|
+
botBuffer = undefined;
|
|
99
|
+
bufferCycle = undefined;
|
|
100
|
+
chatIds = undefined;
|
|
101
|
+
log = undefined;
|
|
102
|
+
tarLevel = undefined;
|
|
103
|
+
modLog('Terminated.');
|
|
104
|
+
}, 1000);
|
|
149
105
|
};
|
|
150
106
|
|
|
151
107
|
export default init;
|
|
152
108
|
export {
|
|
153
109
|
addChatId,
|
|
154
110
|
end,
|
|
155
|
-
getLogger,
|
|
156
111
|
init,
|
|
157
112
|
removeChatId,
|
|
158
113
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "utilitas",
|
|
3
3
|
"description": "Just another common utility for Node.js.",
|
|
4
|
-
"version": "1989.9.
|
|
4
|
+
"version": "1989.9.36",
|
|
5
5
|
"private": false,
|
|
6
6
|
"homepage": "https://github.com/Leask/utilitas",
|
|
7
7
|
"main": "index.mjs",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"url": "https://github.com/Leask/utilitas.git"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@sentry/node": "^6.17.
|
|
28
|
+
"@sentry/node": "^6.17.7",
|
|
29
29
|
"base64url": "^3.0.1",
|
|
30
30
|
"colors": "1.4.0",
|
|
31
31
|
"fast-geoip": "^1.1.58",
|
|
@@ -40,11 +40,10 @@
|
|
|
40
40
|
"node-mailjet": "^3.3.5",
|
|
41
41
|
"ping": "^0.4.1",
|
|
42
42
|
"qs": "^6.10.3",
|
|
43
|
+
"tail": "^2.2.4",
|
|
43
44
|
"telegraf": "^4.7.0",
|
|
44
45
|
"telesignsdk": "^2.2.1",
|
|
45
46
|
"twilio": "^3.74.0",
|
|
46
|
-
"uuid": "^8.3.2"
|
|
47
|
-
"winston": "^3.5.1",
|
|
48
|
-
"winston-papertrail-mproved": "^1.0.7"
|
|
47
|
+
"uuid": "^8.3.2"
|
|
49
48
|
}
|
|
50
49
|
}
|