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

Use Uint8Array instead of Buffer #8

Merged
merged 5 commits into from
Jul 5, 2024
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
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 20
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
Binary file added fixture/kitten-progressive.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fixture/kitten.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {Buffer} from 'node:buffer';
import {Readable as ReadableStream} from 'node:stream';
import type {Readable as ReadableStream} from 'node:stream';

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).
Checks if an `Uint8Array` contains a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).

@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.
Expand All @@ -19,7 +18,7 @@ declare const isProgressive: {
//=> false
```
*/
buffer(buffer: Buffer): boolean;
buffer(buffer: Uint8Array): boolean;

/**
Checks if a `stream.Readable` produces a JPEG image that is [progressive](http://www.faqs.org/faqs/jpeg-faq/part1/section-11.html).
Expand Down
52 changes: 18 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,34 @@
import fs from 'node:fs';
import {Buffer} from 'node:buffer';
import {readChunk} from 'read-chunk';
import {indexOf} from 'uint8array-extras';

// https://en.wikipedia.org/wiki/JPEG
// SOF2 [0xFF, 0xC2] = Start Of Frame (Progressive DCT)
const SOF2 = 0xC2;
const SOF2 = new Uint8Array([0xFF, 0xC2]);

const fromBuffer = buffer => {
let previousByte;

for (const currentByte of buffer) {
if (previousByte !== 0xFF) {
previousByte = currentByte;
continue;
}

if (currentByte === SOF2) {
return true;
}

previousByte = currentByte;
}

return false;
};
const fromBuffer = buffer => indexOf(buffer, SOF2) !== -1;

const isProgressive = {};

isProgressive.buffer = fromBuffer;

isProgressive.stream = readableStream => new Promise((resolve, reject) => {
let previousLastByte = Buffer.alloc(1);
// The first byte is for the previous last byte if we have multiple data events.
const buffer = new Uint8Array(1 + readableStream.readableHighWaterMark);

const end = () => {
resolve(false);
};

readableStream.on('data', data => {
previousLastByte = Buffer.of(data[data.length - 1]);
buffer.set(data, 1);

if (fromBuffer(Buffer.concat([previousLastByte, data]))) {
if (fromBuffer(buffer)) {
resolve(true);
readableStream.removeListener('end', end);
}

buffer.set(data.at(-1));
});

readableStream.on('error', reject);
Expand All @@ -53,25 +39,23 @@ isProgressive.stream = readableStream => new Promise((resolve, reject) => {
isProgressive.file = async filePath => fromBuffer(await readChunk(filePath, {length: 65_535}));

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);
// We read two bytes at a time here as it usually appears early in the file and reading 65535 would be wasteful
const BUFFER_LENGTH = 2;
const buffer = new Uint8Array(1 + BUFFER_LENGTH);
const read = fs.openSync(filepath, 'r');
let bytesRead = BUFFER_LENGTH;
let currentByte;
let previousByte;
let isProgressive = false;

while (bytesRead === BUFFER_LENGTH) {
bytesRead = fs.readSync(read, buffer, 0, 1);
currentByte = buffer[0];
while (bytesRead !== 0) {
bytesRead = fs.readSync(read, buffer, 1, BUFFER_LENGTH);

isProgressive = fromBuffer(buffer);

if (previousByte === 0xFF && currentByte === SOF2) {
isProgressive = true;
if (isProgressive) {
break;
}

previousByte = currentByte;
buffer.set(buffer.at(-1), 0);
}

fs.closeSync(read);
Expand Down
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Buffer} from 'node:buffer';
import type {IncomingMessage} from 'node:http';
import https from 'node:https';
import {expectType} from 'tsd';
import isProgressive from './index.js';

expectType<Promise<boolean>>(isProgressive.file('baseline.jpg'));
expectType<boolean>(isProgressive.fileSync('progressive.jpg'));
https.get('/', response => {
https.get('/', (response: IncomingMessage) => {
expectType<Promise<boolean>>(isProgressive.stream(response));
});
expectType<boolean>(isProgressive.buffer(Buffer.from('1')));
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"type": "module",
"exports": "./index.js",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -40,11 +40,12 @@
"fs"
],
"dependencies": {
"read-chunk": "^4.0.2"
"read-chunk": "^5.0.0",
"uint8array-extras": "^1.2.0"
},
"devDependencies": {
"ava": "^3.15.0",
"tsd": "^0.18.0",
"xo": "^0.45.0"
"ava": "^6.1.3",
"tsd": "^0.31.1",
"xo": "^0.58.0"
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Returns whether the `buffer` is a progressive JPEG image.

##### buffer

Type: `Buffer`
Type: `Uint8Array`

The buffer of a JPEG image.

Expand Down
12 changes: 10 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,34 @@ import isProgressive from './index.js';

const getPath = name => `fixture/${name}.jpg`;

test('.buffer()', t => {
test.serial('.buffer()', t => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why serial?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're reading the same files as the other tests that are run in parallel. This blocks the reading of the files in the other tests making them slower or causes hangs where the file cannot be opened because it's being accessed asynchronously.

I benchmarked the methods by running each test multiple times and could only get it to behave consistently if the synchronous tests are run serially.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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})));
t.false(isProgressive.buffer(readChunkSync(getPath('kitten'), {length: 65_535})));
t.true(isProgressive.buffer(readChunkSync(getPath('kitten-progressive'), {length: 65_535})));
});

test('.stream()', async t => {
t.true(await isProgressive.stream(fs.createReadStream(getPath('progressive'))));
t.true(await isProgressive.stream(fs.createReadStream(getPath('curious-exif'))));
t.false(await isProgressive.stream(fs.createReadStream(getPath('baseline'))));
t.false(await isProgressive.stream(fs.createReadStream(getPath('kitten'))));
t.true(await isProgressive.stream(fs.createReadStream(getPath('kitten-progressive'))));
});

test('.file()', async t => {
t.true(await isProgressive.file(getPath('progressive')));
t.true(await isProgressive.file(getPath('curious-exif')));
t.false(await isProgressive.file(getPath('baseline')));
t.false(await isProgressive.file(getPath('kitten')));
t.true(await isProgressive.file(getPath('kitten-progressive')));
});

test('.fileSync()', t => {
test.serial('.fileSync()', t => {
t.true(isProgressive.fileSync(getPath('progressive')));
t.true(isProgressive.fileSync(getPath('curious-exif')));
t.false(isProgressive.fileSync(getPath('baseline')));
t.false(isProgressive.fileSync(getPath('kitten')));
t.true(isProgressive.fileSync(getPath('kitten-progressive')));
});