aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2021-06-01 13:25:41 +0200
committerChocobozzz <chocobozzz@cpy.re>2021-06-02 16:57:07 +0200
commit1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1 (patch)
tree1c3a8e3123da47e264533727f5e479edb5cda316
parent76148b27f7501bac061992136852be4303370c8d (diff)
downloadPeerTube-1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1.tar.gz
PeerTube-1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1.tar.zst
PeerTube-1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1.zip
refactor deprecated body-parser usage
-rw-r--r--package.json1
-rw-r--r--server.ts37
-rw-r--r--server/helpers/express-utils.ts4
-rw-r--r--support/doc/api/openapi.yaml2
-rw-r--r--yarn.lock2
5 files changed, 25 insertions, 21 deletions
diff --git a/package.json b/package.json
index a5a47b6c9..22b8be224 100644
--- a/package.json
+++ b/package.json
@@ -80,7 +80,6 @@
80 "bcrypt": "5.0.1", 80 "bcrypt": "5.0.1",
81 "bittorrent-tracker": "^9.0.0", 81 "bittorrent-tracker": "^9.0.0",
82 "bluebird": "^3.5.0", 82 "bluebird": "^3.5.0",
83 "body-parser": "^1.12.4",
84 "bull": "^3.4.2", 83 "bull": "^3.4.2",
85 "bytes": "^3.0.0", 84 "bytes": "^3.0.0",
86 "chokidar": "^3.4.2", 85 "chokidar": "^3.4.2",
diff --git a/server.ts b/server.ts
index 1834256d5..66c9173ca 100644
--- a/server.ts
+++ b/server.ts
@@ -7,7 +7,6 @@ if (isTestInstance()) {
7} 7}
8 8
9// ----------- Node modules ----------- 9// ----------- Node modules -----------
10import * as bodyParser from 'body-parser'
11import * as express from 'express' 10import * as express from 'express'
12import * as morgan from 'morgan' 11import * as morgan from 'morgan'
13import * as cors from 'cors' 12import * as cors from 'cors'
@@ -170,14 +169,22 @@ app.use(morgan('combined', {
170 skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping' 169 skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping'
171})) 170}))
172 171
172// Response helpers used for errors
173app.use(apiResponseHelpers)
174
173// For body requests 175// For body requests
174app.use(bodyParser.urlencoded({ extended: false })) 176app.use(express.urlencoded({ extended: false }))
175app.use(bodyParser.json({ 177app.use(express.json({
176 type: [ 'application/json', 'application/*+json' ], 178 type: [ 'application/json', 'application/*+json' ],
177 limit: '500kb', 179 limit: '500kb',
178 verify: (req: express.Request, _, buf: Buffer) => { 180 verify: (req: express.Request, res: express.Response, buf: Buffer) => {
179 const valid = isHTTPSignatureDigestValid(buf, req) 181 const valid = isHTTPSignatureDigestValid(buf, req)
180 if (valid !== true) throw new Error('Invalid digest') 182 if (valid !== true) {
183 res.fail({
184 status: HttpStatusCode.FORBIDDEN_403,
185 message: 'Invalid digest'
186 })
187 }
181 } 188 }
182})) 189}))
183 190
@@ -187,9 +194,6 @@ app.use(cookieParser())
187// W3C DNT Tracking Status 194// W3C DNT Tracking Status
188app.use(advertiseDoNotTrack) 195app.use(advertiseDoNotTrack)
189 196
190// Response helpers used in developement
191app.use(apiResponseHelpers)
192
193// ----------- Views, routes and static files ----------- 197// ----------- Views, routes and static files -----------
194 198
195// API 199// API
@@ -222,23 +226,22 @@ if (cliOptions.client) app.use('/', clientsRouter)
222 226
223// ----------- Errors ----------- 227// ----------- Errors -----------
224 228
225// Catch 404 and forward to error handler 229// Catch unmatched routes
226app.use(function (req, res, next) { 230app.use((req, res: express.Response) => {
227 const err = new Error('Not Found') 231 res.status(HttpStatusCode.NOT_FOUND_404).end()
228 err['status'] = HttpStatusCode.NOT_FOUND_404
229 next(err)
230}) 232})
231 233
232app.use(function (err, req, res, next) { 234// Catch thrown errors
235app.use((err, req, res: express.Response, next) => {
236 // Format error to be logged
233 let error = 'Unknown error.' 237 let error = 'Unknown error.'
234 if (err) { 238 if (err) {
235 error = err.stack || err.message || err 239 error = err.stack || err.message || err
236 } 240 }
237 241 // Handling Sequelize error traces
238 // Sequelize error
239 const sql = err.parent ? err.parent.sql : undefined 242 const sql = err.parent ? err.parent.sql : undefined
240
241 logger.error('Error in controller.', { err: error, sql }) 243 logger.error('Error in controller.', { err: error, sql })
244
242 return res.fail({ 245 return res.fail({
243 status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500, 246 status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
244 message: err.message, 247 message: err.message,
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts
index e3ff93cdd..bca59a83c 100644
--- a/server/helpers/express-utils.ts
+++ b/server/helpers/express-utils.ts
@@ -131,8 +131,8 @@ const apiResponseHelpers = (req, res: express.Response, next = null) => {
131 res.fail = (options) => { 131 res.fail = (options) => {
132 const { data, status, message, title, type, docs, instance } = { 132 const { data, status, message, title, type, docs, instance } = {
133 data: null, 133 data: null,
134 status: HttpStatusCode.BAD_REQUEST_400, 134 ...options,
135 ...options 135 status: options.status || HttpStatusCode.BAD_REQUEST_400
136 } 136 }
137 137
138 const extension = new ProblemDocumentExtension({ 138 const extension = new ProblemDocumentExtension({
diff --git a/support/doc/api/openapi.yaml b/support/doc/api/openapi.yaml
index 52a834056..9f40d74c6 100644
--- a/support/doc/api/openapi.yaml
+++ b/support/doc/api/openapi.yaml
@@ -46,6 +46,7 @@ info:
46 46
47 { 47 {
48 "detail": "Video not found", 48 "detail": "Video not found",
49 "docs": "https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo",
49 "status": 404, 50 "status": 404,
50 "title": "Not Found", 51 "title": "Not Found",
51 "type": "about:blank" 52 "type": "about:blank"
@@ -67,6 +68,7 @@ info:
67 68
68 { 69 {
69 "detail": "Incorrect request parameters: id", 70 "detail": "Incorrect request parameters: id",
71 "docs": "https://docs.joinpeertube.org/api-rest-reference.html#operation/getVideo",
70 "instance": "/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180", 72 "instance": "/api/v1/videos/9c9de5e8-0a1e-484a-b099-e80766180",
71 "invalid-params": { 73 "invalid-params": {
72 "id": { 74 "id": {
diff --git a/yarn.lock b/yarn.lock
index 4731b61f4..41cba2477 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1658,7 +1658,7 @@ bn.js@^5.1.1:
1658 resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002" 1658 resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002"
1659 integrity sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw== 1659 integrity sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw==
1660 1660
1661body-parser@1.19.0, body-parser@^1.12.4: 1661body-parser@1.19.0:
1662 version "1.19.0" 1662 version "1.19.0"
1663 resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a" 1663 resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
1664 integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw== 1664 integrity sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==