Skip to content

Commit

Permalink
refactor: apply last refactorings before 0.26.0 (#309)
Browse files Browse the repository at this point in the history
* refactor: refactor library files

* refactor: refactor tests
  • Loading branch information
JKRhb authored Oct 17, 2021
1 parent 3355e9b commit f35b58a
Show file tree
Hide file tree
Showing 20 changed files with 989 additions and 659 deletions.
163 changes: 91 additions & 72 deletions lib/agent.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion lib/observe_read_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class ObserveReadStream extends IncomingMessage {
packetToMessage(this, packet)
}

if (typeof this.headers.Observe !== 'number') {
return
}

// First notification
if (this._lastId === undefined) {
this._lastId = this.headers.Observe - 1
Expand All @@ -48,7 +52,7 @@ class ObserveReadStream extends IncomingMessage {
close (eagerDeregister) {
this.push(null)
this.emit('close')
if (eagerDeregister) {
if (eagerDeregister === true) {
this.emit('deregister')
}
}
Expand Down
5 changes: 2 additions & 3 deletions lib/observe_write_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ class ObserveWriteStream extends Writable {

this._request = request
this._send = send
this.statusCode = ''

this._counter = 0

this.on('finish', () => {
if (this._counter === 0) { // we have sent no messages
this._doSend(null)
this._doSend()
}
})
}
Expand All @@ -56,7 +55,7 @@ class ObserveWriteStream extends Writable {
this._send(this, packet)

this._packet.confirmable = this._request.confirmable
this._packet.ack = !this._request.confirmable
this._packet.ack = this._request.confirmable === false
delete this._packet.messageId
delete this._packet.payload
}
Expand Down
17 changes: 12 additions & 5 deletions lib/option_converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ignoreOption('Content-Length')
ignoreOption('Accept-Ranges')

module.exports.isIgnored = (name) => {
return !!ignoredOptions[name]
return ignoredOptions[name] != null
}

// ETag option registration
Expand Down Expand Up @@ -99,6 +99,12 @@ registerOption('Max-Age', fromUint, toUint)
const formatsString = {}
const formatsBinaries = {}

/**
* Registers a new Content-Format.
*
* @param name Media-Type and parameters.
* @param value The numeric code of the Content-Format.
*/
const registerFormat = (name, value) => {
let bytes

Expand Down Expand Up @@ -187,18 +193,19 @@ function contentFormatToString (value) {
return formatsBinaries[0]
}

let numericValue
if (value.length === 1) {
value = value.readUInt8(0)
numericValue = value.readUInt8(0)
} else if (value.length === 2) {
value = value.readUInt16BE(0)
numericValue = value.readUInt16BE(0)
} else {
return null
}

const result = formatsBinaries[value]
const result = formatsBinaries[numericValue]

if (result == null) {
return value
return numericValue
}

return result
Expand Down
16 changes: 7 additions & 9 deletions lib/outgoing_message.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class OutgoingMessage extends BufferList {
reset: false
}

if (request.confirmable) {
if (request.confirmable === true) {
// replying in piggyback
this._packet.ack = true

Expand Down Expand Up @@ -54,10 +54,11 @@ class OutgoingMessage extends BufferList {

const packet = this._packet

packet.code = toCode(this.code || this.statusCode)
const code = this.code !== '' ? this.code : this.statusCode
packet.code = toCode(code)
packet.payload = this

if (this._ackTimer) {
if (this._ackTimer != null) {
clearTimeout(this._ackTimer)
}

Expand All @@ -80,7 +81,7 @@ class OutgoingMessage extends BufferList {
packet.ack = false
packet.token = Buffer.alloc(0)

if (this._ackTimer) {
if (this._ackTimer != null) {
clearTimeout(this._ackTimer)
}

Expand All @@ -94,12 +95,9 @@ class OutgoingMessage extends BufferList {

writeHead (code, headers) {
const packet = this._packet
let header
packet.code = String(code).replace(/(^\d[^.])/, '$1.')
for (header in headers) {
if (Object.prototype.hasOwnProperty.call(headers, header)) {
this.setOption(header, headers[header])
}
for (const [header, value] of Object.entries(headers)) {
this.setOption(header, value)
}
}

Expand Down
22 changes: 14 additions & 8 deletions lib/retry_send.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
* See the included LICENSE file for more details.
*/

const parameters = require('./parameters').parameters
const EventEmitter = require('events').EventEmitter
const parse = require('coap-packet').parse
const { EventEmitter } = require('events')
const { parse } = require('coap-packet')
const { parameters } = require('./parameters')

class RetrySendError extends Error {
constructor (retransmitTimeout) {
super(`No reply in ${retransmitTimeout} seconds.`)
this.retransmitTimeout = retransmitTimeout
}
}

class RetrySend extends EventEmitter {
constructor (sock, port, host, maxRetransmit) {
Expand Down Expand Up @@ -48,7 +55,7 @@ class RetrySend extends EventEmitter {
this._sendAttemp = 0
}

if (!avoidBackoff && ++this._sendAttemp <= this._maxRetransmit) {
if (avoidBackoff !== true && ++this._sendAttemp <= this._maxRetransmit) {
this._bOffTimer = setTimeout(this._bOff, this._currentTime)
}

Expand All @@ -59,11 +66,10 @@ class RetrySend extends EventEmitter {
this._message = message
this._send(avoidBackoff)

const timeout = avoidBackoff ? parameters.maxRTT : parameters.exchangeLifetime
const timeout = avoidBackoff === true ? parameters.maxRTT : parameters.exchangeLifetime
this._timer = setTimeout(() => {
const err = new Error('No reply in ' + timeout + 's')
err.retransmitTimeout = timeout
if (!avoidBackoff) {
const err = new RetrySendError(timeout)
if (avoidBackoff === false) {
this.emit('error', err)
}
this.emit('timeout', err)
Expand Down
12 changes: 6 additions & 6 deletions lib/segmentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class SegmentedTransmission {
this.lastByte = 0

this.req = req
this.payload = packet.payload
this.payload = packet.payload || Buffer.alloc(0)
this.packet = packet

this.packet.payload = null
this.packet.payload = undefined
this.resendCount = 0
}

Expand All @@ -50,7 +50,7 @@ class SegmentedTransmission {
this.req.setOption('Block1', generateBlockOption(this.blockState))
}

isCorrectACK (packet, retBlockState) {
isCorrectACK (retBlockState) {
return retBlockState.num === this.blockState.num// && packet.code == "2.31"
}

Expand All @@ -68,11 +68,10 @@ class SegmentedTransmission {

/**
*
* @param {Packet} packet The packet received which contained the ack
* @param {Object} retBlockState The received block state from the other end
* @returns {Boolean} Returns true if the ACK was for the correct block.
*/
receiveACK (packet, retBlockState) {
receiveACK (retBlockState) {
if (this.blockState.size !== retBlockState.size) {
this.setBlockSizeExp(retBlockState.size)
}
Expand Down Expand Up @@ -106,7 +105,8 @@ class SegmentedTransmission {
buf = generate(this.packet)
} catch (err) {
this.req.sender.reset()
return this.req.emit('error', err)
this.req.emit('error', err)
return
}
this.req.sender.send(buf, !this.packet.confirmable)
}
Expand Down
Loading

0 comments on commit f35b58a

Please sign in to comment.