From 75d93af2460d0da793fce50add901172b19b29ea Mon Sep 17 00:00:00 2001 From: mrfry Date: Tue, 22 Mar 2022 11:19:32 +0100 Subject: [PATCH] Added licence notice to many files, other minor type fixes --- src/middlewares/auth.middleware.ts | 28 +++++- src/middlewares/reqlogger.middleware.ts | 20 +++++ src/middlewares/socketAuth.middleware.ts | 20 +++++ src/modules/api/msgsDbStruct.ts | 20 +++++ src/modules/api/submodules/chat.ts | 31 +++++-- src/modules/api/submodules/feedback.ts | 20 +++++ src/modules/api/submodules/forum.ts | 36 ++++++-- src/modules/api/submodules/qminingapi.ts | 30 +++++-- src/modules/api/submodules/quickvote.ts | 27 +++++- src/modules/api/submodules/ranklist.ts | 24 +++++- src/modules/api/submodules/todos.ts | 22 ++++- src/modules/api/submodules/userFiles.ts | 91 ++++++++++++-------- src/modules/api/submodules/userManagement.ts | 20 +++++ src/modules/api/usersDBStruct.ts | 20 ++++- src/modules/qmining/qmining.ts | 2 +- src/types/basicTypes.ts | 26 +++++- src/utils/actions.ts | 5 +- src/utils/classes.ts | 28 ++++-- src/utils/dbtools.ts | 20 +++++ src/utils/logger.ts | 12 --- src/utils/utils.ts | 42 +++++++-- src/utils/workerPool.ts | 46 +++++++--- 22 files changed, 483 insertions(+), 107 deletions(-) diff --git a/src/middlewares/auth.middleware.ts b/src/middlewares/auth.middleware.ts index ffe1137..fa997dc 100644 --- a/src/middlewares/auth.middleware.ts +++ b/src/middlewares/auth.middleware.ts @@ -1,4 +1,24 @@ -import type { Response, NextFunction } from 'express' +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + +import type { Response, NextFunction, RequestHandler } from 'express' import type { Request } from '../types/basicTypes' import type { Database } from 'better-sqlite3' @@ -7,7 +27,7 @@ import utils from '../utils/utils' import dbtools from '../utils/dbtools' interface Options { - userDB: any + userDB: Database jsonResponse: boolean exceptions: Array } @@ -36,7 +56,7 @@ function renderLogin(_req: Request, res: Response, jsonResponse: boolean) { } } -export default function (options: Options): any { +export default function (options: Options): RequestHandler { const { userDB, jsonResponse, @@ -133,7 +153,7 @@ export default function (options: Options): any { } } -function GetUserBySessionID(db: any, sessionID: string) { +function GetUserBySessionID(db: Database, sessionID: string) { logger.DebugLog(`Getting user from db`, 'auth', 2) const session = dbtools.Select(db, 'sessions', { diff --git a/src/middlewares/reqlogger.middleware.ts b/src/middlewares/reqlogger.middleware.ts index f9ebfd0..8c7fc82 100644 --- a/src/middlewares/reqlogger.middleware.ts +++ b/src/middlewares/reqlogger.middleware.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import logger from '../utils/logger' import type { Response, NextFunction } from 'express' import type { Request } from '../types/basicTypes' diff --git a/src/middlewares/socketAuth.middleware.ts b/src/middlewares/socketAuth.middleware.ts index 9b20707..ca36bb1 100644 --- a/src/middlewares/socketAuth.middleware.ts +++ b/src/middlewares/socketAuth.middleware.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import cookie from 'cookie' import logger from '../utils/logger' diff --git a/src/modules/api/msgsDbStruct.ts b/src/modules/api/msgsDbStruct.ts index b32e5fb..bb8f208 100644 --- a/src/modules/api/msgsDbStruct.ts +++ b/src/modules/api/msgsDbStruct.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + const DbStruct = { msgs: { tableStruct: { diff --git a/src/modules/api/submodules/chat.ts b/src/modules/api/submodules/chat.ts index 9b7561c..6e9db9a 100644 --- a/src/modules/api/submodules/chat.ts +++ b/src/modules/api/submodules/chat.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import { Server as socket, Socket } from 'socket.io' import utils from '../../../utils/utils' @@ -179,15 +199,8 @@ function setup(data: SubmoduleData): void { }) app.get('/hasNewMsg', (req: Request, res) => { - let userid: any = req.query.userid - if (!userid || isNaN(userid)) { - res.json({ - success: false, - msg: 'Query "userid" (number) is required!', - }) - return - } - userid = parseInt(userid) + const user: User = req.session.user + const userid: number = user.id const groups = dbtools .runStatement( diff --git a/src/modules/api/submodules/feedback.ts b/src/modules/api/submodules/feedback.ts index 3468acb..525758b 100644 --- a/src/modules/api/submodules/feedback.ts +++ b/src/modules/api/submodules/feedback.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import { Response } from 'express' import logger from '../../../utils/logger' diff --git a/src/modules/api/submodules/forum.ts b/src/modules/api/submodules/forum.ts index 6c68dc0..08d6c0a 100644 --- a/src/modules/api/submodules/forum.ts +++ b/src/modules/api/submodules/forum.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import { v4 as uuidv4 } from 'uuid' import logger from '../../../utils/logger' @@ -175,7 +195,7 @@ function setup(data: SubmoduleData): void { app.get('/forumEntries', (req: Request, res) => { logger.LogReq(req) - const forumName: any = req.query.forumName + const forumName: string = req.query.forumName if (!forumName) { res.json({ success: false, @@ -251,7 +271,7 @@ function setup(data: SubmoduleData): void { forumDir ) const user: User = req.session.user - const admins: any = utils.FileExists(adminUsersFile) + const admins: string[] = utils.FileExists(adminUsersFile) ? utils.ReadJSON(adminUsersFile) : [] @@ -260,7 +280,7 @@ function setup(data: SubmoduleData): void { date: utils.GetDateString(), user: user.id, title: title, - admin: admins.includes(user.id), + admin: admins.includes(user.id.toString()), commentCount: 0, } @@ -290,7 +310,7 @@ function setup(data: SubmoduleData): void { ) => { logger.LogReq(req) const { postKey } = req.body - const forumName: any = req.body.forumName + const forumName = req.body.forumName if (!forumName) { res.json({ success: false, @@ -334,7 +354,7 @@ function setup(data: SubmoduleData): void { ) => { logger.LogReq(req) - const forumName: any = req.body.forumName + const forumName = req.body.forumName if (!forumName) { res.json({ success: false, @@ -347,7 +367,7 @@ function setup(data: SubmoduleData): void { forumDir ) const user: User = req.session.user - const admins: any = utils.FileExists(adminUsersFile) + const admins: string[] = utils.FileExists(adminUsersFile) ? utils.ReadJSON(adminUsersFile) : [] const { type, path, postKey } = req.body @@ -373,7 +393,7 @@ function setup(data: SubmoduleData): void { date: utils.GetDateString(), user: user.id, content: content, - admin: admins.includes(user.id), + admin: admins.includes(user.id.toString()), } if (!postData.comments) { postData.comments = [] @@ -418,7 +438,7 @@ function setup(data: SubmoduleData): void { ) => { logger.LogReq(req) - const forumName: any = req.body.forumName + const forumName = req.body.forumName if (!forumName) { res.json({ success: false, diff --git a/src/modules/api/submodules/qminingapi.ts b/src/modules/api/submodules/qminingapi.ts index c9d3f7f..6d78aa7 100644 --- a/src/modules/api/submodules/qminingapi.ts +++ b/src/modules/api/submodules/qminingapi.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import fs from 'fs' import { Response } from 'express' import { fork, ChildProcess } from 'child_process' @@ -137,7 +157,7 @@ function getDetailedRes(questionDbs: QuestionDb[]) { }) } -function getMotd(version: any, motd: string) { +function getMotd(version: string, motd: string) { if (version) { if (version.startsWith('2.0.')) { if (utils.FileExists(oldMotdFile)) { @@ -485,7 +505,7 @@ function setup(data: SubmoduleData): Submodule { app.get('/allqr.txt', function (req: Request, res: Response) { logger.LogReq(req) - const db: any = req.query.db + const db: string = req.query.db let stringifiedData = '' res.setHeader('content-type', 'text/plain; charset=utf-8') @@ -728,7 +748,7 @@ function setup(data: SubmoduleData): Submodule { utils.WriteFile('[]', registeredScriptsFile) } - const ua: any = req.headers['user-agent'] + const ua: string = req.headers['user-agent'] const registeredScripts: RegisteredUserEntry[] = utils.ReadJSON( registeredScriptsFile ) @@ -739,7 +759,7 @@ function setup(data: SubmoduleData): Submodule { }) if (index === -1) { - const x: any = { + const x: RegisteredUserEntry = { cid: cid, version: version, installSource: installSource, @@ -975,7 +995,7 @@ function setup(data: SubmoduleData): Submodule { // TODO: get status of it cleaning logger.LogReq(req) const user: User = req.session.user - const status: any = req.query.status + const status: string = req.query.status if (status) { if (!questionCleaner) { diff --git a/src/modules/api/submodules/quickvote.ts b/src/modules/api/submodules/quickvote.ts index a7204f9..65485b7 100644 --- a/src/modules/api/submodules/quickvote.ts +++ b/src/modules/api/submodules/quickvote.ts @@ -1,6 +1,27 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import logger from '../../../utils/logger' import utils from '../../../utils/utils' -import { Request, SubmoduleData, User } from '../../../types/basicTypes' +import type { Request, SubmoduleData, User } from '../../../types/basicTypes' +import type { Response } from 'express' const quickVoteResultsDir = 'stats/qvote' const quickVotes = 'stats/qvote/votes.json' @@ -20,9 +41,9 @@ interface QuickVote { function setup(data: SubmoduleData): void { const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data - app.get('/quickvote', (req: Request, res: any) => { + app.get('/quickvote', (req: Request, res: Response) => { const key = req.query.key.toString() - const val: any = req.query.val + const val: string = req.query.val const user: User = req.session.user if (!key || !val) { diff --git a/src/modules/api/submodules/ranklist.ts b/src/modules/api/submodules/ranklist.ts index bd5e417..fa01690 100644 --- a/src/modules/api/submodules/ranklist.ts +++ b/src/modules/api/submodules/ranklist.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import logger from '../../../utils/logger' import utils from '../../../utils/utils' import { Request, SubmoduleData, User } from '../../../types/basicTypes' @@ -40,10 +60,10 @@ function mergeObjSum(a: Subjects, b: Subjects) { function setup(data: SubmoduleData): void { const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data - app.get('/ranklist', (req: Request, res: any) => { + app.get('/ranklist', (req: Request, res) => { logger.LogReq(req) let result: IdStats - const querySince: any = req.query.since + const querySince: string = req.query.since const user: User = req.session.user if (!querySince) { diff --git a/src/modules/api/submodules/todos.ts b/src/modules/api/submodules/todos.ts index f5b7e17..53e5d11 100644 --- a/src/modules/api/submodules/todos.ts +++ b/src/modules/api/submodules/todos.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import { Response } from 'express' import logger from '../../../utils/logger' @@ -59,7 +79,7 @@ function setup(data: SubmoduleData): void { app.get('/voteTodo', (req: Request, res: Response) => { logger.LogReq(req) const userId = req.session.user.id - const id: any = req.query.id + const id: string = req.query.id const todos: Todos = utils.ReadJSON(todosFile) if (!id) { diff --git a/src/modules/api/submodules/userFiles.ts b/src/modules/api/submodules/userFiles.ts index a7f0627..be6c942 100644 --- a/src/modules/api/submodules/userFiles.ts +++ b/src/modules/api/submodules/userFiles.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import fs from 'fs' import logger from '../../../utils/logger' @@ -112,7 +132,7 @@ function setup(data: SubmoduleData): void { utils.CreatePath(userFilesDir, true) } - const subdir: any = req.query.subdir + const subdir: string = req.query.subdir if (subdir) { const result = listDir(publicDir, subdir, userFilesDir) @@ -138,43 +158,46 @@ function setup(data: SubmoduleData): void { } }) - app.post('/deleteUserFile', (req: Request, res) => { - logger.LogReq(req) - const dir: any = req.body.dir - const fname: any = req.body.fname - if (!dir || !fname) { + app.post( + '/deleteUserFile', + (req: Request<{ dir: string; fname: string }>, res) => { + logger.LogReq(req) + const dir: string = req.body.dir + const fname: string = req.body.fname + if (!dir || !fname) { + res.json({ + success: false, + msg: `'dir' or 'fname' is undefined!`, + }) + return + } + const safeDir = dir.replace(/\.+/g, '').replace(/\/+/g, '') + const safeFname = fname.replace(/\.+/g, '.').replace(/\/+/g, '') + const filePath = userFilesDir + '/' + safeDir + '/' + safeFname + + if (!utils.FileExists(filePath)) { + res.json({ + success: false, + msg: `path does not exists!`, + }) + return + } + utils.deleteFile(filePath) + const usersFile = userFilesDir + '/' + safeDir + '/' + dataFileName + const users = utils.ReadJSON(usersFile) + delete users[safeFname] + utils.WriteFile(JSON.stringify(users), usersFile) + res.json({ - success: false, - msg: `'dir' or 'fname' is undefined!`, + success: true, }) - return } - const safeDir = dir.replace(/\.+/g, '').replace(/\/+/g, '') - const safeFname = fname.replace(/\.+/g, '.').replace(/\/+/g, '') - const filePath = userFilesDir + '/' + safeDir + '/' + safeFname + ) - if (!utils.FileExists(filePath)) { - res.json({ - success: false, - msg: `path does not exists!`, - }) - return - } - utils.deleteFile(filePath) - const usersFile = userFilesDir + '/' + safeDir + '/' + dataFileName - const users = utils.ReadJSON(usersFile) - delete users[safeFname] - utils.WriteFile(JSON.stringify(users), usersFile) - - res.json({ - success: true, - }) - }) - - app.post('/newUserDir', (req: Request, res) => { + app.post('/newUserDir', (req: Request<{ name: string }>, res) => { logger.LogReq(req) - const name: any = req.body.name + const name: string = req.body.name if (!name) { res.json({ success: false, @@ -198,7 +221,7 @@ function setup(data: SubmoduleData): void { }) }) - app.post('/uploadUserFile', (req: Request, res) => { + app.post('/uploadUserFile', (req: Request<{ dir: string }>, res) => { logger.LogReq(req) const user: User = req.session.user @@ -241,7 +264,7 @@ function setup(data: SubmoduleData): void { }) }) - app.post('/voteFile', (req: Request, res) => { + app.post('/voteFile', (req: Request<{ path: string; to: string }>, res) => { logger.LogReq(req) const user: User = req.session.user // { path: 'userFiles/test/2021-04-28_10-59.png', to: 'up' } 19 diff --git a/src/modules/api/submodules/userManagement.ts b/src/modules/api/submodules/userManagement.ts index 1aa91f8..0b5aaab 100644 --- a/src/modules/api/submodules/userManagement.ts +++ b/src/modules/api/submodules/userManagement.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import { v4 as uuidv4 } from 'uuid' import type { Database } from 'better-sqlite3' diff --git a/src/modules/api/usersDBStruct.ts b/src/modules/api/usersDBStruct.ts index 6bdbbe6..abcce7c 100644 --- a/src/modules/api/usersDBStruct.ts +++ b/src/modules/api/usersDBStruct.ts @@ -1,4 +1,22 @@ -export interface Users {} +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ const DBStruct = { users: { diff --git a/src/modules/qmining/qmining.ts b/src/modules/qmining/qmining.ts index 23a71f5..9e37c86 100644 --- a/src/modules/qmining/qmining.ts +++ b/src/modules/qmining/qmining.ts @@ -69,7 +69,7 @@ function GetApp(): ModuleType { }) app.use(express.static(nextdir)) const linksFile = 'data/links.json' - let links: any = {} + let links: { [key: string]: string } = {} function loadDonateURL() { try { diff --git a/src/types/basicTypes.ts b/src/types/basicTypes.ts index 25a494b..5854629 100644 --- a/src/types/basicTypes.ts +++ b/src/types/basicTypes.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import express from 'express' import { SearchResultQuestion } from '../utils/classes' import type { Database } from 'better-sqlite3' @@ -77,8 +97,8 @@ export interface Request extends express.Request { body: T cookies: any session: any - busboy: any files: any + query: { [key: string]: string } } export interface SubmoduleData { @@ -112,8 +132,8 @@ export interface RegisteredUserEntry { installSource: string date: string userAgent: string - uid: number - loginDate: string + loginDate?: string + uid?: number } export interface Submodule { diff --git a/src/utils/actions.ts b/src/utils/actions.ts index 8b92c12..9d8042b 100755 --- a/src/utils/actions.ts +++ b/src/utils/actions.ts @@ -1,5 +1,6 @@ /* ---------------------------------------------------------------------------- -Question Server + + Question Server GitLab: This program is free software: you can redistribute it and/or modify @@ -437,7 +438,7 @@ export function backupData(questionDbs: Array): void { logger.Log(`Backing up ${data.name}...`) writeData( data.data, - `${path}${data.name}_${utils.GetDateString(true)}.json` + `${path}${data.name}_${utils.GetDateString(undefined, true)}.json` ) logger.Log('Done') } catch (err) { diff --git a/src/utils/classes.ts b/src/utils/classes.ts index 1014105..33d5c8c 100755 --- a/src/utils/classes.ts +++ b/src/utils/classes.ts @@ -1,4 +1,23 @@ -// import os from 'os' +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import { isMainThread, parentPort, workerData } from 'worker_threads' import logger from './logger' @@ -13,7 +32,7 @@ import { editDb, Edits } from './actions' export interface WorkerResult { msg: string workerIndex: number - result: SearchResultQuestion[] + result?: SearchResultQuestion[] } interface DetailedMatch { @@ -557,7 +576,7 @@ function doSearch( function setNoPossibleAnswersPenalties( possibleAnswers: QuestionData['possibleAnswers'], result: SearchResultQuestion[] -): any { +): SearchResultQuestion[] { if (!Array.isArray(possibleAnswers)) { return result } @@ -605,9 +624,6 @@ interface WorkData { } if (!isMainThread) { - // os.setPriority(10) - // logger.Log(`Worker thread priority set to ${os.getPriority()}`) - const { workerIndex, initData, diff --git a/src/utils/dbtools.ts b/src/utils/dbtools.ts index f106351..b0eb8b9 100644 --- a/src/utils/dbtools.ts +++ b/src/utils/dbtools.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + // https://www.sqlitetutorial.net/sqlite-nodejs/ // https://github.com/JoshuaWise/better-sqlite3/blob/HEAD/docs/api.md diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 8085619..6134c10 100755 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -106,18 +106,6 @@ function LogReq( statusCode?: string | number ): void { try { - const ip: any = - req.headers['cf-connecting-ip'] || req.connection.remoteAddress - // if (!toFile) { - // ip = expandWithSpaces(ip, 39) - // } - const nolog = noLogips.some((noLogip) => { - return ip.includes(noLogip) - }) - if (nolog) { - return - } - let logEntry = '' // logHashed(ip) let dl = DELIM if (req.url.includes('lred')) { diff --git a/src/utils/utils.ts b/src/utils/utils.ts index b20ee83..d9bf83e 100755 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,3 +1,23 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + export default { ReadFile: ReadFile, ReadJSON: ReadJSON, @@ -24,7 +44,7 @@ import { Request } from '../types/basicTypes' interface URLFormatOptions { pathname?: string - query?: any + query?: { [key: string]: string } } function formatUrl(options: URLFormatOptions): string { @@ -42,8 +62,11 @@ function formatUrl(options: URLFormatOptions): string { return path + queryString } -function GetDateString(noTime?: boolean): string { - const date = new Date() +function GetDateString( + referenceDate?: Date | string, + noTime?: boolean +): string { + const date = referenceDate ? new Date(referenceDate) : new Date() if (noTime) { return ( @@ -183,7 +206,14 @@ function deleteFile(fname: string): Boolean { return false } -function uploadFile(req: Request, path: string): Promise { +function uploadFile( + req: Request, + path: string +): Promise<{ + body: Request['body'] + fileName: string + filePath: string +}> { return new Promise((resolve, reject) => { try { if (!req.files) { @@ -235,7 +265,7 @@ function uploadFile(req: Request, path: string): Promise { }) } -function statFile(file: string): any { +function statFile(file: string): fs.Stats { if (FileExists(file)) { return fs.statSync(file) } else { @@ -243,7 +273,7 @@ function statFile(file: string): any { } } -function renameFile(oldPath: string, newPath: string): any { +function renameFile(oldPath: string, newPath: string): string { if (FileExists(oldPath)) { fs.renameSync(oldPath, newPath) return newPath diff --git a/src/utils/workerPool.ts b/src/utils/workerPool.ts index 96edf60..6e12c65 100644 --- a/src/utils/workerPool.ts +++ b/src/utils/workerPool.ts @@ -1,10 +1,30 @@ +/* ---------------------------------------------------------------------------- + + Question Server + GitLab: + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + ------------------------------------------------------------------------- */ + import { Worker } from 'worker_threads' import { v4 as uuidv4 } from 'uuid' import { EventEmitter } from 'events' import os from 'os' import logger from './logger' -import { Result } from './actions' +import { Result, Edits } from './actions' import type { Question, QuestionDb, QuestionData } from '../types/basicTypes' import type { WorkerResult } from './classes' @@ -27,7 +47,7 @@ interface TaskObject { searchTillMatchPercent?: number [key: string]: any } - | { dbIndex: number; edits: any } + | { dbIndex: number; edits: Edits } | QuestionDb | Result } @@ -76,7 +96,7 @@ function handleWorkerError(worker: WorkerObj, err: Error) { } // TODO: accuire all workers here, and handle errors so they can be removed if threads exit -export function msgAllWorker(data: TaskObject): Promise { +export function msgAllWorker(data: TaskObject): Promise { logger.DebugLog('MSGING ALL WORKER', 'job', 1) return new Promise((resolve) => { const promises: Promise[] = [] @@ -93,7 +113,7 @@ export function msgAllWorker(data: TaskObject): Promise { export function doALongTask( obj: TaskObject, targetWorkerIndex?: number -): Promise { +): Promise { if (Object.keys(pendingJobs).length > alertOnPendingCount) { logger.Log( `More than ${alertOnPendingCount} callers waiting for free resource! (${ @@ -120,7 +140,7 @@ export function doALongTask( }) } -export function initWorkerPool(initData: any): Array { +export function initWorkerPool(initData: Array): Array { if (workers) { logger.Log('WORKERS ALREADY EXISTS', logger.GetColor('redbg')) return null @@ -150,7 +170,6 @@ export function initWorkerPool(initData: any): Array { function processJob() { if (Object.keys(pendingJobs).length > 0) { - // FIXME: FIFO OR ANYTHING ELSE (JOB PROCESSING ORDER) const keys = Object.keys(pendingJobs) let jobKey: string, freeWorker: WorkerObj let i = 0 @@ -239,11 +258,18 @@ function doSomething(currWorker: WorkerObj, obj: TaskObject) { }) } -const workerTs = (file: string, wkOpts: any) => { - wkOpts.eval = true - if (!wkOpts.workerData) { - wkOpts.workerData = {} +const workerTs = ( + file: string, + wkOpts: { + workerData: { + workerIndex: number + initData: QuestionDb[] + __filename?: string + } + eval?: boolean } +) => { + wkOpts.eval = true wkOpts.workerData.__filename = file return new Worker( `