diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..f19deff --- /dev/null +++ b/index.d.ts @@ -0,0 +1,22 @@ +import { EventEmitter } from "events"; +import { BaseMQTTSNPacket, MQTTPacket } 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..0e408f1 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": "./index.d.ts", "scripts": { "test": "tape test.js | faucet" }, diff --git a/packet.d.ts b/packet.d.ts new file mode 100644 index 0000000..01b6075 --- /dev/null +++ b/packet.d.ts @@ -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 extends BaseMQTTSNPacket { + cmd: "publish"; + dup: boolean; + qos: QOS; + topicIdType: TT; + topicId: TID; + msgId: number; + payload: Buffer; +} + +type PublishShortTopic = Publish; +type PublishPredefined = Publish; +type PublishRegisteredTopic = Publish; + +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; +type UnsubscribeRegisteredTopic = Unsubscribe; +type UnsubscribeShortTopic = Unsubscribe; + +interface Subscribe extends Unsubscribe { + cmd: "subscribe"; + dup: boolean; + qos: QOS; +} + +declare type SubscribePredefined = Subscribe; +declare type SubscribeRegisteredTopic = Subscribe; +declare type SubscribeShortTopic = Subscribe; + +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; \ No newline at end of file