1.0.0 • Published 4 years ago

beach-body v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

beach-body

NPM Version NPM Downloads Build

zero-dependency util to read and validate the body of a readable stream

Install

Yarn

yarn add beach-body

NPM

npm install beach-body

API

import getBody from 'beach-body';

getBody(stream: Readable, options?: BeachBodyOptions): Promise<string>

BeachBodyOptions

propertytypedefaultdescription
lengthnumber | undefinedundefinedExpected length in bytes of the content
limitnumber | string | undefinedundefinedMaximum allowed size in bytes of the content
encodingBufferEncoding | undefinedutf8Encoding to decode the stream buffer with

Errors

Types

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

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.

Examples

Simple Node example

const http = require('http');
const getBody = require('beach-body');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer(async (req, res) => {
  const body = await getBody(req, {
    length: req.headers['content-length'],
    limit: '1mb',
  });

  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end(body);
});

License

MIT