<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Determine the Encoding of a HTML Byte Stream]]></title><description><![CDATA[<p dir="auto">&lt;h2&gt;Determine the Encoding of a HTML Byte Stream&lt;/h2&gt;</p>
<p dir="auto">&lt;p&gt;This package implements the HTML Standard's &lt;a href="<a href="https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm" rel="nofollow ugc">https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm</a>"&gt;encoding sniffing algorithm&lt;/a&gt; in all its glory. The most interesting part of this is how it pre-scans the first 1024 bytes in order to search for certain &lt;code&gt;&lt;meta charset&gt;&lt;/code&gt;-related patterns.&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const htmlEncodingSniffer = require("html-encoding-sniffer");<br />
const fs = require("fs");</p>
<p dir="auto">const htmlBuffer = fs.readFileSync("./html-page.html");<br />
const sniffedEncoding = htmlEncodingSniffer(htmlBuffer);<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;The returned value will be a canonical &lt;a href="<a href="https://encoding.spec.whatwg.org/#names-and-labels" rel="nofollow ugc">https://encoding.spec.whatwg.org/#names-and-labels</a>"&gt;encoding name&lt;/a&gt; (not a label). You might then combine this with the &lt;a href="<a href="https://github.com/jsdom/whatwg-encoding" rel="nofollow ugc">https://github.com/jsdom/whatwg-encoding</a>"&gt;whatwg-encoding&lt;/a&gt; package to decode the result:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const whatwgEncoding = require("whatwg-encoding");<br />
const htmlString = whatwgEncoding.decode(htmlBuffer, sniffedEncoding);<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;h3&gt;Options&lt;/h3&gt;</p>
<p dir="auto">&lt;p&gt;You can pass two potential options to &lt;code&gt;htmlEncodingSniffer&lt;/code&gt;:&lt;/p&gt;</p>
<p dir="auto">&lt;pre&gt;&lt;code&gt;<br />
const sniffedEncoding = htmlEncodingSniffer(htmlBuffer, {<br />
transportLayerEncodingLabel,<br />
defaultEncoding<br />
});<br />
&lt;/code&gt;&lt;/pre&gt;</p>
<p dir="auto">&lt;p&gt;These represent two possible inputs into the &lt;a href="<a href="https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm" rel="nofollow ugc">https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm</a>"&gt;encoding sniffing algorithm&lt;/a&gt;:&lt;/p&gt;</p>
<p dir="auto">&lt;ul&gt;<br />
&lt;li&gt;&lt;code&gt;transportLayerEncodingLabel&lt;/code&gt; is an encoding label that is obtained from the "transport layer" (probably a HTTP &lt;code&gt;Content-Type&lt;/code&gt; header), which overrides everything but a BOM.&lt;/li&gt;<br />
&lt;li&gt;&lt;code&gt;defaultEncoding&lt;/code&gt; is the ultimate fallback encoding used if no valid encoding is supplied by the transport layer, and no encoding is sniffed from the bytes. It defaults to &lt;code&gt;"windows-1252"&lt;/code&gt;, as recommended by the algorithm's table of suggested defaults for "All other locales" (including the &lt;code&gt;en&lt;/code&gt; locale).&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;h3&gt;Credits&lt;/h3&gt;</p>
<p dir="auto">&lt;p&gt;This package was originally based on the excellent work of &lt;a href="<a href="https://github.com/nicolashenry" rel="nofollow ugc">https://github.com/nicolashenry</a>"&gt;@nicolashenry&lt;/a&gt;, &lt;a href="<a href="https://github.com/tmpvar/jsdom/blob/16fd85618f2705d181232f6552125872a37164bc/lib/jsdom/living/helpers/encoding.js" rel="nofollow ugc">https://github.com/tmpvar/jsdom/blob/16fd85618f2705d181232f6552125872a37164bc/lib/jsdom/living/helpers/encoding.js</a>"&gt;in jsdom&lt;/a&gt;. It has since been pulled out into this separate package.&lt;/p&gt;</p>
<p dir="auto">&lt;hr&gt;</p>
<p dir="auto">&lt;p&gt;本文由模极社发布&lt;/p&gt;</p>
<p dir="auto">&lt;h4&gt;<img src="https://forum.mj7.cn/assets/plugins/nodebb-plugin-emoji/emoji/android/1f4cb.png?v=k0o2a4s5i30" class="not-responsive emoji emoji-android emoji--clipboard" title=":clipboard:" alt="📋" /> 内容概览&lt;/h4&gt;</p>
<p dir="auto">&lt;table&gt;<br />
&lt;tr&gt;&lt;td&gt;分类&lt;/td&gt;&lt;td&gt;要点&lt;/td&gt;&lt;td&gt;建议/说明&lt;/td&gt;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;td&gt;核心概念&lt;/td&gt;&lt;td&gt;Determine the Encodi&lt;/td&gt;&lt;td&gt;详见正文&lt;/td&gt;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;td&gt;操作步骤&lt;/td&gt;&lt;td&gt;Determine the Encodi&lt;/td&gt;&lt;td&gt;详见正文&lt;/td&gt;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;td&gt;注意事项&lt;/td&gt;&lt;td&gt;Determine the Encodi&lt;/td&gt;&lt;td&gt;详见正文&lt;/td&gt;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;td&gt;适用场景&lt;/td&gt;&lt;td&gt;Determine the Encodi&lt;/td&gt;&lt;td&gt;详见正文&lt;/td&gt;&lt;/tr&gt;<br />
&lt;tr&gt;&lt;td&gt;常见问题&lt;/td&gt;&lt;td&gt;Determine the Encodi&lt;/td&gt;&lt;td&gt;详见正文&lt;/td&gt;&lt;/tr&gt;<br />
&lt;/table&gt;</p>
<p dir="auto">&lt;hr&gt;<br />
&lt;h4&gt;<img src="https://forum.mj7.cn/assets/plugins/nodebb-plugin-emoji/emoji/android/1f517.png?v=k0o2a4s5i30" class="not-responsive emoji emoji-android emoji--link" title=":link:" alt="🔗" /> 你可能还感兴趣&lt;/h4&gt;<br />
&lt;ul&gt;<br />
&lt;li&gt;&lt;a href="/t/4496/4496"&gt;@dcloudio/uni-app-plus&lt;/a&gt;&lt;/li&gt;<br />
&lt;li&gt;&lt;a href="/t/4434/4434"&gt;@babel/plugin-transform-block-scoped-functions&lt;/a&gt;&lt;/li&gt;<br />
&lt;li&gt;&lt;a href="/t/2870/2870"&gt;酷科机械手产品系列与选型指南&lt;/a&gt;&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;hr&gt;<br />
&lt;p&gt;&lt;strong&gt;相关推荐：&lt;/strong&gt;&lt;/p&gt;<br />
&lt;ul&gt;<br />
&lt;li&gt;&lt;a href="<a href="https://www.mj7.cn/t/topic" rel="nofollow ugc">https://www.mj7.cn/t/topic</a>"&gt;注塑产品报价不会算？完整成本计算模板来了（附公式+案例）&lt;/a&gt;&lt;/li&gt;<br />
&lt;li&gt;&lt;a href="<a href="https://www.mj7.cn/t/pp" rel="nofollow ugc">https://www.mj7.cn/t/pp</a>"&gt;PP塑料：性能参数与注塑成型指南&lt;/a&gt;&lt;/li&gt;<br />
&lt;li&gt;&lt;a href="<a href="https://www.mj7.cn/t/pa" rel="nofollow ugc">https://www.mj7.cn/t/pa</a>"&gt;PA塑料：性能参数与注塑成型指南&lt;/a&gt;&lt;/li&gt;<br />
&lt;/ul&gt;</p>
<p dir="auto">&lt;hr&gt;<br />
&lt;p&gt;新手建议收藏，老手欢迎指点。&lt;/p&gt;</p>
]]></description><link>https://forum.mj7.cn/topic/7212/determine-the-encoding-of-a-html-byte-stream</link><generator>RSS for Node</generator><lastBuildDate>Fri, 02 Oct 2026 09:34:46 GMT</lastBuildDate><atom:link href="https://forum.mj7.cn/topic/7212.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Apr 2026 21:58:11 GMT</pubDate><ttl>60</ttl></channel></rss>