36 lines
1.5 KiB
JavaScript
36 lines
1.5 KiB
JavaScript
/* 探测新增图标源可达性 */
|
|
const https = require('https');
|
|
function fetch(url) {
|
|
return new Promise((resolve, reject) => {
|
|
const req = https.get(url, { timeout: 15000 }, (res) => {
|
|
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
res.resume();
|
|
return resolve(fetch(res.headers.location.startsWith('/') ? 'https://unpkg.com' + res.headers.location : res.headers.location));
|
|
}
|
|
if (res.statusCode !== 200) { res.resume(); return reject(new Error(res.statusCode + ' ' + url)); }
|
|
const chunks = [];
|
|
res.on('data', c => chunks.push(c));
|
|
res.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
});
|
|
req.on('error', reject);
|
|
req.on('timeout', () => req.destroy(new Error('timeout')));
|
|
});
|
|
}
|
|
(async () => {
|
|
const urls = {
|
|
bootstrap: 'https://unpkg.com/bootstrap-icons@1.11.3/bootstrap-icons.svg',
|
|
fa_solid: 'https://unpkg.com/@fortawesome/fontawesome-free@6.5.2/sprites/solid.svg',
|
|
fa_regular: 'https://unpkg.com/@fortawesome/fontawesome-free@6.5.2/sprites/regular.svg',
|
|
fa_brands: 'https://unpkg.com/@fortawesome/fontawesome-free@6.5.2/sprites/brands.svg'
|
|
};
|
|
for (const [k, u] of Object.entries(urls)) {
|
|
try {
|
|
const s = await fetch(u);
|
|
const syms = (s.match(/<symbol/g) || []).length;
|
|
console.log(`${k}: OK ${(s.length / 1024).toFixed(0)}KB symbols=${syms}`);
|
|
} catch (e) {
|
|
console.log(`${k}: FAIL ${e.message}`);
|
|
}
|
|
}
|
|
})();
|