Arcjet utilities for extracting the body from a stream
npm install @arcjet/body@arcjet/body[Arcjet][arcjet] utilities for getting the body from a stream.
- npm package (@arcjet/body)
- GitHub source code (body/ in arcjet/arcjet-js)
This is an internal utility to help us read streams from various frameworks.
It’s in part a fork of [stream-utils/raw-body][node-raw-body].
We chose to fork so that we can cut away functionality that we do not use
and keep our dependency tree as light as possible.
Specifically it always parses the stream as a UTF-8 string instead of a Buffer
and only supports promises instead of callbacks.
You should not use this but use [stream-utils/raw-body][node-raw-body] or one
of the alternatives instead.
This package matches our current needs which are likely different from yours.
This package is ESM only.
Install with npm in Node.js:
``sh`
npm install @arcjet/body
`ts
import fs from "node:fs";
import { readBody } from "@arcjet/body";
const body = await readBody(fs.createReadStream("example.ts"), { limit: 1024 });
console.log(body);
`
This package exports the identifiers
[readBodyWeb][api-read-body-web] andreadBody
[][api-read-body].
There is no default export.
This package exports the [TypeScript][] types
[ReadBodyOpts][api-read-body-opts] andReadableStreamLike
[][api-readable-stream-like].
Configuration (TypeScript type).
###### Fields
- expectedLength (number, optional)limit
— length of the stream in bytes;
rejects an error if the contents of the stream do not add up to this length;
useful when the exact size is known
- (number, required)
— limit of the body in bytes;
rejects an error if the body ends up being larger than this limit;
used to prevent reading too much data from malicious clients.
Minimal Node.js stream interface (TypeScript type).
`ts`
interface ReadableStreamLike {
on?: EventHandlerLike | null | undefined;
readable?: boolean | null | undefined;
removeListener?: EventHandlerLike | null | undefined;
}
Read the body of a
[web stream][web-stream].
###### Parameters
- stream (ReadableStream)options
— stream
- ([ReadBodyOpts][api-read-body-opts], required)
— configuration
###### Returns
Promise that resolves to a concatenated body (Promise).
Read the body of a Node.js stream.
###### Parameters
- stream (ReadableStream)options
— stream
- ([ReadBodyOpts][api-read-body-opts], required)
— configuration
###### Returns
Promise that resolves to a concatenated body (Promise).
[Apache License, Version 2.0][apache-license] © [Arcjet Labs, Inc.][arcjet]
Derivative work based on [raw-body`][node-raw-body] licensed under
[MIT][node-raw-body-license] © Jonathan Ong and Douglas Christopher Wilson.
Our work removes features that we do not use: no buffers, no sync interface.
It adds a version for web streams.
[apache-license]: http://www.apache.org/licenses/LICENSE-2.0
[api-read-body-opts]: #readbodyopts
[api-read-body-web]: #readbodywebstream-options
[api-read-body]: #readbodystream-options
[api-readable-stream-like]: #readablestreamlike
[arcjet]: https://arcjet.com
[node-raw-body-license]: https://github.com/stream-utils/raw-body/blob/191e4b6/LICENSE
[node-raw-body]: https://github.com/stream-utils/raw-body/blob/191e4b6/test/index.js
[typescript]: https://www.typescriptlang.org
[web-stream]: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream