@akc42/server-utils 4.0.1 → 4.0.3

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # server-utils
2
2
  A Set of Utilities that I generally use on SPA projects for the server side of the project
3
3
 
4
- It consists of 4 separate packages 8 entry points.
4
+ It consists of 4 separate packages 5 entry points.
5
5
 
6
6
  The packages are:-
7
7
 
@@ -40,41 +40,6 @@ This `debug` instance also remembers the time between calls and this time is log
40
40
 
41
41
  **Logger** is a function that is a wrapper for *Debug* where `shortdate` and `immediate` are both true.
42
42
 
43
- **messageFormatter** is the routine that formats the raw message that has been written to the database.
44
-
45
- It is called with the following parameters in order:-
46
-
47
- - *logid* The `logid` (the primary key) of the message in the database (only used in the message output if the item was
48
- a crash).
49
- - *logtime* This is either a unix timestamp *or* a string with the date and/or time in it. It should be in the same
50
- format as being formatted (see above).
51
- - *crash* a 0 or 1 dependant on if this message was a crash or not.
52
- - *shortdate* a 0 or 1 dependant on if this message has a short date or not,
53
- - *ipaddress* should be a valid ip address or `null`.
54
- - *topic*
55
- - *message* Just a single string
56
- - *colourspec*
57
- - *gap* Gap in milliseconds (this routine does the conversion to minutes if a `shortdate`).
58
-
59
- It returns an Object with 4 properties
60
-
61
- - *dayoutput* If the first message of the day, text with the date (only) in it, otherwise a zero length string.
62
- - *message* The complete formatted message
63
- - *logid* The `logid` the formatter was called with.
64
- - *ip* The `ipaddress` the formatter was called with.
65
-
66
- **DebugHelper** is a helper function for *Debug* and performs most of its work. It is called with the same parameters as *Debug* plus an additional one; *writer*. *writer* should be a callback function that can do something with the message and then return the return object that a debug call does. In the use by *Debug* this function is the one writes the data to the database, but other writers can be provided. For instance the *Debug* function is the `@akc42/app-utils` package uses the writer to send the message from the client to the server.
67
-
68
- *writer* is called with the following parameters (all described above for *debug*, although in this case they *must* be supplied)
69
-
70
- - *logtime* (unix timestamp)
71
- - *crash* (0 or 1)
72
- - *shortdate* (0 or 1)
73
- - *ipaddress* (or `null`)
74
- - *topic*
75
- - *colourspec*
76
- - *gap*
77
- - *immediate* (`true` or `false`)
78
43
 
79
44
  ## Responder
80
45
 
package/debug.js CHANGED
@@ -20,8 +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 } from './message-formatter.js';
24
- import { DebugHelper} from './debug-helper.js';
23
+ import { messageFormatter, COLOURS, DebugHelper } from '@akc42/app-utils';
25
24
 
26
25
  class DebugLogEvents extends EventEmitter {}
