From 61ae90233a94a9ebb6b071cd18535ac3f0e03260 Mon Sep 17 00:00:00 2001 From: Simon Ingeson Date: Thu, 3 Apr 2025 20:13:19 -0400 Subject: [PATCH] refactor: move req.body = undefined to just before read attempt Reasoning: the various parsers should not modify the request object until it is clear it should be applied, setting a property, even to undefined, may affect assumptions elsewhere. For example, `'req' in body` would be truthy even if the rest of the middleware was not applied. Arguably, it can be removed entirely since all tests are still passing without it. But keeping it around in case there are other historical reasons behind it. In either case, the current version is not compatible with, for example, tRPC v11 when using FormData. So without this change, the workaround is to add a condition to avoid applying the middleware at all. Example: ```js const json = express.json(); app.use((req, res, next) => { if (req.url.includes('trpc')) return next(); return json(req, res, next); }); ``` --- lib/types/json.js | 8 ++++---- lib/types/raw.js | 8 ++++---- lib/types/text.js | 8 ++++---- lib/types/urlencoded.js | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/types/json.js b/lib/types/json.js index 078ce710..75948ccc 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -90,10 +90,6 @@ function json (options) { return } - if (!('body' in req)) { - req.body = undefined - } - // skip requests without bodies if (!typeis.hasBody(req)) { debug('skip empty body') @@ -121,6 +117,10 @@ function json (options) { return } + if (!('body' in req)) { + req.body = undefined + } + // read read(req, res, next, parse, debug, { encoding: charset, diff --git a/lib/types/raw.js b/lib/types/raw.js index 3788ff27..f96814e1 100644 --- a/lib/types/raw.js +++ b/lib/types/raw.js @@ -44,10 +44,6 @@ function raw (options) { return } - if (!('body' in req)) { - req.body = undefined - } - // skip requests without bodies if (!typeis.hasBody(req)) { debug('skip empty body') @@ -64,6 +60,10 @@ function raw (options) { return } + if (!('body' in req)) { + req.body = undefined + } + // read read(req, res, next, parse, debug, { encoding: null, diff --git a/lib/types/text.js b/lib/types/text.js index 3e0ab1bb..2920a86c 100644 --- a/lib/types/text.js +++ b/lib/types/text.js @@ -46,10 +46,6 @@ function text (options) { return } - if (!('body' in req)) { - req.body = undefined - } - // skip requests without bodies if (!typeis.hasBody(req)) { debug('skip empty body') @@ -69,6 +65,10 @@ function text (options) { // get charset var charset = getCharset(req) || defaultCharset + if (!('body' in req)) { + req.body = undefined + } + // read read(req, res, next, parse, debug, { encoding: charset, diff --git a/lib/types/urlencoded.js b/lib/types/urlencoded.js index f993425e..2768b2de 100644 --- a/lib/types/urlencoded.js +++ b/lib/types/urlencoded.js @@ -58,10 +58,6 @@ function urlencoded (options) { return } - if (!('body' in req)) { - req.body = undefined - } - // skip requests without bodies if (!typeis.hasBody(req)) { debug('skip empty body') @@ -89,6 +85,10 @@ function urlencoded (options) { return } + if (!('body' in req)) { + req.body = undefined + } + // read read(req, res, next, parse, debug, { encoding: charset,