-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathinline-fonts.ts
191 lines (154 loc) · 5.49 KB
/
inline-fonts.ts
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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as cacache from 'cacache';
import * as fs from 'fs';
import * as https from 'https';
import * as proxyAgent from 'https-proxy-agent';
import { URL } from 'url';
import { findCachePath } from '../cache-path';
import { cachingDisabled } from '../environment-options';
import { htmlRewritingStream } from './html-rewriting-stream';
const cacheFontsPath: string | undefined = cachingDisabled ? undefined : findCachePath('angular-build-fonts');
const packageVersion = require('../../../package.json').version;
const enum UserAgent {
Chrome = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
IE = 'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11. 0) like Gecko',
}
const SUPPORTED_PROVIDERS = [
'fonts.googleapis.com',
];
export interface InlineFontsOptions {
minify?: boolean;
WOFFSupportNeeded: boolean;
}
export class InlineFontsProcessor {
constructor(private options: InlineFontsOptions) { }
async process(content: string): Promise<string> {
const hrefList: string[] = [];
// Collector link tags with href
const { rewriter: collectorStream } = await htmlRewritingStream(content);
collectorStream.on('startTag', tag => {
const { tagName, attrs } = tag;
if (tagName !== 'link') {
return;
}
// <link tag with rel="stylesheet" and a href.
const href = attrs.find(({ name, value }) => name === 'rel' && value === 'stylesheet')
&& attrs.find(({ name }) => name === 'href')?.value;
if (href) {
hrefList.push(href);
}
});
await new Promise(resolve => collectorStream.on('finish', resolve));
// Download stylesheets
const hrefsContent = await this.processHrefs(hrefList);
if (hrefsContent.size === 0) {
return content;
}
// Replace link with style tag.
const { rewriter, transformedContent } = await htmlRewritingStream(content);
rewriter.on('startTag', tag => {
const { tagName, attrs } = tag;
if (tagName !== 'link') {
rewriter.emitStartTag(tag);
return;
}
const hrefAttr = attrs.some(({ name, value }) => name === 'rel' && value === 'stylesheet')
&& attrs.find(({ name, value }) => name === 'href' && hrefsContent.has(value));
if (hrefAttr) {
const href = hrefAttr.value;
const cssContent = hrefsContent.get(href);
rewriter.emitRaw(`<style type="text/css">${cssContent}</style>`);
} else {
rewriter.emitStartTag(tag);
}
});
return transformedContent;
}
private async getResponse(url: URL, userAgent: UserAgent): Promise<string> {
const key = `${packageVersion}|${url}|${userAgent}`;
if (cacheFontsPath) {
const entry = await cacache.get.info(cacheFontsPath, key);
if (entry) {
return fs.promises.readFile(entry.path, 'utf8');
}
}
let agent: proxyAgent.HttpsProxyAgent | undefined;
const httpsProxy = process.env.HTTPS_PROXY ?? process.env.https_proxy;
if (httpsProxy) {
agent = proxyAgent(httpsProxy);
}
const data = await new Promise<string>((resolve, reject) => {
let rawResponse = '';
https.get(
url,
{
agent,
rejectUnauthorized: false,
headers: {
'user-agent': userAgent,
},
},
res => {
if (res.statusCode !== 200) {
reject(new Error(`Inlining of fonts failed. ${url} returned status code: ${res.statusCode}.`));
return;
}
res
.on('data', chunk => rawResponse += chunk)
.on('end', () => resolve(rawResponse));
},
)
.on('error', e =>
reject(new Error(
`Inlining of fonts failed. An error has occurred while retrieving ${url} over the internet.\n` +
e.message,
)));
});
if (cacheFontsPath) {
await cacache.put(cacheFontsPath, key, data);
}
return data;
}
private async processHrefs(hrefList: string[]): Promise<Map<string, string>> {
const hrefsContent = new Map<string, string>();
for (const hrefPath of hrefList) {
// Need to convert '//' to 'https://' because the URL parser will fail with '//'.
const normalizedHref = hrefPath.startsWith('//') ? `https:${hrefPath}` : hrefPath;
if (!normalizedHref.startsWith('http')) {
// Non valid URL.
// Example: relative path styles.css.
continue;
}
const url = new URL(normalizedHref);
// Force HTTPS protocol
url.protocol = 'https:';
if (!SUPPORTED_PROVIDERS.includes(url.hostname)) {
// Provider not supported.
continue;
}
// The order IE -> Chrome is important as otherwise Chrome will load woff1.
let cssContent = '';
if (this.options.WOFFSupportNeeded) {
cssContent += await this.getResponse(url, UserAgent.IE);
}
cssContent += await this.getResponse(url, UserAgent.Chrome);
if (this.options.minify) {
cssContent = cssContent
// Comments.
.replace(/\/\*([\s\S]*?)\*\//g, '')
// New lines.
.replace(/\n/g, '')
// Safe spaces.
.replace(/\s?[\{\:\;]\s+/g, s => s.trim());
}
hrefsContent.set(hrefPath, cssContent);
}
return hrefsContent;
}
}