Skip to content

Commit

Permalink
Downgrade qos to client sub if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinDmello committed Feb 2, 2019
1 parent 003a06b commit ca42eb0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ function Client (broker, conn) {
that.broker.authorizeForward(that, _packet)
if (toForward) {
var packet = new QoSPacket(toForward, that)
// Downgrading to client subscription qos if needed
var clientSub = that.subscriptions[packet.topic]
if (clientSub && clientSub.qos && clientSub.qos < packet.qos) {
packet.qos = clientSub.qos
}
packet.writeCallback = cb
if (that.clean) {
writeQoS(null, that, packet)
Expand Down
33 changes: 33 additions & 0 deletions test/qos2.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,39 @@ test('subscribe QoS 0, but publish QoS 2', function (t) {
})
})

test('subscribe QoS 1, but publish QoS 2', function (t) {
var broker = aedes()
var publisher = connect(setup(broker))
var subscriber = connect(setup(broker))
var expected = {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 1,
dup: false,
length: 14,
retain: false
}

subscribe(t, subscriber, 'hello', 1, function () {
subscriber.outStream.once('data', function (packet) {
delete packet.messageId
t.deepEqual(packet, expected, 'packet must match')
t.end()
})

publish(t, publisher, {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 2,
retain: false,
messageId: 42,
dup: false
})
})
})

test('restore QoS 2 subscriptions not clean', function (t) {
var broker = aedes()
var publisher
Expand Down

0 comments on commit ca42eb0

Please sign in to comment.