diff --git a/package.json b/package.json index 45a73ed..e06afd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aedes-packet", - "version": "2.3.1", + "version": "2.3.2", "description": "Basic data structure for packets in Aedes ", "main": "packet.js", "types": "packet.d.ts", diff --git a/packet.d.ts b/packet.d.ts index a5f5b2d..fe521be 100644 --- a/packet.d.ts +++ b/packet.d.ts @@ -11,6 +11,7 @@ declare namespace aedes { type AedesPacket = IPacket & { brokerId: string brokerCounter: number + ttlInSeconds?: number } function Packet(object?: AedesPacket) : aedes.AedesPacket diff --git a/packet.js b/packet.js index 219b7ca..a53dfd0 100644 --- a/packet.js +++ b/packet.js @@ -9,6 +9,7 @@ function Packet (original, broker) { this.qos = original.qos || 0 this.retain = original.retain || false this.dup = original.dup || false + this.ttlInSeconds = original.properties?.messageExpiryInterval // [MQTT-2.3.1-5] if (this.qos > 0 || this.cmd !== 'publish') { // [MQTT-2.3.1-1] diff --git a/test.js b/test.js index 5899d3e..ba9eaac 100644 --- a/test.js +++ b/test.js @@ -13,6 +13,7 @@ test('Packet defaults - PUBLISH, QoS 0', function (t) { t.equal(instance.qos, 0) t.equal(instance.dup, false) t.equal(instance.retain, false) + t.equal(instance.ttlInSeconds, undefined) t.notOk(Object.prototype.hasOwnProperty.call(instance, 'messageId')) t.end() }) @@ -29,6 +30,7 @@ test('Packet defaults - PUBREL, QoS 0', function (t) { t.equal(instance.retain, false) t.ok(Object.prototype.hasOwnProperty.call(instance, 'messageId')) t.equal(instance.messageId, undefined) + t.equal(instance.ttlInSeconds, undefined) t.end() }) @@ -44,6 +46,7 @@ test('Packet defaults - PUBLISH, QoS 1', function (t) { t.equal(instance.retain, false) t.ok(Object.prototype.hasOwnProperty.call(instance, 'messageId')) t.equal(instance.messageId, undefined) + t.equal(instance.ttlInSeconds, undefined) t.end() }) @@ -58,6 +61,7 @@ test('Packet defaults - PUBLISH, dup=true', function (t) { t.equal(instance.dup, true) t.equal(instance.retain, false) t.equal(instance.messageId, undefined) + t.equal(instance.ttlInSeconds, undefined) t.end() }) @@ -82,7 +86,43 @@ test('Packet copies over most data', function (t) { payload: 'world', qos: 2, dup: true, - retain: true + retain: true, + ttlInSeconds: undefined + } + + t.ok(Object.prototype.hasOwnProperty.call(instance, 'messageId')) + t.equal(instance.messageId, undefined) + delete instance.messageId + t.deepEqual(instance, expected) + t.end() +}) + +test('Packet understands properties.messageExpiryInterval', function (t) { + const original = { + cmd: 'publish', + brokerId: 'A56c', + brokerCounter: 42, + topic: 'hello', + payload: 'world', + qos: 2, + dup: true, + retain: true, + messageId: 24, + properties: { + messageExpiryInterval: 4 + } + } + const instance = new Packet(original) + const expected = { + cmd: 'publish', + brokerId: 'A56c', + brokerCounter: 42, + topic: 'hello', + payload: 'world', + qos: 2, + dup: true, + retain: true, + ttlInSeconds: 4 } t.ok(Object.prototype.hasOwnProperty.call(instance, 'messageId')) @@ -114,7 +154,8 @@ test('Packet fills in broker data', function (t) { payload: 'world', qos: 2, dup: false, - retain: true + retain: true, + ttlInSeconds: undefined } t.ok(Object.prototype.hasOwnProperty.call(instance, 'messageId'))