Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
})
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() })
})
})
server.listen(port, function() {
send(generate())
server.on('request', function(req, res) {
done()
})
})
})
server.on('message', function(msg, rsinfo) {
var packet = parse(msg)
, toSend = generate({
messageId: packet.messageId
, token: packet.token
, payload: new Buffer('42')
, ack: true
, code: '2.00'
})
server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)
expect(parse(msg).payload.toString()).to.eql('hello world')
done()
})
}
function ssend(rsinfo, packet) {
var toSend = generate(packet)
server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)
}
server.once('message', function (msg, rsinfo) {
var packet = parse(msg)
, toSend = generate({
token: packet.token
, messageId: packet.messageId
, payload: new Buffer('42')
, confirmable: true
, code: '2.00'
})
server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)
toSend = generate({
token: packet.token
, messageId: packet.messageId
, payload: new Buffer('42')
, confirmable: true
, code: '2.00'
})
server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address)
})
response = new Message(packet, function(response, packet) {
var buf
, sender = new RetrySend(sock, rsinfo.port, rsinfo.address)
try {
buf = generate(packet)
} catch(err) {
return response.emit('error', err)
}
if (Message === OutMessage) {
sender.on('error', response.emit.bind(response, 'error'))
} else {
buf.response = response
sender.on('error', function() {
response.end()
})
}
var key = toKey(rsinfo.address, rsinfo.port,
packet, packet.ack || !packet.confirmable)
lru.set(key, buf)
CoAPServer.prototype._sendReverseProxied = function(packet, rsinfo, callback) {
var host = rsinfo.address
, port = rsinfo.port
, message = generate(packet)
this._sock.send(message, 0, message.length, port, host, callback)
}