Skip to content

Commit

Permalink
Merge 08d95a4 into d0a214b
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson authored Dec 18, 2021
2 parents d0a214b + 08d95a4 commit bf0ecfd
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 21 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.x
===

* `urlencoded` parser now defaults `extended` to `false`
* Use `on-finished` to determine when body read

1.19.1 / 2021-12-10
===================

Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ encoded into the URL-encoded format, allowing for a JSON-like experience
with URL-encoded. For more information, please
[see the qs library](https://www.npmjs.org/package/qs#readme).

Defaults to `true`, but using the default has been deprecated. Please
research into the difference between `qs` and `querystring` and choose the
appropriate setting.
Defaults to `false`.

##### inflate

Expand Down Expand Up @@ -380,7 +378,7 @@ var bodyParser = require('body-parser')
var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.urlencoded())

// parse application/json
app.use(bodyParser.json())
Expand Down Expand Up @@ -408,7 +406,7 @@ var app = express()
var jsonParser = bodyParser.json()

// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var urlencodedParser = bodyParser.urlencoded()

// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
Expand Down
3 changes: 0 additions & 3 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ function read (req, res, next, parse, debug, options) {
var opts = options
var stream

// flag as parsed
req._body = true

// read options
var encoding = opts.encoding !== null
? opts.encoding
Expand Down
3 changes: 2 additions & 1 deletion lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var bytes = require('bytes')
var contentType = require('content-type')
var createError = require('http-errors')
var debug = require('debug')('body-parser:json')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')

Expand Down Expand Up @@ -96,7 +97,7 @@ function json (options) {
}

return function jsonParser (req, res, next) {
if (req._body) {
if (isFinished(req)) {
debug('body already parsed')
next()
return
Expand Down
3 changes: 2 additions & 1 deletion lib/types/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

var bytes = require('bytes')
var debug = require('debug')('body-parser:raw')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')

Expand Down Expand Up @@ -53,7 +54,7 @@ function raw (options) {
}

return function rawParser (req, res, next) {
if (req._body) {
if (isFinished(req)) {
debug('body already parsed')
next()
return
Expand Down
3 changes: 2 additions & 1 deletion lib/types/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
var bytes = require('bytes')
var contentType = require('content-type')
var debug = require('debug')('body-parser:text')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')

Expand Down Expand Up @@ -55,7 +56,7 @@ function text (options) {
}

return function textParser (req, res, next) {
if (req._body) {
if (isFinished(req)) {
debug('body already parsed')
next()
return
Expand Down
11 changes: 3 additions & 8 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var bytes = require('bytes')
var contentType = require('content-type')
var createError = require('http-errors')
var debug = require('debug')('body-parser:urlencoded')
var deprecate = require('depd')('body-parser')
var isFinished = require('on-finished').isFinished
var read = require('../read')
var typeis = require('type-is')

Expand All @@ -43,12 +43,7 @@ var parsers = Object.create(null)
function urlencoded (options) {
var opts = options || {}

// notice because option default will flip in next major
if (opts.extended === undefined) {
deprecate('undefined extended: provide extended option')
}

var extended = opts.extended !== false
var extended = Boolean(opts.extended)
var inflate = opts.inflate !== false
var limit = typeof opts.limit !== 'number'
? bytes.parse(opts.limit || '100kb')
Expand Down Expand Up @@ -77,7 +72,7 @@ function urlencoded (options) {
}

return function urlencodedParser (req, res, next) {
if (req._body) {
if (isFinished(req)) {
debug('body already parsed')
next()
return
Expand Down
4 changes: 2 additions & 2 deletions test/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ describe('bodyParser.urlencoded()', function () {
.expect(200, '{"user":"tobi"}', done)
})

it('should parse extended syntax', function (done) {
it('should not parse extended syntax', function (done) {
request(this.server)
.post('/')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send('user[name][first]=Tobi')
.expect(200, '{"user":{"name":{"first":"Tobi"}}}', done)
.expect(200, '{"user[name][first]":"Tobi"}', done)
})

describe('with extended option', function () {
Expand Down

0 comments on commit bf0ecfd

Please sign in to comment.