Comparing version 1.2.0 to 2.0.0
2.0.0 / 2015-05-04 | ||
================== | ||
* json parser support strict mode | ||
1.2.0 / 2015-04-29 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -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); | ||
} | ||
}; |
{ | ||
"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 @@ |
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
7309
162
77
+ Addedqs@2.4.2(transitive)
- Removedqs@2.3.3(transitive)
Updatedqs@~2.4.1
Updatedraw-body@~1.3.4