|
9 | 9 | import { HttpsProxyAgent } from 'https-proxy-agent'; |
10 | 10 | import { createHash } from 'node:crypto'; |
11 | 11 | import { readFile, rm, writeFile } from 'node:fs/promises'; |
12 | | -import * as https from 'node:https'; |
| 12 | +import { Agent, get as httpsGet } from 'node:https'; |
13 | 13 | import { join } from 'node:path'; |
14 | 14 | import { NormalizedCachedOptions } from '../normalize-cache'; |
15 | 15 | import { htmlRewritingStream } from './html-rewriting-stream'; |
@@ -194,56 +194,49 @@ export class InlineFontsProcessor { |
194 | 194 | } catch {} |
195 | 195 | } |
196 | 196 |
|
197 | | - let agent: HttpsProxyAgent<string> | undefined; |
198 | 197 | const httpsProxy = process.env.HTTPS_PROXY ?? process.env.https_proxy; |
199 | | - |
200 | | - if (httpsProxy) { |
201 | | - agent = new HttpsProxyAgent(httpsProxy); |
202 | | - } |
203 | | - |
204 | 198 | const data = await new Promise<string>((resolve, reject) => { |
205 | 199 | let rawResponse = ''; |
206 | | - https |
207 | | - .get( |
208 | | - url, |
209 | | - { |
210 | | - agent, |
211 | | - headers: { |
212 | | - /** |
213 | | - * Always use a Windows UA. This is because Google fonts will including hinting in fonts for Windows. |
214 | | - * Hinting is a technique used with Windows files to improve appearance however |
215 | | - * results in 20-50% larger file sizes. |
216 | | - * |
217 | | - * @see http://google3/java/com/google/fonts/css/OpenSansWebFontsCssBuilder.java?l=22 |
218 | | - * @see https://fonts.google.com/knowledge/glossary/hinting (short) |
219 | | - * @see https://glyphsapp.com/learn/hinting-manual-truetype-hinting (deep dive) |
220 | | - */ |
221 | | - 'user-agent': |
222 | | - 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', |
223 | | - }, |
| 200 | + httpsGet( |
| 201 | + url, |
| 202 | + { |
| 203 | + // TODO(alanagius): remove casting https://github.com/TooTallNate/proxy-agents/issues/379 is fixed. |
| 204 | + agent: httpsProxy ? (new HttpsProxyAgent(httpsProxy) as Agent) : undefined, |
| 205 | + headers: { |
| 206 | + /** |
| 207 | + * Always use a Windows UA. This is because Google fonts will including hinting in fonts for Windows. |
| 208 | + * Hinting is a technique used with Windows files to improve appearance however |
| 209 | + * results in 20-50% larger file sizes. |
| 210 | + * |
| 211 | + * @see http://google3/java/com/google/fonts/css/OpenSansWebFontsCssBuilder.java?l=22 |
| 212 | + * @see https://fonts.google.com/knowledge/glossary/hinting (short) |
| 213 | + * @see https://glyphsapp.com/learn/hinting-manual-truetype-hinting (deep dive) |
| 214 | + */ |
| 215 | + 'user-agent': |
| 216 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', |
224 | 217 | }, |
225 | | - (res) => { |
226 | | - if (res.statusCode !== 200) { |
227 | | - reject( |
228 | | - new Error( |
229 | | - `Inlining of fonts failed. ${url} returned status code: ${res.statusCode}.`, |
230 | | - ), |
231 | | - ); |
232 | | - |
233 | | - return; |
234 | | - } |
235 | | - |
236 | | - res.on('data', (chunk) => (rawResponse += chunk)).on('end', () => resolve(rawResponse)); |
237 | | - }, |
238 | | - ) |
239 | | - .on('error', (e) => |
240 | | - reject( |
241 | | - new Error( |
242 | | - `Inlining of fonts failed. An error has occurred while retrieving ${url} over the internet.\n` + |
243 | | - e.message, |
244 | | - ), |
| 218 | + }, |
| 219 | + (res) => { |
| 220 | + if (res.statusCode !== 200) { |
| 221 | + reject( |
| 222 | + new Error( |
| 223 | + `Inlining of fonts failed. ${url} returned status code: ${res.statusCode}.`, |
| 224 | + ), |
| 225 | + ); |
| 226 | + |
| 227 | + return; |
| 228 | + } |
| 229 | + |
| 230 | + res.on('data', (chunk) => (rawResponse += chunk)).on('end', () => resolve(rawResponse)); |
| 231 | + }, |
| 232 | + ).on('error', (e) => |
| 233 | + reject( |
| 234 | + new Error( |
| 235 | + `Inlining of fonts failed. An error has occurred while retrieving ${url} over the internet.\n` + |
| 236 | + e.message, |
245 | 237 | ), |
246 | | - ); |
| 238 | + ), |
| 239 | + ); |
247 | 240 | }); |
248 | 241 |
|
249 | 242 | if (cacheFile) { |
|
0 commit comments