27
26
  /*
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akc42/server-utils",
3
- "version": "4.0.1",
3
+ "version": "4.0.3",
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",
@@ -22,7 +22,8 @@
22
22
  },
23
23
  "homepage": "https://github.com/akc42/server-utils#readme",
24
24
  "dependencies": {
25
- "@akc42/sqlite-db": "3.0.6",
26
- "chalk": "^5.6.2"
25
+ "@akc42/sqlite-db": "3.0.7",
26
+ "@akc42/app-utils": "5.0.11",
27
+ "chalk":"^5.6.2"
27
28
  }
28
29
  }
package/server-utils.js CHANGED
@@ -22,17 +22,12 @@
22
22
 
23
23
  import {getVersion}from './version.js';
24
24
  import {Responder} from './responder.js';
25
- import { messageFormatter, COLOURS } from './message-formatter.js';
26
- import { DebugHelper } from './debug-helper.js';
27
25
  import { Debug, Logger} from './debug.js';
28
26
  import { nullif0len } from './utils.js';
29
27
  export {
30
- COLOURS,
31
28
  Debug,
32
- DebugHelper,
33
29
  getVersion,
34
30
  Logger,
35
- messageFormatter,
36
31
  nullif0len,
37
32
  Responder
38
33
  };
package/debug-helper.js DELETED
@@ -1,60 +0,0 @@
1
- /**
2
- @licence
3
- Copyright (c) 2025 Alan Chandler, all rights reserved
4
-
5
- This file is part of PASv5, an implementation of the Patient Administration
6
- System used to support Accuvision's Laser Eye Clinics.
7
-
8
- PASv5 is licenced to Accuvision (and its successors in interest) free of royality payments
9
- and in perpetuity in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10
- implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Accuvision
11
- may modify, or employ an outside party to modify, any of the software provided that
12
- this modified software is only used as part of Accuvision's internal business processes.
13
-
14
- The software may be run on either Accuvision's own computers or on external computing
15
- facilities provided by a third party, provided that the software remains soley for use
16
- by Accuvision (or by potential or existing customers in interacting with Accuvision).
17
- */
18
-
19
- export function DebugHelper(topic, colourspec, shortdate, immediate, writer) {
20
- const t = topic;
21
- const cs = (colourspec in COLOURS.COLOURS) || COLOURS.hexmatch.test(colourspec) || COLOURS.rgbmatch.test(colourspec) ? colourspec : null;
22
- const sd = shortdate? 1:0;
23
- let timestamp = Date.now();
24
- const i = immediate;
25
- return function (c, logtime ,ip, ...args) {
26
- let crash = 1;
27
- if (c !== 'crash') {
28
- if (ip !== undefined) {
29
- if (Array.isArray(ip)) args = ip.concat(args); else args.unshift(ip);
30
- }
31
- ip = logtime;
32
- logtime = c;
33
- crash = 0;
34
- }
35
-
36
- const fromDate = new Date(); //logtime is only possible if later than midnight last night.
37
- const from = fromDate.setHours(0,0,0,0)
38
- if (!(Number.isInteger(logtime) && logtime > from)) {
39
- if (ip !== undefined) {
40
- if (Array.isArray(ip)) args = ip.concat(args); else args.unshift(ip);
41
- }
42
- ip = logtime;
43
- logtime = Date.now();
44
- }
45
- if (!ipmatch.test(ip)) {
46
- if (ip !== undefined){
47
- if (Array.isArray(ip)) args = ip.concat(args); else args.unshift(ip);
48
- }
49
- ip = null
50
- }
51
- const now = Date.now();
52
- const gap = now - timestamp;
53
- timestamp = now;
54
- const message = args.reduce((cum, arg) => {
55
- if (arg === undefined) return cum;
56
- return `${cum} ${arg}`.trim();
57
- },'');
58
- return writer(logtime, crash, sd, ip, t, message, cs, gap,i)
59
- }
60
- };
@@ -1,84 +0,0 @@
1
- /**
2
- @licence
3
- Copyright (c) 2026 Alan Chandler, all rights reserved
4
-
5
- This file is part of Server Utils.
6
-
7
- Server 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
- Server 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 Server Utils. If not, see <http://www.gnu.org/licenses/>.
19
- */
20
- import chalk from "chalk";
21
- export const COLOURS = {
22
- hexmatch: /^#(?:[0-9a-fA-F]{3}){1,2}$/,
23
- rgbmatch: /^(\d{1,3}), ?(\d{1,3}), ?(\d{1,3})$/,
24
- COLOURS: {
25
- app: chalk.rgb(255, 136, 0).bold, //orange,
26
- db: chalk.greenBright,
27
- api: chalk.magentaBright,
28
- client: chalk.redBright,
29
- log: chalk.hex('#ff651d'),
30
- mail: chalk.cyanBright,
31
- //error like have backGround colouring
32
- auth: chalk.whiteBright.bgBlue,
33
- error: chalk.whiteBright.bgHex('#ff1165')
34
- }
35
- };
36
- const ipmatch = /^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)(\.|$)){4}$/;
37
- const datematch =/^(\d{4})-([01]\d)-([0-3]\d) ([0-2]\d):([0-5]\d)(:([0-5]\d)(\.(\d{1,3}))?)?$/;
38
- let lastdate = '';
39
-
40
- export function messageFormatter(logid,logtime, crash, shortdate, ipaddress, topic, message, colourspec, gap) {
41
- let matches;
42
- if (typeof logtime === 'string') {
43
- matches = logtime.match(datematch);
44
- } else {
45
- matches = [logtime];
46
- }
47
- const logdate = new Date(matches[0]);
48
- logdate.setMinutes(logdate.getMinutes() + logdate.getTimezoneOffset());
49
- const displaydate = `${logdate.getFullYear()}-${(logdate.getMonth() + 1).toString().padStart(2,'0')}-${logdate.getDate().toString().padStart(2,'0')}`;
50
- const displaytime = `${logdate.getHours().toString().padStart(2,'0')}:${logdate.getMinutes().toString().padStart(2,'0')}:${
51
- logdate.getSeconds().toString().padStart(2,'0')}.${
52
- (typeof logtime === 'string')? (matches[9]??'').toString().padStart(3,'0') : logdate.getMilliseconds().toString().padStart(3,'0')}`;
53
- const d = chalk.blueBright(`${displaydate} ${shortdate === 1? displaytime.slice(0,-7): displaytime}`);
54
- const ip = ipmatch.test(ipaddress)? chalk.red(` [${ipaddress}]`) : '';
55
- const t = chalk.greenBright(`(${topic})`);
56
- let m;
57
- let l = '';
58
- if (crash === 1) {
59
- l = chalk.whiteBright(` ${logid}`)
60
- m = chalk.white.bgRed(message);
61
- } else if (colourspec in COLOURS.COLOURS) {
62
- m = COLOURS.COLOURS[colourspec](message);
63
- } else if (COLOURS.hexmatch.test(colourspec)) {
64
- m = chalk.hex(colourspec)(message);
65
- } else if (COLOURS.rgbmatch.test(colourspec)) {
66
- const matches = COLOURS.rgbmatch.exec(colourspec);
67
- m = chalk.rgb(matches[1], matches[2], matches[3])(message);
68
- } else {
69
- m = chalk.cyan(message)
70
- }
71
- const g = Number.isInteger(gap)?chalk.whiteBright(` gap: ${shortdate? Math.round(gap/60000) + ' mins': gap + 'ms'}`):'';
72
- let dayoutput = '';
73
- if (lastdate !== displaydate) {
74
- dayoutput = `${chalk.whiteBright(displaydate)}:\n`
75
- lastdate = displaydate;
76
- }
77
-
78
- return {
79
- dayoutput: dayoutput,
80
- message:`${d}${l}${ip} ${t} ${m}${g}`,
81
- logid: logid,
82
- ip: ipaddress
83
- }
84
- };