Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require Node.js 8, add TypeScript definition #5

Merged
merged 1 commit into from
Apr 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[package.json]
[*.yml]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* 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
13 changes: 0 additions & 13 deletions .jshintrc

This file was deleted.

1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
sudo: false
language: node_js
node_js:
- 'iojs'
- '0.12'
- '0.10'
- '10'
- '8'
32 changes: 32 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference types="node"/>

/**
Check if a Buffer/Uint8Array is a [PNG](http://en.wikipedia.org/wiki/Portable_Network_Graphics) image.

@param buffer - The buffer to check. It only needs the first 8 bytes.
@returns Whether `buffer` contains a PNG image.

@example
```
// Node.js:
import readChunk = require('read-chunk'); // npm install read-chunk
import isPng = require('is-png');

const buffer = readChunk.sync('unicorn.png', 0, 8);

isPng(buffer);
//=> true

// Browser:
(async () => {
const response = await fetch('unicorn.png');
const buffer = await response.arrayBuffer();

isPng(new Uint8Array(buffer));
//=> true
})();
```
*/
declare function isPng(buffer: Uint8Array | Buffer): boolean;

export = isPng;
23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
'use strict';
module.exports = function (buf) {
if (!buf || buf.length < 8) {

module.exports = buffer => {
if (!buffer || buffer.length < 8) {
return false;
}

return buf[0] === 137 &&
buf[1] === 80 &&
buf[2] === 78 &&
buf[3] === 71 &&
buf[4] === 13 &&
buf[5] === 10 &&
buf[6] === 26 &&
buf[7] === 10;
return (
buffer[0] === 137 &&
buffer[1] === 80 &&
buffer[2] === 78 &&
buffer[3] === 71 &&
buffer[4] === 13 &&
buffer[5] === 10 &&
buffer[6] === 26 &&
buffer[7] === 10
);
};
13 changes: 13 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference lib="dom"/>
import {expectType} from 'tsd';
import isPng = require('.');

expectType<boolean>(isPng(new Uint8Array(0)));
expectType<boolean>(isPng(Buffer.from('')));

(async () => {
const response = await fetch('unicorn.png');
const buffer = await response.arrayBuffer();

isPng(new Uint8Array(buffer));
})();
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
89 changes: 47 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
{
"name": "is-png",
"version": "1.1.0",
"description": "Check if a Buffer/Uint8Array is a PNG image",
"license": "MIT",
"repository": "sindresorhus/is-png",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "http://sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "mocha"
},
"files": [
"index.js"
],
"keywords": [
"png",
"portable",
"network",
"graphics",
"image",
"img",
"pic",
"picture",
"photo",
"type",
"detect",
"check",
"is",
"exif",
"binary",
"buffer",
"uint8array"
],
"devDependencies": {
"mocha": "*",
"read-chunk": "^1.0.0"
}
"name": "is-png",
"version": "1.1.0",
"description": "Check if a Buffer/Uint8Array is a PNG image",
"license": "MIT",
"repository": "sindresorhus/is-png",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"png",
"portable",
"network",
"graphics",
"image",
"img",
"pic",
"picture",
"photo",
"type",
"detect",
"check",
"is",
"exif",
"binary",
"buffer",
"uint8array"
],
"devDependencies": {
"@types/node": "^11.13.5",
"ava": "^1.4.1",
"read-chunk": "^3.2.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"dependencies": {}
}
29 changes: 14 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Used by [image-type](https://github.com/sindresorhus/image-type).
## Install

```sh
$ npm install --save is-png
$ npm install is-png
```


Expand All @@ -17,9 +17,9 @@ $ npm install --save is-png
##### Node.js

```js
var readChunk = require('read-chunk'); // npm install read-chunk
var isPng = require('is-png');
var buffer = readChunk.sync('unicorn.png', 0, 8);
const readChunk = require('read-chunk'); // npm install read-chunk
const isPng = require('is-png');
const buffer = readChunk.sync('unicorn.png', 0, 8);

isPng(buffer);
//=> true
Expand All @@ -28,28 +28,27 @@ isPng(buffer);
##### Browser

```js
var xhr = new XMLHttpRequest();
xhr.open('GET', 'unicorn.png');
xhr.responseType = 'arraybuffer';
(async () => {
const response = await fetch('unicorn.png');
const buffer = await response.arrayBuffer();

xhr.onload = function () {
isPng(new Uint8Array(this.response));
isPng(new Uint8Array(buffer));
//=> true
};

xhr.send();
})();
```


## API

### isPng(buffer)

Accepts a Buffer (Node.js) or Uint8Array.
Accepts a Buffer (Node.js) or Uint8Array. Returns a `boolean` of whether `buffer` contains a PNG image.

#### buffer

It only needs the first 8 bytes.
The buffer to check. It only needs the first 8 bytes.


## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)
13 changes: 6 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict';
var assert = require('assert');
var readChunk = require('read-chunk');
var isPng = require('./');
import test from 'ava';
import readChunk from 'read-chunk';
import isPng from '.';

function check(filename) {
return isPng(readChunk.sync(filename, 0, 8));
}

it('should detect PNG from Buffer', function () {
assert(check('fixture.png'));
assert(!check('fixture.jpg'));
test('main', t => {
t.true(check('fixture.png'));
t.false(check('fixture.jpg'));
});