@seriousme/opifex
Version:
MQTT client & server for Deno & NodeJS
79 lines • 2.54 kB
TypeScript
/**
* @module mqttConn
*/
import { type AnyPacket } from "../mqttPacket/mod.ts";
import type { SockConn } from "../socket/socket.ts";
import { Conn } from "../socket/socket.ts";
/**
* Common MQTT connection error messages
*/
export declare const MqttConnError: {
readonly invalidPacket: "Invalid Packet";
readonly packetTooLarge: "Packet too large";
readonly UnexpectedEof: "Unexpected EOF";
};
/**
* Interface for MQTT connection handling
*/
export interface IMqttConn extends AsyncIterable<AnyPacket> {
/** Underlying connection */
readonly conn: Conn;
/** Whether connection is closed */
readonly isClosed: boolean;
/** Reason for connection closure if any */
readonly reason: string | undefined;
/** Async iterator for receiving packets */
[Symbol.asyncIterator](): AsyncIterableIterator<AnyPacket>;
/** Send an MQTT packet */
send(data: AnyPacket): Promise<void>;
/** Close the connection */
close(): void;
}
/**
* Read a complete MQTT packet from the connection
* @param conn Connection to read from
* @param maxPacketSize Maximum allowed packet size
* @returns Decoded MQTT packet
* @throws Error if packet is invalid or too large
*/
export declare function readPacket(conn: Conn, maxPacketSize: number): Promise<AnyPacket>;
/**
* MQTT Connection class implementing IMqttConn interface
*/
export declare class MqttConn implements IMqttConn {
/** Underlying connection */
readonly conn: Conn;
/** Maximum allowed packet size */
private readonly maxPacketSize;
/** Reason for connection closure if any */
private _reason;
/** Whether connection is closed */
private _isClosed;
/**
* Create new MQTT connection
* @param options Connection options
* @param options.conn Underlying socket connection
* @param options.maxPacketSize Maximum allowed packet size (default 2MB)
*/
constructor({ conn, maxPacketSize, }: {
conn: SockConn;
maxPacketSize?: number;
});
/** Get reason for connection closure */
get reason(): string | undefined;
/**
* Async iterator for receiving packets
* @yields MQTT packets
*/
[Symbol.asyncIterator](): AsyncIterableIterator<AnyPacket>;
/**
* Send an MQTT packet
* @param data Packet to send
*/
send(data: AnyPacket): Promise<void>;
/** Whether connection is closed */
get isClosed(): boolean;
/** Close the connection */
close(): void;
}
//# sourceMappingURL=mqttConn.d.ts.map