Skip to content

Commit

Permalink
prettier --write '**/*.{js,md}'
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 2, 2020
1 parent 56e1d04 commit ee79742
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
27 changes: 13 additions & 14 deletions README.md
@@ -1,5 +1,4 @@
node-pngquant
=============
# node-pngquant

[![NPM version](https://badge.fury.io/js/pngquant.svg)](http://badge.fury.io/js/pngquant)
[![Build Status](https://travis-ci.org/papandreou/node-pngquant.svg?branch=master)](https://travis-ci.org/papandreou/node-pngquant)
Expand All @@ -13,7 +12,7 @@ the `pngquant` binary (defaults to `[256]`):

```javascript
var PngQuant = require('pngquant'),
myPngQuanter = new PngQuant([192, '--quality', '60-80', '--nofs', '-']);
myPngQuanter = new PngQuant([192, '--quality', '60-80', '--nofs', '-']);

sourceStream.pipe(myPngQuanter).pipe(destinationStream);
```
Expand All @@ -23,27 +22,27 @@ quantized to 128):

```javascript
var PngQuant = require('pngquant'),
http = require('http');
http = require('http');

http.createServer(function (req, res) {
http
.createServer(function (req, res) {
if (req.headers['content-type'] === 'image/png') {
res.writeHead(200, {'Content-Type': 'image/png'});
req.pipe(new PngQuant([128])).pipe(res);
res.writeHead(200, { 'Content-Type': 'image/png' });
req.pipe(new PngQuant([128])).pipe(res);
} else {
res.writeHead(400);
res.end('Feed me a PNG!');
res.writeHead(400);
res.end('Feed me a PNG!');
}
}).listen(1337);
})
.listen(1337);
```

Installation
------------
## Installation

Make sure you have node.js and npm installed, and that the `pngquant` binary is in your PATH, then run:

npm install pngquant

License
-------
## License

3-clause BSD license -- see the `LICENSE` file for details.
26 changes: 13 additions & 13 deletions lib/PngQuant.js
Expand Up @@ -22,7 +22,7 @@ function PngQuant(pngQuantArgs) {

util.inherits(PngQuant, Stream);

PngQuant.getBinaryPath = memoizeAsync(cb => {
PngQuant.getBinaryPath = memoizeAsync((cb) => {
if (PngQuant.binaryPath !== undefined) {
setImmediate(() => {
cb(null, PngQuant.binaryPath);
Expand All @@ -46,35 +46,35 @@ PngQuant.getBinaryPath = memoizeAsync(cb => {
});
});

PngQuant.setBinaryPath = binaryPath => {
PngQuant.setBinaryPath = (binaryPath) => {
PngQuant.binaryPath = binaryPath;
};

PngQuant.prototype._error = function(err) {
PngQuant.prototype._error = function (err) {
if (!this.hasEnded) {
this.hasEnded = true;
this.cleanUp();
this.emit('error', err);
}
};

PngQuant.prototype.cleanUp = function() {
PngQuant.prototype.cleanUp = function () {
if (this.pngQuantProcess) {
this.pngQuantProcess.kill();
this.pngQuantProcess = null;
}
this.bufferedChunks = null;
};

PngQuant.prototype.destroy = function() {
PngQuant.prototype.destroy = function () {
if (!this.hasEnded) {
this.hasEnded = true;
this.cleanUp();
this.bufferedChunks = null;
}
};

PngQuant.prototype.write = function(chunk) {
PngQuant.prototype.write = function (chunk) {
if (this.hasEnded) {
return;
}
Expand All @@ -98,7 +98,7 @@ PngQuant.prototype.write = function(chunk) {
this.pngQuantProcess.stdin.once('error', this._error.bind(this));
this.pngQuantProcess.stdout.once('error', this._error.bind(this));

this.pngQuantProcess.stderr.on('data', data => {
this.pngQuantProcess.stderr.on('data', (data) => {
if (!this.hasEnded) {
this._error(
new Error(
Expand All @@ -109,7 +109,7 @@ PngQuant.prototype.write = function(chunk) {
}
});

this.pngQuantProcess.once('exit', exitCode => {
this.pngQuantProcess.once('exit', (exitCode) => {
if (this.hasEnded) {
return;
}
Expand All @@ -124,7 +124,7 @@ PngQuant.prototype.write = function(chunk) {
});

this.pngQuantProcess.stdout
.on('data', chunk => {
.on('data', (chunk) => {
this.seenDataOnStdout = true;
this.emit('data', chunk);
})
Expand All @@ -147,7 +147,7 @@ PngQuant.prototype.write = function(chunk) {
if (this.pauseStdoutOfPngQuantProcessAfterStartingIt) {
this.pngQuantProcess.stdout.pause();
}
this.bufferedChunks.forEach(function(bufferedChunk) {
this.bufferedChunks.forEach(function (bufferedChunk) {
// In node.js 12.6.0+ the error event be invoked syncronously, which means
// that we might clean up between the chunks. Guard against dereferencing
// 'stdin' of null in that case:
Expand All @@ -169,7 +169,7 @@ PngQuant.prototype.write = function(chunk) {
}
};

PngQuant.prototype.end = function(chunk) {
PngQuant.prototype.end = function (chunk) {
if (this.hasEnded) {
return;
}
Expand All @@ -186,15 +186,15 @@ PngQuant.prototype.end = function(chunk) {
}
};

PngQuant.prototype.pause = function() {
PngQuant.prototype.pause = function () {
if (this.pngQuantProcess) {
this.pngQuantProcess.stdout.pause();
} else {
this.pauseStdoutOfPngQuantProcessAfterStartingIt = true;
}
};

PngQuant.prototype.resume = function() {
PngQuant.prototype.resume = function () {
if (this.pngQuantProcess) {
this.pngQuantProcess.stdout.resume();
} else {
Expand Down
28 changes: 14 additions & 14 deletions test/PngQuant.js
Expand Up @@ -8,7 +8,7 @@ const pathModule = require('path');
const fs = require('fs');
const semver = require('semver');

it.skipIf = function(condition) {
it.skipIf = function (condition) {
(condition ? it.skip : it).apply(
it,
Array.prototype.slice.call(arguments, 1)
Expand All @@ -24,15 +24,15 @@ describe('PngQuant', () => {
'when piped through',
new PngQuant([128, '--quality', '60-80', '--nofs']),
'to yield output satisfying',
expect.it(resultPngBuffer => {
expect.it((resultPngBuffer) => {
expect(resultPngBuffer.length, 'to be within', 0, 8285);
})
));

it.skipIf(
semver.satisfies(process.version.replace(/^v/, ''), '>=0.12.0'),
'should not emit data events while paused',
done => {
(done) => {
const pngQuant = new PngQuant();

function fail() {
Expand All @@ -50,7 +50,7 @@ describe('PngQuant', () => {
const chunks = [];

pngQuant
.on('data', chunk => {
.on('data', (chunk) => {
chunks.push(chunk);
})
.on('end', () => {
Expand All @@ -64,24 +64,24 @@ describe('PngQuant', () => {
}
);

it('should emit an error if an invalid image is processed', done => {
it('should emit an error if an invalid image is processed', (done) => {
const pngQuant = new PngQuant();

pngQuant
.on('error', () => {
done();
})
.on('data', chunk => {
.on('data', (chunk) => {
done(new Error('PngQuant emitted data when an error was expected'));
})
.on('end', chunk => {
.on('end', (chunk) => {
done(new Error('PngQuant emitted end when an error was expected'));
});

pngQuant.end(Buffer.from('qwvopeqwovkqvwiejvq', 'utf-8'));
});

it('should emit a single error if an invalid command line is specified', done => {
it('should emit a single error if an invalid command line is specified', (done) => {
const pngQuant = new PngQuant(['--blabla']);

let seenError = false;
Expand All @@ -96,10 +96,10 @@ describe('PngQuant', () => {
setTimeout(done, 100);
}
})
.on('data', chunk => {
.on('data', (chunk) => {
done(new Error('PngQuant emitted data when an error was expected'));
})
.on('end', chunk => {
.on('end', (chunk) => {
done(new Error('PngQuant emitted end when an error was expected'));
});

Expand All @@ -110,7 +110,7 @@ describe('PngQuant', () => {
it('should kill the underlying child process', () => {
const pngQuant = new PngQuant(['-grayscale']);

return expect.promise(run => {
return expect.promise((run) => {
pngQuant.write('JFIF');
setTimeout(
run(function waitForPngQuantProcess() {
Expand Down Expand Up @@ -141,10 +141,10 @@ describe('PngQuant', () => {
});

expect.addAssertion('<Stream> to error', (expect, subject) =>
expect.promise(run => {
expect.promise((run) => {
subject.once(
'error',
run(err => err)
run((err) => err)
);
})
);
Expand All @@ -153,7 +153,7 @@ describe('PngQuant', () => {
'<Stream> to error with <any>',
(expect, subject, value) => {
expect.errorMode = 'nested';
return expect(subject, 'to error').then(err =>
return expect(subject, 'to error').then((err) =>
expect(err, 'to satisfy', value)
);
}
Expand Down

0 comments on commit ee79742

Please sign in to comment.