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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12068
6
138
114
+ Addedkoa@^2.0.0
+ Addedaccepts@1.3.8(transitive)
+ Addedcache-content-type@1.0.1(transitive)
+ Addedco@4.6.0(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addedcookies@0.9.1(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddeep-equal@1.0.1(transitive)
+ Addeddelegates@1.0.0(transitive)
+ Addeddepd@1.1.2(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedencodeurl@1.0.2(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedfresh@0.5.2(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhttp-assert@1.5.0(transitive)
+ Addedhttp-errors@1.8.1(transitive)
+ Addedis-generator-function@1.0.10(transitive)
+ Addedkeygrip@1.1.0(transitive)
+ Addedkoa@2.15.3(transitive)
+ Addedkoa-compose@4.2.0(transitive)
+ Addedkoa-convert@2.0.0(transitive)
+ Addedms@2.1.3(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedonly@0.0.2(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstatuses@1.5.0(transitive)
+ Addedtsscmp@1.0.6(transitive)
+ Addedvary@1.1.2(transitive)
+ Addedylru@1.4.0(transitive)
- Removedextend@1.3.0
- Removedextend@1.3.0(transitive)