Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-compress

Package Overview
Dependencies
Maintainers
9
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-compress

Fastify compression utils

  • 0.9.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7K
increased by7.12%
Maintainers
9
Weekly downloads
 
Created
Source

fastify-compress

Greenkeeper badge

Build Status js-standard-style

Adds compression utils to the Fastify reply object.
Support gzip, deflate and brotli.

Install

npm i fastify-compress --save

Usage

This plugins adds two functionalities to Fastify, a compress utility and a global compression hook.

Currently the following headers are supported:

  • 'deflate'
  • 'gzip'
  • 'br'
  • '*'

If the 'accept-encoding' header specifies no preferred encoding with an asterisk * the payload will be compressed with gzip.

If an unsupported encoding is received, it will automatically return a 406 error, if the 'accept-encoding' header is missing, it will not compress the payload.

It automatically defines if a payload should be compressed or not based on its Content-Type, if no content type is present, it will assume is application/json.

Global hook

The global compression hook is enabled by default if you want to disable it, pass the option { global: false }.

fastify.register(
  require('fastify-compress'),
  { global: false }
)

Remember that thanks to the Fastify encapsulation model, you can set a global compression, but running it only in a subset of routes is you wrap them inside a plugin.

reply.compress

This plugin add a compress function to reply that accepts a stream or a string and compress it based on the 'accept-encoding' header. If a js object is passed in, will be stringified as json.

const fs = require('fs')
const fastify = require('fastify')

fastify.register(require('fastify-compress'), { global: false })

fastify.get('/', (req, reply) => {
  reply
    .type('text/plain')
    .compress(fs.createReadStream('./package.json'))
})

fastify.listen(3000, function (err) {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
})

Options

Threshold

You can set a custom threshold below which it will not be made compression, default to 1024.

fastify.register(
  require('fastify-compress'),
  { threshold: 2048 }
)

customTypes

mime-db is used to determine if a Content-Type should be compressed. You can compress additional content types via regular expression.

fastify.register(
  require('fastify-compress'),
  { customTypes: /x-protobuf$/ }
)

Brotli

Brotli compression is enabled by default if your Node.js(>= v11.7.0) supports it natively.

For the Node.js versions that not support brotli natively, it's not enabled by default, if you need it we recommend to install iltorb and pass it as option.

fastify.register(
  require('fastify-compress'),
  { brotli: require('iltorb') }
)

Disable compression by header

You can selectively disable the response compression by using the x-no-compression header in the request.

Inflate pre-compressed bodies for clients that do not support compression

Optional feature to inflate pre-compressed data if the client doesn't include one of the supported compression types in its Accept-Encoding header.

fastify.register(
  require('fastify-compress'),
  { inflateIfDeflated: true }
)

fastify.get('/file', (req, reply) =>
  // will inflate the file  on the way out for clients
  // that indicate they do not support compression
  reply.send(fs.createReadStream('./file.gz')))

Note

Please have in mind that in large scale scenarios, you should use a proxy like Nginx to handle response-compression.

Acknowledgements

This project is kindly sponsored by:

License

Licensed under MIT.

Keywords

FAQs

Package last updated on 25 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc