-
Notifications
You must be signed in to change notification settings - Fork 653
/
Copy pathserve_font.js
101 lines (89 loc) · 2.94 KB
/
serve_font.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
import express from 'express';
import { getFontsPbf, listFonts } from './utils.js';
/**
* Initializes and returns an Express app that serves font files.
* @param {object} options - Configuration options for the server.
* @param {object} allowedFonts - An object containing allowed fonts.
* @param {object} programOpts - An object containing the program options.
* @returns {Promise<express.Application>} - A promise that resolves to the Express app.
*/
export async function serve_font(options, allowedFonts, programOpts) {
const { verbose } = programOpts;
const app = express().disable('x-powered-by');
const lastModified = new Date().toUTCString();
const fontPath = options.paths.fonts;
const existingFonts = {};
/**
* Handles requests for a font file.
* @param {object} req - Express request object.
* @param {object} res - Express response object.
* @param {string} req.params.fontstack - Name of the font stack.
* @param {string} req.params.range - The range of the font (e.g. 0-255).
* @returns {Promise<void>}
*/
app.get('/fonts/:fontstack/:range.pbf', async (req, res) => {
const sRange = String(req.params.range).replace(/\n|\r/g, '');
const sFontStack = String(decodeURI(req.params.fontstack)).replace(
/\n|\r/g,
'',
);
if (verbose) {
console.log(
`Handling font request for: /fonts/%s/%s.pbf`,
sFontStack,
sRange,
);
}
const modifiedSince = req.get('if-modified-since');
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 {
const concatenated = await getFontsPbf(
options.serveAllFonts ? null : allowedFonts,
fontPath,
sFontStack,
sRange,
existingFonts,
);
res.header('Content-type', 'application/x-protobuf');
res.header('Last-Modified', lastModified);
return res.send(concatenated);
} catch (err) {
console.error(
`Error serving font: %s/%s.pbf, Error: %s`,
sFontStack,
sRange,
String(err),
);
return res
.status(400)
.header('Content-Type', 'text/plain')
.send('Error serving font');
}
});
/**
* Handles requests for a list of all available fonts.
* @param {object} req - Express request object.
* @param {object} res - Express response object.
* @returns {void}
*/
app.get('/fonts.json', (req, res) => {
if (verbose) {
console.log('Handling list font request for /fonts.json');
}
res.header('Content-type', 'application/json');
return res.send(
Object.keys(options.serveAllFonts ? existingFonts : allowedFonts).sort(),
);
});
const fonts = await listFonts(options.paths.fonts);
Object.assign(existingFonts, fonts);
return app;
}