Continue TS Implementation

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-07-25 01:00:36 +01:00
parent d07cd177b3
commit 93c8aed105
10 changed files with 171 additions and 22 deletions

6
app.ts
View File

@ -54,7 +54,7 @@ log.out(`app: Starting OwlBoard - Backend Version: ${version.app} - ` +
`API versions: ${version.api}`, 'init'); `API versions: ${version.api}`, 'init');
// Express Error Handling: // Express Error Handling:
app.use((err, req, res, next) => { app.use((err: Error, req: Request, res, next) => {
const statusCode = err.statuscode || 500; const statusCode = err.statuscode || 500;
console.error(err.message, err.stack); console.error(err.message, err.stack);
res.status(statusCode).json({'message': err.message}); res.status(statusCode).json({'message': err.message});
@ -103,11 +103,11 @@ mode === 'development'
: null; : null;
// Start Express // Start Express
app.listen(srvPort, srvListen, (error) =>{ app.listen(srvPort, srvListen, (error: Error) =>{
if(!error) { if(!error) {
log.out(`app.listen: Listening on http://${srvListen}:${srvPort}`, 'init'); log.out(`app.listen: Listening on http://${srvListen}:${srvPort}`, 'init');
log.out('app.listen: State - alive', 'init'); log.out('app.listen: State - alive', 'init');
} else { } else {
log.out(`app.listen: Error occurred, server can't start ${error}`, 'err'); log.out(`app.listen: Error occurred, server can't start ${error.message}`, 'err');
} }
}); });

100
package-lock.json generated
View File

@ -18,7 +18,9 @@
"juice": "^9.0.0", "juice": "^9.0.0",
"ldbs-json": "^1.2.1", "ldbs-json": "^1.2.1",
"mongodb": "^4.13.0", "mongodb": "^4.13.0",
"nodemailer": "^6.9.1" "nodemailer": "^6.9.1",
"redis": "^4.6.7",
"zlib": "^1.0.5"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^8.39.0", "eslint": "^8.39.0",
@ -823,6 +825,59 @@
"node": ">= 8" "node": ">= 8"
} }
}, },
"node_modules/@redis/bloom": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@redis/bloom/-/bloom-1.2.0.tgz",
"integrity": "sha512-HG2DFjYKbpNmVXsa0keLHp/3leGJz1mjh09f2RLGGLQZzSHpkmZWuwJbAvo3QcRY8p80m5+ZdXZdYOSBLlp7Cg==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@redis/client": {
"version": "1.5.8",
"resolved": "https://registry.npmjs.org/@redis/client/-/client-1.5.8.tgz",
"integrity": "sha512-xzElwHIO6rBAqzPeVnCzgvrnBEcFL1P0w8P65VNLRkdVW8rOE58f52hdj0BDgmsdOm4f1EoXPZtH4Fh7M/qUpw==",
"dependencies": {
"cluster-key-slot": "1.1.2",
"generic-pool": "3.9.0",
"yallist": "4.0.0"
},
"engines": {
"node": ">=14"
}
},
"node_modules/@redis/graph": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@redis/graph/-/graph-1.1.0.tgz",
"integrity": "sha512-16yZWngxyXPd+MJxeSr0dqh2AIOi8j9yXKcKCwVaKDbH3HTuETpDVPcLujhFYVPtYrngSco31BUcSa9TH31Gqg==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@redis/json": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.4.tgz",
"integrity": "sha512-LUZE2Gdrhg0Rx7AN+cZkb1e6HjoSKaeeW8rYnt89Tly13GBI5eP4CwDVr+MY8BAYfCg4/N15OUrtLoona9uSgw==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@redis/search": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/@redis/search/-/search-1.1.3.tgz",
"integrity": "sha512-4Dg1JjvCevdiCBTZqjhKkGoC5/BcB7k9j99kdMnaXFXg8x4eyOIVg9487CMv7/BUVkFLZCaIh8ead9mU15DNng==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@redis/time-series": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.0.4.tgz",
"integrity": "sha512-ThUIgo2U/g7cCuZavucQTQzA9g9JbDDY2f64u3AbAoz/8vE2lt2U37LamDUVChhaDA3IRT9R6VvJwqnUfTJzng==",
"peerDependencies": {
"@redis/client": "^1.0.0"
}
},
"node_modules/@smithy/abort-controller": { "node_modules/@smithy/abort-controller": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/@smithy/abort-controller/-/abort-controller-1.0.2.tgz", "resolved": "https://registry.npmjs.org/@smithy/abort-controller/-/abort-controller-1.0.2.tgz",
@ -1675,6 +1730,14 @@
"node": ">= 4.0" "node": ">= 4.0"
} }
}, },
"node_modules/cluster-key-slot": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.2.tgz",
"integrity": "sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/color-convert": { "node_modules/color-convert": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
@ -2417,6 +2480,14 @@
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
}, },
"node_modules/generic-pool": {
"version": "3.9.0",
"resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.9.0.tgz",
"integrity": "sha512-hymDOu5B53XvN4QT9dBmZxPX4CWhBPPLguTZ9MMFeFa/Kg0xWVfylOVNlJji/E7yTZWFd/q9GO5TxDLq156D7g==",
"engines": {
"node": ">= 4"
}
},
"node_modules/get-intrinsic": { "node_modules/get-intrinsic": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.1.tgz", "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.1.tgz",
@ -3287,6 +3358,19 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/redis": {
"version": "4.6.7",
"resolved": "https://registry.npmjs.org/redis/-/redis-4.6.7.tgz",
"integrity": "sha512-KrkuNJNpCwRm5vFJh0tteMxW8SaUzkm5fBH7eL5hd/D0fAkzvapxbfGPP/r+4JAXdQuX7nebsBkBqA2RHB7Usw==",
"dependencies": {
"@redis/bloom": "1.2.0",
"@redis/client": "1.5.8",
"@redis/graph": "1.1.0",
"@redis/json": "1.0.4",
"@redis/search": "1.1.3",
"@redis/time-series": "1.0.4"
}
},
"node_modules/relateurl": { "node_modules/relateurl": {
"version": "0.2.7", "version": "0.2.7",
"resolved": "https://registry.npmjs.org/relateurl/-/relateurl-0.2.7.tgz", "resolved": "https://registry.npmjs.org/relateurl/-/relateurl-0.2.7.tgz",
@ -3890,6 +3974,11 @@
"node": ">=4.0" "node": ">=4.0"
} }
}, },
"node_modules/yallist": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
},
"node_modules/yocto-queue": { "node_modules/yocto-queue": {
"version": "0.1.0", "version": "0.1.0",
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
@ -3901,6 +3990,15 @@
"funding": { "funding": {
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
},
"node_modules/zlib": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/zlib/-/zlib-1.0.5.tgz",
"integrity": "sha512-40fpE2II+Cd3k8HWTWONfeKE2jL+P42iWJ1zzps5W51qcTsOUKM5Q5m2PFb0CLxlmFAaUuUdJGc3OfZy947v0w==",
"hasInstallScript": true,
"engines": {
"node": ">=0.2.0"
}
} }
} }
} }

