Comparing version 0.4.3 to 0.4.4
@@ -70,5 +70,9 @@ const bl = require('bl') | ||
console.log('closing?') | ||
if (this._msgInFlight !== 0) | ||
return | ||
console.log('closing!') | ||
this._sock.close() | ||
@@ -148,5 +152,5 @@ this._sock = null | ||
response = new ObserveStream(packet, rsinfo) | ||
response.on('close', this._cleanUp.bind(this)) | ||
response.on('close', function() { | ||
delete that._tkToReq[packet.token.readUInt32BE(0)] | ||
that._cleanUp() | ||
}) | ||
@@ -153,0 +157,0 @@ } else |
@@ -28,2 +28,3 @@ | ||
this.push(null) | ||
this.emit('close') | ||
} | ||
@@ -30,0 +31,0 @@ |
{ | ||
"name": "coap", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "A CoAP library for node modelled after 'http'", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,7 +26,10 @@ | ||
function doReq() { | ||
function doReq(confirmable) { | ||
if (!confirmable) | ||
confirmable = false | ||
return request({ | ||
port: port | ||
, agent: agent | ||
, confirmable: false | ||
, confirmable: confirmable | ||
}).end() | ||
@@ -43,3 +46,3 @@ } | ||
if (firstRsinfo) { | ||
expect(rsinfo).to.eql(firstRsinfo); | ||
expect(rsinfo.port).to.eql(firstRsinfo.port); | ||
done() | ||
@@ -61,3 +64,2 @@ } else { | ||
server.on('message', function(msg, rsinfo) { | ||
console.log(parse(msg)) | ||
if (total === 2) { | ||
@@ -137,3 +139,3 @@ // nothing to do | ||
it('should discard the request after receiving the payload', function(done) { | ||
it('should discard the request after receiving the payload for NON requests', function(done) { | ||
var req = doReq() | ||
@@ -155,2 +157,3 @@ | ||
server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address) | ||
// duplicate, as there was some retransmission | ||
@@ -165,2 +168,167 @@ server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address) | ||
}) | ||
it('should discard the request after receiving the payload for piggyback CON requests', function(done) { | ||
var req = doReq(true) | ||
// it is needed to keep the agent open | ||
doReq(true) | ||
server.once('message', function(msg, rsinfo) { | ||
var packet = parse(msg) | ||
, toSend = generate({ | ||
messageId: packet.messageId | ||
, token: packet.token | ||
, code: '2.00' | ||
, confirmable: false | ||
, ack: true | ||
, payload: new Buffer(5) | ||
}) | ||
server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address) | ||
// duplicate, as there was some retransmission | ||
server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address) | ||
}) | ||
req.on('response', function(res) { | ||
// fails if it emits 'response' twice | ||
done() | ||
}) | ||
}) | ||
it('should close the socket if there are no pending requests', function(done) { | ||
var firstRsinfo | ||
, req = doReq() | ||
server.on('message', function(msg, rsinfo) { | ||
var packet = parse(msg) | ||
, toSend = generate({ | ||
messageId: packet.messageId | ||
, token: packet.token | ||
, code: '2.00' | ||
, confirmable: false | ||
, ack: true | ||
, payload: new Buffer(5) | ||
}) | ||
server.send(toSend, 0, toSend.length, rsinfo.port, rsinfo.address) | ||
if (firstRsinfo) { | ||
expect(rsinfo.port).not.to.eql(firstRsinfo.port); | ||
done() | ||
} else { | ||
firstRsinfo = rsinfo | ||
} | ||
}) | ||
req.on('response', function(res) { | ||
setImmediate(doReq) | ||
}) | ||
}) | ||
describe('observe problems', function() { | ||
function sendObserve(opts) { | ||
var toSend = generate({ | ||
messageId: opts.messageId | ||
, token: opts.token | ||
, code: '2.05' | ||
, confirmable: opts.confirmable | ||
, ack: opts.ack | ||
, payload: new Buffer(5) | ||
, options: [{ | ||
name: 'Observe' | ||
, value: new Buffer([opts.num]) | ||
}] | ||
}) | ||
server.send(toSend, 0, toSend.length, opts.rsinfo.port, opts.rsinfo.address) | ||
} | ||
it('should discard the request after receiving the payload for piggyback CON requests with observe request', function(done) { | ||
var req = request({ | ||
port: port | ||
, agent: agent | ||
, observe: true | ||
, confirmable: true | ||
}).end() | ||
server.once('message', function(msg, rsinfo) { | ||
var packet = parse(msg) | ||
sendObserve({ | ||
num: 1 | ||
, messageId: packet.messageId | ||
, token: packet.token | ||
, confirmable: false | ||
, ack: true | ||
, rsinfo: rsinfo | ||
}) | ||
// duplicate, as there was some retransmission | ||
sendObserve({ | ||
num: 1 | ||
, messageId: packet.messageId | ||
, token: packet.token | ||
, confirmable: false | ||
, ack: true | ||
, rsinfo: rsinfo | ||
}) | ||
// some more data | ||
sendObserve({ | ||
num: 2 | ||
, token: packet.token | ||
, confirmable: true | ||
, ack: false | ||
, rsinfo: rsinfo | ||
}) | ||
}) | ||
req.on('response', function(res) { | ||
// fails if it emits 'response' twice | ||
done() | ||
}) | ||
}) | ||
it('should close the socket if there are no pending requests', function(done) { | ||
var firstRsinfo | ||
, req = request({ | ||
port: port | ||
, agent: agent | ||
, observe: true | ||
, confirmable: true | ||
}).end() | ||
server.once('message', function(msg, rsinfo) { | ||
var packet = parse(msg) | ||
sendObserve({ | ||
num: 1 | ||
, messageId: packet.messageId | ||
, token: packet.token | ||
, confirmable: false | ||
, ack: true | ||
, rsinfo: rsinfo | ||
}) | ||
}) | ||
server.on('message', function(msg, rsinfo) { | ||
if (firstRsinfo) { | ||
expect(rsinfo.port).not.to.eql(firstRsinfo.port); | ||
done() | ||
} else { | ||
firstRsinfo = rsinfo | ||
} | ||
}) | ||
req.on('response', function(res) { | ||
res.close() | ||
console.log(agent._requests) | ||
setImmediate(doReq) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -678,2 +678,22 @@ | ||
it('should retry with the same message id', function(done) { | ||
var req = doReq() | ||
, messageId | ||
server.on('message', function(msg) { | ||
var packet = parse(msg) | ||
if (!messageId) | ||
messageId = packet.messageId | ||
expect(packet.messageId).to.eql(messageId) | ||
}) | ||
req.on('error', function(err) { | ||
done() | ||
}) | ||
fastForward(100, 247 * 1000) | ||
}) | ||
it('should retry four times before 45s', function(done) { | ||
@@ -765,13 +785,24 @@ var req = doReq() | ||
it('should emit data elements as they are send by the server', function(done) { | ||
it('should ack the update', function(done) { | ||
var req = doObserve() | ||
server.on('message', function(msg) { | ||
if (parse(msg).ack) | ||
done() | ||
}) | ||
}) | ||
it('should emit any more data after close', function(done) { | ||
var req = doObserve() | ||
req.on('response', function(res) { | ||
res.once('data', function(data) { | ||
expect(data.toString()).to.eql('42') | ||
res.close() | ||
done() | ||
res.once('data', function(data) { | ||
expect(data.toString()).to.eql('24') | ||
done() | ||
res.on('data', function(data) { | ||
done(new Error('this should never happen')) | ||
}) | ||
@@ -778,0 +809,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
97785
2628