-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathutils.js
173 lines (143 loc) · 4.84 KB
/
utils.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
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
const https = require('https');
const postcss = require('postcss');
const userAgents = require('./user-agents.json');
const getSortedObject = object => {
let sortedObject = {};
Object.keys(object)
.sort()
.forEach(key => {
const entry = object[key];
if (Array.isArray(entry) || typeof entry !== 'object') {
sortedObject[key] = entry;
} else {
sortedObject[key] = getSortedObject(entry);
}
});
return sortedObject;
};
const fetch = async(options, delay = 0) => {
await new Promise(resolve => setTimeout(resolve, delay));
return new Promise((resolve) => {
https.get(options, response => {
let result = '';
response.on('data', data => {
result += data;
});
response.on('end', () => {
resolve(result);
});
});
});
};
const convertFont = async ({ convertedFont, family, format }, fetchOptions) => {
let { variants, unicodeRange } = convertedFont;
const css = await fetch(fetchOptions, Math.random() * 5000);
if (css) {
let subset = null;
const root = postcss.parse(css);
root.each(rule => {
if (rule.type === 'comment') {
subset = rule.text;
}
if (rule.type === 'atrule' && rule.name === 'font-face') {
let fontStyle = 'normal';
let fontWeight = '400';
rule.walkDecls('font-weight', decl => {
fontWeight = decl.value;
});
rule.walkDecls('font-style', decl => {
fontStyle = decl.value;
});
variants[fontStyle] = variants[fontStyle] || {};
variants[fontStyle][fontWeight] = variants[fontStyle][fontWeight] || {
local: [],
url: {}
};
rule.walkDecls('src', decl => {
postcss.list.comma(decl.value).forEach(value => {
value.replace(
/(local|url)\((.+?)\)/g,
(match, type, path) => {
if (type === 'local') {
if (
variants[fontStyle][fontWeight].local.indexOf(path) === -1
) {
variants[fontStyle][fontWeight].local.push(path);
}
} else if (type === 'url') {
variants[fontStyle][fontWeight].url[format] = path;
}
}
);
});
});
rule.walkDecls('unicode-range', decl => {
unicodeRange = {
...unicodeRange,
[subset]: decl.value
}
});
console.log('Captured', family, fontStyle, fontWeight, 'as', format, '...');
}
});
return {
...convertedFont,
variants,
unicodeRange
};
} else {
console.log('Rejected', family, 'as', format, '...');
return null;
}
};
const getFetchOptions = ({ family, variants, format, pathCb }) => {
const userAgent = userAgents[format];
const variantsList = ['eot', 'svg'].includes(format)
? variants
: [variants.join(',')];
return variantsList.map(variant => ({
host: 'fonts.googleapis.com',
path: encodeURI(pathCb({ family, variant })),
headers: {
'User-Agent': userAgent
}
}));
}
const convertFontsOptions = async (fonts, pathCb) => {
let results = {};
for (const font of fonts) {
const { family, variants, ...originalFont } = font;
const agents = Object.keys(userAgents);
let convertedFont = {
...originalFont,
variants: {},
unicodeRange: {}
};
for (const format of agents) {
const optionsList = getFetchOptions({ family, variants, format, pathCb });
for (const options of optionsList) {
convertedFont = await convertFont({ convertedFont, family, format }, options);
}
}
results[family] = convertedFont;
}
return results;
};
const getChunkedFonts = fonts => fonts.reduce((acc, font) => {
const key = font?.family[0];
acc[key] = acc[key]
? [
...acc[key],
font
]
: [font]
return acc;
}, {})
module.exports = {
fetch,
convertFont,
getSortedObject,
getFetchOptions,
getChunkedFonts,
convertFontsOptions
}