Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 17, 2021
1 parent cc99005 commit 46a926b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 51 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ jobs:
node-version:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
22 changes: 9 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="node"/>

/**
Check if a Buffer/Uint8Array is a [PNG](https://en.wikipedia.org/wiki/Portable_Network_Graphics) image.
Expand All @@ -9,24 +7,22 @@ Check if a Buffer/Uint8Array is a [PNG](https://en.wikipedia.org/wiki/Portable_N
@example
```
// Node.js:
import readChunk = require('read-chunk');
import isPng = require('is-png');
import readChunk from 'read-chunk';
import isPng from '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();
import readChunk from 'read-chunk';
const response = await fetch('unicorn.png');
const buffer = await response.arrayBuffer();
isPng(new Uint8Array(buffer));
//=> true
})();
isPng(new Uint8Array(buffer));
//=> true
```
*/
declare function isPng(buffer: Uint8Array | Buffer): boolean;

export = isPng;
export default function isPng(buffer: Uint8Array | Buffer): boolean;
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

module.exports = buffer => {
export default function isPng(buffer) {
if (!buffer || buffer.length < 8) {
return false;
}
Expand All @@ -15,4 +13,4 @@ module.exports = buffer => {
buffer[6] === 0x1A &&
buffer[7] === 0x0A
);
};
}
10 changes: 4 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/// <reference lib="dom"/>
import {expectType} from 'tsd';
import isPng = require('.');
import isPng from './index.js';

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

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

isPng(new Uint8Array(buffer));
})();
isPng(new Uint8Array(buffer));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://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:

Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Check if a Buffer/Uint8Array is a PNG image",
"license": "MIT",
"repository": "sindresorhus/is-png",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -37,10 +40,10 @@
"uint8array"
],
"devDependencies": {
"@types/node": "^11.13.5",
"ava": "^1.4.1",
"@types/node": "^14.14.41",
"ava": "^3.15.0",
"read-chunk": "^3.2.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}
26 changes: 9 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

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

## Install

```
$ npm install is-png
```


## Usage

##### Node.js

```js
const readChunk = require('read-chunk'); // npm install read-chunk
const isPng = require('is-png');
import readChunk from 'read-chunk'; // npm install read-chunk
import isPng from 'is-png';

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

isPng(buffer);
Expand All @@ -26,15 +25,14 @@ isPng(buffer);
##### Browser

```js
(async () => {
const response = await fetch('unicorn.png');
const buffer = await response.arrayBuffer();
import isPng from 'is-png';

isPng(new Uint8Array(buffer));
//=> true
})();
```
const response = await fetch('unicorn.png');
const buffer = await response.arrayBuffer();

isPng(new Uint8Array(buffer));
//=> true
```

## API

Expand All @@ -46,12 +44,6 @@ Accepts a Buffer (Node.js) or Uint8Array. Returns a `boolean` of whether `buffer

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


## Related

- [file-type](https://github.com/sindresorhus/file-type) - Detect the file type of a Buffer/Uint8Array/ArrayBuffer


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import readChunk from 'read-chunk';
import isPng from '.';
import isPng from './index.js';

const check = filename => isPng(readChunk.sync(filename, 0, 8));

Expand Down

0 comments on commit 46a926b

Please sign in to comment.