file-type
Advanced tools
Comparing version 18.2.1 to 18.3.0
@@ -21,24 +21,5 @@ import type {FileTypeResult} from './core.js'; | ||
/** | ||
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, | ||
@@ -45,0 +26,0 @@ supportedMimeTypes, |
@@ -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, |
@@ -86,2 +86,3 @@ import type {Readable as ReadableStream} from 'node:stream'; | ||
| '3g2' | ||
| 'j2c' | ||
| 'jp2' | ||
@@ -148,3 +149,8 @@ | 'jpm' | ||
| 'dwg' | ||
| 'parquet'; | ||
| 'parquet' | ||
| 'class' | ||
| 'arj' | ||
| 'cpio' | ||
| 'ace' | ||
; // eslint-disable-line semi-style | ||
@@ -236,2 +242,3 @@ export type MimeType = | ||
| 'image/bpg' | ||
| 'image/j2c' | ||
| 'image/jp2' | ||
@@ -290,3 +297,8 @@ | 'image/jpx' | ||
| 'image/vnd.dwg' | ||
| 'application/x-parquet'; | ||
| 'application/x-parquet' | ||
| 'application/java-vm' | ||
| 'application/x-arj' | ||
| 'application/x-cpio' | ||
| 'application/x-ace-compressed' | ||
; // eslint-disable-line semi-style | ||
@@ -408,1 +420,19 @@ export type FileTypeResult = { | ||
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>; |
50
core.js
@@ -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 -- | ||
@@ -917,2 +936,9 @@ | ||
if (this.check([0xCA, 0xFE, 0xBA, 0xBE])) { | ||
return { | ||
ext: 'class', | ||
mime: 'application/java-vm', | ||
}; | ||
} | ||
// -- 6-byte signatures -- | ||
@@ -968,2 +994,9 @@ | ||
if (this.checkString('070707')) { | ||
return { | ||
ext: 'cpio', | ||
mime: 'application/x-cpio', | ||
}; | ||
} | ||
// -- 7-byte signatures -- | ||
@@ -994,2 +1027,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 -- | ||
@@ -1166,2 +1209,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])) { | ||
@@ -1168,0 +1218,0 @@ // JPEG-2000 family |
{ | ||
"name": "file-type", | ||
"version": "18.2.1", | ||
"version": "18.3.0", | ||
"description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer", | ||
@@ -135,2 +135,3 @@ "license": "MIT", | ||
"3gp", | ||
"j2c", | ||
"jp2", | ||
@@ -201,3 +202,7 @@ "jpm", | ||
"dwg", | ||
"parquet" | ||
"parquet", | ||
"class", | ||
"arj", | ||
"cpio", | ||
"ace" | ||
], | ||
@@ -211,8 +216,8 @@ "dependencies": { | ||
"@tokenizer/token": "^0.3.0", | ||
"@types/node": "^18.7.13", | ||
"ava": "^5.1.0", | ||
"@types/node": "^18.16.0", | ||
"ava": "^5.2.0", | ||
"commonmark": "^0.30.0", | ||
"noop-stream": "^1.0.0", | ||
"tsd": "^0.25.0", | ||
"xo": "^0.53.1" | ||
"tsd": "^0.28.1", | ||
"xo": "^0.54.1" | ||
}, | ||
@@ -219,0 +224,0 @@ "xo": { |
@@ -201,4 +201,2 @@ # file-type | ||
**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. | ||
@@ -350,2 +348,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 | ||
@@ -358,2 +357,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 | ||
@@ -372,2 +372,4 @@ - [`arw`](https://en.wikipedia.org/wiki/Raw_image_format#ARW) - Sony Alpha Raw 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) | ||
@@ -405,2 +407,3 @@ - [`cr3`](https://fileinfo.com/extension/cr3) - Canon Raw image file (v3) | ||
- [`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 | ||
@@ -407,0 +410,0 @@ - [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000 |
@@ -84,2 +84,3 @@ export const extensions = [ | ||
'3g2', | ||
'j2c', | ||
'jp2', | ||
@@ -146,2 +147,6 @@ 'jpm', | ||
'parquet', | ||
'class', | ||
'arj', | ||
'cpio', | ||
'ace', | ||
]; | ||
@@ -233,2 +238,3 @@ | ||
'image/bpg', | ||
'image/j2c', | ||
'image/jp2', | ||
@@ -289,2 +295,6 @@ 'image/jpx', | ||
'application/x-parquet', | ||
'application/java-vm', | ||
'application/x-arj', | ||
'application/x-cpio', | ||
'application/x-ace-compressed', | ||
]; |
84605
2190
513