Skip to content

Commit

Permalink
Merge pull request #330 from JKRhb/fix-observe
Browse files Browse the repository at this point in the history
fix: fix problem with (boolean) observe parameters
  • Loading branch information
Apollon77 authored Apr 16, 2022
2 parents da1de84 + eb21df7 commit 67c12da
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
14 changes: 9 additions & 5 deletions lib/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ class Agent extends EventEmitter {
}
}

const observe = req.url.observe != null && [true, 0, '0'].includes(req.url.observe)

if (req.response != null) {
const response: any = req.response
if (response.append != null) {
Expand All @@ -332,12 +334,12 @@ class Agent extends EventEmitter {
}
} else if (block2 != null && packet.token != null) {
this._tkToReq.delete(packet.token.toString('hex'))
} else if (req.url.observe !== true && !req.multicast) {
} else if (!observe && !req.multicast) {
// it is not, so delete the token
this._tkToReq.delete(packet.token.toString('hex'))
}

if (req.url.observe === true && packet.code !== '4.04') {
if (observe && packet.code !== '4.04') {
response = new ObserveStream(packet, rsinfo, outSocket)
response.on('close', () => {
this._tkToReq.delete(packet.token.toString('hex'))
Expand Down Expand Up @@ -512,10 +514,12 @@ class Agent extends EventEmitter {

if (typeof (url.observe) === 'number') {
req.setOption('Observe', url.observe)
} else if (url.observe == null) {
req.on('response', this._cleanUp.bind(this))
} else if (url.observe) {
} else if (typeof (url.observe) === 'string') {
req.setOption('Observe', parseInt(url.observe))
} else if (url.observe === true || url.observe != null) {
req.setOption('Observe', 0)
} else {
req.on('response', this._cleanUp.bind(this))
}

this._requests++
Expand Down
2 changes: 1 addition & 1 deletion models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export interface CoapRequestParams {
port?: number
method?: CoapMethod
confirmable?: boolean
observe?: 0 | 1 | boolean
observe?: 0 | 1 | boolean | string
pathname?: string
query?: string
options?: Partial<Record<OptionName, OptionValue>>
Expand Down
53 changes: 53 additions & 0 deletions test/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { expect } from 'chai'
import { createSocket, Socket } from 'dgram'
import OutgoingMessage from '../lib/outgoing_message'
import { AddressInfo } from 'net'
import ObserveReadStream from '../lib/observe_read_stream'

describe('Agent config', function () {
it('should get agent instance through custom config', function (done) {
Expand Down Expand Up @@ -485,5 +486,57 @@ describe('Agent', function () {
setImmediate(doReq)
})
})

it('should allow observe with non-confirmable requests', function (done) {
const req = request({
port: port,
agent: agent,
observe: true,
confirmable: false
}).end()

let counter = 0

server.on('message', (msg, rsinfo) => {
const packet = parse(msg)

sendObserve({
num: 1,
messageId: packet.messageId,
token: packet.token,
confirmable: false,
ack: false,
rsinfo: rsinfo
})

// duplicate, as there was some retransmission
sendObserve({
num: 1,
messageId: packet.messageId,
token: packet.token,
confirmable: false,
ack: false,
rsinfo: rsinfo
})

// some more data
sendObserve({
num: 2,
token: packet.token,
confirmable: false,
ack: false,
rsinfo: rsinfo
})
})

req.on('response', (res: ObserveReadStream) => {
res.on('data', (chunk) => {
counter++
if (counter >= 2) {
done()
}
})
})
})
})
})

0 comments on commit 67c12da

Please sign in to comment.