@seriousme/opifex
Version:
MQTT client & server for Deno & NodeJS
149 lines • 6.07 kB
TypeScript
/**
* @module MQTT Packet Encoding/Decoding
* @description This module provides comprehensive encoding and decoding functionality for MQTT packets
* used in both server and client implementations. It handles all MQTT packet types and their
* transformations between binary and object representations.
*/
import type { ClientId, Dup, PacketId, Payload, QoS, ReturnCodes, TAuthenticationResult, Topic, TopicFilter, TPacketType } from "./types.ts";
import { PacketNameByType, PacketType } from "./PacketType.ts";
import { invalidTopic, invalidTopicFilter, invalidUTF8 } from "./validators.ts";
import { decodeLength, encodeLength } from "./length.ts";
import { type ConnectPacket } from "./connect.ts";
import { type ConnackPacket } from "./connack.ts";
import { AuthenticationResult, AuthenticationResultByNumber } from "./AuthenticationResult.ts";
import { type PublishPacket } from "./publish.ts";
import { type PubackPacket } from "./puback.ts";
import { type PubrecPacket } from "./pubrec.ts";
import { type PubrelPacket } from "./pubrel.ts";
import { type PubcompPacket } from "./pubcomp.ts";
import { type SubscribePacket } from "./subscribe.ts";
import { type SubackPacket } from "./suback.ts";
import { type UnsubscribePacket } from "./unsubscribe.ts";
import { type UnsubackPacket } from "./unsuback.ts";
import { type PingreqPacket } from "./pingreq.ts";
import { type PingresPacket } from "./pingres.ts";
import { type DisconnectPacket } from "./disconnect.ts";
/**
* this can be any possible MQTT packet
*/
export type AnyPacket = ConnectPacket | ConnackPacket | PublishPacket | PubackPacket | PubrecPacket | PubrelPacket | PubcompPacket | SubscribePacket | SubackPacket | UnsubscribePacket | UnsubackPacket | PingreqPacket | PingresPacket | DisconnectPacket;
export type { ClientId, ConnackPacket, ConnectPacket, DisconnectPacket, Dup, PacketId, Payload, PingreqPacket, PingresPacket, PubackPacket, PubcompPacket, PublishPacket, PubrecPacket, PubrelPacket, QoS, ReturnCodes, SubackPacket, SubscribePacket, TAuthenticationResult, Topic, TopicFilter, TPacketType, UnsubackPacket, UnsubscribePacket, };
export type { Subscription } from "./subscribe.ts";
export { AuthenticationResult, AuthenticationResultByNumber, decodeLength, encodeLength, invalidTopic, invalidTopicFilter, invalidUTF8, PacketNameByType, PacketType, };
/**
* Array mapping MQTT packet types to their corresponding encode/decode handlers
* Index corresponds to packet type number.
*/
export declare const packetsByType: readonly [null, {
encode(packet: ConnectPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array, flags: number): ConnectPacket;
}, {
encode(packet: ConnackPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array, _flags: number): ConnackPacket | undefined;
}, {
encode(packet: PublishPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array, flags: number): PublishPacket;
}, {
encode(packet: PubackPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array): PubackPacket;
}, {
encode(packet: PubrecPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array): PubrecPacket;
}, {
encode(packet: PubrelPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array): PubrelPacket;
}, {
encode(packet: PubcompPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array): PubcompPacket;
}, {
encode(packet: SubscribePacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array, flags: number): SubscribePacket;
}, {
encode(packet: SubackPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array): SubackPacket;
}, {
encode(packet: UnsubscribePacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array, flags: number): UnsubscribePacket;
}, {
encode(packet: UnsubackPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array): UnsubackPacket;
}, {
encode(_packet: PingreqPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array, flags: number): PingreqPacket;
}, {
encode(_packet: PingresPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array): PingresPacket;
}, {
encode(_packet: DisconnectPacket): {
flags: number;
bytes: number[];
};
decode(buffer: Uint8Array, _flags: number): DisconnectPacket;
}];
/**
* @function encode
* @description Encodes an MQTT packet object into a binary Uint8Array format
* @param {AnyPacket} packet - The MQTT packet object to encode
* @returns {Uint8Array} The encoded packet as a binary buffer
* @throws {Error} If packet encoding fails
*/
export declare function encode(packet: AnyPacket): Uint8Array;
/**
* @function decodePayload
* @description Decodes a packet payload from binary format into an MQTT packet object
* @param {number} firstByte - The first byte of the MQTT packet containing type and flags
* @param {Uint8Array} buffer - The binary buffer containing the packet payload
* @returns {AnyPacket} The decoded MQTT packet object
* @throws {Error} If packet decoding fails
*/
export declare function decodePayload(firstByte: number, buffer: Uint8Array): AnyPacket;
/**
* @function decode
* @description Decodes a complete MQTT packet from binary format into a packet object
* @param {Uint8Array} buffer - The binary buffer containing the complete MQTT packet
* @returns {AnyPacket} The decoded MQTT packet object
* @throws {DecoderError} If packet decoding fails due to invalid format or insufficient data
*/
export declare function decode(buffer: Uint8Array): AnyPacket;
export { getLengthDecoder } from "../mqttPacket/length.ts";
export type { LengthDecoderResult } from "../mqttPacket/length.ts";
//# sourceMappingURL=mod.d.ts.map