# Code coverage report for lib/spdy-transport/window.js

# all files / lib/spdy-transport/ window.js

97.37% Statements 74/76
96.15% Branches 25/26
92.31% Functions 12/13
97.33% Lines 73/75

    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
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175

    1×
    1×
    1×

    1×
    3434×

    3434×
    3434×
    3434×
    3434×
    3434×
    3434×

    3434×
    3434×

    1×

    1×
    900×

    900×
    900×

    1×
    193×
    193×

    193×
    193×

    193×

    1×

    1×

    12549×
    1061×

    1061×

    1061×

    11488×

    11488×
    4×
    4×

    11484×

    11484×
    4769×
    4769×

    11484×
    4189×
    4189×

    11484×

    11484×

    1×
    3108×

    1×
    2086×

    1×
    2315×

    1×
    8519×

    1×
    3997×

    1×

    11484×
    1057×

    10427×

    10427×
    1057×

    1057×

    1057×

    1057×
    1057×

    10427×

    1×
    1717×
    1717×
    1717×

    1717×
    1717×

    1×

    1×
    513×

     'use strict'

    var util = require('util')
    var EventEmitter = require('events').EventEmitter
    var debug = {
      server: require('debug')('spdy:window:server'),
      client: require('debug')('spdy:window:client')
    }

    function Side (window, name, options) {
      EventEmitter.call(this)

      this.name = name
      this.window = window
      this.current = options.size
      this.max = options.size
      this.limit = options.max
      this.lowWaterMark = options.lowWaterMark === undefined
        ? this.max / 2
        : options.lowWaterMark

      this._refilling = false
      this._refillQueue = []
    }
    util.inherits(Side, EventEmitter)

    Side.prototype.setMax = function setMax (max) {
      this.window.debug('id=%d side=%s setMax=%d',
                        this.window.id,
                        this.name,
                        max)
      this.max = max
      this.lowWaterMark = this.max / 2
    }

    Side.prototype.updateMax = function updateMax (max) {
      var delta = max - this.max
      this.window.debug('id=%d side=%s updateMax=%d delta=%d',
                        this.window.id,
                        this.name,
                        max,
                        delta)

      this.max = max
      this.lowWaterMark = max / 2

      this.update(delta)
    }

    Side.prototype.setLowWaterMark = function setLowWaterMark (lwm) {
      this.lowWaterMark = lwm
    }

    Side.prototype.update = function update (size, callback) {
      // Not enough space for the update, wait for refill
      if (size <= 0 && callback && this.isEmpty()) {
        this.window.debug('id=%d side=%s wait for refill=%d [%d/%d]',
                          this.window.id,
                          this.name,
                          -size,
                          this.current,
                          this.max)
        this._refillQueue.push({
          size: size,
          callback: callback
        })
        return
      }

      this.current += size

      if (this.current > this.limit) {
        this.emit('overflow')
        return
      }

      this.window.debug('id=%d side=%s update by=%d [%d/%d]',
                        this.window.id,
                        this.name,
                        size,
                        this.current,
                        this.max)

      // Time to send WINDOW_UPDATE
      if (size < 0 && this.isDraining()) {
        this.window.debug('id=%d side=%s drained', this.window.id, this.name)
        this.emit('drain')
      }

      // Time to write
      if (size > 0 && this.current > 0 && this.current <= size) {
        this.window.debug('id=%d side=%s full', this.window.id, this.name)
        this.emit('full')
      }

      this._processRefillQueue()

      if (callback) { process.nextTick(callback) }
    }

    Side.prototype.getCurrent = function getCurrent () {
      return this.current
    }

    Side.prototype.getMax = function getMax () {
      return this.max
    }

    Side.prototype.getDelta = function getDelta () {
      return this.max - this.current
    }

    Side.prototype.isDraining = function isDraining () {
      return this.current <= this.lowWaterMark
    }

    Side.prototype.isEmpty = function isEmpty () {
      return this.current <= 0
    }

    // Private

    Side.prototype._processRefillQueue = function _processRefillQueue () {
      // Prevent recursion
      if (this._refilling) {
        return
      }
      this._refilling = true

      while (this._refillQueue.length > 0) {
        var item = this._refillQueue[0]

        Iif (this.isEmpty()) {
          break
        }

        this.window.debug('id=%d side=%s refilled for size=%d',
                          this.window.id,
                          this.name,
                          -item.size)

        this._refillQueue.shift()
        this.update(item.size, item.callback)
      }

      this._refilling = false
    }

    function Window (options) {
      this.id = options.id
      this.isServer = options.isServer
      this.debug = this.isServer ? debug.server : debug.client

      this.recv = new Side(this, 'recv', options.recv)
      this.send = new Side(this, 'send', options.send)
    }
    module.exports = Window

    Window.prototype.clone = function clone (id) {
      return new Window({
        id: id,
        isServer: this.isServer,
        recv: {
          size: this.recv.max,
          max: this.recv.limit,
          lowWaterMark: this.recv.lowWaterMark
        },
        send: {
          size: this.send.max,
          max: this.send.limit,
          lowWaterMark: this.send.lowWaterMark
        }
      })
    }