utilitas 1989.9.28 → 1989.9.32

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 CHANGED
@@ -15,18 +15,18 @@ const c = (s, r) => { return ~~utilitas.insensitiveCompare(s, r, { w: true }) };
15
15
  let bot;
16
16
 
17
17
  const questions = [{
18
- q: ['The Ultimate Question of Life, the Universe, and Everything',
19
- 'The answer to life the universe and everything'],
20
- a: '42',
21
- }, {
22
18
  q: ['THE THREE LAWS'],
23
19
  a: ['- A robot may not injure a human being or, through inaction, allow a human being to come to harm.',
24
20
  '- A robot must obey the orders given it by human beings except where such orders would conflict with the First Law.',
25
21
  '- A robot must protect its own existence as long as such protection does not conflict with the First or Second Laws.'].join('\n'),
22
+ }, {
23
+ q: ['The Ultimate Question of Life, the Universe, and Everything',
24
+ 'The answer to life the universe and everything'],
25
+ a: '42',
26
26
  }];
27
27
 
28
28
  const subconscious = {
29
- run: true, name: 'Subconscious', func: async (bot) => {
29
+ run: true, name: 'subconscious', func: async (bot) => {
30
30
  bot.use(async (ctx, next) => {
31
31
  log(`Updated: ${ctx.update.update_id}`);
32
32
  process.stdout.write(`${JSON.stringify(ctx.update)}\n`);
package/lib/tape.mjs CHANGED
@@ -7,12 +7,16 @@ import * as utilitas from './utilitas.mjs';
7
7
  // Handle, report, or silently ignore connection errors and failures
8
8
  const handleError = (err) => { process.stdout.write(`${err.message}\n`); };
9
9
  const consoleMap = { log: 'verbose', info: 0, debug: 0, warn: 0, error: 0 };
10
- const [TAPE, defBufSize, maxLength] = ['TAPE', 100, 4096];
10
+ const [TAPE, maxLength, defBufCycle, maxBufCycle] = ['TAPE', 4096, 10, 100];
11
11
  const modLog = (content) => { return utilitas.modLog(content, TAPE); };
12
12
  const getLogger = async () => { return (await init()).logger; };
13
13
  const [BOT, RSYSLOG, PAPERTRAIL] = ['BOT', 'RSYSLOG', 'PAPERTRAIL'];
14
+ const getSendTxt = (arr) => { return arr.map(x => x[1]).join('\n'); };
15
+ const getSndSize = (arr) => { return getSendTxt(arr).length; };
16
+ const getBufSize = () => { return maxLength * bufferCycle; };
17
+ const nextLen = () => { return botBuffer?.[0]?.[1].length || (maxLength + 1); };
14
18
 
15
- let chatIds, tarLevel, botBuffer, bufferSize, logger, silent, provider;
19
+ let chatIds, tarLevel, botBuffer, bufferCycle, logger, silent, provider;
16
20
 
17
21
  // Do something after the connection to the Papertrail server is established
18
22
  const handleConnect = (data) => {
@@ -25,9 +29,14 @@ const hookConsole = () => {
25
29
  const bakAct = `_${act}`;
26
30
  console[bakAct] = console[act];
27
31
  console[act] = function() {
28
- const str = [...arguments].map(utilitas.ensureString).join(' ');
29
- logger && logger.log(tar, str.replace(/\u001b\[\d+m/g, ''));
30
32
  console[bakAct].apply(console, arguments);
33
+ const str = [...arguments].map(
34
+ utilitas.ensureString
35
+ ).join(' ').replace(/\u001b\[\d+m/g, '').split('');
36
+ while (str.length) {
37
+ const t = str.splice(0, maxLength).join('').trim();
38
+ t.length && logger && logger.log(tar, str);
39
+ }
31
40
  };
32
41
  }
33
42
  };
@@ -77,8 +86,8 @@ const botLoggerInit = async (options) => {
77
86
  chatIds = utilitas.ensureArray(options?.chatId);
78
87
  utilitas.assert(chatIds.length, 'ChatId is required.', 501);
79
88
  handleConnect(`Sending logs via bot, chatId: ${chatIds.join(', ')}.`);
80
- bufferSize = utilitas.ensureInt(
81
- options?.bufferSize || defBufSize, { min: 1, max: defBufSize }
89
+ bufferCycle = utilitas.ensureInt(
90
+ options?.bufferCycle || defBufCycle, { min: 1, max: maxBufCycle }
82
91
  );
83
92
  logger = botLogger;
84
93
  await event.loop(
@@ -89,11 +98,11 @@ const botLoggerInit = async (options) => {
89
98
  };
90
99
 
91
100
  const botLoggerSync = async () => {
92
- let fetched = (botBuffer?.length ? botBuffer.splice(0) : [
93
- ]).map(x => x[1]).join('\n').slice(0, maxLength);
94
- if (!fetched.length) { return; }
101
+ let f = [];
102
+ while (getSndSize(f) + nextLen() <= maxLength) { f.push(botBuffer.shift()) }
103
+ if (!(f = getSendTxt(f)).length) { return; };
95
104
  for (let id of chatIds) {
96
- try { await bot.send(id, fetched); } catch (err) { handleError(err); }
105
+ try { await bot.send(id, f); } catch (err) { handleError(err); }
97
106
  }
98
107
  };
99
108
 
@@ -101,7 +110,7 @@ const botLogger = {
101
110
  log: (level, message) => {
102
111
  if (tarLevel !== 'verbose' && level === 'verbose') { return; }
103
112
  (botBuffer = botBuffer || []).push([level, message]);
104
- botBuffer.splice(0, botBuffer.length - defBufSize);
113
+ while (getSndSize(botBuffer) > getBufSize()) { botBuffer.shift(); }
105
114
  },
106
115
  end: () => {
107
116
  chatIds = undefined; botBuffer = undefined; event.end(TAPE);
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.28",
4
+ "version": "1989.9.32",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",