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 🙏

© 2026 – Pkg Stats / Ryan Hefner

smart-compress-middleware

v1.0.1

Published

Intelligent Express middleware for dynamic compression with pluggable AI/ML strategies

Readme

smart-compress-middleware

npm version License: MIT

Intelligent Express middleware developed my Maheen Meshram for dynamic compression with pluggable AI/ML strategies. Automatically selects the best compression algorithm (gzip, Brotli, or none) based on payload size, content type, and request headers.

Features

  • 🚀 Dynamic compression algorithm selection
  • 🧠 Pluggable decision strategies (rule-based, AI, adaptive, hybrid)
  • 📊 Real-time dashboard with WebSocket updates
  • 🌊 Streaming compression (memory-efficient for large payloads)
  • 📦 Buffered compression for small/medium responses
  • ⚙️ Configurable size thresholds
  • 📈 Performance feedback loop for learning
  • 🛡️ Graceful error handling with automatic fallback
  • 🔌 Zero dependencies (peer dependency: Express)
  • 📝 TypeScript definitions included

Installation

npm install smart-compress-middleware

Quick Start

const express = require('express');
const smartCompress = require('smart-compress-middleware');

const app = express();

// Basic usage with defaults
app.use(smartCompress());

// With custom options
app.use(smartCompress({
  enabled: true,
  smallThreshold: 1024,      // Don't compress < 1KB
  largeThreshold: 5120,       // Use Brotli for > 5KB
  strategy: 'adaptive',       // 'rule-based', 'ai', 'adaptive', 'hybrid'
  logging: true,
  benchmark: true
}));

app.get('/api/data', (req, res) => {
  res.json({ message: 'This will be compressed automatically!' });
});

app.listen(3000);

Dashboard Setup

Add a real-time compression dashboard to monitor performance:

const express = require('express');
const http = require('http');
const smartCompress = require('smart-compress-middleware');

const app = express();
const server = http.createServer(app);

// Apply middleware
app.use(smartCompress());

// Setup dashboard (requires HTTP server for WebSocket)
smartCompress.setupDashboard(app, server, {
  path: '/dashboard'  // Access at http://localhost:3000/dashboard
});

server.listen(3000);

Configuration Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | enabled | boolean | true | Enable/disable compression | | smallThreshold | number | 1024 | Min size (bytes) for compression | | largeThreshold | number | 5120 | Size (bytes) to prefer Brotli | | strategy | string | 'rule-based' | Decision strategy to use | | aiModelEndpoint | string | null | External AI model endpoint | | logging | boolean | true | Enable console logging | | benchmark | boolean | true | Enable benchmark logging |

Decision Strategies

1. Rule-Based (Default)

Traditional size-based thresholds. Fast and predictable.

app.use(smartCompress({ strategy: 'rule-based' }));
  • < 1KB → no compression
  • 1KB-5KB → gzip
  • > 5KB → Brotli (if supported)

2. AI-Based

Machine learning predictions with historical learning.

app.use(smartCompress({ 
  strategy: 'ai',
  aiModelEndpoint: 'http://your-ml-service/predict'  // Optional
}));

Features:

  • Learns from compression performance
  • Considers multiple factors (size, content type, path)
  • Automatic fallback to rule-based on failure
  • Mock implementation ready for real ML models

3. Adaptive

Learns from real-time performance metrics.

app.use(smartCompress({ strategy: 'adaptive' }));

Features:

  • Tracks compression ratio and speed by payload size
  • Adjusts decisions based on historical performance
  • No external dependencies
  • Self-optimizing over time

4. Hybrid

Combines multiple strategies with voting.

app.use(smartCompress({ strategy: 'hybrid' }));

Features:

  • Weighted, unanimous, or majority voting
  • Leverages strengths of different approaches
  • Maximum reliability

Runtime Strategy Switching

Switch strategies without restarting:

const smartCompress = require('smart-compress-middleware');

// Switch strategy
smartCompress.engine.setStrategy('adaptive');

// Get current strategy
const current = smartCompress.engine.getCurrentStrategyName();

// List available strategies
const strategies = smartCompress.engine.listStrategies();

// Get statistics
const stats = smartCompress.engine.getStats();

Custom Strategies

Create your own decision strategy:

const { strategies } = require('smart-compress-middleware');

class CustomStrategy extends strategies.BaseStrategy {
  async decide(context) {
    const { size, acceptEncoding, contentType, path, isStreaming } = context;
    
    // Your custom logic here
    
    return {
      algorithm: 'gzip',  // 'gzip', 'brotli', or 'none'
      reason: 'Custom decision logic',
      confidence: 0.85    // Optional: 0-1 confidence score
    };
  }
  
  // Optional: Implement learning
  learn(feedback) {
    // Update internal state based on performance
  }
  
  getName() {
    return 'custom';
  }
}

// Register and use
const smartCompress = require('smart-compress-middleware');
smartCompress.engine.registerStrategy('custom', new CustomStrategy());
smartCompress.engine.setStrategy('custom');

API Routes

When dashboard is enabled, these routes are available:

# Get current strategy
GET /smart-compress-api/strategy/current

# Get strategy statistics
GET /smart-compress-api/strategy/stats

# Switch strategy
POST /smart-compress-api/strategy/switch
Body: { "strategy": "adaptive" }

# List all strategies
GET /smart-compress-api/strategy/list

# Dashboard stats
GET /smart-compress-api/dashboard/stats

TypeScript Support

Full TypeScript definitions included:

import smartCompress, { 
  CompressionOptions, 
  DecisionEngine,
  strategies 
} from 'smart-compress-middleware';

const options: CompressionOptions = {
  enabled: true,
  strategy: 'adaptive',
  smallThreshold: 1024
};

app.use(smartCompress(options));

Performance

  • Non-blocking streaming compression
  • Minimal memory overhead (no full payload buffering)
  • Efficient stream handling with backpressure support
  • Automatic detection of buffered vs streaming responses

Compression Logic

The middleware automatically:

  • Respects Accept-Encoding headers
  • Sets Content-Encoding headers
  • Adds X-Compression-Algorithm header
  • Handles errors gracefully (falls back to uncompressed)
  • Never crashes your server

Dashboard Features

The live dashboard shows:

  1. Real-time Statistics

    • Total requests processed
    • Average compression ratio
    • Average compression time
    • Total bytes saved
  2. Visual Charts

    • Compression ratio by algorithm
    • Request count per algorithm
  3. Live Activity Log

    • Recent compression events
    • Color-coded by algorithm
    • Detailed metrics per request
  4. Interactive Controls

    • Clear logs
    • Test endpoints
    • Auto-reconnecting WebSocket

Examples

See the /example directory for a complete working example:

git clone https://github.com/yourusername/smart-compress-middleware.git
cd smart-compress-middleware
npm install
npm start

Visit http://localhost:3000/dashboard to see the dashboard in action.

Testing

npm test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT © 2026

Links

Changelog

1.0.1

  • Initial release
  • Rule-based, AI, adaptive, and hybrid strategies
  • Real-time dashboard
  • Streaming compression support
  • TypeScript definitions