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

cli.mddocs/

Command Line Interface

Shell interface for MIME type operations, supporting both type-to-extension and extension-to-type lookups with comprehensive flag support.

Installation and Access

The mime command is automatically available after installing the package:

npm install mime
# or
npm install -g mime  # for global access

Capabilities

Basic MIME Type Lookup

Get MIME type for a file path or extension.

mime [path_or_extension]

Usage Examples:

# File extensions
mime js                          # text/javascript
mime json                        # application/json
mime png                         # image/png

# File paths
mime script.js                   # text/javascript
mime data/config.json           # application/json
mime images/photo.jpg           # image/jpeg
mime package.tar.gz             # application/gzip

# Complex paths
mime "path with spaces/file.txt" # text/plain
mime ./src/index.ts             # text/typescript

Reverse Lookup (Type to Extension)

Get default extension for a MIME type using the --reverse flag.

mime --reverse [mime_type]
mime -r [mime_type]

Usage Examples:

# Standard MIME types
mime -r text/plain              # txt
mime -r application/json        # json
mime -r image/jpeg              # jpeg
mime -r text/javascript         # js

# MIME types with parameters (automatically handled)
mime -r "text/html; charset=utf-8"  # html
mime --reverse "application/json; charset=utf-8"  # json

Version Information

Display package version information.

mime --version
mime -v
mime --v

Output Example:

4.0.7

Package Name

Display the package name.

mime --name  
mime -n
mime --n

Output Example:

mime

Help Information

Display comprehensive usage help.

mime --help
mime -h
mime --h

Help Output:

mime - A comprehensive library for mime-type mapping

Usage:

  mime [flags] [path_or_extension]

  Flags:
    --help, -h                     Show this message
    --version, -v                  Display the version
    --name, -n                     Print the name of the program
    --reverse, -r                  Print the extension of the mime type

  Note: the command will exit after it executes if a command is specified
  The path_or_extension is the path to the file or the extension of the file.

  Examples:
    mime --help
    mime --version
    mime --name
    mime -v
    mime --reverse application/text
    mime src/log.js
    mime new.py
    mime foo.sh

Exit Codes

The CLI uses standard exit codes for scripting integration:

# Success (type/extension found)
mime script.js                  # Exit code: 0, Output: text/javascript
mime -r text/plain              # Exit code: 0, Output: txt

# Failure (type/extension not found)
mime unknown_extension          # Exit code: 1, No output
mime -r unknown/mimetype        # Exit code: 1, No output

# Info commands always succeed
mime --version                  # Exit code: 0
mime --help                     # Exit code: 0

Scripting Examples

Shell Script Integration

#!/bin/bash

# Check if file has expected MIME type
file="data.json"
expected="application/json"
actual=$(mime "$file")

if [ "$actual" = "$expected" ]; then
    echo "File type correct: $actual"
else
    echo "Unexpected file type: $actual (expected $expected)"
    exit 1
fi

Batch Processing

# Process all JavaScript files in a directory
for file in src/*.js; do
    type=$(mime "$file")
    echo "$file: $type"
done

# Find files by MIME type
find . -type f | while read file; do
    if [ "$(mime "$file" 2>/dev/null)" = "text/javascript" ]; then
        echo "JavaScript file: $file"
    fi
done

Conditional Processing

# Process file based on MIME type
file="input.data"
mime_type=$(mime "$file")

case "$mime_type" in
    "application/json")
        echo "Processing JSON file..."
        jq . "$file"
        ;;
    "text/plain")
        echo "Processing text file..."
        cat "$file"
        ;;
    "image/"*)
        echo "Processing image file..."
        # image processing logic
        ;;
    *)
        echo "Unknown file type: $mime_type"
        exit 1
        ;;
esac

Validate File Extensions

# Check if file extension matches MIME type
check_file_consistency() {
    local file="$1"
    local mime_type=$(mime "$file")
    local expected_ext=$(mime -r "$mime_type")
    local actual_ext="${file##*.}"
    
    if [ "$actual_ext" = "$expected_ext" ]; then
        echo "✓ $file: consistent ($mime_type)"
    else
        echo "✗ $file: extension '$actual_ext' doesn't match type '$mime_type' (expected '$expected_ext')"
    fi
}

# Usage
check_file_consistency "script.js"      # ✓ script.js: consistent (text/javascript)
check_file_consistency "data.js"        # ✗ data.js: extension 'js' doesn't match type 'application/json' (expected 'json')

Error Handling

Invalid Arguments

# No arguments (tries to get type of empty string)
mime                            # Exit code: 1, No output

# Invalid file paths (no extension detected)
mime "file_without_extension"   # Exit code: 1, No output
mime "path/to/directory/"       # Exit code: 1, No output

Reverse Lookup Errors

# Invalid MIME type
mime -r "not-a-mime-type"       # Exit code: 1, No output
mime -r ""                      # Exit code: 1, No output

# Missing argument for reverse lookup
mime -r                         # Uses last argument as MIME type

Silent Failure Mode

The CLI fails silently (no error messages) to facilitate clean scripting:

# Check if command succeeded without capturing output
if mime "unknown.ext" >/dev/null 2>&1; then
    echo "MIME type found"
else
    echo "MIME type not found"
fi

Implementation Details

The CLI is implemented as a thin wrapper around the main MIME library:

  • Binary: bin/cli.js
  • Implementation: src/mime_cli.ts
  • Runtime: Node.js with ES modules
  • Dependencies: Only the core mime library

The CLI processes arguments sequentially and exits immediately after executing any flag command, making it suitable for both interactive use and shell scripting.

docs

cli.md

custom-instances.md

index.md

mime-operations.md

tile.json