Install mmmagic via npm
masterInstall the mmmagic package using npm. Ensure you have Node.js v4.0.0 or newer installed.
npm install mmmagicrepository·master·Indexed 20 days ago
https://github.com/mscdex/mmmagicAn asynchronous Node.js binding for libmagic used to detect file content types, MIME types, and general descriptions via data inspection. It provides a Magic class to inspect files via filesystem paths using detectFile() or raw data via Buffers using detect().
Install the mmmagic package using npm. Ensure you have Node.js v4.0.0 or newer installed.
npm install mmmagicUse the Magic class to inspect files or buffers. You can configure the output format (e.g., general description vs. MIME type) by passing bitmask flags to the constructor.
1. Get general description of a file
By default, new Magic() returns a human-readable description.
2. Get MIME type for a file
Pass mmm.MAGIC_MIME_TYPE to the constructor to receive the MIME type string.
3. Get MIME type and encoding
Pass mmm.MAGIC_MIME (which is a combination of MAGIC_MIME_TYPE and MAGIC_MIME_ENCODING) to receive the MIME type and charset (e.g., application/x-dosexec; charset=binary).
4. Inspect a Buffer
Use the .detect(buffer, callback) method to inspect raw data without a file path.
var mmm = require('mmmagic'),
Magic = mmm.Magic;
// Get MIME type and encoding
var magic = new Magic(mmm.MAGIC_MIME);
magic.detectFile('path/to/file', function(err, result) {
if (err) throw err;
console.log(result); // e.g., 'application/x-dosexec; charset=binary'
});
// Inspect a Buffer
var buf = Buffer.from('import Options\nfrom os import unlink, symlink');
magic.detect(buf, function(err, result) {
if (err) throw err;
console.log(result); // e.g., 'Python script, ASCII text executable'
});libgnurx is a Win32 library containing the regex functionality extracted from glibc 2.5. It is designed to provide GNU regex capabilities on Windows without the heavy dependencies of the full glibc locale handling or i18n systems.
Important Limitations:
[[:alpha:]]) will most likely only work for single-byte codepages.libmagic library is the core component of the file(1) command. It allows 3rd party programs to identify file types by checking 'magic numbers' (signatures) of thousands of different file types without needing to use fork() and exec() to run the file command-line utility. This makes it an efficient way to integrate file type identification directly into your own applications.libmagic uses a set of rules defined in magic files (typically located at /etc/magic) to identify file types. The identification process generally follows these stages:
fsmagic): The first set of tests based on filesystem information.softmagic): The second set of tests based on the rules parsed from /etc/magic.ascmagic): A final set of tests based on hardwired assumptions within the code.It supports features like CDF file parsing, indirect magic, and recursion (name/use).
When compiling applications on Windows that require libgnurx, use the following library files depending on your build environment:
libgnurx.dll.a.configure and looks for the -lregex flag, use the provided libregex.a file.# If using configure scripts that look for -lregex:
# The library libregex.a is provided for this purpose.
# For standard GCC linking:
# Use libgnurx.dll.aThe Magic constructor creates a new instance for file inspection.
Signature:
new Magic(magicSource, flags)
magicSource (optional):String path to a compatible magic file.Buffer containing the contents of a compatible magic file.false: Tells mmmagic to search for a magic file using the MAGIC environment variable or standard filesystem paths.false, the bundled magic file is used.flags (optional): A bitmask of constants available on the require('mmmagic') object to configure behavior.Available Flags:
// Available constants on the mmmagic module:
// MAGIC_NONE, MAGIC_DEBUG, MAGIC_SYMLINK, MAGIC_DEVICES,
// MAGIC_MIME_TYPE, MAGIC_CONTINUE, MAGIC_CHECK, MAGIC_PRESERVE_ATIME,
// MAGIC_RAW, MAGIC_MIME_ENCODING, MAGIC_MIME, MAGIC_APPLE,
// MAGIC_NO_CHECK_TAR, MAGIC_NO_CHECK_SOFT, MAGIC_NO_CHECK_APPTYPE,
// MAGIC_NO_CHECK_ELF, MAGIC_NO_CHECK_TEXT, MAGIC_NO_CHECK_CDF,
// MAGIC_NO_CHECK_TOKENS, MAGIC_NO_CHECK_ENCODINGThe following flags can be used with the Magic constructor to modify inspection behavior. These are accessible as constants on the require('mmmagic') object.
MAGIC_NONE - No flags set
MAGIC_DEBUG - Turn on debugging
MAGIC_SYMLINK - Follow symlinks (default for non-Windows)
MAGIC_DEVICES - Look at the contents of devices
MAGIC_MIME_TYPE - Return the MIME type
MAGIC_CONTINUE - Return all matches (returned as an array of strings)
MAGIC_CHECK - Print warnings to stderr
MAGIC_PRESERVE_ATIME - Restore access time on exit
MAGIC_RAW - Don't translate unprintable chars
MAGIC_MIME_ENCODING - Return the MIME encoding
MAGIC_MIME - (MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING)
MAGIC_APPLE - Return the Apple creator and type
MAGIC_NO_CHECK_TAR - Don't check for tar files
MAGIC_NO_CHECK_SOFT - Don't check magic entries
MAGIC_NO_CHECK_APPTYPE - Don't check application type
MAGIC_NO_CHECK_ELF - Don't check for elf details
MAGIC_NO_CHECK_TEXT - Don't check for text files
MAGIC_NO_CHECK_CDF - Don't check for cdf files
MAGIC_NO_CHECK_TOKENS - Don't check tokens
MAGIC_NO_CHECK_ENCODING - Don't check text encodingsThe Magic class is the primary interface for interacting with the underlying magic library. It allows you to inspect files to determine their type, MIME type, or other metadata using various flags.
Note: The library automatically sets a fallback path for the magic database, but you can interact with the Magic class directly via the exported module.
const { Magic } = require('mmmagic');
// Example usage (assuming Magic methods are available on the class)
const magic = new Magic();
// magic.file('path/to/file', (err, result) => { ... });The mmmagic module exports several bitwise flags used to configure how file identification is performed. These flags can be combined using bitwise OR (|) to request multiple behaviors (e.g., returning both MIME type and encoding).
{
MAGIC_NONE: 0x000000, /* No flags (default for Windows) */
MAGIC_DEBUG: 0x000001, /* Turn on debugging */
MAGIC_SYMLINK: 0x000002, /* Follow symlinks (default for *nix) */
MAGIC_DEVICES: 0x000008, /* Look at the contents of devices */
MAGIC_MIME_TYPE: 0x000010, /* Return the MIME type */
MAGIC_CONTINUE: 0x000020, /* Return all matches */
MAGIC_CHECK: 0x000040, /* Print warnings to stderr */
MAGIC_PRESERVE_ATIME: 0x000080, /* Restore access time on exit */
MAGIC_RAW: 0x000100, /* Don't translate unprintable chars */
MAGIC_MIME_ENCODING: 0x000400, /* Return the MIME encoding */
MAGIC_MIME: (0x000010|0x000400),/* (MAGIC_MIME_TYPE|MAGIC_MIME_ENCODING) */
MAGIC_APPLE: 0x000800, /* Return the Apple creator and type */
/* Check suppression flags */
MAGIC_NO_CHECK_TAR: 0x002000, /* Don't check for tar files */
MAGIC_NO_CHECK_SOFT: 0x004000, /* Don't check magic entries */
MAGIC_NO_CHECK_APPTYPE: 0x008000, /* Don't check application type */
MAGIC_NO_CHECK_ELF: 0x010000, /* Don't check for elf details */
MAGIC_NO_CHECK_TEXT: 0x020000, /* Don't check for text files */
MAGIC_NO_CHECK_CDF: 0x040000, /* Don't check for cdf files */
MAGIC_NO_CHECK_TOKENS: 0x100000, /* Don't check tokens */
MAGIC_NO_CHECK_ENCODING: 0x200000 /* Don't check text encodings */
}Inspects the file located at the provided path.
Signature:
detectFile(path, callback)
path (String): The filesystem path to the file.callback (Function): A callback function receiving (err, result).err (Error | null): Error object if inspection fails, otherwise null.result (String): The inspection result (e.g., MIME type or description).magic.detectFile('path/to/file', function(err, result) {
if (err) throw err;
console.log(result);
});Inspects the contents of a provided Buffer.
Signature:
detect(data, callback)
data (Buffer): The buffer containing the data to inspect.callback (Function): A callback function receiving (err, result).err (Error | null): Error object if inspection fails, otherwise null.result (String): The inspection result (e.g., MIME type or description).var buf = Buffer.from('some data');
magic.detect(buf, function(err, result) {
if (err) throw err;
console.log(result);
});