View File

@ -9,7 +9,9 @@
"juice": "^9.0.0", "juice": "^9.0.0",
"ldbs-json": "^1.2.1", "ldbs-json": "^1.2.1",
"mongodb": "^4.13.0", "mongodb": "^4.13.0",
"nodemailer": "^6.9.1" "nodemailer": "^6.9.1",
"redis": "^4.6.7",
"zlib": "^1.0.5"
}, },
"name": "owlboard", "name": "owlboard",
"description": "OwlBoard is an API and PWA for live rail departure board in the UK.", "description": "OwlBoard is an API and PWA for live rail departure board in the UK.",
@ -18,7 +20,8 @@
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js", "start": "node app.js",
"run": "node app.js" "run": "tsc && node dist/app.js",
"build": "tsc"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -0,0 +1,24 @@
import { createClient } from "redis";
import zlib from 'zlib';
const client = createClient({
url: "redis:PORT"
});
client.on('error', err => console.log('Redis Client Error', err));
async function addToCache(key: string, value: Object): Promise<boolean> {
throw new Error("Unable to post to cache")
}
async function getFromCache(key: string): Promise<Object> {
throw new Error("Unable to retreive")
}
/*
await client.connect();
await client.set('key', 'value');
const value = await client.get('key');
await client.disconnect();
*/

View File

