@ -17,7 +17,7 @@ import {
DOCUMENT
} from '@angular/core' ;
import { forkJoin , from , Observable , ReplaySubject , throwError } from 'rxjs' ;
import { forkJoin , from , Observable , of , ReplaySubject , throwError } from 'rxjs' ;
import { HttpClient } from '@angular/common/http' ;
import { IModulesMap } from '@modules/common/modules-map.models' ;
import { TbResourceId } from '@shared/models/id/tb-resource-id' ;
@ -26,7 +26,7 @@ import { AuthService } from '@core/auth/auth.service';
import { select , Store } from '@ngrx/store' ;
import { selectIsAuthenticated } from '@core/auth/auth.selectors' ;
import { AppState } from '@core/core.state' ;
import { map , tap } from 'rxjs/operators' ;
import { catchError , map , switchM ap , tap } from 'rxjs/operators' ;
import { RequestConfig } from '@core/http/http-utils' ;
import { isJSResource , removeTbResourcePrefix } from '@shared/models/resource.models' ;
@ -96,6 +96,22 @@ const resourceFileType = (url: string, baseUrl: string): string => {
return /^(css|js)\d*$/ . exec ( segment ) ? . [ 1 ] ;
} ;
// The file type of a resource served under a URL that does not tell it, as Google Fonts' /icon, from the Content-Type
// of a HEAD response. Works only for same-origin URLs and servers that allow cross-origin requests.
const resourceFileTypeFromContentType = ( url : string ) : Observable < string > = >
from ( fetch ( url , { method : 'HEAD' } ) ) . pipe (
map ( response = > {
const mimeType = response . headers . get ( 'content-type' ) ? . split ( ';' ) [ 0 ] . trim ( ) . toLowerCase ( ) ;
if ( mimeType === 'text/css' ) {
return 'css' ;
} else if ( /^(text|application)\/(x-)?(javascript|ecmascript)$/ . test ( mimeType ) ) {
return 'js' ;
}
return undefined ;
} ) ,
catchError ( ( ) = > of ( undefined ) )
) ;
const matchesSelector = ( selectors : ɵCssSelectorList , selector : string ) = >
selectors . some ( s = > s . some ( s1 = > typeof s1 === 'string' && s1 === selector ) ) ;
@ -160,12 +176,17 @@ export class ResourcesService {
}
const fileType = resourceFileType ( url , this . document . baseURI ) ;
if ( ! fileType ) {
return throwError ( ( ) = > new Error ( ` Unable to detect file type from url: ${ url } ` ) ) ;
} else if ( fileType !== 'css' && fileType !== 'js' ) {
return throwError ( ( ) = > new Error ( ` Unsupported file type: ${ fileType } ` ) ) ;
}
return this . loadResourceByType ( fileType , url ) ;
const fileType $ = fileType ? of ( fileType ) : resourceFileTypeFromContentType ( url ) ;
return fileType $ . pipe (
switchMap ( type = > {
if ( ! type ) {
return throwError ( ( ) = > new Error ( ` Unable to detect file type from url: ${ url } ` ) ) ;
} else if ( type !== 'css' && type !== 'js' ) {
return throwError ( ( ) = > new Error ( ` Unsupported file type: ${ type } ` ) ) ;
}
return this . loadedResources [ url ] ? this . loadedResources [ url ] . asObservable ( ) : this . loadResourceByType ( type , url ) ;
} )
) ;
}
public downloadResource ( downloadUrl : string , config? : RequestConfig ) : Observable < any > {