{"name":"html-encoding-sniffer","description":"Sniff the encoding from a HTML byte stream","keywords":["encoding","html"],"version":"6.0.0","author":{"name":"Domenic Denicola","email":"d@domenic.me","url":"https://domenic.me/"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/jsdom/html-encoding-sniffer.git"},"main":"lib/html-encoding-sniffer.js","scripts":{"test":"node --test","lint":"eslint"},"dependencies":{"@exodus/bytes":"^1.6.0"},"devDependencies":{"@domenic/eslint-config":"^4.0.1","eslint":"^9.39.2","globals":"^16.5.0"},"engines":{"node":"^20.19.0 || ^22.12.0 || >=24.0.0"},"gitHead":"a2f5ea718eea331aa7b098f40044f72adc49d45e","_id":"html-encoding-sniffer@6.0.0","bugs":{"url":"https://github.com/jsdom/html-encoding-sniffer/issues"},"homepage":"https://github.com/jsdom/html-encoding-sniffer#readme","_nodeVersion":"24.12.0","_npmVersion":"11.6.2","dist":{"integrity":"sha512-CV9TW3Y3f8/wT0BRFc1/KAVQ3TUHiXmaAb6VW9vtiMFf7SLoMd1PdAc4W3KFOFETBJUb90KatHqlsZMWV+R9Gg==","shasum":"f8d9390b3b348b50d4f61c16dd2ef5c05980a882","tarball":"http://registry-npm.ywwl.com/html-encoding-sniffer/download/html-encoding-sniffer-6.0.0.tgz","fileCount":4,"unpackedSize":12148,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/html-encoding-sniffer@6.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIBtfh2skBBvPWE1KaLzPt5wPA4Ps8/oLkGXBI/qFDMY3AiAAue6pSOKkv8c9MZFLbhThTnI/cgZrvDpIHGDghT7x1w=="}],"size":4099,"noattachment":false,"key":"/html-encoding-sniffer/-/html-encoding-sniffer-6.0.0.tgz"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:3e41e1dd-6fba-4b18-a39b-23d35ea8520a"}},"directories":{},"maintainers":[{"name":"timothygu","email":"timothygu99@gmail.com"},{"name":"domenic","email":"d@domenic.me"},{"name":"sebmaster","email":"sebmaster16@gmail.com"},{"name":"zirro","email":"code@zirro.se"},{"name":"tmpvar","email":"tmpvar@gmail.com"},{"name":"joris-van-der-wel","email":"joris@jorisvanderwel.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/html-encoding-sniffer_6.0.0_1766721283929_0.05459638768167974"},"_hasShrinkwrap":false,"readme":"# Determine the Encoding of a HTML Byte Stream\n\nThis package implements the HTML Standard's [encoding sniffing algorithm](https://html.spec.whatwg.org/multipage/syntax.html#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.\n\n```js\nconst htmlEncodingSniffer = require(\"html-encoding-sniffer\");\nconst fs = require(\"fs\");\n\nconst htmlBytes = fs.readFileSync(\"./html-page.html\");\nconst sniffedEncoding = htmlEncodingSniffer(htmlBytes);\n```\n\nThe passed bytes are given as a `Uint8Array`; the Node.js `Buffer` subclass of `Uint8Array` will also work, as shown above.\n\nThe returned value will be a canonical [encoding name](https://encoding.spec.whatwg.org/#names-and-labels) (not a label). You might then combine this with the [`@exodus/bytes`](https://github.com/ExodusOSS/bytes/) package to decode the result:\n\n```js\nconst { TextDecoder } = require(\"@exodus/bytes\");\nconst htmlString = (new TextDecoder(sniffedEncoding)).decode(htmlBytes);\n```\n\n## Options\n\nYou can pass the following options to `htmlEncodingSniffer`:\n\n```js\nconst sniffedEncoding = htmlEncodingSniffer(htmlBytes, {\n  xml,\n  transportLayerEncodingLabel,\n  defaultEncoding,\n});\n```\n\nThe `xml` option is a boolean, defaulting to `false`. If set to `true`, then we bypass the [HTML encoding sniffing algorithm](https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm) and compute the encoding based on the presence of a BOM, or the other options provided. (In the future, we may perform sniffing of the `<?xml?>` declaration, but for now that is not implemented.)\n\nThe `transportLayerEncodingLabel` is an encoding label that is obtained from the \"transport layer\" (probably a HTTP `Content-Type` header), which overrides everything but a BOM.\n\nThe `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\"`.\n\n## Credits\n\nThis package was originally based on the excellent work of [@nicolashenry](https://github.com/nicolashenry), [in jsdom](https://github.com/tmpvar/jsdom/blob/16fd85618f2705d181232f6552125872a37164bc/lib/jsdom/living/helpers/encoding.js). It has since been pulled out into this separate package.\n","publish_time":1766721284115,"_cnpm_publish_time":1766721284115,"dist-tags":{"latest":"7.0.0"}}