Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-body-parser

Package Overview
Dependencies
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-body-parser - npm Package Compare versions

Comparing version 0.2.1 to 1.0.0

LICENSE

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

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