6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
+ import * as cacache from 'cacache' ;
10
+ import { readFile } from 'fs' ;
9
11
import * as https from 'https' ;
12
+ import { promisify } from 'util' ;
13
+ import { findCachePath } from '../cache-path' ;
14
+ import { cachingDisabled } from '../environment-options' ;
10
15
import { htmlRewritingStream } from './html-rewriting-stream' ;
11
16
17
+ const cacheFontsPath = cachingDisabled ? undefined : findCachePath ( 'angular-build-fonts' ) ;
18
+ const packageVersion = require ( '../../../package.json' ) . version ;
19
+
12
20
const enum UserAgent {
13
21
Chrome = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko)' ,
14
22
IE = 'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko' ,
@@ -25,8 +33,6 @@ export interface InlineFontsOptions {
25
33
}
26
34
27
35
export class InlineFontsProcessor {
28
- private readonly ResponseCache = new Map < string , string > ( ) ;
29
-
30
36
async process ( options : InlineFontsOptions ) : Promise < string > {
31
37
const {
32
38
content,
@@ -89,16 +95,17 @@ export class InlineFontsProcessor {
89
95
}
90
96
91
97
private async getResponse ( url : string , userAgent : UserAgent ) : Promise < string > {
92
- const key = url + userAgent ;
98
+ const key = ` ${ packageVersion } | ${ url } | ${ userAgent } ` ;
93
99
94
- if ( this . ResponseCache . has ( key ) ) {
95
- // tslint:disable-next-line: no-non-null-assertion
96
- return this . ResponseCache . get ( key ) ! ;
100
+ if ( cacheFontsPath ) {
101
+ const entry = await cacache . get . info ( cacheFontsPath , key ) ;
102
+ if ( entry ) {
103
+ return promisify ( readFile ) ( entry . path , 'utf8' ) ;
104
+ }
97
105
}
98
106
99
- return new Promise ( ( resolve , reject ) => {
107
+ const data = await new Promise < string > ( ( resolve , reject ) => {
100
108
let rawResponse = '' ;
101
-
102
109
https . get (
103
110
url ,
104
111
{
@@ -109,15 +116,17 @@ export class InlineFontsProcessor {
109
116
res => {
110
117
res
111
118
. on ( 'data' , chunk => rawResponse += chunk )
112
- . on ( 'end' , ( ) => {
113
- const response = rawResponse . toString ( ) ;
114
- this . ResponseCache . set ( key , response ) ;
115
- resolve ( response ) ;
116
- } ) ;
119
+ . on ( 'end' , ( ) => resolve ( rawResponse . toString ( ) ) ) ;
117
120
} ,
118
121
)
119
122
. on ( 'error' , e => reject ( e ) ) ;
120
123
} ) ;
124
+
125
+ if ( cacheFontsPath ) {
126
+ await cacache . put ( cacheFontsPath , key , data ) ;
127
+ }
128
+
129
+ return data ;
121
130
}
122
131
123
132
private async processHrefs ( hrefList : string [ ] , minifyInlinedCSS : boolean , WOFF1SupportNeeded : boolean ) : Promise < Map < string , string > > {
0 commit comments