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

raw-body

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

raw-body - npm Package Compare versions

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.

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