/
PngQuant.js
183 lines (162 loc) · 5.29 KB
/
PngQuant.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* global describe, it, beforeEach, afterEach, setTimeout, __dirname */
const expect = require('unexpected')
.clone()
.use(require('unexpected-stream'))
.use(require('unexpected-sinon'));
const sinon = require('sinon');
const PngQuant = require('../lib/PngQuant');
const pathModule = require('path');
const fs = require('fs');
const semver = require('semver');
it.skipIf = function(condition) {
(condition ? it.skip : it).apply(
it,
Array.prototype.slice.call(arguments, 1)
);
};
describe('PngQuant', () => {
it('should produce a smaller file with lower quality and ordered dithering', () =>
expect(
fs.createReadStream(
pathModule.resolve(__dirname, 'purplealpha24bit.png')
),
'when piped through',
new PngQuant([128, '--quality', '60-80', '--nofs']),
'to yield output satisfying',
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 => {
const pngQuant = new PngQuant();
function fail() {
done(new Error('PngQuant emitted data while it was paused!'));
}
pngQuant.pause();
pngQuant.on('data', fail).on('error', done);
fs.createReadStream(
pathModule.resolve(__dirname, 'purplealpha24bit.png')
).pipe(pngQuant);
setTimeout(() => {
pngQuant.removeListener('data', fail);
const chunks = [];
pngQuant
.on('data', chunk => {
chunks.push(chunk);
})
.on('end', () => {
const resultPngBuffer = Buffer.concat(chunks);
expect(resultPngBuffer.length, 'to be within', 0, 8285);
done();
});
pngQuant.resume();
}, 1000);
}
);
it('should emit an error if an invalid image is processed', done => {
const pngQuant = new PngQuant();
pngQuant
.on('error', err => {
done();
})
.on('data', chunk => {
done(new Error('PngQuant emitted data when an error was expected'));
})
.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 => {
const pngQuant = new PngQuant(['--blabla']);
let seenError = false;
pngQuant
.on('error', err => {
expect(pngQuant.commandLine, 'to match', /pngquant --blabla/);
if (seenError) {
done(new Error('More than one error event was emitted'));
} else {
seenError = true;
setTimeout(done, 100);
}
})
.on('data', chunk => {
done(new Error('PngQuant emitted data when an error was expected'));
})
.on('end', chunk => {
done(new Error('PngQuant emitted end when an error was expected'));
});
pngQuant.end(Buffer.from('qwvopeqwovkqvwiejvq', 'utf-8'));
});
describe('#destroy', () => {
it('should kill the underlying child process', () => {
const pngQuant = new PngQuant(['-grayscale']);
return expect.promise(run => {
pngQuant.write('JFIF');
setTimeout(
run(function waitForPngQuantProcess() {
const pngQuantProcess = pngQuant.pngQuantProcess;
if (pngQuant.pngQuantProcess) {
sinon.spy(pngQuantProcess, 'kill');
pngQuant.destroy();
sinon.spy(pngQuant, 'emit');
expect(pngQuantProcess.kill, 'to have calls satisfying', () => {
pngQuantProcess.kill();
});
expect(pngQuant.pngQuantProcess, 'to be falsy');
expect(pngQuant.bufferedChunks, 'to be falsy');
setTimeout(
run(() => {
expect(pngQuant.emit, 'to have calls satisfying', []);
}),
10
);
} else {
setTimeout(run(waitForPngQuantProcess), 0);
}
}),
0
);
});
});
});
expect.addAssertion('<Stream> to error', (expect, subject) =>
expect.promise(run => {
subject.once('error', run(err => err));
})
);
expect.addAssertion(
'<Stream> to error with <any>',
(expect, subject, value) => {
expect.errorMode = 'nested';
return expect(subject, 'to error').then(err =>
expect(err, 'to satisfy', value)
);
}
);
describe('with an overridden #binaryPath', () => {
const childProcess = require('child_process');
beforeEach(() => {
PngQuant.getBinaryPath.purgeAll(); // Make sure we don't get a cached value from one of the other tests
PngQuant.binaryPath = '/foo/bar';
sinon.spy(childProcess, 'spawn');
});
afterEach(() => {
PngQuant.binaryPath = null;
childProcess.spawn.restore();
});
it('should try launching that binary instead of the one found on the system', () =>
expect(
fs.createReadStream(
pathModule.resolve(__dirname, 'purplealpha24bit.png')
),
'when piped through',
new PngQuant([128]),
'to error with',
/\/foo\/bar ENOENT|write EPIPE/
));
});
});