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-compress

v0.6.1

Published

A compression plugin for Vapr

Downloads

8

Readme

vapr-compress Build Status

Installation

npm install --save vapr
npm install --save vapr-compress

Usage

This plugin applies standard compression to Vapr responses.

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

route.use(compress());
route.use((req) => {
  return [['hello world']]; // This will be gzipped
});

Details

This plugin utilizes content negotiation to decide when to compress a response. It will try to use the Transfer-Encoding header if supported by the client (as indicated by the TE header), but will fallback to the Content-Encoding/Accept-Encoding headers when necessary. Both the gzip and deflate encodings are supported, but gzip will be prioritized when the client has not indicated a preference.

If the response body is null or undefined, compression will be skipped. However, empty strings and buffers are still considered eligible (for the sake of cache coherence, response size is not taken into consideration by default). Compression is also skipped for HEAD requests, but headers are still set as if it were a GET request.

Unless the forced option is used or the only option is set to "transfer-encoding", the Vary header will be automatically updated to make caches aware of the content negotiation.

Options

Any options passed to the plugin are forwarded to the zlib core module. In addition, the behavior of the plugin can be customized with the options below.

options.only = null

This option is used to restrict compression to either "content-encoding" or "transfer-encoding", disabling the opposite style.

route.use(compress({ only: 'transfer-encoding' }));

options.aggressive = false

By default, if the client doesn't provide any content negotiation headers, compression will be skipped. However, if aggressive is true, compression (Content-Encoding) will be used in such cases.

route.use(compress({ aggressive: true }));

Since the aggressive option modifies the behavior of Content-Encoding compression, it cannot be used when the only option is set to "transfer-encoding".

options.forced = null

This option is used to bypass content negotiation. It can be set to either "content-encoding" or "transfer-encoding", indicating which style of compression to use.

route.use(compress({ forced: 'content-encoding' }));

Since the aggressive and only options modify the behavior of content negotiation but the forced option turns off content negotiation entirely, the neither of the former options can be used in combination with forced.

options.anyStatus = false

The default behavior is to skip responses that have a status code of 300 or higher. If this option is set to true, however, responses of any status code will be eligible for compression.

route.use(compress({ anyStatus: true }));

options.condition = null

Optionally, a condition function can be provided to bypass compression on a per-request basis. For each eligible request, the function will be invoked with request and response as arguments. If the function does not return true, compression will be skipped for that request.

route.use(compress({
  anyStatus: true,
  condition: (req, res) => res.code < 500,
}));