file-type
Advanced tools
Comparing version 11.1.0 to 12.0.0
@@ -8,2 +8,3 @@ /// <reference types="node"/> | ||
| 'png' | ||
| 'apng' | ||
| 'gif' | ||
@@ -17,2 +18,4 @@ | 'webp' | ||
| 'nef' | ||
| 'rw2' | ||
| 'raf' | ||
| 'tif' | ||
@@ -19,0 +22,0 @@ | 'bmp' |
58
index.js
@@ -20,5 +20,6 @@ 'use strict'; | ||
const check = (header, options) => { | ||
options = Object.assign({ | ||
offset: 0 | ||
}, options); | ||
options = { | ||
offset: 0, | ||
...options | ||
}; | ||
@@ -50,2 +51,20 @@ for (let i = 0; i < header.length; i++) { | ||
if (check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) { | ||
// APNG format (https://wiki.mozilla.org/APNG_Specification) | ||
// 1. Find the first IDAT (image data) chunk (49 44 41 54) | ||
// 2. Check if there is an "acTL" chunk before the IDAT one (61 63 54 4C) | ||
// Offset calculated as follows: | ||
// - 8 bytes: PNG signature | ||
// - 4 (length) + 4 (chunk type) + 13 (chunk data) + 4 (CRC): IHDR chunk | ||
const startIndex = 33; | ||
const firstImageDataChunkIndex = buffer.findIndex((el, i) => i >= startIndex && buffer[i] === 0x49 && buffer[i + 1] === 0x44 && buffer[i + 2] === 0x41 && buffer[i + 3] === 0x54); | ||
const sliced = buffer.subarray(startIndex, firstImageDataChunkIndex); | ||
if (sliced.findIndex((el, i) => sliced[i] === 0x61 && sliced[i + 1] === 0x63 && sliced[i + 2] === 0x54 && sliced[i + 3] === 0x4C) >= 0) { | ||
return { | ||
ext: 'apng', | ||
mime: 'image/apng' | ||
}; | ||
} | ||
return { | ||
@@ -96,3 +115,8 @@ ext: 'png', | ||
if (check([0x49, 0x49, 0x2A, 0x00, 0x10, 0xFB, 0x86, 0x01])) { | ||
if ( | ||
check([0x49, 0x49, 0x2A, 0x00]) && | ||
(check([0x10, 0xFB, 0x86, 0x01], {offset: 4}) || | ||
check([0x08, 0x00, 0x00, 0x00, 0x13, 0x00], {offset: 4}) || | ||
check([0x08, 0x00, 0x00, 0x00, 0x12, 0x00], {offset: 4})) | ||
) { | ||
return { | ||
@@ -104,3 +128,7 @@ ext: 'arw', | ||
if (check([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00, 0x2D])) { | ||
if ( | ||
check([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00]) && | ||
(check([0x2D, 0x00, 0xFE, 0x00], {offset: 8}) || | ||
check([0x27, 0x00, 0xFE, 0x00], {offset: 8})) | ||
) { | ||
return { | ||
@@ -112,3 +140,6 @@ ext: 'dng', | ||
if (check([0x49, 0x49, 0x2A, 0x00, 0x30, 0x3D, 0x72, 0x01, 0x1C])) { | ||
if ( | ||
check([0x49, 0x49, 0x2A, 0x00]) && | ||
check([0x1C, 0x00, 0xFE, 0x00], {offset: 8}) | ||
) { | ||
return { | ||
@@ -120,2 +151,17 @@ ext: 'nef', | ||
if (check([0x49, 0x49, 0x55, 0x00, 0x18, 0x00, 0x00, 0x00, 0x88, 0xE7, 0x74, 0xD8])) { | ||
return { | ||
ext: 'rw2', | ||
mime: 'image/x-panasonic-rw2' | ||
}; | ||
} | ||
// `raf` is here just to keep all the raw image detectors together. | ||
if (checkString('FUJIFILMCCD-RAW')) { | ||
return { | ||
ext: 'raf', | ||
mime: 'image/x-fujifilm-raf' | ||
}; | ||
} | ||
if ( | ||
@@ -122,0 +168,0 @@ check([0x49, 0x49, 0x2A, 0x0]) || |
{ | ||
"name": "file-type", | ||
"version": "11.1.0", | ||
"version": "12.0.0", | ||
"description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
@@ -46,2 +46,3 @@ "scripts": { | ||
"png", | ||
"apng", | ||
"gif", | ||
@@ -55,2 +56,4 @@ "webp", | ||
"nef", | ||
"rw2", | ||
"raf", | ||
"tif", | ||
@@ -145,4 +148,5 @@ "bmp", | ||
"devDependencies": { | ||
"@types/node": "^11.12.2", | ||
"@types/node": "^12.0.2", | ||
"ava": "^1.4.1", | ||
"noop-stream": "^0.1.0", | ||
"pify": "^4.0.1", | ||
@@ -149,0 +153,0 @@ "read-chunk": "^3.2.0", |
@@ -14,7 +14,3 @@ # file-type [](https://travis-ci.org/sindresorhus/file-type) | ||
<a href="https://www.patreon.com/sindresorhus"> | ||
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160"> | ||
</a> | ||
## Usage | ||
@@ -46,2 +42,3 @@ | ||
response.destroy(); | ||
console.log(fileType(chunk)); | ||
@@ -56,2 +53,3 @@ //=> {ext: 'gif', mime: 'image/gif'} | ||
```js | ||
const stream = require('stream'); | ||
const fs = require('fs'); | ||
@@ -65,3 +63,3 @@ const crypto = require('crypto'); | ||
const stream = await fileType.stream(read.pipe(decipher)); | ||
const stream = await fileType.stream(stream.pipeline(read, decipher)); | ||
@@ -97,3 +95,3 @@ console.log(stream.fileType); | ||
Returns an `Object` with: | ||
Returns an `object` with: | ||
@@ -134,2 +132,3 @@ - `ext` - One of the [supported file types](#supported-file-types) | ||
- [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics) | ||
- [`apng`](https://en.wikipedia.org/wiki/APNG) - Animated Portable Network Graphics | ||
- [`gif`](https://en.wikipedia.org/wiki/GIF) | ||
@@ -143,2 +142,4 @@ - [`webp`](https://en.wikipedia.org/wiki/WebP) | ||
- [`nef`](https://www.nikonusa.com/en/learn-and-explore/a/products-and-innovation/nikon-electronic-format-nef.html) - Nikon Electronic Format image file | ||
- [`rw2`](https://en.wikipedia.org/wiki/Raw_image_format) - Panasonic RAW image file | ||
- [`raf`](https://en.wikipedia.org/wiki/Raw_image_format) - Fujifilm RAW image file | ||
- [`tif`](https://en.wikipedia.org/wiki/Tagged_Image_File_Format) | ||
@@ -254,3 +255,3 @@ - [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format) | ||
## Created by | ||
## Maintainers | ||
@@ -261,4 +262,12 @@ - [Sindre Sorhus](https://github.com/sindresorhus) | ||
## License | ||
--- | ||
MIT | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-file-type?utm_source=npm-file-type&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
42997
1128
265
7