From 68d14e2cabf2a01c4d6d2ac19ac661205eeb875e Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Fri, 21 Jul 2017 10:25:24 +0200 Subject: [PATCH] Do not forward $SYS topics for +/# subscriptions. Fixes: https://github.com/mcollina/aedes/issues/135 --- aedes.js | 6 +++++- lib/handlers/subscribe.js | 9 ++++++++- test/events.js | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/aedes.js b/aedes.js index 4e6bf1a1..133e351f 100644 --- a/aedes.js +++ b/aedes.js @@ -200,8 +200,12 @@ function DoEnqueues () { } } +// + is 43 +// # is 35 function removeSharp (sub) { - return sub.topic !== '#' + var code = sub.topic.charCodeAt(0) + console.log(sub.topic, code) + return code !== 43 && code !== 35 } function doEnqueue (sub, done) { diff --git a/lib/handlers/subscribe.js b/lib/handlers/subscribe.js index ece4cb0f..e0848805 100644 --- a/lib/handlers/subscribe.js +++ b/lib/handlers/subscribe.js @@ -100,7 +100,7 @@ function subTopic (sub, done) { break } - if (sub.topic === '#') { + if (isWildcardThatMatchesSys(sub.topic)) { func = blockSys(func) } @@ -118,6 +118,13 @@ function subTopic (sub, done) { } } +// + is 43 +// # is 35 +function isWildcardThatMatchesSys (topic) { + var code = topic.charCodeAt(0) + return code === 43 || code === 35 +} + function completeSubscribe (err) { var packet = this.packet var client = this.client diff --git a/test/events.js b/test/events.js index 0c4286d8..25df8ea3 100644 --- a/test/events.js +++ b/test/events.js @@ -40,6 +40,25 @@ test('does not forward $SYS topics to # subscription', function (t) { }) }) +test('does not forward $SYS topics to +/# subscription', function (t) { + t.plan(4) + var s = connect(setup()) + + subscribe(t, s, '+/#', 0, function () { + s.outStream.once('data', function (packet) { + t.fail('no packet should be received') + }) + + s.broker.mq.emit({ + cmd: 'publish', + topic: '$SYS/hello', + payload: 'world' + }, function () { + t.pass('nothing happened') + }) + }) +}) + test('does not store $SYS topics to QoS 1 # subscription', function (t) { t.plan(3)