npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

file-type-magic

v0.4.0

Published

Rust + WebAssembly file type checker for npm

Downloads

46

Readme

file-type-magic

file-type-magic is a small npm package scaffold backed by a Rust WebAssembly module. It checks file types from raw bytes using well-known file signatures. Runs in broswer and node.

What it does

  • Detects a file type from a byte buffer
  • Checks whether a buffer matches an expected file type
  • Runs the detection logic inside Rust and ships it through npm as WASM

Alt text

Supported types

  • png
  • jpeg
  • gif
  • pdf
  • zip
  • webp
  • wasm
  • gzip
  • bmp
  • tar

detectFileType returns the canonical labels above. matchesFileType also accepts common aliases, dot-prefixed extensions, and MIME types such as jpg, .png, image/jpeg, and application/x-gzip.

Prerequisites

Install the wasm target and wasm-pack if they are not already available:

rustup target add wasm32-unknown-unknown
cargo +stable install wasm-pack

Build

npm run build

This generates the published library files in dist/ and the internal browser wasm-pack build artifacts in pkg/browser.

The published library surface lives in dist/:

  • dist/index.mjs for browser/bundler consumers
  • dist/node.mjs and dist/node.cjs for Node consumers
  • dist/node-runtime/ for the Node wasm runtime files

The npm scripts prepend the rustup toolchain binary directory to PATH so cargo and rustc stay on the same toolchain, even on systems that also have a Homebrew Rust install.

Test

npm test

Usage

The root package export selects the right build for the runtime:

  • Node require() resolves to the Node wrapper
  • Node import resolves to the Node ESM wrapper
  • Browser/bundler import resolves to the Vite-packaged browser build

Explicit subpaths are also available:

  • file-type-magic/node
  • file-type-magic/browser

CommonJS

const {
  detectFileType,
  detectFileTypeFromStream,
  matchesFileType,
  isSupportedFile,
  requiredBytesForDetection,
} = require("file-type-magic");

const pngBytes = new Uint8Array([
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
]);

console.log(detectFileType(pngBytes));
console.log(matchesFileType(pngBytes, "png"));
console.log(isSupportedFile(pngBytes));

CommonJS stream

const fs = require("node:fs");
const { detectFileTypeFromStream, requiredBytesForDetection } = require("file-type-magic");

(async () => {
  console.log(requiredBytesForDetection);
  console.log(await detectFileTypeFromStream(fs.createReadStream("./file.png")));
})();

ESM

import {
  detectFileType,
  matchesFileType,
  isSupportedFile,
} from "file-type-magic";

const pngBytes = new Uint8Array([
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
]);

console.log(detectFileType(pngBytes));
console.log(matchesFileType(pngBytes, "png"));
console.log(isSupportedFile(pngBytes));

Browser stream

import { detectFileTypeFromStream, requiredBytesForDetection } from "file-type-magic";

const fileInput = document.querySelector("input[type=file]");

fileInput.addEventListener("change", async () => {
  const [file] = fileInput.files ?? [];

  if (!file) {
    return;
  }

  const kind = await detectFileTypeFromStream(file.stream());
  console.log(requiredBytesForDetection, kind);
});

API

detectFileType(bytes: Uint8Array): string | undefined

Returns the detected file type if the signature is known.

detectFileTypeFromStream(source: ReadableStream | AsyncIterable): Promise<string | undefined>

Reads only the first requiredBytesForDetection bytes from a binary stream and detects the file type.

matchesFileType(bytes: Uint8Array, expected: string): boolean

Returns true when the detected type matches expected, including canonical labels, common aliases, dot-prefixed extensions, and MIME types.

matchesFileTypeFromStream(source: ReadableStream | AsyncIterable, expected: string): Promise<boolean>

Reads only the first requiredBytesForDetection bytes from a binary stream and compares the detected type.

isSupportedFile(bytes: Uint8Array): boolean

Returns true when the byte buffer matches any supported signature.

isSupportedFileFromStream(source: ReadableStream | AsyncIterable): Promise<boolean>

Reads only the first requiredBytesForDetection bytes from a binary stream and reports whether the signature is supported.

requiredBytesForDetection: number

The number of bytes the stream helpers need to read to cover all supported signatures.