@ -1,5 +1,5 @@
const log = require('../utils/log.utils'); const logs = require('../utils/logs.utils');
const crypto = require('crypto'); const crypt = require('crypt');
const db = require('../services/dbAccess.services'); const db = require('../services/dbAccess.services');
const fs = require('fs/promises'); const fs = require('fs/promises');
const minify = require('../utils/minify.utils'); const minify = require('../utils/minify.utils');
@ -8,7 +8,7 @@ const minify = require('../utils/minify.utils');
async function isAuthed(uuid: string) { // Needs testing async function isAuthed(uuid: string) { // Needs testing
const q = {uuid: uuid}; const q = {uuid: uuid};
const res = await db.query('users', q); const res = await db.query('users', q);
log.out('authUtils.checkUser: DB Query answer: ' + logs.out('authUtils.checkUser: DB Query answer: ' +
JSON.stringify(res[0]), 'dbug'); JSON.stringify(res[0]), 'dbug');
const authorized = res && res[0] && res[0].domain; const authorized = res && res[0] && res[0].domain;
if (authorized) db.userAtime(uuid); if (authorized) db.userAtime(uuid);
@ -20,7 +20,7 @@ async function checkRequest(key: string) {
const collection = 'registrations'; const collection = 'registrations';
const query = {uuid: key}; const query = {uuid: key};
const res = await db.query(collection, query); const res = await db.query(collection, query);
log.out('authUtils.checkRequest: DB Query result: ' + logs.out('authUtils.checkRequest: DB Query result: ' +
JSON.stringify(res), 'dbug'); JSON.stringify(res), 'dbug');
const result = res.length > 0 && res[0].time const result = res.length > 0 && res[0].time
? { result: true, domain: res[0].domain } ? { result: true, domain: res[0].domain }
@ -30,7 +30,7 @@ async function checkRequest(key: string) {
// Creates an API key for a user // Creates an API key for a user
async function generateKey() { // Needs testing & moving to 'register.utils' async function generateKey() { // Needs testing & moving to 'register.utils'
return crypto.randomUUID(); return crypt.randomUUID();
} }
async function generateConfirmationEmail(eml: string, uuid: string) { async function generateConfirmationEmail(eml: string, uuid: string) {
@ -46,7 +46,7 @@ async function generateConfirmationEmail(eml: string, uuid: string) {
html: htmlMin html: htmlMin
}; };
} catch(err) { } catch(err) {
log.out('mailServices.generateConfirmationEmail: Error reading template, ' + logs.out('mailServices.generateConfirmationEmail: Error reading template, ' +
err, 'err'); err, 'err');
return false; return false;
} }
@ -57,4 +57,11 @@ module.exports = {
generateKey, generateKey,
generateConfirmationEmail, generateConfirmationEmail,
checkRequest checkRequest
}; };
export {
isAuthed,
generateKey,
generateConfirmationEmail,
checkRequest
}

View File

@ -1,8 +1,10 @@
const log = require('../utils/log.utils'); // Log Helper const log = require('../utils/log.utils'); // Log Helper
const db = require('../services/dbAccess.services'); // DB Access const db = require('../services/dbAccess.services'); // DB Access
const san = require('../utils/sanitizer.utils'); // Sanitiser //const san = require('../utils/sanitizer.utils'); // Sanitiser
async function checkCrs(input){ import * as san from '../utils/sanitizer.utils'
async function checkCrs(input = ""){
var INPUT = input.toUpperCase(); var INPUT = input.toUpperCase();
var query = { var query = {
'$or':[ '$or':[
@ -22,10 +24,10 @@ async function cleanMessages(input){
log.out('ldbUtils.cleanMessages: Deprecated function has been called', 'err'); log.out('ldbUtils.cleanMessages: Deprecated function has been called', 'err');
var out = []; var out = [];
if (typeof input.message == 'string') { if (typeof input.message == 'string') {
out.push(await san.cleanNrcc(input.message)); out.push(san.cleanNrcc(input.message));
} else if (typeof input.message == 'object') { } else if (typeof input.message == 'object') {
for(var i = 0; i < input.message.length; i++) { for(var i = 0; i < input.message.length; i++) {
out.push(await san.cleanNrcc(input.message[i])); out.push(san.cleanNrcc(input.message[i]));
} }
} }
return out; return out;

View File

@ -14,4 +14,6 @@ async function out(msg: string, level = 'othr') {
module.exports = { module.exports = {
out out
}; };
export { out }

View File

@ -1,10 +1,13 @@
const htmlShrink = require('html-minifier').minify; const htmlShrink = require('html-minifier').minify;
const juice = require('juice'); const juice = require('juice');
module.exports = async function minifyMail(input: string): Promise<string> { async function minifyMail(input: string): Promise<string> {
const inlined: string = juice(input); const inlined: string = juice(input);
return htmlShrink(inlined, { return htmlShrink(inlined, {
removeComments: true, removeComments: true,
collapseWhitespace: true collapseWhitespace: true
}); });
}; };
module.exports = {minifyMail}
export {minifyMail}

View File

@ -33,4 +33,14 @@ module.exports = {
removeNonNumeric, removeNonNumeric,
cleanNrcc, cleanNrcc,
getDomainFromEmail, getDomainFromEmail,
}; };
export {
cleanApiEndpointTxt,
cleanApiEndpointNum,
removeNonAlpha,
removeNonAlphanumeric,
removeNonNumeric,
cleanNrcc,
getDomainFromEmail,
}

View File

@ -45,7 +45,7 @@
/* JavaScript Support */ /* JavaScript Support */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ "checkJs": false, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */ /* Emit */