-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Artur Wolny
committed
Jan 1, 2021
1 parent
4162c10
commit b6735a6
Showing
3 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { EventEmitter } from "events"; | ||
import { BaseMQTTSNPacket, MQTTPacket } from "./packet"; | ||
|
||
type OnPacketFn = <T extends MQTTPacket = MQTTPacket>(packet: T) => void; | ||
|
||
interface Parser extends EventEmitter { | ||
parse(buf: Buffer): number; | ||
|
||
on(event: "packet", listener: OnPacketFn): this; | ||
off(event: "packet", listener: OnPacketFn): this; | ||
once(event: "packet", listener: OnPacketFn): this; | ||
addListener(event: "packet", listener: OnPacketFn): this; | ||
prependListener(event: "packet", listener: OnPacketFn): this; | ||
prependOnceListener(event: "packet", listener: OnPacketFn): this; | ||
listeners(event: "packet"): Function[]; | ||
rawListeners(event: "packet"): Function[]; | ||
} | ||
|
||
export var parser: { | ||
new(opts?: { isClient?: boolean }): Parser; | ||
}; | ||
export function generate<P extends BaseMQTTSNPacket = BaseMQTTSNPacket>(packet: P): Buffer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
type ReturnCodeString = | ||
| "Accepted" | ||
| "Rejected: congestion" | ||
| "Rejected: invalid topic ID" | ||
| "Rejected: not supported"; | ||
|
||
type QOS = number; | ||
|
||
type TopicType = | ||
| "normal" | ||
| "pre-defined" | ||
| "short topic"; | ||
|
||
interface BaseMQTTSNPacket { | ||
cmd: "advertise" | "searchgw" | "gwinfo" | "connect" | "connack" | "willtopicresp" | "willmsgresp" | "willtopicreq" | ||
| "willmsgreq" | "pingresp" | "willtopic" | "willtopicupd" | "willmsg" | "willmsgupd" | "register" | "regack" | ||
| "publish" | "puback" | "pubcomp" | "pubrec" | "pubrel" | "unsuback" | "unsubscribe" | "subscribe" | "suback" | ||
| "pingreq" | "disconnect"; | ||
|
||
length?: number; | ||
} | ||
|
||
interface GenericAck extends BaseMQTTSNPacket { | ||
returnCode: ReturnCodeString | null; | ||
} | ||
|
||
interface Advertise extends BaseMQTTSNPacket { | ||
cmd: "advertise"; | ||
gwId: number; | ||
duration: number; | ||
} | ||
|
||
interface SearchGw extends BaseMQTTSNPacket { | ||
cmd: "searchgw"; | ||
radius: number; | ||
} | ||
|
||
interface GwInfo extends BaseMQTTSNPacket { | ||
cmd: "gwinfo"; | ||
gwId: number; | ||
gwAdd?: Buffer; | ||
} | ||
|
||
interface Connect extends BaseMQTTSNPacket { | ||
cmd: "connect"; | ||
duration: number; | ||
clientId: string; | ||
will: boolean; | ||
cleanSession: boolean; | ||
} | ||
|
||
interface ConnAck extends GenericAck { | ||
cmd: "connack"; | ||
} | ||
|
||
interface WillTopic extends BaseMQTTSNPacket { | ||
cmd: "willtopic"; | ||
qos?: QOS; | ||
retain?: boolean; | ||
willTopic?: string; | ||
} | ||
|
||
interface WillTopicResp extends GenericAck { | ||
cmd: "willtopicresp"; | ||
} | ||
|
||
interface WillTopicUpd extends BaseMQTTSNPacket { | ||
cmd: "willtopicupd"; | ||
qos?: QOS; | ||
retain?: boolean; | ||
willTopic?: string; | ||
} | ||
|
||
interface WillMsg extends BaseMQTTSNPacket { | ||
cmd: "willmsg"; | ||
willMsg: string; | ||
} | ||
|
||
interface WillMsgResp extends GenericAck { | ||
cmd: "willmsgresp"; | ||
} | ||
|
||
interface WillMsgUpd extends WillMsg { | ||
cmd: "willmsgupd"; | ||
} | ||
|
||
interface Register extends BaseMQTTSNPacket { | ||
cmd: "register"; | ||
topicId: number; | ||
msgId: number; | ||
topicName: string; | ||
} | ||
|
||
interface RegAck extends GenericAck { | ||
cmd: "regack"; | ||
topicId: number; | ||
msgId: number; | ||
} | ||
|
||
interface Publish<TT extends TopicType, TID> extends BaseMQTTSNPacket { | ||
cmd: "publish"; | ||
dup: boolean; | ||
qos: QOS; | ||
topicIdType: TT; | ||
topicId: TID; | ||
msgId: number; | ||
payload: Buffer; | ||
} | ||
|
||
type PublishShortTopic = Publish<TopicType.SHORT, string>; | ||
type PublishPredefined = Publish<TopicType.PREDEFINED, number>; | ||
type PublishRegisteredTopic = Publish<TopicType.NORMAL, number>; | ||
|
||
interface PubAck extends GenericAck { | ||
cmd: "puback"; | ||
topicId: number; | ||
msgId: number; | ||
} | ||
|
||
interface PubComp extends BaseMQTTSNPacket { | ||
cmd: "pubcomp"; | ||
msgId: number; | ||
} | ||
|
||
interface PubRec extends BaseMQTTSNPacket { | ||
cmd: "pubrec"; | ||
msgId: number; | ||
} | ||
|
||
interface PubRel extends BaseMQTTSNPacket { | ||
cmd: "pubrel"; | ||
msgId: number; | ||
} | ||
|
||
interface UnsubAck extends BaseMQTTSNPacket { | ||
cmd: "unsuback"; | ||
msgId: number; | ||
} | ||
|
||
interface Unsubscribe<TT extends TopicType, TN, TID> extends BaseMQTTSNPacket { | ||
cmd: "unsubscribe"; | ||
msgId: number; | ||
topicIdType: TT; | ||
topicName: TN; | ||
topicId: TID; | ||
} | ||
|
||
type UnsubscribePredefined = Unsubscribe<TopicType.PREDEFINED, undefined, number>; | ||
type UnsubscribeRegisteredTopic = Unsubscribe<TopicType.NORMAL, undefined, number>; | ||
type UnsubscribeShortTopic = Unsubscribe<TopicType.SHORT, string, undefined>; | ||
|
||
interface Subscribe<TT extends TopicType, TN, TID> extends Unsubscribe<TT, TN, TID> { | ||
cmd: "subscribe"; | ||
dup: boolean; | ||
qos: QOS; | ||
} | ||
|
||
declare type SubscribePredefined = Subscribe<TopicType.PREDEFINED, undefined, number>; | ||
declare type SubscribeRegisteredTopic = Subscribe<TopicType.NORMAL, undefined, number>; | ||
declare type SubscribeShortTopic = Subscribe<TopicType.SHORT, string, undefined>; | ||
|
||
interface SubAck extends GenericAck { | ||
cmd: "suback"; | ||
topicId: number; | ||
msgId: number; | ||
qos: QOS; | ||
} | ||
|
||
interface PingReq extends BaseMQTTSNPacket { | ||
cmd: "pingreq"; | ||
clientId?: string; | ||
} | ||
|
||
interface Disconnect extends BaseMQTTSNPacket { | ||
cmd: "disconnect"; | ||
duration?: number; | ||
} | ||
|
||
|
||
export type MQTTPacket = | ||
| Advertise | SearchGw | GwInfo | ||
| Connect | Disconnect | ConnAck | ||
| WillMsg | WillMsgUpd | WillTopic | WillTopicUpd | WillMsgResp | WillTopicResp | ||
| Register | ||
| RegAck | ||
| PublishPredefined | PublishRegisteredTopic | PublishShortTopic | ||
| PubAck | PubComp | PubRec | PubRel | ||
| SubscribePredefined | SubscribeRegisteredTopic | SubscribeShortTopic | ||
| SubAck | ||
| UnsubscribePredefined | UnsubscribeRegisteredTopic | UnsubscribeShortTopic | ||
| UnsubAck | ||
| PingReq | ||
| BaseMQTTSNPacket; |