import { CreateRequestHandlerOptions } from '../../interfaces' ; import { PostGraphileResponse } from './frameworks' ; internal export default function setupServerSentEvents ( res , options ) { const req = res . getNodeServerRequest ( ) ; const { _emitter , watchPg } = options ; req . socket . setTimeout ( 0 ) ; req . socket . setNoDelay ( true ) ; req . socket . setKeepAlive ( true ) ; res . statusCode = 200 ; res . setHeader ( 'X-Accel-Buffering' , 'no' ) ; res . setHeader ( 'Content-Type' , 'text/event-stream' ) ; res . setHeader ( 'Cache-Control' , 'no-cache, no-transform' ) ; if ( req . httpVersionMajor >= 2 ) { } else { res . setHeader ( 'Connection' , 'keep-alive' ) ; } const stream = res . endWithStream ( ) ; stream . write ( 'event: open\n\n' ) ; const schemaChangedCb = ( ) => stream . write ( 'event: change\ndata: schema\n\n' ) ; if ( watchPg ) _emitter . on ( 'schemas:changed' , schemaChangedCb ) ; const cleanup = ( ) => { req . removeListener ( 'close' , cleanup ) ; req . removeListener ( 'finish' , cleanup ) ; req . removeListener ( 'error' , cleanup ) ; _emitter . removeListener ( 'test:close' , cleanup ) ; _emitter . removeListener ( 'schemas:changed' , schemaChangedCb ) ; stream . end ( ) ; } ; req . on ( 'close' , cleanup ) ; req . on ( 'finish' , cleanup ) ; req . on ( 'error' , cleanup ) ; _emitter . on ( 'test:close' , cleanup ) ; }