mrmime

repository·main·Indexed 43 days ago

https://github.com/lukeed/mrmime

A tiny (2.8kB) and fast utility for retrieving MIME types from file extensions or filenames. Version 2.0.1 provides a lookup() function to resolve MIME types and an exported mimes dictionary for customization.

Tokens
859
Snippets
5
Records
6
Agent score
55%

What's inside mrmime

  1. How the mrmime build process works

    main

    The bin/index.ts file is a build script used to generate the distribution files for the mrmime package. It performs the following steps:

    1. MIME Data Aggregation: It iterates through the mime-db to collect extensions and their corresponding MIME types.
    2. Conflict Resolution: When multiple MIME types are associated with the same extension, it uses a priority-based comparison logic to decide which to keep. The priority order is: iana > apache > nginx. If sources are equal, it prefers the shorter MIME type string.
    3. Filtering: It ignores experimental or vendor-specific MIME types matching the regex [/](x-|vnd\.).
    4. Code Generation: It reads a template from src/$index.ts, replaces a placeholder {} with the generated MIME mapping, and produces:
      • index.mjs (ESM)
      • index.js (CJS)
      • deno/mod.ts (Deno-compatible TypeScript)
    5. Deno Support: It creates a deno/ directory, copies the readme.md, and generates a typed version of the ESM module for Deno, attempting to run deno fmt if not in a CI environment.
  2. Customize the MIME dictionary

    main

    You can extend or override the default MIME type dictionary by modifying the exported mimes object. This allows you to add support for custom or experimental extensions.

    import { lookup, mimes } from 'mrmime';
    
    // Add extension to dictionary
    mimes['xyz'] = 'hello/world';
    
    lookup('xyz'); //=> "hello/world"
  3. Get a MIME type with lookup()

    main

    The lookup(input) function returns the MIME type associated with a given extension or filename.

    Behavioral Details:

    • The input is automatically cast to a string, lowercased, and trimmed.
    • If a full filename or filepath is provided, the function extracts and uses only the extension.
    • If no matching MIME type is found, it returns undefined.

    Returns: string | undefined

    import { lookup } from 'mrmime';
    
    lookup('txt');        //=> "text/plain"
    lookup('.txt');       //=> "text/plain"
    lookup('a.txt');      //=> "text/plain"
    lookup('.xyz');       //=> undefined
  4. Access the mimes dictionary

    main

    The mimes constant is a Record<string, string> that contains a mapping of MIME types to their corresponding file extensions. You can use this to inspect available mappings or iterate through the dictionary.

    import { mimes } from 'mrmime';
    
    // Example: Accessing a specific mapping
    const mimeType = mimes['json']; // 'application/json'
  5. Look up a MIME type by extension using lookup()

    main

    The lookup function takes a file extension as a string and returns the corresponding MIME type. If no mapping is found for the provided extension, it returns undefined.

    import { lookup } from 'mrmime';
    
    const mime = lookup('json'); // 'application/json'
    const unknown = lookup('unknown-ext'); // undefined