-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
143 lines (131 loc) · 3.51 KB
/
server.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import http from 'http'
import fs from 'fs'
import fsp from 'fs/promises'
import p from 'path-to-regexp'
const FontMetadata = JSON.parse(
fs.readFileSync('./google-fonts-repository/metadata/google-fonts-v2.json'),
)
{
// Compute some statistics.
const /** @type {string[]} */ allCategories = []
const /** @type {string[]} */ allSubsets = []
const /** @type {string[]} */ allStyles = []
const /** @type {string[]} */ allWeights = []
for (const fontId in FontMetadata) {
const category = FontMetadata[fontId].category
if (!allCategories.includes(category)) {
allCategories.push(category)
}
const subsets = FontMetadata[fontId].subsets
for (const subset of subsets) {
if (!allSubsets.includes(subset)) {
allSubsets.push(subset)
}
}
const styles = FontMetadata[fontId].styles
for (const style of styles) {
if (!allStyles.includes(style)) {
allStyles.push(style)
}
}
const weights = FontMetadata[fontId].weights
for (const weight of weights) {
if (!allWeights.includes(weight)) {
allWeights.push(weight)
}
}
allWeights.sort()
}
console.info('categories', allCategories)
// console.info('subsets', allSubsets)
console.info('styles', allStyles)
console.info('weights', allWeights)
}
const server = http.createServer(async (req, res) => {
{
const fn = p.match('/api/font/get-all-metadata')
if (req.method === 'GET' && fn(req.url)) {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(FontMetadata))
return
}
}
{
const fn = p.match('/api/font/get-metadata/:fontId')
if (req.method === 'POST' && fn(req.url)) {
const { fontId } = fn(req.url).params
const json = await fsp.readFile(
`./google-fonts-repository/fonts/google/${fontId}/metadata.json`,
)
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(json)
return
}
}
{
const fn = p.match('/api/font/get-metadata2/:fontId')
if (req.method === 'POST' && fn(req.url)) {
const { fontId } = fn(req.url).params
const metadata = FontMetadata[fontId]
if (metadata) {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(metadata))
} else {
res.writeHead(404)
res.end()
}
return
}
}
{
const fn = p.match('/fonts/get-full-stylesheet/:fontId.css')
if (req.method === 'GET' && fn(req.url)) {
const { fontId } = fn(req.url).params
let stylesheet = ''
for (const stat of await fsp.readdir(
`./google-fonts-repository/fonts/google/${fontId}`,
{
withFileTypes: true,
},
)) {
if (!stat.isFile()) {
continue
}
if (/^[0-9]{3}/.test(stat.name)) {
stylesheet += (
await fsp.readFile(
`./google-fonts-repository/fonts/google/${fontId}/${stat.name}`,
'utf-8',
)
).replaceAll('./files/', `/static/${fontId}/`)
stylesheet += '\n'
}
}
res.setHeader('Content-Type', 'text/css')
res.write(stylesheet)
res.end()
return
}
}
{
const fn = p.match('/static/:fontId/:fontFile')
if (req.method === 'GET' && fn(req.url)) {
const match = p.match('/static/:fontId/:fontFile')(req.url)
const fontId = match.params.fontId
const fontFile = match.params.fontFile
const content = await fsp.readFile(
`./google-fonts-repository/fonts/google/${fontId}/files/${fontFile}`,
)
res.write(content)
res.end()
return
}
}
{
console.log(`No match found: "${req.method}: ${req.url}"`)
res.writeHead(404)
res.end()
return
}
})
server.listen(3001, () => console.log('Server listening on port 3001'))