Skip to content

Commit

Permalink
Merge 3f5e93e into 424dadd
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 19, 2022
2 parents 424dadd + 3f5e93e commit 25f9349
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 63 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ jobs:
strategy:
matrix:
name:
- Node.js 0.8
- Node.js 0.10
- Node.js 0.12
- io.js 1.x
Expand All @@ -32,11 +31,6 @@ jobs:
- Node.js 17.x

include:
- name: Node.js 0.8
node-version: "0.8"
npm-i: mocha@2.5.3 supertest@1.1.0
npm-rm: nyc

- name: Node.js 0.10
node-version: "0.10"
npm-i: mocha@3.5.3 nyc@10.3.2 supertest@2.0.0
Expand Down Expand Up @@ -114,21 +108,11 @@ jobs:
shell: bash -eo pipefail -l {0}
run: |
nvm install --default ${{ matrix.node-version }}
if [[ "${{ matrix.node-version }}" == 0.* && "$(cut -d. -f2 <<< "${{ matrix.node-version }}")" -lt 10 ]]; then
nvm install --alias=npm 0.10
nvm use ${{ matrix.node-version }}
sed -i '1s;^.*$;'"$(printf '#!%q' "$(nvm which npm)")"';' "$(readlink -f "$(which npm)")"
npm config set strict-ssl false
fi
dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH"
- name: Configure npm
run: npm config set shrinkwrap false

- name: Remove npm module(s) ${{ matrix.npm-rm }}
run: npm rm --silent --save-dev ${{ matrix.npm-rm }}
if: matrix.npm-rm != ''

- name: Install npm module(s) ${{ matrix.npm-i }}
run: npm install --save-dev ${{ matrix.npm-i }}
if: matrix.npm-i != ''
Expand Down
13 changes: 13 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
2.x
===

This incorporates all changes after 1.19.1 up to 1.19.2.

2.0.0-beta.1 / 2021-12-17
=========================

* `req.body` is no longer always initialized to `{}`
- it is left `undefined` unless a body is parsed
* `urlencoded` parser now defaults `extended` to `false`
* Use `on-finished` to determine when body read

1.19.2 / 2022-02-15
===================

Expand Down
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ var bodyParser = require('body-parser')

The `bodyParser` object exposes various factories to create middlewares. All
middlewares will populate the `req.body` property with the parsed body when
the `Content-Type` request header matches the `type` option, or an empty
object (`{}`) if there was no body to parse, the `Content-Type` was not matched,
or an error occurred.
the `Content-Type` request header matches the `type` option.

The various errors returned by this module are described in the
[errors section](#errors).
Expand Down Expand Up @@ -237,9 +235,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,15 +376,15 @@ 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())

app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.body, null, 2))
res.end(String(JSON.stringify(req.body, null, 2)))
})
```

Expand All @@ -408,15 +404,17 @@ 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) {
if (!req.body || !req.body.username) res.sendStatus(400)
res.send('welcome, ' + req.body.username)
})

// POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {
if (!req.body) res.sendStatus(400)
// create user in req.body
})
```
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
7 changes: 5 additions & 2 deletions 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,13 +97,15 @@ function json (options) {
}

