Added licence notice to many files, other minor type fixes

This commit is contained in:
mrfry 2022-03-22 11:19:32 +01:00
parent d9175c1fc2
commit 75d93af246
22 changed files with 483 additions and 107 deletions

View file

@ -1,4 +1,24 @@
import type { Response, NextFunction } from 'express'
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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<string>
}
@ -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', {

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
import logger from '../utils/logger'
import type { Response, NextFunction } from 'express'
import type { Request } from '../types/basicTypes'

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
import cookie from 'cookie'
import logger from '../utils/logger'

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
const DbStruct = {
msgs: {
tableStruct: {

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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(

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
import { Response } from 'express'
import logger from '../../../utils/logger'

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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,

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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) {

View file

@ -1,6 +1,27 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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) {

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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) {

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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) {

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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,10 +158,12 @@ function setup(data: SubmoduleData): void {
}
})
app.post('/deleteUserFile', (req: Request, res) => {
app.post(
'/deleteUserFile',
(req: Request<{ dir: string; fname: string }>, res) => {
logger.LogReq(req)
const dir: any = req.body.dir
const fname: any = req.body.fname
const dir: string = req.body.dir
const fname: string = req.body.fname
if (!dir || !fname) {
res.json({
success: false,
@ -169,12 +191,13 @@ function setup(data: SubmoduleData): void {
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

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
import { v4 as uuidv4 } from 'uuid'
import type { Database } from 'better-sqlite3'

View file

@ -1,4 +1,22 @@
export interface Users {}
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
const DBStruct = {
users: {

View file

@ -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 {

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
import express from 'express'
import { SearchResultQuestion } from '../utils/classes'
import type { Database } from 'better-sqlite3'
@ -77,8 +97,8 @@ export interface Request<T = any> 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 {

View file

@ -1,5 +1,6 @@
/* ----------------------------------------------------------------------------
Question Server
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
This program is free software: you can redistribute it and/or modify
@ -437,7 +438,7 @@ export function backupData(questionDbs: Array<QuestionDb>): 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) {

View file

@ -1,4 +1,23 @@
// import os from 'os'
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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,

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
// https://www.sqlitetutorial.net/sqlite-nodejs/
// https://github.com/JoshuaWise/better-sqlite3/blob/HEAD/docs/api.md

View file

@ -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')) {

View file

@ -1,3 +1,23 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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<any> {
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<any> {
})
}
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

View file

@ -1,10 +1,30 @@
/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
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.gnu.org/licenses/>.
------------------------------------------------------------------------- */
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<any> {
export function msgAllWorker(data: TaskObject): Promise<WorkerResult[]> {
logger.DebugLog('MSGING ALL WORKER', 'job', 1)
return new Promise((resolve) => {
const promises: Promise<WorkerResult>[] = []
@ -93,7 +113,7 @@ export function msgAllWorker(data: TaskObject): Promise<any> {
export function doALongTask(
obj: TaskObject,
targetWorkerIndex?: number
): Promise<any> {
): Promise<WorkerResult> {
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<WorkerObj> {
export function initWorkerPool(initData: Array<QuestionDb>): Array<WorkerObj> {
if (workers) {
logger.Log('WORKERS ALREADY EXISTS', logger.GetColor('redbg'))
return null
@ -150,7 +170,6 @@ export function initWorkerPool(initData: any): Array<WorkerObj> {
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(
`