Comparing version 0.2.0 to 1.0.0
21
index.js
@@ -17,10 +17,10 @@ var bytes = require('bytes') | ||
var expected = null | ||
if (!isNaN(options.expected)) | ||
expected = parseInt(options.expected, 10) | ||
var length = null | ||
if (!isNaN(options.length)) | ||
length = parseInt(options.length, 10) | ||
if (limit !== null && expected !== null && expected > limit) { | ||
if (limit !== null && length !== null && length > limit) { | ||
var err = new Error('request entity too large') | ||
err.status = 413 | ||
err.expected = expected | ||
err.length = length | ||
err.limit = limit | ||
@@ -62,8 +62,10 @@ stream.resume() // dump stream | ||
function onEnd() { | ||
if (expected !== null && received !== expected) { | ||
function onEnd(err) { | ||
if (err) { | ||
done(err) | ||
} else if (length !== null && received !== length) { | ||
var err = new Error('request size did not match content length') | ||
err.status = 400 | ||
err.received = received | ||
err.expected = expected | ||
err.length = length | ||
done(err) | ||
@@ -82,6 +84,5 @@ } else { | ||
stream.removeListener('end', onEnd) | ||
stream.removeListener('error', done) | ||
stream.removeListener('error', cleanup) | ||
stream.removeListener('error', onEnd) | ||
stream.removeListener('close', cleanup) | ||
} | ||
} |
{ | ||
"name": "raw-body", | ||
"description": "Get and validate the raw body of a readable stream.", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Jonathan Ong", |
# Raw Body [![Build Status](https://travis-ci.org/stream-utils/raw-body.png)](https://travis-ci.org/stream-utils/raw-body) | ||
Gets the entire buffer of a stream and validates its length against an expected length and limit. | ||
Gets the entire buffer of a stream and validates its length against an expected length and maximum limit. | ||
Ideal for parsing request bodies. | ||
@@ -15,4 +15,4 @@ | ||
getRawBody(req, { | ||
expected: req.headers['content-length'], | ||
limit: 1 * 1024 * 1024 // 1 mb | ||
length: req.headers['content-length'], | ||
limit: '1mb' | ||
}, function (err, buffer) { | ||
@@ -28,5 +28,16 @@ if (err) | ||
or in Koa generator: | ||
```js | ||
app.use(function* (next) { | ||
var buffer = yield getRawBody(this.req, { | ||
length: this.length, | ||
limit: '1mb' | ||
}) | ||
}) | ||
``` | ||
### Options | ||
- `expected` - The expected length of the stream. | ||
- `length` - The length length of the stream. | ||
If the contents of the stream do not add up to this length, | ||
@@ -33,0 +44,0 @@ an `400` error code is returned. |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5285
70
1
85