mp-holistic
Version:
si prefigge l'umile compito di aiutare nella creazione di : - un server API REST mediante express - un semplice ORM per Postgres Questo tramite decoratori o classi particolari.
258 lines • 9.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InizializzaLogbaseOut = exports.ConstruisciErrore = exports.Rispondi = exports.InizializzaLogbaseIn = exports.CalcolaChiaveMemoryCache = exports.GestioneErrore = exports.SostituisciRicorsivo = exports.IsJsonString = void 0;
const crypto_1 = __importDefault(require("crypto"));
const ErroreMio_1 = require("./ErroreMio");
const main_express_1 = require("../main.express");
const fs_1 = __importDefault(require("fs"));
function IsJsonString(str) {
try {
// eslint-disable-next-line no-useless-escape
if (/^[\],:{}\s]*$/.test(str.replace(/\\["\\\/bfnrtu]/g, '@').
// eslint-disable-next-line no-useless-escape
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
//the json is ok
if (typeof str === 'object') {
return true;
}
else {
return false;
}
}
else {
//the json is not ok
return false;
}
}
catch (e) {
return false;
}
}
exports.IsJsonString = IsJsonString;
function SostituisciRicorsivo(sanific, currentNode) {
// eslint-disable-next-line prefer-const
for (let attribut in currentNode) {
if (typeof currentNode[attribut] === 'object') {
currentNode[attribut] = SostituisciRicorsivo(sanific, currentNode[attribut]);
}
else {
for (let index = 0; index < sanific.length; index++) {
const element = sanific[index];
if (attribut == element.campoDaCercare) {
currentNode[attribut] = element.valoreFuturo;
}
}
}
return currentNode;
}
}
exports.SostituisciRicorsivo = SostituisciRicorsivo;
function GestioneErrore(item) {
var _a;
let errore;
const messaggio = (_a = '_CLASSE_->' + item.nomeClasse) !== null && _a !== void 0 ? _a : '' + '_FUNZIONE_->' + item.nomeFunzione;
if (item.error instanceof ErroreMio_1.ErroreMio) {
const tmp = item.error;
tmp.percorsoErrore = messaggio + '->' + tmp.percorsoErrore;
errore = tmp;
}
else {
errore = new ErroreMio_1.ErroreMio({
codiceErrore: 499,
messaggio: '' + item.error,
percorsoErrore: messaggio
});
}
return errore;
}
exports.GestioneErrore = GestioneErrore;
function CalcolaChiaveMemoryCache(req) {
const keySHA = 'Besagno';
const headerTmp = req.headers['authorization'] != undefined ? String(req.headers['authorization']) : JSON.stringify({
"Aauthorization9X": "10"
});
const tmp = '-' + JSON.stringify(req.body) + '-' + headerTmp + '-' + JSON.stringify(req.query) + '-';
const tmpmd = crypto_1.default.createHmac('sha1', keySHA)
.update(tmp).digest('hex');
const ritorno = '__express__' + req.url + '__MP__' + tmpmd + '__';
return ritorno;
}
exports.CalcolaChiaveMemoryCache = CalcolaChiaveMemoryCache;
function InizializzaLogbaseIn(req, nomeMetodo) {
const params = req.params;
const body = req.body;
const data = new Date(Date.now());
const header = JSON.parse(JSON.stringify(req.headers));
const local = req.socket.localAddress + " : " + req.socket.localPort;
const remote = req.socket.remoteAddress + " : " + req.socket.remotePort;
const url = req.originalUrl;
/* const tmp = "Arrivato in : " + nomeMetodo + "\n"
+ "Data : " + new Date(Date.now()) + "\n"
+ "url : " + req.originalUrl + "\n"
+ "query : " + JSON.stringify(req.query) + "\n"
+ "body : " + JSON.stringify(req.body) + "\n"
+ "header : " + JSON.stringify(req.headers) + "\n"
+ "soket : " + "\n"
+ "local : " + req.socket.localAddress + " : " + req.socket.localPort + "\n"
+ "remote : " + req.socket.remoteAddress + " : " + req.socket.remotePort + "\n"; */
const tmp = {
params: params,
body: body,
data: data,
header: header,
local: local,
remote: remote,
url: url,
nomeMetodo: nomeMetodo
};
return tmp;
}
exports.InizializzaLogbaseIn = InizializzaLogbaseIn;
function Rispondi(res, item, id, isFile, key, durationSecondi /* , url: string */) {
res.statusCode = Number.parseInt('' + item.stato);
if (isFile) {
try {
if (isFile.send == undefined) {
res.sendFile(isFile.path);
}
else {
isFile.send(res, item);
}
}
catch (error) {
if (isFile.catch == undefined) {
res.status(598).send({});
}
else {
try {
isFile.catch(res, error);
}
catch (error) {
res.status(598).send({});
}
}
}
}
else {
res.send(item.body);
}
id.fine = new Date().getTime();
id.differenza = id.fine - id.inizio;
if (id && 'res' in id && res) {
id.res = res;
}
let cache = [];
let tmpReq = '';
let tmpRes = '';
try {
tmpReq = //id.req ?? '';
JSON.stringify(id.req, (key, value) => {
if (typeof value === 'object' && value !== null && cache) {
// Duplicate reference found, discard key
if (cache.includes(value))
return;
// Store value in our collection
cache.push(value);
}
return value;
});
cache = undefined;
}
catch (error) {
tmpReq = 'errore : ' + error;
}
try {
tmpRes = //id.res ?? '';
JSON.stringify(id.req, (key, value) => {
if (typeof value === 'object' && value !== null && cache) {
// Duplicate reference found, discard key
if (cache.includes(value))
return;
// Store value in our collection
cache.push(value);
}
return value;
});
cache = undefined;
}
catch (error) {
tmpRes = 'error : ' + error;
}
const stamp = `
<!--################
processo:${main_express_1.MainExpress.portaProcesso}
id:${id.id};
inizio:${new Date(id.inizio).toISOString()};
inizio-time: ${id.inizio}
tempo:${id.differenza};
req:
${tmpReq}
res:
${tmpRes}
################--!>
`;
console.log(stamp);
//fs.writeFileSync('./LogExpress/Processo_' + MainExpress.portaProcesso + '/' + 'log.txt', stamp,);
/* var logger = fs.createWriteStream('./LogExpress/Processo_' + MainExpress.portaProcesso + '/' + 'log.txt', {
flags: 'a'
});
logger.write(stamp); */
try {
fs_1.default.appendFileSync('./LogExpress/Processo_' + main_express_1.MainExpress.portaProcesso + '/' + 'log.txt', stamp);
}
catch (error) {
console.log(error);
}
if (key != undefined) {
const tempo = (durationSecondi !== null && durationSecondi !== void 0 ? durationSecondi : 1);
main_express_1.MainExpress.cache.set(key, { body: item.body, stato: res.statusCode }, tempo /* 1000 */);
}
}
exports.Rispondi = Rispondi;
function ConstruisciErrore(messaggio) {
return {
stato: 500,
body: {
errore: messaggio
}
};
}
exports.ConstruisciErrore = ConstruisciErrore;
function InizializzaLogbaseOut(res, nomeMetodo) {
var _a, _b, _c, _d;
const params = {};
const body = {};
const data = new Date(Date.now());
const header = res.getHeaders();
const local = ((_a = res.socket) === null || _a === void 0 ? void 0 : _a.localAddress) + " : " + ((_b = res.socket) === null || _b === void 0 ? void 0 : _b.localPort);
const remote = ((_c = res.socket) === null || _c === void 0 ? void 0 : _c.remoteAddress) + " : " + ((_d = res.socket) === null || _d === void 0 ? void 0 : _d.remotePort);
const url = '';
const tmp = {
params: params,
body: body,
data: data,
header: header,
local: local,
remote: remote,
url: url,
nomeMetodo: nomeMetodo
};
/* const tmp = "Arrivato in : " + nomeMetodo + "\n"
+ "Data : " + new Date(Date.now()) + "\n"
+ "headersSent : " + req.headersSent + "\n"
+ "json : " + req.json + "\n"
+ "send : " + req.send + "\n"
+ "sendDate : " + req.sendDate + "\n"
+ "statusCode : " + req.statusCode + '\n'
+ "statuMessage : " + req.statusMessage + '\n'
+ "soket : " + "\n"
+ "local : " + t1 + "\n"
+ "remote : " + t2 + "\n"; */
return tmp;
}
exports.InizializzaLogbaseOut = InizializzaLogbaseOut;
//# sourceMappingURL=utility.js.map