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 0.1.0 to 0.2.0

.travis.yml

70

index.js

@@ -1,5 +0,9 @@

/*!
* koa-body
* Copyright (c) 2014. Daryl Lau (@daryllau), Charlike Mike Reagent (@tunnckoCore)
* MIT LICENSE
/**
* koa-body - index.js
* Copyright(c) 2014
* MIT Licensed
*
* @author Daryl Lau (@dlau)
* @author Charlike Mike Reagent (@tunnckoCore)
* @api private
*/

@@ -13,3 +17,4 @@

var co_body = require('co-body');
var buddy = require('co-body');
var forms = require('formidable');

@@ -23,32 +28,33 @@ /**

/**
* Initialize module middleware with the given `options`:
* - `jsonLimit` limits application/json request body, co-body's option
* - `formLimit` limits application/x-www-form-urlencoded request body, co-body's option
* - `patchNode` Set the body parameter in the **Node** request object `this.req.body`
* - `patchKoa` Set the body parameter in the **Koa** request object `this.request.body`
* - `encoding` request encoding, co-body's option
*
* @param {Object} options
* @return {Function}
* @see https://github.com/dlau/koa-body
* @api public
*/
function requestbody(opts) {
var jsonLimit = (opts && 'jsonLimit' in opts) ? opts.jsonLimit : '1mb',
formLimit = (opts && 'formLimit' in opts) ? opts.formLimit : '56kb',
patchNode = (opts && 'patchNode' in opts) ? opts.patchNode : false,
patchKoa = (opts && 'patchKoa' in opts) ? opts.patchKoa : true,
encoding = (opts && 'encoding' in opts) ? opts.encoding : 'utf-8';
opts = opts || {}
opts.patchNode = (opts && 'patchNode' in opts) ? opts.patchNode : false,
opts.patchKoa = (opts && 'patchKoa' in opts) ? opts.patchKoa : true,
opts.multipart = (opts && 'multipart' in opts) ? opts.multipart : false;
opts.encoding = (opts && 'encoding' in opts) ? opts.encoding : 'utf-8',
opts.jsonLimit = (opts && 'jsonLimit' in opts) ? opts.jsonLimit : '1mb',
opts.formLimit = (opts && 'formLimit' in opts) ? opts.formLimit : '56kb';
opts.formidable = (opts && 'formidable' in opts) ? opts.formidable : {};
return function *(next){
var body;
if(this.is('application/json')){
body = yield co_body.json(this,{encoding: encoding, limit: jsonLimit});
if (this.is('json')) {
body = yield buddy.json(this, {encoding: opts.encoding, limit: opts.jsonLimit});
}
else if(this.is('application/x-www-form-urlencoded')){
body = yield co_body.form(this,{encoding: encoding, limit: formLimit});
else if (this.is('urlencoded')) {
body = yield buddy.form(this, {encoding: opts.encoding, limit: opts.formLimit});
}
if(patchNode){
else if (opts.multipart && this.is('multipart')) {
body = yield formy(this, opts.formidable);
}
if (opts.patchNode) {
this.req.body = body;
}
if(patchKoa){
if (opts.patchKoa) {
this.request.body = body;

@@ -59,1 +65,19 @@ }

};
/**
* Donable formidable
*
* @param {Stream} ctx
* @param {Object} opts
* @return {Object}
* @api private
*/
function formy(ctx, opts) {
return function(done) {
var form = new forms.IncomingForm(opts)
form.parse(ctx.req, function(err, fields, files) {
if (err) return done(err)
done(null, {fields: fields, files: files})
})
}
}
{
"name": "koa-body",
"version": "0.1.0",
"description": "koa middleware that parses request body",
"files": [
"index.js",
"example.js",
"test.js",
"README.md",
"LICENSE",
"Makefile",
".gitignore",
".npmignore"
],
"version": "0.2.0",
"description": "A koa body parser middleware. Support multipart, urlencoded and json request bodies.",
"main": "index.js",
"scripts": {
"start": "make start",
"test": "make test"
"test": "node_modules/.bin/mocha --harmony-generators",
"examples-multer": "node --harmony examples/multer.js",
"examples-koa-router": "node --harmony examples/koa-router.js"
},
"author": {
"name": "Daryl Lau",
"email": "daryl@weak.io",
"url": "https://github.com/dlau"
},
"repository": {
"type": "git",
"url": "https://github.com/dlau/koa-body"
"url": "git://github.com/dlau/koa-body.git"
},
"keywords": [
"koa",
"request",
"middleware",
"bodyparser",
"parse",
"urlencoded",
"multipart",
"json",
"bodyjson",
"parsejson",
"parseform",
"body",
"parser",
"form"
],
"files": [
".gitignore",
".npmignore",
".travis.yml",
"LICENSE",
"Makefile",
"README.md",
"example.js",
"index.js",
"test.js"
],
"dependencies": {
"co-body": "*"
"co-body": "*",
"extend": "1.3.0",
"formidable": "1.0.15"
},
"devDependencies": {
"koa": "*",
"koa-router": "*",
"koa-resource-router": "*",
"multiline": "*",
"mocha": "*",

@@ -46,10 +56,13 @@ "should": "*",

{
"name": "Daryl Lau"
"name": "Daryl Lau",
"email": "daryl@weak.io",
"url": "https://github.com/dlau"
},
{
"name": "Charlike Mike Reagent"
"name": "Charlike Mike Reagent",
"email": "mameto_100@mail.bg",
"url": "https://github.com/tunnckoCore"
}
],
"author": "Daryl Lau (http://weak.io/)",
"license": "MIT"
}
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)
================
KoaJS middleware that patch body (to Koa#req.body or Node#request.body, or bother) and parse body to json/form with co-body.
[koa](https://github.com/koajs/koa) middleware for parsing a request body.
This is a simple wrapper around [co-body](https://github.com/co/co-body). Provides similar functionality to the express's [request body parser](http://expressjs.com/api.html#req.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
[`co-body`](https://github.com/visionmedia/co-body) and [`formidable`](https://felixge/node-formidable).
Doesn't support multipart. [Discuss](https://github.com/dlau/koa-body/issues/1)
## Related module
- [`koa-better-body`](https://github.com/tunnckoCore/koa-better-body)
## Install
>Install with [npm](https://github.com/npm/npm)
## Install
```

@@ -16,54 +17,34 @@ $ npm install koa-body

## Features
- 15 tests
- can handle three type requests
* **multipart/form-data**
* **application/x-www-urlencoded**
* **application/json**
- option for patch to Koa or Node, or either
- file uploads
- body, fields and files limiting
- 2 dependencies only
## Options
Initialize koa-better-body middleware with the given `options`:
- **patchNode** Set the body parameter in the **Node** request object `this.req.body`
*defauls to false*
- **patchKoa** Set the body parameter in the **Koa** request object `this.request.body`
*defaults to true*
- **jsonLimit** limits application/json request body, co-body's option
*defauls to '1mb'*
- **formLimit** limits application/x-www-form-urlencoded request body, co-body's option
*defauls to '56kb'*
- **encoding** request encoding, co-body's option
*defauls to 'utf-8'*
## 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
## Usage
```js
var app = require('koa')()
, betterBody = require('./index');
/**
* By default body is patching to
* Koa's ctx.request
*/
app.use(betterBody({patchNode: false, jsonLimit: '1kb', formLimit: '1kb'}));
var app = require('koa')(),
koaBody = require('koa-body');
app.use(function *(next){
var patchKoa = (this.request.body) ? this.request.body : 'patchKoa=true, by default';
var patchNode = (this.req.body) ? this.req.body : 'patchNode=false, by default';
app.use(koaBody({formidable:{uploadDir: __dirname}}));
app.use(function *(next) {
if (this.request.method == 'POST') {
this.status = 201;
} else if (this.request.method == 'GET') {
this.status = 200
console.log(this.request.body);
// => POST body
this.body = JSON.stringify(this.request.body);
}
this.body = JSON.stringify({koa: patchKoa, node: patchNode});
if (this.request.method == 'PUT') {
this.status = 200;
this.body = 'resource updated successfully';
}
if (this.request.method == 'DELETE') {
this.status = 204;
this.body = 'resource deleted successfully';
}
yield next;
});
var port = process.env.PORT || 3333;
app.listen(port);
console.log('Koa server start listening to port '+port);
app.listen(3131)
console.log('curl -i http://localhost:3131/ -d "name=test"');
```
**For a more comprehensive example, see** `examples/multipart.js`

@@ -84,29 +65,43 @@ ## Usage with [koa-router](https://github.com/alexmingoia/koa-router)

// => POST body
this.body = JSON.stringify(this.request.body);
}
);
app.listen(3131)
console.log('curl -i http://localhost:3131/ -d "name=test"');
```
## Features
- 14 passing (174ms) tests
- co-body options: jsonLimit, formLimit, encoding
- freedom to choose - patch body to
* KoaJS Context `this.request.body`
* NodeJS Context `this.req.body`
- only 1 dependency: `co-body`
## Test, Bench, Example
First run `npm install` before run anything.
```
npm test
npm start
```
## Options
> Options available for `koa-body`. Four custom options, and others are from `raw-body` and `formidable`.
## Credit
- `patchNode` **{Boolean}** Patch request body to Node's `ctx.req`, default `false`
- `patchKoa` **{Boolean}** Patch request body to Koa's `ctx.request`, default `true`
- `jsonLimit` **{String|Integer}** The byte limit of the JSON body, default `1mb`
- `formLimit` **{String|Integer}** The byte limit of the form body, default `56kb`
- `encoding` **{String}** Sets encoding for incoming form fields, default `utf-8`
- `multipart` **{Boolean}** Parse multipart bodies, default `false`
- `formidable` **{Object}** Options to pass to the formidable multipart parser
|[![Daryl Lau](https://avatars2.githubusercontent.com/u/2764274?s=144)](https://github.com/dlau)| [![Charlike Mike Reagent](https://avatars2.githubusercontent.com/u/5038030?s=144)](https://github.com/tunnckoCore)|
|---|---|
|[Daryl Lau](https://github.com/dlau) (creator) | [George Yanev](https://github.com/tunnckoCore) (contrib)|
## Some options for formidable
> 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 `10`
- `maxFieldsSize` **{Integer}** Limits the amount of memory a field (not file) can allocate _in bytes_, default `2mb`
- `uploadDir` **{String}** Sets the directory for placing file uploads in, default `os.tmpDir()`
- `keepExtensions` **{Boolean}** Files written to `uploadDir` will include the extensions of the original files, default `true`
- `hash` **{String}** If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`, default `false`
- `multiples` **{Boolean}** Multiple file uploads or no, default `true`
## LICENSE
The MIT License, 2014 [Daryl Lau](http://weak.io) ([@daryllau](https://twitter.com/tunnckoCore)), [Charlike Mike Reagent](https://github.com/tunnckoCore) ([@tunnckoCore](https://twitter.com/tunnckoCore))
**Note**: You can patch request body to Node or Koa in same time if you want.
## Tests
> As usual - `npm test` **or** if you have [mocha][mocha-url] globally - `mocha --harmony-generators`.
```
$ npm test
```
## License
The MIT License, 2014 [Charlike Mike Reagent](https://github.com/tunnckoCore) ([@tunnckoCore](https://twitter.com/tunnckoCore)) and [Daryl Lau](https://github.com/dlau) ([@daryllau](https://twitter.com/daryllau))

Sorry, the diff of this file is not supported yet

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