koa-body-parser
Advanced tools
Comparing version 0.2.1 to 1.0.0
115
index.js
@@ -1,99 +0,36 @@ | ||
var getRawBody = require('raw-body') | ||
var zlib = require('zlib') | ||
module.exports = function (app, options) { | ||
options = options || {} | ||
/** | ||
* Module dependencies | ||
*/ | ||
var limit = options.limit || '1mb' | ||
var strict = options.strict !== false | ||
var reviver = options.reviver | ||
var querystring = options.querystring || require('querystring') | ||
var parse = require('co-body'); | ||
app.request.urlencoded = function* (lim) { | ||
if (!this.is('application/x-www-form-urlencoded') || this.length === 0) | ||
return | ||
/** | ||
* Parse request body into ctx.request.body | ||
* | ||
* @param {Object} opts | ||
* @return { GeneratorFunction} | ||
* @api public | ||
*/ | ||
var str = yield* this.string(lim) | ||
module.exports = function (opts) { | ||
var opts = opts || {}; | ||
var empty = opts.empty === undefined || opts.empty; | ||
try { | ||
return querystring.parse(str, options) | ||
} catch (err) { | ||
err.status = 400 | ||
throw err | ||
} | ||
} | ||
return function *(next) { | ||
var encoding = 'transfer-encoding' in this.req.headers; | ||
var length = 'content-length' in this.req.headers && | ||
this.req.headers['content-length'] !== 0; | ||
app.request.json = function* (lim) { | ||
if (!this.is('json') || this.length === 0) | ||
return | ||
var str = yield* this.string(lim) | ||
str = str.trim() | ||
if (!str.length) | ||
this.ctx.error(400, 'invalid json, empty body') | ||
if (strict !== false) { | ||
var first = str[0] | ||
if ('{' !== first && '[' !== first) | ||
this.ctx.error(400, 'invalid json') | ||
} | ||
try { | ||
return JSON.parse(str, reviver) | ||
} catch (err) { | ||
err.status = 400 | ||
throw err | ||
} | ||
} | ||
app.request.string = function* (lim) { | ||
var buffer = yield* this.buffer(lim) | ||
return buffer.toString('utf8') | ||
} | ||
app.request.buffer = function* (lim) { | ||
var decoder | ||
switch (this.get('content-encoding') || 'identity') { | ||
case 'gzip': | ||
decoder = zlib.createGunzip() | ||
break | ||
case 'deflate': | ||
decoder = zlib.createInflate() | ||
break | ||
case 'identity': | ||
break | ||
default: | ||
this.ctx.error(415, 'invalid content-encoding') | ||
} | ||
var req = this.req | ||
if (decoder) { | ||
var close = function () { | ||
decoder.close() | ||
cleanup() | ||
if (encoding || length) { | ||
try { | ||
this.request.body = yield parse(this.request); | ||
} catch (err) { | ||
if (err.status !== 415 || !empty) | ||
throw err; | ||
} | ||
var cleanup = function () { | ||
req = decoder = null | ||
req.removeListener('end', cleanup) | ||
req.removeListener('error', cleanup) | ||
req.removeListener('close', cleanup) | ||
} | ||
req.once('close', close) | ||
req.once('error', close) | ||
req.once('end', cleanup) | ||
} | ||
return yield getRawBody(decoder | ||
? req.pipe(decoder) | ||
: req, { | ||
limit: lim || limit, | ||
length: !decoder && this.length | ||
}) | ||
yield next; | ||
} | ||
return app | ||
} | ||
}; |
{ | ||
"name": "koa-body-parser", | ||
"description": "Request body parser for koa", | ||
"version": "0.2.1", | ||
"author": { | ||
"name": "Jonathan Ong", | ||
"email": "me@jongleberry.com", | ||
"url": "http://jongleberry.com", | ||
"twitter": "https://twitter.com/jongleberry" | ||
"version": "1.0.0", | ||
"description": "Parse request body into ctx.request.body", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"repository": "https://github.com/thomseddon/koa-body-parser", | ||
"keywords": [ | ||
"koa" | ||
], | ||
"author": "Thom Seddon", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/koajs/body-parser.git" | ||
}, | ||
"bugs": { | ||
"mail": "me@jongleberry.com", | ||
"url": "https://github.com/koajs/body-parser/issues" | ||
}, | ||
"dependencies": { | ||
"raw-body": "~1.0.1" | ||
"co-body": "0.0.1" | ||
}, | ||
"peerDependencies": { | ||
"koa": "*" | ||
}, | ||
"devDependencies": { | ||
"koa": "koajs/koa", | ||
"mocha": "*", | ||
"should": "*", | ||
"supertest": "*" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"engines": { | ||
"node": "> 0.11.4" | ||
"mocha": "~1.17.1", | ||
"should": "~3.1.2", | ||
"supertest": "~0.9.0", | ||
"koa": "~0.3.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
8
107
1
5401
2
39
1
2
+ Addedco-body@0.0.1
+ Addedbytes@1.0.0(transitive)
+ Addedco-body@0.0.1(transitive)
+ Addedqs@0.6.6(transitive)
+ Addedraw-body@1.1.7(transitive)
+ Addedstring_decoder@0.10.31(transitive)
- Removedraw-body@~1.0.1
- Removedaccepts@1.3.8(transitive)
- Removedbytes@0.2.1(transitive)
- Removedcache-content-type@1.0.1(transitive)
- Removedcall-bind-apply-helpers@1.0.1(transitive)
- Removedcall-bound@1.0.3(transitive)
- Removedco@4.6.0(transitive)
- Removedcontent-disposition@0.5.4(transitive)
- Removedcontent-type@1.0.5(transitive)
- Removedcookies@0.9.1(transitive)
- Removeddebug@4.4.0(transitive)
- Removeddeep-equal@1.0.1(transitive)
- Removeddelegates@1.0.0(transitive)
- Removeddepd@1.1.22.0.0(transitive)
- Removeddestroy@1.2.0(transitive)
- Removeddunder-proto@1.0.1(transitive)
- Removedee-first@1.1.1(transitive)
- Removedencodeurl@1.0.2(transitive)
- Removedes-define-property@1.0.1(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.0.0(transitive)
- Removedescape-html@1.0.3(transitive)
- Removedfresh@0.5.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.7(transitive)
- Removedget-proto@1.0.1(transitive)
- Removedgopd@1.2.0(transitive)
- Removedhas-symbols@1.1.0(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhttp-assert@1.5.0(transitive)
- Removedhttp-errors@1.8.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-generator-function@1.1.0(transitive)
- Removedis-regex@1.2.1(transitive)
- Removedkeygrip@1.1.0(transitive)
- Removedkoa@2.15.3(transitive)
- Removedkoa-compose@4.2.0(transitive)
- Removedkoa-convert@2.0.0(transitive)
- Removedmath-intrinsics@1.1.0(transitive)
- Removedmedia-typer@0.3.0(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedms@2.1.3(transitive)
- Removednegotiator@0.6.3(transitive)
- Removedon-finished@2.4.1(transitive)
- Removedonly@0.0.2(transitive)
- Removedparseurl@1.3.3(transitive)
- Removedraw-body@1.0.1(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafe-regex-test@1.1.0(transitive)
- Removedsetprototypeof@1.2.0(transitive)
- Removedstatuses@1.5.0(transitive)
- Removedtoidentifier@1.0.1(transitive)
- Removedtsscmp@1.0.6(transitive)
- Removedtype-is@1.6.18(transitive)
- Removedvary@1.1.2(transitive)
- Removedylru@1.4.0(transitive)