file-type

Detect the file type of a buffer, stream, or file by reading its magic number.

Library
npm
v22.2.0
4,339 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
80 /100 Excellent
Development Activity 76
Maintenance 80
Community 64
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
89 /100 Excellent
Architecture 88
Code Quality 92
Innovation 90
Learning Curve 82

file-type is a JavaScript library that determines the true type of binary data by inspecting its magic number (the signature bytes at the start of a file) rather than trusting a filename extension or a client-supplied MIME type. It returns the canonical extension and MIME type for hundreds of formats spanning images, audio, video, archives, documents, and fonts.

Built on a streaming tokenizer, it works across Node.js and the browser and can read from files, Uint8Array/ArrayBuffer buffers, Blobs, and web ReadableStreams. Because it only needs the leading bytes of a file, it can identify large or remote files without downloading them in full, making it a common building block for secure upload validation and content pipelines.

What You Get

  • Detection from files, buffers, Blobs, web ReadableStreams, and custom tokenizers via a small set of fileTypeFrom* functions.
  • Support for hundreds of binary formats across images, audio, video, archives, documents, and fonts, each mapped to its canonical extension and MIME type.
  • Streaming detection that only reads the leading bytes, so large or remote files can be identified without loading them entirely.
  • First-class TypeScript definitions plus exported supportedExtensions and supportedMimeTypes sets.
  • A customDetectors hook and reusable FileTypeParser class for extending detection to formats not built in.

Common Use Cases

  • Validating file uploads on a server by verifying the real type instead of trusting the extension or client MIME header.
  • Routing or transcoding media by detecting whether incoming bytes are an image, audio, video, or archive.
  • Inspecting remote files over HTTP range requests or S3 without downloading the whole object.
  • Enforcing allow-lists of accepted formats in content pipelines and asset-processing jobs.

Under The Hood

Architecture

Detection is organized around a streaming tokenizer abstraction. The fileTypeFrom* entry points in source/index.js normalize their input (file path, Uint8Array/ArrayBuffer, Blob, or web ReadableStream) into an strtok3 tokenizer, then hand it to a FileTypeParser whose parse method walks an ordered ladder of magic-number checks, reading only as many leading bytes as each signature requires. More involved container formats (PNG, ZIP, EBML/Matroska, ASF) live in dedicated modules under source/detectors/, keeping the main dispatch readable, and source/supported.js centralizes the extension and MIME tables exported as supportedExtensions and supportedMimeTypes.

Tech Stack

The package is authored as pure ESM ("type": "module") targeting Node.js >= 22, with no build step for consumers beyond its TypeScript definitions. It depends on a small, focused tokenizer stack — strtok3, token-types, @tokenizer/inflate, and uint8array-extras — rather than pulling in heavy runtime dependencies. Development uses ava for tests, xo for linting, tsd for type-definition testing, and esbuild.

Code Quality

Quality is a clear strength. The repository ships an exceptionally large test.js (roughly 280KB) exercised against a fixture/ directory of real sample files, so detection is validated against genuine binaries rather than hand-crafted byte stubs. Linting is enforced with xo and the public type surface is checked with tsd via source/index.test-d.ts. Naming is consistent and descriptive (fileTypeFromBuffer, fileTypeFromStream), and functions return undefined on no match rather than throwing, giving predictable control flow.

API Design The public API is small, orthogonal, and ergonomic: a family of fileTypeFrom{Buffer,File,Stream,Blob,Tokenizer} functions that each resolve to {ext, mime} or undefined, plus a fileTypeStream helper and a FileTypeParser class for advanced reuse. Extensibility is handled cleanly through a customDetectors option, and full TypeScript types plus a thorough README with copy-paste examples mean most users are productive after a single import.

Used by 44 apps in this directory

TypeScript
96%
Other

Refly

AI Development · Automation · No Code Platforms

7,537

Build deterministic, versioned agent skills from natural language—run them anywhere from Claude Code to Slack.

View details
68
Repo Health
76
Technical
63
Dependency
Built with
TypeScript 96%
Updated 1 weeks ago
TypeScript
97%
Other

Rocket.Chat

Team Chat

46,304

The secure, self-hosted team communications platform for organizations that cannot compromise on data sovereignty.

View details
96
Repo Health
79
Technical
63
Dependency
Built with
TypeScript 97%
Updated 5 days ago
TypeScript
98%
MIT

sigle

Blogging

489

A decentralized, open-source writing platform that permanently stores your stories on the Stacks blockchain and Arweave — where Web3 content creators own their words forever.

View details
81
Repo Health
78
Technical
69
Dependency
Built with
TypeScript 98%
Updated 2 weeks ago
TypeScript
89%
Other

strapi

CMS

73,368

Open-source headless CMS that auto-generates REST and GraphQL APIs from your content models, with a fully customizable admin panel you control.

View details
93
Repo Health
84
Technical
63
Dependency
Built with
TypeScript 89%
JavaScript 11%
Updated 6 days ago
TypeScript
48%
MIT

supermemory

AI Development · AI Memory · Note Taking

31,370

The state-of-the-art memory and context engine for AI agents — ranked #1 on all three major AI memory benchmarks.

View details
88
Repo Health
82
Technical
67
Dependency
Built with
TypeScript 48%
MDX 32%
Python 17%
Updated 5 days ago
TypeScript
84%
Other

twenty

CRM

59,615

The open-source CRM you build, ship, and version like the rest of your stack — with customizable objects, AI agents, and a TypeScript SDK.

View details
92
Repo Health
82
Technical
64
Dependency
Built with
TypeScript 84%
MDX 14%
Updated 6 days ago
TypeScript
95%
Apache 2.0

Void

AI Code Assistants · Automation · Code Editors

28,733

Open-source AI code editor with direct LLM integration and data privacy

View details
44
Repo Health
84
Technical
68
Dependency
Built with
TypeScript 95%
Updated 4 months ago
TypeScript
41%
AGPL 3.0

Wiki.js

Collaboration · Knowledge Management

29,081

A modern, self-hosted wiki platform built on Node.js with a rich plugin ecosystem for authentication, search, storage, and rendering that adapts to any team's infrastructure.

View details
95
Repo Health
67
Technical
60
Dependency
Built with
TypeScript 41%
Vue 39%
JavaScript 16%
Updated 5 days ago

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