Skip to content

Commit

Permalink
Merge 42de1bf into ee91374
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 22, 2023
2 parents ee91374 + 42de1bf commit c23d501
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 89 deletions.
28 changes: 2 additions & 26 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 @@ -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
24 changes: 24 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
2.x
===

This incorporates all changes after 1.19.1 up to 1.20.2.

* 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
=========================

* `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
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 @@ -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
3 changes: 0 additions & 3 deletions lib/read.js
Original file line number Diff line number Diff line change
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
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 @@ -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
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
11 changes: 5 additions & 6 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.20.2",
"version": "2.0.0-beta.1",
"contributors": [
"Douglas Christopher Wilson <doug@somethingdoug.com>",
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
Expand All @@ -11,14 +11,14 @@
"dependencies": {
"bytes": "3.1.2",
"content-type": "~1.0.5",
"debug": "2.6.9",
"debug": "3.1.0",
"depd": "2.0.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 +44,7 @@
"index.js"
],
"engines": {
"node": ">= 0.8",
"npm": "1.2.8000 || >= 1.4.16"
"node": ">= 0.10"
},
"scripts": {
"lint": "eslint .",
Expand Down
9 changes: 6 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,10 @@ 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.type + '] ' + err.message) : JSON.stringify(req.body))
res.end(err
? ('[' + err.type + '] ' + err.message)
: (JSON.stringify(req.body) || typeof req.body)
)
})
})
}
Loading

0 comments on commit c23d501

Please sign in to comment.