refactor deprecated body-parser usage
This commit is contained in:
parent
76148b27f7
commit
1cfbdd30d9
|
@ -80,7 +80,6 @@
|
||||||
"bcrypt": "5.0.1",
|
"bcrypt": "5.0.1",
|
||||||
"bittorrent-tracker": "^9.0.0",
|
"bittorrent-tracker": "^9.0.0",
|
||||||
"bluebird": "^3.5.0",
|
"bluebird": "^3.5.0",
|
||||||
"body-parser": "^1.12.4",
|
|
||||||
"bull": "^3.4.2",
|
"bull": "^3.4.2",
|
||||||
"bytes": "^3.0.0",
|
"bytes": "^3.0.0",
|
||||||
"chokidar": "^3.4.2",
|
"chokidar": "^3.4.2",
|
||||||
|
|
37
server.ts
37
server.ts
|
@ -7,7 +7,6 @@ if (isTestInstance()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------- Node modules -----------
|
// ----------- Node modules -----------
|
||||||
import * as bodyParser from 'body-parser'
|
|
||||||
import * as express from 'express'
|
import * as express from 'express'
|
||||||
import * as morgan from 'morgan'
|
import * as morgan from 'morgan'
|
||||||
import * as cors from 'cors'
|
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'
|
skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping'
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
// Response helpers used for errors
|
||||||
|
app.use(apiResponseHelpers)
|
||||||
|
|
||||||
// For body requests
|
// For body requests
|
||||||
app.use(bodyParser.urlencoded({ extended: false }))
|
app.use(express.urlencoded({ extended: false }))
|
||||||
app.use(bodyParser.json({
|
app.use(express.json({
|
||||||
type: [ 'application/json', 'application/*+json' ],
|
type: [ 'application/json', 'application/*+json' ],
|
||||||
limit: '500kb',
|
limit: '500kb',
|
||||||
verify: (req: express.Request, _, buf: Buffer) => {
|
verify: (req: express.Request, res: express.Response, buf: Buffer) => {
|
||||||
const valid = isHTTPSignatureDigestValid(buf, req)
|
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
|
// W3C DNT Tracking Status
|
||||||
app.use(advertiseDoNotTrack)
|
app.use(advertiseDoNotTrack)
|
||||||
|
|
||||||
// Response helpers used in developement
|
|
||||||
app.use(apiResponseHelpers)
|
|
||||||
|
|
||||||
// ----------- Views, routes and static files -----------
|
// ----------- Views, routes and static files -----------
|
||||||
|
|
||||||
// API
|
// API
|
||||||
|
@ -222,23 +226,22 @@ if (cliOptions.client) app.use('/', clientsRouter)
|
||||||
|
|
||||||
// ----------- Errors -----------
|
// ----------- Errors -----------
|
||||||
|
|
||||||
// Catch 404 and forward to error handler
|
// Catch unmatched routes
|
||||||
app.use(function (req, res, next) {
|
app.use((req, res: express.Response) => {
|
||||||
const err = new Error('Not Found')
|
res.status(HttpStatusCode.NOT_FOUND_404).end()
|
||||||
err['status'] = HttpStatusCode.NOT_FOUND_404
|
|
||||||
next(err)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
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.'
|
let error = 'Unknown error.'
|
||||||
if (err) {
|
if (err) {
|
||||||
error = err.stack || err.message || err
|
error = err.stack || err.message || err
|
||||||
}
|
}
|
||||||
|
// Handling Sequelize error traces
|
||||||
// Sequelize error
|
|
||||||
const sql = err.parent ? err.parent.sql : undefined
|
const sql = err.parent ? err.parent.sql : undefined
|
||||||
|
|
||||||
logger.error('Error in controller.', { err: error, sql })
|
logger.error('Error in controller.', { err: error, sql })
|
||||||
|
|
||||||
return res.fail({
|
return res.fail({
|
||||||
status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
|
status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
|
||||||
message: err.message,
|
message: err.message,
|
||||||
|
|
|
@ -131,8 +131,8 @@ const apiResponseHelpers = (req, res: express.Response, next = null) => {
|
||||||
res.fail = (options) => {
|
res.fail = (options) => {
|
||||||
const { data, status, message, title, type, docs, instance } = {
|
const { data, status, message, title, type, docs, instance } = {
|
||||||
data: null,
|
data: null,
|
||||||
status: HttpStatusCode.BAD_REQUEST_400,
|
...options,
|
||||||
...options
|
status: options.status || HttpStatusCode.BAD_REQUEST_400
|
||||||
}
|
}
|
||||||
|
|
||||||
const extension = new ProblemDocumentExtension({
|
const extension = new ProblemDocumentExtension({
|
||||||
|
|
|
@ -46,6 +46,7 @@ info:
|
||||||
|
|
||||||
{
|
{
|
||||||
"detail": "Video not found",
|
"detail": "Video not found",
|
||||||
|
"docs": "https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo",
|
||||||
"status": 404,
|
"status": 404,
|
||||||
"title": "Not Found",
|
"title": "Not Found",
|
||||||
"type": "about:blank"
|
"type": "about:blank"
|
||||||
|
@ -67,6 +68,7 @@ info:
|
||||||
|
|
||||||
{
|
{
|
||||||
"detail": "Incorrect request parameters: id",
|
"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",
|
"instance": "/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180",
|
||||||
"invalid-params": {
|
"invalid-params": {
|
||||||
"id": {
|
"id": {
|
||||||
|
|
|
@ -1658,7 +1658,7 @@ bn.js@^5.1.1:
|
||||||
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002"
|
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002"
|
||||||
integrity sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw==
|
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"
|
version "1.19.0"
|
||||||
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
|
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
|
||||||
integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==
|
integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==
|
||||||
|
|
Loading…
Reference in New Issue