-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (61 loc) · 2.01 KB
/
index.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
const fs = require('fs');
const express = require('express');
const ensureFontsGenerated = require('./ensurefontsgenerated.js');
const app = express();
const port = process.env.PORT || 8484;
const noCors = process.env.NOCORS || false;
if (!noCors) {
cors = require('cors');
app.use(cors());
}
function isNewRequest(req,res,next) {
let requestDateString = req.header('If-Modified-Since');
if (requestDateString) {
let requestDate = new Date(requestDateString);
if (requestDate >= serverStartDate) {
req.notModified = true;
}
}
if (!req.notModified) {
res.append('Last-Modified', serverStartDate.toISOString());
}
next();
}
app.use(isNewRequest);
const noLogs = process.env.NOLOGS || false;
if (!noLogs) {
const logger = require('morgan');
app.use(logger(':date[iso] ":method :url :status :res[content-length] - :response-time ms"'));
}
let fontDir = `${__dirname}/fonts/_output`;
ensureFontsGenerated(fontDir);
if (!fs.existsSync(fontDir)) {
console.error(`font directory not found (${fontDir})`);
process.exit(1);
}
let serverStartDate = new Date();
app.get('/:fontfamilies/:range', (req,res)=>{
if (req.notModified) {
res.status(304).send('Not Modified');
return;
}
let fontFamilies = req.params.fontfamilies.split(',');
let range = req.params.range;
let found = false;
for (let fontFamily of fontFamilies) {
if (fs.existsSync(`${fontDir}/${fontFamily}`)) {
let filename = `${fontDir}/${fontFamily}/${range}`;
if (fs.existsSync(filename)) {
res.setHeader('Content-Type', 'application/x-protobuf');
fs.createReadStream(filename).pipe(res);
found = true;
break;
}
}
}
if (!found) {
res.status(404).send('File not found');
}
})
app.listen(port);
console.log(`Font/Glyph server listening on port ${port}, cors ${noCors?'disabled':'enabled'}, logging ${noLogs?'disabled':'enabled'}`);