raw-body

  • Version 4.0.0
  • Published
  • 30.1 kB
  • 2 dependencies
  • MIT license

Install

npm i raw-body
yarn add raw-body
pnpm add raw-body

Overview

Get and validate the raw body of a readable stream.

Index

Functions

function getRawBody

getRawBody: {
(stream: NodeRawBodyStream, callback: Callback<Buffer>): void;
(
stream: any,
options: Encoding | Readonly<Options & { encoding: Encoding }>,
callback: Callback<string>
): void;
(stream: any, options: Readonly<Options>, callback: Callback<Buffer>): void;
(
stream: any,
options: Encoding | Readonly<Options & { encoding: Encoding }>
): Promise<string>;
(stream: any, options?: Readonly<Options>): Promise<Buffer>;
};
  • Gets the entire buffer of a node stream as a Buffer, delivered to the callback. Validates the stream's length against an expected length and maximum limit. Ideal for parsing request bodies.

  • Gets the entire buffer of a node stream decoded as a string with the given encoding, delivered to the callback. Validates the stream's length against an expected length and maximum limit. Ideal for parsing request bodies.

  • Gets the entire buffer of a node stream decoded as a string with the given encoding. Validates the stream's length against an expected length and maximum limit. Ideal for parsing request bodies.

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

function getRawBodyWeb

getRawBodyWeb: {
(stream: WebRawBodyStream, callback: Callback<Buffer>): void;
(
stream: WebRawBodyStream,
options: Encoding | Readonly<Options & { encoding: Encoding }>,
callback: Callback<string>
): void;
(
stream: WebRawBodyStream,
options: Readonly<Options>,
callback: Callback<Buffer>
): void;
(
stream: WebRawBodyStream,
options: Encoding | Readonly<Options & { encoding: Encoding }>
): Promise<string>;
(stream: WebRawBodyStream, options?: Readonly<Options>): Promise<Buffer>;
};
  • Gets the entire buffer of a web ReadableStream as a Buffer, delivered to the callback. Validates the stream's length against an expected length and maximum limit. Ideal for parsing request bodies.

  • Gets the entire buffer of a web ReadableStream decoded as a string with the given encoding, delivered to the callback. Validates the stream's length against an expected length and maximum limit. Ideal for parsing request bodies.

  • Gets the entire buffer of a web ReadableStream decoded as a string with the given encoding. Validates the stream's length against an expected length and maximum limit. Ideal for parsing request bodies.

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

Interfaces

interface Decoder

interface Decoder {}
  • A streaming decoder, turning body chunks into a string.

method end

end: () => string | undefined;

    method write

    write: (chunk: Buffer) => string;

      interface Options

      interface Options {}

        property decoder

        decoder?: (encoding: string) => Decoder;
        • A function that receives the encoding and returns the decoder used to turn the body into a string, instead of the built-in TextDecoder. Compatible with iconv-lite's getDecoder. Throwing signals the encoding is unsupported.

        property encoding

        encoding?: Encoding | false | null;
        • 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 encoding supported by TextDecoder. false (or any other falsy value) disables decoding, returning a Buffer.

        property length

        length?: number | string | null;
        • The expected length of the stream.

        property limit

        limit?: number | string | null;
        • 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'.

        interface RawBodyError

        interface RawBodyError extends Error {}

          property code

          code?: string;
          • The error code, when there is one, e.g. ECONNABORTED for aborted requests.

          property encoding

          encoding?: string;
          • The encoding.

          property expected

          expected?: number;

            property length

            length?: number;
            • The expected length of the stream.

            property limit

            limit?: number;
            • The limit in bytes.

            property received

            received?: number;
            • The received bytes.

            property status

            status?: number;
            • The corresponding status code for the error. Errors created by raw-body always carry one, but errors passed through from the stream or a custom decoder may not.

            property statusCode

            statusCode?: number;

              property type

              type?: RawBodyErrorType;
              • The error type. Set on every error created by raw-body, but absent on errors passed through from the stream or a custom decoder.

              Type Aliases

              type Encoding

              type Encoding = string | true;
              • The encoding to decode the body with. true decodes as utf-8.

              type NodeRawBodyStream

              type NodeRawBodyStream = NodeJS.ReadableStream | Readable;
              • The stream types accepted by getRawBody.

              type RawBodyErrorType

              type RawBodyErrorType =
              | 'encoding.unsupported'
              | 'entity.too.large'
              | 'request.aborted'
              | 'request.size.invalid'
              | 'stream.encoding.set'
              | 'stream.not.readable';
              • The type values of the errors created by raw-body.

              type WebRawBodyStream

              type WebRawBodyStream = ReadableStream<Uint8Array | string>;
              • The stream types accepted by getRawBodyWeb.

              Package Files (1)

              Dependencies (2)

              Dev Dependencies (11)

              Peer Dependencies (0)

              No peer dependencies.

              Badge

              To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

              You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/raw-body.

              • Markdown
                [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/raw-body)
              • HTML
                <a href="https://www.jsdocs.io/package/raw-body"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>