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.
Install this library with NPM or Yarn.
NPM:
npm install check-magic-number
Yarn:
yarn add check-magic-number
import { readFileSync } from "fs";
import { fromBuffer } from "check-magic-number";
const extension = fromBuffer(Buffer.from(readFileSync("./example.png")));
Output:
png
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.
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.