Install mrmime via npm
mainInstall mrmime as a dependency using npm to use it in your project.
$ npm install --save mrmimerepository·main·Indexed 43 days ago
https://github.com/lukeed/mrmimeA 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.
Install mrmime as a dependency using npm to use it in your project.
$ npm install --save mrmimeThe bin/index.ts file is a build script used to generate the distribution files for the mrmime package. It performs the following steps:
mime-db to collect extensions and their corresponding MIME types.iana > apache > nginx. If sources are equal, it prefers the shorter MIME type string.[/](x-|vnd\.).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)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.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"The lookup(input) function returns the MIME type associated with a given extension or filename.
Behavioral Details:
input is automatically cast to a string, lowercased, and trimmed.undefined.Returns: string | undefined
import { lookup } from 'mrmime';
lookup('txt'); //=> "text/plain"
lookup('.txt'); //=> "text/plain"
lookup('a.txt'); //=> "text/plain"
lookup('.xyz'); //=> undefinedThe 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'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