Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var checkBlock2Option = function(msg, payload_req1, req1_block2_num, payload_len) {
var res = parse(msg)
// Have block2 option?
var block2Buff = getOption(res.options, 'Block2')
expect(block2Buff instanceof Buffer).to.eql(true)
var block2 = parseBlock2(block2Buff)
expect(block2).to.not.eql(null)
var expectMore = (req1_block2_num + 1) * 16 <= payload_len
// Have correct num / moreBlock2 fields?
expect(block2.num).to.eql(req1_block2_num)
expect(block2.moreBlock2).to.eql(expectMore)
}
delete this._msgIdToReq[req._packet.messageId]
req._packet.messageId = that._nextMessageId()
this._msgIdToReq[req._packet.messageId] = req
// next block2 request
var block2Val = createBlock2({
moreBlock2: false,
num: block2.num+1,
size: block2.size
})
if (!block2Val) {
req.sender.reset()
return req.emit('error', new Error('failed to create block2'))
}
req.setOption('Block2', block2Val)
req.sender.send(generate(req._packet))
return
}
else {
// get full payload
packet.payload = req._totalPayload
// clear the payload incase of block2
req._totalPayload = new Buffer(0)
}
}
if (req.response) {
if (req.response.append) {
// it is an observe request
// and we are already streaming
return req.response.append(packet)
packet.confirmable = true
}
// multicast message should be forced non-confirmable
if (url.multicast === true) {
req.multicast = true
packet.confirmable = false
}
if (!(packet.ack || packet.reset)) {
packet.messageId = that._nextMessageId()
packet.token = that._nextToken()
}
try {
buf = generate(packet)
} catch(err) {
req.sender.reset()
return req.emit('error', err)
}
that._msgIdToReq[packet.messageId] = req
that._tkToReq[that._lastToken] = req
req.sender.send(buf, !packet.confirmable)
})
this._sock.on('message', function(msg, rsinfo) {
var packet
, message
, outSocket
try {
packet = parse(msg)
} catch(err) {
return
}
if (packet.code[0] === '0' && packet.code !== '0.00') {
// ignore this packet since it's not a response.
return
}
outSocket = that._sock.address();
that._handle(msg, rsinfo, outSocket)
})
it('should reply in piggyback', function(done) {
send(generate(packet))
server.on('request', function(req, res) {
res.end('42')
})
client.on('message', function(msg) {
var response = parse(msg)
expect(response.ack).to.be.true
expect(response.messageId).to.eql(packet.messageId)
expect(response.payload).to.eql(new Buffer('42'))
done()
})
})
server.listen(sock, function() {
expect(server._sock).to.eql(sock)
sock.emit('message', generate(), { address: '127.0.0.1', port: nextPort() })
})
})
client.on('message', function(msg, rinfo) {
var result = parse(msg)
expect(result.code).to.eql('0.00')
expect(result.reset).to.eql(true)
expect(result.ack).to.eql(false)
expect(result.token.length).to.eql(0)
expect(result.payload.length).to.eql(0)
done()
});
server.on('request', function(req, res) {
client.on('message', function(msg) {
var res = parse(msg)
send(generate({
code: '0.00'
, messageId: res.messageId
, ack: true }))
messages++
})
})
client.on('message', function(msg) {
expect(parse(msg).options[0].value).to.eql(buf1)
expect(parse(msg).options[1].value).to.eql(buf2)
done()
})
})
server.on('message', function (msg, rsinfo) {
var packet = parse(msg)
, toSend = generate({
messageId: packet.messageId
, code: '0.00'
, ack: true
, payload: 'this payload invalidates empty message'
})
expect(packet.code).to.be.eq('0.01');
messages++
server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)
})