Lotsa typescript bullshit

This commit is contained in:
mrfry 2020-11-24 10:47:04 +01:00
parent b7ac485689
commit b927988017
65 changed files with 801 additions and 8447 deletions
src/utils

View file

@ -21,7 +21,7 @@
const hr =
'---------------------------------------------------------------------------------'
module.exports = {
export default {
GetDateString: GetDateString,
Log: Log,
DebugLog: DebugLog,
@ -37,6 +37,7 @@ module.exports = {
const DELIM = C('green') + '|' + C()
// import express from 'express'
import utils from '../utils/utils'
const vlogDir = './stats/vlogs/'
const logDir = './stats/logs/'
@ -65,17 +66,17 @@ let writes = 0
let noLogips = []
function setNewLogfileName() {
function setNewLogfileName(): void {
logFileName = utils.GetDateString(true)
}
function GetDateString() {
function GetDateString(): string {
const date = new Date()
const dateString = utils.GetDateString()
return GetRandomColor(date.getHours().toString()) + dateString + C()
}
function DebugLog(msg, name, lvl) {
function DebugLog(msg: string | object, name: string, lvl: number): void {
if (lvl <= debugLevel) {
if (msg === 'hr') {
msg = hr
@ -94,18 +95,26 @@ function DebugLog(msg, name, lvl) {
}
}
function Log(msg, color) {
function Log(msg: string | object, color?: string): void {
let log = msg
if (typeof s !== 'object') {
if (typeof msg !== 'object') {
const delimiter = DELIM + C(color)
log = C(color) + GetDateString() + delimiter + msg + C()
}
console.log(log)
utils.AppendToFile(log, logDir + logFileName)
utils.AppendToFile(
typeof log === 'string' ? log : JSON.stringify(log),
logDir + logFileName
)
}
function LogReq(req, toFile, sc) {
// FIXME: express.Request type, but with .cookies and .session
function LogReq(
req: any /*express.Request*/,
toFile?: boolean,
sc?: string
): void {
try {
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
@ -159,7 +168,7 @@ function LogReq(req, toFile, sc) {
}
} catch (err) {
console.log(err)
Log('Error at logging lol', GetColor('redbg'), true)
Log('Error at logging lol', GetColor('redbg'))
}
}
@ -182,7 +191,7 @@ function setNoLogReadInterval() {
parseNoLogFile(utils.ReadFile(nologFile))
}
function Load() {
function Load(): void {
Log('Loading logger...')
try {
uvData = JSON.parse(utils.ReadFile(uStatsFile))
@ -217,7 +226,12 @@ function Load() {
setNoLogReadInterval()
}
function LogStat(url, ip, hostname, userId) {
function LogStat(
url: string,
ip: string,
hostname: string,
userId: number
): void {
const nolog = noLogips.some((noLogips) => {
return ip.includes(noLogips)
})
@ -327,11 +341,11 @@ function Save() {
}
}
function logHashed(msg) {
function logHashed(msg: string): string {
return GetRandomColor(msg.toString()) + msg + C()
}
function GetRandomColor(msg) {
function GetRandomColor(msg): string {
if (!msg) {
return 'red'
}
@ -342,11 +356,11 @@ function GetRandomColor(msg) {
return C(colors[res % colors.length])
}
function GetColor(color) {
function GetColor(color: string): string {
return color
}
function C(color?: string) {
function C(color?: string): string {
if (color !== undefined) {
color = color.toLowerCase()
}