shrink-ray-current: Node.js Compression Middleware

4.1.3 · active · verified Wed Apr 22

Shrink-ray-current is a Node.js compression middleware designed for Express/Connect applications, providing advanced content encoding capabilities beyond standard gzip. Currently stable at version 4.1.3 (last updated April 2021), it's a maintained fork of the abandoned `shrink-ray` package, ensuring its dependencies are up-to-date. Its key differentiators include built-in support for Brotli and Zopfli (the latter specifically for asynchronous compression of static assets), alongside traditional deflate and gzip. The middleware leverages ETag caching to significantly boost performance for static files, claiming to be 3x faster and using a quarter of the CPU time compared to the standard `compression` middleware in benchmarks, by utilizing higher quality compression algorithms for cached content. Releases appear to be driven by dependency updates or minor bug fixes rather than a fixed schedule.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates integrating `shrink-ray-current` as Express/Connect middleware for both dynamic and (simulated) static content, showing basic setup and enabling compression for routes.

const express = require('express');
const shrinkRay = require('shrink-ray-current');

const app = express();

// Use the middleware with default options
app.use(shrinkRay());

// Example route for dynamic content
app.get('/', (req, res) => {
  res.send('Hello World! This content will be compressed.');
});

// Example route for static-like content (e.g., a large JSON file)
app.get('/data', (req, res) => {
  const largeData = Array(1000).fill({
    id: Math.random(),
    name: 'Item ' + Math.random(),
    description: 'This is a long description for a test item to ensure compression benefits are visible.',
    details: {
      field1: 'value1',
      field2: 'value2',
      field3: 'value3',
      nested: {
        a: 1,
        b: 2,
        c: 3
      }
    }
  });
  res.json(largeData);
});

// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server listening on port ${port}`);
  console.log('Try accessing / and /data with accept-encoding headers (e.g., curl -H "Accept-Encoding: gzip, deflate, br" http://localhost:3000/data).');
});

view raw JSON →