npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ts-stack/body-parser

v1.2.0

Published

Node.js body parser writen in TypeScript, in promise style, in ESM format, without support Node.js version < 20.6.0.

Downloads

687

Readme

@ts-stack/body-parser

Node.js body parser writen in TypeScript, in promise style, in ESM format, without support Node.js version < 20.6.0. This library is a fork of the well-known ExpressJS body parser library (from this commit).

Note As request body's shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting. For example, body.foo.toString() may fail in multiple ways, for example the foo property may not be there or may not be a string, and toString may not be a function and instead a string or other user input.

Learn about the anatomy of an HTTP transaction in Node.js.

This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in busboy.

This module provides the following parsers: JSON, Raw, Text, URL-encoded body parsers.

Installation

npm install @ts-stack/body-parser

Please make sure that Node.js (version >= 20.6.0) is installed on your operating system.

Usage

The @ts-stack/body-parser module exposes various factories to create parsers. All parsers return parsed body in Promise when the Content-Type request header matches the type option, or an empty object ({}) if there was no body to parse, the Content-Type was not matched.

The various errors returned by this module are described in the errors section.

import http from 'http';
import { getJsonParser } from '@ts-stack/body-parser';

import { InterfaceOfBody } from './types.js';

const jsonParser = getJsonParser({ limit: '1kb' });

http.createServer(async function (req, res) {
  try {
    const body = await jsonParser<InterfaceOfBody>(req, req.headers);
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain')
    res.write('you posted:\n');
    res.end(JSON.stringify(body));
  } catch (err: any) {
    // handling an error
  }
});

Alternatively, you can use the BodyParserGroup helper. It is designed for cases when you do not know which parser is needed for a specific route. When creating an instance of the BodyParserGroup class, you can pass options for the corresponding parsers, after which you can use the parse method as follows:

import { BodyParserGroup } from '@ts-stack/body-parser';

const bodyParserGroup = new BodyParserGroup({
  jsonOptions: config.jsonOptions,
  textOptions: config.textOptions,
  urlencodedOptions: config.urlencodedOptions,
  rawOptions: config.rawOptions,
});

const body = await bodyParserGroup.parse(req, req.headers, {});

Change accepted type for parsers

All the parser factories accept a type option which allows you to change the Content-Type that the parser will parse.

import { getJsonParser, getRawParser, getTextParser } from '@ts-stack/body-parser';

// parse various different custom JSON types as JSON
const jsonParser = getJsonParser({ type: 'application/*+json' });

// parse some custom thing into a Buffer
const rawParser = getRawParser({ type: 'application/vnd.custom-type' });

// parse an HTML body into a string
const textParser = getTextParser({ type: 'text/html' });

Errors

The parsers provided by this module create errors using the http-errors module. The errors will typically have a status/statusCode property that contains the suggested HTTP response code, an expose property to determine if the message property should be displayed to the client, a type property to determine the type of error without matching against the message, and a body property containing the read body, if available.

The following are the common errors created, though any error can come through for various reasons.

content encoding unsupported

This error will occur when the request had a Content-Encoding header that contained an encoding but the "inflation" option was set to false. The status property is set to 415, the type property is set to 'encoding.unsupported', and the charset property will be set to the encoding that is unsupported.

entity parse failed

This error will occur when the request contained an entity that could not be parsed by the parser. The status property is set to 400, the type property is set to 'entity.parse.failed', and the body property is set to the entity value that failed parsing.

entity verify failed

This error will occur when the request contained an entity that could not be failed verification by the defined verify option. The status property is set to 403, the type property is set to 'entity.verify.failed', and the body property is set to the entity value that failed verification.

request aborted

This error will occur when the request is aborted by the client before reading the body has finished. The received property will be set to the number of bytes received before the request was aborted and the expected property is set to the number of expected bytes. The status property is set to 400 and type property is set to 'request.aborted'.

request entity too large

This error will occur when the request body's size is larger than the "limit" option. The limit property will be set to the byte limit and the length property will be set to the request body's length. The status property is set to 413 and the type property is set to 'entity.too.large'.

request size did not match content length

This error will occur when the request's length did not match the length from the Content-Length header. This typically occurs when the request is malformed, typically when the Content-Length header was calculated based on characters instead of bytes. The status property is set to 400 and the type property is set to 'request.size.invalid'.

stream encoding should not be set

This error will occur when something called the req.setEncoding method prior to this parser. This module operates directly on bytes only and you cannot call req.setEncoding when using this module. The status property is set to 500 and the type property is set to 'stream.encoding.set'.

stream is not readable

This error will occur when the request is no longer readable when this parser attempts to read it. This typically means something other than a parser from this module read the request body already and the parser was also configured to read the same request. The status property is set to 500 and the type property is set to 'stream.not.readable'.

too many parameters

This error will occur when the content of the request exceeds the configured parameterLimit for the urlencoded parser. The status property is set to 413 and the type property is set to 'parameters.too.many'.

unsupported charset "BOGUS"

This error will occur when the request had a charset parameter in the Content-Type header, but the iconv-lite module does not support it OR the parser does not support it. The charset is contained in the message as well as in the charset property. The status property is set to 415, the type property is set to 'charset.unsupported', and the charset property is set to the charset that is unsupported.

unsupported content encoding "bogus"

This error will occur when the request had a Content-Encoding header that contained an unsupported encoding. The encoding is contained in the message as well as in the encoding property. The status property is set to 415, the type property is set to 'encoding.unsupported', and the encoding property is set to the encoding that is unsupported.

License

MIT