Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: preUnsubscribe handler #605

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const defaultOptions = {
authorizePublish: defaultAuthorizePublish,
authorizeSubscribe: defaultAuthorizeSubscribe,
authorizeForward: defaultAuthorizeForward,
preUnsubscribe: defaultPreUnsubscribe,
published: defaultPublished,
trustProxy: false,
trustedProxies: [],
Expand Down Expand Up @@ -69,6 +70,7 @@ function Aedes (opts) {
this.authorizePublish = opts.authorizePublish
this.authorizeSubscribe = opts.authorizeSubscribe
this.authorizeForward = opts.authorizeForward
this.preUnsubscribe = opts.preUnsubscribe
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a defaultPreSubscribe function and add it to defaultOptions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, good point. Thanks, done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should that allow some async stuff like the others do? maybe adding a callbackk to it?

this.published = opts.published

this.decodeProtocol = opts.decodeProtocol
Expand Down Expand Up @@ -337,6 +339,10 @@ function defaultAuthorizeForward (client, packet) {
return packet
}

function defaultPreUnsubscribe (client, packet, callback) {
callback(client, packet)
}

function defaultPublished (packet, client, callback) {
callback(null)
}
Expand Down
27 changes: 27 additions & 0 deletions docs/Aedes.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- [Handler: authorizePublish (client, packet, callback)](#handler-authorizepublish-client-packet-callback)
- [Handler: authorizeSubscribe (client, subscription, callback)](#handler-authorizesubscribe-client-subscription-callback)
- [Handler: authorizeForward (client, packet)](#handler-authorizeforward-client-packet)
- [Handler: preUnsubscribe (client, packet, callback)](#handler-preunsubscribe-client-packet-callback)
- [Handler: published (packet, client, callback)](#handler-published-packet-client-callback)

## new Aedes([options]) / new Aedes.Server([options])
Expand Down Expand Up @@ -386,6 +387,31 @@ aedes.authorizeForward = function (client, packet) {
}
```

## Handler: preUnsubscribe (client, packet, callback)

- client: [`<Client>`](./Client.md)
- packet: `<aedes-packet>` & [`UNSUBSCRIBE`][UNSUBSCRIBE]
- callback: `<Function>`

Invoked when a client unsubscribes from topics. Just like
[`authorizeSubscribe`](#handler-authorizesubscribe-client-subscription-callback), this function allows modifying the topics before executing the unsubscribe. This may be needed in cases where topics were modified in `authorizeSubscribe`. For example:

```js
aedes.authorizeSubscribe = (client, subscription, callback) => {
// overwrite subscription: force client to its namespace
subscription.topic = `/${client.id}/${subscription.topic}`;
callback(null, subscription);
}

aedes.preUnsubscribe = (client, packet, callback) => {
// overwrite unsubscriptions: force client to its namespace
for (let i in packet.unsubscriptions) {
packet.unsubscriptions[i] = `/${client.id}/${packet.unsubscriptions[i]}`;
}
callback(packet, client)
}
```

## Handler: published (packet, client, callback)

- packet: `<aedes-packet>` & [`PUBLISH`][PUBLISH]
Expand All @@ -397,6 +423,7 @@ same as [`Event: publish`](#event-publish), but provides a backpressure function
[CONNECT]: https://github.com/mqttjs/mqtt-packet#connect
[CONNACK]: https://github.com/mqttjs/mqtt-packet#connack
[SUBSCRIBE]: https://github.com/mqttjs/mqtt-packet#subscribe
[UNSUBSCRIBE]: https://github.com/mqttjs/mqtt-packet#unsubscribe
[PINGREQ]: https://github.com/mqttjs/mqtt-packet#pingreq
[PUBLISH]: https://github.com/mqttjs/mqtt-packet#publish
[PUBREL]: https://github.com/mqttjs/mqtt-packet#pubrel
8 changes: 7 additions & 1 deletion lib/handlers/unsubscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ function UnsubscribeState (client, packet, finish) {
this.finish = finish
}

function preUnsubscribe (client, packet, done) {
client.broker.preUnsubscribe(client, packet, (c, p) =>
handleUnsubscribe(c, p, done))
}

function handleUnsubscribe (client, packet, done) {
const broker = client.broker

const unsubscriptions = packet.unsubscriptions
let err

Expand Down Expand Up @@ -101,4 +107,4 @@ function completeUnsubscribe (err) {

function noop () {}

module.exports = handleUnsubscribe
module.exports = preUnsubscribe