Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 17, 2021
1 parent ed6da27 commit 6ee615d
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 109 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
126 changes: 62 additions & 64 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,79 @@
/// <reference types="node"/>
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<boolean>;
https.get(url, async response => {
console.log(await isProgressive.stream(response));
//=> true
});
```
*/
stream(stream: ReadableStream): Promise<boolean>;

/**
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<boolean>;

/**
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<boolean>;

/**
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;
22 changes: 13 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 = () => {
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -76,3 +78,5 @@ exports.fileSync = filepath => {

return isProgressive;
};

export default isProgressive;
7 changes: 4 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Promise<boolean>>(isProgressive.file('baseline.jpg'));
expectType<boolean>(isProgressive.fileSync('progressive.jpg'));
https.get('/', response => {
expectType<Promise<boolean>>(isProgressive.stream(response));
});
expectType<boolean>(isProgressive.buffer(Buffer.from(1)));
expectType<boolean>(isProgressive.buffer(Buffer.from('1')));
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 <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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 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"
Expand All @@ -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"
}
}
24 changes: 11 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@ 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
```

```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';

Expand All @@ -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.

Expand All @@ -65,7 +63,7 @@ Returns a `Promise<boolean>` 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)

Expand All @@ -75,7 +73,7 @@ Returns a `Promise<boolean>` indicating whether the file at the `filePath` is a

Type: `string`

File path to the image.
The file path to the image.

#### .fileSync(filePath)

Expand All @@ -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

Expand Down
15 changes: 7 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down

0 comments on commit 6ee615d

Please sign in to comment.