66 * found in the LICENSE file at https://angular.io/license
77 */
88
9+ import * as cacache from 'cacache' ;
10+ import { readFile } from 'fs' ;
911import * as https from 'https' ;
12+ import { promisify } from 'util' ;
13+ import { findCachePath } from '../cache-path' ;
14+ import { cachingDisabled } from '../environment-options' ;
1015import { htmlRewritingStream } from './html-rewriting-stream' ;
1116
17+ const cacheFontsPath = cachingDisabled ? undefined : findCachePath ( 'angular-build-fonts' ) ;
18+ const packageVersion = require ( '../../../package.json' ) . version ;
19+
1220const enum UserAgent {
1321 Chrome = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko)' ,
1422 IE = 'Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko' ,
@@ -25,8 +33,6 @@ export interface InlineFontsOptions {
2533}
2634
2735export class InlineFontsProcessor {
28- private readonly ResponseCache = new Map < string , string > ( ) ;
29-
3036 async process ( options : InlineFontsOptions ) : Promise < string > {
3137 const {
3238 content,
@@ -89,16 +95,17 @@ export class InlineFontsProcessor {
8995 }
9096
9197 private async getResponse ( url : string , userAgent : UserAgent ) : Promise < string > {
92- const key = url + userAgent ;
98+ const key = ` ${ packageVersion } | ${ url } | ${ userAgent } ` ;
9399
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+ }
97105 }
98106
99- return new Promise ( ( resolve , reject ) => {
107+ const data = await new Promise < string > ( ( resolve , reject ) => {
100108 let rawResponse = '' ;
101-
102109 https . get (
103110 url ,
104111 {
@@ -109,15 +116,17 @@ export class InlineFontsProcessor {
109116 res => {
110117 res
111118 . 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 ( ) ) ) ;
117120 } ,
118121 )
119122 . on ( 'error' , e => reject ( e ) ) ;
120123 } ) ;
124+
125+ if ( cacheFontsPath ) {
126+ await cacache . put ( cacheFontsPath , key , data ) ;
127+ }
128+
129+ return data ;
121130 }
122131
123132 private async processHrefs ( hrefList : string [ ] , minifyInlinedCSS : boolean , WOFF1SupportNeeded : boolean ) : Promise < Map < string , string > > {
0 commit comments