diff --git a/.gitignore b/.gitignore index 0fa6951f..f15b98e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.nyc_output/ coverage/ node_modules/ npm-debug.log diff --git a/.travis.yml b/.travis.yml index fc5cf851..1a38cdc2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,12 +10,12 @@ node_js: - "5.12" - "6.17" - "7.10" - - "8.16" + - "8.17" - "9.11" - - "10.16" + - "10.19" - "11.15" - - "12.7" -sudo: false + - "12.16" + - "13.11" cache: directories: - node_modules @@ -66,15 +66,19 @@ before_install: # Configure eslint for linting if node_version_lt '8.0'; then npm_remove_module_re '^eslint(-|$)' fi - - | - # Configure istanbul for coverage - if node_version_lt '0.10'; then npm_remove_module_re '^istanbul$' - fi - | # Configure mocha for testing if node_version_lt '0.10'; then npm_use_module 'mocha' '2.5.3' elif node_version_lt '4.0' ; then npm_use_module 'mocha' '3.5.3' elif node_version_lt '6.0' ; then npm_use_module 'mocha' '5.2.0' + elif node_version_lt '8.0' ; then npm_use_module 'mocha' '6.2.2' + fi + - | + # Configure nyc for coverage + if node_version_lt '0.10'; then npm_remove_module_re '^nyc$' + elif node_version_lt '4.0' ; then npm_use_module 'nyc' '10.3.2' + elif node_version_lt '6.0' ; then npm_use_module 'nyc' '11.9.0' + elif node_version_lt '8.0' ; then npm_use_module 'nyc' '14.1.1' fi - | # Configure supertest for http calls @@ -95,8 +99,8 @@ before_scrpt: npm -s ls ||: script: - | - # Run test script, depending on istanbul install - if npm_module_installed 'istanbul'; then npm run-script test-travis + # Run test script, depending on nyc install + if npm_module_installed 'nyc'; then npm run-script test-travis else npm test fi - | @@ -105,8 +109,8 @@ script: fi after_script: - | - # Upload coverage to coveralls if exists - if [[ -e ./coverage/lcov.info ]]; then + # Upload coverage to coveralls + if [[ -d .nyc_output ]]; then npm install --save-dev coveralls@2 - coveralls < ./coverage/lcov.info + nyc report --reporter=text-lcov | coveralls fi diff --git a/HISTORY.md b/HISTORY.md index 1798afaa..63288c30 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,8 +3,10 @@ unreleased * deps: http-errors@1.7.3 - deps: inherits@2.0.4 + * deps: qs@6.9.3 * deps: raw-body@2.4.1 - deps: http-errors@1.7.3 + * deps: safe-buffer@5.2.0 * deps: type-is@~1.6.18 1.19.0 / 2019-04-25 diff --git a/README.md b/README.md index abf8debe..283ae54e 100644 --- a/README.md +++ b/README.md @@ -279,14 +279,15 @@ encoding of the request. The parsing can be aborted by throwing an error. ## Errors -The middlewares provided by this module create errors depending on the error -condition during parsing. The errors will typically have a `status`/`statusCode` -property that contains the suggested HTTP response code, an `expose` property -to determine if the `message` property should be displayed to the client, a -`type` property to determine the type of error without matching against the -`message`, and a `body` property containing the read body, if available. - -The following are the common errors emitted, though any error can come through +The middlewares provided by this module create errors using the +[`http-errors` module](https://www.npmjs.com/package/http-errors). The errors +will typically have a `status`/`statusCode` property that contains the suggested +HTTP response code, an `expose` property to determine if the `message` property +should be displayed to the client, a `type` property to determine the type of +error without matching against the `message`, and a `body` property containing +the read body, if available. + +The following are the common errors created, though any error can come through for various reasons. ### content encoding unsupported @@ -297,6 +298,20 @@ contained an encoding but the "inflation" option was set to `false`. The `'encoding.unsupported'`, and the `charset` property will be set to the encoding that is unsupported. +### entity parse failed + +This error will occur when the request contained an entity that could not be +parsed by the middleware. The `status` property is set to `400`, the `type` +property is set to `'entity.parse.failed'`, and the `body` property is set to +the entity value that failed parsing. + +### entity verify failed + +This error will occur when the request contained an entity that could not be +failed verification by the defined `verify` option. The `status` property is +set to `403`, the `type` property is set to `'entity.verify.failed'`, and the +`body` property is set to the entity value that failed verification. + ### request aborted This error will occur when the request is aborted by the client before reading diff --git a/package.json b/package.json index 4dca8bc5..1ea75517 100644 --- a/package.json +++ b/package.json @@ -16,22 +16,22 @@ "http-errors": "1.7.3", "iconv-lite": "0.4.24", "on-finished": "~2.3.0", - "qs": "6.9.1", + "qs": "6.9.3", "raw-body": "2.4.1", "type-is": "~1.6.18" }, "devDependencies": { - "eslint": "6.0.0", - "eslint-config-standard": "12.0.0", - "eslint-plugin-import": "2.18.0", - "eslint-plugin-markdown": "1.0.0", - "eslint-plugin-node": "9.1.0", + "eslint": "6.8.0", + "eslint-config-standard": "14.1.1", + "eslint-plugin-import": "2.20.1", + "eslint-plugin-markdown": "1.0.2", + "eslint-plugin-node": "9.2.0", "eslint-plugin-promise": "4.2.1", - "eslint-plugin-standard": "4.0.0", - "istanbul": "0.4.5", + "eslint-plugin-standard": "4.0.1", "methods": "1.1.2", - "mocha": "6.1.4", - "safe-buffer": "5.1.2", + "mocha": "7.1.1", + "nyc": "15.0.0", + "safe-buffer": "5.2.0", "supertest": "4.0.2" }, "files": [ @@ -46,7 +46,7 @@ "scripts": { "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --require test/support/env --reporter spec --check-leaks --bail test/", - "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/", - "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/" + "test-cov": "nyc --reporter=html --reporter=text npm test", + "test-travis": "nyc --reporter=text npm test" } } diff --git a/test/body-parser.js b/test/body-parser.js index efdd7f98..e32cf94d 100644 --- a/test/body-parser.js +++ b/test/body-parser.js @@ -114,9 +114,11 @@ describe('bodyParser()', function () { describe('with verify option', function () { it('should apply to json', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x20) throw new Error('no leading space') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x20) throw new Error('no leading space') + } + }) request(server) .post('/') @@ -126,9 +128,11 @@ describe('bodyParser()', function () { }) it('should apply to urlencoded', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x20) throw new Error('no leading space') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x20) throw new Error('no leading space') + } + }) request(server) .post('/') diff --git a/test/json.js b/test/json.js index 3fd42d90..ae745712 100644 --- a/test/json.js +++ b/test/json.js @@ -390,9 +390,11 @@ describe('bodyParser.json()', function () { }) it('should error from verify', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x5b) throw new Error('no arrays') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x5b) throw new Error('no arrays') + } + }) request(server) .post('/') @@ -402,9 +404,11 @@ describe('bodyParser.json()', function () { }) it('should error with type = "entity.verify.failed"', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x5b) throw new Error('no arrays') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x5b) throw new Error('no arrays') + } + }) request(server) .post('/') @@ -415,12 +419,14 @@ describe('bodyParser.json()', function () { }) it('should allow custom codes', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] !== 0x5b) return - var err = new Error('no arrays') - err.status = 400 - throw err - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] !== 0x5b) return + var err = new Error('no arrays') + err.status = 400 + throw err + } + }) request(server) .post('/') @@ -430,12 +436,14 @@ describe('bodyParser.json()', function () { }) it('should allow custom type', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] !== 0x5b) return - var err = new Error('no arrays') - err.type = 'foo.bar' - throw err - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] !== 0x5b) return + var err = new Error('no arrays') + err.type = 'foo.bar' + throw err + } + }) request(server) .post('/') @@ -446,9 +454,11 @@ describe('bodyParser.json()', function () { }) it('should include original body on error object', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x5b) throw new Error('no arrays') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x5b) throw new Error('no arrays') + } + }) request(server) .post('/') @@ -459,9 +469,11 @@ describe('bodyParser.json()', function () { }) it('should allow pass-through', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x5b) throw new Error('no arrays') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x5b) throw new Error('no arrays') + } + }) request(server) .post('/') @@ -471,9 +483,11 @@ describe('bodyParser.json()', function () { }) it('should work with different charsets', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x5b) throw new Error('no arrays') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x5b) throw new Error('no arrays') + } + }) var test = request(server).post('/') test.set('Content-Type', 'application/json; charset=utf-16') @@ -482,9 +496,11 @@ describe('bodyParser.json()', function () { }) it('should 415 on unknown charset prior to verify', function (done) { - var server = createServer({ verify: function (req, res, buf) { - throw new Error('unexpected verify call') - } }) + var server = createServer({ + verify: function (req, res, buf) { + throw new Error('unexpected verify call') + } + }) var test = request(server).post('/') test.set('Content-Type', 'application/json; charset=x-bogus') diff --git a/test/raw.js b/test/raw.js index 50b59d0d..36dc1ef6 100644 --- a/test/raw.js +++ b/test/raw.js @@ -248,9 +248,11 @@ describe('bodyParser.raw()', function () { }) it('should error from verify', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x00) throw new Error('no leading null') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x00) throw new Error('no leading null') + } + }) var test = request(server).post('/') test.set('Content-Type', 'application/octet-stream') @@ -259,12 +261,14 @@ describe('bodyParser.raw()', function () { }) it('should allow custom codes', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] !== 0x00) return - var err = new Error('no leading null') - err.status = 400 - throw err - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] !== 0x00) return + var err = new Error('no leading null') + err.status = 400 + throw err + } + }) var test = request(server).post('/') test.set('Content-Type', 'application/octet-stream') @@ -273,9 +277,11 @@ describe('bodyParser.raw()', function () { }) it('should allow pass-through', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x00) throw new Error('no leading null') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x00) throw new Error('no leading null') + } + }) var test = request(server).post('/') test.set('Content-Type', 'application/octet-stream') diff --git a/test/text.js b/test/text.js index 4f902680..01781d7f 100644 --- a/test/text.js +++ b/test/text.js @@ -271,9 +271,11 @@ describe('bodyParser.text()', function () { }) it('should error from verify', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x20) throw new Error('no leading space') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x20) throw new Error('no leading space') + } + }) request(server) .post('/') @@ -283,12 +285,14 @@ describe('bodyParser.text()', function () { }) it('should allow custom codes', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] !== 0x20) return - var err = new Error('no leading space') - err.status = 400 - throw err - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] !== 0x20) return + var err = new Error('no leading space') + err.status = 400 + throw err + } + }) request(server) .post('/') @@ -298,9 +302,11 @@ describe('bodyParser.text()', function () { }) it('should allow pass-through', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x20) throw new Error('no leading space') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x20) throw new Error('no leading space') + } + }) request(server) .post('/') @@ -310,9 +316,11 @@ describe('bodyParser.text()', function () { }) it('should 415 on unknown charset prior to verify', function (done) { - var server = createServer({ verify: function (req, res, buf) { - throw new Error('unexpected verify call') - } }) + var server = createServer({ + verify: function (req, res, buf) { + throw new Error('unexpected verify call') + } + }) var test = request(server).post('/') test.set('Content-Type', 'text/plain; charset=x-bogus') diff --git a/test/urlencoded.js b/test/urlencoded.js index 3d0c16af..e867c0f4 100644 --- a/test/urlencoded.js +++ b/test/urlencoded.js @@ -519,9 +519,11 @@ describe('bodyParser.urlencoded()', function () { }) it('should error from verify', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x20) throw new Error('no leading space') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x20) throw new Error('no leading space') + } + }) request(server) .post('/') @@ -531,9 +533,11 @@ describe('bodyParser.urlencoded()', function () { }) it('should error with type = "entity.verify.failed"', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x20) throw new Error('no leading space') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x20) throw new Error('no leading space') + } + }) request(server) .post('/') @@ -544,12 +548,14 @@ describe('bodyParser.urlencoded()', function () { }) it('should allow custom codes', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] !== 0x20) return - var err = new Error('no leading space') - err.status = 400 - throw err - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] !== 0x20) return + var err = new Error('no leading space') + err.status = 400 + throw err + } + }) request(server) .post('/') @@ -559,12 +565,14 @@ describe('bodyParser.urlencoded()', function () { }) it('should allow custom type', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] !== 0x20) return - var err = new Error('no leading space') - err.type = 'foo.bar' - throw err - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] !== 0x20) return + var err = new Error('no leading space') + err.type = 'foo.bar' + throw err + } + }) request(server) .post('/') @@ -575,9 +583,11 @@ describe('bodyParser.urlencoded()', function () { }) it('should allow pass-through', function (done) { - var server = createServer({ verify: function (req, res, buf) { - if (buf[0] === 0x5b) throw new Error('no arrays') - } }) + var server = createServer({ + verify: function (req, res, buf) { + if (buf[0] === 0x5b) throw new Error('no arrays') + } + }) request(server) .post('/') @@ -587,9 +597,11 @@ describe('bodyParser.urlencoded()', function () { }) it('should 415 on unknown charset prior to verify', function (done) { - var server = createServer({ verify: function (req, res, buf) { - throw new Error('unexpected verify call') - } }) + var server = createServer({ + verify: function (req, res, buf) { + throw new Error('unexpected verify call') + } + }) var test = request(server).post('/') test.set('Content-Type', 'application/x-www-form-urlencoded; charset=x-bogus')