Skip to content

Commit

Permalink
Require Node.js 6, add more image types, add TypeScript definition (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 6, 2019
1 parent 3fbcd9a commit a1287d2
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sudo: false
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
49 changes: 49 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference types="node"/>

export type ImageType =
| 'jpg'
| 'png'
| 'gif'
| 'webp'
| 'flif'
| 'cr2'
| 'tif'
| 'bmp'
| 'jxr'
| 'psd'
| 'ico'
| 'bpg'
| 'jp2'
| 'jpm'
| 'jpx'
| 'heic'
| 'cur'
| 'dcm';

export interface ImageTypeResult {
/**
* One of the supported [file types](https://github.com/sindresorhus/image-type#supported-file-types).
*/
ext: ImageType;

/**
* The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
*/
mime: string;
}

/**
* Detect the image type of a `Buffer`/`Uint8Array`.
*
* @param input - Input to examine to determine the file type. It only needs the first `.minimumBytes` bytes.
*/
declare const imageType: {
(input: Buffer | Uint8Array): ImageTypeResult | null;

/**
* The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hard-code it.
*/
readonly minimumBytes: number;
};

export default imageType;
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@ const imageExts = new Set([
'png',
'gif',
'webp',
'flif',
'cr2',
'tif',
'bmp',
'jxr',
'psd'
'psd',
'ico',
'bpg',
'jp2',
'jpm',
'jpx',
'heic',
'cur',
'dcm'
]);

module.exports = input => {
const imageType = input => {
const ret = fileType(input);
return imageExts.has(ret && ret.ext) ? ret : null;
};

module.exports = imageType;
module.exports.default = imageType;

Object.defineProperty(imageType, 'minimumBytes', {value: fileType.minimumBytes});
18 changes: 18 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {expectType} from 'tsd-check';
import imageType, {ImageTypeResult, ImageType} from '.';

imageType(new Buffer([0xff, 0xd8, 0xff]));
imageType(new Uint8Array([0xff, 0xd8, 0xff]));

expectType<ImageTypeResult | null>(imageType(new Buffer([0xff, 0xd8, 0xff])));
expectType<ImageTypeResult | null>(
imageType(new Uint8Array([0xff, 0xd8, 0xff]))
);

const result = imageType(new Buffer([0xff, 0xd8, 0xff]));
if (result != null) {
expectType<ImageType>(result.ext);
expectType<string>(result.mime);
}

expectType<number>(imageType.minimumBytes);
107 changes: 55 additions & 52 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
{
"name": "image-type",
"version": "3.0.0",
"description": "Detect the image type of a Buffer/Uint8Array",
"license": "MIT",
"repository": "sindresorhus/image-type",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"image",
"img",
"pic",
"picture",
"photo",
"type",
"detect",
"check",
"is",
"exif",
"binary",
"buffer",
"uint8array",
"png",
"jpg",
"jpeg",
"gif",
"webp",
"tif",
"bmp",
"jxr",
"psd",
"mime"
],
"dependencies": {
"file-type": "^4.1.0"
},
"devDependencies": {
"ava": "*",
"read-chunk": "^2.0.0",
"xo": "*"
}
"name": "image-type",
"version": "3.0.0",
"description": "Detect the image type of a Buffer/Uint8Array",
"license": "MIT",
"repository": "sindresorhus/image-type",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd-check"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"image",
"img",
"pic",
"picture",
"photo",
"type",
"detect",
"check",
"is",
"exif",
"binary",
"buffer",
"uint8array",
"png",
"jpg",
"jpeg",
"gif",
"webp",
"tif",
"bmp",
"jxr",
"psd",
"mime"
],
"dependencies": {
"file-type": "^10.9.0"
},
"devDependencies": {
"@types/node": "^11.10.4",
"ava": "^1.3.1",
"read-chunk": "^3.0.0",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
33 changes: 24 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,34 @@ Or `null` when no match.

Type: `Buffer` `Uint8Array`

It only needs the first 12 bytes.
It only needs the first `.minimumBytes` bytes.

### imageType.minimumBytes

Type: `number`

The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hardcode it.

## Supported file types

- `jpg`
- `png`
- `gif`
- `webp`
- `tif`
- `bmp`
- `jxr`
- `psd`
- [`jpg`](https://en.wikipedia.org/wiki/JPEG)
- [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics)
- [`gif`](https://en.wikipedia.org/wiki/GIF)
- [`webp`](https://en.wikipedia.org/wiki/WebP)
- [`flif`](https://en.wikipedia.org/wiki/Free_Lossless_Image_Format)
- [`cr2`](https://fileinfo.com/extension/cr2)
- [`tif`](https://en.wikipedia.org/wiki/Tagged_Image_File_Format)
- [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format)
- [`jxr`](https://en.wikipedia.org/wiki/JPEG_XR)
- [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format)
- [`ico`](https://en.wikipedia.org/wiki/ICO_(file_format))
- [`bpg`](https://bellard.org/bpg/)
- [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`jpm`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`jpx`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
- [`heic`](https://nokiatech.github.io/heif/technical.html)
- [`cur`](https://en.wikipedia.org/wiki/ICO_(file_format))
- [`dcm`](https://en.wikipedia.org/wiki/DICOM#Data_format) - DICOM Image File

*SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).*

Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from 'ava';
import readChunk from 'read-chunk';
import m from './';
import imageType from '.';

const check = filename => m(readChunk.sync(filename, 0, 12)).ext;
const check = filename => imageType(readChunk.sync(filename, 0, 12)).ext;

test(t => {
test('main', t => {
t.is(check('fixture.png'), 'png');
t.is(check('fixture.psd'), 'psd');
});

0 comments on commit a1287d2

Please sign in to comment.