A tiny, lightweight JavaScript API to query mime-db (Media-type / MIME-type database).
npm install mime-db-liteA 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.
importhtml
`For Node.js:
Install as
`bash
npm install mime-db-lite
`Import as
`javascript
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.
`javascript
const db = new DB();
`To use an in-memory cache, instantiate as:
`javascript
const db = new DB({ cacheMaxEntries: });
`
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:
`javascript
const db = new DB({ fetch: custFetch });
`or, use an arrow function
`javascript
const db = new DB({
fetch: async (url) => {
// Your code here
}
});
`
API
$3
Returns a promise that fulfills with an array of file-extensions.mimeTypeString representing a MIME-type with structure:
`
type/subtypetype/subtype;parameter=value
`š The returned promise is rejected with
Not Found error if the provided mimeType is unavailable in mime-db.Examples:
`javascript
console.log(await db.mimeToExtensions('application/mp4'));
// Prints [ 'mp4', 'mpg4', 'mp4s', 'm4p' ]console.log(await db.getExtensions('application/javascript; charset=utf-8'));
// Prints [ 'js' ]
`$3
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.extensionString 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:
`javascript
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' ]
`$3
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:
`javascript
console.log(await db.getType('.deb'));
// Prints 'application/x-debian-package'
`$3
Returns a promise that fulfills with the mime-db data object for the provided MIME-type.mimeTypeString 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:
`javascript
console.log(JSON.stringify(await db.query('application/javascript')));
// Prints '{"source":"apache","charset":"UTF-8","compressible":true,"extensions":["js"]}'
``If you like this project, you can show your appreciation by
- giving it a star ā
- sponsoring me through š

Thank you š.