CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mime

A comprehensive library for mime-type mapping

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

mime-operations.mddocs/

MIME Type Operations

Core functionality for resolving MIME types from file paths and extensions, with robust path handling and comprehensive type database coverage.

Capabilities

Get MIME Type from Path

Determines the MIME type for a given file path or extension with intelligent path parsing.

/**
 * Get mime type associated with an extension or file path
 * @param path - File path or extension to look up
 * @returns MIME type string or null if not found
 */
function getType(path: string): string | null;

Usage Examples:

import mime from "mime";

// File extensions
mime.getType("js");                    // "text/javascript"
mime.getType("json");                  // "application/json"
mime.getType("png");                   // "image/png"

// File paths (various formats)
mime.getType("script.js");             // "text/javascript"
mime.getType("dir/data.json");         // "application/json"
mime.getType("dir\\image.png");        // "image/png" (Windows paths)
mime.getType(".hidden.txt");           // "text/plain"
mime.getType("file.tar.gz");           // "application/gzip"

// Returns null for unrecognized or invalid inputs
mime.getType("unknown_extension");     // null
mime.getType("file_without_extension"); // null
mime.getType("path/file");             // null (no extension)

Path Processing Rules:

  • Removes directory path components, only considers filename
  • Extracts extension after the last dot
  • Case-insensitive matching
  • Handles both Unix (/) and Windows (\) path separators
  • Returns null for files without extensions in directory paths

Get Default Extension from MIME Type

Retrieves the default (primary) file extension for a given MIME type.

/**
 * Get default file extension associated with a mime type
 * @param type - MIME type to look up
 * @returns Primary extension string or null if not found
 */
function getExtension(type: string): string | null;

Usage Examples:

import mime from "mime";

// Standard MIME types
mime.getExtension("text/plain");                    // "txt"
mime.getExtension("application/json");              // "json"
mime.getExtension("image/jpeg");                    // "jpeg"
mime.getExtension("application/javascript");        // "js"

// MIME types with parameters (automatically stripped)
mime.getExtension("text/html; charset=utf-8");      // "html"
mime.getExtension("application/json; charset=utf-8"); // "json"

// Returns null for unrecognized types
mime.getExtension("unknown/type");                  // null
mime.getExtension("invalid-mime-type");             // null

Parameter Handling:

  • Automatically removes HTTP header parameters (charset, boundary, etc.)
  • Trims whitespace from type string
  • Case-insensitive type matching

Get All Extensions from MIME Type

Retrieves all file extensions associated with a given MIME type (new in v4.0).

/**
 * Get all file extensions associated with a mime type
 * @param type - MIME type to look up
 * @returns Set of all extensions or null if not found
 */
function getAllExtensions(type: string): Set<string> | null;

Usage Examples:

import mime from "mime";

// Multiple extensions for common types
mime.getAllExtensions("image/jpeg");      // Set(3) { "jpeg", "jpg", "jpe" }
mime.getAllExtensions("text/javascript"); // Set(2) { "js", "mjs" }
mime.getAllExtensions("application/zip"); // Set(1) { "zip" }

// Single extension types
mime.getAllExtensions("text/plain");      // Set(1) { "txt" }

// Returns null for unrecognized types
mime.getAllExtensions("unknown/type");    // null

Set Iteration:

const extensions = mime.getAllExtensions("image/jpeg");
if (extensions) {
  for (const ext of extensions) {
    console.log(ext); // "jpeg", "jpg", "jpe"
  }
  
  // Convert to array if needed
  const extArray = Array.from(extensions); // ["jpeg", "jpg", "jpe"]
}

Version Differences

Full vs Lite Versions

// Full version - includes all MIME types (800+ types, 1000+ extensions)
import mime from "mime";

// Lite version - excludes unofficial types (prs.*, x-*, vnd.*)
import mime from "mime/lite";

Type Coverage:

  • Full version: Standard types + vendor-specific + experimental types
  • Lite version: Only standard, widely-supported MIME types
  • Both versions have identical API signatures and behavior
  • Choose lite version for smaller bundle size, full version for comprehensive coverage

Error Handling

All methods handle invalid input gracefully:

// Invalid inputs return null, never throw
mime.getType(null);        // null
mime.getType(undefined);   // null
mime.getType(123);         // null
mime.getExtension(null);   // null
mime.getAllExtensions(""); // null

Type Safety

// Return types are explicitly nullable
const type: string | null = mime.getType("file.js");
const ext: string | null = mime.getExtension("text/plain");
const exts: Set<string> | null = mime.getAllExtensions("image/png");

// Always check for null before use
if (type !== null) {
  console.log(`MIME type: ${type}`);
}

docs

cli.md

custom-instances.md

index.md

mime-operations.md

tile.json