-
Notifications
You must be signed in to change notification settings - Fork 16
/
css.mjs
83 lines (83 loc) · 2.45 KB
/
css.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
export function css(request, reply, data, domain, subsets) {
try {
const payload = [];
let {
family: familyQuery,
display = null,
subset = null,
ver = null,
} = request.query;
const families = familyQuery.split("|");
for (let family of families) {
let style = "normal";
let weights = ["400"];
const name = family.split(":")[0];
const foundWeights = family
.split(":")[1]
.split(",")
.map((weight) => {
if (weight === "i" || weight === "italic") {
weight = "400i";
return weight;
}
if (
/^[a-zA-Z]+$/.test(weight) &&
weight !== "i" &&
weight !== "italic" &&
weight !== "b" &&
weight !== "bold" &&
weight !== "bi" &&
weight !== "ib" &&
weight !== "bolditalic" &&
weight !== "italicbold"
) {
return "400";
}
return weight;
});
if (foundWeights.length > 0) {
weights = foundWeights;
}
const dashFamily = name.toLowerCase().replace(/ /g, "-");
const properties = data.find((f) => f.id === dashFamily);
if (weights.length > 0) {
for (let weight of weights) {
for (const sub of properties?.subsets) {
if (weight.includes("italic") || weight.includes("i")) {
style = "italic";
weight = weight.replace("italic", "").replace("i", "");
}
if (weight.includes("b") || weight.includes("bold")) {
weight = "700";
}
let css = `
/* ${sub} */
@font-face {
font-family: '${name}';
font-style: ${style};
font-weight: ${weight};`;
if (display)
css += `
font-display: swap;`;
css += `
src: url(https://${domain}/${dashFamily}/${style}/${weight}.woff2) format('woff2');
unicode-range: ${subsets[sub]};
}`;
payload.push(css);
}
}
}
}
if (payload.length === 0) {
throw 'Wrong request. Please <a href="https://docs.coollabs.io/contact">contact us</a> if you think this is a bug.';
}
reply.header("content-type", "text/css");
return reply.send(payload.join(" ").trim());
} catch (error) {
if (typeof error === "string") {
reply.header("content-type", "text/html");
throw error;
}
throw { statusCode: 500, message: error.message };
}
}