Huge News!Announcing our $40M Series B led by Abstract Ventures.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 1.6.0 to 2.0.0

127

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,3 +42,3 @@ 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';

@@ -50,23 +50,28 @@ opts.formLimit = 'formLimit' in opts ? opts.formLimit : '56kb';

return function *(next){
var body = {};
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')) {
body = yield 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
});
} else if (opts.urlencoded && ctx.is('urlencoded')) {
bodyPromise = buddy.form(ctx, {
encoding: opts.encoding,
limit: opts.formLimit
});
} 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')) {
body = yield buddy.form(this, {encoding: opts.encoding, limit: opts.formLimit});
}
else if (opts.text && this.is('text')) {
body = yield buddy.text(this, {encoding: opts.encoding, limit: opts.textLimit});
}
else if (opts.multipart && this.is('multipart')) {
body = yield 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 {

@@ -78,9 +83,20 @@ throw parsingError;

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

@@ -98,40 +114,39 @@ }

function formy(ctx, opts) {
return function(done) {
return new Promise(function (resolve, reject) {
var fields = {};
var files = {};
var form = new forms.IncomingForm(opts)
form
.on('end', function() {
done(null, {fields: fields, files: files});
})
.on('error', function(err) {
done(err);
})
.on('field', function(field, value) {
if (fields[field]) {
if (Array.isArray(fields[field])) {
fields[field].push(value);
} else {
fields[field] = [fields[field], value];
}
var form = new forms.IncomingForm(opts);
form.on('end', function () {
return resolve({
fields: fields,
files: files
});
}).on('error', function (err) {
return reject(err);
}).on('field', function (field, value) {
if (fields[field]) {
if (Array.isArray(fields[field])) {
fields[field].push(value);
} else {
fields[field] = value;
fields[field] = [fields[field], value];
}
})
.on('file', function(field, file) {
if (files[field]) {
if (Array.isArray(files[field])) {
files[field].push(file);
} else {
files[field] = [files[field], file];
}
} else {
fields[field] = value;
}
}).on('file', function (field, file) {
if (files[field]) {
if (Array.isArray(files[field])) {
files[field].push(file);
} else {
files[field] = file;
files[field] = [files[field], file];
}
});
if(opts.onFileBegin) {
} else {
files[field] = file;
}
});
if (opts.onFileBegin) {
form.on('fileBegin', opts.onFileBegin);
}
form.parse(ctx.req);
};
});
}
{
"name": "koa-body",
"version": "1.6.0",
"version": "2.0.0",
"description": "A koa body parser middleware. Support multipart, urlencoded and json request bodies.",

@@ -13,3 +13,3 @@ "main": "index.js",

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

@@ -43,9 +43,7 @@ },

"co-body": "*",
"extend": "1.3.0",
"formidable": "1.0.17"
"formidable": "1.0.17",
"koa": "^2.0.0"
},
"devDependencies": {
"koa": "*",
"koa-resource-router": "*",
"koa-router": "*",
"koa-router": "^7.0.1",
"lodash": "^3.3.1",

@@ -52,0 +50,0 @@ "mocha": "*",

@@ -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

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

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

@@ -27,3 +23,2 @@ * **multipart/form-data**

- body, fields and files limiting
- 2 dependencies only

@@ -30,0 +25,0 @@

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