Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2.0 #66

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 2 additions & 26 deletions .github/workflows/ci.yml
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 @@ -34,11 +33,6 @@ jobs:
- Node.js 19.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 @@ -124,14 +118,6 @@ 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
npm install -g --prefix "$(which node)/../.." npm@1.2.8000
sed -i '1s;^.*$;'"$(printf '#!%q' "$(nvm which npm)")"';' "$(readlink -f "$(which npm)")"
fi
dirname "$(nvm which ${{ matrix.node-version }})" >> "$GITHUB_PATH"

- name: Configure npm
Expand All @@ -142,10 +128,6 @@ jobs:
npm config set shrinkwrap false
fi

- 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 Expand Up @@ -177,19 +159,14 @@ jobs:
- name: Run tests
shell: bash
run: |
if npm -ps ls nyc | grep -q nyc; then
npm run test-ci
cp coverage/lcov.info "coverage/${{ matrix.name }}.lcov"
else
npm test
fi
npm run test-ci
cp coverage/lcov.info "coverage/${{ matrix.name }}.lcov"

- name: Lint code
if: steps.list_env.outputs.eslint != ''
run: npm run lint

- name: Collect code coverage
if: steps.list_env.outputs.nyc != ''
run: |
if [[ -d ./coverage ]]; then
mv ./coverage "./${{ matrix.name }}"
Expand All @@ -199,7 +176,6 @@ jobs:

- name: Upload code coverage
uses: actions/upload-artifact@v3
if: steps.list_env.outputs.nyc != ''
with:
name: coverage
path: ./coverage
Expand Down
26 changes: 26 additions & 0 deletions HISTORY.md
@@ -1,3 +1,29 @@
2.0.0-beta.2 / 2023-02-23
=========================

This incorporates all changes after 1.19.1 up to 1.20.2.

* Remove deprecated `bodyParser()` combination middleware
* deps: debug@3.1.0
- Add `DEBUG_HIDE_DATE` environment variable
- Change timer to per-namespace instead of global
- Change non-TTY date format
- Remove `DEBUG_FD` environment variable support
- Support 256 namespace colors
* deps: iconv-lite@0.5.2
- Add encoding cp720
- Add encoding UTF-32
* deps: raw-body@3.0.0-beta.1

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

* Drop support for Node.js 0.8
* `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.20.2 / 2023-02-21
===================

Expand Down
16 changes: 7 additions & 9 deletions README.md
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 @@ -388,15 +384,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 @@ -416,15 +412,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
32 changes: 3 additions & 29 deletions index.js
Expand Up @@ -6,13 +6,6 @@

'use strict'

/**
* Module dependencies.
* @private
*/

var deprecate = require('depd')('body-parser')

/**
* Cache of loaded parsers.
* @private
Expand All @@ -34,8 +27,7 @@ var parsers = Object.create(null)
* @type {Parsers}
*/

exports = module.exports = deprecate.function(bodyParser,
'bodyParser: use individual json/urlencoded middlewares')
exports = module.exports = bodyParser

/**
* JSON parser.
Expand Down Expand Up @@ -90,26 +82,8 @@ Object.defineProperty(exports, 'urlencoded', {
* @public
*/

function bodyParser (options) {
// use default type for parsers
var opts = Object.create(options || null, {
type: {
configurable: true,
enumerable: true,
value: undefined,
writable: true
}
})

var _urlencoded = exports.urlencoded(opts)
var _json = exports.json(opts)

return function bodyParser (req, res, next) {
_json(req, res, function (err) {
if (err) return next(err)
_urlencoded(req, res, next)
})
}
function bodyParser () {
throw new Error('The bodyParser() generic has been split into individual middleware to use instead.')
}

/**
Expand Down
3 changes: 0 additions & 3 deletions lib/read.js
Expand Up @@ -42,9 +42,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
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 @@ -99,13 +100,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
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
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
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
12 changes: 5 additions & 7 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "body-parser",
"description": "Node.js body parsing middleware",
"version": "1.20.2",
"version": "2.0.0-beta.2",
"contributors": [
"Douglas Christopher Wilson <doug@somethingdoug.com>",
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
Expand All @@ -11,14 +11,13 @@
"dependencies": {
"bytes": "3.1.2",
"content-type": "~1.0.5",
"debug": "2.6.9",
"depd": "2.0.0",
"debug": "3.1.0",
"destroy": "1.2.0",
"http-errors": "2.0.0",
"iconv-lite": "0.4.24",
"iconv-lite": "0.5.2",
"on-finished": "2.4.1",
"qs": "6.11.0",
"raw-body": "2.5.2",
"raw-body": "3.0.0-beta.1",
"type-is": "~1.6.18",
"unpipe": "1.0.0"
},
Expand All @@ -44,8 +43,7 @@
"index.js"
],
"engines": {
"node": ">= 0.8",
"npm": "1.2.8000 || >= 1.4.16"
"node": ">= 0.10"
},
"scripts": {
"lint": "eslint .",
Expand Down