added / fixed some types

This commit is contained in:
mrfry 2022-03-14 19:35:42 +01:00
parent 5f12284bb8
commit bc5c293539
41 changed files with 4378 additions and 8304 deletions

View file

@ -1,3 +1,7 @@
import type { Response, NextFunction } from 'express'
import type { Request } from '../types/basicTypes'
import type { Database } from 'better-sqlite3'
import logger from '../utils/logger'
import utils from '../utils/utils'
import dbtools from '../utils/dbtools'
@ -18,8 +22,8 @@ export const testUser = {
createdBy: 1,
}
function renderLogin(req, res, jsonResponse) {
res.status('401') // Unauthorized
function renderLogin(_req: Request, res: Response, jsonResponse: boolean) {
res.status(401) // Unauthorized
if (jsonResponse) {
res.json({
result: 'nouser',
@ -33,9 +37,17 @@ function renderLogin(req, res, jsonResponse) {
}
export default function (options: Options): any {
const { userDB, jsonResponse, exceptions } = options
const {
userDB,
jsonResponse,
exceptions,
}: {
userDB: Database
jsonResponse: boolean
exceptions: string[]
} = options
return function (req, res, next) {
return function (req: Request, res: Response, next: NextFunction) {
const sessionID = req.cookies.sessionID
const isException = exceptions.some((exc) => {
return req.url.split('?')[0] === exc
@ -44,7 +56,7 @@ export default function (options: Options): any {
if (process.env.NS_NOUSER) {
req.session = {
user: testUser,
sessionID: sessionID || 111111111111111111,
sessionID: sessionID || 11111111111,
isException: false,
}
next()

View file

@ -1,4 +1,6 @@
import logger from '../utils/logger'
import type { Response, NextFunction } from 'express'
import type { Request } from '../types/basicTypes'
interface Options {
loggableKeywords: Array<string>
@ -13,7 +15,7 @@ export default function (options: Options): any {
const exceptions = options.exceptions || []
const excludeFromStats = options.excludeFromStats || []
return function (req, res, next) {
return function (req: Request, res: Response, next: NextFunction) {
res.on('finish', function () {
// TODO: test this
const isException = exceptions.some((ex) => {
@ -24,7 +26,6 @@ export default function (options: Options): any {
return
}
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
let hostname = 'NOHOST'
if (req.hostname) {
hostname = req.hostname.replace('www.', '').split('.')[0]
@ -59,7 +60,6 @@ export default function (options: Options): any {
if (res.statusCode !== 404 && shouldLogStat) {
logger.LogStat(
req.url,
ip,
hostname,
req.session && req.session.user ? req.session.user.id : 'NOUSER'
)

View file

@ -1,6 +1,8 @@
import cookie from 'cookie'
import logger from '../utils/logger'
import dbtools from '../utils/dbtools'
import cookie from 'cookie'
import { Socket } from '../types/basicTypes'
import { testUser } from './auth.middleware'
@ -11,7 +13,7 @@ interface Options {
export default function SocketAuth(options: Options): any {
const { userDB } = options
return (socket, next) => {
return (socket: Socket, next: (arg0?: any) => void) => {
try {
const cookies = cookie.parse(socket.handshake.headers.cookie || '')
const sessionID = cookies.sessionID