New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

koa-body

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-body - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

index.d.ts

75

index.js

@@ -36,3 +36,3 @@ /**

opts.patchNode = 'patchNode' in opts ? opts.patchNode : false;
opts.patchKoa = 'patchKoa' in opts ? opts.patchKoa : true;
opts.patchKoa = 'patchKoa' in opts ? opts.patchKoa : true;
opts.multipart = 'multipart' in opts ? opts.multipart : false;

@@ -42,4 +42,5 @@ opts.urlencoded = 'urlencoded' in opts ? opts.urlencoded : true;

opts.text = 'text' in opts ? opts.text : true;
opts.encoding = 'encoding' in opts ? opts.encoding : 'utf-8';
opts.encoding = 'encoding' in opts ? opts.encoding : 'utf-8';
opts.jsonLimit = 'jsonLimit' in opts ? opts.jsonLimit : '1mb';
opts.jsonStrict = 'jsonStrict' in opts ? opts.jsonStrict : true;
opts.formLimit = 'formLimit' in opts ? opts.formLimit : '56kb';

@@ -51,23 +52,30 @@ opts.queryString = 'queryString' in opts ? opts.queryString : null;

return function *(next){
return function (ctx, next) {
var bodyPromise;
// so don't parse the body in strict mode
if (!opts.strict || ["GET", "HEAD", "DELETE"].indexOf(this.method.toUpperCase()) === -1) {
if (!opts.strict || ["GET", "HEAD", "DELETE"].indexOf(ctx.method.toUpperCase()) === -1) {
try {
if (opts.json && this.is('json')) {
bodyPromise = buddy.json(this, {encoding: opts.encoding, limit: opts.jsonLimit});
if (opts.json && ctx.is('json')) {
bodyPromise = buddy.json(ctx, {
encoding: opts.encoding,
limit: opts.jsonLimit,
strict: opts.jsonStrict
});
} else if (opts.urlencoded && ctx.is('urlencoded')) {
bodyPromise = buddy.form(ctx, {
encoding: opts.encoding,
limit: opts.formLimit,
queryString: opts.queryString
});
} else if (opts.text && ctx.is('text')) {
bodyPromise = buddy.text(ctx, {
encoding: opts.encoding,
limit: opts.textLimit
});
} else if (opts.multipart && ctx.is('multipart')) {
bodyPromise = formy(ctx, opts.formidable);
}
else if (opts.urlencoded && this.is('urlencoded')) {
bodyPromise = buddy.form(this, {encoding: opts.encoding, limit: opts.formLimit, queryString: opts.queryString});
}
else if (opts.text && this.is('text')) {
bodyPromise = buddy.text(this, {encoding: opts.encoding, limit: opts.textLimit});
}
else if (opts.multipart && this.is('multipart')) {
bodyPromise = formy(this, opts.formidable);
}
} catch(parsingError) {
if (typeof(opts.onError) === 'function') {
opts.onError(parsingError, this);
} catch (parsingError) {
if (typeof opts.onError === 'function') {
opts.onError(parsingError, ctx);
} else {

@@ -80,30 +88,29 @@ throw parsingError;

bodyPromise = bodyPromise || Promise.resolve({});
bodyPromise = bodyPromise.catch((parsingError) => {
return bodyPromise.catch(function(parsingError) {
if (typeof opts.onError === 'function') {
opts.onError(parsingError, this);
opts.onError(parsingError, ctx);
} else {
throw parsingError;
}
return next();
})
.then((body) => {
.then(function(body) {
if (opts.patchNode) {
if (isMultiPart(this, opts)) {
this.req.body = body.fields;
this.req.files = body.files;
if (isMultiPart(ctx, opts)) {
ctx.req.body = body.fields;
ctx.req.files = body.files;
} else {
this.req.body = body;
ctx.req.body = body;
}
}
if (opts.patchKoa) {
if (isMultiPart(this, opts)) {
this.request.body = body.fields;
this.request.files = body.files;
if (isMultiPart(ctx, opts)) {
ctx.request.body = body.fields;
ctx.request.files = body.files;
} else {
this.request.body = body;
ctx.request.body = body;
}
}
});
yield bodyPromise;
yield next;
return next();
})
};

@@ -110,0 +117,0 @@ }

{
"name": "koa-body",
"version": "3.0.0",
"version": "4.0.0",
"description": "A Koa body parser middleware. Supports multipart, urlencoded and JSON request bodies.",
"main": "index.js",
"types": "./index.d.ts",
"scripts": {

@@ -13,3 +14,3 @@ "test": "mocha",

"name": "Daryl Lau",
"email": "daryl@weak.io",
"email": "dlau00@gmail.com",
"url": "https://github.com/dlau"

@@ -39,12 +40,11 @@ },

"co-body": "^5.1.1",
"formidable": "^1.0.17"
"formidable": "^1.1.1"
},
"devDependencies": {
"koa": "^1.6.0",
"koa-resource-router": "^0.4.0",
"koa-router": "^5.4.2",
"lodash": "^3.3.1",
"mocha": "^5.2.0",
"should": "^13.2.1",
"supertest": "2.0.0"
"@types/koa": "^2.0.39",
"koa": "^2.0.0",
"koa-router": "^7.0.1",
"mocha": "5.2.0",
"should": "13.2.1",
"supertest": "3.1.0"
},

@@ -54,3 +54,3 @@ "contributors": [

"name": "Daryl Lau",
"email": "daryl@weak.io",
"email": "dlau00@gmail.com",
"url": "https://github.com/dlau"

@@ -57,0 +57,0 @@ },

@@ -1,10 +0,7 @@

koa-body [![Build Status](https://travis-ci.org/dlau/koa-body.png)](https://travis-ci.org/dlau/koa-body) [![Dependencies Status](https://david-dm.org/dlau/koa-body/status.svg)](https://david-dm.org/dlau/koa-body)
koa-body [![Build Status](https://travis-ci.org/dlau/koa-body.svg?branch=koa2)](https://travis-ci.org/dlau/koa-body) [![Dependencies Status](https://david-dm.org/dlau/koa-body/status.svg)](https://david-dm.org/dlau/koa-body)
================
> A full-feature [`koa`](https://github.com/koajs/koa) body parser middleware. Support `multipart`, `urlencoded` and `json` request bodies. Provides same functionality as Express's bodyParser - [`multer`](https://github.com/expressjs/multer). And all that is wrapped only around
> A full-featured [`koa`](https://github.com/koajs/koa) body parser middleware. Support `multipart`, `urlencoded` and `json` request bodies. Provides same functionality as Express's bodyParser - [`multer`](https://github.com/expressjs/multer). And all that is wrapped only around
[`co-body`](https://github.com/visionmedia/co-body) and [`formidable`](https://github.com/felixge/node-formidable).
## Related module
- [`koa-better-body`](https://github.com/tunnckoCore/koa-better-body)
## Install

@@ -28,3 +25,2 @@ >Install with [npm](https://github.com/npm/npm)

## Features
- 15 tests
- can handle three type requests

@@ -37,24 +33,36 @@ * **multipart/form-data**

- body, fields and files limiting
- 2 dependencies only
## Hello world
```sh
npm install koa
npm install koa-body
nvm install v8.11.2 # Note - Koa requires node v7.6.0+ for async/await support
```
index.js:
```js
const Koa = require('koa');
const koaBody = require('koa-body');
## Usage like [multer](https://github.com/expressjs/multer)
> It's very simple, because you can access the fields and files in the `ctx.request.body` or `ctx.req.body` JSON object
const app = new Koa();
```js
var app = require('koa')(),
koaBody = require('koa-body');
app.use(koaBody());
app.use(ctx => {
ctx.body = `Request Body: ${JSON.stringify(ctx.request.body)}`;
});
app.use(koaBody({formidable:{uploadDir: __dirname}}));
app.use(function *(next) {
if (this.request.method == 'POST') {
console.log(this.request.body);
// => POST body
this.body = JSON.stringify(this.request.body);
}
yield next;
});
app.listen(3131)
console.log('curl -i http://localhost:3131/ -d "name=test"');
app.listen(3000);
```
```sh
$ node index.js
$ curl -i http://localhost:3000/users -d "name=test"
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 29
Date: Wed, 03 May 2017 02:09:44 GMT
Connection: keep-alive
Request Body: {"name":"test"}%
```
**For a more comprehensive example, see** `examples/multipart.js`

@@ -66,11 +74,12 @@

```js
var app = require('koa')(),
router = require('koa-router')(),
koaBody = require('koa-body')();
const Koa = require('koa');
const app = new Koa();
const router = require('koa-router')();
const koaBody = require('koa-body');
router.post('/users', koaBody,
function *(next) {
console.log(this.request.body);
router.post('/users', koaBody(),
(ctx) => {
console.log(ctx.request.body);
// => POST body
this.body = JSON.stringify(this.request.body);
ctx.body = JSON.stringify(ctx.request.body);
}

@@ -81,4 +90,4 @@ );

app.listen(3131);
console.log('curl -i http://localhost:3131/users -d "name=test"');
app.listen(3000);
console.log('curl -i http://localhost:3000/users -d "name=test"');
```

@@ -100,2 +109,3 @@

- `json` **{Boolean}** Parse json bodies, default `true`
- `jsonStrict` **{Boolean}** Toggles co-body strict mode; if set to true - only parses arrays or objects, default `true`
- `formidable` **{Object}** Options to pass to the formidable multipart parser

@@ -112,5 +122,4 @@ - `onError` **{Function}** Custom error handle, if throw an error, you can customize the response - onError(error, context), default will throw

> See [node-formidable](https://github.com/felixge/node-formidable) for a full list of options
- `bytesExpected` **{Integer}** The expected number of bytes in this form, default `null`
- `maxFields` **{Integer}** Limits the number of fields that the querystring parser will decode, default `1000`
- `maxFieldsSize` **{Integer}** Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default `2mb (2 * 2 * 1024)`
- `maxFieldsSize` **{Integer}** Limits the amount of memory all fields together (except files) can allocate in bytes. If this value is exceeded, an 'error' event is emitted, default `2mb (2 * 1024 * 1024)`
- `uploadDir` **{String}** Sets the directory for placing file uploads in, default `os.tmpDir()`

@@ -127,4 +136,2 @@ - `keepExtensions` **{Boolean}** Files written to `uploadDir` will include the extensions of the original files, default `false`

## Tests
> As usual - `npm test` **or** if you have [mocha](https://mochajs.org) globally - `mocha --harmony-generators`.
```

@@ -131,0 +138,0 @@ $ npm test

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc