Home
Docs
GitHub
Pricing
Blog
Log In

Run Sandworm Audit for your App

Get started
Hold on, we're currently generating a fresh version of this report
Package created
24 Apr 2014
Version published
7 Jan 2024
Maintainers
1
Total deps
0
Direct deps
0
License
MIT
Generating a report...
Hold on while we generate a fresh audit report for this package.

Frequently Asked Questions

What does file-type do?

File-Type is a powerful npm package developed by Sindre Sorhus that allows for the detection of a file type from a Buffer, Uint8Array, or ArrayBuffer. This resource is designed to detect binary-based file formats and checks the magic number of a buffer to ascertain the file type. However, keep in mind that File-Type is not suitable for text-based formats like '.txt', '.csv', and '.svg'. Notably, the package also welcomes contributions for commonly used modern file formats.

How do you use file-type?

To utilize File-Type, you would first install the npm package using the command npm install file-type. It's important to note that this package is an ESM package, meaning your project also needs to be ESM.

When it comes to using File-Type in your project, there are multiple methods you can use, ranging from detecting file types from files, buffers, streams, and more. For instance, if you want to determine the file type from a file, you can import fileTypeFromFile from 'file-type' and use it as follows:

import {fileTypeFromFile} from 'file-type';

console.log(await fileTypeFromFile('Unicorn.png'));
//=> {ext: 'png', mime: 'image/png'}

In a case where you want to determine the file type from a Buffer, you can import fileTypeFromBuffer and readChunk:

import {fileTypeFromBuffer} from 'file-type';
import {readChunk} from 'read-chunk';

const buffer = await readChunk('Unicorn.png', {length: 4100});

console.log(await fileTypeFromBuffer(buffer));
//=> {ext: 'png', mime: 'image/png'}

For determining a file type from a stream:

import fs from 'node:fs';
import {fileTypeFromStream} from 'file-type';

const stream = fs.createReadStream('Unicorn.mp4');

console.log(await fileTypeFromStream(stream));
//=> {ext: 'mp4', mime: 'video/mp4'}

Then you could also determine file type from a remote:

import got from 'got';
import {fileTypeFromStream} from 'file-type';

const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';

const stream = got.stream(url);

console.log(await fileTypeFromStream(stream));
//=> {ext: 'jpg', mime: 'image/jpeg'}

For usage in a browser:

import {fileTypeFromStream} from 'file-type';

const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';

const response = await fetch(url);
const fileType = await fileTypeFromStream(response.body);

console.log(fileType);
//=> {ext: 'jpg', mime: 'image/jpeg'}

Where are the file-type docs?

Comprehensive documentation for File-Type can be found directly in the File-Type repository on GitHub. This documentation includes details on its usage, API, and supported file types. Whether you're trying to identify the file type of a Buffer, Uint8Array or ArrayBuffer, or trying to understand the promise for an object that's returned from the numerous methods; the File-Type docs provide all the pertinent information.