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

@hechang27/tree-magic-js

v1.1.6

Published

Fast MIME type detection for Node.js, powered by Rust

Readme

tree-magic-js

Fast MIME type detection for Node.js, powered by Rust.

npm version License: MIT

Overview

tree-magic-js is a simple N-API wrapper that wraps Rust crate tree_magic_mini, which is a high-performance MIME type detection library. It can detect file types from both file buffers and file paths.

Installation

npm install @hechang27/tree-magic-js

Quick Start

import { inferFromBuffer, inferFromPath } from "@hechang27/tree-magic-js";
import { readFile } from "fs/promises";

// Detect MIME type from file buffer
const buffer = await readFile("example.jpg");
const mimeType = await inferFromBuffer(buffer);
console.log(mimeType); // 'image/jpeg'

// Detect MIME type from file path
const mimeType2 = await inferFromPath("./document.pdf");
console.log(mimeType2); // 'application/pdf'

API Reference

inferFromBuffer(bytes: Buffer): Promise<string>

Detects the MIME type of a file from its buffer contents.

Parameters:

  • bytes (Buffer): The file content as a Buffer

Returns:

  • Promise<string>: The detected MIME type (e.g., 'image/jpeg', 'text/plain')

Example:

import { inferFromBuffer } from "@hechang27/tree-magic-js";
import { readFile } from "fs/promises";

const buffer = await readFile("photo.png");
const mimeType = await inferFromBuffer(buffer);
console.log(mimeType); // 'image/png'

inferFromPath(path: string): Promise<string | null>

Detects the MIME type of a file from its file path.

Parameters:

  • path (string): Path to the file

Returns:

  • Promise<string | null>: The detected MIME type, or null if the file doesn't exist

Example:

import { inferFromPath } from "@hechang27/tree-magic-js";

const mimeType = await inferFromPath("./video.mp4");
console.log(mimeType); // 'video/mp4'

const notFound = await inferFromPath("./nonexistent.txt");
console.log(notFound); // null

matchBuffer(mimeType: string, bytes: Buffer): Promise<boolean>

Checks if a buffer matches a specific MIME type.

Parameters:

  • mimeType (string): The MIME type to check against
  • bytes (Buffer): The file content as a Buffer

Returns:

  • Promise<boolean>: true if the buffer matches the MIME type, false otherwise

Example:

import { matchBuffer } from "@hechang27/tree-magic-js";
import { readFile } from "fs/promises";

const buffer = await readFile("image.jpg");
const isJpeg = await matchBuffer("image/jpeg", buffer);
console.log(isJpeg); // true

const isPng = await matchBuffer("image/png", buffer);
console.log(isPng); // false

matchPath(mimeType: string, path: string): Promise<boolean>

Checks if a file matches a specific MIME type.

Parameters:

  • mimeType (string): The MIME type to check against
  • path (string): Path to the file

Returns:

  • Promise<boolean>: true if the file matches the MIME type, false otherwise

Example:

import { matchPath } from "@hechang27/tree-magic-js";

const isVideo = await matchPath("video/mp4", "./movie.mp4");
console.log(isVideo); // true

Configuration

tree-magic-js supports several environment variables for customization:

TREE_MAGIC_DIR

Specify a custom directory where the MIME database is located or should be stored.

export TREE_MAGIC_DIR="/path/to/custom/mime/database"

TREE_MAGIC_URL

Specify a custom URL to download the MIME database from.

export TREE_MAGIC_URL="https://custom-server.com/mime-database.zip"

XDG_DATA_HOME

Override the XDG data home directory (Linux/Unix).

export XDG_DATA_HOME="/custom/data/home"

XDG_DATA_DIRS

Override the XDG data directories (Linux/Unix).

export XDG_DATA_DIRS="/usr/local/share:/usr/share:/custom/share"

How It Works

On first use, tree-magic-js automatically:

  1. Searches for existing MIME databases in standard system locations:

    • Linux: /usr/share/mime, /usr/local/share/mime, ~/.local/share/mime
    • macOS: /opt/homebrew/share/mime
    • Windows: C:\msys64\mingw64\share\mime

    Or uses the mime database in provided location if TREE_MAGIC_DIR is present and valid

  2. Downloads the database if not found locally:

    • Downloads from the configured URL (default: GitHub release)
    • Extracts to platform-appropriate data directory and set the TREE_MAGIC_DIR to point to that location.
  3. The underlying rust library will use the MIME databases specified by TREE_MAGIC_DIR, if not present, it will attempt to search one of the system paths. Without the MIME database present, the library can only differentiate between text/plain and binary/octet-stream.

Platform Support

tree-magic-js provides pre-built native binaries for:

  • macOS: x64, ARM64 (Apple Silicon)
  • Windows: x64, x86, ARM64
  • Linux: x64, ARM64, ARMv7 (glibc and musl)
  • FreeBSD: x64, ARM64
  • Android: ARM64, ARMv7

System Requirements

  • Node.js: Version 22 or higher
  • Internet connection: Required for initial database download (if not already present)

Custom Database

To use a custom MIME database:

# Point to your custom database directory
export TREE_MAGIC_DIR="/path/to/custom/mime/db"

# Or provide a custom download URL
# default URL is https://github.com/hechang27-sprt/build-shared-mime-info/releases/download/db-20251031/mime-database.zip
export TREE_MAGIC_URL="https://your-server.com/custom-mime-db.zip"

Examples

File Upload Validation

import { inferFromBuffer, matchBuffer } from "@hechang27/tree-magic-js";

async function validateUpload(fileBuffer, allowedType) {
    if (await matchBuffer(allowedType, fileBuffer)) {
        return { valid: true, buf: fileBuffer };
    }

    return { valid: false };
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License - see the LICENSE file for details.

Links