mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 17:55:17 -04:00
Refactored middleware -> Typescript
Signed-off-by: Yannick Bungers <git@innay.de> Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
a36c36d86a
commit
6cbd436454
8 changed files with 52 additions and 57 deletions
|
@ -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()
|
||||
}
|
13
lib/web/middleware/checkURIValid.ts
Normal file
13
lib/web/middleware/checkURIValid.ts
Normal 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)
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const config = require('../../config')
|
||||
|
||||
module.exports = function (req, res, next) {
|
||||
res.set({
|
||||
'CodiMD-Version': config.version
|
||||
})
|
||||
return next()
|
||||
}
|
9
lib/web/middleware/codiMDVersion.ts
Normal file
9
lib/web/middleware/codiMDVersion.ts
Normal 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()
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
}
|
16
lib/web/middleware/redirectWithoutTrailingSlashes.ts
Normal file
16
lib/web/middleware/redirectWithoutTrailingSlashes.ts
Normal 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()
|
||||
}
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
}
|
14
lib/web/middleware/tooBusy.ts
Normal file
14
lib/web/middleware/tooBusy.ts
Normal 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()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue