Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mime-db-cdn

SomajitDey595MIT4.0.1-1.54.0

A mime-db equivalent to serve data for each entry separately over CDN. Enables small, selective download for any given query.

mime, types, mime-db, cdn, browser, frontend, dynamic-import, media-type, mime-type, file-extension, file-type, extensions, database

readme

💁 mime-db-cdn

NPM Version Build Test Publish NPM package

🚀 A mime-db equivalent that lets you download just the data you need via multiple CDNs. Unlike mime-db, one no more needs to download the entire database file at once.

🚀 Mirrors the complete mime-db database, including the unofficial MIME-types (prs.*, x-*, vnd.*).

🚀 Additionally includes file-extension => MIME-type(s) reverse lookup data, with the MIME-types sorted in descending order of importance.

🚀 Also offers a portable JavaScript SDK (ESM) to access the database through dynamic imports, providing memory-efficiency for both browsers and server-side runtimes.

Motivation

mime-db packages the entire MIME-types database into a JSON file. Using mime-db directly, therefore, entails downloading the entire database at once, albeit from a CDN. If you just need to query a handful of MIME-types or file-extensions, downloading the entire database followed by loading and retaining it in memory would be an overkill. This problem persists with popular packages based on mime-db, such as mime or mime-types, which, in order to function, must first store the entire database locally on the user's machine.

The current project solves this problem by fragmenting mime-db into tiny JSON files, each storing data only for one of the available MIME-types or file-extensions. To understand the file-structure, explore the directories in the database branch. Each of these JSON files is readily downloadable using any of the free CDNs that serve GitHub or npm contents, e.g.

Usage

This section presents CDN links to download just the desired data from our MIME-types database using any HTTP client, e.g.

  • any browser,
  • curl or wget in Linux,
  • the Fetch API in JavaScript etc..

👉 For the JS-SDK usage, see the SDK section below.

👉 <ref> below refers to either

👉 For simplicity, only 2 CDNs (jsdelivr and unpkg) are explicitly mentioned. It is trivial to construct similar URLs for other available CDNs.

MIME-type to file-extension(s)

Download as JSON:

From jsdelivr,

https://cdn.jsdelivr.net/gh/somajitdey/mime-db-cdn@<ref>/mime-types/<mime-type>/data.json
https://cdn.jsdelivr.net/npm/mime-db-cdn@<tag>/mime-types/<mime-type>/data.json

From unpkg,

https://unpkg.com/mime-db-cdn@<tag>/mime-types/<mime-type>/data.json

Above, replace <mime-type> with your chosen MIME-type e.g. image/jpeg.

👉 Apart from an extensions array, the JSON for a MIME-type also contains these relevant data. See examples below.

File-extension to MIME-type(s)

Download as JSON:

From jsdelivr,

https://cdn.jsdelivr.net/gh/somajitdey/mime-db-cdn@<ref>/extensions/<extension>/data.json
https://cdn.jsdelivr.net/npm/mime-db-cdn@<tag>/extensions/<extension>/data.json

From unpkg,

https://unpkg.com/mime-db-cdn@<tag>/extensions/<extension>/data.json

Above, replace <extension> with your chosen extension e.g. jpg.

👉 The JSON contains an array of mimeTypes. Note that this array of MIME-types is sorted in descending order of importance according to the same logic used by mime-types. See examples below. If only one MIME-type is required, simply choose the first element from the array!

Content-Type HTTP header

From jsdelivr,

https://cdn.jsdelivr.net/gh/somajitdey/mime-db-cdn@<ref>/file-types/type.<extension>
https://cdn.jsdelivr.net/npm/mime-db-cdn@<tag>/file-types/type.<extension>

From unpkg,

https://unpkg.com/mime-db-cdn@<tag>/file-types/type.<extension>

Content-Type header received for a GET request to the above URLs might contain the desired MIME-type, as provided by the CDN provider.

👉 The above links also return the actual MIME-types as text in the response body. For extensions shared by multiple MIME-types, one MIME-type is listed per line. Note that these MIME-types are not sorted in any particular order. If a sorted list is required, download the JSON.

Versioning

The source mime-db release, from which our database is built, is always recorded as a semver pre-release.

Format: <our version>-<source mime-db version>

For example, our version 4.0.0-1.54.0 is equivalent to the mime-db release v1.54.0.

Examples

Just click on any of the links in this section.

👉 Data for image/jpeg from mime-db release v1.54.0 =>

https://cdn.jsdelivr.net/gh/somajitdey/mime-db-cdn@4.0.0-1.54.0/mime-types/image/jpeg/data.json

👉 MIME-type(s) for file-extension exe=>

https://unpkg.com/mime-db-cdn@4.0.0-1.54.0/extensions/exe/data.json

👉 Proper Content-Type for file-extensions json and png =>

https://unpkg.com/mime-db-cdn@4.0.0-1.54.0/file-types/type.json

https://unpkg.com/mime-db-cdn@4.0.0-1.54.0/file-types/type.png

JavaScript SDK

Our JS-SDK is designed to be memory-efficient regardless of how big the original MIME-type database ever becomes. This is acheived through dynamic imports, viz. importing data as and when needed.

For browsers, this means more requests to the CDNs in return for low memory footprint. Thanks to caching by the browsers, however, the increased CDN requests might ultimately be a non-issue.

For server-side runtimes like Node, dynamically importing tiny JSONs avoids complexities of reading and parsing a huge JSON database using streams.

Install and Import

For browsers:

<script type="module">
    import { mimeToExtensions, extensionToMimes } from 'https://cdn.jsdelivr.net/npm/mime-db-cdn@4.0.0-1.54.0/index.min.js';

    // Your code here ...
</script>

For Node.js:

Install as

npm install mime-db-cdn

Import as

import { mimeToExtensions, extensionToMimes } from 'mime-db-cdn';

API

mimeToExtensions(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 mimeToExtensions('application/mp4'));
// Prints [ 'mp4', 'mpg4', 'mp4s', 'm4p' ]

console.log(await mimeToExtensions('application/javascript; charset=utf-8'));
// Prints [ 'js' ]

extensionToMimes(extension)

Returns a promise that fulfills with an array of MIME-types.

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 extensionToMimes('dir/subdir/path.version.js'));

console.log(await extensionToMimes('js'));

console.log(await extensionToMimes('.js'));

// Prints [ 'application/javascript', 'text/javascript' ]

Alternatives

Reliability of this database

The upstream mime-db is checked daily for updates. If a new release is available upstream, this database is built afresh from that release. A new git-tag, and an npm package are then released, with appropriate versioning.

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

Sponsor

Thank you 💚.