Skip to content

Commit

Permalink
Always call the callback + reliable erroring
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow authored and espadrine committed Apr 12, 2017
1 parent 0760d17 commit 8b77d16
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
9 changes: 7 additions & 2 deletions gh-badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ if (color[0] === ':') {

badge(badgeData, function produceOutput(svg) {
if (/png|jpg|gif/.test(format)) {
svg2img(svg, format, function (data) {
process.stdout.write(data);
svg2img(svg, format, function (err, data) {
if (err) {
console.error(err);
process.exit(1);
} else {
process.stdout.write(data);
}
});
} else {
console.log(svg);
Expand Down
18 changes: 8 additions & 10 deletions lib/svg-to-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function (svg, format, callback) {
if (imgCache.has(cacheIndex)) {
// We own a cache for this svg conversion.
var result = imgCache.get(cacheIndex);
callback(result);
callback(null, result);
return;
}

Expand All @@ -20,15 +20,13 @@ module.exports = function (svg, format, callback) {
.density(90)
.background(format === 'jpg' ? '#FFFFFF' : 'none')
.flatten()
.stream(format, function (err, stdout, stderr) {
if (err) { console.error(err); return; }
var chunks = [];
stdout.on('data', function(chunk) { chunks.push(chunk); });
stdout.on('finish', function() {
var result = Buffer.concat(chunks);
imgCache.set(cacheIndex, result);
callback(result);
});
.toBuffer(format, function (err, data) {
if (err) {
callback(err);
} else {
imgCache.set(cacheIndex, data);
callback(null, data);
}
});
};

Expand Down
6 changes: 6 additions & 0 deletions public/500.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!doctype html><meta charset=utf-8><title>A server error occurred</title>

<h1> I have nothing to offer for this request </h1>
<p> … but blood, toil, tears and sweat.
</p>
<p>And trying again later.</p>
9 changes: 7 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6127,8 +6127,13 @@ function sendSVG(res, askres, end) {

function sendOther(format, res, askres, end) {
askres.setHeader('Content-Type', 'image/' + format);
svg2img(res, format, function (data) {
end(null, {template: streamFromString(data)});
svg2img(res, format, function (err, data) {
if (err) {
console.error('svg2img error', err);
end(null, {template: '500.html'});
} else {
end(null, {template: streamFromString(data)});
}
});
}

Expand Down
13 changes: 8 additions & 5 deletions test/svg-to-img.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe('The rasterizer', function () {

it('should produce PNG', function(done) {
badge({ text: ['cactus', 'grown'], format: 'svg' }, svg => {
svg2img(svg, 'png', data => {
svg2img(svg, 'png', (err, data) => {
assert.equal(err, null);
assert.ok(isPng(data));
done();
});
Expand All @@ -21,12 +22,14 @@ describe('The rasterizer', function () {

it('should cache its results', function(done) {
badge({ text: ['will-this', 'be-cached?'], format: 'svg' }, svg => {
svg2img(svg, 'png', data1 => {
assert.ok(isPng(data1));
svg2img(svg, 'png', (err, data) => {
assert.equal(err, null);
assert.ok(isPng(data));
assert.equal(cacheGet.called, false);

svg2img(svg, 'png', data2 => {
assert.ok(isPng(data2));
svg2img(svg, 'png', (err, data) => {
assert.equal(err, null);
assert.ok(isPng(data));
assert.ok(cacheGet.calledOnce);

done();
Expand Down

0 comments on commit 8b77d16

Please sign in to comment.