pax_global_header00006660000000000000000000000064122255737250014524gustar00rootroot0000000000000052 comment=6a1ad21b39946dd41dac7444781f95a62dd818dc raw-body-0.0.3/000077500000000000000000000000001222557372500132505ustar00rootroot00000000000000raw-body-0.0.3/.gitignore000066400000000000000000000011271222557372500152410ustar00rootroot00000000000000# Compiled source # ################### *.com *.class *.dll *.exe *.o *.so # Packages # ############ # it's better to unpack these files and commit the raw source # git has its own built in compression methods *.7z *.dmg *.gz *.iso *.jar *.rar *.tar *.zip # Logs and databases # ###################### *.log *.sql *.sqlite # OS generated files # ###################### .DS_Store* ehthumbs.db Icon? Thumbs.db # Node.js # ########### lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz pids logs results node_modules npm-debug.log # Components # ############## /build /components /vendors *.origraw-body-0.0.3/.npmignore000066400000000000000000000000071222557372500152440ustar00rootroot00000000000000test.jsraw-body-0.0.3/.travis.yml000066400000000000000000000000531222557372500153570ustar00rootroot00000000000000node_js: - "0.10" - "0.8" language: node_jsraw-body-0.0.3/Makefile000066400000000000000000000002001222557372500147000ustar00rootroot00000000000000BIN = ./node_modules/.bin/ test: @${BIN}mocha \ --reporter spec \ --bail clean: @rm -rf node_modules .PHONY: test cleanraw-body-0.0.3/README.md000066400000000000000000000044511222557372500145330ustar00rootroot00000000000000# 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. Ideal for parsing request bodies. This is the callback version of [cat-stream](https://github.com/jonathanong/cat-stream), which is much more convoluted because streams suck. ## API ```js var getRawBody = require('raw-body') app.use(function (req, res, next) { getRawBody(req, { expected: req.headers['content-length'], limit: 1 * 1024 * 1024 // 1 mb }, function (err, buffer) { if (err) return next(err) req.rawBody = buffer next() }) }) ``` ### Options - `expected` - The expected length of the stream. If the contents of the stream do not add up to this length, an `400` error code is returned. - `limit` - The byte limit of the body. If the body ends up being larger than this limit, a `413` error code is returned. ### Strings This library only returns the raw buffer. If you want the string, you can do something like this: ```js getRawBody(req, function (err, buffer) { if (err) return next(err) req.text = buffer.toString('utf8') next() }) ``` ## License The MIT License (MIT) Copyright (c) 2013 Jonathan Ong me@jongleberry.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.raw-body-0.0.3/index.js000066400000000000000000000032271222557372500147210ustar00rootroot00000000000000module.exports = function (stream, options, callback) { if (typeof options === 'function') { callback = options options = {} } var limit = typeof options.limit === 'number' ? options.limit : null var expected = !isNaN(options.expected) ? parseInt(options.expected, 10) : null if (limit !== null && expected !== null && expected > limit) { var err = new Error('request entity too large') err.status = 413 err.expected = expected err.limit = limit callback(err) stream.resume() // dump stream cleanup() return } var received = 0 var buffers = [] stream.on('data', onData) stream.once('end', onEnd) stream.once('error', callback) stream.once('error', cleanup) stream.once('close', cleanup) function onData(chunk) { buffers.push(chunk) received += chunk.length if (limit !== null && received > limit) { var err = new Error('request entity too large') err.status = 413 err.received = received err.limit = limit callback(err) cleanup() } } function onEnd() { if (expected !== null && received !== expected) { var err = new Error('request size did not match content length') err.status = 400 err.received = received err.expected = expected callback(err) } else { callback(null, Buffer.concat(buffers)) } cleanup() } function cleanup() { received = buffers = null stream.removeListener('data', onData) stream.removeListener('end', onEnd) stream.removeListener('error', callback) stream.removeListener('error', cleanup) stream.removeListener('close', cleanup) } }raw-body-0.0.3/package.json000066400000000000000000000011441222557372500155360ustar00rootroot00000000000000{ "name": "raw-body", "description": "Get and validate the raw body of a readable stream.", "version": "0.0.3", "author": { "name": "Jonathan Ong", "email": "me@jongleberry.com", "url": "http://jongleberry.com", "twitter": "https://twitter.com/jongleberry" }, "license": "MIT", "repository": { "type": "git", "url": "https://github.com/stream-utils/raw-body.git" }, "bugs": { "mail": "me@jongleberry.com", "url": "https://github.com/stream-utils/raw-body/issues" }, "devDependencies": { "mocha": "~1.12" }, "scripts": { "test": "make test" } } raw-body-0.0.3/test.js000066400000000000000000000052341222557372500145710ustar00rootroot00000000000000var assert = require('assert') var fs = require('fs') var path = require('path') var Stream = require('stream') var getRawBody = require('./') var file = path.join(__dirname, 'index.js') var length = fs.statSync(file).size var string = fs.readFileSync(file, 'utf8') function createStream() { return fs.createReadStream(file) } function checkBuffer(buf) { assert.ok(Buffer.isBuffer(buf)) assert.equal(buf.length, length) assert.equal(buf.toString('utf8'), string) } describe('Raw Body', function () { it('should work without any options', function (done) { getRawBody(createStream(), function (err, buf) { assert.ifError(err) checkBuffer(buf) done() }) }) it('should work with expected length', function (done) { getRawBody(createStream(), { expected: length }, function (err, buf) { assert.ifError(err) checkBuffer(buf) done() }) }) it('should work with limit', function (done) { getRawBody(createStream(), { limit: length + 1 }, function (err, buf) { assert.ifError(err) checkBuffer(buf) done() }) }) it('should work with limit and expected length', function (done) { getRawBody(createStream(), { expected: length, limit: length + 1 }, function (err, buf) { assert.ifError(err) checkBuffer(buf) done() }) }) it('should check options for limit and expected length', function (done) { var stream = createStream() // Stream should still be consumed. stream.once('end', done) getRawBody(stream, { expected: length, limit: length - 1 }, function (err, buf) { assert.equal(err.status, 413) }) }) it('should work with an empty stream', function (done) { var stream = new Stream() getRawBody(stream, { expected: 0, limit: 1 }, function (err, buf) { assert.ifError(err) assert.equal(buf.length, 0) done() }) stream.emit('end') }) it('should throw on empty string and incorrect expected length', function (done) { var stream = new Stream() getRawBody(stream, { expected: 1, limit: 2 }, function (err, buf) { assert.equal(err.status, 400) done() }) stream.emit('end') }) it('should throw if length > limit', function (done) { getRawBody(createStream(), { limit: length - 1 }, function (err, buf) { assert.equal(err.status, 413) done() }) }) it('should throw if length !== expected length', function (done) { getRawBody(createStream(), { expected: length - 1 }, function (err, buf) { assert.equal(err.status, 400) done() }) }) })