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

elysia-compress

v1.2.1

Published

Elysia Compression that supports Brotli, GZIP, and Deflate compression

Downloads

1,496

Readme

elysia-compress

CI Test NPM version Codacy coverage GitHub License NPM Downloads

Add compression to Elysia Server. Supports gzip, deflate, and brotli.

Note Brotli Compression is only available and supported by Bun v1.1.8 or higher

Install

bun add elysia-compress

Usage

This plugin provides a function to automatically compress every Response sent by Elysia Response. Especially on responses in the form of JSON Objects, Text and Stream (Server Sent Events).

Currently, the following encoding tokens are supported, using the first acceptable token in this order:

  1. br
  2. gzip
  3. deflate

If an unsupported encoding is received or if the 'accept-encoding' header is missing, it will not compress the payload.

The plugin automatically decides if a payload should be compressed based on its content-type; if no content type is present, it will assume text/plain. But if you send a response in the form of an Object then it will be detected automatically as application/json

To improve performance, and given data compression is a resource-intensive operation, caching compressed responses can significantly reduce the load on your server. By setting an appropriate TTL (time to live, or how long you want your responses cached), you can ensure that frequently accessed data is served quickly without repeatedly compressing the same content. elysia-compress saves the data in-memory, so it's probably best if you set some sensible defaults (maybe even per-route or group) so as to not increase unnecessarily your memory usage

Global Hook

The global compression hook is enabled by default. To disable it, pass the option { as: 'scoped' } or { as: 'scoped' } You can read in-depth about Elysia Scope on this page

import { Elysia } from 'elysia'
import { compression } from 'elysia-compress'

const app = new Elysia()
  .use(
    compression({
      as: 'scoped',
    }),
  )
  .get('/', () => ({ hello: 'world' }))

Compress Options

threshold

The minimum byte size for a response to be compressed. Defaults to 1024.

const app = new Elysia().use(
  compression({
    threshold: 2048,
  }),
)

Disable compression by header

You can selectively disable response compression by using the x-no-compression header in the request. You can still disable this option by adding disableByHeader: true to options. Default to false

const app = new Elysia().use(
  compression({
    disableByHeader: true,
  }),
)

brotliOptions and zlibOptions

You can tune compression by setting the brotliOptions and zlibOptions properties. These properties are passed directly to native node zlib methods, so they should match the corresponding class definitions.

const app = new Elysia().use(
  compression({
    brotliOptions: {
      params: {
        [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT, // useful for APIs that primarily return text
        [zlib.constants.BROTLI_PARAM_QUALITY]: 4, // default is 4, max is 11, min is 0
      },
    },
    zlibOptions: {
      level: 6, // default is typically 6, max is 9, min is 0
    },
  }),
)

Customize encoding priority

By default, elysia-compress prioritizes compression as described Usage. You can change that by passing an array of compression tokens to the encodings option:

const app = new Elysia().use(
  compression({
    // Only support gzip and deflate, and prefer deflate to gzip
    encodings: ['deflate', 'gzip'],
  }),
)

Cache TTL

You can specify a time-to-live (TTL) for the cache entries to define how long the compressed responses should be cached. The TTL is specified in seconds and defaults to 86400 (24 hours)

const app = new Elysia().use(
  compression({
    TTL: 3600, // Cache TTL of 1 hour
  }),
)

This allows you to control how long the cached compressed responses are stored, helping to balance between performance and memory usage

Contributors