{"name":"html-encoding-sniffer","version":"5.0.0","keywords":["encoding","html"],"author":{"url":"https://domenic.me/","name":"Domenic Denicola","email":"d@domenic.me"},"license":"MIT","_id":"html-encoding-sniffer@5.0.0","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"}],"homepage":"https://github.com/jsdom/html-encoding-sniffer#readme","bugs":{"url":"https://github.com/jsdom/html-encoding-sniffer/issues"},"dist":{"shasum":"7cc221eb3cd85c9a38f43335e83c941156874984","tarball":"http://registry-npm.ywwl.com/html-encoding-sniffer/download/html-encoding-sniffer-5.0.0.tgz","fileCount":4,"integrity":"sha512-eiwhCTJq0UfO/rlLHqIb0yguSKhVE5a44IZag4Sd71hxGKOGISFV0SB+7CRdRxaexVfjB9kNvPpPKL1F3O/X+g==","signatures":[{"sig":"MEUCIQCxMUNeSAXzPopwBMEur02BjyOnRV0V9UdsQ5OMAPi2ngIgWcToi/2jcPHYbqbPYhLpvTxxmR+Bj/aFOgAczkN6QLs=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/html-encoding-sniffer@5.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":11809,"size":3968,"noattachment":false,"key":"/html-encoding-sniffer/-/html-encoding-sniffer-5.0.0.tgz"},"main":"lib/html-encoding-sniffer.js","engines":{"node":"^20.19.0 || ^22.12.0 || >=24.0.0"},"gitHead":"fab2967107d2caea4fa032fc247537a403f12461","scripts":{"lint":"eslint","test":"node --test"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:3e41e1dd-6fba-4b18-a39b-23d35ea8520a"}},"repository":{"url":"git+https://github.com/jsdom/html-encoding-sniffer.git","type":"git"},"_npmVersion":"11.6.2","description":"Sniff the encoding from a HTML byte stream","directories":{},"_nodeVersion":"24.12.0","dependencies":{"@exodus/bytes":"^1.2.0"},"_hasShrinkwrap":false,"devDependencies":{"eslint":"^9.39.2","globals":"^16.5.0","@domenic/eslint-config":"^4.0.1"},"_npmOperationalInternal":{"tmp":"tmp/html-encoding-sniffer_5.0.0_1766466264105_0.3090489086520247","host":"s3://npm-registry-packages-npm-production"},"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":1766466264244,"_cnpm_publish_time":1766466264244,"dist-tags":{"latest":"7.0.0"}}