chardet

Detects the character encoding of a buffer or file using statistical byte-occurrence analysis, with zero dependencies.

Library
npm
v2.2.0
311stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
62/100Good
Development Activity56
Maintenance52
Community60
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
58/100Fair
Architecture78
Code Quality82
Innovation45
Learning Curve25

chardet is a pure TypeScript library that determines the most likely character encoding of raw bytes — a Buffer, Uint8Array, or file on disk — by tallying byte-occurrence statistics and running them through a set of dedicated recognisers for UTF-8, UTF-16/32, ISO-2022 (JP/KR/CN), Shift_JIS, Big5, EUC-JP/KR, GB18030, the ISO-8859 family, several Windows code pages, and KOI8-R. It returns either a single best-guess encoding via detect() or a full confidence-ranked list via analyse(), so callers can inspect alternatives when the input is ambiguous.

The library has no runtime dependencies and no native bindings, ships a ~22KB package, and works identically in Node.js and the browser by swapping a small filesystem shim. It is commonly reached for when data of unknown provenance — uploaded files, scraped or imported text, log data from mixed regional sources — needs to be decoded correctly before it can be parsed as UTF-8 text.

What You Get

  • A detect() function that returns the single most probable encoding name for a Buffer or Uint8Array
  • An analyse() function returning every candidate encoding with a confidence score and, where applicable, a detected language
  • detectFile() (async) and detectFileSync() helpers that read from disk and run detection directly against file contents
  • sampleSize/offset options to scan only part of a large file for faster, lower-accuracy detection
  • Zero runtime dependencies and no native/C++ bindings — a pure TypeScript implementation
  • Identical behavior in Node.js and browsers via a swappable filesystem shim

Common Use Cases

  • Detecting the encoding of user-uploaded text files before decoding them to UTF-8 in a backend service
  • Picking the correct decoder for CSV or data-import pipelines that ingest files from varied regional sources
  • Sniffing file encoding in CLI tools (editors, search utilities) to avoid mojibake when rendering contents
  • Sampling only the first few KB of very large files or logs to cheaply detect encoding before full processing

Under The Hood

Architecture The library follows a simple, single-purpose strategy pattern. The entry point (src/index.ts) exports detect/analyse/detectFile/detectFileSync, all built on top of a fixed array of Recogniser instances (Utf8, UTF_16BE/LE, UTF_32BE/LE, sjis, big5, euc_jp, euc_kr, gb_18030, ISO_2022_JP/KR/CN, the ISO_8859_x family, several windows_x code pages, KOI8_R, and Ascii), each implementing a shared match/name/language contract defined in src/encoding/index.ts. analyse() builds one shared Context (a 256-slot byte histogram plus C1-byte and raw-buffer flags), maps every recogniser over it, filters out non-matches, and sorts by confidence — no dependency injection, no configuration layer, no external state. The one platform seam is fs/node.ts vs fs/browser.ts, swapped via package.json’s browser field so detectFile/detectFileSync behave identically under Node and bundlers. Adding a new encoding is a pure extension (a new class plus an entry in the recognisers array), which shows the shared interface was deliberately kept minimal to make the recogniser set easy to grow.

Tech Stack 100% TypeScript, targeting both Node.js and the browser from one build. package.json declares zero runtime dependencies; devDependencies are limited to typescript, vitest with @vitest/coverage-v8 for testing, tsx for running scripts, prettier for formatting, and semantic-release for automated versioned publishing. The build step (tsc -p tsconfig.build.json) emits lib/*.js plus .d.ts typings consumed via the main/typings package.json fields. GitHub Actions workflows handle test runs and a semantic-release publish pipeline, and Renovate is configured for automated dependency-update PRs.

Code Quality Every encoding module (ascii.ts, utf8.ts, unicode.ts, mbcs.ts, sbcs.ts, iso2022.ts) has a matching *.test.ts file, alongside index.test.ts and utils.test.ts, run via Vitest with coverage collection and a separate type-check-only test script in CI. Error handling is explicit rather than swallowed: invalid (non-buffer) input throws a descriptive error, and file-reading paths reject on I/O errors while still closing file descriptors. Naming is consistent with established IANA/ICU encoding identifiers, strict TypeScript is used throughout with typed public interfaces (Recogniser, Context, Match, EncodingName), and Prettier enforces formatting. There is no dedicated ESLint configuration, so lint-equivalent checks rely on the TypeScript compiler and formatter alone.

What Makes It Unique The detection heuristics themselves are not novel — they are openly credited, direct ports of the statistical byte-occurrence approach used by ICU4J and uchardet for each encoding family. The genuinely distinctive parts are packaging and ergonomics: a dependency-free, very small (~22KB) dual Node/browser build, a confidence-ranked analyse() result rather than a single opaque guess, and a sampleSize/offset option that lets callers trade completeness for speed on large files — a practical knob that many comparable encoding-detection libraries don’t expose.

Used by 7 apps in this directory

TypeScript
99%
AGPL 3.0

Cherry Studio

AI Assistants

51,521

All-in-one AI desktop client with 300+ assistants and multi-model support

View details
89
Repo Health
84
Technical
71
Dependency
Built with
TypeScript99%
Updated today
Go
38%
MIT

ezBookkeeping

Invoicing Finance

5,535

Lightweight self-hosted personal finance manager with AI receipt scanning, multi-currency support, and MCP integration for complete data privacy.

View details
88
Repo Health
80
Technical
72
Dependency
Built with
Go38%
Vue34%
TypeScript25%
Updated yesterday
TypeScript
75%
Apache 2.0

Fern

Developer Tools

3,772

Fern turns a single OpenAPI, AsyncAPI, or Protobuf definition into type-safe SDKs for nine languages and a hosted API documentation site, all from one CLI and one source of truth.

View details
90
Repo Health
86
Technical
66
Dependency
Built with
TypeScript75%
Updated yesterday
TypeScript
92%
Other

n8n

Automation · No Code Platforms

203,555

Code when you need it, UI when you don't — the workflow automation platform built for technical teams who refuse to choose.

View details
95
Repo Health
87
Technical
66
Dependency
Built with
TypeScript92%
Updated today
TypeScript
50%
ZLIB

Portainer

Devops

38,446

A lightweight, open-source web UI that puts Docker, Kubernetes, and Podman management within reach of any team—no CLI expertise required.

View details
92
Repo Health
79
Technical
65
Dependency
Built with
TypeScript50%
Go38%
Updated 5 days ago
TypeScript
98%
Apache 2.0

Tianji

Analytics · Monitoring

3,086

Replace Google Analytics, UptimeKuma, and Prometheus with one self-hosted platform that tracks websites, monitors uptime, and reports server health.

View details
86
Repo Health
76
Technical
65
Dependency
Built with
TypeScript98%
Updated yesterday
JavaScript
57%
MIT

Uptime Kuma

Monitoring

91,056

Self-hosted monitoring for every service you run — 23 monitor types, 95 notification channels, live dashboards, and public status pages with no vendor lock-in.

View details
91
Repo Health
81
Technical
63
Dependency
Built with
JavaScript57%
Vue42%
Updated today

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search