-
Notifications
You must be signed in to change notification settings - Fork 16
/
gather-fonts.mjs
213 lines (178 loc) · 6.18 KB
/
gather-fonts.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import got from "got";
import { promises as fs } from "fs";
import { promisify } from "node:util";
import stream from "node:stream";
import css from "css";
import pLimit from "p-limit";
import "dotenv/config";
const CONCURRENT_DOWNLOADS = 10;
const MAX_RETRIES = 3;
const CACHE_FILE = "./font-cache.json";
const base = "gwfh.mranftl.com";
const storageZoneName = "coollabs-fonts";
const pipeline = promisify(stream.pipeline);
const limit = pLimit(CONCURRENT_DOWNLOADS);
// Progress tracking
let totalFonts = 0;
let processedFonts = 0;
let totalVariants = 0;
let processedVariants = 0;
let startTime = Date.now();
let lastLogTime = 0;
function formatTime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
return `${hours}h ${minutes}m ${secs}s`;
}
function logProgress() {
const now = Date.now();
// Throttle updates to max once per second
if (now - lastLogTime < 1000) return;
lastLogTime = now;
const elapsedSeconds = Math.floor((Date.now() - startTime) / 1000);
const remainingFonts = totalFonts - processedFonts;
const remainingVariants = totalVariants - processedVariants;
let progressText = `Progress: ${processedFonts}/${totalFonts} fonts, ${processedVariants}/${totalVariants} variants | `;
progressText += `Time: ${formatTime(elapsedSeconds)} | `;
if (processedVariants > 0) {
const avgTimePerVariant = elapsedSeconds / processedVariants;
const estimatedRemainingSeconds = Math.floor(avgTimePerVariant * remainingVariants);
progressText += `ETA: ${formatTime(estimatedRemainingSeconds)}`;
}
console.log(progressText);
}
async function fetchWithRetry(url, options = {}, retries = MAX_RETRIES) {
try {
return await got.get(url, options).json();
} catch (error) {
if (retries > 0) {
await new Promise(resolve => setTimeout(resolve, 1000));
return fetchWithRetry(url, options, retries - 1);
}
throw error;
}
}
async function loadCache() {
try {
const cache = await fs.readFile(CACHE_FILE, 'utf-8');
return JSON.parse(cache);
} catch {
return {};
}
}
async function saveCache(cache) {
await fs.writeFile(CACHE_FILE, JSON.stringify(cache));
}
async function processFont(family, cache) {
const cacheKey = `font_${family}`;
let variants;
if (cache[cacheKey]) {
variants = cache[cacheKey];
} else {
const response = await fetchWithRetry(`https://${base}/api/fonts/${family}`);
variants = response.variants;
cache[cacheKey] = variants;
}
totalVariants += variants.length;
logProgress();
await Promise.all(
variants.map(variant =>
limit(async () => {
const id = variant.fontWeight;
const dir = `./${family}/${variant.fontStyle}`;
try {
await pipeline(
got.stream(`${variant.woff2}`),
await got.stream.put(
`https://storage.bunnycdn.com/${storageZoneName}/${dir}/${id}.woff2`,
{
headers: {
AccessKey: process.env.BUNNY_API_KEY,
},
retry: { limit: MAX_RETRIES }
},
),
new stream.PassThrough()
);
processedVariants++;
logProgress();
} catch (err) {
console.error(`\nError processing ${family}/${variant.fontStyle}/${id}:`, err.message);
logProgress(); // Redraw progress after error message
}
})
)
);
processedFonts++;
logProgress();
}
async function generateSubsets(data) {
const subsets = new Set(data.flatMap(d => d.subsets));
const allSubsets = {};
const totalSubsets = subsets.size;
let processedSubsets = 0;
console.log(`Processing subsets: 0/${totalSubsets}`);
await Promise.all(
Array.from(subsets).map(async subset => {
const example = data.find(d => d.subsets.includes(subset));
try {
const gf = await got
.get(`https://fonts.googleapis.com/css2?family=${example.family}`, {
headers: {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
},
retry: { limit: MAX_RETRIES }
})
.text();
const parsedCss = css.parse(gf);
let currentSubset;
parsedCss.stylesheet.rules.forEach(a => {
if (a.type === "comment") {
currentSubset = a.comment.trim();
allSubsets[currentSubset] = "";
}
if (a.type === "font-face") {
const value = a.declarations.find(d => d.property === "unicode-range")?.value;
if (value) allSubsets[currentSubset] = value;
}
});
processedSubsets++;
console.log(`Processing subsets: ${processedSubsets}/${totalSubsets}`);
} catch (err) {
console.error(`Error processing subset ${subset}:`, err.message);
console.log(`Processing subsets: ${processedSubsets}/${totalSubsets}`);
}
})
);
return allSubsets;
}
async function main() {
console.log('Starting font processing...');
startTime = Date.now();
const cache = await loadCache();
const data = await fetchWithRetry(`https://${base}/api/fonts/`);
const families = data.map(d => d.id).filter(Boolean);
totalFonts = families.length;
console.log(`Found ${totalFonts} font families to process`);
// Generate and save subsets in parallel with font processing
const subsetsPromise = generateSubsets(data);
if (process.env.BUNNY_API_KEY) {
// Process fonts in parallel with limited concurrency
await Promise.all(
families.map(family => processFont(family, cache))
);
}
const allSubsets = await subsetsPromise;
await Promise.all([
fs.writeFile("./subsets.json", JSON.stringify(allSubsets)),
saveCache(cache)
]);
const totalTime = Math.floor((Date.now() - startTime) / 1000);
console.log('\n=== Final Statistics ===');
console.log(`Total execution time: ${formatTime(totalTime)}`);
console.log(`Processed ${processedFonts} fonts with ${processedVariants} variants`);
console.log(`Average time per variant: ${(totalTime / processedVariants).toFixed(2)}s`);
console.log('=====================');
}
main().catch(console.error);