diff --git a/mqttsn.d.ts b/mqttsn.d.ts new file mode 100644 index 0000000..7a51d86 --- /dev/null +++ b/mqttsn.d.ts @@ -0,0 +1,22 @@ +import { EventEmitter } from "events"; +import { BaseMQTTSNPacket, MQTTSNPacket } from "./packet"; + +type OnPacketFn = (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

(packet: P): Buffer; diff --git a/package.json b/package.json index 7aa6dc4..d32e4b3 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.0.2", "description": "Parse and generate MQTT-SN packets", "main": "mqttsn.js", + "typings": "./mqttsn.d.ts", "scripts": { "test": "tape test.js | faucet" }, @@ -30,10 +31,12 @@ }, "homepage": "https://github.com/ithinuel/mqttsn-packet", "devDependencies": { + "@types/node": "^14.14.22", "faucet": "0.0.1", "pre-commit": "^1.1.2", "readable-stream": "^2.0.4", - "tape": "^4.2.2" + "tape": "^4.2.2", + "typescript": "^4.1.3" }, "dependencies": { "bl": "^1.0.0" diff --git a/packet.d.ts b/packet.d.ts new file mode 100644 index 0000000..896a835 --- /dev/null +++ b/packet.d.ts @@ -0,0 +1,198 @@ +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 BaseMQTTSNPacket { + cmd: "willmsgupd"; + willMsg: string; +} + +interface Register extends BaseMQTTSNPacket { + cmd: "register"; + topicId: number; + msgId: number; + topicName: string; +} + +interface RegAck extends GenericAck { + cmd: "regack"; + topicId: number; + msgId: number; +} + +interface Publish extends BaseMQTTSNPacket { + cmd: "publish"; + dup: boolean; + qos: QOS; + topicIdType: TT; + topicId: TID; + msgId: number; + payload: Buffer; +} + +type PublishShortTopic = Publish<"short topic", string>; +type PublishPredefined = Publish<"pre-defined", number>; +type PublishRegisteredTopic = Publish<"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 extends BaseMQTTSNPacket { + cmd: "unsubscribe"; + msgId: number; + topicIdType: TT; + topicName: TN; + topicId: TID; +} + +type UnsubscribePredefined = Unsubscribe<"pre-defined", undefined, number>; +type UnsubscribeRegisteredTopic = Unsubscribe<"normal", undefined, number>; +type UnsubscribeShortTopic = Unsubscribe<"short topic", string, undefined>; + +interface Subscribe extends BaseMQTTSNPacket { + cmd: "subscribe"; + msgId: number; + topicIdType: TT; + topicName: TN; + topicId: TID; + dup: boolean; + qos: QOS; +} + +declare type SubscribePredefined = Subscribe<"pre-defined", undefined, number>; +declare type SubscribeRegisteredTopic = Subscribe<"normal", undefined, number>; +declare type SubscribeShortTopic = Subscribe<"short topic", 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 MQTTSNPacket = + | 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; \ No newline at end of file