All files / src connection.js

94.12% Statements 16/17
100% Branches 0/0
83.33% Functions 5/6
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44  1x 1x 1x           9x 9x 9x         5x 5x 5x               6x 36x   6x 6x               6x       1x  
const
SocketDispatcher  = require('./dispatcher'),
SocketEmitter     = require('./emitter'),
SocketContext     = require('./context')
 
class SocketConnection
{
  constructor(log)
  {
    this.log        = log
    this.dispatcher = new SocketDispatcher(log)
    this.emitter    = new SocketEmitter(log)
  }
 
  onConnection(socket)
  {
    this.logSocketEvents(socket, this.log)
    const context = new SocketContext(socket, this.emitter)
    this.attachDataEventToDispatcherWithAContext(socket, this.dispatcher, context)
  }
 
  /**
   * @protected
   */
  logSocketEvents(socket, log)
  {
    for(const event of ['close','connection','drain','end','lookup','timeout'])
      socket.on(event, () => log.info('connection:', event))
 
    for(const event of ['error'])
      socket.on(event, (...a) => log.info('connection:', event, ...a))
  }
 
  /**
   * @protected
   */
  attachDataEventToDispatcherWithAContext(socket, dispatcher, context)
  {
    socket.on('data', dispatcher.dispatch.bind(dispatcher, context))
  }
}
 
module.exports = SocketConnection