All files / node-coap/lib observe_read_stream.js

96.67% Statements 29/30
91.67% Branches 11/12
100% Functions 4/4
96.67% Lines 29/30

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                    1x 1x 1x     21x   21x 21x   21x 21x 21x 21x     1x   1x 44x     44x     44x 21x     44x 44x   44x 36x 36x 36x       1x 5x 5x 5x 1x         1x   1x  
'use strict'
 
/*
 * Copyright (c) 2013-2021 node-coap contributors.
 *
 * node-coap is licensed under an MIT +no-false-attribs license.
 * All rights not explicitly granted in the MIT license are reserved.
 * See the included LICENSE file for more details.
 */
 
var Readable = require('readable-stream').Readable
  , util     = require('util')
  , pktToMsg = require('./helpers').packetToMessage
 
function ObserveReadStream(packet, rsinfo, outSocket) {
  Readable.call(this, { objectMode: true })
 
  this.rsinfo = rsinfo
  this.outSocket = outSocket
 
  this._lastId = undefined
  this._lastTime = 0
  this._disableFiltering = false
  this.append(packet)
}
 
util.inherits(ObserveReadStream, Readable)
 
ObserveReadStream.prototype.append = function(packet) {
  Iif (!this.readable)
    return
 
  pktToMsg(this, packet)
 
  // First notification
  if (this._lastId === undefined) {
    this._lastId = this.headers['Observe'] - 1
  }
 
  const dseq = (this.headers['Observe'] - this._lastId) & 0xffffff
  const dtime = Date.now() - this._lastTime
 
  if (this._disableFiltering || (dseq > 0 && dseq < (1 << 23)) || dtime > 128*1000) {
    this._lastId = this.headers['Observe']
    this._lastTime = Date.now()
    this.push(packet.payload)
  }
}
 
ObserveReadStream.prototype.close = function(eagerDeregister) {
  this.push(null)
  this.emit('close')
  if (eagerDeregister) {
    this.emit('deregister')
  }
}
 
// nothing to do, data will be pushed from the server
ObserveReadStream.prototype._read = function() {}
 
module.exports = ObserveReadStream