shrink-ray-current: Node.js Compression Middleware
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
-
Cannot find module 'iltorb' (or 'node-zopfli-es')
cause The peer dependencies for Brotli and Zopfli compression were not installed after `shrink-ray-current` v4.0.0.fixInstall the missing optional dependencies: `npm install --save-optional iltorb node-zopfli-es`. -
node-gyp rebuild failed
cause Native modules (iltorb, node-zopfli-es) failed to compile during installation, typically due to missing system-level build tools.fixEnsure you have the Node.js native build toolchain installed. This usually includes Python, Make, and a C++ compiler (e.g., Visual Studio Build Tools on Windows, Xcode Command Line Tools on macOS, or `build-essential` on Debian/Ubuntu).
Warnings
- breaking As of v4.0.0, `iltorb` (Brotli) and `node-zopfli-es` (Zopfli) are now peer dependencies. They are no longer installed automatically.
- gotcha The package `shrink-ray-current` is a maintained fork of the original `shrink-ray`, which is abandoned. Ensure you install and import the correct package.
- gotcha Native module dependencies (`iltorb` and `node-zopfli-es`) require a Node.js native build toolchain (e.g., node-gyp, Python, C++ compiler) to install successfully. If these fail to build, `shrink-ray-current` will fall back to using only gzip/deflate.
- gotcha Zopfli compression is specifically noted for asynchronous compression of *static assets only* (e.g., content with ETags). It is not applied to all outgoing responses by default.
Install
-
npm install shrink-ray-current -
yarn add shrink-ray-current -
pnpm add shrink-ray-current
Imports
- shrinkRay
const shrinkRay = require('shrink-ray');const shrinkRay = require('shrink-ray-current'); - shrinkRay
import { shrinkRay } from 'shrink-ray-current';import shrinkRay from 'shrink-ray-current';
- Options
import type { Options } from 'shrink-ray-current';
Quickstart
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).');
});