module . exports = function modifyIndexHtmlMiddleware ( compiler ) { return modifyResponse ( ( req , res ) => { const contentType = res . getHeader ( 'Content-Type' ) ; if ( contentType && contentType . startsWith ( 'text/html' ) ) { return true ; } return false ; } , ( req , res , body ) => { res . set ( { 'content-security-policy' : '' , } ) ; const bodyText = body . toString ( ) ; let html = compiler . readLocalIndexHtml ( ) ; html = injectCsrf ( bodyText , html ) ; html = injectBearer ( bodyText , html ) ; return html ; } ) ; } ; function injectCsrf ( source , target ) { var value = source . match ( new RegExp ( /<meta name="grv_csrf_token" content="[a-zA-Z0-9_.-=]*"\/>/ ) ) ; if ( value ) { return target . replace ( new RegExp ( /<meta name="grv_csrf_token" content="{{ \.XCSRF }}"( )?\/>/ ) , value [ 0 ] ) ; } return target ; } function injectBearer ( source , target ) { var value = source . match ( new RegExp ( /<meta name="grv_bearer_token" content="[a-zA-Z0-9_.-=]*"\/>/ ) ) ; if ( value ) { return target . replace ( new RegExp ( /<meta name="grv_bearer_token" content="{{ \.Session }}"( )?\/>/ ) , value [ 0 ] ) ; } return target ; } function modifyResponse ( checkCallback , modifyCallback ) { return function expressModifyResponse ( req , res , next ) { var _end = res . end ; var _write = res . write ; var checked = false ; var buffers = [ ] ; var addBuffer = ( chunk , encoding ) => { if ( chunk === undefined ) return ; if ( typeof chunk === 'string' ) { chunk = new Buffer ( chunk , encoding ) ; } buffers . push ( chunk ) ; } ; var _writeHead = res . writeHead ; res . writeHead = function ( ) { var headers = arguments . length > 2 ? arguments [ 2 ] : arguments [ 1 ] ; var contentType = this . getHeader ( 'content-type' ) ; if ( typeof contentType != 'undefined' && contentType . indexOf ( 'text/html' ) == 0 ) { res . isHtml = true ; res . removeHeader ( 'Content-Length' ) ; if ( headers ) { delete headers [ 'content-length' ] ; } } _writeHead . apply ( res , arguments ) ; } ; res . write = function write ( chunk , encoding ) { if ( ! checked ) { checked = true ; var hook = checkCallback ( req , res ) ; if ( ! hook ) { res . end = _end ; res . write = _write ; return res . write ( chunk , encoding ) ; } else { addBuffer ( chunk , encoding ) ; } } else { addBuffer ( chunk , encoding ) ; } } ; res . end = function end ( chunk , encoding ) { if ( ! checked ) { checked = true ; var hook = checkCallback ( req , res ) ; if ( ! hook ) { res . end = _end ; res . write = _write ; return res . end ( chunk , encoding ) ; } else { addBuffer ( chunk , encoding ) ; } } else { addBuffer ( chunk , encoding ) ; } var buffer = Buffer . concat ( buffers ) ; Promise . resolve ( modifyCallback ( req , res , buffer ) ) . then ( result => { if ( typeof result === 'string' ) { result = new Buffer ( result , 'utf-8' ) ; } if ( res . getHeader ( 'Content-Length' ) ) { res . setHeader ( 'Content-Length' , String ( result . length ) ) ; } res . end = _end ; res . write = _write ; res . write ( result ) ; res . end ( ) ; } ) . catch ( e => { next ( e ) ; } ) ; } ; next ( ) ; } ; }