check-magic-number
v1.0.1
Published
Check magic number from Buffer
Downloads
15
Readme
Check Magic Number
A library to determine the file type of a given buffer of data. It works by checking the start of the buffer against a known set of "magic numbers" that are associated with specific file types.
What Do I Use
Install
Install this library with NPM or Yarn.
NPM:
npm install check-magic-number
Yarn:
yarn add check-magic-number
Usage
import { readFileSync } from "fs";
import { fromBuffer } from "check-magic-number";
const extension = fromBuffer(Buffer.from(readFileSync("./example.png")));
Output:
png
Supported Types
The following file types are currently supported:
- PNG
- JPG
- GIF
The list of supported file types can be found in the magicNumbers object in the source code.
Limitations
This library only examines the start of the given buffer to determine the file type. This means that it may not be able to identify some file types, or it may identify a file as the wrong type if the buffer contains data that happens to match one of the magic numbers at the start of the file.
In general, this library should be considered a best effort and not relied upon for definitive identification of file types.