Determine the Encoding of a HTML Byte Stream

This package implements the HTML Standard's encoding sniffing algorithm 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 <meta charset>-related patterns.

```js const htmlEncodingSniffer = require("html-encoding-sniffer"); const fs = require("fs");

const htmlBuffer = fs.readFileSync("./html-page.html"); const sniffedEncoding = htmlEncodingSniffer(htmlBuffer); ```

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

js const whatwgEncoding = require("whatwg-encoding"); const htmlString = whatwgEncoding.decode(htmlBuffer, sniffedEncoding);

Options

You can pass two potential options to htmlEncodingSniffer:

js const sniffedEncoding = htmlEncodingSniffer(htmlBuffer, { transportLayerEncodingLabel, defaultEncoding });

These represent two possible inputs into the encoding sniffing algorithm:

Credits

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