Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

raw-body

stream-utils116.8mMIT3.0.0TypeScript support: included

Get and validate the raw body of a readable stream.

readme

raw-body

NPM Version NPM Downloads Node.js Version Build status Test coverage

Gets the entire buffer of a stream either as a Buffer or a string. Validates the stream's length against an expected length and maximum limit. Ideal for parsing request bodies.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install raw-body

TypeScript

This module includes a TypeScript declaration file to enable auto complete in compatible editors and type information for TypeScript projects. This module depends on the Node.js types, so install @types/node:

$ npm install @types/node

API

var getRawBody = require('raw-body')

getRawBody(stream, [options], [callback])

Returns a promise if no callback specified and global Promise exists.

Options:

  • length - The 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. This is the number of bytes or any string format supported by bytes, for example 1000, '500kb' or '3mb'. If the body ends up being larger than this limit, a 413 error code is returned.
  • encoding - The encoding to use to decode the body into a string. By default, a Buffer instance will be returned when no encoding is specified. Most likely, you want utf-8, so setting encoding to true will decode as utf-8. You can use any type of encoding supported by iconv-lite.

You can also pass a string in place of options to just specify the encoding.

If an error occurs, the stream will be paused, everything unpiped, and you are responsible for correctly disposing the stream. For HTTP requests, you may need to finish consuming the stream if you want to keep the socket open for future requests. For streams that use file descriptors, you should stream.destroy() or stream.close() to prevent leaks.

Errors

This module creates errors depending on the error condition during reading. The error may be an error from the underlying Node.js implementation, but is otherwise an error created by this module, which has the following attributes:

  • limit - the limit in bytes
  • length and expected - the expected length of the stream
  • received - the received bytes
  • encoding - the invalid encoding
  • status and statusCode - the corresponding status code for the error
  • type - the error type

Types

The errors from this module have a type property which allows for the programmatic determination of the type of error returned.

encoding.unsupported

This error will occur when the encoding option is specified, but the value does not map to an encoding supported by the iconv-lite module.

entity.too.large

This error will occur when the limit option is specified, but the stream has an entity that is larger.

request.aborted

This error will occur when the request stream is aborted by the client before reading the body has finished.

request.size.invalid

This error will occur when the length option is specified, but the stream has emitted more bytes.

stream.encoding.set

This error will occur when the given stream has an encoding set on it, making it a decoded stream. The stream should not have an encoding set and is expected to emit Buffer objects.

stream.not.readable

This error will occur when the given stream is not readable.

Examples

Simple Express example

var contentType = require('content-type')
var express = require('express')
var getRawBody = require('raw-body')

var app = express()

app.use(function (req, res, next) {
  getRawBody(req, {
    length: req.headers['content-length'],
    limit: '1mb',
    encoding: contentType.parse(req).parameters.charset
  }, function (err, string) {
    if (err) return next(err)
    req.text = string
    next()
  })
})

// now access req.text

Simple Koa example

var contentType = require('content-type')
var getRawBody = require('raw-body')
var koa = require('koa')

var app = koa()

app.use(function * (next) {
  this.text = yield getRawBody(this.req, {
    length: this.req.headers['content-length'],
    limit: '1mb',
    encoding: contentType.parse(this.req).parameters.charset
  })
  yield next
})

// now access this.text

Using as a promise

To use this library as a promise, simply omit the callback and a promise is returned, provided that a global Promise is defined.

var getRawBody = require('raw-body')
var http = require('http')

var server = http.createServer(function (req, res) {
  getRawBody(req)
    .then(function (buf) {
      res.statusCode = 200
      res.end(buf.length + ' bytes submitted')
    })
    .catch(function (err) {
      res.statusCode = 500
      res.end(err.message)
    })
})

server.listen(3000)

Using with TypeScript

import * as getRawBody from 'raw-body';
import * as http from 'http';

const server = http.createServer((req, res) => {
  getRawBody(req)
  .then((buf) => {
    res.statusCode = 200;
    res.end(buf.length + ' bytes submitted');
  })
  .catch((err) => {
    res.statusCode = err.statusCode;
    res.end(err.message);
  });
});

server.listen(3000);

License

MIT

changelog

3.0.0 / 2024-07-25

  • deps: iconv-lite@0.6.3
    • Fix HKSCS encoding to prefer Big5 codes
    • Fix minor issue in UTF-32 decoder's endianness detection code
    • Update 'gb18030' encoding to :2005 edition

3.0.0-beta.1 / 2023-02-21

  • Change TypeScript argument to NodeJS.ReadableStream interface
  • Drop support for Node.js 0.8
  • deps: iconv-lite@0.5.2
    • Add encoding cp720
    • Add encoding UTF-32

