@akc42/server-utils 4.0.7 → 4.0.9
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/debug-utils.js +128 -0
- package/debug.js +1 -1
- package/package.json +1 -2
- package/server-utils.js +2 -0
package/debug-utils.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@licence
|
|
3
|
+
Copyright (c) 2026 Alan Chandler, all rights reserved
|
|
4
|
+
|
|
5
|
+
This file is part of @akc42/app-utils.
|
|
6
|
+
|
|
7
|
+
@akc42/app-utils is free software: you can redistribute it and/or modify
|
|
8
|
+
it under the terms of the GNU General Public License as published by
|
|
9
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
(at your option) any later version.
|
|
11
|
+
|
|
12
|
+
@akc42/app-utils is distributed in the hope that it will be useful,
|
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
GNU General Public License for more details.
|
|
16
|
+
|
|
17
|
+
You should have received a copy of the GNU General Public License
|
|
18
|
+
along with @akc42/app-utils. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
*/
|
|
20
|
+
import chalk from "chalk";
|
|
21
|
+
|
|
22
|
+
export const COLOURS = {
|
|
23
|
+
hexmatch: /^#(?:[0-9a-fA-F]{3}){1,2}$/,
|
|
24
|
+
rgbmatch: /^(\d{1,3}), ?(\d{1,3}), ?(\d{1,3})$/,
|
|
25
|
+
COLOURS: {
|
|
26
|
+
app: chalk.rgb(255, 136, 0).bold, //orange,
|
|
27
|
+
db: chalk.greenBright,
|
|
28
|
+
api: chalk.magentaBright,
|
|
29
|
+
client: chalk.redBright,
|
|
30
|
+
log: chalk.hex('#ff651d'),
|
|
31
|
+
mail: chalk.cyanBright,
|
|
32
|
+
//error like have backGround colouring
|
|
33
|
+
auth: chalk.whiteBright.bgBlue,
|
|
34
|
+
error: chalk.whiteBright.bgHex('#ff1165')
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const ipmatch = /^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.|$)){4}$/;
|
|
38
|
+
const datematch =/^(\d{4})-([01]\d)-([0-3]\d) ([0-2]\d):([0-5]\d)(:([0-5]\d)(\.(\d{1,3}))?)?$/;
|
|
39
|
+
let lastdate = '';
|
|
40
|
+
|
|
41
|
+
export function messageFormatter(logid,logtime, crash, shortdate, ipaddress, topic, message, colourspec, gap) {
|
|
42
|
+
let matches;
|
|
43
|
+
if (typeof logtime === 'string') {
|
|
44
|
+
matches = logtime.match(datematch);
|
|
45
|
+
} else {
|
|
46
|
+
matches = [logtime];
|
|
47
|
+
}
|
|
48
|
+
const logdate = new Date(matches[0]);
|
|
49
|
+
logdate.setMinutes(logdate.getMinutes() + logdate.getTimezoneOffset());
|
|
50
|
+
const displaydate = `${logdate.getFullYear()}-${(logdate.getMonth() + 1).toString().padStart(2,'0')}-${logdate.getDate().toString().padStart(2,'0')}`;
|
|
51
|
+
const displaytime = `${logdate.getHours().toString().padStart(2,'0')}:${logdate.getMinutes().toString().padStart(2,'0')}:${
|
|
52
|
+
logdate.getSeconds().toString().padStart(2,'0')}.${
|
|
53
|
+
(typeof logtime === 'string')? (matches[9]??'').toString().padStart(3,'0') : logdate.getMilliseconds().toString().padStart(3,'0')}`;
|
|
54
|
+
const d = chalk.blueBright(`${displaydate} ${shortdate === 1? displaytime.slice(0,-7): displaytime}`);
|
|
55
|
+
const ip = ipmatch.test(ipaddress)? chalk.red(` [${ipaddress}]`) : '';
|
|
56
|
+
const t = chalk.greenBright(`(${topic})`);
|
|
57
|
+
let m;
|
|
58
|
+
let l = '';
|
|
59
|
+
if (crash === 1) {
|
|
60
|
+
l = chalk.whiteBright(` ${logid}`)
|
|
61
|
+
m = chalk.white.bgRed(message);
|
|
62
|
+
} else if (colourspec in COLOURS.COLOURS) {
|
|
63
|
+
m = COLOURS.COLOURS[colourspec](message);
|
|
64
|
+
} else if (COLOURS.hexmatch.test(colourspec)) {
|
|
65
|
+
m = chalk.hex(colourspec)(message);
|
|
66
|
+
} else if (COLOURS.rgbmatch.test(colourspec)) {
|
|
67
|
+
const matches = COLOURS.rgbmatch.exec(colourspec);
|
|
68
|
+
m = chalk.rgb(matches[1], matches[2], matches[3])(message);
|
|
69
|
+
} else {
|
|
70
|
+
m = chalk.cyan(message)
|
|
71
|
+
}
|
|
72
|
+
const g = Number.isInteger(gap)?chalk.whiteBright(` gap: ${shortdate? Math.round(gap/60000) + ' mins': gap + 'ms'}`):'';
|
|
73
|
+
let dayoutput = '';
|
|
74
|
+
if (lastdate !== displaydate) {
|
|
75
|
+
dayoutput = `${chalk.whiteBright(displaydate)}:\n`
|
|
76
|
+
lastdate = displaydate;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
dayoutput: dayoutput,
|
|
81
|
+
message:`${d}${l}${ip} ${t} ${m}${g}`,
|
|
82
|
+
logid: logid,
|
|
83
|
+
ip: ipaddress
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export function DebugHelper(topic, colourspec, shortdate, immediate, writer) {
|
|
88
|
+
const t = topic;
|
|
89
|
+
const cs = (colourspec in COLOURS.COLOURS) || COLOURS.hexmatch.test(colourspec) || COLOURS.rgbmatch.test(colourspec) ? colourspec : null;
|
|
90
|
+
const sd = shortdate? 1:0;
|
|
91
|
+
let timestamp = Date.now();
|
|
92
|
+
const i = immediate;
|
|
93
|
+
return function (c, logtime ,ip, ...args) {
|
|
94
|
+
let crash = 1;
|
|
95
|
+
if (c !== 'crash') {
|
|
96
|
+
if (ip !== undefined) {
|
|
97
|
+
if (Array.isArray(ip)) args = ip.concat(args); else args.unshift(ip);
|
|
98
|
+
}
|
|
99
|
+
ip = logtime;
|
|
100
|
+
logtime = c;
|
|
101
|
+
crash = 0;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const fromDate = new Date(); //logtime is only possible if later than midnight last night.
|
|
105
|
+
const from = fromDate.setHours(0,0,0,0)
|
|
106
|
+
if (!(Number.isInteger(logtime) && logtime > from.getTime())) {
|
|
107
|
+
if (ip !== undefined) {
|
|
108
|
+
if (Array.isArray(ip)) args = ip.concat(args); else args.unshift(ip);
|
|
109
|
+
}
|
|
110
|
+
ip = logtime;
|
|
111
|
+
logtime = Date.now();
|
|
112
|
+
}
|
|
113
|
+
if (!ipmatch.test(ip)) {
|
|
114
|
+
if (ip !== undefined){
|
|
115
|
+
if (Array.isArray(ip)) args = ip.concat(args); else args.unshift(ip);
|
|
116
|
+
}
|
|
117
|
+
ip = null
|
|
118
|
+
}
|
|
119
|
+
const now = Date.now();
|
|
120
|
+
const gap = now - timestamp;
|
|
121
|
+
timestamp = now;
|
|
122
|
+
const message = args.reduce((cum, arg) => {
|
|
123
|
+
if (arg === undefined) return cum;
|
|
124
|
+
return `${cum} ${arg}`.trim();
|
|
125
|
+
},'');
|
|
126
|
+
return writer(logtime, crash, sd, ip, t, message, cs, gap,i)
|
|
127
|
+
}
|
|
128
|
+
};
|
package/debug.js
CHANGED
|
@@ -20,7 +20,7 @@ import { EventEmitter } from 'node:events';
|
|
|
20
20
|
import { setTimeout } from 'node:timers/promises';
|
|
21
21
|
import chalk from "chalk";
|
|
22
22
|
import {openDatabase} from '@akc42/sqlite-db';
|
|
23
|
-
import { messageFormatter, COLOURS, DebugHelper } from '
|
|
23
|
+
import { messageFormatter, COLOURS, DebugHelper } from './debug-utils.js';
|
|
24
24
|
|
|
25
25
|
class DebugLogEvents extends EventEmitter {}
|
|
26
26
|
/*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akc42/server-utils",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A Set of Utilities to use on the Server Side of a Project",
|
|
6
6
|
"main": "./server-utils.js",
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"homepage": "https://github.com/akc42/server-utils#readme",
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@akc42/sqlite-db": "3.0.7",
|
|
26
|
-
"@akc42/app-utils": "5.0.13",
|
|
27
26
|
"chalk":"^5.6.2"
|
|
28
27
|
}
|
|
29
28
|
}
|
package/server-utils.js
CHANGED
|
@@ -23,9 +23,11 @@
|
|
|
23
23
|
import {getVersion}from './version.js';
|
|
24
24
|
import {Responder} from './responder.js';
|
|
25
25
|
import { Debug, Logger, logWriter, DebugLog, close, getDebugLog} from './debug.js';
|
|
26
|
+
import {COLOURS} from './debug-utils.js';
|
|
26
27
|
import { nullif0len } from './utils.js';
|
|
27
28
|
export {
|
|
28
29
|
close,
|
|
30
|
+
COLOURS,
|
|
29
31
|
Debug,
|
|
30
32
|
DebugLog,
|
|
31
33
|
getDebugLog,
|