mime-db-lite

A tiny, lightweight, portable JavaScript module to query the complete mime-db (a popular Media-type / MIME-type / Content-type database). Data is actually sourced from mime-db-cdn, which mirrors mime-db with added features.
🚀 Consumes less run-time memory by fetching data as and when needed, with optional caching. Ideal for low footprint applications.
🚀 Cross-platform: Runs on browsers, server-side (e.g. NodeJS) or serverless (e.g. Cloudflare Workers V8 isolates).
🚀 Standard: ESM.
🚀 Promise-based; exports async
methods.
🚀 Accepts custom fetch()
method, if any.
Install and import
For browsers:
<script type="module">
import DB from 'https://cdn.jsdelivr.net/npm/mime-db-lite@2.0.0/index.min.js';
// Above, replace 2.0.0 with the actual version you'd be using, or use 'latest'
// Your code here ...
</script>
For Node.js:
Install as
npm install mime-db-lite
Import as
import DB from 'mime-db-lite';
Instantiation, custom fetch()
and LRU cache
Create an instance of the imported DB class to use the fetch() API provided by the runtime and forgo caching.
👉 For browsers, the native fetch()
will still use the HTTP-cache.
const db = new DB();
To use an in-memory cache, instantiate as:
const db = new DB({ cacheMaxEntries: <Integer> });
<Integer>
denotes the maximum number of entries the cache can store, beyond which the LRU entries will be evicted to make just enough space.
👉 Using an LRU cache claims more run-time memory in return for faster lookups. For browsers this may be unnecessary, because browser-native fetch()
already uses HTTP-cache by default.
To use a custom fetch()
method, e.g. async function custFetch (url) { ... }
, instantiate as:
const db = new DB({ fetch: custFetch });
or, use an arrow function
const db = new DB({
fetch: async (url) => {
// Your code here
}
});
API
db.mimeToExtensions(mimeType)
or its alias db.getExtensions(mimeType)
Returns a promise that fulfills with an array of file-extensions.
mimeType
String representing a MIME-type with structure:
type/subtype
type/subtype;parameter=value
👉 The returned promise is rejected with Not Found
error if the provided mimeType
is unavailable in mime-db.
Examples:
console.log(await db.mimeToExtensions('application/mp4'));
// Prints [ 'mp4', 'mpg4', 'mp4s', 'm4p' ]
console.log(await db.getExtensions('application/javascript; charset=utf-8'));
// Prints [ 'js' ]
db.extensionToMimes(extension)
or its alias db.getTypes(extension)
Returns a promise that fulfills with an array of MIME-types. Note that this array of MIME-types is sorted in descending order of importance according to the same logic used by mime-types. If only one MIME-type is required, simply choose the first element from the array, or use the db.getType()
method.
extension
String representing either of
- path, e.g.
dir/subdir/file.ext
- file-extension with or without the leading dot, e.g.
mp4
, .mp4
- file-name, e.g.
file.mp4
, file.version.1.2.0.mp4
👉 The returned promise is rejected with Not Found
error if the provided mimeType
is unavailable in mime-db.
Examples:
console.log(await db.extensionToMimes('dir/subdir/path.version.js'));
console.log(await db.getTypes('js'));
console.log(await db.getTypes('.js'));
// Prints [ 'application/javascript', 'text/javascript' ]
db.extensionToMime(extension)
or its alias db.getType(extension)
Same as db.getTypes()
above but the returned promise resolves with only a single MIME-type instead of an array. The returned MIME-type has the highest score according to the logic used by mime-types.
Examples:
console.log(await db.getType('.deb'));
// Prints 'application/x-debian-package'
db.query(mimeType)
Returns a promise that fulfills with the mime-db data object for the provided MIME-type.
mimeType
String representing a MIME-type in the form: type/subtype
👉 The returned promise is rejected with Not Found
error if the provided mimeType
is unavailable in mime-db.
Examples:
console.log(JSON.stringify(await db.query('application/javascript')));
// Prints '{"source":"apache","charset":"UTF-8","compressible":true,"extensions":["js"]}'
DB.mimeDbCdnVersion
A constant that stores the mime-db-cdn release version data is fetched from.
DB.cdnBase
A constant that stores the CDN via which data is accessed.
Contribute
To register new media types in the database, contribute directly to mime-db.
If you like this project, you can show your appreciation by

Thank you 💚.