Try to create the appropriate data type on http errors

This commit is contained in:
Tim Schaub 2017-08-22 14:01:51 -04:00
parent bb09f3df64
commit 2e46700cd9

View file

@ -3,6 +3,7 @@
var advancedPool = require('advanced-pool'), var advancedPool = require('advanced-pool'),
fs = require('fs'), fs = require('fs'),
path = require('path'), path = require('path'),
url = require('url'),
util = require('util'), util = require('util'),
zlib = require('zlib'); zlib = require('zlib');
@ -35,6 +36,58 @@ mbgl.on('message', function(e) {
} }
}); });
/**
* Cache of response data by sharp output format. The empty string represents
* an unknown or unsupported format.
*/
var cachedErrorResponses = {
'': new Buffer(0)
};
/**
* Lookup of sharp output formats by file extension.
*/
var extensionToFormat = {
'.jpg': 'jpeg',
'.jpeg': 'jpeg',
'.png': 'png',
'.webp': 'webp'
};
/**
* Create an appropriate mbgl response for http errors.
* @param {Object} req The mbgl req object.
* @param {http.IncomingMessage} res The incoming response.
* @param {Function} callback The mbgl callback.
*/
function createErrorResponse(req, res, callback) {
var parts = url.parse(req.url);
var extension = path.extname(parts.pathname).toLowerCase();
var format = extensionToFormat[extension] || '';
var data = cachedErrorResponses[format];
if (data) {
callback(null, {data: data});
return;
}
// create an "empty" response image
var color = new Color('rgba(255,255,255,0)');
var array = color.array();
var channels = array.length == 4 && format != 'jpeg' ? 4 : 3;
sharp(new Buffer(array), {
raw: {
width: 1,
height: 1,
channels: channels
}
}).toFormat(format).toBuffer(function(err, buffer, info) {
if (!err) {
cachedErrorResponses[format] = buffer;
}
callback(null, {data: buffer});
});
}
module.exports = function(options, repo, params, id, dataResolver) { module.exports = function(options, repo, params, id, dataResolver) {
var app = express().disable('x-powered-by'); var app = express().disable('x-powered-by');
@ -137,8 +190,8 @@ module.exports = function(options, repo, params, id, dataResolver) {
gzip: true gzip: true
}, function(err, res, body) { }, function(err, res, body) {
if (err) { if (err) {
//console.log('HTTP tile error', err); // console.log('HTTP request error', err);
callback(null, { data: new Buffer(0) }); createErrorResponse(req, res, callback);
} else if (res.statusCode == 200) { } else if (res.statusCode == 200) {
var response = {}; var response = {};
@ -156,8 +209,8 @@ module.exports = function(options, repo, params, id, dataResolver) {
callback(null, response); callback(null, response);
} else { } else {
//console.log('HTTP error', JSON.parse(body).message); // console.log('HTTP response error', req.url, res.statusCode);
callback(null, { data: new Buffer(0) }); createErrorResponse(req, res, callback);
} }
}); });
} }