-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractFontsProvider.ts
138 lines (110 loc) · 4.4 KB
/
AbstractFontsProvider.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
import fs from 'fs';
import request from 'sync-request';
import FontsProviderException from '../Exceptions/FontsProviderException';
import { FontFace, FontFaceProvider, FontFaceSrc, FontFaceSrcMeta, HandlerOptions } from '../interfaces';
import FontFaceParser from '../Parsers/FontFaceParser';
import makeFontFaceSrcFileName from '../utils/makeFontFaceSrcFileName';
export default abstract class AbstractFontsProvider {
protected readonly fontFaceProvider: FontFaceProvider;
protected readonly options: HandlerOptions;
protected readonly fontDir: string;
protected readonly fontPath: string;
protected constructor(fontFaceProvider: FontFaceProvider, options: HandlerOptions = {}) {
this.fontFaceProvider = fontFaceProvider;
this.options = options;
this.fontDir = this.fontFaceProvider.fontDir || this.options?.fontDir || '';
this.fontPath = this.fontFaceProvider.fontPath || this.options?.fontPath || '';
if (!this.fontDir.trim().length) {
throw FontsProviderException.undefinedFontDir(this.fontFaceProvider);
}
if (!this.fontPath.trim().length) {
throw FontsProviderException.undefinedFontPath(this.fontFaceProvider);
}
}
protected abstract canHandle(fontFaceProvider: FontFaceProvider): boolean;
protected abstract handle(fontFaceProvider: FontFaceProvider, options: HandlerOptions): FontFace[];
protected go(): FontFace[] {
const css = this.getCss();
const fontFaces = this.parseCss(css);
return fontFaces.map((fontFace) => this.handleFontFace(fontFace));
}
protected getCss(): string {
return this.request(this.fontFaceProvider.url) as string;
}
protected parseCss(css: string): FontFace[] {
return FontFaceParser.parse(css);
}
protected handleFontFace(fontFace: FontFace): FontFace {
const sources = (fontFace.src as FontFaceSrc[]).map((src) => {
const meta = this.saveFontFile(src, fontFace);
return {
...src,
url: `${this.fontPath}/${meta.fileName}`,
};
});
return {
...fontFace,
src: sources,
};
}
protected saveFontFile(src: FontFaceSrc, fontFace: FontFace): FontFaceSrcMeta {
const meta = this.getFontFaceSrcMeta(src, fontFace);
this.fetchFontFile(meta);
return meta;
}
protected getFontFaceSrcMeta(src: FontFaceSrc, fontFace: FontFace): FontFaceSrcMeta {
let url = src.url;
try {
new URL(src.url);
} catch (error) {
// The url is probably relative to the origin.
const origin = new URL(this.fontFaceProvider.url).origin;
url = `${origin}//${url.replace(/^(\/)+/g, '')}`;
}
return {
url,
// format: src.format,
fileName: makeFontFaceSrcFileName(src, fontFace),
// urlHash: hash(src.url),
// extension: determineFontFaceSrcExtension(src),
// fontFamily: fontFace.fontFamily,
// fontWeight: fontFace.fontWeight,
// fontStyle: fontFace.fontStyle,
};
}
protected fetchFontFile(meta: FontFaceSrcMeta) {
const path = `${this.fontDir}/${meta.fileName}`;
if (fs.existsSync(path)) {
return;
}
if (!fs.existsSync(this.fontDir)) {
fs.mkdirSync(this.fontDir, { recursive: true });
}
fs.writeFileSync(path, this.request(meta.url, false));
}
protected request(url: string, utf8Encoded: boolean = true): string | Buffer {
try {
return request('GET', url, this.requestOptions()).getBody((utf8Encoded ? 'utf8' : null) as string);
} catch (error) {
throw FontsProviderException.failedRequest(url, error);
}
}
protected requestOptions() {
return {
maxRedirects: 5,
timeout: 5000,
retry: true,
maxRetries: 3,
retryDelay: 1,
...(this.fontFaceProvider.request || {}),
headers: this.requestHeaders(),
};
}
protected requestHeaders() {
return {
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
...(this.fontFaceProvider.request?.headers || {}),
};
}
}