import { RequestHandlerContext } from 'src/core/server' ; import { CoreContext } from '../core_context' ; import { IRouter , RouteConfig , InternalHttpServiceSetup , KibanaRequest , KibanaResponseFactory , InternalHttpServicePreboot , } from '../http' ; import { Logger } from '../logging' ; import { InternalRenderingServicePreboot , InternalRenderingServiceSetup } from '../rendering' ; import { CoreService } from '../../types' ; import { InternalHttpResourcesSetup , HttpResources , HttpResourcesResponseOptions , HttpResourcesRenderOptions , HttpResourcesRequestHandler , HttpResourcesServiceToolkit , } from './types' ; import { getApmConfig } from './get_apm_config' ; export interface PrebootDeps { http : InternalHttpServicePreboot ; rendering : InternalRenderingServicePreboot ; } export interface SetupDeps { http : InternalHttpServiceSetup ; rendering : InternalRenderingServiceSetup ; } export class HttpResourcesService implements CoreService < InternalHttpResourcesSetup > { private readonly logger : Logger ; constructor ( core ) { this . logger = core . logger . get ( 'http-resources' ) ; } preboot ( deps ) { this . logger . debug ( 'prebooting HttpResourcesService' ) ; return { createRegistrar : this . createRegistrar . bind ( this , deps ) , } ; } setup ( deps ) { this . logger . debug ( 'setting up HttpResourcesService' ) ; return { createRegistrar : this . createRegistrar . bind ( this , deps ) , } ; } start ( ) { } stop ( ) { } private createRegistrar ( deps , router ) { return { register : < P , Q , B , Context extends RequestHandlerContext = RequestHandlerContext > ( route , handler ) => { return router . get < P , Q , B > ( route , ( context , request , response ) => { return handler ( context as Context , request , { ... response , ... this . createResponseToolkit ( deps , context , request , response ) , } ) ; } ) ; } , } ; } private createResponseToolkit ( deps , context , request , response ) { const cspHeader = deps . http . csp . header ; return { async renderCoreApp ( options = { } ) { const apmConfig = getApmConfig ( request . url . pathname ) ; const body = await deps . rendering . render ( request , context . core . uiSettings . client , { includeUserSettings : true , vars : { apmConfig , } , } ) ; return response . ok ( { body , headers : { ... options . headers , 'content-security-policy' : cspHeader } , } ) ; } , async renderAnonymousCoreApp ( options = { } ) { const apmConfig = getApmConfig ( request . url . pathname ) ; const body = await deps . rendering . render ( request , context . core . uiSettings . client , { includeUserSettings : false , vars : { apmConfig , } , } ) ; return response . ok ( { body , headers : { ... options . headers , 'content-security-policy' : cspHeader } , } ) ; } , renderHtml ( options ) { return response . ok ( { body : options . body , headers : { ... options . headers , 'content-type' : 'text/html' , 'content-security-policy' : cspHeader , } , } ) ; } , renderJs ( options ) { return response . ok ( { body : options . body , headers : { ... options . headers , 'content-type' : 'text/javascript' , 'content-security-policy' : cspHeader , } , } ) ; } , } ; } }