Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
file-type
Advanced tools
The file-type npm package is used to detect the file type of a Buffer/Uint8Array/ArrayBuffer. It supports many file types including images, audio, video, fonts, and archive formats. It is particularly useful when the file extension is unknown or has been changed, as it checks the file signature against a list of known file types.
Detecting file type from a Buffer
This feature allows you to detect the file type of a file by reading it into a Buffer and using the `fromBuffer` method to determine the file type.
const FileType = require('file-type');
const fs = require('fs');
(async () => {
const buffer = fs.readFileSync('example.png');
const fileType = await FileType.fromBuffer(buffer);
console.log(fileType);
})();
Detecting file type from a stream
This feature allows you to detect the file type of a file by creating a readable stream and using the `fromStream` method to determine the file type.
const FileType = require('file-type');
const fs = require('fs');
(async () => {
const stream = fs.createReadStream('example.png');
const fileType = await FileType.fromStream(stream);
console.log(fileType);
})();
Detecting file type from a file path
This feature allows you to detect the file type directly from a file path using the `fromFile` method.
const FileType = require('file-type');
(async () => {
const fileType = await FileType.fromFile('example.png');
console.log(fileType);
})();
The 'mmmagic' package is an async libmagic binding for node.js for content type detection. It uses magic numbers to detect the file type, similar to file-type, but it requires libmagic to be installed on the system, which can be a downside compared to the pure JavaScript implementation of file-type.
The 'buffer-type' package is another module for detecting the content type of a Buffer. It is less popular and has fewer file signatures compared to file-type, which means it may not recognize as many file types.
The 'file-signature' package allows for identifying file types by checking their magic number signature. It is similar to file-type but has a smaller set of supported file types and a simpler API.
Detect the file type of a Buffer/Uint8Array
$ npm install --save file-type
$ bower install --save file-type
$ component install sindresorhus/file-type
var readChunk = require('read-chunk'); // npm install read-chunk
var fileType = require('file-type');
var buffer = readChunk('unicorn.png', 0, 12);
fileType(buffer);
//=> png
var xhr = new XMLHttpRequest();
xhr.open('GET', 'unicorn.png');
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
fileType(new Uint8Array(this.response));
//=> png
};
xhr.send();
Returns one of the supported file types or false
.
Type: buffer
(Node.js), uint8array
It only needs the first 12 bytes.
$ npm install --global file-type
$ file-type --help
Usage
$ cat <filename> | file-type
$ file-type <filename>
Example
$ cat unicorn.png | file-type
png
PR welcome for additional file types
MIT © Sindre Sorhus
FAQs
Detect the file type of a file, stream, or data
The npm package file-type receives a total of 18,935,561 weekly downloads. As such, file-type popularity was classified as popular.
We found that file-type demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.