html-encoding-sniffer

中文文档
7.0.0发布于 2026/09/21MITCommonJS构建来源已验证近 30 天下载 4.5亿

Sniff the encoding of an HTML or XML byte stream

原始使用文档

Determine the Encoding of an HTML or XML Byte Stream

This package determines the character encoding of an HTML or XML byte stream. It follows the HTML Standard's encoding sniffing algorithm for HTML, and WebKit's encoding sniffing behavior for XML.

const htmlEncodingSniffer = require("html-encoding-sniffer");
const fs = require("node:fs");

const bytes = fs.readFileSync("./page.html");
const sniffedEncoding = htmlEncodingSniffer(bytes);

For XML, pass { xml: true }:

const bytes = fs.readFileSync("./document.xml");
const sniffedEncoding = htmlEncodingSniffer(bytes, { xml: true });

The passed bytes are given as a Uint8Array; the Node.js Buffer subclass of Uint8Array will also work, as shown above.

The returned value will be a canonical encoding name (not a label). You might then combine this with the @exodus/bytes package to decode the result:

const { TextDecoder } = require("@exodus/bytes/encoding.js");
const decodedString = (new TextDecoder(sniffedEncoding)).decode(bytes);

Options

You can pass the following options to htmlEncodingSniffer:

const sniffedEncoding = htmlEncodingSniffer(bytes, {
  xml,
  transportLayerEncodingLabel,
  defaultEncoding,
  maxPrescanBytes
});

The xml option is a boolean, defaulting to false. It selects XML sniffing when true and HTML sniffing when false. XML sniffing ignores HTML <meta> declarations and leaves well-formedness validation to the caller's XML parser.

The transportLayerEncodingLabel is an encoding label that is obtained from the "transport layer" (probably a HTTP Content-Type header), which overrides everything but a BOM.

The defaultEncoding is the ultimate fallback encoding used if no valid encoding is supplied by the transport layer, and no encoding is sniffed from the bytes. For HTML, it defaults to "windows-1252", as recommended by the algorithm's table of suggested defaults for "All other locales" (including the en locale). For XML, it defaults to "UTF-8".

The maxPrescanBytes option sets how many bytes to scan for HTML <meta> encoding declarations, defaulting to 1024. Set a larger value to recognize declarations later in the input, Infinity to scan the entire input, or 0 to skip meta scanning. It must be a non-negative integer or Infinity. This limit does not apply to XML declarations or UTF-16 byte signatures. Supplying it when xml is true throws a TypeError.

Credits

This package was originally based on the excellent work of @nicolashenry, in jsdom. It has since been pulled out into this separate package.

版本与发布记录

html-encoding-sniffer 共发布 10 个版本,首次发布于 2016/10/16;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
7.0.0latest2026/09/211 个月前
6.0.02025/12/2610 个月前
5.0.02025/12/2310 个月前
4.0.02023/11/122 年前
3.0.02021/09/195 年前
2.0.12020/02/246 年前

关于 html-encoding-sniffer 的常见问题

html-encoding-sniffer 是什么?

Sniff the encoding of an HTML or XML byte stream

如何安装 html-encoding-sniffer?

在项目目录运行 npm install html-encoding-sniffer,或使用 pnpm add / yarn add / bun add。

html-encoding-sniffer 支持 TypeScript 吗?

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

html-encoding-sniffer 支持 ESM 还是 CommonJS?

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

html-encoding-sniffer 需要什么版本的 Node.js?

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

html-encoding-sniffer 可以商用吗?

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

html-encoding-sniffer 的下载量有多少?

根据 npm 官方下载统计,html-encoding-sniffer 在 2026/10/06 – 2026/11/04 这 30 天内被下载 450,783,567 次,日均约 15,026,119 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

html-encoding-sniffer 还在维护吗?

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