Skip to content

Commit

Permalink
feat: implement verify topic
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Jun 17, 2023
1 parent 32c55b1 commit 3bd79ca
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/simple-mq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type { Buffer } from 'buffer';
import { EventEmitter } from 'events';
import { Server, Client } from './';

const TOPIC_PART_LIMIT = 16;

export interface Options {
authenticate?: (user?: string, pass?: Buffer) => Promise<boolean> | boolean;
retryInterval?: number;
Expand Down Expand Up @@ -47,6 +49,14 @@ function matchRule(rule: string, topic: string) {
return ruleParts.length === topicParts.length;
}

const verifyTopic = (topic: string) => {
if (/^[^#]*(?:\/?#|\/?[^#]*)?$/.test(topic)) {
return topic.split('/').length <= TOPIC_PART_LIMIT;
} else {
return false;
}
};

export class SimpleMQBroker extends EventEmitter {
protected server: Server;
protected clients: { [id: string]: Client };
Expand Down Expand Up @@ -172,10 +182,14 @@ export class SimpleMQBroker extends EventEmitter {
});
client.on('subscribe', (packet: ISubscribePacket) => {
if (!session) return;
const granted: QoS[] = [];
const granted: number[] = [];
for (const sub of packet.subscriptions) {
session!.subs[sub.topic] = sub.qos;
granted.push(sub.qos);
if (!verifyTopic(sub.topic)) {
granted.push(128);
} else {
session!.subs[sub.topic] = sub.qos;
granted.push(sub.qos);
}
}
client.suback({
messageId: packet.messageId,
Expand Down

0 comments on commit 3bd79ca

Please sign in to comment.