From 5a551b1de6f5e2200b8a838207b56ea1198bdb96 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 22 Jun 2023 14:51:07 +0800 Subject: [PATCH 1/2] fix: compatible extra semicolon on content-type header (#153) --- .github/workflows/nodejs.yml | 15 +++++++++++++++ .travis.yml | 11 ----------- index.js | 27 ++++++++++++--------------- package.json | 7 ++++--- test/middleware.spec.js | 32 +++++++++++++++----------------- 5 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 .github/workflows/nodejs.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..e22e1d1 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,15 @@ +name: CI + +on: + push: + branches: [ master ] + + pull_request: + branches: [ master ] + +jobs: + Job: + name: Node.js + uses: node-modules/github-actions/.github/workflows/node-test.yml@master + with: + version: '12, 14, 16, 18, 20' diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c994bb1..0000000 --- a/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: node_js -node_js: - - 8 - - 10 - - 12 - - stable -script: - - npm run test-ci -after_script: - - npm install coveralls - - cat ./coverage/lcov.info | coveralls diff --git a/index.js b/index.js index e7a70e6..0d01853 100644 --- a/index.js +++ b/index.js @@ -1,21 +1,8 @@ -/** ! - * koa-body-parser - index.js - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * dead_horse (http://deadhorse.me) - * fengmk2 (http://fengmk2.com) - */ - 'use strict'; -/** - * Module dependencies. - */ - const parse = require('co-body'); const copy = require('copy-to'); +const typeis = require('type-is'); /** * @param [Object] opts @@ -94,7 +81,8 @@ module.exports = function(opts) { async function parseBody(ctx) { if ( enableJson && - ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes)) + ((detectJSON && detectJSON(ctx)) || + isTypes(ctx.request.get('content-type'), jsonTypes)) ) { return await parse.json(ctx, jsonOpts); // eslint-disable-line no-return-await } @@ -137,3 +125,12 @@ function extendType(original, extend) { function checkEnable(types, type) { return types.includes(type); } + +function isTypes(contentTypeValue, types) { + if (typeof contentTypeValue === 'string') { + // trim extra semicolon + contentTypeValue = contentTypeValue.replace(/;$/, ''); + } + + return typeis.is(contentTypeValue, types); +} diff --git a/package.json b/package.json index 8a589bd..c2e17c4 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "lint:fix": "xo --fix", "test": "mocha --require should test/*.spec.js --exit", "coverage": "nyc npm run test --reporter=lcov", - "test-ci": "npm run lint && npm run coverage" + "ci": "npm run lint && npm run coverage" }, "repository": { "type": "git", @@ -34,7 +34,7 @@ "eslint-config-xo-lass": "^1.0.3", "husky": "^4.2.5", "koa": "^2", - "mocha": "^7.1.1", + "mocha": "^10.2.0", "nyc": "^15.0.1", "should": "^13.2.3", "supertest": "^4.0.2", @@ -42,7 +42,8 @@ }, "dependencies": { "co-body": "^6.0.0", - "copy-to": "^2.0.1" + "copy-to": "^2.0.1", + "type-is": "^1.6.18" }, "xo": { "prettier": true, diff --git a/test/middleware.spec.js b/test/middleware.spec.js index 236c119..70b0bc9 100644 --- a/test/middleware.spec.js +++ b/test/middleware.spec.js @@ -1,20 +1,3 @@ -/** ! - * koa-body-parser - test/middleware.test.js - * - * Copyright(c) 2014 - * MIT Licensed - * - * Authors: - * dead_horse (http://deadhorse.me) - * fengmk2 (http://fengmk2.com) - */ - -'use strict'; - -/** - * Module dependencies. - */ - const path = require('path'); const request = require('supertest'); const Koa = require('koa'); @@ -61,6 +44,21 @@ describe('test/middleware.test.js', function() { .expect({foo: 'bar'}, done); }); + it('should parse json body with `content-type: application/json;charset=utf-8;` headers ok', async () => { + app.use(bodyParser()); + + app.use(async ctx => { + ctx.request.body.should.eql({foo: 'bar'}); + ctx.request.rawBody.should.equal('{"foo": "bar"}'); + ctx.body = ctx.request.body; + }); + await request(app.listen()) + .post('/') + .set('Content-type', 'application/json;charset=utf-8;') + .send('{"foo": "bar"}') + .expect({foo: 'bar'}); + }); + it('should parse json patch', function(done) { const app = App(); app.use(async ctx => { From 5678a79e64d7833e0dd7734c9e9de40126e14d98 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 22 Jun 2023 14:52:24 +0800 Subject: [PATCH 2/2] Release 4.4.1 --- History.md | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index d541590..696d48a 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,10 @@ +4.4.1 / 2023-06-22 +================== + +**fixes** + * [[`5a551b1`](http://github.com/koajs/bodyparser/commit/5a551b1de6f5e2200b8a838207b56ea1198bdb96)] - fix: compatible extra semicolon on content-type header (#153) (fengmk2 <>) + 4.4.0 / 2023-03-15 ================== diff --git a/package.json b/package.json index c2e17c4..3cf98cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "koa-bodyparser", - "version": "4.4.0", + "version": "4.4.1", "description": "a body parser for Koa", "main": "index.js", "scripts": {