Reuse the function for creating empty responses
This commit is contained in:
parent
2e46700cd9
commit
7bdb7afcb9
1 changed files with 67 additions and 78 deletions
|
@ -36,14 +36,6 @@ 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.
|
* Lookup of sharp output formats by file extension.
|
||||||
*/
|
*/
|
||||||
|
@ -54,24 +46,42 @@ var extensionToFormat = {
|
||||||
'.webp': 'webp'
|
'.webp': 'webp'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cache of response data by sharp output format and color. Entry for empty
|
||||||
|
* string is for unknown or unsupported formats.
|
||||||
|
*/
|
||||||
|
var cachedEmptyResponses = {
|
||||||
|
'': new Buffer(0)
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an appropriate mbgl response for http errors.
|
* Create an appropriate mbgl response for http errors.
|
||||||
* @param {Object} req The mbgl req object.
|
* @param {string} format The format (a sharp format or 'pbf').
|
||||||
* @param {http.IncomingMessage} res The incoming response.
|
* @param {string} color The background color (or empty string for transparent).
|
||||||
* @param {Function} callback The mbgl callback.
|
* @param {Function} callback The mbgl callback.
|
||||||
*/
|
*/
|
||||||
function createErrorResponse(req, res, callback) {
|
function createEmptyResponse(format, color, callback) {
|
||||||
var parts = url.parse(req.url);
|
if (!format || format === 'pbf') {
|
||||||
var extension = path.extname(parts.pathname).toLowerCase();
|
callback(null, {data: cachedEmptyResponses['']});
|
||||||
var format = extensionToFormat[extension] || '';
|
return;
|
||||||
var data = cachedErrorResponses[format];
|
}
|
||||||
|
|
||||||
|
if (format === 'jpg') {
|
||||||
|
format = 'jpeg';
|
||||||
|
}
|
||||||
|
if (!color) {
|
||||||
|
color = 'rgba(255,255,255,0)';
|
||||||
|
}
|
||||||
|
|
||||||
|
var cacheKey = format + ',' + color;
|
||||||
|
var data = cachedEmptyResponses[cacheKey];
|
||||||
if (data) {
|
if (data) {
|
||||||
callback(null, {data: data});
|
callback(null, {data: data});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create an "empty" response image
|
// create an "empty" response image
|
||||||
var color = new Color('rgba(255,255,255,0)');
|
var color = new Color(color);
|
||||||
var array = color.array();
|
var array = color.array();
|
||||||
var channels = array.length == 4 && format != 'jpeg' ? 4 : 3;
|
var channels = array.length == 4 && format != 'jpeg' ? 4 : 3;
|
||||||
sharp(new Buffer(array), {
|
sharp(new Buffer(array), {
|
||||||
|
@ -82,7 +92,7 @@ function createErrorResponse(req, res, callback) {
|
||||||
}
|
}
|
||||||
}).toFormat(format).toBuffer(function(err, buffer, info) {
|
}).toFormat(format).toBuffer(function(err, buffer, info) {
|
||||||
if (!err) {
|
if (!err) {
|
||||||
cachedErrorResponses[format] = buffer;
|
cachedEmptyResponses[cacheKey] = buffer;
|
||||||
}
|
}
|
||||||
callback(null, {data: buffer});
|
callback(null, {data: buffer});
|
||||||
});
|
});
|
||||||
|
@ -162,10 +172,11 @@ module.exports = function(options, repo, params, id, dataResolver) {
|
||||||
source.getTile(z, x, y, function(err, data, headers) {
|
source.getTile(z, x, y, function(err, data, headers) {
|
||||||
if (err) {
|
if (err) {
|
||||||
//console.log('MBTiles error, serving empty', err);
|
//console.log('MBTiles error, serving empty', err);
|
||||||
callback(null, { data: source.emptyTile });
|
createEmptyResponse(source.format, source.color, callback);
|
||||||
} else {
|
return;
|
||||||
var response = {};
|
}
|
||||||
|
|
||||||
|
var response = {};
|
||||||
if (headers['Last-Modified']) {
|
if (headers['Last-Modified']) {
|
||||||
response.modified = new Date(headers['Last-Modified']);
|
response.modified = new Date(headers['Last-Modified']);
|
||||||
}
|
}
|
||||||
|
@ -181,7 +192,6 @@ module.exports = function(options, repo, params, id, dataResolver) {
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, response);
|
callback(null, response);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} else if (protocol == 'http' || protocol == 'https') {
|
} else if (protocol == 'http' || protocol == 'https') {
|
||||||
request({
|
request({
|
||||||
|
@ -189,12 +199,16 @@ module.exports = function(options, repo, params, id, dataResolver) {
|
||||||
encoding: null,
|
encoding: null,
|
||||||
gzip: true
|
gzip: true
|
||||||
}, function(err, res, body) {
|
}, function(err, res, body) {
|
||||||
if (err) {
|
var parts = url.parse(req.url);
|
||||||
// console.log('HTTP request error', err);
|
var extension = path.extname(parts.pathname).toLowerCase();
|
||||||
createErrorResponse(req, res, callback);
|
var format = extensionToFormat[extension] || '';
|
||||||
} else if (res.statusCode == 200) {
|
if (err || res.statusCode < 200 || res.statusCode >= 300) {
|
||||||
var response = {};
|
// console.log('HTTP error', err || res.statusCode);
|
||||||
|
createEmptyResponse(format, '', callback);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = {};
|
||||||
if (res.headers.modified) {
|
if (res.headers.modified) {
|
||||||
response.modified = new Date(res.headers.modified);
|
response.modified = new Date(res.headers.modified);
|
||||||
}
|
}
|
||||||
|
@ -206,12 +220,7 @@ module.exports = function(options, repo, params, id, dataResolver) {
|
||||||
}
|
}
|
||||||
|
|
||||||
response.data = body;
|
response.data = body;
|
||||||
|
|
||||||
callback(null, response);
|
callback(null, response);
|
||||||
} else {
|
|
||||||
// console.log('HTTP response error', req.url, res.statusCode);
|
|
||||||
createErrorResponse(req, res, callback);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -321,26 +330,6 @@ module.exports = function(options, repo, params, id, dataResolver) {
|
||||||
source = options.dataDecoratorFunc(name, 'tilejson', source);
|
source = options.dataDecoratorFunc(name, 'tilejson', source);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source.format == 'pbf') {
|
|
||||||
map.sources[name].emptyTile = new Buffer(0);
|
|
||||||
} else {
|
|
||||||
var color = new Color(source.color || 'rgba(255,255,255,0)');
|
|
||||||
var format = source.format;
|
|
||||||
if (format == 'jpg') {
|
|
||||||
format = 'jpeg';
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
map.sources[name].emptyTile = buffer;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (!attributionOverride &&
|
if (!attributionOverride &&
|
||||||
source.attribution && source.attribution.length > 0) {
|
source.attribution && source.attribution.length > 0) {
|
||||||
if (tileJSON.attribution.length > 0) {
|
if (tileJSON.attribution.length > 0) {
|
||||||
|
|
Loading…
Reference in a new issue