Skip to content

Commit 61ae902

Browse files
committed
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); }); ```
1 parent 0aa4e11 commit 61ae902

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

lib/types/json.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ function json (options) {
9090
return
9191
}
9292

93-
if (!('body' in req)) {
94-
req.body = undefined
95-
}
96-
9793
// skip requests without bodies
9894
if (!typeis.hasBody(req)) {
9995
debug('skip empty body')
@@ -121,6 +117,10 @@ function json (options) {
121117
return
122118
}
123119

120+
if (!('body' in req)) {
121+
req.body = undefined
122+
}
123+
124124
// read
125125
read(req, res, next, parse, debug, {
126126
encoding: charset,

lib/types/raw.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ function raw (options) {
4444
return
4545
}
4646

47-
if (!('body' in req)) {
48-
req.body = undefined
49-
}
50-
5147
// skip requests without bodies
5248
if (!typeis.hasBody(req)) {
5349
debug('skip empty body')
@@ -64,6 +60,10 @@ function raw (options) {
6460
return
6561
}
6662

63+
if (!('body' in req)) {
64+
req.body = undefined
65+
}
66+
6767
// read
6868
read(req, res, next, parse, debug, {
6969
encoding: null,

lib/types/text.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ function text (options) {
4646
return
4747
}
4848

49-
if (!('body' in req)) {
50-
req.body = undefined
51-
}
52-
5349
// skip requests without bodies
5450
if (!typeis.hasBody(req)) {
5551
debug('skip empty body')
@@ -69,6 +65,10 @@ function text (options) {
6965
// get charset
7066
var charset = getCharset(req) || defaultCharset
7167

68+
if (!('body' in req)) {
69+
req.body = undefined
70+
}
71+
7272
// read
7373
read(req, res, next, parse, debug, {
7474
encoding: charset,

lib/types/urlencoded.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ function urlencoded (options) {
5858
return
5959
}
6060

61-
if (!('body' in req)) {
62-
req.body = undefined
63-
}
64-
6561
// skip requests without bodies
6662
if (!typeis.hasBody(req)) {
6763
debug('skip empty body')
@@ -89,6 +85,10 @@ function urlencoded (options) {
8985
return
9086
}
9187

88+
if (!('body' in req)) {
89+
req.body = undefined
90+
}
91+
9292
// read
9393
read(req, res, next, parse, debug, {
9494
encoding: charset,

0 commit comments

Comments
 (0)