Socket
Socket
Sign inDemoInstall

file-type

Package Overview
Dependencies
Maintainers
1
Versions
149
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

file-type - npm Package Compare versions

Comparing version 18.0.0 to 18.5.0

29

browser.d.ts

@@ -1,2 +0,2 @@

import {FileTypeResult} from './core.js';
import type {FileTypeResult} from './core.js';

@@ -21,29 +21,10 @@ /**

/**
Detect the file type of a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
__Note:__ This method is only available in the browser.
@example
```
import {fileTypeFromBlob} from 'file-type';
const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
type: 'plain/text',
endings: 'native'
});
console.log(await fileTypeFromBlob(blob));
//=> {ext: 'txt', mime: 'plain/text'}
```
*/
export declare function fileTypeFromBlob(blob: Blob): Promise<FileTypeResult | undefined>;
export {
fileTypeFromBuffer,
fileTypeFromBlob,
supportedExtensions,
supportedMimeTypes,
FileTypeResult,
FileExtension,
MimeType,
type FileTypeResult,
type FileExtension,
type MimeType,
} from './core.js';

@@ -1,35 +0,4 @@

import {Buffer} from 'node:buffer';
import {ReadableWebToNodeStream} from 'readable-web-to-node-stream';
import {fileTypeFromBuffer, fileTypeFromStream as coreFileTypeFromStream} from './core.js';
import {fileTypeFromStream as coreFileTypeFromStream} from './core.js';
/**
Convert Blobs to ArrayBuffer.
@param {Blob} blob - Web API Blob.
@returns {Promise<ArrayBuffer>}
*/
function blobToArrayBuffer(blob) {
if (blob.arrayBuffer) {
return blob.arrayBuffer();
}
// TODO: Remove when stop supporting older environments
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.addEventListener('loadend', event => {
resolve(event.target.result);
});
fileReader.addEventListener('error', event => {
reject(new Error(event.message));
});
fileReader.addEventListener('abort', event => {
reject(new Error(event.type));
});
fileReader.readAsArrayBuffer(blob);
});
}
export async function fileTypeFromStream(stream) {

@@ -42,7 +11,2 @@ const readableWebToNodeStream = new ReadableWebToNodeStream(stream);

export async function fileTypeFromBlob(blob) {
const buffer = await blobToArrayBuffer(blob);
return fileTypeFromBuffer(Buffer.from(buffer));
}
export {

@@ -49,0 +13,0 @@ fileTypeFromTokenizer,

58

core.d.ts

@@ -1,3 +0,3 @@

import {Readable as ReadableStream} from 'node:stream';
import {ITokenizer} from 'strtok3';
import type {Readable as ReadableStream} from 'node:stream';
import type {ITokenizer} from 'strtok3';

@@ -86,2 +86,3 @@ export type FileExtension =

| '3g2'
| 'j2c'
| 'jp2'

@@ -144,3 +145,14 @@ | 'jpm'

| 'jxl'
| 'vcf';
| 'vcf'
| 'jls'
| 'pst'
| 'dwg'
| 'parquet'
| 'class'
| 'arj'
| 'cpio'
| 'ace'
| 'avro'
| 'icc'
; // eslint-disable-line semi-style

@@ -232,2 +244,3 @@ export type MimeType =

| 'image/bpg'
| 'image/j2c'
| 'image/jp2'

@@ -282,5 +295,16 @@ | 'image/jpx'

| 'image/jxl'
| 'application/zstd';
| 'application/zstd'
| 'image/jls'
| 'application/vnd.ms-outlook'
| 'image/vnd.dwg'
| 'application/x-parquet'
| 'application/java-vm'
| 'application/x-arj'
| 'application/x-cpio'
| 'application/x-ace-compressed'
| 'application/avro'
| 'application/vnd.iccprofile'
; // eslint-disable-line semi-style
export interface FileTypeResult {
export type FileTypeResult = {
/**

@@ -295,3 +319,3 @@ One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).

readonly mime: MimeType;
}
};

@@ -362,3 +386,3 @@ export type ReadableStreamWithFileType = ReadableStream & {

export interface StreamOptions {
export type StreamOptions = {
/**

@@ -370,3 +394,3 @@ The default sample size in bytes.

readonly sampleSize?: number;
}
};

@@ -403,1 +427,19 @@ /**

export function fileTypeStream(readableStream: ReadableStream, options?: StreamOptions): Promise<ReadableStreamWithFileType>;
/**
Detect the file type of a [`Blob`](https://nodejs.org/api/buffer.html#class-blob).
@example
```
import {fileTypeFromBlob} from 'file-type';
const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
type: 'plain/text',
endings: 'native'
});
console.log(await fileTypeFromBlob(blob));
//=> {ext: 'txt', mime: 'plain/text'}
```
*/
export declare function fileTypeFromBlob(blob: Blob): Promise<FileTypeResult | undefined>;

@@ -36,2 +36,7 @@ import {Buffer} from 'node:buffer';

export async function fileTypeFromBlob(blob) {
const buffer = await blob.arrayBuffer();
return fileTypeFromBuffer(new Uint8Array(buffer));
}
function _check(buffer, headers, options) {

@@ -148,2 +153,16 @@ options = {

if (this.check([0xC7, 0x71])) {
return {
ext: 'cpio',
mime: 'application/x-cpio',
};
}
if (this.check([0x60, 0xEA])) {
return {
ext: 'arj',
mime: 'application/x-arj',
};
}
// -- 3-byte signatures --

@@ -164,9 +183,2 @@

if (this.check([0xFF, 0xD8, 0xFF])) {
return {
ext: 'jpg',
mime: 'image/jpeg',
};
}
if (this.check([0x49, 0x49, 0xBC])) {

@@ -228,2 +240,24 @@ return {

// Requires a sample size of 4 bytes
if (this.check([0xFF, 0xD8, 0xFF])) {
if (this.check([0xF7], {offset: 3})) { // JPG7/SOF55, indicating a ISO/IEC 14495 / JPEG-LS file
return {
ext: 'jls',
mime: 'image/jls',
};
}
return {
ext: 'jpg',
mime: 'image/jpeg',
};
}
if (this.check([0x4F, 0x62, 0x6A, 0x01])) {
return {
ext: 'avro',
mime: 'application/avro',
};
}
if (this.checkString('FLIF')) {

@@ -610,13 +644,20 @@ return {

if (this.checkString('%PDF')) {
await tokenizer.ignore(1350);
const maxBufferSize = 10 * 1024 * 1024;
const buffer = Buffer.alloc(Math.min(maxBufferSize, tokenizer.fileInfo.size));
await tokenizer.readBuffer(buffer, {mayBeLess: true});
try {
await tokenizer.ignore(1350);
const maxBufferSize = 10 * 1024 * 1024;
const buffer = Buffer.alloc(Math.min(maxBufferSize, tokenizer.fileInfo.size));
await tokenizer.readBuffer(buffer, {mayBeLess: true});
// Check if this is an Adobe Illustrator file
if (buffer.includes(Buffer.from('AIPrivateData'))) {
return {
ext: 'ai',
mime: 'application/postscript',
};
// Check if this is an Adobe Illustrator file
if (buffer.includes(Buffer.from('AIPrivateData'))) {
return {
ext: 'ai',
mime: 'application/postscript',
};
}
} catch (error) {
// Swallow end of stream error if file is too small for the Adobe AI check
if (!(error instanceof strtok3.EndOfStreamError)) {
throw error;
}
}

@@ -808,2 +849,16 @@

if (this.check([0x21, 0x42, 0x44, 0x4E])) {
return {
ext: 'pst',
mime: 'application/vnd.ms-outlook',
};
}
if (this.checkString('PAR1')) {
return {
ext: 'parquet',
mime: 'application/x-parquet',
};
}
// -- 5-byte signatures --

@@ -892,2 +947,9 @@

if (this.check([0xCA, 0xFE, 0xBA, 0xBE])) {
return {
ext: 'class',
mime: 'application/java-vm',
};
}
// -- 6-byte signatures --

@@ -933,2 +995,19 @@

if (this.checkString('AC')) {
const version = this.buffer.toString('binary', 2, 6);
if (version.match('^d*') && version >= 1000 && version <= 1050) {
return {
ext: 'dwg',
mime: 'image/vnd.dwg',
};
}
}
if (this.checkString('070707')) {
return {
ext: 'cpio',
mime: 'application/x-cpio',
};
}
// -- 7-byte signatures --

@@ -959,2 +1038,12 @@

if (this.checkString('**ACE', {offset: 7})) {
await tokenizer.peekBuffer(this.buffer, {length: 14, mayBeLess: true});
if (this.checkString('**', {offset: 12})) {
return {
ext: 'ace',
mime: 'application/x-ace-compressed',
};
}
}
// -- 8-byte signatures --

@@ -1131,2 +1220,9 @@

if (this.check([0xFF, 0x4F, 0xFF, 0x51])) {
return {
ext: 'j2c',
mime: 'image/j2c',
};
}
if (this.check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) {

@@ -1228,2 +1324,9 @@ // JPEG-2000 family

if (this.check([0x61, 0x63, 0x73, 0x70], {offset: 36})) {
return {
ext: 'icc',
mime: 'application/vnd.iccprofile',
};
}
// -- 15-byte signatures --

@@ -1497,4 +1600,4 @@

await this.tokenizer.ignore(ifdOffset);
const fileType = await this.readTiffIFD(false);
return fileType ? fileType : {
const fileType = await this.readTiffIFD(bigEndian);
return fileType ?? {
ext: 'tif',

@@ -1501,0 +1604,0 @@ mime: 'image/tiff',

@@ -1,3 +0,2 @@

import {Readable as ReadableStream} from 'node:stream';
import {FileTypeResult} from './core.js';
import type {FileTypeResult} from './core.js';

@@ -4,0 +3,0 @@ /**

{
"name": "file-type",
"version": "18.0.0",
"version": "18.5.0",
"description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer",

@@ -135,2 +135,3 @@ "license": "MIT",

"3gp",
"j2c",
"jp2",

@@ -197,3 +198,13 @@ "jpm",

"jxl",
"vcf"
"vcf",
"jls",
"pst",
"dwg",
"parquet",
"class",
"arj",
"cpio",
"ace",
"avro",
"icc"
],

@@ -207,8 +218,8 @@ "dependencies": {

"@tokenizer/token": "^0.3.0",
"@types/node": "^18.7.13",
"ava": "^4.3.1",
"@types/node": "^20.1.2",
"ava": "^5.2.0",
"commonmark": "^0.30.0",
"noop-stream": "^1.0.0",
"tsd": "^0.22.0",
"xo": "^0.51.0"
"tsd": "^0.28.1",
"xo": "^0.54.2"
},

@@ -225,3 +236,5 @@ "xo": {

"@typescript-eslint/no-unsafe-assignment": "off",
"unicorn/text-encoding-identifier-case": "off"
"unicorn/text-encoding-identifier-case": "off",
"unicorn/switch-case-braces": "off",
"unicorn/prefer-top-level-await": "off"
}

@@ -228,0 +241,0 @@ },

@@ -11,32 +11,2 @@ # file-type

<br>
---
<div align="center">
<p>
<p>
<sup>
<a href="https://github.com/sponsors/sindresorhus">My open source work is supported by the community</a>
</sup>
</p>
<sup>Special thanks to:</sup>
<br>
<br>
<a href="https://bit.io/?utm_campaign=github_repo&utm_medium=referral&utm_content=file-type&utm_source=github">
<div>
<img src="https://sindresorhus.com/assets/thanks/bitio-logo.svg" width="190" alt="bit.io">
</div>
<b>Instant, shareable cloud PostgreSQL database</b>
<div>
<sup>Import any dataset in seconds, share with anyone with a click, try without signing up</sup>
</div>
</a>
</p>
</div>
---
<br>
## Install

@@ -202,4 +172,2 @@

**Note:** This method is only available in the browser.
The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.

@@ -351,2 +319,3 @@

- [`ac3`](https://www.atsc.org/standard/a522012-digital-audio-compression-ac-3-e-ac-3-standard-12172012/) - ATSC A/52 Audio File
- [`ace`](https://en.wikipedia.org/wiki/ACE_(compressed_file_format)) - ACE archive
- [`ai`](https://en.wikipedia.org/wiki/Adobe_Illustrator_Artwork) - Adobe Illustrator Artwork

@@ -359,2 +328,3 @@ - [`aif`](https://en.wikipedia.org/wiki/Audio_Interchange_File_Format) - Audio Interchange file

- [`ar`](https://en.wikipedia.org/wiki/Ar_(Unix)) - Archive file
- [`arj`](https://en.wikipedia.org/wiki/ARJ) - Archive file
- [`arrow`](https://arrow.apache.org) - Columnar format for tables of data

@@ -366,2 +336,3 @@ - [`arw`](https://en.wikipedia.org/wiki/Raw_image_format#ARW) - Sony Alpha Raw image file

- [`avif`](https://en.wikipedia.org/wiki/AV1#AV1_Image_File_Format_(AVIF)) - AV1 Image File Format
- [`avro`](https://en.wikipedia.org/wiki/Apache_Avro#Avro_Object_Container_File) - Object container file developed by Apache Avro
- [`blend`](https://wiki.blender.org/index.php/Dev:Source/Architecture/File_Format) - Blender project

@@ -374,2 +345,4 @@ - [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format) - Bitmap image file

- [`chm`](https://en.wikipedia.org/wiki/Microsoft_Compiled_HTML_Help) - Microsoft Compiled HTML Help
- [`class`](https://en.wikipedia.org/wiki/Java_class_file) - Java class file
- [`cpio`](https://en.wikipedia.org/wiki/Cpio) - Cpio archive
- [`cr2`](https://fileinfo.com/extension/cr2) - Canon Raw image file (v2)

@@ -385,2 +358,3 @@ - [`cr3`](https://fileinfo.com/extension/cr3) - Canon Raw image file (v3)

- [`dsf`](https://dsd-guide.com/sites/default/files/white-papers/DSFFileFormatSpec_E.pdf) - Sony DSD Stream File (DSF)
- [`dwg`](https://en.wikipedia.org/wiki/.dwg) - Autodesk CAD file
- [`elf`](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) - Unix Executable and Linkable Format

@@ -402,2 +376,3 @@ - [`eot`](https://en.wikipedia.org/wiki/Embedded_OpenType) - Embedded OpenType font

- [`heic`](https://nokiatech.github.io/heif/technical.html) - High Efficiency Image File Format
- [`icc`](https://en.wikipedia.org/wiki/ICC_profile) - ICC Profile
- [`icns`](https://en.wikipedia.org/wiki/Apple_Icon_Image_format) - Apple Icon image

@@ -408,2 +383,4 @@ - [`ico`](https://en.wikipedia.org/wiki/ICO_(file_format)) - Windows icon file

- [`it`](https://wiki.openmpt.org/Manual:_Module_formats#The_Impulse_Tracker_format_.28.it.29) - Audio module format: Impulse Tracker
- [`j2c`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`jls`](https://en.wikipedia.org/wiki/Lossless_JPEG#JPEG-LS) - Lossless/near-lossless compression standard for continuous-tone images
- [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000

@@ -422,3 +399,3 @@ - [`jpg`](https://en.wikipedia.org/wiki/JPEG) - Joint Photographic Experts Group image

- [`m4p`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#Filename_extensions) - MPEG-4 files with audio streams encrypted by FairPlay Digital Rights Management as were sold through the iTunes Store
- [`m4v`](https://en.wikipedia.org/wiki/M4V) - MPEG-4 Visual bitstreams
- [`m4v`](https://en.wikipedia.org/wiki/M4V) - Video container format developed by Apple, which is very similar to the MP4 format
- [`mid`](https://en.wikipedia.org/wiki/MIDI) - Musical Instrument Digital Interface file

@@ -451,2 +428,3 @@ - [`mie`](https://en.wikipedia.org/wiki/Sidecar_file) - Dedicated meta information format which supports storage of binary as well as textual meta information

- [`otf`](https://en.wikipedia.org/wiki/OpenType) - OpenType font
- [`parquet`](https://en.wikipedia.org/wiki/Apache_Parquet) - Apache Parquet
- [`pcap`](https://wiki.wireshark.org/Development/LibpcapFileFormat) - Libpcap File Format

@@ -459,2 +437,3 @@ - [`pdf`](https://en.wikipedia.org/wiki/Portable_Document_Format) - Portable Document Format

- [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format) - Adobe Photoshop document
- [`pst`](https://en.wikipedia.org/wiki/Personal_Storage_Table) - Personal Storage Table file
- [`qcp`](https://en.wikipedia.org/wiki/QCP) - Tagged and chunked data

@@ -505,8 +484,2 @@ - [`raf`](https://en.wikipedia.org/wiki/Raw_image_format) - Fujifilm RAW image file

## file-type for enterprise
Available as part of the Tidelift Subscription.
The maintainers of file-type and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-file-type?utm_source=npm-file-type&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
## Related

@@ -519,4 +492,7 @@

- [Sindre Sorhus](https://github.com/sindresorhus)
- [Borewit](https://github.com/Borewit)
**Former**
- [Mikael Finstad](https://github.com/mifi)
- [Ben Brook](https://github.com/bencmbrook)
- [Borewit](https://github.com/Borewit)

@@ -84,2 +84,3 @@ export const extensions = [

'3g2',
'j2c',
'jp2',

@@ -142,2 +143,12 @@ 'jpm',

'vcf',
'jls',
'pst',
'dwg',
'parquet',
'class',
'arj',
'cpio',
'ace',
'avro',
'icc',
];

@@ -229,2 +240,3 @@

'image/bpg',
'image/j2c',
'image/jp2',

@@ -281,2 +293,12 @@ 'image/jpx',

'application/zstd',
'image/jls',
'application/vnd.ms-outlook',
'image/vnd.dwg',
'application/x-parquet',
'application/java-vm',
'application/x-arj',
'application/x-cpio',
'application/x-ace-compressed',
'application/avro',
'application/vnd.iccprofile',
];
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc