Update serve_font.js
This commit is contained in:
parent
d85226f7d0
commit
9a98b38db7
1 changed files with 6 additions and 41 deletions
|
@ -30,41 +30,15 @@ export async function serve_font(options, allowedFonts, programOpts) {
|
||||||
* @returns {Promise<void>}
|
* @returns {Promise<void>}
|
||||||
*/
|
*/
|
||||||
app.get('/fonts/:fontstack/:range.pbf', async (req, res) => {
|
app.get('/fonts/:fontstack/:range.pbf', async (req, res) => {
|
||||||
let fontstack = req.params.fontstack;
|
|
||||||
const fontStackParts = fontstack.split(',');
|
|
||||||
const sanitizedFontStack = fontStackParts
|
|
||||||
.map((font) => {
|
|
||||||
const fontMatch = font?.match(/^[\w\s-]+$/);
|
|
||||||
return fontMatch?.[0] || 'invalid';
|
|
||||||
})
|
|
||||||
.filter(Boolean)
|
|
||||||
.join(',');
|
|
||||||
if (sanitizedFontStack.length == 0) {
|
|
||||||
return res.status(400).send('Invalid font stack format');
|
|
||||||
}
|
|
||||||
|
|
||||||
fontstack = decodeURI(sanitizedFontStack);
|
|
||||||
let range = req.params.range;
|
|
||||||
const rangeMatch = range?.match(/^[\d-]+$/);
|
|
||||||
const sanitizedRange = rangeMatch?.[0] || 'invalid';
|
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
console.log(
|
console.log(
|
||||||
`Handling font request for: /fonts/%s/%s.pbf`,
|
`Handling font request for: /fonts/%s/%s.pbf`,
|
||||||
sanitizedFontStack.replace(/\n|\r/g, ''),
|
String(req.params.fontstack).replace(/\n|\r/g, ''),
|
||||||
sanitizedRange.replace(/\n|\r/g, ''),
|
String(req.params.range).replace(/\n|\r/g, ''),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
const fontstack = decodeURI(req.params.fontstack);
|
||||||
const modifiedSince = req.get('if-modified-since');
|
const range = req.params.range;
|
||||||
const cc = req.get('cache-control');
|
|
||||||
if (modifiedSince && (!cc || cc.indexOf('no-cache') === -1)) {
|
|
||||||
if (
|
|
||||||
new Date(lastModified).getTime() === new Date(modifiedSince).getTime()
|
|
||||||
) {
|
|
||||||
return res.sendStatus(304);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const concatenated = await getFontsPbf(
|
const concatenated = await getFontsPbf(
|
||||||
options.serveAllFonts ? null : allowedFonts,
|
options.serveAllFonts ? null : allowedFonts,
|
||||||
|
@ -73,21 +47,12 @@ export async function serve_font(options, allowedFonts, programOpts) {
|
||||||
range,
|
range,
|
||||||
existingFonts,
|
existingFonts,
|
||||||
);
|
);
|
||||||
|
|
||||||
res.header('Content-type', 'application/x-protobuf');
|
res.header('Content-type', 'application/x-protobuf');
|
||||||
res.header('Last-Modified', lastModified);
|
res.header('Last-Modified', lastModified);
|
||||||
return res.send(concatenated);
|
return res.send(concatenated);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(
|
console.error('Error serving font:', err);
|
||||||
`Error serving font: %s/%s.pbf, Error: %s`,
|
return res.status(400).header('Content-Type', 'text/plain').send(err);
|
||||||
sanitizedFontStack.replace(/\n|\r/g, ''),
|
|
||||||
sanitizedRange.replace(/\n|\r/g, ''),
|
|
||||||
String(err),
|
|
||||||
);
|
|
||||||
return res
|
|
||||||
.status(400)
|
|
||||||
.header('Content-Type', 'text/plain')
|
|
||||||
.send('Error serving font');
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue