beach-body
v1.0.0
Published
zero-dependency util to read and validate the body of a readable stream
Downloads
9
Readme
beach-body
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
| property | type | default | description |
| ---------- | ----------------------------- | ----------- | -------------------------------------------- |
| length
| number | undefined
| undefined
| Expected length in bytes of the content |
| limit
| number | string | undefined
| undefined
| Maximum allowed size in bytes of the content |
| encoding
| BufferEncoding | undefined
| utf8
| Encoding 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);
});