An ESM package detects binary file types from buffers, files, streams, and blobs by reading magic numbers, not text formats like CSV or SVG.
Overview
file-type helps Node and web code identify binary formats when extensions or MIME headers are missing or untrusted. Detection inspects the beginning of data for known signatures and returns a type descriptor or undefined when nothing matches.
APIs cover Uint8Array and ArrayBuffer buffers, file paths where node:fs is available, web ReadableStream inputs, Blob and File objects, and custom tokenizer sources for partial reads. A stream helper can attach detected type metadata to an existing readable stream for pipeline use.
The library maintains broad built-in format support and allows custom detectors that run before defaults. Options include limits on read buffer size, sample size for streaming detection, MPEG frame tolerance, and abort signals on parsers.
Detection is explicitly a best-effort hint: results do not guarantee validity of file contents. The authors recommend size limits and worker isolation when handling untrusted uploads on servers.
Key features
- Buffer and file path detection
- Stream and Blob detection
- Tokenizer-based partial reads
- Custom detector plugins
- Supported extension and MIME listings
- Stream pipeline type attachment
- Configurable buffer size limits
- AbortSignal support on parsers
Use cases
- Validating upload content types on servers
- Sniffing formats from partial downloads
- Detecting types in object storage workflows
- Adding custom format signatures
- Typing streams in middleware pipelines