forked from coollabsio/fonts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
94 lines (88 loc) · 2.92 KB
/
index.mjs
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
import fastify from 'fastify'
import got from 'got'
import 'dotenv/config'
const domain = process.env.DOMAIN
const server = fastify()
const data = await got.get(`https://google-webfonts-helper.herokuapp.com/api/fonts/`).json()
const subsets = {
devanagari: {
'unicode-range': 'U+0900-097F, U+1CD0-1CF6, U+1CF8-1CF9, U+200C-200D, U+20A8, U+20B9, U+25CC, U+A830-A839, U+A8E0-A8FB'
},
'latin-ext': {
'unicode-range': 'U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF'
},
latin: {
'unicode-range': 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD'
},
vietnamese: {
'unicode-range': 'U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+1EA0-1EF9, U+20AB'
},
cyrillic: {
'unicode-range': 'U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116'
},
'cyrillic-ext': {
'unicode-range': 'U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F'
},
'thai': {
'unicode-range': 'U+0E01-0E5B, U+200C-200D, U+25CC'
},
greek: {
'unicode-range': 'U+0370-03FF'
},
'greek-ext': {
'unicode-range': 'U+1F00-1FFF'
}
}
server.get('/', (response, reply) => {
reply.redirect('https://fonts.coollabs.io')
})
server.get('/css2', async (request, reply) => {
let { family: families, display } = request.query
if (families) {
if (typeof families === 'string') {
families = [families]
}
const payload = []
for (let family of families) {
let style = 'normal'
let weights = ['400']
let dashFamily = family.toLowerCase().replace(/ /g, '-')
if (family.includes('wght')) {
weights = family.split(':')[1].split('@')[1].split(';').filter(n => n)
dashFamily = family.toLowerCase().replace(/ /g, '-').split(':')[0]
}
family = family.split(':')[0]
const properties = data.find(f => f.id === dashFamily)
for (let weight of weights) {
for (const subset of properties?.subsets) {
if (weight.includes(',')) {
style = weight.split(',')[0] === '0' ? 'normal' : 'italic'
}
weight = weight.includes(',') ? weight.split(',')[1] : weight
payload.push(`
/* ${subset} */
@font-face {
font-family: '${family}';
font-style: ${style};
font-weight: ${weight};
${display ? 'font-display: swap;' : 'font-display: auto;'}
src: url(https://${domain}/${dashFamily}/${style}/${weight}.woff2) format('woff2');
unicode-range: ${subsets[subset]['unicode-range']};
}
`)
}
}
}
reply.raw.writeHead(200, { 'Content-Type': 'text/css;charset=UTF-8' })
reply.raw.write(payload.join(' ').trim())
return reply.raw.end()
} else {
throw { statusCode: 500, message: 'Wrong request' }
}
})
try {
await server.listen(3000, '0.0.0.0')
} catch (err) {
server.log.error(err)
process.exit(1)
}