{"id":18429639,"url":"https://github.com/jsdom/html-encoding-sniffer","last_synced_at":"2025-04-09T14:15:24.787Z","repository":{"id":46985542,"uuid":"71028772","full_name":"jsdom/html-encoding-sniffer","owner":"jsdom","description":"Sniff the encoding from a HTML byte stream","archived":false,"fork":false,"pushed_at":"2023-11-12T07:38:03.000Z","size":77,"stargazers_count":19,"open_issues_count":2,"forks_count":10,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-31T12:47:22.899Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsdom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"tidelift":"npm/jsdom"}},"created_at":"2016-10-16T03:52:04.000Z","updated_at":"2024-05-23T13:12:18.000Z","dependencies_parsed_at":"2024-02-26T01:49:25.589Z","dependency_job_id":"e6f569e0-6bf5-4f57-abab-ab58641256ea","html_url":"https://github.com/jsdom/html-encoding-sniffer","commit_stats":{"total_commits":20,"total_committers":3,"mean_commits":6.666666666666667,"dds":"0.15000000000000002","last_synced_commit":"7de54310087887b3fbb2c89b05d5fbc36da82078"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsdom%2Fhtml-encoding-sniffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsdom%2Fhtml-encoding-sniffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsdom%2Fhtml-encoding-sniffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsdom%2Fhtml-encoding-sniffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsdom","download_url":"https://codeload.github.com/jsdom/html-encoding-sniffer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054194,"owners_count":21039952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T05:18:00.762Z","updated_at":"2025-04-09T14:15:24.759Z","avatar_url":"https://github.com/jsdom.png","language":"JavaScript","funding_links":["https://tidelift.com/funding/github/npm/jsdom"],"categories":[],"sub_categories":[],"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 `\u003cmeta charset\u003e`-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 [whatwg-encoding](https://github.com/jsdom/whatwg-encoding) package to decode the result:\n\n```js\nconst whatwgEncoding = require(\"whatwg-encoding\");\nconst htmlString = whatwgEncoding.decode(htmlBytes, sniffedEncoding);\n```\n\n## Options\n\nYou can pass two potential options to `htmlEncodingSniffer`:\n\n```js\nconst sniffedEncoding = htmlEncodingSniffer(htmlBytes, {\n  transportLayerEncodingLabel,\n  defaultEncoding\n});\n```\n\nThese represent two possible inputs into the [encoding sniffing algorithm](https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm):\n\n- `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- `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. It defaults to `\"windows-1252\"`, as recommended by the algorithm's table of suggested defaults for \"All other locales\" (including the `en` locale).\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsdom%2Fhtml-encoding-sniffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsdom%2Fhtml-encoding-sniffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsdom%2Fhtml-encoding-sniffer/lists"}