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

vapr-input

v0.6.2

Published

A Vapr plugin for handling requests based on Content-Type

Downloads

16

Readme

vapr-input Build Status

Installation

npm install --save vapr
npm install --save vapr-input

Usage

This plugin is used to declare which media types are acceptable for an incoming request's body. If someone makes a request with an unsupported media type, they'll receive 415 Unsupported Media Type.

When a valid request is received, the corresponding parser function will be invoked with the raw body stream (a River) as its argument, and the result will become available at req.body.

const input = require('vapr-input');
const app = require('vapr')();
const route = app.get('/foo');

route.use(input({
  'application/json': raw => raw.all().then(Buffer.concat).then(JSON.parse),
  'application/xml': raw => raw.all().then(Buffer.concat).then(parseXML),
}));

route.use((req) => {
  req.body; // { foo: 'bar' };
});

Options

Media parameters are negotiated in a case-insensitive manner because many common parameters (e.g., charset) are case-insensitive. If you're using media parameters that are case-sensitive, you can reverse this behavior by setting the strictParameters option.

route.use(input({
  'strictParameters': true,
  'application/foo; some-strange-parameter=hello': serializationFunction,
}));

For the sake of simplicity and security, if someone makes a request with a charset parameter besides utf-8 or us-ascii, they'll receive 415 Unsupported Media Type. This behavior can be suppressed with the anyCharset option.

route.use(input({
  'anyCharset': true,
  'application/xml': async (raw, params) => {
    if (params.get('charset') !== 'utf-16le') throw 415;
    return parseXML(Buffer.concat(await raw.all()).toString('utf16le'));
  },
}));

As shown above, each parser function receives a second argument, which is a Map of the media parameters found within the Content-Type header. All parameter keys will be lowercased and, unless the strictParameters option is set, all parameter values will be lowercased as well.

You can optionally pass a default function, which is used when no other media type is matched.

route.use(input({
  'text/plain': raw => ...,
  'default': raw => ...,
}));

To reuse another parser as the default, set the default option to a string.

route.use(input({
  'text/plain': raw => ...,
  'default': 'text/plain',
}));

Sometimes you may wish to defer parsing the body until you really need to. By setting the deferred option, no parsing will happen automatically. Instead, req.body will be a function that triggers the correct parser and returns a promise for the result.

route.use(input({
  'deferred': true,
  'application/json': raw => raw.all().then(Buffer.concat).then(JSON.parse),
}));

route.use(async (req) => {
  const body = await req.body();
});