lib/client-stream.js
/**
 * client.middleware(req, res) Serve the client with a long-lived EventStream
 * client.reload(err) Send a "reload" or "build-error" event to a connected client
 * client.close() Prepare for shutdown by clearing any timers
 */
client.middleware(req, res) Serve the client with a long-lived EventStream
client.reload(err) Send a "reload" or "build-error" event to a connected client
client.close() Prepare for shutdown by clearing any timers
client . middleware ( req res ) Serve the client with a long - lived EventStream client . reload ( err ) Send a " reload " or " build - error " event to a connected client client . close () Prepare for shutdown by clearing any timers
function createEventStream() {
  const pingTimer = setInterval(ping, PING_INTERVAL)
  let liveSocket = null
  function middleware(_, res) {
    liveSocket = res
    liveSocket.writeHead(200, {
      'Transfer-Encoding': 'identity',
      'Content-Type': 'text/event-stream'
    })
    liveSocket.write('retry:' + RETRY_INTERVAL + '\n')
    liveSocket.on('close', onSocketClose)
  }
  function onSocketClose(){ liveSocket = null }
  function reload(err, files) {
    if (err) emit('build-error', err)
    else emit('reload', JSON.stringify(files))
  }
  function emit(name, payload) {
    if (liveSocket) liveSocket.write(`event: ${name}\ndata: ${payload}\n\n`)
  }
  function ping() {
    if (liveSocket) liveSocket.write(':ping\n')
  }
  function close() {
    clearInterval(pingTimer)
  }
  return {middleware, reload, close, emit}
}
function createEventStream ( ) { const pingTimer = setInterval ( ping , PING_INTERVAL ) let liveSocket = null function middleware ( _ , res ) { liveSocket = res liveSocket . writeHead ( 200 , { 'Transfer-Encoding' : 'identity' , 'Content-Type' : 'text/event-stream' } ) liveSocket . write ( 'retry:' + RETRY_INTERVAL + '\n' ) liveSocket . on ( 'close' , onSocketClose ) } function onSocketClose ( ) { liveSocket = null } function reload ( err , files ) { if ( err ) emit ( 'build-error' , err ) else emit ( 'reload' , JSON . stringify ( files ) ) } function emit ( name , payload ) { if ( liveSocket ) liveSocket . write ( ` ${ name } \n ${ payload } \n \n ` ) } function ping ( ) { if ( liveSocket ) liveSocket . write ( ':ping\n' ) } function close ( ) { clearInterval ( pingTimer ) } return { middleware , reload , close , emit } }
client.middleware(req, res) Serve the client with a long-lived EventStream
 client.reload(err) Send a "reload" or "build-error" event to a connected client
 client.close() Prepare for shutdown by clearing any timers
client . middleware ( req res ) Serve the client with a long - lived EventStream client . reload ( err ) Send a " reload " or " build - error " event to a connected client client . close () Prepare for shutdown by clearing any timers
/**
   * Chunked transfer-encoding disabled: http://www.w3.org/TR/2009/WD-eventsource-20091029/#notes
   * @param {http.IncomingMessage} req
   * @param {http.ServerResponse} res
   */ /** Tell the client to reload */ /** Use this periodically to keep the socket open */