Refactored middleware -> Typescript

Signed-off-by: Yannick Bungers <git@innay.de>
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
Yannick Bungers 2020-04-12 21:20:03 +02:00 committed by David Mehren
parent a36c36d86a
commit 6cbd436454
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
8 changed files with 52 additions and 57 deletions

View file

@ -1,14 +0,0 @@
'use strict'
const logger = require('../../logger')
const errors = require('../../errors')
module.exports = function (req, res, next) {
try {
decodeURIComponent(req.path)
} catch (err) {
logger.error(err)
return errors.errorBadRequest(res)
}
next()
}

View file

@ -0,0 +1,13 @@
import { logger } from '../../logger'
import { errors } from '../../errors'
import { NextFunction, Request, Response } from 'express'
export default function checkURI (req: Request, res: Response, next: NextFunction) {
try {
decodeURIComponent(req.path)
next()
} catch (err) {
logger.error(err)
errors.errorBadRequest(res)
}
}

View file

@ -1,10 +0,0 @@
'use strict'
const config = require('../../config')
module.exports = function (req, res, next) {
res.set({
'CodiMD-Version': config.version
})
return next()
}

View file

@ -0,0 +1,9 @@
import { config } from '../../config'
import { NextFunction, Request, Response } from 'express'
export default function version (req: Request, res: Response, next: NextFunction) {
res.set({
'CodiMD-Version': config.version
})
return next()
}

View file

@ -1,17 +0,0 @@
'use strict'
const config = require('../../config')
module.exports = function (req, res, next) {
if (req.method === 'GET' && req.path.substr(-1) === '/' && req.path.length > 1) {
const queryString = req.url.slice(req.path.length)
const urlPath = req.path.slice(0, -1)
let serverURL = config.serverURL
if (config.urlPath) {
serverURL = serverURL.slice(0, -(config.urlPath.length + 1))
}
res.redirect(301, serverURL + urlPath + queryString)
} else {
next()
}
}

View file

@ -0,0 +1,16 @@
import { NextFunction, Request, Response } from 'express'
import { config } from '../../config'
export default function (req: Request, res: Response, next: NextFunction): void {
if (req.method === 'GET' && req.path.substr(-1) === '/' && req.path.length > 1) {
const queryString: string = req.url.slice(req.path.length)
const urlPath: string = req.path.slice(0, -1)
let serverURL: string = config.serverURL
if (config.urlPath) {
serverURL = serverURL.slice(0, -(config.urlPath.length + 1))
}
res.redirect(301, serverURL + urlPath + queryString)
} else {
next()
}
}

View file

@ -1,16 +0,0 @@
'use strict'
const toobusy = require('toobusy-js')
const errors = require('../../errors')
const config = require('../../config')
toobusy.maxLag(config.tooBusyLag)
module.exports = function (req, res, next) {
if (toobusy()) {
errors.errorServiceUnavailable(res)
} else {
next()
}
}

View file

@ -0,0 +1,14 @@
import toobusy from 'toobusy-js'
import { errors } from '../../errors'
import { config } from '../../config'
import { NextFunction, Request, Response } from 'express'
toobusy.maxLag(config.tooBusyLag)
export default function tooBusy (req: Request, res: Response, next: NextFunction): void {
if (toobusy()) {
errors.errorServiceUnavailable(res)
} else {
next()
}
}