Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
An async libmagic binding for node.js for detecting content types by data inspection
The mmmagic npm package is a Node.js library used for detecting the MIME type and encoding of files. It is a binding to the libmagic library, which is used to identify file types based on their content rather than their file extension.
Detect MIME type of a file
This feature allows you to detect the MIME type of a file. The code sample demonstrates how to use mmmagic to read a file and determine its MIME type, which is useful for handling files based on their content type.
const fs = require('fs');
const mmm = require('mmmagic'), Magic = mmm.Magic;
const magic = new Magic(mmm.MAGIC_MIME_TYPE);
magic.detectFile('path/to/file', function(err, result) {
if (err) throw err;
console.log(result); // e.g., 'image/jpeg'
});
Detect MIME encoding of a file
This feature allows you to detect the MIME encoding of a file. The code sample shows how to use mmmagic to determine the encoding of a file, which can be important for processing files correctly.
const fs = require('fs');
const mmm = require('mmmagic'), Magic = mmm.Magic;
const magic = new Magic(mmm.MAGIC_MIME_ENCODING);
magic.detectFile('path/to/file', function(err, result) {
if (err) throw err;
console.log(result); // e.g., 'binary'
});
Detect MIME type and encoding of a file
This feature allows you to detect both the MIME type and encoding of a file simultaneously. The code sample demonstrates how to use mmmagic to get both pieces of information in one call, which can be efficient for applications that need both details.
const fs = require('fs');
const mmm = require('mmmagic'), Magic = mmm.Magic;
const magic = new Magic(mmm.MAGIC_MIME);
magic.detectFile('path/to/file', function(err, result) {
if (err) throw err;
console.log(result); // e.g., 'image/jpeg; charset=binary'
});
The file-type package is a popular alternative to mmmagic for detecting the MIME type of files. It is a pure JavaScript implementation and does not require native bindings, making it easier to install and use across different platforms. However, it may not support as many file types as mmmagic, which relies on the extensive libmagic database.
The mime package is another alternative that provides a way to look up the MIME type based on file extension. Unlike mmmagic, it does not inspect the file content, so it is faster but less accurate for files with misleading extensions.
magic-bytes.js is a library that detects file types based on magic numbers (specific byte sequences at the start of files). It is similar to mmmagic in that it inspects file content, but it is implemented in JavaScript and may not cover as many file types as mmmagic, which uses the comprehensive libmagic database.
An async libmagic binding for node.js for detecting content types by data inspection.
npm install mmmagic
var Magic = require('mmmagic').Magic;
var magic = new Magic();
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
// output on Windows with 32-bit node:
// PE32 executable (DLL) (GUI) Intel 80386, for MS Windows
});
var mmm = require('mmmagic'),
Magic = mmm.Magic;
var magic = new Magic(mmm.MAGIC_MIME_TYPE);
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
// output on Windows with 32-bit node:
// application/x-dosexec
});
var mmm = require('mmmagic'),
Magic = mmm.Magic;
var magic = new Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_MIME_ENCODING);
// the above flags can also be shortened down to just: mmm.MAGIC_MIME
magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
if (err) throw err;
console.log(result);
// output on Windows with 32-bit node:
// application/x-dosexec; charset=binary
});
var Magic = require('mmmagic').Magic;
var magic = new Magic(),
buf = new Buffer('import Options\nfrom os import unlink, symlink');
magic.detect(buf, function(err, result) {
if (err) throw err;
console.log(result);
// output: Python script, ASCII text executable
});
(constructor)([< mixed >magicSource][, < Integer >flags]) - Creates and returns a new Magic instance. magicSource
(if specified) can either be a path string that points to a (compatible) magic file to use or it can be a Buffer containing the contents of a (compatible) magic file. If magicSource
is not a string and not false
, the bundled magic file will be used. If magicSource
is false
, mmmagic will default to searching for a magic file to use (order of magic file searching: MAGIC
env var -> various file system paths (see man file
)). flags is a bitmask with the following valid values (available as constants on require('mmmagic')
):
detectFile(< String >path, < Function >callback) - (void) - Inspects the file pointed at by path. The callback receives two arguments: an < Error > object in case of error (null otherwise), and a < String > containing the result of the inspection.
detect(< Buffer >data, < Function >callback) - (void) - Inspects the contents of data. The callback receives two arguments: an < Error > object in case of error (null otherwise), and a < String > containing the result of the inspection.
FAQs
An async libmagic binding for node.js for detecting content types by data inspection
The npm package mmmagic receives a total of 73,608 weekly downloads. As such, mmmagic popularity was classified as popular.
We found that mmmagic demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.