refactor deprecated body-parser usage

pull/4158/head
Rigel Kent 2021-06-01 13:25:41 +02:00 committed by Chocobozzz
parent 76148b27f7
commit 1cfbdd30d9
5 changed files with 25 additions and 21 deletions

View File

@ -80,7 +80,6 @@
"bcrypt": "5.0.1",
"bittorrent-tracker": "^9.0.0",
"bluebird": "^3.5.0",
"body-parser": "^1.12.4",
"bull": "^3.4.2",
"bytes": "^3.0.0",
"chokidar": "^3.4.2",

View File

@ -7,7 +7,6 @@ if (isTestInstance()) {
}
// ----------- Node modules -----------
import * as bodyParser from 'body-parser'
import * as express from 'express'
import * as morgan from 'morgan'
import * as cors from 'cors'
@ -170,14 +169,22 @@ app.use(morgan('combined', {
skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping'
}))
// Response helpers used for errors
app.use(apiResponseHelpers)
// For body requests
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json({
app.use(express.urlencoded({ extended: false }))
app.use(express.json({
type: [ 'application/json', 'application/*+json' ],
limit: '500kb',
verify: (req: express.Request, _, buf: Buffer) => {
verify: (req: express.Request, res: express.Response, buf: Buffer) => {
const valid = isHTTPSignatureDigestValid(buf, req)
if (valid !== true) throw new Error('Invalid digest')
if (valid !== true) {
res.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'Invalid digest'
})
}
}
}))
@ -187,9 +194,6 @@ app.use(cookieParser())
// W3C DNT Tracking Status
app.use(advertiseDoNotTrack)
// Response helpers used in developement
app.use(apiResponseHelpers)
// ----------- Views, routes and static files -----------
// API
@ -222,23 +226,22 @@ if (cliOptions.client) app.use('/', clientsRouter)
// ----------- Errors -----------
// Catch 404 and forward to error handler
app.use(function (req, res, next) {
const err = new Error('Not Found')
err['status'] = HttpStatusCode.NOT_FOUND_404
next(err)
// Catch unmatched routes
app.use((req, res: express.Response) => {
res.status(HttpStatusCode.NOT_FOUND_404).end()
})
app.use(function (err, req, res, next) {
// Catch thrown errors
app.use((err, req, res: express.Response, next) => {
// Format error to be logged
let error = 'Unknown error.'
if (err) {
error = err.stack || err.message || err
}
// Sequelize error
// Handling Sequelize error traces
const sql = err.parent ? err.parent.sql : undefined
logger.error('Error in controller.', { err: error, sql })
return res.fail({
status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
message: err.message,

View File

@ -131,8 +131,8 @@ const apiResponseHelpers = (req, res: express.Response, next = null) => {
res.fail = (options) => {
const { data, status, message, title, type, docs, instance } = {
data: null,
status: HttpStatusCode.BAD_REQUEST_400,
...options
...options,
status: options.status || HttpStatusCode.BAD_REQUEST_400
}
const extension = new ProblemDocumentExtension({

View File

@ -46,6 +46,7 @@ info:
{
"detail": "Video not found",
"docs": "https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo",
"status": 404,
"title": "Not Found",
"type": "about:blank"
@ -67,6 +68,7 @@ info:
{
"detail": "Incorrect request parameters: id",
"docs": "https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo",
"instance": "/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180",
"invalid-params": {
"id": {

View File

@ -1658,7 +1658,7 @@ bn.js@^5.1.1:
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002"
integrity sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw==
body-parser@1.19.0, body-parser@^1.12.4:
body-parser@1.19.0:
version "1.19.0"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==