2.5.2 / 2023-02-21

  • Fix error message for non-stream argument

2.5.1 / 2022-02-28

  • Fix error on early async hooks implementations

2.5.0 / 2022-02-21

2.4.3 / 2022-02-14

2.4.2 / 2021-11-16

2.4.1 / 2019-06-25

2.4.0 / 2019-04-17

2.3.3 / 2018-05-08

2.3.2 / 2017-09-09

2.3.1 / 2017-09-07

2.3.0 / 2017-08-04

  • Add TypeScript definitions
  • Use http-errors for standard emitted errors
  • deps: bytes@2.5.0
  • deps: iconv-lite@0.4.18
    • Add support for React Native
    • Add a warning if not loaded as utf-8
    • Fix CESU-8 decoding in Node.js 8
    • Improve speed of ISO-8859-1 encoding

2.2.0 / 2017-01-02

  • deps: iconv-lite@0.4.15
    • Added encoding MS-31J
    • Added encoding MS-932
    • Added encoding MS-936
    • Added encoding MS-949
    • Added encoding MS-950
    • Fix GBK/GB18030 handling of Euro character

2.1.7 / 2016-06-19

  • deps: bytes@2.4.0
  • perf: remove double-cleanup on happy path

2.1.6 / 2016-03-07

  • deps: bytes@2.3.0
    • Drop partial bytes on all parsed units
    • Fix parsing byte string that looks like hex

2.1.5 / 2015-11-30

2.1.4 / 2015-09-27

  • Fix masking critical errors from iconv-lite
  • deps: iconv-lite@0.4.12
    • Fix CESU-8 decoding in Node.js 4.x

2.1.3 / 2015-09-12

  • Fix sync callback when attaching data listener causes sync read
    • Node.js 0.10 compatibility issue

2.1.2 / 2015-07-05

  • Fix error stack traces to skip makeError
  • deps: iconv-lite@0.4.11
    • Add encoding CESU-8

2.1.1 / 2015-06-14

  • Use unpipe module for unpiping requests

2.1.0 / 2015-05-28

  • deps: iconv-lite@0.4.10
    • Improved UTF-16 endianness detection
    • Leading BOM is now removed when decoding
    • The encoding UTF-16 without BOM now defaults to UTF-16LE when detection fails

2.0.2 / 2015-05-21

2.0.1 / 2015-05-10

  • Fix a false-positive when unpiping in Node.js 0.8

2.0.0 / 2015-05-08

  • Return a promise without callback instead of thunk
  • deps: bytes@2.0.1
    • units no longer case sensitive when parsing

1.3.4 / 2015-04-15

  • Fix hanging callback if request aborts during read
  • deps: iconv-lite@0.4.8
    • Add encoding alias UNICODE-1-1-UTF-7

1.3.3 / 2015-02-08

1.3.2 / 2015-01-20

1.3.1 / 2014-11-21

1.3.0 / 2014-07-20

  • Fully unpipe the stream on error
    • Fixes Cannot switch to old mode now error on Node.js 0.10+

1.2.3 / 2014-07-20

1.2.2 / 2014-06-19

  • Send invalid encoding error to callback

1.2.1 / 2014-06-15

1.2.0 / 2014-06-13

  • Passing string as options interpreted as encoding
  • Support all encodings from iconv-lite

1.1.7 / 2014-06-12

  • use string_decoder module from npm

1.1.6 / 2014-05-27

  • check encoding for old streams1
  • support node.js < 0.10.6

1.1.5 / 2014-05-14

  • bump bytes

1.1.4 / 2014-04-19

  • allow true as an option
  • bump bytes

1.1.3 / 2014-03-02

  • fix case when length=null

1.1.2 / 2013-12-01

  • be less strict on state.encoding check

1.1.1 / 2013-11-27

  • add engines

1.1.0 / 2013-11-27

  • add err.statusCode and err.type
  • allow for encoding option to be true
  • pause the stream instead of dumping on error
  • throw if the stream's encoding is set

1.0.1 / 2013-11-19

  • dont support streams1, throw if dev set encoding

1.0.0 / 2013-11-17

  • rename expected option to length

0.2.0 / 2013-11-15

  • republish

0.1.1 / 2013-11-15

  • use bytes

0.1.0 / 2013-11-11

  • generator support

0.0.3 / 2013-10-10

  • update repo

0.0.2 / 2013-09-14

  • dump stream on bad headers
  • listen to events after defining received and buffers

0.0.1 / 2013-09-14

  • Initial release