@akc42/server-utils 1.0.7 → 1.3.0
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 +10 -1
- package/logger.js +36 -28
- package/package.json +3 -4
- package/version.js +22 -14
- package/database.js +0 -65
package/README.md
CHANGED
|
@@ -23,7 +23,16 @@ with `new Responder(response);` and the resultant object has three methods;
|
|
|
23
23
|
promise which resolves when any blockage is cleared.
|
|
24
24
|
- `end` signifies the end of stream. Any attempt to call the other two methods after this has been called will throw an error.
|
|
25
25
|
|
|
26
|
-
`version` provides a promise that ultimately resolves to an object which has two
|
|
26
|
+
`version` provides a promise that ultimately resolves to an object which has two
|
|
27
|
+
fields. `version` which is the version string and `year` which is the copyright
|
|
28
|
+
year. It does this with the help of the npm module `app-root-path` read the
|
|
29
|
+
instructions for that module if it doesn't give the right result for you. The
|
|
30
|
+
project root is where either the `.git` directory exists (in which case
|
|
31
|
+
`version` will ask git for the version and calculate the copyright year from the
|
|
32
|
+
last git log entry) or where a `release.info` file is sitting (in which case
|
|
33
|
+
`version` will expect that to contain a version string and have a modification
|
|
34
|
+
time from which the copyright year can be derived). If neither of those
|
|
35
|
+
possibilities exist it will try to get the version info from the `package.json` file.
|
|
27
36
|
|
|
28
37
|
These can either be installed all together
|
|
29
38
|
```
|
package/logger.js
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
(function() {
|
|
25
25
|
'use strict';
|
|
26
26
|
|
|
27
|
-
const
|
|
27
|
+
const chalkPromise = import('chalk');
|
|
28
28
|
const { isIP } = require('net');
|
|
29
29
|
let cyrb53;
|
|
30
30
|
|
|
@@ -43,35 +43,43 @@
|
|
|
43
43
|
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
46
|
+
const COLOURPromise = new Promise(async accept => {
|
|
47
|
+
const {default: chalk} = await import('chalk');
|
|
48
|
+
accept({
|
|
49
|
+
app: chalk.rgb(255, 136, 0).bold, //orange,
|
|
50
|
+
db: chalk.greenBright,
|
|
51
|
+
api: chalk.magentaBright,
|
|
52
|
+
client: chalk.redBright,
|
|
53
|
+
log: chalk.yellowBright,
|
|
54
|
+
mail: chalk.cyanBright,
|
|
55
|
+
//error like have backGround colouring
|
|
56
|
+
auth: chalk.black.bgCyan,
|
|
57
|
+
err: chalk.white.bgBlue,
|
|
58
|
+
error: chalk.white.bgRed
|
|
59
|
+
});
|
|
60
|
+
});
|
|
57
61
|
|
|
58
|
-
|
|
62
|
+
|
|
63
|
+
function logger(ip,level, ...messages) {
|
|
59
64
|
if (process.env.LOG_NONE === undefined) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
65
|
+
COLOURPromise.then(COLOURS => {
|
|
66
|
+
let logLine = '';
|
|
67
|
+
if (process.env.LOG_NO_DATE === undefined) logLine += new Date().toISOString() + ': ';
|
|
68
|
+
let message;
|
|
69
|
+
let logcolor;
|
|
70
|
+
if (isIP(ip) === 0 ) {
|
|
71
|
+
logcolor = ip;
|
|
72
|
+
message = level + messages.join(' ');
|
|
73
|
+
} else {
|
|
74
|
+
const client = process.env.LOG_IP_HIDDEN !== undefined ? cyrb53(ip): ip;
|
|
75
|
+
logLine += COLOURS.client(client + ': ');
|
|
76
|
+
logcolor = level
|
|
77
|
+
message = messages.join(' ');
|
|
78
|
+
}
|
|
79
|
+
logLine += COLOURS[logcolor](message);
|
|
80
|
+
//eslint-disable-next-line no-console
|
|
81
|
+
console.log(logLine.trim());
|
|
82
|
+
});
|
|
75
83
|
}
|
|
76
84
|
}
|
|
77
85
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akc42/server-utils",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "A Set of Utilities to use on the Server Side of a Project",
|
|
5
5
|
"main": "utils.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,8 +22,7 @@
|
|
|
22
22
|
"homepage": "https://github.com/akc42/server-utils#readme",
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"app-root-path": "^3.0.0",
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"debug": "^4.2.0"
|
|
25
|
+
"chalk": "^5.0.0",
|
|
26
|
+
"debug": "^4.3.3"
|
|
28
27
|
}
|
|
29
28
|
}
|
package/version.js
CHANGED
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
const child = require('child_process');
|
|
25
25
|
const root = require('app-root-path').toString();
|
|
26
26
|
|
|
27
|
+
|
|
27
28
|
function shCmd(cmd) {
|
|
28
29
|
debug('About to execute Command ', cmd);
|
|
29
30
|
return new Promise((resolve, reject) => {
|
|
@@ -48,21 +49,16 @@
|
|
|
48
49
|
debug('Look for git')
|
|
49
50
|
await fs.access(path.resolve(root, '.git'));
|
|
50
51
|
debug('Git found, so use it to get data')
|
|
52
|
+
//we get here if there is a git directory, so we can look up version and latest commit from them
|
|
53
|
+
version = await shCmd('git describe --abbrev=0 --tags');
|
|
54
|
+
//git is installed and we found a tag
|
|
51
55
|
try {
|
|
52
|
-
|
|
53
|
-
version = await shCmd('git describe --abbrev=0 --tags');
|
|
54
|
-
try {
|
|
55
|
-
vtime = await shCmd('git log -1 --format=%cd');
|
|
56
|
-
} catch (e) {
|
|
57
|
-
vtime = new Date(); //fake it;
|
|
58
|
-
}
|
|
56
|
+
vtime = await shCmd('git log -1 --format=%cd');
|
|
59
57
|
} catch (e) {
|
|
60
|
-
|
|
61
|
-
version = 'v0.0.1';
|
|
62
|
-
vtime = new Date();
|
|
58
|
+
vtime = new Date(); //fake it;
|
|
63
59
|
}
|
|
64
60
|
} catch (e) {
|
|
65
|
-
//no git, so we must look for a version file
|
|
61
|
+
//no git, or no tag, so we must look for a version file
|
|
66
62
|
try {
|
|
67
63
|
debug('Git approach failed, so look for release info');
|
|
68
64
|
version = await fs.readFile(path.resolve(root, 'release.info'), 'utf8');
|
|
@@ -73,10 +69,22 @@
|
|
|
73
69
|
vtime = new Date();
|
|
74
70
|
}
|
|
75
71
|
} catch(e) {
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
//no release info file, so use package.json
|
|
73
|
+
try {
|
|
74
|
+
const pjsonfile = path.resolve(root, 'package.json');
|
|
75
|
+
const pjson = require(pjsonfile);
|
|
76
|
+
version = 'v'+ pjson.version;
|
|
77
|
+
try {
|
|
78
|
+
const { mtime } = await fs.stat(pjsonfile);
|
|
79
|
+
vtime = mtime;
|
|
80
|
+
} catch (e) {
|
|
81
|
+
vtime = new Date();
|
|
82
|
+
}
|
|
83
|
+
} catch(e) {
|
|
84
|
+
version = 'v1.0.0';
|
|
85
|
+
vtime = new Date();
|
|
86
|
+
}
|
|
78
87
|
}
|
|
79
|
-
|
|
80
88
|
} finally {
|
|
81
89
|
const copyrightTime = new Date(vtime);
|
|
82
90
|
debug('Resolving with Git copyright Year is ', copyrightTime.getUTCFullYear());
|
package/database.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
@licence
|
|
3
|
-
Copyright (c) 2020 Alan Chandler, all rights reserved
|
|
4
|
-
|
|
5
|
-
This file is part of Meeting.
|
|
6
|
-
|
|
7
|
-
Meeting 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
|
-
Meeting 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 Meeting. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
-
*/
|
|
20
|
-
(()=>{
|
|
21
|
-
'use strict';
|
|
22
|
-
const path = require('path');
|
|
23
|
-
const fs = require('fs');
|
|
24
|
-
|
|
25
|
-
const debug = require('debug')('database');
|
|
26
|
-
|
|
27
|
-
const Database = require('better-sqlite3');
|
|
28
|
-
const root = require('app-root-path').toString();
|
|
29
|
-
const dbfilename = path.resolve(root,process.env.DATABASE_DB_DIR, process.env.DATABASE_DB);
|
|
30
|
-
let db;
|
|
31
|
-
try {
|
|
32
|
-
db = new Database(dbfilename, {fileMustExist:true, timeout: parseInt(process.env.DATABASE_DB_BUSY,10)});
|
|
33
|
-
} catch(e) {
|
|
34
|
-
if (e.code === 'SQLITE_CANTOPEN') {
|
|
35
|
-
//looks like database didn't exist, so we had better make if from scratch
|
|
36
|
-
try {
|
|
37
|
-
debug ('could not open database as it did not exist - so now going to create it');
|
|
38
|
-
db = new Database(dbfilename, { fileMustExist: false, timeout: parseInt(process.env.DATABASE_DB_BUSY,10) });
|
|
39
|
-
debug('Opened database - ready to start creating structure');
|
|
40
|
-
const database = fs.readFileSync(path.resolve(root, process.env.DATABASE_INIT_FILE), 'utf8');
|
|
41
|
-
db.exec(database);
|
|
42
|
-
if (process.env.DATABASE_DB_PIN === undefined) {
|
|
43
|
-
/*
|
|
44
|
-
Make ourselves a random, pin which I can use as a tokenKey and then write it into the database
|
|
45
|
-
|
|
46
|
-
*/
|
|
47
|
-
const pin = 'T' + ('000000' + (Math.floor(Math.random() * 999999)).toString()).slice(-6); //make a new pin
|
|
48
|
-
debug('going to use', pin, 'as our token key');
|
|
49
|
-
db.prepare(`UPDATE settings SET value = ? WHERE name = 'token_key'`).run(pin);
|
|
50
|
-
}
|
|
51
|
-
debug('Successfully updated blank database with script')
|
|
52
|
-
} catch (e) {
|
|
53
|
-
fs.unlinkSync(dbfilename); //failed to create it. so delete it so we can correct problem and try again.
|
|
54
|
-
throw new Error(`Encountered ${e.toString()} error when trying to create ${dbfilename}`)
|
|
55
|
-
}
|
|
56
|
-
} else {
|
|
57
|
-
throw new Error(`Encountered ${e.toString()} error when opening database`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
db.pragma('foreign_keys = ON');
|
|
62
|
-
|
|
63
|
-
process.on('exit', () => db.close());
|
|
64
|
-
module.exports = db;
|
|
65
|
-
})();
|