Fast MIME type detection for Node.js, powered by Rust.
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.
npm install @hechang27/tree-magic-jsimport { 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'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'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, ornullif 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); // nullChecks if a buffer matches a specific MIME type.
Parameters:
mimeType(string): The MIME type to check againstbytes(Buffer): The file content as a Buffer
Returns:
Promise<boolean>:trueif the buffer matches the MIME type,falseotherwise
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); // falseChecks if a file matches a specific MIME type.
Parameters:
mimeType(string): The MIME type to check againstpath(string): Path to the file
Returns:
Promise<boolean>:trueif the file matches the MIME type,falseotherwise
Example:
import { matchPath } from "@hechang27/tree-magic-js";
const isVideo = await matchPath("video/mp4", "./movie.mp4");
console.log(isVideo); // truetree-magic-js supports several environment variables for customization:
Specify a custom directory where the MIME database is located or should be stored.
export TREE_MAGIC_DIR="/path/to/custom/mime/database"Specify a custom URL to download the MIME database from.
export TREE_MAGIC_URL="https://custom-server.com/mime-database.zip"Override the XDG data home directory (Linux/Unix).
export XDG_DATA_HOME="/custom/data/home"Override the XDG data directories (Linux/Unix).
export XDG_DATA_DIRS="/usr/local/share:/usr/share:/custom/share"On first use, tree-magic-js automatically:
-
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_DIRis present and valid - Linux:
-
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_DIRto point to that location.
-
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 betweentext/plainandbinary/octet-stream.
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
- Node.js: Version 22 or higher
- Internet connection: Required for initial database download (if not already present)
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"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 };
}Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see the LICENSE file for details.