diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 18531b3..441975c 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- - 14
- - 12
- - 10
- - 8
+ - 16
steps:
- uses: actions/checkout@v2
- - uses: actions/setup-node@v1
+ - uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
diff --git a/index.d.ts b/index.d.ts
index a15e8db..6affd36 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -1,81 +1,79 @@
-///
-import {Readable as ReadableStream} from 'stream';
+import {Buffer} from 'node:buffer';
+import {Readable as ReadableStream} from 'node:stream';
-/**
-Checks if a `Buffer` contains a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).
+declare const isProgressive: {
+ /**
+ Checks if a `Buffer` contains a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).
-@param buffer - Buffer of a JPEG image. Must be at least `65535` bytes when the file is larger than that.
-@returns Whether the `buffer` is a progressive JPEG image.
+ @param buffer - The buffer of a JPEG image. Must be at least `65535` bytes when the file is larger than that.
+ @returns Whether the `buffer` is a progressive JPEG image.
-@example
-```
-import {promisify} from 'util';
-import {readFile} from 'fs';
-import isProgressive = require('is-progressive');
+ @example
+ ```
+ import {promisify} from 'node:util';
+ import {readFile} from 'node:fs/promises';
+ import isProgressive from 'is-progressive';
-const readFileP = promisify(readFile);
-
-(async () => {
- const buffer = await readFileP('baseline.jpg');
+ const buffer = await readFile('baseline.jpg');
console.log(await isProgressive.buffer(buffer));
//=> false
-})();
-```
-*/
-export function buffer(buffer: Buffer): boolean;
+ ```
+ */
+ buffer(buffer: Buffer): boolean;
-/**
-Checks if a `stream.Readable` produces a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).
+ /**
+ Checks if a `stream.Readable` produces a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).
-@param stream - Data stream with a JPEG image.
-@returns Whether the `stream` is a progressive JPEG image.
+ @param stream - A data stream with a JPEG image.
+ @returns Whether the `stream` is a progressive JPEG image.
-@example
-```
-// Check if a remote JPEG image is progressive without downloading the whole file
-import * as https from 'https';
-import isProgressive = require('is-progressive');
+ @example
+ ```
+ // Check if a remote JPEG image is progressive without downloading the whole file
+ import https from 'https';
+ import isProgressive from 'is-progressive';
-const url = 'https://raw.githubusercontent.com/sindresorhus/is-progressive/main/fixture/progressive.jpg';
+ const url = 'https://raw.githubusercontent.com/sindresorhus/is-progressive/main/fixture/progressive.jpg';
-https.get(url, async response => {
- console.log(await isProgressive.stream(response));
- //=> true
-});
-```
-*/
-export function stream(stream: ReadableStream): Promise;
+ https.get(url, async response => {
+ console.log(await isProgressive.stream(response));
+ //=> true
+ });
+ ```
+ */
+ stream(stream: ReadableStream): Promise;
-/**
-Checks if a file is a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).
+ /**
+ Checks if a file is a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).
-@param filePath - File path to the image.
-@returns Whether the file at the `filePath` is a progressive JPEG image.
+ @param filePath - The file path to the image.
+ @returns Whether the file at the `filePath` is a progressive JPEG image.
-@example
-```
-import isProgressive = require('is-progressive');
+ @example
+ ```
+ import isProgressive from 'is-progressive';
-(async () => {
console.log(await isProgressive.file('baseline.jpg'));
//=> false
-})();
-```
-*/
-export function file(filePath: string): Promise;
-
-/**
-Synchronously checks if a file is a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).
-
-@param filePath - File path to the image.
-@returns Whether the the file at the `filePath` is a progressive JPEG.
-
-@example
-```
-import isProgressive = require('is-progressive');
-
-isProgressive.fileSync('progressive.jpg');
-//=> true
-```
-*/
-export function fileSync(filePath: string): boolean;
+ ```
+ */
+ file(filePath: string): Promise;
+
+ /**
+ Synchronously checks if a file is a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).
+
+ @param filePath - The file path to the image.
+ @returns Whether the the file at the `filePath` is a progressive JPEG.
+
+ @example
+ ```
+ import isProgressive from 'is-progressive';
+
+ isProgressive.fileSync('progressive.jpg');
+ //=> true
+ ```
+ */
+ fileSync(filePath: string): boolean;
+};
+
+export default isProgressive;
diff --git a/index.js b/index.js
index 348e471..8fa9550 100644
--- a/index.js
+++ b/index.js
@@ -1,12 +1,12 @@
-'use strict';
-const fs = require('fs');
-const readChunk = require('read-chunk');
+import fs from 'node:fs';
+import {Buffer} from 'node:buffer';
+import {readChunk} from 'read-chunk';
// https://en.wikipedia.org/wiki/JPEG
// SOF2 [0xFF, 0xC2] = Start Of Frame (Progressive DCT)
const SOF2 = 0xC2;
-const search = buffer => {
+const fromBuffer = buffer => {
let previousByte;
for (const currentByte of buffer) {
@@ -25,9 +25,11 @@ const search = buffer => {
return false;
};
-exports.buffer = search;
+const isProgressive = {};
-exports.stream = readableStream => new Promise((resolve, reject) => {
+isProgressive.buffer = fromBuffer;
+
+isProgressive.stream = readableStream => new Promise((resolve, reject) => {
let previousLastByte = Buffer.alloc(1);
const end = () => {
@@ -37,7 +39,7 @@ exports.stream = readableStream => new Promise((resolve, reject) => {
readableStream.on('data', data => {
previousLastByte = Buffer.of(data[data.length - 1]);
- if (search(Buffer.concat([previousLastByte, data]))) {
+ if (fromBuffer(Buffer.concat([previousLastByte, data]))) {
resolve(true);
readableStream.removeListener('end', end);
}
@@ -48,9 +50,9 @@ exports.stream = readableStream => new Promise((resolve, reject) => {
});
// The metadata section has a maximum size of 65535 bytes
-exports.file = async filePath => search(await readChunk(filePath, 0, 65535));
+isProgressive.file = async filePath => fromBuffer(await readChunk(filePath, {length: 65_535}));
-exports.fileSync = filepath => {
+isProgressive.fileSync = filepath => {
// We read one byte at the time here as it usually appears early in the file and reading 65535 would be wasteful
const BUFFER_LENGTH = 1;
const buffer = Buffer.alloc(BUFFER_LENGTH);
@@ -76,3 +78,5 @@ exports.fileSync = filepath => {
return isProgressive;
};
+
+export default isProgressive;
diff --git a/index.test-d.ts b/index.test-d.ts
index 1eef904..3bb6383 100644
--- a/index.test-d.ts
+++ b/index.test-d.ts
@@ -1,10 +1,11 @@
+import {Buffer} from 'node:buffer';
+import https from 'node:https';
import {expectType} from 'tsd';
-import * as https from 'https';
-import isProgressive = require('.');
+import isProgressive from './index.js';
expectType>(isProgressive.file('baseline.jpg'));
expectType(isProgressive.fileSync('progressive.jpg'));
https.get('/', response => {
expectType>(isProgressive.stream(response));
});
-expectType(isProgressive.buffer(Buffer.from(1)));
+expectType(isProgressive.buffer(Buffer.from('1')));
diff --git a/license b/license
index e7af2f7..fa7ceba 100644
--- a/license
+++ b/license
@@ -1,6 +1,6 @@
MIT License
-Copyright (c) Sindre Sorhus (sindresorhus.com)
+Copyright (c) Sindre Sorhus (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:
diff --git a/package.json b/package.json
index b08acf3..aed47dc 100644
--- a/package.json
+++ b/package.json
@@ -4,13 +4,16 @@
"description": "Check if JPEG images are progressive",
"license": "MIT",
"repository": "sindresorhus/is-progressive",
+ "funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
- "url": "sindresorhus.com"
+ "url": "https://sindresorhus.com"
},
+ "type": "module",
+ "exports": "./index.js",
"engines": {
- "node": ">=8"
+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -37,11 +40,11 @@
"fs"
],
"dependencies": {
- "read-chunk": "^3.2.0"
+ "read-chunk": "^4.0.2"
},
"devDependencies": {
- "ava": "^1.4.1",
- "tsd": "^0.7.2",
- "xo": "^0.24.0"
+ "ava": "^3.15.0",
+ "tsd": "^0.18.0",
+ "xo": "^0.45.0"
}
}
diff --git a/readme.md b/readme.md
index 3ecf40c..a461b0b 100644
--- a/readme.md
+++ b/readme.md
@@ -10,19 +10,17 @@ The check is fast as it only reads a small part of the file.
## Install
-```
-$ npm install is-progressive
+```sh
+npm install is-progressive
```
## Usage
```js
-const isProgressive = require('is-progressive');
+import isProgressive from 'is-progressive';
-(async () => {
- console.log(await isProgressive.file('baseline.jpg'));
- //=> false
-})();
+console.log(await isProgressive.file('baseline.jpg'));
+//=> false
isProgressive.fileSync('progressive.jpg');
//=> true
@@ -30,8 +28,8 @@ isProgressive.fileSync('progressive.jpg');
```js
// Check if a remote JPEG image is progressive without downloading the whole file
-const https = require('https');
-const isProgressive = require('is-progressive');
+import https from 'https';
+import isProgressive from 'is-progressive';
const url = 'https://raw.githubusercontent.com/sindresorhus/is-progressive/main/fixture/progressive.jpg';
@@ -53,7 +51,7 @@ Returns whether the `buffer` is a progressive JPEG image.
Type: `Buffer`
-Buffer of a JPEG image.
+The buffer of a JPEG image.
Must be at least `65535` bytes when the file is larger than that.
@@ -65,7 +63,7 @@ Returns a `Promise` indicating whether the file stream is a progressive
Type: `stream.Readable`
-Data stream with a JPEG image.
+A data stream with a JPEG image.
#### .file(filePath)
@@ -75,7 +73,7 @@ Returns a `Promise` indicating whether the file at the `filePath` is a
Type: `string`
-File path to the image.
+The file path to the image.
#### .fileSync(filePath)
@@ -85,7 +83,7 @@ Whether the the file at the `filePath` is a progressive JPEG.
Type: `string`
-File path to the image.
+The file path to the image.
## Build-system integration
diff --git a/test.js b/test.js
index c3e7643..5463033 100644
--- a/test.js
+++ b/test.js
@@ -1,15 +1,14 @@
-import fs from 'fs';
-import path from 'path';
+import fs from 'node:fs';
import test from 'ava';
-import readChunk from 'read-chunk';
-import isProgressive from '.';
+import {readChunkSync} from 'read-chunk';
+import isProgressive from './index.js';
-const getPath = name => path.join(__dirname, `fixture/${name}.jpg`);
+const getPath = name => `fixture/${name}.jpg`;
test('.buffer()', t => {
- t.true(isProgressive.buffer(readChunk.sync(getPath('progressive'), 0, 65535)));
- t.true(isProgressive.buffer(readChunk.sync(getPath('curious-exif'), 0, 65535)));
- t.false(isProgressive.buffer(readChunk.sync(getPath('baseline'), 0, 65535)));
+ t.true(isProgressive.buffer(readChunkSync(getPath('progressive'), {length: 65_535})));
+ t.true(isProgressive.buffer(readChunkSync(getPath('curious-exif'), {length: 65_535})));
+ t.false(isProgressive.buffer(readChunkSync(getPath('baseline'), {length: 65_535})));
});
test('.stream()', async t => {