Socket
Socket
Sign inDemoInstall

co-body

Package Overview
Dependencies
Maintainers
2
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

co-body - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

5

History.md
2.0.0 / 2015-05-04
==================
* json parser support strict mode
1.2.0 / 2015-04-29

@@ -3,0 +8,0 @@ ==================

25

lib/json.js

@@ -8,2 +8,6 @@

// Allowed whitespace is defined in RFC 7159
// http://www.rfc-editor.org/rfc/rfc7159.txt
var strictJSONReg = /^[\x20\x09\x0a\x0d]*(\[|\{)/;
/**

@@ -30,18 +34,29 @@ * Return a a thunk which parses json requests.

opts.limit = opts.limit || '1mb';
var strict = opts.strict !== false;
return function(done){
if (len === 0) return done(null, null);
raw(req, opts, function(err, str){
if (err) return done(err);
var body;
try {
done(null, JSON.parse(str));
body = parse(str);
} catch (err) {
err.status = 400;
err.body = str;
done(err);
return done(err);
}
done(null, body);
});
};
function parse(str){
if (!strict) return str ? JSON.parse(str) : str;
// strict mode always return object
if (!str) return {};
// strict JSON test
if (!strictJSONReg.test(str)) {
throw new Error('invalid JSON, only supports object and array');
}
return JSON.parse(str);
}
};

12

package.json
{
"name": "co-body",
"version": "1.2.0",
"version": "2.0.0",
"repository": "cojs/co-body",

@@ -16,11 +16,11 @@ "description": "request body parsing for co",

"dependencies": {
"qs": "~2.3.3",
"raw-body": "~1.3.3"
"qs": "~2.4.1",
"raw-body": "~1.3.4"
},
"devDependencies": {
"istanbul-harmony": "^0.3.1",
"koa": "*",
"istanbul-harmony": "~0.3.12",
"koa": "~0.20.0",
"mocha": "*",
"should": "*",
"supertest": "*"
"supertest": "~0.8.2"
},

@@ -27,0 +27,0 @@ "license": "MIT",

@@ -31,6 +31,7 @@

Available via [raw-body](https://github.com/stream-utils/raw-body/blob/master/index.js):
- `limit` number or string representing the request size limit (1mb for json and 56kb for form-urlencoded)
- `strict` when set to `true`, JSON parser will only accept arrays and objects; when `false` will accept anything `JSON.parse` accepts. Defaults to `true`. (also `strict` mode will always return object).
more options available via [raw-body](https://github.com/stream-utils/raw-body#getrawbodystream-options-callback):
## Example

@@ -37,0 +38,0 @@

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