return function jsonParser (req, res, next) {
if (req._body) {
if (isFinished(req)) {
debug('body already parsed')
next()
return
}

req.body = req.body || {}
if (!('body' in req)) {
req.body = undefined
}

// skip requests without bodies
if (!typeis.hasBody(req)) {
Expand Down
7 changes: 5 additions & 2 deletions 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,13 +54,15 @@ function raw (options) {
}

return function rawParser (req, res, next) {
if (req._body) {
if (isFinished(req)) {
debug('body already parsed')
next()
return
}

req.body = req.body || {}
if (!('body' in req)) {
req.body = undefined
}

// skip requests without bodies
if (!typeis.hasBody(req)) {
Expand Down
7 changes: 5 additions & 2 deletions 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,13 +56,15 @@ function text (options) {
}

return function textParser (req, res, next) {
if (req._body) {
if (isFinished(req)) {
debug('body already parsed')
next()
return
}

req.body = req.body || {}
if (!('body' in req)) {
req.body = undefined
}

// skip requests without bodies
if (!typeis.hasBody(req)) {
Expand Down
15 changes: 6 additions & 9 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,13 +72,15 @@ function urlencoded (options) {
}

return function urlencodedParser (req, res, next) {
if (req._body) {
if (isFinished(req)) {
debug('body already parsed')
next()
return
}

req.body = req.body || {}
if (!('body' in req)) {
req.body = undefined
}

// skip requests without bodies
if (!typeis.hasBody(req)) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "body-parser",
"description": "Node.js body parsing middleware",
"version": "1.19.2",
"version": "2.0.0-beta.1",
"contributors": [
"Douglas Christopher Wilson <doug@somethingdoug.com>",
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
Expand Down Expand Up @@ -41,7 +41,7 @@
"index.js"
],
"engines": {
"node": ">= 0.8"
"node": ">= 0.10"
},
"scripts": {
"lint": "eslint .",
Expand Down
6 changes: 3 additions & 3 deletions test/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ describe('bodyParser()', function () {
this.server = createServer()
})

it('should default to {}', function (done) {
it('should default req.body to undefined', function (done) {
request(this.server)
.post('/')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})

it('should parse JSON', function (done) {
Expand Down Expand Up @@ -149,7 +149,7 @@ function createServer (opts) {
return http.createServer(function (req, res) {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(req.body))
res.end(err ? err.message : (JSON.stringify(req.body) || typeof req.body))
})
})
}
8 changes: 4 additions & 4 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('bodyParser.json()', function () {
.get('/')
.set('Content-Type', 'application/json')
.unset('Transfer-Encoding')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})

it('should 400 when invalid content-length', function (done) {
Expand Down Expand Up @@ -315,7 +315,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('{"user":"tobi"}')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -347,7 +347,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/x-json')
.send('{"user":"tobi"}')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -662,7 +662,7 @@ function createServer (opts) {
res.end(err[req.headers['x-error-property'] || 'message'])
} else {
res.statusCode = 200
res.end(JSON.stringify(req.body))
res.end(JSON.stringify(req.body) || typeof req.body)
}
})
})
Expand Down
6 changes: 3 additions & 3 deletions test/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('bodyParser.raw()', function () {
var test = request(this.server).post('/')
test.set('Content-Type', 'application/octet-stream')
test.write(Buffer.from('000102', 'hex'))
test.expect(200, '{}', done)
test.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -206,7 +206,7 @@ describe('bodyParser.raw()', function () {
var test = request(this.server).post('/')
test.set('Content-Type', 'application/x-foo')
test.write(Buffer.from('000102', 'hex'))
test.expect(200, '{}', done)
test.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -384,7 +384,7 @@ function createServer (opts) {
return
}

res.end(JSON.stringify(req.body))
res.end(JSON.stringify(req.body) || typeof req.body)
})
})
}
6 changes: 3 additions & 3 deletions test/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('bodyParser.text()', function () {
.post('/')
.set('Content-Type', 'text/plain')
.send('user is tobi')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -228,7 +228,7 @@ describe('bodyParser.text()', function () {
.post('/')
.set('Content-Type', 'text/xml')
.send('<user>tobi</user>')
.expect(200, '{}', done)
.expect(200, 'undefined', done)
})
})

Expand Down Expand Up @@ -442,7 +442,7 @@ function createServer (opts) {
return http.createServer(function (req, res) {
_bodyParser(req, res, function (err) {
res.statusCode = err ? (err.status || 500) : 200
res.end(err ? err.message : JSON.stringify(req.body))
res.end(err ? err.message : (JSON.stringify(req.body) || typeof req.body))
})
})
}
Loading

0 comments on commit 25f9349

Please sign in to comment.