Code Monkey home page Code Monkey logo

elysia-compress's Introduction

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

Cache Server-Sent-Events

By default, elysia-compress will not compress responses in Server-Sent Events. If you want to enable compression in Server-Sent Events, you can set the compressStream option to true.

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

Contributors

License

This plugins is licensed under the MIT License.

elysia-compress's People

Contributors

renovate[bot] avatar vermaysha avatar m-gonzalo avatar

Stargazers

Sunny Luo avatar Marvin Amador Campos avatar 🐙 avatar  avatar Meriah Ibrahim Abderrahim avatar JJ Teoh avatar Emirhan Kurt avatar Fihaa avatar theleonardo avatar loclv avatar mtt avatar Jurjen Vorhauer avatar Maverick Fabroa avatar Hans Askov avatar Yehezkiel Dio Sinolungan avatar  avatar Huilén Solís avatar Kainoa Kanter avatar  avatar Alexander Radyushin avatar

Watchers

 avatar

Forkers

m-gonzalo

elysia-compress's Issues

Error 500 with `Stream`

  • I'm using simple examples from https://elysiajs.com/patterns/stream.html
  • (if relevant) had to enable cors plugin in my usecase
  • fails with error 500 only on the stream endpoint, others work fine
  • if I set threshold: 0 then error 500 is avoided but the response contains {"name":"TypeError","message":"undefined is not an object (evaluating 'res.headers.get')"}
  • documentation says that stream compression is disabled by default (I have not modified any settings)

Let me know if you need any other info

Should be implemented as a `.mapResponse` instead of `.onAfterHandle`

This plugin interferes with other middlewares that might return a response early on the lifecycle.

I'm writing a caching plugin, and it needs to access the status of the request to know if it can save the response. But elysia-compress sets all headers to undefined. So approximately half of the requests are never cached. This ratio might have something to do with the order Elysia calls the plugins.

The docs state that it's recommended to compress the data after it's handled by any handler function (including an afterHandle) https://elysiajs.com/life-cycle/map-response.html

Every afterHandle function that returns something halts the processing of the request and the returned value is sent to the client.

PS: I also implemented a caching system for your plugin so it doesn't have to compress every request all the time. Along with my normal cache plugin, it makes for micro second response times. I'll submit a PR after this issue is fixed

BUG: Not working well with CORS

import cors from "@elysiajs/cors";
import { compression } from "elysia-compress";

const app = new Elysia()
	.use(serverTiming())
	.use(cors())
	.use(compression({ as: "global", threshold: 0 }))

But it's working if I put the threshold to 0. The http request is coming from another port using vite + react + @elysiajs/eden

Let me know if you need more information :)

Not working with official static pluigin

When using with official static plugin all static paths return 200 with Content-Length:0 and empty body.

Looks like it related to how this plugin handles streams. It expects response.stream to be present, but static plugin sets response.body to stream

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

bun
package.json
  • @elysiajs/cors ^1.1.0
  • @elysiajs/stream ^1.1.0
  • @eslint/js ^9.9.0
  • bun-types ^1.1.24
  • elysia ^1.1.6
  • eslint ^9.9.0
  • eslint-config-prettier ^9.1.0
  • eslint-plugin-prettier ^5.2.1
  • husky ^9.1.4
  • lint-staged ^15.2.9
  • prettier ^3.3.3
  • rimraf ^6.0.1
  • typescript ^5.5.4
  • typescript-eslint ^7.18.0
  • typescript ^5.5.4
  • elysia >= 1.1.6
  • node >=18.20.4
github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • oven-sh/setup-bun v2
  • actions/setup-node v4
  • codacy/codacy-coverage-reporter-action v1.3.0
.github/workflows/publish.yml
  • actions/checkout v4
  • actions/setup-node v4
  • oven-sh/setup-bun v2

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.