Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

read-body

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

read-body

Buffer a readable stream into memory.

latest
Source
npmnpm
Version
0.2.1
Version published
Maintainers
1
Created
Source

read-body v0.0.1

Buffer a readable stream into memory.

import {readBody} from 'read-body'

// Read the stream into a buffer (limit of 1mb).
let buffer = await readBody(stream)

// Clear the cached buffer.
stream.body = null

// Read the same stream again, but with a limit of 100mb.
buffer = await readBody(stream, 1e8)

// Parse the stream as JSON.
const json = await stream.json()

// Do the two lines above in one fell swoop.
buffer = await readBody(stream, {json: true, maxBytes: 1e8})

When readBody is called, the stream has its body property set to a promise that resolves with a buffer. By default, the buffer is limited to 1mb max. If the limit is exceeded, the promise is rejected, the buffer is destroyed, and the stream is drained. The body property is used to prevent reading the same stream more than once.

Instead of passing json: true via the options object, try calling the .json() method which is available on the stream once the promise is resolved.

For convenience, the bytes and maxBytes properties are added to the stream once the promise is resolved.

readJson(input, ?config)

Shortcut for readBody(stream, {json: true}).

import {readJson} from 'read-body'

// Read the stream into a JSON value (limit of 1mb).
const json = await readJson(stream)

FAQs

Package last updated on 17 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts