whatwg-mimetype

中文文档
5.0.0发布于 2026/01/20MITCommonJS零依赖构建来源已验证近 30 天下载 6亿

Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard

原始使用文档
文档目录4 节
  1. APIs
  2. MIMEType
  3. MIMETypeParameters
  4. computedMIMEType()

Parse, serialize, manipulate, and sniff MIME types

This package will parse MIME types into a structured format, which can then be manipulated and serialized:

const { MIMEType } = require("whatwg-mimetype");

const mimeType = new MIMEType(`Text/HTML;Charset="utf-8"`);

console.assert(mimeType.toString() === "text/html;charset=utf-8");

console.assert(mimeType.type === "text");
console.assert(mimeType.subtype === "html");
console.assert(mimeType.essence === "text/html");
console.assert(mimeType.parameters.get("charset") === "utf-8");

mimeType.parameters.set("charset", "windows-1252");
console.assert(mimeType.parameters.get("charset") === "windows-1252");
console.assert(mimeType.toString() === "text/html;charset=windows-1252");

console.assert(mimeType.isHTML() === true);
console.assert(mimeType.isXML() === false);

It can also determine the MIME type of a resource by sniffing its contents:

const { computedMIMEType } = require("whatwg-mimetype");

const pngBytes = new Uint8Array([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);

computedMIMEType(pngBytes).essence;                                    // "image/png"
computedMIMEType(pngBytes, { contentTypeHeader: "image/gif" }).essence // "image/png"
computedMIMEType(pngBytes, { contentTypeHeader: "text/html" }).essence // "text/html"

Parsing, serialization, and sniffing are all fairly complex processes; see the specification for more details:

This package's algorithms conform to those of the WHATWG MIME Sniffing Standard, as of commit e7594d5.

APIs

MIMEType

The MIMEType class's constructor takes a string which it will attempt to parse into a MIME type; if parsing fails, an Error will be thrown.

The parse() static factory method

As an alternative to the constructor, you can use MIMEType.parse(string). The only difference is that parse() will return null on failed parsing, whereas the constructor will throw. It thus makes the most sense to use the constructor in cases where unparseable MIME types would be exceptional, and use parse() when dealing with input from some unconstrained source.

Properties

  • type: the MIME type's type, e.g. "text"
  • subtype: the MIME type's subtype, e.g. "html"
  • essence: the MIME type's essence, e.g. "text/html"
  • parameters: an instance of MIMETypeParameters, containing this MIME type's parameters

type and subtype can be changed. They will be validated to be non-empty and only contain HTTP token code points.

essence is only a getter, and cannot be changed.

parameters is also a getter, but the contents of the MIMETypeParameters object are mutable, as described below.

Methods

  • toString() serializes the MIME type to a string
  • isHTML(): returns true if this instance represents a HTML MIME type
  • isXML(): returns true if this instance represents an XML MIME type
  • isJavaScript({ prohibitParameters }): returns true if this instance represents a JavaScript MIME type. prohibitParameters can be set to true to disallow any parameters, i.e. to test if the MIME type's serialization is a JavaScript MIME type essence match.

Note: the isHTML(), isXML(), and isJavaScript() methods are speculative, and may be removed or changed in future major versions. See whatwg/mimesniff#48 for brainstorming in this area. Currently we implement these mainly because they are useful in jsdom.

MIMETypeParameters

The MIMETypeParameters class, instances of which are returned by mimeType.parameters, has equivalent surface API to a JavaScript Map.

However, MIMETypeParameters methods will always interpret their arguments as appropriate for MIME types, so e.g. parameter names will be lowercased, and attempting to set invalid characters will throw.

Some examples:

const { MIMEType } = require("whatwg-mimetype");

const mimeType = new MIMEType(`x/x;a=b;c=D;E="F"`);

// Logs:
// a b
// c D
// e F
for (const [name, value] of mimeType.parameters) {
  console.log(name, value);
}

console.assert(mimeType.parameters.has("a"));
console.assert(mimeType.parameters.has("A"));
console.assert(mimeType.parameters.get("A") === "b");

mimeType.parameters.set("Q", "X");
console.assert(mimeType.parameters.get("q") === "X");
console.assert(mimeType.toString() === "x/x;a=b;c=d;e=F;q=X");

// Throws:
mimeType.parameters.set("@", "x");

computedMIMEType()

The computedMIMEType(resource, options) function determines a resource's MIME type using the full MIME type sniffing algorithm. This includes:

  • complexities around how the supplied Content-Type header or other external information interacts with (but usually does not override) the sniffing process;
  • the ability to set a "no-sniff" flag, like the one used when X-Content-Type-Options: nosniff is present, which mostly (but not entirely) prevents sniffing; and
  • the "Apache bug" check which occurs when the Content-Type header is one of several specific values.

That is, this doesn't just implement the rules for identifying an unknown MIME type. It gives you everything you need for a full browser-compatible MIME sniffing procedure.

Arguments

  • resource (Uint8Array): The resource bytes.
  • options.contentTypeHeader (string): The Content-Type header value, for HTTP resources.
  • options.providedType (string): The MIME type from the filesystem or another protocol (like FTP), for non-HTTP resources.
  • options.noSniff (boolean, default false): Whether the X-Content-Type-Options: nosniff header was present.
  • options.isSupported (function, default () => true): A predicate called with a MIMEType instance to check if an image/audio/video MIME type is supported by the user agent. If it returns false, sniffing is skipped for that type.

The contentTypeHeader and providedType options will be stringified, so you could also supply a MIMEType (or a Node.js util module MIMEType).

Return value

A MIMEType instance representing the computed MIME type.

版本与发布记录

whatwg-mimetype 共发布 8 个版本,首次发布于 2017/12/10;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.0.0latest2026/01/209 个月前
4.0.02023/11/112 年前
3.0.02021/09/195 年前
2.3.02018/11/217 年前
2.2.02018/09/158 年前
2.1.02018/02/058 年前

关于 whatwg-mimetype 的常见问题

whatwg-mimetype 是什么?

Parses, serializes, and manipulates MIME types, according to the WHATWG MIME Sniffing Standard

如何安装 whatwg-mimetype?

在项目目录运行 npm install whatwg-mimetype,或使用 pnpm add / yarn add / bun add。

whatwg-mimetype 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/whatwg-mimetype 是否存在。

whatwg-mimetype 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

whatwg-mimetype 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

whatwg-mimetype 可以商用吗?

whatwg-mimetype 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

whatwg-mimetype 的下载量有多少?

根据 npm 官方下载统计,whatwg-mimetype 在 2026/10/05 – 2026/11/03 这 30 天内被下载 595,202,980 次,日均约 19,840,099 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

whatwg-mimetype 还在维护吗?

最新版本 5.0.0 发布于 2026/01/20(9 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 1 个;首次发布于 2017/12/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。