Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The mime npm package is a utility for handling and transforming file MIME types. It provides functionality to lookup the MIME type for a file based on its extension, determine the default extension for a MIME type, and define custom MIME type mappings.
Lookup MIME type by extension
This feature allows you to get the MIME type for a given file extension. In the code sample, we are looking up the MIME type for a '.json' file, which returns 'application/json'.
const mime = require('mime');
const mimeType = mime.getType('json'); // 'application/json'
Lookup extension by MIME type
This feature enables you to find the default file extension for a given MIME type. In the code sample, we are finding the extension for the MIME type 'application/json', which returns 'json'.
const mime = require('mime');
const extension = mime.getExtension('application/json'); // 'json'
Define custom MIME type mappings
This feature allows you to define custom MIME type mappings. In the code sample, we are adding a custom MIME type 'text/x-some-format' with multiple extensions. The second argument 'true' indicates that the custom types should take precedence over the built-in types.
const mime = require('mime');
mime.define({ 'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'] }, true);
The 'mime-types' package is similar to 'mime' in that it provides MIME type lookup based on file extension and vice versa. It is based on the mime-db dataset, which is a comprehensive dataset of MIME types compiled from various sources. Compared to 'mime', it might offer a more extensive and updated list of MIME types.
The 'file-type' package goes beyond simple MIME type lookup by extension; it can detect the actual MIME type of a file or a Buffer by checking the file's magic numbers (file signatures). This can be more reliable than 'mime' when dealing with files that have incorrect or missing extensions.
A comprehensive, compact MIME type module.
Version 2 is a breaking change from 1.x, as the semver implies. Specifically:
lookup()
renamed to getType()
extension()
renamed to getExtension()
charset()
and load()
methods have been removedIf you prefer the legacy version of this module please npm install mime@^1
. Version 1 docs may be found here.
npm install mime
For the full version (800+ MIME types, 1,000+ extensions):
const mime = require('mime');
mime.getType('txt'); // ⇨ 'text/plain'
mime.getExtension('text/plain'); // ⇨ 'txt'
See Mime API below for API details.
There is also a "lite" version of this module that omits vendor-specific
(*/vnd.*
) and experimental (*/x-*
) types. It weighs in at ~2.5KB, compared
to 8KB for the full version. To load the lite version:
const mime = require('mime/lite');
To use this module in the browser, you would typlically use webpack or browserify to package your code. However, browser-ready versions are available via wzrd.in. E.g. For the full version:
<script src="http://wzrd.in/standalone/mime@latest"></script>
Or, for the "lite" version:
<script src="http://wzrd.in/standalone/mime%2flite@latest"></script>
Then:
<script>
mime.getType(...); // etc.
</script>
For those of you wondering about the difference between these [popular] NPM modules, here's a brief rundown ...
mime-db
is "the source of
truth" for MIME type information. It is not an API. Rather, it is a canonical
dataset of mime type definitions pulled from IANA, Apache, NGINX, and custom mappings
submitted by the Node.js community.
mime-types
is a thin
wrapper around mime-db that provides an API drop-in compatible(ish) with mime @ < v1.3.6
API.
mime
is, as of v2, a self-contained module bundled with a pre-optimized version
of the mime-db
dataset. It provides a simplified API with the following characteristics:
mime.getType(mime.getExtension(type)) == type
will always be trueModule | Size |
---|---|
mime-db | 18 KB |
mime-types | same as mime-db |
mime | 8 KB |
mime/lite | 2 KB |
Both require('mime')
and require('mime/lite')
return instances of the MIME
class, documented below.
Most users of this module will not need to create Mime instances directly. However if you would like to create custom mappings, you may do so as follows ...
// Require Mime class
const Mime = require('mime/Mime');
// Define mime type -> extensions map
const typeMap = {
'text/abc': ['abc', 'alpha', 'bet'],
'text/def': ['leppard']
};
// Create and use Mime instance
const myMime = new Mime(typeMap);
myMime.getType('abc'); // ⇨ 'text/abc'
myMime.getExtension('text/def'); // ⇨ 'leppard'
If more than one map argument is provided, each map is define()
ed (see below), in order.
Get mime type for the given path or extension. E.g.
mime.getType('js'); // ⇨ 'application/javascript'
mime.getType('json'); // ⇨ 'application/json'
mime.getType('txt'); // ⇨ 'text/plain'
mime.getType('dir/text.txt'); // ⇨ 'text/plain'
mime.getType('dir\\text.txt'); // ⇨ 'text/plain'
mime.getType('.text.txt'); // ⇨ 'text/plain'
mime.getType('.txt'); // ⇨ 'text/plain'
null
is returned in cases where an extension is not detected or recognized
mime.getType('foo/txt'); // ⇨ null
mime.getType('bogus_type'); // ⇨ null
Get extension for the given mime type. Charset options (often included in Content-Type headers) are ignored.
mime.getExtension('text/plain'); // ⇨ 'txt'
mime.getExtension('application/json'); // ⇨ 'json'
mime.getExtension('text/html; charset=utf8'); // ⇨ 'html'
Define [more] type mappings.
typeMap
is a map of type -> extensions, as documented in new Mime
, above.
By default this method will throw an error if you try to map a type to an
extension that is already assigned to another type. Passing true
for the
force
argument will suppress this behavior (overriding any previous mapping).
mime.define({'text/x-abc': ['abc', 'abcd']});
mime.getType('abcd'); // ⇨ 'text/x-abc'
mime.getExtension('text/x-abc') // ⇨ 'abc'
mime [path_or_extension]
E.g.
> mime scripts/jquery.js
application/javascript
Markdown generated from src/README_js.md by
FAQs
A comprehensive library for mime-type mapping
The npm package mime receives a total of 23,701,505 weekly downloads. As such, mime popularity was classified as popular.
We found that mime demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.