or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Compressible

A utility library for determining whether HTTP Content-Type and MIME types are compressible. The package provides intelligent compression decisions by checking MIME types against the comprehensive mime-db database and applying fallback patterns for common compressible types.

Package Information

  • Package Name: compressible
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install compressible

Core Imports

const compressible = require('compressible');

Basic Usage

const compressible = require('compressible');

// Check various content types
console.log(compressible('text/html'));        // => true
console.log(compressible('image/png'));        // => false
console.log(compressible('application/json')); // => true
console.log(compressible('video/mp4'));        // => false

// Handles Content-Type strings with parameters
console.log(compressible('text/html; charset=utf-8'));        // => true
console.log(compressible('application/json; charset=utf-8')); // => true

// Case insensitive
console.log(compressible('TEXT/HTML'));       // => true
console.log(compressible('Application/JSON')); // => true

// Unknown or uncertain types return undefined
console.log(compressible('application/x-unknown')); // => undefined

// Invalid input returns false
console.log(compressible(null));      // => false
console.log(compressible(undefined)); // => false
console.log(compressible(42));        // => false

Architecture

The compressible package follows a simple two-tier decision architecture:

  • Primary Lookup: Authoritative compression data from the comprehensive mime-db database
  • Fallback Patterns: Regex-based pattern matching for common compressible types (text/*, /+json, /+text, /+xml)
  • Input Processing: Parameter stripping and case normalization for robust Content-Type handling
  • Graceful Degradation: Returns undefined for uncertain cases rather than making assumptions

This design ensures accurate compression decisions while providing predictable fallback behavior for edge cases.

Capabilities

Content-Type Compression Check

Determines if a given MIME type or Content-Type string is suitable for HTTP compression algorithms like gzip or brotli.

/**
 * Checks if a type is compressible.
 * 
 * @param {string} type - MIME type or Content-Type string to check
 * @returns {boolean|undefined} true if compressible, false if not compressible, undefined if uncertain
 */
function compressible(type)

Parameters:

  • type (string): A MIME type (e.g., "text/html") or Content-Type string with parameters (e.g., "text/html; charset=utf-8")

Returns:

  • true: The type is known to be compressible
  • false: The type is definitely not compressible, or the input is invalid
  • undefined: The compressibility is uncertain (not explicitly defined in mime-db and doesn't match fallback patterns)

Input Processing:

  • Automatically strips parameters from Content-Type strings
  • Normalizes MIME types to lowercase
  • Handles whitespace around parameters gracefully

Compression Logic:

  1. Database Lookup: First checks the mime-db database for explicit compressibility information
  2. Fallback Patterns: If not in database, applies these rules:
    • text/* types → true (e.g., text/html, text/plain, text/css)
    • */*+json types → true (e.g., application/hal+json)
    • */*+text types → true (e.g., application/something+text)
    • */*+xml types → true (e.g., application/soap+xml)
  3. Unknown Types: Returns undefined for types that don't match any pattern

Error Handling:

  • Invalid input types (null, undefined, numbers, booleans) return false
  • Empty strings return false
  • Malformed Content-Type strings are handled gracefully

Usage Examples:

// Common web content types
compressible('text/html');               // => true
compressible('text/css');                // => true
compressible('text/javascript');         // => true
compressible('application/json');        // => true
compressible('application/xml');         // => true

// Images and media (typically not compressible)
compressible('image/jpeg');              // => false
compressible('image/png');               // => false
compressible('video/mp4');               // => false
compressible('audio/mpeg');              // => false

// Structured data formats
compressible('application/hal+json');    // => true
compressible('application/soap+xml');    // => true
compressible('application/rss+xml');     // => true

// Content-Type with parameters
compressible('text/html; charset=utf-8');           // => true
compressible('application/json; charset=utf-8');    // => true

// Unknown or custom types
compressible('application/x-custom');    // => undefined
compressible('custom/type');             // => undefined

// Invalid inputs
compressible(null);                      // => false
compressible(undefined);                 // => false
compressible('');                        // => false
compressible(42);                        // => false

Dependencies

  • mime-db (>= 1.43.0 < 2): Comprehensive database of MIME type information used for authoritative compression decisions

Common Use Cases

  • Web Servers: Determine which responses to compress before sending to clients
  • Proxy Servers: Make intelligent compression decisions for upstream responses
  • CDN Configuration: Automatically configure compression rules based on content types
  • Build Tools: Optimize asset compression in build pipelines
  • HTTP Middleware: Add compression logic to Express.js or similar frameworks
  • API Gateways: Apply compression selectively based on response content types