# encoding-sniffer

> Implementation of the HTML encoding sniffer algo, with stream support

Latest version **1.0.2** (published 2026-03-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install encoding-sniffer
pnpm add encoding-sniffer
yarn add encoding-sniffer
bun add encoding-sniffer
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2026-03-17 |
| First published | 2022-10-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20.19.0 |
| Dependencies | 1 |
| Unpacked size | 129.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 8 |
| Author | Felix Boehm |
| Maintainers | fb55 |

## Links

- npm: https://www.npmjs.com/package/encoding-sniffer
- Repository: https://github.com/fb55/encoding-sniffer
- Homepage: https://github.com/fb55/encoding-sniffer#readme
- Issues: https://github.com/fb55/encoding-sniffer/issues
- Funding: https://github.com/fb55/encoding-sniffer?sponsor=1
- npm.io page: https://npm.io/package/encoding-sniffer

## Dependencies (1)

- [@exodus/bytes](https://npm.io/package/@exodus/bytes.md) ^1.15.0

## Recent versions

- 1.0.2 (latest) — 2026-03-17
- 1.0.1 — 2026-03-17
- 1.0.0 — 2026-03-17
- 0.2.1 — 2025-06-08
- 0.2.0 — 2024-06-16
- 0.1.0 — 2024-01-05
- 0.0.2 — 2022-12-30
- 0.0.1 — 2022-11-04
- 0.0.0 — 2022-10-10

## README

# encoding-sniffer [![Node.js CI](https://github.com/fb55/encoding-sniffer/actions/workflows/nodejs-test.yml/badge.svg)](https://github.com/fb55/encoding-sniffer/actions/workflows/nodejs-test.yml)

An implementation of the HTML Standard's
[encoding sniffing algorithm](https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm),
with stream support.

This module wraps around [`@exodus/bytes`](https://github.com/ExodusOSS/bytes)
to make decoding buffers and streams incredibly easy.

## Features

- Support for streams
- Support for XML encoding types, including UTF-16 prefixes and
  `<?xml encoding="...">`
- Allows decoding streams and buffers with a single function call

## Installation

```bash
npm install encoding-sniffer
```

## Usage

```js
import { DecodeStream, getEncoding, decodeBuffer } from "encoding-sniffer";

/**
 * All functions accept an optional options object.
 *
 * Available options are (with default values):
 */
const options = {
    /**
     * The maximum number of bytes to sniff. Defaults to `1024`.
     */
    maxBytes: 1024,
    /**
     * The encoding specified by the user. If set, this will only be overridden
     * by a Byte Order Mark (BOM).
     */
    userEncoding: undefined,
    /**
     * The encoding specified by the transport layer. If set, this will only be
     * overridden by a Byte Order Mark (BOM) or the user encoding.
     */
    transportLayerEncodingLabel: undefined,
    /**
     * The default encoding to use, if no encoding can be detected.
     *
     * Defaults to `"windows-1252"`.
     */
    defaultEncoding: "windows-1252",
};

// Use the `DecodeStream` transform stream to automatically decode
// the contents of a stream as they are read
const decodeStream = new DecodeStream(options);

// Or, use the `getEncoding` function to detect the encoding of a buffer
const encoding = getEncoding(buffer, options);

// Use the `decodeBuffer` function to decode the contents of a buffer
const decodedBuffer = decodeBuffer(buffer, options);
```

## Security contact information

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure.

## License

This project is licensed under the MIT License. See the [LICENSE](/LICENSE) file
for more information.

---
_Source: https://npm.io/package/encoding-sniffer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
