Fix CORS headers, add not_found to icon responses, do not show warning when region is empty

This commit is contained in:
Vjacheslav Trushkin 2020-07-22 20:59:53 +03:00
parent 34873413b1
commit fd339f8062
3 changed files with 33 additions and 5 deletions

5
app.js
View File

@ -82,8 +82,9 @@ if (app.config['env-region']) {
}
}
if (
app.config.region.length > 10 ||
!app.config.region.match(/^[a-z0-9_-]+$/i)
app.config.region !== '' &&
(app.config.region.length > 10 ||
!app.config.region.match(/^[a-z0-9_-]+$/i))
) {
app.config.region = '';
app.error('Invalid value for region config variable.');

View File

@ -71,11 +71,24 @@ module.exports = (app, req, res, prefix, query, ext) => {
return 404;
}
let result = collection.getIcons(params.icons.split(','));
let icons = params.icons.split(',');
let result = collection.getIcons(icons, true);
if (result === null || !Object.keys(result.icons).length) {
return 404;
// Return 404 for JSON query, data for JS query
if (result === null) {
if (ext === 'json') {
return 404;
}
return {
js: ext === 'js',
defaultCallback: 'SimpleSVG._loaderCallback',
data: {
prefix: prefix,
not_found: icons,
},
};
}
if (
result.aliases !== void 0 &&
!Object.keys(result.aliases).length

View File

@ -28,6 +28,12 @@ const callbackMatch = /^[a-z0-9_.]+$/i;
module.exports = (app, req, res, result) => {
if (typeof result === 'number') {
// Send error
if (app.config.cors) {
res.header('Access-Control-Allow-Origin', app.config.cors.origins);
res.header('Access-Control-Allow-Methods', app.config.cors.methods);
res.header('Access-Control-Allow-Headers', app.config.cors.headers);
res.header('Access-Control-Max-Age', app.config.cors.timeout);
}
res.sendStatus(result);
return;
}
@ -71,6 +77,14 @@ module.exports = (app, req, res, result) => {
}
}
// CORS
if (app.config.cors) {
res.header('Access-Control-Allow-Origin', app.config.cors.origins);
res.header('Access-Control-Allow-Methods', app.config.cors.methods);
res.header('Access-Control-Allow-Headers', app.config.cors.headers);
res.header('Access-Control-Max-Age', app.config.cors.timeout);
}
// Send cache header
if (
app.config.cache &&