diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2021-06-01 13:25:41 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-06-02 16:57:07 +0200 |
commit | 1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1 (patch) | |
tree | 1c3a8e3123da47e264533727f5e479edb5cda316 | |
parent | 76148b27f7501bac061992136852be4303370c8d (diff) | |
download | PeerTube-1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1.tar.gz PeerTube-1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1.tar.zst PeerTube-1cfbdd30d9913bfaa0c7e54f82e5b953646bb0d1.zip |
refactor deprecated body-parser usage
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | server.ts | 37 | ||||
-rw-r--r-- | server/helpers/express-utils.ts | 4 | ||||
-rw-r--r-- | support/doc/api/openapi.yaml | 2 | ||||
-rw-r--r-- | yarn.lock | 2 |
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", |
@@ -7,7 +7,6 @@ if (isTestInstance()) { | |||
7 | } | 7 | } |
8 | 8 | ||
9 | // ----------- Node modules ----------- | 9 | // ----------- Node modules ----------- |
10 | import * as bodyParser from 'body-parser' | ||
11 | import * as express from 'express' | 10 | import * as express from 'express' |
12 | import * as morgan from 'morgan' | 11 | import * as morgan from 'morgan' |
13 | import * as cors from 'cors' | 12 | import * 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 | ||
173 | app.use(apiResponseHelpers) | ||
174 | |||
173 | // For body requests | 175 | // For body requests |
174 | app.use(bodyParser.urlencoded({ extended: false })) | 176 | app.use(express.urlencoded({ extended: false })) |
175 | app.use(bodyParser.json({ | 177 | app.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 |
188 | app.use(advertiseDoNotTrack) | 195 | app.use(advertiseDoNotTrack) |
189 | 196 | ||
190 | // Response helpers used in developement | ||
191 | app.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 |
226 | app.use(function (req, res, next) { | 230 | app.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 | ||
232 | app.use(function (err, req, res, next) { | 234 | // Catch thrown errors |
235 | app.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": { |
@@ -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 | ||
1661 | body-parser@1.19.0, body-parser@^1.12.4: | 1661 | body-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== |